Sync usage with man page.
[netbsd-mini2440.git] / sbin / fsck_lfs / lfs.c
blob108d08c6d1df48d86a0e19708b546e9682672432
1 /* $NetBSD: lfs.c,v 1.30 2009/02/22 20:28:05 ad Exp $ */
2 /*-
3 * Copyright (c) 2003 The NetBSD Foundation, Inc.
4 * All rights reserved.
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
11 * are met:
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
41 * are met:
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
61 * SUCH DAMAGE.
63 * @(#)ufs_bmap.c 8.8 (Berkeley) 8/11/95
67 #include <sys/types.h>
68 #include <sys/param.h>
69 #include <sys/time.h>
70 #include <sys/buf.h>
71 #include <sys/mount.h>
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufsmount.h>
75 #define vnode uvnode
76 #include <ufs/lfs/lfs.h>
77 #undef vnode
79 #include <assert.h>
80 #include <err.h>
81 #include <errno.h>
82 #include <stdarg.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include <util.h>
89 #include "bufcache.h"
90 #include "vnode.h"
91 #include "lfs_user.h"
92 #include "segwrite.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];
103 extern int nvnodes;
105 static int
106 lfs_fragextend(struct uvnode *, int, int, daddr_t, struct ubuf **);
108 int fsdirty = 0;
109 void (*panic_func)(int, const char *, va_list) = my_vpanic;
112 * LFS buffer and uvnode operations
116 lfs_vop_strategy(struct ubuf * bp)
118 int count;
120 if (bp->b_flags & B_READ) {
121 count = kops.ko_pread(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
122 dbtob(bp->b_blkno));
123 if (count == bp->b_bcount)
124 bp->b_flags |= B_DONE;
125 } else {
126 count = kops.ko_pwrite(bp->b_vp->v_fd, bp->b_data, bp->b_bcount,
127 dbtob(bp->b_blkno));
128 if (count == 0) {
129 perror("pwrite");
130 return -1;
132 bp->b_flags &= ~B_DELWRI;
133 reassignbuf(bp, bp->b_vp);
135 return 0;
139 lfs_vop_bwrite(struct ubuf * bp)
141 struct lfs *fs;
143 fs = bp->b_vp->v_fs;
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);
149 brelse(bp, 0);
150 return 0;
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)
162 struct inode *ip;
163 struct ubuf *bp;
164 struct indir a[NIADDR + 1], *xap;
165 daddr_t daddr;
166 daddr_t metalbn;
167 int error, num;
169 ip = VTOI(vp);
171 if (bn >= 0 && bn < NDADDR) {
172 if (nump != NULL)
173 *nump = 0;
174 *bnp = fsbtodb(fs, ip->i_ffs1_db[bn]);
175 if (*bnp == 0)
176 *bnp = -1;
177 return (0);
179 xap = ap == NULL ? a : ap;
180 if (!nump)
181 nump = &num;
182 if ((error = ufs_getlbns(fs, vp, bn, xap, nump)) != 0)
183 return (error);
185 num = *nump;
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)
197 break;
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.
202 if (bp)
203 brelse(bp, 0);
205 xap->in_exists = 1;
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;
211 VOP_STRATEGY(bp);
213 daddr = ((ufs_daddr_t *) bp->b_data)[xap->in_off];
215 if (bp)
216 brelse(bp, 0);
218 daddr = fsbtodb(fs, (ufs_daddr_t) daddr);
219 *bnp = daddr == 0 ? -1 : daddr;
220 return (0);
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;
236 int64_t blockcnt;
237 int lbc;
238 int i, numlevels, off;
239 int lognindir, indir;
241 metalbn = 0; /* XXXGCC -Wuninitialized [sh3] */
243 if (nump)
244 *nump = 0;
245 numlevels = 0;
246 realbn = bn;
247 if (bn < 0)
248 bn = -bn;
250 lognindir = -1;
251 for (indir = fs->lfs_nindir; indir; indir >>= 1)
252 ++lognindir;
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. */
259 bn -= NDADDR;
260 for (lbc = 0, i = NIADDR;; i--, bn -= blockcnt) {
261 if (i == 0)
262 return (EFBIG);
264 lbc += lognindir;
265 blockcnt = (int64_t) 1 << lbc;
267 if (bn < blockcnt)
268 break;
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;
280 ap->in_exists = 0;
281 ap++;
282 for (++numlevels; i <= NIADDR; i++) {
283 /* If searching for a meta-data block, quit when found. */
284 if (metalbn == realbn)
285 break;
287 lbc -= lognindir;
288 blockcnt = (int64_t) 1 << lbc;
289 off = (bn >> lbc) & (fs->lfs_nindir - 1);
291 ++numlevels;
292 ap->in_lbn = metalbn;
293 ap->in_off = off;
294 ap->in_exists = 0;
295 ++ap;
297 metalbn -= -1 + (off << lbc);
299 if (nump)
300 *nump = numlevels;
301 return (0);
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. */
311 struct ufs1_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)
326 return (ldip);
327 return NULL;
331 * lfs_raw_vget makes us a new vnode from the inode at the given disk address.
332 * XXX it currently loses atime information.
334 struct uvnode *
335 lfs_raw_vget(struct lfs * fs, ino_t ino, int fd, ufs_daddr_t daddr)
337 struct uvnode *vp;
338 struct inode *ip;
339 struct ufs1_dinode *dip;
340 struct ubuf *bp;
341 int i, hash;
343 vp = ecalloc(1, sizeof(*vp));
344 vp->v_fd = fd;
345 vp->v_fs = fs;
346 vp->v_usecount = 0;
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));
359 vp->v_data = ip;
360 /* ip->i_vnode = vp; */
361 ip->i_number = ino;
362 ip->i_lockf = 0;
363 ip->i_diroff = 0;
364 ip->i_lfs_effnblks = 0;
365 ip->i_flag = 0;
367 /* Load inode block and find inode */
368 if (daddr > 0) {
369 bread(fs->lfs_devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
370 NULL, 0, &bp);
371 bp->b_flags |= B_AGE;
372 dip = lfs_ifind(fs, ino, bp);
373 if (dip == NULL) {
374 brelse(bp, 0);
375 free(ip);
376 free(vp);
377 return NULL;
379 memcpy(ip->i_din.ffs1_din, dip, sizeof(*dip));
380 brelse(bp, 0);
382 ip->i_number = ino;
383 /* ip->i_devvp = fs->lfs_devvp; */
384 ip->i_lfs = fs;
386 ip->i_lfs_effnblks = ip->i_ffs1_blocks;
387 ip->i_lfs_osize = ip->i_ffs1_size;
388 #if 0
389 if (fs->lfs_version > 1) {
390 ip->i_ffs1_atime = ts.tv_sec;
391 ip->i_ffs1_atimensec = ts.tv_nsec;
393 #endif
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);
400 ++nvnodes;
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);
405 return vp;
408 static struct uvnode *
409 lfs_vget(void *vfs, ino_t ino)
411 struct lfs *fs = (struct lfs *)vfs;
412 ufs_daddr_t daddr;
413 struct ubuf *bp;
414 IFILE *ifp;
416 LFS_IENTRY(ifp, fs, ino, bp);
417 daddr = ifp->if_daddr;
418 brelse(bp, 0);
419 if (daddr <= 0 || dtosn(fs, daddr) >= fs->lfs_nseg)
420 return NULL;
421 return lfs_raw_vget(fs, ino, fs->lfs_ivnode->v_fd, daddr);
424 /* Check superblock magic number and checksum */
425 static int
426 check_sb(struct lfs *fs)
428 u_int32_t checksum;
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);
434 return 1;
436 /* checksum */
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);
441 return 1;
443 return 0;
446 /* Initialize LFS library; load superblocks and choose which to use. */
447 struct lfs *
448 lfs_init(int devfd, daddr_t sblkno, daddr_t idaddr, int dummy_read, int debug)
450 struct uvnode *devvp;
451 struct ubuf *bp;
452 int tryalt;
453 struct lfs *fs, *altfs;
454 int error;
456 vfs_init();
458 devvp = ecalloc(1, sizeof(*devvp));
459 devvp->v_fs = NULL;
460 devvp->v_fd = devfd;
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);
467 tryalt = 0;
468 if (dummy_read) {
469 if (sblkno == 0)
470 sblkno = btodb(LFS_LABELPAD);
471 fs = ecalloc(1, sizeof(*fs));
472 fs->lfs_devvp = devvp;
473 } else {
474 if (sblkno == 0) {
475 sblkno = btodb(LFS_LABELPAD);
476 tryalt = 1;
477 } else if (debug) {
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;
485 brelse(bp, 0);
487 if (tryalt) {
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;
494 brelse(bp, 0);
496 if (check_sb(fs) || fs->lfs_idaddr <= 0) {
497 if (debug)
498 printf("Primary superblock is no good, using first alternate\n");
499 free(fs);
500 fs = altfs;
501 } else {
502 /* If both superblocks check out, try verification */
503 if (check_sb(altfs)) {
504 if (debug)
505 printf("First alternate superblock is no good, using primary\n");
506 free(altfs);
507 } else {
508 if (lfs_verify(fs, altfs, devvp, debug) == fs) {
509 free(altfs);
510 } else {
511 free(fs);
512 fs = altfs;
517 if (check_sb(fs)) {
518 free(fs);
519 return NULL;
523 /* Compatibility */
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;
529 fs->lfs_fsbtodb = 0;
532 if (!dummy_read) {
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));
538 if (idaddr == 0)
539 idaddr = fs->lfs_idaddr;
540 else
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,
545 idaddr);
546 if (fs->lfs_ivnode == NULL)
547 return NULL;
549 register_vget((void *)fs, lfs_vget);
551 return fs;
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).
564 ufs_daddr_t
565 try_verify(struct lfs *osb, struct uvnode *devvp, ufs_daddr_t goal, int debug)
567 ufs_daddr_t daddr, odaddr;
568 SEGSUM *sp;
569 int i, bc, hitclean;
570 struct ubuf *bp;
571 ufs_daddr_t nodirop_daddr;
572 u_int64_t serial;
574 bc = 0;
575 hitclean = 0;
576 odaddr = -1;
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)
589 break;
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,
597 NULL, 0, &bp);
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))) {
608 brelse(bp, 0);
609 if (debug) {
610 if (sp->ss_magic != SS_MAGIC)
611 pwarn("pseg at 0x%x: "
612 "wrong magic number\n",
613 (int)daddr);
614 else if (sp->ss_ident != osb->lfs_ident)
615 pwarn("pseg at 0x%x: "
616 "expected ident %llx, got %llx\n",
617 (int)daddr,
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);
624 else
625 pwarn("pseg at 0x%x: "
626 "summary checksum wrong\n",
627 (int)daddr);
629 break;
631 if (debug && sp->ss_serial != serial)
632 pwarn("warning, serial=%d ss_serial=%d\n",
633 (int)serial, (int)sp->ss_serial);
634 ++serial;
635 bc = check_summary(osb, sp, daddr, debug, devvp, NULL);
636 if (bc == 0) {
637 brelse(bp, 0);
638 break;
640 if (debug)
641 pwarn("summary good: 0x%x/%d\n", (int)daddr,
642 (int)sp->ss_serial);
643 assert (bc > 0);
644 odaddr = 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)) {
649 daddr = sp->ss_next;
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))
660 hitclean = 1;
661 if (hitclean == 0 && (sp->ss_flags & SS_CONT) == 0)
662 nodirop_daddr = daddr;
664 brelse(bp, 0);
667 if (goal == 0)
668 return nodirop_daddr;
669 else
670 return daddr;
673 /* Use try_verify to check whether the newer superblock is valid. */
674 struct lfs *
675 lfs_verify(struct lfs *sb0, struct lfs *sb1, struct uvnode *devvp, int debug)
677 ufs_daddr_t daddr;
678 struct lfs *osb, *nsb;
681 * Verify the checkpoint of the newer superblock,
682 * if the timestamp/serial number of the two superblocks is
683 * different.
686 osb = NULL;
687 if (debug)
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) {
698 osb = sb1;
699 nsb = sb0;
700 } else {
701 osb = sb0;
702 nsb = sb1;
704 } else {
705 if (sb0->lfs_serial > sb1->lfs_serial) {
706 osb = sb1;
707 nsb = sb0;
708 } else {
709 osb = sb0;
710 nsb = sb1;
713 if (debug) {
714 printf("Attempting to verify newer checkpoint...");
715 fflush(stdout);
717 daddr = try_verify(osb, devvp, nsb->lfs_offset, debug);
719 if (debug)
720 printf("done.\n");
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);
724 sbdirty();
725 } else {
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 */
731 return osb;
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 *)))
739 FINFO *fp;
740 int bc; /* Bytes in partial segment */
741 int nblocks;
742 ufs_daddr_t seg_addr, daddr;
743 ufs_daddr_t *dp, *idp;
744 struct ubuf *bp;
745 int i, j, k, datac, len;
746 long sn;
747 u_int32_t *datap;
748 u_int32_t ccksum;
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);
758 assert(bc >= 0);
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)
764 << fs->lfs_bshift);
765 assert(bc >= 0);
766 fp = (FINFO *) (fp->fi_blocks + fp->fi_nblocks);
767 if (((char *)fp) - (char *)sp > fs->lfs_sumsize)
768 return 0;
770 datap = emalloc(nblocks * sizeof(*datap));
771 datac = 0;
773 dp = (ufs_daddr_t *) sp;
774 dp += fs->lfs_sumsize / sizeof(ufs_daddr_t);
775 dp--;
777 idp = dp;
778 daddr = pseg_addr + btofsb(fs, fs->lfs_sumsize);
779 fp = (FINFO *) (sp + 1);
780 for (i = 0, j = 0;
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)));
786 if (debug)
787 pwarn("*idp=%x, daddr=%" PRIx32 "\n", *idp,
788 daddr);
789 break;
791 while (j < howmany(sp->ss_ninos, INOPB(fs)) && *idp == daddr) {
792 bread(devvp, fsbtodb(fs, daddr), fs->lfs_ibsize,
793 NOCRED, 0, &bp);
794 datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
795 brelse(bp, 0);
797 ++j;
798 daddr += btofsb(fs, fs->lfs_ibsize);
799 --idp;
801 if (i < sp->ss_nfinfo) {
802 if (func)
803 func(daddr, fp);
804 for (k = 0; k < fp->fi_nblocks; k++) {
805 len = (k == fp->fi_nblocks - 1 ?
806 fp->fi_lastlength
807 : fs->lfs_bsize);
808 bread(devvp, fsbtodb(fs, daddr), len,
809 NOCRED, 0, &bp);
810 datap[datac++] = ((u_int32_t *) (bp->b_data))[0];
811 brelse(bp, 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);
828 free(datap);
829 return 0;
831 free(datap);
832 assert(bc >= 0);
833 return bc;
836 /* print message and exit */
837 void
838 my_vpanic(int fatal, const char *fmt, va_list ap)
840 (void) vprintf(fmt, ap);
841 exit(8);
844 void
845 call_panic(const char *fmt, ...)
847 va_list ap;
849 va_start(ap, fmt);
850 panic_func(1, fmt, ap);
851 va_end(ap);
854 /* Allocate a new inode. */
855 struct uvnode *
856 lfs_valloc(struct lfs *fs, ino_t ino)
858 struct ubuf *bp, *cbp;
859 struct ifile *ifp;
860 ino_t new_ino;
861 int error;
862 int new_gen;
863 CLEANERINFO *cip;
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 */
878 brelse(bp, 0);
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);
884 return NULL;
888 /* Set superblock modified bit and increment file count. */
889 sbdirty();
890 ++fs->lfs_nfiles;
892 return lfs_raw_vget(fs, ino, fs->lfs_devvp->v_fd, 0x0);
895 #ifdef IN_FSCK_LFS
896 void reset_maxino(ino_t);
897 #endif
900 * Add a new block to the Ifile, to accommodate future file creations.
903 extend_ifile(struct lfs *fs)
905 struct uvnode *vp;
906 struct inode *ip;
907 IFILE *ifp;
908 IFILE_V1 *ifp_v1;
909 struct ubuf *bp, *cbp;
910 daddr_t i, blkno, max;
911 ino_t oldlast;
912 CLEANERINFO *cip;
914 vp = fs->lfs_ivnode;
915 ip = VTOI(vp);
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) *
923 fs->lfs_ifpb;
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;
935 ifp_v1--;
936 ifp_v1->if_nextfree = oldlast;
937 } else {
938 for (ifp = (IFILE *)bp->b_data; i < max; ++ifp) {
939 ifp->if_version = 1;
940 ifp->if_daddr = LFS_UNUSED_DADDR;
941 ifp->if_nextfree = ++i;
943 ifp--;
944 ifp->if_nextfree = oldlast;
946 LFS_PUT_TAILFREE(fs, cip, cbp, max - 1);
948 LFS_BWRITE_LOG(bp);
950 #ifdef IN_FSCK_LFS
951 reset_maxino(((ip->i_ffs1_size >> fs->lfs_bshift) - fs->lfs_segtabsz -
952 fs->lfs_cleansz) * fs->lfs_ifpb);
953 #endif
954 return 0;
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)
973 int offset;
974 daddr_t daddr, idaddr;
975 struct ubuf *ibp, *bp;
976 struct inode *ip;
977 struct lfs *fs;
978 struct indir indirs[NIADDR+2], *idp;
979 daddr_t lbn, lastblock;
980 int bb, bcount;
981 int error, frags, i, nsize, osize, num;
983 ip = VTOI(vp);
984 fs = ip->i_lfs;
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
997 * blocks.
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
1001 * to rewrite it.
1004 if (bpp)
1005 *bpp = NULL;
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,
1013 lastblock,
1014 (bpp ? &bp : NULL))))
1015 return (error);
1016 ip->i_ffs1_size = ip->i_ffs1_size =
1017 (lastblock + 1) * fs->lfs_bsize;
1018 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1019 if (bpp)
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);
1039 if (bpp) {
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;
1046 } else {
1047 if (nsize <= osize) {
1048 /* No need to extend */
1049 if (bpp && (error = bread(vp, lbn, osize,
1050 NOCRED, 0, &bp)))
1051 return error;
1052 } else {
1053 /* Extend existing block */
1054 if ((error =
1055 lfs_fragextend(vp, osize, nsize, lbn,
1056 (bpp ? &bp : NULL))))
1057 return error;
1059 if (bpp)
1060 *bpp = bp;
1062 return 0;
1065 error = ufs_bmaparray(fs, vp, lbn, &daddr, &indirs[0], &num);
1066 if (error)
1067 return (error);
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; */
1076 bcount = 0;
1077 if (daddr == UNASSIGNED) {
1078 bcount = bb;
1080 for (i = 1; i < num; ++i) {
1081 if (!indirs[i].in_exists) {
1082 bcount += bb;
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
1096 if (num > 1) {
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,
1100 fs->lfs_bsize);
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;
1107 VOP_STRATEGY(ibp);
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.
1114 /* XXX ondisk32 */
1115 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
1116 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
1117 UNWRITTEN;
1118 /* XXX ondisk32 */
1119 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
1120 if ((error = VOP_BWRITE(ibp)))
1121 return error;
1128 * Get the existing block from the cache, if requested.
1130 frags = fsbtofrags(fs, bb);
1131 if (bpp)
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) {
1143 if (bpp) {
1144 /* Note the new address */
1145 bp->b_blkno = UNWRITTEN;
1148 switch (num) {
1149 case 0:
1150 ip->i_ffs1_db[lbn] = UNWRITTEN;
1151 break;
1152 case 1:
1153 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
1154 break;
1155 default:
1156 idp = &indirs[num - 1];
1157 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
1158 0, &ibp))
1159 panic("lfs_balloc: bread bno %lld",
1160 (long long)idp->in_lbn);
1161 /* XXX ondisk32 */
1162 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
1163 VOP_BWRITE(ibp);
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;
1173 else {
1175 * We need to read the block to preserve the
1176 * existing bytes.
1178 bp->b_blkno = daddr;
1179 bp->b_flags |= B_READ;
1180 VOP_STRATEGY(bp);
1181 return 0;
1185 return (0);
1189 lfs_fragextend(struct uvnode *vp, int osize, int nsize, daddr_t lbn,
1190 struct ubuf **bpp)
1192 struct inode *ip;
1193 struct lfs *fs;
1194 long bb;
1195 int error;
1196 size_t obufsize;
1198 ip = VTOI(vp);
1199 fs = ip->i_lfs;
1200 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
1201 error = 0;
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))) {
1210 brelse(*bpp, 0);
1211 goto out;
1214 fs->lfs_bfree -= bb;
1215 ip->i_lfs_effnblks += bb;
1216 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1218 if (bpp) {
1219 obufsize = (*bpp)->b_bufsize;
1220 (*bpp)->b_data = erealloc((*bpp)->b_data, nsize);
1221 (void)memset((*bpp)->b_data + osize, 0, nsize - osize);
1224 out:
1225 return (error);