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