setup.c revision 1.26 1 1.26 tsutsui /* $NetBSD: setup.c,v 1.26 2009/03/02 11:31:59 tsutsui Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1980, 1986, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.13 agc * 3. Neither the name of the University nor the names of its contributors
16 1.13 agc * may be used to endorse or promote products derived from this software
17 1.13 agc * without specific prior written permission.
18 1.13 agc *
19 1.13 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.13 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.13 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.13 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.13 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.13 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.13 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.13 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.13 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.13 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.13 agc * SUCH DAMAGE.
30 1.13 agc */
31 1.13 agc
32 1.13 agc /*
33 1.13 agc * Copyright (c) 1997 Manuel Bouyer.
34 1.13 agc *
35 1.13 agc * Redistribution and use in source and binary forms, with or without
36 1.13 agc * modification, are permitted provided that the following conditions
37 1.13 agc * are met:
38 1.13 agc * 1. Redistributions of source code must retain the above copyright
39 1.13 agc * notice, this list of conditions and the following disclaimer.
40 1.13 agc * 2. Redistributions in binary form must reproduce the above copyright
41 1.13 agc * notice, this list of conditions and the following disclaimer in the
42 1.13 agc * documentation and/or other materials provided with the distribution.
43 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
44 1.1 bouyer * must display the following acknowledgement:
45 1.14 bouyer * This product includes software developed by Manuel Bouyer.
46 1.14 bouyer * 4. The name of the author may not be used to endorse or promote products
47 1.14 bouyer * derived from this software without specific prior written permission.
48 1.1 bouyer *
49 1.15 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50 1.15 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 1.15 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 1.15 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53 1.15 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54 1.15 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 1.15 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 1.15 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 1.15 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58 1.15 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 1.1 bouyer */
60 1.1 bouyer
61 1.4 lukem #include <sys/cdefs.h>
62 1.1 bouyer #ifndef lint
63 1.1 bouyer #if 0
64 1.1 bouyer static char sccsid[] = "@(#)setup.c 8.5 (Berkeley) 11/23/94";
65 1.1 bouyer #else
66 1.26 tsutsui __RCSID("$NetBSD: setup.c,v 1.26 2009/03/02 11:31:59 tsutsui Exp $");
67 1.1 bouyer #endif
68 1.1 bouyer #endif /* not lint */
69 1.1 bouyer
70 1.8 christos #define FSTYPENAMES
71 1.1 bouyer #include <sys/param.h>
72 1.1 bouyer #include <sys/time.h>
73 1.1 bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
74 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
75 1.1 bouyer #include <sys/stat.h>
76 1.1 bouyer #include <sys/ioctl.h>
77 1.1 bouyer #include <sys/disklabel.h>
78 1.1 bouyer #include <sys/file.h>
79 1.1 bouyer
80 1.1 bouyer #include <errno.h>
81 1.1 bouyer #include <stdio.h>
82 1.1 bouyer #include <stdlib.h>
83 1.1 bouyer #include <string.h>
84 1.1 bouyer #include <ctype.h>
85 1.1 bouyer
86 1.1 bouyer #include "fsck.h"
87 1.1 bouyer #include "extern.h"
88 1.1 bouyer #include "fsutil.h"
89 1.25 lukem #include "exitvalues.h"
90 1.1 bouyer
91 1.19 christos void badsb(int, const char *);
92 1.17 xtraeme int calcsb(const char *, int, struct m_ext2fs *);
93 1.17 xtraeme static struct disklabel *getdisklabel(const char *, int);
94 1.17 xtraeme static int readsb(int);
95 1.1 bouyer
96 1.1 bouyer int
97 1.17 xtraeme setup(const char *dev)
98 1.1 bouyer {
99 1.4 lukem long cg, asked, i;
100 1.1 bouyer long bmapsize;
101 1.1 bouyer struct disklabel *lp;
102 1.1 bouyer off_t sizepb;
103 1.1 bouyer struct stat statb;
104 1.1 bouyer struct m_ext2fs proto;
105 1.1 bouyer int doskipclean;
106 1.1 bouyer u_int64_t maxfilesize;
107 1.1 bouyer
108 1.1 bouyer havesb = 0;
109 1.1 bouyer fswritefd = -1;
110 1.1 bouyer doskipclean = skipclean;
111 1.1 bouyer if (stat(dev, &statb) < 0) {
112 1.1 bouyer printf("Can't stat %s: %s\n", dev, strerror(errno));
113 1.23 tsutsui return 0;
114 1.1 bouyer }
115 1.1 bouyer if (!S_ISCHR(statb.st_mode)) {
116 1.1 bouyer pfatal("%s is not a character device", dev);
117 1.1 bouyer if (reply("CONTINUE") == 0)
118 1.23 tsutsui return 0;
119 1.1 bouyer }
120 1.1 bouyer if ((fsreadfd = open(dev, O_RDONLY)) < 0) {
121 1.1 bouyer printf("Can't open %s: %s\n", dev, strerror(errno));
122 1.23 tsutsui return 0;
123 1.1 bouyer }
124 1.1 bouyer if (preen == 0)
125 1.1 bouyer printf("** %s", dev);
126 1.1 bouyer if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) {
127 1.1 bouyer fswritefd = -1;
128 1.1 bouyer if (preen)
129 1.1 bouyer pfatal("NO WRITE ACCESS");
130 1.1 bouyer printf(" (NO WRITE)");
131 1.1 bouyer }
132 1.1 bouyer if (preen == 0)
133 1.1 bouyer printf("\n");
134 1.1 bouyer fsmodified = 0;
135 1.1 bouyer lfdir = 0;
136 1.1 bouyer initbarea(&sblk);
137 1.1 bouyer initbarea(&asblk);
138 1.6 bouyer sblk.b_un.b_buf = malloc(SBSIZE);
139 1.6 bouyer asblk.b_un.b_buf = malloc(SBSIZE);
140 1.1 bouyer if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL)
141 1.25 lukem errexit("cannot allocate space for superblock");
142 1.1 bouyer if ((lp = getdisklabel((char *)NULL, fsreadfd)) != NULL)
143 1.1 bouyer dev_bsize = secsize = lp->d_secsize;
144 1.1 bouyer else
145 1.1 bouyer dev_bsize = secsize = DEV_BSIZE;
146 1.1 bouyer /*
147 1.1 bouyer * Read in the superblock, looking for alternates if necessary
148 1.1 bouyer */
149 1.1 bouyer if (readsb(1) == 0) {
150 1.1 bouyer if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0)
151 1.23 tsutsui return 0;
152 1.1 bouyer if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0)
153 1.23 tsutsui return 0;
154 1.1 bouyer for (cg = 1; cg < proto.e2fs_ncg; cg++) {
155 1.1 bouyer bflag = fsbtodb(&proto,
156 1.23 tsutsui cg * proto.e2fs.e2fs_bpg +
157 1.23 tsutsui proto.e2fs.e2fs_first_dblock);
158 1.1 bouyer if (readsb(0) != 0)
159 1.1 bouyer break;
160 1.1 bouyer }
161 1.1 bouyer if (cg >= proto.e2fs_ncg) {
162 1.1 bouyer printf("%s %s\n%s %s\n%s %s\n",
163 1.23 tsutsui "SEARCH FOR ALTERNATE SUPER-BLOCK",
164 1.23 tsutsui "FAILED. YOU MUST USE THE",
165 1.23 tsutsui "-b OPTION TO FSCK_FFS TO SPECIFY THE",
166 1.23 tsutsui "LOCATION OF AN ALTERNATE",
167 1.23 tsutsui "SUPER-BLOCK TO SUPPLY NEEDED",
168 1.23 tsutsui "INFORMATION; SEE fsck_ext2fs(8).");
169 1.23 tsutsui return 0;
170 1.1 bouyer }
171 1.1 bouyer doskipclean = 0;
172 1.1 bouyer pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag);
173 1.1 bouyer }
174 1.1 bouyer if (debug)
175 1.1 bouyer printf("state = %d\n", sblock.e2fs.e2fs_state);
176 1.1 bouyer if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) {
177 1.1 bouyer if (doskipclean) {
178 1.1 bouyer pwarn("%sile system is clean; not checking\n",
179 1.23 tsutsui preen ? "f" : "** F");
180 1.23 tsutsui return -1;
181 1.1 bouyer }
182 1.1 bouyer if (!preen)
183 1.1 bouyer pwarn("** File system is already clean\n");
184 1.1 bouyer }
185 1.1 bouyer maxfsblock = sblock.e2fs.e2fs_bcount;
186 1.1 bouyer maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg;
187 1.1 bouyer sizepb = sblock.e2fs_bsize;
188 1.1 bouyer maxfilesize = sblock.e2fs_bsize * NDADDR - 1;
189 1.1 bouyer for (i = 0; i < NIADDR; i++) {
190 1.1 bouyer sizepb *= NINDIR(&sblock);
191 1.1 bouyer maxfilesize += sizepb;
192 1.1 bouyer }
193 1.1 bouyer /*
194 1.1 bouyer * Check and potentially fix certain fields in the super block.
195 1.1 bouyer */
196 1.20 christos if (/* (sblock.e2fs.e2fs_rbcount < 0) || */
197 1.23 tsutsui (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) {
198 1.1 bouyer pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK",
199 1.23 tsutsui sblock.e2fs.e2fs_rbcount);
200 1.1 bouyer if (reply("SET TO DEFAULT") == 1) {
201 1.23 tsutsui sblock.e2fs.e2fs_rbcount =
202 1.23 tsutsui sblock.e2fs.e2fs_bcount * MINFREE / 100;
203 1.1 bouyer sbdirty();
204 1.6 bouyer dirty(&asblk);
205 1.1 bouyer }
206 1.1 bouyer }
207 1.1 bouyer if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) {
208 1.1 bouyer pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK",
209 1.23 tsutsui sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg);
210 1.1 bouyer return 0;
211 1.1 bouyer }
212 1.1 bouyer if (asblk.b_dirty && !bflag) {
213 1.6 bouyer copyback_sb(&asblk);
214 1.1 bouyer flush(fswritefd, &asblk);
215 1.1 bouyer }
216 1.1 bouyer /*
217 1.1 bouyer * read in the summary info.
218 1.1 bouyer */
219 1.1 bouyer
220 1.1 bouyer sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize);
221 1.9 bouyer if (sblock.e2fs_gd == NULL)
222 1.25 lukem errexit("out of memory");
223 1.1 bouyer asked = 0;
224 1.23 tsutsui for (i = 0; i < sblock.e2fs_ngdb; i++) {
225 1.23 tsutsui if (bread(fsreadfd,
226 1.23 tsutsui (char *)&sblock.e2fs_gd[i * sblock.e2fs_bsize /
227 1.23 tsutsui sizeof(struct ext2_gd)],
228 1.23 tsutsui fsbtodb(&sblock, ((sblock.e2fs_bsize > 1024) ? 0 : 1) +
229 1.23 tsutsui i + 1),
230 1.23 tsutsui sblock.e2fs_bsize) != 0 && !asked) {
231 1.1 bouyer pfatal("BAD SUMMARY INFORMATION");
232 1.1 bouyer if (reply("CONTINUE") == 0)
233 1.25 lukem exit(FSCK_EXIT_CHECK_FAILED);
234 1.1 bouyer asked++;
235 1.1 bouyer }
236 1.1 bouyer }
237 1.1 bouyer /*
238 1.1 bouyer * allocate and initialize the necessary maps
239 1.1 bouyer */
240 1.1 bouyer bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t));
241 1.23 tsutsui blockmap = calloc((unsigned int)bmapsize, sizeof(char));
242 1.1 bouyer if (blockmap == NULL) {
243 1.1 bouyer printf("cannot alloc %u bytes for blockmap\n",
244 1.23 tsutsui (unsigned int)bmapsize);
245 1.1 bouyer goto badsblabel;
246 1.1 bouyer }
247 1.23 tsutsui statemap = calloc((unsigned int)(maxino + 2), sizeof(char));
248 1.1 bouyer if (statemap == NULL) {
249 1.1 bouyer printf("cannot alloc %u bytes for statemap\n",
250 1.23 tsutsui (unsigned int)(maxino + 1));
251 1.1 bouyer goto badsblabel;
252 1.1 bouyer }
253 1.23 tsutsui typemap = calloc((unsigned int)(maxino + 1), sizeof(char));
254 1.11 bouyer if (typemap == NULL) {
255 1.11 bouyer printf("cannot alloc %u bytes for typemap\n",
256 1.23 tsutsui (unsigned int)(maxino + 1));
257 1.23 tsutsui goto badsblabel;
258 1.11 bouyer }
259 1.23 tsutsui lncntp = calloc((unsigned)(maxino + 1), sizeof(int16_t));
260 1.1 bouyer if (lncntp == NULL) {
261 1.23 tsutsui printf("cannot alloc %u bytes for lncntp\n",
262 1.23 tsutsui (unsigned int)((maxino + 1) * sizeof(int16_t)));
263 1.1 bouyer goto badsblabel;
264 1.1 bouyer }
265 1.1 bouyer for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) {
266 1.6 bouyer numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs);
267 1.1 bouyer }
268 1.1 bouyer inplast = 0;
269 1.1 bouyer listmax = numdirs + 10;
270 1.23 tsutsui inpsort = calloc((unsigned int)listmax, sizeof(struct inoinfo *));
271 1.23 tsutsui inphead = calloc((unsigned int)numdirs, sizeof(struct inoinfo *));
272 1.1 bouyer if (inpsort == NULL || inphead == NULL) {
273 1.23 tsutsui printf("cannot alloc %u bytes for inphead\n",
274 1.23 tsutsui (unsigned int)(numdirs * sizeof(struct inoinfo *)));
275 1.1 bouyer goto badsblabel;
276 1.1 bouyer }
277 1.1 bouyer bufinit();
278 1.23 tsutsui return 1;
279 1.1 bouyer
280 1.1 bouyer badsblabel:
281 1.1 bouyer ckfini(0);
282 1.23 tsutsui return 0;
283 1.1 bouyer }
284 1.1 bouyer
285 1.1 bouyer /*
286 1.6 bouyer * Read in the super block and its summary info, convert to host byte order.
287 1.1 bouyer */
288 1.1 bouyer static int
289 1.17 xtraeme readsb(int listerr)
290 1.1 bouyer {
291 1.1 bouyer daddr_t super = bflag ? bflag : SBOFF / dev_bsize;
292 1.1 bouyer
293 1.6 bouyer if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0)
294 1.23 tsutsui return 0;
295 1.1 bouyer sblk.b_bno = super;
296 1.1 bouyer sblk.b_size = SBSIZE;
297 1.6 bouyer
298 1.6 bouyer /* Copy the superblock in memory */
299 1.12 bouyer e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs);
300 1.6 bouyer
301 1.1 bouyer /*
302 1.1 bouyer * run a few consistency checks of the super block
303 1.1 bouyer */
304 1.1 bouyer if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) {
305 1.23 tsutsui badsb(listerr, "MAGIC NUMBER WRONG");
306 1.23 tsutsui return 0;
307 1.1 bouyer }
308 1.1 bouyer if (sblock.e2fs.e2fs_log_bsize > 2) {
309 1.23 tsutsui badsb(listerr, "BAD LOG_BSIZE");
310 1.23 tsutsui return 0;
311 1.1 bouyer }
312 1.26 tsutsui if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
313 1.26 tsutsui (!powerof2(sblock.e2fs.e2fs_inode_size) ||
314 1.26 tsutsui sblock.e2fs.e2fs_inode_size < sizeof(struct ext2fs_dinode) ||
315 1.26 tsutsui sblock.e2fs.e2fs_inode_size >
316 1.26 tsutsui (1024 << sblock.e2fs.e2fs_log_bsize))) {
317 1.26 tsutsui badsb(listerr, "BAD INODE_SIZE");
318 1.26 tsutsui return 0;
319 1.26 tsutsui }
320 1.1 bouyer
321 1.1 bouyer /* compute the dynamic fields of the in-memory sb */
322 1.1 bouyer /* compute dynamic sb infos */
323 1.1 bouyer sblock.e2fs_ncg =
324 1.23 tsutsui howmany(sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock,
325 1.23 tsutsui sblock.e2fs.e2fs_bpg);
326 1.1 bouyer /* XXX assume hw bsize = 512 */
327 1.1 bouyer sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1;
328 1.1 bouyer sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize;
329 1.1 bouyer sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize;
330 1.1 bouyer sblock.e2fs_qbmask = sblock.e2fs_bsize - 1;
331 1.1 bouyer sblock.e2fs_bmask = ~sblock.e2fs_qbmask;
332 1.1 bouyer sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg,
333 1.23 tsutsui sblock.e2fs_bsize / sizeof(struct ext2_gd));
334 1.26 tsutsui sblock.e2fs_ipb = sblock.e2fs_bsize / EXT2_DINODE_SIZE(&sblock);
335 1.21 tsutsui sblock.e2fs_itpg = howmany(sblock.e2fs.e2fs_ipg, sblock.e2fs_ipb);
336 1.1 bouyer
337 1.1 bouyer /*
338 1.1 bouyer * Compute block size that the filesystem is based on,
339 1.1 bouyer * according to fsbtodb, and adjust superblock block number
340 1.1 bouyer * so we can tell if this is an alternate later.
341 1.1 bouyer */
342 1.1 bouyer super *= dev_bsize;
343 1.1 bouyer dev_bsize = sblock.e2fs_bsize / fsbtodb(&sblock, 1);
344 1.1 bouyer sblk.b_bno = super / dev_bsize;
345 1.6 bouyer
346 1.21 tsutsui if (sblock.e2fs_ncg == 1) {
347 1.21 tsutsui /* no alternate superblock; assume it's okay */
348 1.21 tsutsui havesb = 1;
349 1.23 tsutsui return 1;
350 1.21 tsutsui }
351 1.6 bouyer getblk(&asblk, 1 * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock,
352 1.6 bouyer (long)SBSIZE);
353 1.6 bouyer if (asblk.b_errs)
354 1.23 tsutsui return 0;
355 1.1 bouyer if (bflag) {
356 1.1 bouyer havesb = 1;
357 1.23 tsutsui return 1;
358 1.1 bouyer }
359 1.1 bouyer
360 1.1 bouyer /*
361 1.1 bouyer * Set all possible fields that could differ, then do check
362 1.1 bouyer * of whole super block against an alternate super block.
363 1.1 bouyer * When an alternate super-block is specified this check is skipped.
364 1.1 bouyer */
365 1.6 bouyer asblk.b_un.b_fs->e2fs_rbcount = sblk.b_un.b_fs->e2fs_rbcount;
366 1.6 bouyer asblk.b_un.b_fs->e2fs_fbcount = sblk.b_un.b_fs->e2fs_fbcount;
367 1.6 bouyer asblk.b_un.b_fs->e2fs_ficount = sblk.b_un.b_fs->e2fs_ficount;
368 1.6 bouyer asblk.b_un.b_fs->e2fs_mtime = sblk.b_un.b_fs->e2fs_mtime;
369 1.6 bouyer asblk.b_un.b_fs->e2fs_wtime = sblk.b_un.b_fs->e2fs_wtime;
370 1.6 bouyer asblk.b_un.b_fs->e2fs_mnt_count = sblk.b_un.b_fs->e2fs_mnt_count;
371 1.23 tsutsui asblk.b_un.b_fs->e2fs_max_mnt_count =
372 1.23 tsutsui sblk.b_un.b_fs->e2fs_max_mnt_count;
373 1.6 bouyer asblk.b_un.b_fs->e2fs_state = sblk.b_un.b_fs->e2fs_state;
374 1.6 bouyer asblk.b_un.b_fs->e2fs_beh = sblk.b_un.b_fs->e2fs_beh;
375 1.6 bouyer asblk.b_un.b_fs->e2fs_lastfsck = sblk.b_un.b_fs->e2fs_lastfsck;
376 1.6 bouyer asblk.b_un.b_fs->e2fs_fsckintv = sblk.b_un.b_fs->e2fs_fsckintv;
377 1.6 bouyer asblk.b_un.b_fs->e2fs_ruid = sblk.b_un.b_fs->e2fs_ruid;
378 1.6 bouyer asblk.b_un.b_fs->e2fs_rgid = sblk.b_un.b_fs->e2fs_rgid;
379 1.10 bouyer asblk.b_un.b_fs->e2fs_block_group_nr =
380 1.10 bouyer sblk.b_un.b_fs->e2fs_block_group_nr;
381 1.18 ws asblk.b_un.b_fs->e2fs_features_rocompat &= ~EXT2F_ROCOMPAT_LARGEFILE;
382 1.18 ws asblk.b_un.b_fs->e2fs_features_rocompat |=
383 1.18 ws sblk.b_un.b_fs->e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE;
384 1.12 bouyer if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
385 1.12 bouyer ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP) ||
386 1.12 bouyer (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP))) {
387 1.10 bouyer if (debug) {
388 1.10 bouyer printf("compat 0x%08x, incompat 0x%08x, compat_ro "
389 1.10 bouyer "0x%08x\n",
390 1.12 bouyer sblock.e2fs.e2fs_features_compat,
391 1.12 bouyer sblock.e2fs.e2fs_features_incompat,
392 1.12 bouyer sblock.e2fs.e2fs_features_rocompat);
393 1.10 bouyer }
394 1.23 tsutsui badsb(listerr, "INCOMPATIBLE FEATURE BITS IN SUPER BLOCK");
395 1.10 bouyer return 0;
396 1.10 bouyer }
397 1.6 bouyer if (memcmp(sblk.b_un.b_fs, asblk.b_un.b_fs, SBSIZE)) {
398 1.1 bouyer if (debug) {
399 1.6 bouyer u_int32_t *nlp, *olp, *endlp;
400 1.1 bouyer
401 1.1 bouyer printf("superblock mismatches\n");
402 1.6 bouyer nlp = (u_int32_t *)asblk.b_un.b_fs;
403 1.6 bouyer olp = (u_int32_t *)sblk.b_un.b_fs;
404 1.23 tsutsui endlp = olp + (SBSIZE / sizeof(*olp));
405 1.1 bouyer for ( ; olp < endlp; olp++, nlp++) {
406 1.1 bouyer if (*olp == *nlp)
407 1.1 bouyer continue;
408 1.23 tsutsui printf("offset %ld, original %ld, "
409 1.23 tsutsui "alternate %ld\n",
410 1.23 tsutsui (long)(olp - (u_int32_t *)sblk.b_un.b_fs),
411 1.23 tsutsui (long)fs2h32(*olp),
412 1.23 tsutsui (long)fs2h32(*nlp));
413 1.1 bouyer }
414 1.1 bouyer }
415 1.1 bouyer badsb(listerr,
416 1.23 tsutsui "VALUES IN SUPER BLOCK DISAGREE WITH "
417 1.23 tsutsui "THOSE IN FIRST ALTERNATE");
418 1.23 tsutsui return 0;
419 1.1 bouyer }
420 1.1 bouyer havesb = 1;
421 1.23 tsutsui return 1;
422 1.1 bouyer }
423 1.1 bouyer
424 1.1 bouyer void
425 1.17 xtraeme copyback_sb(struct bufarea *bp)
426 1.6 bouyer {
427 1.6 bouyer /* Copy the in-memory superblock back to buffer */
428 1.24 tsutsui bp->b_un.b_fs->e2fs_icount = h2fs32(sblock.e2fs.e2fs_icount);
429 1.24 tsutsui bp->b_un.b_fs->e2fs_bcount = h2fs32(sblock.e2fs.e2fs_bcount);
430 1.24 tsutsui bp->b_un.b_fs->e2fs_rbcount = h2fs32(sblock.e2fs.e2fs_rbcount);
431 1.24 tsutsui bp->b_un.b_fs->e2fs_fbcount = h2fs32(sblock.e2fs.e2fs_fbcount);
432 1.24 tsutsui bp->b_un.b_fs->e2fs_ficount = h2fs32(sblock.e2fs.e2fs_ficount);
433 1.6 bouyer bp->b_un.b_fs->e2fs_first_dblock =
434 1.24 tsutsui h2fs32(sblock.e2fs.e2fs_first_dblock);
435 1.24 tsutsui bp->b_un.b_fs->e2fs_log_bsize = h2fs32(sblock.e2fs.e2fs_log_bsize);
436 1.24 tsutsui bp->b_un.b_fs->e2fs_fsize = h2fs32(sblock.e2fs.e2fs_fsize);
437 1.24 tsutsui bp->b_un.b_fs->e2fs_bpg = h2fs32(sblock.e2fs.e2fs_bpg);
438 1.24 tsutsui bp->b_un.b_fs->e2fs_fpg = h2fs32(sblock.e2fs.e2fs_fpg);
439 1.24 tsutsui bp->b_un.b_fs->e2fs_ipg = h2fs32(sblock.e2fs.e2fs_ipg);
440 1.24 tsutsui bp->b_un.b_fs->e2fs_mtime = h2fs32(sblock.e2fs.e2fs_mtime);
441 1.24 tsutsui bp->b_un.b_fs->e2fs_wtime = h2fs32(sblock.e2fs.e2fs_wtime);
442 1.24 tsutsui bp->b_un.b_fs->e2fs_lastfsck = h2fs32(sblock.e2fs.e2fs_lastfsck);
443 1.24 tsutsui bp->b_un.b_fs->e2fs_fsckintv = h2fs32(sblock.e2fs.e2fs_fsckintv);
444 1.24 tsutsui bp->b_un.b_fs->e2fs_creator = h2fs32(sblock.e2fs.e2fs_creator);
445 1.24 tsutsui bp->b_un.b_fs->e2fs_rev = h2fs32(sblock.e2fs.e2fs_rev);
446 1.24 tsutsui bp->b_un.b_fs->e2fs_mnt_count = h2fs16(sblock.e2fs.e2fs_mnt_count);
447 1.6 bouyer bp->b_un.b_fs->e2fs_max_mnt_count =
448 1.24 tsutsui h2fs16(sblock.e2fs.e2fs_max_mnt_count);
449 1.24 tsutsui bp->b_un.b_fs->e2fs_magic = h2fs16(sblock.e2fs.e2fs_magic);
450 1.24 tsutsui bp->b_un.b_fs->e2fs_state = h2fs16(sblock.e2fs.e2fs_state);
451 1.24 tsutsui bp->b_un.b_fs->e2fs_beh = h2fs16(sblock.e2fs.e2fs_beh);
452 1.24 tsutsui bp->b_un.b_fs->e2fs_ruid = h2fs16(sblock.e2fs.e2fs_ruid);
453 1.24 tsutsui bp->b_un.b_fs->e2fs_rgid = h2fs16(sblock.e2fs.e2fs_rgid);
454 1.6 bouyer }
455 1.6 bouyer
456 1.6 bouyer void
457 1.19 christos badsb(int listerr, const char *s)
458 1.1 bouyer {
459 1.1 bouyer
460 1.1 bouyer if (!listerr)
461 1.1 bouyer return;
462 1.1 bouyer if (preen)
463 1.1 bouyer printf("%s: ", cdevname());
464 1.1 bouyer pfatal("BAD SUPER BLOCK: %s\n", s);
465 1.1 bouyer }
466 1.1 bouyer
467 1.1 bouyer /*
468 1.1 bouyer * Calculate a prototype superblock based on information in the disk label.
469 1.1 bouyer * When done the cgsblock macro can be calculated and the fs_ncg field
470 1.1 bouyer * can be used. Do NOT attempt to use other macros without verifying that
471 1.1 bouyer * their needed information is available!
472 1.1 bouyer */
473 1.1 bouyer
474 1.1 bouyer int
475 1.17 xtraeme calcsb(const char *dev, int devfd, struct m_ext2fs *fs)
476 1.1 bouyer {
477 1.4 lukem struct disklabel *lp;
478 1.4 lukem struct partition *pp;
479 1.4 lukem char *cp;
480 1.1 bouyer
481 1.16 dsl cp = strchr(dev, '\0');
482 1.16 dsl if (cp-- == dev ||
483 1.16 dsl ((*cp < 'a' || *cp > 'h') && !isdigit((unsigned char)*cp))) {
484 1.1 bouyer pfatal("%s: CANNOT FIGURE OUT FILE SYSTEM PARTITION\n", dev);
485 1.23 tsutsui return 0;
486 1.1 bouyer }
487 1.1 bouyer lp = getdisklabel(dev, devfd);
488 1.16 dsl if (isdigit((unsigned char)*cp))
489 1.1 bouyer pp = &lp->d_partitions[0];
490 1.1 bouyer else
491 1.1 bouyer pp = &lp->d_partitions[*cp - 'a'];
492 1.6 bouyer if (pp->p_fstype != FS_EX2FS) {
493 1.6 bouyer pfatal("%s: NOT LABELED AS A EXT2 FILE SYSTEM (%s)\n",
494 1.23 tsutsui dev, pp->p_fstype < FSMAXTYPES ?
495 1.23 tsutsui fstypenames[pp->p_fstype] : "unknown");
496 1.23 tsutsui return 0;
497 1.1 bouyer }
498 1.1 bouyer memset(fs, 0, sizeof(struct m_ext2fs));
499 1.6 bouyer fs->e2fs_bsize = pp->p_fsize;
500 1.6 bouyer fs->e2fs.e2fs_log_bsize = pp->p_fsize / 1024;
501 1.1 bouyer fs->e2fs.e2fs_bcount = (pp->p_size * DEV_BSIZE) / fs->e2fs_bsize;
502 1.6 bouyer fs->e2fs.e2fs_first_dblock = (fs->e2fs.e2fs_log_bsize == 0) ? 1 : 0;
503 1.1 bouyer fs->e2fs.e2fs_bpg = fs->e2fs_bsize * NBBY;
504 1.1 bouyer fs->e2fs_bshift = LOG_MINBSIZE + fs->e2fs.e2fs_log_bsize;
505 1.1 bouyer fs->e2fs_qbmask = fs->e2fs_bsize - 1;
506 1.1 bouyer fs->e2fs_bmask = ~fs->e2fs_qbmask;
507 1.1 bouyer fs->e2fs_ncg =
508 1.23 tsutsui howmany(fs->e2fs.e2fs_bcount - fs->e2fs.e2fs_first_dblock,
509 1.23 tsutsui fs->e2fs.e2fs_bpg);
510 1.1 bouyer fs->e2fs_fsbtodb = fs->e2fs.e2fs_log_bsize + 1;
511 1.1 bouyer fs->e2fs_ngdb = howmany(fs->e2fs_ncg,
512 1.23 tsutsui fs->e2fs_bsize / sizeof(struct ext2_gd));
513 1.1 bouyer
514 1.23 tsutsui return 1;
515 1.1 bouyer }
516 1.1 bouyer
517 1.1 bouyer static struct disklabel *
518 1.17 xtraeme getdisklabel(const char *s, int fd)
519 1.1 bouyer {
520 1.1 bouyer static struct disklabel lab;
521 1.1 bouyer
522 1.1 bouyer if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
523 1.1 bouyer if (s == NULL)
524 1.23 tsutsui return NULL;
525 1.1 bouyer pwarn("ioctl (GCINFO): %s\n", strerror(errno));
526 1.25 lukem errexit("%s: can't read disk label", s);
527 1.1 bouyer }
528 1.23 tsutsui return &lab;
529 1.12 bouyer }
530 1.12 bouyer
531 1.12 bouyer daddr_t
532 1.17 xtraeme cgoverhead(int c)
533 1.12 bouyer {
534 1.12 bouyer int overh;
535 1.23 tsutsui overh =
536 1.23 tsutsui 1 /* block bitmap */ +
537 1.23 tsutsui 1 /* inode bitmap */ +
538 1.23 tsutsui sblock.e2fs_itpg;
539 1.12 bouyer if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
540 1.12 bouyer sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) {
541 1.12 bouyer if (cg_has_sb(c) == 0)
542 1.12 bouyer return overh;
543 1.12 bouyer }
544 1.23 tsutsui overh += 1 /* superblock */ + sblock.e2fs_ngdb;
545 1.12 bouyer return overh;
546 1.1 bouyer }
547