1 /* $NetBSD: lfs.c,v 1.30 2009/02/22 20:28:05 ad Exp $ */
3 * Copyright (c) 2003 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Konrad E. Schroder <perseant@hhhh.org>.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1989, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
33 * (c) UNIX System Laboratories, Inc.
34 * All or some portions of this file are derived from material licensed
35 * to the University of California by American Telephone and Telegraph
36 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
37 * the permission of UNIX System Laboratories, Inc.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
67 #include <sys/types.h>
68 #include <sys/param.h>
71 #include <sys/mount.h>
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufsmount.h>
76 #include <ufs/lfs/lfs.h>
93 #include "kernelops.h"
95 #define panic call_panic
97 extern u_int32_t
cksum(void *, size_t);
98 extern u_int32_t
lfs_sb_cksum(struct dlfs
*);
99 extern void pwarn(const char *, ...);
101 extern struct uvnodelst vnodelist
;
102 extern struct uvnodelst getvnodelist
[VNODE_HASH_MAX
];
106 lfs_fragextend(struct uvnode
*, int, int, daddr_t
, struct ubuf
**);
109 void (*panic_func
)(int, const char *, va_list) = my_vpanic
;
112 * LFS buffer and uvnode operations
116 lfs_vop_strategy(struct ubuf
* bp
)
120 if (bp
->b_flags
& B_READ
) {
121 count
= kops
.ko_pread(bp
->b_vp
->v_fd
, bp
->b_data
, bp
->b_bcount
,
123 if (count
== bp
->b_bcount
)
124 bp
->b_flags
|= B_DONE
;
126 count
= kops
.ko_pwrite(bp
->b_vp
->v_fd
, bp
->b_data
, bp
->b_bcount
,
132 bp
->b_flags
&= ~B_DELWRI
;
133 reassignbuf(bp
, bp
->b_vp
);
139 lfs_vop_bwrite(struct ubuf
* bp
)
144 if (!(bp
->b_flags
& B_DELWRI
)) {
145 fs
->lfs_avail
-= btofsb(fs
, bp
->b_bcount
);
147 bp
->b_flags
|= B_DELWRI
| B_LOCKED
;
148 reassignbuf(bp
, bp
->b_vp
);
154 * ufs_bmaparray does the bmap conversion, and if requested returns the
155 * array of logical blocks which must be traversed to get to a block.
156 * Each entry contains the offset into that block that gets you to the
157 * next block and the disk address of the block (if it is assigned).
160 ufs_bmaparray(struct lfs
* fs
, struct uvnode
* vp
, daddr_t bn
, daddr_t
* bnp
, struct indir
* ap
, int *nump
)
164 struct indir a
[NIADDR
+ 1], *xap
;
171 if (bn
>= 0 && bn
< NDADDR
) {
174 *bnp
= fsbtodb(fs
, ip
->i_ffs1_db
[bn
]);
179 xap
= ap
== NULL
? a
: ap
;
182 if ((error
= ufs_getlbns(fs
, vp
, bn
, xap
, nump
)) != 0)
187 /* Get disk address out of indirect block array */
188 daddr
= ip
->i_ffs1_ib
[xap
->in_off
];
190 for (bp
= NULL
, ++xap
; --num
; ++xap
) {
191 /* Exit the loop if there is no disk address assigned yet and
192 * the indirect block isn't in the cache, or if we were
193 * looking for an indirect block and we've found it. */
195 metalbn
= xap
->in_lbn
;
196 if ((daddr
== 0 && !incore(vp
, metalbn
)) || metalbn
== bn
)
199 * If we get here, we've either got the block in the cache
200 * or we have a disk address for it, go fetch it.
206 bp
= getblk(vp
, metalbn
, fs
->lfs_bsize
);
208 if (!(bp
->b_flags
& (B_DONE
| B_DELWRI
))) {
209 bp
->b_blkno
= fsbtodb(fs
, daddr
);
210 bp
->b_flags
|= B_READ
;
213 daddr
= ((ufs_daddr_t
*) bp
->b_data
)[xap
->in_off
];
218 daddr
= fsbtodb(fs
, (ufs_daddr_t
) daddr
);
219 *bnp
= daddr
== 0 ? -1 : daddr
;
224 * Create an array of logical block number/offset pairs which represent the
225 * path of indirect blocks required to access a data block. The first "pair"
226 * contains the logical block number of the appropriate single, double or
227 * triple indirect block and the offset into the inode indirect block array.
228 * Note, the logical block number of the inode single/double/triple indirect
229 * block appears twice in the array, once with the offset into the i_ffs1_ib and
230 * once with the offset into the page itself.
233 ufs_getlbns(struct lfs
* fs
, struct uvnode
* vp
, daddr_t bn
, struct indir
* ap
, int *nump
)
235 daddr_t metalbn
, realbn
;
238 int i
, numlevels
, off
;
239 int lognindir
, indir
;
241 metalbn
= 0; /* XXXGCC -Wuninitialized [sh3] */
251 for (indir
= fs
->lfs_nindir
; indir
; indir
>>= 1)
254 /* Determine the number of levels of indirection. After this loop is
255 * done, blockcnt indicates the number of data blocks possible at the
256 * given level of indirection, and NIADDR - i is the number of levels
257 * of indirection needed to locate the requested block. */
260 for (lbc
= 0, i
= NIADDR
;; i
--, bn
-= blockcnt
) {
265 blockcnt
= (int64_t) 1 << lbc
;
271 /* Calculate the address of the first meta-block. */
272 metalbn
= -((realbn
>= 0 ? realbn
: -realbn
) - bn
+ NIADDR
- i
);
274 /* At each iteration, off is the offset into the bap array which is an
275 * array of disk addresses at the current level of indirection. The
276 * logical block number and the offset in that block are stored into
277 * the argument array. */
278 ap
->in_lbn
= metalbn
;
279 ap
->in_off
= off
= NIADDR
- i
;
282 for (++numlevels
; i
<= NIADDR
; i
++) {
283 /* If searching for a meta-data block, quit when found. */
284 if (metalbn
== realbn
)
288 blockcnt
= (int64_t) 1 << lbc
;
289 off
= (bn
>> lbc
) & (fs
->lfs_nindir
- 1);
292 ap
->in_lbn
= metalbn
;
297 metalbn
-= -1 + (off
<< lbc
);
305 lfs_vop_bmap(struct uvnode
* vp
, daddr_t lbn
, daddr_t
* daddrp
)
307 return ufs_bmaparray(vp
->v_fs
, vp
, lbn
, daddrp
, NULL
, NULL
);
310 /* Search a block for a specific dinode. */
312 lfs_ifind(struct lfs
* fs
, ino_t ino
, struct ubuf
* bp
)
314 struct ufs1_dinode
*dip
= (struct ufs1_dinode
*) bp
->b_data
;
315 struct ufs1_dinode
*ldip
, *fin
;
317 fin
= dip
+ INOPB(fs
);
320 * Read the inode block backwards, since later versions of the
321 * inode will supercede earlier ones. Though it is unlikely, it is
322 * possible that the same inode will appear in the same inode block.
324 for (ldip
= fin
- 1; ldip
>= dip
; --ldip
)
325 if (ldip
->di_inumber
== ino
)
331 * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
332 * XXX it currently loses atime information.
335 lfs_raw_vget(struct lfs
* fs
, ino_t ino
, int fd
, ufs_daddr_t daddr
)
339 struct ufs1_dinode
*dip
;
343 vp
= ecalloc(1, sizeof(*vp
));
347 vp
->v_strategy_op
= lfs_vop_strategy
;
348 vp
->v_bwrite_op
= lfs_vop_bwrite
;
349 vp
->v_bmap_op
= lfs_vop_bmap
;
350 LIST_INIT(&vp
->v_cleanblkhd
);
351 LIST_INIT(&vp
->v_dirtyblkhd
);
353 ip
= ecalloc(1, sizeof(*ip
));
355 ip
->i_din
.ffs1_din
= ecalloc(1, sizeof(*ip
->i_din
.ffs1_din
));
357 /* Initialize the inode -- from lfs_vcreate. */
358 ip
->inode_ext
.lfs
= ecalloc(1, sizeof(*ip
->inode_ext
.lfs
));
360 /* ip->i_vnode = vp; */
364 ip
->i_lfs_effnblks
= 0;
367 /* Load inode block and find inode */
369 bread(fs
->lfs_devvp
, fsbtodb(fs
, daddr
), fs
->lfs_ibsize
,
371 bp
->b_flags
|= B_AGE
;
372 dip
= lfs_ifind(fs
, ino
, bp
);
379 memcpy(ip
->i_din
.ffs1_din
, dip
, sizeof(*dip
));
383 /* ip->i_devvp = fs->lfs_devvp; */
386 ip
->i_lfs_effnblks
= ip
->i_ffs1_blocks
;
387 ip
->i_lfs_osize
= ip
->i_ffs1_size
;
389 if (fs
->lfs_version
> 1) {
390 ip
->i_ffs1_atime
= ts
.tv_sec
;
391 ip
->i_ffs1_atimensec
= ts
.tv_nsec
;
395 memset(ip
->i_lfs_fragsize
, 0, NDADDR
* sizeof(*ip
->i_lfs_fragsize
));
396 for (i
= 0; i
< NDADDR
; i
++)
397 if (ip
->i_ffs1_db
[i
] != 0)
398 ip
->i_lfs_fragsize
[i
] = blksize(fs
, ip
, i
);
401 hash
= ((int)(intptr_t)fs
+ ino
) & (VNODE_HASH_MAX
- 1);
402 LIST_INSERT_HEAD(&getvnodelist
[hash
], vp
, v_getvnodes
);
403 LIST_INSERT_HEAD(&vnodelist
, vp
, v_mntvnodes
);
408 static struct uvnode
*
409 lfs_vget(void *vfs
, ino_t ino
)
411 struct lfs
*fs
= (struct lfs
*)vfs
;
416 LFS_IENTRY(ifp
, fs
, ino
, bp
);
417 daddr
= ifp
->if_daddr
;
419 if (daddr
<= 0 || dtosn(fs
, daddr
) >= fs
->lfs_nseg
)
421 return lfs_raw_vget(fs
, ino
, fs
->lfs_ivnode
->v_fd
, daddr
);
424 /* Check superblock magic number and checksum */
426 check_sb(struct lfs
*fs
)
430 if (fs
->lfs_magic
!= LFS_MAGIC
) {
431 printf("Superblock magic number (0x%lx) does not match "
432 "expected 0x%lx\n", (unsigned long) fs
->lfs_magic
,
433 (unsigned long) LFS_MAGIC
);
437 checksum
= lfs_sb_cksum(&(fs
->lfs_dlfs
));
438 if (fs
->lfs_cksum
!= checksum
) {
439 printf("Superblock checksum (%lx) does not match computed checksum (%lx)\n",
440 (unsigned long) fs
->lfs_cksum
, (unsigned long) checksum
);
446 /* Initialize LFS library; load superblocks and choose which to use. */
448 lfs_init(int devfd
, daddr_t sblkno
, daddr_t idaddr
, int dummy_read
, int debug
)
450 struct uvnode
*devvp
;
453 struct lfs
*fs
, *altfs
;
458 devvp
= ecalloc(1, sizeof(*devvp
));
461 devvp
->v_strategy_op
= raw_vop_strategy
;
462 devvp
->v_bwrite_op
= raw_vop_bwrite
;
463 devvp
->v_bmap_op
= raw_vop_bmap
;
464 LIST_INIT(&devvp
->v_cleanblkhd
);
465 LIST_INIT(&devvp
->v_dirtyblkhd
);
470 sblkno
= btodb(LFS_LABELPAD
);
471 fs
= ecalloc(1, sizeof(*fs
));
472 fs
->lfs_devvp
= devvp
;
475 sblkno
= btodb(LFS_LABELPAD
);
478 printf("No -b flag given, not attempting to verify checkpoint\n");
480 error
= bread(devvp
, sblkno
, LFS_SBPAD
, NOCRED
, 0, &bp
);
481 fs
= ecalloc(1, sizeof(*fs
));
482 fs
->lfs_dlfs
= *((struct dlfs
*) bp
->b_data
);
483 fs
->lfs_devvp
= devvp
;
484 bp
->b_flags
|= B_INVAL
;
488 error
= bread(devvp
, fsbtodb(fs
, fs
->lfs_sboffs
[1]),
489 LFS_SBPAD
, NOCRED
, 0, &bp
);
490 altfs
= ecalloc(1, sizeof(*altfs
));
491 altfs
->lfs_dlfs
= *((struct dlfs
*) bp
->b_data
);
492 altfs
->lfs_devvp
= devvp
;
493 bp
->b_flags
|= B_INVAL
;
496 if (check_sb(fs
) || fs
->lfs_idaddr
<= 0) {
498 printf("Primary superblock is no good, using first alternate\n");
502 /* If both superblocks check out, try verification */
503 if (check_sb(altfs
)) {
505 printf("First alternate superblock is no good, using primary\n");
508 if (lfs_verify(fs
, altfs
, devvp
, debug
) == fs
) {
524 if (fs
->lfs_version
< 2) {
525 fs
->lfs_sumsize
= LFS_V1_SUMMARY_SIZE
;
526 fs
->lfs_ibsize
= fs
->lfs_bsize
;
527 fs
->lfs_start
= fs
->lfs_sboffs
[0];
528 fs
->lfs_tstamp
= fs
->lfs_otstamp
;
533 fs
->lfs_suflags
= emalloc(2 * sizeof(u_int32_t
*));
534 fs
->lfs_suflags
[0] = emalloc(fs
->lfs_nseg
* sizeof(u_int32_t
));
535 fs
->lfs_suflags
[1] = emalloc(fs
->lfs_nseg
* sizeof(u_int32_t
));
539 idaddr
= fs
->lfs_idaddr
;
541 fs
->lfs_idaddr
= idaddr
;
542 /* NB: If dummy_read!=0, idaddr==0 here so we get a fake inode. */
543 fs
->lfs_ivnode
= lfs_raw_vget(fs
,
544 (dummy_read
? LFS_IFILE_INUM
: fs
->lfs_ifile
), devvp
->v_fd
,
546 if (fs
->lfs_ivnode
== NULL
)
549 register_vget((void *)fs
, lfs_vget
);
555 * Check partial segment validity between fs->lfs_offset and the given goal.
557 * If goal == 0, just keep on going until the segments stop making sense,
558 * and return the address of the last valid partial segment.
560 * If goal != 0, return the address of the first partial segment that failed,
561 * or "goal" if we reached it without failure (the partial segment *at* goal
562 * need not be valid).
565 try_verify(struct lfs
*osb
, struct uvnode
*devvp
, ufs_daddr_t goal
, int debug
)
567 ufs_daddr_t daddr
, odaddr
;
571 ufs_daddr_t nodirop_daddr
;
577 daddr
= osb
->lfs_offset
;
578 nodirop_daddr
= daddr
;
579 serial
= osb
->lfs_serial
;
580 while (daddr
!= goal
) {
582 * Don't mistakenly read a superblock, if there is one here.
584 if (sntod(osb
, dtosn(osb
, daddr
)) == daddr
) {
585 if (daddr
== osb
->lfs_start
)
586 daddr
+= btofsb(osb
, LFS_LABELPAD
);
587 for (i
= 0; i
< LFS_MAXNUMSB
; i
++) {
588 if (osb
->lfs_sboffs
[i
] < daddr
)
590 if (osb
->lfs_sboffs
[i
] == daddr
)
591 daddr
+= btofsb(osb
, LFS_SBPAD
);
595 /* Read in summary block */
596 bread(devvp
, fsbtodb(osb
, daddr
), osb
->lfs_sumsize
,
598 sp
= (SEGSUM
*)bp
->b_data
;
601 * Check for a valid segment summary belonging to our fs.
603 if (sp
->ss_magic
!= SS_MAGIC
||
604 sp
->ss_ident
!= osb
->lfs_ident
||
605 sp
->ss_serial
< serial
|| /* XXX strengthen this */
606 sp
->ss_sumsum
!= cksum(&sp
->ss_datasum
, osb
->lfs_sumsize
-
607 sizeof(sp
->ss_sumsum
))) {
610 if (sp
->ss_magic
!= SS_MAGIC
)
611 pwarn("pseg at 0x%x: "
612 "wrong magic number\n",
614 else if (sp
->ss_ident
!= osb
->lfs_ident
)
615 pwarn("pseg at 0x%x: "
616 "expected ident %llx, got %llx\n",
618 (long long)sp
->ss_ident
,
619 (long long)osb
->lfs_ident
);
620 else if (sp
->ss_serial
>= serial
)
621 pwarn("pseg at 0x%x: "
622 "serial %d < %d\n", (int)daddr
,
623 (int)sp
->ss_serial
, (int)serial
);
625 pwarn("pseg at 0x%x: "
626 "summary checksum wrong\n",
631 if (debug
&& sp
->ss_serial
!= serial
)
632 pwarn("warning, serial=%d ss_serial=%d\n",
633 (int)serial
, (int)sp
->ss_serial
);
635 bc
= check_summary(osb
, sp
, daddr
, debug
, devvp
, NULL
);
641 pwarn("summary good: 0x%x/%d\n", (int)daddr
,
645 daddr
+= btofsb(osb
, osb
->lfs_sumsize
+ bc
);
646 if (dtosn(osb
, odaddr
) != dtosn(osb
, daddr
) ||
647 dtosn(osb
, daddr
) != dtosn(osb
, daddr
+
648 btofsb(osb
, osb
->lfs_sumsize
+ osb
->lfs_bsize
) - 1)) {
653 * Check for the beginning and ending of a sequence of
654 * dirops. Writes from the cleaner never involve new
655 * information, and are always checkpoints; so don't try
656 * to roll forward through them. Likewise, psegs written
657 * by a previous roll-forward attempt are not interesting.
659 if (sp
->ss_flags
& (SS_CLEAN
| SS_RFW
))
661 if (hitclean
== 0 && (sp
->ss_flags
& SS_CONT
) == 0)
662 nodirop_daddr
= daddr
;
668 return nodirop_daddr
;
673 /* Use try_verify to check whether the newer superblock is valid. */
675 lfs_verify(struct lfs
*sb0
, struct lfs
*sb1
, struct uvnode
*devvp
, int debug
)
678 struct lfs
*osb
, *nsb
;
681 * Verify the checkpoint of the newer superblock,
682 * if the timestamp/serial number of the two superblocks is
688 pwarn("sb0 %lld, sb1 %lld",
689 (long long) sb0
->lfs_serial
,
690 (long long) sb1
->lfs_serial
);
692 if ((sb0
->lfs_version
== 1 &&
693 sb0
->lfs_otstamp
!= sb1
->lfs_otstamp
) ||
694 (sb0
->lfs_version
> 1 &&
695 sb0
->lfs_serial
!= sb1
->lfs_serial
)) {
696 if (sb0
->lfs_version
== 1) {
697 if (sb0
->lfs_otstamp
> sb1
->lfs_otstamp
) {
705 if (sb0
->lfs_serial
> sb1
->lfs_serial
) {
714 printf("Attempting to verify newer checkpoint...");
717 daddr
= try_verify(osb
, devvp
, nsb
->lfs_offset
, debug
);
721 if (daddr
== nsb
->lfs_offset
) {
722 pwarn("** Newer checkpoint verified, recovered %lld seconds of data\n",
723 (long long) nsb
->lfs_tstamp
- (long long) osb
->lfs_tstamp
);
726 pwarn("** Newer checkpoint invalid, lost %lld seconds of data\n", (long long) nsb
->lfs_tstamp
- (long long) osb
->lfs_tstamp
);
728 return (daddr
== nsb
->lfs_offset
? nsb
: osb
);
730 /* Nothing to check */
734 /* Verify a partial-segment summary; return the number of bytes on disk. */
736 check_summary(struct lfs
*fs
, SEGSUM
*sp
, ufs_daddr_t pseg_addr
, int debug
,
737 struct uvnode
*devvp
, void (func(ufs_daddr_t
, FINFO
*)))
740 int bc
; /* Bytes in partial segment */
742 ufs_daddr_t seg_addr
, daddr
;
743 ufs_daddr_t
*dp
, *idp
;
745 int i
, j
, k
, datac
, len
;
750 sn
= dtosn(fs
, pseg_addr
);
751 seg_addr
= sntod(fs
, sn
);
753 /* We've already checked the sumsum, just do the data bounds and sum */
755 /* Count the blocks. */
756 nblocks
= howmany(sp
->ss_ninos
, INOPB(fs
));
757 bc
= nblocks
<< (fs
->lfs_version
> 1 ? fs
->lfs_ffshift
: fs
->lfs_bshift
);
760 fp
= (FINFO
*) (sp
+ 1);
761 for (i
= 0; i
< sp
->ss_nfinfo
; i
++) {
762 nblocks
+= fp
->fi_nblocks
;
763 bc
+= fp
->fi_lastlength
+ ((fp
->fi_nblocks
- 1)
766 fp
= (FINFO
*) (fp
->fi_blocks
+ fp
->fi_nblocks
);
767 if (((char *)fp
) - (char *)sp
> fs
->lfs_sumsize
)
770 datap
= emalloc(nblocks
* sizeof(*datap
));
773 dp
= (ufs_daddr_t
*) sp
;
774 dp
+= fs
->lfs_sumsize
/ sizeof(ufs_daddr_t
);
778 daddr
= pseg_addr
+ btofsb(fs
, fs
->lfs_sumsize
);
779 fp
= (FINFO
*) (sp
+ 1);
781 i
< sp
->ss_nfinfo
|| j
< howmany(sp
->ss_ninos
, INOPB(fs
)); i
++) {
782 if (i
>= sp
->ss_nfinfo
&& *idp
!= daddr
) {
783 pwarn("Not enough inode blocks in pseg at 0x%" PRIx32
784 ": found %d, wanted %d\n",
785 pseg_addr
, j
, howmany(sp
->ss_ninos
, INOPB(fs
)));
787 pwarn("*idp=%x, daddr=%" PRIx32
"\n", *idp
,
791 while (j
< howmany(sp
->ss_ninos
, INOPB(fs
)) && *idp
== daddr
) {
792 bread(devvp
, fsbtodb(fs
, daddr
), fs
->lfs_ibsize
,
794 datap
[datac
++] = ((u_int32_t
*) (bp
->b_data
))[0];
798 daddr
+= btofsb(fs
, fs
->lfs_ibsize
);
801 if (i
< sp
->ss_nfinfo
) {
804 for (k
= 0; k
< fp
->fi_nblocks
; k
++) {
805 len
= (k
== fp
->fi_nblocks
- 1 ?
808 bread(devvp
, fsbtodb(fs
, daddr
), len
,
810 datap
[datac
++] = ((u_int32_t
*) (bp
->b_data
))[0];
812 daddr
+= btofsb(fs
, len
);
814 fp
= (FINFO
*) (fp
->fi_blocks
+ fp
->fi_nblocks
);
818 if (datac
!= nblocks
) {
819 pwarn("Partial segment at 0x%llx expected %d blocks counted %d\n",
820 (long long) pseg_addr
, nblocks
, datac
);
822 ccksum
= cksum(datap
, nblocks
* sizeof(u_int32_t
));
823 /* Check the data checksum */
824 if (ccksum
!= sp
->ss_datasum
) {
825 pwarn("Partial segment at 0x%" PRIx32
" data checksum"
826 " mismatch: given 0x%x, computed 0x%x\n",
827 pseg_addr
, sp
->ss_datasum
, ccksum
);
836 /* print message and exit */
838 my_vpanic(int fatal
, const char *fmt
, va_list ap
)
840 (void) vprintf(fmt
, ap
);
845 call_panic(const char *fmt
, ...)
850 panic_func(1, fmt
, ap
);
854 /* Allocate a new inode. */
856 lfs_valloc(struct lfs
*fs
, ino_t ino
)
858 struct ubuf
*bp
, *cbp
;
865 /* Get the head of the freelist. */
866 LFS_GET_HEADFREE(fs
, cip
, cbp
, &new_ino
);
869 * Remove the inode from the free list and write the new start
870 * of the free list into the superblock.
872 LFS_IENTRY(ifp
, fs
, new_ino
, bp
);
873 if (ifp
->if_daddr
!= LFS_UNUSED_DADDR
)
874 panic("lfs_valloc: inuse inode %d on the free list", new_ino
);
875 LFS_PUT_HEADFREE(fs
, cip
, cbp
, ifp
->if_nextfree
);
877 new_gen
= ifp
->if_version
; /* version was updated by vfree */
880 /* Extend IFILE so that the next lfs_valloc will succeed. */
881 if (fs
->lfs_freehd
== LFS_UNUSED_INUM
) {
882 if ((error
= extend_ifile(fs
)) != 0) {
883 LFS_PUT_HEADFREE(fs
, cip
, cbp
, new_ino
);
888 /* Set superblock modified bit and increment file count. */
892 return lfs_raw_vget(fs
, ino
, fs
->lfs_devvp
->v_fd
, 0x0);
896 void reset_maxino(ino_t
);
900 * Add a new block to the Ifile, to accommodate future file creations.
903 extend_ifile(struct lfs
*fs
)
909 struct ubuf
*bp
, *cbp
;
910 daddr_t i
, blkno
, max
;
916 blkno
= lblkno(fs
, ip
->i_ffs1_size
);
918 lfs_balloc(vp
, ip
->i_ffs1_size
, fs
->lfs_bsize
, &bp
);
919 ip
->i_ffs1_size
+= fs
->lfs_bsize
;
920 ip
->i_flag
|= IN_MODIFIED
;
922 i
= (blkno
- fs
->lfs_segtabsz
- fs
->lfs_cleansz
) *
924 LFS_GET_HEADFREE(fs
, cip
, cbp
, &oldlast
);
925 LFS_PUT_HEADFREE(fs
, cip
, cbp
, i
);
926 max
= i
+ fs
->lfs_ifpb
;
927 fs
->lfs_bfree
-= btofsb(fs
, fs
->lfs_bsize
);
929 if (fs
->lfs_version
== 1) {
930 for (ifp_v1
= (IFILE_V1
*)bp
->b_data
; i
< max
; ++ifp_v1
) {
931 ifp_v1
->if_version
= 1;
932 ifp_v1
->if_daddr
= LFS_UNUSED_DADDR
;
933 ifp_v1
->if_nextfree
= ++i
;
936 ifp_v1
->if_nextfree
= oldlast
;
938 for (ifp
= (IFILE
*)bp
->b_data
; i
< max
; ++ifp
) {
940 ifp
->if_daddr
= LFS_UNUSED_DADDR
;
941 ifp
->if_nextfree
= ++i
;
944 ifp
->if_nextfree
= oldlast
;
946 LFS_PUT_TAILFREE(fs
, cip
, cbp
, max
- 1);
951 reset_maxino(((ip
->i_ffs1_size
>> fs
->lfs_bshift
) - fs
->lfs_segtabsz
-
952 fs
->lfs_cleansz
) * fs
->lfs_ifpb
);
958 * Allocate a block, and to inode and filesystem block accounting for it
959 * and for any indirect blocks the may need to be created in order for
960 * this block to be created.
962 * Blocks which have never been accounted for (i.e., which "do not exist")
963 * have disk address 0, which is translated by ufs_bmap to the special value
964 * UNASSIGNED == -1, as in the historical UFS.
966 * Blocks which have been accounted for but which have not yet been written
967 * to disk are given the new special disk address UNWRITTEN == -2, so that
968 * they can be differentiated from completely new blocks.
971 lfs_balloc(struct uvnode
*vp
, off_t startoffset
, int iosize
, struct ubuf
**bpp
)
974 daddr_t daddr
, idaddr
;
975 struct ubuf
*ibp
, *bp
;
978 struct indir indirs
[NIADDR
+2], *idp
;
979 daddr_t lbn
, lastblock
;
981 int error
, frags
, i
, nsize
, osize
, num
;
985 offset
= blkoff(fs
, startoffset
);
986 lbn
= lblkno(fs
, startoffset
);
989 * Three cases: it's a block beyond the end of file, it's a block in
990 * the file that may or may not have been assigned a disk address or
991 * we're writing an entire block.
993 * Note, if the daddr is UNWRITTEN, the block already exists in
994 * the cache (it was read or written earlier). If so, make sure
995 * we don't count it as a new block or zero out its contents. If
996 * it did not, make sure we allocate any necessary indirect
999 * If we are writing a block beyond the end of the file, we need to
1000 * check if the old last block was a fragment. If it was, we need
1007 /* Check for block beyond end of file and fragment extension needed. */
1008 lastblock
= lblkno(fs
, ip
->i_ffs1_size
);
1009 if (lastblock
< NDADDR
&& lastblock
< lbn
) {
1010 osize
= blksize(fs
, ip
, lastblock
);
1011 if (osize
< fs
->lfs_bsize
&& osize
> 0) {
1012 if ((error
= lfs_fragextend(vp
, osize
, fs
->lfs_bsize
,
1014 (bpp
? &bp
: NULL
))))
1016 ip
->i_ffs1_size
= ip
->i_ffs1_size
=
1017 (lastblock
+ 1) * fs
->lfs_bsize
;
1018 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1020 (void) VOP_BWRITE(bp
);
1025 * If the block we are writing is a direct block, it's the last
1026 * block in the file, and offset + iosize is less than a full
1027 * block, we can write one or more fragments. There are two cases:
1028 * the block is brand new and we should allocate it the correct
1029 * size or it already exists and contains some fragments and
1030 * may need to extend it.
1032 if (lbn
< NDADDR
&& lblkno(fs
, ip
->i_ffs1_size
) <= lbn
) {
1033 osize
= blksize(fs
, ip
, lbn
);
1034 nsize
= fragroundup(fs
, offset
+ iosize
);
1035 if (lblktosize(fs
, lbn
) >= ip
->i_ffs1_size
) {
1036 /* Brand new block or fragment */
1037 frags
= numfrags(fs
, nsize
);
1038 bb
= fragstofsb(fs
, frags
);
1040 *bpp
= bp
= getblk(vp
, lbn
, nsize
);
1041 bp
->b_blkno
= UNWRITTEN
;
1043 ip
->i_lfs_effnblks
+= bb
;
1044 fs
->lfs_bfree
-= bb
;
1045 ip
->i_ffs1_db
[lbn
] = UNWRITTEN
;
1047 if (nsize
<= osize
) {
1048 /* No need to extend */
1049 if (bpp
&& (error
= bread(vp
, lbn
, osize
,
1053 /* Extend existing block */
1055 lfs_fragextend(vp
, osize
, nsize
, lbn
,
1056 (bpp
? &bp
: NULL
))))
1065 error
= ufs_bmaparray(fs
, vp
, lbn
, &daddr
, &indirs
[0], &num
);
1069 daddr
= (daddr_t
)((int32_t)daddr
); /* XXX ondisk32 */
1072 * Do byte accounting all at once, so we can gracefully fail *before*
1073 * we start assigning blocks.
1075 bb
= fsbtodb(fs
, 1); /* bb = VFSTOUFS(vp->v_mount)->um_seqinc; */
1077 if (daddr
== UNASSIGNED
) {
1080 for (i
= 1; i
< num
; ++i
) {
1081 if (!indirs
[i
].in_exists
) {
1085 fs
->lfs_bfree
-= bcount
;
1086 ip
->i_lfs_effnblks
+= bcount
;
1088 if (daddr
== UNASSIGNED
) {
1089 if (num
> 0 && ip
->i_ffs1_ib
[indirs
[0].in_off
] == 0) {
1090 ip
->i_ffs1_ib
[indirs
[0].in_off
] = UNWRITTEN
;
1094 * Create new indirect blocks if necessary
1097 idaddr
= ip
->i_ffs1_ib
[indirs
[0].in_off
];
1098 for (i
= 1; i
< num
; ++i
) {
1099 ibp
= getblk(vp
, indirs
[i
].in_lbn
,
1101 if (!indirs
[i
].in_exists
) {
1102 memset(ibp
->b_data
, 0, ibp
->b_bufsize
);
1103 ibp
->b_blkno
= UNWRITTEN
;
1104 } else if (!(ibp
->b_flags
& (B_DELWRI
| B_DONE
))) {
1105 ibp
->b_blkno
= fsbtodb(fs
, idaddr
);
1106 ibp
->b_flags
|= B_READ
;
1110 * This block exists, but the next one may not.
1111 * If that is the case mark it UNWRITTEN to
1112 * keep the accounting straight.
1115 if (((int32_t *)ibp
->b_data
)[indirs
[i
].in_off
] == 0)
1116 ((int32_t *)ibp
->b_data
)[indirs
[i
].in_off
] =
1119 idaddr
= ((int32_t *)ibp
->b_data
)[indirs
[i
].in_off
];
1120 if ((error
= VOP_BWRITE(ibp
)))
1128 * Get the existing block from the cache, if requested.
1130 frags
= fsbtofrags(fs
, bb
);
1132 *bpp
= bp
= getblk(vp
, lbn
, blksize(fs
, ip
, lbn
));
1135 * The block we are writing may be a brand new block
1136 * in which case we need to do accounting.
1138 * We can tell a truly new block because ufs_bmaparray will say
1139 * it is UNASSIGNED. Once we allocate it we will assign it the
1140 * disk address UNWRITTEN.
1142 if (daddr
== UNASSIGNED
) {
1144 /* Note the new address */
1145 bp
->b_blkno
= UNWRITTEN
;
1150 ip
->i_ffs1_db
[lbn
] = UNWRITTEN
;
1153 ip
->i_ffs1_ib
[indirs
[0].in_off
] = UNWRITTEN
;
1156 idp
= &indirs
[num
- 1];
1157 if (bread(vp
, idp
->in_lbn
, fs
->lfs_bsize
, NOCRED
,
1159 panic("lfs_balloc: bread bno %lld",
1160 (long long)idp
->in_lbn
);
1162 ((int32_t *)ibp
->b_data
)[idp
->in_off
] = UNWRITTEN
;
1165 } else if (bpp
&& !(bp
->b_flags
& (B_DONE
|B_DELWRI
))) {
1167 * Not a brand new block, also not in the cache;
1168 * read it in from disk.
1170 if (iosize
== fs
->lfs_bsize
)
1171 /* Optimization: I/O is unnecessary. */
1172 bp
->b_blkno
= daddr
;
1175 * We need to read the block to preserve the
1178 bp
->b_blkno
= daddr
;
1179 bp
->b_flags
|= B_READ
;
1189 lfs_fragextend(struct uvnode
*vp
, int osize
, int nsize
, daddr_t lbn
,
1200 bb
= (long)fragstofsb(fs
, numfrags(fs
, nsize
- osize
));
1204 * If we are not asked to actually return the block, all we need
1205 * to do is allocate space for it. UBC will handle dirtying the
1206 * appropriate things and making sure it all goes to disk.
1207 * Don't bother to read in that case.
1209 if (bpp
&& (error
= bread(vp
, lbn
, osize
, NOCRED
, 0, bpp
))) {
1214 fs
->lfs_bfree
-= bb
;
1215 ip
->i_lfs_effnblks
+= bb
;
1216 ip
->i_flag
|= IN_CHANGE
| IN_UPDATE
;
1219 obufsize
= (*bpp
)->b_bufsize
;
1220 (*bpp
)->b_data
= erealloc((*bpp
)->b_data
, nsize
);
1221 (void)memset((*bpp
)->b_data
+ osize
, 0, nsize
- osize
);