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