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