xref: /netbsd-src/sbin/fsck_lfs/setup.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /* $NetBSD: setup.c,v 1.36 2008/05/16 09:21:59 hannken 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/file.h>
72 
73 #include <ufs/ufs/inode.h>
74 #include <ufs/ufs/ufsmount.h>
75 #define vnode uvnode
76 #include <ufs/lfs/lfs.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 <string.h>
85 #include <time.h>
86 #include <util.h>
87 
88 #include "bufcache.h"
89 #include "vnode.h"
90 #include "lfs_user.h"
91 
92 #include "fsck.h"
93 #include "extern.h"
94 #include "fsutil.h"
95 
96 extern u_int32_t cksum(void *, size_t);
97 static struct disklabel *getdisklabel(const char *, int);
98 static uint64_t calcmaxfilesize(int);
99 
100 ufs_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 = 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 disklabel *lp;
167 	struct stat statb;
168 	int doskipclean;
169 	u_int64_t maxfilesize;
170 	int open_flags;
171 	struct uvnode *ivp;
172 	struct ubuf *bp;
173 	int i, isdirty;
174 	long sn, curseg;
175 	SEGUSE *sup;
176 
177 	havesb = 0;
178 	doskipclean = skipclean;
179 	if (stat(dev, &statb) < 0) {
180 		pfatal("Can't stat %s: %s\n", dev, strerror(errno));
181 		return (0);
182 	}
183 	if (!S_ISCHR(statb.st_mode) && skipclean) {
184 		pfatal("%s is not a character device", dev);
185 		if (reply("CONTINUE") == 0)
186 			return (0);
187 	}
188 	if (nflag)
189 		open_flags = O_RDONLY;
190 	else
191 		open_flags = O_RDWR;
192 
193 	if ((fsreadfd = open(dev, open_flags)) < 0) {
194 		pfatal("Can't open %s: %s\n", dev, strerror(errno));
195 		return (0);
196 	}
197 	if (nflag) {
198 		if (preen)
199 			pfatal("NO WRITE ACCESS");
200 		printf("** %s (NO WRITE)\n", dev);
201 		quiet = 0;
202 	} else if (!preen && !quiet)
203 		printf("** %s\n", dev);
204 
205 	fsmodified = 0;
206 	lfdir = 0;
207 
208 	/* Initialize time in case we have to write */
209 	time(&write_time);
210 
211 	bufinit(0); /* XXX we could make a better guess */
212 	fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug);
213 	if (fs == NULL) {
214 		if (preen)
215 			printf("%s: ", cdevname());
216 		errexit("BAD SUPER BLOCK OR IFILE INODE NOT FOUND");
217 	}
218 	if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
219 		dev_bsize = secsize = lp->d_secsize;
220 	else
221 		dev_bsize = secsize = DEV_BSIZE;
222 
223         /* Resize buffer cache now that we have a superblock to guess from. */
224         bufrehash((fs->lfs_segtabsz + maxino / fs->lfs_ifpb) << 4);
225 
226 	if (fs->lfs_pflags & LFS_PF_CLEAN) {
227 		if (doskipclean) {
228 			if (!quiet)
229 				pwarn("%sile system is clean; not checking\n",
230 				      preen ? "f" : "** F");
231 			return (-1);
232 		}
233 		if (!preen)
234 			pwarn("** File system is already clean\n");
235 	}
236 
237 	if (idaddr) {
238 		daddr_t tdaddr;
239 		SEGSUM *sp;
240 		FINFO *fp;
241 		int bc;
242 
243 		if (debug)
244 			pwarn("adjusting offset, serial for -i 0x%lx\n",
245 				(unsigned long)idaddr);
246 		tdaddr = sntod(fs, dtosn(fs, idaddr));
247 		if (sntod(fs, dtosn(fs, tdaddr)) == tdaddr) {
248 			if (tdaddr == fs->lfs_start)
249 				tdaddr += btofsb(fs, LFS_LABELPAD);
250 			for (i = 0; i < LFS_MAXNUMSB; i++) {
251 				if (fs->lfs_sboffs[i] == tdaddr)
252 					tdaddr += btofsb(fs, LFS_SBPAD);
253 				if (fs->lfs_sboffs[i] > tdaddr)
254 					break;
255 			}
256 		}
257 		fs->lfs_offset = tdaddr;
258 		if (debug)
259 			pwarn("begin with offset/serial 0x%x/%d\n",
260 				(int)fs->lfs_offset, (int)fs->lfs_serial);
261 		while (tdaddr < idaddr) {
262 			bread(fs->lfs_devvp, fsbtodb(fs, tdaddr),
263 			      fs->lfs_sumsize,
264 			      NULL, 0, &bp);
265 			sp = (SEGSUM *)bp->b_data;
266 			if (sp->ss_sumsum != cksum(&sp->ss_datasum,
267 						   fs->lfs_sumsize -
268 						   sizeof(sp->ss_sumsum))) {
269 				brelse(bp, 0);
270 				if (debug)
271 					printf("bad cksum at %x\n",
272 					       (unsigned)tdaddr);
273 				break;
274 			}
275 			fp = (FINFO *)(sp + 1);
276 			bc = howmany(sp->ss_ninos, INOPB(fs)) <<
277 				(fs->lfs_version > 1 ? fs->lfs_ffshift :
278 						       fs->lfs_bshift);
279 			for (i = 0; i < sp->ss_nfinfo; i++) {
280 				bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
281 					<< fs->lfs_bshift);
282 				fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
283 			}
284 
285 			tdaddr += btofsb(fs, bc) + 1;
286 			fs->lfs_offset = tdaddr;
287 			fs->lfs_serial = sp->ss_serial + 1;
288 			brelse(bp, 0);
289 		}
290 
291 		/*
292 		 * Set curseg, nextseg appropriately -- inlined from
293 		 * lfs_newseg()
294 		 */
295 		curseg = dtosn(fs, fs->lfs_offset);
296 		fs->lfs_curseg = sntod(fs, curseg);
297 		for (sn = curseg + fs->lfs_interleave;;) {
298 			sn = (sn + 1) % fs->lfs_nseg;
299 			if (sn == curseg)
300 				errx(1, "init: no clean segments");
301 			LFS_SEGENTRY(sup, fs, sn, bp);
302 			isdirty = sup->su_flags & SEGUSE_DIRTY;
303 			brelse(bp, 0);
304 
305 			if (!isdirty)
306 				break;
307 		}
308 
309 		/* Skip superblock if necessary */
310 		for (i = 0; i < LFS_MAXNUMSB; i++)
311 			if (fs->lfs_offset == fs->lfs_sboffs[i])
312 				fs->lfs_offset += btofsb(fs, LFS_SBPAD);
313 
314 		++fs->lfs_nactive;
315 		fs->lfs_nextseg = sntod(fs, sn);
316 		if (debug) {
317 			pwarn("offset = 0x%" PRIx32 ", serial = %" PRId64 "\n",
318 				fs->lfs_offset, fs->lfs_serial);
319 			pwarn("curseg = %" PRIx32 ", nextseg = %" PRIx32 "\n",
320 				fs->lfs_curseg, fs->lfs_nextseg);
321 		}
322 
323 		if (!nflag && !skipclean) {
324 			fs->lfs_idaddr = idaddr;
325 			fsmodified = 1;
326 			sbdirty();
327 		}
328 	}
329 
330 	if (debug) {
331 		pwarn("idaddr    = 0x%lx\n", idaddr ? (unsigned long)idaddr :
332 			(unsigned long)fs->lfs_idaddr);
333 		pwarn("dev_bsize = %lu\n", dev_bsize);
334 		pwarn("lfs_bsize = %lu\n", (unsigned long) fs->lfs_bsize);
335 		pwarn("lfs_fsize = %lu\n", (unsigned long) fs->lfs_fsize);
336 		pwarn("lfs_frag  = %lu\n", (unsigned long) fs->lfs_frag);
337 		pwarn("lfs_inopb = %lu\n", (unsigned long) fs->lfs_inopb);
338 	}
339 	if (fs->lfs_version == 1)
340 		maxfsblock = fs->lfs_size * (fs->lfs_bsize / dev_bsize);
341 	else
342 		maxfsblock = fs->lfs_size;
343 	maxfilesize = calcmaxfilesize(fs->lfs_bshift);
344 	if (/* fs->lfs_minfree < 0 || */ fs->lfs_minfree > 99) {
345 		pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
346 		    fs->lfs_minfree);
347 		if (reply("SET TO DEFAULT") == 1) {
348 			fs->lfs_minfree = 10;
349 			sbdirty();
350 		}
351 	}
352 	if (fs->lfs_bmask != fs->lfs_bsize - 1) {
353 		pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK (SHOULD BE 0x%x)",
354 		    (unsigned int) fs->lfs_bmask,
355 		    (unsigned int) fs->lfs_bsize - 1);
356 		fs->lfs_bmask = fs->lfs_bsize - 1;
357 		if (preen)
358 			printf(" (FIXED)\n");
359 		if (preen || reply("FIX") == 1) {
360 			sbdirty();
361 		}
362 	}
363 	if (fs->lfs_ffmask != fs->lfs_fsize - 1) {
364 		pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
365 		    fs->lfs_ffmask);
366 		fs->lfs_ffmask = fs->lfs_fsize - 1;
367 		if (preen)
368 			printf(" (FIXED)\n");
369 		if (preen || reply("FIX") == 1) {
370 			sbdirty();
371 		}
372 	}
373 	if (fs->lfs_fbmask != (1 << fs->lfs_fbshift) - 1) {
374 		pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
375 		    fs->lfs_ffmask);
376 		fs->lfs_fbmask = (1 << fs->lfs_fbshift) - 1;
377 		if (preen)
378 			printf(" (FIXED)\n");
379 		if (preen || reply("FIX") == 1) {
380 			sbdirty();
381 		}
382 	}
383 	if (fs->lfs_maxfilesize != maxfilesize) {
384 		pwarn(
385 		    "INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (SHOULD BE %llu WITH BSHIFT %d)",
386 		    (unsigned long long) fs->lfs_maxfilesize,
387 		    (unsigned long long) maxfilesize, (int)fs->lfs_bshift);
388 		if (preen)
389 			printf(" (FIXED)\n");
390 		if (preen || reply("FIX") == 1) {
391 			fs->lfs_maxfilesize = maxfilesize;
392 			sbdirty();
393 		}
394 	}
395 	if (fs->lfs_maxsymlinklen != MAXSYMLINKLEN_UFS1) {
396 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
397 		    fs->lfs_maxsymlinklen);
398 		fs->lfs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
399 		if (preen)
400 			printf(" (FIXED)\n");
401 		if (preen || reply("FIX") == 1) {
402 			sbdirty();
403 		}
404 	}
405 
406 	/*
407 	 * Read in the Ifile; we'll be using it a lot.
408 	 * XXX If the Ifile is corrupted we are in bad shape.  We need to
409 	 * XXX run through the segment headers of the entire disk to
410 	 * XXX reconstruct the inode table, then pretend all segments are
411 	 * XXX dirty while we do the rest.
412 	 */
413 	ivp = fs->lfs_ivnode;
414 	maxino = ((VTOI(ivp)->i_ffs1_size - (fs->lfs_cleansz + fs->lfs_segtabsz)
415 		* fs->lfs_bsize) / fs->lfs_bsize) * fs->lfs_ifpb;
416 	if (debug)
417 		pwarn("maxino    = %llu\n", (unsigned long long)maxino);
418 	for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += fs->lfs_bsize) {
419 		bread(ivp, i >> fs->lfs_bshift, fs->lfs_bsize, NOCRED, 0, &bp);
420 		/* XXX check B_ERROR */
421 		brelse(bp, 0);
422 	}
423 
424 	/*
425 	 * allocate and initialize the necessary maps
426 	 */
427 	din_table = ecalloc(maxino, sizeof(*din_table));
428 	seg_table = ecalloc(fs->lfs_nseg, sizeof(SEGUSE));
429 	/* Get segment flags */
430 	for (i = 0; i < fs->lfs_nseg; i++) {
431 		LFS_SEGENTRY(sup, fs, i, bp);
432 		seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
433 		if (preen)
434 			seg_table[i].su_nbytes = sup->su_nbytes;
435 		brelse(bp, 0);
436 	}
437 
438 	/* Initialize Ifile entry */
439 	din_table[fs->lfs_ifile] = fs->lfs_idaddr;
440 	seg_table[dtosn(fs, fs->lfs_idaddr)].su_nbytes += DINODE1_SIZE;
441 
442 #ifndef VERBOSE_BLOCKMAP
443 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
444 	blockmap = ecalloc(bmapsize, sizeof(char));
445 #else
446 	bmapsize = maxfsblock * sizeof(ino_t);
447 	blockmap = ecalloc(maxfsblock, sizeof(ino_t));
448 #endif
449 	statemap = ecalloc(maxino, sizeof(char));
450 	typemap = ecalloc(maxino, sizeof(char));
451 	lncntp = ecalloc(maxino, sizeof(int16_t));
452 
453 	if (preen) {
454 		n_files = fs->lfs_nfiles;
455 		n_blks  = fs->lfs_dsize - fs->lfs_bfree;
456 		numdirs = maxino;
457 		inplast = 0;
458 		listmax = numdirs + 10;
459 		inpsort = ecalloc(listmax, sizeof(struct inoinfo *));
460 		inphead = ecalloc(numdirs, sizeof(struct inoinfo *));
461 	}
462 
463 	return (1);
464 
465 	ckfini(0);
466 	return (0);
467 }
468 
469 static struct disklabel *
470 getdisklabel(const char *s, int fd)
471 {
472 	static struct disklabel lab;
473 
474 	if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
475 		if (s == NULL)
476 			return ((struct disklabel *) NULL);
477 		pwarn("ioctl (GCINFO): %s\n", strerror(errno));
478 		return NULL;
479 	}
480 	return (&lab);
481 }
482