setup.c revision 1.52 1 /* $NetBSD: setup.c,v 1.52 2015/08/02 18:14:16 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
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 if (sp->ss_sumsum != cksum(&sp->ss_datasum,
265 lfs_sb_getsumsize(fs) -
266 sizeof(sp->ss_sumsum))) {
267 brelse(bp, 0);
268 if (debug)
269 printf("bad cksum at %jx\n",
270 (uintmax_t)tdaddr);
271 break;
272 }
273 fp = (FINFO *)(sp + 1);
274 bc = howmany(sp->ss_ninos, LFS_INOPB(fs)) <<
275 (lfs_sb_getversion(fs) > 1 ? lfs_sb_getffshift(fs) :
276 lfs_sb_getbshift(fs));
277 for (i = 0; i < sp->ss_nfinfo; i++) {
278 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
279 << lfs_sb_getbshift(fs));
280 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
281 }
282
283 tdaddr += lfs_btofsb(fs, bc) + 1;
284 lfs_sb_setoffset(fs, tdaddr);
285 lfs_sb_setserial(fs, sp->ss_serial + 1);
286 brelse(bp, 0);
287 }
288
289 /*
290 * Set curseg, nextseg appropriately -- inlined from
291 * lfs_newseg()
292 */
293 curseg = lfs_dtosn(fs, lfs_sb_getoffset(fs));
294 lfs_sb_setcurseg(fs, lfs_sntod(fs, curseg));
295 for (sn = curseg + lfs_sb_getinterleave(fs);;) {
296 sn = (sn + 1) % lfs_sb_getnseg(fs);
297 if (sn == curseg)
298 errx(1, "init: no clean segments");
299 LFS_SEGENTRY(sup, fs, sn, bp);
300 isdirty = sup->su_flags & SEGUSE_DIRTY;
301 brelse(bp, 0);
302
303 if (!isdirty)
304 break;
305 }
306
307 /* Skip superblock if necessary */
308 for (i = 0; i < LFS_MAXNUMSB; i++)
309 if (lfs_sb_getoffset(fs) == lfs_sb_getsboff(fs, i))
310 lfs_sb_addoffset(fs, lfs_btofsb(fs, LFS_SBPAD));
311
312 ++fs->lfs_nactive;
313 lfs_sb_setnextseg(fs, lfs_sntod(fs, sn));
314 if (debug) {
315 pwarn("offset = 0x%" PRIx64 ", serial = %" PRIu64 "\n",
316 lfs_sb_getoffset(fs), lfs_sb_getserial(fs));
317 pwarn("curseg = %" PRIx64 ", nextseg = %" PRIx64 "\n",
318 lfs_sb_getcurseg(fs), lfs_sb_getnextseg(fs));
319 }
320
321 if (!nflag && !skipclean) {
322 lfs_sb_setidaddr(fs, idaddr);
323 fsmodified = 1;
324 sbdirty();
325 }
326 }
327
328 if (debug) {
329 pwarn("idaddr = 0x%jx\n", idaddr ? (uintmax_t)idaddr :
330 (uintmax_t)lfs_sb_getidaddr(fs));
331 pwarn("dev_bsize = %lu\n", dev_bsize);
332 pwarn("lfs_bsize = %lu\n", (unsigned long) lfs_sb_getbsize(fs));
333 pwarn("lfs_fsize = %lu\n", (unsigned long) lfs_sb_getfsize(fs));
334 pwarn("lfs_frag = %lu\n", (unsigned long) lfs_sb_getfrag(fs));
335 pwarn("lfs_inopb = %lu\n", (unsigned long) lfs_sb_getinopb(fs));
336 }
337 if (lfs_sb_getversion(fs) == 1)
338 maxfsblock = lfs_blkstofrags(fs, lfs_sb_getsize(fs));
339 else
340 maxfsblock = lfs_sb_getsize(fs);
341 maxfilesize = calcmaxfilesize(lfs_sb_getbshift(fs));
342 if (/* lfs_sb_getminfree(fs) < 0 || */ lfs_sb_getminfree(fs) > 99) {
343 pfatal("IMPOSSIBLE MINFREE=%u IN SUPERBLOCK",
344 lfs_sb_getminfree(fs));
345 if (reply("SET TO DEFAULT") == 1) {
346 lfs_sb_setminfree(fs, 10);
347 sbdirty();
348 }
349 }
350 if (lfs_sb_getbmask(fs) != lfs_sb_getbsize(fs) - 1) {
351 pwarn("INCORRECT BMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
352 (uintmax_t)lfs_sb_getbmask(fs),
353 lfs_sb_getbsize(fs) - 1);
354 lfs_sb_setbmask(fs, lfs_sb_getbsize(fs) - 1);
355 if (preen)
356 printf(" (FIXED)\n");
357 if (preen || reply("FIX") == 1) {
358 sbdirty();
359 }
360 }
361 if (lfs_sb_getffmask(fs) != lfs_sb_getfsize(fs) - 1) {
362 pwarn("INCORRECT FFMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
363 (uintmax_t)lfs_sb_getffmask(fs),
364 lfs_sb_getfsize(fs) - 1);
365 lfs_sb_setffmask(fs, lfs_sb_getfsize(fs) - 1);
366 if (preen)
367 printf(" (FIXED)\n");
368 if (preen || reply("FIX") == 1) {
369 sbdirty();
370 }
371 }
372 if (lfs_sb_getfbmask(fs) != (1U << lfs_sb_getfbshift(fs)) - 1) {
373 pwarn("INCORRECT FBMASK=0x%jx IN SUPERBLOCK (SHOULD BE 0x%x)",
374 (uintmax_t)lfs_sb_getfbmask(fs),
375 (1U << lfs_sb_getfbshift(fs)) - 1);
376 lfs_sb_setfbmask(fs, (1U << lfs_sb_getfbshift(fs)) - 1);
377 if (preen)
378 printf(" (FIXED)\n");
379 if (preen || reply("FIX") == 1) {
380 sbdirty();
381 }
382 }
383 if (lfs_sb_getmaxfilesize(fs) != maxfilesize) {
384 pwarn(
385 "INCORRECT MAXFILESIZE=%ju IN SUPERBLOCK (SHOULD BE %ju WITH BSHIFT %u)",
386 (uintmax_t) lfs_sb_getmaxfilesize(fs),
387 (uintmax_t) maxfilesize, lfs_sb_getbshift(fs));
388 if (preen)
389 printf(" (FIXED)\n");
390 if (preen || reply("FIX") == 1) {
391 lfs_sb_setmaxfilesize(fs, maxfilesize);
392 sbdirty();
393 }
394 }
395 if (lfs_sb_getmaxsymlinklen(fs) != ULFS1_MAXSYMLINKLEN) {
396 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK (SHOULD BE %zu)",
397 lfs_sb_getmaxsymlinklen(fs), ULFS1_MAXSYMLINKLEN);
398 lfs_sb_setmaxsymlinklen(fs, ULFS1_MAXSYMLINKLEN);
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 - (lfs_sb_getcleansz(fs) + lfs_sb_getsegtabsz(fs))
415 * lfs_sb_getbsize(fs)) / lfs_sb_getbsize(fs)) * lfs_sb_getifpb(fs);
416 if (debug)
417 pwarn("maxino = %llu\n", (unsigned long long)maxino);
418 for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += lfs_sb_getbsize(fs)) {
419 bread(ivp, i >> lfs_sb_getbshift(fs), lfs_sb_getbsize(fs), 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(lfs_sb_getnseg(fs), sizeof(SEGUSE));
429 /* Get segment flags */
430 for (i = 0; i < lfs_sb_getnseg(fs); 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[lfs_sb_getifile(fs)] = lfs_sb_getidaddr(fs);
440 seg_table[lfs_dtosn(fs, lfs_sb_getidaddr(fs))].su_nbytes += LFS_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 = lfs_sb_getnfiles(fs);
455 n_blks = lfs_sb_getdsize(fs) - lfs_sb_getbfree(fs);
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