Home | History | Annotate | Line # | Download | only in fsck_lfs
pass5.c revision 1.10
      1  1.10  perseant /* $NetBSD: pass5.c,v 1.10 2002/05/23 04:05:11 perseant Exp $	 */
      2   1.4  perseant 
      3   1.4  perseant /*-
      4   1.4  perseant  * Copyright (c) 2000 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  * 3. All advertising materials mentioning features or use of this software
     19   1.4  perseant  *    must display the following acknowledgement:
     20   1.4  perseant  *      This product includes software developed by the NetBSD
     21   1.4  perseant  *      Foundation, Inc. and its contributors.
     22   1.4  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.4  perseant  *    contributors may be used to endorse or promote products derived
     24   1.4  perseant  *    from this software without specific prior written permission.
     25   1.4  perseant  *
     26   1.4  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.4  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.4  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.4  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.4  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.4  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.4  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.4  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.4  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.4  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.4  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37   1.4  perseant  */
     38   1.1  perseant 
     39   1.1  perseant #include <sys/param.h>
     40   1.1  perseant #include <sys/time.h>
     41   1.1  perseant #include <ufs/ufs/dinode.h>
     42   1.3  perseant #include <ufs/ufs/dir.h>
     43   1.3  perseant #include <sys/mount.h>
     44   1.3  perseant #include <ufs/lfs/lfs.h>
     45  1.10  perseant #include <ufs/lfs/lfs_extern.h>
     46   1.3  perseant 
     47   1.1  perseant #include <string.h>
     48   1.1  perseant #include "fsck.h"
     49   1.1  perseant #include "extern.h"
     50   1.3  perseant #include "fsutil.h"
     51   1.3  perseant 
     52   1.4  perseant extern SEGUSE  *seg_table;
     53   1.1  perseant 
     54   1.1  perseant void
     55   1.1  perseant pass5()
     56   1.1  perseant {
     57   1.4  perseant 	SEGUSE         *su;
     58   1.3  perseant 	struct bufarea *bp;
     59   1.4  perseant 	int             i;
     60   1.6  perseant 	unsigned long   bb; /* total number of used blocks (lower bound) */
     61   1.6  perseant 	unsigned long   ubb; /* upper bound number of used blocks */
     62   1.6  perseant 	unsigned long   avail; /* blocks available for writing */
     63   1.8  perseant 	unsigned long	dmeta; /* blocks in segsums and inodes */
     64   1.7  perseant 	int             nclean; /* clean segments */
     65   1.9  perseant 	size_t          labelskew;
     66   1.3  perseant 
     67   1.3  perseant 	/*
     68   1.3  perseant 	 * Check segment holdings against actual holdings.  Check for
     69   1.3  perseant 	 * "clean" segments that contain live data.
     70   1.3  perseant 	 */
     71   1.7  perseant 	nclean = 0;
     72   1.6  perseant 	avail = 0;
     73   1.6  perseant 	bb = ubb = 0;
     74   1.8  perseant 	dmeta = 0;
     75   1.4  perseant 	for (i = 0; i < sblock.lfs_nseg; i++) {
     76   1.3  perseant 		su = lfs_gseguse(i, &bp);
     77   1.3  perseant 		if (!(su->su_flags & SEGUSE_DIRTY) &&
     78   1.4  perseant 		    seg_table[i].su_nbytes > 0) {
     79   1.3  perseant 			pwarn("%d bytes contained in 'clean' segment %d\n",
     80   1.3  perseant 			      seg_table[i].su_nbytes, i);
     81   1.4  perseant 			if (preen || reply("fix")) {
     82   1.3  perseant 				su->su_flags |= SEGUSE_DIRTY;
     83   1.3  perseant 				dirty(bp);
     84   1.3  perseant 			}
     85   1.3  perseant 		}
     86   1.3  perseant 		if ((su->su_flags & SEGUSE_DIRTY) &&
     87   1.4  perseant 		    su->su_nbytes != seg_table[i].su_nbytes) {
     88   1.5  perseant 			pwarn("segment %d claims %d bytes but has %d",
     89   1.3  perseant 			      i, su->su_nbytes, seg_table[i].su_nbytes);
     90   1.5  perseant 			if (su->su_nbytes > seg_table[i].su_nbytes)
     91   1.5  perseant 				pwarn(" (high by %d)\n", su->su_nbytes -
     92   1.5  perseant 				      seg_table[i].su_nbytes);
     93   1.5  perseant 			else
     94   1.5  perseant 				pwarn(" (low by %d)\n", - su->su_nbytes +
     95   1.5  perseant 				      seg_table[i].su_nbytes);
     96   1.4  perseant 			if (preen || reply("fix")) {
     97   1.3  perseant 				su->su_nbytes = seg_table[i].su_nbytes;
     98   1.3  perseant 				dirty(bp);
     99   1.1  perseant 			}
    100   1.1  perseant 		}
    101   1.6  perseant 		if (su->su_flags & SEGUSE_DIRTY) {
    102   1.9  perseant 			bb += btofsb(&sblock, su->su_nbytes +
    103   1.9  perseant 				    su->su_nsums * sblock.lfs_sumsize);
    104   1.9  perseant 			ubb += btofsb(&sblock, su->su_nbytes +
    105   1.9  perseant 				     su->su_nsums * sblock.lfs_sumsize +
    106   1.9  perseant 				     su->su_ninos * sblock.lfs_ibsize);
    107   1.9  perseant 			dmeta += btofsb(&sblock,
    108   1.9  perseant 				sblock.lfs_sumsize * su->su_nsums);
    109   1.9  perseant 			dmeta += btofsb(&sblock,
    110   1.9  perseant 				sblock.lfs_ibsize * su->su_ninos);
    111   1.6  perseant 		} else {
    112   1.7  perseant 			nclean++;
    113   1.9  perseant 			avail += segtod(&sblock, 1);
    114   1.6  perseant 			if (su->su_flags & SEGUSE_SUPERBLOCK)
    115   1.9  perseant 				avail -= btofsb(&sblock, LFS_SBPAD);
    116   1.9  perseant 			if (i == 0 && sblock.lfs_version > 1 &&
    117   1.9  perseant 			    sblock.lfs_start < btofsb(&sblock, LFS_LABELPAD))
    118   1.9  perseant 				avail -= btofsb(&sblock, LFS_LABELPAD) -
    119   1.9  perseant 					sblock.lfs_start;
    120   1.6  perseant 		}
    121   1.3  perseant 		bp->b_flags &= ~B_INUSE;
    122   1.6  perseant 	}
    123   1.6  perseant 	/* Also may be available bytes in current seg */
    124   1.9  perseant 	i = dtosn(&sblock, sblock.lfs_offset);
    125   1.9  perseant 	avail += sntod(&sblock, i + 1) - sblock.lfs_offset;
    126   1.6  perseant 	/* But do not count minfreesegs */
    127   1.9  perseant 	avail -= segtod(&sblock, (sblock.lfs_minfreeseg -
    128   1.9  perseant 				   (sblock.lfs_minfreeseg / 2)));
    129   1.6  perseant 
    130   1.8  perseant 	if (dmeta != sblock.lfs_dmeta) {
    131   1.8  perseant 		pwarn("dmeta given as %d, should be %ld\n", sblock.lfs_dmeta,
    132   1.8  perseant 			dmeta);
    133   1.8  perseant 		if (preen || reply("fix")) {
    134   1.8  perseant 			sblock.lfs_dmeta = dmeta;
    135   1.8  perseant 			sbdirty();
    136   1.8  perseant 		}
    137   1.8  perseant 	}
    138   1.6  perseant 	if (avail != sblock.lfs_avail) {
    139   1.6  perseant 		pwarn("avail given as %d, should be %ld\n", sblock.lfs_avail,
    140   1.6  perseant 		      avail);
    141   1.6  perseant 		if (preen || reply("fix")) {
    142   1.6  perseant 			sblock.lfs_avail = avail;
    143   1.7  perseant 			sbdirty();
    144   1.7  perseant 		}
    145   1.7  perseant 	}
    146   1.7  perseant 	if (nclean != sblock.lfs_nclean) {
    147   1.7  perseant 		pwarn("nclean given as %d, should be %d\n", sblock.lfs_nclean,
    148   1.7  perseant 		      nclean);
    149   1.7  perseant 		if (preen || reply("fix")) {
    150   1.7  perseant 			sblock.lfs_nclean = nclean;
    151   1.6  perseant 			sbdirty();
    152   1.6  perseant 		}
    153   1.6  perseant 	}
    154   1.9  perseant 	labelskew = (sblock.lfs_version == 1 ? 0 :
    155   1.9  perseant 		btofsb(&sblock, LFS_LABELPAD));
    156   1.9  perseant 	if (sblock.lfs_bfree > sblock.lfs_dsize - bb - labelskew ||
    157   1.9  perseant 	    sblock.lfs_bfree < sblock.lfs_dsize - ubb - labelskew) {
    158   1.6  perseant 		pwarn("bfree given as %d, should be between %ld and %ld\n",
    159   1.9  perseant 		      sblock.lfs_bfree, sblock.lfs_dsize - ubb - labelskew,
    160   1.9  perseant 		      sblock.lfs_dsize - bb - labelskew);
    161   1.6  perseant 		if (preen || reply("fix")) {
    162   1.9  perseant 			sblock.lfs_bfree = sblock.lfs_dsize - labelskew -
    163   1.9  perseant 				(ubb + bb) / 2;
    164   1.6  perseant 			sbdirty();
    165   1.6  perseant 		}
    166   1.1  perseant 	}
    167   1.1  perseant }
    168