setup.c revision 1.41 1 /* $NetBSD: setup.c,v 1.41 2013/06/06 00:54:49 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 _SYS_VNODE_H_ /* XXX */
75 #define vnode uvnode
76 #include <ufs/lfs/ulfs_inode.h>
77 #include <ufs/lfs/ulfsmount.h>
78 #include <ufs/lfs/lfs.h>
79 #undef vnode
80
81 #include <ctype.h>
82 #include <err.h>
83 #include <errno.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <unistd.h>
87 #include <string.h>
88 #include <time.h>
89 #include <util.h>
90
91 #include "bufcache.h"
92 #include "vnode.h"
93 #include "lfs_user.h"
94
95 #include "fsck.h"
96 #include "extern.h"
97 #include "fsutil.h"
98
99 extern u_int32_t cksum(void *, size_t);
100 static uint64_t calcmaxfilesize(int);
101
102 ulfs_daddr_t *din_table;
103 SEGUSE *seg_table;
104
105 #ifdef DKTYPENAMES
106 int useless(void);
107
108 int
109 useless(void)
110 {
111 char **foo = (char **) dktypenames;
112 char **bar = (char **) fscknames;
113
114 return foo - bar;
115 }
116 #endif
117
118 /*
119 * calculate the maximum file size allowed with the specified block shift.
120 */
121 static uint64_t
122 calcmaxfilesize(int bshift)
123 {
124 uint64_t nptr; /* number of block pointers per block */
125 uint64_t maxblock;
126
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((fs->lfs_segtabsz + maxino / fs->lfs_ifpb) << 4);
222
223 if (fs->lfs_pflags & 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%lx\n",
242 (unsigned long)idaddr);
243 tdaddr = sntod(fs, dtosn(fs, idaddr));
244 if (sntod(fs, dtosn(fs, tdaddr)) == tdaddr) {
245 if (tdaddr == fs->lfs_start)
246 tdaddr += btofsb(fs, LFS_LABELPAD);
247 for (i = 0; i < LFS_MAXNUMSB; i++) {
248 if (fs->lfs_sboffs[i] == tdaddr)
249 tdaddr += btofsb(fs, LFS_SBPAD);
250 if (fs->lfs_sboffs[i] > tdaddr)
251 break;
252 }
253 }
254 fs->lfs_offset = tdaddr;
255 if (debug)
256 pwarn("begin with offset/serial 0x%x/%d\n",
257 (int)fs->lfs_offset, (int)fs->lfs_serial);
258 while (tdaddr < idaddr) {
259 bread(fs->lfs_devvp, fsbtodb(fs, tdaddr),
260 fs->lfs_sumsize,
261 NULL, 0, &bp);
262 sp = (SEGSUM *)bp->b_data;
263 if (sp->ss_sumsum != cksum(&sp->ss_datasum,
264 fs->lfs_sumsize -
265 sizeof(sp->ss_sumsum))) {
266 brelse(bp, 0);
267 if (debug)
268 printf("bad cksum at %x\n",
269 (unsigned)tdaddr);
270 break;
271 }
272 fp = (FINFO *)(sp + 1);
273 bc = howmany(sp->ss_ninos, INOPB(fs)) <<
274 (fs->lfs_version > 1 ? fs->lfs_ffshift :
275 fs->lfs_bshift);
276 for (i = 0; i < sp->ss_nfinfo; i++) {
277 bc += fp->fi_lastlength + ((fp->fi_nblocks - 1)
278 << fs->lfs_bshift);
279 fp = (FINFO *)(fp->fi_blocks + fp->fi_nblocks);
280 }
281
282 tdaddr += btofsb(fs, bc) + 1;
283 fs->lfs_offset = tdaddr;
284 fs->lfs_serial = sp->ss_serial + 1;
285 brelse(bp, 0);
286 }
287
288 /*
289 * Set curseg, nextseg appropriately -- inlined from
290 * lfs_newseg()
291 */
292 curseg = dtosn(fs, fs->lfs_offset);
293 fs->lfs_curseg = sntod(fs, curseg);
294 for (sn = curseg + fs->lfs_interleave;;) {
295 sn = (sn + 1) % fs->lfs_nseg;
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 (fs->lfs_offset == fs->lfs_sboffs[i])
309 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
310
311 ++fs->lfs_nactive;
312 fs->lfs_nextseg = sntod(fs, sn);
313 if (debug) {
314 pwarn("offset = 0x%" PRIx32 ", serial = %" PRId64 "\n",
315 fs->lfs_offset, fs->lfs_serial);
316 pwarn("curseg = %" PRIx32 ", nextseg = %" PRIx32 "\n",
317 fs->lfs_curseg, fs->lfs_nextseg);
318 }
319
320 if (!nflag && !skipclean) {
321 fs->lfs_idaddr = idaddr;
322 fsmodified = 1;
323 sbdirty();
324 }
325 }
326
327 if (debug) {
328 pwarn("idaddr = 0x%lx\n", idaddr ? (unsigned long)idaddr :
329 (unsigned long)fs->lfs_idaddr);
330 pwarn("dev_bsize = %lu\n", dev_bsize);
331 pwarn("lfs_bsize = %lu\n", (unsigned long) fs->lfs_bsize);
332 pwarn("lfs_fsize = %lu\n", (unsigned long) fs->lfs_fsize);
333 pwarn("lfs_frag = %lu\n", (unsigned long) fs->lfs_frag);
334 pwarn("lfs_inopb = %lu\n", (unsigned long) fs->lfs_inopb);
335 }
336 if (fs->lfs_version == 1)
337 maxfsblock = fs->lfs_size * (fs->lfs_bsize / dev_bsize);
338 else
339 maxfsblock = fs->lfs_size;
340 maxfilesize = calcmaxfilesize(fs->lfs_bshift);
341 if (/* fs->lfs_minfree < 0 || */ fs->lfs_minfree > 99) {
342 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
343 fs->lfs_minfree);
344 if (reply("SET TO DEFAULT") == 1) {
345 fs->lfs_minfree = 10;
346 sbdirty();
347 }
348 }
349 if (fs->lfs_bmask != fs->lfs_bsize - 1) {
350 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK (SHOULD BE 0x%x)",
351 (unsigned int) fs->lfs_bmask,
352 (unsigned int) fs->lfs_bsize - 1);
353 fs->lfs_bmask = fs->lfs_bsize - 1;
354 if (preen)
355 printf(" (FIXED)\n");
356 if (preen || reply("FIX") == 1) {
357 sbdirty();
358 }
359 }
360 if (fs->lfs_ffmask != fs->lfs_fsize - 1) {
361 pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
362 fs->lfs_ffmask);
363 fs->lfs_ffmask = fs->lfs_fsize - 1;
364 if (preen)
365 printf(" (FIXED)\n");
366 if (preen || reply("FIX") == 1) {
367 sbdirty();
368 }
369 }
370 if (fs->lfs_fbmask != (1 << fs->lfs_fbshift) - 1) {
371 pwarn("INCORRECT FBMASK=%" PRId64 " IN SUPERBLOCK",
372 fs->lfs_fbmask);
373 fs->lfs_fbmask = (1 << fs->lfs_fbshift) - 1;
374 if (preen)
375 printf(" (FIXED)\n");
376 if (preen || reply("FIX") == 1) {
377 sbdirty();
378 }
379 }
380 if (fs->lfs_maxfilesize != maxfilesize) {
381 pwarn(
382 "INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (SHOULD BE %llu WITH BSHIFT %d)",
383 (unsigned long long) fs->lfs_maxfilesize,
384 (unsigned long long) maxfilesize, (int)fs->lfs_bshift);
385 if (preen)
386 printf(" (FIXED)\n");
387 if (preen || reply("FIX") == 1) {
388 fs->lfs_maxfilesize = maxfilesize;
389 sbdirty();
390 }
391 }
392 if (fs->lfs_maxsymlinklen != ULFS1_MAXSYMLINKLEN) {
393 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
394 fs->lfs_maxsymlinklen);
395 fs->lfs_maxsymlinklen = ULFS1_MAXSYMLINKLEN;
396 if (preen)
397 printf(" (FIXED)\n");
398 if (preen || reply("FIX") == 1) {
399 sbdirty();
400 }
401 }
402
403 /*
404 * Read in the Ifile; we'll be using it a lot.
405 * XXX If the Ifile is corrupted we are in bad shape. We need to
406 * XXX run through the segment headers of the entire disk to
407 * XXX reconstruct the inode table, then pretend all segments are
408 * XXX dirty while we do the rest.
409 */
410 ivp = fs->lfs_ivnode;
411 maxino = ((VTOI(ivp)->i_ffs1_size - (fs->lfs_cleansz + fs->lfs_segtabsz)
412 * fs->lfs_bsize) / fs->lfs_bsize) * fs->lfs_ifpb;
413 if (debug)
414 pwarn("maxino = %llu\n", (unsigned long long)maxino);
415 for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += fs->lfs_bsize) {
416 bread(ivp, i >> fs->lfs_bshift, fs->lfs_bsize, NOCRED, 0, &bp);
417 /* XXX check B_ERROR */
418 brelse(bp, 0);
419 }
420
421 /*
422 * allocate and initialize the necessary maps
423 */
424 din_table = ecalloc(maxino, sizeof(*din_table));
425 seg_table = ecalloc(fs->lfs_nseg, sizeof(SEGUSE));
426 /* Get segment flags */
427 for (i = 0; i < fs->lfs_nseg; i++) {
428 LFS_SEGENTRY(sup, fs, i, bp);
429 seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
430 if (preen)
431 seg_table[i].su_nbytes = sup->su_nbytes;
432 brelse(bp, 0);
433 }
434
435 /* Initialize Ifile entry */
436 din_table[fs->lfs_ifile] = fs->lfs_idaddr;
437 seg_table[dtosn(fs, fs->lfs_idaddr)].su_nbytes += DINODE1_SIZE;
438
439 #ifndef VERBOSE_BLOCKMAP
440 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
441 blockmap = ecalloc(bmapsize, sizeof(char));
442 #else
443 bmapsize = maxfsblock * sizeof(ino_t);
444 blockmap = ecalloc(maxfsblock, sizeof(ino_t));
445 #endif
446 statemap = ecalloc(maxino, sizeof(char));
447 typemap = ecalloc(maxino, sizeof(char));
448 lncntp = ecalloc(maxino, sizeof(int16_t));
449
450 if (preen) {
451 n_files = fs->lfs_nfiles;
452 n_blks = fs->lfs_dsize - fs->lfs_bfree;
453 numdirs = maxino;
454 inplast = 0;
455 listmax = numdirs + 10;
456 inpsort = ecalloc(listmax, sizeof(struct inoinfo *));
457 inphead = ecalloc(numdirs, sizeof(struct inoinfo *));
458 }
459
460 return (1);
461
462 ckfini(0);
463 return (0);
464 }
465
466