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