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