17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate 237c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 247c478bd9Sstevel@tonic-gate /* from UCB 5.2 9/11/85 */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * newfs: friendly front end to mkfs 287c478bd9Sstevel@tonic-gate * 29355d6bb5Sswilcox * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 307c478bd9Sstevel@tonic-gate * Use is subject to license terms. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/param.h> 347c478bd9Sstevel@tonic-gate #include <sys/types.h> 357c478bd9Sstevel@tonic-gate #include <locale.h> 367c478bd9Sstevel@tonic-gate #include <sys/stat.h> 377c478bd9Sstevel@tonic-gate #include <sys/buf.h> 387c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h> 397c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 407c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h> 417c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include <errno.h> 447c478bd9Sstevel@tonic-gate #include <stdio.h> 457c478bd9Sstevel@tonic-gate #include <string.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <stdarg.h> 487c478bd9Sstevel@tonic-gate #include <stdio.h> 497c478bd9Sstevel@tonic-gate #include <fcntl.h> 507c478bd9Sstevel@tonic-gate #include <unistd.h> 517c478bd9Sstevel@tonic-gate #include <limits.h> 527c478bd9Sstevel@tonic-gate #include <libintl.h> 537c478bd9Sstevel@tonic-gate #include <sys/dkio.h> 547c478bd9Sstevel@tonic-gate #include <sys/vtoc.h> 557c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 567c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h> 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #include <fslib.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate static unsigned int number(char *, char *, int, int); 617c478bd9Sstevel@tonic-gate static int64_t number64(char *, char *, int, int64_t); 627c478bd9Sstevel@tonic-gate static diskaddr_t getdiskbydev(char *); 637c478bd9Sstevel@tonic-gate static int yes(void); 647c478bd9Sstevel@tonic-gate static int notrand(char *); 657c478bd9Sstevel@tonic-gate static void usage(); 667c478bd9Sstevel@tonic-gate static diskaddr_t get_device_size(int, char *); 677c478bd9Sstevel@tonic-gate static diskaddr_t brute_force_get_device_size(int); 687c478bd9Sstevel@tonic-gate static int validate_size(char *disk, diskaddr_t size); 697c478bd9Sstevel@tonic-gate static void exenv(void); 707c478bd9Sstevel@tonic-gate static struct fs *read_sb(char *); 717c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 727c478bd9Sstevel@tonic-gate static void fatal(char *fmt, ...); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate #define EPATH "PATH=/usr/sbin:/sbin:" 757c478bd9Sstevel@tonic-gate #define CPATH "/sbin" /* an EPATH element */ 767c478bd9Sstevel@tonic-gate #define MB (1024 * 1024) 777c478bd9Sstevel@tonic-gate #define GBSEC ((1024 * 1024 * 1024) / DEV_BSIZE) /* sectors in a GB */ 787c478bd9Sstevel@tonic-gate #define MINFREESEC ((64 * 1024 * 1024) / DEV_BSIZE) /* sectors in 64 MB */ 797c478bd9Sstevel@tonic-gate #define MINCPG (16) /* traditional */ 807c478bd9Sstevel@tonic-gate #define MAXDEFDENSITY (8 * 1024) /* arbitrary */ 817c478bd9Sstevel@tonic-gate #define MINDENSITY (2 * 1024) /* traditional */ 827c478bd9Sstevel@tonic-gate #define MIN_MTB_DENSITY (1024 * 1024) 837c478bd9Sstevel@tonic-gate #define POWEROF2(num) (((num) & ((num) - 1)) == 0) 847c478bd9Sstevel@tonic-gate #define SECTORS_PER_TERABYTE (1LL << 31) 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * The following constant specifies an upper limit for file system size 877c478bd9Sstevel@tonic-gate * that is actually a lot bigger than we expect to support with UFS. (Since 887c478bd9Sstevel@tonic-gate * it's specified in sectors, the file system size would be 2**44 * 512, 897c478bd9Sstevel@tonic-gate * which is 2**53, which is 8192 Terabytes.) However, it's useful 907c478bd9Sstevel@tonic-gate * for checking the basic sanity of a size value that is input on the 917c478bd9Sstevel@tonic-gate * command line. 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate #define FS_SIZE_UPPER_LIMIT 0x100000000000LL 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* For use with number() */ 967c478bd9Sstevel@tonic-gate #define NR_NONE 0 977c478bd9Sstevel@tonic-gate #define NR_PERCENT 0x01 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * The following two constants set the default block and fragment sizes. 1017c478bd9Sstevel@tonic-gate * Both constants must be a power of 2 and meet the following constraints: 1027c478bd9Sstevel@tonic-gate * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 1037c478bd9Sstevel@tonic-gate * DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE 1047c478bd9Sstevel@tonic-gate * DESBLKSIZE / DESFRAGSIZE <= 8 1057c478bd9Sstevel@tonic-gate */ 1067c478bd9Sstevel@tonic-gate #define DESBLKSIZE 8192 1077c478bd9Sstevel@tonic-gate #define DESFRAGSIZE 1024 1087c478bd9Sstevel@tonic-gate 109*6451fdbcSvsakar #define dprintf(x) if (debug) printf x 110*6451fdbcSvsakar 111*6451fdbcSvsakar int debug = 0; /* enable debug output */ 1127c478bd9Sstevel@tonic-gate static int Nflag; /* run mkfs without writing file system */ 1137c478bd9Sstevel@tonic-gate static int Tflag; /* set up file system for growth to over 1 TB */ 1147c478bd9Sstevel@tonic-gate static int verbose; /* show mkfs line before exec */ 1157c478bd9Sstevel@tonic-gate static int fsize = 0; /* fragment size */ 1167c478bd9Sstevel@tonic-gate static int fsize_flag = 0; /* fragment size was specified on cmd line */ 1177c478bd9Sstevel@tonic-gate static int bsize; /* block size */ 1187c478bd9Sstevel@tonic-gate static int ntracks; /* # tracks/cylinder */ 1197c478bd9Sstevel@tonic-gate static int ntracks_set = 0; /* true if the user specified ntracks */ 1207c478bd9Sstevel@tonic-gate static int optim = FS_OPTTIME; /* optimization, t(ime) or s(pace) */ 1217c478bd9Sstevel@tonic-gate static int nsectors; /* # sectors/track */ 122*6451fdbcSvsakar static int geom_nsectors; /* # sectors/track as returned by GEOM ioctl */ 1237c478bd9Sstevel@tonic-gate static int cpg; /* cylinders/cylinder group */ 1247c478bd9Sstevel@tonic-gate static int cpg_set = 0; /* true if the user specified cpg */ 1257c478bd9Sstevel@tonic-gate static int minfree = -1; /* free space threshold */ 1267c478bd9Sstevel@tonic-gate static int rpm; /* revolutions/minute of drive */ 1277c478bd9Sstevel@tonic-gate static int rpm_set = 0; /* true if the user specified rpm */ 1287c478bd9Sstevel@tonic-gate static int nrpos = 8; /* # of distinguished rotational positions */ 1297c478bd9Sstevel@tonic-gate /* 8 is the historical default */ 1307c478bd9Sstevel@tonic-gate static int nrpos_set = 0; /* true if the user specified nrpos */ 1317c478bd9Sstevel@tonic-gate static int density = 0; /* number of bytes per inode */ 1327c478bd9Sstevel@tonic-gate static int apc; /* alternates per cylinder */ 1337c478bd9Sstevel@tonic-gate static int apc_set = 0; /* true if the user specified apc */ 1347c478bd9Sstevel@tonic-gate static int rot = -1; /* rotational delay (msecs) */ 1357c478bd9Sstevel@tonic-gate static int rot_set = 0; /* true if the user specified rot */ 1367c478bd9Sstevel@tonic-gate static int maxcontig = -1; /* maximum number of contig blocks */ 137355d6bb5Sswilcox static int text_sb = 0; /* no disk changes; just final sb text dump */ 138355d6bb5Sswilcox static int binary_sb = 0; /* no disk changes; just final sb binary dump */ 1397c478bd9Sstevel@tonic-gate static int label_type; /* see types below */ 1407c478bd9Sstevel@tonic-gate 141*6451fdbcSvsakar /* 142*6451fdbcSvsakar * The variable use_efi_dflts is an indicator of whether to use EFI logic 143*6451fdbcSvsakar * or the geometry logic in laying out the filesystem. This is decided 144*6451fdbcSvsakar * based on the size of the disk and is used only for non-EFI labeled disks. 145*6451fdbcSvsakar */ 146*6451fdbcSvsakar static int use_efi_dflts = 0; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate static char device[MAXPATHLEN]; 1497c478bd9Sstevel@tonic-gate static char cmd[BUFSIZ]; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate extern char *getfullrawname(); /* from libadm */ 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate int 1547c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate char *special, *name; 1577c478bd9Sstevel@tonic-gate struct stat64 st; 1587c478bd9Sstevel@tonic-gate int status; 1597c478bd9Sstevel@tonic-gate int option; 160*6451fdbcSvsakar int nsect; 1617c478bd9Sstevel@tonic-gate struct fs *sbp; /* Pointer to superblock (if present) */ 1627c478bd9Sstevel@tonic-gate diskaddr_t actual_fssize; 1637c478bd9Sstevel@tonic-gate diskaddr_t max_possible_fssize; 1647c478bd9Sstevel@tonic-gate diskaddr_t req_fssize = 0; 1657c478bd9Sstevel@tonic-gate diskaddr_t fssize = 0; 1667c478bd9Sstevel@tonic-gate char *req_fssize_str = NULL; /* requested size argument */ 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 1717c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 1727c478bd9Sstevel@tonic-gate #endif 1737c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate opterr = 0; /* We print our own errors, disable getopt's message */ 176355d6bb5Sswilcox while ((option = getopt(argc, argv, 177355d6bb5Sswilcox "vNBSs:C:d:t:o:a:b:f:c:m:n:r:i:T")) != EOF) { 1787c478bd9Sstevel@tonic-gate switch (option) { 179355d6bb5Sswilcox case 'S': 180355d6bb5Sswilcox text_sb++; 181355d6bb5Sswilcox break; 182355d6bb5Sswilcox case 'B': 183355d6bb5Sswilcox binary_sb++; 184355d6bb5Sswilcox break; 1857c478bd9Sstevel@tonic-gate case 'v': 1867c478bd9Sstevel@tonic-gate verbose++; 1877c478bd9Sstevel@tonic-gate break; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate case 'N': 1907c478bd9Sstevel@tonic-gate Nflag++; 1917c478bd9Sstevel@tonic-gate break; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate case 's': 1947c478bd9Sstevel@tonic-gate /* 1957c478bd9Sstevel@tonic-gate * The maximum file system size is a lot smaller 1967c478bd9Sstevel@tonic-gate * than FS_SIZE_UPPER_LIMIT, but until we find out 1977c478bd9Sstevel@tonic-gate * the device size and block size, we don't know 1987c478bd9Sstevel@tonic-gate * what it is. So save the requested size in a 1997c478bd9Sstevel@tonic-gate * string so that we can print it out later if we 2007c478bd9Sstevel@tonic-gate * determine it's too big. 2017c478bd9Sstevel@tonic-gate */ 2027c478bd9Sstevel@tonic-gate req_fssize = number64("fssize", optarg, NR_NONE, 2037c478bd9Sstevel@tonic-gate FS_SIZE_UPPER_LIMIT); 2047c478bd9Sstevel@tonic-gate if (req_fssize < 1024) 2057c478bd9Sstevel@tonic-gate fatal(gettext( 2067c478bd9Sstevel@tonic-gate "%s: fssize must be at least 1024"), 2077c478bd9Sstevel@tonic-gate optarg); 2087c478bd9Sstevel@tonic-gate req_fssize_str = strdup(optarg); 2097c478bd9Sstevel@tonic-gate if (req_fssize_str == NULL) 2107c478bd9Sstevel@tonic-gate fatal(gettext( 2117c478bd9Sstevel@tonic-gate "Insufficient memory for string copy.")); 2127c478bd9Sstevel@tonic-gate break; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate case 'C': 2157c478bd9Sstevel@tonic-gate maxcontig = number("maxcontig", optarg, NR_NONE, -1); 2167c478bd9Sstevel@tonic-gate if (maxcontig < 0) 2177c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad maxcontig"), optarg); 2187c478bd9Sstevel@tonic-gate break; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate case 'd': 2217c478bd9Sstevel@tonic-gate rot = number("rotdelay", optarg, NR_NONE, 0); 2227c478bd9Sstevel@tonic-gate rot_set = 1; 2237c478bd9Sstevel@tonic-gate if (rot < 0 || rot > 1000) 2247c478bd9Sstevel@tonic-gate fatal(gettext( 2257c478bd9Sstevel@tonic-gate "%s: bad rotational delay"), optarg); 2267c478bd9Sstevel@tonic-gate break; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate case 't': 2297c478bd9Sstevel@tonic-gate ntracks = number("ntrack", optarg, NR_NONE, 16); 2307c478bd9Sstevel@tonic-gate ntracks_set = 1; 2317c478bd9Sstevel@tonic-gate if ((ntracks < 0) || 2327c478bd9Sstevel@tonic-gate (ntracks > INT_MAX)) 2337c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad total tracks"), optarg); 2347c478bd9Sstevel@tonic-gate break; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate case 'o': 2377c478bd9Sstevel@tonic-gate if (strcmp(optarg, "space") == 0) 2387c478bd9Sstevel@tonic-gate optim = FS_OPTSPACE; 2397c478bd9Sstevel@tonic-gate else if (strcmp(optarg, "time") == 0) 2407c478bd9Sstevel@tonic-gate optim = FS_OPTTIME; 2417c478bd9Sstevel@tonic-gate else 2427c478bd9Sstevel@tonic-gate fatal(gettext( 2437c478bd9Sstevel@tonic-gate "%s: bad optimization preference (options are `space' or `time')"), 2447c478bd9Sstevel@tonic-gate optarg); 2457c478bd9Sstevel@tonic-gate break; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate case 'a': 2487c478bd9Sstevel@tonic-gate apc = number("apc", optarg, NR_NONE, 0); 2497c478bd9Sstevel@tonic-gate apc_set = 1; 2507c478bd9Sstevel@tonic-gate if (apc < 0 || apc > 32768) /* see mkfs.c */ 2517c478bd9Sstevel@tonic-gate fatal(gettext( 2527c478bd9Sstevel@tonic-gate "%s: bad alternates per cyl"), optarg); 2537c478bd9Sstevel@tonic-gate break; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate case 'b': 2567c478bd9Sstevel@tonic-gate bsize = number("bsize", optarg, NR_NONE, DESBLKSIZE); 2577c478bd9Sstevel@tonic-gate if (bsize < MINBSIZE || bsize > MAXBSIZE) 2587c478bd9Sstevel@tonic-gate fatal(gettext( 2597c478bd9Sstevel@tonic-gate "%s: bad block size"), optarg); 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate case 'f': 2637c478bd9Sstevel@tonic-gate fsize = number("fragsize", optarg, NR_NONE, 2647c478bd9Sstevel@tonic-gate DESFRAGSIZE); 2657c478bd9Sstevel@tonic-gate fsize_flag++; 2667c478bd9Sstevel@tonic-gate /* xxx ought to test against bsize for upper limit */ 2677c478bd9Sstevel@tonic-gate if (fsize < DEV_BSIZE) 2687c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad frag size"), optarg); 2697c478bd9Sstevel@tonic-gate break; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate case 'c': 2727c478bd9Sstevel@tonic-gate cpg = number("cpg", optarg, NR_NONE, 16); 2737c478bd9Sstevel@tonic-gate cpg_set = 1; 2747c478bd9Sstevel@tonic-gate if (cpg < 1) 2757c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad cylinders/group"), 2767c478bd9Sstevel@tonic-gate optarg); 2777c478bd9Sstevel@tonic-gate break; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate case 'm': 2807c478bd9Sstevel@tonic-gate minfree = number("minfree", optarg, NR_PERCENT, 10); 2817c478bd9Sstevel@tonic-gate if (minfree < 0 || minfree > 99) 2827c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad free space %%"), optarg); 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate case 'n': 2867c478bd9Sstevel@tonic-gate nrpos = number("nrpos", optarg, NR_NONE, 8); 2877c478bd9Sstevel@tonic-gate nrpos_set = 1; 2887c478bd9Sstevel@tonic-gate if (nrpos <= 0) 2897c478bd9Sstevel@tonic-gate fatal(gettext( 2907c478bd9Sstevel@tonic-gate "%s: bad number of rotational positions"), 2917c478bd9Sstevel@tonic-gate optarg); 2927c478bd9Sstevel@tonic-gate break; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate case 'r': 2957c478bd9Sstevel@tonic-gate rpm = number("rpm", optarg, NR_NONE, 3600); 2967c478bd9Sstevel@tonic-gate rpm_set = 1; 2977c478bd9Sstevel@tonic-gate if (rpm < 0) 2987c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad revs/minute"), optarg); 2997c478bd9Sstevel@tonic-gate break; 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate case 'i': 3027c478bd9Sstevel@tonic-gate /* xxx ought to test against fsize */ 3037c478bd9Sstevel@tonic-gate density = number("nbpi", optarg, NR_NONE, 2048); 3047c478bd9Sstevel@tonic-gate if (density < DEV_BSIZE) 3057c478bd9Sstevel@tonic-gate fatal(gettext("%s: bad bytes per inode"), 3067c478bd9Sstevel@tonic-gate optarg); 3077c478bd9Sstevel@tonic-gate break; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate case 'T': 3107c478bd9Sstevel@tonic-gate Tflag++; 3117c478bd9Sstevel@tonic-gate break; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate default: 3147c478bd9Sstevel@tonic-gate usage(); 3157c478bd9Sstevel@tonic-gate fatal(gettext("-%c: unknown flag"), optopt); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate } 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate /* At this point, there should only be one argument left: */ 3207c478bd9Sstevel@tonic-gate /* The raw-special-device itself. If not, print usage message. */ 3217c478bd9Sstevel@tonic-gate if ((argc - optind) != 1) { 3227c478bd9Sstevel@tonic-gate usage(); 3237c478bd9Sstevel@tonic-gate exit(1); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate name = argv[optind]; 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate special = getfullrawname(name); 3297c478bd9Sstevel@tonic-gate if (special == NULL) { 3307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("newfs: malloc failed\n")); 3317c478bd9Sstevel@tonic-gate exit(1); 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate if (*special == '\0') { 3357c478bd9Sstevel@tonic-gate if (strchr(name, '/') != NULL) { 3367c478bd9Sstevel@tonic-gate if (stat64(name, &st) < 0) { 3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3387c478bd9Sstevel@tonic-gate gettext("newfs: %s: %s\n"), 3397c478bd9Sstevel@tonic-gate name, strerror(errno)); 3407c478bd9Sstevel@tonic-gate exit(2); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate fatal(gettext("%s: not a raw disk device"), name); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate (void) sprintf(device, "/dev/rdsk/%s", name); 3457c478bd9Sstevel@tonic-gate if ((special = getfullrawname(device)) == NULL) { 3467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3477c478bd9Sstevel@tonic-gate gettext("newfs: malloc failed\n")); 3487c478bd9Sstevel@tonic-gate exit(1); 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate if (*special == '\0') { 3527c478bd9Sstevel@tonic-gate (void) sprintf(device, "/dev/%s", name); 3537c478bd9Sstevel@tonic-gate if ((special = getfullrawname(device)) == NULL) { 3547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 3557c478bd9Sstevel@tonic-gate gettext("newfs: malloc failed\n")); 3567c478bd9Sstevel@tonic-gate exit(1); 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate if (*special == '\0') 3597c478bd9Sstevel@tonic-gate fatal(gettext( 3607c478bd9Sstevel@tonic-gate "%s: not a raw disk device"), name); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * getdiskbydev() determines the characteristics of the special 3667c478bd9Sstevel@tonic-gate * device on which the file system will be built. In the case 3677c478bd9Sstevel@tonic-gate * of devices with SMI labels (that is, non-EFI labels), the 3687c478bd9Sstevel@tonic-gate * following characteristics are set (if they were not already 3697c478bd9Sstevel@tonic-gate * set on the command line, since the command line settings 3707c478bd9Sstevel@tonic-gate * take precedence): 3717c478bd9Sstevel@tonic-gate * 3727c478bd9Sstevel@tonic-gate * nsectors - sectors per track 3737c478bd9Sstevel@tonic-gate * ntracks - tracks per cylinder 3747c478bd9Sstevel@tonic-gate * rpm - disk revolutions per minute 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate * apc is NOT set 3777c478bd9Sstevel@tonic-gate * 3787c478bd9Sstevel@tonic-gate * getdiskbydev() also sets the following quantities for all 3797c478bd9Sstevel@tonic-gate * devices, if not already set: 3807c478bd9Sstevel@tonic-gate * 3817c478bd9Sstevel@tonic-gate * bsize - file system block size 3827c478bd9Sstevel@tonic-gate * maxcontig 3837c478bd9Sstevel@tonic-gate * label_type (efi, vtoc, or other) 3847c478bd9Sstevel@tonic-gate * 3857c478bd9Sstevel@tonic-gate * getdiskbydev() returns the actual size of the device, in 3867c478bd9Sstevel@tonic-gate * sectors. 3877c478bd9Sstevel@tonic-gate */ 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate actual_fssize = getdiskbydev(special); 3907c478bd9Sstevel@tonic-gate 3917c478bd9Sstevel@tonic-gate if (req_fssize == 0) { 3927c478bd9Sstevel@tonic-gate fssize = actual_fssize; 3937c478bd9Sstevel@tonic-gate } else { 3947c478bd9Sstevel@tonic-gate /* 3957c478bd9Sstevel@tonic-gate * If the user specified a size larger than what we've 3967c478bd9Sstevel@tonic-gate * determined as the actual size of the device, see if the 3977c478bd9Sstevel@tonic-gate * size specified by the user can be read. If so, use it, 3987c478bd9Sstevel@tonic-gate * since some devices and volume managers may not support 3997c478bd9Sstevel@tonic-gate * the vtoc and EFI interfaces we use to determine device 4007c478bd9Sstevel@tonic-gate * size. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate if (req_fssize > actual_fssize && 4037c478bd9Sstevel@tonic-gate validate_size(special, req_fssize)) { 4047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4057c478bd9Sstevel@tonic-gate "Warning: the requested size of this file system\n" 4067c478bd9Sstevel@tonic-gate "(%lld sectors) is greater than the size of the\n" 4077c478bd9Sstevel@tonic-gate "device reported by the driver (%lld sectors).\n" 4087c478bd9Sstevel@tonic-gate "However, a read of the device at the requested size\n" 4097c478bd9Sstevel@tonic-gate "does succeed, so the requested size will be used.\n"), 4107c478bd9Sstevel@tonic-gate req_fssize, actual_fssize); 4117c478bd9Sstevel@tonic-gate fssize = req_fssize; 4127c478bd9Sstevel@tonic-gate } else { 4137c478bd9Sstevel@tonic-gate fssize = MIN(req_fssize, actual_fssize); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate if (label_type == LABEL_TYPE_VTOC) { 418*6451fdbcSvsakar if (!use_efi_dflts) { 419*6451fdbcSvsakar if (nsectors < 0) 420*6451fdbcSvsakar fatal(gettext("%s: no default #sectors/track"), 421*6451fdbcSvsakar special); 422*6451fdbcSvsakar if (ntracks < 0) 423*6451fdbcSvsakar fatal(gettext("%s: no default #tracks"), 424*6451fdbcSvsakar special); 425*6451fdbcSvsakar } 4267c478bd9Sstevel@tonic-gate if (rpm < 0) 4277c478bd9Sstevel@tonic-gate fatal(gettext( 4287c478bd9Sstevel@tonic-gate "%s: no default revolutions/minute value"), 4297c478bd9Sstevel@tonic-gate special); 4307c478bd9Sstevel@tonic-gate if (rpm < 60) { 4317c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4327c478bd9Sstevel@tonic-gate gettext("Warning: setting rpm to 60\n")); 4337c478bd9Sstevel@tonic-gate rpm = 60; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate if (label_type == LABEL_TYPE_EFI || label_type == LABEL_TYPE_OTHER) { 4377c478bd9Sstevel@tonic-gate if (ntracks_set) 4387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4397c478bd9Sstevel@tonic-gate "Warning: ntracks is obsolete for this device and will be ignored.\n")); 4407c478bd9Sstevel@tonic-gate if (cpg_set) 4417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4427c478bd9Sstevel@tonic-gate "Warning: cylinders/group is obsolete for this device and will be ignored.\n")); 4437c478bd9Sstevel@tonic-gate if (rpm_set) 4447c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4457c478bd9Sstevel@tonic-gate "Warning: rpm is obsolete for this device and will be ignored.\n")); 4467c478bd9Sstevel@tonic-gate if (rot_set) 4477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4487c478bd9Sstevel@tonic-gate "Warning: rotational delay is obsolete for this device and" 4497c478bd9Sstevel@tonic-gate " will be ignored.\n")); 4507c478bd9Sstevel@tonic-gate if (nrpos_set) 4517c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4527c478bd9Sstevel@tonic-gate "Warning: number of rotational positions is obsolete for this device and\n" 4537c478bd9Sstevel@tonic-gate "will be ignored.\n")); 4547c478bd9Sstevel@tonic-gate if (apc_set) 4557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 4567c478bd9Sstevel@tonic-gate "Warning: number of alternate sectors per cylinder is obsolete for this\n" 4577c478bd9Sstevel@tonic-gate "device and will be ignored.\n")); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate /* 4607c478bd9Sstevel@tonic-gate * We need these for the call to mkfs, even though they are 4617c478bd9Sstevel@tonic-gate * meaningless. 4627c478bd9Sstevel@tonic-gate */ 4637c478bd9Sstevel@tonic-gate rpm = 60; 4647c478bd9Sstevel@tonic-gate nrpos = 1; 4657c478bd9Sstevel@tonic-gate apc = 0; 4667c478bd9Sstevel@tonic-gate rot = -1; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* 4697c478bd9Sstevel@tonic-gate * These values are set to produce a file system with 4707c478bd9Sstevel@tonic-gate * a cylinder group size of 48MB. For disks with 4717c478bd9Sstevel@tonic-gate * non-EFI labels, most geometries result in cylinder 4727c478bd9Sstevel@tonic-gate * groups of around 40 - 50 MB, so we arbitrarily choose 4737c478bd9Sstevel@tonic-gate * 48MB for disks with EFI labels. mkfs will reduce 4747c478bd9Sstevel@tonic-gate * cylinders per group even further if necessary. 4757c478bd9Sstevel@tonic-gate */ 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate cpg = 16; 4787c478bd9Sstevel@tonic-gate nsectors = 128; 4797c478bd9Sstevel@tonic-gate ntracks = 48; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate /* 4827c478bd9Sstevel@tonic-gate * mkfs produces peculiar results for file systems 4837c478bd9Sstevel@tonic-gate * that are smaller than one cylinder so don't allow 4847c478bd9Sstevel@tonic-gate * them to be created (this check is only made for 4857c478bd9Sstevel@tonic-gate * disks with EFI labels. Eventually, it should probably 4867c478bd9Sstevel@tonic-gate * be enforced for all disks.) 4877c478bd9Sstevel@tonic-gate */ 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate if (fssize < nsectors * ntracks) { 4907c478bd9Sstevel@tonic-gate fatal(gettext( 4917c478bd9Sstevel@tonic-gate "file system size must be at least %d sectors"), 4927c478bd9Sstevel@tonic-gate nsectors * ntracks); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate if (fssize > INT_MAX) 4977c478bd9Sstevel@tonic-gate Tflag = 1; 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate /* 5007c478bd9Sstevel@tonic-gate * If the user requested that the file system be set up for 5017c478bd9Sstevel@tonic-gate * eventual growth to over a terabyte, or if it's already greater 5027c478bd9Sstevel@tonic-gate * than a terabyte, set the inode density (nbpi) to MIN_MTB_DENSITY 5037c478bd9Sstevel@tonic-gate * (unless the user has specified a larger nbpi), set the frag size 5047c478bd9Sstevel@tonic-gate * equal to the block size, and set the cylinders-per-group value 5057c478bd9Sstevel@tonic-gate * passed to mkfs to -1, which tells mkfs to make cylinder groups 5067c478bd9Sstevel@tonic-gate * as large as possible. 5077c478bd9Sstevel@tonic-gate */ 5087c478bd9Sstevel@tonic-gate if (Tflag) { 5097c478bd9Sstevel@tonic-gate if (density < MIN_MTB_DENSITY) 5107c478bd9Sstevel@tonic-gate density = MIN_MTB_DENSITY; 5117c478bd9Sstevel@tonic-gate fsize = bsize; 5127c478bd9Sstevel@tonic-gate cpg = -1; /* says make cyl groups as big as possible */ 5137c478bd9Sstevel@tonic-gate } else { 5147c478bd9Sstevel@tonic-gate if (fsize == 0) 5157c478bd9Sstevel@tonic-gate fsize = DESFRAGSIZE; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate if (!POWEROF2(fsize)) { 5197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5207c478bd9Sstevel@tonic-gate "newfs: fragment size must a power of 2, not %d\n"), fsize); 5217c478bd9Sstevel@tonic-gate fsize = bsize/8; 5227c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5237c478bd9Sstevel@tonic-gate "newfs: fragsize reset to %ld\n"), fsize); 5247c478bd9Sstevel@tonic-gate } 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * The file system is limited in size by the fragment size. 5287c478bd9Sstevel@tonic-gate * The number of fragments in the file system must fit into 5297c478bd9Sstevel@tonic-gate * a signed 32-bit quantity, so the number of sectors in the 5307c478bd9Sstevel@tonic-gate * file system is INT_MAX * the number of sectors in a frag. 5317c478bd9Sstevel@tonic-gate */ 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate max_possible_fssize = ((uint64_t)fsize)/DEV_BSIZE * INT_MAX; 5347c478bd9Sstevel@tonic-gate if (fssize > max_possible_fssize) 5357c478bd9Sstevel@tonic-gate fssize = max_possible_fssize; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate /* 5387c478bd9Sstevel@tonic-gate * Now fssize is the final size of the file system (in sectors). 5397c478bd9Sstevel@tonic-gate * If it's less than what the user requested, print a message. 5407c478bd9Sstevel@tonic-gate */ 5417c478bd9Sstevel@tonic-gate if (fssize < req_fssize) { 5427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5437c478bd9Sstevel@tonic-gate "newfs: requested size of %s disk blocks is too large.\n"), 5447c478bd9Sstevel@tonic-gate req_fssize_str); 5457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5467c478bd9Sstevel@tonic-gate "newfs: Resetting size to %lld\n"), fssize); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate /* 5507c478bd9Sstevel@tonic-gate * fssize now equals the size (in sectors) of the file system 5517c478bd9Sstevel@tonic-gate * that will be created. 5527c478bd9Sstevel@tonic-gate */ 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate /* XXX - following defaults are both here and in mkfs */ 5557c478bd9Sstevel@tonic-gate if (density <= 0) { 5567c478bd9Sstevel@tonic-gate if (fssize < GBSEC) 5577c478bd9Sstevel@tonic-gate density = MINDENSITY; 5587c478bd9Sstevel@tonic-gate else 5597c478bd9Sstevel@tonic-gate density = (int)((((longlong_t)fssize + (GBSEC - 1)) / 5607c478bd9Sstevel@tonic-gate GBSEC) * MINDENSITY); 5617c478bd9Sstevel@tonic-gate if (density <= 0) 5627c478bd9Sstevel@tonic-gate density = MINDENSITY; 5637c478bd9Sstevel@tonic-gate if (density > MAXDEFDENSITY) 5647c478bd9Sstevel@tonic-gate density = MAXDEFDENSITY; 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate if (cpg == 0) { 5677c478bd9Sstevel@tonic-gate /* 5687c478bd9Sstevel@tonic-gate * maxcpg calculation adapted from mkfs 5697c478bd9Sstevel@tonic-gate * In the case of disks with EFI labels, cpg has 5707c478bd9Sstevel@tonic-gate * already been set, so we won't enter this code. 5717c478bd9Sstevel@tonic-gate */ 5727c478bd9Sstevel@tonic-gate long maxcpg, maxipg; 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate maxipg = roundup(bsize * NBBY / 3, 5757c478bd9Sstevel@tonic-gate bsize / sizeof (struct inode)); 576*6451fdbcSvsakar 577*6451fdbcSvsakar nsect = (nsectors == -1) ? geom_nsectors : nsectors; 578*6451fdbcSvsakar 5797c478bd9Sstevel@tonic-gate maxcpg = (bsize - sizeof (struct cg) - howmany(maxipg, NBBY)) / 5807c478bd9Sstevel@tonic-gate (sizeof (long) + nrpos * sizeof (short) + 581*6451fdbcSvsakar nsect / (MAXFRAG * NBBY)); 5827c478bd9Sstevel@tonic-gate cpg = (fssize / GBSEC) * 32; 5837c478bd9Sstevel@tonic-gate if (cpg > maxcpg) 5847c478bd9Sstevel@tonic-gate cpg = maxcpg; 5857c478bd9Sstevel@tonic-gate if (cpg <= 0) 5867c478bd9Sstevel@tonic-gate cpg = MINCPG; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate if (minfree < 0) { 5897c478bd9Sstevel@tonic-gate minfree = ((float)MINFREESEC / fssize) * 100; 5907c478bd9Sstevel@tonic-gate if (minfree > 10) 5917c478bd9Sstevel@tonic-gate minfree = 10; 5927c478bd9Sstevel@tonic-gate if (minfree <= 0) 5937c478bd9Sstevel@tonic-gate minfree = 1; 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate #ifdef i386 /* Bug 1170182 */ 5967c478bd9Sstevel@tonic-gate if (ntracks > 32 && (ntracks % 16) != 0) { 5977c478bd9Sstevel@tonic-gate ntracks -= (ntracks % 16); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate #endif 6007c478bd9Sstevel@tonic-gate /* 6017c478bd9Sstevel@tonic-gate * Confirmation 6027c478bd9Sstevel@tonic-gate */ 6037c478bd9Sstevel@tonic-gate if (isatty(fileno(stdin)) && !Nflag) { 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * If we can read a valid superblock, report the mount 6067c478bd9Sstevel@tonic-gate * point on which this filesystem was last mounted. 6077c478bd9Sstevel@tonic-gate */ 6087c478bd9Sstevel@tonic-gate if (((sbp = read_sb(special)) != 0) && 6097c478bd9Sstevel@tonic-gate (*sbp->fs_fsmnt != '\0')) { 6107c478bd9Sstevel@tonic-gate (void) printf(gettext( 6117c478bd9Sstevel@tonic-gate "newfs: %s last mounted as %s\n"), 6127c478bd9Sstevel@tonic-gate special, sbp->fs_fsmnt); 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate (void) printf(gettext( 6157c478bd9Sstevel@tonic-gate "newfs: construct a new file system %s: (y/n)? "), 6167c478bd9Sstevel@tonic-gate special); 6177c478bd9Sstevel@tonic-gate (void) fflush(stdout); 6187c478bd9Sstevel@tonic-gate if (!yes()) 6197c478bd9Sstevel@tonic-gate exit(0); 6207c478bd9Sstevel@tonic-gate } 621*6451fdbcSvsakar dprintf(("DeBuG newfs : nsect=%d ntrak=%d cpg=%d\n", 622*6451fdbcSvsakar nsectors, ntracks, cpg)); 6237c478bd9Sstevel@tonic-gate /* 6247c478bd9Sstevel@tonic-gate * If alternates-per-cylinder is ever implemented: 6257c478bd9Sstevel@tonic-gate * need to get apc from dp->d_apc if no -a switch??? 6267c478bd9Sstevel@tonic-gate */ 6277c478bd9Sstevel@tonic-gate (void) sprintf(cmd, 628355d6bb5Sswilcox "mkfs -F ufs %s%s%s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s", 629355d6bb5Sswilcox Nflag ? "-o N " : "", binary_sb ? "-o calcbinsb " : "", 630355d6bb5Sswilcox text_sb ? "-o calcsb " : "", special, 6317c478bd9Sstevel@tonic-gate fssize, nsectors, ntracks, bsize, fsize, cpg, minfree, rpm/60, 6327c478bd9Sstevel@tonic-gate density, optim == FS_OPTSPACE ? "s" : "t", apc, rot, nrpos, 6337c478bd9Sstevel@tonic-gate maxcontig, Tflag ? "y" : "n"); 6347c478bd9Sstevel@tonic-gate if (verbose) { 6357c478bd9Sstevel@tonic-gate (void) printf("%s\n", cmd); 6367c478bd9Sstevel@tonic-gate (void) fflush(stdout); 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate exenv(); 6397c478bd9Sstevel@tonic-gate if (status = system(cmd)) 6407c478bd9Sstevel@tonic-gate exit(status >> 8); 6417c478bd9Sstevel@tonic-gate if (Nflag) 6427c478bd9Sstevel@tonic-gate exit(0); 6437c478bd9Sstevel@tonic-gate (void) sprintf(cmd, "/usr/sbin/fsirand %s", special); 6447c478bd9Sstevel@tonic-gate if (notrand(special) && (status = system(cmd)) != 0) 6457c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6467c478bd9Sstevel@tonic-gate gettext("%s: failed, status = %d\n"), 6477c478bd9Sstevel@tonic-gate cmd, status); 6487c478bd9Sstevel@tonic-gate return (0); 6497c478bd9Sstevel@tonic-gate } 6507c478bd9Sstevel@tonic-gate 6517c478bd9Sstevel@tonic-gate static void 6527c478bd9Sstevel@tonic-gate exenv(void) 6537c478bd9Sstevel@tonic-gate { 6547c478bd9Sstevel@tonic-gate char *epath; /* executable file path */ 6557c478bd9Sstevel@tonic-gate char *cpath; /* current path */ 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate if ((cpath = getenv("PATH")) == NULL) { 6587c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("newfs: no PATH in env\n")); 6597c478bd9Sstevel@tonic-gate /* 6607c478bd9Sstevel@tonic-gate * Background: the Bourne shell interpolates "." into 6617c478bd9Sstevel@tonic-gate * the path where said path starts with a colon, ends 6627c478bd9Sstevel@tonic-gate * with a colon, or has two adjacent colons. Thus, 6637c478bd9Sstevel@tonic-gate * the path ":/sbin::/usr/sbin:" is equivalent to 6647c478bd9Sstevel@tonic-gate * ".:/sbin:.:/usr/sbin:.". Now, we have no cpath, 6657c478bd9Sstevel@tonic-gate * and epath ends in a colon (to make for easy 6667c478bd9Sstevel@tonic-gate * catenation in the normal case). By the above, if 6677c478bd9Sstevel@tonic-gate * we use "", then "." becomes part of path. That's 6687c478bd9Sstevel@tonic-gate * bad, so use CPATH (which is just a duplicate of some 6697c478bd9Sstevel@tonic-gate * element in EPATH). No point in opening ourselves 6707c478bd9Sstevel@tonic-gate * up to a Trojan horse attack when we don't have to.... 6717c478bd9Sstevel@tonic-gate */ 6727c478bd9Sstevel@tonic-gate cpath = CPATH; 6737c478bd9Sstevel@tonic-gate } 6747c478bd9Sstevel@tonic-gate if ((epath = malloc(strlen(EPATH) + strlen(cpath) + 1)) == NULL) { 6757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("newfs: malloc failed\n")); 6767c478bd9Sstevel@tonic-gate exit(1); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate (void) strcpy(epath, EPATH); 6797c478bd9Sstevel@tonic-gate (void) strcat(epath, cpath); 6807c478bd9Sstevel@tonic-gate if (putenv(epath) < 0) { 6817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("newfs: putenv failed\n")); 6827c478bd9Sstevel@tonic-gate exit(1); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate static int 6877c478bd9Sstevel@tonic-gate yes(void) 6887c478bd9Sstevel@tonic-gate { 6897c478bd9Sstevel@tonic-gate int i, b; 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate i = b = getchar(); 6927c478bd9Sstevel@tonic-gate while (b != '\n' && b != '\0' && b != EOF) 6937c478bd9Sstevel@tonic-gate b = getchar(); 6947c478bd9Sstevel@tonic-gate return (i == 'y'); 6957c478bd9Sstevel@tonic-gate } 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate /* 6987c478bd9Sstevel@tonic-gate * xxx Caller must run fmt through gettext(3) for us, if we ever 6997c478bd9Sstevel@tonic-gate * xxx go the i18n route.... 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate static void 7027c478bd9Sstevel@tonic-gate fatal(char *fmt, ...) 7037c478bd9Sstevel@tonic-gate { 7047c478bd9Sstevel@tonic-gate va_list pvar; 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "newfs: "); 7077c478bd9Sstevel@tonic-gate va_start(pvar, fmt); 7087c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, pvar); 7097c478bd9Sstevel@tonic-gate va_end(pvar); 7107c478bd9Sstevel@tonic-gate (void) putc('\n', stderr); 7117c478bd9Sstevel@tonic-gate exit(10); 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate static diskaddr_t 7157c478bd9Sstevel@tonic-gate getdiskbydev(char *disk) 7167c478bd9Sstevel@tonic-gate { 7177c478bd9Sstevel@tonic-gate struct dk_geom g; 7187c478bd9Sstevel@tonic-gate struct dk_cinfo ci; 7197c478bd9Sstevel@tonic-gate diskaddr_t actual_size; 7207c478bd9Sstevel@tonic-gate int fd; 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if ((fd = open64(disk, 0)) < 0) { 7237c478bd9Sstevel@tonic-gate perror(disk); 7247c478bd9Sstevel@tonic-gate exit(1); 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate /* 7287c478bd9Sstevel@tonic-gate * get_device_size() determines the actual size of the 7297c478bd9Sstevel@tonic-gate * device, and also the disk's attributes, such as geometry. 7307c478bd9Sstevel@tonic-gate */ 7317c478bd9Sstevel@tonic-gate actual_size = get_device_size(fd, disk); 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate if (label_type == LABEL_TYPE_VTOC) { 7347c478bd9Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &g)) 7357c478bd9Sstevel@tonic-gate fatal(gettext( 7367c478bd9Sstevel@tonic-gate "%s: Unable to read Disk geometry"), disk); 737*6451fdbcSvsakar dprintf(("DeBuG newfs : geom=%ld, CHSLIMIT=%d\n", 738*6451fdbcSvsakar g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect, CHSLIMIT)); 739*6451fdbcSvsakar if (((g.dkg_ncyl * g.dkg_nhead * g.dkg_nsect) > CHSLIMIT) && 740*6451fdbcSvsakar !Tflag) { 741*6451fdbcSvsakar dprintf(("DeBuG newfs : geom > CHSLIMIT\n")); 742*6451fdbcSvsakar use_efi_dflts = 1; 743*6451fdbcSvsakar } 744*6451fdbcSvsakar /* 745*6451fdbcSvsakar * Though the nsector that is passed to mkfs is decided here 746*6451fdbcSvsakar * based on use_efi_dflts, we still need the geometry 747*6451fdbcSvsakar * based dkg_nsect for the cpg calculation later. 748*6451fdbcSvsakar */ 749*6451fdbcSvsakar geom_nsectors = g.dkg_nsect; 7507c478bd9Sstevel@tonic-gate if (nsectors == 0) 751*6451fdbcSvsakar nsectors = use_efi_dflts ? -1 : g.dkg_nsect; 7527c478bd9Sstevel@tonic-gate if (ntracks == 0) 753*6451fdbcSvsakar ntracks = use_efi_dflts ? -1 : g.dkg_nhead; 7547c478bd9Sstevel@tonic-gate if (rpm == 0) 7557c478bd9Sstevel@tonic-gate rpm = ((int)g.dkg_rpm <= 0) ? 3600: g.dkg_rpm; 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate if (bsize == 0) 7597c478bd9Sstevel@tonic-gate bsize = DESBLKSIZE; 7607c478bd9Sstevel@tonic-gate /* 7617c478bd9Sstevel@tonic-gate * Adjust maxcontig by the device's maxtransfer. If maxtransfer 7627c478bd9Sstevel@tonic-gate * information is not available, default to the min of a MB and 7637c478bd9Sstevel@tonic-gate * maxphys. 7647c478bd9Sstevel@tonic-gate */ 7657c478bd9Sstevel@tonic-gate if (maxcontig == -1 && ioctl(fd, DKIOCINFO, &ci) == 0) { 7667c478bd9Sstevel@tonic-gate maxcontig = ci.dki_maxtransfer * DEV_BSIZE; 7677c478bd9Sstevel@tonic-gate if (maxcontig < 0) { 7687c478bd9Sstevel@tonic-gate int error, gotit, maxphys; 7697c478bd9Sstevel@tonic-gate gotit = fsgetmaxphys(&maxphys, &error); 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * If we cannot get the maxphys value, default 7737c478bd9Sstevel@tonic-gate * to ufs_maxmaxphys (MB). 7747c478bd9Sstevel@tonic-gate */ 7757c478bd9Sstevel@tonic-gate if (gotit) { 7767c478bd9Sstevel@tonic-gate maxcontig = MIN(maxphys, MB); 7777c478bd9Sstevel@tonic-gate } else { 7787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 7797c478bd9Sstevel@tonic-gate "Warning: Could not get system value for maxphys. The value for maxcontig\n" 7807c478bd9Sstevel@tonic-gate "will default to 1MB.\n")); 7817c478bd9Sstevel@tonic-gate maxcontig = MB; 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate maxcontig /= bsize; 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate (void) close(fd); 7877c478bd9Sstevel@tonic-gate return (actual_size); 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* 7917c478bd9Sstevel@tonic-gate * Figure out how big the partition we're dealing with is. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate static diskaddr_t 7947c478bd9Sstevel@tonic-gate get_device_size(int fd, char *name) 7957c478bd9Sstevel@tonic-gate { 7967c478bd9Sstevel@tonic-gate struct vtoc vtoc; 7977c478bd9Sstevel@tonic-gate dk_gpt_t *efi_vtoc; 7987c478bd9Sstevel@tonic-gate diskaddr_t slicesize; 7997c478bd9Sstevel@tonic-gate 8007c478bd9Sstevel@tonic-gate int index = read_vtoc(fd, &vtoc); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate if (index >= 0) { 8037c478bd9Sstevel@tonic-gate label_type = LABEL_TYPE_VTOC; 8047c478bd9Sstevel@tonic-gate } else { 8057c478bd9Sstevel@tonic-gate if (index == VT_ENOTSUP || index == VT_ERROR) { 8067c478bd9Sstevel@tonic-gate /* it might be an EFI label */ 8077c478bd9Sstevel@tonic-gate index = efi_alloc_and_read(fd, &efi_vtoc); 8087c478bd9Sstevel@tonic-gate if (index >= 0) 8097c478bd9Sstevel@tonic-gate label_type = LABEL_TYPE_EFI; 8107c478bd9Sstevel@tonic-gate } 8117c478bd9Sstevel@tonic-gate } 8127c478bd9Sstevel@tonic-gate 8137c478bd9Sstevel@tonic-gate if (index < 0) { 8147c478bd9Sstevel@tonic-gate /* 8157c478bd9Sstevel@tonic-gate * Since both attempts to read the label failed, we're 8167c478bd9Sstevel@tonic-gate * going to fall back to a brute force approach to 8177c478bd9Sstevel@tonic-gate * determining the device's size: see how far out we can 8187c478bd9Sstevel@tonic-gate * perform reads on the device. 8197c478bd9Sstevel@tonic-gate */ 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate slicesize = brute_force_get_device_size(fd); 8227c478bd9Sstevel@tonic-gate if (slicesize == 0) { 8237c478bd9Sstevel@tonic-gate switch (index) { 8247c478bd9Sstevel@tonic-gate case VT_ERROR: 8257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 8267c478bd9Sstevel@tonic-gate "newfs: %s: %s\n"), name, strerror(errno)); 8277c478bd9Sstevel@tonic-gate exit(10); 8287c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 8297c478bd9Sstevel@tonic-gate case VT_EIO: 8307c478bd9Sstevel@tonic-gate fatal(gettext( 8317c478bd9Sstevel@tonic-gate "%s: I/O error accessing VTOC"), name); 8327c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 8337c478bd9Sstevel@tonic-gate case VT_EINVAL: 8347c478bd9Sstevel@tonic-gate fatal(gettext( 8357c478bd9Sstevel@tonic-gate "%s: Invalid field in VTOC"), name); 8367c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 8377c478bd9Sstevel@tonic-gate default: 8387c478bd9Sstevel@tonic-gate fatal(gettext( 8397c478bd9Sstevel@tonic-gate "%s: unknown error accessing VTOC"), 8407c478bd9Sstevel@tonic-gate name); 8417c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate } else { 8447c478bd9Sstevel@tonic-gate label_type = LABEL_TYPE_OTHER; 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate } 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate if (label_type == LABEL_TYPE_EFI) { 8497c478bd9Sstevel@tonic-gate slicesize = efi_vtoc->efi_parts[index].p_size; 8507c478bd9Sstevel@tonic-gate efi_free(efi_vtoc); 8517c478bd9Sstevel@tonic-gate } else if (label_type == LABEL_TYPE_VTOC) { 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * In the vtoc struct, p_size is a 32-bit signed quantity. 8547c478bd9Sstevel@tonic-gate * In the dk_gpt struct (efi's version of the vtoc), p_size 8557c478bd9Sstevel@tonic-gate * is an unsigned 64-bit quantity. By casting the vtoc's 8567c478bd9Sstevel@tonic-gate * psize to an unsigned 32-bit quantity, it will be copied 8577c478bd9Sstevel@tonic-gate * to 'slicesize' (an unsigned 64-bit diskaddr_t) without 8587c478bd9Sstevel@tonic-gate * sign extension. 8597c478bd9Sstevel@tonic-gate */ 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate slicesize = (uint32_t)vtoc.v_part[index].p_size; 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate return (slicesize); 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate /* 8687c478bd9Sstevel@tonic-gate * brute_force_get_device_size 8697c478bd9Sstevel@tonic-gate * 8707c478bd9Sstevel@tonic-gate * Determine the size of the device by seeing how far we can 8717c478bd9Sstevel@tonic-gate * read. Doing an llseek( , , SEEK_END) would probably work 8727c478bd9Sstevel@tonic-gate * in most cases, but we've seen at least one third-party driver 8737c478bd9Sstevel@tonic-gate * which doesn't correctly support the SEEK_END option when the 8747c478bd9Sstevel@tonic-gate * the device is greater than a terabyte. 8757c478bd9Sstevel@tonic-gate */ 8767c478bd9Sstevel@tonic-gate 8777c478bd9Sstevel@tonic-gate static diskaddr_t 8787c478bd9Sstevel@tonic-gate brute_force_get_device_size(int fd) 8797c478bd9Sstevel@tonic-gate { 8807c478bd9Sstevel@tonic-gate diskaddr_t min_fail = 0; 8817c478bd9Sstevel@tonic-gate diskaddr_t max_succeed = 0; 8827c478bd9Sstevel@tonic-gate diskaddr_t cur_db_off; 8837c478bd9Sstevel@tonic-gate char buf[DEV_BSIZE]; 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate /* 8867c478bd9Sstevel@tonic-gate * First, see if we can read the device at all, just to 8877c478bd9Sstevel@tonic-gate * eliminate errors that have nothing to do with the 8887c478bd9Sstevel@tonic-gate * device's size. 8897c478bd9Sstevel@tonic-gate */ 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate if (((llseek(fd, (offset_t)0, SEEK_SET)) == -1) || 8927c478bd9Sstevel@tonic-gate ((read(fd, buf, DEV_BSIZE)) == -1)) 8937c478bd9Sstevel@tonic-gate return (0); /* can't determine size */ 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * Now, go sequentially through the multiples of 4TB 8977c478bd9Sstevel@tonic-gate * to find the first read that fails (this isn't strictly 8987c478bd9Sstevel@tonic-gate * the most efficient way to find the actual size if the 8997c478bd9Sstevel@tonic-gate * size really could be anything between 0 and 2**64 bytes. 9007c478bd9Sstevel@tonic-gate * We expect the sizes to be less than 16 TB for some time, 9017c478bd9Sstevel@tonic-gate * so why do a bunch of reads that are larger than that? 9027c478bd9Sstevel@tonic-gate * However, this algorithm *will* work for sizes of greater 9037c478bd9Sstevel@tonic-gate * than 16 TB. We're just not optimizing for those sizes.) 9047c478bd9Sstevel@tonic-gate */ 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate for (cur_db_off = SECTORS_PER_TERABYTE * 4; 9077c478bd9Sstevel@tonic-gate min_fail == 0 && cur_db_off < FS_SIZE_UPPER_LIMIT; 9087c478bd9Sstevel@tonic-gate cur_db_off += 4 * SECTORS_PER_TERABYTE) { 9097c478bd9Sstevel@tonic-gate if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE), 9107c478bd9Sstevel@tonic-gate SEEK_SET)) == -1) || 9117c478bd9Sstevel@tonic-gate ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)) 9127c478bd9Sstevel@tonic-gate min_fail = cur_db_off; 9137c478bd9Sstevel@tonic-gate else 9147c478bd9Sstevel@tonic-gate max_succeed = cur_db_off; 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate if (min_fail == 0) 9187c478bd9Sstevel@tonic-gate return (0); 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate /* 9217c478bd9Sstevel@tonic-gate * We now know that the size of the device is less than 9227c478bd9Sstevel@tonic-gate * min_fail and greater than or equal to max_succeed. Now 9237c478bd9Sstevel@tonic-gate * keep splitting the difference until the actual size in 9247c478bd9Sstevel@tonic-gate * sectors in known. We also know that the difference 9257c478bd9Sstevel@tonic-gate * between max_succeed and min_fail at this time is 9267c478bd9Sstevel@tonic-gate * 4 * SECTORS_PER_TERABYTE, which is a power of two, which 9277c478bd9Sstevel@tonic-gate * simplifies the math below. 9287c478bd9Sstevel@tonic-gate */ 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate while (min_fail - max_succeed > 1) { 9317c478bd9Sstevel@tonic-gate cur_db_off = max_succeed + (min_fail - max_succeed)/2; 9327c478bd9Sstevel@tonic-gate if (((llseek(fd, (offset_t)(cur_db_off * DEV_BSIZE), 9337c478bd9Sstevel@tonic-gate SEEK_SET)) == -1) || 9347c478bd9Sstevel@tonic-gate ((read(fd, buf, DEV_BSIZE)) != DEV_BSIZE)) 9357c478bd9Sstevel@tonic-gate min_fail = cur_db_off; 9367c478bd9Sstevel@tonic-gate else 9377c478bd9Sstevel@tonic-gate max_succeed = cur_db_off; 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate /* the size is the last successfully read sector offset plus one */ 9417c478bd9Sstevel@tonic-gate return (max_succeed + 1); 9427c478bd9Sstevel@tonic-gate } 9437c478bd9Sstevel@tonic-gate 9447c478bd9Sstevel@tonic-gate /* 9457c478bd9Sstevel@tonic-gate * validate_size 9467c478bd9Sstevel@tonic-gate * 9477c478bd9Sstevel@tonic-gate * Return 1 if the device appears to be at least "size" sectors long. 9487c478bd9Sstevel@tonic-gate * Return 0 if it's shorter or we can't read it. 9497c478bd9Sstevel@tonic-gate */ 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate static int 9527c478bd9Sstevel@tonic-gate validate_size(char *disk, diskaddr_t size) 9537c478bd9Sstevel@tonic-gate { 9547c478bd9Sstevel@tonic-gate char buf[DEV_BSIZE]; 9557c478bd9Sstevel@tonic-gate int fd, rc; 9567c478bd9Sstevel@tonic-gate 9577c478bd9Sstevel@tonic-gate if ((fd = open64(disk, O_RDONLY)) < 0) { 9587c478bd9Sstevel@tonic-gate perror(disk); 9597c478bd9Sstevel@tonic-gate exit(1); 9607c478bd9Sstevel@tonic-gate } 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate if ((llseek(fd, (offset_t)((size - 1) * DEV_BSIZE), SEEK_SET) == -1) || 9637c478bd9Sstevel@tonic-gate (read(fd, buf, DEV_BSIZE)) != DEV_BSIZE) 9647c478bd9Sstevel@tonic-gate rc = 0; 9657c478bd9Sstevel@tonic-gate else 9667c478bd9Sstevel@tonic-gate rc = 1; 9677c478bd9Sstevel@tonic-gate (void) close(fd); 9687c478bd9Sstevel@tonic-gate return (rc); 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * read_sb(char * rawdev) - Attempt to read the superblock from a raw device 9737c478bd9Sstevel@tonic-gate * 9747c478bd9Sstevel@tonic-gate * Returns: 9757c478bd9Sstevel@tonic-gate * 0 : 9767c478bd9Sstevel@tonic-gate * Could not read a valid superblock for a variety of reasons. 9777c478bd9Sstevel@tonic-gate * Since 'newfs' handles any fatal conditions, we're not going 9787c478bd9Sstevel@tonic-gate * to make any guesses as to why this is failing or what should 9797c478bd9Sstevel@tonic-gate * be done about it. 9807c478bd9Sstevel@tonic-gate * 9817c478bd9Sstevel@tonic-gate * struct fs *: 9827c478bd9Sstevel@tonic-gate * A pointer to (what we think is) a valid superblock. The 9837c478bd9Sstevel@tonic-gate * space for the superblock is static (inside the function) 9847c478bd9Sstevel@tonic-gate * since we will only be reading the values from it. 9857c478bd9Sstevel@tonic-gate */ 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate struct fs * 9887c478bd9Sstevel@tonic-gate read_sb(char *fsdev) 9897c478bd9Sstevel@tonic-gate { 9907c478bd9Sstevel@tonic-gate static struct fs sblock; 9917c478bd9Sstevel@tonic-gate struct stat64 statb; 9927c478bd9Sstevel@tonic-gate int dskfd; 9937c478bd9Sstevel@tonic-gate char *bufp = NULL; 9947c478bd9Sstevel@tonic-gate int bufsz = 0; 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate if (stat64(fsdev, &statb) < 0) 9977c478bd9Sstevel@tonic-gate return (0); 9987c478bd9Sstevel@tonic-gate 9997c478bd9Sstevel@tonic-gate if ((dskfd = open64(fsdev, O_RDONLY)) < 0) 10007c478bd9Sstevel@tonic-gate return (0); 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate /* 10037c478bd9Sstevel@tonic-gate * We need a buffer whose size is a multiple of DEV_BSIZE in order 10047c478bd9Sstevel@tonic-gate * to read from a raw device (which we were probably passed). 10057c478bd9Sstevel@tonic-gate */ 10067c478bd9Sstevel@tonic-gate bufsz = ((sizeof (sblock) / DEV_BSIZE) + 1) * DEV_BSIZE; 10077c478bd9Sstevel@tonic-gate if ((bufp = malloc(bufsz)) == NULL) { 10087c478bd9Sstevel@tonic-gate (void) close(dskfd); 10097c478bd9Sstevel@tonic-gate return (0); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate if (llseek(dskfd, (offset_t)SBOFF, SEEK_SET) < 0 || 10137c478bd9Sstevel@tonic-gate read(dskfd, bufp, bufsz) < 0) { 10147c478bd9Sstevel@tonic-gate (void) close(dskfd); 10157c478bd9Sstevel@tonic-gate free(bufp); 10167c478bd9Sstevel@tonic-gate return (0); 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate (void) close(dskfd); /* Done with the file */ 10197c478bd9Sstevel@tonic-gate 10207c478bd9Sstevel@tonic-gate (void) memcpy(&sblock, bufp, sizeof (sblock)); 10217c478bd9Sstevel@tonic-gate free(bufp); /* Don't need this anymore */ 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate if (((sblock.fs_magic != FS_MAGIC) && 10247c478bd9Sstevel@tonic-gate (sblock.fs_magic != MTB_UFS_MAGIC)) || 10257c478bd9Sstevel@tonic-gate sblock.fs_ncg < 1 || sblock.fs_cpg < 1) 10267c478bd9Sstevel@tonic-gate return (0); 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl || 10297c478bd9Sstevel@tonic-gate (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) 10307c478bd9Sstevel@tonic-gate return (0); 10317c478bd9Sstevel@tonic-gate 10327c478bd9Sstevel@tonic-gate if (sblock.fs_sbsize < 0 || sblock.fs_sbsize > SBSIZE) 10337c478bd9Sstevel@tonic-gate return (0); 10347c478bd9Sstevel@tonic-gate 10357c478bd9Sstevel@tonic-gate return (&sblock); 10367c478bd9Sstevel@tonic-gate } 10377c478bd9Sstevel@tonic-gate 10387c478bd9Sstevel@tonic-gate /* 10397c478bd9Sstevel@tonic-gate * Read the UFS file system on the raw device SPECIAL. If it does not 10407c478bd9Sstevel@tonic-gate * appear to be a UFS file system, return non-zero, indicating that 10417c478bd9Sstevel@tonic-gate * fsirand should be called (and it will spit out an error message). 10427c478bd9Sstevel@tonic-gate * If it is a UFS file system, take a look at the inodes in the first 10437c478bd9Sstevel@tonic-gate * cylinder group. If they appear to be randomized (non-zero), return 10447c478bd9Sstevel@tonic-gate * zero, which will cause fsirand to not be called. If the inode generation 10457c478bd9Sstevel@tonic-gate * counts are all zero, then we must call fsirand, so return non-zero. 10467c478bd9Sstevel@tonic-gate */ 10477c478bd9Sstevel@tonic-gate 10487c478bd9Sstevel@tonic-gate #define RANDOMIZED 0 10497c478bd9Sstevel@tonic-gate #define NOT_RANDOMIZED 1 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate static int 10527c478bd9Sstevel@tonic-gate notrand(char *special) 10537c478bd9Sstevel@tonic-gate { 10547c478bd9Sstevel@tonic-gate long fsbuf[SBSIZE / sizeof (long)]; 10557c478bd9Sstevel@tonic-gate struct dinode dibuf[MAXBSIZE/sizeof (struct dinode)]; 10567c478bd9Sstevel@tonic-gate struct fs *fs; 10577c478bd9Sstevel@tonic-gate struct dinode *dip; 10587c478bd9Sstevel@tonic-gate offset_t seekaddr; 10597c478bd9Sstevel@tonic-gate int bno, inum; 10607c478bd9Sstevel@tonic-gate int fd; 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate fs = (struct fs *)fsbuf; 10637c478bd9Sstevel@tonic-gate if ((fd = open64(special, 0)) == -1) 10647c478bd9Sstevel@tonic-gate return (NOT_RANDOMIZED); 10657c478bd9Sstevel@tonic-gate if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1 || 10667c478bd9Sstevel@tonic-gate read(fd, (char *)fs, SBSIZE) != SBSIZE || 10677c478bd9Sstevel@tonic-gate ((fs->fs_magic != FS_MAGIC) && (fs->fs_magic != MTB_UFS_MAGIC))) { 10687c478bd9Sstevel@tonic-gate (void) close(fd); 10697c478bd9Sstevel@tonic-gate return (NOT_RANDOMIZED); 10707c478bd9Sstevel@tonic-gate } 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate /* looks like a UFS file system; read the first cylinder group */ 10737c478bd9Sstevel@tonic-gate bsize = INOPB(fs) * sizeof (struct dinode); 10747c478bd9Sstevel@tonic-gate inum = 0; 10757c478bd9Sstevel@tonic-gate while (inum < fs->fs_ipg) { 10767c478bd9Sstevel@tonic-gate bno = itod(fs, inum); 10777c478bd9Sstevel@tonic-gate seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE; 10787c478bd9Sstevel@tonic-gate if (llseek(fd, seekaddr, 0) == -1 || 10797c478bd9Sstevel@tonic-gate read(fd, (char *)dibuf, bsize) != bsize) { 10807c478bd9Sstevel@tonic-gate (void) close(fd); 10817c478bd9Sstevel@tonic-gate return (NOT_RANDOMIZED); 10827c478bd9Sstevel@tonic-gate } 10837c478bd9Sstevel@tonic-gate for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) { 10847c478bd9Sstevel@tonic-gate if (dip->di_gen != 0) { 10857c478bd9Sstevel@tonic-gate (void) close(fd); 10867c478bd9Sstevel@tonic-gate return (RANDOMIZED); 10877c478bd9Sstevel@tonic-gate } 10887c478bd9Sstevel@tonic-gate inum++; 10897c478bd9Sstevel@tonic-gate } 10907c478bd9Sstevel@tonic-gate } 10917c478bd9Sstevel@tonic-gate (void) close(fd); 10927c478bd9Sstevel@tonic-gate return (NOT_RANDOMIZED); 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate 10957c478bd9Sstevel@tonic-gate static void 10967c478bd9Sstevel@tonic-gate usage(void) 10977c478bd9Sstevel@tonic-gate { 10987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 10997c478bd9Sstevel@tonic-gate "usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n")); 11007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("where mkfs-options are:\n")); 11017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11027c478bd9Sstevel@tonic-gate "\t-N do not create file system, just print out parameters\n")); 11037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11047c478bd9Sstevel@tonic-gate "\t-T configure file system for eventual growth to over a terabyte\n")); 11057c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-s file system size (sectors)\n")); 11067c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-b block size\n")); 11077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-f frag size\n")); 11087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-t tracks/cylinder\n")); 11097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-c cylinders/group\n")); 11107c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-m minimum free space %%\n")); 11117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11127c478bd9Sstevel@tonic-gate "\t-o optimization preference (`space' or `time')\n")); 11137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-r revolutions/minute\n")); 11147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-i number of bytes per inode\n")); 11157c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11167c478bd9Sstevel@tonic-gate "\t-a number of alternates per cylinder\n")); 11177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-C maxcontig\n")); 11187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("\t-d rotational delay\n")); 11197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11207c478bd9Sstevel@tonic-gate "\t-n number of rotational positions\n")); 1121355d6bb5Sswilcox (void) fprintf(stderr, gettext( 1122355d6bb5Sswilcox "\t-S print a textual version of the calculated superblock to stdout\n")); 1123355d6bb5Sswilcox (void) fprintf(stderr, gettext( 1124355d6bb5Sswilcox "\t-B dump a binary version of the calculated superblock to stdout\n")); 11257c478bd9Sstevel@tonic-gate } 11267c478bd9Sstevel@tonic-gate 11277c478bd9Sstevel@tonic-gate /* 11287c478bd9Sstevel@tonic-gate * Error-detecting version of atoi(3). Adapted from mkfs' number(). 11297c478bd9Sstevel@tonic-gate */ 11307c478bd9Sstevel@tonic-gate static unsigned int 11317c478bd9Sstevel@tonic-gate number(char *param, char *value, int flags, int def_value) 11327c478bd9Sstevel@tonic-gate { 11337c478bd9Sstevel@tonic-gate char *cs; 11347c478bd9Sstevel@tonic-gate int n; 11357c478bd9Sstevel@tonic-gate int cut = INT_MAX / 10; /* limit to avoid overflow */ 11367c478bd9Sstevel@tonic-gate int minus = 0; 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate cs = value; 11397c478bd9Sstevel@tonic-gate if (*cs == '-') { 11407c478bd9Sstevel@tonic-gate minus = 1; 11417c478bd9Sstevel@tonic-gate cs += 1; 11427c478bd9Sstevel@tonic-gate } 11437c478bd9Sstevel@tonic-gate if ((*cs < '0') || (*cs > '9')) { 11447c478bd9Sstevel@tonic-gate goto bail_out; 11457c478bd9Sstevel@tonic-gate } 11467c478bd9Sstevel@tonic-gate n = 0; 11477c478bd9Sstevel@tonic-gate while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) { 11487c478bd9Sstevel@tonic-gate n = n*10 + *cs++ - '0'; 11497c478bd9Sstevel@tonic-gate } 11507c478bd9Sstevel@tonic-gate if (minus) 11517c478bd9Sstevel@tonic-gate n = -n; 11527c478bd9Sstevel@tonic-gate for (;;) { 11537c478bd9Sstevel@tonic-gate switch (*cs++) { 11547c478bd9Sstevel@tonic-gate case '\0': 11557c478bd9Sstevel@tonic-gate return (n); 11567c478bd9Sstevel@tonic-gate 11577c478bd9Sstevel@tonic-gate case '0': case '1': case '2': case '3': case '4': 11587c478bd9Sstevel@tonic-gate case '5': case '6': case '7': case '8': case '9': 11597c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 11607c478bd9Sstevel@tonic-gate "newfs: value for %s overflowed, using %d\n"), 11617c478bd9Sstevel@tonic-gate param, def_value); 11627c478bd9Sstevel@tonic-gate return (def_value); 11637c478bd9Sstevel@tonic-gate 11647c478bd9Sstevel@tonic-gate case '%': 11657c478bd9Sstevel@tonic-gate if (flags & NR_PERCENT) 11667c478bd9Sstevel@tonic-gate break; 11677c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 11687c478bd9Sstevel@tonic-gate 11697c478bd9Sstevel@tonic-gate default: 11707c478bd9Sstevel@tonic-gate bail_out: 11717c478bd9Sstevel@tonic-gate fatal(gettext("bad numeric arg for %s: \"%s\""), 11727c478bd9Sstevel@tonic-gate param, value); 11737c478bd9Sstevel@tonic-gate 11747c478bd9Sstevel@tonic-gate } 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate /* NOTREACHED */ 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate /* 11807c478bd9Sstevel@tonic-gate * Error-detecting version of atoi(3). Adapted from mkfs' number(). 11817c478bd9Sstevel@tonic-gate */ 11827c478bd9Sstevel@tonic-gate static int64_t 11837c478bd9Sstevel@tonic-gate number64(char *param, char *value, int flags, int64_t def_value) 11847c478bd9Sstevel@tonic-gate { 11857c478bd9Sstevel@tonic-gate char *cs; 11867c478bd9Sstevel@tonic-gate int64_t n; 11877c478bd9Sstevel@tonic-gate int64_t cut = FS_SIZE_UPPER_LIMIT/ 10; /* limit to avoid overflow */ 11887c478bd9Sstevel@tonic-gate int minus = 0; 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate cs = value; 11917c478bd9Sstevel@tonic-gate if (*cs == '-') { 11927c478bd9Sstevel@tonic-gate minus = 1; 11937c478bd9Sstevel@tonic-gate cs += 1; 11947c478bd9Sstevel@tonic-gate } 11957c478bd9Sstevel@tonic-gate if ((*cs < '0') || (*cs > '9')) { 11967c478bd9Sstevel@tonic-gate goto bail_out; 11977c478bd9Sstevel@tonic-gate } 11987c478bd9Sstevel@tonic-gate n = 0; 11997c478bd9Sstevel@tonic-gate while ((*cs >= '0') && (*cs <= '9') && (n <= cut)) { 12007c478bd9Sstevel@tonic-gate n = n*10 + *cs++ - '0'; 12017c478bd9Sstevel@tonic-gate } 12027c478bd9Sstevel@tonic-gate if (minus) 12037c478bd9Sstevel@tonic-gate n = -n; 12047c478bd9Sstevel@tonic-gate for (;;) { 12057c478bd9Sstevel@tonic-gate switch (*cs++) { 12067c478bd9Sstevel@tonic-gate case '\0': 12077c478bd9Sstevel@tonic-gate return (n); 12087c478bd9Sstevel@tonic-gate 12097c478bd9Sstevel@tonic-gate case '0': case '1': case '2': case '3': case '4': 12107c478bd9Sstevel@tonic-gate case '5': case '6': case '7': case '8': case '9': 12117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 12127c478bd9Sstevel@tonic-gate "newfs: value for %s overflowed, using %d\n"), 12137c478bd9Sstevel@tonic-gate param, def_value); 12147c478bd9Sstevel@tonic-gate return (def_value); 12157c478bd9Sstevel@tonic-gate 12167c478bd9Sstevel@tonic-gate case '%': 12177c478bd9Sstevel@tonic-gate if (flags & NR_PERCENT) 12187c478bd9Sstevel@tonic-gate break; 12197c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate default: 12227c478bd9Sstevel@tonic-gate bail_out: 12237c478bd9Sstevel@tonic-gate fatal(gettext("bad numeric arg for %s: \"%s\""), 12247c478bd9Sstevel@tonic-gate param, value); 12257c478bd9Sstevel@tonic-gate 12267c478bd9Sstevel@tonic-gate } 12277c478bd9Sstevel@tonic-gate } 12287c478bd9Sstevel@tonic-gate /* NOTREACHED */ 12297c478bd9Sstevel@tonic-gate } 1230