setup.c revision 1.102 1 /* $NetBSD: setup.c,v 1.102 2018/10/05 09:49:23 hannken Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
36 #else
37 __RCSID("$NetBSD: setup.c,v 1.102 2018/10/05 09:49:23 hannken Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <sys/ioctl.h>
45 #include <sys/file.h>
46 #include <sys/disk.h>
47
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ufs/dir.h>
50 #include <ufs/ufs/ufs_bswap.h>
51 #include <ufs/ufs/quota2.h>
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
54
55 #include <ctype.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61
62 #include "fsck.h"
63 #include "extern.h"
64 #include "fsutil.h"
65 #include "partutil.h"
66 #include "exitvalues.h"
67
68 #define POWEROF2(num) (((num) & ((num) - 1)) == 0)
69
70 static void badsb(int, const char *);
71 static int calcsb(const char *, int, struct fs *);
72 static int readsb(int);
73 #ifndef NO_APPLE_UFS
74 static int readappleufs(void);
75 #endif
76 static int check_snapinum(void);
77
78 int16_t sblkpostbl[256];
79
80 /*
81 * Read in a superblock finding an alternate if necessary.
82 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
83 * is already clean (preen mode only).
84 */
85 int
86 setup(const char *dev, const char *origdev)
87 {
88 long cg, size, asked, i, j;
89 long bmapsize;
90 struct disk_geom geo;
91 struct dkwedge_info dkw;
92 off_t sizepb;
93 struct stat statb;
94 struct fs proto;
95 int doskipclean;
96 u_int64_t maxfilesize;
97 struct csum *ccsp;
98 int fd;
99
100 havesb = 0;
101 fswritefd = -1;
102 doskipclean = skipclean;
103 if (stat(dev, &statb) < 0) {
104 printf("Can't stat %s: %s\n", dev, strerror(errno));
105 return (0);
106 }
107 if (!forceimage && !S_ISCHR(statb.st_mode)) {
108 pfatal("%s is not a character device", dev);
109 if (reply("CONTINUE") == 0)
110 return (0);
111 }
112 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
113 printf("Can't open %s: %s\n", dev, strerror(errno));
114 return (0);
115 }
116 if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
117 fswritefd = -1;
118 if (preen)
119 pfatal("NO WRITE ACCESS");
120 printf("** %s (NO WRITE)\n", dev);
121 quiet = 0;
122 } else
123 if (!preen && !quiet)
124 printf("** %s\n", dev);
125 fsmodified = 0;
126 lfdir = 0;
127 initbarea(&sblk);
128 initbarea(&asblk);
129 sblk.b_un.b_buf = malloc(SBLOCKSIZE);
130 sblock = malloc(SBLOCKSIZE);
131 asblk.b_un.b_buf = malloc(SBLOCKSIZE);
132 altsblock = malloc(SBLOCKSIZE);
133 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL ||
134 sblock == NULL || altsblock == NULL)
135 errexit("Cannot allocate space for superblock");
136 if (strcmp(dev, origdev) && !forceimage) {
137 /*
138 * dev isn't the original fs (for example it's a snapshot)
139 * do getdiskinfo on the original device
140 */
141 fd = open(origdev, O_RDONLY);
142 if (fd < 0) {
143 warn("Can't open %s", origdev);
144 return (0);
145 }
146 } else {
147 fd = fsreadfd;
148 }
149 if (!forceimage && getdiskinfo(origdev, fd, NULL, &geo, &dkw) != -1)
150 dev_bsize = secsize = geo.dg_secsize;
151 else
152 dev_bsize = secsize = DEV_BSIZE;
153 /*
154 * Read in the superblock, looking for alternates if necessary
155 */
156 if (readsb(1) == 0) {
157 if (bflag || preen || forceimage ||
158 calcsb(dev, fsreadfd, &proto) == 0)
159 return(0);
160 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
161 return (0);
162 for (cg = 0; cg < proto.fs_ncg; cg++) {
163 bflag = FFS_FSBTODB(&proto, cgsblock(&proto, cg));
164 if (readsb(0) != 0)
165 break;
166 }
167 if (cg >= proto.fs_ncg) {
168 printf("%s %s\n%s %s\n%s %s\n",
169 "SEARCH FOR ALTERNATE SUPER-BLOCK",
170 "FAILED. YOU MUST USE THE",
171 "-b OPTION TO fsck_ffs TO SPECIFY THE",
172 "LOCATION OF AN ALTERNATE",
173 "SUPER-BLOCK TO SUPPLY NEEDED",
174 "INFORMATION; SEE fsck_ffs(8).");
175 return(0);
176 }
177 doskipclean = 0;
178 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
179 }
180
181 if (!quota2_check_doquota())
182 doskipclean = 0;
183
184 /* ffs_superblock_layout() == 2 */
185 if (sblock->fs_magic != FS_UFS1_MAGIC ||
186 (sblock->fs_old_flags & FS_FLAGS_UPDATED) != 0) {
187 /* can have WAPBL */
188 if (check_wapbl() != 0) {
189 doskipclean = 0;
190 }
191 if (sblock->fs_flags & FS_DOWAPBL) {
192 if (preen && doskipclean) {
193 if (!quiet)
194 pwarn("file system is journaled; "
195 "not checking\n");
196 return (-1);
197 }
198 if (!quiet)
199 pwarn("** File system is journaled; "
200 "replaying journal\n");
201 replay_wapbl();
202 doskipclean = 0;
203 sblock->fs_flags &= ~FS_DOWAPBL;
204 sbdirty();
205 /* Although we may have updated the superblock from
206 * the journal, we are still going to do a full check,
207 * so we don't bother to re-read the superblock from
208 * the journal.
209 * XXX, instead we could re-read the superblock and
210 * then not force doskipclean = 0
211 */
212 }
213 }
214 if (debug)
215 printf("clean = %d\n", sblock->fs_clean);
216
217 if (doswap)
218 doskipclean = 0;
219
220 if (sblock->fs_clean & FS_ISCLEAN) {
221 if (doskipclean) {
222 if (!quiet)
223 pwarn("%sile system is clean; not checking\n",
224 preen ? "f" : "** F");
225 return (-1);
226 }
227 if (!preen && !doswap)
228 pwarn("** File system is already clean\n");
229 }
230 maxfsblock = sblock->fs_size;
231 maxino = sblock->fs_ncg * sblock->fs_ipg;
232 sizepb = sblock->fs_bsize;
233 maxfilesize = sblock->fs_bsize * UFS_NDADDR - 1;
234 for (i = 0; i < UFS_NIADDR; i++) {
235 sizepb *= FFS_NINDIR(sblock);
236 maxfilesize += sizepb;
237 }
238 if ((!is_ufs2 && cvtlevel >= 4) &&
239 (sblock->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
240 if (preen)
241 pwarn("CONVERTING TO NEW SUPERBLOCK LAYOUT\n");
242 else if (!reply("CONVERT TO NEW SUPERBLOCK LAYOUT"))
243 return(0);
244 sblock->fs_old_flags |= FS_FLAGS_UPDATED;
245 /* Disable the postbl tables */
246 sblock->fs_old_cpc = 0;
247 sblock->fs_old_nrpos = 1;
248 sblock->fs_old_trackskew = 0;
249 /* The other fields have already been updated by
250 * sb_oldfscompat_read
251 */
252 sbdirty();
253 }
254 if (!is_ufs2 && cvtlevel == 3 &&
255 (sblock->fs_old_flags & FS_FLAGS_UPDATED)) {
256 if (preen)
257 pwarn("DOWNGRADING TO OLD SUPERBLOCK LAYOUT\n");
258 else if (!reply("DOWNGRADE TO OLD SUPERBLOCK LAYOUT"))
259 return(0);
260 sblock->fs_old_flags &= ~FS_FLAGS_UPDATED;
261 sb_oldfscompat_write(sblock, sblock);
262 sblock->fs_old_flags &= ~FS_FLAGS_UPDATED; /* just in case */
263 /* Leave postbl tables disabled, but blank its superblock region anyway */
264 sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
265 sblock->fs_old_cpc = 0;
266 sblock->fs_old_nrpos = 1;
267 sblock->fs_old_trackskew = 0;
268 memset(&sblock->fs_old_postbl_start, 0xff, 256);
269 sb_oldfscompat_read(sblock, &sblocksave);
270 sbdirty();
271 }
272 /*
273 * Check and potentially fix certain fields in the super block.
274 */
275 if (sblock->fs_flags & ~(FS_KNOWN_FLAGS)) {
276 pfatal("UNKNOWN FLAGS=0x%08x IN SUPERBLOCK", sblock->fs_flags);
277 if (reply("CLEAR") == 1) {
278 sblock->fs_flags &= FS_KNOWN_FLAGS;
279 sbdirty();
280 }
281 }
282 if (sblock->fs_optim != FS_OPTTIME && sblock->fs_optim != FS_OPTSPACE) {
283 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
284 if (reply("SET TO DEFAULT") == 1) {
285 sblock->fs_optim = FS_OPTTIME;
286 sbdirty();
287 }
288 }
289 if ((sblock->fs_minfree < 0 || sblock->fs_minfree > 99)) {
290 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
291 sblock->fs_minfree);
292 if (reply("SET TO DEFAULT") == 1) {
293 sblock->fs_minfree = 10;
294 sbdirty();
295 }
296 }
297 if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
298 (sblock->fs_old_interleave < 1 ||
299 sblock->fs_old_interleave > sblock->fs_old_nsect)) {
300 pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK",
301 sblock->fs_old_interleave);
302 sblock->fs_old_interleave = 1;
303 if (preen)
304 printf(" (FIXED)\n");
305 if (preen || reply("SET TO DEFAULT") == 1) {
306 sbdirty();
307 dirty(&asblk);
308 }
309 }
310 if (!is_ufs2 && sblock->fs_old_postblformat != FS_42POSTBLFMT &&
311 (sblock->fs_old_npsect < sblock->fs_old_nsect ||
312 sblock->fs_old_npsect > sblock->fs_old_nsect*2)) {
313 pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK",
314 sblock->fs_old_npsect);
315 sblock->fs_old_npsect = sblock->fs_old_nsect;
316 if (preen)
317 printf(" (FIXED)\n");
318 if (preen || reply("SET TO DEFAULT") == 1) {
319 sbdirty();
320 dirty(&asblk);
321 }
322 }
323 if (sblock->fs_bmask != ~(sblock->fs_bsize - 1)) {
324 pwarn("INCORRECT BMASK=0x%x IN SUPERBLOCK",
325 sblock->fs_bmask);
326 sblock->fs_bmask = ~(sblock->fs_bsize - 1);
327 if (preen)
328 printf(" (FIXED)\n");
329 if (preen || reply("FIX") == 1) {
330 sbdirty();
331 dirty(&asblk);
332 }
333 }
334 if (sblock->fs_fmask != ~(sblock->fs_fsize - 1)) {
335 pwarn("INCORRECT FMASK=0x%x IN SUPERBLOCK",
336 sblock->fs_fmask);
337 sblock->fs_fmask = ~(sblock->fs_fsize - 1);
338 if (preen)
339 printf(" (FIXED)\n");
340 if (preen || reply("FIX") == 1) {
341 sbdirty();
342 dirty(&asblk);
343 }
344 }
345 if (check_snapinum()) {
346 if (preen)
347 printf(" (FIXED)\n");
348 if (preen || reply("FIX") == 1) {
349 sbdirty();
350 dirty(&asblk);
351 }
352 }
353 if (is_ufs2 || sblock->fs_old_inodefmt >= FS_44INODEFMT) {
354 if (sblock->fs_maxfilesize != maxfilesize) {
355 pwarn("INCORRECT MAXFILESIZE=%lld IN SUPERBLOCK",
356 (unsigned long long)sblock->fs_maxfilesize);
357 sblock->fs_maxfilesize = maxfilesize;
358 if (preen)
359 printf(" (FIXED)\n");
360 if (preen || reply("FIX") == 1) {
361 sbdirty();
362 dirty(&asblk);
363 }
364 }
365 if ((is_ufs2 && sblock->fs_maxsymlinklen != UFS2_MAXSYMLINKLEN)
366 ||
367 (!is_ufs2 && sblock->fs_maxsymlinklen != UFS1_MAXSYMLINKLEN))
368 {
369 pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK",
370 sblock->fs_maxsymlinklen);
371 sblock->fs_maxsymlinklen = is_ufs2 ?
372 UFS2_MAXSYMLINKLEN : UFS1_MAXSYMLINKLEN;
373 if (preen)
374 printf(" (FIXED)\n");
375 if (preen || reply("FIX") == 1) {
376 sbdirty();
377 dirty(&asblk);
378 }
379 }
380 if (sblock->fs_qbmask != ~sblock->fs_bmask) {
381 pwarn("INCORRECT QBMASK=%#llx IN SUPERBLOCK",
382 (unsigned long long)sblock->fs_qbmask);
383 sblock->fs_qbmask = ~sblock->fs_bmask;
384 if (preen)
385 printf(" (FIXED)\n");
386 if (preen || reply("FIX") == 1) {
387 sbdirty();
388 dirty(&asblk);
389 }
390 }
391 if (sblock->fs_qfmask != ~sblock->fs_fmask) {
392 pwarn("INCORRECT QFMASK=%#llx IN SUPERBLOCK",
393 (unsigned long long)sblock->fs_qfmask);
394 sblock->fs_qfmask = ~sblock->fs_fmask;
395 if (preen)
396 printf(" (FIXED)\n");
397 if (preen || reply("FIX") == 1) {
398 sbdirty();
399 dirty(&asblk);
400 }
401 }
402 newinofmt = 1;
403 } else {
404 sblock->fs_qbmask = ~sblock->fs_bmask;
405 sblock->fs_qfmask = ~sblock->fs_fmask;
406 newinofmt = 0;
407 }
408 /*
409 * Convert to new inode format.
410 */
411 if (!is_ufs2 && cvtlevel >= 2 &&
412 sblock->fs_old_inodefmt < FS_44INODEFMT) {
413 if (preen)
414 pwarn("CONVERTING TO NEW INODE FORMAT\n");
415 else if (!reply("CONVERT TO NEW INODE FORMAT"))
416 return(0);
417 doinglevel2++;
418 sblock->fs_old_inodefmt = FS_44INODEFMT;
419 sblock->fs_maxfilesize = maxfilesize;
420 sblock->fs_maxsymlinklen = UFS1_MAXSYMLINKLEN;
421 sblock->fs_qbmask = ~sblock->fs_bmask;
422 sblock->fs_qfmask = ~sblock->fs_fmask;
423 sbdirty();
424 dirty(&asblk);
425 }
426 /*
427 * Convert to new cylinder group format.
428 */
429 if (!is_ufs2 && cvtlevel >= 1 &&
430 sblock->fs_old_postblformat == FS_42POSTBLFMT) {
431 if (preen)
432 pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n");
433 else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT"))
434 return(0);
435 doinglevel1++;
436 sblock->fs_old_postblformat = FS_DYNAMICPOSTBLFMT;
437 sblock->fs_old_nrpos = 8;
438 sblock->fs_old_postbloff =
439 (char *)(&sblock->fs_old_postbl_start) -
440 (char *)(&sblock->fs_firstfield);
441 sblock->fs_old_rotbloff =
442 (char *)(&sblock->fs_magic+1) -
443 (char *)(&sblock->fs_firstfield);
444 sblock->fs_cgsize =
445 ffs_fragroundup(sblock, CGSIZE(sblock));
446 sbdirty();
447 dirty(&asblk);
448 }
449 if (asblk.b_dirty && !bflag) {
450 memmove(sblk.b_un.b_fs, sblock, SBLOCKSIZE);
451 sb_oldfscompat_write(sblk.b_un.b_fs, sblocksave);
452 if (needswap)
453 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
454 memmove(asblk.b_un.b_fs, sblk.b_un.b_fs, (size_t)sblock->fs_sbsize);
455 flush(fswritefd, &asblk);
456 }
457 /*
458 * read in the summary info.
459 */
460 asked = 0;
461 sblock->fs_csp = (struct csum *)calloc(1, sblock->fs_cssize);
462 if (sblock->fs_csp == NULL) {
463 pwarn("cannot alloc %u bytes for summary info\n",
464 sblock->fs_cssize);
465 goto badsblabel;
466 }
467 for (i = 0, j = 0; i < sblock->fs_cssize; i += sblock->fs_bsize, j++) {
468 size = sblock->fs_cssize - i < sblock->fs_bsize ?
469 sblock->fs_cssize - i : sblock->fs_bsize;
470 ccsp = (struct csum *)((char *)sblock->fs_csp + i);
471 if (bread(fsreadfd, (char *)ccsp,
472 FFS_FSBTODB(sblock, sblock->fs_csaddr + j * sblock->fs_frag),
473 size) != 0 && !asked) {
474 pfatal("BAD SUMMARY INFORMATION");
475 if (reply("CONTINUE") == 0) {
476 markclean = 0;
477 exit(FSCK_EXIT_CHECK_FAILED);
478 }
479 asked++;
480 }
481 if (doswap) {
482 ffs_csum_swap(ccsp, ccsp, size);
483 bwrite(fswritefd, (char *)ccsp,
484 FFS_FSBTODB(sblock,
485 sblock->fs_csaddr + j * sblock->fs_frag),
486 size);
487 }
488 if (needswap)
489 ffs_csum_swap(ccsp, ccsp, size);
490 }
491 /*
492 * allocate and initialize the necessary maps
493 */
494 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
495 blockmap = calloc((unsigned)bmapsize, sizeof (char));
496 if (blockmap == NULL) {
497 pwarn("cannot alloc %u bytes for blockmap\n",
498 (unsigned)bmapsize);
499 goto badsblabel;
500 }
501 inostathead = calloc((unsigned)(sblock->fs_ncg),
502 sizeof(struct inostatlist));
503 if (inostathead == NULL) {
504 pwarn("cannot alloc %u bytes for inostathead\n",
505 (unsigned)(sizeof(struct inostatlist) * (sblock->fs_ncg)));
506 goto badsblabel;
507 }
508 /*
509 * cs_ndir may be inaccurate, particularly if we're using the -b
510 * option, so set a minimum to prevent bogus subdirectory reconnects
511 * and really inefficient directory scans.
512 * Also set a maximum in case the value is too large.
513 */
514 numdirs = sblock->fs_cstotal.cs_ndir;
515 if (numdirs < 1024)
516 numdirs = 1024;
517 if ((ino_t)numdirs > maxino + 1)
518 numdirs = maxino + 1;
519 dirhash = numdirs;
520 inplast = 0;
521 listmax = numdirs + 10;
522 inpsort = calloc((unsigned)listmax, sizeof(*inpsort));
523 inphead = calloc((unsigned)numdirs, sizeof(*inphead));
524 if (inpsort == NULL || inphead == NULL) {
525 pwarn("cannot alloc %u bytes for inphead\n",
526 (unsigned)(numdirs * sizeof(struct inoinfo *)));
527 goto badsblabel;
528 }
529 cgrp = malloc(sblock->fs_cgsize);
530 if (cgrp == NULL) {
531 pwarn("cannot alloc %u bytes for cylinder group\n",
532 sblock->fs_cgsize);
533 goto badsblabel;
534 }
535 bufinit();
536 if (sblock->fs_flags & FS_DOSOFTDEP)
537 usedsoftdep = 1;
538 else
539 usedsoftdep = 0;
540
541 #ifndef NO_APPLE_UFS
542 if (!forceimage && dkw.dkw_parent[0])
543 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0)
544 isappleufs = 1;
545
546 if (readappleufs())
547 isappleufs = 1;
548 #endif
549
550 if (isappleufs)
551 dirblksiz = APPLEUFS_DIRBLKSIZ;
552 else
553 dirblksiz = UFS_DIRBLKSIZ;
554
555 if (debug)
556 #ifndef NO_APPLE_UFS
557 printf("isappleufs = %d, dirblksiz = %d\n", isappleufs, dirblksiz);
558 #else
559 printf("dirblksiz = %d\n", dirblksiz);
560 #endif
561
562 if (sblock->fs_flags & FS_DOQUOTA2) {
563 /* allocate the quota hash table */
564 /*
565 * first compute the size of the hash table
566 * We know the smallest block size is 4k, so we can use 2k
567 * for the hash table; as an entry is 8 bytes we can store
568 * 256 entries. So let start q2h_hash_shift at 8
569 */
570 for (q2h_hash_shift = 8;
571 q2h_hash_shift < 15;
572 q2h_hash_shift++) {
573 if ((sizeof(uint64_t) << (q2h_hash_shift + 1)) +
574 sizeof(struct quota2_header) >
575 (size_t)sblock->fs_bsize)
576 break;
577 }
578 q2h_hash_mask = (1 << q2h_hash_shift) - 1;
579 if (debug) {
580 printf("quota hash shift %d, %d entries, mask 0x%x\n",
581 q2h_hash_shift, (1 << q2h_hash_shift),
582 q2h_hash_mask);
583 }
584 uquot_user_hash =
585 calloc((1 << q2h_hash_shift), sizeof(struct uquot_hash));
586 uquot_group_hash =
587 calloc((1 << q2h_hash_shift), sizeof(struct uquot_hash));
588 if (uquot_user_hash == NULL || uquot_group_hash == NULL)
589 errexit("Cannot allocate space for quotas hash\n");
590 } else {
591 uquot_user_hash = uquot_group_hash = NULL;
592 q2h_hash_shift = q2h_hash_mask = 0;
593 }
594 return (1);
595 badsblabel:
596 markclean=0;
597 ckfini(1);
598 return (0);
599 }
600
601 #ifndef NO_APPLE_UFS
602 static int
603 readappleufs(void)
604 {
605 daddr_t label = APPLEUFS_LABEL_OFFSET / dev_bsize;
606 struct appleufslabel *appleufs;
607 int i;
608
609 /* XXX do we have to deal with APPLEUFS_LABEL_OFFSET not
610 * being block aligned (CD's?)
611 */
612 if (APPLEUFS_LABEL_SIZE % dev_bsize != 0)
613 return 0;
614 if (bread(fsreadfd, (char *)appleufsblk.b_un.b_fs, label,
615 (long)APPLEUFS_LABEL_SIZE) != 0)
616 return 0;
617 appleufsblk.b_bno = label;
618 appleufsblk.b_size = APPLEUFS_LABEL_SIZE;
619
620 appleufs = appleufsblk.b_un.b_appleufs;
621
622 if (ntohl(appleufs->ul_magic) != APPLEUFS_LABEL_MAGIC) {
623 if (!isappleufs) {
624 return 0;
625 } else {
626 pfatal("MISSING APPLEUFS VOLUME LABEL\n");
627 if (reply("FIX") == 0) {
628 return 1;
629 }
630 ffs_appleufs_set(appleufs, NULL, -1, 0);
631 appleufsdirty();
632 }
633 }
634
635 if (ntohl(appleufs->ul_version) != APPLEUFS_LABEL_VERSION) {
636 pwarn("INCORRECT APPLE UFS VERSION NUMBER (%d should be %d)",
637 ntohl(appleufs->ul_version),APPLEUFS_LABEL_VERSION);
638 if (preen) {
639 printf(" (CORRECTED)\n");
640 }
641 if (preen || reply("CORRECT")) {
642 appleufs->ul_version = htonl(APPLEUFS_LABEL_VERSION);
643 appleufsdirty();
644 }
645 }
646
647 if (ntohs(appleufs->ul_namelen) > APPLEUFS_MAX_LABEL_NAME) {
648 pwarn("APPLE UFS LABEL NAME TOO LONG");
649 if (preen) {
650 printf(" (TRUNCATED)\n");
651 }
652 if (preen || reply("TRUNCATE")) {
653 appleufs->ul_namelen = htons(APPLEUFS_MAX_LABEL_NAME);
654 appleufsdirty();
655 }
656 }
657
658 if (ntohs(appleufs->ul_namelen) == 0) {
659 pwarn("MISSING APPLE UFS LABEL NAME");
660 if (preen) {
661 printf(" (FIXED)\n");
662 }
663 if (preen || reply("FIX")) {
664 ffs_appleufs_set(appleufs, NULL, -1, 0);
665 appleufsdirty();
666 }
667 }
668
669 /* Scan name for first illegal character */
670 for (i=0;i<ntohs(appleufs->ul_namelen);i++) {
671 if ((appleufs->ul_name[i] == '\0') ||
672 (appleufs->ul_name[i] == ':') ||
673 (appleufs->ul_name[i] == '/')) {
674 pwarn("APPLE UFS LABEL NAME CONTAINS ILLEGAL CHARACTER");
675 if (preen) {
676 printf(" (TRUNCATED)\n");
677 }
678 if (preen || reply("TRUNCATE")) {
679 appleufs->ul_namelen = i+1;
680 appleufsdirty();
681 }
682 break;
683 }
684 }
685
686 /* Check the checksum last, because if anything else was wrong,
687 * then the checksum gets reset anyway.
688 */
689 appleufs->ul_checksum = 0;
690 appleufs->ul_checksum = ffs_appleufs_cksum(appleufs);
691 if (appleufsblk.b_un.b_appleufs->ul_checksum != appleufs->ul_checksum) {
692 pwarn("INVALID APPLE UFS CHECKSUM (%#04x should be %#04x)",
693 appleufsblk.b_un.b_appleufs->ul_checksum, appleufs->ul_checksum);
694 if (preen) {
695 printf(" (CORRECTED)\n");
696 }
697 if (preen || reply("CORRECT")) {
698 appleufsdirty();
699 } else {
700 /* put the incorrect checksum back in place */
701 appleufs->ul_checksum = appleufsblk.b_un.b_appleufs->ul_checksum;
702 }
703 }
704 return 1;
705 }
706 #endif /* !NO_APPLE_UFS */
707
708 /*
709 * Detect byte order. Return 0 if valid magic found, -1 otherwise.
710 */
711 static int
712 detect_byteorder(struct fs *fs, int sblockoff)
713 {
714 if (sblockoff == SBLOCK_UFS2 && (fs->fs_magic == FS_UFS1_MAGIC ||
715 fs->fs_magic == FS_UFS1_MAGIC_SWAPPED))
716 /* Likely to be the first alternate of a fs with 64k blocks */
717 return -1;
718 if (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC) {
719 #ifndef NO_FFS_EI
720 if (endian == 0 || BYTE_ORDER == endian) {
721 needswap = 0;
722 doswap = do_blkswap = do_dirswap = 0;
723 } else {
724 needswap = 1;
725 doswap = do_blkswap = do_dirswap = 1;
726 }
727 #endif
728 return 0;
729 }
730 #ifndef NO_FFS_EI
731 else if (fs->fs_magic == FS_UFS1_MAGIC_SWAPPED ||
732 fs->fs_magic == FS_UFS2_MAGIC_SWAPPED) {
733 if (endian == 0 || BYTE_ORDER != endian) {
734 needswap = 1;
735 doswap = do_blkswap = do_dirswap = 0;
736 } else {
737 needswap = 0;
738 doswap = do_blkswap = do_dirswap = 1;
739 }
740 return 0;
741 }
742 #endif
743 return -1;
744 }
745
746 /*
747 * Possible superblock locations ordered from most to least likely.
748 */
749 static off_t sblock_try[] = SBLOCKSEARCH;
750
751 /*
752 * Read in the super block and its summary info.
753 */
754 static int
755 readsb(int listerr)
756 {
757 daddr_t super = 0;
758 struct fs *fs;
759 int i;
760
761 if (bflag) {
762 super = bflag;
763 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super,
764 (long)SBLOCKSIZE) != 0)
765 return (0);
766 fs = sblk.b_un.b_fs;
767 if (detect_byteorder(fs, -1) < 0) {
768 badsb(listerr, "MAGIC NUMBER WRONG");
769 return (0);
770 }
771 } else {
772 for (i = 0; sblock_try[i] != -1; i++) {
773 super = sblock_try[i] / dev_bsize;
774 if (bread(fsreadfd, (char *)sblk.b_un.b_fs,
775 super, (long)SBLOCKSIZE) != 0)
776 continue;
777 fs = sblk.b_un.b_fs;
778 if (detect_byteorder(fs, sblock_try[i]) == 0)
779 break;
780 }
781 if (sblock_try[i] == -1) {
782 badsb(listerr, "CAN'T FIND SUPERBLOCK");
783 return (0);
784 }
785 }
786 if (doswap) {
787 if (preen)
788 errx(FSCK_EXIT_USAGE,
789 "Incompatible options -B and -p");
790 if (nflag)
791 errx(FSCK_EXIT_USAGE,
792 "Incompatible options -B and -n");
793 if (endian == LITTLE_ENDIAN) {
794 if (!reply("CONVERT TO LITTLE ENDIAN"))
795 return 0;
796 } else if (endian == BIG_ENDIAN) {
797 if (!reply("CONVERT TO BIG ENDIAN"))
798 return 0;
799 } else
800 pfatal("INTERNAL ERROR: unknown endian");
801 }
802 if (needswap)
803 pwarn("** Swapped byte order\n");
804 /* swap SB byte order if asked */
805 if (doswap)
806 ffs_sb_swap(sblk.b_un.b_fs, sblk.b_un.b_fs);
807
808 memmove(sblock, sblk.b_un.b_fs, SBLOCKSIZE);
809 if (needswap)
810 ffs_sb_swap(sblk.b_un.b_fs, sblock);
811
812 is_ufs2 = sblock->fs_magic == FS_UFS2_MAGIC;
813
814 /*
815 * run a few consistency checks of the super block
816 */
817 if (sblock->fs_sbsize > SBLOCKSIZE)
818 { badsb(listerr, "SIZE PREPOSTEROUSLY LARGE"); return (0); }
819 /*
820 * Compute block size that the filesystem is based on,
821 * according to FFS_FSBTODB, and adjust superblock block number
822 * so we can tell if this is an alternate later.
823 */
824 super *= dev_bsize;
825 dev_bsize = sblock->fs_fsize / FFS_FSBTODB(sblock, 1);
826 sblk.b_bno = super / dev_bsize;
827 sblk.b_size = SBLOCKSIZE;
828 if (bflag)
829 goto out;
830 /*
831 * Set all possible fields that could differ, then do check
832 * of whole super block against an alternate super block->
833 * When an alternate super-block is specified this check is skipped.
834 */
835 getblk(&asblk, cgsblock(sblock, sblock->fs_ncg - 1), sblock->fs_sbsize);
836 if (asblk.b_errs)
837 return (0);
838 /* swap SB byte order if asked */
839 if (doswap)
840 ffs_sb_swap(asblk.b_un.b_fs, asblk.b_un.b_fs);
841
842 memmove(altsblock, asblk.b_un.b_fs, sblock->fs_sbsize);
843 if (needswap)
844 ffs_sb_swap(asblk.b_un.b_fs, altsblock);
845 if (cmpsblks(sblock, altsblock)) {
846 if (debug) {
847 uint32_t *nlp, *olp, *endlp;
848
849 printf("superblock mismatches\n");
850 nlp = (uint32_t *)altsblock;
851 olp = (uint32_t *)sblock;
852 endlp = olp + (sblock->fs_sbsize / sizeof *olp);
853 for ( ; olp < endlp; olp++, nlp++) {
854 if (*olp == *nlp)
855 continue;
856 printf("offset %#x, original 0x%08x, alternate "
857 "0x%08x\n",
858 (int)((uint8_t *)olp-(uint8_t *)sblock),
859 *olp, *nlp);
860 }
861 }
862 badsb(listerr,
863 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
864 /*
865 return (0);
866 */
867 }
868 out:
869
870 sb_oldfscompat_read(sblock, &sblocksave);
871
872 /* Now we know the SB is valid, we can write it back if needed */
873 if (doswap) {
874 sbdirty();
875 dirty(&asblk);
876 }
877 havesb = 1;
878 return (1);
879 }
880
881 int
882 cmpsblks(const struct fs *sb, struct fs *asb)
883 {
884 if (!is_ufs2 && ((sb->fs_old_flags & FS_FLAGS_UPDATED) == 0)) {
885 if (sb->fs_old_postblformat < FS_DYNAMICPOSTBLFMT)
886 return cmpsblks42(sb, asb);
887 else
888 return cmpsblks44(sb, asb);
889 }
890 if (asb->fs_sblkno != sb->fs_sblkno ||
891 asb->fs_cblkno != sb->fs_cblkno ||
892 asb->fs_iblkno != sb->fs_iblkno ||
893 asb->fs_dblkno != sb->fs_dblkno ||
894 asb->fs_ncg != sb->fs_ncg ||
895 asb->fs_bsize != sb->fs_bsize ||
896 asb->fs_fsize != sb->fs_fsize ||
897 asb->fs_frag != sb->fs_frag ||
898 asb->fs_bmask != sb->fs_bmask ||
899 asb->fs_fmask != sb->fs_fmask ||
900 asb->fs_bshift != sb->fs_bshift ||
901 asb->fs_fshift != sb->fs_fshift ||
902 asb->fs_fragshift != sb->fs_fragshift ||
903 asb->fs_fsbtodb != sb->fs_fsbtodb ||
904 asb->fs_sbsize != sb->fs_sbsize ||
905 asb->fs_nindir != sb->fs_nindir ||
906 asb->fs_inopb != sb->fs_inopb ||
907 asb->fs_cssize != sb->fs_cssize ||
908 asb->fs_ipg != sb->fs_ipg ||
909 asb->fs_fpg != sb->fs_fpg ||
910 asb->fs_magic != sb->fs_magic)
911 return 1;
912 return 0;
913 }
914
915 /* BSD 4.2 performed the following superblock comparison
916 * It should correspond to FS_42POSTBLFMT
917 * (although note that in 4.2, the fs_old_postblformat
918 * field didn't exist and the corresponding bits are
919 * located near the end of the postbl itself, where they
920 * are not likely to be used.)
921 */
922 int
923 cmpsblks42(const struct fs *sb, struct fs *asb)
924 {
925 asb->fs_firstfield = sb->fs_firstfield; /* fs_link */
926 asb->fs_unused_1 = sb->fs_unused_1; /* fs_rlink */
927 asb->fs_old_time = sb->fs_old_time; /* fs_time */
928 asb->fs_old_cstotal = sb->fs_old_cstotal; /* fs_cstotal */
929 asb->fs_cgrotor = sb->fs_cgrotor;
930 asb->fs_fmod = sb->fs_fmod;
931 asb->fs_clean = sb->fs_clean;
932 asb->fs_ronly = sb->fs_ronly;
933 asb->fs_old_flags = sb->fs_old_flags;
934 asb->fs_maxcontig = sb->fs_maxcontig;
935 asb->fs_minfree = sb->fs_minfree;
936 asb->fs_old_rotdelay = sb->fs_old_rotdelay;
937 asb->fs_maxbpg = sb->fs_maxbpg;
938
939 /* The former fs_csp, totaling 128 bytes */
940 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
941 asb->fs_contigdirs = sb->fs_contigdirs;
942 asb->fs_csp = sb->fs_csp;
943 asb->fs_maxcluster = sb->fs_maxcluster;
944 asb->fs_active = sb->fs_active;
945
946 /* The former fs_fsmnt, totaling 512 bytes */
947 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
948 memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
949
950 return memcmp(sb, asb, sb->fs_sbsize);
951 }
952
953 /* BSD 4.4 performed the following superblock comparison
954 * This was used in NetBSD through 1.6.1
955 *
956 * Note that this implementation is destructive to asb.
957 */
958 int
959 cmpsblks44(const struct fs *sb, struct fs *asb)
960 {
961 /*
962 * "Copy fields which we don't care if they're different in the
963 * alternate superblocks, as they're either likely to be
964 * different because they're per-cylinder-group specific, or
965 * because they're transient details which are only maintained
966 * in the primary superblock."
967 */
968 asb->fs_firstfield = sb->fs_firstfield;
969 asb->fs_unused_1 = sb->fs_unused_1;
970 asb->fs_old_time = sb->fs_old_time;
971 asb->fs_old_cstotal = sb->fs_old_cstotal;
972 asb->fs_cgrotor = sb->fs_cgrotor;
973 asb->fs_fmod = sb->fs_fmod;
974 asb->fs_clean = sb->fs_clean;
975 asb->fs_ronly = sb->fs_ronly;
976 asb->fs_old_flags = sb->fs_old_flags;
977 asb->fs_maxcontig = sb->fs_maxcontig;
978 asb->fs_minfree = sb->fs_minfree;
979 asb->fs_optim = sb->fs_optim;
980 asb->fs_old_rotdelay = sb->fs_old_rotdelay;
981 asb->fs_maxbpg = sb->fs_maxbpg;
982
983 /* The former fs_csp and fs_maxcluster, totaling 128 bytes */
984 memmove(asb->fs_ocsp, sb->fs_ocsp, sizeof sb->fs_ocsp);
985 asb->fs_contigdirs = sb->fs_contigdirs;
986 asb->fs_csp = sb->fs_csp;
987 asb->fs_maxcluster = sb->fs_maxcluster;
988 asb->fs_active = sb->fs_active;
989
990 /* The former fs_fsmnt, totaling 512 bytes */
991 memmove(asb->fs_fsmnt, sb->fs_fsmnt, sizeof sb->fs_fsmnt);
992 memmove(asb->fs_volname, sb->fs_volname, sizeof sb->fs_volname);
993
994 /* The former fs_sparecon, totaling 200 bytes */
995 memmove(asb->fs_snapinum,
996 sb->fs_snapinum, sizeof sb->fs_snapinum);
997 asb->fs_avgfilesize = sb->fs_avgfilesize;
998 asb->fs_avgfpdir = sb->fs_avgfpdir;
999 asb->fs_save_cgsize = sb->fs_save_cgsize;
1000 memmove(asb->fs_sparecon32,
1001 sb->fs_sparecon32, sizeof sb->fs_sparecon32);
1002 asb->fs_flags = sb->fs_flags;
1003
1004 /* Original comment:
1005 * "The following should not have to be copied, but need to be."
1006 */
1007 asb->fs_fsbtodb = sb->fs_fsbtodb;
1008 asb->fs_old_interleave = sb->fs_old_interleave;
1009 asb->fs_old_npsect = sb->fs_old_npsect;
1010 asb->fs_old_nrpos = sb->fs_old_nrpos;
1011 asb->fs_state = sb->fs_state;
1012 asb->fs_qbmask = sb->fs_qbmask;
1013 asb->fs_qfmask = sb->fs_qfmask;
1014 asb->fs_state = sb->fs_state;
1015 asb->fs_maxfilesize = sb->fs_maxfilesize;
1016
1017 /*
1018 * "Compare the superblocks, effectively checking every other
1019 * field to see if they differ."
1020 */
1021 return memcmp(sb, asb, sb->fs_sbsize);
1022 }
1023
1024
1025 static void
1026 badsb(int listerr, const char *s)
1027 {
1028
1029 if (!listerr)
1030 return;
1031 if (preen)
1032 printf("%s: ", cdevname());
1033 pfatal("BAD SUPER BLOCK: %s\n", s);
1034 }
1035
1036 /*
1037 * Calculate a prototype superblock based on information in the disk label.
1038 * When done the cgsblock macro can be calculated and the fs_ncg field
1039 * can be used. Do NOT attempt to use other macros without verifying that
1040 * their needed information is available!
1041 */
1042 static int
1043 calcsb(const char *dev, int devfd, struct fs *fs)
1044 {
1045 struct dkwedge_info dkw;
1046 struct disk_geom geo;
1047 int i, nspf;
1048
1049 if (getdiskinfo(dev, fsreadfd, NULL, &geo, &dkw) == -1)
1050 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
1051 if (dkw.dkw_parent[0] == '\0') {
1052 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
1053 return (0);
1054 }
1055 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_FFS)
1056 #ifndef NO_APPLE_UFS
1057 && strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS)
1058 #endif
1059 ) {
1060 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
1061 dev, dkw.dkw_ptype);
1062 return (0);
1063 }
1064 if (geo.dg_secsize == 0) {
1065 pfatal("%s: CANNOT FIGURE OUT SECTOR SIZE\n", dev);
1066 return 0;
1067 }
1068 if (geo.dg_secpercyl == 0) {
1069 pfatal("%s: CANNOT FIGURE OUT SECTORS PER CYLINDER\n", dev);
1070 return 0;
1071 }
1072 if (sblk.b_un.b_fs->fs_fsize == 0) {
1073 pfatal("%s: CANNOT FIGURE OUT FRAG BLOCK SIZE\n", dev);
1074 return 0;
1075 }
1076 if (sblk.b_un.b_fs->fs_fpg == 0) {
1077 pfatal("%s: CANNOT FIGURE OUT FRAGS PER GROUP\n", dev);
1078 return 0;
1079 }
1080 if (sblk.b_un.b_fs->fs_old_cpg == 0) {
1081 pfatal("%s: CANNOT FIGURE OUT OLD CYLINDERS PER GROUP\n", dev);
1082 return 0;
1083 }
1084 memcpy(fs, sblk.b_un.b_fs, sizeof(struct fs));
1085 nspf = fs->fs_fsize / geo.dg_secsize;
1086 fs->fs_old_nspf = nspf;
1087 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
1088 fs->fs_fsbtodb++;
1089 dev_bsize = geo.dg_secsize;
1090 if (fs->fs_magic == FS_UFS2_MAGIC) {
1091 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
1092 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
1093 fs->fs_old_cgmask = 0xffffffff;
1094 for (i = geo.dg_ntracks; i > 1; i >>= 1)
1095 fs->fs_old_cgmask <<= 1;
1096 if (!POWEROF2(geo.dg_ntracks))
1097 fs->fs_old_cgmask <<= 1;
1098 fs->fs_old_cgoffset = roundup(
1099 howmany(geo.dg_nsectors, nspf), fs->fs_frag);
1100 fs->fs_fpg = (fs->fs_old_cpg * geo.dg_secpercyl) / nspf;
1101 fs->fs_ncg = howmany(fs->fs_size / geo.dg_secpercyl,
1102 fs->fs_old_cpg);
1103 }
1104 return (1);
1105 }
1106
1107 /*
1108 * Test the list of snapshot inode numbers for duplicates and repair.
1109 */
1110 static int
1111 check_snapinum(void)
1112 {
1113 int loc, loc2, res;
1114 int *snapinum = &sblock->fs_snapinum[0];
1115
1116 res = 0;
1117
1118 if (isappleufs)
1119 return 0;
1120
1121 for (loc = 0; loc < FSMAXSNAP; loc++) {
1122 if (snapinum[loc] == 0)
1123 break;
1124 for (loc2 = loc + 1; loc2 < FSMAXSNAP; loc2++) {
1125 if (snapinum[loc2] == 0 ||
1126 snapinum[loc2] == snapinum[loc])
1127 break;
1128 }
1129 if (loc2 >= FSMAXSNAP || snapinum[loc2] == 0)
1130 continue;
1131 pwarn("SNAPSHOT INODE %u ALREADY ON LIST%s", snapinum[loc2],
1132 (res ? "" : "\n"));
1133 res = 1;
1134 for (loc2 = loc + 1; loc2 < FSMAXSNAP; loc2++) {
1135 if (snapinum[loc2] == 0)
1136 break;
1137 snapinum[loc2 - 1] = snapinum[loc2];
1138 }
1139 snapinum[loc2 - 1] = 0;
1140 loc--;
1141 }
1142
1143 return res;
1144 }
1145