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