1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 #pragma ident "%Z%%M% %I% %E% SMI" 24 /* from UCB 5.2 9/11/85 */ 25 26 /* 27 * newfs: friendly front end to mkfs 28 * 29 * Copyright 1991, 1997, 2001-2003 Sun Microsystems, Inc. All rights reserved. 30 * Use is subject to license terms. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/types.h> 35 #include <locale.h> 36 #include <sys/stat.h> 37 #include <sys/buf.h> 38 #include <sys/fs/ufs_fs.h> 39 #include <sys/vnode.h> 40 #include <sys/fs/ufs_inode.h> 41 #include <sys/sysmacros.h> 42 43 #include <errno.h> 44 #include <stdio.h> 45 #include <string.h> 46 #include <stdlib.h> 47 #include <stdarg.h> 48 #include <stdio.h> 49 #include <fcntl.h> 50 #include <unistd.h> 51 #include <limits.h> 52 #include <libintl.h> 53 #include <sys/dkio.h> 54 #include <sys/vtoc.h> 55 #include <sys/mkdev.h> 56 #include <sys/efi_partition.h> 57 58 #include <fslib.h> 59 60 static unsigned int number(char *, char *, int, int); 61 static int64_t number64(char *, char *, int, int64_t); 62 static diskaddr_t getdiskbydev(char *); 63 static int yes(void); 64 static int notrand(char *); 65 static void usage(); 66 static diskaddr_t get_device_size(int, char *); 67 static diskaddr_t brute_force_get_device_size(int); 68 static int validate_size(char *disk, diskaddr_t size); 69 static void exenv(void); 70 static struct fs *read_sb(char *); 71 /*PRINTFLIKE1*/ 72 static void fatal(char *fmt, ...); 73 74 #define EPATH "PATH=/usr/sbin:/sbin:" 75 #define CPATH "/sbin" /* an EPATH element */ 76 #define MB (1024 * 1024) 77 #define GBSEC ((1024 * 1024 * 1024) / DEV_BSIZE) /* sectors in a GB */ 78 #define MINFREESEC ((64 * 1024 * 1024) / DEV_BSIZE) /* sectors in 64 MB */ 79 #define MINCPG (16) /* traditional */ 80 #define MAXDEFDENSITY (8 * 1024) /* arbitrary */ 81 #define MINDENSITY (2 * 1024) /* traditional */ 82 #define MIN_MTB_DENSITY (1024 * 1024) 83 #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 84 #define SECTORS_PER_TERABYTE (1LL << 31) 85 /* 86 * The following constant specifies an upper limit for file system size 87 * that is actually a lot bigger than we expect to support with UFS. (Since 88 * it's specified in sectors, the file system size would be 2**44 * 512, 89 * which is 2**53, which is 8192 Terabytes.) However, it's useful 90 * for checking the basic sanity of a size value that is input on the 91 * command line. 92 */ 93 #define FS_SIZE_UPPER_LIMIT 0x100000000000LL 94 95 /* For use with number() */ 96 #define NR_NONE 0 97 #define NR_PERCENT 0x01 98 99 /* 100 * The following two constants set the default block and fragment sizes. 101 * Both constants must be a power of 2 and meet the following constraints: 102 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 103 * DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE 104 * DESBLKSIZE / DESFRAGSIZE <= 8 105 */ 106 #define DESBLKSIZE 8192 107 #define DESFRAGSIZE 1024 108 109 static int Nflag; /* run mkfs without writing file system */ 110 static int Tflag; /* set up file system for growth to over 1 TB */ 111 static int verbose; /* show mkfs line before exec */ 112 static int fsize = 0; /* fragment size */ 113 static int fsize_flag = 0; /* fragment size was specified on cmd line */ 114 static int bsize; /* block size */ 115 static int ntracks; /* # tracks/cylinder */ 116 static int ntracks_set = 0; /* true if the user specified ntracks */ 117 static int optim = FS_OPTTIME; /* optimization, t(ime) or s(pace) */ 118 static int nsectors; /* # sectors/track */ 119 static int cpg; /* cylinders/cylinder group */ 120 static int cpg_set = 0; /* true if the user specified cpg */ 121 static int minfree = -1; /* free space threshold */ 122 static int rpm; /* revolutions/minute of drive */ 123 static int rpm_set = 0; /* true if the user specified rpm */ 124 static int nrpos = 8; /* # of distinguished rotational positions */ 125 /* 8 is the historical default */ 126 static int nrpos_set = 0; /* true if the user specified nrpos */ 127 static int density = 0; /* number of bytes per inode */ 128 static int apc; /* alternates per cylinder */ 129 static int apc_set = 0; /* true if the user specified apc */ 130 static int rot = -1; /* rotational delay (msecs) */ 131 static int rot_set = 0; /* true if the user specified rot */ 132 static int maxcontig = -1; /* maximum number of contig blocks */ 133 static int label_type; /* see types below */ 134 135 #define LABEL_TYPE_VTOC 1 136 #define LABEL_TYPE_EFI 2 137 #define LABEL_TYPE_OTHER 3 138 139 static char device[MAXPATHLEN]; 140 static char cmd[BUFSIZ]; 141 142 extern char *getfullrawname(); /* from libadm */ 143 144 int 145 main(int argc, char *argv[]) 146 { 147 char *special, *name; 148 struct stat64 st; 149 int status; 150 int option; 151 struct fs *sbp; /* Pointer to superblock (if present) */ 152 diskaddr_t actual_fssize; 153 diskaddr_t max_possible_fssize; 154 diskaddr_t req_fssize = 0; 155 diskaddr_t fssize = 0; 156 char *req_fssize_str = NULL; /* requested size argument */ 157 158 (void) setlocale(LC_ALL, ""); 159 160 #if !defined(TEXT_DOMAIN) 161 #define TEXT_DOMAIN "SYS_TEST" 162 #endif 163 (void) textdomain(TEXT_DOMAIN); 164 165 opterr = 0; /* We print our own errors, disable getopt's message */ 166 while ((option = getopt(argc, argv, "vNs:C:d:t:o:a:b:f:c:m:n:r:i:T")) 167 != EOF) { 168 switch (option) { 169 case 'v': 170 verbose++; 171 break; 172 173 case 'N': 174 Nflag++; 175 break; 176 177 case 's': 178 /* 179 * The maximum file system size is a lot smaller 180 * than FS_SIZE_UPPER_LIMIT, but until we find out 181 * the device size and block size, we don't know 182 * what it is. So save the requested size in a 183 * string so that we can print it out later if we 184 * determine it's too big. 185 */ 186 req_fssize = number64("fssize", optarg, NR_NONE, 187 FS_SIZE_UPPER_LIMIT); 188 if (req_fssize < 1024) 189 fatal(gettext( 190 "%s: fssize must be at least 1024"), 191 optarg); 192 req_fssize_str = strdup(optarg); 193 if (req_fssize_str == NULL) 194 fatal(gettext( 195 "Insufficient memory for string copy.")); 196 break; 197 198 case 'C': 199 maxcontig = number("maxcontig", optarg, NR_NONE, -1); 200 if (maxcontig < 0) 201 fatal(gettext("%s: bad maxcontig"), optarg); 202 break; 203 204 case 'd': 205 rot = number("rotdelay", optarg, NR_NONE, 0); 206 rot_set = 1; 207 if (rot < 0 || rot > 1000) 208 fatal(gettext( 209 "%s: bad rotational delay"), optarg); 210 break; 211 212 case 't': 213 ntracks = number("ntrack", optarg, NR_NONE, 16); 214 ntracks_set = 1; 215 if ((ntracks < 0) || 216 (ntracks > INT_MAX)) 217 fatal(gettext("%s: bad total tracks"), optarg); 218 break; 219 220 case 'o': 221 if (strcmp(optarg, "space") == 0) 222 optim = FS_OPTSPACE; 223 else if (strcmp(optarg, "time") == 0) 224 optim = FS_OPTTIME; 225 else 226 fatal(gettext( 227 "%s: bad optimization preference (options are `space' or `time')"), 228 optarg); 229 break; 230 231 case 'a': 232 apc = number("apc", optarg, NR_NONE, 0); 233 apc_set = 1; 234 if (apc < 0 || apc > 32768) /* see mkfs.c */ 235 fatal(gettext( 236 "%s: bad alternates per cyl"), optarg); 237 break; 238 239 case 'b': 240 bsize = number("bsize", optarg, NR_NONE, DESBLKSIZE); 241 if (bsize < MINBSIZE || bsize > MAXBSIZE) 242 fatal(gettext( 243 "%s: bad block size"), optarg); 244 break; 245 246 case 'f': 247 fsize = number("fragsize", optarg, NR_NONE, 248 DESFRAGSIZE); 249 fsize_flag++; 250 /* xxx ought to test against bsize for upper limit */ 251 if (fsize < DEV_BSIZE) 252 fatal(gettext("%s: bad frag size"), optarg); 253 break; 254 255 case 'c': 256 cpg = number("cpg", optarg, NR_NONE, 16); 257 cpg_set = 1; 258 if (cpg < 1) 259 fatal(gettext("%s: bad cylinders/group"), 260 optarg); 261 break; 262 263 case 'm': 264 minfree = number("minfree", optarg, NR_PERCENT, 10); 265 if (minfree < 0 || minfree > 99) 266 fatal(gettext("%s: bad free space %%"), optarg); 267 break; 268 269 case 'n': 270 nrpos = number("nrpos", optarg, NR_NONE, 8); 271 nrpos_set = 1; 272 if (nrpos <= 0) 273 fatal(gettext( 274 "%s: bad number of rotational positions"), 275 optarg); 276 break; 277 278 case 'r': 279 rpm = number("rpm", optarg, NR_NONE, 3600); 280 rpm_set = 1; 281 if (rpm < 0) 282 fatal(gettext("%s: bad revs/minute"), optarg); 283 break; 284 285 case 'i': 286 /* xxx ought to test against fsize */ 287 density = number("nbpi", optarg, NR_NONE, 2048); 288 if (density < DEV_BSIZE) 289 fatal(gettext("%s: bad bytes per inode"), 290 optarg); 291 break; 292 293 case 'T': 294 Tflag++; 295 break; 296 297 default: 298 usage(); 299 fatal(gettext("-%c: unknown flag"), optopt); 300 } 301 } 302 303 /* At this point, there should only be one argument left: */ 304 /* The raw-special-device itself. If not, print usage message. */ 305 if ((argc - optind) != 1) { 306 usage(); 307 exit(1); 308 } 309 310 name = argv[optind]; 311 312 special = getfullrawname(name); 313 if (special == NULL) { 314 (void) fprintf(stderr, gettext("newfs: malloc failed\n")); 315 exit(1); 316 } 317 318 if (*special == '\0') { 319 if (strchr(name, '/') != NULL) { 320 if (stat64(name, &st) < 0) { 321 (void) fprintf(stderr, 322 gettext("newfs: %s: %s\n"), 323 name, strerror(errno)); 324 exit(2); 325 } 326 fatal(gettext("%s: not a raw disk device"), name); 327 } 328 (void) sprintf(device, "/dev/rdsk/%s", name); 329 if ((special = getfullrawname(device)) == NULL) { 330 (void) fprintf(stderr, 331 gettext("newfs: malloc failed\n")); 332 exit(1); 333 } 334 335 if (*special == '\0') { 336 (void) sprintf(device, "/dev/%s", name); 337 if ((special = getfullrawname(device)) == NULL) { 338 (void) fprintf(stderr, 339 gettext("newfs: malloc failed\n")); 340 exit(1); 341 } 342 if (*special == '\0') 343 fatal(gettext( 344 "%s: not a raw disk device"), name); 345 } 346 } 347 348 /* 349 * getdiskbydev() determines the characteristics of the special 350 * device on which the file system will be built. In the case 351 * of devices with SMI labels (that is, non-EFI labels), the 352 * following characteristics are set (if they were not already 353 * set on the command line, since the command line settings 354 * take precedence): 355 * 356 * nsectors - sectors per track 357 * ntracks - tracks per cylinder 358 * rpm - disk revolutions per minute 359 * 360 * apc is NOT set 361 * 362 * getdiskbydev() also sets the following quantities for all 363 * devices, if not already set: 364 * 365 * bsize - file system block size 366 * maxcontig 367 * label_type (efi, vtoc, or other) 368 * 369 * getdiskbydev() returns the actual size of the device, in 370 * sectors. 371 */ 372 373 actual_fssize = getdiskbydev(special); 374 375 if (req_fssize == 0) { 376 fssize = actual_fssize; 377 } else { 378 /* 379 * If the user specified a size larger than what we've 380 * determined as the actual size of the device, see if the 381 * size specified by the user can be read. If so, use it, 382 * since some devices and volume managers may not support 383 * the vtoc and EFI interfaces we use to determine device 384 * size. 385 */ 386 if (req_fssize > actual_fssize && 387 validate_size(special, req_fssize)) { 388 (void) fprintf(stderr, gettext( 389 "Warning: the requested size of this file system\n" 390 "(%lld sectors) is greater than the size of the\n" 391 "device reported by the driver (%lld sectors).\n" 392 "However, a read of the device at the requested size\n" 393 "does succeed, so the requested size will be used.\n"), 394 req_fssize, actual_fssize); 395 fssize = req_fssize; 396 } else { 397 fssize = MIN(req_fssize, actual_fssize); 398 } 399 } 400 401 if (label_type == LABEL_TYPE_VTOC) { 402 if (nsectors < 0) 403 fatal(gettext("%s: no default #sectors/track"), 404 special); 405 if (ntracks < 0) 406 fatal(gettext("%s: no default #tracks"), special); 407 if (rpm < 0) 408 fatal(gettext( 409 "%s: no default revolutions/minute value"), 410 special); 411 if (rpm < 60) { 412 (void) fprintf(stderr, 413 gettext("Warning: setting rpm to 60\n")); 414 rpm = 60; 415 } 416 } 417 if (label_type == LABEL_TYPE_EFI || label_type == LABEL_TYPE_OTHER) { 418 if (ntracks_set) 419 (void) fprintf(stderr, gettext( 420 "Warning: ntracks is obsolete for this device and will be ignored.\n")); 421 if (cpg_set) 422 (void) fprintf(stderr, gettext( 423 "Warning: cylinders/group is obsolete for this device and will be ignored.\n")); 424 if (rpm_set) 425 (void) fprintf(stderr, gettext( 426 "Warning: rpm is obsolete for this device and will be ignored.\n")); 427 if (rot_set) 428 (void) fprintf(stderr, gettext( 429 "Warning: rotational delay is obsolete for this device and" 430 " will be ignored.\n")); 431 if (nrpos_set) 432 (void) fprintf(stderr, gettext( 433 "Warning: number of rotational positions is obsolete for this device and\n" 434 "will be ignored.\n")); 435 if (apc_set) 436 (void) fprintf(stderr, gettext( 437 "Warning: number of alternate sectors per cylinder is obsolete for this\n" 438 "device and will be ignored.\n")); 439 440 /* 441 * We need these for the call to mkfs, even though they are 442 * meaningless. 443 */ 444 rpm = 60; 445 nrpos = 1; 446 apc = 0; 447 rot = -1; 448 449 /* 450 * These values are set to produce a file system with 451 * a cylinder group size of 48MB. For disks with 452 * non-EFI labels, most geometries result in cylinder 453 * groups of around 40 - 50 MB, so we arbitrarily choose 454 * 48MB for disks with EFI labels. mkfs will reduce 455 * cylinders per group even further if necessary. 456 */ 457 458 cpg = 16; 459 nsectors = 128; 460 ntracks = 48; 461 462 /* 463 * mkfs produces peculiar results for file systems 464 * that are smaller than one cylinder so don't allow 465 * them to be created (this check is only made for 466 * disks with EFI labels. Eventually, it should probably 467 * be enforced for all disks.) 468 */ 469 470 if (fssize < nsectors * ntracks) { 471 fatal(gettext( 472 "file system size must be at least %d sectors"), 473 nsectors * ntracks); 474 } 475 } 476 477 if (fssize > INT_MAX) 478 Tflag = 1; 479 480 /* 481 * If the user requested that the file system be set up for 482 * eventual growth to over a terabyte, or if it's already greater 483 * than a terabyte, set the inode density (nbpi) to MIN_MTB_DENSITY 484 * (unless the user has specified a larger nbpi), set the frag size 485 * equal to the block size, and set the cylinders-per-group value 486 * passed to mkfs to -1, which tells mkfs to make cylinder groups 487 * as large as possible. 488 */ 489 if (Tflag) { 490 if (density < MIN_MTB_DENSITY) 491 density = MIN_MTB_DENSITY; 492 fsize = bsize; 493 cpg = -1; /* says make cyl groups as big as possible */ 494 } else { 495 if (fsize == 0) 496 fsize = DESFRAGSIZE; 497 } 498 499 if (!POWEROF2(fsize)) { 500 (void) fprintf(stderr, gettext( 501 "newfs: fragment size must a power of 2, not %d\n"), fsize); 502 fsize = bsize/8; 503 (void) fprintf(stderr, gettext( 504 "newfs: fragsize reset to %ld\n"), fsize); 505 } 506 507 /* 508 * The file system is limited in size by the fragment size. 509 * The number of fragments in the file system must fit into 510 * a signed 32-bit quantity, so the number of sectors in the 511 * file system is INT_MAX * the number of sectors in a frag. 512 */ 513 514 max_possible_fssize = ((uint64_t)fsize)/DEV_BSIZE * INT_MAX; 515 if (fssize > max_possible_fssize) 516 fssize = max_possible_fssize; 517 518 /* 519 * Now fssize is the final size of the file system (in sectors). 520 * If it's less than what the user requested, print a message. 521 */ 522 if (fssize < req_fssize) { 523 (void) fprintf(stderr, gettext( 524 "newfs: requested size of %s disk blocks is too large.\n"), 525 req_fssize_str); 526 (void) fprintf(stderr, gettext( 527 "newfs: Resetting size to %lld\n"), fssize); 528 } 529 530 /* 531 * fssize now equals the size (in sectors) of the file system 532 * that will be created. 533 */ 534 535 /* XXX - following defaults are both here and in mkfs */ 536 if (density <= 0) { 537 if (fssize < GBSEC) 538 density = MINDENSITY; 539 else 540 density = (int)((((longlong_t)fssize + (GBSEC - 1)) / 541 GBSEC) * MINDENSITY); 542 if (density <= 0) 543 density = MINDENSITY; 544 if (density > MAXDEFDENSITY) 545 density = MAXDEFDENSITY; 546 } 547 if (cpg == 0) { 548 /* 549 * maxcpg calculation adapted from mkfs 550 * In the case of disks with EFI labels, cpg has 551 * already been set, so we won't enter this code. 552 */ 553 long maxcpg, maxipg; 554 555 maxipg = roundup(bsize * NBBY / 3, 556 bsize / sizeof (struct inode)); 557 maxcpg = (bsize - sizeof (struct cg) - howmany(maxipg, NBBY)) / 558 (sizeof (long) + nrpos * sizeof (short) + 559 nsectors / (MAXFRAG * NBBY)); 560 cpg = (fssize / GBSEC) * 32; 561 if (cpg > maxcpg) 562 cpg = maxcpg; 563 if (cpg <= 0) 564 cpg = MINCPG; 565 } 566 if (minfree < 0) { 567 minfree = ((float)MINFREESEC / fssize) * 100; 568 if (minfree > 10) 569 minfree = 10; 570 if (minfree <= 0) 571 minfree = 1; 572 } 573 #ifdef i386 /* Bug 1170182 */ 574 if (ntracks > 32 && (ntracks % 16) != 0) { 575 ntracks -= (ntracks % 16); 576 } 577 #endif 578 /* 579 * Confirmation 580 */ 581 if (isatty(fileno(stdin)) && !Nflag) { 582 /* 583 * If we can read a valid superblock, report the mount 584 * point on which this filesystem was last mounted. 585 */ 586 if (((sbp = read_sb(special)) != 0) && 587 (*sbp->fs_fsmnt != '\0')) { 588 (void) printf(gettext( 589 "newfs: %s last mounted as %s\n"), 590 special, sbp->fs_fsmnt); 591 } 592 (void) printf(gettext( 593 "newfs: construct a new file system %s: (y/n)? "), 594 special); 595 (void) fflush(stdout); 596 if (!yes()) 597 exit(0); 598 } 599 /* 600 * If alternates-per-cylinder is ever implemented: 601 * need to get apc from dp->d_apc if no -a switch??? 602 */ 603 (void) sprintf(cmd, 604 "mkfs -F ufs %s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s", 605 Nflag ? "-o N " : "", special, 606 fssize, nsectors, ntracks, bsize, fsize, cpg, minfree, rpm/60, 607 density, optim == FS_OPTSPACE ? "s" : "t", apc, rot, nrpos, 608 maxcontig, Tflag ? "y" : "n"); 609 if (verbose) { 610 (void) printf("%s\n", cmd); 611 (void) fflush(stdout); 612 } 613 exenv(); 614 if (status = system(cmd)) 615 exit(status >> 8); 616 if (Nflag) 617 exit(0); 618 (void) sprintf(cmd, "/usr/sbin/fsirand %s", special); 619 if (notrand(special) && (status = system(cmd)) != 0) 620 (void) fprintf(stderr, 621 gettext("%s: failed, status = %d\n"), 622 cmd, status); 623 return (0); 624 } 625 626 static void 627 exenv(void) 628 { 629 char *epath; /* executable file path */ 630 char *cpath; /* current path */ 631 632 if ((cpath = getenv("PATH")) == NULL) { 633 (void) fprintf(stderr, gettext("newfs: no PATH in env\n")); 634 /* 635 * Background: the Bourne shell interpolates "." into 636 * the path where said path starts with a colon, ends 637 * with a colon, or has two adjacent colons. Thus, 638 * the path ":/sbin::/usr/sbin:" is equivalent to 639 * ".:/sbin:.:/usr/sbin:.". Now, we have no cpath, 640 * and epath ends in a colon (to make for easy 641 * catenation in the normal case). By the above, if 642 * we use "", then "." becomes part of path. That's 643 * bad, so use CPATH (which is just a duplicate of some 644 * element in EPATH). No point in opening ourselves 645 * up to a Trojan horse attack when we don't have to.... 646 */ 647 cpath = CPATH; 648 } 649 if ((epath = malloc(strlen(EPATH) + strlen(cpath) + 1)) == NULL) { 650 (void) fprintf(stderr, gettext("newfs: malloc failed\n")); 651 exit(1); 652 } 653 (void) strcpy(epath, EPATH); 654 (void) strcat(epath, cpath); 655 if (putenv(epath) < 0) { 656 (void) fprintf(stderr, gettext("newfs: putenv failed\n")); 657 exit(1); 658 } 659 } 660 661 static int 662 yes(void) 663 { 664 int i, b; 665 666 i = b = getchar(); 667 while (b != '\n' && b != '\0' && b != EOF) 668 b = getchar(); 669 return (i == 'y'); 670 } 671 672 /* 673 * xxx Caller must run fmt through gettext(3) for us, if we ever 674 * xxx go the i18n route.... 675 */ 676 static void 677 fatal(char *fmt, ...) 678 { 679 va_list pvar; 680 681 (void) fprintf(stderr, "newfs: "); 682 va_start(pvar, fmt); 683 (void) vfprintf(stderr, fmt, pvar); 684 va_end(pvar); 685 (void) putc('\n', stderr); 686 exit(10); 687 } 688 689 static diskaddr_t 690 getdiskbydev(char *disk) 691 { 692 struct dk_geom g; 693 struct dk_cinfo ci; 694 diskaddr_t actual_size; 695 int fd; 696 697 if ((fd = open64(disk, 0)) < 0) { 698 perror(disk); 699 exit(1); 700 } 701 702 /* 703 * get_device_size() determines the actual size of the 704 * device, and also the disk's attributes, such as geometry. 705 */ 706 actual_size = get_device_size(fd, disk); 707 708 if (label_type == LABEL_TYPE_VTOC) { 709 if (ioctl(fd, DKIOCGGEOM, &g)) 710 fatal(gettext( 711 "%s: Unable to read Disk geometry"), disk); 712 if (nsectors == 0) 713 nsectors = g.dkg_nsect; 714 if (ntracks == 0) 715 ntracks = g.dkg_nhead; 716 if (rpm == 0) 717 rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm; 718 } 719 720 if (bsize == 0) 721 bsize = DESBLKSIZE; 722 /* 723 * Adjust maxcontig by the device's maxtransfer. If maxtransfer 724 * information is not available, default to the min of a MB and 725 * maxphys. 726 */ 727 if (maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) { 728 maxcontig = ci.dki_maxtransfer * DEV_BSIZE; 729 if (maxcontig < 0) { 730 int error, gotit, maxphys; 731 gotit = fsgetmaxphys(&maxphys, &error); 732 733 /* 734 * If we cannot get the maxphys value, default 735 * to ufs_maxmaxphys (MB). 736 */ 737 if (gotit) { 738 maxcontig = MIN(maxphys, MB); 739 } else { 740 (void) fprintf(stderr, gettext( 741 "Warning: Could not get system value for maxphys. The value for maxcontig\n" 742 "will default to 1MB.\n")); 743 maxcontig = MB; 744 } 745 } 746 maxcontig /= bsize; 747 } 748 (void) close(fd); 749 return (actual_size); 750 } 751 752 /* 753 * Figure out how big the partition we're dealing with is. 754 */ 755 static diskaddr_t 756 get_device_size(int fd, char *name) 757 { 758 struct vtoc vtoc; 759 dk_gpt_t *efi_vtoc; 760 diskaddr_t slicesize; 761 762 int index = read_vtoc(fd, &vtoc); 763 764 if (index >= 0) { 765 label_type = LABEL_TYPE_VTOC; 766 } else { 767 if (index == VT_ENOTSUP || index == VT_ERROR) { 768 /* it might be an EFI label */ 769 index = efi_alloc_and_read(fd, &efi_vtoc); 770 if (index >= 0) 771 label_type = LABEL_TYPE_EFI; 772 } 773 } 774 775 if (index < 0) { 776 /* 777 * Since both attempts to read the label failed, we're 778 * going to fall back to a brute force approach to 779 * determining the device's size: see how far out we can 780 * perform reads on the device. 781 */ 782 783 slicesize = brute_force_get_device_size(fd); 784 if (slicesize == 0) { 785 switch (index) { 786 case VT_ERROR: 787 (void) fprintf(stderr, gettext( 788 "newfs: %s: %s\n"), name, strerror(errno)); 789 exit(10); 790 /*NOTREACHED*/ 791 case VT_EIO: 792 fatal(gettext( 793 "%s: I/O error accessing VTOC"), name); 794 /*NOTREACHED*/ 795 case VT_EINVAL: 796 fatal(gettext( 797 "%s: Invalid field in VTOC"), name); 798 /*NOTREACHED*/ 799 default: 800 fatal(gettext( 801 "%s: unknown error accessing VTOC"), 802 name); 803 /*NOTREACHED*/ 804 } 805 } else { 806 label_type = LABEL_TYPE_OTHER; 807 } 808 } 809 810 if (label_type == LABEL_TYPE_EFI) { 811 slicesize = efi_vtoc->efi_parts[index].p_size; 812 efi_free(efi_vtoc); 813 } else if (label_type == LABEL_TYPE_VTOC) { 814 /* 815 * In the vtoc struct, p_size is a 32-bit signed quantity. 816 * In the dk_gpt struct (efi's version of the vtoc), p_size 817 * is an unsigned 64-bit quantity. By casting the vtoc's 818 * psize to an unsigned 32-bit quantity, it will be copied 819 * to 'slicesize' (an unsigned 64-bit diskaddr_t) without 820 * sign extension. 821 */ 822 823 slicesize = (uint32_t)vtoc.v_part[index].p_size; 824 } 825 826 return (slicesize); 827 } 828 829 /* 830 * brute_force_get_device_size 831 * 832 * Determine the size of the device by seeing how far we can 833 * read. Doing an llseek( , , SEEK_END) would probably work 834 * in most cases, but we've seen at least one third-party driver 835 * which doesn't correctly support the SEEK_END option when the 836 * the device is greater than a terabyte. 837 */ 838 839 static diskaddr_t 840 brute_force_get_device_size(int fd) 841 { 842 diskaddr_t min_fail = 0; 843 diskaddr_t max_succeed = 0; 844 diskaddr_t cur_db_off; 845 char buf[DEV_BSIZE]; 846 847 /* 848 * First, see if we can read the device at all, just to 849 * eliminate errors that have nothing to do with the 850 * device's size. 851 */ 852 853 if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) || 854 ((read(fd, buf, DEV_BSIZE)) == -1)) 855 return (0); /* can't determine size */ 856 857 /* 858 * Now, go sequentially through the multiples of 4TB 859 * to find the first read that fails (this isn't strictly 860 * the most efficient way to find the actual size if the 861 * size really could be anything between 0 and 2**64 bytes. 862 * We expect the sizes to be less than 16 TB for some time, 863 * so why do a bunch of reads that are larger than that? 864 * However, this algorithm *will* work for sizes of greater 865 * than 16 TB. We're just not optimizing for those sizes.) 866 */ 867 868 for (cur_db_off = SECTORS_PER_TERABYTE * 4; 869 min_fail == 0 && cur_db_off < FS_SIZE_UPPER_LIMIT; 870 cur_db_off += 4 * SECTORS_PER_TERABYTE) { 871 if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE), 872 SEEK_SET)) == -1) || 873 ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)) 874 min_fail = cur_db_off; 875 else 876 max_succeed = cur_db_off; 877 } 878 879 if (min_fail == 0) 880 return (0); 881 882 /* 883 * We now know that the size of the device is less than 884 * min_fail and greater than or equal to max_succeed. Now 885 * keep splitting the difference until the actual size in 886 * sectors in known. We also know that the difference 887 * between max_succeed and min_fail at this time is 888 * 4 * SECTORS_PER_TERABYTE, which is a power of two, which 889 * simplifies the math below. 890 */ 891 892 while (min_fail - max_succeed > 1) { 893 cur_db_off = max_succeed + (min_fail - max_succeed)/2; 894 if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE), 895 SEEK_SET)) == -1) || 896 ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)) 897 min_fail = cur_db_off; 898 else 899 max_succeed = cur_db_off; 900 } 901 902 /* the size is the last successfully read sector offset plus one */ 903 return (max_succeed + 1); 904 } 905 906 /* 907 * validate_size 908 * 909 * Return 1 if the device appears to be at least "size" sectors long. 910 * Return 0 if it's shorter or we can't read it. 911 */ 912 913 static int 914 validate_size(char *disk, diskaddr_t size) 915 { 916 char buf[DEV_BSIZE]; 917 int fd, rc; 918 919 if ((fd = open64(disk, O_RDONLY)) < 0) { 920 perror(disk); 921 exit(1); 922 } 923 924 if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) || 925 (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE) 926 rc = 0; 927 else 928 rc = 1; 929 (void) close(fd); 930 return (rc); 931 } 932 933 /* 934 * read_sb(char * rawdev) - Attempt to read the superblock from a raw device 935 * 936 * Returns: 937 * 0 : 938 * Could not read a valid superblock for a variety of reasons. 939 * Since 'newfs' handles any fatal conditions, we're not going 940 * to make any guesses as to why this is failing or what should 941 * be done about it. 942 * 943 * struct fs *: 944 * A pointer to (what we think is) a valid superblock. The 945 * space for the superblock is static (inside the function) 946 * since we will only be reading the values from it. 947 */ 948 949 struct fs * 950 read_sb(char *fsdev) 951 { 952 static struct fs sblock; 953 struct stat64 statb; 954 int dskfd; 955 char *bufp = NULL; 956 int bufsz = 0; 957 958 if (stat64(fsdev, &statb) < 0) 959 return (0); 960 961 if ((dskfd = open64(fsdev, O_RDONLY)) < 0) 962 return (0); 963 964 /* 965 * We need a buffer whose size is a multiple of DEV_BSIZE in order 966 * to read from a raw device (which we were probably passed). 967 */ 968 bufsz = ((sizeof (sblock) / DEV_BSIZE) + 1) * DEV_BSIZE; 969 if ((bufp = malloc(bufsz)) == NULL) { 970 (void) close(dskfd); 971 return (0); 972 } 973 974 if (llseek(dskfd, (offset_t)SBOFF, SEEK_SET) < 0 || 975 read(dskfd, bufp, bufsz) < 0) { 976 (void) close(dskfd); 977 free(bufp); 978 return (0); 979 } 980 (void) close(dskfd); /* Done with the file */ 981 982 (void) memcpy(&sblock, bufp, sizeof (sblock)); 983 free(bufp); /* Don't need this anymore */ 984 985 if (((sblock.fs_magic != FS_MAGIC) && 986 (sblock.fs_magic != MTB_UFS_MAGIC)) || 987 sblock.fs_ncg < 1 || sblock.fs_cpg < 1) 988 return (0); 989 990 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 991 (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 992 return (0); 993 994 if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE) 995 return (0); 996 997 return (&sblock); 998 } 999 1000 /* 1001 * Read the UFS file system on the raw device SPECIAL. If it does not 1002 * appear to be a UFS file system, return non-zero, indicating that 1003 * fsirand should be called (and it will spit out an error message). 1004 * If it is a UFS file system, take a look at the inodes in the first 1005 * cylinder group. If they appear to be randomized (non-zero), return 1006 * zero, which will cause fsirand to not be called. If the inode generation 1007 * counts are all zero, then we must call fsirand, so return non-zero. 1008 */ 1009 1010 #define RANDOMIZED 0 1011 #define NOT_RANDOMIZED 1 1012 1013 static int 1014 notrand(char *special) 1015 { 1016 long fsbuf[SBSIZE / sizeof (long)]; 1017 struct dinode dibuf[MAXBSIZE/sizeof (struct dinode)]; 1018 struct fs *fs; 1019 struct dinode *dip; 1020 offset_t seekaddr; 1021 int bno, inum; 1022 int fd; 1023 1024 fs = (struct fs *)fsbuf; 1025 if ((fd = open64(special, 0)) == -1) 1026 return (NOT_RANDOMIZED); 1027 if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1 || 1028 read(fd, (char *)fs, SBSIZE) != SBSIZE || 1029 ((fs->fs_magic != FS_MAGIC) && (fs->fs_magic != MTB_UFS_MAGIC))) { 1030 (void) close(fd); 1031 return (NOT_RANDOMIZED); 1032 } 1033 1034 /* looks like a UFS file system; read the first cylinder group */ 1035 bsize = INOPB(fs) * sizeof (struct dinode); 1036 inum = 0; 1037 while (inum < fs->fs_ipg) { 1038 bno = itod(fs, inum); 1039 seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE; 1040 if (llseek(fd, seekaddr, 0) == -1 || 1041 read(fd, (char *)dibuf, bsize) != bsize) { 1042 (void) close(fd); 1043 return (NOT_RANDOMIZED); 1044 } 1045 for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) { 1046 if (dip->di_gen != 0) { 1047 (void) close(fd); 1048 return (RANDOMIZED); 1049 } 1050 inum++; 1051 } 1052 } 1053 (void) close(fd); 1054 return (NOT_RANDOMIZED); 1055 } 1056 1057 static void 1058 usage(void) 1059 { 1060 (void) fprintf(stderr, gettext( 1061 "usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n")); 1062 (void) fprintf(stderr, gettext("where mkfs-options are:\n")); 1063 (void) fprintf(stderr, gettext( 1064 "\t-N do not create file system, just print out parameters\n")); 1065 (void) fprintf(stderr, gettext( 1066 "\t-T configure file system for eventual growth to over a terabyte\n")); 1067 (void) fprintf(stderr, gettext("\t-s file system size (sectors)\n")); 1068 (void) fprintf(stderr, gettext("\t-b block size\n")); 1069 (void) fprintf(stderr, gettext("\t-f frag size\n")); 1070 (void) fprintf(stderr, gettext("\t-t tracks/cylinder\n")); 1071 (void) fprintf(stderr, gettext("\t-c cylinders/group\n")); 1072 (void) fprintf(stderr, gettext("\t-m minimum free space %%\n")); 1073 (void) fprintf(stderr, gettext( 1074 "\t-o optimization preference (`space' or `time')\n")); 1075 (void) fprintf(stderr, gettext("\t-r revolutions/minute\n")); 1076 (void) fprintf(stderr, gettext("\t-i number of bytes per inode\n")); 1077 (void) fprintf(stderr, gettext( 1078 "\t-a number of alternates per cylinder\n")); 1079 (void) fprintf(stderr, gettext("\t-C maxcontig\n")); 1080 (void) fprintf(stderr, gettext("\t-d rotational delay\n")); 1081 (void) fprintf(stderr, gettext( 1082 "\t-n number of rotational positions\n")); 1083 } 1084 1085 /* 1086 * Error-detecting version of atoi(3). Adapted from mkfs' number(). 1087 */ 1088 static unsigned int 1089 number(char *param, char *value, int flags, int def_value) 1090 { 1091 char *cs; 1092 int n; 1093 int cut = INT_MAX / 10; /* limit to avoid overflow */ 1094 int minus = 0; 1095 1096 cs = value; 1097 if (*cs == '-') { 1098 minus = 1; 1099 cs += 1; 1100 } 1101 if ((*cs < '0') || (*cs > '9')) { 1102 goto bail_out; 1103 } 1104 n = 0; 1105 while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) { 1106 n = n*10 + *cs++ - '0'; 1107 } 1108 if (minus) 1109 n = -n; 1110 for (;;) { 1111 switch (*cs++) { 1112 case '\0': 1113 return (n); 1114 1115 case '0': case '1': case '2': case '3': case '4': 1116 case '5': case '6': case '7': case '8': case '9': 1117 (void) fprintf(stderr, gettext( 1118 "newfs: value for %s overflowed, using %d\n"), 1119 param, def_value); 1120 return (def_value); 1121 1122 case '%': 1123 if (flags & NR_PERCENT) 1124 break; 1125 /* FALLTHROUGH */ 1126 1127 default: 1128 bail_out: 1129 fatal(gettext("bad numeric arg for %s: \"%s\""), 1130 param, value); 1131 1132 } 1133 } 1134 /* NOTREACHED */ 1135 } 1136 1137 /* 1138 * Error-detecting version of atoi(3). Adapted from mkfs' number(). 1139 */ 1140 static int64_t 1141 number64(char *param, char *value, int flags, int64_t def_value) 1142 { 1143 char *cs; 1144 int64_t n; 1145 int64_t cut = FS_SIZE_UPPER_LIMIT/ 10; /* limit to avoid overflow */ 1146 int minus = 0; 1147 1148 cs = value; 1149 if (*cs == '-') { 1150 minus = 1; 1151 cs += 1; 1152 } 1153 if ((*cs < '0') || (*cs > '9')) { 1154 goto bail_out; 1155 } 1156 n = 0; 1157 while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) { 1158 n = n*10 + *cs++ - '0'; 1159 } 1160 if (minus) 1161 n = -n; 1162 for (;;) { 1163 switch (*cs++) { 1164 case '\0': 1165 return (n); 1166 1167 case '0': case '1': case '2': case '3': case '4': 1168 case '5': case '6': case '7': case '8': case '9': 1169 (void) fprintf(stderr, gettext( 1170 "newfs: value for %s overflowed, using %d\n"), 1171 param, def_value); 1172 return (def_value); 1173 1174 case '%': 1175 if (flags & NR_PERCENT) 1176 break; 1177 /* FALLTHROUGH */ 1178 1179 default: 1180 bail_out: 1181 fatal(gettext("bad numeric arg for %s: \"%s\""), 1182 param, value); 1183 1184 } 1185 } 1186 /* NOTREACHED */ 1187 } 1188