xref: /freebsd/sbin/fsck_ffs/suj.c (revision 51015e6d0f570239b0c2088dc6cf2b018928375d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/disk.h>
34 #include <sys/disklabel.h>
35 #include <sys/mount.h>
36 #include <sys/stat.h>
37 
38 #include <ufs/ufs/extattr.h>
39 #include <ufs/ufs/quota.h>
40 #include <ufs/ufs/ufsmount.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
43 #include <ufs/ffs/fs.h>
44 
45 #include <assert.h>
46 #include <err.h>
47 #include <setjmp.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <stdint.h>
52 #include <libufs.h>
53 #include <string.h>
54 #include <strings.h>
55 #include <sysexits.h>
56 #include <time.h>
57 
58 #include "fsck.h"
59 
60 #define	DOTDOT_OFFSET	DIRECTSIZ(1)
61 
62 struct suj_seg {
63 	TAILQ_ENTRY(suj_seg) ss_next;
64 	struct jsegrec	ss_rec;
65 	uint8_t		*ss_blk;
66 };
67 
68 struct suj_rec {
69 	TAILQ_ENTRY(suj_rec) sr_next;
70 	union jrec	*sr_rec;
71 };
72 TAILQ_HEAD(srechd, suj_rec);
73 
74 struct suj_ino {
75 	LIST_ENTRY(suj_ino)	si_next;
76 	struct srechd		si_recs;
77 	struct srechd		si_newrecs;
78 	struct srechd		si_movs;
79 	struct jtrncrec		*si_trunc;
80 	ino_t			si_ino;
81 	char			si_skipparent;
82 	char			si_hasrecs;
83 	char			si_blkadj;
84 	char			si_linkadj;
85 	int			si_mode;
86 	nlink_t			si_nlinkadj;
87 	nlink_t			si_nlink;
88 	nlink_t			si_dotlinks;
89 };
90 LIST_HEAD(inohd, suj_ino);
91 
92 struct suj_blk {
93 	LIST_ENTRY(suj_blk)	sb_next;
94 	struct srechd		sb_recs;
95 	ufs2_daddr_t		sb_blk;
96 };
97 LIST_HEAD(blkhd, suj_blk);
98 
99 struct suj_cg {
100 	LIST_ENTRY(suj_cg)	sc_next;
101 	struct blkhd		sc_blkhash[HASHSIZE];
102 	struct inohd		sc_inohash[HASHSIZE];
103 	struct ino_blk		*sc_lastiblk;
104 	struct suj_ino		*sc_lastino;
105 	struct suj_blk		*sc_lastblk;
106 	struct bufarea		*sc_cgbp;
107 	struct cg		*sc_cgp;
108 	int			sc_cgx;
109 };
110 
111 static LIST_HEAD(cghd, suj_cg) cghash[HASHSIZE];
112 static struct suj_cg *lastcg;
113 
114 static TAILQ_HEAD(seghd, suj_seg) allsegs;
115 static uint64_t oldseq;
116 static struct fs *fs = NULL;
117 static ino_t sujino;
118 
119 /*
120  * Summary statistics.
121  */
122 static uint64_t freefrags;
123 static uint64_t freeblocks;
124 static uint64_t freeinos;
125 static uint64_t freedir;
126 static uint64_t jbytes;
127 static uint64_t jrecs;
128 
129 static jmp_buf	jmpbuf;
130 
131 typedef void (*ino_visitor)(ino_t, ufs_lbn_t, ufs2_daddr_t, int);
132 static void err_suj(const char *, ...) __dead2;
133 static void ino_trunc(ino_t, off_t);
134 static void ino_decr(ino_t);
135 static void ino_adjust(struct suj_ino *);
136 static void ino_build(struct suj_ino *);
137 static int blk_isfree(ufs2_daddr_t);
138 static void initsuj(void);
139 
140 static void *
141 errmalloc(size_t n)
142 {
143 	void *a;
144 
145 	a = Malloc(n);
146 	if (a == NULL)
147 		err(EX_OSERR, "malloc(%zu)", n);
148 	return (a);
149 }
150 
151 /*
152  * When hit a fatal error in journalling check, print out
153  * the error and then offer to fallback to normal fsck.
154  */
155 static void
156 err_suj(const char * restrict fmt, ...)
157 {
158 	va_list ap;
159 
160 	if (preen)
161 		(void)fprintf(stdout, "%s: ", cdevname);
162 
163 	va_start(ap, fmt);
164 	(void)vfprintf(stdout, fmt, ap);
165 	va_end(ap);
166 
167 	longjmp(jmpbuf, -1);
168 }
169 
170 /*
171  * Lookup a cg by number in the hash so we can keep track of which cgs
172  * need stats rebuilt.
173  */
174 static struct suj_cg *
175 cg_lookup(int cgx)
176 {
177 	struct cghd *hd;
178 	struct suj_cg *sc;
179 	struct bufarea *cgbp;
180 
181 	if (cgx < 0 || cgx >= fs->fs_ncg)
182 		err_suj("Bad cg number %d\n", cgx);
183 	if (lastcg && lastcg->sc_cgx == cgx)
184 		return (lastcg);
185 	cgbp = cglookup(cgx);
186 	if (!check_cgmagic(cgx, cgbp, 0))
187 		err_suj("UNABLE TO REBUILD CYLINDER GROUP %d", cgx);
188 	hd = &cghash[HASH(cgx)];
189 	LIST_FOREACH(sc, hd, sc_next)
190 		if (sc->sc_cgx == cgx) {
191 			sc->sc_cgbp = cgbp;
192 			sc->sc_cgp = sc->sc_cgbp->b_un.b_cg;
193 			lastcg = sc;
194 			return (sc);
195 		}
196 	sc = errmalloc(sizeof(*sc));
197 	bzero(sc, sizeof(*sc));
198 	sc->sc_cgbp = cgbp;
199 	sc->sc_cgp = sc->sc_cgbp->b_un.b_cg;
200 	sc->sc_cgx = cgx;
201 	LIST_INSERT_HEAD(hd, sc, sc_next);
202 	return (sc);
203 }
204 
205 /*
206  * Lookup an inode number in the hash and allocate a suj_ino if it does
207  * not exist.
208  */
209 static struct suj_ino *
210 ino_lookup(ino_t ino, int creat)
211 {
212 	struct suj_ino *sino;
213 	struct inohd *hd;
214 	struct suj_cg *sc;
215 
216 	sc = cg_lookup(ino_to_cg(fs, ino));
217 	if (sc->sc_lastino && sc->sc_lastino->si_ino == ino)
218 		return (sc->sc_lastino);
219 	hd = &sc->sc_inohash[HASH(ino)];
220 	LIST_FOREACH(sino, hd, si_next)
221 		if (sino->si_ino == ino)
222 			return (sino);
223 	if (creat == 0)
224 		return (NULL);
225 	sino = errmalloc(sizeof(*sino));
226 	bzero(sino, sizeof(*sino));
227 	sino->si_ino = ino;
228 	TAILQ_INIT(&sino->si_recs);
229 	TAILQ_INIT(&sino->si_newrecs);
230 	TAILQ_INIT(&sino->si_movs);
231 	LIST_INSERT_HEAD(hd, sino, si_next);
232 
233 	return (sino);
234 }
235 
236 /*
237  * Lookup a block number in the hash and allocate a suj_blk if it does
238  * not exist.
239  */
240 static struct suj_blk *
241 blk_lookup(ufs2_daddr_t blk, int creat)
242 {
243 	struct suj_blk *sblk;
244 	struct suj_cg *sc;
245 	struct blkhd *hd;
246 
247 	sc = cg_lookup(dtog(fs, blk));
248 	if (sc->sc_lastblk && sc->sc_lastblk->sb_blk == blk)
249 		return (sc->sc_lastblk);
250 	hd = &sc->sc_blkhash[HASH(fragstoblks(fs, blk))];
251 	LIST_FOREACH(sblk, hd, sb_next)
252 		if (sblk->sb_blk == blk)
253 			return (sblk);
254 	if (creat == 0)
255 		return (NULL);
256 	sblk = errmalloc(sizeof(*sblk));
257 	bzero(sblk, sizeof(*sblk));
258 	sblk->sb_blk = blk;
259 	TAILQ_INIT(&sblk->sb_recs);
260 	LIST_INSERT_HEAD(hd, sblk, sb_next);
261 
262 	return (sblk);
263 }
264 
265 static int
266 blk_overlaps(struct jblkrec *brec, ufs2_daddr_t start, int frags)
267 {
268 	ufs2_daddr_t bstart;
269 	ufs2_daddr_t bend;
270 	ufs2_daddr_t end;
271 
272 	end = start + frags;
273 	bstart = brec->jb_blkno + brec->jb_oldfrags;
274 	bend = bstart + brec->jb_frags;
275 	if (start < bend && end > bstart)
276 		return (1);
277 	return (0);
278 }
279 
280 static int
281 blk_equals(struct jblkrec *brec, ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t start,
282     int frags)
283 {
284 
285 	if (brec->jb_ino != ino || brec->jb_lbn != lbn)
286 		return (0);
287 	if (brec->jb_blkno + brec->jb_oldfrags != start)
288 		return (0);
289 	if (brec->jb_frags < frags)
290 		return (0);
291 	return (1);
292 }
293 
294 static void
295 blk_setmask(struct jblkrec *brec, int *mask)
296 {
297 	int i;
298 
299 	for (i = brec->jb_oldfrags; i < brec->jb_oldfrags + brec->jb_frags; i++)
300 		*mask |= 1 << i;
301 }
302 
303 /*
304  * Determine whether a given block has been reallocated to a new location.
305  * Returns a mask of overlapping bits if any frags have been reused or
306  * zero if the block has not been re-used and the contents can be trusted.
307  *
308  * This is used to ensure that an orphaned pointer due to truncate is safe
309  * to be freed.  The mask value can be used to free partial blocks.
310  */
311 static int
312 blk_freemask(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags)
313 {
314 	struct suj_blk *sblk;
315 	struct suj_rec *srec;
316 	struct jblkrec *brec;
317 	int mask;
318 	int off;
319 
320 	/*
321 	 * To be certain we're not freeing a reallocated block we lookup
322 	 * this block in the blk hash and see if there is an allocation
323 	 * journal record that overlaps with any fragments in the block
324 	 * we're concerned with.  If any fragments have been reallocated
325 	 * the block has already been freed and re-used for another purpose.
326 	 */
327 	mask = 0;
328 	sblk = blk_lookup(blknum(fs, blk), 0);
329 	if (sblk == NULL)
330 		return (0);
331 	off = blk - sblk->sb_blk;
332 	TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
333 		brec = (struct jblkrec *)srec->sr_rec;
334 		/*
335 		 * If the block overlaps but does not match
336 		 * exactly this record refers to the current
337 		 * location.
338 		 */
339 		if (blk_overlaps(brec, blk, frags) == 0)
340 			continue;
341 		if (blk_equals(brec, ino, lbn, blk, frags) == 1)
342 			mask = 0;
343 		else
344 			blk_setmask(brec, &mask);
345 	}
346 	if (debug)
347 		printf("blk_freemask: blk %jd sblk %jd off %d mask 0x%X\n",
348 		    blk, sblk->sb_blk, off, mask);
349 	return (mask >> off);
350 }
351 
352 /*
353  * Determine whether it is safe to follow an indirect.  It is not safe
354  * if any part of the indirect has been reallocated or the last journal
355  * entry was an allocation.  Just allocated indirects may not have valid
356  * pointers yet and all of their children will have their own records.
357  * It is also not safe to follow an indirect if the cg bitmap has been
358  * cleared as a new allocation may write to the block prior to the journal
359  * being written.
360  *
361  * Returns 1 if it's safe to follow the indirect and 0 otherwise.
362  */
363 static int
364 blk_isindir(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn)
365 {
366 	struct suj_blk *sblk;
367 	struct jblkrec *brec;
368 
369 	sblk = blk_lookup(blk, 0);
370 	if (sblk == NULL)
371 		return (1);
372 	if (TAILQ_EMPTY(&sblk->sb_recs))
373 		return (1);
374 	brec = (struct jblkrec *)TAILQ_LAST(&sblk->sb_recs, srechd)->sr_rec;
375 	if (blk_equals(brec, ino, lbn, blk, fs->fs_frag))
376 		if (brec->jb_op == JOP_FREEBLK)
377 			return (!blk_isfree(blk));
378 	return (0);
379 }
380 
381 /*
382  * Check to see if the requested block is available.
383  * We can just check in the cylinder-group maps as
384  * they will only have usable blocks in them.
385  */
386 ufs2_daddr_t
387 suj_checkblkavail(blkno, frags)
388 	ufs2_daddr_t blkno;
389 	long frags;
390 {
391 	struct bufarea *cgbp;
392 	struct cg *cgp;
393 	ufs2_daddr_t j, k, baseblk;
394 	long cg;
395 
396 	if ((u_int64_t)blkno > sblock.fs_size)
397 		return (0);
398 	cg = dtog(&sblock, blkno);
399 	cgbp = cglookup(cg);
400 	cgp = cgbp->b_un.b_cg;
401 	if (!check_cgmagic(cg, cgbp, 0))
402 		return (-((cg + 1) * sblock.fs_fpg - sblock.fs_frag));
403 	baseblk = dtogd(&sblock, blkno);
404 	for (j = 0; j <= sblock.fs_frag - frags; j++) {
405 		if (!isset(cg_blksfree(cgp), baseblk + j))
406 			continue;
407 		for (k = 1; k < frags; k++)
408 			if (!isset(cg_blksfree(cgp), baseblk + j + k))
409 				break;
410 		if (k < frags) {
411 			j += k;
412 			continue;
413 		}
414 		for (k = 0; k < frags; k++)
415 			clrbit(cg_blksfree(cgp), baseblk + j + k);
416 		n_blks += frags;
417 		if (frags == sblock.fs_frag)
418 			cgp->cg_cs.cs_nbfree--;
419 		else
420 			cgp->cg_cs.cs_nffree -= frags;
421 		cgdirty(cgbp);
422 		return ((cg * sblock.fs_fpg) + baseblk + j);
423 	}
424 	return (0);
425 }
426 
427 /*
428  * Clear an inode from the cg bitmap.  If the inode was already clear return
429  * 0 so the caller knows it does not have to check the inode contents.
430  */
431 static int
432 ino_free(ino_t ino, int mode)
433 {
434 	struct suj_cg *sc;
435 	uint8_t *inosused;
436 	struct cg *cgp;
437 	int cg;
438 
439 	cg = ino_to_cg(fs, ino);
440 	ino = ino % fs->fs_ipg;
441 	sc = cg_lookup(cg);
442 	cgp = sc->sc_cgp;
443 	inosused = cg_inosused(cgp);
444 	/*
445 	 * The bitmap may never have made it to the disk so we have to
446 	 * conditionally clear.  We can avoid writing the cg in this case.
447 	 */
448 	if (isclr(inosused, ino))
449 		return (0);
450 	freeinos++;
451 	clrbit(inosused, ino);
452 	if (ino < cgp->cg_irotor)
453 		cgp->cg_irotor = ino;
454 	cgp->cg_cs.cs_nifree++;
455 	if ((mode & IFMT) == IFDIR) {
456 		freedir++;
457 		cgp->cg_cs.cs_ndir--;
458 	}
459 	cgdirty(sc->sc_cgbp);
460 
461 	return (1);
462 }
463 
464 /*
465  * Free 'frags' frags starting at filesystem block 'bno' skipping any frags
466  * set in the mask.
467  */
468 static void
469 blk_free(ino_t ino, ufs2_daddr_t bno, int mask, int frags)
470 {
471 	ufs1_daddr_t fragno, cgbno;
472 	struct suj_cg *sc;
473 	struct cg *cgp;
474 	int i, cg;
475 	uint8_t *blksfree;
476 
477 	if (debug)
478 		printf("Freeing %d frags at blk %jd mask 0x%x\n",
479 		    frags, bno, mask);
480 	/*
481 	 * Check to see if the block needs to be claimed by a snapshot.
482 	 * If wanted, the snapshot references it. Otherwise we free it.
483 	 */
484 	if (snapblkfree(fs, bno, lfragtosize(fs, frags), ino,
485 	    suj_checkblkavail))
486 		return;
487 	cg = dtog(fs, bno);
488 	sc = cg_lookup(cg);
489 	cgp = sc->sc_cgp;
490 	cgbno = dtogd(fs, bno);
491 	blksfree = cg_blksfree(cgp);
492 
493 	/*
494 	 * If it's not allocated we only wrote the journal entry
495 	 * and never the bitmaps.  Here we unconditionally clear and
496 	 * resolve the cg summary later.
497 	 */
498 	if (frags == fs->fs_frag && mask == 0) {
499 		fragno = fragstoblks(fs, cgbno);
500 		ffs_setblock(fs, blksfree, fragno);
501 		freeblocks++;
502 	} else {
503 		/*
504 		 * deallocate the fragment
505 		 */
506 		for (i = 0; i < frags; i++)
507 			if ((mask & (1 << i)) == 0 && isclr(blksfree, cgbno +i)) {
508 				freefrags++;
509 				setbit(blksfree, cgbno + i);
510 			}
511 	}
512 	cgdirty(sc->sc_cgbp);
513 }
514 
515 /*
516  * Returns 1 if the whole block starting at 'bno' is marked free and 0
517  * otherwise.
518  */
519 static int
520 blk_isfree(ufs2_daddr_t bno)
521 {
522 	struct suj_cg *sc;
523 
524 	sc = cg_lookup(dtog(fs, bno));
525 	return ffs_isblock(fs, cg_blksfree(sc->sc_cgp), dtogd(fs, bno));
526 }
527 
528 /*
529  * Determine whether a block exists at a particular lbn in an inode.
530  * Returns 1 if found, 0 if not.  lbn may be negative for indirects
531  * or ext blocks.
532  */
533 static int
534 blk_isat(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int *frags)
535 {
536 	struct inode ip;
537 	union dinode *dp;
538 	ufs2_daddr_t nblk;
539 
540 	ginode(ino, &ip);
541 	dp = ip.i_dp;
542 	if (DIP(dp, di_nlink) == 0 || DIP(dp, di_mode) == 0) {
543 		irelse(&ip);
544 		return (0);
545 	}
546 	nblk = ino_blkatoff(dp, ino, lbn, frags, NULL);
547 	irelse(&ip);
548 	return (nblk == blk);
549 }
550 
551 /*
552  * Clear the directory entry at diroff that should point to child.  Minimal
553  * checking is done and it is assumed that this path was verified with isat.
554  */
555 static void
556 ino_clrat(ino_t parent, off_t diroff, ino_t child)
557 {
558 	union dinode *dip;
559 	struct direct *dp;
560 	struct inode ip;
561 	ufs2_daddr_t blk;
562 	struct bufarea *bp;
563 	ufs_lbn_t lbn;
564 	int blksize;
565 	int frags;
566 	int doff;
567 
568 	if (debug)
569 		printf("Clearing inode %ju from parent %ju at offset %jd\n",
570 		    (uintmax_t)child, (uintmax_t)parent, diroff);
571 
572 	lbn = lblkno(fs, diroff);
573 	doff = blkoff(fs, diroff);
574 	ginode(parent, &ip);
575 	dip = ip.i_dp;
576 	blk = ino_blkatoff(dip, parent, lbn, &frags, NULL);
577 	blksize = sblksize(fs, DIP(dip, di_size), lbn);
578 	irelse(&ip);
579 	bp = getdatablk(blk, blksize, BT_DIRDATA);
580 	if (bp->b_errs != 0)
581 		err_suj("ino_clrat: UNRECOVERABLE I/O ERROR");
582 	dp = (struct direct *)&bp->b_un.b_buf[doff];
583 	if (dp->d_ino != child)
584 		errx(1, "Inode %ju does not exist in %ju at %jd",
585 		    (uintmax_t)child, (uintmax_t)parent, diroff);
586 	dp->d_ino = 0;
587 	dirty(bp);
588 	brelse(bp);
589 	/*
590 	 * The actual .. reference count will already have been removed
591 	 * from the parent by the .. remref record.
592 	 */
593 }
594 
595 /*
596  * Determines whether a pointer to an inode exists within a directory
597  * at a specified offset.  Returns the mode of the found entry.
598  */
599 static int
600 ino_isat(ino_t parent, off_t diroff, ino_t child, int *mode, int *isdot)
601 {
602 	struct inode ip;
603 	union dinode *dip;
604 	struct bufarea *bp;
605 	struct direct *dp;
606 	ufs2_daddr_t blk;
607 	ufs_lbn_t lbn;
608 	int blksize;
609 	int frags;
610 	int dpoff;
611 	int doff;
612 
613 	*isdot = 0;
614 	ginode(parent, &ip);
615 	dip = ip.i_dp;
616 	*mode = DIP(dip, di_mode);
617 	if ((*mode & IFMT) != IFDIR) {
618 		if (debug) {
619 			/*
620 			 * This can happen if the parent inode
621 			 * was reallocated.
622 			 */
623 			if (*mode != 0)
624 				printf("Directory %ju has bad mode %o\n",
625 				    (uintmax_t)parent, *mode);
626 			else
627 				printf("Directory %ju has zero mode\n",
628 				    (uintmax_t)parent);
629 		}
630 		irelse(&ip);
631 		return (0);
632 	}
633 	lbn = lblkno(fs, diroff);
634 	doff = blkoff(fs, diroff);
635 	blksize = sblksize(fs, DIP(dip, di_size), lbn);
636 	if (diroff + DIRECTSIZ(1) > DIP(dip, di_size) || doff >= blksize) {
637 		if (debug)
638 			printf("ino %ju absent from %ju due to offset %jd"
639 			    " exceeding size %jd\n",
640 			    (uintmax_t)child, (uintmax_t)parent, diroff,
641 			    DIP(dip, di_size));
642 		irelse(&ip);
643 		return (0);
644 	}
645 	blk = ino_blkatoff(dip, parent, lbn, &frags, NULL);
646 	irelse(&ip);
647 	if (blk <= 0) {
648 		if (debug)
649 			printf("Sparse directory %ju", (uintmax_t)parent);
650 		return (0);
651 	}
652 	bp = getdatablk(blk, blksize, BT_DIRDATA);
653 	if (bp->b_errs != 0)
654 		err_suj("ino_isat: UNRECOVERABLE I/O ERROR");
655 	/*
656 	 * Walk through the records from the start of the block to be
657 	 * certain we hit a valid record and not some junk in the middle
658 	 * of a file name.  Stop when we reach or pass the expected offset.
659 	 */
660 	dpoff = rounddown(doff, DIRBLKSIZ);
661 	do {
662 		dp = (struct direct *)&bp->b_un.b_buf[dpoff];
663 		if (dpoff == doff)
664 			break;
665 		if (dp->d_reclen == 0)
666 			break;
667 		dpoff += dp->d_reclen;
668 	} while (dpoff <= doff);
669 	if (dpoff > fs->fs_bsize)
670 		err_suj("Corrupt directory block in dir ino %ju\n",
671 		    (uintmax_t)parent);
672 	/* Not found. */
673 	if (dpoff != doff) {
674 		if (debug)
675 			printf("ino %ju not found in %ju, lbn %jd, dpoff %d\n",
676 			    (uintmax_t)child, (uintmax_t)parent, lbn, dpoff);
677 		brelse(bp);
678 		return (0);
679 	}
680 	/*
681 	 * We found the item in question.  Record the mode and whether it's
682 	 * a . or .. link for the caller.
683 	 */
684 	if (dp->d_ino == child) {
685 		if (child == parent)
686 			*isdot = 1;
687 		else if (dp->d_namlen == 2 &&
688 		    dp->d_name[0] == '.' && dp->d_name[1] == '.')
689 			*isdot = 1;
690 		*mode = DTTOIF(dp->d_type);
691 		brelse(bp);
692 		return (1);
693 	}
694 	if (debug)
695 		printf("ino %ju doesn't match dirent ino %ju in parent %ju\n",
696 		    (uintmax_t)child, (uintmax_t)dp->d_ino, (uintmax_t)parent);
697 	brelse(bp);
698 	return (0);
699 }
700 
701 #define	VISIT_INDIR	0x0001
702 #define	VISIT_EXT	0x0002
703 #define	VISIT_ROOT	0x0004	/* Operation came via root & valid pointers. */
704 
705 /*
706  * Read an indirect level which may or may not be linked into an inode.
707  */
708 static void
709 indir_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, uint64_t *frags,
710     ino_visitor visitor, int flags)
711 {
712 	struct bufarea *bp;
713 	ufs_lbn_t lbnadd;
714 	ufs2_daddr_t nblk;
715 	ufs_lbn_t nlbn;
716 	int level;
717 	int i;
718 
719 	/*
720 	 * Don't visit indirect blocks with contents we can't trust.  This
721 	 * should only happen when indir_visit() is called to complete a
722 	 * truncate that never finished and not when a pointer is found via
723 	 * an inode.
724 	 */
725 	if (blk == 0)
726 		return;
727 	level = lbn_level(lbn);
728 	if (level == -1)
729 		err_suj("Invalid level for lbn %jd\n", lbn);
730 	if ((flags & VISIT_ROOT) == 0 && blk_isindir(blk, ino, lbn) == 0) {
731 		if (debug)
732 			printf("blk %jd ino %ju lbn %jd(%d) is not indir.\n",
733 			    blk, (uintmax_t)ino, lbn, level);
734 		goto out;
735 	}
736 	lbnadd = 1;
737 	for (i = level; i > 0; i--)
738 		lbnadd *= NINDIR(fs);
739 	bp = getdatablk(blk, fs->fs_bsize, BT_LEVEL1 + level);
740 	if (bp->b_errs != 0)
741 		err_suj("indir_visit: UNRECOVERABLE I/O ERROR");
742 	for (i = 0; i < NINDIR(fs); i++) {
743 		if ((nblk = IBLK(bp, i)) == 0)
744 			continue;
745 		if (level == 0) {
746 			nlbn = -lbn + i * lbnadd;
747 			(*frags) += fs->fs_frag;
748 			visitor(ino, nlbn, nblk, fs->fs_frag);
749 		} else {
750 			nlbn = (lbn + 1) - (i * lbnadd);
751 			indir_visit(ino, nlbn, nblk, frags, visitor, flags);
752 		}
753 	}
754 	brelse(bp);
755 out:
756 	if (flags & VISIT_INDIR) {
757 		(*frags) += fs->fs_frag;
758 		visitor(ino, lbn, blk, fs->fs_frag);
759 	}
760 }
761 
762 /*
763  * Visit each block in an inode as specified by 'flags' and call a
764  * callback function.  The callback may inspect or free blocks.  The
765  * count of frags found according to the size in the file is returned.
766  * This is not valid for sparse files but may be used to determine
767  * the correct di_blocks for a file.
768  */
769 static uint64_t
770 ino_visit(union dinode *dp, ino_t ino, ino_visitor visitor, int flags)
771 {
772 	ufs_lbn_t nextlbn;
773 	ufs_lbn_t tmpval;
774 	ufs_lbn_t lbn;
775 	uint64_t size;
776 	uint64_t fragcnt;
777 	int mode;
778 	int frags;
779 	int i;
780 
781 	size = DIP(dp, di_size);
782 	mode = DIP(dp, di_mode) & IFMT;
783 	fragcnt = 0;
784 	if ((flags & VISIT_EXT) &&
785 	    fs->fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize) {
786 		for (i = 0; i < UFS_NXADDR; i++) {
787 			if (dp->dp2.di_extb[i] == 0)
788 				continue;
789 			frags = sblksize(fs, dp->dp2.di_extsize, i);
790 			frags = numfrags(fs, frags);
791 			fragcnt += frags;
792 			visitor(ino, -1 - i, dp->dp2.di_extb[i], frags);
793 		}
794 	}
795 	/* Skip datablocks for short links and devices. */
796 	if (mode == IFBLK || mode == IFCHR ||
797 	    (mode == IFLNK && size < fs->fs_maxsymlinklen))
798 		return (fragcnt);
799 	for (i = 0; i < UFS_NDADDR; i++) {
800 		if (DIP(dp, di_db[i]) == 0)
801 			continue;
802 		frags = sblksize(fs, size, i);
803 		frags = numfrags(fs, frags);
804 		fragcnt += frags;
805 		visitor(ino, i, DIP(dp, di_db[i]), frags);
806 	}
807 	/*
808 	 * We know the following indirects are real as we're following
809 	 * real pointers to them.
810 	 */
811 	flags |= VISIT_ROOT;
812 	for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; i < UFS_NIADDR; i++,
813 	    lbn = nextlbn) {
814 		nextlbn = lbn + tmpval;
815 		tmpval *= NINDIR(fs);
816 		if (DIP(dp, di_ib[i]) == 0)
817 			continue;
818 		indir_visit(ino, -lbn - i, DIP(dp, di_ib[i]), &fragcnt, visitor,
819 		    flags);
820 	}
821 	return (fragcnt);
822 }
823 
824 /*
825  * Null visitor function used when we just want to count blocks and
826  * record the lbn.
827  */
828 ufs_lbn_t visitlbn;
829 static void
830 null_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
831 {
832 	if (lbn > 0)
833 		visitlbn = lbn;
834 }
835 
836 /*
837  * Recalculate di_blocks when we discover that a block allocation or
838  * free was not successfully completed.  The kernel does not roll this back
839  * because it would be too expensive to compute which indirects were
840  * reachable at the time the inode was written.
841  */
842 static void
843 ino_adjblks(struct suj_ino *sino)
844 {
845 	struct inode ip;
846 	union dinode *dp;
847 	uint64_t blocks;
848 	uint64_t frags;
849 	off_t isize;
850 	off_t size;
851 	ino_t ino;
852 
853 	ino = sino->si_ino;
854 	ginode(ino, &ip);
855 	dp = ip.i_dp;
856 	/* No need to adjust zero'd inodes. */
857 	if (DIP(dp, di_mode) == 0) {
858 		irelse(&ip);
859 		return;
860 	}
861 	/*
862 	 * Visit all blocks and count them as well as recording the last
863 	 * valid lbn in the file.  If the file size doesn't agree with the
864 	 * last lbn we need to truncate to fix it.  Otherwise just adjust
865 	 * the blocks count.
866 	 */
867 	visitlbn = 0;
868 	frags = ino_visit(dp, ino, null_visit, VISIT_INDIR | VISIT_EXT);
869 	blocks = fsbtodb(fs, frags);
870 	/*
871 	 * We assume the size and direct block list is kept coherent by
872 	 * softdep.  For files that have extended into indirects we truncate
873 	 * to the size in the inode or the maximum size permitted by
874 	 * populated indirects.
875 	 */
876 	if (visitlbn >= UFS_NDADDR) {
877 		isize = DIP(dp, di_size);
878 		size = lblktosize(fs, visitlbn + 1);
879 		if (isize > size)
880 			isize = size;
881 		/* Always truncate to free any unpopulated indirects. */
882 		ino_trunc(ino, isize);
883 		irelse(&ip);
884 		return;
885 	}
886 	if (blocks == DIP(dp, di_blocks)) {
887 		irelse(&ip);
888 		return;
889 	}
890 	if (debug)
891 		printf("ino %ju adjusting block count from %jd to %jd\n",
892 		    (uintmax_t)ino, DIP(dp, di_blocks), blocks);
893 	DIP_SET(dp, di_blocks, blocks);
894 	inodirty(&ip);
895 	irelse(&ip);
896 }
897 
898 static void
899 blk_free_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
900 {
901 
902 	blk_free(ino, blk, blk_freemask(blk, ino, lbn, frags), frags);
903 }
904 
905 /*
906  * Free a block or tree of blocks that was previously rooted in ino at
907  * the given lbn.  If the lbn is an indirect all children are freed
908  * recursively.
909  */
910 static void
911 blk_free_lbn(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags, int follow)
912 {
913 	uint64_t resid;
914 	int mask;
915 
916 	mask = blk_freemask(blk, ino, lbn, frags);
917 	resid = 0;
918 	if (lbn <= -UFS_NDADDR && follow && mask == 0)
919 		indir_visit(ino, lbn, blk, &resid, blk_free_visit, VISIT_INDIR);
920 	else
921 		blk_free(ino, blk, mask, frags);
922 }
923 
924 static void
925 ino_setskip(struct suj_ino *sino, ino_t parent)
926 {
927 	int isdot;
928 	int mode;
929 
930 	if (ino_isat(sino->si_ino, DOTDOT_OFFSET, parent, &mode, &isdot))
931 		sino->si_skipparent = 1;
932 }
933 
934 static void
935 ino_remref(ino_t parent, ino_t child, uint64_t diroff, int isdotdot)
936 {
937 	struct suj_ino *sino;
938 	struct suj_rec *srec;
939 	struct jrefrec *rrec;
940 
941 	/*
942 	 * Lookup this inode to see if we have a record for it.
943 	 */
944 	sino = ino_lookup(child, 0);
945 	/*
946 	 * Tell any child directories we've already removed their
947 	 * parent link cnt.  Don't try to adjust our link down again.
948 	 */
949 	if (sino != NULL && isdotdot == 0)
950 		ino_setskip(sino, parent);
951 	/*
952 	 * No valid record for this inode.  Just drop the on-disk
953 	 * link by one.
954 	 */
955 	if (sino == NULL || sino->si_hasrecs == 0) {
956 		ino_decr(child);
957 		return;
958 	}
959 	/*
960 	 * Use ino_adjust() if ino_check() has already processed this
961 	 * child.  If we lose the last non-dot reference to a
962 	 * directory it will be discarded.
963 	 */
964 	if (sino->si_linkadj) {
965 		if (sino->si_nlink == 0)
966 			err_suj("ino_remref: ino %ld mode 0%o about to go "
967 			    "negative\n", sino->si_ino, sino->si_mode);
968 		sino->si_nlink--;
969 		if (isdotdot)
970 			sino->si_dotlinks--;
971 		ino_adjust(sino);
972 		return;
973 	}
974 	/*
975 	 * If we haven't yet processed this inode we need to make
976 	 * sure we will successfully discover the lost path.  If not
977 	 * use nlinkadj to remember.
978 	 */
979 	TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
980 		rrec = (struct jrefrec *)srec->sr_rec;
981 		if (rrec->jr_parent == parent &&
982 		    rrec->jr_diroff == diroff)
983 			return;
984 	}
985 	sino->si_nlinkadj++;
986 }
987 
988 /*
989  * Free the children of a directory when the directory is discarded.
990  */
991 static void
992 ino_free_children(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
993 {
994 	struct suj_ino *sino;
995 	struct bufarea *bp;
996 	struct direct *dp;
997 	off_t diroff;
998 	int skipparent;
999 	int isdotdot;
1000 	int dpoff;
1001 	int size;
1002 
1003 	sino = ino_lookup(ino, 0);
1004 	if (sino)
1005 		skipparent = sino->si_skipparent;
1006 	else
1007 		skipparent = 0;
1008 	size = lfragtosize(fs, frags);
1009 	bp = getdatablk(blk, size, BT_DIRDATA);
1010 	if (bp->b_errs != 0)
1011 		err_suj("ino_free_children: UNRECOVERABLE I/O ERROR");
1012 	dp = (struct direct *)&bp->b_un.b_buf[0];
1013 	for (dpoff = 0; dpoff < size && dp->d_reclen; dpoff += dp->d_reclen) {
1014 		dp = (struct direct *)&bp->b_un.b_buf[dpoff];
1015 		if (dp->d_ino == 0 || dp->d_ino == UFS_WINO)
1016 			continue;
1017 		if (dp->d_namlen == 1 && dp->d_name[0] == '.')
1018 			continue;
1019 		isdotdot = dp->d_namlen == 2 && dp->d_name[0] == '.' &&
1020 		    dp->d_name[1] == '.';
1021 		if (isdotdot && skipparent == 1)
1022 			continue;
1023 		if (debug)
1024 			printf("Directory %ju removing ino %ju name %s\n",
1025 			    (uintmax_t)ino, (uintmax_t)dp->d_ino, dp->d_name);
1026 		diroff = lblktosize(fs, lbn) + dpoff;
1027 		ino_remref(ino, dp->d_ino, diroff, isdotdot);
1028 	}
1029 	brelse(bp);
1030 }
1031 
1032 /*
1033  * Reclaim an inode, freeing all blocks and decrementing all children's
1034  * link counts.  Free the inode back to the cg.
1035  */
1036 static void
1037 ino_reclaim(struct inode *ip, ino_t ino, int mode)
1038 {
1039 	union dinode *dp;
1040 	uint32_t gen;
1041 
1042 	dp = ip->i_dp;
1043 	if (ino == UFS_ROOTINO)
1044 		err_suj("Attempting to free UFS_ROOTINO\n");
1045 	if (debug)
1046 		printf("Truncating and freeing ino %ju, nlink %d, mode %o\n",
1047 		    (uintmax_t)ino, DIP(dp, di_nlink), DIP(dp, di_mode));
1048 
1049 	/* We are freeing an inode or directory. */
1050 	if ((DIP(dp, di_mode) & IFMT) == IFDIR)
1051 		ino_visit(dp, ino, ino_free_children, 0);
1052 	DIP_SET(dp, di_nlink, 0);
1053 	if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0)
1054 		snapremove(ino);
1055 	ino_visit(dp, ino, blk_free_visit, VISIT_EXT | VISIT_INDIR);
1056 	/* Here we have to clear the inode and release any blocks it holds. */
1057 	gen = DIP(dp, di_gen);
1058 	if (fs->fs_magic == FS_UFS1_MAGIC)
1059 		bzero(dp, sizeof(struct ufs1_dinode));
1060 	else
1061 		bzero(dp, sizeof(struct ufs2_dinode));
1062 	DIP_SET(dp, di_gen, gen);
1063 	inodirty(ip);
1064 	ino_free(ino, mode);
1065 	return;
1066 }
1067 
1068 /*
1069  * Adjust an inode's link count down by one when a directory goes away.
1070  */
1071 static void
1072 ino_decr(ino_t ino)
1073 {
1074 	struct inode ip;
1075 	union dinode *dp;
1076 	int reqlink;
1077 	int nlink;
1078 	int mode;
1079 
1080 	ginode(ino, &ip);
1081 	dp = ip.i_dp;
1082 	nlink = DIP(dp, di_nlink);
1083 	mode = DIP(dp, di_mode);
1084 	if (nlink < 1)
1085 		err_suj("Inode %d link count %d invalid\n", ino, nlink);
1086 	if (mode == 0)
1087 		err_suj("Inode %d has a link of %d with 0 mode\n", ino, nlink);
1088 	nlink--;
1089 	if ((mode & IFMT) == IFDIR)
1090 		reqlink = 2;
1091 	else
1092 		reqlink = 1;
1093 	if (nlink < reqlink) {
1094 		if (debug)
1095 			printf("ino %ju not enough links to live %d < %d\n",
1096 			    (uintmax_t)ino, nlink, reqlink);
1097 		ino_reclaim(&ip, ino, mode);
1098 		irelse(&ip);
1099 		return;
1100 	}
1101 	DIP_SET(dp, di_nlink, nlink);
1102 	inodirty(&ip);
1103 	irelse(&ip);
1104 }
1105 
1106 /*
1107  * Adjust the inode link count to 'nlink'.  If the count reaches zero
1108  * free it.
1109  */
1110 static void
1111 ino_adjust(struct suj_ino *sino)
1112 {
1113 	struct jrefrec *rrec;
1114 	struct suj_rec *srec;
1115 	struct suj_ino *stmp;
1116 	union dinode *dp;
1117 	struct inode ip;
1118 	nlink_t nlink;
1119 	nlink_t reqlink;
1120 	int recmode;
1121 	int isdot;
1122 	int mode;
1123 	ino_t ino;
1124 
1125 	nlink = sino->si_nlink;
1126 	ino = sino->si_ino;
1127 	mode = sino->si_mode & IFMT;
1128 	/*
1129 	 * If it's a directory with no dot links, it was truncated before
1130 	 * the name was cleared.  We need to clear the dirent that
1131 	 * points at it.
1132 	 */
1133 	if (mode == IFDIR && nlink == 1 && sino->si_dotlinks == 0) {
1134 		sino->si_nlink = nlink = 0;
1135 		TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1136 			rrec = (struct jrefrec *)srec->sr_rec;
1137 			if (ino_isat(rrec->jr_parent, rrec->jr_diroff, ino,
1138 			    &recmode, &isdot) == 0)
1139 				continue;
1140 			ino_clrat(rrec->jr_parent, rrec->jr_diroff, ino);
1141 			break;
1142 		}
1143 		if (srec == NULL)
1144 			errx(1, "Directory %ju name not found", (uintmax_t)ino);
1145 	}
1146 	/*
1147 	 * If it's a directory with no real names pointing to it go ahead
1148 	 * and truncate it.  This will free any children.
1149 	 */
1150 	if (mode == IFDIR && nlink - sino->si_dotlinks == 0) {
1151 		sino->si_nlink = nlink = 0;
1152 		/*
1153 		 * Mark any .. links so they know not to free this inode
1154 		 * when they are removed.
1155 		 */
1156 		TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1157 			rrec = (struct jrefrec *)srec->sr_rec;
1158 			if (rrec->jr_diroff == DOTDOT_OFFSET) {
1159 				stmp = ino_lookup(rrec->jr_parent, 0);
1160 				if (stmp)
1161 					ino_setskip(stmp, ino);
1162 			}
1163 		}
1164 	}
1165 	ginode(ino, &ip);
1166 	dp = ip.i_dp;
1167 	mode = DIP(dp, di_mode) & IFMT;
1168 	if (nlink > UFS_LINK_MAX)
1169 		err_suj("ino %ju nlink manipulation error, new %ju, old %d\n",
1170 		    (uintmax_t)ino, (uintmax_t)nlink, DIP(dp, di_nlink));
1171 	if (debug)
1172 	       printf("Adjusting ino %ju, nlink %ju, old link %d lastmode %o\n",
1173 		    (uintmax_t)ino, (uintmax_t)nlink, DIP(dp, di_nlink),
1174 		    sino->si_mode);
1175 	if (mode == 0) {
1176 		if (debug)
1177 			printf("ino %ju, zero inode freeing bitmap\n",
1178 			    (uintmax_t)ino);
1179 		ino_free(ino, sino->si_mode);
1180 		irelse(&ip);
1181 		return;
1182 	}
1183 	/* XXX Should be an assert? */
1184 	if (mode != sino->si_mode && debug)
1185 		printf("ino %ju, mode %o != %o\n",
1186 		    (uintmax_t)ino, mode, sino->si_mode);
1187 	if ((mode & IFMT) == IFDIR)
1188 		reqlink = 2;
1189 	else
1190 		reqlink = 1;
1191 	/* If the inode doesn't have enough links to live, free it. */
1192 	if (nlink < reqlink) {
1193 		if (debug)
1194 			printf("ino %ju not enough links to live %ju < %ju\n",
1195 			    (uintmax_t)ino, (uintmax_t)nlink,
1196 			    (uintmax_t)reqlink);
1197 		ino_reclaim(&ip, ino, mode);
1198 		irelse(&ip);
1199 		return;
1200 	}
1201 	/* If required write the updated link count. */
1202 	if (DIP(dp, di_nlink) == nlink) {
1203 		if (debug)
1204 			printf("ino %ju, link matches, skipping.\n",
1205 			    (uintmax_t)ino);
1206 		irelse(&ip);
1207 		return;
1208 	}
1209 	DIP_SET(dp, di_nlink, nlink);
1210 	inodirty(&ip);
1211 	irelse(&ip);
1212 }
1213 
1214 /*
1215  * Truncate some or all blocks in an indirect, freeing any that are required
1216  * and zeroing the indirect.
1217  */
1218 static void
1219 indir_trunc(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, ufs_lbn_t lastlbn,
1220 	union dinode *dp)
1221 {
1222 	struct bufarea *bp;
1223 	ufs_lbn_t lbnadd;
1224 	ufs2_daddr_t nblk;
1225 	ufs_lbn_t next;
1226 	ufs_lbn_t nlbn;
1227 	int isdirty;
1228 	int level;
1229 	int i;
1230 
1231 	if (blk == 0)
1232 		return;
1233 	isdirty = 0;
1234 	level = lbn_level(lbn);
1235 	if (level == -1)
1236 		err_suj("Invalid level for lbn %jd\n", lbn);
1237 	lbnadd = 1;
1238 	for (i = level; i > 0; i--)
1239 		lbnadd *= NINDIR(fs);
1240 	bp = getdatablk(blk, fs->fs_bsize, BT_LEVEL1 + level);
1241 	if (bp->b_errs != 0)
1242 		err_suj("indir_trunc: UNRECOVERABLE I/O ERROR");
1243 	for (i = 0; i < NINDIR(fs); i++) {
1244 		if ((nblk = IBLK(bp, i)) == 0)
1245 			continue;
1246 		if (level != 0) {
1247 			nlbn = (lbn + 1) - (i * lbnadd);
1248 			/*
1249 			 * Calculate the lbn of the next indirect to
1250 			 * determine if any of this indirect must be
1251 			 * reclaimed.
1252 			 */
1253 			next = -(lbn + level) + ((i+1) * lbnadd);
1254 			if (next <= lastlbn)
1255 				continue;
1256 			indir_trunc(ino, nlbn, nblk, lastlbn, dp);
1257 			/* If all of this indirect was reclaimed, free it. */
1258 			nlbn = next - lbnadd;
1259 			if (nlbn < lastlbn)
1260 				continue;
1261 		} else {
1262 			nlbn = -lbn + i * lbnadd;
1263 			if (nlbn < lastlbn)
1264 				continue;
1265 		}
1266 		isdirty = 1;
1267 		blk_free(ino, nblk, 0, fs->fs_frag);
1268 		IBLK_SET(bp, i, 0);
1269 	}
1270 	if (isdirty)
1271 		dirty(bp);
1272 	brelse(bp);
1273 }
1274 
1275 /*
1276  * Truncate an inode to the minimum of the given size or the last populated
1277  * block after any over size have been discarded.  The kernel would allocate
1278  * the last block in the file but fsck does not and neither do we.  This
1279  * code never extends files, only shrinks them.
1280  */
1281 static void
1282 ino_trunc(ino_t ino, off_t size)
1283 {
1284 	struct inode ip;
1285 	union dinode *dp;
1286 	struct bufarea *bp;
1287 	ufs2_daddr_t bn;
1288 	uint64_t totalfrags;
1289 	ufs_lbn_t nextlbn;
1290 	ufs_lbn_t lastlbn;
1291 	ufs_lbn_t tmpval;
1292 	ufs_lbn_t lbn;
1293 	ufs_lbn_t i;
1294 	int blksize, frags;
1295 	off_t cursize;
1296 	off_t off;
1297 	int mode;
1298 
1299 	ginode(ino, &ip);
1300 	dp = ip.i_dp;
1301 	mode = DIP(dp, di_mode) & IFMT;
1302 	cursize = DIP(dp, di_size);
1303 	/* If no size change, nothing to do */
1304 	if (size == cursize) {
1305 		irelse(&ip);
1306 		return;
1307 	}
1308 	if (debug)
1309 		printf("Truncating ino %ju, mode %o to size %jd from size %jd\n",
1310 		    (uintmax_t)ino, mode, size, cursize);
1311 
1312 	/* Skip datablocks for short links and devices. */
1313 	if (mode == 0 || mode == IFBLK || mode == IFCHR ||
1314 	    (mode == IFLNK && cursize < fs->fs_maxsymlinklen)) {
1315 		irelse(&ip);
1316 		return;
1317 	}
1318 	/* Don't extend. */
1319 	if (size > cursize) {
1320 		irelse(&ip);
1321 		return;
1322 	}
1323 	if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) {
1324 		if (size > 0)
1325 			err_suj("Partial truncation of ino %ju snapshot file\n",
1326 			    (uintmax_t)ino);
1327 		snapremove(ino);
1328 	}
1329 	lastlbn = lblkno(fs, blkroundup(fs, size));
1330 	for (i = lastlbn; i < UFS_NDADDR; i++) {
1331 		if ((bn = DIP(dp, di_db[i])) == 0)
1332 			continue;
1333 		blksize = sblksize(fs, cursize, i);
1334 		blk_free(ino, bn, 0, numfrags(fs, blksize));
1335 		DIP_SET(dp, di_db[i], 0);
1336 	}
1337 	/*
1338 	 * Follow indirect blocks, freeing anything required.
1339 	 */
1340 	for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; i < UFS_NIADDR; i++,
1341 	    lbn = nextlbn) {
1342 		nextlbn = lbn + tmpval;
1343 		tmpval *= NINDIR(fs);
1344 		/* If we're not freeing any in this indirect range skip it. */
1345 		if (lastlbn >= nextlbn)
1346 			continue;
1347 		if ((bn = DIP(dp, di_ib[i])) == 0)
1348   			continue;
1349 		indir_trunc(ino, -lbn - i, bn, lastlbn, dp);
1350   		/* If we freed everything in this indirect free the indir. */
1351   		if (lastlbn > lbn)
1352   			continue;
1353 		blk_free(ino, bn, 0, fs->fs_frag);
1354 		DIP_SET(dp, di_ib[i], 0);
1355 	}
1356 	/*
1357 	 * Now that we've freed any whole blocks that exceed the desired
1358 	 * truncation size, figure out how many blocks remain and what the
1359 	 * last populated lbn is.  We will set the size to this last lbn
1360 	 * rather than worrying about allocating the final lbn as the kernel
1361 	 * would've done.  This is consistent with normal fsck behavior.
1362 	 */
1363 	visitlbn = 0;
1364 	totalfrags = ino_visit(dp, ino, null_visit, VISIT_INDIR | VISIT_EXT);
1365 	if (size > lblktosize(fs, visitlbn + 1))
1366 		size = lblktosize(fs, visitlbn + 1);
1367 	/*
1368 	 * If we're truncating direct blocks we have to adjust frags
1369 	 * accordingly.
1370 	 */
1371 	if (visitlbn < UFS_NDADDR && totalfrags) {
1372 		long oldspace, newspace;
1373 
1374 		bn = DIP(dp, di_db[visitlbn]);
1375 		if (bn == 0)
1376 			err_suj("Bad blk at ino %ju lbn %jd\n",
1377 			    (uintmax_t)ino, visitlbn);
1378 		oldspace = sblksize(fs, cursize, visitlbn);
1379 		newspace = sblksize(fs, size, visitlbn);
1380 		if (oldspace != newspace) {
1381 			bn += numfrags(fs, newspace);
1382 			frags = numfrags(fs, oldspace - newspace);
1383 			blk_free(ino, bn, 0, frags);
1384 			totalfrags -= frags;
1385 		}
1386 	}
1387 	DIP_SET(dp, di_blocks, fsbtodb(fs, totalfrags));
1388 	DIP_SET(dp, di_size, size);
1389 	inodirty(&ip);
1390 	/*
1391 	 * If we've truncated into the middle of a block or frag we have
1392 	 * to zero it here.  Otherwise the file could extend into
1393 	 * uninitialized space later.
1394 	 */
1395 	off = blkoff(fs, size);
1396 	if (off && DIP(dp, di_mode) != IFDIR) {
1397 		long clrsize;
1398 
1399 		bn = ino_blkatoff(dp, ino, visitlbn, &frags, NULL);
1400 		if (bn == 0)
1401 			err_suj("Block missing from ino %ju at lbn %jd\n",
1402 			    (uintmax_t)ino, visitlbn);
1403 		clrsize = frags * fs->fs_fsize;
1404 		bp = getdatablk(bn, clrsize, BT_DATA);
1405 		if (bp->b_errs != 0)
1406 			err_suj("ino_trunc: UNRECOVERABLE I/O ERROR");
1407 		clrsize -= off;
1408 		bzero(&bp->b_un.b_buf[off], clrsize);
1409 		dirty(bp);
1410 		brelse(bp);
1411 	}
1412 	irelse(&ip);
1413 	return;
1414 }
1415 
1416 /*
1417  * Process records available for one inode and determine whether the
1418  * link count is correct or needs adjusting.
1419  */
1420 static void
1421 ino_check(struct suj_ino *sino)
1422 {
1423 	struct suj_rec *srec;
1424 	struct jrefrec *rrec;
1425 	nlink_t dotlinks;
1426 	nlink_t newlinks;
1427 	nlink_t removes;
1428 	nlink_t nlink;
1429 	ino_t ino;
1430 	int isdot;
1431 	int isat;
1432 	int mode;
1433 
1434 	if (sino->si_hasrecs == 0)
1435 		return;
1436 	ino = sino->si_ino;
1437 	rrec = (struct jrefrec *)TAILQ_FIRST(&sino->si_recs)->sr_rec;
1438 	nlink = rrec->jr_nlink;
1439 	newlinks = 0;
1440 	dotlinks = 0;
1441 	removes = sino->si_nlinkadj;
1442 	TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1443 		rrec = (struct jrefrec *)srec->sr_rec;
1444 		isat = ino_isat(rrec->jr_parent, rrec->jr_diroff,
1445 		    rrec->jr_ino, &mode, &isdot);
1446 		if (isat && (mode & IFMT) != (rrec->jr_mode & IFMT))
1447 			err_suj("Inode mode/directory type mismatch %o != %o\n",
1448 			    mode, rrec->jr_mode);
1449 		if (debug)
1450 			printf("jrefrec: op %d ino %ju, nlink %ju, parent %ju, "
1451 			    "diroff %jd, mode %o, isat %d, isdot %d\n",
1452 			    rrec->jr_op, (uintmax_t)rrec->jr_ino,
1453 			    (uintmax_t)rrec->jr_nlink,
1454 			    (uintmax_t)rrec->jr_parent,
1455 			    (uintmax_t)rrec->jr_diroff,
1456 			    rrec->jr_mode, isat, isdot);
1457 		mode = rrec->jr_mode & IFMT;
1458 		if (rrec->jr_op == JOP_REMREF)
1459 			removes++;
1460 		newlinks += isat;
1461 		if (isdot)
1462 			dotlinks += isat;
1463 	}
1464 	/*
1465 	 * The number of links that remain are the starting link count
1466 	 * subtracted by the total number of removes with the total
1467 	 * links discovered back in.  An incomplete remove thus
1468 	 * makes no change to the link count but an add increases
1469 	 * by one.
1470 	 */
1471 	if (debug)
1472 		printf(
1473 		    "ino %ju nlink %ju newlinks %ju removes %ju dotlinks %ju\n",
1474 		    (uintmax_t)ino, (uintmax_t)nlink, (uintmax_t)newlinks,
1475 		    (uintmax_t)removes, (uintmax_t)dotlinks);
1476 	nlink += newlinks;
1477 	nlink -= removes;
1478 	sino->si_linkadj = 1;
1479 	sino->si_nlink = nlink;
1480 	sino->si_dotlinks = dotlinks;
1481 	sino->si_mode = mode;
1482 	ino_adjust(sino);
1483 }
1484 
1485 /*
1486  * Process records available for one block and determine whether it is
1487  * still allocated and whether the owning inode needs to be updated or
1488  * a free completed.
1489  */
1490 static void
1491 blk_check(struct suj_blk *sblk)
1492 {
1493 	struct suj_rec *srec;
1494 	struct jblkrec *brec;
1495 	struct suj_ino *sino;
1496 	ufs2_daddr_t blk;
1497 	int mask;
1498 	int frags;
1499 	int isat;
1500 
1501 	/*
1502 	 * Each suj_blk actually contains records for any fragments in that
1503 	 * block.  As a result we must evaluate each record individually.
1504 	 */
1505 	sino = NULL;
1506 	TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
1507 		brec = (struct jblkrec *)srec->sr_rec;
1508 		frags = brec->jb_frags;
1509 		blk = brec->jb_blkno + brec->jb_oldfrags;
1510 		isat = blk_isat(brec->jb_ino, brec->jb_lbn, blk, &frags);
1511 		if (sino == NULL || sino->si_ino != brec->jb_ino) {
1512 			sino = ino_lookup(brec->jb_ino, 1);
1513 			sino->si_blkadj = 1;
1514 		}
1515 		if (debug)
1516 			printf("op %d blk %jd ino %ju lbn %jd frags %d isat %d (%d)\n",
1517 			    brec->jb_op, blk, (uintmax_t)brec->jb_ino,
1518 			    brec->jb_lbn, brec->jb_frags, isat, frags);
1519 		/*
1520 		 * If we found the block at this address we still have to
1521 		 * determine if we need to free the tail end that was
1522 		 * added by adding contiguous fragments from the same block.
1523 		 */
1524 		if (isat == 1) {
1525 			if (frags == brec->jb_frags)
1526 				continue;
1527 			mask = blk_freemask(blk, brec->jb_ino, brec->jb_lbn,
1528 			    brec->jb_frags);
1529 			mask >>= frags;
1530 			blk += frags;
1531 			frags = brec->jb_frags - frags;
1532 			blk_free(brec->jb_ino, blk, mask, frags);
1533 			continue;
1534 		}
1535 		/*
1536 	 	 * The block wasn't found, attempt to free it.  It won't be
1537 		 * freed if it was actually reallocated.  If this was an
1538 		 * allocation we don't want to follow indirects as they
1539 		 * may not be written yet.  Any children of the indirect will
1540 		 * have their own records.  If it's a free we need to
1541 		 * recursively free children.
1542 		 */
1543 		blk_free_lbn(blk, brec->jb_ino, brec->jb_lbn, brec->jb_frags,
1544 		    brec->jb_op == JOP_FREEBLK);
1545 	}
1546 }
1547 
1548 /*
1549  * Walk the list of inode records for this cg and resolve moved and duplicate
1550  * inode references now that we have a complete picture.
1551  */
1552 static void
1553 cg_build(struct suj_cg *sc)
1554 {
1555 	struct suj_ino *sino;
1556 	int i;
1557 
1558 	for (i = 0; i < HASHSIZE; i++)
1559 		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1560 			ino_build(sino);
1561 }
1562 
1563 /*
1564  * Handle inodes requiring truncation.  This must be done prior to
1565  * looking up any inodes in directories.
1566  */
1567 static void
1568 cg_trunc(struct suj_cg *sc)
1569 {
1570 	struct suj_ino *sino;
1571 	int i;
1572 
1573 	for (i = 0; i < HASHSIZE; i++) {
1574 		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) {
1575 			if (sino->si_trunc) {
1576 				ino_trunc(sino->si_ino,
1577 				    sino->si_trunc->jt_size);
1578 				sino->si_blkadj = 0;
1579 				sino->si_trunc = NULL;
1580 			}
1581 			if (sino->si_blkadj)
1582 				ino_adjblks(sino);
1583 		}
1584 	}
1585 }
1586 
1587 static void
1588 cg_adj_blk(struct suj_cg *sc)
1589 {
1590 	struct suj_ino *sino;
1591 	int i;
1592 
1593 	for (i = 0; i < HASHSIZE; i++) {
1594 		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) {
1595 			if (sino->si_blkadj)
1596 				ino_adjblks(sino);
1597 		}
1598 	}
1599 }
1600 
1601 /*
1602  * Free any partially allocated blocks and then resolve inode block
1603  * counts.
1604  */
1605 static void
1606 cg_check_blk(struct suj_cg *sc)
1607 {
1608 	struct suj_blk *sblk;
1609 	int i;
1610 
1611 
1612 	for (i = 0; i < HASHSIZE; i++)
1613 		LIST_FOREACH(sblk, &sc->sc_blkhash[i], sb_next)
1614 			blk_check(sblk);
1615 }
1616 
1617 /*
1618  * Walk the list of inode records for this cg, recovering any
1619  * changes which were not complete at the time of crash.
1620  */
1621 static void
1622 cg_check_ino(struct suj_cg *sc)
1623 {
1624 	struct suj_ino *sino;
1625 	int i;
1626 
1627 	for (i = 0; i < HASHSIZE; i++)
1628 		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1629 			ino_check(sino);
1630 }
1631 
1632 static void
1633 cg_apply(void (*apply)(struct suj_cg *))
1634 {
1635 	struct suj_cg *scg;
1636 	int i;
1637 
1638 	for (i = 0; i < HASHSIZE; i++)
1639 		LIST_FOREACH(scg, &cghash[i], sc_next)
1640 			apply(scg);
1641 }
1642 
1643 /*
1644  * Process the unlinked but referenced file list.  Freeing all inodes.
1645  */
1646 static void
1647 ino_unlinked(void)
1648 {
1649 	struct inode ip;
1650 	union dinode *dp;
1651 	uint16_t mode;
1652 	ino_t inon;
1653 	ino_t ino;
1654 
1655 	ino = fs->fs_sujfree;
1656 	fs->fs_sujfree = 0;
1657 	while (ino != 0) {
1658 		ginode(ino, &ip);
1659 		dp = ip.i_dp;
1660 		mode = DIP(dp, di_mode) & IFMT;
1661 		inon = DIP(dp, di_freelink);
1662 		DIP_SET(dp, di_freelink, 0);
1663 		inodirty(&ip);
1664 		/*
1665 		 * XXX Should this be an errx?
1666 		 */
1667 		if (DIP(dp, di_nlink) == 0) {
1668 			if (debug)
1669 				printf("Freeing unlinked ino %ju mode %o\n",
1670 				    (uintmax_t)ino, mode);
1671 			ino_reclaim(&ip, ino, mode);
1672 		} else if (debug)
1673 			printf("Skipping ino %ju mode %o with link %d\n",
1674 			    (uintmax_t)ino, mode, DIP(dp, di_nlink));
1675 		ino = inon;
1676 		irelse(&ip);
1677 	}
1678 }
1679 
1680 /*
1681  * Append a new record to the list of records requiring processing.
1682  */
1683 static void
1684 ino_append(union jrec *rec)
1685 {
1686 	struct jrefrec *refrec;
1687 	struct jmvrec *mvrec;
1688 	struct suj_ino *sino;
1689 	struct suj_rec *srec;
1690 
1691 	mvrec = &rec->rec_jmvrec;
1692 	refrec = &rec->rec_jrefrec;
1693 	if (debug && mvrec->jm_op == JOP_MVREF)
1694 		printf("ino move: ino %ju, parent %ju, "
1695 		    "diroff %jd, oldoff %jd\n",
1696 		    (uintmax_t)mvrec->jm_ino, (uintmax_t)mvrec->jm_parent,
1697 		    (uintmax_t)mvrec->jm_newoff, (uintmax_t)mvrec->jm_oldoff);
1698 	else if (debug &&
1699 	    (refrec->jr_op == JOP_ADDREF || refrec->jr_op == JOP_REMREF))
1700 		printf("ino ref: op %d, ino %ju, nlink %ju, "
1701 		    "parent %ju, diroff %jd\n",
1702 		    refrec->jr_op, (uintmax_t)refrec->jr_ino,
1703 		    (uintmax_t)refrec->jr_nlink,
1704 		    (uintmax_t)refrec->jr_parent, (uintmax_t)refrec->jr_diroff);
1705 	sino = ino_lookup(((struct jrefrec *)rec)->jr_ino, 1);
1706 	sino->si_hasrecs = 1;
1707 	srec = errmalloc(sizeof(*srec));
1708 	srec->sr_rec = rec;
1709 	TAILQ_INSERT_TAIL(&sino->si_newrecs, srec, sr_next);
1710 }
1711 
1712 /*
1713  * Add a reference adjustment to the sino list and eliminate dups.  The
1714  * primary loop in ino_build_ref() checks for dups but new ones may be
1715  * created as a result of offset adjustments.
1716  */
1717 static void
1718 ino_add_ref(struct suj_ino *sino, struct suj_rec *srec)
1719 {
1720 	struct jrefrec *refrec;
1721 	struct suj_rec *srn;
1722 	struct jrefrec *rrn;
1723 
1724 	refrec = (struct jrefrec *)srec->sr_rec;
1725 	/*
1726 	 * We walk backwards so that the oldest link count is preserved.  If
1727 	 * an add record conflicts with a remove keep the remove.  Redundant
1728 	 * removes are eliminated in ino_build_ref.  Otherwise we keep the
1729 	 * oldest record at a given location.
1730 	 */
1731 	for (srn = TAILQ_LAST(&sino->si_recs, srechd); srn;
1732 	    srn = TAILQ_PREV(srn, srechd, sr_next)) {
1733 		rrn = (struct jrefrec *)srn->sr_rec;
1734 		if (rrn->jr_parent != refrec->jr_parent ||
1735 		    rrn->jr_diroff != refrec->jr_diroff)
1736 			continue;
1737 		if (rrn->jr_op == JOP_REMREF || refrec->jr_op == JOP_ADDREF) {
1738 			rrn->jr_mode = refrec->jr_mode;
1739 			return;
1740 		}
1741 		/*
1742 		 * Adding a remove.
1743 		 *
1744 		 * Replace the record in place with the old nlink in case
1745 		 * we replace the head of the list.  Abandon srec as a dup.
1746 		 */
1747 		refrec->jr_nlink = rrn->jr_nlink;
1748 		srn->sr_rec = srec->sr_rec;
1749 		return;
1750 	}
1751 	TAILQ_INSERT_TAIL(&sino->si_recs, srec, sr_next);
1752 }
1753 
1754 /*
1755  * Create a duplicate of a reference at a previous location.
1756  */
1757 static void
1758 ino_dup_ref(struct suj_ino *sino, struct jrefrec *refrec, off_t diroff)
1759 {
1760 	struct jrefrec *rrn;
1761 	struct suj_rec *srn;
1762 
1763 	rrn = errmalloc(sizeof(*refrec));
1764 	*rrn = *refrec;
1765 	rrn->jr_op = JOP_ADDREF;
1766 	rrn->jr_diroff = diroff;
1767 	srn = errmalloc(sizeof(*srn));
1768 	srn->sr_rec = (union jrec *)rrn;
1769 	ino_add_ref(sino, srn);
1770 }
1771 
1772 /*
1773  * Add a reference to the list at all known locations.  We follow the offset
1774  * changes for a single instance and create duplicate add refs at each so
1775  * that we can tolerate any version of the directory block.  Eliminate
1776  * removes which collide with adds that are seen in the journal.  They should
1777  * not adjust the link count down.
1778  */
1779 static void
1780 ino_build_ref(struct suj_ino *sino, struct suj_rec *srec)
1781 {
1782 	struct jrefrec *refrec;
1783 	struct jmvrec *mvrec;
1784 	struct suj_rec *srp;
1785 	struct suj_rec *srn;
1786 	struct jrefrec *rrn;
1787 	off_t diroff;
1788 
1789 	refrec = (struct jrefrec *)srec->sr_rec;
1790 	/*
1791 	 * Search for a mvrec that matches this offset.  Whether it's an add
1792 	 * or a remove we can delete the mvref after creating a dup record in
1793 	 * the old location.
1794 	 */
1795 	if (!TAILQ_EMPTY(&sino->si_movs)) {
1796 		diroff = refrec->jr_diroff;
1797 		for (srn = TAILQ_LAST(&sino->si_movs, srechd); srn; srn = srp) {
1798 			srp = TAILQ_PREV(srn, srechd, sr_next);
1799 			mvrec = (struct jmvrec *)srn->sr_rec;
1800 			if (mvrec->jm_parent != refrec->jr_parent ||
1801 			    mvrec->jm_newoff != diroff)
1802 				continue;
1803 			diroff = mvrec->jm_oldoff;
1804 			TAILQ_REMOVE(&sino->si_movs, srn, sr_next);
1805 			free(srn);
1806 			ino_dup_ref(sino, refrec, diroff);
1807 		}
1808 	}
1809 	/*
1810 	 * If a remove wasn't eliminated by an earlier add just append it to
1811 	 * the list.
1812 	 */
1813 	if (refrec->jr_op == JOP_REMREF) {
1814 		ino_add_ref(sino, srec);
1815 		return;
1816 	}
1817 	/*
1818 	 * Walk the list of records waiting to be added to the list.  We
1819 	 * must check for moves that apply to our current offset and remove
1820 	 * them from the list.  Remove any duplicates to eliminate removes
1821 	 * with corresponding adds.
1822 	 */
1823 	TAILQ_FOREACH_SAFE(srn, &sino->si_newrecs, sr_next, srp) {
1824 		switch (srn->sr_rec->rec_jrefrec.jr_op) {
1825 		case JOP_ADDREF:
1826 			/*
1827 			 * This should actually be an error we should
1828 			 * have a remove for every add journaled.
1829 			 */
1830 			rrn = (struct jrefrec *)srn->sr_rec;
1831 			if (rrn->jr_parent != refrec->jr_parent ||
1832 			    rrn->jr_diroff != refrec->jr_diroff)
1833 				break;
1834 			TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
1835 			break;
1836 		case JOP_REMREF:
1837 			/*
1838 			 * Once we remove the current iteration of the
1839 			 * record at this address we're done.
1840 			 */
1841 			rrn = (struct jrefrec *)srn->sr_rec;
1842 			if (rrn->jr_parent != refrec->jr_parent ||
1843 			    rrn->jr_diroff != refrec->jr_diroff)
1844 				break;
1845 			TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
1846 			ino_add_ref(sino, srec);
1847 			return;
1848 		case JOP_MVREF:
1849 			/*
1850 			 * Update our diroff based on any moves that match
1851 			 * and remove the move.
1852 			 */
1853 			mvrec = (struct jmvrec *)srn->sr_rec;
1854 			if (mvrec->jm_parent != refrec->jr_parent ||
1855 			    mvrec->jm_oldoff != refrec->jr_diroff)
1856 				break;
1857 			ino_dup_ref(sino, refrec, mvrec->jm_oldoff);
1858 			refrec->jr_diroff = mvrec->jm_newoff;
1859 			TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
1860 			break;
1861 		default:
1862 			err_suj("ino_build_ref: Unknown op %d\n",
1863 			    srn->sr_rec->rec_jrefrec.jr_op);
1864 		}
1865 	}
1866 	ino_add_ref(sino, srec);
1867 }
1868 
1869 /*
1870  * Walk the list of new records and add them in-order resolving any
1871  * dups and adjusted offsets.
1872  */
1873 static void
1874 ino_build(struct suj_ino *sino)
1875 {
1876 	struct suj_rec *srec;
1877 
1878 	while ((srec = TAILQ_FIRST(&sino->si_newrecs)) != NULL) {
1879 		TAILQ_REMOVE(&sino->si_newrecs, srec, sr_next);
1880 		switch (srec->sr_rec->rec_jrefrec.jr_op) {
1881 		case JOP_ADDREF:
1882 		case JOP_REMREF:
1883 			ino_build_ref(sino, srec);
1884 			break;
1885 		case JOP_MVREF:
1886 			/*
1887 			 * Add this mvrec to the queue of pending mvs.
1888 			 */
1889 			TAILQ_INSERT_TAIL(&sino->si_movs, srec, sr_next);
1890 			break;
1891 		default:
1892 			err_suj("ino_build: Unknown op %d\n",
1893 			    srec->sr_rec->rec_jrefrec.jr_op);
1894 		}
1895 	}
1896 	if (TAILQ_EMPTY(&sino->si_recs))
1897 		sino->si_hasrecs = 0;
1898 }
1899 
1900 /*
1901  * Modify journal records so they refer to the base block number
1902  * and a start and end frag range.  This is to facilitate the discovery
1903  * of overlapping fragment allocations.
1904  */
1905 static void
1906 blk_build(struct jblkrec *blkrec)
1907 {
1908 	struct suj_rec *srec;
1909 	struct suj_blk *sblk;
1910 	struct jblkrec *blkrn;
1911 	ufs2_daddr_t blk;
1912 	int frag;
1913 
1914 	if (debug)
1915 		printf("blk_build: op %d blkno %jd frags %d oldfrags %d "
1916 		    "ino %ju lbn %jd\n",
1917 		    blkrec->jb_op, (uintmax_t)blkrec->jb_blkno,
1918 		    blkrec->jb_frags, blkrec->jb_oldfrags,
1919 		    (uintmax_t)blkrec->jb_ino, (uintmax_t)blkrec->jb_lbn);
1920 
1921 	blk = blknum(fs, blkrec->jb_blkno);
1922 	frag = fragnum(fs, blkrec->jb_blkno);
1923 	sblk = blk_lookup(blk, 1);
1924 	/*
1925 	 * Rewrite the record using oldfrags to indicate the offset into
1926 	 * the block.  Leave jb_frags as the actual allocated count.
1927 	 */
1928 	blkrec->jb_blkno -= frag;
1929 	blkrec->jb_oldfrags = frag;
1930 	if (blkrec->jb_oldfrags + blkrec->jb_frags > fs->fs_frag)
1931 		err_suj("Invalid fragment count %d oldfrags %d\n",
1932 		    blkrec->jb_frags, frag);
1933 	/*
1934 	 * Detect dups.  If we detect a dup we always discard the oldest
1935 	 * record as it is superseded by the new record.  This speeds up
1936 	 * later stages but also eliminates free records which are used
1937 	 * to indicate that the contents of indirects can be trusted.
1938 	 */
1939 	TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
1940 		blkrn = (struct jblkrec *)srec->sr_rec;
1941 		if (blkrn->jb_ino != blkrec->jb_ino ||
1942 		    blkrn->jb_lbn != blkrec->jb_lbn ||
1943 		    blkrn->jb_blkno != blkrec->jb_blkno ||
1944 		    blkrn->jb_frags != blkrec->jb_frags ||
1945 		    blkrn->jb_oldfrags != blkrec->jb_oldfrags)
1946 			continue;
1947 		if (debug)
1948 			printf("Removed dup.\n");
1949 		/* Discard the free which is a dup with an alloc. */
1950 		if (blkrec->jb_op == JOP_FREEBLK)
1951 			return;
1952 		TAILQ_REMOVE(&sblk->sb_recs, srec, sr_next);
1953 		free(srec);
1954 		break;
1955 	}
1956 	srec = errmalloc(sizeof(*srec));
1957 	srec->sr_rec = (union jrec *)blkrec;
1958 	TAILQ_INSERT_TAIL(&sblk->sb_recs, srec, sr_next);
1959 }
1960 
1961 static void
1962 ino_build_trunc(struct jtrncrec *rec)
1963 {
1964 	struct suj_ino *sino;
1965 
1966 	if (debug)
1967 		printf("ino_build_trunc: op %d ino %ju, size %jd\n",
1968 		    rec->jt_op, (uintmax_t)rec->jt_ino,
1969 		    (uintmax_t)rec->jt_size);
1970 	sino = ino_lookup(rec->jt_ino, 1);
1971 	if (rec->jt_op == JOP_SYNC) {
1972 		sino->si_trunc = NULL;
1973 		return;
1974 	}
1975 	if (sino->si_trunc == NULL || sino->si_trunc->jt_size > rec->jt_size)
1976 		sino->si_trunc = rec;
1977 }
1978 
1979 /*
1980  * Build up tables of the operations we need to recover.
1981  */
1982 static void
1983 suj_build(void)
1984 {
1985 	struct suj_seg *seg;
1986 	union jrec *rec;
1987 	int off;
1988 	int i;
1989 
1990 	TAILQ_FOREACH(seg, &allsegs, ss_next) {
1991 		if (debug)
1992 			printf("seg %jd has %d records, oldseq %jd.\n",
1993 			    seg->ss_rec.jsr_seq, seg->ss_rec.jsr_cnt,
1994 			    seg->ss_rec.jsr_oldest);
1995 		off = 0;
1996 		rec = (union jrec *)seg->ss_blk;
1997 		for (i = 0; i < seg->ss_rec.jsr_cnt; off += JREC_SIZE, rec++) {
1998 			/* skip the segrec. */
1999 			if ((off % real_dev_bsize) == 0)
2000 				continue;
2001 			switch (rec->rec_jrefrec.jr_op) {
2002 			case JOP_ADDREF:
2003 			case JOP_REMREF:
2004 			case JOP_MVREF:
2005 				ino_append(rec);
2006 				break;
2007 			case JOP_NEWBLK:
2008 			case JOP_FREEBLK:
2009 				blk_build((struct jblkrec *)rec);
2010 				break;
2011 			case JOP_TRUNC:
2012 			case JOP_SYNC:
2013 				ino_build_trunc((struct jtrncrec *)rec);
2014 				break;
2015 			default:
2016 				err_suj("Unknown journal operation %d (%d)\n",
2017 				    rec->rec_jrefrec.jr_op, off);
2018 			}
2019 			i++;
2020 		}
2021 	}
2022 }
2023 
2024 /*
2025  * Prune the journal segments to those we care about based on the
2026  * oldest sequence in the newest segment.  Order the segment list
2027  * based on sequence number.
2028  */
2029 static void
2030 suj_prune(void)
2031 {
2032 	struct suj_seg *seg;
2033 	struct suj_seg *segn;
2034 	uint64_t newseq;
2035 	int discard;
2036 
2037 	if (debug)
2038 		printf("Pruning up to %jd\n", oldseq);
2039 	/* First free the expired segments. */
2040 	TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2041 		if (seg->ss_rec.jsr_seq >= oldseq)
2042 			continue;
2043 		TAILQ_REMOVE(&allsegs, seg, ss_next);
2044 		free(seg->ss_blk);
2045 		free(seg);
2046 	}
2047 	/* Next ensure that segments are ordered properly. */
2048 	seg = TAILQ_FIRST(&allsegs);
2049 	if (seg == NULL) {
2050 		if (debug)
2051 			printf("Empty journal\n");
2052 		return;
2053 	}
2054 	newseq = seg->ss_rec.jsr_seq;
2055 	for (;;) {
2056 		seg = TAILQ_LAST(&allsegs, seghd);
2057 		if (seg->ss_rec.jsr_seq >= newseq)
2058 			break;
2059 		TAILQ_REMOVE(&allsegs, seg, ss_next);
2060 		TAILQ_INSERT_HEAD(&allsegs, seg, ss_next);
2061 		newseq = seg->ss_rec.jsr_seq;
2062 
2063 	}
2064 	if (newseq != oldseq) {
2065 		TAILQ_FOREACH(seg, &allsegs, ss_next) {
2066 			printf("%jd, ", seg->ss_rec.jsr_seq);
2067 		}
2068 		printf("\n");
2069 		err_suj("Journal file sequence mismatch %jd != %jd\n",
2070 		    newseq, oldseq);
2071 	}
2072 	/*
2073 	 * The kernel may asynchronously write segments which can create
2074 	 * gaps in the sequence space.  Throw away any segments after the
2075 	 * gap as the kernel guarantees only those that are contiguously
2076 	 * reachable are marked as completed.
2077 	 */
2078 	discard = 0;
2079 	TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2080 		if (!discard && newseq++ == seg->ss_rec.jsr_seq) {
2081 			jrecs += seg->ss_rec.jsr_cnt;
2082 			jbytes += seg->ss_rec.jsr_blocks * real_dev_bsize;
2083 			continue;
2084 		}
2085 		discard = 1;
2086 		if (debug)
2087 			printf("Journal order mismatch %jd != %jd pruning\n",
2088 			    newseq-1, seg->ss_rec.jsr_seq);
2089 		TAILQ_REMOVE(&allsegs, seg, ss_next);
2090 		free(seg->ss_blk);
2091 		free(seg);
2092 	}
2093 	if (debug)
2094 		printf("Processing journal segments from %jd to %jd\n",
2095 		    oldseq, newseq-1);
2096 }
2097 
2098 /*
2099  * Verify the journal inode before attempting to read records.
2100  */
2101 static int
2102 suj_verifyino(union dinode *dp)
2103 {
2104 
2105 	if (DIP(dp, di_nlink) != 1) {
2106 		printf("Invalid link count %d for journal inode %ju\n",
2107 		    DIP(dp, di_nlink), (uintmax_t)sujino);
2108 		return (-1);
2109 	}
2110 
2111 	if ((DIP(dp, di_flags) & (SF_IMMUTABLE | SF_NOUNLINK)) !=
2112 	    (SF_IMMUTABLE | SF_NOUNLINK)) {
2113 		printf("Invalid flags 0x%X for journal inode %ju\n",
2114 		    DIP(dp, di_flags), (uintmax_t)sujino);
2115 		return (-1);
2116 	}
2117 
2118 	if (DIP(dp, di_mode) != (IFREG | IREAD)) {
2119 		printf("Invalid mode %o for journal inode %ju\n",
2120 		    DIP(dp, di_mode), (uintmax_t)sujino);
2121 		return (-1);
2122 	}
2123 
2124 	if (DIP(dp, di_size) < SUJ_MIN) {
2125 		printf("Invalid size %jd for journal inode %ju\n",
2126 		    DIP(dp, di_size), (uintmax_t)sujino);
2127 		return (-1);
2128 	}
2129 
2130 	if (DIP(dp, di_modrev) != fs->fs_mtime) {
2131 		printf("Journal timestamp does not match fs mount time\n");
2132 		return (-1);
2133 	}
2134 
2135 	return (0);
2136 }
2137 
2138 struct jblocks {
2139 	struct jextent *jb_extent;	/* Extent array. */
2140 	int		jb_avail;	/* Available extents. */
2141 	int		jb_used;	/* Last used extent. */
2142 	int		jb_head;	/* Allocator head. */
2143 	int		jb_off;		/* Allocator extent offset. */
2144 };
2145 struct jextent {
2146 	ufs2_daddr_t	je_daddr;	/* Disk block address. */
2147 	int		je_blocks;	/* Disk block count. */
2148 };
2149 
2150 static struct jblocks *suj_jblocks;
2151 
2152 static struct jblocks *
2153 jblocks_create(void)
2154 {
2155 	struct jblocks *jblocks;
2156 	int size;
2157 
2158 	jblocks = errmalloc(sizeof(*jblocks));
2159 	jblocks->jb_avail = 10;
2160 	jblocks->jb_used = 0;
2161 	jblocks->jb_head = 0;
2162 	jblocks->jb_off = 0;
2163 	size = sizeof(struct jextent) * jblocks->jb_avail;
2164 	jblocks->jb_extent = errmalloc(size);
2165 	bzero(jblocks->jb_extent, size);
2166 
2167 	return (jblocks);
2168 }
2169 
2170 /*
2171  * Return the next available disk block and the amount of contiguous
2172  * free space it contains.
2173  */
2174 static ufs2_daddr_t
2175 jblocks_next(struct jblocks *jblocks, int bytes, int *actual)
2176 {
2177 	struct jextent *jext;
2178 	ufs2_daddr_t daddr;
2179 	int freecnt;
2180 	int blocks;
2181 
2182 	blocks = btodb(bytes);
2183 	jext = &jblocks->jb_extent[jblocks->jb_head];
2184 	freecnt = jext->je_blocks - jblocks->jb_off;
2185 	if (freecnt == 0) {
2186 		jblocks->jb_off = 0;
2187 		if (++jblocks->jb_head > jblocks->jb_used)
2188 			return (0);
2189 		jext = &jblocks->jb_extent[jblocks->jb_head];
2190 		freecnt = jext->je_blocks;
2191 	}
2192 	if (freecnt > blocks)
2193 		freecnt = blocks;
2194 	*actual = dbtob(freecnt);
2195 	daddr = jext->je_daddr + jblocks->jb_off;
2196 
2197 	return (daddr);
2198 }
2199 
2200 /*
2201  * Advance the allocation head by a specified number of bytes, consuming
2202  * one journal segment.
2203  */
2204 static void
2205 jblocks_advance(struct jblocks *jblocks, int bytes)
2206 {
2207 
2208 	jblocks->jb_off += btodb(bytes);
2209 }
2210 
2211 static void
2212 jblocks_destroy(struct jblocks *jblocks)
2213 {
2214 
2215 	free(jblocks->jb_extent);
2216 	free(jblocks);
2217 }
2218 
2219 static void
2220 jblocks_add(struct jblocks *jblocks, ufs2_daddr_t daddr, int blocks)
2221 {
2222 	struct jextent *jext;
2223 	int size;
2224 
2225 	jext = &jblocks->jb_extent[jblocks->jb_used];
2226 	/* Adding the first block. */
2227 	if (jext->je_daddr == 0) {
2228 		jext->je_daddr = daddr;
2229 		jext->je_blocks = blocks;
2230 		return;
2231 	}
2232 	/* Extending the last extent. */
2233 	if (jext->je_daddr + jext->je_blocks == daddr) {
2234 		jext->je_blocks += blocks;
2235 		return;
2236 	}
2237 	/* Adding a new extent. */
2238 	if (++jblocks->jb_used == jblocks->jb_avail) {
2239 		jblocks->jb_avail *= 2;
2240 		size = sizeof(struct jextent) * jblocks->jb_avail;
2241 		jext = errmalloc(size);
2242 		bzero(jext, size);
2243 		bcopy(jblocks->jb_extent, jext,
2244 		    sizeof(struct jextent) * jblocks->jb_used);
2245 		free(jblocks->jb_extent);
2246 		jblocks->jb_extent = jext;
2247 	}
2248 	jext = &jblocks->jb_extent[jblocks->jb_used];
2249 	jext->je_daddr = daddr;
2250 	jext->je_blocks = blocks;
2251 
2252 	return;
2253 }
2254 
2255 /*
2256  * Add a file block from the journal to the extent map.  We can't read
2257  * each file block individually because the kernel treats it as a circular
2258  * buffer and segments may span mutliple contiguous blocks.
2259  */
2260 static void
2261 suj_add_block(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
2262 {
2263 
2264 	jblocks_add(suj_jblocks, fsbtodb(fs, blk), fsbtodb(fs, frags));
2265 }
2266 
2267 static void
2268 suj_read(void)
2269 {
2270 	uint8_t block[1 * 1024 * 1024];
2271 	struct suj_seg *seg;
2272 	struct jsegrec *recn;
2273 	struct jsegrec *rec;
2274 	ufs2_daddr_t blk;
2275 	int readsize;
2276 	int blocks;
2277 	int recsize;
2278 	int size;
2279 	int i;
2280 
2281 	/*
2282 	 * Read records until we exhaust the journal space.  If we find
2283 	 * an invalid record we start searching for a valid segment header
2284 	 * at the next block.  This is because we don't have a head/tail
2285 	 * pointer and must recover the information indirectly.  At the gap
2286 	 * between the head and tail we won't necessarily have a valid
2287 	 * segment.
2288 	 */
2289 restart:
2290 	for (;;) {
2291 		size = sizeof(block);
2292 		blk = jblocks_next(suj_jblocks, size, &readsize);
2293 		if (blk == 0)
2294 			return;
2295 		size = readsize;
2296 		/*
2297 		 * Read 1MB at a time and scan for records within this block.
2298 		 */
2299 		if (pread(fsreadfd, &block, size, dbtob(blk)) != size) {
2300 			err_suj("Error reading journal block %jd\n",
2301 			    (intmax_t)blk);
2302 		}
2303 		for (rec = (void *)block; size; size -= recsize,
2304 		    rec = (struct jsegrec *)((uintptr_t)rec + recsize)) {
2305 			recsize = real_dev_bsize;
2306 			if (rec->jsr_time != fs->fs_mtime) {
2307 #ifdef notdef
2308 				if (debug)
2309 					printf("Rec time %jd != fs mtime %jd\n",
2310 					    rec->jsr_time, fs->fs_mtime);
2311 #endif
2312 				jblocks_advance(suj_jblocks, recsize);
2313 				continue;
2314 			}
2315 			if (rec->jsr_cnt == 0) {
2316 				if (debug)
2317 					printf("Found illegal count %d\n",
2318 					    rec->jsr_cnt);
2319 				jblocks_advance(suj_jblocks, recsize);
2320 				continue;
2321 			}
2322 			blocks = rec->jsr_blocks;
2323 			recsize = blocks * real_dev_bsize;
2324 			if (recsize > size) {
2325 				/*
2326 				 * We may just have run out of buffer, restart
2327 				 * the loop to re-read from this spot.
2328 				 */
2329 				if (size < fs->fs_bsize &&
2330 				    size != readsize &&
2331 				    recsize <= fs->fs_bsize)
2332 					goto restart;
2333 				if (debug)
2334 					printf("Found invalid segsize %d > %d\n",
2335 					    recsize, size);
2336 				recsize = real_dev_bsize;
2337 				jblocks_advance(suj_jblocks, recsize);
2338 				continue;
2339 			}
2340 			/*
2341 			 * Verify that all blocks in the segment are present.
2342 			 */
2343 			for (i = 1; i < blocks; i++) {
2344 				recn = (void *)((uintptr_t)rec) + i *
2345 				    real_dev_bsize;
2346 				if (recn->jsr_seq == rec->jsr_seq &&
2347 				    recn->jsr_time == rec->jsr_time)
2348 					continue;
2349 				if (debug)
2350 					printf("Incomplete record %jd (%d)\n",
2351 					    rec->jsr_seq, i);
2352 				recsize = i * real_dev_bsize;
2353 				jblocks_advance(suj_jblocks, recsize);
2354 				goto restart;
2355 			}
2356 			seg = errmalloc(sizeof(*seg));
2357 			seg->ss_blk = errmalloc(recsize);
2358 			seg->ss_rec = *rec;
2359 			bcopy((void *)rec, seg->ss_blk, recsize);
2360 			if (rec->jsr_oldest > oldseq)
2361 				oldseq = rec->jsr_oldest;
2362 			TAILQ_INSERT_TAIL(&allsegs, seg, ss_next);
2363 			jblocks_advance(suj_jblocks, recsize);
2364 		}
2365 	}
2366 }
2367 
2368 /*
2369  * Orchestrate the verification of a filesystem via the softupdates journal.
2370  */
2371 int
2372 suj_check(const char *filesys)
2373 {
2374 	struct inodesc idesc;
2375 	struct csum *cgsum;
2376 	union dinode *jip;
2377 	struct inode ip;
2378 	uint64_t blocks;
2379 	int i, retval;
2380 	struct suj_seg *seg;
2381 	struct suj_seg *segn;
2382 
2383 	initsuj();
2384 	fs = &sblock;
2385 	if (real_dev_bsize == 0 && ioctl(fsreadfd, DIOCGSECTORSIZE,
2386 	    &real_dev_bsize) == -1)
2387 		real_dev_bsize = secsize;
2388 	if (debug)
2389 		printf("dev_bsize %u\n", real_dev_bsize);
2390 
2391 	/*
2392 	 * Set an exit point when SUJ check failed
2393 	 */
2394 	retval = setjmp(jmpbuf);
2395 	if (retval != 0) {
2396 		pwarn("UNEXPECTED SU+J INCONSISTENCY\n");
2397 		TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2398 			TAILQ_REMOVE(&allsegs, seg, ss_next);
2399 				free(seg->ss_blk);
2400 				free(seg);
2401 		}
2402 		if (reply("FALLBACK TO FULL FSCK") == 0) {
2403 			ckfini(0);
2404 			exit(EEXIT);
2405 		} else
2406 			return (-1);
2407 	}
2408 
2409 	/*
2410 	 * Search the root directory for the SUJ_FILE.
2411 	 */
2412 	idesc.id_type = DATA;
2413 	idesc.id_fix = IGNORE;
2414 	idesc.id_number = UFS_ROOTINO;
2415 	idesc.id_func = findino;
2416 	idesc.id_name = SUJ_FILE;
2417 	ginode(UFS_ROOTINO, &ip);
2418 	if ((ckinode(ip.i_dp, &idesc) & FOUND) == FOUND) {
2419 		sujino = idesc.id_parent;
2420 		irelse(&ip);
2421 	} else {
2422 		printf("Journal inode removed.  Use tunefs to re-create.\n");
2423 		sblock.fs_flags &= ~FS_SUJ;
2424 		sblock.fs_sujfree = 0;
2425 		irelse(&ip);
2426 		return (-1);
2427 	}
2428 	/*
2429 	 * Fetch the journal inode and verify it.
2430 	 */
2431 	ginode(sujino, &ip);
2432 	jip = ip.i_dp;
2433 	printf("** SU+J Recovering %s\n", filesys);
2434 	if (suj_verifyino(jip) != 0 || (!preen && !reply("USE JOURNAL"))) {
2435 		irelse(&ip);
2436 		return (-1);
2437 	}
2438 	/*
2439 	 * Build a list of journal blocks in jblocks before parsing the
2440 	 * available journal blocks in with suj_read().
2441 	 */
2442 	printf("** Reading %jd byte journal from inode %ju.\n",
2443 	    DIP(jip, di_size), (uintmax_t)sujino);
2444 	suj_jblocks = jblocks_create();
2445 	blocks = ino_visit(jip, sujino, suj_add_block, 0);
2446 	if (blocks != numfrags(fs, DIP(jip, di_size))) {
2447 		printf("Sparse journal inode %ju.\n", (uintmax_t)sujino);
2448 		irelse(&ip);
2449 		return (-1);
2450 	}
2451 	irelse(&ip);
2452 	suj_read();
2453 	jblocks_destroy(suj_jblocks);
2454 	suj_jblocks = NULL;
2455 	if (preen || reply("RECOVER")) {
2456 		printf("** Building recovery table.\n");
2457 		suj_prune();
2458 		suj_build();
2459 		cg_apply(cg_build);
2460 		printf("** Resolving unreferenced inode list.\n");
2461 		ino_unlinked();
2462 		printf("** Processing journal entries.\n");
2463 		cg_apply(cg_trunc);
2464 		cg_apply(cg_check_blk);
2465 		cg_apply(cg_adj_blk);
2466 		cg_apply(cg_check_ino);
2467 	}
2468 	if (preen == 0 && (jrecs > 0 || jbytes > 0) && reply("WRITE CHANGES") == 0)
2469 		return (0);
2470 	/*
2471 	 * Check block counts of snapshot inodes and
2472 	 * make copies of any needed snapshot blocks.
2473 	 */
2474 	for (i = 0; i < snapcnt; i++)
2475 		check_blkcnt(&snaplist[i]);
2476 	snapflush(suj_checkblkavail);
2477 	/*
2478 	 * Recompute the fs summary info from correct cs summaries.
2479 	 */
2480 	bzero(&fs->fs_cstotal, sizeof(struct csum_total));
2481 	for (i = 0; i < fs->fs_ncg; i++) {
2482 		cgsum = &fs->fs_cs(fs, i);
2483 		fs->fs_cstotal.cs_nffree += cgsum->cs_nffree;
2484 		fs->fs_cstotal.cs_nbfree += cgsum->cs_nbfree;
2485 		fs->fs_cstotal.cs_nifree += cgsum->cs_nifree;
2486 		fs->fs_cstotal.cs_ndir += cgsum->cs_ndir;
2487 	}
2488 	fs->fs_pendinginodes = 0;
2489 	fs->fs_pendingblocks = 0;
2490 	fs->fs_clean = 1;
2491 	fs->fs_time = time(NULL);
2492 	fs->fs_mtime = time(NULL);
2493 	sbdirty();
2494 	ckfini(1);
2495 	if (jrecs > 0 || jbytes > 0) {
2496 		printf("** %jd journal records in %jd bytes for %.2f%% utilization\n",
2497 		    jrecs, jbytes, ((float)jrecs / (float)(jbytes / JREC_SIZE)) * 100);
2498 		printf("** Freed %jd inodes (%jd dirs) %jd blocks, and %jd frags.\n",
2499 		    freeinos, freedir, freeblocks, freefrags);
2500 	}
2501 
2502 	return (0);
2503 }
2504 
2505 static void
2506 initsuj(void)
2507 {
2508 	int i;
2509 
2510 	for (i = 0; i < HASHSIZE; i++)
2511 		LIST_INIT(&cghash[i]);
2512 	lastcg = NULL;
2513 	TAILQ_INIT(&allsegs);
2514 	oldseq = 0;
2515 	fs = NULL;
2516 	sujino = 0;
2517 	freefrags = 0;
2518 	freeblocks = 0;
2519 	freeinos = 0;
2520 	freedir = 0;
2521 	jbytes = 0;
2522 	jrecs = 0;
2523 	suj_jblocks = NULL;
2524 }
2525