xref: /netbsd-src/sbin/fsck_lfs/setup.c (revision 63aea4bd5b445e491ff0389fe27ec78b3099dba3)
1 /* $NetBSD: setup.c,v 1.60 2015/10/03 08:29:21 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@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 	long bmapsize;
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 	size_t sumstart;
177 
178 	havesb = 0;
179 	doskipclean = skipclean;
180 	if (stat(dev, &statb) < 0) {
181 		pfatal("Can't stat %s: %s\n", dev, strerror(errno));
182 		return (0);
183 	}
184 	if (!S_ISCHR(statb.st_mode) && skipclean) {
185 		pfatal("%s is not a character device", dev);
186 		if (reply("CONTINUE") == 0)
187 			return (0);
188 	}
189 	if (nflag)
190 		open_flags = O_RDONLY;
191 	else
192 		open_flags = O_RDWR;
193 
194 	if ((fsreadfd = open(dev, open_flags)) < 0) {
195 		pfatal("Can't open %s: %s\n", dev, strerror(errno));
196 		return (0);
197 	}
198 	if (nflag) {
199 		if (preen)
200 			pfatal("NO WRITE ACCESS");
201 		printf("** %s (NO WRITE)\n", dev);
202 		quiet = 0;
203 	} else if (!preen && !quiet)
204 		printf("** %s\n", dev);
205 
206 	fsmodified = 0;
207 	lfdir = 0;
208 
209 	/* Initialize time in case we have to write */
210 	time(&write_time);
211 
212 	bufinit(0); /* XXX we could make a better guess */
213 	fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug);
214 	if (fs == NULL) {
215 		if (preen)
216 			printf("%s: ", cdevname());
217 		errexit("BAD SUPER BLOCK OR IFILE INODE NOT FOUND");
218 	}
219 
220         /* Resize buffer cache now that we have a superblock to guess from. */
221         bufrehash((lfs_sb_getsegtabsz(fs) + maxino / lfs_sb_getifpb(fs)) << 4);
222 
223 	if (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) {
224 		if (doskipclean) {
225 			if (!quiet)
226 				pwarn("%sile system is clean; not checking\n",
227 				      preen ? "f" : "** F");
228 			return (-1);
229 		}
230 		if (!preen)
231 			pwarn("** File system is already clean\n");
232 	}
233 
234 	if (idaddr) {
235 		daddr_t tdaddr;
236 		SEGSUM *sp;
237 		FINFO *fp;
238 		int bc;
239 
240 		if (debug)
241 			pwarn("adjusting offset, serial for -i 0x%jx\n",
242 				(uintmax_t)idaddr);
243 		tdaddr = lfs_sntod(fs, lfs_dtosn(fs, idaddr));
244 		if (lfs_sntod(fs, lfs_dtosn(fs, tdaddr)) == tdaddr) {
245 			if (tdaddr == lfs_sb_gets0addr(fs))
246 				tdaddr += lfs_btofsb(fs, LFS_LABELPAD);
247 			for (i = 0; i < LFS_MAXNUMSB; i++) {
248 				if (lfs_sb_getsboff(fs, i) == tdaddr)
249 					tdaddr += lfs_btofsb(fs, LFS_SBPAD);
250 				if (lfs_sb_getsboff(fs, i) > tdaddr)
251 					break;
252 			}
253 		}
254 		lfs_sb_setoffset(fs, tdaddr);
255 		if (debug)
256 			pwarn("begin with offset/serial 0x%jx/%jd\n",
257 				(uintmax_t)lfs_sb_getoffset(fs),
258 				(intmax_t)lfs_sb_getserial(fs));
259 		while (tdaddr < idaddr) {
260 			bread(fs->lfs_devvp, LFS_FSBTODB(fs, tdaddr),
261 			      lfs_sb_getsumsize(fs),
262 			      0, &bp);
263 			sp = (SEGSUM *)bp->b_data;
264 			sumstart = lfs_ss_getsumstart(fs);
265 			if (lfs_ss_getsumsum(fs, sp) !=
266 			    cksum((char *)sp + sumstart,
267 				  lfs_sb_getsumsize(fs) - sumstart)) {
268 				brelse(bp, 0);
269 				if (debug)
270 					printf("bad cksum at %jx\n",
271 					       (uintmax_t)tdaddr);
272 				break;
273 			}
274 			fp = SEGSUM_FINFOBASE(fs, sp);
275 			bc = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) <<
276 				(lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) :
277 						       lfs_sb_getbshift(fs));
278 			for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) {
279 				bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1)
280 					<< lfs_sb_getbshift(fs));
281 				fp = NEXT_FINFO(fs, fp);
282 			}
283 
284 			tdaddr += lfs_btofsb(fs, bc) + 1;
285 			lfs_sb_setoffset(fs, tdaddr);
286 			lfs_sb_setserial(fs, lfs_ss_getserial(fs, sp) + 1);
287 			brelse(bp, 0);
288 		}
289 
290 		/*
291 		 * Set curseg, nextseg appropriately -- inlined from
292 		 * lfs_newseg()
293 		 */
294 		curseg = lfs_dtosn(fs, lfs_sb_getoffset(fs));
295 		lfs_sb_setcurseg(fs, lfs_sntod(fs, curseg));
296 		for (sn = curseg + lfs_sb_getinterleave(fs);;) {
297 			sn = (sn + 1) % lfs_sb_getnseg(fs);
298 			if (sn == curseg)
299 				errx(1, "init: no clean segments");
300 			LFS_SEGENTRY(sup, fs, sn, bp);
301 			isdirty = sup->su_flags & SEGUSE_DIRTY;
302 			brelse(bp, 0);
303 
304 			if (!isdirty)
305 				break;
306 		}
307 
308 		/* Skip superblock if necessary */
309 		for (i = 0; i < LFS_MAXNUMSB; i++)
310 			if (lfs_sb_getoffset(fs) == lfs_sb_getsboff(fs, i))
311 				lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
312 
313 		++fs->lfs_nactive;
314 		lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
315 		if (debug) {
316 			pwarn("offset = 0x%" PRIx64 ", serial = %" PRIu64 "\n",
317 				lfs_sb_getoffset(fs), lfs_sb_getserial(fs));
318 			pwarn("curseg = %" PRIx64 ", nextseg = %" PRIx64 "\n",
319 				lfs_sb_getcurseg(fs), lfs_sb_getnextseg(fs));
320 		}
321 
322 		if (!nflag && !skipclean) {
323 			lfs_sb_setidaddr(fs, idaddr);
324 			fsmodified = 1;
325 			sbdirty();
326 		}
327 	}
328 
329 	if (debug) {
330 		pwarn("idaddr    = 0x%jx\n", idaddr ? (uintmax_t)idaddr :
331 			(uintmax_t)lfs_sb_getidaddr(fs));
332 		pwarn("dev_bsize = %lu\n", dev_bsize);
333 		pwarn("lfs_bsize = %lu\n", (unsigned long) lfs_sb_getbsize(fs));
334 		pwarn("lfs_fsize = %lu\n", (unsigned long) lfs_sb_getfsize(fs));
335 		pwarn("lfs_frag  = %lu\n", (unsigned long) lfs_sb_getfrag(fs));
336 		pwarn("lfs_inopb = %lu\n", (unsigned long) lfs_sb_getinopb(fs));
337 	}
338 	if (lfs_sb_getversion(fs) == 1)
339 		maxfsblock = lfs_blkstofrags(fs, lfs_sb_getsize(fs));
340 	else
341 		maxfsblock = lfs_sb_getsize(fs);
342 	maxfilesize = calcmaxfilesize(lfs_sb_getbshift(fs));
343 	if (/* lfs_sb_getminfree(fs) < 0 || */ lfs_sb_getminfree(fs) > 99) {
344 		pfatal("IMPOSSIBLE MINFREE=%u IN SUPERBLOCK",
345 		    lfs_sb_getminfree(fs));
346 		if (reply("SET TO DEFAULT") == 1) {
347 			lfs_sb_setminfree(fs, 10);
348 			sbdirty();
349 		}
350 	}
351 	if (lfs_sb_getbmask(fs) != lfs_sb_getbsize(fs) - 1) {
352 		pwarn("INCORRECT BMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
353 		    (uintmax_t)lfs_sb_getbmask(fs),
354 		    lfs_sb_getbsize(fs) - 1);
355 		lfs_sb_setbmask(fs, lfs_sb_getbsize(fs) - 1);
356 		if (preen)
357 			printf(" (FIXED)\n");
358 		if (preen || reply("FIX") == 1) {
359 			sbdirty();
360 		}
361 	}
362 	if (lfs_sb_getffmask(fs) != lfs_sb_getfsize(fs) - 1) {
363 		pwarn("INCORRECT FFMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
364 		    (uintmax_t)lfs_sb_getffmask(fs),
365 		    lfs_sb_getfsize(fs) - 1);
366 		lfs_sb_setffmask(fs, lfs_sb_getfsize(fs) - 1);
367 		if (preen)
368 			printf(" (FIXED)\n");
369 		if (preen || reply("FIX") == 1) {
370 			sbdirty();
371 		}
372 	}
373 	if (lfs_sb_getfbmask(fs) != (1U << lfs_sb_getfbshift(fs)) - 1) {
374 		pwarn("INCORRECT FBMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
375 		    (uintmax_t)lfs_sb_getfbmask(fs),
376 		      (1U << lfs_sb_getfbshift(fs)) - 1);
377 		lfs_sb_setfbmask(fs, (1U << lfs_sb_getfbshift(fs)) - 1);
378 		if (preen)
379 			printf(" (FIXED)\n");
380 		if (preen || reply("FIX") == 1) {
381 			sbdirty();
382 		}
383 	}
384 	if (lfs_sb_getmaxfilesize(fs) != maxfilesize) {
385 		pwarn(
386 		    "INCORRECT MAXFILESIZE=%ju IN SUPERBLOCK (SHOULD BE %ju WITH BSHIFT %u)",
387 		    (uintmax_t) lfs_sb_getmaxfilesize(fs),
388 		    (uintmax_t) maxfilesize, lfs_sb_getbshift(fs));
389 		if (preen)
390 			printf(" (FIXED)\n");
391 		if (preen || reply("FIX") == 1) {
392 			lfs_sb_setmaxfilesize(fs, maxfilesize);
393 			sbdirty();
394 		}
395 	}
396 	if (lfs_sb_getmaxsymlinklen(fs) != LFS_MAXSYMLINKLEN(fs)) {
397 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK (SHOULD BE %zu)",
398 		    lfs_sb_getmaxsymlinklen(fs), LFS_MAXSYMLINKLEN(fs));
399 		lfs_sb_setmaxsymlinklen(fs, LFS_MAXSYMLINKLEN(fs));
400 		if (preen)
401 			printf(" (FIXED)\n");
402 		if (preen || reply("FIX") == 1) {
403 			sbdirty();
404 		}
405 	}
406 
407 	/*
408 	 * Read in the Ifile; we'll be using it a lot.
409 	 * XXX If the Ifile is corrupted we are in bad shape.  We need to
410 	 * XXX run through the segment headers of the entire disk to
411 	 * XXX reconstruct the inode table, then pretend all segments are
412 	 * XXX dirty while we do the rest.
413 	 */
414 	ivp = fs->lfs_ivnode;
415 	maxino = ((lfs_dino_getsize(fs, VTOI(ivp)->i_din) - (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
416 		* lfs_sb_getbsize(fs)) / lfs_sb_getbsize(fs)) * lfs_sb_getifpb(fs);
417 	if (debug)
418 		pwarn("maxino    = %llu\n", (unsigned long long)maxino);
419 	for (i = 0; i < lfs_dino_getsize(fs, VTOI(ivp)->i_din); i += lfs_sb_getbsize(fs)) {
420 		bread(ivp, i >> lfs_sb_getbshift(fs), lfs_sb_getbsize(fs), 0, &bp);
421 		/* XXX check B_ERROR */
422 		brelse(bp, 0);
423 	}
424 
425 	/*
426 	 * allocate and initialize the necessary maps
427 	 */
428 	din_table = ecalloc(maxino, sizeof(*din_table));
429 	seg_table = ecalloc(lfs_sb_getnseg(fs), sizeof(SEGUSE));
430 	/* Get segment flags */
431 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
432 		LFS_SEGENTRY(sup, fs, i, bp);
433 		seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
434 		if (preen)
435 			seg_table[i].su_nbytes = sup->su_nbytes;
436 		brelse(bp, 0);
437 	}
438 
439 	/* Initialize Ifile entry */
440 	din_table[LFS_IFILE_INUM] = lfs_sb_getidaddr(fs);
441 	seg_table[lfs_dtosn(fs, lfs_sb_getidaddr(fs))].su_nbytes += DINOSIZE(fs);
442 
443 #ifndef VERBOSE_BLOCKMAP
444 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
445 	blockmap = ecalloc(bmapsize, sizeof(char));
446 #else
447 	bmapsize = maxfsblock * sizeof(ino_t);
448 	blockmap = ecalloc(maxfsblock, sizeof(ino_t));
449 #endif
450 	statemap = ecalloc(maxino, sizeof(char));
451 	typemap = ecalloc(maxino, sizeof(char));
452 	lncntp = ecalloc(maxino, sizeof(int16_t));
453 
454 	if (preen) {
455 		n_files = lfs_sb_getnfiles(fs);
456 		n_blks  = lfs_sb_getdsize(fs) - lfs_sb_getbfree(fs);
457 		numdirs = maxino;
458 		inplast = 0;
459 		listmax = numdirs + 10;
460 		inpsort = ecalloc(listmax, sizeof(struct inoinfo *));
461 		inphead = ecalloc(numdirs, sizeof(struct inoinfo *));
462 	}
463 
464 	return (1);
465 
466 	ckfini(0);
467 	return (0);
468 }
469 
470