1/* $NetBSD: pass0.c,v 1.43 2020/04/03 19:36:33 joerg 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#define vnode uvnode 67#include <ufs/lfs/lfs.h> 68#include <ufs/lfs/lfs_accessors.h> 69#include <ufs/lfs/lfs_inode.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 "lfs_user.h" 81 82#include "fsck.h" 83#include "extern.h" 84#include "fsutil.h" 85 86/* 87 * Pass 0. Check the LFS partial segments for valid checksums, correcting 88 * if necessary. Also check for valid offsets for inode and finfo blocks. 89 */ 90/* 91 * XXX more could be done here---consistency between inode-held blocks and 92 * finfo blocks, for one thing. 93 */ 94 95#define dbshift (fs->lfs_bshift - fs->lfs_blktodb) 96 97void 98pass0(void) 99{ 100 daddr_t daddr; 101 CLEANERINFO *cip; 102 IFILE *ifp; 103 struct ubuf *bp, *cbp; 104 ino_t ino, plastino, nextino, lowfreeino, freehd; 105 char *visited; 106 int writeit = 0; 107 108 /* 109 * Check the inode free list for inuse inodes, and cycles. 110 * Make sure that all free inodes are in fact on the list. 111 */ 112 visited = ecalloc(maxino, sizeof(*visited)); 113 plastino = 0; 114 lowfreeino = maxino; 115 LFS_CLEANERINFO(cip, fs, cbp); 116 freehd = ino = lfs_ci_getfree_head(fs, cip); 117 brelse(cbp, 0); 118 119 while (ino) { 120 if (lowfreeino > ino) 121 lowfreeino = ino; 122 if (ino >= maxino) { 123 pwarn("OUT OF RANGE INO %llu ON FREE LIST\n", 124 (unsigned long long)ino); 125 break; 126 } 127 if (visited[ino]) { 128 pwarn("INO %llu ALREADY FOUND ON FREE LIST\n", 129 (unsigned long long)ino); 130 if (preen || reply("FIX") == 1) { 131 /* plastino can't be zero */ 132 LFS_IENTRY(ifp, fs, plastino, bp); 133 lfs_if_setnextfree(fs, ifp, 0); 134 VOP_BWRITE(bp); 135 } 136 break; 137 } 138 visited[ino] = 1; 139 LFS_IENTRY(ifp, fs, ino, bp); 140 nextino = lfs_if_getnextfree(fs, ifp); 141 daddr = lfs_if_getdaddr(fs, ifp); 142 brelse(bp, 0); 143 if (daddr) { 144 pwarn("INO %llu WITH DADDR 0x%llx ON FREE LIST\n", 145 (unsigned long long)ino, (long long) daddr); 146 if (preen || reply("FIX") == 1) { 147 if (plastino == 0) { 148 freehd = nextino; 149 sbdirty(); 150 } else { 151 LFS_IENTRY(ifp, fs, plastino, bp); 152 lfs_if_setnextfree(fs, ifp, nextino); 153 VOP_BWRITE(bp); 154 } 155 ino = nextino; 156 continue; 157 } 158 } 159 plastino = ino; 160 ino = nextino; 161 } 162 163 164 /* 165 * Make sure all free inodes were found on the list 166 */ 167 for (ino = maxino - 1; ino > ULFS_ROOTINO; --ino) { 168 if (visited[ino]) 169 continue; 170 171 LFS_IENTRY(ifp, fs, ino, bp); 172 if (lfs_if_getdaddr(fs, ifp)) { 173 brelse(bp, 0); 174 continue; 175 } 176 pwarn("INO %llu FREE BUT NOT ON FREE LIST\n", 177 (unsigned long long)ino); 178 if (preen || reply("FIX") == 1) { 179 assert(ino != freehd); 180 lfs_if_setnextfree(fs, ifp, freehd); 181 VOP_BWRITE(bp); 182 183 freehd = ino; 184 sbdirty(); 185 186 /* If freelist was empty, this is the tail */ 187 if (plastino == 0) 188 plastino = ino; 189 } else 190 brelse(bp, 0); 191 } 192 193 LFS_CLEANERINFO(cip, fs, cbp); 194 if (lfs_ci_getfree_head(fs, cip) != freehd) { 195 /* They've already given us permission for this change */ 196 lfs_ci_setfree_head(fs, cip, freehd); 197 writeit = 1; 198 } 199 if (freehd != lfs_sb_getfreehd(fs)) { 200 pwarn("FREE LIST HEAD IN SUPERBLOCK SHOULD BE %ju (WAS %ju)\n", 201 (uintmax_t)freehd, (uintmax_t)lfs_sb_getfreehd(fs)); 202 if (preen || reply("FIX")) { 203 lfs_sb_setfreehd(fs, freehd); 204 sbdirty(); 205 } 206 } 207 if (lfs_ci_getfree_tail(fs, cip) != plastino) { 208 pwarn("FREE LIST TAIL SHOULD BE %llu (WAS %llu)\n", 209 (unsigned long long)plastino, 210 (unsigned long long)lfs_ci_getfree_tail(fs, cip)); 211 if (preen || reply("FIX")) { 212 lfs_ci_setfree_tail(fs, cip, plastino); 213 writeit = 1; 214 } 215 } 216 217 if (writeit) 218 LFS_SYNC_CLEANERINFO(cip, fs, cbp, writeit); 219 else 220 brelse(cbp, 0); 221 222 if (lfs_sb_getfreehd(fs) == 0) { 223 pwarn("%sree list head is 0x0\n", preen ? "f" : "F"); 224 if (preen || reply("FIX")) 225 extend_ifile(fs); 226 } 227} 228