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