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