setup.c (14301B)
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 #ifndef lint 57 #if 0 58 static char sccsid[] = "@(#)setup.c 8.5 (Berkeley) 11/23/94"; 59 #else 60 __RCSID("$NetBSD: setup.c,v 1.39 2019/03/31 13:16:52 mlelstv Exp $"); 61 #endif 62 #endif /* not lint */ 63 64 #define FSTYPENAMES 65 #include <sys/param.h> 66 #include <sys/time.h> 67 #include <ufs/ext2fs/ext2fs_dinode.h> 68 #include <ufs/ext2fs/ext2fs.h> 69 #include <sys/stat.h> 70 #include <sys/ioctl.h> 71 #include <sys/file.h> 72 73 #include <errno.h> 74 #include <fcntl.h> 75 #include <stdio.h> 76 #include <stdlib.h> 77 #include <string.h> 78 #include <ctype.h> 79 80 #include "fsck.h" 81 #include "extern.h" 82 #include "fsutil.h" 83 #include "diskutil.h" 84 #include "exitvalues.h" 85 86 void badsb(int, const char *); 87 static int readsb(int); 88 89 static uint64_t volume_bytes; 90 int 91 setup(const char *dev) 92 { 93 long cg, i; 94 long bmapsize; 95 struct stat statb; 96 struct BlockDev target = {0}; 97 off_t sizepb; 98 uint64_t maxfilesize; 99 int flags = O_RDONLY | O_NONBLOCK | O_NOCTTY; 100 101 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX) 102 flags |= O_EXCL; 103 #endif 104 havesb = 0; 105 fswritefd = -1; 106 if ((fsreadfd = open(dev, flags)) < 0 || fstat(fsreadfd, &statb) < 0) 107 enprintf(FSCK_EXIT_CHECK_FAILED, "%s:", dev); 108 if (S_ISREG(statb.st_mode)) 109 volume_bytes = statb.st_size; 110 else if (S_ISBLK(statb.st_mode) || S_ISCHR(statb.st_mode)) { 111 target.fd = fsreadfd; 112 blockdev_sys_size(&target); 113 volume_bytes = target.size; 114 } 115 if (!volume_bytes) 116 errexit("%s: cannot determine volume size", dev); 117 printf("** %s (NO WRITE)\n", dev); 118 fsmodified = 0; 119 lfdir = 0; 120 initbarea(&sblk); 121 initbarea(&asblk); 122 sblk.b_un.b_buf = malloc(SBSIZE); 123 asblk.b_un.b_buf = malloc(SBSIZE); 124 if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) 125 errexit("cannot allocate space for superblock"); 126 dev_bsize = secsize = DEV_BSIZE; 127 if (!readsb(1)) 128 return 0; 129 maxfsblock = sblock.e2fs.e2fs_bcount; 130 maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg; 131 sizepb = sblock.e2fs_bsize; 132 maxfilesize = sblock.e2fs_bsize * EXT2FS_NDADDR - 1; 133 for (i = 0; i < EXT2FS_NIADDR; i++) { 134 sizepb *= EXT2_NINDIR(&sblock); 135 maxfilesize += sizepb; 136 } 137 /* 138 * Check and potentially fix certain fields in the super block. 139 */ 140 if (/* (sblock.e2fs.e2fs_rbcount < 0) || */ 141 (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount) 142 ) { 143 pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK", sblock.e2fs.e2fs_rbcount); 144 if (reply("SET TO DEFAULT") == 1) { 145 sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * MINFREE / 100; 146 sbdirty(); 147 dirty(&asblk); 148 } 149 } 150 if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) { 151 pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK", sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg); 152 return 0; 153 } 154 if (asblk.b_dirty && !bflag) { 155 copyback_sb(&asblk); 156 flush(fswritefd, &asblk); 157 } 158 /* 159 * read in the summary info. 160 */ 161 162 sblock.e2fs_gd = ecalloc(sblock.e2fs_ncg, sizeof(*sblock.e2fs_gd)); 163 for (i = 0; i < sblock.e2fs_ngdb; i++) { 164 char buf[4096]; 165 int j; 166 int perblock = sblock.e2fs_bsize / E2FS_REV0_GD_SIZE; 167 168 bread( 169 fsreadfd, 170 buf, 171 EXT2_FSBTODB(&sblock, sblock.e2fs.e2fs_first_dblock + i + 1), 172 sblock.e2fs_bsize 173 ); 174 for (j = 0; j < perblock && i * perblock + j < sblock.e2fs_ncg; j++) 175 memcpy(&sblock.e2fs_gd[i * perblock + j], buf + j * E2FS_REV0_GD_SIZE, E2FS_REV0_GD_SIZE); 176 } 177 for (cg = 0; cg < sblock.e2fs_ncg; cg++) { 178 const struct ext2_gd *gd = &sblock.e2fs_gd[cg]; 179 uint64_t first = (uint64_t)cg * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock; 180 uint64_t end = MIN(first + sblock.e2fs.e2fs_bpg, maxfsblock); 181 uint32_t blockmap = fs2h32(gd->ext2bgd_b_bitmap); 182 uint32_t inodemap = fs2h32(gd->ext2bgd_i_bitmap); 183 uint32_t table = fs2h32(gd->ext2bgd_i_tables); 184 185 if (cgoverhead(cg) > end - first || (fs2h16(gd->ext2bgd_flags) & ~E2FS_BG_INODE_ZEROED) 186 || blockmap < first || blockmap >= end || inodemap < first || inodemap >= end 187 || table < first || table >= end || sblock.e2fs_itpg > end - table) 188 errexit("invalid or unsupported group %ld", cg); 189 } 190 /* 191 * allocate and initialize the necessary maps 192 */ 193 bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); 194 blockmap = calloc((size_t)bmapsize, sizeof(char)); 195 if (blockmap == NULL) { 196 printf("cannot alloc %u bytes for blockmap\n", (unsigned int)bmapsize); 197 goto badsblabel; 198 } 199 statemap = calloc((size_t)(maxino + 2), sizeof(char)); 200 if (statemap == NULL) { 201 printf("cannot alloc %u bytes for statemap\n", (unsigned int)(maxino + 1)); 202 goto badsblabel; 203 } 204 typemap = calloc((size_t)(maxino + 1), sizeof(char)); 205 if (typemap == NULL) { 206 printf("cannot alloc %u bytes for typemap\n", (unsigned int)(maxino + 1)); 207 goto badsblabel; 208 } 209 lncntp = calloc((size_t)(maxino + 1), sizeof(int16_t)); 210 if (lncntp == NULL) { 211 printf("cannot alloc %u bytes for lncntp\n", (unsigned int)((maxino + 1) * sizeof(int16_t))); 212 goto badsblabel; 213 } 214 for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) { 215 numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs); 216 } 217 inplast = 0; 218 numdirs = MAX(numdirs, 1); 219 listmax = numdirs + 10; 220 inpsort = calloc((unsigned int)listmax, sizeof(struct inoinfo *)); 221 inphead = calloc((unsigned int)numdirs, sizeof(struct inoinfo *)); 222 if (inpsort == NULL || inphead == NULL) { 223 printf( 224 "cannot alloc %u bytes for inphead\n", (unsigned int)(numdirs * sizeof(struct inoinfo *)) 225 ); 226 goto badsblabel; 227 } 228 bufinit(); 229 return 1; 230 231 badsblabel: 232 ckfini(0); 233 return 0; 234 } 235 236 /* 237 * Read in the super block and its summary info, convert to host byte order. 238 */ 239 static int 240 readsb(int listerr) 241 { 242 daddr_t super = bflag ? bflag : SBOFF / dev_bsize; 243 244 if (bread(fsreadfd, (char *)sblk.b_un.b_fs, super, (long)SBSIZE) != 0) 245 return 0; 246 sblk.b_bno = super; 247 sblk.b_size = SBSIZE; 248 249 /* Copy the superblock in memory */ 250 e2fs_sbload(sblk.b_un.b_fs, &sblock.e2fs); 251 252 /* 253 * run a few consistency checks of the super block 254 */ 255 if (sblock.e2fs.e2fs_magic != E2FS_MAGIC) { 256 badsb(listerr, "MAGIC NUMBER WRONG"); 257 return 0; 258 } 259 if (sblock.e2fs.e2fs_log_bsize > 2) { 260 badsb(listerr, "BAD LOG_BSIZE"); 261 return 0; 262 } 263 if (sblock.e2fs.e2fs_rev > E2FS_REV0 264 && (!powerof2(sblock.e2fs.e2fs_inode_size) 265 || sblock.e2fs.e2fs_inode_size < EXT2_REV0_DINODE_SIZE 266 || sblock.e2fs.e2fs_inode_size > (1024 << sblock.e2fs.e2fs_log_bsize))) { 267 badsb(listerr, "BAD INODE_SIZE"); 268 return 0; 269 } 270 271 if (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_REPLAY_JOURNAL) 272 errexit("journal replay required"); 273 if ((sblock.e2fs.e2fs_features_incompat & ~EXT2F_INCOMPAT_SUPP_FSCK) 274 || (sblock.e2fs.e2fs_features_rocompat & ~EXT2F_ROCOMPAT_SUPP_FSCK) 275 || (sblock.e2fs.e2fs_features_compat & ~EXT2F_COMPAT_SUPP_FSCK)) 276 errexit("unsupported ext layout"); 277 if ((sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_HASJOURNAL) 278 && (fs2h32(sblk.b_un.b_fs->e3fs_journal_inum) != 8 || sblk.b_un.b_fs->e3fs_journal_dev != 0)) 279 errexit("unsupported journal layout"); 280 if (sblk.b_un.b_fs->e3fs_last_orphan || sblock.e2fs.e2fs_state != E2FS_ISCLEAN) 281 errexit("filesystem requires recovery"); 282 if (sblock.e2fs.e2fs_rev > E2FS_REV1 283 || sblock.e2fs.e2fs_first_dblock != (sblock.e2fs.e2fs_log_bsize == 0) 284 || sblock.e2fs.e2fs_bcount <= sblock.e2fs.e2fs_first_dblock || !sblock.e2fs.e2fs_bpg 285 || !sblock.e2fs.e2fs_ipg || sblock.e2fs.e2fs_bpg > (8192U << sblock.e2fs.e2fs_log_bsize) 286 || sblock.e2fs.e2fs_ipg > (8192U << sblock.e2fs.e2fs_log_bsize) || sblock.e2fs.e2fs_ipg % 8 287 || (uint64_t)sblock.e2fs.e2fs_bcount * (1024U << sblock.e2fs.e2fs_log_bsize) > volume_bytes) 288 errexit("invalid ext geometry"); 289 sblock.e2fs_ncg = howmany( 290 (uint64_t)sblock.e2fs.e2fs_bcount - sblock.e2fs.e2fs_first_dblock, sblock.e2fs.e2fs_bpg 291 ); 292 sblock.e2fs_fsbtodb = sblock.e2fs.e2fs_log_bsize + 1; 293 sblock.e2fs_bsize = 1024 << sblock.e2fs.e2fs_log_bsize; 294 sblock.e2fs_bshift = LOG_MINBSIZE + sblock.e2fs.e2fs_log_bsize; 295 sblock.e2fs_qbmask = sblock.e2fs_bsize - 1; 296 sblock.e2fs_bmask = ~sblock.e2fs_qbmask; 297 sblock.e2fs_ngdb = howmany(sblock.e2fs_ncg, sblock.e2fs_bsize / E2FS_REV0_GD_SIZE); 298 sblock.e2fs_ipb = sblock.e2fs_bsize / EXT2_DINODE_SIZE(&sblock); 299 sblock.e2fs_itpg = howmany(sblock.e2fs.e2fs_ipg, sblock.e2fs_ipb); 300 301 if ((uint64_t)sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg != sblock.e2fs.e2fs_icount 302 || sblock.e2fs.e2fs_icount < EXT2_FIRSTINO || sblock.e2fs.e2fs_icount >= UINT32_MAX - 2 303 || 1 + sblock.e2fs_ngdb > sblock.e2fs.e2fs_bpg) 304 errexit("invalid ext group or inode count"); 305 havesb = 1; 306 return 1; 307 } 308 309 void 310 copyback_sb(struct bufarea *bp) 311 { 312 /* Copy the in-memory superblock back to buffer */ 313 bp->b_un.b_fs->e2fs_icount = h2fs32(sblock.e2fs.e2fs_icount); 314 bp->b_un.b_fs->e2fs_bcount = h2fs32(sblock.e2fs.e2fs_bcount); 315 bp->b_un.b_fs->e2fs_rbcount = h2fs32(sblock.e2fs.e2fs_rbcount); 316 bp->b_un.b_fs->e2fs_fbcount = h2fs32(sblock.e2fs.e2fs_fbcount); 317 bp->b_un.b_fs->e2fs_ficount = h2fs32(sblock.e2fs.e2fs_ficount); 318 bp->b_un.b_fs->e2fs_first_dblock = h2fs32(sblock.e2fs.e2fs_first_dblock); 319 bp->b_un.b_fs->e2fs_log_bsize = h2fs32(sblock.e2fs.e2fs_log_bsize); 320 bp->b_un.b_fs->e2fs_fsize = h2fs32(sblock.e2fs.e2fs_fsize); 321 bp->b_un.b_fs->e2fs_bpg = h2fs32(sblock.e2fs.e2fs_bpg); 322 bp->b_un.b_fs->e2fs_fpg = h2fs32(sblock.e2fs.e2fs_fpg); 323 bp->b_un.b_fs->e2fs_ipg = h2fs32(sblock.e2fs.e2fs_ipg); 324 bp->b_un.b_fs->e2fs_mtime = h2fs32(sblock.e2fs.e2fs_mtime); 325 bp->b_un.b_fs->e2fs_wtime = h2fs32(sblock.e2fs.e2fs_wtime); 326 bp->b_un.b_fs->e2fs_lastfsck = h2fs32(sblock.e2fs.e2fs_lastfsck); 327 bp->b_un.b_fs->e2fs_fsckintv = h2fs32(sblock.e2fs.e2fs_fsckintv); 328 bp->b_un.b_fs->e2fs_creator = h2fs32(sblock.e2fs.e2fs_creator); 329 bp->b_un.b_fs->e2fs_rev = h2fs32(sblock.e2fs.e2fs_rev); 330 bp->b_un.b_fs->e2fs_mnt_count = h2fs16(sblock.e2fs.e2fs_mnt_count); 331 bp->b_un.b_fs->e2fs_max_mnt_count = h2fs16(sblock.e2fs.e2fs_max_mnt_count); 332 bp->b_un.b_fs->e2fs_magic = h2fs16(sblock.e2fs.e2fs_magic); 333 bp->b_un.b_fs->e2fs_state = h2fs16(sblock.e2fs.e2fs_state); 334 bp->b_un.b_fs->e2fs_beh = h2fs16(sblock.e2fs.e2fs_beh); 335 bp->b_un.b_fs->e2fs_ruid = h2fs16(sblock.e2fs.e2fs_ruid); 336 bp->b_un.b_fs->e2fs_rgid = h2fs16(sblock.e2fs.e2fs_rgid); 337 } 338 339 void 340 badsb(int listerr, const char *s) 341 { 342 if (!listerr) 343 return; 344 if (preen) 345 printf("%s: ", cdevname()); 346 pfatal("BAD SUPER BLOCK: %s\n", s); 347 } 348 349 /* 350 * Calculate a prototype superblock based on information in the disk label. 351 * When done the cgsblock macro can be calculated and the fs_ncg field 352 * can be used. Do NOT attempt to use other macros without verifying that 353 * their needed information is available! 354 */ 355 356 daddr_t 357 cgoverhead(int c) 358 { 359 int overh; 360 overh = 1 /* block bitmap */ + 1 /* inode bitmap */ + sblock.e2fs_itpg; 361 if (sblock.e2fs.e2fs_rev > E2FS_REV0 362 && sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) { 363 if (cg_has_sb(c) == 0) 364 return overh; 365 } 366 overh += 1 /* superblock */ + sblock.e2fs_ngdb; 367 return overh; 368 }