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