/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* newfs: friendly front end to mkfs
*
*/
#include <locale.h>
#include <sys/sysmacros.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <libintl.h>
#include <sys/efi_partition.h>
#include <fslib.h>
static unsigned int number(char *, char *, int, int);
static diskaddr_t getdiskbydev(char *);
static int yes(void);
static int notrand(char *);
static void usage();
static diskaddr_t get_device_size(int, char *);
static diskaddr_t brute_force_get_device_size(int);
static void exenv(void);
/*PRINTFLIKE1*/
/*
* The following constant specifies an upper limit for file system size
* that is actually a lot bigger than we expect to support with UFS. (Since
* it's specified in sectors, the file system size would be 2**44 * 512,
* which is 2**53, which is 8192 Terabytes.) However, it's useful
* for checking the basic sanity of a size value that is input on the
* command line.
*/
/* For use with number() */
#define NR_NONE 0
/*
* The following two constants set the default block and fragment sizes.
* Both constants must be a power of 2 and meet the following constraints:
* MINBSIZE <= DESBLKSIZE <= MAXBSIZE
* DEV_BSIZE <= DESFRAGSIZE <= DESBLKSIZE
* DESBLKSIZE / DESFRAGSIZE <= 8
*/
#ifdef DEBUG
#else
#define dprintf(x)
#endif
/* 8 is the historical default */
/*
* The variable use_efi_dflts is an indicator of whether to use EFI logic
* or the geometry logic in laying out the filesystem. This is decided
* disks and removable media.
*/
static int use_efi_dflts = 0;
static int isremovable = 0;
static int ishotpluggable = 0;
extern char *getfullrawname(); /* from libadm */
int
{
int status;
int option;
#if !defined(TEXT_DOMAIN)
#endif
(void) textdomain(TEXT_DOMAIN);
opterr = 0; /* We print our own errors, disable getopt's message */
"vNBSs:C:d:t:o:a:b:f:c:m:n:r:i:T")) != EOF) {
switch (option) {
case 'S':
text_sb++;
break;
case 'B':
binary_sb++;
break;
case 'v':
verbose++;
break;
case 'N':
Nflag++;
break;
case 's':
/*
* The maximum file system size is a lot smaller
* than FS_SIZE_UPPER_LIMIT, but until we find out
* the device size and block size, we don't know
* what it is. So save the requested size in a
* string so that we can print it out later if we
* determine it's too big.
*/
if (req_fssize < 1024)
"%s: fssize must be at least 1024"),
optarg);
if (req_fssize_str == NULL)
"Insufficient memory for string copy."));
break;
case 'C':
if (maxcontig < 0)
break;
case 'd':
rot_set = 1;
"%s: bad rotational delay"), optarg);
break;
case 't':
ntracks_set = 1;
if ((ntracks < 0) ||
break;
case 'o':
optim = FS_OPTSPACE;
optim = FS_OPTTIME;
else
"%s: bad optimization preference (options are `space' or `time')"), optarg);
break;
case 'a':
apc_set = 1;
"%s: bad alternates per cyl"), optarg);
break;
case 'b':
"%s: bad block size"), optarg);
break;
case 'f':
fsize_flag++;
/* xxx ought to test against bsize for upper limit */
break;
case 'c':
cpg_set = 1;
if (cpg < 1)
optarg);
break;
case 'm':
break;
case 'n':
nrpos_set = 1;
if (nrpos <= 0)
"%s: bad number of rotational positions"),
optarg);
break;
case 'r':
rpm_set = 1;
if (rpm < 0)
break;
case 'i':
/* xxx ought to test against fsize */
optarg);
break;
case 'T':
Tflag++;
break;
default:
usage();
}
}
/* At this point, there should only be one argument left: */
/* The raw-special-device itself. If not, print usage message. */
usage();
exit(1);
}
exit(1);
}
if (*special == '\0') {
gettext("newfs: %s: %s\n"),
exit(2);
}
}
gettext("newfs: malloc failed\n"));
exit(1);
}
if (*special == '\0') {
name);
gettext("newfs: malloc failed\n"));
exit(1);
}
if (*special == '\0')
"%s: not a raw disk device"), name);
}
}
/*
* getdiskbydev() determines the characteristics of the special
* device on which the file system will be built. In the case
* of devices with SMI labels (that is, non-EFI labels), the
* following characteristics are set (if they were not already
* set on the command line, since the command line settings
* take precedence):
*
* nsectors - sectors per track
* ntracks - tracks per cylinder
* rpm - disk revolutions per minute
*
* apc is NOT set
*
* getdiskbydev() also sets the following quantities for all
* devices, if not already set:
*
* bsize - file system block size
* maxcontig
* label_type (efi, vtoc, or other)
*
* getdiskbydev() returns the actual size of the device, in
* sectors.
*/
if (req_fssize == 0) {
} else {
/*
* If the user specified a size larger than what we've
* determined as the actual size of the device, see if the
* size specified by the user can be read. If so, use it,
* since some devices and volume managers may not support
* the vtoc and EFI interfaces we use to determine device
* size.
*/
if (req_fssize > actual_fssize &&
"Warning: the requested size of this file system\n"
"(%lld sectors) is greater than the size of the\n"
"device reported by the driver (%lld sectors).\n"
"However, a read of the device at the requested size\n"
"does succeed, so the requested size will be used.\n"),
fssize = req_fssize;
} else {
}
}
if (label_type == LABEL_TYPE_VTOC) {
if (nsectors < 0)
special);
if (!use_efi_dflts) {
if (ntracks < 0)
special);
}
if (rpm < 0)
"%s: no default revolutions/minute value"),
special);
if (rpm < 60) {
gettext("Warning: setting rpm to 60\n"));
rpm = 60;
}
}
if (ntracks_set)
"Warning: ntracks is obsolete for this device and will be ignored.\n"));
if (cpg_set)
if (rpm_set)
"Warning: rpm is obsolete for this device and will be ignored.\n"));
if (rot_set)
"Warning: rotational delay is obsolete for this device and"
" will be ignored.\n"));
if (nrpos_set)
"Warning: number of rotational positions is obsolete for this device and\n"
"will be ignored.\n"));
if (apc_set)
"Warning: number of alternate sectors per cylinder is obsolete for this\n"
"device and will be ignored.\n"));
/*
* We need these for the call to mkfs, even though they are
* meaningless.
*/
rpm = 60;
nrpos = 1;
apc = 0;
rot = -1;
/*
* These values are set to produce a file system with
* a cylinder group size of 48MB. For disks with
* non-EFI labels, most geometries result in cylinder
* groups of around 40 - 50 MB, so we arbitrarily choose
* 48MB for disks with EFI labels. mkfs will reduce
* cylinders per group even further if necessary.
*/
cpg = 16;
nsectors = 128;
ntracks = 48;
/*
* mkfs produces peculiar results for file systems
* that are smaller than one cylinder so don't allow
* them to be created (this check is only made for
* disks with EFI labels. Eventually, it should probably
* be enforced for all disks.)
*/
"file system size must be at least %d sectors"),
}
}
Tflag = 1;
/*
* If the user requested that the file system be set up for
* eventual growth to over a terabyte, or if it's already greater
* than a terabyte, set the inode density (nbpi) to MIN_MTB_DENSITY
* (unless the user has specified a larger nbpi), set the frag size
* equal to the block size, and set the cylinders-per-group value
* passed to mkfs to -1, which tells mkfs to make cylinder groups
* as large as possible.
*/
if (Tflag) {
if (density < MIN_MTB_DENSITY)
} else {
if (fsize == 0)
fsize = DESFRAGSIZE;
}
"newfs: fragment size must a power of 2, not %d\n"), fsize);
"newfs: fragsize reset to %ld\n"), fsize);
}
/*
* The file system is limited in size by the fragment size.
* The number of fragments in the file system must fit into
* a signed 32-bit quantity, so the number of sectors in the
* file system is INT_MAX * the number of sectors in a frag.
*/
if (fssize > max_possible_fssize)
/*
* Now fssize is the final size of the file system (in sectors).
* If it's less than what the user requested, print a message.
*/
if (fssize < req_fssize) {
"newfs: requested size of %s disk blocks is too large.\n"),
"newfs: Resetting size to %lld\n"), fssize);
}
/*
* fssize now equals the size (in sectors) of the file system
* that will be created.
*/
/* XXX - following defaults are both here and in mkfs */
if (density <= 0) {
else
GBSEC) * MINDENSITY);
if (density <= 0)
if (density > MAXDEFDENSITY)
}
if (cpg == 0) {
/*
* maxcpg calculation adapted from mkfs
* In the case of disks with EFI labels, cpg has
* already been set, so we won't enter this code.
*/
(sizeof (long) + nrpos * sizeof (short) +
if (cpg <= 0)
}
if (minfree < 0) {
if (minfree > 10)
minfree = 10;
if (minfree <= 0)
minfree = 1;
}
#ifdef i386 /* Bug 1170182 */
}
#endif
/*
* Confirmation
*/
/*
* If we can read a valid superblock, report the mount
* point on which this filesystem was last mounted.
*/
"newfs: %s last mounted as %s\n"),
}
"newfs: construct a new file system %s: (y/n)? "),
special);
if (!yes())
exit(0);
}
dprintf(("DeBuG newfs : nsect=%d ntrak=%d cpg=%d\n",
/*
* If alternates-per-cylinder is ever implemented:
* need to get apc from dp->d_apc if no -a switch???
*/
"%s%s%s%s %lld %d %d %d %d %d %d %d %d %s %d %d %d %d %s",
if (verbose) {
}
exenv();
if (Nflag)
exit(0);
gettext("%s: failed, status = %d\n"),
return (0);
}
static void
exenv(void)
{
/*
* Background: the Bourne shell interpolates "." into
* the path where said path starts with a colon, ends
* with a colon, or has two adjacent colons. Thus,
* and epath ends in a colon (to make for easy
* catenation in the normal case). By the above, if
* we use "", then "." becomes part of path. That's
* bad, so use CPATH (which is just a duplicate of some
* element in EPATH). No point in opening ourselves
* up to a Trojan horse attack when we don't have to....
*/
}
exit(1);
}
exit(1);
}
}
static int
yes(void)
{
int i, b;
i = b = getchar();
b = getchar();
return (i == 'y');
}
/*
* xxx Caller must run fmt through gettext(3) for us, if we ever
* xxx go the i18n route....
*/
static void
{
exit(10);
}
static diskaddr_t
{
struct dk_geom g;
int fd;
exit(1);
}
/*
* get_device_size() determines the actual size of the
* device, and also the disk's attributes, such as geometry.
*/
if (label_type == LABEL_TYPE_VTOC) {
/*
* Geometry information does not make sense for removable or
* hotpluggable media anyway, so indicate mkfs to use EFI
* default parameters.
*/
dprintf(("DeBuG newfs : Unable to determine if %s is"
" Removable Media. Proceeding with system"
" determined parameters.\n", disk));
isremovable = 0;
}
/* If removable check if a floppy disk */
if (isremovable) {
dprintf(("DeBuG newfs : Unable to get media"
" info from %s.\n", disk));
} else {
isremovable = 0;
}
}
}
dprintf(("DeBuG newfs : Unable to determine if %s is"
" Hotpluggable Media. Proceeding with system"
" determined parameters.\n", disk));
ishotpluggable = 0;
}
use_efi_dflts = 1;
"%s: Unable to read Disk geometry"), disk);
use_efi_dflts = 1;
}
dprintf(("DeBuG newfs : geom=%llu, CHSLIMIT=%d "
"isremovable = %d ishotpluggable = %d use_efi_dflts = %d\n",
/*
* The ntracks that is passed to mkfs is decided here based
* on 'use_efi_dflts' and whether ntracks was specified as a
* command line parameter to newfs.
* If ntracks of -1 is passed to mkfs, mkfs uses DEF_TRACKS_EFI
* and DEF_SECTORS_EFI for ntracks and nsectors respectively.
*/
if (nsectors == 0)
if (ntracks == 0)
if (rpm == 0)
}
if (bsize == 0)
bsize = DESBLKSIZE;
/*
* Adjust maxcontig by the device's maxtransfer. If maxtransfer
* information is not available, default to the min of a MB and
* maxphys.
*/
if (maxcontig < 0) {
/*
* If we cannot get the maxphys value, default
* to ufs_maxmaxphys (MB).
*/
if (gotit) {
} else {
"Warning: Could not get system value for maxphys. The value for maxcontig\n"
"will default to 1MB.\n"));
}
}
}
return (actual_size);
}
/*
* Figure out how big the partition we're dealing with is.
*/
static diskaddr_t
{
if (index >= 0) {
} else {
/* it might be an EFI label */
if (index >= 0)
}
}
if (index < 0) {
/*
* Since both attempts to read the label failed, we're
* going to fall back to a brute force approach to
* determining the device's size: see how far out we can
* perform reads on the device.
*/
if (slicesize == 0) {
switch (index) {
case VT_ERROR:
exit(10);
/*NOTREACHED*/
case VT_EIO:
"%s: I/O error accessing VTOC"), name);
/*NOTREACHED*/
case VT_EINVAL:
"%s: Invalid field in VTOC"), name);
/*NOTREACHED*/
default:
"%s: unknown error accessing VTOC"),
name);
/*NOTREACHED*/
}
} else {
}
}
if (label_type == LABEL_TYPE_EFI) {
} else if (label_type == LABEL_TYPE_VTOC) {
}
return (slicesize);
}
/*
* brute_force_get_device_size
*
* Determine the size of the device by seeing how far we can
* read. Doing an llseek( , , SEEK_END) would probably work
* in most cases, but we've seen at least one third-party driver
* which doesn't correctly support the SEEK_END option when the
* the device is greater than a terabyte.
*/
static diskaddr_t
{
/*
* First, see if we can read the device at all, just to
* eliminate errors that have nothing to do with the
* device's size.
*/
return (0); /* can't determine size */
/*
* Now, go sequentially through the multiples of 4TB
* to find the first read that fails (this isn't strictly
* the most efficient way to find the actual size if the
* size really could be anything between 0 and 2**64 bytes.
* We expect the sizes to be less than 16 TB for some time,
* so why do a bunch of reads that are larger than that?
* However, this algorithm *will* work for sizes of greater
* than 16 TB. We're just not optimizing for those sizes.)
*/
SEEK_SET)) == -1) ||
else
}
if (min_fail == 0)
return (0);
/*
* We now know that the size of the device is less than
* min_fail and greater than or equal to max_succeed. Now
* keep splitting the difference until the actual size in
* sectors in known. We also know that the difference
* between max_succeed and min_fail at this time is
* 4 * SECTORS_PER_TERABYTE, which is a power of two, which
* simplifies the math below.
*/
SEEK_SET)) == -1) ||
else
}
/* the size is the last successfully read sector offset plus one */
return (max_succeed + 1);
}
/*
* validate_size
*
* Return 1 if the device appears to be at least "size" sectors long.
* Return 0 if it's shorter or we can't read it.
*/
static int
{
exit(1);
}
rc = 0;
else
rc = 1;
return (rc);
}
/*
* read_sb(char * rawdev) - Attempt to read the superblock from a raw device
*
* Returns:
* 0 :
* Could not read a valid superblock for a variety of reasons.
* Since 'newfs' handles any fatal conditions, we're not going
* to make any guesses as to why this is failing or what should
* be done about it.
*
* struct fs *:
* A pointer to (what we think is) a valid superblock. The
* space for the superblock is static (inside the function)
* since we will only be reading the values from it.
*/
struct fs *
{
int dskfd;
int bufsz = 0;
return (0);
return (0);
/*
* We need a buffer whose size is a multiple of DEV_BSIZE in order
* to read from a raw device (which we were probably passed).
*/
return (0);
}
return (0);
}
return (0);
return (0);
return (0);
return (&sblock);
}
/*
* Read the UFS file system on the raw device SPECIAL. If it does not
* appear to be a UFS file system, return non-zero, indicating that
* fsirand should be called (and it will spit out an error message).
* If it is a UFS file system, take a look at the inodes in the first
* cylinder group. If they appear to be randomized (non-zero), return
* zero, which will cause fsirand to not be called. If the inode generation
* counts are all zero, then we must call fsirand, so return non-zero.
*/
#define RANDOMIZED 0
static int
{
int fd;
return (NOT_RANDOMIZED);
return (NOT_RANDOMIZED);
}
/* looks like a UFS file system; read the first cylinder group */
inum = 0;
return (NOT_RANDOMIZED);
}
return (RANDOMIZED);
}
inum++;
}
}
return (NOT_RANDOMIZED);
}
static void
usage(void)
{
"usage: newfs [ -v ] [ mkfs-options ] raw-special-device\n"));
"\t-N do not create file system, just print out parameters\n"));
"\t-T configure file system for eventual growth to over a terabyte\n"));
"\t-o optimization preference (`space' or `time')\n"));
"\t-a number of alternates per cylinder\n"));
"\t-n number of rotational positions\n"));
"\t-S print a textual version of the calculated superblock to stdout\n"));
"\t-B dump a binary version of the calculated superblock to stdout\n"));
}
/*
* Error-detecting version of atoi(3). Adapted from mkfs' number().
*/
static unsigned int
{
char *cs;
int n;
int minus = 0;
if (*cs == '-') {
minus = 1;
cs += 1;
}
goto bail_out;
}
n = 0;
}
if (minus)
n = -n;
for (;;) {
switch (*cs++) {
case '\0':
return (n);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
"newfs: value for %s overflowed, using %d\n"),
return (def_value);
case '%':
if (flags & NR_PERCENT)
break;
/* FALLTHROUGH */
default:
}
}
/* NOTREACHED */
}
/*
* Error-detecting version of atoi(3). Adapted from mkfs' number().
*/
static int64_t
{
char *cs;
int64_t n;
int minus = 0;
if (*cs == '-') {
minus = 1;
cs += 1;
}
goto bail_out;
}
n = 0;
}
if (minus)
n = -n;
for (;;) {
switch (*cs++) {
case '\0':
return (n);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
"newfs: value for %s overflowed, using %d\n"),
return (def_value);
case '%':
if (flags & NR_PERCENT)
break;
/* FALLTHROUGH */
default:
}
}
/* NOTREACHED */
}