Deleted Added
full compact
setup.c (100935) setup.c (101037)
1/*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
37#endif
38static const char rcsid[] =
1/*
2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static const char sccsid[] = "@(#)setup.c 8.10 (Berkeley) 5/9/95";
37#endif
38static const char rcsid[] =
39 "$FreeBSD: head/sbin/fsck_ffs/setup.c 100935 2002-07-30 13:01:25Z phk $";
39 "$FreeBSD: head/sbin/fsck_ffs/setup.c 101037 2002-07-31 12:01:14Z mux $";
40#endif /* not lint */
41
42#define DKTYPENAMES
43#include <sys/param.h>
44#include <sys/stat.h>
45#include <sys/disklabel.h>
46#include <sys/file.h>
47#include <sys/sysctl.h>
48
49#include <ufs/ufs/dinode.h>
50#include <ufs/ffs/fs.h>
51
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
40#endif /* not lint */
41
42#define DKTYPENAMES
43#include <sys/param.h>
44#include <sys/stat.h>
45#include <sys/disklabel.h>
46#include <sys/file.h>
47#include <sys/sysctl.h>
48
49#include <ufs/ufs/dinode.h>
50#include <ufs/ffs/fs.h>
51
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
55#include <stdint.h>
55#include <string.h>
56
57#include "fsck.h"
58
59struct bufarea asblk;
60#define altsblock (*asblk.b_un.b_fs)
61#define POWEROF2(num) (((num) & ((num) - 1)) == 0)
62
63static void badsb(int listerr, const char *s);
64static int calcsb(char *dev, int devfd, struct fs *fs);
65static struct disklabel *getdisklabel(char *s, int fd);
66
67/*
68 * Read in a superblock finding an alternate if necessary.
69 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
70 * is already clean (preen mode only).
71 */
72int
73setup(char *dev)
74{
75 long cg, asked, i, j;
76 long bmapsize;
77 struct stat statb;
78 struct fs proto;
79 size_t size;
80
81 havesb = 0;
82 fswritefd = -1;
83 cursnapshot = 0;
84 if (stat(dev, &statb) < 0) {
85 printf("Can't stat %s: %s\n", dev, strerror(errno));
86 if (bkgrdflag) {
87 unlink(snapname);
88 bkgrdflag = 0;
89 }
90 return (0);
91 }
92 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
93 (statb.st_mode & S_IFMT) != S_IFBLK) {
94 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
95 unlink(snapname);
96 printf("background fsck lacks a snapshot\n");
97 exit(EEXIT);
98 }
99 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
100 cursnapshot = statb.st_ino;
101 } else {
102 if (cvtlevel == 0 ||
103 (statb.st_flags & SF_SNAPSHOT) == 0) {
104 if (preen && bkgrdflag) {
105 unlink(snapname);
106 bkgrdflag = 0;
107 }
108 pfatal("%s is not a disk device", dev);
109 if (reply("CONTINUE") == 0) {
110 if (bkgrdflag) {
111 unlink(snapname);
112 bkgrdflag = 0;
113 }
114 return (0);
115 }
116 } else {
117 if (bkgrdflag) {
118 unlink(snapname);
119 bkgrdflag = 0;
120 }
121 pfatal("cannot convert a snapshot");
122 exit(EEXIT);
123 }
124 }
125 }
126 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
127 if (bkgrdflag) {
128 unlink(snapname);
129 bkgrdflag = 0;
130 }
131 printf("Can't open %s: %s\n", dev, strerror(errno));
132 return (0);
133 }
134 if (bkgrdflag) {
135 unlink(snapname);
136 size = MIBSIZE;
137 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
138 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
139 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
140 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
141 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
142 pfatal("kernel lacks background fsck support\n");
143 exit(EEXIT);
144 }
145 cmd.version = FFS_CMD_VERSION;
146 cmd.handle = fsreadfd;
147 fswritefd = -1;
148 }
149 if (preen == 0)
150 printf("** %s", dev);
151 if (bkgrdflag == 0 &&
152 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
153 fswritefd = -1;
154 if (preen)
155 pfatal("NO WRITE ACCESS");
156 printf(" (NO WRITE)");
157 }
158 if (preen == 0)
159 printf("\n");
160 /*
161 * Read in the superblock, looking for alternates if necessary
162 */
163 if (readsb(1) == 0) {
164 skipclean = 0;
165 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
166 return(0);
167 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
168 return (0);
169 for (cg = 0; cg < proto.fs_ncg; cg++) {
170 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
171 if (readsb(0) != 0)
172 break;
173 }
174 if (cg >= proto.fs_ncg) {
175 printf("%s %s\n%s %s\n%s %s\n",
176 "SEARCH FOR ALTERNATE SUPER-BLOCK",
177 "FAILED. YOU MUST USE THE",
178 "-b OPTION TO FSCK TO SPECIFY THE",
179 "LOCATION OF AN ALTERNATE",
180 "SUPER-BLOCK TO SUPPLY NEEDED",
181 "INFORMATION; SEE fsck(8).");
182 bflag = 0;
183 return(0);
184 }
185 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
186 bflag = 0;
187 }
188 if (skipclean && preen && sblock.fs_clean) {
189 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
190 return (-1);
191 }
192 maxfsblock = sblock.fs_size;
193 maxino = sblock.fs_ncg * sblock.fs_ipg;
194 /*
195 * Check and potentially fix certain fields in the super block.
196 */
197 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
198 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
199 if (reply("SET TO DEFAULT") == 1) {
200 sblock.fs_optim = FS_OPTTIME;
201 sbdirty();
202 }
203 }
204 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
205 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
206 sblock.fs_minfree);
207 if (reply("SET TO DEFAULT") == 1) {
208 sblock.fs_minfree = 10;
209 sbdirty();
210 }
211 }
212 if (sblock.fs_magic == FS_UFS1_MAGIC &&
213 sblock.fs_old_inodefmt < FS_44INODEFMT) {
214 pwarn("Format of filesystem is too old.\n");
215 pwarn("Must update to modern format using a version of fsck\n");
216 pfatal("from before 2002 with the command ``fsck -c 2''\n");
217 exit(EEXIT);
218 }
219 if (asblk.b_dirty && !bflag) {
220 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
221 flush(fswritefd, &asblk);
222 }
223 /*
224 * read in the summary info.
225 */
226 asked = 0;
227 sblock.fs_csp = calloc(1, sblock.fs_cssize);
228 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
229 size = sblock.fs_cssize - i < sblock.fs_bsize ?
230 sblock.fs_cssize - i : sblock.fs_bsize;
231 if (bread(fsreadfd, (char *)sblock.fs_csp + i,
232 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
233 size) != 0 && !asked) {
234 pfatal("BAD SUMMARY INFORMATION");
235 if (reply("CONTINUE") == 0) {
236 ckfini(0);
237 exit(EEXIT);
238 }
239 asked++;
240 }
241 }
242 /*
243 * allocate and initialize the necessary maps
244 */
245 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
246 blockmap = calloc((unsigned)bmapsize, sizeof (char));
247 if (blockmap == NULL) {
248 printf("cannot alloc %u bytes for blockmap\n",
249 (unsigned)bmapsize);
250 goto badsb;
251 }
252 inostathead = calloc((unsigned)(sblock.fs_ncg),
253 sizeof(struct inostatlist));
254 if (inostathead == NULL) {
255 printf("cannot alloc %u bytes for inostathead\n",
256 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
257 goto badsb;
258 }
259 numdirs = sblock.fs_cstotal.cs_ndir;
260 dirhash = numdirs;
261 if (numdirs == 0) {
262 printf("numdirs is zero, try using an alternate superblock\n");
263 goto badsb;
264 }
265 inplast = 0;
266 listmax = numdirs + 10;
267 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
268 sizeof(struct inoinfo *));
269 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
270 sizeof(struct inoinfo *));
271 if (inpsort == NULL || inphead == NULL) {
56#include <string.h>
57
58#include "fsck.h"
59
60struct bufarea asblk;
61#define altsblock (*asblk.b_un.b_fs)
62#define POWEROF2(num) (((num) & ((num) - 1)) == 0)
63
64static void badsb(int listerr, const char *s);
65static int calcsb(char *dev, int devfd, struct fs *fs);
66static struct disklabel *getdisklabel(char *s, int fd);
67
68/*
69 * Read in a superblock finding an alternate if necessary.
70 * Return 1 if successful, 0 if unsuccessful, -1 if filesystem
71 * is already clean (preen mode only).
72 */
73int
74setup(char *dev)
75{
76 long cg, asked, i, j;
77 long bmapsize;
78 struct stat statb;
79 struct fs proto;
80 size_t size;
81
82 havesb = 0;
83 fswritefd = -1;
84 cursnapshot = 0;
85 if (stat(dev, &statb) < 0) {
86 printf("Can't stat %s: %s\n", dev, strerror(errno));
87 if (bkgrdflag) {
88 unlink(snapname);
89 bkgrdflag = 0;
90 }
91 return (0);
92 }
93 if ((statb.st_mode & S_IFMT) != S_IFCHR &&
94 (statb.st_mode & S_IFMT) != S_IFBLK) {
95 if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) {
96 unlink(snapname);
97 printf("background fsck lacks a snapshot\n");
98 exit(EEXIT);
99 }
100 if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) {
101 cursnapshot = statb.st_ino;
102 } else {
103 if (cvtlevel == 0 ||
104 (statb.st_flags & SF_SNAPSHOT) == 0) {
105 if (preen && bkgrdflag) {
106 unlink(snapname);
107 bkgrdflag = 0;
108 }
109 pfatal("%s is not a disk device", dev);
110 if (reply("CONTINUE") == 0) {
111 if (bkgrdflag) {
112 unlink(snapname);
113 bkgrdflag = 0;
114 }
115 return (0);
116 }
117 } else {
118 if (bkgrdflag) {
119 unlink(snapname);
120 bkgrdflag = 0;
121 }
122 pfatal("cannot convert a snapshot");
123 exit(EEXIT);
124 }
125 }
126 }
127 if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
128 if (bkgrdflag) {
129 unlink(snapname);
130 bkgrdflag = 0;
131 }
132 printf("Can't open %s: %s\n", dev, strerror(errno));
133 return (0);
134 }
135 if (bkgrdflag) {
136 unlink(snapname);
137 size = MIBSIZE;
138 if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0||
139 sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0||
140 sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0||
141 sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 ||
142 sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) {
143 pfatal("kernel lacks background fsck support\n");
144 exit(EEXIT);
145 }
146 cmd.version = FFS_CMD_VERSION;
147 cmd.handle = fsreadfd;
148 fswritefd = -1;
149 }
150 if (preen == 0)
151 printf("** %s", dev);
152 if (bkgrdflag == 0 &&
153 (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) {
154 fswritefd = -1;
155 if (preen)
156 pfatal("NO WRITE ACCESS");
157 printf(" (NO WRITE)");
158 }
159 if (preen == 0)
160 printf("\n");
161 /*
162 * Read in the superblock, looking for alternates if necessary
163 */
164 if (readsb(1) == 0) {
165 skipclean = 0;
166 if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
167 return(0);
168 if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
169 return (0);
170 for (cg = 0; cg < proto.fs_ncg; cg++) {
171 bflag = fsbtodb(&proto, cgsblock(&proto, cg));
172 if (readsb(0) != 0)
173 break;
174 }
175 if (cg >= proto.fs_ncg) {
176 printf("%s %s\n%s %s\n%s %s\n",
177 "SEARCH FOR ALTERNATE SUPER-BLOCK",
178 "FAILED. YOU MUST USE THE",
179 "-b OPTION TO FSCK TO SPECIFY THE",
180 "LOCATION OF AN ALTERNATE",
181 "SUPER-BLOCK TO SUPPLY NEEDED",
182 "INFORMATION; SEE fsck(8).");
183 bflag = 0;
184 return(0);
185 }
186 pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
187 bflag = 0;
188 }
189 if (skipclean && preen && sblock.fs_clean) {
190 pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n");
191 return (-1);
192 }
193 maxfsblock = sblock.fs_size;
194 maxino = sblock.fs_ncg * sblock.fs_ipg;
195 /*
196 * Check and potentially fix certain fields in the super block.
197 */
198 if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) {
199 pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK");
200 if (reply("SET TO DEFAULT") == 1) {
201 sblock.fs_optim = FS_OPTTIME;
202 sbdirty();
203 }
204 }
205 if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) {
206 pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK",
207 sblock.fs_minfree);
208 if (reply("SET TO DEFAULT") == 1) {
209 sblock.fs_minfree = 10;
210 sbdirty();
211 }
212 }
213 if (sblock.fs_magic == FS_UFS1_MAGIC &&
214 sblock.fs_old_inodefmt < FS_44INODEFMT) {
215 pwarn("Format of filesystem is too old.\n");
216 pwarn("Must update to modern format using a version of fsck\n");
217 pfatal("from before 2002 with the command ``fsck -c 2''\n");
218 exit(EEXIT);
219 }
220 if (asblk.b_dirty && !bflag) {
221 memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize);
222 flush(fswritefd, &asblk);
223 }
224 /*
225 * read in the summary info.
226 */
227 asked = 0;
228 sblock.fs_csp = calloc(1, sblock.fs_cssize);
229 for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
230 size = sblock.fs_cssize - i < sblock.fs_bsize ?
231 sblock.fs_cssize - i : sblock.fs_bsize;
232 if (bread(fsreadfd, (char *)sblock.fs_csp + i,
233 fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
234 size) != 0 && !asked) {
235 pfatal("BAD SUMMARY INFORMATION");
236 if (reply("CONTINUE") == 0) {
237 ckfini(0);
238 exit(EEXIT);
239 }
240 asked++;
241 }
242 }
243 /*
244 * allocate and initialize the necessary maps
245 */
246 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short));
247 blockmap = calloc((unsigned)bmapsize, sizeof (char));
248 if (blockmap == NULL) {
249 printf("cannot alloc %u bytes for blockmap\n",
250 (unsigned)bmapsize);
251 goto badsb;
252 }
253 inostathead = calloc((unsigned)(sblock.fs_ncg),
254 sizeof(struct inostatlist));
255 if (inostathead == NULL) {
256 printf("cannot alloc %u bytes for inostathead\n",
257 (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg)));
258 goto badsb;
259 }
260 numdirs = sblock.fs_cstotal.cs_ndir;
261 dirhash = numdirs;
262 if (numdirs == 0) {
263 printf("numdirs is zero, try using an alternate superblock\n");
264 goto badsb;
265 }
266 inplast = 0;
267 listmax = numdirs + 10;
268 inpsort = (struct inoinfo **)calloc((unsigned)listmax,
269 sizeof(struct inoinfo *));
270 inphead = (struct inoinfo **)calloc((unsigned)numdirs,
271 sizeof(struct inoinfo *));
272 if (inpsort == NULL || inphead == NULL) {
272 printf("cannot alloc %u bytes for inphead\n",
273 (unsigned)numdirs * sizeof(struct inoinfo *));
273 printf("cannot alloc %ju bytes for inphead\n",
274 (uintmax_t)numdirs * sizeof(struct inoinfo *));
274 goto badsb;
275 }
276 bufinit();
277 if (sblock.fs_flags & FS_DOSOFTDEP)
278 usedsoftdep = 1;
279 else
280 usedsoftdep = 0;
281 return (1);
282
283badsb:
284 ckfini(0);
285 return (0);
286}
287
288/*
289 * Possible superblock locations ordered from most to least likely.
290 */
291static int sblock_try[] = SBLOCKSEARCH;
292
293/*
294 * Read in the super block and its summary info.
295 */
296int
297readsb(int listerr)
298{
299 ufs2_daddr_t super;
300 int i;
301
302 if (bflag) {
303 super = bflag;
304 if ((bread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
305 return (0);
306 } else {
307 for (i = 0; sblock_try[i] != -1; i++) {
308 super = sblock_try[i] / dev_bsize;
309 if ((bread(fsreadfd, (char *)&sblock, super,
310 (long)SBLOCKSIZE)))
311 return (0);
312 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
313 (sblock.fs_magic == FS_UFS2_MAGIC &&
314 sblock.fs_sblockloc ==
315 numfrags(&sblock, sblock_try[i]))) &&
316 sblock.fs_ncg >= 1 &&
317 sblock.fs_bsize >= SBLOCKSIZE &&
318 sblock.fs_bsize >= sizeof(struct fs))
319 break;
320 }
321 if (sblock_try[i] == -1) {
322 fprintf(stderr, "Cannot find filesystem superblock\n");
323 return (0);
324 }
325 }
326 /*
327 * Compute block size that the filesystem is based on,
328 * according to fsbtodb, and adjust superblock block number
329 * so we can tell if this is an alternate later.
330 */
331 super *= dev_bsize;
332 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
333 sblk.b_bno = super / dev_bsize;
334 sblk.b_size = SBLOCKSIZE;
335 if (bflag) {
336 havesb = 1;
337 return (1);
338 }
339 /*
340 * Compare all fields that should not differ in alternate super block.
341 * When an alternate super-block is specified this check is skipped.
342 */
343 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
344 if (asblk.b_errs)
345 return (0);
346 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
347 altsblock.fs_cblkno != sblock.fs_cblkno ||
348 altsblock.fs_iblkno != sblock.fs_iblkno ||
349 altsblock.fs_dblkno != sblock.fs_dblkno ||
350 altsblock.fs_ncg != sblock.fs_ncg ||
351 altsblock.fs_bsize != sblock.fs_bsize ||
352 altsblock.fs_fsize != sblock.fs_fsize ||
353 altsblock.fs_frag != sblock.fs_frag ||
354 altsblock.fs_bmask != sblock.fs_bmask ||
355 altsblock.fs_fmask != sblock.fs_fmask ||
356 altsblock.fs_bshift != sblock.fs_bshift ||
357 altsblock.fs_fshift != sblock.fs_fshift ||
358 altsblock.fs_fragshift != sblock.fs_fragshift ||
359 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
360 altsblock.fs_sbsize != sblock.fs_sbsize ||
361 altsblock.fs_nindir != sblock.fs_nindir ||
362 altsblock.fs_inopb != sblock.fs_inopb ||
363 altsblock.fs_cssize != sblock.fs_cssize ||
364 altsblock.fs_ipg != sblock.fs_ipg ||
365 altsblock.fs_fpg != sblock.fs_fpg ||
366 altsblock.fs_magic != sblock.fs_magic) {
367 badsb(listerr,
368 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
369 return (0);
370 }
371 /*
372 * If not yet done, update UFS1 superblock with new wider fields.
373 */
374 if (sblock.fs_magic == FS_UFS1_MAGIC &&
375 sblock.fs_maxbsize != sblock.fs_bsize) {
376 sblock.fs_maxbsize = sblock.fs_bsize;
377 sblock.fs_time = sblock.fs_old_time;
378 sblock.fs_size = sblock.fs_old_size;
379 sblock.fs_dsize = sblock.fs_old_dsize;
380 sblock.fs_csaddr = sblock.fs_old_csaddr;
381 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
382 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
383 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
384 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
385 }
386 havesb = 1;
387 return (1);
388}
389
390static void
391badsb(int listerr, const char *s)
392{
393
394 if (!listerr)
395 return;
396 if (preen)
397 printf("%s: ", cdevname);
398 pfatal("BAD SUPER BLOCK: %s\n", s);
399}
400
401void
402sblock_init(void)
403{
404 struct disklabel *lp;
405
406 fswritefd = -1;
407 fsmodified = 0;
408 lfdir = 0;
409 initbarea(&sblk);
410 initbarea(&asblk);
411 sblk.b_un.b_buf = malloc(SBLOCKSIZE);
412 asblk.b_un.b_buf = malloc(SBLOCKSIZE);
413 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
414 errx(EEXIT, "cannot allocate space for superblock");
415 if ((lp = getdisklabel(NULL, fsreadfd)))
416 dev_bsize = secsize = lp->d_secsize;
417 else
418 dev_bsize = secsize = DEV_BSIZE;
419}
420
421/*
422 * Calculate a prototype superblock based on information in the disk label.
423 * When done the cgsblock macro can be calculated and the fs_ncg field
424 * can be used. Do NOT attempt to use other macros without verifying that
425 * their needed information is available!
426 */
427static int
428calcsb(char *dev, int devfd, struct fs *fs)
429{
430 struct disklabel *lp;
431 struct partition *pp;
432 char *cp;
433 int i, nspf;
434
435 cp = strchr(dev, '\0') - 1;
436 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
437 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
438 return (0);
439 }
440 lp = getdisklabel(dev, devfd);
441 if (isdigit(*cp))
442 pp = &lp->d_partitions[0];
443 else
444 pp = &lp->d_partitions[*cp - 'a'];
445 if (pp->p_fstype != FS_BSDFFS) {
446 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
447 dev, pp->p_fstype < FSMAXTYPES ?
448 fstypenames[pp->p_fstype] : "unknown");
449 return (0);
450 }
451 if (pp->p_fsize == 0 || pp->p_frag == 0 ||
452 pp->p_cpg == 0 || pp->p_size == 0) {
453 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
454 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
455 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
456 return (0);
457 }
458 memset(fs, 0, sizeof(struct fs));
459 fs->fs_fsize = pp->p_fsize;
460 fs->fs_frag = pp->p_frag;
461 fs->fs_size = pp->p_size;
462 fs->fs_sblkno = roundup(
463 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
464 fs->fs_frag);
465 nspf = fs->fs_fsize / lp->d_secsize;
466 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
467 fs->fs_fsbtodb++;
468 dev_bsize = lp->d_secsize;
469 if (fs->fs_magic == FS_UFS2_MAGIC) {
470 fs->fs_fpg = pp->p_cpg;
471 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
472 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
473 fs->fs_old_cpg = pp->p_cpg;
474 fs->fs_old_cgmask = 0xffffffff;
475 for (i = lp->d_ntracks; i > 1; i >>= 1)
476 fs->fs_old_cgmask <<= 1;
477 if (!POWEROF2(lp->d_ntracks))
478 fs->fs_old_cgmask <<= 1;
479 fs->fs_old_cgoffset = roundup(howmany(lp->d_nsectors, nspf),
480 fs->fs_frag);
481 fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
482 fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
483 fs->fs_old_cpg);
484 }
485 return (1);
486}
487
488static struct disklabel *
489getdisklabel(char *s, int fd)
490{
491 static struct disklabel lab;
492
493 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
494 if (s == NULL)
495 return ((struct disklabel *)NULL);
496 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
497 errx(EEXIT, "%s: can't read disk label", s);
498 }
499 return (&lab);
500}
275 goto badsb;
276 }
277 bufinit();
278 if (sblock.fs_flags & FS_DOSOFTDEP)
279 usedsoftdep = 1;
280 else
281 usedsoftdep = 0;
282 return (1);
283
284badsb:
285 ckfini(0);
286 return (0);
287}
288
289/*
290 * Possible superblock locations ordered from most to least likely.
291 */
292static int sblock_try[] = SBLOCKSEARCH;
293
294/*
295 * Read in the super block and its summary info.
296 */
297int
298readsb(int listerr)
299{
300 ufs2_daddr_t super;
301 int i;
302
303 if (bflag) {
304 super = bflag;
305 if ((bread(fsreadfd, (char *)&sblock, super, (long)SBLOCKSIZE)))
306 return (0);
307 } else {
308 for (i = 0; sblock_try[i] != -1; i++) {
309 super = sblock_try[i] / dev_bsize;
310 if ((bread(fsreadfd, (char *)&sblock, super,
311 (long)SBLOCKSIZE)))
312 return (0);
313 if ((sblock.fs_magic == FS_UFS1_MAGIC ||
314 (sblock.fs_magic == FS_UFS2_MAGIC &&
315 sblock.fs_sblockloc ==
316 numfrags(&sblock, sblock_try[i]))) &&
317 sblock.fs_ncg >= 1 &&
318 sblock.fs_bsize >= SBLOCKSIZE &&
319 sblock.fs_bsize >= sizeof(struct fs))
320 break;
321 }
322 if (sblock_try[i] == -1) {
323 fprintf(stderr, "Cannot find filesystem superblock\n");
324 return (0);
325 }
326 }
327 /*
328 * Compute block size that the filesystem is based on,
329 * according to fsbtodb, and adjust superblock block number
330 * so we can tell if this is an alternate later.
331 */
332 super *= dev_bsize;
333 dev_bsize = sblock.fs_fsize / fsbtodb(&sblock, 1);
334 sblk.b_bno = super / dev_bsize;
335 sblk.b_size = SBLOCKSIZE;
336 if (bflag) {
337 havesb = 1;
338 return (1);
339 }
340 /*
341 * Compare all fields that should not differ in alternate super block.
342 * When an alternate super-block is specified this check is skipped.
343 */
344 getblk(&asblk, cgsblock(&sblock, sblock.fs_ncg - 1), sblock.fs_sbsize);
345 if (asblk.b_errs)
346 return (0);
347 if (altsblock.fs_sblkno != sblock.fs_sblkno ||
348 altsblock.fs_cblkno != sblock.fs_cblkno ||
349 altsblock.fs_iblkno != sblock.fs_iblkno ||
350 altsblock.fs_dblkno != sblock.fs_dblkno ||
351 altsblock.fs_ncg != sblock.fs_ncg ||
352 altsblock.fs_bsize != sblock.fs_bsize ||
353 altsblock.fs_fsize != sblock.fs_fsize ||
354 altsblock.fs_frag != sblock.fs_frag ||
355 altsblock.fs_bmask != sblock.fs_bmask ||
356 altsblock.fs_fmask != sblock.fs_fmask ||
357 altsblock.fs_bshift != sblock.fs_bshift ||
358 altsblock.fs_fshift != sblock.fs_fshift ||
359 altsblock.fs_fragshift != sblock.fs_fragshift ||
360 altsblock.fs_fsbtodb != sblock.fs_fsbtodb ||
361 altsblock.fs_sbsize != sblock.fs_sbsize ||
362 altsblock.fs_nindir != sblock.fs_nindir ||
363 altsblock.fs_inopb != sblock.fs_inopb ||
364 altsblock.fs_cssize != sblock.fs_cssize ||
365 altsblock.fs_ipg != sblock.fs_ipg ||
366 altsblock.fs_fpg != sblock.fs_fpg ||
367 altsblock.fs_magic != sblock.fs_magic) {
368 badsb(listerr,
369 "VALUES IN SUPER BLOCK DISAGREE WITH THOSE IN FIRST ALTERNATE");
370 return (0);
371 }
372 /*
373 * If not yet done, update UFS1 superblock with new wider fields.
374 */
375 if (sblock.fs_magic == FS_UFS1_MAGIC &&
376 sblock.fs_maxbsize != sblock.fs_bsize) {
377 sblock.fs_maxbsize = sblock.fs_bsize;
378 sblock.fs_time = sblock.fs_old_time;
379 sblock.fs_size = sblock.fs_old_size;
380 sblock.fs_dsize = sblock.fs_old_dsize;
381 sblock.fs_csaddr = sblock.fs_old_csaddr;
382 sblock.fs_cstotal.cs_ndir = sblock.fs_old_cstotal.cs_ndir;
383 sblock.fs_cstotal.cs_nbfree = sblock.fs_old_cstotal.cs_nbfree;
384 sblock.fs_cstotal.cs_nifree = sblock.fs_old_cstotal.cs_nifree;
385 sblock.fs_cstotal.cs_nffree = sblock.fs_old_cstotal.cs_nffree;
386 }
387 havesb = 1;
388 return (1);
389}
390
391static void
392badsb(int listerr, const char *s)
393{
394
395 if (!listerr)
396 return;
397 if (preen)
398 printf("%s: ", cdevname);
399 pfatal("BAD SUPER BLOCK: %s\n", s);
400}
401
402void
403sblock_init(void)
404{
405 struct disklabel *lp;
406
407 fswritefd = -1;
408 fsmodified = 0;
409 lfdir = 0;
410 initbarea(&sblk);
411 initbarea(&asblk);
412 sblk.b_un.b_buf = malloc(SBLOCKSIZE);
413 asblk.b_un.b_buf = malloc(SBLOCKSIZE);
414 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
415 errx(EEXIT, "cannot allocate space for superblock");
416 if ((lp = getdisklabel(NULL, fsreadfd)))
417 dev_bsize = secsize = lp->d_secsize;
418 else
419 dev_bsize = secsize = DEV_BSIZE;
420}
421
422/*
423 * Calculate a prototype superblock based on information in the disk label.
424 * When done the cgsblock macro can be calculated and the fs_ncg field
425 * can be used. Do NOT attempt to use other macros without verifying that
426 * their needed information is available!
427 */
428static int
429calcsb(char *dev, int devfd, struct fs *fs)
430{
431 struct disklabel *lp;
432 struct partition *pp;
433 char *cp;
434 int i, nspf;
435
436 cp = strchr(dev, '\0') - 1;
437 if (cp == (char *)-1 || ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))) {
438 pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
439 return (0);
440 }
441 lp = getdisklabel(dev, devfd);
442 if (isdigit(*cp))
443 pp = &lp->d_partitions[0];
444 else
445 pp = &lp->d_partitions[*cp - 'a'];
446 if (pp->p_fstype != FS_BSDFFS) {
447 pfatal("%s: NOT LABELED AS A BSD FILE SYSTEM (%s)\n",
448 dev, pp->p_fstype < FSMAXTYPES ?
449 fstypenames[pp->p_fstype] : "unknown");
450 return (0);
451 }
452 if (pp->p_fsize == 0 || pp->p_frag == 0 ||
453 pp->p_cpg == 0 || pp->p_size == 0) {
454 pfatal("%s: %s: type %s fsize %d, frag %d, cpg %d, size %d\n",
455 dev, "INCOMPLETE LABEL", fstypenames[pp->p_fstype],
456 pp->p_fsize, pp->p_frag, pp->p_cpg, pp->p_size);
457 return (0);
458 }
459 memset(fs, 0, sizeof(struct fs));
460 fs->fs_fsize = pp->p_fsize;
461 fs->fs_frag = pp->p_frag;
462 fs->fs_size = pp->p_size;
463 fs->fs_sblkno = roundup(
464 howmany(lp->d_bbsize + lp->d_sbsize, fs->fs_fsize),
465 fs->fs_frag);
466 nspf = fs->fs_fsize / lp->d_secsize;
467 for (fs->fs_fsbtodb = 0, i = nspf; i > 1; i >>= 1)
468 fs->fs_fsbtodb++;
469 dev_bsize = lp->d_secsize;
470 if (fs->fs_magic == FS_UFS2_MAGIC) {
471 fs->fs_fpg = pp->p_cpg;
472 fs->fs_ncg = howmany(fs->fs_size, fs->fs_fpg);
473 } else /* if (fs->fs_magic == FS_UFS1_MAGIC) */ {
474 fs->fs_old_cpg = pp->p_cpg;
475 fs->fs_old_cgmask = 0xffffffff;
476 for (i = lp->d_ntracks; i > 1; i >>= 1)
477 fs->fs_old_cgmask <<= 1;
478 if (!POWEROF2(lp->d_ntracks))
479 fs->fs_old_cgmask <<= 1;
480 fs->fs_old_cgoffset = roundup(howmany(lp->d_nsectors, nspf),
481 fs->fs_frag);
482 fs->fs_fpg = (fs->fs_old_cpg * lp->d_secpercyl) / nspf;
483 fs->fs_ncg = howmany(fs->fs_size / lp->d_secpercyl,
484 fs->fs_old_cpg);
485 }
486 return (1);
487}
488
489static struct disklabel *
490getdisklabel(char *s, int fd)
491{
492 static struct disklabel lab;
493
494 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
495 if (s == NULL)
496 return ((struct disklabel *)NULL);
497 pwarn("ioctl (GCINFO): %s\n", strerror(errno));
498 errx(EEXIT, "%s: can't read disk label", s);
499 }
500 return (&lab);
501}