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