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