newfs.c revision 204919
11558Srgrimes/*
298542Smckusick * Copyright (c) 2002 Networks Associates Technology, Inc.
398542Smckusick * All rights reserved.
498542Smckusick *
598542Smckusick * This software was developed for the FreeBSD Project by Marshall
698542Smckusick * Kirk McKusick and Network Associates Laboratories, the Security
798542Smckusick * Research Division of Network Associates, Inc. under DARPA/SPAWAR
898542Smckusick * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9110884Smckusick * research program.
1098542Smckusick *
111558Srgrimes * Copyright (c) 1983, 1989, 1993, 1994
121558Srgrimes *	The Regents of the University of California.  All rights reserved.
131558Srgrimes *
141558Srgrimes * Redistribution and use in source and binary forms, with or without
151558Srgrimes * modification, are permitted provided that the following conditions
161558Srgrimes * are met:
171558Srgrimes * 1. Redistributions of source code must retain the above copyright
181558Srgrimes *    notice, this list of conditions and the following disclaimer.
191558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
201558Srgrimes *    notice, this list of conditions and the following disclaimer in the
211558Srgrimes *    documentation and/or other materials provided with the distribution.
221558Srgrimes * 4. Neither the name of the University nor the names of its contributors
231558Srgrimes *    may be used to endorse or promote products derived from this software
241558Srgrimes *    without specific prior written permission.
251558Srgrimes *
261558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361558Srgrimes * SUCH DAMAGE.
371558Srgrimes */
381558Srgrimes
39114589Sobrien#if 0
401558Srgrimes#ifndef lint
4137664Scharnierstatic const char copyright[] =
4237664Scharnier"@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
4337664Scharnier	The Regents of the University of California.  All rights reserved.\n";
441558Srgrimes#endif /* not lint */
451558Srgrimes
461558Srgrimes#ifndef lint
4737664Scharnierstatic char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
48114589Sobrien#endif /* not lint */
4937664Scharnier#endif
50114589Sobrien#include <sys/cdefs.h>
51114589Sobrien__FBSDID("$FreeBSD: head/sbin/newfs/newfs.c 204919 2010-03-09 19:31:08Z sobomax $");
521558Srgrimes
531558Srgrimes/*
541558Srgrimes * newfs: friendly front end to mkfs
551558Srgrimes */
561558Srgrimes#include <sys/param.h>
571558Srgrimes#include <sys/stat.h>
5895357Sphk#include <sys/disk.h>
591558Srgrimes#include <sys/disklabel.h>
601558Srgrimes#include <sys/file.h>
611558Srgrimes#include <sys/mount.h>
621558Srgrimes
6337707Scharnier#include <ufs/ufs/dir.h>
6437707Scharnier#include <ufs/ufs/dinode.h>
651558Srgrimes#include <ufs/ffs/fs.h>
6623682Speter#include <ufs/ufs/ufsmount.h>
671558Srgrimes
681558Srgrimes#include <ctype.h>
6937664Scharnier#include <err.h>
701558Srgrimes#include <errno.h>
71135460Spjd#include <inttypes.h>
721558Srgrimes#include <paths.h>
7393777Sbde#include <stdarg.h>
741558Srgrimes#include <stdio.h>
751558Srgrimes#include <stdlib.h>
761558Srgrimes#include <string.h>
771558Srgrimes#include <syslog.h>
781558Srgrimes#include <unistd.h>
791558Srgrimes
80204654Ssobomax#include <libutil.h>
81204654Ssobomax
8292717Sphk#include "newfs.h"
831558Srgrimes
84174675Sphkint	Eflag;			/* Erase previous disk contents */
85110174Sgordonint	Lflag;			/* add a volume label */
86102231Strhodesint	Nflag;			/* run without writing file system */
87113751Srwatsonint	Oflag = 2;		/* file system format (1 => UFS1, 2 => UFS2) */
8892722Sphkint	Rflag;			/* regression test */
89102231Strhodesint	Uflag;			/* enable soft updates for file system */
90174675Sphkint	Xflag = 0;		/* exit in middle of newfs for testing */
91163842Spjdint	Jflag;			/* enable gjournal for file system */
92126254Srwatsonint	lflag;			/* enable multilabel for file system */
93140603Swesint	nflag;			/* do not create .snap directory */
94174011Syarintmax_t fssize;		/* file system size */
95204909Ssobomaxint	sectorsize;		/* bytes/sector */
961558Srgrimesint	realsectorsize;		/* bytes/sector in hardware */
97204919Ssobomaxint	fsize = 0;		/* fragment size */
98204919Ssobomaxint	bsize = 0;		/* block size */
99204919Ssobomaxint	maxbsize = 0;		/* maximum clustering */
100204919Ssobomaxint	maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
1011558Srgrimesint	minfree = MINFREE;	/* free space threshold */
1021558Srgrimesint	opt = DEFAULTOPT;	/* optimization preference (space or time) */
103204919Ssobomaxint	density;		/* number of bytes per inode */
104204919Ssobomaxint	maxcontig = 0;		/* max contiguous blocks to allocate */
105204919Ssobomaxint	maxbpg;			/* maximum blocks per file in a cyl group */
106204919Ssobomaxint	avgfilesize = AVFILESIZ;/* expected average file size */
107204919Ssobomaxint	avgfilesperdir = AFPDIR;/* expected number of files per directory */
108110174Sgordonu_char	*volumelabel = NULL;	/* volume label for filesystem */
109110671Sjmallettstruct uufsd disk;		/* libufs disk structure */
1101558Srgrimes
11192763Sphkstatic char	device[MAXPATHLEN];
112185588Sluigistatic u_char   bootarea[BBSIZE];
113185588Sluigistatic int	is_file;		/* work on a file, not a device */
114185588Sluigistatic char	*dkname;
11593777Sbdestatic char	*disktype;
11693777Sbdestatic int	unlabeled;
1171558Srgrimes
118174011Syarstatic void getfssize(intmax_t *, const char *p, intmax_t, intmax_t);
11995357Sphkstatic struct disklabel *getdisklabel(char *s);
12095357Sphkstatic void rewritelabel(char *s, struct disklabel *lp);
12193777Sbdestatic void usage(void);
122204909Ssobomaxstatic int expand_number_int(const char *buf, int *num);
12337664Scharnier
124185588Sluigiufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */
125185588Sluigi
1261558Srgrimesint
12792711Siedowsemain(int argc, char *argv[])
1281558Srgrimes{
12992483Sphk	struct partition *pp;
13092483Sphk	struct disklabel *lp;
1311558Srgrimes	struct partition oldpartition;
1321558Srgrimes	struct stat st;
13395357Sphk	char *cp, *special;
134174011Syar	intmax_t reserved;
135204615Ssobomax	int ch, i, rval;
13695357Sphk	off_t mediasize;
137185588Sluigi	char part_name;		/* partition name, default to full disk */
1381558Srgrimes
139185588Sluigi	part_name = 'c';
140174011Syar	reserved = 0;
14192717Sphk	while ((ch = getopt(argc, argv,
142174675Sphk	    "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:lm:no:r:s:")) != -1)
1431558Srgrimes		switch (ch) {
144122785Swes		case 'E':
145174675Sphk			Eflag = 1;
146122785Swes			break;
147163842Spjd		case 'J':
148163842Spjd			Jflag = 1;
149163842Spjd			break;
150110174Sgordon		case 'L':
151110174Sgordon			volumelabel = optarg;
152110174Sgordon			i = -1;
153110174Sgordon			while (isalnum(volumelabel[++i]));
154110174Sgordon			if (volumelabel[i] != '\0') {
155110174Sgordon				errx(1, "bad volume label. Valid characters are alphanumerics.");
156110174Sgordon			}
157110174Sgordon			if (strlen(volumelabel) >= MAXVOLLEN) {
158110174Sgordon				errx(1, "bad volume label. Length is longer than %d.",
159110174Sgordon				    MAXVOLLEN);
160110174Sgordon			}
161110174Sgordon			Lflag = 1;
162110174Sgordon			break;
1631558Srgrimes		case 'N':
1641558Srgrimes			Nflag = 1;
1651558Srgrimes			break;
16698542Smckusick		case 'O':
16798542Smckusick			if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
168102231Strhodes				errx(1, "%s: bad file system format value",
16998542Smckusick				    optarg);
17098542Smckusick			break;
17192722Sphk		case 'R':
17292722Sphk			Rflag = 1;
17392722Sphk			break;
1741558Srgrimes		case 'S':
175204909Ssobomax			rval = expand_number_int(optarg, &sectorsize);
176204615Ssobomax			if (rval < 0 || sectorsize <= 0)
17795357Sphk				errx(1, "%s: bad sector size", optarg);
1781558Srgrimes			break;
1791558Srgrimes		case 'T':
1801558Srgrimes			disktype = optarg;
1811558Srgrimes			break;
18275078Sobrien		case 'U':
18375078Sobrien			Uflag = 1;
18475078Sobrien			break;
185174675Sphk		case 'X':
186174675Sphk			Xflag++;
187174675Sphk			break;
1881558Srgrimes		case 'a':
189204919Ssobomax			rval = expand_number_int(optarg, &maxcontig);
190204615Ssobomax			if (rval < 0 || maxcontig <= 0)
19195357Sphk				errx(1, "%s: bad maximum contiguous blocks",
1921558Srgrimes				    optarg);
1931558Srgrimes			break;
1941558Srgrimes		case 'b':
195204919Ssobomax			rval = expand_number_int(optarg, &bsize);
196204615Ssobomax			if (rval < 0)
197204615Ssobomax				 errx(1, "%s: bad block size",
198204615Ssobomax                                    optarg);
199204615Ssobomax			if (bsize < MINBSIZE)
200107412Smckusick				errx(1, "%s: block size too small, min is %d",
201107412Smckusick				    optarg, MINBSIZE);
202107412Smckusick			if (bsize > MAXBSIZE)
203107412Smckusick				errx(1, "%s: block size too large, max is %d",
204107412Smckusick				    optarg, MAXBSIZE);
2051558Srgrimes			break;
2061558Srgrimes		case 'c':
207204919Ssobomax			rval = expand_number_int(optarg, &maxblkspercg);
208204615Ssobomax			if (rval < 0 || maxblkspercg <= 0)
20998542Smckusick				errx(1, "%s: bad blocks per cylinder group",
21098542Smckusick				    optarg);
2111558Srgrimes			break;
21298542Smckusick		case 'd':
213204919Ssobomax			rval = expand_number_int(optarg, &maxbsize);
214204615Ssobomax			if (rval < 0 || maxbsize < MINBSIZE)
21598542Smckusick				errx(1, "%s: bad extent block size", optarg);
21698542Smckusick			break;
2171558Srgrimes		case 'e':
218204919Ssobomax			rval = expand_number_int(optarg, &maxbpg);
219204615Ssobomax			if (rval < 0 || maxbpg <= 0)
22098542Smckusick			  errx(1, "%s: bad blocks per file in a cylinder group",
2211558Srgrimes				    optarg);
2221558Srgrimes			break;
2231558Srgrimes		case 'f':
224204919Ssobomax			rval = expand_number_int(optarg, &fsize);
225204615Ssobomax			if (rval < 0 || fsize <= 0)
22695357Sphk				errx(1, "%s: bad fragment size", optarg);
2271558Srgrimes			break;
22875377Smckusick		case 'g':
229204919Ssobomax			rval = expand_number_int(optarg, &avgfilesize);
230204615Ssobomax			if (rval < 0 || avgfilesize <= 0)
23195357Sphk				errx(1, "%s: bad average file size", optarg);
23275377Smckusick			break;
23375377Smckusick		case 'h':
234204919Ssobomax			rval = expand_number_int(optarg, &avgfilesperdir);
235204615Ssobomax			if (rval < 0 || avgfilesperdir <= 0)
23698542Smckusick			       errx(1, "%s: bad average files per dir", optarg);
23775377Smckusick			break;
2381558Srgrimes		case 'i':
239204919Ssobomax			rval = expand_number_int(optarg, &density);
240204615Ssobomax			if (rval < 0 || density <= 0)
24195357Sphk				errx(1, "%s: bad bytes per inode", optarg);
2421558Srgrimes			break;
243126254Srwatson		case 'l':
244126254Srwatson			lflag = 1;
245126254Srwatson			break;
2461558Srgrimes		case 'm':
2471558Srgrimes			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
24895357Sphk				errx(1, "%s: bad free space %%", optarg);
2491558Srgrimes			break;
250140603Swes		case 'n':
251140603Swes			nflag = 1;
252140603Swes			break;
2531558Srgrimes		case 'o':
25477420Sphk			if (strcmp(optarg, "space") == 0)
25577420Sphk				opt = FS_OPTSPACE;
25677420Sphk			else if (strcmp(optarg, "time") == 0)
25777420Sphk				opt = FS_OPTTIME;
25877420Sphk			else
25995357Sphk				errx(1,
26092533Sbde		"%s: unknown optimization preference: use `space' or `time'",
26192533Sbde				    optarg);
2621558Srgrimes			break;
263174011Syar		case 'r':
264174011Syar			errno = 0;
265174011Syar			reserved = strtoimax(optarg, &cp, 0);
266174011Syar			if (errno != 0 || cp == optarg ||
267174011Syar			    *cp != '\0' || reserved < 0)
268174011Syar				errx(1, "%s: bad reserved size", optarg);
269174011Syar			break;
270185588Sluigi		case 'p':
271185588Sluigi			is_file = 1;
272185588Sluigi			part_name = optarg[0];
273185588Sluigi			break;
274185588Sluigi
2751558Srgrimes		case 's':
276135460Spjd			errno = 0;
277174011Syar			fssize = strtoimax(optarg, &cp, 0);
278174011Syar			if (errno != 0 || cp == optarg ||
279174011Syar			    *cp != '\0' || fssize < 0)
280174011Syar				errx(1, "%s: bad file system size", optarg);
2811558Srgrimes			break;
2821558Srgrimes		case '?':
2831558Srgrimes		default:
2841558Srgrimes			usage();
2851558Srgrimes		}
2861558Srgrimes	argc -= optind;
2871558Srgrimes	argv += optind;
2881558Srgrimes
28995357Sphk	if (argc != 1)
2901558Srgrimes		usage();
2911558Srgrimes
2921558Srgrimes	special = argv[0];
293185588Sluigi	if (!special[0])
294185588Sluigi		err(1, "empty file/special name");
29523682Speter	cp = strrchr(special, '/');
2961558Srgrimes	if (cp == 0) {
2971558Srgrimes		/*
29892533Sbde		 * No path prefix; try prefixing _PATH_DEV.
2991558Srgrimes		 */
30092533Sbde		snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
3011558Srgrimes		special = device;
3021558Srgrimes	}
3031558Srgrimes
304185588Sluigi	if (is_file) {
305185588Sluigi		/* bypass ufs_disk_fillout_blank */
306185588Sluigi		bzero( &disk, sizeof(disk));
307185588Sluigi		disk.d_bsize = 1;
308185588Sluigi		disk.d_name = special;
309185588Sluigi		disk.d_fd = open(special, O_RDONLY);
310185588Sluigi		if (disk.d_fd < 0 ||
311185588Sluigi		    (!Nflag && ufs_disk_write(&disk) == -1))
312185588Sluigi			errx(1, "%s: ", special);
313185588Sluigi	} else if (ufs_disk_fillout_blank(&disk, special) == -1 ||
314110671Sjmallett	    (!Nflag && ufs_disk_write(&disk) == -1)) {
315110671Sjmallett		if (disk.d_error != NULL)
316110671Sjmallett			errx(1, "%s: %s", special, disk.d_error);
317110671Sjmallett		else
318110671Sjmallett			err(1, "%s", special);
319110671Sjmallett	}
320110671Sjmallett	if (fstat(disk.d_fd, &st) < 0)
32195357Sphk		err(1, "%s", special);
322185588Sluigi	if ((st.st_mode & S_IFMT) != S_IFCHR) {
323185588Sluigi		warn("%s: not a character-special device", special);
324185588Sluigi		is_file = 1;	/* assume it is a file */
325185588Sluigi		dkname = special;
326185588Sluigi		if (sectorsize == 0)
327185588Sluigi			sectorsize = 512;
328185588Sluigi		mediasize = st.st_size;
329185588Sluigi		/* set fssize from the partition */
330185588Sluigi	} else {
331185588Sluigi	    if (sectorsize == 0)
332174012Syar		if (ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize) == -1)
333185588Sluigi		    sectorsize = 0;	/* back out on error for safety */
334185588Sluigi	    if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
335174011Syar		getfssize(&fssize, special, mediasize / sectorsize, reserved);
336185588Sluigi	}
33795357Sphk	pp = NULL;
33895357Sphk	lp = getdisklabel(special);
33995357Sphk	if (lp != NULL) {
340185990Sluigi		if (!is_file) /* already set for files */
341185990Sluigi			part_name = special[strlen(special) - 1];
342185990Sluigi		if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) &&
343185990Sluigi				!isdigit(part_name))
344185990Sluigi			errx(1, "%s: can't figure out file system partition",
345185990Sluigi					special);
346185588Sluigi		cp = &part_name;
34795360Sphk		if (isdigit(*cp))
34895360Sphk			pp = &lp->d_partitions[RAW_PART];
34995357Sphk		else
35095357Sphk			pp = &lp->d_partitions[*cp - 'a'];
35195357Sphk		oldpartition = *pp;
35295357Sphk		if (pp->p_size == 0)
35395357Sphk			errx(1, "%s: `%c' partition is unavailable",
35495357Sphk			    special, *cp);
35595357Sphk		if (pp->p_fstype == FS_BOOT)
35695357Sphk			errx(1, "%s: `%c' partition overlaps boot program",
35795357Sphk			    special, *cp);
358174011Syar		getfssize(&fssize, special, pp->p_size, reserved);
35995357Sphk		if (sectorsize == 0)
36095357Sphk			sectorsize = lp->d_secsize;
36195357Sphk		if (fsize == 0)
36295357Sphk			fsize = pp->p_fsize;
36395357Sphk		if (bsize == 0)
36495357Sphk			bsize = pp->p_frag * pp->p_fsize;
365185588Sluigi		if (is_file)
366185588Sluigi			part_ofs = pp->p_offset;
3671558Srgrimes	}
36895357Sphk	if (sectorsize <= 0)
36995357Sphk		errx(1, "%s: no default sector size", special);
37095357Sphk	if (fsize <= 0)
37195357Sphk		fsize = MAX(DFL_FRAGSIZE, sectorsize);
37295357Sphk	if (bsize <= 0)
37395357Sphk		bsize = MIN(DFL_BLKSIZE, 8 * fsize);
3741558Srgrimes	if (minfree < MINFREE && opt != FS_OPTSPACE) {
3751558Srgrimes		fprintf(stderr, "Warning: changing optimization to space ");
3761558Srgrimes		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
3771558Srgrimes		opt = FS_OPTSPACE;
3781558Srgrimes	}
3791558Srgrimes	realsectorsize = sectorsize;
3801558Srgrimes	if (sectorsize != DEV_BSIZE) {		/* XXX */
38120061Ssos		int secperblk = sectorsize / DEV_BSIZE;
38220061Ssos
38320061Ssos		sectorsize = DEV_BSIZE;
38420061Ssos		fssize *= secperblk;
385104308Sphk		if (pp != NULL)
38695357Sphk			pp->p_size *= secperblk;
38720061Ssos	}
38895357Sphk	mkfs(pp, special);
38995357Sphk	if (!unlabeled) {
39095357Sphk		if (realsectorsize != DEV_BSIZE)
39198542Smckusick			pp->p_size /= realsectorsize / DEV_BSIZE;
39295357Sphk		if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
39395357Sphk			rewritelabel(special, lp);
39495357Sphk	}
395110671Sjmallett	ufs_disk_close(&disk);
3961558Srgrimes	exit(0);
3971558Srgrimes}
3981558Srgrimes
399174011Syarvoid
400174011Syargetfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved)
401174011Syar{
402174011Syar	intmax_t available;
403174011Syar
404174011Syar	available = disksize - reserved;
405174011Syar	if (available <= 0)
406174011Syar		errx(1, "%s: reserved not less than device size %jd",
407174011Syar		    s, disksize);
408174011Syar	if (*fsz == 0)
409174011Syar		*fsz = available;
410174011Syar	else if (*fsz > available)
411174011Syar		errx(1, "%s: maximum file system size is %jd",
412174011Syar		    s, available);
413174011Syar}
414174011Syar
4151558Srgrimesstruct disklabel *
41695357Sphkgetdisklabel(char *s)
4171558Srgrimes{
4181558Srgrimes	static struct disklabel lab;
41995357Sphk	struct disklabel *lp;
4201558Srgrimes
421185588Sluigi	if (is_file) {
422185588Sluigi		if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
423185588Sluigi			err(4, "cannot read bootarea");
424185588Sluigi		if (bsd_disklabel_le_dec(
425185588Sluigi		    bootarea + (0 /* labeloffset */ +
426185588Sluigi				1 /* labelsoffset */ * sectorsize),
427185588Sluigi		    &lab, MAXPARTITIONS))
428185588Sluigi			errx(1, "no valid label found");
429185588Sluigi
430185588Sluigi		lp = &lab;
431185588Sluigi		return &lab;
432185588Sluigi	}
433185588Sluigi
434174012Syar	if (ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab) != -1)
43595357Sphk		return (&lab);
43695357Sphk	unlabeled++;
43795357Sphk	if (disktype) {
43895357Sphk		lp = getdiskbyname(disktype);
43995357Sphk		if (lp != NULL)
4401558Srgrimes			return (lp);
4411558Srgrimes	}
44295357Sphk	return (NULL);
4431558Srgrimes}
4441558Srgrimes
44577420Sphkvoid
44695357Sphkrewritelabel(char *s, struct disklabel *lp)
44737775Sbde{
44837775Sbde	if (unlabeled)
44937775Sbde		return;
45037775Sbde	lp->d_checksum = 0;
45137775Sbde	lp->d_checksum = dkcksum(lp);
452185588Sluigi	if (is_file) {
453185588Sluigi		bsd_disklabel_le_enc(bootarea + 0 /* labeloffset */ +
454185588Sluigi			1 /* labelsoffset */ * sectorsize, lp);
455185588Sluigi		lseek(disk.d_fd, 0, SEEK_SET);
456185588Sluigi		if (write(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
457185588Sluigi			errx(1, "cannot write label");
458185588Sluigi		return;
459185588Sluigi	}
460174012Syar	if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) == -1)
461103797Sphk		warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
46237775Sbde}
46337775Sbde
46437664Scharnierstatic void
465110065Sjmallettusage()
4661558Srgrimes{
46777420Sphk	fprintf(stderr,
46877420Sphk	    "usage: %s [ -fsoptions ] special-device%s\n",
46995357Sphk	    getprogname(),
47077420Sphk	    " [device-type]");
4711558Srgrimes	fprintf(stderr, "where fsoptions are:\n");
472203534Sdelphij	fprintf(stderr, "\t-E Erase previous disk content\n");
473167179Spjd	fprintf(stderr, "\t-J Enable journaling via gjournal\n");
474110174Sgordon	fprintf(stderr, "\t-L volume label to add to superblock\n");
4751558Srgrimes	fprintf(stderr,
476102231Strhodes	    "\t-N do not create file system, just print out parameters\n");
477102231Strhodes	fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
478203534Sdelphij	fprintf(stderr, "\t-R regression test, suppress random factors\n");
4791558Srgrimes	fprintf(stderr, "\t-S sector size\n");
4801558Srgrimes	fprintf(stderr, "\t-T disktype\n");
48175078Sobrien	fprintf(stderr, "\t-U enable soft updates\n");
4821558Srgrimes	fprintf(stderr, "\t-a maximum contiguous blocks\n");
4831558Srgrimes	fprintf(stderr, "\t-b block size\n");
48498542Smckusick	fprintf(stderr, "\t-c blocks per cylinders group\n");
48598542Smckusick	fprintf(stderr, "\t-d maximum extent size\n");
4861558Srgrimes	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
4871558Srgrimes	fprintf(stderr, "\t-f frag size\n");
48875377Smckusick	fprintf(stderr, "\t-g average file size\n");
48975377Smckusick	fprintf(stderr, "\t-h average files per directory\n");
4901558Srgrimes	fprintf(stderr, "\t-i number of bytes per inode\n");
491140611Sru	fprintf(stderr, "\t-l enable multilabel MAC\n");
492140611Sru	fprintf(stderr, "\t-n do not create .snap directory\n");
4931558Srgrimes	fprintf(stderr, "\t-m minimum free space %%\n");
4941558Srgrimes	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
495185588Sluigi	fprintf(stderr, "\t-p partition name (a..h)\n");
496174011Syar	fprintf(stderr, "\t-r reserved sectors at the end of device\n");
497174011Syar	fprintf(stderr, "\t-s file system size (sectors)\n");
4981558Srgrimes	exit(1);
4991558Srgrimes}
500204909Ssobomax
501204909Ssobomaxstatic int
502204909Ssobomaxexpand_number_int(const char *buf, int *num)
503204909Ssobomax{
504204909Ssobomax	int64_t num64;
505204909Ssobomax	int rval;
506204909Ssobomax
507204909Ssobomax	rval = expand_number(buf, &num64);
508204919Ssobomax	if (rval < 0)
509204909Ssobomax		return (rval);
510204919Ssobomax	if (num64 > INT_MAX || num64 < INT_MIN) {
511204909Ssobomax		errno = ERANGE;
512204909Ssobomax		return (-1);
513204909Ssobomax	}
514204909Ssobomax	*num = (int)num64;
515204909Ssobomax	return (0);
516204909Ssobomax}
517