xref: /netbsd/sbin/fsck_lfs/setup.c (revision 913768d6)
1 /* $NetBSD: setup.c,v 1.62 2020/04/03 19:36:33 joerg 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 "lfs_user.h"
92 
93 #include "fsck.h"
94 #include "extern.h"
95 #include "fsutil.h"
96 
97 static uint64_t calcmaxfilesize(unsigned);
98 
99 daddr_t *din_table;
100 SEGUSE *seg_table;
101 
102 #ifdef DKTYPENAMES
103 int useless(void);
104 
105 int
useless(void)106 useless(void)
107 {
108 	char **foo = (char **) dktypenames;
109 	char **bar = (char **) fscknames;
110 
111 	return foo - bar;
112 }
113 #endif
114 
115 /*
116  * calculate the maximum file size allowed with the specified block shift.
117  */
118 static uint64_t
calcmaxfilesize(unsigned bshift)119 calcmaxfilesize(unsigned bshift)
120 {
121 	uint64_t nptr; /* number of block pointers per block */
122 	uint64_t maxblock;
123 
124 	nptr = (1 << bshift) / LFS_BLKPTRSIZE(fs);
125 	maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
126 
127 	return maxblock << bshift;
128 }
129 
130 void
reset_maxino(ino_t len)131 reset_maxino(ino_t len)
132 {
133 	if (debug)
134 		pwarn("maxino reset from %lld to %lld\n", (long long)maxino,
135 			(long long)len);
136 
137 	din_table = erealloc(din_table, len * sizeof(*din_table));
138 	statemap = erealloc(statemap, len * sizeof(char));
139 	typemap = erealloc(typemap, len * sizeof(char));
140 	lncntp = erealloc(lncntp, len * sizeof(int16_t));
141 
142 	memset(din_table + maxino, 0, (len - maxino) * sizeof(*din_table));
143 	memset(statemap + maxino, USTATE, (len - maxino) * sizeof(char));
144 	memset(typemap + maxino, 0, (len - maxino) * sizeof(char));
145 	memset(lncntp + maxino, 0, (len - maxino) * sizeof(int16_t));
146 
147 	maxino = len;
148 
149 	/*
150 	 * We can't roll forward after allocating new inodes in previous
151 	 * phases, or thy would conflict (lost+found, for example, might
152 	 * disappear to be replaced by a file found in roll-forward).
153 	 */
154 	no_roll_forward = 1;
155 
156 	return;
157 }
158 
159 int
setup(const char * dev)160 setup(const char *dev)
161 {
162 #ifndef VERBOSE_BLOCKMAP
163 	long bmapsize;
164 #endif
165 	struct stat statb;
166 	int doskipclean;
167 	u_int64_t maxfilesize;
168 	int open_flags;
169 	struct uvnode *ivp;
170 	struct ubuf *bp;
171 	int i, isdirty;
172 	long sn, curseg;
173 	SEGUSE *sup;
174 	size_t sumstart;
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%jx\n",
240 				(uintmax_t)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 			sumstart = lfs_ss_getsumstart(fs);
263 			if (lfs_ss_getsumsum(fs, sp) !=
264 			    cksum((char *)sp + sumstart,
265 				  lfs_sb_getsumsize(fs) - sumstart)) {
266 				brelse(bp, 0);
267 				if (debug)
268 					printf("bad cksum at %jx\n",
269 					       (uintmax_t)tdaddr);
270 				break;
271 			}
272 			fp = SEGSUM_FINFOBASE(fs, sp);
273 			bc = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) <<
274 				(lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) :
275 						       lfs_sb_getbshift(fs));
276 			for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) {
277 				bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1)
278 					<< lfs_sb_getbshift(fs));
279 				fp = NEXT_FINFO(fs, fp);
280 			}
281 
282 			tdaddr += lfs_btofsb(fs, bc) + 1;
283 			lfs_sb_setoffset(fs, tdaddr);
284 			lfs_sb_setserial(fs, lfs_ss_getserial(fs, sp) + 1);
285 			brelse(bp, 0);
286 		}
287 
288 		/*
289 		 * Set curseg, nextseg appropriately -- inlined from
290 		 * lfs_newseg()
291 		 */
292 		curseg = lfs_dtosn(fs, lfs_sb_getoffset(fs));
293 		lfs_sb_setcurseg(fs, lfs_sntod(fs, curseg));
294 		for (sn = curseg + lfs_sb_getinterleave(fs);;) {
295 			sn = (sn + 1) % lfs_sb_getnseg(fs);
296 			if (sn == curseg)
297 				errx(1, "init: no clean segments");
298 			LFS_SEGENTRY(sup, fs, sn, bp);
299 			isdirty = sup->su_flags & SEGUSE_DIRTY;
300 			brelse(bp, 0);
301 
302 			if (!isdirty)
303 				break;
304 		}
305 
306 		/* Skip superblock if necessary */
307 		for (i = 0; i < LFS_MAXNUMSB; i++)
308 			if (lfs_sb_getoffset(fs) == lfs_sb_getsboff(fs, i))
309 				lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
310 
311 		++fs->lfs_nactive;
312 		lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
313 		if (debug) {
314 			pwarn("offset = 0x%" PRIx64 ", serial = %" PRIu64 "\n",
315 				lfs_sb_getoffset(fs), lfs_sb_getserial(fs));
316 			pwarn("curseg = %" PRIx64 ", nextseg = %" PRIx64 "\n",
317 				lfs_sb_getcurseg(fs), lfs_sb_getnextseg(fs));
318 		}
319 
320 		if (!nflag && !skipclean) {
321 			lfs_sb_setidaddr(fs, idaddr);
322 			fsmodified = 1;
323 			sbdirty();
324 		}
325 	}
326 
327 	if (debug) {
328 		pwarn("idaddr    = 0x%jx\n", idaddr ? (uintmax_t)idaddr :
329 			(uintmax_t)lfs_sb_getidaddr(fs));
330 		pwarn("dev_bsize = %lu\n", dev_bsize);
331 		pwarn("lfs_bsize = %lu\n", (unsigned long) lfs_sb_getbsize(fs));
332 		pwarn("lfs_fsize = %lu\n", (unsigned long) lfs_sb_getfsize(fs));
333 		pwarn("lfs_frag  = %lu\n", (unsigned long) lfs_sb_getfrag(fs));
334 		pwarn("lfs_inopb = %lu\n", (unsigned long) lfs_sb_getinopb(fs));
335 	}
336 	if (lfs_sb_getversion(fs) == 1)
337 		maxfsblock = lfs_blkstofrags(fs, lfs_sb_getsize(fs));
338 	else
339 		maxfsblock = lfs_sb_getsize(fs);
340 	maxfilesize = calcmaxfilesize(lfs_sb_getbshift(fs));
341 	if (/* lfs_sb_getminfree(fs) < 0 || */ lfs_sb_getminfree(fs) > 99) {
342 		pfatal("IMPOSSIBLE MINFREE=%u IN SUPERBLOCK",
343 		    lfs_sb_getminfree(fs));
344 		if (reply("SET TO DEFAULT") == 1) {
345 			lfs_sb_setminfree(fs, 10);
346 			sbdirty();
347 		}
348 	}
349 	if (lfs_sb_getbmask(fs) != lfs_sb_getbsize(fs) - 1) {
350 		pwarn("INCORRECT BMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
351 		    (uintmax_t)lfs_sb_getbmask(fs),
352 		    lfs_sb_getbsize(fs) - 1);
353 		lfs_sb_setbmask(fs, lfs_sb_getbsize(fs) - 1);
354 		if (preen)
355 			printf(" (FIXED)\n");
356 		if (preen || reply("FIX") == 1) {
357 			sbdirty();
358 		}
359 	}
360 	if (lfs_sb_getffmask(fs) != lfs_sb_getfsize(fs) - 1) {
361 		pwarn("INCORRECT FFMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
362 		    (uintmax_t)lfs_sb_getffmask(fs),
363 		    lfs_sb_getfsize(fs) - 1);
364 		lfs_sb_setffmask(fs, lfs_sb_getfsize(fs) - 1);
365 		if (preen)
366 			printf(" (FIXED)\n");
367 		if (preen || reply("FIX") == 1) {
368 			sbdirty();
369 		}
370 	}
371 	if (lfs_sb_getfbmask(fs) != (1U << lfs_sb_getfbshift(fs)) - 1) {
372 		pwarn("INCORRECT FBMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
373 		    (uintmax_t)lfs_sb_getfbmask(fs),
374 		      (1U << lfs_sb_getfbshift(fs)) - 1);
375 		lfs_sb_setfbmask(fs, (1U << lfs_sb_getfbshift(fs)) - 1);
376 		if (preen)
377 			printf(" (FIXED)\n");
378 		if (preen || reply("FIX") == 1) {
379 			sbdirty();
380 		}
381 	}
382 	if (lfs_sb_getmaxfilesize(fs) != maxfilesize) {
383 		pwarn(
384 		    "INCORRECT MAXFILESIZE=%ju IN SUPERBLOCK (SHOULD BE %ju WITH BSHIFT %u)",
385 		    (uintmax_t) lfs_sb_getmaxfilesize(fs),
386 		    (uintmax_t) maxfilesize, lfs_sb_getbshift(fs));
387 		if (preen)
388 			printf(" (FIXED)\n");
389 		if (preen || reply("FIX") == 1) {
390 			lfs_sb_setmaxfilesize(fs, maxfilesize);
391 			sbdirty();
392 		}
393 	}
394 	if (lfs_sb_getmaxsymlinklen(fs) != LFS_MAXSYMLINKLEN(fs)) {
395 		pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK (SHOULD BE %zu)",
396 		    lfs_sb_getmaxsymlinklen(fs), LFS_MAXSYMLINKLEN(fs));
397 		lfs_sb_setmaxsymlinklen(fs, LFS_MAXSYMLINKLEN(fs));
398 		if (preen)
399 			printf(" (FIXED)\n");
400 		if (preen || reply("FIX") == 1) {
401 			sbdirty();
402 		}
403 	}
404 
405 	/*
406 	 * Read in the Ifile; we'll be using it a lot.
407 	 * XXX If the Ifile is corrupted we are in bad shape.  We need to
408 	 * XXX run through the segment headers of the entire disk to
409 	 * XXX reconstruct the inode table, then pretend all segments are
410 	 * XXX dirty while we do the rest.
411 	 */
412 	ivp = fs->lfs_ivnode;
413 	maxino = ((lfs_dino_getsize(fs, VTOI(ivp)->i_din) - (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
414 		* lfs_sb_getbsize(fs)) / lfs_sb_getbsize(fs)) * lfs_sb_getifpb(fs);
415 	if (debug)
416 		pwarn("maxino    = %llu\n", (unsigned long long)maxino);
417 	for (i = 0; i < lfs_dino_getsize(fs, VTOI(ivp)->i_din); i += lfs_sb_getbsize(fs)) {
418 		bread(ivp, i >> lfs_sb_getbshift(fs), lfs_sb_getbsize(fs), 0, &bp);
419 		/* XXX check B_ERROR */
420 		brelse(bp, 0);
421 	}
422 
423 	/*
424 	 * allocate and initialize the necessary maps
425 	 */
426 	din_table = ecalloc(maxino, sizeof(*din_table));
427 	seg_table = ecalloc(lfs_sb_getnseg(fs), sizeof(SEGUSE));
428 	/* Get segment flags */
429 	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
430 		LFS_SEGENTRY(sup, fs, i, bp);
431 		seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
432 		if (preen)
433 			seg_table[i].su_nbytes = sup->su_nbytes;
434 		brelse(bp, 0);
435 	}
436 
437 	/* Initialize Ifile entry */
438 	din_table[LFS_IFILE_INUM] = lfs_sb_getidaddr(fs);
439 	seg_table[lfs_dtosn(fs, lfs_sb_getidaddr(fs))].su_nbytes += DINOSIZE(fs);
440 
441 #ifndef VERBOSE_BLOCKMAP
442 	bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
443 	blockmap = ecalloc(bmapsize, sizeof(char));
444 #else
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