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