Home | History | Annotate | Line # | Download | only in fsck_lfs
pass5.c revision 1.33
      1 /* $NetBSD: pass5.c,v 1.33 2015/08/02 18:08:12 dholland Exp $	 */
      2 
      3 /*-
      4  * Copyright (c) 2000, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/time.h>
     35 #include <sys/buf.h>
     36 #include <sys/mount.h>
     37 
     38 #define vnode uvnode
     39 #include <ufs/lfs/lfs.h>
     40 #include <ufs/lfs/lfs_accessors.h>
     41 #include <ufs/lfs/lfs_inode.h>
     42 #undef vnode
     43 
     44 #include <string.h>
     45 
     46 #include "bufcache.h"
     47 #include "vnode.h"
     48 #include "lfs_user.h"
     49 
     50 #include "fsck.h"
     51 #include "extern.h"
     52 #include "fsutil.h"
     53 
     54 extern SEGUSE *seg_table;
     55 extern off_t locked_queue_bytes;
     56 
     57 void
     58 pass5(void)
     59 {
     60 	SEGUSE *su;
     61 	struct ubuf *bp;
     62 	int i;
     63 	daddr_t bb;		/* total number of used blocks (lower bound) */
     64 	daddr_t ubb;		/* upper bound number of used blocks */
     65 	daddr_t avail;		/* blocks available for writing */
     66 	unsigned long dmeta;	/* blocks in segsums and inodes */
     67 	int nclean;		/* clean segments */
     68 	size_t labelskew;
     69 	int diddirty;
     70 
     71 	/*
     72 	 * Check segment holdings against actual holdings.  Check for
     73 	 * "clean" segments that contain live data.  If we are only
     74 	 * rolling forward, we can't check the segment holdings, but
     75 	 * we can still check the cleanerinfo data.
     76 	 */
     77 	nclean = 0;
     78 	avail = 0;
     79 	bb = ubb = 0;
     80 	dmeta = 0;
     81 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
     82 		diddirty = 0;
     83 		LFS_SEGENTRY(su, fs, i, bp);
     84 		if (!preen && !(su->su_flags & SEGUSE_DIRTY) &&
     85 		    seg_table[i].su_nbytes > 0) {
     86 			pwarn("CLEAN SEGMENT %d CONTAINS %d BYTES\n",
     87 			    i, seg_table[i].su_nbytes);
     88 			if (reply("MARK SEGMENT DIRTY")) {
     89 				su->su_flags |= SEGUSE_DIRTY;
     90 				++diddirty;
     91 			}
     92 		}
     93 		if (!preen && su->su_nbytes != seg_table[i].su_nbytes) {
     94 			pwarn("SEGMENT %d CLAIMS %d BYTES BUT HAS %d",
     95 			    i, su->su_nbytes, seg_table[i].su_nbytes);
     96 			if ((int32_t)su->su_nbytes >
     97 			    (int32_t)seg_table[i].su_nbytes)
     98 				pwarn(" (HIGH BY %d)\n", su->su_nbytes -
     99 				    seg_table[i].su_nbytes);
    100 			else
    101 				pwarn(" (LOW BY %d)\n", -su->su_nbytes +
    102 				    seg_table[i].su_nbytes);
    103 			if (reply("FIX")) {
    104 				su->su_nbytes = seg_table[i].su_nbytes;
    105 				++diddirty;
    106 			}
    107 		}
    108 		if (su->su_flags & SEGUSE_DIRTY) {
    109 			bb += lfs_btofsb(fs, su->su_nbytes +
    110 			    su->su_nsums * lfs_sb_getsumsize(fs));
    111 			ubb += lfs_btofsb(fs, su->su_nbytes +
    112 			    su->su_nsums * lfs_sb_getsumsize(fs) +
    113 			    su->su_ninos * lfs_sb_getibsize(fs));
    114 			dmeta += lfs_btofsb(fs,
    115 			    lfs_sb_getsumsize(fs) * su->su_nsums);
    116 			dmeta += lfs_btofsb(fs,
    117 			    lfs_sb_getibsize(fs) * su->su_ninos);
    118 		} else {
    119 			nclean++;
    120 			avail += lfs_segtod(fs, 1);
    121 			if (su->su_flags & SEGUSE_SUPERBLOCK)
    122 				avail -= lfs_btofsb(fs, LFS_SBPAD);
    123 			if (i == 0 && fs->lfs_version > 1 &&
    124 			    lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
    125 				avail -= lfs_btofsb(fs, LFS_LABELPAD) -
    126 				    lfs_sb_gets0addr(fs);
    127 		}
    128 		if (diddirty)
    129 			VOP_BWRITE(bp);
    130 		else
    131 			brelse(bp, 0);
    132 	}
    133 
    134 	/* Also may be available bytes in current seg */
    135 	i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
    136 	avail += lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs);
    137 	/* But do not count minfreesegs */
    138 	avail -= lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) -
    139 		(lfs_sb_getminfreeseg(fs) / 2)));
    140 	/* Note we may have bytes to write yet */
    141 	avail -= lfs_btofsb(fs, locked_queue_bytes);
    142 
    143 	if (idaddr)
    144 		pwarn("NOTE: when using -i, expect discrepancies in dmeta,"
    145 		      " avail, nclean, bfree\n");
    146 	if (dmeta != lfs_sb_getdmeta(fs)) {
    147 		pwarn("DMETA GIVEN AS %d, SHOULD BE %ld\n",
    148 		    lfs_sb_getdmeta(fs), dmeta);
    149 		if (preen || reply("FIX")) {
    150 			lfs_sb_setdmeta(fs, dmeta);
    151 			sbdirty();
    152 		}
    153 	}
    154 	if (avail != lfs_sb_getavail(fs)) {
    155 		pwarn("AVAIL GIVEN AS %jd, SHOULD BE %ld\n",
    156 		      (intmax_t)lfs_sb_getavail(fs), avail);
    157 		if (preen || reply("FIX")) {
    158 			lfs_sb_setavail(fs, avail);
    159 			sbdirty();
    160 		}
    161 	}
    162 	if (nclean != lfs_sb_getnclean(fs)) {
    163 		pwarn("NCLEAN GIVEN AS %d, SHOULD BE %d\n", lfs_sb_getnclean(fs),
    164 		    nclean);
    165 		if (preen || reply("FIX")) {
    166 			lfs_sb_setnclean(fs, nclean);
    167 			sbdirty();
    168 		}
    169 	}
    170 
    171 	labelskew = 0;
    172 	if (fs->lfs_version > 1 &&
    173 	    lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
    174 		labelskew = lfs_btofsb(fs, LFS_LABELPAD);
    175 	if (lfs_sb_getbfree(fs) > lfs_sb_getdsize(fs) - bb - labelskew ||
    176 	    lfs_sb_getbfree(fs) < lfs_sb_getdsize(fs) - ubb - labelskew) {
    177 		pwarn("BFREE GIVEN AS %jd, SHOULD BE BETWEEN %jd AND %jd\n",
    178 		    (intmax_t)lfs_sb_getbfree(fs),
    179 		    (intmax_t)(lfs_sb_getdsize(fs) - ubb - labelskew),
    180 		    (intmax_t)(lfs_sb_getdsize(fs) - bb - labelskew));
    181 		if (preen || reply("FIX")) {
    182 			lfs_sb_setbfree(fs,
    183 				((lfs_sb_getdsize(fs) - labelskew - ubb) +
    184 				 lfs_sb_getdsize(fs) - labelskew - bb) / 2);
    185 			sbdirty();
    186 		}
    187 	}
    188 }
    189