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