2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright 2009, 2010 Jeffrey W. Roberson <jeff@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/disklabel.h>
35 #include <sys/mount.h>
38 #include <ufs/ufs/extattr.h>
39 #include <ufs/ufs/quota.h>
40 #include <ufs/ufs/ufsmount.h>
41 #include <ufs/ufs/dinode.h>
42 #include <ufs/ufs/dir.h>
43 #include <ufs/ffs/fs.h>
60 #define DOTDOT_OFFSET DIRECTSIZ(1)
63 TAILQ_ENTRY(suj_seg) ss_next;
64 struct jsegrec ss_rec;
69 TAILQ_ENTRY(suj_rec) sr_next;
72 TAILQ_HEAD(srechd, suj_rec);
75 LIST_ENTRY(suj_ino) si_next;
76 struct srechd si_recs;
77 struct srechd si_newrecs;
78 struct srechd si_movs;
79 struct jtrncrec *si_trunc;
90 LIST_HEAD(inohd, suj_ino);
93 LIST_ENTRY(suj_blk) sb_next;
94 struct srechd sb_recs;
97 LIST_HEAD(blkhd, suj_blk);
100 LIST_ENTRY(suj_cg) sc_next;
101 struct blkhd sc_blkhash[HASHSIZE];
102 struct inohd sc_inohash[HASHSIZE];
103 struct ino_blk *sc_lastiblk;
104 struct suj_ino *sc_lastino;
105 struct suj_blk *sc_lastblk;
106 struct bufarea *sc_cgbp;
111 static LIST_HEAD(cghd, suj_cg) cghash[HASHSIZE];
112 static struct suj_cg *lastcg;
114 static TAILQ_HEAD(seghd, suj_seg) allsegs;
115 static uint64_t oldseq;
116 static struct fs *fs = NULL;
120 * Summary statistics.
122 static uint64_t freefrags;
123 static uint64_t freeblocks;
124 static uint64_t freeinos;
125 static uint64_t freedir;
126 static uint64_t jbytes;
127 static uint64_t jrecs;
129 static jmp_buf jmpbuf;
131 typedef void (*ino_visitor)(ino_t, ufs_lbn_t, ufs2_daddr_t, int);
132 static void err_suj(const char *, ...) __dead2;
133 static void ino_trunc(ino_t, off_t);
134 static void ino_decr(ino_t);
135 static void ino_adjust(struct suj_ino *);
136 static void ino_build(struct suj_ino *);
137 static int blk_isfree(ufs2_daddr_t);
138 static void initsuj(void);
147 err(EX_OSERR, "malloc(%zu)", n);
152 * When hit a fatal error in journalling check, print out
153 * the error and then offer to fallback to normal fsck.
156 err_suj(const char * restrict fmt, ...)
161 (void)fprintf(stdout, "%s: ", cdevname);
164 (void)vfprintf(stdout, fmt, ap);
171 * Lookup a cg by number in the hash so we can keep track of which cgs
172 * need stats rebuilt.
174 static struct suj_cg *
179 struct bufarea *cgbp;
181 if (cgx < 0 || cgx >= fs->fs_ncg)
182 err_suj("Bad cg number %d\n", cgx);
183 if (lastcg && lastcg->sc_cgx == cgx)
185 cgbp = cglookup(cgx);
186 if (!check_cgmagic(cgx, cgbp))
187 err_suj("UNABLE TO REBUILD CYLINDER GROUP %d", cgx);
188 hd = &cghash[HASH(cgx)];
189 LIST_FOREACH(sc, hd, sc_next)
190 if (sc->sc_cgx == cgx) {
192 sc->sc_cgp = sc->sc_cgbp->b_un.b_cg;
196 sc = errmalloc(sizeof(*sc));
197 bzero(sc, sizeof(*sc));
199 sc->sc_cgp = sc->sc_cgbp->b_un.b_cg;
201 LIST_INSERT_HEAD(hd, sc, sc_next);
206 * Lookup an inode number in the hash and allocate a suj_ino if it does
209 static struct suj_ino *
210 ino_lookup(ino_t ino, int creat)
212 struct suj_ino *sino;
216 sc = cg_lookup(ino_to_cg(fs, ino));
217 if (sc->sc_lastino && sc->sc_lastino->si_ino == ino)
218 return (sc->sc_lastino);
219 hd = &sc->sc_inohash[HASH(ino)];
220 LIST_FOREACH(sino, hd, si_next)
221 if (sino->si_ino == ino)
225 sino = errmalloc(sizeof(*sino));
226 bzero(sino, sizeof(*sino));
228 TAILQ_INIT(&sino->si_recs);
229 TAILQ_INIT(&sino->si_newrecs);
230 TAILQ_INIT(&sino->si_movs);
231 LIST_INSERT_HEAD(hd, sino, si_next);
237 * Lookup a block number in the hash and allocate a suj_blk if it does
240 static struct suj_blk *
241 blk_lookup(ufs2_daddr_t blk, int creat)
243 struct suj_blk *sblk;
247 sc = cg_lookup(dtog(fs, blk));
248 if (sc->sc_lastblk && sc->sc_lastblk->sb_blk == blk)
249 return (sc->sc_lastblk);
250 hd = &sc->sc_blkhash[HASH(fragstoblks(fs, blk))];
251 LIST_FOREACH(sblk, hd, sb_next)
252 if (sblk->sb_blk == blk)
256 sblk = errmalloc(sizeof(*sblk));
257 bzero(sblk, sizeof(*sblk));
259 TAILQ_INIT(&sblk->sb_recs);
260 LIST_INSERT_HEAD(hd, sblk, sb_next);
266 blk_overlaps(struct jblkrec *brec, ufs2_daddr_t start, int frags)
273 bstart = brec->jb_blkno + brec->jb_oldfrags;
274 bend = bstart + brec->jb_frags;
275 if (start < bend && end > bstart)
281 blk_equals(struct jblkrec *brec, ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t start,
285 if (brec->jb_ino != ino || brec->jb_lbn != lbn)
287 if (brec->jb_blkno + brec->jb_oldfrags != start)
289 if (brec->jb_frags < frags)
295 blk_setmask(struct jblkrec *brec, int *mask)
299 for (i = brec->jb_oldfrags; i < brec->jb_oldfrags + brec->jb_frags; i++)
304 * Determine whether a given block has been reallocated to a new location.
305 * Returns a mask of overlapping bits if any frags have been reused or
306 * zero if the block has not been re-used and the contents can be trusted.
308 * This is used to ensure that an orphaned pointer due to truncate is safe
309 * to be freed. The mask value can be used to free partial blocks.
312 blk_freemask(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags)
314 struct suj_blk *sblk;
315 struct suj_rec *srec;
316 struct jblkrec *brec;
321 * To be certain we're not freeing a reallocated block we lookup
322 * this block in the blk hash and see if there is an allocation
323 * journal record that overlaps with any fragments in the block
324 * we're concerned with. If any fragments have been reallocated
325 * the block has already been freed and re-used for another purpose.
328 sblk = blk_lookup(blknum(fs, blk), 0);
331 off = blk - sblk->sb_blk;
332 TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
333 brec = (struct jblkrec *)srec->sr_rec;
335 * If the block overlaps but does not match
336 * exactly this record refers to the current
339 if (blk_overlaps(brec, blk, frags) == 0)
341 if (blk_equals(brec, ino, lbn, blk, frags) == 1)
344 blk_setmask(brec, &mask);
347 printf("blk_freemask: blk %jd sblk %jd off %d mask 0x%X\n",
348 blk, sblk->sb_blk, off, mask);
349 return (mask >> off);
353 * Determine whether it is safe to follow an indirect. It is not safe
354 * if any part of the indirect has been reallocated or the last journal
355 * entry was an allocation. Just allocated indirects may not have valid
356 * pointers yet and all of their children will have their own records.
357 * It is also not safe to follow an indirect if the cg bitmap has been
358 * cleared as a new allocation may write to the block prior to the journal
361 * Returns 1 if it's safe to follow the indirect and 0 otherwise.
364 blk_isindir(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn)
366 struct suj_blk *sblk;
367 struct jblkrec *brec;
369 sblk = blk_lookup(blk, 0);
372 if (TAILQ_EMPTY(&sblk->sb_recs))
374 brec = (struct jblkrec *)TAILQ_LAST(&sblk->sb_recs, srechd)->sr_rec;
375 if (blk_equals(brec, ino, lbn, blk, fs->fs_frag))
376 if (brec->jb_op == JOP_FREEBLK)
377 return (!blk_isfree(blk));
382 * Check to see if the requested block is available.
383 * We can just check in the cylinder-group maps as
384 * they will only have usable blocks in them.
387 suj_checkblkavail(ufs2_daddr_t blkno, long frags)
389 struct bufarea *cgbp;
391 ufs2_daddr_t j, k, baseblk;
394 if ((u_int64_t)blkno > sblock.fs_size)
396 cg = dtog(&sblock, blkno);
398 cgp = cgbp->b_un.b_cg;
399 if (!check_cgmagic(cg, cgbp))
400 return (-((cg + 1) * sblock.fs_fpg - sblock.fs_frag));
401 baseblk = dtogd(&sblock, blkno);
402 for (j = 0; j <= sblock.fs_frag - frags; j++) {
403 if (!isset(cg_blksfree(cgp), baseblk + j))
405 for (k = 1; k < frags; k++)
406 if (!isset(cg_blksfree(cgp), baseblk + j + k))
412 for (k = 0; k < frags; k++)
413 clrbit(cg_blksfree(cgp), baseblk + j + k);
415 if (frags == sblock.fs_frag)
416 cgp->cg_cs.cs_nbfree--;
418 cgp->cg_cs.cs_nffree -= frags;
420 return ((cg * sblock.fs_fpg) + baseblk + j);
426 * Clear an inode from the cg bitmap. If the inode was already clear return
427 * 0 so the caller knows it does not have to check the inode contents.
430 ino_free(ino_t ino, int mode)
437 cg = ino_to_cg(fs, ino);
438 ino = ino % fs->fs_ipg;
441 inosused = cg_inosused(cgp);
443 * The bitmap may never have made it to the disk so we have to
444 * conditionally clear. We can avoid writing the cg in this case.
446 if (isclr(inosused, ino))
449 clrbit(inosused, ino);
450 if (ino < cgp->cg_irotor)
451 cgp->cg_irotor = ino;
452 cgp->cg_cs.cs_nifree++;
453 if ((mode & IFMT) == IFDIR) {
455 cgp->cg_cs.cs_ndir--;
457 cgdirty(sc->sc_cgbp);
463 * Free 'frags' frags starting at filesystem block 'bno' skipping any frags
467 blk_free(ino_t ino, ufs2_daddr_t bno, int mask, int frags)
469 ufs1_daddr_t fragno, cgbno;
476 printf("Freeing %d frags at blk %jd mask 0x%x\n",
479 * Check to see if the block needs to be claimed by a snapshot.
480 * If wanted, the snapshot references it. Otherwise we free it.
482 if (snapblkfree(fs, bno, lfragtosize(fs, frags), ino,
488 cgbno = dtogd(fs, bno);
489 blksfree = cg_blksfree(cgp);
492 * If it's not allocated we only wrote the journal entry
493 * and never the bitmaps. Here we unconditionally clear and
494 * resolve the cg summary later.
496 if (frags == fs->fs_frag && mask == 0) {
497 fragno = fragstoblks(fs, cgbno);
498 ffs_setblock(fs, blksfree, fragno);
502 * deallocate the fragment
504 for (i = 0; i < frags; i++)
505 if ((mask & (1 << i)) == 0 && isclr(blksfree, cgbno +i)) {
507 setbit(blksfree, cgbno + i);
510 cgdirty(sc->sc_cgbp);
514 * Returns 1 if the whole block starting at 'bno' is marked free and 0
518 blk_isfree(ufs2_daddr_t bno)
522 sc = cg_lookup(dtog(fs, bno));
523 return ffs_isblock(fs, cg_blksfree(sc->sc_cgp), dtogd(fs, bno));
527 * Determine whether a block exists at a particular lbn in an inode.
528 * Returns 1 if found, 0 if not. lbn may be negative for indirects
532 blk_isat(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int *frags)
540 if (DIP(dp, di_nlink) == 0 || DIP(dp, di_mode) == 0) {
544 nblk = ino_blkatoff(dp, ino, lbn, frags, NULL);
546 return (nblk == blk);
550 * Clear the directory entry at diroff that should point to child. Minimal
551 * checking is done and it is assumed that this path was verified with isat.
554 ino_clrat(ino_t parent, off_t diroff, ino_t child)
567 printf("Clearing inode %ju from parent %ju at offset %jd\n",
568 (uintmax_t)child, (uintmax_t)parent, diroff);
570 lbn = lblkno(fs, diroff);
571 doff = blkoff(fs, diroff);
574 blk = ino_blkatoff(dip, parent, lbn, &frags, NULL);
575 blksize = sblksize(fs, DIP(dip, di_size), lbn);
577 bp = getdatablk(blk, blksize, BT_DIRDATA);
579 err_suj("ino_clrat: UNRECOVERABLE I/O ERROR");
580 dp = (struct direct *)&bp->b_un.b_buf[doff];
581 if (dp->d_ino != child)
582 errx(1, "Inode %ju does not exist in %ju at %jd",
583 (uintmax_t)child, (uintmax_t)parent, diroff);
588 * The actual .. reference count will already have been removed
589 * from the parent by the .. remref record.
594 * Determines whether a pointer to an inode exists within a directory
595 * at a specified offset. Returns the mode of the found entry.
598 ino_isat(ino_t parent, off_t diroff, ino_t child, int *mode, int *isdot)
614 *mode = DIP(dip, di_mode);
615 if ((*mode & IFMT) != IFDIR) {
618 * This can happen if the parent inode
622 printf("Directory %ju has bad mode %o\n",
623 (uintmax_t)parent, *mode);
625 printf("Directory %ju has zero mode\n",
631 lbn = lblkno(fs, diroff);
632 doff = blkoff(fs, diroff);
633 blksize = sblksize(fs, DIP(dip, di_size), lbn);
634 if (diroff + DIRECTSIZ(1) > DIP(dip, di_size) || doff >= blksize) {
636 printf("ino %ju absent from %ju due to offset %jd"
637 " exceeding size %jd\n",
638 (uintmax_t)child, (uintmax_t)parent, diroff,
643 blk = ino_blkatoff(dip, parent, lbn, &frags, NULL);
647 printf("Sparse directory %ju", (uintmax_t)parent);
650 bp = getdatablk(blk, blksize, BT_DIRDATA);
652 err_suj("ino_isat: UNRECOVERABLE I/O ERROR");
654 * Walk through the records from the start of the block to be
655 * certain we hit a valid record and not some junk in the middle
656 * of a file name. Stop when we reach or pass the expected offset.
658 dpoff = rounddown(doff, DIRBLKSIZ);
660 dp = (struct direct *)&bp->b_un.b_buf[dpoff];
663 if (dp->d_reclen == 0)
665 dpoff += dp->d_reclen;
666 } while (dpoff <= doff);
667 if (dpoff > fs->fs_bsize)
668 err_suj("Corrupt directory block in dir ino %ju\n",
673 printf("ino %ju not found in %ju, lbn %jd, dpoff %d\n",
674 (uintmax_t)child, (uintmax_t)parent, lbn, dpoff);
679 * We found the item in question. Record the mode and whether it's
680 * a . or .. link for the caller.
682 if (dp->d_ino == child) {
685 else if (dp->d_namlen == 2 &&
686 dp->d_name[0] == '.' && dp->d_name[1] == '.')
688 *mode = DTTOIF(dp->d_type);
693 printf("ino %ju doesn't match dirent ino %ju in parent %ju\n",
694 (uintmax_t)child, (uintmax_t)dp->d_ino, (uintmax_t)parent);
699 #define VISIT_INDIR 0x0001
700 #define VISIT_EXT 0x0002
701 #define VISIT_ROOT 0x0004 /* Operation came via root & valid pointers. */
704 * Read an indirect level which may or may not be linked into an inode.
707 indir_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, uint64_t *frags,
708 ino_visitor visitor, int flags)
718 * Don't visit indirect blocks with contents we can't trust. This
719 * should only happen when indir_visit() is called to complete a
720 * truncate that never finished and not when a pointer is found via
725 level = lbn_level(lbn);
727 err_suj("Invalid level for lbn %jd\n", lbn);
728 if ((flags & VISIT_ROOT) == 0 && blk_isindir(blk, ino, lbn) == 0) {
730 printf("blk %jd ino %ju lbn %jd(%d) is not indir.\n",
731 blk, (uintmax_t)ino, lbn, level);
735 for (i = level; i > 0; i--)
736 lbnadd *= NINDIR(fs);
737 bp = getdatablk(blk, fs->fs_bsize, BT_LEVEL1 + level);
739 err_suj("indir_visit: UNRECOVERABLE I/O ERROR\n");
740 for (i = 0; i < NINDIR(fs); i++) {
741 if ((nblk = IBLK(bp, i)) == 0)
744 nlbn = -lbn + i * lbnadd;
745 (*frags) += fs->fs_frag;
746 visitor(ino, nlbn, nblk, fs->fs_frag);
748 nlbn = (lbn + 1) - (i * lbnadd);
749 indir_visit(ino, nlbn, nblk, frags, visitor, flags);
754 if (flags & VISIT_INDIR) {
755 (*frags) += fs->fs_frag;
756 visitor(ino, lbn, blk, fs->fs_frag);
761 * Visit each block in an inode as specified by 'flags' and call a
762 * callback function. The callback may inspect or free blocks. The
763 * count of frags found according to the size in the file is returned.
764 * This is not valid for sparse files but may be used to determine
765 * the correct di_blocks for a file.
768 ino_visit(union dinode *dp, ino_t ino, ino_visitor visitor, int flags)
779 size = DIP(dp, di_size);
780 mode = DIP(dp, di_mode) & IFMT;
782 if ((flags & VISIT_EXT) &&
783 fs->fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize) {
784 for (i = 0; i < UFS_NXADDR; i++) {
785 if (dp->dp2.di_extb[i] == 0)
787 frags = sblksize(fs, dp->dp2.di_extsize, i);
788 frags = numfrags(fs, frags);
790 visitor(ino, -1 - i, dp->dp2.di_extb[i], frags);
793 /* Skip datablocks for short links and devices. */
794 if (mode == IFBLK || mode == IFCHR ||
795 (mode == IFLNK && size < fs->fs_maxsymlinklen))
797 for (i = 0; i < UFS_NDADDR; i++) {
798 if (DIP(dp, di_db[i]) == 0)
800 frags = sblksize(fs, size, i);
801 frags = numfrags(fs, frags);
803 visitor(ino, i, DIP(dp, di_db[i]), frags);
806 * We know the following indirects are real as we're following
807 * real pointers to them.
810 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; i < UFS_NIADDR; i++,
812 nextlbn = lbn + tmpval;
813 tmpval *= NINDIR(fs);
814 if (DIP(dp, di_ib[i]) == 0)
816 indir_visit(ino, -lbn - i, DIP(dp, di_ib[i]), &fragcnt, visitor,
823 * Null visitor function used when we just want to count blocks and
828 null_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
835 * Recalculate di_blocks when we discover that a block allocation or
836 * free was not successfully completed. The kernel does not roll this back
837 * because it would be too expensive to compute which indirects were
838 * reachable at the time the inode was written.
841 ino_adjblks(struct suj_ino *sino)
854 /* No need to adjust zero'd inodes. */
855 if (DIP(dp, di_mode) == 0) {
860 * Visit all blocks and count them as well as recording the last
861 * valid lbn in the file. If the file size doesn't agree with the
862 * last lbn we need to truncate to fix it. Otherwise just adjust
866 frags = ino_visit(dp, ino, null_visit, VISIT_INDIR | VISIT_EXT);
867 blocks = fsbtodb(fs, frags);
869 * We assume the size and direct block list is kept coherent by
870 * softdep. For files that have extended into indirects we truncate
871 * to the size in the inode or the maximum size permitted by
872 * populated indirects.
874 if (visitlbn >= UFS_NDADDR) {
875 isize = DIP(dp, di_size);
876 size = lblktosize(fs, visitlbn + 1);
879 /* Always truncate to free any unpopulated indirects. */
880 ino_trunc(ino, isize);
884 if (blocks == DIP(dp, di_blocks)) {
889 printf("ino %ju adjusting block count from %jd to %jd\n",
890 (uintmax_t)ino, DIP(dp, di_blocks), blocks);
891 DIP_SET(dp, di_blocks, blocks);
897 blk_free_visit(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
900 blk_free(ino, blk, blk_freemask(blk, ino, lbn, frags), frags);
904 * Free a block or tree of blocks that was previously rooted in ino at
905 * the given lbn. If the lbn is an indirect all children are freed
909 blk_free_lbn(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t lbn, int frags, int follow)
914 mask = blk_freemask(blk, ino, lbn, frags);
916 if (lbn <= -UFS_NDADDR && follow && mask == 0)
917 indir_visit(ino, lbn, blk, &resid, blk_free_visit, VISIT_INDIR);
919 blk_free(ino, blk, mask, frags);
923 ino_setskip(struct suj_ino *sino, ino_t parent)
928 if (ino_isat(sino->si_ino, DOTDOT_OFFSET, parent, &mode, &isdot))
929 sino->si_skipparent = 1;
933 ino_remref(ino_t parent, ino_t child, uint64_t diroff, int isdotdot)
935 struct suj_ino *sino;
936 struct suj_rec *srec;
937 struct jrefrec *rrec;
940 * Lookup this inode to see if we have a record for it.
942 sino = ino_lookup(child, 0);
944 * Tell any child directories we've already removed their
945 * parent link cnt. Don't try to adjust our link down again.
947 if (sino != NULL && isdotdot == 0)
948 ino_setskip(sino, parent);
950 * No valid record for this inode. Just drop the on-disk
953 if (sino == NULL || sino->si_hasrecs == 0) {
958 * Use ino_adjust() if ino_check() has already processed this
959 * child. If we lose the last non-dot reference to a
960 * directory it will be discarded.
962 if (sino->si_linkadj) {
963 if (sino->si_nlink == 0)
964 err_suj("ino_remref: ino %ld mode 0%o about to go "
965 "negative\n", sino->si_ino, sino->si_mode);
973 * If we haven't yet processed this inode we need to make
974 * sure we will successfully discover the lost path. If not
975 * use nlinkadj to remember.
977 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
978 rrec = (struct jrefrec *)srec->sr_rec;
979 if (rrec->jr_parent == parent &&
980 rrec->jr_diroff == diroff)
987 * Free the children of a directory when the directory is discarded.
990 ino_free_children(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
992 struct suj_ino *sino;
1001 sino = ino_lookup(ino, 0);
1003 skipparent = sino->si_skipparent;
1006 size = lfragtosize(fs, frags);
1007 bp = getdatablk(blk, size, BT_DIRDATA);
1008 if (bp->b_errs != 0)
1009 err_suj("ino_free_children: UNRECOVERABLE I/O ERROR");
1010 dp = (struct direct *)&bp->b_un.b_buf[0];
1011 for (dpoff = 0; dpoff < size && dp->d_reclen; dpoff += dp->d_reclen) {
1012 dp = (struct direct *)&bp->b_un.b_buf[dpoff];
1013 if (dp->d_ino == 0 || dp->d_ino == UFS_WINO)
1015 if (dp->d_namlen == 1 && dp->d_name[0] == '.')
1017 isdotdot = dp->d_namlen == 2 && dp->d_name[0] == '.' &&
1018 dp->d_name[1] == '.';
1019 if (isdotdot && skipparent == 1)
1022 printf("Directory %ju removing ino %ju name %s\n",
1023 (uintmax_t)ino, (uintmax_t)dp->d_ino, dp->d_name);
1024 diroff = lblktosize(fs, lbn) + dpoff;
1025 ino_remref(ino, dp->d_ino, diroff, isdotdot);
1031 * Reclaim an inode, freeing all blocks and decrementing all children's
1032 * link counts. Free the inode back to the cg.
1035 ino_reclaim(struct inode *ip, ino_t ino, int mode)
1041 if (ino == UFS_ROOTINO)
1042 err_suj("Attempting to free UFS_ROOTINO\n");
1044 printf("Truncating and freeing ino %ju, nlink %d, mode %o\n",
1045 (uintmax_t)ino, DIP(dp, di_nlink), DIP(dp, di_mode));
1047 /* We are freeing an inode or directory. */
1048 if ((DIP(dp, di_mode) & IFMT) == IFDIR)
1049 ino_visit(dp, ino, ino_free_children, 0);
1050 DIP_SET(dp, di_nlink, 0);
1051 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0)
1053 ino_visit(dp, ino, blk_free_visit, VISIT_EXT | VISIT_INDIR);
1054 /* Here we have to clear the inode and release any blocks it holds. */
1055 gen = DIP(dp, di_gen);
1056 if (fs->fs_magic == FS_UFS1_MAGIC)
1057 bzero(dp, sizeof(struct ufs1_dinode));
1059 bzero(dp, sizeof(struct ufs2_dinode));
1060 DIP_SET(dp, di_gen, gen);
1062 ino_free(ino, mode);
1067 * Adjust an inode's link count down by one when a directory goes away.
1080 nlink = DIP(dp, di_nlink);
1081 mode = DIP(dp, di_mode);
1083 err_suj("Inode %d link count %d invalid\n", ino, nlink);
1085 err_suj("Inode %d has a link of %d with 0 mode\n", ino, nlink);
1087 if ((mode & IFMT) == IFDIR)
1091 if (nlink < reqlink) {
1093 printf("ino %ju not enough links to live %d < %d\n",
1094 (uintmax_t)ino, nlink, reqlink);
1095 ino_reclaim(&ip, ino, mode);
1099 DIP_SET(dp, di_nlink, nlink);
1105 * Adjust the inode link count to 'nlink'. If the count reaches zero
1109 ino_adjust(struct suj_ino *sino)
1111 struct jrefrec *rrec;
1112 struct suj_rec *srec;
1113 struct suj_ino *stmp;
1123 nlink = sino->si_nlink;
1125 mode = sino->si_mode & IFMT;
1127 * If it's a directory with no dot links, it was truncated before
1128 * the name was cleared. We need to clear the dirent that
1131 if (mode == IFDIR && nlink == 1 && sino->si_dotlinks == 0) {
1132 sino->si_nlink = nlink = 0;
1133 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1134 rrec = (struct jrefrec *)srec->sr_rec;
1135 if (ino_isat(rrec->jr_parent, rrec->jr_diroff, ino,
1136 &recmode, &isdot) == 0)
1138 ino_clrat(rrec->jr_parent, rrec->jr_diroff, ino);
1142 errx(1, "Directory %ju name not found", (uintmax_t)ino);
1145 * If it's a directory with no real names pointing to it go ahead
1146 * and truncate it. This will free any children.
1148 if (mode == IFDIR && nlink - sino->si_dotlinks == 0) {
1149 sino->si_nlink = nlink = 0;
1151 * Mark any .. links so they know not to free this inode
1152 * when they are removed.
1154 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1155 rrec = (struct jrefrec *)srec->sr_rec;
1156 if (rrec->jr_diroff == DOTDOT_OFFSET) {
1157 stmp = ino_lookup(rrec->jr_parent, 0);
1159 ino_setskip(stmp, ino);
1165 mode = DIP(dp, di_mode) & IFMT;
1166 if (nlink > UFS_LINK_MAX)
1167 err_suj("ino %ju nlink manipulation error, new %ju, old %d\n",
1168 (uintmax_t)ino, (uintmax_t)nlink, DIP(dp, di_nlink));
1170 printf("Adjusting ino %ju, nlink %ju, old link %d lastmode %o\n",
1171 (uintmax_t)ino, (uintmax_t)nlink, DIP(dp, di_nlink),
1175 printf("ino %ju, zero inode freeing bitmap\n",
1177 ino_free(ino, sino->si_mode);
1181 /* XXX Should be an assert? */
1182 if (mode != sino->si_mode && debug)
1183 printf("ino %ju, mode %o != %o\n",
1184 (uintmax_t)ino, mode, sino->si_mode);
1185 if ((mode & IFMT) == IFDIR)
1189 /* If the inode doesn't have enough links to live, free it. */
1190 if (nlink < reqlink) {
1192 printf("ino %ju not enough links to live %ju < %ju\n",
1193 (uintmax_t)ino, (uintmax_t)nlink,
1194 (uintmax_t)reqlink);
1195 ino_reclaim(&ip, ino, mode);
1199 /* If required write the updated link count. */
1200 if (DIP(dp, di_nlink) == nlink) {
1202 printf("ino %ju, link matches, skipping.\n",
1207 DIP_SET(dp, di_nlink, nlink);
1213 * Truncate some or all blocks in an indirect, freeing any that are required
1214 * and zeroing the indirect.
1217 indir_trunc(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, ufs_lbn_t lastlbn,
1232 level = lbn_level(lbn);
1234 err_suj("Invalid level for lbn %jd\n", lbn);
1236 for (i = level; i > 0; i--)
1237 lbnadd *= NINDIR(fs);
1238 bp = getdatablk(blk, fs->fs_bsize, BT_LEVEL1 + level);
1239 if (bp->b_errs != 0)
1240 err_suj("indir_trunc: UNRECOVERABLE I/O ERROR");
1241 for (i = 0; i < NINDIR(fs); i++) {
1242 if ((nblk = IBLK(bp, i)) == 0)
1245 nlbn = (lbn + 1) - (i * lbnadd);
1247 * Calculate the lbn of the next indirect to
1248 * determine if any of this indirect must be
1251 next = -(lbn + level) + ((i+1) * lbnadd);
1252 if (next <= lastlbn)
1254 indir_trunc(ino, nlbn, nblk, lastlbn, dp);
1255 /* If all of this indirect was reclaimed, free it. */
1256 nlbn = next - lbnadd;
1260 nlbn = -lbn + i * lbnadd;
1265 blk_free(ino, nblk, 0, fs->fs_frag);
1274 * Truncate an inode to the minimum of the given size or the last populated
1275 * block after any over size have been discarded. The kernel would allocate
1276 * the last block in the file but fsck does not and neither do we. This
1277 * code never extends files, only shrinks them.
1280 ino_trunc(ino_t ino, off_t size)
1286 uint64_t totalfrags;
1299 mode = DIP(dp, di_mode) & IFMT;
1300 cursize = DIP(dp, di_size);
1301 /* If no size change, nothing to do */
1302 if (size == cursize) {
1307 printf("Truncating ino %ju, mode %o to size %jd from size %jd\n",
1308 (uintmax_t)ino, mode, size, cursize);
1310 /* Skip datablocks for short links and devices. */
1311 if (mode == 0 || mode == IFBLK || mode == IFCHR ||
1312 (mode == IFLNK && cursize < fs->fs_maxsymlinklen)) {
1317 if (size > cursize) {
1321 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) {
1323 err_suj("Partial truncation of ino %ju snapshot file\n",
1327 lastlbn = lblkno(fs, blkroundup(fs, size));
1328 for (i = lastlbn; i < UFS_NDADDR; i++) {
1329 if ((bn = DIP(dp, di_db[i])) == 0)
1331 blksize = sblksize(fs, cursize, i);
1332 blk_free(ino, bn, 0, numfrags(fs, blksize));
1333 DIP_SET(dp, di_db[i], 0);
1336 * Follow indirect blocks, freeing anything required.
1338 for (i = 0, tmpval = NINDIR(fs), lbn = UFS_NDADDR; i < UFS_NIADDR; i++,
1340 nextlbn = lbn + tmpval;
1341 tmpval *= NINDIR(fs);
1342 /* If we're not freeing any in this indirect range skip it. */
1343 if (lastlbn >= nextlbn)
1345 if ((bn = DIP(dp, di_ib[i])) == 0)
1347 indir_trunc(ino, -lbn - i, bn, lastlbn, dp);
1348 /* If we freed everything in this indirect free the indir. */
1351 blk_free(ino, bn, 0, fs->fs_frag);
1352 DIP_SET(dp, di_ib[i], 0);
1355 * Now that we've freed any whole blocks that exceed the desired
1356 * truncation size, figure out how many blocks remain and what the
1357 * last populated lbn is. We will set the size to this last lbn
1358 * rather than worrying about allocating the final lbn as the kernel
1359 * would've done. This is consistent with normal fsck behavior.
1362 totalfrags = ino_visit(dp, ino, null_visit, VISIT_INDIR | VISIT_EXT);
1363 if (size > lblktosize(fs, visitlbn + 1))
1364 size = lblktosize(fs, visitlbn + 1);
1366 * If we're truncating direct blocks we have to adjust frags
1369 if (visitlbn < UFS_NDADDR && totalfrags) {
1370 long oldspace, newspace;
1372 bn = DIP(dp, di_db[visitlbn]);
1374 err_suj("Bad blk at ino %ju lbn %jd\n",
1375 (uintmax_t)ino, visitlbn);
1376 oldspace = sblksize(fs, cursize, visitlbn);
1377 newspace = sblksize(fs, size, visitlbn);
1378 if (oldspace != newspace) {
1379 bn += numfrags(fs, newspace);
1380 frags = numfrags(fs, oldspace - newspace);
1381 blk_free(ino, bn, 0, frags);
1382 totalfrags -= frags;
1385 DIP_SET(dp, di_blocks, fsbtodb(fs, totalfrags));
1386 DIP_SET(dp, di_size, size);
1389 * If we've truncated into the middle of a block or frag we have
1390 * to zero it here. Otherwise the file could extend into
1391 * uninitialized space later.
1393 off = blkoff(fs, size);
1394 if (off && DIP(dp, di_mode) != IFDIR) {
1397 bn = ino_blkatoff(dp, ino, visitlbn, &frags, NULL);
1399 err_suj("Block missing from ino %ju at lbn %jd\n",
1400 (uintmax_t)ino, visitlbn);
1401 clrsize = frags * fs->fs_fsize;
1402 bp = getdatablk(bn, clrsize, BT_DATA);
1403 if (bp->b_errs != 0)
1404 err_suj("ino_trunc: UNRECOVERABLE I/O ERROR");
1406 bzero(&bp->b_un.b_buf[off], clrsize);
1415 * Process records available for one inode and determine whether the
1416 * link count is correct or needs adjusting.
1419 ino_check(struct suj_ino *sino)
1421 struct suj_rec *srec;
1422 struct jrefrec *rrec;
1432 if (sino->si_hasrecs == 0)
1435 rrec = (struct jrefrec *)TAILQ_FIRST(&sino->si_recs)->sr_rec;
1436 nlink = rrec->jr_nlink;
1439 removes = sino->si_nlinkadj;
1440 TAILQ_FOREACH(srec, &sino->si_recs, sr_next) {
1441 rrec = (struct jrefrec *)srec->sr_rec;
1442 isat = ino_isat(rrec->jr_parent, rrec->jr_diroff,
1443 rrec->jr_ino, &mode, &isdot);
1444 if (isat && (mode & IFMT) != (rrec->jr_mode & IFMT))
1445 err_suj("Inode mode/directory type mismatch %o != %o\n",
1446 mode, rrec->jr_mode);
1448 printf("jrefrec: op %d ino %ju, nlink %ju, parent %ju, "
1449 "diroff %jd, mode %o, isat %d, isdot %d\n",
1450 rrec->jr_op, (uintmax_t)rrec->jr_ino,
1451 (uintmax_t)rrec->jr_nlink,
1452 (uintmax_t)rrec->jr_parent,
1453 (uintmax_t)rrec->jr_diroff,
1454 rrec->jr_mode, isat, isdot);
1455 mode = rrec->jr_mode & IFMT;
1456 if (rrec->jr_op == JOP_REMREF)
1463 * The number of links that remain are the starting link count
1464 * subtracted by the total number of removes with the total
1465 * links discovered back in. An incomplete remove thus
1466 * makes no change to the link count but an add increases
1471 "ino %ju nlink %ju newlinks %ju removes %ju dotlinks %ju\n",
1472 (uintmax_t)ino, (uintmax_t)nlink, (uintmax_t)newlinks,
1473 (uintmax_t)removes, (uintmax_t)dotlinks);
1476 sino->si_linkadj = 1;
1477 sino->si_nlink = nlink;
1478 sino->si_dotlinks = dotlinks;
1479 sino->si_mode = mode;
1484 * Process records available for one block and determine whether it is
1485 * still allocated and whether the owning inode needs to be updated or
1489 blk_check(struct suj_blk *sblk)
1491 struct suj_rec *srec;
1492 struct jblkrec *brec;
1493 struct suj_ino *sino;
1500 * Each suj_blk actually contains records for any fragments in that
1501 * block. As a result we must evaluate each record individually.
1504 TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
1505 brec = (struct jblkrec *)srec->sr_rec;
1506 frags = brec->jb_frags;
1507 blk = brec->jb_blkno + brec->jb_oldfrags;
1508 isat = blk_isat(brec->jb_ino, brec->jb_lbn, blk, &frags);
1509 if (sino == NULL || sino->si_ino != brec->jb_ino) {
1510 sino = ino_lookup(brec->jb_ino, 1);
1511 sino->si_blkadj = 1;
1514 printf("op %d blk %jd ino %ju lbn %jd frags %d isat %d (%d)\n",
1515 brec->jb_op, blk, (uintmax_t)brec->jb_ino,
1516 brec->jb_lbn, brec->jb_frags, isat, frags);
1518 * If we found the block at this address we still have to
1519 * determine if we need to free the tail end that was
1520 * added by adding contiguous fragments from the same block.
1523 if (frags == brec->jb_frags)
1525 mask = blk_freemask(blk, brec->jb_ino, brec->jb_lbn,
1529 frags = brec->jb_frags - frags;
1530 blk_free(brec->jb_ino, blk, mask, frags);
1534 * The block wasn't found, attempt to free it. It won't be
1535 * freed if it was actually reallocated. If this was an
1536 * allocation we don't want to follow indirects as they
1537 * may not be written yet. Any children of the indirect will
1538 * have their own records. If it's a free we need to
1539 * recursively free children.
1541 blk_free_lbn(blk, brec->jb_ino, brec->jb_lbn, brec->jb_frags,
1542 brec->jb_op == JOP_FREEBLK);
1547 * Walk the list of inode records for this cg and resolve moved and duplicate
1548 * inode references now that we have a complete picture.
1551 cg_build(struct suj_cg *sc)
1553 struct suj_ino *sino;
1556 for (i = 0; i < HASHSIZE; i++)
1557 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1562 * Handle inodes requiring truncation. This must be done prior to
1563 * looking up any inodes in directories.
1566 cg_trunc(struct suj_cg *sc)
1568 struct suj_ino *sino;
1571 for (i = 0; i < HASHSIZE; i++) {
1572 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) {
1573 if (sino->si_trunc) {
1574 ino_trunc(sino->si_ino,
1575 sino->si_trunc->jt_size);
1576 sino->si_blkadj = 0;
1577 sino->si_trunc = NULL;
1579 if (sino->si_blkadj)
1586 cg_adj_blk(struct suj_cg *sc)
1588 struct suj_ino *sino;
1591 for (i = 0; i < HASHSIZE; i++) {
1592 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next) {
1593 if (sino->si_blkadj)
1600 * Free any partially allocated blocks and then resolve inode block
1604 cg_check_blk(struct suj_cg *sc)
1606 struct suj_blk *sblk;
1610 for (i = 0; i < HASHSIZE; i++)
1611 LIST_FOREACH(sblk, &sc->sc_blkhash[i], sb_next)
1616 * Walk the list of inode records for this cg, recovering any
1617 * changes which were not complete at the time of crash.
1620 cg_check_ino(struct suj_cg *sc)
1622 struct suj_ino *sino;
1625 for (i = 0; i < HASHSIZE; i++)
1626 LIST_FOREACH(sino, &sc->sc_inohash[i], si_next)
1631 cg_apply(void (*apply)(struct suj_cg *))
1636 for (i = 0; i < HASHSIZE; i++)
1637 LIST_FOREACH(scg, &cghash[i], sc_next)
1642 * Process the unlinked but referenced file list. Freeing all inodes.
1653 ino = fs->fs_sujfree;
1658 mode = DIP(dp, di_mode) & IFMT;
1659 inon = DIP(dp, di_freelink);
1660 DIP_SET(dp, di_freelink, 0);
1663 * XXX Should this be an errx?
1665 if (DIP(dp, di_nlink) == 0) {
1667 printf("Freeing unlinked ino %ju mode %o\n",
1668 (uintmax_t)ino, mode);
1669 ino_reclaim(&ip, ino, mode);
1671 printf("Skipping ino %ju mode %o with link %d\n",
1672 (uintmax_t)ino, mode, DIP(dp, di_nlink));
1679 * Append a new record to the list of records requiring processing.
1682 ino_append(union jrec *rec)
1684 struct jrefrec *refrec;
1685 struct jmvrec *mvrec;
1686 struct suj_ino *sino;
1687 struct suj_rec *srec;
1689 mvrec = &rec->rec_jmvrec;
1690 refrec = &rec->rec_jrefrec;
1691 if (debug && mvrec->jm_op == JOP_MVREF)
1692 printf("ino move: ino %ju, parent %ju, "
1693 "diroff %jd, oldoff %jd\n",
1694 (uintmax_t)mvrec->jm_ino, (uintmax_t)mvrec->jm_parent,
1695 (uintmax_t)mvrec->jm_newoff, (uintmax_t)mvrec->jm_oldoff);
1697 (refrec->jr_op == JOP_ADDREF || refrec->jr_op == JOP_REMREF))
1698 printf("ino ref: op %d, ino %ju, nlink %ju, "
1699 "parent %ju, diroff %jd\n",
1700 refrec->jr_op, (uintmax_t)refrec->jr_ino,
1701 (uintmax_t)refrec->jr_nlink,
1702 (uintmax_t)refrec->jr_parent, (uintmax_t)refrec->jr_diroff);
1703 sino = ino_lookup(((struct jrefrec *)rec)->jr_ino, 1);
1704 sino->si_hasrecs = 1;
1705 srec = errmalloc(sizeof(*srec));
1707 TAILQ_INSERT_TAIL(&sino->si_newrecs, srec, sr_next);
1711 * Add a reference adjustment to the sino list and eliminate dups. The
1712 * primary loop in ino_build_ref() checks for dups but new ones may be
1713 * created as a result of offset adjustments.
1716 ino_add_ref(struct suj_ino *sino, struct suj_rec *srec)
1718 struct jrefrec *refrec;
1719 struct suj_rec *srn;
1720 struct jrefrec *rrn;
1722 refrec = (struct jrefrec *)srec->sr_rec;
1724 * We walk backwards so that the oldest link count is preserved. If
1725 * an add record conflicts with a remove keep the remove. Redundant
1726 * removes are eliminated in ino_build_ref. Otherwise we keep the
1727 * oldest record at a given location.
1729 for (srn = TAILQ_LAST(&sino->si_recs, srechd); srn;
1730 srn = TAILQ_PREV(srn, srechd, sr_next)) {
1731 rrn = (struct jrefrec *)srn->sr_rec;
1732 if (rrn->jr_parent != refrec->jr_parent ||
1733 rrn->jr_diroff != refrec->jr_diroff)
1735 if (rrn->jr_op == JOP_REMREF || refrec->jr_op == JOP_ADDREF) {
1736 rrn->jr_mode = refrec->jr_mode;
1742 * Replace the record in place with the old nlink in case
1743 * we replace the head of the list. Abandon srec as a dup.
1745 refrec->jr_nlink = rrn->jr_nlink;
1746 srn->sr_rec = srec->sr_rec;
1749 TAILQ_INSERT_TAIL(&sino->si_recs, srec, sr_next);
1753 * Create a duplicate of a reference at a previous location.
1756 ino_dup_ref(struct suj_ino *sino, struct jrefrec *refrec, off_t diroff)
1758 struct jrefrec *rrn;
1759 struct suj_rec *srn;
1761 rrn = errmalloc(sizeof(*refrec));
1763 rrn->jr_op = JOP_ADDREF;
1764 rrn->jr_diroff = diroff;
1765 srn = errmalloc(sizeof(*srn));
1766 srn->sr_rec = (union jrec *)rrn;
1767 ino_add_ref(sino, srn);
1771 * Add a reference to the list at all known locations. We follow the offset
1772 * changes for a single instance and create duplicate add refs at each so
1773 * that we can tolerate any version of the directory block. Eliminate
1774 * removes which collide with adds that are seen in the journal. They should
1775 * not adjust the link count down.
1778 ino_build_ref(struct suj_ino *sino, struct suj_rec *srec)
1780 struct jrefrec *refrec;
1781 struct jmvrec *mvrec;
1782 struct suj_rec *srp;
1783 struct suj_rec *srn;
1784 struct jrefrec *rrn;
1787 refrec = (struct jrefrec *)srec->sr_rec;
1789 * Search for a mvrec that matches this offset. Whether it's an add
1790 * or a remove we can delete the mvref after creating a dup record in
1793 if (!TAILQ_EMPTY(&sino->si_movs)) {
1794 diroff = refrec->jr_diroff;
1795 for (srn = TAILQ_LAST(&sino->si_movs, srechd); srn; srn = srp) {
1796 srp = TAILQ_PREV(srn, srechd, sr_next);
1797 mvrec = (struct jmvrec *)srn->sr_rec;
1798 if (mvrec->jm_parent != refrec->jr_parent ||
1799 mvrec->jm_newoff != diroff)
1801 diroff = mvrec->jm_oldoff;
1802 TAILQ_REMOVE(&sino->si_movs, srn, sr_next);
1804 ino_dup_ref(sino, refrec, diroff);
1808 * If a remove wasn't eliminated by an earlier add just append it to
1811 if (refrec->jr_op == JOP_REMREF) {
1812 ino_add_ref(sino, srec);
1816 * Walk the list of records waiting to be added to the list. We
1817 * must check for moves that apply to our current offset and remove
1818 * them from the list. Remove any duplicates to eliminate removes
1819 * with corresponding adds.
1821 TAILQ_FOREACH_SAFE(srn, &sino->si_newrecs, sr_next, srp) {
1822 switch (srn->sr_rec->rec_jrefrec.jr_op) {
1825 * This should actually be an error we should
1826 * have a remove for every add journaled.
1828 rrn = (struct jrefrec *)srn->sr_rec;
1829 if (rrn->jr_parent != refrec->jr_parent ||
1830 rrn->jr_diroff != refrec->jr_diroff)
1832 TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
1836 * Once we remove the current iteration of the
1837 * record at this address we're done.
1839 rrn = (struct jrefrec *)srn->sr_rec;
1840 if (rrn->jr_parent != refrec->jr_parent ||
1841 rrn->jr_diroff != refrec->jr_diroff)
1843 TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
1844 ino_add_ref(sino, srec);
1848 * Update our diroff based on any moves that match
1849 * and remove the move.
1851 mvrec = (struct jmvrec *)srn->sr_rec;
1852 if (mvrec->jm_parent != refrec->jr_parent ||
1853 mvrec->jm_oldoff != refrec->jr_diroff)
1855 ino_dup_ref(sino, refrec, mvrec->jm_oldoff);
1856 refrec->jr_diroff = mvrec->jm_newoff;
1857 TAILQ_REMOVE(&sino->si_newrecs, srn, sr_next);
1860 err_suj("ino_build_ref: Unknown op %d\n",
1861 srn->sr_rec->rec_jrefrec.jr_op);
1864 ino_add_ref(sino, srec);
1868 * Walk the list of new records and add them in-order resolving any
1869 * dups and adjusted offsets.
1872 ino_build(struct suj_ino *sino)
1874 struct suj_rec *srec;
1876 while ((srec = TAILQ_FIRST(&sino->si_newrecs)) != NULL) {
1877 TAILQ_REMOVE(&sino->si_newrecs, srec, sr_next);
1878 switch (srec->sr_rec->rec_jrefrec.jr_op) {
1881 ino_build_ref(sino, srec);
1885 * Add this mvrec to the queue of pending mvs.
1887 TAILQ_INSERT_TAIL(&sino->si_movs, srec, sr_next);
1890 err_suj("ino_build: Unknown op %d\n",
1891 srec->sr_rec->rec_jrefrec.jr_op);
1894 if (TAILQ_EMPTY(&sino->si_recs))
1895 sino->si_hasrecs = 0;
1899 * Modify journal records so they refer to the base block number
1900 * and a start and end frag range. This is to facilitate the discovery
1901 * of overlapping fragment allocations.
1904 blk_build(struct jblkrec *blkrec)
1906 struct suj_rec *srec;
1907 struct suj_blk *sblk;
1908 struct jblkrec *blkrn;
1913 printf("blk_build: op %d blkno %jd frags %d oldfrags %d "
1914 "ino %ju lbn %jd\n",
1915 blkrec->jb_op, (uintmax_t)blkrec->jb_blkno,
1916 blkrec->jb_frags, blkrec->jb_oldfrags,
1917 (uintmax_t)blkrec->jb_ino, (uintmax_t)blkrec->jb_lbn);
1919 blk = blknum(fs, blkrec->jb_blkno);
1920 frag = fragnum(fs, blkrec->jb_blkno);
1921 if (blkrec->jb_blkno < 0 || blk + fs->fs_frag - frag > fs->fs_size)
1922 err_suj("Out-of-bounds journal block number %jd\n",
1924 sblk = blk_lookup(blk, 1);
1926 * Rewrite the record using oldfrags to indicate the offset into
1927 * the block. Leave jb_frags as the actual allocated count.
1929 blkrec->jb_blkno -= frag;
1930 blkrec->jb_oldfrags = frag;
1931 if (blkrec->jb_oldfrags + blkrec->jb_frags > fs->fs_frag)
1932 err_suj("Invalid fragment count %d oldfrags %d\n",
1933 blkrec->jb_frags, frag);
1935 * Detect dups. If we detect a dup we always discard the oldest
1936 * record as it is superseded by the new record. This speeds up
1937 * later stages but also eliminates free records which are used
1938 * to indicate that the contents of indirects can be trusted.
1940 TAILQ_FOREACH(srec, &sblk->sb_recs, sr_next) {
1941 blkrn = (struct jblkrec *)srec->sr_rec;
1942 if (blkrn->jb_ino != blkrec->jb_ino ||
1943 blkrn->jb_lbn != blkrec->jb_lbn ||
1944 blkrn->jb_blkno != blkrec->jb_blkno ||
1945 blkrn->jb_frags != blkrec->jb_frags ||
1946 blkrn->jb_oldfrags != blkrec->jb_oldfrags)
1949 printf("Removed dup.\n");
1950 /* Discard the free which is a dup with an alloc. */
1951 if (blkrec->jb_op == JOP_FREEBLK)
1953 TAILQ_REMOVE(&sblk->sb_recs, srec, sr_next);
1957 srec = errmalloc(sizeof(*srec));
1958 srec->sr_rec = (union jrec *)blkrec;
1959 TAILQ_INSERT_TAIL(&sblk->sb_recs, srec, sr_next);
1963 ino_build_trunc(struct jtrncrec *rec)
1965 struct suj_ino *sino;
1968 printf("ino_build_trunc: op %d ino %ju, size %jd\n",
1969 rec->jt_op, (uintmax_t)rec->jt_ino,
1970 (uintmax_t)rec->jt_size);
1971 if (chkfilesize(IFREG, rec->jt_size) == 0)
1972 err_suj("ino_build: truncation size too large %ju\n",
1973 (intmax_t)rec->jt_size);
1974 sino = ino_lookup(rec->jt_ino, 1);
1975 if (rec->jt_op == JOP_SYNC) {
1976 sino->si_trunc = NULL;
1979 if (sino->si_trunc == NULL || sino->si_trunc->jt_size > rec->jt_size)
1980 sino->si_trunc = rec;
1984 * Build up tables of the operations we need to recover.
1989 struct suj_seg *seg;
1994 TAILQ_FOREACH(seg, &allsegs, ss_next) {
1996 printf("seg %jd has %d records, oldseq %jd.\n",
1997 seg->ss_rec.jsr_seq, seg->ss_rec.jsr_cnt,
1998 seg->ss_rec.jsr_oldest);
2000 rec = (union jrec *)seg->ss_blk;
2001 for (i = 0; i < seg->ss_rec.jsr_cnt; off += JREC_SIZE, rec++) {
2002 /* skip the segrec. */
2003 if ((off % real_dev_bsize) == 0)
2005 switch (rec->rec_jrefrec.jr_op) {
2013 blk_build((struct jblkrec *)rec);
2017 ino_build_trunc((struct jtrncrec *)rec);
2020 err_suj("Unknown journal operation %d (%d)\n",
2021 rec->rec_jrefrec.jr_op, off);
2029 * Prune the journal segments to those we care about based on the
2030 * oldest sequence in the newest segment. Order the segment list
2031 * based on sequence number.
2036 struct suj_seg *seg;
2037 struct suj_seg *segn;
2042 printf("Pruning up to %jd\n", oldseq);
2043 /* First free the expired segments. */
2044 TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2045 if (seg->ss_rec.jsr_seq >= oldseq)
2047 TAILQ_REMOVE(&allsegs, seg, ss_next);
2051 /* Next ensure that segments are ordered properly. */
2052 seg = TAILQ_FIRST(&allsegs);
2055 printf("Empty journal\n");
2058 newseq = seg->ss_rec.jsr_seq;
2060 seg = TAILQ_LAST(&allsegs, seghd);
2061 if (seg->ss_rec.jsr_seq >= newseq)
2063 TAILQ_REMOVE(&allsegs, seg, ss_next);
2064 TAILQ_INSERT_HEAD(&allsegs, seg, ss_next);
2065 newseq = seg->ss_rec.jsr_seq;
2068 if (newseq != oldseq) {
2069 TAILQ_FOREACH(seg, &allsegs, ss_next) {
2070 printf("%jd, ", seg->ss_rec.jsr_seq);
2073 err_suj("Journal file sequence mismatch %jd != %jd\n",
2077 * The kernel may asynchronously write segments which can create
2078 * gaps in the sequence space. Throw away any segments after the
2079 * gap as the kernel guarantees only those that are contiguously
2080 * reachable are marked as completed.
2083 TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2084 if (!discard && newseq++ == seg->ss_rec.jsr_seq) {
2085 jrecs += seg->ss_rec.jsr_cnt;
2086 jbytes += seg->ss_rec.jsr_blocks * real_dev_bsize;
2091 printf("Journal order mismatch %jd != %jd pruning\n",
2092 newseq-1, seg->ss_rec.jsr_seq);
2093 TAILQ_REMOVE(&allsegs, seg, ss_next);
2098 printf("Processing journal segments from %jd to %jd\n",
2103 * Verify the journal inode before attempting to read records.
2106 suj_verifyino(union dinode *dp)
2109 if (DIP(dp, di_nlink) != 1) {
2110 printf("Invalid link count %d for journal inode %ju\n",
2111 DIP(dp, di_nlink), (uintmax_t)sujino);
2115 if ((DIP(dp, di_flags) & (SF_IMMUTABLE | SF_NOUNLINK)) !=
2116 (SF_IMMUTABLE | SF_NOUNLINK)) {
2117 printf("Invalid flags 0x%X for journal inode %ju\n",
2118 DIP(dp, di_flags), (uintmax_t)sujino);
2122 if (DIP(dp, di_mode) != (IFREG | IREAD)) {
2123 printf("Invalid mode %o for journal inode %ju\n",
2124 DIP(dp, di_mode), (uintmax_t)sujino);
2128 if (DIP(dp, di_size) < SUJ_MIN) {
2129 printf("Invalid size %jd for journal inode %ju\n",
2130 DIP(dp, di_size), (uintmax_t)sujino);
2134 if (DIP(dp, di_modrev) != fs->fs_mtime) {
2135 printf("Journal timestamp does not match fs mount time\n");
2143 struct jextent *jb_extent; /* Extent array. */
2144 int jb_avail; /* Available extents. */
2145 int jb_used; /* Last used extent. */
2146 int jb_head; /* Allocator head. */
2147 int jb_off; /* Allocator extent offset. */
2150 ufs2_daddr_t je_daddr; /* Disk block address. */
2151 int je_blocks; /* Disk block count. */
2154 static struct jblocks *suj_jblocks;
2156 static struct jblocks *
2157 jblocks_create(void)
2159 struct jblocks *jblocks;
2162 jblocks = errmalloc(sizeof(*jblocks));
2163 jblocks->jb_avail = 10;
2164 jblocks->jb_used = 0;
2165 jblocks->jb_head = 0;
2166 jblocks->jb_off = 0;
2167 size = sizeof(struct jextent) * jblocks->jb_avail;
2168 jblocks->jb_extent = errmalloc(size);
2169 bzero(jblocks->jb_extent, size);
2175 * Return the next available disk block and the amount of contiguous
2176 * free space it contains.
2179 jblocks_next(struct jblocks *jblocks, int bytes, int *actual)
2181 struct jextent *jext;
2186 blocks = btodb(bytes);
2187 jext = &jblocks->jb_extent[jblocks->jb_head];
2188 freecnt = jext->je_blocks - jblocks->jb_off;
2190 jblocks->jb_off = 0;
2191 if (++jblocks->jb_head > jblocks->jb_used)
2193 jext = &jblocks->jb_extent[jblocks->jb_head];
2194 freecnt = jext->je_blocks;
2196 if (freecnt > blocks)
2198 *actual = dbtob(freecnt);
2199 daddr = jext->je_daddr + jblocks->jb_off;
2205 * Advance the allocation head by a specified number of bytes, consuming
2206 * one journal segment.
2209 jblocks_advance(struct jblocks *jblocks, int bytes)
2212 jblocks->jb_off += btodb(bytes);
2216 jblocks_destroy(struct jblocks *jblocks)
2219 free(jblocks->jb_extent);
2224 jblocks_add(struct jblocks *jblocks, ufs2_daddr_t daddr, int blocks)
2226 struct jextent *jext;
2229 jext = &jblocks->jb_extent[jblocks->jb_used];
2230 /* Adding the first block. */
2231 if (jext->je_daddr == 0) {
2232 jext->je_daddr = daddr;
2233 jext->je_blocks = blocks;
2236 /* Extending the last extent. */
2237 if (jext->je_daddr + jext->je_blocks == daddr) {
2238 jext->je_blocks += blocks;
2241 /* Adding a new extent. */
2242 if (++jblocks->jb_used == jblocks->jb_avail) {
2243 jblocks->jb_avail *= 2;
2244 size = sizeof(struct jextent) * jblocks->jb_avail;
2245 jext = errmalloc(size);
2247 bcopy(jblocks->jb_extent, jext,
2248 sizeof(struct jextent) * jblocks->jb_used);
2249 free(jblocks->jb_extent);
2250 jblocks->jb_extent = jext;
2252 jext = &jblocks->jb_extent[jblocks->jb_used];
2253 jext->je_daddr = daddr;
2254 jext->je_blocks = blocks;
2260 * Add a file block from the journal to the extent map. We can't read
2261 * each file block individually because the kernel treats it as a circular
2262 * buffer and segments may span mutliple contiguous blocks.
2265 suj_add_block(ino_t ino, ufs_lbn_t lbn, ufs2_daddr_t blk, int frags)
2268 jblocks_add(suj_jblocks, fsbtodb(fs, blk), fsbtodb(fs, frags));
2274 uint8_t block[1 * 1024 * 1024];
2275 struct suj_seg *seg;
2276 struct jsegrec *recn;
2277 struct jsegrec *rec;
2286 * Read records until we exhaust the journal space. If we find
2287 * an invalid record we start searching for a valid segment header
2288 * at the next block. This is because we don't have a head/tail
2289 * pointer and must recover the information indirectly. At the gap
2290 * between the head and tail we won't necessarily have a valid
2295 size = sizeof(block);
2296 blk = jblocks_next(suj_jblocks, size, &readsize);
2301 * Read 1MB at a time and scan for records within this block.
2303 if (pread(fsreadfd, &block, size, dbtob(blk)) != size) {
2304 err_suj("Error reading journal block %jd\n",
2307 for (rec = (void *)block; size; size -= recsize,
2308 rec = (struct jsegrec *)((uintptr_t)rec + recsize)) {
2309 recsize = real_dev_bsize;
2310 if (rec->jsr_time != fs->fs_mtime) {
2313 printf("Rec time %jd != fs mtime %jd\n",
2314 rec->jsr_time, fs->fs_mtime);
2316 jblocks_advance(suj_jblocks, recsize);
2319 if (rec->jsr_cnt == 0) {
2321 printf("Found illegal count %d\n",
2323 jblocks_advance(suj_jblocks, recsize);
2326 blocks = rec->jsr_blocks;
2327 recsize = blocks * real_dev_bsize;
2328 if (recsize > size) {
2330 * We may just have run out of buffer, restart
2331 * the loop to re-read from this spot.
2333 if (size < fs->fs_bsize &&
2335 recsize <= fs->fs_bsize)
2338 printf("Found invalid segsize %d > %d\n",
2340 recsize = real_dev_bsize;
2341 jblocks_advance(suj_jblocks, recsize);
2345 * Verify that all blocks in the segment are present.
2347 for (i = 1; i < blocks; i++) {
2348 recn = (void *)((uintptr_t)rec) + i *
2350 if (recn->jsr_seq == rec->jsr_seq &&
2351 recn->jsr_time == rec->jsr_time)
2354 printf("Incomplete record %jd (%d)\n",
2356 recsize = i * real_dev_bsize;
2357 jblocks_advance(suj_jblocks, recsize);
2360 seg = errmalloc(sizeof(*seg));
2361 seg->ss_blk = errmalloc(recsize);
2363 bcopy((void *)rec, seg->ss_blk, recsize);
2364 if (rec->jsr_oldest > oldseq)
2365 oldseq = rec->jsr_oldest;
2366 TAILQ_INSERT_TAIL(&allsegs, seg, ss_next);
2367 jblocks_advance(suj_jblocks, recsize);
2373 * Orchestrate the verification of a filesystem via the softupdates journal.
2376 suj_check(const char *filesys)
2378 struct inodesc idesc;
2384 struct suj_seg *seg;
2385 struct suj_seg *segn;
2389 if (real_dev_bsize == 0 && ioctl(fsreadfd, DIOCGSECTORSIZE,
2390 &real_dev_bsize) == -1)
2391 real_dev_bsize = secsize;
2393 printf("dev_bsize %u\n", real_dev_bsize);
2396 * Set an exit point when SUJ check failed
2398 retval = setjmp(jmpbuf);
2400 pwarn("UNEXPECTED SU+J INCONSISTENCY\n");
2401 TAILQ_FOREACH_SAFE(seg, &allsegs, ss_next, segn) {
2402 TAILQ_REMOVE(&allsegs, seg, ss_next);
2406 if (reply("FALLBACK TO FULL FSCK") == 0) {
2414 * Search the root directory for the SUJ_FILE.
2416 idesc.id_type = DATA;
2417 idesc.id_fix = IGNORE;
2418 idesc.id_number = UFS_ROOTINO;
2419 idesc.id_func = findino;
2420 idesc.id_name = SUJ_FILE;
2421 ginode(UFS_ROOTINO, &ip);
2422 if ((ckinode(ip.i_dp, &idesc) & FOUND) == FOUND) {
2423 sujino = idesc.id_parent;
2426 printf("Journal inode removed. Use tunefs to re-create.\n");
2427 sblock.fs_flags &= ~FS_SUJ;
2428 sblock.fs_sujfree = 0;
2433 * Fetch the journal inode and verify it.
2435 ginode(sujino, &ip);
2437 printf("** SU+J Recovering %s\n", filesys);
2438 if (suj_verifyino(jip) != 0 || (!preen && !reply("USE JOURNAL"))) {
2443 * Build a list of journal blocks in jblocks before parsing the
2444 * available journal blocks in with suj_read().
2446 printf("** Reading %jd byte journal from inode %ju.\n",
2447 DIP(jip, di_size), (uintmax_t)sujino);
2448 suj_jblocks = jblocks_create();
2449 blocks = ino_visit(jip, sujino, suj_add_block, 0);
2450 if (blocks != numfrags(fs, DIP(jip, di_size))) {
2451 printf("Sparse journal inode %ju.\n", (uintmax_t)sujino);
2457 jblocks_destroy(suj_jblocks);
2459 if (preen || reply("RECOVER")) {
2460 printf("** Building recovery table.\n");
2464 printf("** Resolving unreferenced inode list.\n");
2466 printf("** Processing journal entries.\n");
2468 cg_apply(cg_check_blk);
2469 cg_apply(cg_adj_blk);
2470 cg_apply(cg_check_ino);
2472 if (preen == 0 && (jrecs > 0 || jbytes > 0) && reply("WRITE CHANGES") == 0)
2475 * Check block counts of snapshot inodes and
2476 * make copies of any needed snapshot blocks.
2478 for (i = 0; i < snapcnt; i++)
2479 check_blkcnt(&snaplist[i]);
2480 snapflush(suj_checkblkavail);
2482 * Recompute the fs summary info from correct cs summaries.
2484 bzero(&fs->fs_cstotal, sizeof(struct csum_total));
2485 for (i = 0; i < fs->fs_ncg; i++) {
2486 cgsum = &fs->fs_cs(fs, i);
2487 fs->fs_cstotal.cs_nffree += cgsum->cs_nffree;
2488 fs->fs_cstotal.cs_nbfree += cgsum->cs_nbfree;
2489 fs->fs_cstotal.cs_nifree += cgsum->cs_nifree;
2490 fs->fs_cstotal.cs_ndir += cgsum->cs_ndir;
2492 fs->fs_pendinginodes = 0;
2493 fs->fs_pendingblocks = 0;
2495 fs->fs_time = time(NULL);
2496 fs->fs_mtime = time(NULL);
2499 if (jrecs > 0 || jbytes > 0) {
2500 printf("** %jd journal records in %jd bytes for %.2f%% utilization\n",
2501 jrecs, jbytes, ((float)jrecs / (float)(jbytes / JREC_SIZE)) * 100);
2502 printf("** Freed %jd inodes (%jd dirs) %jd blocks, and %jd frags.\n",
2503 freeinos, freedir, freeblocks, freefrags);
2514 for (i = 0; i < HASHSIZE; i++)
2515 LIST_INIT(&cghash[i]);
2517 TAILQ_INIT(&allsegs);