Home | History | Annotate | Line # | Download | only in fsck_lfs
      1  1.43     joerg /* $NetBSD: pass0.c,v 1.43 2020/04/03 19:36:33 joerg Exp $	 */
      2   1.3    kleink 
      3  1.14  perseant /*-
      4  1.14  perseant  * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
      5  1.14  perseant  * All rights reserved.
      6  1.14  perseant  *
      7  1.14  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.14  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  1.14  perseant  *
     10  1.14  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.14  perseant  * modification, are permitted provided that the following conditions
     12  1.14  perseant  * are met:
     13  1.14  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.14  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.14  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.14  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.14  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.14  perseant  *
     19  1.14  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.14  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.14  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.14  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.14  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.14  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.14  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.14  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.14  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.14  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.14  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30  1.14  perseant  */
     31  1.14  perseant /*-
     32   1.1  perseant  * Copyright (c) 1980, 1986, 1993
     33   1.1  perseant  *	The Regents of the University of California.  All rights reserved.
     34   1.1  perseant  *
     35   1.1  perseant  * Redistribution and use in source and binary forms, with or without
     36   1.1  perseant  * modification, are permitted provided that the following conditions
     37   1.1  perseant  * are met:
     38   1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     39   1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     40   1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     41   1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     42   1.1  perseant  *    documentation and/or other materials provided with the distribution.
     43  1.16       agc  * 3. Neither the name of the University nor the names of its contributors
     44   1.1  perseant  *    may be used to endorse or promote products derived from this software
     45   1.1  perseant  *    without specific prior written permission.
     46   1.1  perseant  *
     47   1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48   1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49   1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50   1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51   1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52   1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53   1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54   1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55   1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56   1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57   1.1  perseant  * SUCH DAMAGE.
     58   1.1  perseant  */
     59   1.1  perseant 
     60  1.14  perseant #include <sys/types.h>
     61   1.1  perseant #include <sys/param.h>
     62   1.1  perseant #include <sys/time.h>
     63  1.14  perseant #include <sys/buf.h>
     64  1.14  perseant #include <sys/mount.h>
     65  1.14  perseant 
     66  1.34  dholland #define vnode uvnode
     67   1.1  perseant #include <ufs/lfs/lfs.h>
     68  1.37  dholland #include <ufs/lfs/lfs_accessors.h>
     69  1.35  dholland #include <ufs/lfs/lfs_inode.h>
     70  1.14  perseant #undef vnode
     71   1.1  perseant 
     72  1.28  perseant #include <assert.h>
     73  1.26  perseant #include <err.h>
     74   1.1  perseant #include <stdio.h>
     75   1.1  perseant #include <stdlib.h>
     76   1.1  perseant #include <string.h>
     77  1.29  christos #include <util.h>
     78   1.1  perseant 
     79  1.14  perseant #include "bufcache.h"
     80  1.24  christos #include "lfs_user.h"
     81  1.14  perseant 
     82   1.1  perseant #include "fsck.h"
     83   1.1  perseant #include "extern.h"
     84   1.1  perseant #include "fsutil.h"
     85   1.1  perseant 
     86   1.1  perseant /*
     87   1.1  perseant  * Pass 0.  Check the LFS partial segments for valid checksums, correcting
     88   1.1  perseant  * if necessary.  Also check for valid offsets for inode and finfo blocks.
     89   1.1  perseant  */
     90   1.5  perseant /*
     91   1.5  perseant  * XXX more could be done here---consistency between inode-held blocks and
     92   1.5  perseant  * finfo blocks, for one thing.
     93   1.5  perseant  */
     94   1.1  perseant 
     95  1.14  perseant #define dbshift (fs->lfs_bshift - fs->lfs_blktodb)
     96   1.1  perseant 
     97   1.6  perseant void
     98  1.17   xtraeme pass0(void)
     99   1.1  perseant {
    100  1.14  perseant 	daddr_t daddr;
    101  1.15  perseant 	CLEANERINFO *cip;
    102  1.14  perseant 	IFILE *ifp;
    103  1.27  perseant 	struct ubuf *bp, *cbp;
    104  1.28  perseant 	ino_t ino, plastino, nextino, lowfreeino, freehd;
    105  1.28  perseant 	char *visited;
    106  1.15  perseant 	int writeit = 0;
    107   1.5  perseant 
    108   1.5  perseant 	/*
    109  1.28  perseant 	 * Check the inode free list for inuse inodes, and cycles.
    110   1.6  perseant 	 * Make sure that all free inodes are in fact on the list.
    111  1.28  perseant 	 */
    112  1.29  christos 	visited = ecalloc(maxino, sizeof(*visited));
    113  1.10     lukem 	plastino = 0;
    114  1.19  perseant 	lowfreeino = maxino;
    115  1.27  perseant 	LFS_CLEANERINFO(cip, fs, cbp);
    116  1.38  dholland 	freehd = ino = lfs_ci_getfree_head(fs, cip);
    117  1.30        ad 	brelse(cbp, 0);
    118  1.27  perseant 
    119   1.5  perseant 	while (ino) {
    120  1.19  perseant 		if (lowfreeino > ino)
    121  1.19  perseant 			lowfreeino = ino;
    122   1.7  perseant 		if (ino >= maxino) {
    123  1.28  perseant 			pwarn("OUT OF RANGE INO %llu ON FREE LIST\n",
    124  1.27  perseant 			    (unsigned long long)ino);
    125   1.7  perseant 			break;
    126   1.7  perseant 		}
    127   1.6  perseant 		if (visited[ino]) {
    128  1.27  perseant 			pwarn("INO %llu ALREADY FOUND ON FREE LIST\n",
    129  1.22  christos 			    (unsigned long long)ino);
    130   1.6  perseant 			if (preen || reply("FIX") == 1) {
    131  1.10     lukem 				/* plastino can't be zero */
    132  1.14  perseant 				LFS_IENTRY(ifp, fs, plastino, bp);
    133  1.39  dholland 				lfs_if_setnextfree(fs, ifp, 0);
    134  1.14  perseant 				VOP_BWRITE(bp);
    135   1.6  perseant 			}
    136   1.6  perseant 			break;
    137   1.6  perseant 		}
    138  1.28  perseant 		visited[ino] = 1;
    139  1.14  perseant 		LFS_IENTRY(ifp, fs, ino, bp);
    140  1.39  dholland 		nextino = lfs_if_getnextfree(fs, ifp);
    141  1.39  dholland 		daddr = lfs_if_getdaddr(fs, ifp);
    142  1.30        ad 		brelse(bp, 0);
    143   1.5  perseant 		if (daddr) {
    144  1.27  perseant 			pwarn("INO %llu WITH DADDR 0x%llx ON FREE LIST\n",
    145  1.22  christos 			    (unsigned long long)ino, (long long) daddr);
    146   1.5  perseant 			if (preen || reply("FIX") == 1) {
    147  1.10     lukem 				if (plastino == 0) {
    148  1.28  perseant 					freehd = nextino;
    149   1.5  perseant 					sbdirty();
    150   1.5  perseant 				} else {
    151  1.14  perseant 					LFS_IENTRY(ifp, fs, plastino, bp);
    152  1.39  dholland 					lfs_if_setnextfree(fs, ifp, nextino);
    153  1.14  perseant 					VOP_BWRITE(bp);
    154   1.5  perseant 				}
    155   1.5  perseant 				ino = nextino;
    156   1.5  perseant 				continue;
    157   1.5  perseant 			}
    158   1.5  perseant 		}
    159  1.10     lukem 		plastino = ino;
    160   1.5  perseant 		ino = nextino;
    161   1.6  perseant 	}
    162  1.15  perseant 
    163  1.15  perseant 
    164   1.6  perseant 	/*
    165   1.6  perseant 	 * Make sure all free inodes were found on the list
    166   1.6  perseant 	 */
    167  1.33  dholland 	for (ino = maxino - 1; ino > ULFS_ROOTINO; --ino) {
    168   1.6  perseant 		if (visited[ino])
    169   1.6  perseant 			continue;
    170   1.6  perseant 
    171  1.14  perseant 		LFS_IENTRY(ifp, fs, ino, bp);
    172  1.39  dholland 		if (lfs_if_getdaddr(fs, ifp)) {
    173  1.30        ad 			brelse(bp, 0);
    174   1.6  perseant 			continue;
    175   1.6  perseant 		}
    176  1.27  perseant 		pwarn("INO %llu FREE BUT NOT ON FREE LIST\n",
    177  1.22  christos 		    (unsigned long long)ino);
    178   1.6  perseant 		if (preen || reply("FIX") == 1) {
    179  1.28  perseant 			assert(ino != freehd);
    180  1.39  dholland 			lfs_if_setnextfree(fs, ifp, freehd);
    181  1.27  perseant 			VOP_BWRITE(bp);
    182  1.27  perseant 
    183  1.28  perseant 			freehd = ino;
    184   1.6  perseant 			sbdirty();
    185  1.27  perseant 
    186  1.27  perseant 			/* If freelist was empty, this is the tail */
    187  1.27  perseant 			if (plastino == 0)
    188  1.27  perseant 				plastino = ino;
    189  1.14  perseant 		} else
    190  1.30        ad 			brelse(bp, 0);
    191   1.5  perseant 	}
    192  1.15  perseant 
    193  1.27  perseant 	LFS_CLEANERINFO(cip, fs, cbp);
    194  1.38  dholland 	if (lfs_ci_getfree_head(fs, cip) != freehd) {
    195  1.28  perseant 		/* They've already given us permission for this change */
    196  1.38  dholland 		lfs_ci_setfree_head(fs, cip, freehd);
    197  1.28  perseant 		writeit = 1;
    198  1.28  perseant 	}
    199  1.36  dholland 	if (freehd != lfs_sb_getfreehd(fs)) {
    200  1.42  dholland 		pwarn("FREE LIST HEAD IN SUPERBLOCK SHOULD BE %ju (WAS %ju)\n",
    201  1.42  dholland 			(uintmax_t)freehd, (uintmax_t)lfs_sb_getfreehd(fs));
    202  1.15  perseant 		if (preen || reply("FIX")) {
    203  1.36  dholland 			lfs_sb_setfreehd(fs, freehd);
    204  1.27  perseant 			sbdirty();
    205  1.15  perseant 		}
    206  1.15  perseant 	}
    207  1.38  dholland 	if (lfs_ci_getfree_tail(fs, cip) != plastino) {
    208  1.27  perseant 		pwarn("FREE LIST TAIL SHOULD BE %llu (WAS %llu)\n",
    209  1.27  perseant 		    (unsigned long long)plastino,
    210  1.38  dholland 		    (unsigned long long)lfs_ci_getfree_tail(fs, cip));
    211  1.15  perseant 		if (preen || reply("FIX")) {
    212  1.38  dholland 			lfs_ci_setfree_tail(fs, cip, plastino);
    213  1.15  perseant 			writeit = 1;
    214  1.15  perseant 		}
    215  1.15  perseant 	}
    216  1.28  perseant 
    217  1.18  perseant 	if (writeit)
    218  1.27  perseant 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, writeit);
    219  1.18  perseant 	else
    220  1.30        ad 		brelse(cbp, 0);
    221  1.19  perseant 
    222  1.36  dholland 	if (lfs_sb_getfreehd(fs) == 0) {
    223  1.20  perseant 		pwarn("%sree list head is 0x0\n", preen ? "f" : "F");
    224  1.28  perseant 		if (preen || reply("FIX"))
    225  1.21  perseant 			extend_ifile(fs);
    226  1.20  perseant 	}
    227   1.1  perseant }
    228