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