setup.c revision 1.21 1 /* $NetBSD: setup.c,v 1.21 2005/04/14 21:15:59 perseant 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1980, 1986, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 */
66
67 /* #define DKTYPENAMES */
68 #define FSTYPENAMES
69 #include <sys/types.h>
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <sys/buf.h>
73 #include <sys/mount.h>
74 #include <sys/queue.h>
75 #include <sys/stat.h>
76 #include <sys/ioctl.h>
77 #include <sys/disklabel.h>
78 #include <sys/file.h>
79
80 #include <ufs/ufs/inode.h>
81 #include <ufs/ufs/ufsmount.h>
82 #define vnode uvnode
83 #include <ufs/lfs/lfs.h>
84 #undef vnode
85
86 #include <ctype.h>
87 #include <err.h>
88 #include <errno.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92
93 #include "bufcache.h"
94 #include "vnode.h"
95 #include "lfs.h"
96
97 #include "fsck.h"
98 #include "extern.h"
99 #include "fsutil.h"
100
101 static struct disklabel *getdisklabel(const char *, int);
102 static uint64_t calcmaxfilesize(int);
103
104 ufs_daddr_t *din_table;
105 SEGUSE *seg_table;
106
107 #ifdef DKTYPENAMES
108 int useless(void);
109
110 int
111 useless(void)
112 {
113 char **foo = (char **) dktypenames;
114 char **bar = (char **) fscknames;
115
116 return foo - bar;
117 }
118 #endif
119
120 /*
121 * calculate the maximum file size allowed with the specified block shift.
122 */
123 static uint64_t
124 calcmaxfilesize(int bshift)
125 {
126 uint64_t nptr; /* number of block pointers per block */
127 uint64_t maxblock;
128
129 nptr = (1 << bshift) / sizeof(uint32_t);
130 maxblock = NDADDR + nptr + nptr * nptr + nptr * nptr * nptr;
131
132 return maxblock << bshift;
133 }
134
135 void
136 reset_maxino(ino_t len)
137 {
138 din_table = (ufs_daddr_t *) realloc(din_table, len * sizeof(*din_table));
139 statemap = realloc(statemap, len * sizeof(char));
140 typemap = realloc(typemap, len * sizeof(char));
141 lncntp = (int16_t *) realloc(lncntp, len * sizeof(int16_t));
142
143 if (din_table == NULL || statemap == NULL || typemap == NULL ||
144 lncntp == NULL) {
145 errexit("expanding tables failed: out of memory");
146 }
147
148 memset(din_table + maxino, 0, (len - maxino) * sizeof(*din_table));
149 memset(statemap + maxino, 0, (len - maxino) * sizeof(char));
150 memset(typemap + maxino, 0, (len - maxino) * sizeof(char));
151 memset(lncntp + maxino, 0, (len - maxino) * sizeof(int16_t));
152
153 return;
154 }
155
156 int
157 setup(const char *dev)
158 {
159 long bmapsize;
160 struct disklabel *lp;
161 struct stat statb;
162 int doskipclean;
163 u_int64_t maxfilesize;
164 int open_flags;
165 struct uvnode *ivp;
166 struct ubuf *bp;
167 int i;
168 SEGUSE *sup;
169
170 havesb = 0;
171 doskipclean = skipclean;
172 if (stat(dev, &statb) < 0) {
173 printf("Can't stat %s: %s\n", dev, strerror(errno));
174 return (0);
175 }
176 if (!S_ISCHR(statb.st_mode)) {
177 pfatal("%s is not a character device", dev);
178 if (reply("CONTINUE") == 0)
179 return (0);
180 }
181 if (nflag)
182 open_flags = O_RDONLY;
183 else
184 open_flags = O_RDWR;
185
186 if ((fsreadfd = open(dev, open_flags)) < 0) {
187 printf("Can't open %s: %s\n", dev, strerror(errno));
188 return (0);
189 }
190 if (nflag) {
191 if (preen)
192 pfatal("NO WRITE ACCESS");
193 printf("** %s (NO WRITE)\n", dev);
194 quiet = 0;
195 } else if (!preen && !quiet)
196 printf("** %s\n", dev);
197
198 fsmodified = 0;
199 lfdir = 0;
200
201 bufinit(0); /* XXX we could make a better guess */
202 fs = lfs_init(fsreadfd, bflag, idaddr, 0, debug);
203 if (fs == NULL) {
204 if (preen)
205 printf("%s: ", cdevname());
206 pfatal("BAD SUPER BLOCK\n");
207 }
208 if ((lp = getdisklabel((char *) NULL, fsreadfd)) != NULL)
209 dev_bsize = secsize = lp->d_secsize;
210 else
211 dev_bsize = secsize = DEV_BSIZE;
212
213 /* Resize buffer cache now that we have a superblock to guess from. */
214 bufrehash((fs->lfs_segtabsz + maxino / fs->lfs_ifpb) << 4);
215
216 if (fs->lfs_pflags & LFS_PF_CLEAN) {
217 if (doskipclean) {
218 if (!quiet)
219 pwarn("%sile system is clean; not checking\n",
220 preen ? "f" : "** F");
221 return (-1);
222 }
223 if (!preen)
224 pwarn("** File system is already clean\n");
225 }
226
227 if (debug) {
228 printf("idaddr = 0x%lx\n", idaddr ? (unsigned long)idaddr :
229 (unsigned long)fs->lfs_idaddr);
230 printf("dev_bsize = %lu\n", dev_bsize);
231 printf("lfs_bsize = %lu\n", (unsigned long) fs->lfs_bsize);
232 printf("lfs_fsize = %lu\n", (unsigned long) fs->lfs_fsize);
233 printf("lfs_frag = %lu\n", (unsigned long) fs->lfs_frag);
234 printf("lfs_inopb = %lu\n", (unsigned long) fs->lfs_inopb);
235 }
236 if (fs->lfs_version == 1)
237 maxfsblock = fs->lfs_size * (fs->lfs_bsize / dev_bsize);
238 else
239 maxfsblock = fs->lfs_size;
240 maxfilesize = calcmaxfilesize(fs->lfs_bshift);
241 if ((fs->lfs_minfree < 0 || fs->lfs_minfree > 99)) {
242 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
243 fs->lfs_minfree);
244 if (reply("SET TO DEFAULT") == 1) {
245 fs->lfs_minfree = 10;
246 sbdirty();
247 }
248 }
249 if (fs->lfs_bmask != fs->lfs_bsize - 1) {
250 pwarn("INCORRECT BMASK=%x IN SUPERBLOCK (should be %x)",
251 (unsigned int) fs->lfs_bmask,
252 (unsigned int) fs->lfs_bsize - 1);
253 fs->lfs_bmask = fs->lfs_bsize - 1;
254 if (preen)
255 printf(" (FIXED)\n");
256 if (preen || reply("FIX") == 1) {
257 sbdirty();
258 }
259 }
260 if (fs->lfs_ffmask != fs->lfs_fsize - 1) {
261 pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
262 fs->lfs_ffmask);
263 fs->lfs_ffmask = fs->lfs_fsize - 1;
264 if (preen)
265 printf(" (FIXED)\n");
266 if (preen || reply("FIX") == 1) {
267 sbdirty();
268 }
269 }
270 if (fs->lfs_fbmask != (1 << fs->lfs_fbshift) - 1) {
271 pwarn("INCORRECT FFMASK=%" PRId64 " IN SUPERBLOCK",
272 fs->lfs_ffmask);
273 fs->lfs_fbmask = (1 << fs->lfs_fbshift) - 1;
274 if (preen)
275 printf(" (FIXED)\n");
276 if (preen || reply("FIX") == 1) {
277 sbdirty();
278 }
279 }
280 if (fs->lfs_maxfilesize != maxfilesize) {
281 pwarn(
282 "INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK (should be %llu with bshift %d)",
283 (unsigned long long) fs->lfs_maxfilesize,
284 (unsigned long long) maxfilesize, (int)fs->lfs_bshift);
285 if (preen)
286 printf(" (FIXED)\n");
287 if (preen || reply("FIX") == 1) {
288 fs->lfs_maxfilesize = maxfilesize;
289 sbdirty();
290 }
291 }
292 if (fs->lfs_maxsymlinklen != MAXSYMLINKLEN_UFS1) {
293 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
294 fs->lfs_maxsymlinklen);
295 fs->lfs_maxsymlinklen = MAXSYMLINKLEN_UFS1;
296 if (preen)
297 printf(" (FIXED)\n");
298 if (preen || reply("FIX") == 1) {
299 sbdirty();
300 }
301 }
302
303 /*
304 * Read in the Ifile; we'll be using it a lot.
305 * XXX If the Ifile is corrupted we are in bad shape. We need to
306 * XXX run through the segment headers of the entire disk to
307 * XXX reconstruct the inode table, then pretend all segments are
308 * XXX dirty while we do the rest.
309 */
310 ivp = fs->lfs_ivnode;
311 maxino = ((VTOI(ivp)->i_ffs1_size - (fs->lfs_cleansz + fs->lfs_segtabsz)
312 * fs->lfs_bsize) / fs->lfs_bsize) * fs->lfs_ifpb;
313 if (debug)
314 printf("maxino = %d\n", maxino);
315 for (i = 0; i < VTOI(ivp)->i_ffs1_size; i += fs->lfs_bsize) {
316 bread(ivp, i >> fs->lfs_bshift, fs->lfs_bsize, NOCRED, &bp);
317 /* XXX check B_ERROR */
318 brelse(bp);
319 }
320
321 /*
322 * allocate and initialize the necessary maps
323 */
324 din_table = (ufs_daddr_t *) malloc(maxino * sizeof(*din_table));
325 memset(din_table, 0, maxino * sizeof(*din_table));
326 seg_table = (SEGUSE *) malloc(fs->lfs_nseg * sizeof(SEGUSE));
327 memset(seg_table, 0, fs->lfs_nseg * sizeof(SEGUSE));
328 /* Get segment flags */
329 for (i = 0; i < fs->lfs_nseg; i++) {
330 LFS_SEGENTRY(sup, fs, i, bp);
331 seg_table[i].su_flags = sup->su_flags & ~SEGUSE_ACTIVE;
332 if (preen)
333 seg_table[i].su_nbytes = sup->su_nbytes;
334 brelse(bp);
335 }
336
337 /* Initialize Ifile entry */
338 din_table[fs->lfs_ifile] = fs->lfs_idaddr;
339 seg_table[dtosn(fs, fs->lfs_idaddr)].su_nbytes += DINODE1_SIZE;
340
341 #ifndef VERBOSE_BLOCKMAP
342 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
343 blockmap = calloc((unsigned) bmapsize, sizeof(char));
344 #else
345 bmapsize = maxfsblock * sizeof(ino_t);
346 blockmap = (ino_t *) calloc(maxfsblock, sizeof(ino_t));
347 #endif
348 if (blockmap == NULL) {
349 printf("cannot alloc %u bytes for blockmap\n",
350 (unsigned) bmapsize);
351 goto badsblabel;
352 }
353 statemap = calloc((unsigned) maxino, sizeof(char));
354 if (statemap == NULL) {
355 printf("cannot alloc %u bytes for statemap\n",
356 (unsigned) maxino);
357 goto badsblabel;
358 }
359 typemap = calloc((unsigned) maxino, sizeof(char));
360 if (typemap == NULL) {
361 printf("cannot alloc %u bytes for typemap\n",
362 (unsigned) maxino);
363 goto badsblabel;
364 }
365 lncntp = (int16_t *) calloc((unsigned) maxino, sizeof(int16_t));
366 if (lncntp == NULL) {
367 printf("cannot alloc %lu bytes for lncntp\n",
368 (unsigned long) maxino * sizeof(int16_t));
369 goto badsblabel;
370 }
371
372 if (preen) {
373 n_files = fs->lfs_nfiles;
374 n_blks = fs->lfs_dsize - fs->lfs_bfree;
375 numdirs = maxino;
376 inplast = 0;
377 listmax = numdirs + 10;
378 inpsort = (struct inoinfo **) calloc((unsigned) listmax,
379 sizeof(struct inoinfo *));
380 inphead = (struct inoinfo **) calloc((unsigned) numdirs,
381 sizeof(struct inoinfo *));
382 if (inpsort == NULL || inphead == NULL) {
383 printf("cannot alloc %lu bytes for inphead\n",
384 (unsigned long) numdirs * sizeof(struct inoinfo *));
385 exit(1);
386 }
387 }
388
389 return (1);
390
391 badsblabel:
392 ckfini(0);
393 return (0);
394 }
395
396 static struct disklabel *
397 getdisklabel(const char *s, int fd)
398 {
399 static struct disklabel lab;
400
401 if (ioctl(fd, DIOCGDINFO, (char *) &lab) < 0) {
402 if (s == NULL)
403 return ((struct disklabel *) NULL);
404 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
405 return NULL;
406 }
407 return (&lab);
408 }
409