1 /* $NetBSD: newfs.c,v 1.122 2026/05/03 17:49:58 kre Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 2002 Networks Associates Technology, Inc. 34 * All rights reserved. 35 * 36 * This software was developed for the FreeBSD Project by Marshall 37 * Kirk McKusick and Network Associates Laboratories, the Security 38 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 39 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 40 * research program 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #include <sys/cdefs.h> 72 #ifndef lint 73 __COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1993, 1994\ 74 The Regents of the University of California. All rights reserved."); 75 #endif /* not lint */ 76 77 #ifndef lint 78 #if 0 79 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95"; 80 #else 81 __RCSID("$NetBSD: newfs.c,v 1.122 2026/05/03 17:49:58 kre Exp $"); 82 #endif 83 #endif /* not lint */ 84 85 /* 86 * newfs: friendly front end to mkfs 87 */ 88 #include <sys/param.h> 89 #include <sys/ioctl.h> 90 #include <sys/disklabel.h> 91 #include <sys/disk.h> 92 #include <sys/file.h> 93 #include <sys/mount.h> 94 #include <sys/sysctl.h> 95 #include <sys/wait.h> 96 97 #include <ufs/ufs/dir.h> 98 #include <ufs/ufs/dinode.h> 99 #include <ufs/ufs/ufsmount.h> 100 #include <ufs/ufs/quota2.h> 101 #include <ufs/ffs/fs.h> 102 103 #include <ctype.h> 104 #include <disktab.h> 105 #include <err.h> 106 #include <errno.h> 107 #include <grp.h> 108 #include <limits.h> 109 #include <paths.h> 110 #include <pwd.h> 111 #include <signal.h> 112 #include <stdint.h> 113 #include <stdio.h> 114 #include <stdlib.h> 115 #include <string.h> 116 #include <syslog.h> 117 #include <unistd.h> 118 #include <util.h> 119 #include <mntopts.h> 120 121 #ifdef MFS 122 #include <mountprog.h> 123 #endif 124 125 #include "dkcksum.h" 126 #include "extern.h" 127 #include "partutil.h" 128 129 struct mntopt mopts[] = { 130 MOPT_STDOPTS, 131 MOPT_ASYNC, 132 MOPT_UPDATE, 133 MOPT_GETARGS, 134 MOPT_NOATIME, 135 { .m_option = NULL }, 136 }; 137 138 static gid_t mfs_group(const char *); 139 static uid_t mfs_user(const char *); 140 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *); 141 static void usage(void) __dead; 142 143 #define COMPAT /* allow non-labeled disks */ 144 145 #ifdef COMPAT 146 const char lmsg[] = "%s: can't read disk label; disk type must be specified"; 147 #else 148 const char lmsg[] = "%s: can't read disk label"; 149 #endif 150 151 /* 152 * The following two constants set the default block and fragment sizes. 153 * Both constants must be a power of 2 and meet the following constraints: 154 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 155 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 156 * DESBLKSIZE / DESFRAGSIZE <= 8 157 */ 158 /* 159 * For file systems smaller than SMALL_FSSIZE we use the S_DFL_* defaults, 160 * otherwise if less than MEDIUM_FSSIZE use M_DFL_*, 161 * otherwise if less than LARGE_FSSIZE use L_DFL_*, 162 * otherwise use LL_DFL_* especially for modern AFT disks. 163 */ 164 #define SMALL_FSSIZE (20*1024*2) 165 #define S_DFL_FRAGSIZE 512 166 #define MEDIUM_FSSIZE (1000*1024*2) 167 #define M_DFL_FRAGSIZE 1024 168 #define LARGE_FSSIZE (128*1024*1024*2) 169 #define L_DFL_FRAGSIZE 2048 170 #define LL_DFL_FRAGSIZE 4096 171 #define DFL_FRAG_BLK 8 172 173 /* Apple requires the fragment size to be at least APPLEUFS_DIRBLKSIZ 174 * but the block size cannot be larger than Darwin's PAGE_SIZE. See 175 * the mount check in Darwin's ffs_mountfs for an explanation. 176 */ 177 #define APPLEUFS_DFL_FRAGSIZE APPLEUFS_DIRBLKSIZ /* 1024 */ 178 #define APPLEUFS_DFL_BLKSIZE 4096 /* default Darwin PAGE_SIZE */ 179 180 /* 181 * Default sector size. 182 */ 183 #define DFL_SECSIZE 512 184 185 /* 186 * Default file system size for "mount_mfs swap /dir" case. 187 */ 188 #define DFL_FSSIZE (8 * 1024 * 1024) 189 190 /* 191 * MAXBLKPG determines the maximum number of data blocks which are 192 * placed in a single cylinder group. The default is one indirect 193 * block worth of data blocks. 194 */ 195 #define MAXBLKPG_UFS1(bsize) ((bsize) / sizeof(int32_t)) 196 #define MAXBLKPG_UFS2(bsize) ((bsize) / sizeof(int64_t)) 197 198 /* 199 * Each file system has a number of inodes statically allocated. 200 * We allocate one inode slot per NFPI fragments, expecting this 201 * to be far more than we will ever need. 202 */ 203 #define NFPI 4 204 205 206 int mfs; /* run as the memory based filesystem */ 207 int Gflag; /* allow garbage parameters (for testing) */ 208 int Nflag; /* run without writing file system */ 209 int Oflag = 1; /* format as an 4.3BSD file system */ 210 int eaflag; /* use UFS2ea fs_magic */ 211 int verbosity; /* amount of printf() output */ 212 #define DEFAULT_VERBOSITY 3 /* 4 is traditional behavior */ 213 int64_t fssize; /* file system size */ 214 int sectorsize; /* bytes/sector */ 215 int fsize = 0; /* fragment size */ 216 int bsize = 0; /* block size */ 217 int maxbsize = 0; /* maximum clustering */ 218 int minfree = MINFREE; /* free space threshold */ 219 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 220 int density; /* number of bytes per inode */ 221 int num_inodes; /* number of inodes (overrides density) */ 222 int maxcontig = 0; /* max contiguous blocks to allocate */ 223 int maxbpg; /* maximum blocks per file in a cyl group */ 224 int avgfilesize = AVFILESIZ;/* expected average file size */ 225 int avgfpdir = AFPDIR; /* expected number of files per directory */ 226 int mntflags = 0; /* flags to be passed to mount */ 227 u_long memleft; /* virtual memory available */ 228 caddr_t membase; /* start address of memory based filesystem */ 229 #ifndef NO_FFS_EI 230 int needswap; /* Filesystem not in native byte order */ 231 #endif 232 char *disktype = NULL; 233 int unlabeled; 234 #ifndef NO_APPLE_UFS 235 char *appleufs_volname = 0; /* Apple UFS volume name */ 236 int isappleufs = 0; 237 #endif 238 int quotas = 0; 239 240 char device[MAXPATHLEN]; 241 242 int 243 main(int argc, char *argv[]) 244 { 245 struct disk_geom geo; 246 struct dkwedge_info dkw; 247 struct statvfs *mp; 248 struct stat sb; 249 int ch, fsi, fso, len, n, Fflag, Iflag, Zflag; 250 const char *s1, *special; 251 char *s2; 252 const char *opstring; 253 int byte_sized = 0; 254 #ifdef MFS 255 struct mfs_args args; 256 char mountfromname[100]; 257 char mounttoname[MAXPATHLEN]; 258 pid_t pid, res; 259 struct statvfs sf; 260 int status; 261 #endif 262 mode_t mfsmode = 01777; /* default mode for a /tmp-type directory */ 263 uid_t mfsuid = 0; /* user root */ 264 gid_t mfsgid = 0; /* group wheel */ 265 mntoptparse_t mo; 266 267 fsi = fso = -1; 268 Fflag = Iflag = Zflag = 0; 269 verbosity = -1; 270 if (strstr(getprogname(), "mfs")) { 271 mfs = 1; 272 } else { 273 /* Undocumented, for ease of testing */ 274 if (argv[1] != NULL && !strcmp(argv[1], "-mfs")) { 275 argv++; 276 argc--; 277 mfs = 1; 278 } 279 } 280 281 opstring = mfs ? 282 "NT:V:a:b:d:e:f:g:h:i:m:n:o:p:q:s:u:" : 283 "B:FGINO:S:T:V:Za:b:d:e:f:g:h:i:l:m:n:o:q:r:s:v:"; 284 while ((ch = getopt(argc, argv, opstring)) != -1) 285 switch (ch) { 286 #ifndef NO_FFS_EI 287 case 'B': 288 if (strcmp(optarg, "be") == 0) { 289 # if BYTE_ORDER == LITTLE_ENDIAN 290 needswap = 1; 291 # endif 292 } else if (strcmp(optarg, "le") == 0) { 293 # if BYTE_ORDER == BIG_ENDIAN 294 needswap = 1; 295 # endif 296 } else 297 usage(); 298 break; 299 #endif 300 case 'F': 301 Fflag = 1; 302 break; 303 case 'G': 304 fprintf(stderr, "WARNING: -G may create file systems " 305 "which cause kernel panics\n"); 306 Gflag = 1; 307 break; 308 case 'I': 309 Iflag = 1; 310 break; 311 case 'N': 312 Nflag = 1; 313 if (verbosity == -1) 314 verbosity = DEFAULT_VERBOSITY; 315 break; 316 case 'O': 317 if (strcmp(optarg, "2ea") == 0) { 318 eaflag = 1; 319 optarg[1] = 0; 320 } 321 Oflag = strsuftoi64("format", optarg, 0, 2, NULL); 322 break; 323 case 'S': 324 /* XXX: non-512 byte sectors almost certainly don't work. */ 325 sectorsize = strsuftoi64("sector size", 326 optarg, 512, 65536, NULL); 327 if (sectorsize & (sectorsize - 1)) 328 errx(1, "sector size `%s' is not a power of 2.", 329 optarg); 330 break; 331 #ifdef COMPAT 332 case 'T': 333 disktype = optarg; 334 break; 335 #endif 336 case 'V': 337 verbosity = strsuftoi64("verbose", optarg, 0, 4, NULL); 338 break; 339 case 'Z': 340 Zflag = 1; 341 break; 342 case 'a': 343 maxcontig = strsuftoi64("maximum contiguous blocks", 344 optarg, 1, INT_MAX, NULL); 345 break; 346 case 'b': 347 bsize = strsuftoi64("block size", 348 optarg, MINBSIZE, MAXBSIZE, NULL); 349 break; 350 case 'd': 351 maxbsize = strsuftoi64("maximum extent size", 352 optarg, 0, INT_MAX, NULL); 353 break; 354 case 'e': 355 maxbpg = strsuftoi64( 356 "blocks per file in a cylinder group", 357 optarg, 1, INT_MAX, NULL); 358 break; 359 case 'f': 360 fsize = strsuftoi64("fragment size", 361 optarg, 1, MAXBSIZE, NULL); 362 break; 363 case 'g': 364 if (mfs) 365 mfsgid = mfs_group(optarg); 366 else { 367 avgfilesize = strsuftoi64("average file size", 368 optarg, 1, INT_MAX, NULL); 369 } 370 break; 371 case 'h': 372 avgfpdir = strsuftoi64("expected files per directory", 373 optarg, 1, INT_MAX, NULL); 374 break; 375 case 'i': 376 density = strsuftoi64("bytes per inode", 377 optarg, 1, INT_MAX, NULL); 378 break; 379 case 'm': 380 minfree = strsuftoi64("free space %", 381 optarg, 0, 99, NULL); 382 break; 383 case 'n': 384 num_inodes = strsuftoi64("number of inodes", 385 optarg, 1, INT_MAX, NULL); 386 break; 387 case 'o': 388 if (mfs) { 389 mo = getmntopts(optarg, mopts, &mntflags, 0); 390 if (mo == NULL) 391 err(1, "getmntopts"); 392 freemntopts(mo); 393 } else { 394 if (strcmp(optarg, "space") == 0) 395 opt = FS_OPTSPACE; 396 else if (strcmp(optarg, "time") == 0) 397 opt = FS_OPTTIME; 398 else 399 errx(1, "%s %s", 400 "unknown optimization preference: ", 401 "use `space' or `time'."); 402 } 403 break; 404 case 'q': 405 if (strcmp(optarg, "user") == 0) 406 quotas |= FS_Q2_DO_TYPE(USRQUOTA); 407 else if (strcmp(optarg, "group") == 0) 408 quotas |= FS_Q2_DO_TYPE(GRPQUOTA); 409 else 410 errx(1, "invalid quota type %s", optarg); 411 break; 412 case 'p': 413 /* mfs only */ 414 if ((mfsmode = strtol(optarg, NULL, 8)) <= 0) 415 errx(1, "bad mode `%s'", optarg); 416 break; 417 case 's': 418 fssize = strsuftoi64("file system size", 419 optarg, INT64_MIN, INT64_MAX, &byte_sized); 420 break; 421 case 'u': 422 /* mfs only */ 423 mfsuid = mfs_user(optarg); 424 break; 425 #ifndef NO_APPLE_UFS 426 case 'v': 427 appleufs_volname = optarg; 428 if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/')) 429 errx(1,"Apple UFS volume name cannot contain ':' or '/'"); 430 if (appleufs_volname[0] == '\0') 431 errx(1,"Apple UFS volume name cannot be zero length"); 432 isappleufs = 1; 433 break; 434 #endif 435 case '?': 436 default: 437 usage(); 438 } 439 argc -= optind; 440 argv += optind; 441 442 if (Oflag < 1 && quotas != 0) 443 errx(1, "in-filesystem quota is incompatible with -O0"); 444 445 if (verbosity == -1) 446 /* Default to not showing CG info if mfs */ 447 verbosity = mfs ? 0 : DEFAULT_VERBOSITY; 448 449 #ifdef MFS 450 /* This is enough to get through the correct kernel code paths */ 451 memset(&args, 0, sizeof args); 452 args.fspec = mountfromname; 453 if (mntflags & (MNT_GETARGS | MNT_UPDATE)) { 454 if ((mntflags & MNT_GETARGS) == 0) 455 mntflags |= MNT_ASYNC; 456 if (mount(MOUNT_MFS, argv[1], mntflags, 457 &args, sizeof args) == -1) 458 err(1, "mount `%s' failed", argv[1]); 459 if (mntflags & MNT_GETARGS) 460 printf("base=%p, size=%ld\n", args.base, args.size); 461 exit(0); 462 } 463 #endif 464 465 if (argc != 2 && (mfs || argc != 1)) 466 usage(); 467 468 memset(&sb, 0, sizeof sb); 469 memset(&dkw, 0, sizeof dkw); 470 special = argv[0]; 471 if (Fflag || mfs) { 472 /* 473 * It's a file system image or an MFS, 474 * no label, use fixed default for sectorsize. 475 */ 476 if (sectorsize == 0) 477 sectorsize = DFL_SECSIZE; 478 479 if (mfs) { 480 /* 481 * Default filesystem size to that of supplied device, 482 * and fall back to 8M 483 */ 484 if (fssize == 0) 485 if (stat(special, &sb) == -1) 486 fssize = DFL_FSSIZE / sectorsize; 487 } else { 488 /* creating image in a regular file */ 489 int fl; 490 if (Nflag) 491 fl = O_RDONLY; 492 else { 493 if (fssize > 0) 494 fl = O_RDWR | O_CREAT; 495 else 496 fl = O_RDWR; 497 } 498 fsi = open(special, fl, 0777); 499 if (fsi == -1) 500 err(1, "can't open file %s", special); 501 if (fstat(fsi, &sb) == -1) 502 err(1, "can't fstat opened %s", special); 503 if (!Nflag) 504 fso = fsi; 505 } 506 } else { /* !Fflag && !mfs */ 507 special = findspecial(special, 1); 508 fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0); 509 special = device; 510 if (fsi < 0 || fstat(fsi, &sb) == -1) 511 err(1, "%s: open for read", special); 512 if (S_ISBLK(sb.st_mode)) { 513 errx(1, "%s is a block device. use raw device", 514 special); 515 } 516 517 if (!Nflag) { 518 fso = open(special, O_WRONLY, 0); 519 if (fso < 0) 520 err(1, "%s: open for write", special); 521 522 /* Bail if target special is mounted */ 523 n = getmntinfo(&mp, MNT_NOWAIT); 524 if (n == 0) 525 err(1, "%s: getmntinfo", special); 526 527 len = sizeof(_PATH_DEV) - 1; 528 s1 = special; 529 if (strncmp(_PATH_DEV, s1, len) == 0) 530 s1 += len; 531 532 while (--n >= 0) { 533 s2 = mp->f_mntfromname; 534 if (strncmp(_PATH_DEV, s2, len) == 0) { 535 s2 += len - 1; 536 *s2 = 'r'; 537 } 538 if (strcmp(s1, s2) == 0 || 539 strcmp(s1, &s2[1]) == 0) 540 errx(1, "%s is mounted on %s", 541 special, mp->f_mntonname); 542 ++mp; 543 } 544 } 545 546 #ifdef COMPAT 547 if (disktype == NULL) 548 disktype = argv[1]; 549 #endif 550 if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1) 551 errx(1, lmsg, special); 552 unlabeled = disktype != NULL; 553 554 if (sectorsize == 0) { 555 sectorsize = geo.dg_secsize; 556 if (sectorsize <= 0) 557 errx(1, "no default sector size"); 558 } 559 560 if (dkw.dkw_parent[0]) { 561 if (dkw.dkw_size == 0) 562 errx(1, "%s partition is unavailable", special); 563 564 #ifndef NO_APPLE_UFS 565 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0) 566 isappleufs = 1; 567 #endif 568 569 if (!Iflag) { 570 static const char m[] = 571 "%s partition type is not `%s'"; 572 if (isappleufs) { 573 if (strcmp(dkw.dkw_ptype, 574 DKW_PTYPE_APPLEUFS)) 575 errx(1, m, 576 special, "Apple UFS"); 577 } else { 578 if (strcmp(dkw.dkw_ptype, 579 DKW_PTYPE_FFS)) 580 errx(1, m, special, "4.2BSD"); 581 } 582 } 583 } /* !Fflag && !mfs */ 584 } 585 586 if (byte_sized) 587 fssize /= sectorsize; 588 if (fssize <= 0) { 589 if (sb.st_size != 0) 590 fssize += sb.st_size / sectorsize; 591 else 592 fssize += dkw.dkw_size; 593 if (fssize <= 0) 594 errx(1, "Unable to determine file system size"); 595 } 596 597 if (dkw.dkw_parent[0] && (uint64_t)fssize > dkw.dkw_size) 598 errx(1, "size %" PRIu64 " exceeds maximum file system size on " 599 "`%s' of %" PRIu64 " sectors", 600 fssize, special, dkw.dkw_size); 601 602 /* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */ 603 if (Fflag && fso != -1 604 && ftruncate(fso, (off_t)fssize * sectorsize) == -1) 605 err(1, "can't ftruncate %s to %" PRId64, special, fssize); 606 607 if (Zflag && fso != -1) { /* pre-zero (and de-sparse) the file */ 608 char *buf; 609 int bufsize, i; 610 off_t bufrem; 611 struct statvfs sfs; 612 613 if (fstatvfs(fso, &sfs) == -1) { 614 warn("can't fstatvfs `%s'", special); 615 bufsize = 8192; 616 } else 617 bufsize = sfs.f_iosize; 618 619 if ((buf = aligned_alloc(DEV_BSIZE, bufsize)) == NULL) 620 err(1, "can't malloc buffer of %d", 621 bufsize); 622 memset(buf, 0, bufsize); 623 bufrem = fssize * sectorsize; 624 if (verbosity > 0) 625 printf( "Creating file system image in `%s', " 626 "size %lld bytes, in %d byte chunks.\n", 627 special, (long long)bufrem, bufsize); 628 while (bufrem > 0) { 629 i = write(fso, buf, MIN(bufsize, bufrem)); 630 if (i == -1) 631 err(1, "writing image"); 632 bufrem -= i; 633 } 634 free(buf); 635 } 636 637 /* Sort out fragment and block sizes */ 638 if (fsize == 0) { 639 fsize = bsize / DFL_FRAG_BLK; 640 if (fsize <= 0) { 641 if (isappleufs) { 642 fsize = APPLEUFS_DFL_FRAGSIZE; 643 } else { 644 if (fssize < SMALL_FSSIZE) 645 fsize = S_DFL_FRAGSIZE; 646 else if (fssize < MEDIUM_FSSIZE) 647 fsize = M_DFL_FRAGSIZE; 648 else if (fssize < LARGE_FSSIZE) 649 fsize = L_DFL_FRAGSIZE; 650 else 651 fsize = LL_DFL_FRAGSIZE; 652 if (fsize < sectorsize) 653 fsize = sectorsize; 654 } 655 } 656 } 657 if (bsize <= 0) { 658 if (isappleufs) 659 bsize = APPLEUFS_DFL_BLKSIZE; 660 else 661 bsize = DFL_FRAG_BLK * fsize; 662 } 663 664 if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) { 665 warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d", 666 fsize, APPLEUFS_DFL_FRAGSIZE); 667 } 668 if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) { 669 warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d", 670 bsize, APPLEUFS_DFL_BLKSIZE); 671 } 672 673 /* 674 * Maxcontig sets the default for the maximum number of blocks 675 * that may be allocated sequentially. With filesystem clustering 676 * it is possible to allocate contiguous blocks up to the maximum 677 * transfer size permitted by the controller or buffering. 678 */ 679 if (maxcontig == 0) 680 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize); 681 if (density == 0) 682 density = NFPI * fsize; 683 if (minfree < MINFREE && opt != FS_OPTSPACE) { 684 warnx("%s %s %d%%", "Warning: changing optimization to space", 685 "because minfree is less than", MINFREE); 686 opt = FS_OPTSPACE; 687 } 688 if (maxbpg == 0) { 689 if (Oflag <= 1) 690 maxbpg = MAXBLKPG_UFS1(bsize); 691 else 692 maxbpg = MAXBLKPG_UFS2(bsize); 693 } 694 mkfs(special, fsi, fso, mfsmode, mfsuid, mfsgid); 695 if (fsi != -1 && fsi != fso) 696 close(fsi); 697 if (fso != -1) 698 close(fso); 699 #ifdef MFS 700 if (mfs) { 701 702 pathadj(argv[1], mounttoname); 703 switch (pid = fork()) { 704 case -1: 705 perror("mfs"); 706 exit(10); 707 case 0: 708 (void)snprintf(mountfromname, sizeof(mountfromname), 709 "mfs:%d", getpid()); 710 break; 711 default: 712 (void)snprintf(mountfromname, sizeof(mountfromname), 713 "mfs:%d", pid); 714 for (;;) { 715 /* 716 * spin until the mount succeeds 717 * or the child exits 718 */ 719 usleep(1); 720 721 /* 722 * XXX Here is a race condition: another process 723 * can mount a filesystem which hides our 724 * ramdisk before we see the success. 725 */ 726 if (statvfs(mounttoname, &sf) < 0) 727 err(88, "statvfs %s", mounttoname); 728 if (!strcmp(sf.f_mntfromname, mountfromname) && 729 !strncmp(sf.f_mntonname, mounttoname, 730 MNAMELEN) && 731 !strcmp(sf.f_fstypename, "mfs")) 732 exit(0); 733 734 res = waitpid(pid, &status, WNOHANG); 735 if (res == -1) 736 err(11, "waitpid"); 737 if (res != pid) 738 continue; 739 if (WIFEXITED(status)) { 740 if (WEXITSTATUS(status) == 0) 741 exit(0); 742 errx(1, "%s: mount: %s", mounttoname, 743 strerror(WEXITSTATUS(status))); 744 } else 745 errx(11, "abnormal termination"); 746 } 747 /* NOTREACHED */ 748 } 749 750 (void) setsid(); 751 (void) close(0); 752 (void) close(1); 753 (void) close(2); 754 (void) chdir("/"); 755 756 args.base = membase; 757 args.size = fssize * sectorsize; 758 if (mount(MOUNT_MFS, mounttoname, mntflags | MNT_ASYNC, 759 &args, sizeof args) == -1) 760 exit(errno); /* parent prints message */ 761 } 762 #endif 763 exit(0); 764 } 765 766 static gid_t 767 mfs_group(const char *gname) 768 { 769 struct group *gp; 770 771 if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname)) 772 errx(1, "unknown gname %s", gname); 773 return gp ? gp->gr_gid : (gid_t)atoi(gname); 774 } 775 776 static uid_t 777 mfs_user(const char *uname) 778 { 779 struct passwd *pp; 780 781 if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname)) 782 errx(1, "unknown user %s", uname); 783 return pp ? pp->pw_uid : (uid_t)atoi(uname); 784 } 785 786 static int64_t 787 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix) 788 { 789 int64_t result, r1; 790 int shift = 0; 791 char *ep; 792 793 errno = 0; 794 r1 = strtoll(arg, &ep, 10); 795 if (ep[0] != '\0' && ep[1] != '\0') 796 errx(1, "%s `%s' is not a valid number.", desc, arg); 797 switch (ep[0]) { 798 case '\0': 799 case 's': case 'S': 800 if (num_suffix != NULL) 801 *num_suffix = 0; 802 break; 803 case 't': case 'T': 804 shift += 10; 805 /* FALLTHROUGH */ 806 case 'g': case 'G': 807 shift += 10; 808 /* FALLTHROUGH */ 809 case 'm': case 'M': 810 shift += 10; 811 /* FALLTHROUGH */ 812 case 'k': case 'K': 813 shift += 10; 814 /* FALLTHROUGH */ 815 case 'b': case 'B': 816 if (num_suffix != NULL) 817 *num_suffix = 1; 818 break; 819 default: 820 errx(1, "`%s' is not a valid suffix for %s.", ep, desc); 821 } 822 result = r1 << shift; 823 if (errno == ERANGE || result >> shift != r1) 824 errx(1, "%s `%s' is too large to convert.", desc, arg); 825 if (result < min) { 826 if (Gflag) { 827 warnx("%s `%s' (%" PRId64 ") is less than the " 828 "minimum (%" PRId64 ").", desc, arg, result, min); 829 } else { 830 errx(1, "%s `%s' (%" PRId64 ") is less than the " 831 "minimum (%" PRId64 ").", desc, arg, result, min); 832 } 833 } 834 if (result > max) { 835 if (Gflag) { 836 warnx("%s `%s' (%" PRId64 ") is greater than the " 837 "maximum (%" PRId64 ").", desc, arg, result, max); 838 } else { 839 errx(1, "%s `%s' (%" PRId64 ") is greater than the " 840 "maximum (%" PRId64 ").", desc, arg, result, max); 841 } 842 } 843 return result; 844 } 845 846 #define NEWFS 1 847 #define MFS_MOUNT 2 848 #define BOTH NEWFS | MFS_MOUNT 849 850 struct help_strings { 851 int flags; 852 const char *str; 853 } const help_strings[] = { 854 #ifndef NO_FFS_EI 855 { NEWFS, "-B byteorder\tbyte order (`be' or `le')" }, 856 #endif 857 { NEWFS, "-F \t\tcreate file system image in regular file" }, 858 { NEWFS, "-G \t\tmake sanity calculations non-fatal (testing only!)" }, 859 { NEWFS, "-I \t\tdo not check that the file system type is '4.2BSD'" }, 860 { BOTH, "-N \t\tdo not create file system, just print out " 861 "parameters" }, 862 { NEWFS, "-O N\t\tfilesystem format: 0 => 4.3BSD, 1 => FFSv1, 2 => FFSv2, 2ea => FFSv2 with extattrs" }, 863 { NEWFS, "-S secsize\tsector size" }, 864 #ifdef COMPAT 865 { NEWFS, "-T disktype\tdisk type" }, 866 #endif 867 { BOTH, "-V verbose\toutput verbosity: 0 ==> none, 4 ==> max" }, 868 { NEWFS, "-Z \t\tpre-zero the image file" }, 869 { BOTH, "-a maxcontig\tmaximum contiguous blocks" }, 870 { BOTH, "-b bsize\tblock size" }, 871 { BOTH, "-d maxbsize\tmaximum extent size" }, 872 { BOTH, "-e maxbpg\tmaximum blocks per file in a cylinder group" 873 }, 874 { BOTH, "-f fsize\tfrag size" }, 875 { NEWFS, "-g avgfilesize\taverage file size" }, 876 { MFS_MOUNT, "-g groupname\tgroup name of mount point" }, 877 { BOTH, "-h avgfpdir\taverage files per directory" }, 878 { BOTH, "-i density\tnumber of bytes per inode" }, 879 { BOTH, "-m minfree\tminimum free space %" }, 880 { BOTH, "-n inodes\tnumber of inodes (overrides -i density)" }, 881 { BOTH, "-o optim\toptimization preference (`space' or `time')" 882 }, 883 { BOTH, "-q (user|group) enable specified quota" }, 884 { MFS_MOUNT, "-p perm\t\tpermissions (in octal)" }, 885 { BOTH, "-s fssize\tfile system size (sectors)" }, 886 { MFS_MOUNT, "-u username\tuser name of mount point" }, 887 #ifndef NO_APPLE_UFS 888 { NEWFS, "-v volname\tApple UFS volume name" }, 889 #endif 890 { 0, NULL } 891 }; 892 893 static void 894 usage(void) 895 { 896 int match; 897 const struct help_strings *hs; 898 899 if (mfs) { 900 fprintf(stderr, 901 "usage: %s [ fsoptions ] special-device mount-point\n", 902 getprogname()); 903 } else 904 fprintf(stderr, 905 "usage: %s [ fsoptions ] special-device%s\n", 906 getprogname(), 907 #ifdef COMPAT 908 " [disk-type]"); 909 #else 910 ""); 911 #endif 912 fprintf(stderr, "where fsoptions are:\n"); 913 914 match = mfs ? MFS_MOUNT : NEWFS; 915 for (hs = help_strings; hs->flags != 0; hs++) 916 if (hs->flags & match) 917 fprintf(stderr, "\t%s\n", hs->str); 918 exit(1); 919 } 920