1 /* $NetBSD: newfs.c,v 1.109 2011/03/06 17:08:17 bouyer 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.109 2011/03/06 17:08:17 bouyer 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_*, otherwise use 161 * L_DFL_*. 162 */ 163 #define SMALL_FSSIZE (20*1024*2) 164 #define S_DFL_FRAGSIZE 512 165 #define MEDIUM_FSSIZE (1000*1024*2) 166 #define M_DFL_FRAGSIZE 1024 167 #define L_DFL_FRAGSIZE 2048 168 #define DFL_FRAG_BLK 8 169 170 /* Apple requires the fragment size to be at least APPLEUFS_DIRBLKSIZ 171 * but the block size cannot be larger than Darwin's PAGE_SIZE. See 172 * the mount check in Darwin's ffs_mountfs for an explanation. 173 */ 174 #define APPLEUFS_DFL_FRAGSIZE APPLEUFS_DIRBLKSIZ /* 1024 */ 175 #define APPLEUFS_DFL_BLKSIZE 4096 /* default Darwin PAGE_SIZE */ 176 177 /* 178 * Default sector size. 179 */ 180 #define DFL_SECSIZE 512 181 182 /* 183 * Default file system size for "mount_mfs swap /dir" case. 184 */ 185 #define DFL_FSSIZE (8 * 1024 * 1024) 186 187 /* 188 * MAXBLKPG determines the maximum number of data blocks which are 189 * placed in a single cylinder group. The default is one indirect 190 * block worth of data blocks. 191 */ 192 #define MAXBLKPG_UFS1(bsize) ((bsize) / sizeof(int32_t)) 193 #define MAXBLKPG_UFS2(bsize) ((bsize) / sizeof(int64_t)) 194 195 /* 196 * Each file system has a number of inodes statically allocated. 197 * We allocate one inode slot per NFPI fragments, expecting this 198 * to be far more than we will ever need. 199 */ 200 #define NFPI 4 201 202 203 int mfs; /* run as the memory based filesystem */ 204 int Gflag; /* allow garbage parameters (for testing) */ 205 int Nflag; /* run without writing file system */ 206 int Oflag = 1; /* format as an 4.3BSD file system */ 207 int verbosity; /* amount of printf() output */ 208 #define DEFAULT_VERBOSITY 3 /* 4 is traditional behavior */ 209 int64_t fssize; /* file system size */ 210 int sectorsize; /* bytes/sector */ 211 int fsize = 0; /* fragment size */ 212 int bsize = 0; /* block size */ 213 int maxbsize = 0; /* maximum clustering */ 214 int minfree = MINFREE; /* free space threshold */ 215 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 216 int density; /* number of bytes per inode */ 217 int num_inodes; /* number of inodes (overrides density) */ 218 int maxcontig = 0; /* max contiguous blocks to allocate */ 219 int maxbpg; /* maximum blocks per file in a cyl group */ 220 int avgfilesize = AVFILESIZ;/* expected average file size */ 221 int avgfpdir = AFPDIR; /* expected number of files per directory */ 222 int mntflags = 0; /* flags to be passed to mount */ 223 u_long memleft; /* virtual memory available */ 224 caddr_t membase; /* start address of memory based filesystem */ 225 int needswap; /* Filesystem not in native byte order */ 226 char *disktype = NULL; 227 int unlabeled; 228 char *appleufs_volname = 0; /* Apple UFS volume name */ 229 int isappleufs = 0; 230 int quotas = 0; 231 232 char device[MAXPATHLEN]; 233 234 int 235 main(int argc, char *argv[]) 236 { 237 struct disk_geom geo; 238 struct dkwedge_info dkw; 239 struct statvfs *mp; 240 struct stat sb; 241 int ch, fsi, fso, len, n, Fflag, Iflag, Zflag; 242 char *cp, *s1, *s2, *special; 243 const char *opstring; 244 int byte_sized = 0; 245 #ifdef MFS 246 struct mfs_args args; 247 char mountfromname[100]; 248 char mounttoname[MAXPATHLEN]; 249 pid_t pid, res; 250 struct statvfs sf; 251 int status; 252 #endif 253 mode_t mfsmode = 01777; /* default mode for a /tmp-type directory */ 254 uid_t mfsuid = 0; /* user root */ 255 gid_t mfsgid = 0; /* group wheel */ 256 mntoptparse_t mo; 257 258 cp = NULL; 259 fsi = fso = -1; 260 Fflag = Iflag = Zflag = 0; 261 verbosity = -1; 262 if (strstr(getprogname(), "mfs")) { 263 mfs = 1; 264 } else { 265 /* Undocumented, for ease of testing */ 266 if (argv[1] != NULL && !strcmp(argv[1], "-mfs")) { 267 argv++; 268 argc--; 269 mfs = 1; 270 } 271 } 272 273 opstring = mfs ? 274 "NT:V:a:b:d:e:f:g:h:i:m:n:o:p:q:s:u:" : 275 "B:FGINO:S:T:V:Za:b:d:e:f:g:h:i:l:m:n:o:q:r:s:v:"; 276 while ((ch = getopt(argc, argv, opstring)) != -1) 277 switch (ch) { 278 case 'B': 279 if (strcmp(optarg, "be") == 0) { 280 #if BYTE_ORDER == LITTLE_ENDIAN 281 needswap = 1; 282 #endif 283 } else if (strcmp(optarg, "le") == 0) { 284 #if BYTE_ORDER == BIG_ENDIAN 285 needswap = 1; 286 #endif 287 } else 288 usage(); 289 break; 290 case 'F': 291 Fflag = 1; 292 break; 293 case 'G': 294 fprintf(stderr, "WARNING: -G may create file systems " 295 "which cause kernel panics\n"); 296 Gflag = 1; 297 break; 298 case 'I': 299 Iflag = 1; 300 break; 301 case 'N': 302 Nflag = 1; 303 if (verbosity == -1) 304 verbosity = DEFAULT_VERBOSITY; 305 break; 306 case 'O': 307 Oflag = strsuftoi64("format", optarg, 0, 2, NULL); 308 break; 309 case 'S': 310 /* XXX: non-512 byte sectors almost certainly don't work. */ 311 sectorsize = strsuftoi64("sector size", 312 optarg, 512, 65536, NULL); 313 if (sectorsize & (sectorsize - 1)) 314 errx(1, "sector size `%s' is not a power of 2.", 315 optarg); 316 break; 317 #ifdef COMPAT 318 case 'T': 319 disktype = optarg; 320 break; 321 #endif 322 case 'V': 323 verbosity = strsuftoi64("verbose", optarg, 0, 4, NULL); 324 break; 325 case 'Z': 326 Zflag = 1; 327 break; 328 case 'a': 329 maxcontig = strsuftoi64("maximum contiguous blocks", 330 optarg, 1, INT_MAX, NULL); 331 break; 332 case 'b': 333 bsize = strsuftoi64("block size", 334 optarg, MINBSIZE, MAXBSIZE, NULL); 335 break; 336 case 'd': 337 maxbsize = strsuftoi64("maximum extent size", 338 optarg, 0, INT_MAX, NULL); 339 break; 340 case 'e': 341 maxbpg = strsuftoi64( 342 "blocks per file in a cylinder group", 343 optarg, 1, INT_MAX, NULL); 344 break; 345 case 'f': 346 fsize = strsuftoi64("fragment size", 347 optarg, 1, MAXBSIZE, NULL); 348 break; 349 case 'g': 350 if (mfs) 351 mfsgid = mfs_group(optarg); 352 else { 353 avgfilesize = strsuftoi64("average file size", 354 optarg, 1, INT_MAX, NULL); 355 } 356 break; 357 case 'h': 358 avgfpdir = strsuftoi64("expected files per directory", 359 optarg, 1, INT_MAX, NULL); 360 break; 361 case 'i': 362 density = strsuftoi64("bytes per inode", 363 optarg, 1, INT_MAX, NULL); 364 break; 365 case 'm': 366 minfree = strsuftoi64("free space %", 367 optarg, 0, 99, NULL); 368 break; 369 case 'n': 370 num_inodes = strsuftoi64("number of inodes", 371 optarg, 1, INT_MAX, NULL); 372 break; 373 case 'o': 374 if (mfs) { 375 mo = getmntopts(optarg, mopts, &mntflags, 0); 376 if (mo == NULL) 377 err(1, "getmntopts"); 378 freemntopts(mo); 379 } else { 380 if (strcmp(optarg, "space") == 0) 381 opt = FS_OPTSPACE; 382 else if (strcmp(optarg, "time") == 0) 383 opt = FS_OPTTIME; 384 else 385 errx(1, "%s %s", 386 "unknown optimization preference: ", 387 "use `space' or `time'."); 388 } 389 break; 390 case 'q': 391 if (strcmp(optarg, "user") == 0) 392 quotas |= FS_Q2_DO_TYPE(USRQUOTA); 393 else if (strcmp(optarg, "group") == 0) 394 quotas |= FS_Q2_DO_TYPE(GRPQUOTA); 395 else 396 errx(1, "invalid quota type %s", optarg); 397 break; 398 case 'p': 399 /* mfs only */ 400 if ((mfsmode = strtol(optarg, NULL, 8)) <= 0) 401 errx(1, "bad mode `%s'", optarg); 402 break; 403 case 's': 404 fssize = strsuftoi64("file system size", 405 optarg, INT64_MIN, INT64_MAX, &byte_sized); 406 break; 407 case 'u': 408 /* mfs only */ 409 mfsuid = mfs_user(optarg); 410 break; 411 case 'v': 412 appleufs_volname = optarg; 413 if (strchr(appleufs_volname, ':') || strchr(appleufs_volname, '/')) 414 errx(1,"Apple UFS volume name cannot contain ':' or '/'"); 415 if (appleufs_volname[0] == '\0') 416 errx(1,"Apple UFS volume name cannot be zero length"); 417 isappleufs = 1; 418 break; 419 case '?': 420 default: 421 usage(); 422 } 423 argc -= optind; 424 argv += optind; 425 426 if (Oflag < 1 && quotas != 0) 427 errx(1, "in-filesystem quota is incompatible with -O0"); 428 429 if (verbosity == -1) 430 /* Default to not showing CG info if mfs */ 431 verbosity = mfs ? 0 : DEFAULT_VERBOSITY; 432 433 #ifdef MFS 434 /* This is enough to get through the correct kernel code paths */ 435 memset(&args, 0, sizeof args); 436 args.fspec = mountfromname; 437 if (mntflags & (MNT_GETARGS | MNT_UPDATE)) { 438 if ((mntflags & MNT_GETARGS) == 0) 439 mntflags |= MNT_ASYNC; 440 if (mount(MOUNT_MFS, argv[1], mntflags, 441 &args, sizeof args) == -1) 442 err(1, "mount `%s' failed", argv[1]); 443 if (mntflags & MNT_GETARGS) 444 printf("base=%p, size=%ld\n", args.base, args.size); 445 exit(0); 446 } 447 #endif 448 449 if (argc != 2 && (mfs || argc != 1)) 450 usage(); 451 452 memset(&sb, 0, sizeof sb); 453 memset(&dkw, 0, sizeof dkw); 454 special = argv[0]; 455 if (Fflag || mfs) { 456 /* 457 * It's a file system image or an MFS, 458 * no label, use fixed default for sectorsize. 459 */ 460 if (sectorsize == 0) 461 sectorsize = DFL_SECSIZE; 462 463 if (mfs) { 464 /* 465 * Default filesystem size to that of supplied device, 466 * and fall back to 8M 467 */ 468 if (fssize == 0) 469 if (stat(special, &sb) == -1) 470 fssize = DFL_FSSIZE / sectorsize; 471 } else { 472 /* creating image in a regular file */ 473 int fl; 474 if (Nflag) 475 fl = O_RDONLY; 476 else { 477 if (fssize > 0) 478 fl = O_RDWR | O_CREAT; 479 else 480 fl = O_RDWR; 481 } 482 fsi = open(special, fl, 0777); 483 if (fsi == -1) 484 err(1, "can't open file %s", special); 485 if (fstat(fsi, &sb) == -1) 486 err(1, "can't fstat opened %s", special); 487 if (!Nflag) 488 fso = fsi; 489 } 490 } else { /* !Fflag && !mfs */ 491 fsi = opendisk(special, O_RDONLY, device, sizeof(device), 0); 492 special = device; 493 if (fsi < 0 || fstat(fsi, &sb) == -1) 494 err(1, "%s: open for read", special); 495 if (S_ISBLK(sb.st_mode)) { 496 errx(1, "%s is a block device. use raw device", 497 special); 498 } 499 500 if (!Nflag) { 501 fso = open(special, O_WRONLY, 0); 502 if (fso < 0) 503 err(1, "%s: open for write", special); 504 505 /* Bail if target special is mounted */ 506 n = getmntinfo(&mp, MNT_NOWAIT); 507 if (n == 0) 508 err(1, "%s: getmntinfo", special); 509 510 len = sizeof(_PATH_DEV) - 1; 511 s1 = special; 512 if (strncmp(_PATH_DEV, s1, len) == 0) 513 s1 += len; 514 515 while (--n >= 0) { 516 s2 = mp->f_mntfromname; 517 if (strncmp(_PATH_DEV, s2, len) == 0) { 518 s2 += len - 1; 519 *s2 = 'r'; 520 } 521 if (strcmp(s1, s2) == 0 || 522 strcmp(s1, &s2[1]) == 0) 523 errx(1, "%s is mounted on %s", 524 special, mp->f_mntonname); 525 ++mp; 526 } 527 } 528 529 #ifdef COMPAT 530 if (disktype == NULL) 531 disktype = argv[1]; 532 #endif 533 if (getdiskinfo(special, fsi, disktype, &geo, &dkw) == -1) 534 errx(1, lmsg, special); 535 unlabeled = disktype != NULL; 536 537 if (sectorsize == 0) { 538 sectorsize = geo.dg_secsize; 539 if (sectorsize <= 0) 540 errx(1, "no default sector size"); 541 } 542 543 if (dkw.dkw_parent[0]) { 544 if (dkw.dkw_size == 0) 545 errx(1, "%s partition is unavailable", special); 546 547 if (strcmp(dkw.dkw_ptype, DKW_PTYPE_APPLEUFS) == 0) 548 isappleufs = 1; 549 550 if (!Iflag) { 551 static const char m[] = 552 "%s partition type is not `%s'"; 553 if (isappleufs) { 554 if (strcmp(dkw.dkw_ptype, 555 DKW_PTYPE_APPLEUFS)) 556 errx(1, m, 557 special, "Apple UFS"); 558 } else { 559 if (strcmp(dkw.dkw_ptype, 560 DKW_PTYPE_FFS)) 561 errx(1, m, special, "4.2BSD"); 562 } 563 } 564 } /* !Fflag && !mfs */ 565 } 566 567 if (byte_sized) 568 fssize /= sectorsize; 569 if (fssize <= 0) { 570 if (sb.st_size != 0) 571 fssize += sb.st_size / sectorsize; 572 else 573 fssize += dkw.dkw_size; 574 if (fssize <= 0) 575 errx(1, "Unable to determine file system size"); 576 } 577 578 if (dkw.dkw_parent[0] && (uint64_t)fssize > dkw.dkw_size) 579 errx(1, "size %" PRIu64 " exceeds maximum file system size on " 580 "`%s' of %" PRIu64 " sectors", 581 fssize, special, dkw.dkw_size); 582 583 /* XXXLUKEM: only ftruncate() regular files ? (dsl: or at all?) */ 584 if (Fflag && fso != -1 585 && ftruncate(fso, (off_t)fssize * sectorsize) == -1) 586 err(1, "can't ftruncate %s to %" PRId64, special, fssize); 587 588 if (Zflag && fso != -1) { /* pre-zero (and de-sparce) the file */ 589 char *buf; 590 int bufsize, i; 591 off_t bufrem; 592 struct statvfs sfs; 593 594 if (fstatvfs(fso, &sfs) == -1) { 595 warn("can't fstatvfs `%s'", special); 596 bufsize = 8192; 597 } else 598 bufsize = sfs.f_iosize; 599 600 if ((buf = calloc(1, bufsize)) == NULL) 601 err(1, "can't malloc buffer of %d", 602 bufsize); 603 bufrem = fssize * sectorsize; 604 if (verbosity > 0) 605 printf( "Creating file system image in `%s', " 606 "size %lld bytes, in %d byte chunks.\n", 607 special, (long long)bufrem, bufsize); 608 while (bufrem > 0) { 609 i = write(fso, buf, MIN(bufsize, bufrem)); 610 if (i == -1) 611 err(1, "writing image"); 612 bufrem -= i; 613 } 614 free(buf); 615 } 616 617 /* Sort out fragment and block sizes */ 618 if (fsize == 0) { 619 fsize = bsize / DFL_FRAG_BLK; 620 if (fsize <= 0) { 621 if (isappleufs) { 622 fsize = APPLEUFS_DFL_FRAGSIZE; 623 } else { 624 if (fssize < SMALL_FSSIZE) 625 fsize = S_DFL_FRAGSIZE; 626 else if (fssize < MEDIUM_FSSIZE) 627 fsize = M_DFL_FRAGSIZE; 628 else 629 fsize = L_DFL_FRAGSIZE; 630 if (fsize < sectorsize) 631 fsize = sectorsize; 632 } 633 } 634 } 635 if (bsize <= 0) { 636 if (isappleufs) 637 bsize = APPLEUFS_DFL_BLKSIZE; 638 else 639 bsize = DFL_FRAG_BLK * fsize; 640 } 641 642 if (isappleufs && (fsize < APPLEUFS_DFL_FRAGSIZE)) { 643 warnx("Warning: chosen fsize of %d is less than Apple UFS minimum of %d", 644 fsize, APPLEUFS_DFL_FRAGSIZE); 645 } 646 if (isappleufs && (bsize > APPLEUFS_DFL_BLKSIZE)) { 647 warnx("Warning: chosen bsize of %d is greater than Apple UFS maximum of %d", 648 bsize, APPLEUFS_DFL_BLKSIZE); 649 } 650 651 /* 652 * Maxcontig sets the default for the maximum number of blocks 653 * that may be allocated sequentially. With filesystem clustering 654 * it is possible to allocate contiguous blocks up to the maximum 655 * transfer size permitted by the controller or buffering. 656 */ 657 if (maxcontig == 0) 658 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize); 659 if (density == 0) 660 density = NFPI * fsize; 661 if (minfree < MINFREE && opt != FS_OPTSPACE) { 662 warnx("%s %s %d%%", "Warning: changing optimization to space", 663 "because minfree is less than", MINFREE); 664 opt = FS_OPTSPACE; 665 } 666 if (maxbpg == 0) { 667 if (Oflag <= 1) 668 maxbpg = MAXBLKPG_UFS1(bsize); 669 else 670 maxbpg = MAXBLKPG_UFS2(bsize); 671 } 672 mkfs(special, fsi, fso, mfsmode, mfsuid, mfsgid); 673 if (fsi != -1 && fsi != fso) 674 close(fsi); 675 if (fso != -1) 676 close(fso); 677 #ifdef MFS 678 if (mfs) { 679 680 pathadj(argv[1], mounttoname); 681 switch (pid = fork()) { 682 case -1: 683 perror("mfs"); 684 exit(10); 685 case 0: 686 (void)snprintf(mountfromname, sizeof(mountfromname), 687 "mfs:%d", getpid()); 688 break; 689 default: 690 (void)snprintf(mountfromname, sizeof(mountfromname), 691 "mfs:%d", pid); 692 for (;;) { 693 /* 694 * spin until the mount succeeds 695 * or the child exits 696 */ 697 usleep(1); 698 699 /* 700 * XXX Here is a race condition: another process 701 * can mount a filesystem which hides our 702 * ramdisk before we see the success. 703 */ 704 if (statvfs(mounttoname, &sf) < 0) 705 err(88, "statvfs %s", mounttoname); 706 if (!strcmp(sf.f_mntfromname, mountfromname) && 707 !strncmp(sf.f_mntonname, mounttoname, 708 MNAMELEN) && 709 !strcmp(sf.f_fstypename, "mfs")) 710 exit(0); 711 712 res = waitpid(pid, &status, WNOHANG); 713 if (res == -1) 714 err(11, "waitpid"); 715 if (res != pid) 716 continue; 717 if (WIFEXITED(status)) { 718 if (WEXITSTATUS(status) == 0) 719 exit(0); 720 errx(1, "%s: mount: %s", mounttoname, 721 strerror(WEXITSTATUS(status))); 722 } else 723 errx(11, "abnormal termination"); 724 } 725 /* NOTREACHED */ 726 } 727 728 (void) setsid(); 729 (void) close(0); 730 (void) close(1); 731 (void) close(2); 732 (void) chdir("/"); 733 734 args.base = membase; 735 args.size = fssize * sectorsize; 736 if (mount(MOUNT_MFS, mounttoname, mntflags | MNT_ASYNC, 737 &args, sizeof args) == -1) 738 exit(errno); /* parent prints message */ 739 } 740 #endif 741 exit(0); 742 } 743 744 static gid_t 745 mfs_group(const char *gname) 746 { 747 struct group *gp; 748 749 if (!(gp = getgrnam(gname)) && !isdigit((unsigned char)*gname)) 750 errx(1, "unknown gname %s", gname); 751 return gp ? gp->gr_gid : (gid_t)atoi(gname); 752 } 753 754 static uid_t 755 mfs_user(const char *uname) 756 { 757 struct passwd *pp; 758 759 if (!(pp = getpwnam(uname)) && !isdigit((unsigned char)*uname)) 760 errx(1, "unknown user %s", uname); 761 return pp ? pp->pw_uid : (uid_t)atoi(uname); 762 } 763 764 static int64_t 765 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix) 766 { 767 int64_t result, r1; 768 int shift = 0; 769 char *ep; 770 771 errno = 0; 772 r1 = strtoll(arg, &ep, 10); 773 if (ep[0] != '\0' && ep[1] != '\0') 774 errx(1, "%s `%s' is not a valid number.", desc, arg); 775 switch (ep[0]) { 776 case '\0': 777 case 's': case 'S': 778 if (num_suffix != NULL) 779 *num_suffix = 0; 780 break; 781 case 't': case 'T': 782 shift += 10; 783 /* FALLTHROUGH */ 784 case 'g': case 'G': 785 shift += 10; 786 /* FALLTHROUGH */ 787 case 'm': case 'M': 788 shift += 10; 789 /* FALLTHROUGH */ 790 case 'k': case 'K': 791 shift += 10; 792 /* FALLTHROUGH */ 793 case 'b': case 'B': 794 if (num_suffix != NULL) 795 *num_suffix = 1; 796 break; 797 default: 798 errx(1, "`%s' is not a valid suffix for %s.", ep, desc); 799 } 800 result = r1 << shift; 801 if (errno == ERANGE || result >> shift != r1) 802 errx(1, "%s `%s' is too large to convert.", desc, arg); 803 if (result < min) { 804 if (Gflag) { 805 warnx("%s `%s' (%" PRId64 ") is less than the " 806 "minimum (%" PRId64 ").", desc, arg, result, min); 807 } else { 808 errx(1, "%s `%s' (%" PRId64 ") is less than the " 809 "minimum (%" PRId64 ").", desc, arg, result, min); 810 } 811 } 812 if (result > max) { 813 if (Gflag) { 814 warnx("%s `%s' (%" PRId64 ") is greater than the " 815 "maximum (%" PRId64 ").", desc, arg, result, max); 816 } else { 817 errx(1, "%s `%s' (%" PRId64 ") is greater than the " 818 "maximum (%" PRId64 ").", desc, arg, result, max); 819 } 820 } 821 return result; 822 } 823 824 #define NEWFS 1 825 #define MFS_MOUNT 2 826 #define BOTH NEWFS | MFS_MOUNT 827 828 struct help_strings { 829 int flags; 830 const char *str; 831 } const help_strings[] = { 832 { NEWFS, "-B byteorder\tbyte order (`be' or `le')" }, 833 { NEWFS, "-F \t\tcreate file system image in regular file" }, 834 { NEWFS, "-G \t\tmake sanity calculations non-fatal (testing only!)" }, 835 { NEWFS, "-I \t\tdo not check that the file system type is '4.2BSD'" }, 836 { BOTH, "-N \t\tdo not create file system, just print out " 837 "parameters" }, 838 { NEWFS, "-O N\t\tfilesystem format: 0 => 4.3BSD, 1 => FFSv1, 2 => FFSv2" }, 839 { NEWFS, "-S secsize\tsector size" }, 840 #ifdef COMPAT 841 { NEWFS, "-T disktype\tdisk type" }, 842 #endif 843 { BOTH, "-V verbose\toutput verbosity: 0 ==> none, 4 ==> max" }, 844 { NEWFS, "-Z \t\tpre-zero the image file" }, 845 { BOTH, "-a maxcontig\tmaximum contiguous blocks" }, 846 { BOTH, "-b bsize\tblock size" }, 847 { BOTH, "-d maxbsize\tmaximum extent size" }, 848 { BOTH, "-e maxbpg\tmaximum blocks per file in a cylinder group" 849 }, 850 { BOTH, "-f fsize\tfrag size" }, 851 { NEWFS, "-g avgfilesize\taverage file size" }, 852 { MFS_MOUNT, "-g groupname\tgroup name of mount point" }, 853 { BOTH, "-h avgfpdir\taverage files per directory" }, 854 { BOTH, "-i density\tnumber of bytes per inode" }, 855 { BOTH, "-m minfree\tminimum free space %%" }, 856 { BOTH, "-n inodes\tnumber of inodes (overrides -i density)" }, 857 { BOTH, "-o optim\toptimization preference (`space' or `time')" 858 }, 859 { BOTH, "-q (user|group) enable specified quota" }, 860 { MFS_MOUNT, "-p perm\t\tpermissions (in octal)" }, 861 { BOTH, "-s fssize\tfile system size (sectors)" }, 862 { MFS_MOUNT, "-u username\tuser name of mount point" }, 863 { NEWFS, "-v volname\tApple UFS volume name" }, 864 { 0, NULL } 865 }; 866 867 static void 868 usage(void) 869 { 870 int match; 871 const struct help_strings *hs; 872 873 if (mfs) { 874 fprintf(stderr, 875 "usage: %s [ fsoptions ] special-device mount-point\n", 876 getprogname()); 877 } else 878 fprintf(stderr, 879 "usage: %s [ fsoptions ] special-device%s\n", 880 getprogname(), 881 #ifdef COMPAT 882 " [disk-type]"); 883 #else 884 ""); 885 #endif 886 fprintf(stderr, "where fsoptions are:\n"); 887 888 match = mfs ? MFS_MOUNT : NEWFS; 889 for (hs = help_strings; hs->flags != 0; hs++) 890 if (hs->flags & match) 891 fprintf(stderr, "\t%s\n", hs->str); 892 exit(1); 893 } 894