Home | History | Annotate | Line # | Download | only in fsck_lfs
pass5.c revision 1.29
      1 /* $NetBSD: pass5.c,v 1.29 2015/05/31 15:44:30 hannken 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_inode.h>
     41 #undef vnode
     42 
     43 #include <string.h>
     44 
     45 #include "bufcache.h"
     46 #include "vnode.h"
     47 #include "lfs_user.h"
     48 
     49 #include "fsck.h"
     50 #include "extern.h"
     51 #include "fsutil.h"
     52 
     53 extern SEGUSE *seg_table;
     54 extern off_t locked_queue_bytes;
     55 
     56 void
     57 pass5(void)
     58 {
     59 	SEGUSE *su;
     60 	struct ubuf *bp;
     61 	int i;
     62 	unsigned long bb;	/* total number of used blocks (lower bound) */
     63 	unsigned long ubb;	/* upper bound number of used blocks */
     64 	unsigned long avail;	/* blocks available for writing */
     65 	unsigned long dmeta;	/* blocks in segsums and inodes */
     66 	int nclean;		/* clean segments */
     67 	size_t labelskew;
     68 	int diddirty;
     69 
     70 	/*
     71 	 * Check segment holdings against actual holdings.  Check for
     72 	 * "clean" segments that contain live data.  If we are only
     73 	 * rolling forward, we can't check the segment holdings, but
     74 	 * we can still check the cleanerinfo data.
     75 	 */
     76 	nclean = 0;
     77 	avail = 0;
     78 	bb = ubb = 0;
     79 	dmeta = 0;
     80 	for (i = 0; i < fs->lfs_nseg; i++) {
     81 		diddirty = 0;
     82 		LFS_SEGENTRY(su, fs, i, bp);
     83 		if (!preen && !(su->su_flags & SEGUSE_DIRTY) &&
     84 		    seg_table[i].su_nbytes > 0) {
     85 			pwarn("CLEAN SEGMENT %d CONTAINS %d BYTES\n",
     86 			    i, seg_table[i].su_nbytes);
     87 			if (reply("MARK SEGMENT DIRTY")) {
     88 				su->su_flags |= SEGUSE_DIRTY;
     89 				++diddirty;
     90 			}
     91 		}
     92 		if (!preen && su->su_nbytes != seg_table[i].su_nbytes) {
     93 			pwarn("SEGMENT %d CLAIMS %d BYTES BUT HAS %d",
     94 			    i, su->su_nbytes, seg_table[i].su_nbytes);
     95 			if ((int32_t)su->su_nbytes >
     96 			    (int32_t)seg_table[i].su_nbytes)
     97 				pwarn(" (HIGH BY %d)\n", su->su_nbytes -
     98 				    seg_table[i].su_nbytes);
     99 			else
    100 				pwarn(" (LOW BY %d)\n", -su->su_nbytes +
    101 				    seg_table[i].su_nbytes);
    102 			if (reply("FIX")) {
    103 				su->su_nbytes = seg_table[i].su_nbytes;
    104 				++diddirty;
    105 			}
    106 		}
    107 		if (su->su_flags & SEGUSE_DIRTY) {
    108 			bb += lfs_btofsb(fs, su->su_nbytes +
    109 			    su->su_nsums * fs->lfs_sumsize);
    110 			ubb += lfs_btofsb(fs, su->su_nbytes +
    111 			    su->su_nsums * fs->lfs_sumsize +
    112 			    su->su_ninos * fs->lfs_ibsize);
    113 			dmeta += lfs_btofsb(fs,
    114 			    fs->lfs_sumsize * su->su_nsums);
    115 			dmeta += lfs_btofsb(fs,
    116 			    fs->lfs_ibsize * su->su_ninos);
    117 		} else {
    118 			nclean++;
    119 			avail += lfs_segtod(fs, 1);
    120 			if (su->su_flags & SEGUSE_SUPERBLOCK)
    121 				avail -= lfs_btofsb(fs, LFS_SBPAD);
    122 			if (i == 0 && fs->lfs_version > 1 &&
    123 			    fs->lfs_s0addr < lfs_btofsb(fs, LFS_LABELPAD))
    124 				avail -= lfs_btofsb(fs, LFS_LABELPAD) -
    125 				    fs->lfs_s0addr;
    126 		}
    127 		if (diddirty)
    128 			VOP_BWRITE(bp);
    129 		else
    130 			brelse(bp, 0);
    131 	}
    132 
    133 	/* Also may be available bytes in current seg */
    134 	i = lfs_dtosn(fs, fs->lfs_offset);
    135 	avail += lfs_sntod(fs, i + 1) - fs->lfs_offset;
    136 	/* But do not count minfreesegs */
    137 	avail -= lfs_segtod(fs, (fs->lfs_minfreeseg -
    138 		(fs->lfs_minfreeseg / 2)));
    139 	/* Note we may have bytes to write yet */
    140 	avail -= lfs_btofsb(fs, locked_queue_bytes);
    141 
    142 	if (idaddr)
    143 		pwarn("NOTE: when using -i, expect discrepancies in dmeta,"
    144 		      " avail, nclean, bfree\n");
    145 	if (dmeta != fs->lfs_dmeta) {
    146 		pwarn("DMETA GIVEN AS %d, SHOULD BE %ld\n", fs->lfs_dmeta,
    147 		    dmeta);
    148 		if (preen || reply("FIX")) {
    149 			fs->lfs_dmeta = dmeta;
    150 			sbdirty();
    151 		}
    152 	}
    153 	if (avail != fs->lfs_avail) {
    154 		pwarn("AVAIL GIVEN AS %d, SHOULD BE %ld\n", fs->lfs_avail,
    155 		    avail);
    156 		if (preen || reply("FIX")) {
    157 			fs->lfs_avail = avail;
    158 			sbdirty();
    159 		}
    160 	}
    161 	if (nclean != fs->lfs_nclean) {
    162 		pwarn("NCLEAN GIVEN AS %d, SHOULD BE %d\n", fs->lfs_nclean,
    163 		    nclean);
    164 		if (preen || reply("FIX")) {
    165 			fs->lfs_nclean = nclean;
    166 			sbdirty();
    167 		}
    168 	}
    169 
    170 	labelskew = 0;
    171 	if (fs->lfs_version > 1 &&
    172 	    fs->lfs_s0addr < lfs_btofsb(fs, LFS_LABELPAD))
    173 		labelskew = lfs_btofsb(fs, LFS_LABELPAD);
    174 	if (fs->lfs_bfree > fs->lfs_dsize - bb - labelskew ||
    175 	    fs->lfs_bfree < fs->lfs_dsize - ubb - labelskew) {
    176 		pwarn("BFREE GIVEN AS %d, SHOULD BE BETWEEN %ld AND %ld\n",
    177 		    fs->lfs_bfree, (fs->lfs_dsize - ubb - labelskew),
    178 		    fs->lfs_dsize - bb - labelskew);
    179 		if (preen || reply("FIX")) {
    180 			fs->lfs_bfree =
    181 				((fs->lfs_dsize - labelskew - ubb) +
    182 				 fs->lfs_dsize - labelskew - bb) / 2;
    183 			sbdirty();
    184 		}
    185 	}
    186 }
    187