1 /* $OpenBSD: setup.c,v 1.13 2005/04/30 13:56:16 niallo Exp $ */
2 /* $NetBSD: setup.c,v 1.1 1997/06/11 11:22:01 bouyer Exp $ */
3
4 /*
5 * Copyright (c) 1997 Manuel Bouyer.
6 * Copyright (c) 1980, 1986, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #define DKTYPENAMES
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <ufs/ext2fs/ext2fs_dinode.h>
38 #include <ufs/ext2fs/ext2fs.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/disklabel.h>
42 #include <sys/file.h>
43
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ctype.h>
50
51 #include "fsck.h"
52 #include "extern.h"
53 #include "fsutil.h"
54
55 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
56
57 void badsb(int, char *);
58 int calcsb(char *, int, struct m_ext2fs *);
59 static struct disklabel *getdisklabel(char *, int);
60 static int readsb(int);
61
62 int
setup(char * dev)63 setup(char *dev)
64 {
65 long cg, asked, i;
66 long bmapsize;
67 struct disklabel *lp;
68 off_t sizepb;
69 struct stat statb;
70 struct m_ext2fs proto;
71 int doskipclean;
72 u_int64_t maxfilesize;
73
74 havesb = 0;
75 fswritefd = -1;
76 doskipclean = skipclean;
77 if (stat(dev, &statb) < 0) {
78 printf("Can't stat %s: %s\n", dev, strerror(errno));
79 return (0);
80 }
81 if (!S_ISCHR(statb.st_mode)) {
82 pfatal("%s is not a character device", dev);
83 if (reply("CONTINUE") == 0)
84 return (0);
85 }
86 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
87 printf("Can't open %s: %s\n", dev, strerror(errno));
88 return (0);
89 }
90 if (preen == 0)
91 printf("** %s", dev);
92 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
93 fswritefd = -1;
94 if (preen)
95 pfatal("NO WRITE ACCESS");
96 printf(" (NO WRITE)");
97 }
98 if (preen == 0)
99 printf("\n");
100 fsmodified = 0;
101 lfdir = 0;
102 initbarea(&sblk);
103 initbarea(&asblk);
104 sblk.b_un.b_buf = malloc(SBSIZE);
105 asblk.b_un.b_buf = malloc(SBSIZE);
106 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
107 errexit("cannot allocate space for superblock\n");
108 if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
109 dev_bsize = secsize = lp->d_secsize;
110 else
111 dev_bsize = secsize = DEV_BSIZE;
112 /*
113 * Read in the superblock, looking for alternates if necessary
114 */
115 if (readsb(1) == 0) {
116 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
117 return(0);
118 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
119 return (0);
120 for (cg = 1; cg < proto.e2fs_ncg; cg++) {
121 bflag = fsbtodb(&proto,
122 cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock);
123 if (readsb(0) != 0)
124 break;
125 }
126 if (cg >= proto.e2fs_ncg) {
127 printf("%s %s\n%s %s\n%s %s\n",
128 "SEARCH FOR ALTERNATE SUPER-BLOCK",
129 "FAILED. YOU MUST USE THE",
130 "-b OPTION TO FSCK_FFS TO SPECIFY THE",
131 "LOCATION OF AN ALTERNATE",
132 "SUPER-BLOCK TO SUPPLY NEEDED",
133 "INFORMATION; SEE fsck_ext2fs(8).");
134 return(0);
135 }
136 doskipclean = 0;
137 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
138 }
139 if (debug)
140 printf("state = %d\n", sblock.e2fs.e2fs_state);
141 if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
142 if (doskipclean) {
143 pwarn("%sile system is clean; not checking\n",
144 preen ? "f" : "** F");
145 return (-1);
146 }
147 if (!preen)
148 pwarn("** File system is already clean\n");
149 }
150 maxfsblock = sblock.e2fs.e2fs_bcount;
151 maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
152 sizepb = sblock.e2fs_bsize;
153 maxfilesize = sblock.e2fs_bsize * NDADDR - 1;
154 for (i = 0; i < NIADDR; i++) {
155 sizepb *= NINDIR(&sblock);
156 maxfilesize += sizepb;
157 }
158 /*
159 * Check and potentially fix certain fields in the super block.
160 */
161 if ((sblock.e2fs.e2fs_rbcount < 0) ||
162 (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
163 pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
164 sblock.e2fs.e2fs_rbcount);
165 if (reply("SET TO DEFAULT") == 1) {
166 sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * 0.1;
167 sbdirty();
168 dirty(&asblk);
169 }
170 }
171 if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
172 pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
173 sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
174 return 0;
175 }
176 if (asblk.b_dirty && !bflag) {
177 copyback_sb(&asblk);
178 flush(fswritefd, &asblk);
179 }
180 /*
181 * read in the summary info.
182 */
183
184 sblock.e2fs_gd = calloc(sblock.e2fs_ngdb, sblock.e2fs_bsize);
185 if (sblock.e2fs_gd == NULL)
186 errexit("out of memory\n");
187 asked = 0;
188 for (i=0; i < sblock.e2fs_ngdb; i++) {
189 if (bread(fsreadfd,(char *)
190 &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
191 fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
192 sblock.e2fs_bsize) != 0 && !asked) {
193 pfatal("BAD SUMMARY INFORMATION");
194 if (reply("CONTINUE") == 0)
195 errexit("%s\n", "");
196 asked++;
197 }
198 }
199 /*
200 * allocate and initialize the necessary maps
201 */
202 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
203 blockmap = calloc((unsigned)bmapsize, sizeof (char));
204 if (blockmap == NULL) {
205 printf("cannot alloc %u bytes for blockmap\n",
206 (unsigned)bmapsize);
207 goto badsblabel;
208 }
209 statemap = calloc((unsigned)(maxino + 2), sizeof(char));
210 if (statemap == NULL) {
211 printf("cannot alloc %u bytes for statemap\n",
212 (unsigned)(maxino + 1));
213 goto badsblabel;
214 }
215 typemap = calloc((unsigned)(maxino + 1), sizeof(char));
216 if (typemap == NULL) {
217 printf("cannot alloc %u bytes for typemap\n",
218 (unsigned)(maxino + 1));
219 goto badsblabel;
220 }
221 lncntp = (int16_t *)calloc((unsigned)(maxino + 1), sizeof(int16_t));
222 if (lncntp == NULL) {
223 printf("cannot alloc %u bytes for lncntp\n",
224 (unsigned)((maxino + 1) * sizeof(int16_t)));
225 goto badsblabel;
226 }
227 for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
228 numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
229 }
230 inplast = 0;
231 listmax = numdirs + 10;
232 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
233 sizeof(struct inoinfo *));
234 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
235 sizeof(struct inoinfo *));
236 if (inpsort == NULL || inphead == NULL) {
237 printf("cannot alloc %u bytes for inphead\n",
238 (unsigned)(numdirs * sizeof(struct inoinfo *)));
239 goto badsblabel;
240 }
241 bufinit();
242 return (1);
243
244 badsblabel:
245 ckfini(0);
246 return (0);
247 }
248
249 /*
250 * Read in the super block and its summary info.
251 */
252 static int
readsb(int listerr)253 readsb(int listerr)
254 {
255 daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
256
257 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
258 return (0);
259 sblk.b_bno = super;
260 sblk.b_size = SBSIZE;
261
262 /* Copy the superblock in memory */
263 e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
264
265 /*
266 * run a few consistency checks of the super block
267 */
268 if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
269 badsb(listerr, "MAGIC NUMBER WRONG"); return (0);
270 }
271 if (sblock.e2fs.e2fs_log_bsize > 2) {
272 badsb(listerr, "BAD LOG_BSIZE"); return (0);
273 }
274
275 /* compute the dynamic fields of the in-memory sb */
276 /* compute dynamic sb infos */
277 sblock.e2fs_ncg =
278 howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
279 sblock.e2fs.e2fs_bpg);
280 /* XXX assume hw bsize = 512 */
281 sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
282 sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
283 sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
284 sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
285 sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
286 sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
287 sblock.e2fs_bsize / sizeof(struct ext2_gd));
288 sblock.e2fs_ipb = sblock.e2fs_bsize / sizeof(struct ext2fs_dinode);
289 sblock.e2fs_itpg = sblock.e2fs.e2fs_ipg/sblock.e2fs_ipb;
290
291 /*
292 * Compute block size that the filesystem is based on,
293 * according to fsbtodb, and adjust superblock block number
294 * so we can tell if this is an alternate later.
295 */
296 super *= dev_bsize;
297 dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1);
298 sblk.b_bno = super / dev_bsize;
299
300 getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
301 (long)SBSIZE);
302 if (asblk.b_errs)
303 return (0);
304 if (bflag) {
305 havesb = 1;
306 return (1);
307 }
308
309 /*
310 * Set all possible fields that could differ, then do check
311 * of whole super block against an alternate super block.
312 * When an alternate super-block is specified this check is skipped.
313 */
314 asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
315 asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
316 asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
317 asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
318 asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
319 asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
320 asblk.b_un.b_fs->e2fs_max_mnt_count = sblk.b_un.b_fs->e2fs_max_mnt_count;
321 asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
322 asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
323 asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
324 asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
325 asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
326 asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
327 asblk.b_un.b_fs->e2fs_block_group_nr =
328 sblk.b_un.b_fs->e2fs_block_group_nr;
329 asblk.b_un.b_fs->e2fs_features_rocompat &= ~EXT2F_ROCOMPAT_LARGEFILE;
330 asblk.b_un.b_fs->e2fs_features_rocompat |=
331 sblk.b_un.b_fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE;
332 if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
333 ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
334 (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
335 if (debug) {
336 printf("compat 0x%08x, incompat 0x%08x, compat_ro "
337 "0x%08x\n",
338 sblock.e2fs.e2fs_features_compat,
339 sblock.e2fs.e2fs_features_incompat,
340 sblock.e2fs.e2fs_features_rocompat);
341 }
342 badsb(listerr,"INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
343 return 0;
344 }
345 if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
346 if (debug) {
347 u_int32_t *nlp, *olp, *endlp;
348
349 printf("superblock mismatches\n");
350 nlp = (u_int32_t *)asblk.b_un.b_fs;
351 olp = (u_int32_t *)sblk.b_un.b_fs;
352 endlp = olp + (SBSIZE / sizeof *olp);
353 for ( ; olp < endlp; olp++, nlp++) {
354 if (*olp == *nlp)
355 continue;
356 printf("offset %ld, original %ld, alternate %ld\n",
357 (long)(olp - (u_int32_t *)sblk.b_un.b_fs),
358 (long)fs2h32(*olp),
359 (long)fs2h32(*nlp));
360 }
361 }
362 badsb(listerr,
363 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
364 return (0);
365 }
366 havesb = 1;
367 return (1);
368 }
369
370 void
copyback_sb(struct bufarea * bp)371 copyback_sb(struct bufarea *bp)
372 {
373 /* Copy the in-memory superblock back to buffer */
374 bp->b_un.b_fs->e2fs_icount = fs2h32(sblock.e2fs.e2fs_icount);
375 bp->b_un.b_fs->e2fs_bcount = fs2h32(sblock.e2fs.e2fs_bcount);
376 bp->b_un.b_fs->e2fs_rbcount = fs2h32(sblock.e2fs.e2fs_rbcount);
377 bp->b_un.b_fs->e2fs_fbcount = fs2h32(sblock.e2fs.e2fs_fbcount);
378 bp->b_un.b_fs->e2fs_ficount = fs2h32(sblock.e2fs.e2fs_ficount);
379 bp->b_un.b_fs->e2fs_first_dblock =
380 fs2h32(sblock.e2fs.e2fs_first_dblock);
381 bp->b_un.b_fs->e2fs_log_bsize = fs2h32(sblock.e2fs.e2fs_log_bsize);
382 bp->b_un.b_fs->e2fs_fsize = fs2h32(sblock.e2fs.e2fs_fsize);
383 bp->b_un.b_fs->e2fs_bpg = fs2h32(sblock.e2fs.e2fs_bpg);
384 bp->b_un.b_fs->e2fs_fpg = fs2h32(sblock.e2fs.e2fs_fpg);
385 bp->b_un.b_fs->e2fs_ipg = fs2h32(sblock.e2fs.e2fs_ipg);
386 bp->b_un.b_fs->e2fs_mtime = fs2h32(sblock.e2fs.e2fs_mtime);
387 bp->b_un.b_fs->e2fs_wtime = fs2h32(sblock.e2fs.e2fs_wtime);
388 bp->b_un.b_fs->e2fs_lastfsck = fs2h32(sblock.e2fs.e2fs_lastfsck);
389 bp->b_un.b_fs->e2fs_fsckintv = fs2h32(sblock.e2fs.e2fs_fsckintv);
390 bp->b_un.b_fs->e2fs_creator = fs2h32(sblock.e2fs.e2fs_creator);
391 bp->b_un.b_fs->e2fs_rev = fs2h32(sblock.e2fs.e2fs_rev);
392 bp->b_un.b_fs->e2fs_mnt_count = fs2h16(sblock.e2fs.e2fs_mnt_count);
393 bp->b_un.b_fs->e2fs_max_mnt_count =
394 fs2h16(sblock.e2fs.e2fs_max_mnt_count);
395 bp->b_un.b_fs->e2fs_magic = fs2h16(sblock.e2fs.e2fs_magic);
396 bp->b_un.b_fs->e2fs_state = fs2h16(sblock.e2fs.e2fs_state);
397 bp->b_un.b_fs->e2fs_beh = fs2h16(sblock.e2fs.e2fs_beh);
398 bp->b_un.b_fs->e2fs_ruid = fs2h16(sblock.e2fs.e2fs_ruid);
399 bp->b_un.b_fs->e2fs_rgid = fs2h16(sblock.e2fs.e2fs_rgid);
400 }
401
402 void
badsb(int listerr,char * s)403 badsb(int listerr, char *s)
404 {
405
406 if (!listerr)
407 return;
408 if (preen)
409 printf("%s: ", cdevname());
410 pfatal("BAD SUPER BLOCK: %s\n", s);
411 }
412
413 /*
414 * Calculate a prototype superblock based on information in the disk label.
415 * When done the cgsblock macro can be calculated and the fs_ncg field
416 * can be used. Do NOT attempt to use other macros without verifying that
417 * their needed information is available!
418 */
419
420 int
calcsb(char * dev,int devfd,struct m_ext2fs * fs)421 calcsb(char *dev, int devfd, struct m_ext2fs *fs)
422 {
423 struct disklabel *lp;
424 struct partition *pp;
425 char *cp;
426
427 cp = strchr(dev, '\0') - 1;
428 if ((cp == (char *)-1 || (*cp < 'a' || *cp > 'h')) && !isdigit(*cp)) {
429 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
430 return (0);
431 }
432 lp = getdisklabel(dev, devfd);
433 if (isdigit(*cp))
434 pp = &lp->d_partitions[0];
435 else
436 pp = &lp->d_partitions[*cp - 'a'];
437 if (pp->p_fstype != FS_EXT2FS) {
438 pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
439 dev, pp->p_fstype < FSMAXTYPES ?
440 fstypenames[pp->p_fstype] : "unknown");
441 return (0);
442 }
443 memset(fs, 0, sizeof(struct m_ext2fs));
444 fs->e2fs_bsize = pp->p_fsize;
445 fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024;
446 fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
447 fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
448 fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
449 fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
450 fs->e2fs_qbmask = fs->e2fs_bsize - 1;
451 fs->e2fs_bmask = ~fs->e2fs_qbmask;
452 fs->e2fs_ncg =
453 howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
454 fs->e2fs.e2fs_bpg);
455 fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
456 fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
457 fs->e2fs_bsize / sizeof(struct ext2_gd));
458
459 return (1);
460 }
461
462 static struct disklabel *
getdisklabel(char * s,int fd)463 getdisklabel(char *s, int fd)
464 {
465 static struct disklabel lab;
466
467 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
468 if (s == NULL)
469 return ((struct disklabel *)NULL);
470 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
471 errexit("%s: can't read disk label\n", s);
472 }
473 return (&lab);
474 }
475
476 daddr_t
cgoverhead(int c)477 cgoverhead(int c)
478 {
479 int overh;
480 overh = 1 /* block bitmap */ +
481 1 /* inode bitmap */ +
482 sblock.e2fs_itpg;
483 if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
484 sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
485 if (cg_has_sb(c) == 0)
486 return overh;
487 }
488 overh += 1 + sblock.e2fs_ngdb;
489 return overh;
490 }
491