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