setup.c revision 1.54 1 /* $NetBSD: setup.c,v 1.54 2015/08/12 18:27:01 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_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 ulfs_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 /* XXX ondisk32 */
127 nptr = (1 << bshift) / sizeof(uint32_t);
128 maxblock = ULFS_NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
129
130 return maxblock << bshift;
131 }
132
133 void
134 reset_maxino(ino_t len)
135 {
136 if (debug)
137 pwarn("maxino reset from %lld to %lld\n", (long long)maxino,
138 (long long)len);
139
140 din_table = erealloc(din_table, len * sizeof(*din_table));
141 statemap = erealloc(statemap, len * sizeof(char));
142 typemap = erealloc(typemap, len * sizeof(char));
143 lncntp = erealloc(lncntp, len * sizeof(int16_t));
144
145 memset(din_table + maxino, 0, (len - maxino) * sizeof(*din_table));
146 memset(statemap + maxino, USTATE, (len - maxino) * sizeof(char));
147 memset(typemap + maxino, 0, (len - maxino) * sizeof(char));
148 memset(lncntp + maxino, 0, (len - maxino) * sizeof(int16_t));
149
150 maxino = len;
151
152 /*
153 * We can't roll forward after allocating new inodes in previous
154 * phases, or thy would conflict (lost+found, for example, might
155 * disappear to be replaced by a file found in roll-forward).
156 */
157 no_roll_forward = 1;
158
159 return;
160 }
161
162 extern time_t write_time;
163
164 int
165 setup(const char *dev)
166 {
167 long bmapsize;
168 struct stat statb;
169 int doskipclean;
170 u_int64_t maxfilesize;
171 int open_flags;
172 struct uvnode *ivp;
173 struct ubuf *bp;
174 int i, isdirty;
175 long sn, curseg;
176 SEGUSE *sup;
177 size_t sumstart;
178
179 havesb = 0;
180 doskipclean = skipclean;
181 if (stat(dev, &statb) < 0) {
182 pfatal("Can't stat %s: %s\n", dev, strerror(errno));
183 return (0);
184 }
185 if (!S_ISCHR(statb.st_mode) && skipclean) {
186 pfatal("%s is not a character device", dev);
187 if (reply("CONTINUE") == 0)
188 return (0);
189 }
190 if (nflag)
191 open_flags = O_RDONLY;
192 else
193 open_flags = O_RDWR;
194
195 if ((fsreadfd = open(dev, open_flags)) < 0) {
196 pfatal("Can't open %s: %s\n", dev, strerror(errno));
197 return (0);
198 }
199 if (nflag) {
200 if (preen)
201 pfatal("NO WRITE ACCESS");
202 printf("** %s (NO WRITE)\n", dev);
203 quiet = 0;
204 } else if (!preen && !quiet)
205 printf("** %s\n", dev);
206
207 fsmodified = 0;
208 lfdir = 0;
209
210 /* Initialize time in case we have to write */
211 time(&write_time);
212
213 bufinit(0); /* XXX we could make a better guess */
214 fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug);
215 if (fs == NULL) {
216 if (preen)
217 printf("%s: ", cdevname());
218 errexit("BAD SUPER BLOCK OR IFILE INODE NOT FOUND");
219 }
220
221 /* Resize buffer cache now that we have a superblock to guess from. */
222 bufrehash((lfs_sb_getsegtabsz(fs) + maxino / lfs_sb_getifpb(fs)) << 4);
223
224 if (lfs_sb_getpflags(fs) & LFS_PF_CLEAN) {
225 if (doskipclean) {
226 if (!quiet)
227 pwarn("%sile system is clean; not checking\n",
228 preen ? "f" : "** F");
229 return (-1);
230 }
231 if (!preen)
232 pwarn("** File system is already clean\n");
233 }
234
235 if (idaddr) {
236 daddr_t tdaddr;
237 SEGSUM *sp;
238 FINFO *fp;
239 int bc;
240
241 if (debug)
242 pwarn("adjusting offset, serial for -i 0x%jx\n",
243 (uintmax_t)idaddr);
244 tdaddr = lfs_sntod(fs, lfs_dtosn(fs, idaddr));
245 if (lfs_sntod(fs, lfs_dtosn(fs, tdaddr)) == tdaddr) {
246 if (tdaddr == lfs_sb_gets0addr(fs))
247 tdaddr += lfs_btofsb(fs, LFS_LABELPAD);
248 for (i = 0; i < LFS_MAXNUMSB; i++) {
249 if (lfs_sb_getsboff(fs, i) == tdaddr)
250 tdaddr += lfs_btofsb(fs, LFS_SBPAD);
251 if (lfs_sb_getsboff(fs, i) > tdaddr)
252 break;
253 }
254 }
255 lfs_sb_setoffset(fs, tdaddr);
256 if (debug)
257 pwarn("begin with offset/serial 0x%jx/%jd\n",
258 (uintmax_t)lfs_sb_getoffset(fs),
259 (intmax_t)lfs_sb_getserial(fs));
260 while (tdaddr < idaddr) {
261 bread(fs->lfs_devvp, LFS_FSBTODB(fs, tdaddr),
262 lfs_sb_getsumsize(fs),
263 0, &bp);
264 sp = (SEGSUM *)bp->b_data;
265 sumstart = lfs_ss_getsumstart(fs);
266 if (lfs_ss_getsumsum(fs, sp) !=
267 cksum((char *)sp + sumstart,
268 lfs_sb_getsumsize(fs) - sumstart)) {
269 brelse(bp, 0);
270 if (debug)
271 printf("bad cksum at %jx\n",
272 (uintmax_t)tdaddr);
273 break;
274 }
275 fp = SEGSUM_FINFOBASE(fs, sp);
276 bc = howmany(lfs_ss_getninos(fs, sp), LFS_INOPB(fs)) <<
277 (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) :
278 lfs_sb_getbshift(fs));
279 for (i = 0; i < lfs_ss_getnfinfo(fs, sp); i++) {
280 bc += lfs_fi_getlastlength(fs, fp) + ((lfs_fi_getnblocks(fs, fp) - 1)
281 << lfs_sb_getbshift(fs));
282 fp = NEXT_FINFO(fs, fp);
283 }
284
285 tdaddr += lfs_btofsb(fs, bc) + 1;
286 lfs_sb_setoffset(fs, tdaddr);
287 lfs_sb_setserial(fs, lfs_ss_getserial(fs, sp) + 1);
288 brelse(bp, 0);
289 }
290
291 /*
292 * Set curseg, nextseg appropriately -- inlined from
293 * lfs_newseg()
294 */
295 curseg = lfs_dtosn(fs, lfs_sb_getoffset(fs));
296 lfs_sb_setcurseg(fs, lfs_sntod(fs, curseg));
297 for (sn = curseg + lfs_sb_getinterleave(fs);;) {
298 sn = (sn + 1) % lfs_sb_getnseg(fs);
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 (lfs_sb_getoffset(fs) == lfs_sb_getsboff(fs, i))
312 lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
313
314 ++fs->lfs_nactive;
315 lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
316 if (debug) {
317 pwarn("offset = 0x%" PRIx64 ", serial = %" PRIu64 "\n",
318 lfs_sb_getoffset(fs), lfs_sb_getserial(fs));
319 pwarn("curseg = %" PRIx64 ", nextseg = %" PRIx64 "\n",
320 lfs_sb_getcurseg(fs), lfs_sb_getnextseg(fs));
321 }
322
323 if (!nflag && !skipclean) {
324 lfs_sb_setidaddr(fs, idaddr);
325 fsmodified = 1;
326 sbdirty();
327 }
328 }
329
330 if (debug) {
331 pwarn("idaddr = 0x%jx\n", idaddr ? (uintmax_t)idaddr :
332 (uintmax_t)lfs_sb_getidaddr(fs));
333 pwarn("dev_bsize = %lu\n", dev_bsize);
334 pwarn("lfs_bsize = %lu\n", (unsigned long) lfs_sb_getbsize(fs));
335 pwarn("lfs_fsize = %lu\n", (unsigned long) lfs_sb_getfsize(fs));
336 pwarn("lfs_frag = %lu\n", (unsigned long) lfs_sb_getfrag(fs));
337 pwarn("lfs_inopb = %lu\n", (unsigned long) lfs_sb_getinopb(fs));
338 }
339 if (lfs_sb_getversion(fs) == 1)
340 maxfsblock = lfs_blkstofrags(fs, lfs_sb_getsize(fs));
341 else
342 maxfsblock = lfs_sb_getsize(fs);
343 maxfilesize = calcmaxfilesize(lfs_sb_getbshift(fs));
344 if (/* lfs_sb_getminfree(fs) < 0 || */ lfs_sb_getminfree(fs) > 99) {
345 pfatal("IMPOSSIBLE MINFREE=%u IN SUPERBLOCK",
346 lfs_sb_getminfree(fs));
347 if (reply("SET TO DEFAULT") == 1) {
348 lfs_sb_setminfree(fs, 10);
349 sbdirty();
350 }
351 }
352 if (lfs_sb_getbmask(fs) != lfs_sb_getbsize(fs) - 1) {
353 pwarn("INCORRECT BMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
354 (uintmax_t)lfs_sb_getbmask(fs),
355 lfs_sb_getbsize(fs) - 1);
356 lfs_sb_setbmask(fs, lfs_sb_getbsize(fs) - 1);
357 if (preen)
358 printf(" (FIXED)\n");
359 if (preen || reply("FIX") == 1) {
360 sbdirty();
361 }
362 }
363 if (lfs_sb_getffmask(fs) != lfs_sb_getfsize(fs) - 1) {
364 pwarn("INCORRECT FFMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
365 (uintmax_t)lfs_sb_getffmask(fs),
366 lfs_sb_getfsize(fs) - 1);
367 lfs_sb_setffmask(fs, lfs_sb_getfsize(fs) - 1);
368 if (preen)
369 printf(" (FIXED)\n");
370 if (preen || reply("FIX") == 1) {
371 sbdirty();
372 }
373 }
374 if (lfs_sb_getfbmask(fs) != (1U << lfs_sb_getfbshift(fs)) - 1) {
375 pwarn("INCORRECT FBMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
376 (uintmax_t)lfs_sb_getfbmask(fs),
377 (1U << lfs_sb_getfbshift(fs)) - 1);
378 lfs_sb_setfbmask(fs, (1U << lfs_sb_getfbshift(fs)) - 1);
379 if (preen)
380 printf(" (FIXED)\n");
381 if (preen || reply("FIX") == 1) {
382 sbdirty();
383 }
384 }
385 if (lfs_sb_getmaxfilesize(fs) != maxfilesize) {
386 pwarn(
387 "INCORRECT MAXFILESIZE=%ju IN SUPERBLOCK (SHOULD BE %ju WITH BSHIFT %u)",
388 (uintmax_t) lfs_sb_getmaxfilesize(fs),
389 (uintmax_t) maxfilesize, lfs_sb_getbshift(fs));
390 if (preen)
391 printf(" (FIXED)\n");
392 if (preen || reply("FIX") == 1) {
393 lfs_sb_setmaxfilesize(fs, maxfilesize);
394 sbdirty();
395 }
396 }
397 if (lfs_sb_getmaxsymlinklen(fs) != ULFS1_MAXSYMLINKLEN) {
398 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK (SHOULD BE %zu)",
399 lfs_sb_getmaxsymlinklen(fs), ULFS1_MAXSYMLINKLEN);
400 lfs_sb_setmaxsymlinklen(fs, ULFS1_MAXSYMLINKLEN);
401 if (preen)
402 printf(" (FIXED)\n");
403 if (preen || reply("FIX") == 1) {
404 sbdirty();
405 }
406 }
407
408 /*
409 * Read in the Ifile; we'll be using it a lot.
410 * XXX If the Ifile is corrupted we are in bad shape. We need to
411 * XXX run through the segment headers of the entire disk to
412 * XXX reconstruct the inode table, then pretend all segments are
413 * XXX dirty while we do the rest.
414 */
415 ivp = fs->lfs_ivnode;
416 maxino = ((VTOI(ivp)->i_ffs1_size - (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
417 * lfs_sb_getbsize(fs)) / lfs_sb_getbsize(fs)) * lfs_sb_getifpb(fs);
418 if (debug)
419 pwarn("maxino = %llu\n", (unsigned long long)maxino);
420 for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += lfs_sb_getbsize(fs)) {
421 bread(ivp, i >> lfs_sb_getbshift(fs), lfs_sb_getbsize(fs), 0, &bp);
422 /* XXX check B_ERROR */
423 brelse(bp, 0);
424 }
425
426 /*
427 * allocate and initialize the necessary maps
428 */
429 din_table = ecalloc(maxino, sizeof(*din_table));
430 seg_table = ecalloc(lfs_sb_getnseg(fs), sizeof(SEGUSE));
431 /* Get segment flags */
432 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
433 LFS_SEGENTRY(sup, fs, i, bp);
434 seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
435 if (preen)
436 seg_table[i].su_nbytes = sup->su_nbytes;
437 brelse(bp, 0);
438 }
439
440 /* Initialize Ifile entry */
441 din_table[lfs_sb_getifile(fs)] = lfs_sb_getidaddr(fs);
442 seg_table[lfs_dtosn(fs, lfs_sb_getidaddr(fs))].su_nbytes += LFS_DINODE1_SIZE;
443
444 #ifndef VERBOSE_BLOCKMAP
445 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
446 blockmap = ecalloc(bmapsize, sizeof(char));
447 #else
448 bmapsize = maxfsblock * sizeof(ino_t);
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