suj.c revision 209716
1207141Sjeff/*-
2207141Sjeff * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
3207141Sjeff * All rights reserved.
4207141Sjeff *
5207141Sjeff * Redistribution and use in source and binary forms, with or without
6207141Sjeff * modification, are permitted provided that the following conditions
7207141Sjeff * are met:
8207141Sjeff * 1. Redistributions of source code must retain the above copyright
9207141Sjeff *    notice, this list of conditions and the following disclaimer.
10207141Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11207141Sjeff *    notice, this list of conditions and the following disclaimer in the
12207141Sjeff *    documentation and/or other materials provided with the distribution.
13207141Sjeff *
14207141Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15207141Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16207141Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17207141Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18207141Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19207141Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20207141Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21207141Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22207141Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23207141Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24207141Sjeff * SUCH DAMAGE.
25207141Sjeff */
26207141Sjeff
27207141Sjeff#include <sys/cdefs.h>
28207141Sjeff__FBSDID("$FreeBSD: head/sbin/fsck_ffs/suj.c 209716 2010-07-06 07:07:29Z jeff $");
29207141Sjeff
30207141Sjeff#include <sys/param.h>
31207141Sjeff#include <sys/disklabel.h>
32207141Sjeff#include <sys/mount.h>
33207141Sjeff#include <sys/stat.h>
34207141Sjeff
35207141Sjeff#include <ufs/ufs/ufsmount.h>
36207141Sjeff#include <ufs/ufs/dinode.h>
37207141Sjeff#include <ufs/ufs/dir.h>
38207141Sjeff#include <ufs/ffs/fs.h>
39207141Sjeff
40209408Sdelphij#include <setjmp.h>
41209408Sdelphij#include <stdarg.h>
42207141Sjeff#include <stdio.h>
43207141Sjeff#include <stdlib.h>
44207141Sjeff#include <stdint.h>
45207141Sjeff#include <libufs.h>
46207141Sjeff#include <string.h>
47207141Sjeff#include <strings.h>
48209408Sdelphij#include <sysexits.h>
49207141Sjeff#include <err.h>
50207141Sjeff#include <assert.h>
51207141Sjeff
52207141Sjeff#include "fsck.h"
53207141Sjeff
54207141Sjeff#define	DOTDOT_OFFSET	DIRECTSIZ(1)
55207141Sjeff#define	SUJ_HASHSIZE	2048
56207141Sjeff#define	SUJ_HASHMASK	(SUJ_HASHSIZE - 1)
57207141Sjeff#define	SUJ_HASH(x)	((x * 2654435761) & SUJ_HASHMASK)
58207141Sjeff
59207141Sjeffstruct suj_seg {
60207141Sjeff	TAILQ_ENTRY(suj_seg) ss_next;
61207141Sjeff	struct jsegrec	ss_rec;
62207141Sjeff	uint8_t		*ss_blk;
63207141Sjeff};
64207141Sjeff
65207141Sjeffstruct suj_rec {
66207141Sjeff	TAILQ_ENTRY(suj_rec) sr_next;
67207141Sjeff	union jrec	*sr_rec;
68207141Sjeff};
69207141SjeffTAILQ_HEAD(srechd, suj_rec);
70207141Sjeff
71207141Sjeffstruct suj_ino {
72207141Sjeff	LIST_ENTRY(suj_ino)	si_next;
73207141Sjeff	struct srechd		si_recs;
74207141Sjeff	struct srechd		si_newrecs;
75207141Sjeff	struct srechd		si_movs;
76207141Sjeff	struct jtrncrec		*si_trunc;
77207141Sjeff	ino_t			si_ino;
78207141Sjeff	char			si_skipparent;
79207141Sjeff	char			si_hasrecs;
80207141Sjeff	char			si_blkadj;
81207141Sjeff	char			si_linkadj;
82207141Sjeff	int			si_mode;
83207141Sjeff	nlink_t			si_nlinkadj;
84207141Sjeff	nlink_t			si_nlink;
85207141Sjeff	nlink_t			si_dotlinks;
86207141Sjeff};
87207141SjeffLIST_HEAD(inohd, suj_ino);
88207141Sjeff
89207141Sjeffstruct suj_blk {
90207141Sjeff	LIST_ENTRY(suj_blk)	sb_next;
91207141Sjeff	struct srechd		sb_recs;
92207141Sjeff	ufs2_daddr_t		sb_blk;
93207141Sjeff};
94207141SjeffLIST_HEAD(blkhd, suj_blk);
95207141Sjeff
96207141Sjeffstruct data_blk {
97207141Sjeff	LIST_ENTRY(data_blk)	db_next;
98207141Sjeff	uint8_t			*db_buf;
99207141Sjeff	ufs2_daddr_t		db_blk;
100207141Sjeff	int			db_size;
101207141Sjeff	int			db_dirty;
102207141Sjeff};
103207141Sjeff
104207141Sjeffstruct ino_blk {
105207141Sjeff	LIST_ENTRY(ino_blk)	ib_next;
106207141Sjeff	uint8_t			*ib_buf;
107207141Sjeff	int			ib_dirty;
108207141Sjeff	ufs2_daddr_t		ib_blk;
109207141Sjeff};
110207141SjeffLIST_HEAD(iblkhd, ino_blk);
111207141Sjeff
112207141Sjeffstruct suj_cg {
113207141Sjeff	LIST_ENTRY(suj_cg)	sc_next;
114207141Sjeff	struct blkhd		sc_blkhash[SUJ_HASHSIZE];
115207141Sjeff	struct inohd		sc_inohash[SUJ_HASHSIZE];
116207141Sjeff	struct iblkhd		sc_iblkhash[SUJ_HASHSIZE];
117207141Sjeff	struct ino_blk		*sc_lastiblk;
118207141Sjeff	struct suj_ino		*sc_lastino;
119207141Sjeff	struct suj_blk		*sc_lastblk;
120207141Sjeff	uint8_t			*sc_cgbuf;
121207141Sjeff	struct cg		*sc_cgp;
122207141Sjeff	int			sc_dirty;
123207141Sjeff	int			sc_cgx;
124207141Sjeff};
125207141Sjeff
126207141SjeffLIST_HEAD(cghd, suj_cg) cghash[SUJ_HASHSIZE];
127207141SjeffLIST_HEAD(dblkhd, data_blk) dbhash[SUJ_HASHSIZE];
128207141Sjeffstruct suj_cg *lastcg;
129207141Sjeffstruct data_blk *lastblk;
130207141Sjeff
131207141SjeffTAILQ_HEAD(seghd, suj_seg) allsegs;
132207141Sjeffuint64_t oldseq;
133207141Sjeffstatic struct uufsd *disk = NULL;
134207141Sjeffstatic struct fs *fs = NULL;
135207141Sjeffino_t sujino;
136207141Sjeff
137207141Sjeff/*
138207141Sjeff * Summary statistics.
139207141Sjeff */
140207141Sjeffuint64_t freefrags;
141207141Sjeffuint64_t freeblocks;
142207141Sjeffuint64_t freeinos;
143207141Sjeffuint64_t freedir;
144207141Sjeffuint64_t jbytes;
145207141Sjeffuint64_t jrecs;
146207141Sjeff
147209408Sdelphijstatic jmp_buf	jmpbuf;
148209408Sdelphij
149207141Sjefftypedef void (*ino_visitor)(ino_t, ufs_lbn_t, ufs2_daddr_t, int);
150209408Sdelphijstatic void err_suj(const char *, ...) __dead2;
151207141Sjeffstatic void ino_trunc(ino_t, off_t);
152207141Sjeffstatic void ino_decr(ino_t);
153207141Sjeffstatic void ino_adjust(struct suj_ino *);
154207141Sjeffstatic void ino_build(struct suj_ino *);
155207141Sjeffstatic int blk_isfree(ufs2_daddr_t);
156207141Sjeff
157207141Sjeffstatic void *
158207141Sjefferrmalloc(size_t n)
159207141Sjeff{
160207141Sjeff	void *a;
161207141Sjeff
162207141Sjeff	a = malloc(n);
163207141Sjeff	if (a == NULL)
164209408Sdelphij		err(EX_OSERR, "malloc(%zu)", n);
165207141Sjeff	return (a);
166207141Sjeff}
167207141Sjeff
168207141Sjeff/*
169209408Sdelphij * When hit a fatal error in journalling check, print out
170209408Sdelphij * the error and then offer to fallback to normal fsck.
171209408Sdelphij */
172209408Sdelphijstatic void
173209408Sdelphijerr_suj(const char * restrict fmt, ...)
174209408Sdelphij{
175209408Sdelphij	va_list ap;
176209408Sdelphij
177209408Sdelphij	if (preen)
178209408Sdelphij		(void)fprintf(stdout, "%s: ", cdevname);
179209408Sdelphij
180209408Sdelphij	va_start(ap, fmt);
181209408Sdelphij	(void)vfprintf(stdout, fmt, ap);
182209408Sdelphij	va_end(ap);
183209408Sdelphij
184209408Sdelphij	longjmp(jmpbuf, -1);
185209408Sdelphij}
186209408Sdelphij
187209408Sdelphij/*
188207141Sjeff * Open the given provider, load superblock.
189207141Sjeff */
190207141Sjeffstatic void
191207141Sjeffopendisk(const char *devnam)
192207141Sjeff{
193207141Sjeff	if (disk != NULL)
194207141Sjeff		return;
195207141Sjeff	disk = malloc(sizeof(*disk));
196207141Sjeff	if (disk == NULL)
197209408Sdelphij		err(EX_OSERR, "malloc(%zu)", sizeof(*disk));
198207141Sjeff	if (ufs_disk_fillout(disk, devnam) == -1) {
199209408Sdelphij		err(EX_OSERR, "ufs_disk_fillout(%s) failed: %s", devnam,
200207141Sjeff		    disk->d_error);
201207141Sjeff	}
202207141Sjeff	fs = &disk->d_fs;
203207141Sjeff}
204207141Sjeff
205207141Sjeff/*
206207141Sjeff * Mark file system as clean, write the super-block back, close the disk.
207207141Sjeff */
208207141Sjeffstatic void
209207141Sjeffclosedisk(const char *devnam)
210207141Sjeff{
211207141Sjeff	struct csum *cgsum;
212207141Sjeff	int i;
213207141Sjeff
214207141Sjeff	/*
215207141Sjeff	 * Recompute the fs summary info from correct cs summaries.
216207141Sjeff	 */
217207141Sjeff	bzero(&fs->fs_cstotal, sizeof(struct csum_total));
218207141Sjeff	for (i = 0; i < fs->fs_ncg; i++) {
219207141Sjeff		cgsum = &fs->fs_cs(fs, i);
220207141Sjeff		fs->fs_cstotal.cs_nffree += cgsum->cs_nffree;
221207141Sjeff		fs->fs_cstotal.cs_nbfree += cgsum->cs_nbfree;
222207141Sjeff		fs->fs_cstotal.cs_nifree += cgsum->cs_nifree;
223207141Sjeff		fs->fs_cstotal.cs_ndir += cgsum->cs_ndir;
224207141Sjeff	}
225207141Sjeff	fs->fs_pendinginodes = 0;
226207141Sjeff	fs->fs_pendingblocks = 0;
227207141Sjeff	fs->fs_clean = 1;
228207141Sjeff	fs->fs_time = time(NULL);
229207141Sjeff	fs->fs_mtime = time(NULL);
230207141Sjeff	if (sbwrite(disk, 0) == -1)
231209408Sdelphij		err(EX_OSERR, "sbwrite(%s)", devnam);
232207141Sjeff	if (ufs_disk_close(disk) == -1)
233209408Sdelphij		err(EX_OSERR, "ufs_disk_close(%s)", devnam);
234207141Sjeff	free(disk);
235207141Sjeff	disk = NULL;
236207141Sjeff	fs = NULL;
237207141Sjeff}
238207141Sjeff
239207141Sjeff/*
240207141Sjeff * Lookup a cg by number in the hash so we can keep track of which cgs
241207141Sjeff * need stats rebuilt.
242207141Sjeff */
243207141Sjeffstatic struct suj_cg *
244207141Sjeffcg_lookup(int cgx)
245207141Sjeff{
246207141Sjeff	struct cghd *hd;
247207141Sjeff	struct suj_cg *sc;
248207141Sjeff
249209408Sdelphij	if (cgx < 0 || cgx >= fs->fs_ncg)
250209408Sdelphij		err_suj("Bad cg number %d\n", cgx);
251207141Sjeff	if (lastcg && lastcg->sc_cgx == cgx)
252207141Sjeff		return (lastcg);
253207141Sjeff	hd = &cghash[SUJ_HASH(cgx)];
254207141Sjeff	LIST_FOREACH(sc, hd, sc_next)
255207141Sjeff		if (sc->sc_cgx == cgx) {
256207141Sjeff			lastcg = sc;
257207141Sjeff			return (sc);
258207141Sjeff		}
259207141Sjeff	sc = errmalloc(sizeof(*sc));
260207141Sjeff	bzero(sc, sizeof(*sc));
261207141Sjeff	sc->sc_cgbuf = errmalloc(fs->fs_bsize);
262207141Sjeff	sc->sc_cgp = (struct cg *)sc->sc_cgbuf;
263207141Sjeff	sc->sc_cgx = cgx;
264207141Sjeff	LIST_INSERT_HEAD(hd, sc, sc_next);
265207141Sjeff	if (bread(disk, fsbtodb(fs, cgtod(fs, sc->sc_cgx)), sc->sc_cgbuf,
266207141Sjeff	    fs->fs_bsize) == -1)
267209408Sdelphij		err_suj("Unable to read cylinder group %d\n", sc->sc_cgx);
268207141Sjeff
269207141Sjeff	return (sc);
270207141Sjeff}
271207141Sjeff
272207141Sjeff/*
273207141Sjeff * Lookup an inode number in the hash and allocate a suj_ino if it does
274207141Sjeff * not exist.
275207141Sjeff */
276207141Sjeffstatic struct suj_ino *
277207141Sjeffino_lookup(ino_t ino, int creat)
278207141Sjeff{
279207141Sjeff	struct suj_ino *sino;
280207141Sjeff	struct inohd *hd;
281207141Sjeff	struct suj_cg *sc;
282207141Sjeff
283207141Sjeff	sc = cg_lookup(ino_to_cg(fs, ino));
284207141Sjeff	if (sc->sc_lastino && sc->sc_lastino->si_ino == ino)
285207141Sjeff		return (sc->sc_lastino);
286207141Sjeff	hd = &sc->sc_inohash[SUJ_HASH(ino)];
287207141Sjeff	LIST_FOREACH(sino, hd, si_next)
288207141Sjeff		if (sino->si_ino == ino)
289207141Sjeff			return (sino);
290207141Sjeff	if (creat == 0)
291207141Sjeff		return (NULL);
292207141Sjeff	sino = errmalloc(sizeof(*sino));
293207141Sjeff	bzero(sino, sizeof(*sino));
294207141Sjeff	sino->si_ino = ino;
295207141Sjeff	TAILQ_INIT(&sino->si_recs);
296207141Sjeff	TAILQ_INIT(&sino->si_newrecs);
297207141Sjeff	TAILQ_INIT(&sino->si_movs);
298207141Sjeff	LIST_INSERT_HEAD(hd, sino, si_next);
299207141Sjeff
300207141Sjeff	return (sino);
301207141Sjeff}
302207141Sjeff
303207141Sjeff/*
304207141Sjeff * Lookup a block number in the hash and allocate a suj_blk if it does
305207141Sjeff * not exist.
306207141Sjeff */
307207141Sjeffstatic struct suj_blk *
308207141Sjeffblk_lookup(ufs2_daddr_t blk, int creat)
309207141Sjeff{
310207141Sjeff	struct suj_blk *sblk;
311207141Sjeff	struct suj_cg *sc;
312207141Sjeff	struct blkhd *hd;
313207141Sjeff
314207141Sjeff	sc = cg_lookup(dtog(fs, blk));
315207141Sjeff	if (sc->sc_lastblk && sc->sc_lastblk->sb_blk == blk)
316207141Sjeff		return (sc->sc_lastblk);
317207141Sjeff	hd = &sc->sc_blkhash[SUJ_HASH(fragstoblks(fs, blk))];
318207141Sjeff	LIST_FOREACH(sblk, hd, sb_next)
319207141Sjeff		if (sblk->sb_blk == blk)
320207141Sjeff			return (sblk);
321207141Sjeff	if (creat == 0)
322207141Sjeff		return (NULL);
323207141Sjeff	sblk = errmalloc(sizeof(*sblk));
324207141Sjeff	bzero(sblk, sizeof(*sblk));
325207141Sjeff	sblk->sb_blk = blk;
326207141Sjeff	TAILQ_INIT(&sblk->sb_recs);
327207141Sjeff	LIST_INSERT_HEAD(hd, sblk, sb_next);
328207141Sjeff
329207141Sjeff	return (sblk);
330207141Sjeff}
331207141Sjeff
332207141Sjeffstatic struct data_blk *
333207141Sjeffdblk_lookup(ufs2_daddr_t blk)
334207141Sjeff{
335207141Sjeff	struct data_blk *dblk;
336207141Sjeff	struct dblkhd *hd;
337207141Sjeff
338207141Sjeff	hd = &dbhash[SUJ_HASH(fragstoblks(fs, blk))];
339207141Sjeff	if (lastblk && lastblk->db_blk == blk)
340207141Sjeff		return (lastblk);
341207141Sjeff	LIST_FOREACH(dblk, hd, db_next)
342207141Sjeff		if (dblk->db_blk == blk)
343207141Sjeff			return (dblk);
344207141Sjeff	/*
345207141Sjeff	 * The inode block wasn't located, allocate a new one.
346207141Sjeff	 */
347207141Sjeff	dblk = errmalloc(sizeof(*dblk));
348207141Sjeff	bzero(dblk, sizeof(*dblk));
349207141Sjeff	LIST_INSERT_HEAD(hd, dblk, db_next);
350207141Sjeff	dblk->db_blk = blk;
351207141Sjeff	return (dblk);
352207141Sjeff}
353207141Sjeff
354207141Sjeffstatic uint8_t *
355207141Sjeffdblk_read(ufs2_daddr_t blk, int size)
356207141Sjeff{
357207141Sjeff	struct data_blk *dblk;
358207141Sjeff
359207141Sjeff	dblk = dblk_lookup(blk);
360207141Sjeff	/*
361207141Sjeff	 * I doubt size mismatches can happen in practice but it is trivial
362207141Sjeff	 * to handle.
363207141Sjeff	 */
364207141Sjeff	if (size != dblk->db_size) {
365207141Sjeff		if (dblk->db_buf)
366207141Sjeff			free(dblk->db_buf);
367207141Sjeff		dblk->db_buf = errmalloc(size);
368207141Sjeff		dblk->db_size = size;
369207141Sjeff		if (bread(disk, fsbtodb(fs, blk), dblk->db_buf, size) == -1)
370209408Sdelphij			err_suj("Failed to read data block %jd\n", blk);
371207141Sjeff	}
372207141Sjeff	return (dblk->db_buf);
373207141Sjeff}
374207141Sjeff
375207141Sjeffstatic void
376207141Sjeffdblk_dirty(ufs2_daddr_t blk)
377207141Sjeff{
378207141Sjeff	struct data_blk *dblk;
379207141Sjeff
380207141Sjeff	dblk = dblk_lookup(blk);
381207141Sjeff	dblk->db_dirty = 1;
382207141Sjeff}
383207141Sjeff
384207141Sjeffstatic void
385207141Sjeffdblk_write(void)
386207141Sjeff{
387207141Sjeff	struct data_blk *dblk;
388207141Sjeff	int i;
389207141Sjeff
390207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++) {
391207141Sjeff		LIST_FOREACH(dblk, &dbhash[i], db_next) {
392207141Sjeff			if (dblk->db_dirty == 0 || dblk->db_size == 0)
393207141Sjeff				continue;
394207141Sjeff			if (bwrite(disk, fsbtodb(fs, dblk->db_blk),
395207141Sjeff			    dblk->db_buf, dblk->db_size) == -1)
396209408Sdelphij				err_suj("Unable to write block %jd\n",
397207141Sjeff				    dblk->db_blk);
398207141Sjeff		}
399207141Sjeff	}
400207141Sjeff}
401207141Sjeff
402207141Sjeffstatic union dinode *
403207141Sjeffino_read(ino_t ino)
404207141Sjeff{
405207141Sjeff	struct ino_blk *iblk;
406207141Sjeff	struct iblkhd *hd;
407207141Sjeff	struct suj_cg *sc;
408207141Sjeff	ufs2_daddr_t blk;
409207141Sjeff	int off;
410207141Sjeff
411207141Sjeff	blk = ino_to_fsba(fs, ino);
412207141Sjeff	sc = cg_lookup(ino_to_cg(fs, ino));
413207141Sjeff	iblk = sc->sc_lastiblk;
414207141Sjeff	if (iblk && iblk->ib_blk == blk)
415207141Sjeff		goto found;
416207141Sjeff	hd = &sc->sc_iblkhash[SUJ_HASH(fragstoblks(fs, blk))];
417207141Sjeff	LIST_FOREACH(iblk, hd, ib_next)
418207141Sjeff		if (iblk->ib_blk == blk)
419207141Sjeff			goto found;
420207141Sjeff	/*
421207141Sjeff	 * The inode block wasn't located, allocate a new one.
422207141Sjeff	 */
423207141Sjeff	iblk = errmalloc(sizeof(*iblk));
424207141Sjeff	bzero(iblk, sizeof(*iblk));
425207141Sjeff	iblk->ib_buf = errmalloc(fs->fs_bsize);
426207141Sjeff	iblk->ib_blk = blk;
427207141Sjeff	LIST_INSERT_HEAD(hd, iblk, ib_next);
428207141Sjeff	if (bread(disk, fsbtodb(fs, blk), iblk->ib_buf, fs->fs_bsize) == -1)
429209408Sdelphij		err_suj("Failed to read inode block %jd\n", blk);
430207141Sjefffound:
431207141Sjeff	sc->sc_lastiblk = iblk;
432207141Sjeff	off = ino_to_fsbo(fs, ino);
433207141Sjeff	if (fs->fs_magic == FS_UFS1_MAGIC)
434207141Sjeff		return (union dinode *)&((struct ufs1_dinode *)iblk->ib_buf)[off];
435207141Sjeff	else
436207141Sjeff		return (union dinode *)&((struct ufs2_dinode *)iblk->ib_buf)[off];
437207141Sjeff}
438207141Sjeff
439207141Sjeffstatic void
440207141Sjeffino_dirty(ino_t ino)
441207141Sjeff{
442207141Sjeff	struct ino_blk *iblk;
443207141Sjeff	struct iblkhd *hd;
444207141Sjeff	struct suj_cg *sc;
445207141Sjeff	ufs2_daddr_t blk;
446207141Sjeff
447207141Sjeff	blk = ino_to_fsba(fs, ino);
448207141Sjeff	sc = cg_lookup(ino_to_cg(fs, ino));
449207141Sjeff	iblk = sc->sc_lastiblk;
450207141Sjeff	if (iblk && iblk->ib_blk == blk) {
451207141Sjeff		iblk->ib_dirty = 1;
452207141Sjeff		return;
453207141Sjeff	}
454207141Sjeff	hd = &sc->sc_iblkhash[SUJ_HASH(fragstoblks(fs, blk))];
455207141Sjeff	LIST_FOREACH(iblk, hd, ib_next) {
456207141Sjeff		if (iblk->ib_blk == blk) {
457207141Sjeff			iblk->ib_dirty = 1;
458207141Sjeff			return;
459207141Sjeff		}
460207141Sjeff	}
461207141Sjeff	ino_read(ino);
462207141Sjeff	ino_dirty(ino);
463207141Sjeff}
464207141Sjeff
465207141Sjeffstatic void
466207141Sjeffiblk_write(struct ino_blk *iblk)
467207141Sjeff{
468207141Sjeff
469207141Sjeff	if (iblk->ib_dirty == 0)
470207141Sjeff		return;
471207141Sjeff	if (bwrite(disk, fsbtodb(fs, iblk->ib_blk), iblk->ib_buf,
472207141Sjeff	    fs->fs_bsize) == -1)
473209408Sdelphij		err_suj("Failed to write inode block %jd\n", iblk->ib_blk);
474207141Sjeff}
475207141Sjeff
476207141Sjeffstatic int
477207141Sjeffblk_overlaps(struct jblkrec *brec, ufs2_daddr_t start, int frags)
478207141Sjeff{
479207141Sjeff	ufs2_daddr_t bstart;
480207141Sjeff	ufs2_daddr_t bend;
481207141Sjeff	ufs2_daddr_t end;
482207141Sjeff
483207141Sjeff	end = start + frags;
484207141Sjeff	bstart = brec->jb_blkno + brec->jb_oldfrags;
485207141Sjeff	bend = bstart + brec->jb_frags;
486207141Sjeff	if (start < bend && end > bstart)
487207141Sjeff		return (1);
488207141Sjeff	return (0);
489207141Sjeff}
490207141Sjeff
491207141Sjeffstatic int
492207141Sjeffblk_equals(struct jblkrec *brec, ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t start,
493207141Sjeff    int frags)
494207141Sjeff{
495207141Sjeff
496207141Sjeff	if (brec->jb_ino != ino || brec->jb_lbn != lbn)
497207141Sjeff		return (0);
498207141Sjeff	if (brec->jb_blkno + brec->jb_oldfrags != start)
499207141Sjeff		return (0);
500207141Sjeff	if (brec->jb_frags != frags)
501207141Sjeff		return (0);
502207141Sjeff	return (1);
503207141Sjeff}
504207141Sjeff
505207141Sjeffstatic void
506207141Sjeffblk_setmask(struct jblkrec *brec, int *mask)
507207141Sjeff{
508207141Sjeff	int i;
509207141Sjeff
510207141Sjeff	for (i = brec->jb_oldfrags; i < brec->jb_oldfrags + brec->jb_frags; i++)
511207141Sjeff		*mask |= 1 << i;
512207141Sjeff}
513207141Sjeff
514207141Sjeff/*
515207141Sjeff * Determine whether a given block has been reallocated to a new location.
516207141Sjeff * Returns a mask of overlapping bits if any frags have been reused or
517207141Sjeff * zero if the block has not been re-used and the contents can be trusted.
518207141Sjeff *
519207141Sjeff * This is used to ensure that an orphaned pointer due to truncate is safe
520207141Sjeff * to be freed.  The mask value can be used to free partial blocks.
521207141Sjeff */
522207141Sjeffstatic int
523207141Sjeffblk_freemask(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags)
524207141Sjeff{
525207141Sjeff	struct suj_blk *sblk;
526207141Sjeff	struct suj_rec *srec;
527207141Sjeff	struct jblkrec *brec;
528207141Sjeff	int mask;
529207141Sjeff	int off;
530207141Sjeff
531207141Sjeff	/*
532207141Sjeff	 * To be certain we're not freeing a reallocated block we lookup
533207141Sjeff	 * this block in the blk hash and see if there is an allocation
534207141Sjeff	 * journal record that overlaps with any fragments in the block
535207141Sjeff	 * we're concerned with.  If any fragments have ben reallocated
536207141Sjeff	 * the block has already been freed and re-used for another purpose.
537207141Sjeff	 */
538207141Sjeff	mask = 0;
539207141Sjeff	sblk = blk_lookup(blknum(fs, blk), 0);
540207141Sjeff	if (sblk == NULL)
541207141Sjeff		return (0);
542207141Sjeff	off = blk - sblk->sb_blk;
543207141Sjeff	TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
544207141Sjeff		brec = (struct jblkrec *)srec->sr_rec;
545207141Sjeff		/*
546207141Sjeff		 * If the block overlaps but does not match
547207141Sjeff		 * exactly it's a new allocation.  If it matches
548207141Sjeff		 * exactly this record refers to the current
549207141Sjeff		 * location.
550207141Sjeff		 */
551207141Sjeff		if (blk_overlaps(brec, blk, frags) == 0)
552207141Sjeff			continue;
553207141Sjeff		if (blk_equals(brec, ino, lbn, blk, frags) == 1)
554207141Sjeff			mask = 0;
555207141Sjeff		else
556207141Sjeff			blk_setmask(brec, &mask);
557207141Sjeff	}
558207141Sjeff	if (debug)
559207141Sjeff		printf("blk_freemask: blk %jd sblk %jd off %d mask 0x%X\n",
560207141Sjeff		    blk, sblk->sb_blk, off, mask);
561207141Sjeff	return (mask >> off);
562207141Sjeff}
563207141Sjeff
564207141Sjeff/*
565207141Sjeff * Determine whether it is safe to follow an indirect.  It is not safe
566207141Sjeff * if any part of the indirect has been reallocated or the last journal
567207141Sjeff * entry was an allocation.  Just allocated indirects may not have valid
568207141Sjeff * pointers yet and all of their children will have their own records.
569207141Sjeff * It is also not safe to follow an indirect if the cg bitmap has been
570207141Sjeff * cleared as a new allocation may write to the block prior to the journal
571207141Sjeff * being written.
572207141Sjeff *
573207141Sjeff * Returns 1 if it's safe to follow the indirect and 0 otherwise.
574207141Sjeff */
575207141Sjeffstatic int
576207141Sjeffblk_isindir(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn)
577207141Sjeff{
578207141Sjeff	struct suj_blk *sblk;
579207141Sjeff	struct jblkrec *brec;
580207141Sjeff
581207141Sjeff	sblk = blk_lookup(blk, 0);
582207141Sjeff	if (sblk == NULL)
583207141Sjeff		return (1);
584207141Sjeff	if (TAILQ_EMPTY(&sblk->sb_recs))
585207141Sjeff		return (1);
586207141Sjeff	brec = (struct jblkrec *)TAILQ_LAST(&sblk->sb_recs, srechd)->sr_rec;
587207141Sjeff	if (blk_equals(brec, ino, lbn, blk, fs->fs_frag))
588207141Sjeff		if (brec->jb_op == JOP_FREEBLK)
589207141Sjeff			return (!blk_isfree(blk));
590207141Sjeff	return (0);
591207141Sjeff}
592207141Sjeff
593207141Sjeff/*
594207141Sjeff * Clear an inode from the cg bitmap.  If the inode was already clear return
595207141Sjeff * 0 so the caller knows it does not have to check the inode contents.
596207141Sjeff */
597207141Sjeffstatic int
598207141Sjeffino_free(ino_t ino, int mode)
599207141Sjeff{
600207141Sjeff	struct suj_cg *sc;
601207141Sjeff	uint8_t *inosused;
602207141Sjeff	struct cg *cgp;
603207141Sjeff	int cg;
604207141Sjeff
605207141Sjeff	cg = ino_to_cg(fs, ino);
606207141Sjeff	ino = ino % fs->fs_ipg;
607207141Sjeff	sc = cg_lookup(cg);
608207141Sjeff	cgp = sc->sc_cgp;
609207141Sjeff	inosused = cg_inosused(cgp);
610207141Sjeff	/*
611207141Sjeff	 * The bitmap may never have made it to the disk so we have to
612207141Sjeff	 * conditionally clear.  We can avoid writing the cg in this case.
613207141Sjeff	 */
614207141Sjeff	if (isclr(inosused, ino))
615207141Sjeff		return (0);
616207141Sjeff	freeinos++;
617207141Sjeff	clrbit(inosused, ino);
618207141Sjeff	if (ino < cgp->cg_irotor)
619207141Sjeff		cgp->cg_irotor = ino;
620207141Sjeff	cgp->cg_cs.cs_nifree++;
621207141Sjeff	if ((mode & IFMT) == IFDIR) {
622207141Sjeff		freedir++;
623207141Sjeff		cgp->cg_cs.cs_ndir--;
624207141Sjeff	}
625207141Sjeff	sc->sc_dirty = 1;
626207141Sjeff
627207141Sjeff	return (1);
628207141Sjeff}
629207141Sjeff
630207141Sjeff/*
631207141Sjeff * Free 'frags' frags starting at filesystem block 'bno' skipping any frags
632207141Sjeff * set in the mask.
633207141Sjeff */
634207141Sjeffstatic void
635207141Sjeffblk_free(ufs2_daddr_t bno, int mask, int frags)
636207141Sjeff{
637207141Sjeff	ufs1_daddr_t fragno, cgbno;
638207141Sjeff	struct suj_cg *sc;
639207141Sjeff	struct cg *cgp;
640207141Sjeff	int i, cg;
641207141Sjeff	uint8_t *blksfree;
642207141Sjeff
643207141Sjeff	if (debug)
644207141Sjeff		printf("Freeing %d frags at blk %jd\n", frags, bno);
645207141Sjeff	cg = dtog(fs, bno);
646207141Sjeff	sc = cg_lookup(cg);
647207141Sjeff	cgp = sc->sc_cgp;
648207141Sjeff	cgbno = dtogd(fs, bno);
649207141Sjeff	blksfree = cg_blksfree(cgp);
650207141Sjeff
651207141Sjeff	/*
652207141Sjeff	 * If it's not allocated we only wrote the journal entry
653207141Sjeff	 * and never the bitmaps.  Here we unconditionally clear and
654207141Sjeff	 * resolve the cg summary later.
655207141Sjeff	 */
656207141Sjeff	if (frags == fs->fs_frag && mask == 0) {
657207141Sjeff		fragno = fragstoblks(fs, cgbno);
658207141Sjeff		ffs_setblock(fs, blksfree, fragno);
659207141Sjeff		freeblocks++;
660207141Sjeff	} else {
661207141Sjeff		/*
662207141Sjeff		 * deallocate the fragment
663207141Sjeff		 */
664207141Sjeff		for (i = 0; i < frags; i++)
665207141Sjeff			if ((mask & (1 << i)) == 0 && isclr(blksfree, cgbno +i)) {
666207141Sjeff				freefrags++;
667207141Sjeff				setbit(blksfree, cgbno + i);
668207141Sjeff			}
669207141Sjeff	}
670207141Sjeff	sc->sc_dirty = 1;
671207141Sjeff}
672207141Sjeff
673207141Sjeff/*
674207141Sjeff * Returns 1 if the whole block starting at 'bno' is marked free and 0
675207141Sjeff * otherwise.
676207141Sjeff */
677207141Sjeffstatic int
678207141Sjeffblk_isfree(ufs2_daddr_t bno)
679207141Sjeff{
680207141Sjeff	struct suj_cg *sc;
681207141Sjeff
682207141Sjeff	sc = cg_lookup(dtog(fs, bno));
683207141Sjeff	return ffs_isblock(fs, cg_blksfree(sc->sc_cgp), dtogd(fs, bno));
684207141Sjeff}
685207141Sjeff
686207141Sjeff/*
687207141Sjeff * Fetch an indirect block to find the block at a given lbn.  The lbn
688207141Sjeff * may be negative to fetch a specific indirect block pointer or positive
689207141Sjeff * to fetch a specific block.
690207141Sjeff */
691207141Sjeffstatic ufs2_daddr_t
692207141Sjeffindir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn)
693207141Sjeff{
694207141Sjeff	ufs2_daddr_t *bap2;
695207141Sjeff	ufs2_daddr_t *bap1;
696207141Sjeff	ufs_lbn_t lbnadd;
697207141Sjeff	ufs_lbn_t base;
698207141Sjeff	int level;
699207141Sjeff	int i;
700207141Sjeff
701207141Sjeff	if (blk == 0)
702207141Sjeff		return (0);
703207141Sjeff	level = lbn_level(cur);
704207141Sjeff	if (level == -1)
705209408Sdelphij		err_suj("Invalid indir lbn %jd\n", lbn);
706207141Sjeff	if (level == 0 && lbn < 0)
707209408Sdelphij		err_suj("Invalid lbn %jd\n", lbn);
708207141Sjeff	bap2 = (void *)dblk_read(blk, fs->fs_bsize);
709207141Sjeff	bap1 = (void *)bap2;
710207141Sjeff	lbnadd = 1;
711207141Sjeff	base = -(cur + level);
712207141Sjeff	for (i = level; i > 0; i--)
713207141Sjeff		lbnadd *= NINDIR(fs);
714207141Sjeff	if (lbn > 0)
715207141Sjeff		i = (lbn - base) / lbnadd;
716207141Sjeff	else
717207141Sjeff		i = (-lbn - base) / lbnadd;
718207141Sjeff	if (i < 0 || i >= NINDIR(fs))
719209408Sdelphij		err_suj("Invalid indirect index %d produced by lbn %jd\n",
720207141Sjeff		    i, lbn);
721207141Sjeff	if (level == 0)
722207141Sjeff		cur = base + (i * lbnadd);
723207141Sjeff	else
724207141Sjeff		cur = -(base + (i * lbnadd)) - (level - 1);
725207141Sjeff	if (fs->fs_magic == FS_UFS1_MAGIC)
726207141Sjeff		blk = bap1[i];
727207141Sjeff	else
728207141Sjeff		blk = bap2[i];
729207141Sjeff	if (cur == lbn)
730207141Sjeff		return (blk);
731209408Sdelphij	if (level == 0)
732209408Sdelphij		err_suj("Invalid lbn %jd at level 0\n", lbn);
733207141Sjeff	return indir_blkatoff(blk, ino, cur, lbn);
734207141Sjeff}
735207141Sjeff
736207141Sjeff/*
737207141Sjeff * Finds the disk block address at the specified lbn within the inode
738207141Sjeff * specified by ip.  This follows the whole tree and honors di_size and
739207141Sjeff * di_extsize so it is a true test of reachability.  The lbn may be
740207141Sjeff * negative if an extattr or indirect block is requested.
741207141Sjeff */
742207141Sjeffstatic ufs2_daddr_t
743207141Sjeffino_blkatoff(union dinode *ip, ino_t ino, ufs_lbn_t lbn, int *frags)
744207141Sjeff{
745207141Sjeff	ufs_lbn_t tmpval;
746207141Sjeff	ufs_lbn_t cur;
747207141Sjeff	ufs_lbn_t next;
748207141Sjeff	int i;
749207141Sjeff
750207141Sjeff	/*
751207141Sjeff	 * Handle extattr blocks first.
752207141Sjeff	 */
753207141Sjeff	if (lbn < 0 && lbn >= -NXADDR) {
754207141Sjeff		lbn = -1 - lbn;
755207141Sjeff		if (lbn > lblkno(fs, ip->dp2.di_extsize - 1))
756207141Sjeff			return (0);
757207141Sjeff		*frags = numfrags(fs, sblksize(fs, ip->dp2.di_extsize, lbn));
758207141Sjeff		return (ip->dp2.di_extb[lbn]);
759207141Sjeff	}
760207141Sjeff	/*
761207141Sjeff	 * Now direct and indirect.
762207141Sjeff	 */
763207141Sjeff	if (DIP(ip, di_mode) == IFLNK &&
764207141Sjeff	    DIP(ip, di_size) < fs->fs_maxsymlinklen)
765207141Sjeff		return (0);
766207141Sjeff	if (lbn >= 0 && lbn < NDADDR) {
767207141Sjeff		*frags = numfrags(fs, sblksize(fs, DIP(ip, di_size), lbn));
768207141Sjeff		return (DIP(ip, di_db[lbn]));
769207141Sjeff	}
770207141Sjeff	*frags = fs->fs_frag;
771207141Sjeff
772207141Sjeff	for (i = 0, tmpval = NINDIR(fs), cur = NDADDR; i < NIADDR; i++,
773207141Sjeff	    tmpval *= NINDIR(fs), cur = next) {
774207141Sjeff		next = cur + tmpval;
775207141Sjeff		if (lbn == -cur - i)
776207141Sjeff			return (DIP(ip, di_ib[i]));
777207141Sjeff		/*
778207141Sjeff		 * Determine whether the lbn in question is within this tree.
779207141Sjeff		 */
780207141Sjeff		if (lbn < 0 && -lbn >= next)
781207141Sjeff			continue;
782207141Sjeff		if (lbn > 0 && lbn >= next)
783207141Sjeff			continue;
784207141Sjeff		return indir_blkatoff(DIP(ip, di_ib[i]), ino, -cur - i, lbn);
785207141Sjeff	}
786209408Sdelphij	err_suj("lbn %jd not in ino\n", lbn);
787209408Sdelphij	/* NOTREACHED */
788207141Sjeff}
789207141Sjeff
790207141Sjeff/*
791207141Sjeff * Determine whether a block exists at a particular lbn in an inode.
792207141Sjeff * Returns 1 if found, 0 if not.  lbn may be negative for indirects
793207141Sjeff * or ext blocks.
794207141Sjeff */
795207141Sjeffstatic int
796207141Sjeffblk_isat(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int *frags)
797207141Sjeff{
798207141Sjeff	union dinode *ip;
799207141Sjeff	ufs2_daddr_t nblk;
800207141Sjeff
801207141Sjeff	ip = ino_read(ino);
802207141Sjeff
803207141Sjeff	if (DIP(ip, di_nlink) == 0 || DIP(ip, di_mode) == 0)
804207141Sjeff		return (0);
805207141Sjeff	nblk = ino_blkatoff(ip, ino, lbn, frags);
806207141Sjeff
807207141Sjeff	return (nblk == blk);
808207141Sjeff}
809207141Sjeff
810207141Sjeff/*
811209716Sjeff * Clear the directory entry at diroff that should point to child.  Minimal
812209716Sjeff * checking is done and it is assumed that this path was verified with isat.
813209716Sjeff */
814209716Sjeffstatic void
815209716Sjeffino_clrat(ino_t parent, off_t diroff, ino_t child)
816209716Sjeff{
817209716Sjeff	union dinode *dip;
818209716Sjeff	struct direct *dp;
819209716Sjeff	ufs2_daddr_t blk;
820209716Sjeff	uint8_t *block;
821209716Sjeff	ufs_lbn_t lbn;
822209716Sjeff	int blksize;
823209716Sjeff	int frags;
824209716Sjeff	int doff;
825209716Sjeff
826209716Sjeff	if (debug)
827209716Sjeff		printf("Clearing inode %d from parent %d at offset %jd\n",
828209716Sjeff		    child, parent, diroff);
829209716Sjeff
830209716Sjeff	lbn = lblkno(fs, diroff);
831209716Sjeff	doff = blkoff(fs, diroff);
832209716Sjeff	dip = ino_read(parent);
833209716Sjeff	blk = ino_blkatoff(dip, parent, lbn, &frags);
834209716Sjeff	blksize = sblksize(fs, DIP(dip, di_size), lbn);
835209716Sjeff	block = dblk_read(blk, blksize);
836209716Sjeff	dp = (struct direct *)&block[doff];
837209716Sjeff	if (dp->d_ino != child)
838209716Sjeff		errx(1, "Inode %d does not exist in %d at %jd",
839209716Sjeff		    child, parent, diroff);
840209716Sjeff	dp->d_ino = 0;
841209716Sjeff	dblk_dirty(blk);
842209716Sjeff	/*
843209716Sjeff	 * The actual .. reference count will already have been removed
844209716Sjeff	 * from the parent by the .. remref record.
845209716Sjeff	 */
846209716Sjeff}
847209716Sjeff
848209716Sjeff/*
849207141Sjeff * Determines whether a pointer to an inode exists within a directory
850207141Sjeff * at a specified offset.  Returns the mode of the found entry.
851207141Sjeff */
852207141Sjeffstatic int
853207141Sjeffino_isat(ino_t parent, off_t diroff, ino_t child, int *mode, int *isdot)
854207141Sjeff{
855207141Sjeff	union dinode *dip;
856207141Sjeff	struct direct *dp;
857207141Sjeff	ufs2_daddr_t blk;
858207141Sjeff	uint8_t *block;
859207141Sjeff	ufs_lbn_t lbn;
860207141Sjeff	int blksize;
861207141Sjeff	int frags;
862207141Sjeff	int dpoff;
863207141Sjeff	int doff;
864207141Sjeff
865207141Sjeff	*isdot = 0;
866207141Sjeff	dip = ino_read(parent);
867207141Sjeff	*mode = DIP(dip, di_mode);
868207141Sjeff	if ((*mode & IFMT) != IFDIR) {
869207141Sjeff		if (debug) {
870207141Sjeff			/*
871207141Sjeff			 * This can happen if the parent inode
872207141Sjeff			 * was reallocated.
873207141Sjeff			 */
874207141Sjeff			if (*mode != 0)
875207141Sjeff				printf("Directory %d has bad mode %o\n",
876207141Sjeff				    parent, *mode);
877207141Sjeff			else
878207141Sjeff				printf("Directory %d zero inode\n", parent);
879207141Sjeff		}
880207141Sjeff		return (0);
881207141Sjeff	}
882207141Sjeff	lbn = lblkno(fs, diroff);
883207141Sjeff	doff = blkoff(fs, diroff);
884207141Sjeff	blksize = sblksize(fs, DIP(dip, di_size), lbn);
885207141Sjeff	if (diroff + DIRECTSIZ(1) > DIP(dip, di_size) || doff >= blksize) {
886207141Sjeff		if (debug)
887207141Sjeff			printf("ino %d absent from %d due to offset %jd"
888207141Sjeff			    " exceeding size %jd\n",
889207141Sjeff			    child, parent, diroff, DIP(dip, di_size));
890207141Sjeff		return (0);
891207141Sjeff	}
892207141Sjeff	blk = ino_blkatoff(dip, parent, lbn, &frags);
893207141Sjeff	if (blk <= 0) {
894207141Sjeff		if (debug)
895207141Sjeff			printf("Sparse directory %d", parent);
896207141Sjeff		return (0);
897207141Sjeff	}
898207141Sjeff	block = dblk_read(blk, blksize);
899207141Sjeff	/*
900207141Sjeff	 * Walk through the records from the start of the block to be
901207141Sjeff	 * certain we hit a valid record and not some junk in the middle
902207141Sjeff	 * of a file name.  Stop when we reach or pass the expected offset.
903207141Sjeff	 */
904207141Sjeff	dpoff = (doff / DIRBLKSIZ) * DIRBLKSIZ;
905207141Sjeff	do {
906207141Sjeff		dp = (struct direct *)&block[dpoff];
907207141Sjeff		if (dpoff == doff)
908207141Sjeff			break;
909207141Sjeff		if (dp->d_reclen == 0)
910207141Sjeff			break;
911207141Sjeff		dpoff += dp->d_reclen;
912207141Sjeff	} while (dpoff <= doff);
913207141Sjeff	if (dpoff > fs->fs_bsize)
914209408Sdelphij		err_suj("Corrupt directory block in dir ino %d\n", parent);
915207141Sjeff	/* Not found. */
916207141Sjeff	if (dpoff != doff) {
917207141Sjeff		if (debug)
918207141Sjeff			printf("ino %d not found in %d, lbn %jd, dpoff %d\n",
919207141Sjeff			    child, parent, lbn, dpoff);
920207141Sjeff		return (0);
921207141Sjeff	}
922207141Sjeff	/*
923207141Sjeff	 * We found the item in question.  Record the mode and whether it's
924207141Sjeff	 * a . or .. link for the caller.
925207141Sjeff	 */
926207141Sjeff	if (dp->d_ino == child) {
927207141Sjeff		if (child == parent)
928207141Sjeff			*isdot = 1;
929207141Sjeff		else if (dp->d_namlen == 2 &&
930207141Sjeff		    dp->d_name[0] == '.' && dp->d_name[1] == '.')
931207141Sjeff			*isdot = 1;
932207141Sjeff		*mode = DTTOIF(dp->d_type);
933207141Sjeff		return (1);
934207141Sjeff	}
935207141Sjeff	if (debug)
936207141Sjeff		printf("ino %d doesn't match dirent ino %d in parent %d\n",
937207141Sjeff		    child, dp->d_ino, parent);
938207141Sjeff	return (0);
939207141Sjeff}
940207141Sjeff
941207141Sjeff#define	VISIT_INDIR	0x0001
942207141Sjeff#define	VISIT_EXT	0x0002
943207141Sjeff#define	VISIT_ROOT	0x0004	/* Operation came via root & valid pointers. */
944207141Sjeff
945207141Sjeff/*
946207141Sjeff * Read an indirect level which may or may not be linked into an inode.
947207141Sjeff */
948207141Sjeffstatic void
949207141Sjeffindir_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, uint64_t *frags,
950207141Sjeff    ino_visitor visitor, int flags)
951207141Sjeff{
952207141Sjeff	ufs2_daddr_t *bap2;
953207141Sjeff	ufs1_daddr_t *bap1;
954207141Sjeff	ufs_lbn_t lbnadd;
955207141Sjeff	ufs2_daddr_t nblk;
956207141Sjeff	ufs_lbn_t nlbn;
957207141Sjeff	int level;
958207141Sjeff	int i;
959207141Sjeff
960207141Sjeff	/*
961207141Sjeff	 * Don't visit indirect blocks with contents we can't trust.  This
962207141Sjeff	 * should only happen when indir_visit() is called to complete a
963207141Sjeff	 * truncate that never finished and not when a pointer is found via
964207141Sjeff	 * an inode.
965207141Sjeff	 */
966207141Sjeff	if (blk == 0)
967207141Sjeff		return;
968207141Sjeff	level = lbn_level(lbn);
969207141Sjeff	if (level == -1)
970209408Sdelphij		err_suj("Invalid level for lbn %jd\n", lbn);
971207141Sjeff	if ((flags & VISIT_ROOT) == 0 && blk_isindir(blk, ino, lbn) == 0) {
972207141Sjeff		if (debug)
973207141Sjeff			printf("blk %jd ino %d lbn %jd(%d) is not indir.\n",
974207141Sjeff			    blk, ino, lbn, level);
975207141Sjeff		goto out;
976207141Sjeff	}
977207141Sjeff	lbnadd = 1;
978207141Sjeff	for (i = level; i > 0; i--)
979207141Sjeff		lbnadd *= NINDIR(fs);
980207141Sjeff	bap1 = (void *)dblk_read(blk, fs->fs_bsize);
981207141Sjeff	bap2 = (void *)bap1;
982207141Sjeff	for (i = 0; i < NINDIR(fs); i++) {
983207141Sjeff		if (fs->fs_magic == FS_UFS1_MAGIC)
984207141Sjeff			nblk = *bap1++;
985207141Sjeff		else
986207141Sjeff			nblk = *bap2++;
987207141Sjeff		if (nblk == 0)
988207141Sjeff			continue;
989207141Sjeff		if (level == 0) {
990207141Sjeff			nlbn = -lbn + i * lbnadd;
991207141Sjeff			(*frags) += fs->fs_frag;
992207141Sjeff			visitor(ino, nlbn, nblk, fs->fs_frag);
993207141Sjeff		} else {
994207141Sjeff			nlbn = (lbn + 1) - (i * lbnadd);
995207141Sjeff			indir_visit(ino, nlbn, nblk, frags, visitor, flags);
996207141Sjeff		}
997207141Sjeff	}
998207141Sjeffout:
999207141Sjeff	if (flags & VISIT_INDIR) {
1000207141Sjeff		(*frags) += fs->fs_frag;
1001207141Sjeff		visitor(ino, lbn, blk, fs->fs_frag);
1002207141Sjeff	}
1003207141Sjeff}
1004207141Sjeff
1005207141Sjeff/*
1006207141Sjeff * Visit each block in an inode as specified by 'flags' and call a
1007207141Sjeff * callback function.  The callback may inspect or free blocks.  The
1008207141Sjeff * count of frags found according to the size in the file is returned.
1009207141Sjeff * This is not valid for sparse files but may be used to determine
1010207141Sjeff * the correct di_blocks for a file.
1011207141Sjeff */
1012207141Sjeffstatic uint64_t
1013207141Sjeffino_visit(union dinode *ip, ino_t ino, ino_visitor visitor, int flags)
1014207141Sjeff{
1015207141Sjeff	ufs_lbn_t nextlbn;
1016207141Sjeff	ufs_lbn_t tmpval;
1017207141Sjeff	ufs_lbn_t lbn;
1018207141Sjeff	uint64_t size;
1019207141Sjeff	uint64_t fragcnt;
1020207141Sjeff	int mode;
1021207141Sjeff	int frags;
1022207141Sjeff	int i;
1023207141Sjeff
1024207141Sjeff	size = DIP(ip, di_size);
1025207141Sjeff	mode = DIP(ip, di_mode) & IFMT;
1026207141Sjeff	fragcnt = 0;
1027207141Sjeff	if ((flags & VISIT_EXT) &&
1028207141Sjeff	    fs->fs_magic == FS_UFS2_MAGIC && ip->dp2.di_extsize) {
1029207141Sjeff		for (i = 0; i < NXADDR; i++) {
1030207141Sjeff			if (ip->dp2.di_extb[i] == 0)
1031207141Sjeff				continue;
1032207141Sjeff			frags = sblksize(fs, ip->dp2.di_extsize, i);
1033207141Sjeff			frags = numfrags(fs, frags);
1034207141Sjeff			fragcnt += frags;
1035207141Sjeff			visitor(ino, -1 - i, ip->dp2.di_extb[i], frags);
1036207141Sjeff		}
1037207141Sjeff	}
1038207141Sjeff	/* Skip datablocks for short links and devices. */
1039207141Sjeff	if (mode == IFBLK || mode == IFCHR ||
1040207141Sjeff	    (mode == IFLNK && size < fs->fs_maxsymlinklen))
1041207141Sjeff		return (fragcnt);
1042207141Sjeff	for (i = 0; i < NDADDR; i++) {
1043207141Sjeff		if (DIP(ip, di_db[i]) == 0)
1044207141Sjeff			continue;
1045207141Sjeff		frags = sblksize(fs, size, i);
1046207141Sjeff		frags = numfrags(fs, frags);
1047207141Sjeff		fragcnt += frags;
1048207141Sjeff		visitor(ino, i, DIP(ip, di_db[i]), frags);
1049207141Sjeff	}
1050207141Sjeff	/*
1051207141Sjeff	 * We know the following indirects are real as we're following
1052207141Sjeff	 * real pointers to them.
1053207141Sjeff	 */
1054207141Sjeff	flags |= VISIT_ROOT;
1055207141Sjeff	for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; i++,
1056207141Sjeff	    lbn = nextlbn) {
1057207141Sjeff		nextlbn = lbn + tmpval;
1058207141Sjeff		tmpval *= NINDIR(fs);
1059207141Sjeff		if (DIP(ip, di_ib[i]) == 0)
1060207141Sjeff			continue;
1061207141Sjeff		indir_visit(ino, -lbn - i, DIP(ip, di_ib[i]), &fragcnt, visitor,
1062207141Sjeff		    flags);
1063207141Sjeff	}
1064207141Sjeff	return (fragcnt);
1065207141Sjeff}
1066207141Sjeff
1067207141Sjeff/*
1068207141Sjeff * Null visitor function used when we just want to count blocks and
1069207141Sjeff * record the lbn.
1070207141Sjeff */
1071207141Sjeffufs_lbn_t visitlbn;
1072207141Sjeffstatic void
1073207141Sjeffnull_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
1074207141Sjeff{
1075207141Sjeff	if (lbn > 0)
1076207141Sjeff		visitlbn = lbn;
1077207141Sjeff}
1078207141Sjeff
1079207141Sjeff/*
1080207141Sjeff * Recalculate di_blocks when we discover that a block allocation or
1081207141Sjeff * free was not successfully completed.  The kernel does not roll this back
1082207141Sjeff * because it would be too expensive to compute which indirects were
1083207141Sjeff * reachable at the time the inode was written.
1084207141Sjeff */
1085207141Sjeffstatic void
1086207141Sjeffino_adjblks(struct suj_ino *sino)
1087207141Sjeff{
1088207141Sjeff	union dinode *ip;
1089207141Sjeff	uint64_t blocks;
1090207141Sjeff	uint64_t frags;
1091207141Sjeff	off_t isize;
1092207141Sjeff	off_t size;
1093207141Sjeff	ino_t ino;
1094207141Sjeff
1095207141Sjeff	ino = sino->si_ino;
1096207141Sjeff	ip = ino_read(ino);
1097207141Sjeff	/* No need to adjust zero'd inodes. */
1098207141Sjeff	if (DIP(ip, di_mode) == 0)
1099207141Sjeff		return;
1100207141Sjeff	/*
1101207141Sjeff	 * Visit all blocks and count them as well as recording the last
1102207141Sjeff	 * valid lbn in the file.  If the file size doesn't agree with the
1103207141Sjeff	 * last lbn we need to truncate to fix it.  Otherwise just adjust
1104207141Sjeff	 * the blocks count.
1105207141Sjeff	 */
1106207141Sjeff	visitlbn = 0;
1107207141Sjeff	frags = ino_visit(ip, ino, null_visit, VISIT_INDIR | VISIT_EXT);
1108207141Sjeff	blocks = fsbtodb(fs, frags);
1109207141Sjeff	/*
1110207141Sjeff	 * We assume the size and direct block list is kept coherent by
1111207141Sjeff	 * softdep.  For files that have extended into indirects we truncate
1112207141Sjeff	 * to the size in the inode or the maximum size permitted by
1113207141Sjeff	 * populated indirects.
1114207141Sjeff	 */
1115207141Sjeff	if (visitlbn >= NDADDR) {
1116207141Sjeff		isize = DIP(ip, di_size);
1117207141Sjeff		size = lblktosize(fs, visitlbn + 1);
1118207141Sjeff		if (isize > size)
1119207141Sjeff			isize = size;
1120207141Sjeff		/* Always truncate to free any unpopulated indirects. */
1121207141Sjeff		ino_trunc(sino->si_ino, isize);
1122207141Sjeff		return;
1123207141Sjeff	}
1124207141Sjeff	if (blocks == DIP(ip, di_blocks))
1125207141Sjeff		return;
1126207141Sjeff	if (debug)
1127207141Sjeff		printf("ino %d adjusting block count from %jd to %jd\n",
1128207141Sjeff		    ino, DIP(ip, di_blocks), blocks);
1129207141Sjeff	DIP_SET(ip, di_blocks, blocks);
1130207141Sjeff	ino_dirty(ino);
1131207141Sjeff}
1132207141Sjeff
1133207141Sjeffstatic void
1134207141Sjeffblk_free_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
1135207141Sjeff{
1136207141Sjeff	int mask;
1137207141Sjeff
1138207141Sjeff	mask = blk_freemask(blk, ino, lbn, frags);
1139207141Sjeff	if (debug)
1140207141Sjeff		printf("blk %jd freemask 0x%X\n", blk, mask);
1141207141Sjeff	blk_free(blk, mask, frags);
1142207141Sjeff}
1143207141Sjeff
1144207141Sjeff/*
1145207141Sjeff * Free a block or tree of blocks that was previously rooted in ino at
1146207141Sjeff * the given lbn.  If the lbn is an indirect all children are freed
1147207141Sjeff * recursively.
1148207141Sjeff */
1149207141Sjeffstatic void
1150207141Sjeffblk_free_lbn(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags, int follow)
1151207141Sjeff{
1152207141Sjeff	uint64_t resid;
1153207141Sjeff	int mask;
1154207141Sjeff
1155207141Sjeff	mask = blk_freemask(blk, ino, lbn, frags);
1156207141Sjeff	if (debug)
1157207141Sjeff		printf("blk %jd freemask 0x%X\n", blk, mask);
1158207141Sjeff	resid = 0;
1159207141Sjeff	if (lbn <= -NDADDR && follow && mask == 0)
1160207141Sjeff		indir_visit(ino, lbn, blk, &resid, blk_free_visit, VISIT_INDIR);
1161207141Sjeff	else
1162207141Sjeff		blk_free(blk, mask, frags);
1163207141Sjeff}
1164207141Sjeff
1165207141Sjeffstatic void
1166207141Sjeffino_setskip(struct suj_ino *sino, ino_t parent)
1167207141Sjeff{
1168207141Sjeff	int isdot;
1169207141Sjeff	int mode;
1170207141Sjeff
1171207141Sjeff	if (ino_isat(sino->si_ino, DOTDOT_OFFSET, parent, &mode, &isdot))
1172207141Sjeff		sino->si_skipparent = 1;
1173207141Sjeff}
1174207141Sjeff
1175209716Sjeffstatic void
1176209716Sjeffino_remref(ino_t parent, ino_t child, uint64_t diroff, int isdotdot)
1177209716Sjeff{
1178209716Sjeff	struct suj_ino *sino;
1179209716Sjeff	struct suj_rec *srec;
1180209716Sjeff	struct jrefrec *rrec;
1181209716Sjeff
1182209716Sjeff	/*
1183209716Sjeff	 * Lookup this inode to see if we have a record for it.
1184209716Sjeff	 */
1185209716Sjeff	sino = ino_lookup(child, 0);
1186209716Sjeff	/*
1187209716Sjeff	 * Tell any child directories we've already removed their
1188209716Sjeff	 * parent link cnt.  Don't try to adjust our link down again.
1189209716Sjeff	 */
1190209716Sjeff	if (sino != NULL && isdotdot == 0)
1191209716Sjeff		ino_setskip(sino, parent);
1192209716Sjeff	/*
1193209716Sjeff	 * No valid record for this inode.  Just drop the on-disk
1194209716Sjeff	 * link by one.
1195209716Sjeff	 */
1196209716Sjeff	if (sino == NULL || sino->si_hasrecs == 0) {
1197209716Sjeff		ino_decr(child);
1198209716Sjeff		return;
1199209716Sjeff	}
1200209716Sjeff	/*
1201209716Sjeff	 * Use ino_adjust() if ino_check() has already processed this
1202209716Sjeff	 * child.  If we lose the last non-dot reference to a
1203209716Sjeff	 * directory it will be discarded.
1204209716Sjeff	 */
1205209716Sjeff	if (sino->si_linkadj) {
1206209716Sjeff		sino->si_nlink--;
1207209716Sjeff		if (isdotdot)
1208209716Sjeff			sino->si_dotlinks--;
1209209716Sjeff		ino_adjust(sino);
1210209716Sjeff		return;
1211209716Sjeff	}
1212209716Sjeff	/*
1213209716Sjeff	 * If we haven't yet processed this inode we need to make
1214209716Sjeff	 * sure we will successfully discover the lost path.  If not
1215209716Sjeff	 * use nlinkadj to remember.
1216209716Sjeff	 */
1217209716Sjeff	TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1218209716Sjeff		rrec = (struct jrefrec *)srec->sr_rec;
1219209716Sjeff		if (rrec->jr_parent == parent &&
1220209716Sjeff		    rrec->jr_diroff == diroff)
1221209716Sjeff			return;
1222209716Sjeff	}
1223209716Sjeff	sino->si_nlinkadj++;
1224209716Sjeff}
1225209716Sjeff
1226207141Sjeff/*
1227207141Sjeff * Free the children of a directory when the directory is discarded.
1228207141Sjeff */
1229207141Sjeffstatic void
1230207141Sjeffino_free_children(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
1231207141Sjeff{
1232207141Sjeff	struct suj_ino *sino;
1233207141Sjeff	struct direct *dp;
1234207141Sjeff	off_t diroff;
1235207141Sjeff	uint8_t *block;
1236207141Sjeff	int skipparent;
1237209716Sjeff	int isdotdot;
1238207141Sjeff	int dpoff;
1239207141Sjeff	int size;
1240207141Sjeff
1241207141Sjeff	sino = ino_lookup(ino, 0);
1242207141Sjeff	if (sino)
1243207141Sjeff		skipparent = sino->si_skipparent;
1244207141Sjeff	else
1245207141Sjeff		skipparent = 0;
1246207141Sjeff	size = lfragtosize(fs, frags);
1247207141Sjeff	block = dblk_read(blk, size);
1248207141Sjeff	dp = (struct direct *)&block[0];
1249207141Sjeff	for (dpoff = 0; dpoff < size && dp->d_reclen; dpoff += dp->d_reclen) {
1250207141Sjeff		dp = (struct direct *)&block[dpoff];
1251207141Sjeff		if (dp->d_ino == 0 || dp->d_ino == WINO)
1252207141Sjeff			continue;
1253207141Sjeff		if (dp->d_namlen == 1 && dp->d_name[0] == '.')
1254207141Sjeff			continue;
1255209716Sjeff		isdotdot = dp->d_namlen == 2 && dp->d_name[0] == '.' &&
1256207141Sjeff		    dp->d_name[1] == '.';
1257209716Sjeff		if (isdotdot && skipparent == 1)
1258207141Sjeff			continue;
1259207141Sjeff		if (debug)
1260207141Sjeff			printf("Directory %d removing ino %d name %s\n",
1261207141Sjeff			    ino, dp->d_ino, dp->d_name);
1262207141Sjeff		diroff = lblktosize(fs, lbn) + dpoff;
1263209716Sjeff		ino_remref(ino, dp->d_ino, diroff, isdotdot);
1264207141Sjeff	}
1265207141Sjeff}
1266207141Sjeff
1267207141Sjeff/*
1268207141Sjeff * Reclaim an inode, freeing all blocks and decrementing all children's
1269207141Sjeff * link counts.  Free the inode back to the cg.
1270207141Sjeff */
1271207141Sjeffstatic void
1272207141Sjeffino_reclaim(union dinode *ip, ino_t ino, int mode)
1273207141Sjeff{
1274207141Sjeff	uint32_t gen;
1275207141Sjeff
1276207141Sjeff	if (ino == ROOTINO)
1277209408Sdelphij		err_suj("Attempting to free ROOTINO\n");
1278207141Sjeff	if (debug)
1279207141Sjeff		printf("Truncating and freeing ino %d, nlink %d, mode %o\n",
1280207141Sjeff		    ino, DIP(ip, di_nlink), DIP(ip, di_mode));
1281207141Sjeff
1282207141Sjeff	/* We are freeing an inode or directory. */
1283207141Sjeff	if ((DIP(ip, di_mode) & IFMT) == IFDIR)
1284207141Sjeff		ino_visit(ip, ino, ino_free_children, 0);
1285207141Sjeff	DIP_SET(ip, di_nlink, 0);
1286207141Sjeff	ino_visit(ip, ino, blk_free_visit, VISIT_EXT | VISIT_INDIR);
1287207141Sjeff	/* Here we have to clear the inode and release any blocks it holds. */
1288207141Sjeff	gen = DIP(ip, di_gen);
1289207141Sjeff	if (fs->fs_magic == FS_UFS1_MAGIC)
1290207141Sjeff		bzero(ip, sizeof(struct ufs1_dinode));
1291207141Sjeff	else
1292207141Sjeff		bzero(ip, sizeof(struct ufs2_dinode));
1293207141Sjeff	DIP_SET(ip, di_gen, gen);
1294207141Sjeff	ino_dirty(ino);
1295207141Sjeff	ino_free(ino, mode);
1296207141Sjeff	return;
1297207141Sjeff}
1298207141Sjeff
1299207141Sjeff/*
1300207141Sjeff * Adjust an inode's link count down by one when a directory goes away.
1301207141Sjeff */
1302207141Sjeffstatic void
1303207141Sjeffino_decr(ino_t ino)
1304207141Sjeff{
1305207141Sjeff	union dinode *ip;
1306207141Sjeff	int reqlink;
1307207141Sjeff	int nlink;
1308207141Sjeff	int mode;
1309207141Sjeff
1310207141Sjeff	ip = ino_read(ino);
1311207141Sjeff	nlink = DIP(ip, di_nlink);
1312207141Sjeff	mode = DIP(ip, di_mode);
1313207141Sjeff	if (nlink < 1)
1314209408Sdelphij		err_suj("Inode %d link count %d invalid\n", ino, nlink);
1315207141Sjeff	if (mode == 0)
1316209408Sdelphij		err_suj("Inode %d has a link of %d with 0 mode\n", ino, nlink);
1317207141Sjeff	nlink--;
1318207141Sjeff	if ((mode & IFMT) == IFDIR)
1319207141Sjeff		reqlink = 2;
1320207141Sjeff	else
1321207141Sjeff		reqlink = 1;
1322207141Sjeff	if (nlink < reqlink) {
1323207141Sjeff		if (debug)
1324207141Sjeff			printf("ino %d not enough links to live %d < %d\n",
1325207141Sjeff			    ino, nlink, reqlink);
1326207141Sjeff		ino_reclaim(ip, ino, mode);
1327207141Sjeff		return;
1328207141Sjeff	}
1329207141Sjeff	DIP_SET(ip, di_nlink, nlink);
1330207141Sjeff	ino_dirty(ino);
1331207141Sjeff}
1332207141Sjeff
1333207141Sjeff/*
1334207141Sjeff * Adjust the inode link count to 'nlink'.  If the count reaches zero
1335207141Sjeff * free it.
1336207141Sjeff */
1337207141Sjeffstatic void
1338207141Sjeffino_adjust(struct suj_ino *sino)
1339207141Sjeff{
1340207141Sjeff	struct jrefrec *rrec;
1341207141Sjeff	struct suj_rec *srec;
1342207141Sjeff	struct suj_ino *stmp;
1343207141Sjeff	union dinode *ip;
1344207141Sjeff	nlink_t nlink;
1345209716Sjeff	int recmode;
1346207141Sjeff	int reqlink;
1347209716Sjeff	int isdot;
1348207141Sjeff	int mode;
1349207141Sjeff	ino_t ino;
1350207141Sjeff
1351207141Sjeff	nlink = sino->si_nlink;
1352207141Sjeff	ino = sino->si_ino;
1353209716Sjeff	mode = sino->si_mode & IFMT;
1354207141Sjeff	/*
1355209716Sjeff	 * If it's a directory with no dot links, it was truncated before
1356209716Sjeff	 * the name was cleared.  We need to clear the dirent that
1357209716Sjeff	 * points at it.
1358209716Sjeff	 */
1359209716Sjeff	if (mode == IFDIR && nlink == 1 && sino->si_dotlinks == 0) {
1360209716Sjeff		sino->si_nlink = nlink = 0;
1361209716Sjeff		TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1362209716Sjeff			rrec = (struct jrefrec *)srec->sr_rec;
1363209716Sjeff			if (ino_isat(rrec->jr_parent, rrec->jr_diroff, ino,
1364209716Sjeff			    &recmode, &isdot) == 0)
1365209716Sjeff				continue;
1366209716Sjeff			ino_clrat(rrec->jr_parent, rrec->jr_diroff, ino);
1367209716Sjeff			break;
1368209716Sjeff		}
1369209716Sjeff		if (srec == NULL)
1370209716Sjeff			errx(1, "Directory %d name not found", ino);
1371209716Sjeff	}
1372209716Sjeff	/*
1373207141Sjeff	 * If it's a directory with no real names pointing to it go ahead
1374207141Sjeff	 * and truncate it.  This will free any children.
1375207141Sjeff	 */
1376209716Sjeff	if (mode == IFDIR && nlink - sino->si_dotlinks == 0) {
1377207141Sjeff		sino->si_nlink = nlink = 0;
1378207141Sjeff		/*
1379207141Sjeff		 * Mark any .. links so they know not to free this inode
1380207141Sjeff		 * when they are removed.
1381207141Sjeff		 */
1382207141Sjeff		TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1383207141Sjeff			rrec = (struct jrefrec *)srec->sr_rec;
1384207141Sjeff			if (rrec->jr_diroff == DOTDOT_OFFSET) {
1385207141Sjeff				stmp = ino_lookup(rrec->jr_parent, 0);
1386207141Sjeff				if (stmp)
1387207141Sjeff					ino_setskip(stmp, ino);
1388207141Sjeff			}
1389207141Sjeff		}
1390207141Sjeff	}
1391207141Sjeff	ip = ino_read(ino);
1392207141Sjeff	mode = DIP(ip, di_mode) & IFMT;
1393207141Sjeff	if (nlink > LINK_MAX)
1394209408Sdelphij		err_suj(
1395209408Sdelphij		    "ino %d nlink manipulation error, new link %d, old link %d\n",
1396207141Sjeff		    ino, nlink, DIP(ip, di_nlink));
1397207141Sjeff	if (debug)
1398207141Sjeff		printf("Adjusting ino %d, nlink %d, old link %d lastmode %o\n",
1399207141Sjeff		    ino, nlink, DIP(ip, di_nlink), sino->si_mode);
1400207141Sjeff	if (mode == 0) {
1401207141Sjeff		if (debug)
1402207141Sjeff			printf("ino %d, zero inode freeing bitmap\n", ino);
1403207141Sjeff		ino_free(ino, sino->si_mode);
1404207141Sjeff		return;
1405207141Sjeff	}
1406207141Sjeff	/* XXX Should be an assert? */
1407207141Sjeff	if (mode != sino->si_mode && debug)
1408207141Sjeff		printf("ino %d, mode %o != %o\n", ino, mode, sino->si_mode);
1409207141Sjeff	if ((mode & IFMT) == IFDIR)
1410207141Sjeff		reqlink = 2;
1411207141Sjeff	else
1412207141Sjeff		reqlink = 1;
1413207141Sjeff	/* If the inode doesn't have enough links to live, free it. */
1414207141Sjeff	if (nlink < reqlink) {
1415207141Sjeff		if (debug)
1416207141Sjeff			printf("ino %d not enough links to live %d < %d\n",
1417207141Sjeff			    ino, nlink, reqlink);
1418207141Sjeff		ino_reclaim(ip, ino, mode);
1419207141Sjeff		return;
1420207141Sjeff	}
1421207141Sjeff	/* If required write the updated link count. */
1422207141Sjeff	if (DIP(ip, di_nlink) == nlink) {
1423207141Sjeff		if (debug)
1424207141Sjeff			printf("ino %d, link matches, skipping.\n", ino);
1425207141Sjeff		return;
1426207141Sjeff	}
1427207141Sjeff	DIP_SET(ip, di_nlink, nlink);
1428207141Sjeff	ino_dirty(ino);
1429207141Sjeff}
1430207141Sjeff
1431207141Sjeff/*
1432207141Sjeff * Truncate some or all blocks in an indirect, freeing any that are required
1433207141Sjeff * and zeroing the indirect.
1434207141Sjeff */
1435207141Sjeffstatic void
1436207141Sjeffindir_trunc(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, ufs_lbn_t lastlbn)
1437207141Sjeff{
1438207141Sjeff	ufs2_daddr_t *bap2;
1439207141Sjeff	ufs1_daddr_t *bap1;
1440207141Sjeff	ufs_lbn_t lbnadd;
1441207141Sjeff	ufs2_daddr_t nblk;
1442207141Sjeff	ufs_lbn_t next;
1443207141Sjeff	ufs_lbn_t nlbn;
1444207141Sjeff	int dirty;
1445207141Sjeff	int level;
1446207141Sjeff	int i;
1447207141Sjeff
1448207141Sjeff	if (blk == 0)
1449207141Sjeff		return;
1450207141Sjeff	dirty = 0;
1451207141Sjeff	level = lbn_level(lbn);
1452207141Sjeff	if (level == -1)
1453209408Sdelphij		err_suj("Invalid level for lbn %jd\n", lbn);
1454207141Sjeff	lbnadd = 1;
1455207141Sjeff	for (i = level; i > 0; i--)
1456207141Sjeff		lbnadd *= NINDIR(fs);
1457207141Sjeff	bap1 = (void *)dblk_read(blk, fs->fs_bsize);
1458207141Sjeff	bap2 = (void *)bap1;
1459207141Sjeff	for (i = 0; i < NINDIR(fs); i++) {
1460207141Sjeff		if (fs->fs_magic == FS_UFS1_MAGIC)
1461207141Sjeff			nblk = *bap1++;
1462207141Sjeff		else
1463207141Sjeff			nblk = *bap2++;
1464207141Sjeff		if (nblk == 0)
1465207141Sjeff			continue;
1466207141Sjeff		if (level != 0) {
1467207141Sjeff			nlbn = (lbn + 1) - (i * lbnadd);
1468207141Sjeff			/*
1469207141Sjeff			 * Calculate the lbn of the next indirect to
1470207141Sjeff			 * determine if any of this indirect must be
1471207141Sjeff			 * reclaimed.
1472207141Sjeff			 */
1473207141Sjeff			next = -(lbn + level) + ((i+1) * lbnadd);
1474207141Sjeff			if (next <= lastlbn)
1475207141Sjeff				continue;
1476207141Sjeff			indir_trunc(ino, nlbn, nblk, lastlbn);
1477207141Sjeff			/* If all of this indirect was reclaimed, free it. */
1478207141Sjeff			nlbn = next - lbnadd;
1479207141Sjeff			if (nlbn < lastlbn)
1480207141Sjeff				continue;
1481207141Sjeff		} else {
1482207141Sjeff			nlbn = -lbn + i * lbnadd;
1483207141Sjeff			if (nlbn < lastlbn)
1484207141Sjeff				continue;
1485207141Sjeff		}
1486207141Sjeff		dirty = 1;
1487207141Sjeff		blk_free(nblk, 0, fs->fs_frag);
1488207141Sjeff		if (fs->fs_magic == FS_UFS1_MAGIC)
1489207141Sjeff			*(bap1 - 1) = 0;
1490207141Sjeff		else
1491207141Sjeff			*(bap2 - 1) = 0;
1492207141Sjeff	}
1493207141Sjeff	if (dirty)
1494207141Sjeff		dblk_dirty(blk);
1495207141Sjeff}
1496207141Sjeff
1497207141Sjeff/*
1498207141Sjeff * Truncate an inode to the minimum of the given size or the last populated
1499207141Sjeff * block after any over size have been discarded.  The kernel would allocate
1500207141Sjeff * the last block in the file but fsck does not and neither do we.  This
1501207141Sjeff * code never extends files, only shrinks them.
1502207141Sjeff */
1503207141Sjeffstatic void
1504207141Sjeffino_trunc(ino_t ino, off_t size)
1505207141Sjeff{
1506207141Sjeff	union dinode *ip;
1507207141Sjeff	ufs2_daddr_t bn;
1508207141Sjeff	uint64_t totalfrags;
1509207141Sjeff	ufs_lbn_t nextlbn;
1510207141Sjeff	ufs_lbn_t lastlbn;
1511207141Sjeff	ufs_lbn_t tmpval;
1512207141Sjeff	ufs_lbn_t lbn;
1513207141Sjeff	ufs_lbn_t i;
1514207141Sjeff	int frags;
1515207141Sjeff	off_t cursize;
1516207141Sjeff	off_t off;
1517207141Sjeff	int mode;
1518207141Sjeff
1519207141Sjeff	ip = ino_read(ino);
1520207141Sjeff	mode = DIP(ip, di_mode) & IFMT;
1521207141Sjeff	cursize = DIP(ip, di_size);
1522207141Sjeff	if (debug)
1523207141Sjeff		printf("Truncating ino %d, mode %o to size %jd from size %jd\n",
1524207141Sjeff		    ino, mode, size, cursize);
1525207141Sjeff
1526207141Sjeff	/* Skip datablocks for short links and devices. */
1527207141Sjeff	if (mode == 0 || mode == IFBLK || mode == IFCHR ||
1528207141Sjeff	    (mode == IFLNK && cursize < fs->fs_maxsymlinklen))
1529207141Sjeff		return;
1530207141Sjeff	/* Don't extend. */
1531207141Sjeff	if (size > cursize)
1532207141Sjeff		size = cursize;
1533207141Sjeff	lastlbn = lblkno(fs, blkroundup(fs, size));
1534207141Sjeff	for (i = lastlbn; i < NDADDR; i++) {
1535207141Sjeff		if (DIP(ip, di_db[i]) == 0)
1536207141Sjeff			continue;
1537207141Sjeff		frags = sblksize(fs, cursize, i);
1538207141Sjeff		frags = numfrags(fs, frags);
1539207141Sjeff		blk_free(DIP(ip, di_db[i]), 0, frags);
1540207141Sjeff		DIP_SET(ip, di_db[i], 0);
1541207141Sjeff	}
1542207141Sjeff	/*
1543207141Sjeff	 * Follow indirect blocks, freeing anything required.
1544207141Sjeff	 */
1545207141Sjeff	for (i = 0, tmpval = NINDIR(fs), lbn = NDADDR; i < NIADDR; i++,
1546207141Sjeff	    lbn = nextlbn) {
1547207141Sjeff		nextlbn = lbn + tmpval;
1548207141Sjeff		tmpval *= NINDIR(fs);
1549207141Sjeff		/* If we're not freeing any in this indirect range skip it. */
1550207141Sjeff		if (lastlbn >= nextlbn)
1551207141Sjeff			continue;
1552207141Sjeff		if (DIP(ip, di_ib[i]) == 0)
1553207141Sjeff			continue;
1554207141Sjeff		indir_trunc(ino, -lbn - i, DIP(ip, di_ib[i]), lastlbn);
1555207141Sjeff		/* If we freed everything in this indirect free the indir. */
1556207141Sjeff		if (lastlbn > lbn)
1557207141Sjeff			continue;
1558207141Sjeff		blk_free(DIP(ip, di_ib[i]), 0, frags);
1559207141Sjeff		DIP_SET(ip, di_ib[i], 0);
1560207141Sjeff	}
1561207141Sjeff	ino_dirty(ino);
1562207141Sjeff	/*
1563207141Sjeff	 * Now that we've freed any whole blocks that exceed the desired
1564207141Sjeff	 * truncation size, figure out how many blocks remain and what the
1565207141Sjeff	 * last populated lbn is.  We will set the size to this last lbn
1566207141Sjeff	 * rather than worrying about allocating the final lbn as the kernel
1567207141Sjeff	 * would've done.  This is consistent with normal fsck behavior.
1568207141Sjeff	 */
1569207141Sjeff	visitlbn = 0;
1570207141Sjeff	totalfrags = ino_visit(ip, ino, null_visit, VISIT_INDIR | VISIT_EXT);
1571207141Sjeff	if (size > lblktosize(fs, visitlbn + 1))
1572207141Sjeff		size = lblktosize(fs, visitlbn + 1);
1573207141Sjeff	/*
1574207141Sjeff	 * If we're truncating direct blocks we have to adjust frags
1575207141Sjeff	 * accordingly.
1576207141Sjeff	 */
1577207141Sjeff	if (visitlbn < NDADDR && totalfrags) {
1578207141Sjeff		long oldspace, newspace;
1579207141Sjeff
1580207141Sjeff		bn = DIP(ip, di_db[visitlbn]);
1581207141Sjeff		if (bn == 0)
1582209408Sdelphij			err_suj("Bad blk at ino %d lbn %jd\n", ino, visitlbn);
1583207141Sjeff		oldspace = sblksize(fs, cursize, visitlbn);
1584207141Sjeff		newspace = sblksize(fs, size, visitlbn);
1585207141Sjeff		if (oldspace != newspace) {
1586207141Sjeff			bn += numfrags(fs, newspace);
1587207141Sjeff			frags = numfrags(fs, oldspace - newspace);
1588207141Sjeff			blk_free(bn, 0, frags);
1589207141Sjeff			totalfrags -= frags;
1590207141Sjeff		}
1591207141Sjeff	}
1592207141Sjeff	DIP_SET(ip, di_blocks, fsbtodb(fs, totalfrags));
1593207141Sjeff	DIP_SET(ip, di_size, size);
1594207141Sjeff	/*
1595207141Sjeff	 * If we've truncated into the middle of a block or frag we have
1596207141Sjeff	 * to zero it here.  Otherwise the file could extend into
1597207141Sjeff	 * uninitialized space later.
1598207141Sjeff	 */
1599207141Sjeff	off = blkoff(fs, size);
1600207141Sjeff	if (off) {
1601207141Sjeff		uint8_t *buf;
1602207141Sjeff		long clrsize;
1603207141Sjeff
1604207141Sjeff		bn = ino_blkatoff(ip, ino, visitlbn, &frags);
1605207141Sjeff		if (bn == 0)
1606209408Sdelphij			err_suj("Block missing from ino %d at lbn %jd\n",
1607207141Sjeff			    ino, visitlbn);
1608207141Sjeff		clrsize = frags * fs->fs_fsize;
1609207141Sjeff		buf = dblk_read(bn, clrsize);
1610207141Sjeff		clrsize -= off;
1611207141Sjeff		buf += off;
1612207141Sjeff		bzero(buf, clrsize);
1613207141Sjeff		dblk_dirty(bn);
1614207141Sjeff	}
1615207141Sjeff	return;
1616207141Sjeff}
1617207141Sjeff
1618207141Sjeff/*
1619207141Sjeff * Process records available for one inode and determine whether the
1620207141Sjeff * link count is correct or needs adjusting.
1621207141Sjeff */
1622207141Sjeffstatic void
1623207141Sjeffino_check(struct suj_ino *sino)
1624207141Sjeff{
1625207141Sjeff	struct suj_rec *srec;
1626207141Sjeff	struct jrefrec *rrec;
1627207141Sjeff	nlink_t dotlinks;
1628207141Sjeff	int newlinks;
1629207141Sjeff	int removes;
1630207141Sjeff	int nlink;
1631207141Sjeff	ino_t ino;
1632207141Sjeff	int isdot;
1633207141Sjeff	int isat;
1634207141Sjeff	int mode;
1635207141Sjeff
1636207141Sjeff	if (sino->si_hasrecs == 0)
1637207141Sjeff		return;
1638207141Sjeff	ino = sino->si_ino;
1639207141Sjeff	rrec = (struct jrefrec *)TAILQ_FIRST(&sino->si_recs)->sr_rec;
1640207141Sjeff	nlink = rrec->jr_nlink;
1641207141Sjeff	newlinks = 0;
1642207141Sjeff	dotlinks = 0;
1643207141Sjeff	removes = sino->si_nlinkadj;
1644207141Sjeff	TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1645207141Sjeff		rrec = (struct jrefrec *)srec->sr_rec;
1646207141Sjeff		isat = ino_isat(rrec->jr_parent, rrec->jr_diroff,
1647207141Sjeff		    rrec->jr_ino, &mode, &isdot);
1648207141Sjeff		if (isat && (mode & IFMT) != (rrec->jr_mode & IFMT))
1649209408Sdelphij			err_suj("Inode mode/directory type mismatch %o != %o\n",
1650207141Sjeff			    mode, rrec->jr_mode);
1651207141Sjeff		if (debug)
1652207141Sjeff			printf("jrefrec: op %d ino %d, nlink %d, parent %d, "
1653207141Sjeff			    "diroff %jd, mode %o, isat %d, isdot %d\n",
1654207141Sjeff			    rrec->jr_op, rrec->jr_ino, rrec->jr_nlink,
1655207141Sjeff			    rrec->jr_parent, rrec->jr_diroff, rrec->jr_mode,
1656207141Sjeff			    isat, isdot);
1657207141Sjeff		mode = rrec->jr_mode & IFMT;
1658207141Sjeff		if (rrec->jr_op == JOP_REMREF)
1659207141Sjeff			removes++;
1660207141Sjeff		newlinks += isat;
1661207141Sjeff		if (isdot)
1662207141Sjeff			dotlinks += isat;
1663207141Sjeff	}
1664207141Sjeff	/*
1665207141Sjeff	 * The number of links that remain are the starting link count
1666207141Sjeff	 * subtracted by the total number of removes with the total
1667207141Sjeff	 * links discovered back in.  An incomplete remove thus
1668207141Sjeff	 * makes no change to the link count but an add increases
1669207141Sjeff	 * by one.
1670207141Sjeff	 */
1671207141Sjeff	if (debug)
1672207141Sjeff		printf("ino %d nlink %d newlinks %d removes %d dotlinks %d\n",
1673207141Sjeff		    ino, nlink, newlinks, removes, dotlinks);
1674207141Sjeff	nlink += newlinks;
1675207141Sjeff	nlink -= removes;
1676207141Sjeff	sino->si_linkadj = 1;
1677207141Sjeff	sino->si_nlink = nlink;
1678207141Sjeff	sino->si_dotlinks = dotlinks;
1679207141Sjeff	sino->si_mode = mode;
1680207141Sjeff	ino_adjust(sino);
1681207141Sjeff}
1682207141Sjeff
1683207141Sjeff/*
1684207141Sjeff * Process records available for one block and determine whether it is
1685207141Sjeff * still allocated and whether the owning inode needs to be updated or
1686207141Sjeff * a free completed.
1687207141Sjeff */
1688207141Sjeffstatic void
1689207141Sjeffblk_check(struct suj_blk *sblk)
1690207141Sjeff{
1691207141Sjeff	struct suj_rec *srec;
1692207141Sjeff	struct jblkrec *brec;
1693207141Sjeff	struct suj_ino *sino;
1694207141Sjeff	ufs2_daddr_t blk;
1695207141Sjeff	int mask;
1696207141Sjeff	int frags;
1697207141Sjeff	int isat;
1698207141Sjeff
1699207141Sjeff	/*
1700207141Sjeff	 * Each suj_blk actually contains records for any fragments in that
1701207141Sjeff	 * block.  As a result we must evaluate each record individually.
1702207141Sjeff	 */
1703207141Sjeff	sino = NULL;
1704207141Sjeff	TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
1705207141Sjeff		brec = (struct jblkrec *)srec->sr_rec;
1706207141Sjeff		frags = brec->jb_frags;
1707207141Sjeff		blk = brec->jb_blkno + brec->jb_oldfrags;
1708207141Sjeff		isat = blk_isat(brec->jb_ino, brec->jb_lbn, blk, &frags);
1709207141Sjeff		if (sino == NULL || sino->si_ino != brec->jb_ino) {
1710207141Sjeff			sino = ino_lookup(brec->jb_ino, 1);
1711207141Sjeff			sino->si_blkadj = 1;
1712207141Sjeff		}
1713207141Sjeff		if (debug)
1714207141Sjeff			printf("op %d blk %jd ino %d lbn %jd frags %d isat %d (%d)\n",
1715207141Sjeff			    brec->jb_op, blk, brec->jb_ino, brec->jb_lbn,
1716207141Sjeff			    brec->jb_frags, isat, frags);
1717207141Sjeff		/*
1718207141Sjeff		 * If we found the block at this address we still have to
1719207141Sjeff		 * determine if we need to free the tail end that was
1720207141Sjeff		 * added by adding contiguous fragments from the same block.
1721207141Sjeff		 */
1722207141Sjeff		if (isat == 1) {
1723207141Sjeff			if (frags == brec->jb_frags)
1724207141Sjeff				continue;
1725207141Sjeff			mask = blk_freemask(blk, brec->jb_ino, brec->jb_lbn,
1726207141Sjeff			    brec->jb_frags);
1727207141Sjeff			mask >>= frags;
1728207141Sjeff			blk += frags;
1729207141Sjeff			frags = brec->jb_frags - frags;
1730207141Sjeff			blk_free(blk, mask, frags);
1731207141Sjeff			continue;
1732207141Sjeff		}
1733207141Sjeff		/*
1734207141Sjeff	 	 * The block wasn't found, attempt to free it.  It won't be
1735207141Sjeff		 * freed if it was actually reallocated.  If this was an
1736207141Sjeff		 * allocation we don't want to follow indirects as they
1737207141Sjeff		 * may not be written yet.  Any children of the indirect will
1738207141Sjeff		 * have their own records.  If it's a free we need to
1739207141Sjeff		 * recursively free children.
1740207141Sjeff		 */
1741207141Sjeff		blk_free_lbn(blk, brec->jb_ino, brec->jb_lbn, brec->jb_frags,
1742207141Sjeff		    brec->jb_op == JOP_FREEBLK);
1743207141Sjeff	}
1744207141Sjeff}
1745207141Sjeff
1746207141Sjeff/*
1747207141Sjeff * Walk the list of inode records for this cg and resolve moved and duplicate
1748207141Sjeff * inode references now that we have a complete picture.
1749207141Sjeff */
1750207141Sjeffstatic void
1751207141Sjeffcg_build(struct suj_cg *sc)
1752207141Sjeff{
1753207141Sjeff	struct suj_ino *sino;
1754207141Sjeff	int i;
1755207141Sjeff
1756207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1757207141Sjeff		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1758207141Sjeff			ino_build(sino);
1759207141Sjeff}
1760207141Sjeff
1761207141Sjeff/*
1762207141Sjeff * Handle inodes requiring truncation.  This must be done prior to
1763207141Sjeff * looking up any inodes in directories.
1764207141Sjeff */
1765207141Sjeffstatic void
1766207141Sjeffcg_trunc(struct suj_cg *sc)
1767207141Sjeff{
1768207141Sjeff	struct suj_ino *sino;
1769207141Sjeff	int i;
1770207141Sjeff
1771207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1772207141Sjeff		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1773207141Sjeff			if (sino->si_trunc) {
1774207141Sjeff				ino_trunc(sino->si_ino,
1775207141Sjeff				    sino->si_trunc->jt_size);
1776207141Sjeff				sino->si_trunc = NULL;
1777207141Sjeff			}
1778207141Sjeff}
1779207141Sjeff
1780207141Sjeff/*
1781207141Sjeff * Free any partially allocated blocks and then resolve inode block
1782207141Sjeff * counts.
1783207141Sjeff */
1784207141Sjeffstatic void
1785207141Sjeffcg_check_blk(struct suj_cg *sc)
1786207141Sjeff{
1787207141Sjeff	struct suj_ino *sino;
1788207141Sjeff	struct suj_blk *sblk;
1789207141Sjeff	int i;
1790207141Sjeff
1791207141Sjeff
1792207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1793207141Sjeff		LIST_FOREACH(sblk, &sc->sc_blkhash[i], sb_next)
1794207141Sjeff			blk_check(sblk);
1795207141Sjeff	/*
1796207141Sjeff	 * Now that we've freed blocks which are not referenced we
1797207141Sjeff	 * make a second pass over all inodes to adjust their block
1798207141Sjeff	 * counts.
1799207141Sjeff	 */
1800207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1801207141Sjeff		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1802207141Sjeff			if (sino->si_blkadj)
1803207141Sjeff				ino_adjblks(sino);
1804207141Sjeff}
1805207141Sjeff
1806207141Sjeff/*
1807207141Sjeff * Walk the list of inode records for this cg, recovering any
1808207141Sjeff * changes which were not complete at the time of crash.
1809207141Sjeff */
1810207141Sjeffstatic void
1811207141Sjeffcg_check_ino(struct suj_cg *sc)
1812207141Sjeff{
1813207141Sjeff	struct suj_ino *sino;
1814207141Sjeff	int i;
1815207141Sjeff
1816207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1817207141Sjeff		LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1818207141Sjeff			ino_check(sino);
1819207141Sjeff}
1820207141Sjeff
1821207141Sjeff/*
1822207141Sjeff * Write a potentially dirty cg.  Recalculate the summary information and
1823207141Sjeff * update the superblock summary.
1824207141Sjeff */
1825207141Sjeffstatic void
1826207141Sjeffcg_write(struct suj_cg *sc)
1827207141Sjeff{
1828207141Sjeff	ufs1_daddr_t fragno, cgbno, maxbno;
1829207141Sjeff	u_int8_t *blksfree;
1830207141Sjeff	struct cg *cgp;
1831207141Sjeff	int blk;
1832207141Sjeff	int i;
1833207141Sjeff
1834207141Sjeff	if (sc->sc_dirty == 0)
1835207141Sjeff		return;
1836207141Sjeff	/*
1837207141Sjeff	 * Fix the frag and cluster summary.
1838207141Sjeff	 */
1839207141Sjeff	cgp = sc->sc_cgp;
1840207141Sjeff	cgp->cg_cs.cs_nbfree = 0;
1841207141Sjeff	cgp->cg_cs.cs_nffree = 0;
1842207141Sjeff	bzero(&cgp->cg_frsum, sizeof(cgp->cg_frsum));
1843207141Sjeff	maxbno = fragstoblks(fs, fs->fs_fpg);
1844207141Sjeff	if (fs->fs_contigsumsize > 0) {
1845207141Sjeff		for (i = 1; i <= fs->fs_contigsumsize; i++)
1846207141Sjeff			cg_clustersum(cgp)[i] = 0;
1847207141Sjeff		bzero(cg_clustersfree(cgp), howmany(maxbno, CHAR_BIT));
1848207141Sjeff	}
1849207141Sjeff	blksfree = cg_blksfree(cgp);
1850207141Sjeff	for (cgbno = 0; cgbno < maxbno; cgbno++) {
1851207141Sjeff		if (ffs_isfreeblock(fs, blksfree, cgbno))
1852207141Sjeff			continue;
1853207141Sjeff		if (ffs_isblock(fs, blksfree, cgbno)) {
1854207141Sjeff			ffs_clusteracct(fs, cgp, cgbno, 1);
1855207141Sjeff			cgp->cg_cs.cs_nbfree++;
1856207141Sjeff			continue;
1857207141Sjeff		}
1858207141Sjeff		fragno = blkstofrags(fs, cgbno);
1859207141Sjeff		blk = blkmap(fs, blksfree, fragno);
1860207141Sjeff		ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
1861207141Sjeff		for (i = 0; i < fs->fs_frag; i++)
1862207141Sjeff			if (isset(blksfree, fragno + i))
1863207141Sjeff				cgp->cg_cs.cs_nffree++;
1864207141Sjeff	}
1865207141Sjeff	/*
1866207141Sjeff	 * Update the superblock cg summary from our now correct values
1867207141Sjeff	 * before writing the block.
1868207141Sjeff	 */
1869207141Sjeff	fs->fs_cs(fs, sc->sc_cgx) = cgp->cg_cs;
1870207141Sjeff	if (bwrite(disk, fsbtodb(fs, cgtod(fs, sc->sc_cgx)), sc->sc_cgbuf,
1871207141Sjeff	    fs->fs_bsize) == -1)
1872209408Sdelphij		err_suj("Unable to write cylinder group %d\n", sc->sc_cgx);
1873207141Sjeff}
1874207141Sjeff
1875207141Sjeff/*
1876207141Sjeff * Write out any modified inodes.
1877207141Sjeff */
1878207141Sjeffstatic void
1879207141Sjeffcg_write_inos(struct suj_cg *sc)
1880207141Sjeff{
1881207141Sjeff	struct ino_blk *iblk;
1882207141Sjeff	int i;
1883207141Sjeff
1884207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1885207141Sjeff		LIST_FOREACH(iblk, &sc->sc_iblkhash[i], ib_next)
1886207141Sjeff			if (iblk->ib_dirty)
1887207141Sjeff				iblk_write(iblk);
1888207141Sjeff}
1889207141Sjeff
1890207141Sjeffstatic void
1891207141Sjeffcg_apply(void (*apply)(struct suj_cg *))
1892207141Sjeff{
1893207141Sjeff	struct suj_cg *scg;
1894207141Sjeff	int i;
1895207141Sjeff
1896207141Sjeff	for (i = 0; i < SUJ_HASHSIZE; i++)
1897207141Sjeff		LIST_FOREACH(scg, &cghash[i], sc_next)
1898207141Sjeff			apply(scg);
1899207141Sjeff}
1900207141Sjeff
1901207141Sjeff/*
1902207141Sjeff * Process the unlinked but referenced file list.  Freeing all inodes.
1903207141Sjeff */
1904207141Sjeffstatic void
1905207141Sjeffino_unlinked(void)
1906207141Sjeff{
1907207141Sjeff	union dinode *ip;
1908207141Sjeff	uint16_t mode;
1909207141Sjeff	ino_t inon;
1910207141Sjeff	ino_t ino;
1911207141Sjeff
1912207141Sjeff	ino = fs->fs_sujfree;
1913207141Sjeff	fs->fs_sujfree = 0;
1914207141Sjeff	while (ino != 0) {
1915207141Sjeff		ip = ino_read(ino);
1916207141Sjeff		mode = DIP(ip, di_mode) & IFMT;
1917207141Sjeff		inon = DIP(ip, di_freelink);
1918207141Sjeff		DIP_SET(ip, di_freelink, 0);
1919207141Sjeff		/*
1920207141Sjeff		 * XXX Should this be an errx?
1921207141Sjeff		 */
1922207141Sjeff		if (DIP(ip, di_nlink) == 0) {
1923207141Sjeff			if (debug)
1924207141Sjeff				printf("Freeing unlinked ino %d mode %o\n",
1925207141Sjeff				    ino, mode);
1926207141Sjeff			ino_reclaim(ip, ino, mode);
1927207141Sjeff		} else if (debug)
1928207141Sjeff			printf("Skipping ino %d mode %o with link %d\n",
1929207141Sjeff			    ino, mode, DIP(ip, di_nlink));
1930207141Sjeff		ino = inon;
1931207141Sjeff	}
1932207141Sjeff}
1933207141Sjeff
1934207141Sjeff/*
1935207141Sjeff * Append a new record to the list of records requiring processing.
1936207141Sjeff */
1937207141Sjeffstatic void
1938207141Sjeffino_append(union jrec *rec)
1939207141Sjeff{
1940207141Sjeff	struct jrefrec *refrec;
1941207141Sjeff	struct jmvrec *mvrec;
1942207141Sjeff	struct suj_ino *sino;
1943207141Sjeff	struct suj_rec *srec;
1944207141Sjeff
1945207141Sjeff	mvrec = &rec->rec_jmvrec;
1946207141Sjeff	refrec = &rec->rec_jrefrec;
1947207141Sjeff	if (debug && mvrec->jm_op == JOP_MVREF)
1948207141Sjeff		printf("ino move: ino %d, parent %d, diroff %jd, oldoff %jd\n",
1949207141Sjeff		    mvrec->jm_ino, mvrec->jm_parent, mvrec->jm_newoff,
1950207141Sjeff		    mvrec->jm_oldoff);
1951207141Sjeff	else if (debug &&
1952207141Sjeff	    (refrec->jr_op == JOP_ADDREF || refrec->jr_op == JOP_REMREF))
1953207141Sjeff		printf("ino ref: op %d, ino %d, nlink %d, "
1954207141Sjeff		    "parent %d, diroff %jd\n",
1955207141Sjeff		    refrec->jr_op, refrec->jr_ino, refrec->jr_nlink,
1956207141Sjeff		    refrec->jr_parent, refrec->jr_diroff);
1957207141Sjeff	/*
1958207141Sjeff	 * Lookup the ino and clear truncate if one is found.  Partial
1959207141Sjeff	 * truncates are always done synchronously so if we discover
1960207141Sjeff	 * an operation that requires a lock the truncation has completed
1961207141Sjeff	 * and can be discarded.
1962207141Sjeff	 */
1963207141Sjeff	sino = ino_lookup(((struct jrefrec *)rec)->jr_ino, 1);
1964207141Sjeff	sino->si_trunc = NULL;
1965207141Sjeff	sino->si_hasrecs = 1;
1966207141Sjeff	srec = errmalloc(sizeof(*srec));
1967207141Sjeff	srec->sr_rec = rec;
1968207141Sjeff	TAILQ_INSERT_TAIL(&sino->si_newrecs, srec, sr_next);
1969207141Sjeff}
1970207141Sjeff
1971207141Sjeff/*
1972207141Sjeff * Add a reference adjustment to the sino list and eliminate dups.  The
1973207141Sjeff * primary loop in ino_build_ref() checks for dups but new ones may be
1974207141Sjeff * created as a result of offset adjustments.
1975207141Sjeff */
1976207141Sjeffstatic void
1977207141Sjeffino_add_ref(struct suj_ino *sino, struct suj_rec *srec)
1978207141Sjeff{
1979207141Sjeff	struct jrefrec *refrec;
1980207141Sjeff	struct suj_rec *srn;
1981207141Sjeff	struct jrefrec *rrn;
1982207141Sjeff
1983207141Sjeff	refrec = (struct jrefrec *)srec->sr_rec;
1984207141Sjeff	/*
1985207141Sjeff	 * We walk backwards so that the oldest link count is preserved.  If
1986207141Sjeff	 * an add record conflicts with a remove keep the remove.  Redundant
1987207141Sjeff	 * removes are eliminated in ino_build_ref.  Otherwise we keep the
1988207141Sjeff	 * oldest record at a given location.
1989207141Sjeff	 */
1990207141Sjeff	for (srn = TAILQ_LAST(&sino->si_recs, srechd); srn;
1991207141Sjeff	    srn = TAILQ_PREV(srn, srechd, sr_next)) {
1992207141Sjeff		rrn = (struct jrefrec *)srn->sr_rec;
1993207141Sjeff		if (rrn->jr_parent != refrec->jr_parent ||
1994207141Sjeff		    rrn->jr_diroff != refrec->jr_diroff)
1995207141Sjeff			continue;
1996207141Sjeff		if (rrn->jr_op == JOP_REMREF || refrec->jr_op == JOP_ADDREF) {
1997207141Sjeff			rrn->jr_mode = refrec->jr_mode;
1998207141Sjeff			return;
1999207141Sjeff		}
2000207141Sjeff		/*
2001207141Sjeff		 * Adding a remove.
2002207141Sjeff		 *
2003207141Sjeff		 * Replace the record in place with the old nlink in case
2004207141Sjeff		 * we replace the head of the list.  Abandon srec as a dup.
2005207141Sjeff		 */
2006207141Sjeff		refrec->jr_nlink = rrn->jr_nlink;
2007207141Sjeff		srn->sr_rec = srec->sr_rec;
2008207141Sjeff		return;
2009207141Sjeff	}
2010207141Sjeff	TAILQ_INSERT_TAIL(&sino->si_recs, srec, sr_next);
2011207141Sjeff}
2012207141Sjeff
2013207141Sjeff/*
2014207141Sjeff * Create a duplicate of a reference at a previous location.
2015207141Sjeff */
2016207141Sjeffstatic void
2017207141Sjeffino_dup_ref(struct suj_ino *sino, struct jrefrec *refrec, off_t diroff)
2018207141Sjeff{
2019207141Sjeff	struct jrefrec *rrn;
2020207141Sjeff	struct suj_rec *srn;
2021207141Sjeff
2022207141Sjeff	rrn = errmalloc(sizeof(*refrec));
2023207141Sjeff	*rrn = *refrec;
2024207141Sjeff	rrn->jr_op = JOP_ADDREF;
2025207141Sjeff	rrn->jr_diroff = diroff;
2026207141Sjeff	srn = errmalloc(sizeof(*srn));
2027207141Sjeff	srn->sr_rec = (union jrec *)rrn;
2028207141Sjeff	ino_add_ref(sino, srn);
2029207141Sjeff}
2030207141Sjeff
2031207141Sjeff/*
2032207141Sjeff * Add a reference to the list at all known locations.  We follow the offset
2033207141Sjeff * changes for a single instance and create duplicate add refs at each so
2034207141Sjeff * that we can tolerate any version of the directory block.  Eliminate
2035207141Sjeff * removes which collide with adds that are seen in the journal.  They should
2036207141Sjeff * not adjust the link count down.
2037207141Sjeff */
2038207141Sjeffstatic void
2039207141Sjeffino_build_ref(struct suj_ino *sino, struct suj_rec *srec)
2040207141Sjeff{
2041207141Sjeff	struct jrefrec *refrec;
2042207141Sjeff	struct jmvrec *mvrec;
2043207141Sjeff	struct suj_rec *srp;
2044207141Sjeff	struct suj_rec *srn;
2045207141Sjeff	struct jrefrec *rrn;
2046207141Sjeff	off_t diroff;
2047207141Sjeff
2048207141Sjeff	refrec = (struct jrefrec *)srec->sr_rec;
2049207141Sjeff	/*
2050207141Sjeff	 * Search for a mvrec that matches this offset.  Whether it's an add
2051207141Sjeff	 * or a remove we can delete the mvref after creating a dup record in
2052207141Sjeff	 * the old location.
2053207141Sjeff	 */
2054207141Sjeff	if (!TAILQ_EMPTY(&sino->si_movs)) {
2055207141Sjeff		diroff = refrec->jr_diroff;
2056207141Sjeff		for (srn = TAILQ_LAST(&sino->si_movs, srechd); srn; srn = srp) {
2057207141Sjeff			srp = TAILQ_PREV(srn, srechd, sr_next);
2058207141Sjeff			mvrec = (struct jmvrec *)srn->sr_rec;
2059207141Sjeff			if (mvrec->jm_parent != refrec->jr_parent ||
2060207141Sjeff			    mvrec->jm_newoff != diroff)
2061207141Sjeff				continue;
2062207141Sjeff			diroff = mvrec->jm_oldoff;
2063207141Sjeff			TAILQ_REMOVE(&sino->si_movs, srn, sr_next);
2064209408Sdelphij			free(srn);
2065207141Sjeff			ino_dup_ref(sino, refrec, diroff);
2066207141Sjeff		}
2067207141Sjeff	}
2068207141Sjeff	/*
2069207141Sjeff	 * If a remove wasn't eliminated by an earlier add just append it to
2070207141Sjeff	 * the list.
2071207141Sjeff	 */
2072207141Sjeff	if (refrec->jr_op == JOP_REMREF) {
2073207141Sjeff		ino_add_ref(sino, srec);
2074207141Sjeff		return;
2075207141Sjeff	}
2076207141Sjeff	/*
2077207141Sjeff	 * Walk the list of records waiting to be added to the list.  We
2078207141Sjeff	 * must check for moves that apply to our current offset and remove
2079207141Sjeff	 * them from the list.  Remove any duplicates to eliminate removes
2080207141Sjeff	 * with corresponding adds.
2081207141Sjeff	 */
2082207141Sjeff	TAILQ_FOREACH_SAFE(srn, &sino->si_newrecs, sr_next, srp) {
2083207141Sjeff		switch (srn->sr_rec->rec_jrefrec.jr_op) {
2084207141Sjeff		case JOP_ADDREF:
2085207141Sjeff			/*
2086207141Sjeff			 * This should actually be an error we should
2087207141Sjeff			 * have a remove for every add journaled.
2088207141Sjeff			 */
2089207141Sjeff			rrn = (struct jrefrec *)srn->sr_rec;
2090207141Sjeff			if (rrn->jr_parent != refrec->jr_parent ||
2091207141Sjeff			    rrn->jr_diroff != refrec->jr_diroff)
2092207141Sjeff				break;
2093207141Sjeff			TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
2094207141Sjeff			break;
2095207141Sjeff		case JOP_REMREF:
2096207141Sjeff			/*
2097207141Sjeff			 * Once we remove the current iteration of the
2098207141Sjeff			 * record at this address we're done.
2099207141Sjeff			 */
2100207141Sjeff			rrn = (struct jrefrec *)srn->sr_rec;
2101207141Sjeff			if (rrn->jr_parent != refrec->jr_parent ||
2102207141Sjeff			    rrn->jr_diroff != refrec->jr_diroff)
2103207141Sjeff				break;
2104207141Sjeff			TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
2105207141Sjeff			ino_add_ref(sino, srec);
2106207141Sjeff			return;
2107207141Sjeff		case JOP_MVREF:
2108207141Sjeff			/*
2109207141Sjeff			 * Update our diroff based on any moves that match
2110207141Sjeff			 * and remove the move.
2111207141Sjeff			 */
2112207141Sjeff			mvrec = (struct jmvrec *)srn->sr_rec;
2113207141Sjeff			if (mvrec->jm_parent != refrec->jr_parent ||
2114207141Sjeff			    mvrec->jm_oldoff != refrec->jr_diroff)
2115207141Sjeff				break;
2116207141Sjeff			ino_dup_ref(sino, refrec, mvrec->jm_oldoff);
2117207141Sjeff			refrec->jr_diroff = mvrec->jm_newoff;
2118207141Sjeff			TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
2119207141Sjeff			break;
2120207141Sjeff		default:
2121209408Sdelphij			err_suj("ino_build_ref: Unknown op %d\n",
2122207141Sjeff			    srn->sr_rec->rec_jrefrec.jr_op);
2123207141Sjeff		}
2124207141Sjeff	}
2125207141Sjeff	ino_add_ref(sino, srec);
2126207141Sjeff}
2127207141Sjeff
2128207141Sjeff/*
2129207141Sjeff * Walk the list of new records and add them in-order resolving any
2130207141Sjeff * dups and adjusted offsets.
2131207141Sjeff */
2132207141Sjeffstatic void
2133207141Sjeffino_build(struct suj_ino *sino)
2134207141Sjeff{
2135207141Sjeff	struct suj_rec *srec;
2136207141Sjeff
2137207141Sjeff	while ((srec = TAILQ_FIRST(&sino->si_newrecs)) != NULL) {
2138207141Sjeff		TAILQ_REMOVE(&sino->si_newrecs, srec, sr_next);
2139207141Sjeff		switch (srec->sr_rec->rec_jrefrec.jr_op) {
2140207141Sjeff		case JOP_ADDREF:
2141207141Sjeff		case JOP_REMREF:
2142207141Sjeff			ino_build_ref(sino, srec);
2143207141Sjeff			break;
2144207141Sjeff		case JOP_MVREF:
2145207141Sjeff			/*
2146207141Sjeff			 * Add this mvrec to the queue of pending mvs.
2147207141Sjeff			 */
2148207141Sjeff			TAILQ_INSERT_TAIL(&sino->si_movs, srec, sr_next);
2149207141Sjeff			break;
2150207141Sjeff		default:
2151209408Sdelphij			err_suj("ino_build: Unknown op %d\n",
2152207141Sjeff			    srec->sr_rec->rec_jrefrec.jr_op);
2153207141Sjeff		}
2154207141Sjeff	}
2155207141Sjeff	if (TAILQ_EMPTY(&sino->si_recs))
2156207141Sjeff		sino->si_hasrecs = 0;
2157207141Sjeff}
2158207141Sjeff
2159207141Sjeff/*
2160207141Sjeff * Modify journal records so they refer to the base block number
2161207141Sjeff * and a start and end frag range.  This is to facilitate the discovery
2162207141Sjeff * of overlapping fragment allocations.
2163207141Sjeff */
2164207141Sjeffstatic void
2165207141Sjeffblk_build(struct jblkrec *blkrec)
2166207141Sjeff{
2167207141Sjeff	struct suj_rec *srec;
2168207141Sjeff	struct suj_blk *sblk;
2169207141Sjeff	struct jblkrec *blkrn;
2170207141Sjeff	struct suj_ino *sino;
2171207141Sjeff	ufs2_daddr_t blk;
2172207141Sjeff	off_t foff;
2173207141Sjeff	int frag;
2174207141Sjeff
2175207141Sjeff	if (debug)
2176207141Sjeff		printf("blk_build: op %d blkno %jd frags %d oldfrags %d "
2177207141Sjeff		    "ino %d lbn %jd\n",
2178207141Sjeff		    blkrec->jb_op, blkrec->jb_blkno, blkrec->jb_frags,
2179207141Sjeff		    blkrec->jb_oldfrags, blkrec->jb_ino, blkrec->jb_lbn);
2180207141Sjeff
2181207141Sjeff	/*
2182207141Sjeff	 * Look up the inode and clear the truncate if any lbns after the
2183207141Sjeff	 * truncate lbn are freed or allocated.
2184207141Sjeff	 */
2185207141Sjeff	sino = ino_lookup(blkrec->jb_ino, 0);
2186207141Sjeff	if (sino && sino->si_trunc) {
2187207141Sjeff		foff = lblktosize(fs, blkrec->jb_lbn);
2188207141Sjeff		foff += lfragtosize(fs, blkrec->jb_frags);
2189207141Sjeff		if (foff > sino->si_trunc->jt_size)
2190207141Sjeff			sino->si_trunc = NULL;
2191207141Sjeff	}
2192207141Sjeff	blk = blknum(fs, blkrec->jb_blkno);
2193207141Sjeff	frag = fragnum(fs, blkrec->jb_blkno);
2194207141Sjeff	sblk = blk_lookup(blk, 1);
2195207141Sjeff	/*
2196207141Sjeff	 * Rewrite the record using oldfrags to indicate the offset into
2197207141Sjeff	 * the block.  Leave jb_frags as the actual allocated count.
2198207141Sjeff	 */
2199207141Sjeff	blkrec->jb_blkno -= frag;
2200207141Sjeff	blkrec->jb_oldfrags = frag;
2201207141Sjeff	if (blkrec->jb_oldfrags + blkrec->jb_frags > fs->fs_frag)
2202209408Sdelphij		err_suj("Invalid fragment count %d oldfrags %d\n",
2203207141Sjeff		    blkrec->jb_frags, frag);
2204207141Sjeff	/*
2205207141Sjeff	 * Detect dups.  If we detect a dup we always discard the oldest
2206207141Sjeff	 * record as it is superseded by the new record.  This speeds up
2207207141Sjeff	 * later stages but also eliminates free records which are used
2208207141Sjeff	 * to indicate that the contents of indirects can be trusted.
2209207141Sjeff	 */
2210207141Sjeff	TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
2211207141Sjeff		blkrn = (struct jblkrec *)srec->sr_rec;
2212207141Sjeff		if (blkrn->jb_ino != blkrec->jb_ino ||
2213207141Sjeff		    blkrn->jb_lbn != blkrec->jb_lbn ||
2214207141Sjeff		    blkrn->jb_blkno != blkrec->jb_blkno ||
2215207141Sjeff		    blkrn->jb_frags != blkrec->jb_frags ||
2216207141Sjeff		    blkrn->jb_oldfrags != blkrec->jb_oldfrags)
2217207141Sjeff			continue;
2218207141Sjeff		if (debug)
2219207141Sjeff			printf("Removed dup.\n");
2220207141Sjeff		/* Discard the free which is a dup with an alloc. */
2221207141Sjeff		if (blkrec->jb_op == JOP_FREEBLK)
2222207141Sjeff			return;
2223207141Sjeff		TAILQ_REMOVE(&sblk->sb_recs, srec, sr_next);
2224207141Sjeff		free(srec);
2225207141Sjeff		break;
2226207141Sjeff	}
2227207141Sjeff	srec = errmalloc(sizeof(*srec));
2228207141Sjeff	srec->sr_rec = (union jrec *)blkrec;
2229207141Sjeff	TAILQ_INSERT_TAIL(&sblk->sb_recs, srec, sr_next);
2230207141Sjeff}
2231207141Sjeff
2232207141Sjeffstatic void
2233207141Sjeffino_build_trunc(struct jtrncrec *rec)
2234207141Sjeff{
2235207141Sjeff	struct suj_ino *sino;
2236207141Sjeff
2237207141Sjeff	if (debug)
2238207141Sjeff		printf("ino_build_trunc: ino %d, size %jd\n",
2239207141Sjeff		    rec->jt_ino, rec->jt_size);
2240207141Sjeff	sino = ino_lookup(rec->jt_ino, 1);
2241207141Sjeff	sino->si_trunc = rec;
2242207141Sjeff}
2243207141Sjeff
2244207141Sjeff/*
2245207141Sjeff * Build up tables of the operations we need to recover.
2246207141Sjeff */
2247207141Sjeffstatic void
2248207141Sjeffsuj_build(void)
2249207141Sjeff{
2250207141Sjeff	struct suj_seg *seg;
2251207141Sjeff	union jrec *rec;
2252207141Sjeff	int off;
2253207141Sjeff	int i;
2254207141Sjeff
2255207141Sjeff	TAILQ_FOREACH(seg, &allsegs, ss_next) {
2256207141Sjeff		if (debug)
2257207141Sjeff			printf("seg %jd has %d records, oldseq %jd.\n",
2258207141Sjeff			    seg->ss_rec.jsr_seq, seg->ss_rec.jsr_cnt,
2259207141Sjeff			    seg->ss_rec.jsr_oldest);
2260207141Sjeff		off = 0;
2261207141Sjeff		rec = (union jrec *)seg->ss_blk;
2262207141Sjeff		for (i = 0; i < seg->ss_rec.jsr_cnt; off += JREC_SIZE, rec++) {
2263207141Sjeff			/* skip the segrec. */
2264207141Sjeff			if ((off % DEV_BSIZE) == 0)
2265207141Sjeff				continue;
2266207141Sjeff			switch (rec->rec_jrefrec.jr_op) {
2267207141Sjeff			case JOP_ADDREF:
2268207141Sjeff			case JOP_REMREF:
2269207141Sjeff			case JOP_MVREF:
2270207141Sjeff				ino_append(rec);
2271207141Sjeff				break;
2272207141Sjeff			case JOP_NEWBLK:
2273207141Sjeff			case JOP_FREEBLK:
2274207141Sjeff				blk_build((struct jblkrec *)rec);
2275207141Sjeff				break;
2276207141Sjeff			case JOP_TRUNC:
2277207141Sjeff				ino_build_trunc((struct jtrncrec *)rec);
2278207141Sjeff				break;
2279207141Sjeff			default:
2280209408Sdelphij				err_suj("Unknown journal operation %d (%d)\n",
2281207141Sjeff				    rec->rec_jrefrec.jr_op, off);
2282207141Sjeff			}
2283207141Sjeff			i++;
2284207141Sjeff		}
2285207141Sjeff	}
2286207141Sjeff}
2287207141Sjeff
2288207141Sjeff/*
2289207141Sjeff * Prune the journal segments to those we care about based on the
2290207141Sjeff * oldest sequence in the newest segment.  Order the segment list
2291207141Sjeff * based on sequence number.
2292207141Sjeff */
2293207141Sjeffstatic void
2294207141Sjeffsuj_prune(void)
2295207141Sjeff{
2296207141Sjeff	struct suj_seg *seg;
2297207141Sjeff	struct suj_seg *segn;
2298207141Sjeff	uint64_t newseq;
2299207141Sjeff	int discard;
2300207141Sjeff
2301207141Sjeff	if (debug)
2302207141Sjeff		printf("Pruning up to %jd\n", oldseq);
2303207141Sjeff	/* First free the expired segments. */
2304207141Sjeff	TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2305207141Sjeff		if (seg->ss_rec.jsr_seq >= oldseq)
2306207141Sjeff			continue;
2307207141Sjeff		TAILQ_REMOVE(&allsegs, seg, ss_next);
2308207141Sjeff		free(seg->ss_blk);
2309207141Sjeff		free(seg);
2310207141Sjeff	}
2311207141Sjeff	/* Next ensure that segments are ordered properly. */
2312207141Sjeff	seg = TAILQ_FIRST(&allsegs);
2313207141Sjeff	if (seg == NULL) {
2314207141Sjeff		if (debug)
2315207141Sjeff			printf("Empty journal\n");
2316207141Sjeff		return;
2317207141Sjeff	}
2318207141Sjeff	newseq = seg->ss_rec.jsr_seq;
2319207141Sjeff	for (;;) {
2320207141Sjeff		seg = TAILQ_LAST(&allsegs, seghd);
2321207141Sjeff		if (seg->ss_rec.jsr_seq >= newseq)
2322207141Sjeff			break;
2323207141Sjeff		TAILQ_REMOVE(&allsegs, seg, ss_next);
2324207141Sjeff		TAILQ_INSERT_HEAD(&allsegs, seg, ss_next);
2325207141Sjeff		newseq = seg->ss_rec.jsr_seq;
2326207141Sjeff
2327207141Sjeff	}
2328209408Sdelphij	if (newseq != oldseq) {
2329209408Sdelphij		err_suj("Journal file sequence mismatch %jd != %jd\n",
2330207141Sjeff		    newseq, oldseq);
2331209408Sdelphij	}
2332207141Sjeff	/*
2333207141Sjeff	 * The kernel may asynchronously write segments which can create
2334207141Sjeff	 * gaps in the sequence space.  Throw away any segments after the
2335207141Sjeff	 * gap as the kernel guarantees only those that are contiguously
2336207141Sjeff	 * reachable are marked as completed.
2337207141Sjeff	 */
2338207141Sjeff	discard = 0;
2339207141Sjeff	TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2340207141Sjeff		if (!discard && newseq++ == seg->ss_rec.jsr_seq) {
2341207141Sjeff			jrecs += seg->ss_rec.jsr_cnt;
2342207141Sjeff			jbytes += seg->ss_rec.jsr_blocks * DEV_BSIZE;
2343207141Sjeff			continue;
2344207141Sjeff		}
2345207141Sjeff		discard = 1;
2346207141Sjeff		if (debug)
2347207141Sjeff			printf("Journal order mismatch %jd != %jd pruning\n",
2348207141Sjeff			    newseq-1, seg->ss_rec.jsr_seq);
2349207141Sjeff		TAILQ_REMOVE(&allsegs, seg, ss_next);
2350207141Sjeff		free(seg->ss_blk);
2351207141Sjeff		free(seg);
2352207141Sjeff	}
2353207141Sjeff	if (debug)
2354207141Sjeff		printf("Processing journal segments from %jd to %jd\n",
2355207141Sjeff		    oldseq, newseq-1);
2356207141Sjeff}
2357207141Sjeff
2358207141Sjeff/*
2359207141Sjeff * Verify the journal inode before attempting to read records.
2360207141Sjeff */
2361207141Sjeffstatic int
2362207141Sjeffsuj_verifyino(union dinode *ip)
2363207141Sjeff{
2364207141Sjeff
2365207141Sjeff	if (DIP(ip, di_nlink) != 1) {
2366207141Sjeff		printf("Invalid link count %d for journal inode %d\n",
2367207141Sjeff		    DIP(ip, di_nlink), sujino);
2368207141Sjeff		return (-1);
2369207141Sjeff	}
2370207141Sjeff
2371207141Sjeff	if ((DIP(ip, di_flags) & (SF_IMMUTABLE | SF_NOUNLINK)) !=
2372207141Sjeff	    (SF_IMMUTABLE | SF_NOUNLINK)) {
2373207141Sjeff		printf("Invalid flags 0x%X for journal inode %d\n",
2374207141Sjeff		    DIP(ip, di_flags), sujino);
2375207141Sjeff		return (-1);
2376207141Sjeff	}
2377207141Sjeff
2378207141Sjeff	if (DIP(ip, di_mode) != (IFREG | IREAD)) {
2379207141Sjeff		printf("Invalid mode %o for journal inode %d\n",
2380207141Sjeff		    DIP(ip, di_mode), sujino);
2381207141Sjeff		return (-1);
2382207141Sjeff	}
2383207141Sjeff
2384207141Sjeff	if (DIP(ip, di_size) < SUJ_MIN || DIP(ip, di_size) > SUJ_MAX) {
2385207141Sjeff		printf("Invalid size %jd for journal inode %d\n",
2386207141Sjeff		    DIP(ip, di_size), sujino);
2387207141Sjeff		return (-1);
2388207141Sjeff	}
2389207141Sjeff
2390207141Sjeff	if (DIP(ip, di_modrev) != fs->fs_mtime) {
2391207141Sjeff		printf("Journal timestamp does not match fs mount time\n");
2392207141Sjeff		return (-1);
2393207141Sjeff	}
2394207141Sjeff
2395207141Sjeff	return (0);
2396207141Sjeff}
2397207141Sjeff
2398207141Sjeffstruct jblocks {
2399207141Sjeff	struct jextent *jb_extent;	/* Extent array. */
2400207141Sjeff	int		jb_avail;	/* Available extents. */
2401207141Sjeff	int		jb_used;	/* Last used extent. */
2402207141Sjeff	int		jb_head;	/* Allocator head. */
2403207141Sjeff	int		jb_off;		/* Allocator extent offset. */
2404207141Sjeff};
2405207141Sjeffstruct jextent {
2406207141Sjeff	ufs2_daddr_t	je_daddr;	/* Disk block address. */
2407207141Sjeff	int		je_blocks;	/* Disk block count. */
2408207141Sjeff};
2409207141Sjeff
2410207141Sjeffstruct jblocks *suj_jblocks;
2411207141Sjeff
2412207141Sjeffstatic struct jblocks *
2413207141Sjeffjblocks_create(void)
2414207141Sjeff{
2415207141Sjeff	struct jblocks *jblocks;
2416207141Sjeff	int size;
2417207141Sjeff
2418207141Sjeff	jblocks = errmalloc(sizeof(*jblocks));
2419207141Sjeff	jblocks->jb_avail = 10;
2420207141Sjeff	jblocks->jb_used = 0;
2421207141Sjeff	jblocks->jb_head = 0;
2422207141Sjeff	jblocks->jb_off = 0;
2423207141Sjeff	size = sizeof(struct jextent) * jblocks->jb_avail;
2424207141Sjeff	jblocks->jb_extent = errmalloc(size);
2425207141Sjeff	bzero(jblocks->jb_extent, size);
2426207141Sjeff
2427207141Sjeff	return (jblocks);
2428207141Sjeff}
2429207141Sjeff
2430207141Sjeff/*
2431207141Sjeff * Return the next available disk block and the amount of contiguous
2432207141Sjeff * free space it contains.
2433207141Sjeff */
2434207141Sjeffstatic ufs2_daddr_t
2435207141Sjeffjblocks_next(struct jblocks *jblocks, int bytes, int *actual)
2436207141Sjeff{
2437207141Sjeff	struct jextent *jext;
2438207141Sjeff	ufs2_daddr_t daddr;
2439207141Sjeff	int freecnt;
2440207141Sjeff	int blocks;
2441207141Sjeff
2442207141Sjeff	blocks = bytes / DEV_BSIZE;
2443207141Sjeff	jext = &jblocks->jb_extent[jblocks->jb_head];
2444207141Sjeff	freecnt = jext->je_blocks - jblocks->jb_off;
2445207141Sjeff	if (freecnt == 0) {
2446207141Sjeff		jblocks->jb_off = 0;
2447207141Sjeff		if (++jblocks->jb_head > jblocks->jb_used)
2448207141Sjeff			return (0);
2449207141Sjeff		jext = &jblocks->jb_extent[jblocks->jb_head];
2450207141Sjeff		freecnt = jext->je_blocks;
2451207141Sjeff	}
2452207141Sjeff	if (freecnt > blocks)
2453207141Sjeff		freecnt = blocks;
2454207141Sjeff	*actual = freecnt * DEV_BSIZE;
2455207141Sjeff	daddr = jext->je_daddr + jblocks->jb_off;
2456207141Sjeff
2457207141Sjeff	return (daddr);
2458207141Sjeff}
2459207141Sjeff
2460207141Sjeff/*
2461207141Sjeff * Advance the allocation head by a specified number of bytes, consuming
2462207141Sjeff * one journal segment.
2463207141Sjeff */
2464207141Sjeffstatic void
2465207141Sjeffjblocks_advance(struct jblocks *jblocks, int bytes)
2466207141Sjeff{
2467207141Sjeff
2468207141Sjeff	jblocks->jb_off += bytes / DEV_BSIZE;
2469207141Sjeff}
2470207141Sjeff
2471207141Sjeffstatic void
2472207141Sjeffjblocks_destroy(struct jblocks *jblocks)
2473207141Sjeff{
2474207141Sjeff
2475207141Sjeff	free(jblocks->jb_extent);
2476207141Sjeff	free(jblocks);
2477207141Sjeff}
2478207141Sjeff
2479207141Sjeffstatic void
2480207141Sjeffjblocks_add(struct jblocks *jblocks, ufs2_daddr_t daddr, int blocks)
2481207141Sjeff{
2482207141Sjeff	struct jextent *jext;
2483207141Sjeff	int size;
2484207141Sjeff
2485207141Sjeff	jext = &jblocks->jb_extent[jblocks->jb_used];
2486207141Sjeff	/* Adding the first block. */
2487207141Sjeff	if (jext->je_daddr == 0) {
2488207141Sjeff		jext->je_daddr = daddr;
2489207141Sjeff		jext->je_blocks = blocks;
2490207141Sjeff		return;
2491207141Sjeff	}
2492207141Sjeff	/* Extending the last extent. */
2493207141Sjeff	if (jext->je_daddr + jext->je_blocks == daddr) {
2494207141Sjeff		jext->je_blocks += blocks;
2495207141Sjeff		return;
2496207141Sjeff	}
2497207141Sjeff	/* Adding a new extent. */
2498207141Sjeff	if (++jblocks->jb_used == jblocks->jb_avail) {
2499207141Sjeff		jblocks->jb_avail *= 2;
2500207141Sjeff		size = sizeof(struct jextent) * jblocks->jb_avail;
2501207141Sjeff		jext = errmalloc(size);
2502207141Sjeff		bzero(jext, size);
2503207141Sjeff		bcopy(jblocks->jb_extent, jext,
2504207141Sjeff		    sizeof(struct jextent) * jblocks->jb_used);
2505207141Sjeff		free(jblocks->jb_extent);
2506207141Sjeff		jblocks->jb_extent = jext;
2507207141Sjeff	}
2508207141Sjeff	jext = &jblocks->jb_extent[jblocks->jb_used];
2509207141Sjeff	jext->je_daddr = daddr;
2510207141Sjeff	jext->je_blocks = blocks;
2511207141Sjeff
2512207141Sjeff	return;
2513207141Sjeff}
2514207141Sjeff
2515207141Sjeff/*
2516207141Sjeff * Add a file block from the journal to the extent map.  We can't read
2517207141Sjeff * each file block individually because the kernel treats it as a circular
2518207141Sjeff * buffer and segments may span mutliple contiguous blocks.
2519207141Sjeff */
2520207141Sjeffstatic void
2521207141Sjeffsuj_add_block(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
2522207141Sjeff{
2523207141Sjeff
2524207141Sjeff	jblocks_add(suj_jblocks, fsbtodb(fs, blk), fsbtodb(fs, frags));
2525207141Sjeff}
2526207141Sjeff
2527207141Sjeffstatic void
2528207141Sjeffsuj_read(void)
2529207141Sjeff{
2530207141Sjeff	uint8_t block[1 * 1024 * 1024];
2531207141Sjeff	struct suj_seg *seg;
2532207141Sjeff	struct jsegrec *recn;
2533207141Sjeff	struct jsegrec *rec;
2534207141Sjeff	ufs2_daddr_t blk;
2535207141Sjeff	int readsize;
2536207141Sjeff	int blocks;
2537207141Sjeff	int recsize;
2538207141Sjeff	int size;
2539207141Sjeff	int i;
2540207141Sjeff
2541207141Sjeff	/*
2542207141Sjeff	 * Read records until we exhaust the journal space.  If we find
2543207141Sjeff	 * an invalid record we start searching for a valid segment header
2544207141Sjeff	 * at the next block.  This is because we don't have a head/tail
2545207141Sjeff	 * pointer and must recover the information indirectly.  At the gap
2546207141Sjeff	 * between the head and tail we won't necessarily have a valid
2547207141Sjeff	 * segment.
2548207141Sjeff	 */
2549207141Sjeffrestart:
2550207141Sjeff	for (;;) {
2551207141Sjeff		size = sizeof(block);
2552207141Sjeff		blk = jblocks_next(suj_jblocks, size, &readsize);
2553207141Sjeff		if (blk == 0)
2554207141Sjeff			return;
2555207141Sjeff		size = readsize;
2556207141Sjeff		/*
2557207141Sjeff		 * Read 1MB at a time and scan for records within this block.
2558207141Sjeff		 */
2559209408Sdelphij		if (bread(disk, blk, &block, size) == -1) {
2560209408Sdelphij			err_suj("Error reading journal block %jd\n",
2561207141Sjeff			    (intmax_t)blk);
2562209408Sdelphij		}
2563207141Sjeff		for (rec = (void *)block; size; size -= recsize,
2564207141Sjeff		    rec = (struct jsegrec *)((uintptr_t)rec + recsize)) {
2565207141Sjeff			recsize = DEV_BSIZE;
2566207141Sjeff			if (rec->jsr_time != fs->fs_mtime) {
2567207141Sjeff				if (debug)
2568207141Sjeff					printf("Rec time %jd != fs mtime %jd\n",
2569207141Sjeff					    rec->jsr_time, fs->fs_mtime);
2570207141Sjeff				jblocks_advance(suj_jblocks, recsize);
2571207141Sjeff				continue;
2572207141Sjeff			}
2573207141Sjeff			if (rec->jsr_cnt == 0) {
2574207141Sjeff				if (debug)
2575207141Sjeff					printf("Found illegal count %d\n",
2576207141Sjeff					    rec->jsr_cnt);
2577207141Sjeff				jblocks_advance(suj_jblocks, recsize);
2578207141Sjeff				continue;
2579207141Sjeff			}
2580207141Sjeff			blocks = rec->jsr_blocks;
2581207141Sjeff			recsize = blocks * DEV_BSIZE;
2582207141Sjeff			if (recsize > size) {
2583207141Sjeff				/*
2584207141Sjeff				 * We may just have run out of buffer, restart
2585207141Sjeff				 * the loop to re-read from this spot.
2586207141Sjeff				 */
2587207141Sjeff				if (size < fs->fs_bsize &&
2588207141Sjeff				    size != readsize &&
2589207141Sjeff				    recsize <= fs->fs_bsize)
2590207141Sjeff					goto restart;
2591207141Sjeff				if (debug)
2592207141Sjeff					printf("Found invalid segsize %d > %d\n",
2593207141Sjeff					    recsize, size);
2594207141Sjeff				recsize = DEV_BSIZE;
2595207141Sjeff				jblocks_advance(suj_jblocks, recsize);
2596207141Sjeff				continue;
2597207141Sjeff			}
2598207141Sjeff			/*
2599207141Sjeff			 * Verify that all blocks in the segment are present.
2600207141Sjeff			 */
2601207141Sjeff			for (i = 1; i < blocks; i++) {
2602207141Sjeff				recn = (void *)
2603207141Sjeff				    ((uintptr_t)rec) + i * DEV_BSIZE;
2604207141Sjeff				if (recn->jsr_seq == rec->jsr_seq &&
2605207141Sjeff				    recn->jsr_time == rec->jsr_time)
2606207141Sjeff					continue;
2607207141Sjeff				if (debug)
2608207141Sjeff					printf("Incomplete record %jd (%d)\n",
2609207141Sjeff					    rec->jsr_seq, i);
2610207141Sjeff				recsize = i * DEV_BSIZE;
2611207141Sjeff				jblocks_advance(suj_jblocks, recsize);
2612207141Sjeff				goto restart;
2613207141Sjeff			}
2614207141Sjeff			seg = errmalloc(sizeof(*seg));
2615207141Sjeff			seg->ss_blk = errmalloc(recsize);
2616207141Sjeff			seg->ss_rec = *rec;
2617207141Sjeff			bcopy((void *)rec, seg->ss_blk, recsize);
2618207141Sjeff			if (rec->jsr_oldest > oldseq)
2619207141Sjeff				oldseq = rec->jsr_oldest;
2620207141Sjeff			TAILQ_INSERT_TAIL(&allsegs, seg, ss_next);
2621207141Sjeff			jblocks_advance(suj_jblocks, recsize);
2622207141Sjeff		}
2623207141Sjeff	}
2624207141Sjeff}
2625207141Sjeff
2626207141Sjeff/*
2627207141Sjeff * Search a directory block for the SUJ_FILE.
2628207141Sjeff */
2629207141Sjeffstatic void
2630207141Sjeffsuj_find(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
2631207141Sjeff{
2632207141Sjeff	char block[MAXBSIZE];
2633207141Sjeff	struct direct *dp;
2634207141Sjeff	int bytes;
2635207141Sjeff	int off;
2636207141Sjeff
2637207141Sjeff	if (sujino)
2638207141Sjeff		return;
2639207141Sjeff	bytes = lfragtosize(fs, frags);
2640207141Sjeff	if (bread(disk, fsbtodb(fs, blk), block, bytes) <= 0)
2641209408Sdelphij		err_suj("Failed to read ROOTINO directory block %jd\n", blk);
2642207141Sjeff	for (off = 0; off < bytes; off += dp->d_reclen) {
2643207141Sjeff		dp = (struct direct *)&block[off];
2644207141Sjeff		if (dp->d_reclen == 0)
2645207141Sjeff			break;
2646207141Sjeff		if (dp->d_ino == 0)
2647207141Sjeff			continue;
2648207141Sjeff		if (dp->d_namlen != strlen(SUJ_FILE))
2649207141Sjeff			continue;
2650207141Sjeff		if (bcmp(dp->d_name, SUJ_FILE, dp->d_namlen) != 0)
2651207141Sjeff			continue;
2652207141Sjeff		sujino = dp->d_ino;
2653207141Sjeff		return;
2654207141Sjeff	}
2655207141Sjeff}
2656207141Sjeff
2657207141Sjeff/*
2658207141Sjeff * Orchestrate the verification of a filesystem via the softupdates journal.
2659207141Sjeff */
2660207141Sjeffint
2661207141Sjeffsuj_check(const char *filesys)
2662207141Sjeff{
2663207141Sjeff	union dinode *jip;
2664207141Sjeff	union dinode *ip;
2665207141Sjeff	uint64_t blocks;
2666209408Sdelphij	int retval;
2667209408Sdelphij	struct suj_seg *seg;
2668209408Sdelphij	struct suj_seg *segn;
2669207141Sjeff
2670207141Sjeff	opendisk(filesys);
2671207141Sjeff	TAILQ_INIT(&allsegs);
2672209408Sdelphij
2673207141Sjeff	/*
2674209408Sdelphij	 * Set an exit point when SUJ check failed
2675209408Sdelphij	 */
2676209408Sdelphij	retval = setjmp(jmpbuf);
2677209408Sdelphij	if (retval != 0) {
2678209408Sdelphij		pwarn("UNEXPECTED SU+J INCONSISTENCY\n");
2679209408Sdelphij		TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2680209408Sdelphij			TAILQ_REMOVE(&allsegs, seg, ss_next);
2681209408Sdelphij				free(seg->ss_blk);
2682209408Sdelphij				free(seg);
2683209408Sdelphij		}
2684209408Sdelphij		if (reply("FALLBACK TO FULL FSCK") == 0) {
2685209408Sdelphij			ckfini(0);
2686209408Sdelphij			exit(EEXIT);
2687209408Sdelphij		} else
2688209408Sdelphij			return (-1);
2689209408Sdelphij	}
2690209408Sdelphij
2691209408Sdelphij	/*
2692207141Sjeff	 * Find the journal inode.
2693207141Sjeff	 */
2694207141Sjeff	ip = ino_read(ROOTINO);
2695207141Sjeff	sujino = 0;
2696207141Sjeff	ino_visit(ip, ROOTINO, suj_find, 0);
2697209408Sdelphij	if (sujino == 0) {
2698209408Sdelphij		printf("Journal inode removed.  Use tunefs to re-create.\n");
2699209408Sdelphij		sblock.fs_flags &= ~FS_SUJ;
2700209408Sdelphij		sblock.fs_sujfree = 0;
2701209408Sdelphij		return (-1);
2702209408Sdelphij	}
2703207141Sjeff	/*
2704207141Sjeff	 * Fetch the journal inode and verify it.
2705207141Sjeff	 */
2706207141Sjeff	jip = ino_read(sujino);
2707207141Sjeff	printf("** SU+J Recovering %s\n", filesys);
2708207141Sjeff	if (suj_verifyino(jip) != 0)
2709207141Sjeff		return (-1);
2710207141Sjeff	/*
2711207141Sjeff	 * Build a list of journal blocks in jblocks before parsing the
2712207141Sjeff	 * available journal blocks in with suj_read().
2713207141Sjeff	 */
2714207141Sjeff	printf("** Reading %jd byte journal from inode %d.\n",
2715207141Sjeff	    DIP(jip, di_size), sujino);
2716207141Sjeff	suj_jblocks = jblocks_create();
2717207141Sjeff	blocks = ino_visit(jip, sujino, suj_add_block, 0);
2718209408Sdelphij	if (blocks != numfrags(fs, DIP(jip, di_size))) {
2719209408Sdelphij		printf("Sparse journal inode %d.\n", sujino);
2720209408Sdelphij		return (-1);
2721209408Sdelphij	}
2722207141Sjeff	suj_read();
2723207141Sjeff	jblocks_destroy(suj_jblocks);
2724207141Sjeff	suj_jblocks = NULL;
2725207141Sjeff	if (preen || reply("RECOVER")) {
2726207141Sjeff		printf("** Building recovery table.\n");
2727207141Sjeff		suj_prune();
2728207141Sjeff		suj_build();
2729207141Sjeff		cg_apply(cg_build);
2730207141Sjeff		printf("** Resolving unreferenced inode list.\n");
2731207141Sjeff		ino_unlinked();
2732207141Sjeff		printf("** Processing journal entries.\n");
2733207141Sjeff		cg_apply(cg_trunc);
2734207141Sjeff		cg_apply(cg_check_blk);
2735207141Sjeff		cg_apply(cg_check_ino);
2736207141Sjeff	}
2737209408Sdelphij	if (preen == 0 && (jrecs > 0 || jbytes > 0) && reply("WRITE CHANGES") == 0)
2738207141Sjeff		return (0);
2739207141Sjeff	/*
2740207141Sjeff	 * To remain idempotent with partial truncations the free bitmaps
2741207141Sjeff	 * must be written followed by indirect blocks and lastly inode
2742207141Sjeff	 * blocks.  This preserves access to the modified pointers until
2743207141Sjeff	 * they are freed.
2744207141Sjeff	 */
2745207141Sjeff	cg_apply(cg_write);
2746207141Sjeff	dblk_write();
2747207141Sjeff	cg_apply(cg_write_inos);
2748207141Sjeff	/* Write back superblock. */
2749207141Sjeff	closedisk(filesys);
2750209408Sdelphij	if (jrecs > 0 || jbytes > 0) {
2751209408Sdelphij		printf("** %jd journal records in %jd bytes for %.2f%% utilization\n",
2752209408Sdelphij		    jrecs, jbytes, ((float)jrecs / (float)(jbytes / JREC_SIZE)) * 100);
2753209408Sdelphij		printf("** Freed %jd inodes (%jd dirs) %jd blocks, and %jd frags.\n",
2754209408Sdelphij		    freeinos, freedir, freeblocks, freefrags);
2755209408Sdelphij	}
2756207141Sjeff
2757207141Sjeff	return (0);
2758207141Sjeff}
2759