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