Home | History | Annotate | Line # | Download | only in fsck_lfs
setup.c revision 1.48
      1 /* $NetBSD: setup.c,v 1.48 2015/07/24 06:59:32 dholland 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 (at) 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((lfs_sb_getsegtabsz(fs) + maxino / lfs_sb_getifpb(fs)) << 4);
    220 
    221 	if (lfs_sb_getpflags(fs) & 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 == lfs_sb_gets0addr(fs))
    244 				tdaddr += lfs_btofsb(fs, LFS_LABELPAD);
    245 			for (i = 0; i < LFS_MAXNUMSB; i++) {
    246 				if (lfs_sb_getsboff(fs, i) == tdaddr)
    247 					tdaddr += lfs_btofsb(fs, LFS_SBPAD);
    248 				if (lfs_sb_getsboff(fs, i) > tdaddr)
    249 					break;
    250 			}
    251 		}
    252 		lfs_sb_setoffset(fs, tdaddr);
    253 		if (debug)
    254 			pwarn("begin with offset/serial 0x%jx/%jd\n",
    255 				(uintmax_t)lfs_sb_getoffset(fs),
    256 				(intmax_t)lfs_sb_getserial(fs));
    257 		while (tdaddr < idaddr) {
    258 			bread(fs->lfs_devvp, LFS_FSBTODB(fs, tdaddr),
    259 			      lfs_sb_getsumsize(fs),
    260 			      0, &bp);
    261 			sp = (SEGSUM *)bp->b_data;
    262 			if (sp->ss_sumsum != cksum(&sp->ss_datasum,
    263 						   lfs_sb_getsumsize(fs) -
    264 						   sizeof(sp->ss_sumsum))) {
    265 				brelse(bp, 0);
    266 				if (debug)
    267 					printf("bad cksum at %x\n",
    268 					       (unsigned)tdaddr);
    269 				break;
    270 			}
    271 			fp = (FINFO *)(sp + 1);
    272 			bc = howmany(sp->ss_ninos, LFS_INOPB(fs)) <<
    273 				(fs->lfs_version > 1 ? lfs_sb_getffshift(fs) :
    274 						       lfs_sb_getbshift(fs));
    275 			for (i = 0; i < sp->ss_nfinfo; i++) {
    276 				bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
    277 					<< lfs_sb_getbshift(fs));
    278 				fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
    279 			}
    280 
    281 			tdaddr += lfs_btofsb(fs, bc) + 1;
    282 			lfs_sb_setoffset(fs, tdaddr);
    283 			lfs_sb_setserial(fs, sp->ss_serial + 1);
    284 			brelse(bp, 0);
    285 		}
    286 
    287 		/*
    288 		 * Set curseg, nextseg appropriately -- inlined from
    289 		 * lfs_newseg()
    290 		 */
    291 		curseg = lfs_dtosn(fs, lfs_sb_getoffset(fs));
    292 		lfs_sb_setcurseg(fs, lfs_sntod(fs, curseg));
    293 		for (sn = curseg + lfs_sb_getinterleave(fs);;) {
    294 			sn = (sn + 1) % lfs_sb_getnseg(fs);
    295 			if (sn == curseg)
    296 				errx(1, "init: no clean segments");
    297 			LFS_SEGENTRY(sup, fs, sn, bp);
    298 			isdirty = sup->su_flags & SEGUSE_DIRTY;
    299 			brelse(bp, 0);
    300 
    301 			if (!isdirty)
    302 				break;
    303 		}
    304 
    305 		/* Skip superblock if necessary */
    306 		for (i = 0; i < LFS_MAXNUMSB; i++)
    307 			if (lfs_sb_getoffset(fs) == lfs_sb_getsboff(fs, i))
    308 				lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
    309 
    310 		++fs->lfs_nactive;
    311 		lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
    312 		if (debug) {
    313 			pwarn("offset = 0x%" PRIx32 ", serial = %" PRIu64 "\n",
    314 				lfs_sb_getoffset(fs), lfs_sb_getserial(fs));
    315 			pwarn("curseg = %" PRIx32 ", nextseg = %" PRIx32 "\n",
    316 				lfs_sb_getcurseg(fs), lfs_sb_getnextseg(fs));
    317 		}
    318 
    319 		if (!nflag && !skipclean) {
    320 			lfs_sb_setidaddr(fs, idaddr);
    321 			fsmodified = 1;
    322 			sbdirty();
    323 		}
    324 	}
    325 
    326 	if (debug) {
    327 		pwarn("idaddr    = 0x%jx\n", idaddr ? (uintmax_t)idaddr :
    328 			(uintmax_t)lfs_sb_getidaddr(fs));
    329 		pwarn("dev_bsize = %lu\n", dev_bsize);
    330 		pwarn("lfs_bsize = %lu\n", (unsigned long) lfs_sb_getbsize(fs));
    331 		pwarn("lfs_fsize = %lu\n", (unsigned long) lfs_sb_getfsize(fs));
    332 		pwarn("lfs_frag  = %lu\n", (unsigned long) lfs_sb_getfrag(fs));
    333 		pwarn("lfs_inopb = %lu\n", (unsigned long) lfs_sb_getinopb(fs));
    334 	}
    335 	if (fs->lfs_version == 1)
    336 		maxfsblock = lfs_sb_getsize(fs) * (lfs_sb_getbsize(fs) / dev_bsize);
    337 	else
    338 		maxfsblock = lfs_sb_getsize(fs);
    339 	maxfilesize = calcmaxfilesize(lfs_sb_getbshift(fs));
    340 	if (/* lfs_sb_getminfree(fs) < 0 || */ lfs_sb_getminfree(fs) > 99) {
    341 		pfatal("IMPOSSIBLE MINFREE=%u IN SUPERBLOCK",
    342 		    lfs_sb_getminfree(fs));
    343 		if (reply("SET TO DEFAULT") == 1) {
    344 			lfs_sb_setminfree(fs, 10);
    345 			sbdirty();
    346 		}
    347 	}
    348 	if (lfs_sb_getbmask(fs) != lfs_sb_getbsize(fs) - 1) {
    349 		pwarn("INCORRECT BMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
    350 		    (uintmax_t)lfs_sb_getbmask(fs),
    351 		    lfs_sb_getbsize(fs) - 1);
    352 		lfs_sb_setbmask(fs, lfs_sb_getbsize(fs) - 1);
    353 		if (preen)
    354 			printf(" (FIXED)\n");
    355 		if (preen || reply("FIX") == 1) {
    356 			sbdirty();
    357 		}
    358 	}
    359 	if (lfs_sb_getffmask(fs) != lfs_sb_getfsize(fs) - 1) {
    360 		pwarn("INCORRECT FFMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
    361 		    (uintmax_t)lfs_sb_getffmask(fs),
    362 		    lfs_sb_getfsize(fs) - 1);
    363 		lfs_sb_setffmask(fs, lfs_sb_getfsize(fs) - 1);
    364 		if (preen)
    365 			printf(" (FIXED)\n");
    366 		if (preen || reply("FIX") == 1) {
    367 			sbdirty();
    368 		}
    369 	}
    370 	if (lfs_sb_getfbmask(fs) != (1U << lfs_sb_getfbshift(fs)) - 1) {
    371 		pwarn("INCORRECT FBMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
    372 		    (uintmax_t)lfs_sb_getfbmask(fs),
    373 		      (1U << lfs_sb_getfbshift(fs)) - 1);
    374 		lfs_sb_setfbmask(fs, (1U << lfs_sb_getfbshift(fs)) - 1);
    375 		if (preen)
    376 			printf(" (FIXED)\n");
    377 		if (preen || reply("FIX") == 1) {
    378 			sbdirty();
    379 		}
    380 	}
    381 	if (lfs_sb_getmaxfilesize(fs) != maxfilesize) {
    382 		pwarn(
    383 		    "INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (SHOULD BE %llu WITH BSHIFT %u)",
    384 		    (unsigned long long) lfs_sb_getmaxfilesize(fs),
    385 		    (unsigned long long) maxfilesize, lfs_sb_getbshift(fs));
    386 		if (preen)
    387 			printf(" (FIXED)\n");
    388 		if (preen || reply("FIX") == 1) {
    389 			lfs_sb_setmaxfilesize(fs, maxfilesize);
    390 			sbdirty();
    391 		}
    392 	}
    393 	if (lfs_sb_getmaxsymlinklen(fs) != ULFS1_MAXSYMLINKLEN) {
    394 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK (SHOULD BE %zu)",
    395 		    lfs_sb_getmaxsymlinklen(fs), ULFS1_MAXSYMLINKLEN);
    396 		lfs_sb_setmaxsymlinklen(fs, ULFS1_MAXSYMLINKLEN);
    397 		if (preen)
    398 			printf(" (FIXED)\n");
    399 		if (preen || reply("FIX") == 1) {
    400 			sbdirty();
    401 		}
    402 	}
    403 
    404 	/*
    405 	 * Read in the Ifile; we'll be using it a lot.
    406 	 * XXX If the Ifile is corrupted we are in bad shape.  We need to
    407 	 * XXX run through the segment headers of the entire disk to
    408 	 * XXX reconstruct the inode table, then pretend all segments are
    409 	 * XXX dirty while we do the rest.
    410 	 */
    411 	ivp = fs->lfs_ivnode;
    412 	maxino = ((VTOI(ivp)->i_ffs1_size - (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
    413 		* lfs_sb_getbsize(fs)) / lfs_sb_getbsize(fs)) * lfs_sb_getifpb(fs);
    414 	if (debug)
    415 		pwarn("maxino    = %llu\n", (unsigned long long)maxino);
    416 	for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += lfs_sb_getbsize(fs)) {
    417 		bread(ivp, i >> lfs_sb_getbshift(fs), lfs_sb_getbsize(fs), 0, &bp);
    418 		/* XXX check B_ERROR */
    419 		brelse(bp, 0);
    420 	}
    421 
    422 	/*
    423 	 * allocate and initialize the necessary maps
    424 	 */
    425 	din_table = ecalloc(maxino, sizeof(*din_table));
    426 	seg_table = ecalloc(lfs_sb_getnseg(fs), sizeof(SEGUSE));
    427 	/* Get segment flags */
    428 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
    429 		LFS_SEGENTRY(sup, fs, i, bp);
    430 		seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
    431 		if (preen)
    432 			seg_table[i].su_nbytes = sup->su_nbytes;
    433 		brelse(bp, 0);
    434 	}
    435 
    436 	/* Initialize Ifile entry */
    437 	din_table[lfs_sb_getifile(fs)] = lfs_sb_getidaddr(fs);
    438 	seg_table[lfs_dtosn(fs, lfs_sb_getidaddr(fs))].su_nbytes += LFS_DINODE1_SIZE;
    439 
    440 #ifndef VERBOSE_BLOCKMAP
    441 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
    442 	blockmap = ecalloc(bmapsize, sizeof(char));
    443 #else
    444 	bmapsize = maxfsblock * sizeof(ino_t);
    445 	blockmap = ecalloc(maxfsblock, sizeof(ino_t));
    446 #endif
    447 	statemap = ecalloc(maxino, sizeof(char));
    448 	typemap = ecalloc(maxino, sizeof(char));
    449 	lncntp = ecalloc(maxino, sizeof(int16_t));
    450 
    451 	if (preen) {
    452 		n_files = lfs_sb_getnfiles(fs);
    453 		n_blks  = lfs_sb_getdsize(fs) - lfs_sb_getbfree(fs);
    454 		numdirs = maxino;
    455 		inplast = 0;
    456 		listmax = numdirs + 10;
    457 		inpsort = ecalloc(listmax, sizeof(struct inoinfo *));
    458 		inphead = ecalloc(numdirs, sizeof(struct inoinfo *));
    459 	}
    460 
    461 	return (1);
    462 
    463 	ckfini(0);
    464 	return (0);
    465 }
    466 
    467