1 /* $OpenBSD: mkfs.c,v 1.101 2020/06/20 07:49:04 otto Exp $ */ 2 /* $NetBSD: mkfs.c,v 1.25 1995/06/18 21:35:38 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 2002 Networks Associates Technology, Inc. 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project by Marshall 9 * Kirk McKusick and Network Associates Laboratories, the Security 10 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 11 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 12 * research program. 13 * 14 * Copyright (c) 1980, 1989, 1993 15 * The Regents of the University of California. All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 */ 41 42 #include <sys/param.h> /* MAXBSIZE DEV_BSIZE roundup btodb setbit */ 43 #include <sys/signal.h> 44 #include <sys/time.h> 45 #include <sys/disklabel.h> 46 #include <sys/ioctl.h> 47 #include <sys/mman.h> 48 #include <sys/resource.h> 49 #include <sys/sysctl.h> 50 51 #include <ufs/ufs/dinode.h> 52 #include <ufs/ufs/dir.h> 53 #include <ufs/ffs/fs.h> 54 55 #include <err.h> 56 #include <string.h> 57 #include <stdlib.h> 58 #include <stdint.h> 59 #include <unistd.h> 60 #include <limits.h> 61 62 #ifndef STANDALONE 63 #include <stdio.h> 64 #include <errno.h> 65 #endif 66 67 #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b)) 68 #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b)) 69 70 /* 71 * Default directory umask. 72 */ 73 #define UMASK 0755 74 75 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 76 77 /* 78 * 'Standard' bad FFS magic. 79 */ 80 #define FS_BAD_MAGIC 0x19960408 81 82 /* 83 * The minimum number of cylinder groups that should be created. 84 */ 85 #define MINCYLGRPS 4 86 87 /* 88 * variables set up by front end. 89 */ 90 extern int mfs; /* run as the memory based filesystem */ 91 extern int Nflag; /* run mkfs without writing file system */ 92 extern int Oflag; /* format as an 4.3BSD file system */ 93 extern daddr_t fssize; /* file system size in 512-byte blocks. */ 94 extern long long sectorsize; /* bytes/sector */ 95 extern int fsize; /* fragment size */ 96 extern int bsize; /* block size */ 97 extern int maxfrgspercg; /* maximum fragments per cylinder group */ 98 extern int minfree; /* free space threshold */ 99 extern int opt; /* optimization preference (space or time) */ 100 extern int density; /* number of bytes per inode */ 101 extern int maxbpg; /* maximum blocks per file in a cyl group */ 102 extern int avgfilesize; /* expected average file size */ 103 extern int avgfilesperdir; /* expected number of files per directory */ 104 extern int quiet; /* quiet flag */ 105 extern caddr_t membase; /* start address of memory based filesystem */ 106 107 union fs_u { 108 struct fs fs; 109 char pad[SBSIZE]; 110 } *fsun; 111 #define sblock fsun->fs 112 113 struct csum *fscs; 114 115 union cg_u { 116 struct cg cg; 117 char pad[MAXBSIZE]; 118 } *cgun; 119 #define acg cgun->cg 120 121 union dinode { 122 struct ufs1_dinode dp1; 123 struct ufs2_dinode dp2; 124 }; 125 126 int fsi, fso; 127 128 static caddr_t iobuf; 129 static long iobufsize; 130 131 daddr_t alloc(int, int); 132 static int charsperline(void); 133 static int ilog2(int); 134 void initcg(u_int, time_t); 135 void wtfs(daddr_t, int, void *); 136 int fsinit1(time_t, mode_t, uid_t, gid_t); 137 int fsinit2(time_t, mode_t, uid_t, gid_t); 138 int makedir(struct direct *, int); 139 void iput(union dinode *, ino_t); 140 void setblock(struct fs *, unsigned char *, int); 141 void clrblock(struct fs *, unsigned char *, int); 142 int isblock(struct fs *, unsigned char *, int); 143 void rdfs(daddr_t, int, void *); 144 void mkfs(struct partition *, char *, int, int, 145 mode_t, uid_t, gid_t); 146 static void checksz(void); 147 148 #ifndef STANDALONE 149 volatile sig_atomic_t cur_cylno; 150 volatile const char *cur_fsys; 151 void siginfo(int sig); 152 153 void 154 siginfo(int sig) 155 { 156 int save_errno = errno; 157 158 dprintf(STDERR_FILENO, "%s: initializing cg %ld/%d\n", 159 cur_fsys, (long)cur_cylno, sblock.fs_ncg); 160 errno = save_errno; 161 } 162 #endif 163 164 void 165 mkfs(struct partition *pp, char *fsys, int fi, int fo, mode_t mfsmode, 166 uid_t mfsuid, gid_t mfsgid) 167 { 168 time_t utime; 169 quad_t sizepb; 170 int i, j, width, origdensity, fragsperinode, minfpg, optimalfpg; 171 int lastminfpg, mincylgrps; 172 uint32_t bpg; 173 long csfrags; 174 u_int cg; 175 char tmpbuf[100]; /* XXX this will break in about 2,500 years */ 176 177 if ((fsun = calloc(1, sizeof (union fs_u))) == NULL || 178 (cgun = calloc(1, sizeof (union cg_u))) == NULL) 179 err(1, "calloc"); 180 181 #ifndef STANDALONE 182 time(&utime); 183 #endif 184 if (mfs) { 185 size_t sz; 186 if (fssize > SIZE_MAX / DEV_BSIZE) { 187 errno = ENOMEM; 188 err(12, "mmap"); 189 } 190 sz = (size_t)fssize * DEV_BSIZE; 191 membase = mmap(NULL, sz, PROT_READ|PROT_WRITE, 192 MAP_ANON|MAP_PRIVATE, -1, (off_t)0); 193 if (membase == MAP_FAILED) 194 err(12, "mmap"); 195 madvise(membase, sz, MADV_RANDOM); 196 } 197 fsi = fi; 198 fso = fo; 199 /* 200 * Validate the given file system size. 201 * Verify that its last block can actually be accessed. 202 */ 203 if (Oflag <= 1 && fssize > INT_MAX) 204 errx(13, "preposterous size %lld, max is %d", (long long)fssize, 205 INT_MAX); 206 if (Oflag == 2 && fssize > MAXDISKSIZE) 207 errx(13, "preposterous size %lld, max is %lld", 208 (long long)fssize, MAXDISKSIZE); 209 210 wtfs(fssize - (sectorsize / DEV_BSIZE), sectorsize, (char *)&sblock); 211 212 sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; 213 sblock.fs_avgfilesize = avgfilesize; 214 sblock.fs_avgfpdir = avgfilesperdir; 215 216 /* 217 * Collect and verify the block and fragment sizes. 218 */ 219 if (!POWEROF2(bsize)) { 220 errx(16, "block size must be a power of 2, not %d", bsize); 221 } 222 if (!POWEROF2(fsize)) { 223 errx(17, "fragment size must be a power of 2, not %d", 224 fsize); 225 } 226 if (fsize < sectorsize) { 227 errx(18, "fragment size %d is too small, minimum is %lld", 228 fsize, sectorsize); 229 } 230 if (bsize < MINBSIZE) { 231 errx(19, "block size %d is too small, minimum is %d", 232 bsize, MINBSIZE); 233 } 234 if (bsize > MAXBSIZE) { 235 errx(19, "block size %d is too large, maximum is %d", 236 bsize, MAXBSIZE); 237 } 238 if (bsize < fsize) { 239 errx(20, "block size (%d) cannot be smaller than fragment size (%d)", 240 bsize, fsize); 241 } 242 sblock.fs_bsize = bsize; 243 sblock.fs_fsize = fsize; 244 245 /* 246 * Calculate the superblock bitmasks and shifts. 247 */ 248 sblock.fs_bmask = ~(sblock.fs_bsize - 1); 249 sblock.fs_fmask = ~(sblock.fs_fsize - 1); 250 sblock.fs_qbmask = ~sblock.fs_bmask; 251 sblock.fs_qfmask = ~sblock.fs_fmask; 252 sblock.fs_bshift = ilog2(sblock.fs_bsize); 253 sblock.fs_fshift = ilog2(sblock.fs_fsize); 254 sblock.fs_frag = numfrags(&sblock, sblock.fs_bsize); 255 if (sblock.fs_frag > MAXFRAG) { 256 errx(21, "fragment size %d is too small, minimum with block " 257 "size %d is %d", sblock.fs_fsize, sblock.fs_bsize, 258 sblock.fs_bsize / MAXFRAG); 259 } 260 sblock.fs_fragshift = ilog2(sblock.fs_frag); 261 sblock.fs_fsbtodb = ilog2(sblock.fs_fsize / DEV_BSIZE); 262 sblock.fs_size = dbtofsb(&sblock, fssize); 263 sblock.fs_nspf = sblock.fs_fsize / DEV_BSIZE; 264 sblock.fs_maxcontig = 1; 265 sblock.fs_nrpos = 1; 266 sblock.fs_cpg = 1; 267 268 /* 269 * Before the file system is fully initialized, mark it as invalid. 270 */ 271 sblock.fs_magic = FS_BAD_MAGIC; 272 273 /* 274 * Set the remaining superblock fields. Note that for FFS1, media 275 * geometry fields are set to fake values. This is for compatibility 276 * with really ancient kernels that might still inspect these values. 277 */ 278 if (Oflag <= 1) { 279 sblock.fs_sblockloc = SBLOCK_UFS1; 280 sblock.fs_nindir = sblock.fs_bsize / sizeof(int32_t); 281 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); 282 if (Oflag == 0) { 283 sblock.fs_maxsymlinklen = 0; 284 sblock.fs_inodefmt = FS_42INODEFMT; 285 } else { 286 sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS1; 287 sblock.fs_inodefmt = FS_44INODEFMT; 288 } 289 sblock.fs_cgoffset = 0; 290 sblock.fs_cgmask = 0xffffffff; 291 sblock.fs_ffs1_size = sblock.fs_size; 292 sblock.fs_rotdelay = 0; 293 sblock.fs_rps = 60; 294 sblock.fs_interleave = 1; 295 sblock.fs_trackskew = 0; 296 sblock.fs_cpc = 0; 297 } else { 298 sblock.fs_inodefmt = FS_44INODEFMT; 299 sblock.fs_sblockloc = SBLOCK_UFS2; 300 sblock.fs_nindir = sblock.fs_bsize / sizeof(int64_t); 301 sblock.fs_inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); 302 sblock.fs_maxsymlinklen = MAXSYMLINKLEN_UFS2; 303 } 304 sblock.fs_sblkno = 305 roundup(howmany(sblock.fs_sblockloc + SBLOCKSIZE, sblock.fs_fsize), 306 sblock.fs_frag); 307 sblock.fs_cblkno = (int32_t)(sblock.fs_sblkno + 308 roundup(howmany(SBSIZE, sblock.fs_fsize), sblock.fs_frag)); 309 sblock.fs_iblkno = sblock.fs_cblkno + sblock.fs_frag; 310 sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; 311 for (sizepb = sblock.fs_bsize, i = 0; i < NIADDR; i++) { 312 sizepb *= NINDIR(&sblock); 313 sblock.fs_maxfilesize += sizepb; 314 } 315 #ifdef notyet 316 /* 317 * It is impossible to create a snapshot in case fs_maxfilesize is 318 * smaller than fssize. 319 */ 320 if (sblock.fs_maxfilesize < (u_quad_t)fssize) 321 warnx("WARNING: You will be unable to create snapshots on this " 322 "file system. Correct by using a larger blocksize."); 323 #endif 324 /* 325 * Calculate the number of blocks to put into each cylinder group. The 326 * first goal is to have at least enough data blocks in each cylinder 327 * group to meet the density requirement. Once this goal is achieved 328 * we try to expand to have at least mincylgrps cylinder groups. Once 329 * this goal is achieved, we pack as many blocks into each cylinder 330 * group map as will fit. 331 * 332 * We start by calculating the smallest number of blocks that we can 333 * put into each cylinder group. If this is too big, we reduce the 334 * density until it fits. 335 */ 336 origdensity = density; 337 for (;;) { 338 fragsperinode = MAXIMUM(numfrags(&sblock, density), 1); 339 340 minfpg = fragsperinode * INOPB(&sblock); 341 if (minfpg > sblock.fs_size) 342 minfpg = sblock.fs_size; 343 344 sblock.fs_ipg = INOPB(&sblock); 345 sblock.fs_fpg = roundup(sblock.fs_iblkno + 346 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 347 if (sblock.fs_fpg < minfpg) 348 sblock.fs_fpg = minfpg; 349 350 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 351 INOPB(&sblock)); 352 sblock.fs_fpg = roundup(sblock.fs_iblkno + 353 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 354 if (sblock.fs_fpg < minfpg) 355 sblock.fs_fpg = minfpg; 356 357 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 358 INOPB(&sblock)); 359 360 if (CGSIZE(&sblock) < (unsigned long)sblock.fs_bsize) 361 break; 362 363 density -= sblock.fs_fsize; 364 } 365 if (density != origdensity) 366 warnx("density reduced from %d to %d bytes per inode", 367 origdensity, density); 368 369 /* 370 * Use a lower value for mincylgrps if the user specified a large 371 * number of blocks per cylinder group. This is needed for, e.g. the 372 * install media which needs to pack 2 files very tightly. 373 */ 374 mincylgrps = MINCYLGRPS; 375 if (maxfrgspercg != INT_MAX) { 376 i = sblock.fs_size / maxfrgspercg; 377 if (i < MINCYLGRPS) 378 mincylgrps = i <= 0 ? 1 : i; 379 } 380 381 /* 382 * Start packing more blocks into the cylinder group until it cannot 383 * grow any larger, the number of cylinder groups drops below 384 * mincylgrps, or we reach the requested size. 385 */ 386 for (;;) { 387 sblock.fs_fpg += sblock.fs_frag; 388 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 389 INOPB(&sblock)); 390 391 if (sblock.fs_fpg > maxfrgspercg || 392 sblock.fs_size / sblock.fs_fpg < mincylgrps || 393 CGSIZE(&sblock) > (unsigned long)sblock.fs_bsize) 394 break; 395 } 396 sblock.fs_fpg -= sblock.fs_frag; 397 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 398 INOPB(&sblock)); 399 if (sblock.fs_fpg > maxfrgspercg) 400 warnx("can't honour -c: minimum is %d", sblock.fs_fpg); 401 402 /* 403 * Check to be sure that the last cylinder group has enough blocks to 404 * be viable. If it is too small, reduce the number of blocks per 405 * cylinder group which will have the effect of moving more blocks into 406 * the last cylinder group. 407 */ 408 optimalfpg = sblock.fs_fpg; 409 for (;;) { 410 sblock.fs_ncg = howmany(sblock.fs_size, sblock.fs_fpg); 411 lastminfpg = roundup(sblock.fs_iblkno + 412 sblock.fs_ipg / INOPF(&sblock), sblock.fs_frag); 413 if (sblock.fs_size < lastminfpg) 414 errx(28, "file system size %jd < minimum size of %d " 415 "fragments", (intmax_t)sblock.fs_size, lastminfpg); 416 417 if (sblock.fs_size % sblock.fs_fpg >= lastminfpg || 418 sblock.fs_size % sblock.fs_fpg == 0) 419 break; 420 421 sblock.fs_fpg -= sblock.fs_frag; 422 sblock.fs_ipg = roundup(howmany(sblock.fs_fpg, fragsperinode), 423 INOPB(&sblock)); 424 } 425 426 if (optimalfpg != sblock.fs_fpg) 427 warnx("reduced number of fragments per cylinder group from %d" 428 " to %d to enlarge last cylinder group", optimalfpg, 429 sblock.fs_fpg); 430 431 if ((ino_t)sblock.fs_ipg * sblock.fs_ncg > UINT_MAX) 432 errx(42, "more than 2^32 inodes, increase density, block or " 433 "fragment size"); 434 435 /* 436 * Back to filling superblock fields. 437 */ 438 if (Oflag <= 1) { 439 sblock.fs_spc = sblock.fs_fpg * sblock.fs_nspf; 440 sblock.fs_nsect = sblock.fs_spc; 441 sblock.fs_npsect = sblock.fs_spc; 442 sblock.fs_ncyl = sblock.fs_ncg; 443 } 444 sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); 445 sblock.fs_dblkno = sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock); 446 sblock.fs_csaddr = cgdmin(&sblock, 0); 447 sblock.fs_cssize = 448 fragroundup(&sblock, sblock.fs_ncg * sizeof(struct csum)); 449 450 fscs = calloc(1, sblock.fs_cssize); 451 if (fscs == NULL) 452 errx(31, "calloc failed"); 453 454 sblock.fs_sbsize = fragroundup(&sblock, sizeof(struct fs)); 455 if (sblock.fs_sbsize > SBLOCKSIZE) 456 sblock.fs_sbsize = SBLOCKSIZE; 457 458 sblock.fs_minfree = minfree; 459 sblock.fs_maxbpg = maxbpg; 460 sblock.fs_optim = opt; 461 sblock.fs_cgrotor = 0; 462 sblock.fs_pendingblocks = 0; 463 sblock.fs_pendinginodes = 0; 464 sblock.fs_fmod = 0; 465 sblock.fs_ronly = 0; 466 sblock.fs_state = 0; 467 sblock.fs_clean = 1; 468 sblock.fs_id[0] = (u_int32_t)utime; 469 sblock.fs_id[1] = (u_int32_t)arc4random(); 470 sblock.fs_fsmnt[0] = '\0'; 471 472 csfrags = howmany(sblock.fs_cssize, sblock.fs_fsize); 473 sblock.fs_dsize = sblock.fs_size - sblock.fs_sblkno - 474 sblock.fs_ncg * (sblock.fs_dblkno - sblock.fs_sblkno); 475 476 sblock.fs_cstotal.cs_nbfree = fragstoblks(&sblock, sblock.fs_dsize) - 477 howmany(csfrags, sblock.fs_frag); 478 sblock.fs_cstotal.cs_nffree = fragnum(&sblock, sblock.fs_size) + 479 (fragnum(&sblock, csfrags) > 0 ? 480 sblock.fs_frag - fragnum(&sblock, csfrags) : 0); 481 sblock.fs_cstotal.cs_nifree = sblock.fs_ncg * sblock.fs_ipg - ROOTINO; 482 sblock.fs_cstotal.cs_ndir = 0; 483 484 sblock.fs_dsize -= csfrags; 485 sblock.fs_time = utime; 486 487 if (Oflag <= 1) { 488 sblock.fs_ffs1_time = sblock.fs_time; 489 sblock.fs_ffs1_dsize = sblock.fs_dsize; 490 sblock.fs_ffs1_csaddr = sblock.fs_csaddr; 491 sblock.fs_ffs1_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 492 sblock.fs_ffs1_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 493 sblock.fs_ffs1_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 494 sblock.fs_ffs1_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 495 } 496 497 /* 498 * Dump out summary information about file system. 499 */ 500 if (!mfs) { 501 #define B2MBFACTOR (1 / (1024.0 * 1024.0)) 502 printf("%s: %.1fMB in %jd sectors of %lld bytes\n", fsys, 503 (float)sblock.fs_size * sblock.fs_fsize * B2MBFACTOR, 504 (intmax_t)fsbtodb(&sblock, sblock.fs_size) / 505 (sectorsize / DEV_BSIZE), sectorsize); 506 printf("%u cylinder groups of %.2fMB, %d blocks, %u" 507 " inodes each\n", sblock.fs_ncg, 508 (float)sblock.fs_fpg * sblock.fs_fsize * B2MBFACTOR, 509 sblock.fs_fpg / sblock.fs_frag, sblock.fs_ipg); 510 #undef B2MBFACTOR 511 checksz(); 512 } 513 514 /* 515 * Wipe out old FFS1 superblock if necessary. 516 */ 517 if (Oflag >= 2) { 518 union fs_u *fsun1; 519 struct fs *fs1; 520 521 fsun1 = calloc(1, sizeof(union fs_u)); 522 if (fsun1 == NULL) 523 err(39, "calloc"); 524 fs1 = &fsun1->fs; 525 rdfs(SBLOCK_UFS1 / DEV_BSIZE, SBSIZE, (char *)fs1); 526 if (fs1->fs_magic == FS_UFS1_MAGIC) { 527 fs1->fs_magic = FS_BAD_MAGIC; 528 wtfs(SBLOCK_UFS1 / DEV_BSIZE, SBSIZE, (char *)fs1); 529 } 530 free(fsun1); 531 } 532 533 wtfs((int)sblock.fs_sblockloc / DEV_BSIZE, SBSIZE, (char *)&sblock); 534 sblock.fs_magic = (Oflag <= 1) ? FS_UFS1_MAGIC : FS_UFS2_MAGIC; 535 536 /* 537 * Now build the cylinders group blocks and 538 * then print out indices of cylinder groups. 539 */ 540 if (!quiet) 541 printf("super-block backups (for fsck -b #) at:\n"); 542 #ifndef STANDALONE 543 else if (!mfs && isatty(STDIN_FILENO)) { 544 signal(SIGINFO, siginfo); 545 cur_fsys = fsys; 546 } 547 #endif 548 i = 0; 549 width = charsperline(); 550 /* 551 * Allocate space for superblock, cylinder group map, and two sets of 552 * inode blocks. 553 */ 554 if (sblock.fs_bsize < SBLOCKSIZE) 555 iobufsize = SBLOCKSIZE + 3 * sblock.fs_bsize; 556 else 557 iobufsize = 4 * sblock.fs_bsize; 558 if ((iobuf = malloc(iobufsize)) == NULL) 559 errx(38, "cannot allocate I/O buffer"); 560 bzero(iobuf, iobufsize); 561 /* 562 * Make a copy of the superblock into the buffer that we will be 563 * writing out in each cylinder group. 564 */ 565 bcopy((char *)&sblock, iobuf, SBLOCKSIZE); 566 for (cg = 0; cg < sblock.fs_ncg; cg++) { 567 cur_cylno = (sig_atomic_t)cg; 568 initcg(cg, utime); 569 if (quiet) 570 continue; 571 j = snprintf(tmpbuf, sizeof tmpbuf, " %lld,", 572 (long long)fsbtodb(&sblock, cgsblock(&sblock, cg))); 573 if (j >= sizeof tmpbuf) 574 j = sizeof tmpbuf - 1; 575 if (j < 0 || i+j >= width) { 576 printf("\n"); 577 i = 0; 578 } 579 i += j; 580 printf("%s", tmpbuf); 581 fflush(stdout); 582 } 583 if (!quiet) 584 printf("\n"); 585 if (Nflag && !mfs) 586 exit(0); 587 /* 588 * Now construct the initial file system, then write out the superblock. 589 */ 590 if (Oflag <= 1) { 591 if (fsinit1(utime, mfsmode, mfsuid, mfsgid)) 592 errx(32, "fsinit1 failed"); 593 sblock.fs_ffs1_cstotal.cs_ndir = sblock.fs_cstotal.cs_ndir; 594 sblock.fs_ffs1_cstotal.cs_nbfree = sblock.fs_cstotal.cs_nbfree; 595 sblock.fs_ffs1_cstotal.cs_nifree = sblock.fs_cstotal.cs_nifree; 596 sblock.fs_ffs1_cstotal.cs_nffree = sblock.fs_cstotal.cs_nffree; 597 } else { 598 if (fsinit2(utime, mfsmode, mfsuid, mfsgid)) 599 errx(32, "fsinit2 failed"); 600 } 601 602 wtfs((int)sblock.fs_sblockloc / DEV_BSIZE, SBSIZE, (char *)&sblock); 603 604 for (i = 0; i < sblock.fs_cssize; i += sblock.fs_bsize) 605 wtfs(fsbtodb(&sblock, sblock.fs_csaddr + numfrags(&sblock, i)), 606 sblock.fs_cssize - i < sblock.fs_bsize ? 607 sblock.fs_cssize - i : sblock.fs_bsize, 608 ((char *)fscs) + i); 609 610 /* 611 * Update information about this partition in pack label, to that it may 612 * be updated on disk. 613 */ 614 pp->p_fstype = FS_BSDFFS; 615 pp->p_fragblock = 616 DISKLABELV1_FFS_FRAGBLOCK(sblock.fs_fsize, sblock.fs_frag); 617 bpg = sblock.fs_fpg / sblock.fs_frag; 618 while (bpg > USHRT_MAX) 619 bpg >>= 1; 620 pp->p_cpg = bpg; 621 } 622 623 /* 624 * Initialize a cylinder group. 625 */ 626 void 627 initcg(u_int cg, time_t utime) 628 { 629 u_int i, j, d, dlower, dupper, blkno, start; 630 daddr_t cbase, dmax; 631 struct ufs1_dinode *dp1; 632 struct ufs2_dinode *dp2; 633 struct csum *cs; 634 635 /* 636 * Determine block bounds for cylinder group. Allow space for 637 * super block summary information in first cylinder group. 638 */ 639 cbase = cgbase(&sblock, cg); 640 dmax = cbase + sblock.fs_fpg; 641 if (dmax > sblock.fs_size) 642 dmax = sblock.fs_size; 643 if (fsbtodb(&sblock, cgsblock(&sblock, cg)) + iobufsize / DEV_BSIZE 644 > fssize) 645 errx(40, "inode table does not fit in cylinder group"); 646 647 dlower = cgsblock(&sblock, cg) - cbase; 648 dupper = cgdmin(&sblock, cg) - cbase; 649 if (cg == 0) 650 dupper += howmany(sblock.fs_cssize, sblock.fs_fsize); 651 cs = &fscs[cg]; 652 memset(&acg, 0, sblock.fs_cgsize); 653 acg.cg_ffs2_time = utime; 654 acg.cg_magic = CG_MAGIC; 655 acg.cg_cgx = cg; 656 acg.cg_ffs2_niblk = sblock.fs_ipg; 657 acg.cg_initediblk = MINIMUM(sblock.fs_ipg, 2 * INOPB(&sblock)); 658 acg.cg_ndblk = dmax - cbase; 659 660 start = sizeof(struct cg); 661 if (Oflag <= 1) { 662 /* Hack to maintain compatibility with old fsck. */ 663 if (cg == sblock.fs_ncg - 1) 664 acg.cg_ncyl = 0; 665 else 666 acg.cg_ncyl = sblock.fs_cpg; 667 acg.cg_time = acg.cg_ffs2_time; 668 acg.cg_ffs2_time = 0; 669 acg.cg_niblk = acg.cg_ffs2_niblk; 670 acg.cg_ffs2_niblk = 0; 671 acg.cg_initediblk = 0; 672 acg.cg_btotoff = start; 673 acg.cg_boff = acg.cg_btotoff + sblock.fs_cpg * sizeof(int32_t); 674 acg.cg_iusedoff = acg.cg_boff + 675 sblock.fs_cpg * sizeof(u_int16_t); 676 } else { 677 acg.cg_iusedoff = start; 678 } 679 680 acg.cg_freeoff = acg.cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT); 681 acg.cg_nextfreeoff = acg.cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT); 682 if (acg.cg_nextfreeoff > sblock.fs_cgsize) 683 errx(37, "panic: cylinder group too big: %u > %d", 684 acg.cg_nextfreeoff, sblock.fs_cgsize); 685 acg.cg_cs.cs_nifree += sblock.fs_ipg; 686 if (cg == 0) { 687 for (i = 0; i < ROOTINO; i++) { 688 setbit(cg_inosused(&acg), i); 689 acg.cg_cs.cs_nifree--; 690 } 691 } 692 if (cg > 0) { 693 /* 694 * In cg 0, space is reserved for boot and super blocks. 695 */ 696 for (d = 0; d < dlower; d += sblock.fs_frag) { 697 blkno = d / sblock.fs_frag; 698 setblock(&sblock, cg_blksfree(&acg), blkno); 699 acg.cg_cs.cs_nbfree++; 700 if (Oflag <= 1) { 701 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 702 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 703 [cbtorpos(&sblock, d)]++; 704 } 705 } 706 } 707 if ((i = dupper % sblock.fs_frag)) { 708 acg.cg_frsum[sblock.fs_frag - i]++; 709 for (d = dupper + sblock.fs_frag - i; dupper < d; dupper++) { 710 setbit(cg_blksfree(&acg), dupper); 711 acg.cg_cs.cs_nffree++; 712 } 713 } 714 for (d = dupper; 715 d + sblock.fs_frag <= acg.cg_ndblk; 716 d += sblock.fs_frag) { 717 blkno = d / sblock.fs_frag; 718 setblock(&sblock, cg_blksfree(&acg), blkno); 719 acg.cg_cs.cs_nbfree++; 720 if (Oflag <= 1) { 721 cg_blktot(&acg)[cbtocylno(&sblock, d)]++; 722 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 723 [cbtorpos(&sblock, d)]++; 724 } 725 } 726 if (d < acg.cg_ndblk) { 727 acg.cg_frsum[acg.cg_ndblk - d]++; 728 for (; d < acg.cg_ndblk; d++) { 729 setbit(cg_blksfree(&acg), d); 730 acg.cg_cs.cs_nffree++; 731 } 732 } 733 *cs = acg.cg_cs; 734 735 /* 736 * Write out the duplicate superblock, the cylinder group map 737 * and two blocks worth of inodes in a single write. 738 */ 739 start = sblock.fs_bsize > SBLOCKSIZE ? sblock.fs_bsize : SBLOCKSIZE; 740 741 if (cg == 0 && acg.cg_cs.cs_nbfree == 0) 742 errx(42, "cg 0: summary info is too large to fit"); 743 744 bcopy((char *)&acg, &iobuf[start], sblock.fs_cgsize); 745 start += sblock.fs_bsize; 746 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 747 dp2 = (struct ufs2_dinode *)(&iobuf[start]); 748 for (i = MINIMUM(sblock.fs_ipg, 2 * INOPB(&sblock)); i != 0; i--) { 749 if (sblock.fs_magic == FS_UFS1_MAGIC) { 750 dp1->di_gen = arc4random(); 751 dp1++; 752 } else { 753 dp2->di_gen = arc4random(); 754 dp2++; 755 } 756 } 757 wtfs(fsbtodb(&sblock, cgsblock(&sblock, cg)), iobufsize, iobuf); 758 759 if (Oflag <= 1) { 760 /* Initialize inodes for FFS1. */ 761 for (i = 2 * sblock.fs_frag; 762 i < sblock.fs_ipg / INOPF(&sblock); 763 i += sblock.fs_frag) { 764 dp1 = (struct ufs1_dinode *)(&iobuf[start]); 765 for (j = 0; j < INOPB(&sblock); j++) { 766 dp1->di_gen = arc4random(); 767 dp1++; 768 } 769 wtfs(fsbtodb(&sblock, cgimin(&sblock, cg) + i), 770 sblock.fs_bsize, &iobuf[start]); 771 } 772 } 773 } 774 775 #define PREDEFDIR 2 776 777 struct direct root_dir[] = { 778 { ROOTINO, sizeof(struct direct), DT_DIR, 1, "." }, 779 { ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, 780 }; 781 struct odirect { 782 u_int32_t d_ino; 783 u_int16_t d_reclen; 784 u_int16_t d_namlen; 785 u_char d_name[MAXNAMLEN + 1]; 786 } oroot_dir[] = { 787 { ROOTINO, sizeof(struct direct), 1, "." }, 788 { ROOTINO, sizeof(struct direct), 2, ".." }, 789 }; 790 791 int 792 fsinit1(time_t utime, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid) 793 { 794 union dinode node; 795 796 /* 797 * Initialize the node 798 */ 799 memset(&node, 0, sizeof(node)); 800 node.dp1.di_atime = utime; 801 node.dp1.di_mtime = utime; 802 node.dp1.di_ctime = utime; 803 804 /* 805 * Create the root directory. 806 */ 807 if (mfs) { 808 node.dp1.di_mode = IFDIR | mfsmode; 809 node.dp1.di_uid = mfsuid; 810 node.dp1.di_gid = mfsgid; 811 } else { 812 node.dp1.di_mode = IFDIR | UMASK; 813 node.dp1.di_uid = geteuid(); 814 node.dp1.di_gid = getegid(); 815 } 816 node.dp1.di_nlink = PREDEFDIR; 817 if (Oflag == 0) 818 node.dp1.di_size = makedir((struct direct *)oroot_dir, 819 PREDEFDIR); 820 else 821 node.dp1.di_size = makedir(root_dir, PREDEFDIR); 822 node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode); 823 if (node.dp1.di_db[0] == 0) 824 return (1); 825 826 node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size)); 827 828 wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, iobuf); 829 iput(&node, ROOTINO); 830 831 #ifdef notyet 832 /* 833 * Create the .snap directory. 834 */ 835 node.dp1.di_mode |= 020; 836 node.dp1.di_gid = gid; 837 node.dp1.di_nlink = SNAPLINKCNT; 838 node.dp1.di_size = makedir(snap_dir, SNAPLINKCNT); 839 840 node.dp1.di_db[0] = alloc(sblock.fs_fsize, node.dp1.di_mode); 841 if (node.dp1.di_db[0] == 0) 842 return (1); 843 844 node.dp1.di_blocks = btodb(fragroundup(&sblock, node.dp1.di_size)); 845 846 wtfs(fsbtodb(&sblock, node.dp1.di_db[0]), sblock.fs_fsize, iobuf); 847 iput(&node, ROOTINO + 1); 848 #endif 849 return (0); 850 } 851 852 int 853 fsinit2(time_t utime, mode_t mfsmode, uid_t mfsuid, gid_t mfsgid) 854 { 855 union dinode node; 856 857 /* 858 * Initialize the node. 859 */ 860 memset(&node, 0, sizeof(node)); 861 node.dp2.di_atime = utime; 862 node.dp2.di_mtime = utime; 863 node.dp2.di_ctime = utime; 864 865 /* 866 * Create the root directory. 867 */ 868 if (mfs) { 869 node.dp2.di_mode = IFDIR | mfsmode; 870 node.dp2.di_uid = mfsuid; 871 node.dp2.di_gid = mfsgid; 872 } else { 873 node.dp2.di_mode = IFDIR | UMASK; 874 node.dp2.di_uid = geteuid(); 875 node.dp2.di_gid = getegid(); 876 } 877 node.dp2.di_nlink = PREDEFDIR; 878 node.dp2.di_size = makedir(root_dir, PREDEFDIR); 879 880 node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode); 881 if (node.dp2.di_db[0] == 0) 882 return (1); 883 884 node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size)); 885 886 wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, iobuf); 887 iput(&node, ROOTINO); 888 889 #ifdef notyet 890 /* 891 * Create the .snap directory. 892 */ 893 node.dp2.di_mode |= 020; 894 node.dp2.di_gid = gid; 895 node.dp2.di_nlink = SNAPLINKCNT; 896 node.dp2.di_size = makedir(snap_dir, SNAPLINKCNT); 897 898 node.dp2.di_db[0] = alloc(sblock.fs_fsize, node.dp2.di_mode); 899 if (node.dp2.di_db[0] == 0) 900 return (1); 901 902 node.dp2.di_blocks = btodb(fragroundup(&sblock, node.dp2.di_size)); 903 904 wtfs(fsbtodb(&sblock, node.dp2.di_db[0]), sblock.fs_fsize, iobuf); 905 iput(&node, ROOTINO + 1); 906 #endif 907 return (0); 908 } 909 910 /* 911 * construct a set of directory entries in "buf". 912 * return size of directory. 913 */ 914 int 915 makedir(struct direct *protodir, int entries) 916 { 917 char *cp; 918 int i, spcleft; 919 920 spcleft = DIRBLKSIZ; 921 for (cp = iobuf, i = 0; i < entries - 1; i++) { 922 protodir[i].d_reclen = DIRSIZ(0, &protodir[i]); 923 memcpy(cp, &protodir[i], protodir[i].d_reclen); 924 cp += protodir[i].d_reclen; 925 spcleft -= protodir[i].d_reclen; 926 } 927 protodir[i].d_reclen = spcleft; 928 memcpy(cp, &protodir[i], DIRSIZ(0, &protodir[i])); 929 return (DIRBLKSIZ); 930 } 931 932 /* 933 * allocate a block or frag 934 */ 935 daddr_t 936 alloc(int size, int mode) 937 { 938 int i, frag; 939 daddr_t d, blkno; 940 941 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 942 (char *)&acg); 943 if (acg.cg_magic != CG_MAGIC) { 944 warnx("cg 0: bad magic number"); 945 return (0); 946 } 947 if (acg.cg_cs.cs_nbfree == 0) { 948 warnx("first cylinder group ran out of space"); 949 return (0); 950 } 951 for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) 952 if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) 953 goto goth; 954 warnx("internal error: can't find block in cyl 0"); 955 return (0); 956 goth: 957 blkno = fragstoblks(&sblock, d); 958 clrblock(&sblock, cg_blksfree(&acg), blkno); 959 acg.cg_cs.cs_nbfree--; 960 sblock.fs_cstotal.cs_nbfree--; 961 fscs[0].cs_nbfree--; 962 if (mode & IFDIR) { 963 acg.cg_cs.cs_ndir++; 964 sblock.fs_cstotal.cs_ndir++; 965 fscs[0].cs_ndir++; 966 } 967 if (Oflag <= 1) { 968 cg_blktot(&acg)[cbtocylno(&sblock, d)]--; 969 cg_blks(&sblock, &acg, cbtocylno(&sblock, d)) 970 [cbtorpos(&sblock, d)]--; 971 } 972 if (size != sblock.fs_bsize) { 973 frag = howmany(size, sblock.fs_fsize); 974 fscs[0].cs_nffree += sblock.fs_frag - frag; 975 sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; 976 acg.cg_cs.cs_nffree += sblock.fs_frag - frag; 977 acg.cg_frsum[sblock.fs_frag - frag]++; 978 for (i = frag; i < sblock.fs_frag; i++) 979 setbit(cg_blksfree(&acg), d + i); 980 } 981 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 982 (char *)&acg); 983 return (d); 984 } 985 986 /* 987 * Allocate an inode on the disk 988 */ 989 void 990 iput(union dinode *ip, ino_t ino) 991 { 992 daddr_t d; 993 994 if (Oflag <= 1) 995 ip->dp1.di_gen = arc4random(); 996 else 997 ip->dp2.di_gen = arc4random(); 998 999 rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1000 (char *)&acg); 1001 if (acg.cg_magic != CG_MAGIC) 1002 errx(41, "cg 0: bad magic number"); 1003 1004 acg.cg_cs.cs_nifree--; 1005 setbit(cg_inosused(&acg), ino); 1006 1007 wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, 1008 (char *)&acg); 1009 1010 sblock.fs_cstotal.cs_nifree--; 1011 fscs[0].cs_nifree--; 1012 if (ino >= sblock.fs_ipg * sblock.fs_ncg) 1013 errx(32, "fsinit: inode value %llu out of range", 1014 (unsigned long long)ino); 1015 d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); 1016 rdfs(d, sblock.fs_bsize, iobuf); 1017 1018 if (Oflag <= 1) 1019 ((struct ufs1_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] = 1020 ip->dp1; 1021 else 1022 ((struct ufs2_dinode *)iobuf)[ino_to_fsbo(&sblock, ino)] = 1023 ip->dp2; 1024 1025 wtfs(d, sblock.fs_bsize, iobuf); 1026 } 1027 1028 /* 1029 * read a block from the file system 1030 */ 1031 void 1032 rdfs(daddr_t bno, int size, void *bf) 1033 { 1034 int n; 1035 1036 if (mfs) { 1037 memcpy(bf, membase + bno * DEV_BSIZE, size); 1038 return; 1039 } 1040 n = pread(fsi, bf, size, (off_t)bno * DEV_BSIZE); 1041 if (n != size) { 1042 err(34, "rdfs: read error on block %lld", (long long)bno); 1043 } 1044 } 1045 1046 /* 1047 * write a block to the file system 1048 */ 1049 void 1050 wtfs(daddr_t bno, int size, void *bf) 1051 { 1052 int n; 1053 1054 if (mfs) { 1055 memcpy(membase + bno * DEV_BSIZE, bf, size); 1056 return; 1057 } 1058 if (Nflag) 1059 return; 1060 n = pwrite(fso, bf, size, (off_t)bno * DEV_BSIZE); 1061 if (n != size) { 1062 err(36, "wtfs: write error on block %lld", (long long)bno); 1063 } 1064 } 1065 1066 /* 1067 * check if a block is available 1068 */ 1069 int 1070 isblock(struct fs *fs, unsigned char *cp, int h) 1071 { 1072 unsigned char mask; 1073 1074 switch (fs->fs_frag) { 1075 case 8: 1076 return (cp[h] == 0xff); 1077 case 4: 1078 mask = 0x0f << ((h & 0x1) << 2); 1079 return ((cp[h >> 1] & mask) == mask); 1080 case 2: 1081 mask = 0x03 << ((h & 0x3) << 1); 1082 return ((cp[h >> 2] & mask) == mask); 1083 case 1: 1084 mask = 0x01 << (h & 0x7); 1085 return ((cp[h >> 3] & mask) == mask); 1086 default: 1087 #ifdef STANDALONE 1088 printf("isblock bad fs_frag %d\n", fs->fs_frag); 1089 #else 1090 warnx("isblock bad fs_frag %d", fs->fs_frag); 1091 #endif 1092 return (0); 1093 } 1094 } 1095 1096 /* 1097 * take a block out of the map 1098 */ 1099 void 1100 clrblock(struct fs *fs, unsigned char *cp, int h) 1101 { 1102 switch ((fs)->fs_frag) { 1103 case 8: 1104 cp[h] = 0; 1105 return; 1106 case 4: 1107 cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); 1108 return; 1109 case 2: 1110 cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); 1111 return; 1112 case 1: 1113 cp[h >> 3] &= ~(0x01 << (h & 0x7)); 1114 return; 1115 default: 1116 #ifdef STANDALONE 1117 printf("clrblock bad fs_frag %d\n", fs->fs_frag); 1118 #else 1119 warnx("clrblock bad fs_frag %d", fs->fs_frag); 1120 #endif 1121 return; 1122 } 1123 } 1124 1125 /* 1126 * put a block into the map 1127 */ 1128 void 1129 setblock(struct fs *fs, unsigned char *cp, int h) 1130 { 1131 switch (fs->fs_frag) { 1132 case 8: 1133 cp[h] = 0xff; 1134 return; 1135 case 4: 1136 cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); 1137 return; 1138 case 2: 1139 cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); 1140 return; 1141 case 1: 1142 cp[h >> 3] |= (0x01 << (h & 0x7)); 1143 return; 1144 default: 1145 #ifdef STANDALONE 1146 printf("setblock bad fs_frag %d\n", fs->fs_frag); 1147 #else 1148 warnx("setblock bad fs_frag %d", fs->fs_frag); 1149 #endif 1150 return; 1151 } 1152 } 1153 1154 /* 1155 * Determine the number of characters in a 1156 * single line. 1157 */ 1158 static int 1159 charsperline(void) 1160 { 1161 int columns; 1162 char *cp; 1163 struct winsize ws; 1164 1165 columns = 0; 1166 if ((cp = getenv("COLUMNS")) != NULL) 1167 columns = strtonum(cp, 1, INT_MAX, NULL); 1168 if (columns == 0 && ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && 1169 ws.ws_col > 0) 1170 columns = ws.ws_col; 1171 if (columns == 0) 1172 columns = 80; 1173 1174 return columns; 1175 } 1176 1177 static int 1178 ilog2(int val) 1179 { 1180 int n; 1181 1182 for (n = 0; n < sizeof(n) * CHAR_BIT; n++) 1183 if (1 << n == val) 1184 return (n); 1185 1186 errx(1, "ilog2: %d is not a power of 2\n", val); 1187 } 1188 1189 struct inoinfo { 1190 struct inoinfo *i_nexthash; /* next entry in hash chain */ 1191 struct inoinfo *i_child, *i_sibling, *i_parentp; 1192 size_t i_isize; /* size of inode */ 1193 ino_t i_number; /* inode number of this entry */ 1194 ino_t i_parent; /* inode number of parent */ 1195 1196 ino_t i_dotdot; /* inode number of `..' */ 1197 u_int i_numblks; /* size of block array in bytes */ 1198 daddr_t i_blks[1]; /* actually longer */ 1199 }; 1200 1201 static void 1202 checksz(void) 1203 { 1204 unsigned long long allocate, maxino, maxfsblock, ndir, bound; 1205 extern int64_t physmem; 1206 struct rlimit datasz; 1207 1208 if (getrlimit(RLIMIT_DATA, &datasz) != 0) 1209 err(1, "can't get rlimit"); 1210 1211 bound = MINIMUM(datasz.rlim_max, physmem); 1212 1213 allocate = 0; 1214 maxino = sblock.fs_ncg * (unsigned long long)sblock.fs_ipg; 1215 maxfsblock = sblock.fs_size; 1216 ndir = maxino / avgfilesperdir; 1217 1218 allocate += roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); 1219 allocate += (maxino + 1) * 3; 1220 allocate += sblock.fs_ncg * sizeof(long); 1221 allocate += (MAXIMUM(ndir, 128) + 10) * sizeof(struct inoinfo); 1222 allocate += MAXIMUM(ndir, 128) * sizeof(struct inoinfo); 1223 1224 if (allocate > bound) 1225 warnx("warning: fsck_ffs will need %lluMB; " 1226 "min(ulimit -dH,physmem) is %lluMB", 1227 allocate / (1024ULL * 1024ULL), 1228 bound / (1024ULL * 1024ULL)); 1229 } 1230