1 /* $NetBSD: setup.c,v 1.44 2013/06/18 18:18:58 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 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 /* #define DKTYPENAMES */ 61 #define FSTYPENAMES 62 #include <sys/types.h> 63 #include <sys/param.h> 64 #include <sys/time.h> 65 #include <sys/buf.h> 66 #include <sys/mount.h> 67 #include <sys/queue.h> 68 #include <sys/stat.h> 69 #include <sys/ioctl.h> 70 #include <sys/disklabel.h> 71 #include <sys/disk.h> 72 #include <sys/file.h> 73 74 #define vnode uvnode 75 #include <ufs/lfs/lfs.h> 76 #include <ufs/lfs/lfs_inode.h> 77 #undef vnode 78 79 #include <ctype.h> 80 #include <err.h> 81 #include <errno.h> 82 #include <stdio.h> 83 #include <stdlib.h> 84 #include <unistd.h> 85 #include <string.h> 86 #include <time.h> 87 #include <util.h> 88 89 #include "bufcache.h" 90 #include "vnode.h" 91 #include "lfs_user.h" 92 93 #include "fsck.h" 94 #include "extern.h" 95 #include "fsutil.h" 96 97 extern u_int32_t cksum(void *, size_t); 98 static uint64_t calcmaxfilesize(int); 99 100 ulfs_daddr_t *din_table; 101 SEGUSE *seg_table; 102 103 #ifdef DKTYPENAMES 104 int useless(void); 105 106 int 107 useless(void) 108 { 109 char **foo = (char **) dktypenames; 110 char **bar = (char **) fscknames; 111 112 return foo - bar; 113 } 114 #endif 115 116 /* 117 * calculate the maximum file size allowed with the specified block shift. 118 */ 119 static uint64_t 120 calcmaxfilesize(int bshift) 121 { 122 uint64_t nptr; /* number of block pointers per block */ 123 uint64_t maxblock; 124 125 nptr = (1 << bshift) / sizeof(uint32_t); 126 maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr; 127 128 return maxblock << bshift; 129 } 130 131 void 132 reset_maxino(ino_t len) 133 { 134 if (debug) 135 pwarn("maxino reset from %lld to %lld\n", (long long)maxino, 136 (long long)len); 137 138 din_table = erealloc(din_table, len * sizeof(*din_table)); 139 statemap = erealloc(statemap, len * sizeof(char)); 140 typemap = erealloc(typemap, len * sizeof(char)); 141 lncntp = erealloc(lncntp, len * sizeof(int16_t)); 142 143 memset(din_table + maxino, 0, (len - maxino) * sizeof(*din_table)); 144 memset(statemap + maxino, USTATE, (len - maxino) * sizeof(char)); 145 memset(typemap + maxino, 0, (len - maxino) * sizeof(char)); 146 memset(lncntp + maxino, 0, (len - maxino) * sizeof(int16_t)); 147 148 maxino = len; 149 150 /* 151 * We can't roll forward after allocating new inodes in previous 152 * phases, or thy would conflict (lost+found, for example, might 153 * disappear to be replaced by a file found in roll-forward). 154 */ 155 no_roll_forward = 1; 156 157 return; 158 } 159 160 extern time_t write_time; 161 162 int 163 setup(const char *dev) 164 { 165 long bmapsize; 166 struct stat statb; 167 int doskipclean; 168 u_int64_t maxfilesize; 169 int open_flags; 170 struct uvnode *ivp; 171 struct ubuf *bp; 172 int i, isdirty; 173 long sn, curseg; 174 SEGUSE *sup; 175 176 havesb = 0; 177 doskipclean = skipclean; 178 if (stat(dev, &statb) < 0) { 179 pfatal("Can't stat %s: %s\n", dev, strerror(errno)); 180 return (0); 181 } 182 if (!S_ISCHR(statb.st_mode) && skipclean) { 183 pfatal("%s is not a character device", dev); 184 if (reply("CONTINUE") == 0) 185 return (0); 186 } 187 if (nflag) 188 open_flags = O_RDONLY; 189 else 190 open_flags = O_RDWR; 191 192 if ((fsreadfd = open(dev, open_flags)) < 0) { 193 pfatal("Can't open %s: %s\n", dev, strerror(errno)); 194 return (0); 195 } 196 if (nflag) { 197 if (preen) 198 pfatal("NO WRITE ACCESS"); 199 printf("** %s (NO WRITE)\n", dev); 200 quiet = 0; 201 } else if (!preen && !quiet) 202 printf("** %s\n", dev); 203 204 fsmodified = 0; 205 lfdir = 0; 206 207 /* Initialize time in case we have to write */ 208 time(&write_time); 209 210 bufinit(0); /* XXX we could make a better guess */ 211 fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug); 212 if (fs == NULL) { 213 if (preen) 214 printf("%s: ", cdevname()); 215 errexit("BAD SUPER BLOCK OR IFILE INODE NOT FOUND"); 216 } 217 218 /* Resize buffer cache now that we have a superblock to guess from. */ 219 bufrehash((fs->lfs_segtabsz + maxino / fs->lfs_ifpb) << 4); 220 221 if (fs->lfs_pflags & LFS_PF_CLEAN) { 222 if (doskipclean) { 223 if (!quiet) 224 pwarn("%sile system is clean; not checking\n", 225 preen ? "f" : "** F"); 226 return (-1); 227 } 228 if (!preen) 229 pwarn("** File system is already clean\n"); 230 } 231 232 if (idaddr) { 233 daddr_t tdaddr; 234 SEGSUM *sp; 235 FINFO *fp; 236 int bc; 237 238 if (debug) 239 pwarn("adjusting offset, serial for -i 0x%lx\n", 240 (unsigned long)idaddr); 241 tdaddr = lfs_sntod(fs, lfs_dtosn(fs, idaddr)); 242 if (lfs_sntod(fs, lfs_dtosn(fs, tdaddr)) == tdaddr) { 243 if (tdaddr == fs->lfs_start) 244 tdaddr += lfs_btofsb(fs, LFS_LABELPAD); 245 for (i = 0; i < LFS_MAXNUMSB; i++) { 246 if (fs->lfs_sboffs[i] == tdaddr) 247 tdaddr += lfs_btofsb(fs, LFS_SBPAD); 248 if (fs->lfs_sboffs[i] > tdaddr) 249 break; 250 } 251 } 252 fs->lfs_offset = tdaddr; 253 if (debug) 254 pwarn("begin with offset/serial 0x%x/%d\n", 255 (int)fs->lfs_offset, (int)fs->lfs_serial); 256 while (tdaddr < idaddr) { 257 bread(fs->lfs_devvp, LFS_FSBTODB(fs, tdaddr), 258 fs->lfs_sumsize, 259 NULL, 0, &bp); 260 sp = (SEGSUM *)bp->b_data; 261 if (sp->ss_sumsum != cksum(&sp->ss_datasum, 262 fs->lfs_sumsize - 263 sizeof(sp->ss_sumsum))) { 264 brelse(bp, 0); 265 if (debug) 266 printf("bad cksum at %x\n", 267 (unsigned)tdaddr); 268 break; 269 } 270 fp = (FINFO *)(sp + 1); 271 bc = howmany(sp->ss_ninos, LFS_INOPB(fs)) << 272 (fs->lfs_version > 1 ? fs->lfs_ffshift : 273 fs->lfs_bshift); 274 for (i = 0; i < sp->ss_nfinfo; i++) { 275 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1) 276 << fs->lfs_bshift); 277 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks); 278 } 279 280 tdaddr += lfs_btofsb(fs, bc) + 1; 281 fs->lfs_offset = tdaddr; 282 fs->lfs_serial = sp->ss_serial + 1; 283 brelse(bp, 0); 284 } 285 286 /* 287 * Set curseg, nextseg appropriately -- inlined from 288 * lfs_newseg() 289 */ 290 curseg = lfs_dtosn(fs, fs->lfs_offset); 291 fs->lfs_curseg = lfs_sntod(fs, curseg); 292 for (sn = curseg + fs->lfs_interleave;;) { 293 sn = (sn + 1) % fs->lfs_nseg; 294 if (sn == curseg) 295 errx(1, "init: no clean segments"); 296 LFS_SEGENTRY(sup, fs, sn, bp); 297 isdirty = sup->su_flags & SEGUSE_DIRTY; 298 brelse(bp, 0); 299 300 if (!isdirty) 301 break; 302 } 303 304 /* Skip superblock if necessary */ 305 for (i = 0; i < LFS_MAXNUMSB; i++) 306 if (fs->lfs_offset == fs->lfs_sboffs[i]) 307 fs->lfs_offset += lfs_btofsb(fs, LFS_SBPAD); 308 309 ++fs->lfs_nactive; 310 fs->lfs_nextseg = lfs_sntod(fs, sn); 311 if (debug) { 312 pwarn("offset = 0x%" PRIx32 ", serial = %" PRId64 "\n", 313 fs->lfs_offset, fs->lfs_serial); 314 pwarn("curseg = %" PRIx32 ", nextseg = %" PRIx32 "\n", 315 fs->lfs_curseg, fs->lfs_nextseg); 316 } 317 318 if (!nflag && !skipclean) { 319 fs->lfs_idaddr = idaddr; 320 fsmodified = 1; 321 sbdirty(); 322 } 323 } 324 325 if (debug) { 326 pwarn("idaddr = 0x%lx\n", idaddr ? (unsigned long)idaddr : 327 (unsigned long)fs->lfs_idaddr); 328 pwarn("dev_bsize = %lu\n", dev_bsize); 329 pwarn("lfs_bsize = %lu\n", (unsigned long) fs->lfs_bsize); 330 pwarn("lfs_fsize = %lu\n", (unsigned long) fs->lfs_fsize); 331 pwarn("lfs_frag = %lu\n", (unsigned long) fs->lfs_frag); 332 pwarn("lfs_inopb = %lu\n", (unsigned long) fs->lfs_inopb); 333 } 334 if (fs->lfs_version == 1) 335 maxfsblock = fs->lfs_size * (fs->lfs_bsize / dev_bsize); 336 else 337 maxfsblock = fs->lfs_size; 338 maxfilesize = calcmaxfilesize(fs->lfs_bshift); 339 if (/* fs->lfs_minfree < 0 || */ fs->lfs_minfree > 99) { 340 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", 341 fs->lfs_minfree); 342 if (reply("SET TO DEFAULT") == 1) { 343 fs->lfs_minfree = 10; 344 sbdirty(); 345 } 346 } 347 if (fs->lfs_bmask != fs->lfs_bsize - 1) { 348 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK (SHOULD BE 0x%x)", 349 (unsigned int) fs->lfs_bmask, 350 (unsigned int) fs->lfs_bsize - 1); 351 fs->lfs_bmask = fs->lfs_bsize - 1; 352 if (preen) 353 printf(" (FIXED)\n"); 354 if (preen || reply("FIX") == 1) { 355 sbdirty(); 356 } 357 } 358 if (fs->lfs_ffmask != fs->lfs_fsize - 1) { 359 pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK", 360 fs->lfs_ffmask); 361 fs->lfs_ffmask = fs->lfs_fsize - 1; 362 if (preen) 363 printf(" (FIXED)\n"); 364 if (preen || reply("FIX") == 1) { 365 sbdirty(); 366 } 367 } 368 if (fs->lfs_fbmask != (1 << fs->lfs_fbshift) - 1) { 369 pwarn("INCORRECT FBMASK=%" PRId64 " IN SUPERBLOCK", 370 fs->lfs_fbmask); 371 fs->lfs_fbmask = (1 << fs->lfs_fbshift) - 1; 372 if (preen) 373 printf(" (FIXED)\n"); 374 if (preen || reply("FIX") == 1) { 375 sbdirty(); 376 } 377 } 378 if (fs->lfs_maxfilesize != maxfilesize) { 379 pwarn( 380 "INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (SHOULD BE %llu WITH BSHIFT %d)", 381 (unsigned long long) fs->lfs_maxfilesize, 382 (unsigned long long) maxfilesize, (int)fs->lfs_bshift); 383 if (preen) 384 printf(" (FIXED)\n"); 385 if (preen || reply("FIX") == 1) { 386 fs->lfs_maxfilesize = maxfilesize; 387 sbdirty(); 388 } 389 } 390 if (fs->lfs_maxsymlinklen != ULFS1_MAXSYMLINKLEN) { 391 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK", 392 fs->lfs_maxsymlinklen); 393 fs->lfs_maxsymlinklen = ULFS1_MAXSYMLINKLEN; 394 if (preen) 395 printf(" (FIXED)\n"); 396 if (preen || reply("FIX") == 1) { 397 sbdirty(); 398 } 399 } 400 401 /* 402 * Read in the Ifile; we'll be using it a lot. 403 * XXX If the Ifile is corrupted we are in bad shape. We need to 404 * XXX run through the segment headers of the entire disk to 405 * XXX reconstruct the inode table, then pretend all segments are 406 * XXX dirty while we do the rest. 407 */ 408 ivp = fs->lfs_ivnode; 409 maxino = ((VTOI(ivp)->i_ffs1_size - (fs->lfs_cleansz + fs->lfs_segtabsz) 410 * fs->lfs_bsize) / fs->lfs_bsize) * fs->lfs_ifpb; 411 if (debug) 412 pwarn("maxino = %llu\n", (unsigned long long)maxino); 413 for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += fs->lfs_bsize) { 414 bread(ivp, i >> fs->lfs_bshift, fs->lfs_bsize, NOCRED, 0, &bp); 415 /* XXX check B_ERROR */ 416 brelse(bp, 0); 417 } 418 419 /* 420 * allocate and initialize the necessary maps 421 */ 422 din_table = ecalloc(maxino, sizeof(*din_table)); 423 seg_table = ecalloc(fs->lfs_nseg, sizeof(SEGUSE)); 424 /* Get segment flags */ 425 for (i = 0; i < fs->lfs_nseg; i++) { 426 LFS_SEGENTRY(sup, fs, i, bp); 427 seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE; 428 if (preen) 429 seg_table[i].su_nbytes = sup->su_nbytes; 430 brelse(bp, 0); 431 } 432 433 /* Initialize Ifile entry */ 434 din_table[fs->lfs_ifile] = fs->lfs_idaddr; 435 seg_table[lfs_dtosn(fs, fs->lfs_idaddr)].su_nbytes += LFS_DINODE1_SIZE; 436 437 #ifndef VERBOSE_BLOCKMAP 438 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); 439 blockmap = ecalloc(bmapsize, sizeof(char)); 440 #else 441 bmapsize = maxfsblock * sizeof(ino_t); 442 blockmap = ecalloc(maxfsblock, sizeof(ino_t)); 443 #endif 444 statemap = ecalloc(maxino, sizeof(char)); 445 typemap = ecalloc(maxino, sizeof(char)); 446 lncntp = ecalloc(maxino, sizeof(int16_t)); 447 448 if (preen) { 449 n_files = fs->lfs_nfiles; 450 n_blks = fs->lfs_dsize - fs->lfs_bfree; 451 numdirs = maxino; 452 inplast = 0; 453 listmax = numdirs + 10; 454 inpsort = ecalloc(listmax, sizeof(struct inoinfo *)); 455 inphead = ecalloc(numdirs, sizeof(struct inoinfo *)); 456 } 457 458 return (1); 459 460 ckfini(0); 461 return (0); 462 } 463 464