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