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