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