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$");
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 */
90218726Smckusickint	jflag;			/* enable soft updates journaling for filesys */
91174675Sphkint	Xflag = 0;		/* exit in middle of newfs for testing */
92163842Spjdint	Jflag;			/* enable gjournal for file system */
93126254Srwatsonint	lflag;			/* enable multilabel for file system */
94140603Swesint	nflag;			/* do not create .snap directory */
95216798Skibint	tflag;			/* enable TRIM */
96174011Syarintmax_t fssize;		/* file system size */
97242379Straszoff_t	mediasize;		/* device size */
98204909Ssobomaxint	sectorsize;		/* bytes/sector */
991558Srgrimesint	realsectorsize;		/* bytes/sector in hardware */
100204919Ssobomaxint	fsize = 0;		/* fragment size */
101204919Ssobomaxint	bsize = 0;		/* block size */
102204919Ssobomaxint	maxbsize = 0;		/* maximum clustering */
103204919Ssobomaxint	maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
1041558Srgrimesint	minfree = MINFREE;	/* free space threshold */
105248623Smckusickint	metaspace;		/* space held for metadata blocks */
1061558Srgrimesint	opt = DEFAULTOPT;	/* optimization preference (space or time) */
107204919Ssobomaxint	density;		/* number of bytes per inode */
108204919Ssobomaxint	maxcontig = 0;		/* max contiguous blocks to allocate */
109204919Ssobomaxint	maxbpg;			/* maximum blocks per file in a cyl group */
110204919Ssobomaxint	avgfilesize = AVFILESIZ;/* expected average file size */
111204919Ssobomaxint	avgfilesperdir = AFPDIR;/* expected number of files per directory */
112110174Sgordonu_char	*volumelabel = NULL;	/* volume label for filesystem */
113110671Sjmallettstruct uufsd disk;		/* libufs disk structure */
1141558Srgrimes
11592763Sphkstatic char	device[MAXPATHLEN];
116185588Sluigistatic u_char   bootarea[BBSIZE];
117185588Sluigistatic int	is_file;		/* work on a file, not a device */
118185588Sluigistatic char	*dkname;
11993777Sbdestatic char	*disktype;
12093777Sbdestatic int	unlabeled;
1211558Srgrimes
122174011Syarstatic void getfssize(intmax_t *, const char *p, intmax_t, intmax_t);
12395357Sphkstatic struct disklabel *getdisklabel(char *s);
12495357Sphkstatic void rewritelabel(char *s, struct disklabel *lp);
12593777Sbdestatic void usage(void);
126204909Ssobomaxstatic int expand_number_int(const char *buf, int *num);
12737664Scharnier
128185588Sluigiufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */
129185588Sluigi
1301558Srgrimesint
13192711Siedowsemain(int argc, char *argv[])
1321558Srgrimes{
13392483Sphk	struct partition *pp;
13492483Sphk	struct disklabel *lp;
1351558Srgrimes	struct partition oldpartition;
1361558Srgrimes	struct stat st;
13795357Sphk	char *cp, *special;
138174011Syar	intmax_t reserved;
139204615Ssobomax	int ch, i, rval;
140185588Sluigi	char part_name;		/* partition name, default to full disk */
1411558Srgrimes
142185588Sluigi	part_name = 'c';
143174011Syar	reserved = 0;
14492717Sphk	while ((ch = getopt(argc, argv,
145248623Smckusick	    "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:jk:lm:no:p:r:s:t")) != -1)
1461558Srgrimes		switch (ch) {
147122785Swes		case 'E':
148174675Sphk			Eflag = 1;
149122785Swes			break;
150163842Spjd		case 'J':
151163842Spjd			Jflag = 1;
152163842Spjd			break;
153110174Sgordon		case 'L':
154110174Sgordon			volumelabel = optarg;
155110174Sgordon			i = -1;
156110174Sgordon			while (isalnum(volumelabel[++i]));
157110174Sgordon			if (volumelabel[i] != '\0') {
158110174Sgordon				errx(1, "bad volume label. Valid characters are alphanumerics.");
159110174Sgordon			}
160110174Sgordon			if (strlen(volumelabel) >= MAXVOLLEN) {
161110174Sgordon				errx(1, "bad volume label. Length is longer than %d.",
162110174Sgordon				    MAXVOLLEN);
163110174Sgordon			}
164110174Sgordon			Lflag = 1;
165110174Sgordon			break;
1661558Srgrimes		case 'N':
1671558Srgrimes			Nflag = 1;
1681558Srgrimes			break;
16998542Smckusick		case 'O':
17098542Smckusick			if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
171102231Strhodes				errx(1, "%s: bad file system format value",
17298542Smckusick				    optarg);
17398542Smckusick			break;
17492722Sphk		case 'R':
17592722Sphk			Rflag = 1;
17692722Sphk			break;
1771558Srgrimes		case 'S':
178204909Ssobomax			rval = expand_number_int(optarg, &sectorsize);
179204615Ssobomax			if (rval < 0 || sectorsize <= 0)
18095357Sphk				errx(1, "%s: bad sector size", optarg);
1811558Srgrimes			break;
1821558Srgrimes		case 'T':
1831558Srgrimes			disktype = optarg;
1841558Srgrimes			break;
185218726Smckusick		case 'j':
186218726Smckusick			jflag = 1;
187218726Smckusick			/* fall through to enable soft updates */
18875078Sobrien		case 'U':
18975078Sobrien			Uflag = 1;
19075078Sobrien			break;
191174675Sphk		case 'X':
192174675Sphk			Xflag++;
193174675Sphk			break;
1941558Srgrimes		case 'a':
195204919Ssobomax			rval = expand_number_int(optarg, &maxcontig);
196204615Ssobomax			if (rval < 0 || maxcontig <= 0)
19795357Sphk				errx(1, "%s: bad maximum contiguous blocks",
1981558Srgrimes				    optarg);
1991558Srgrimes			break;
2001558Srgrimes		case 'b':
201204919Ssobomax			rval = expand_number_int(optarg, &bsize);
202204615Ssobomax			if (rval < 0)
203204615Ssobomax				 errx(1, "%s: bad block size",
204204615Ssobomax                                    optarg);
205204615Ssobomax			if (bsize < MINBSIZE)
206107412Smckusick				errx(1, "%s: block size too small, min is %d",
207107412Smckusick				    optarg, MINBSIZE);
208107412Smckusick			if (bsize > MAXBSIZE)
209107412Smckusick				errx(1, "%s: block size too large, max is %d",
210107412Smckusick				    optarg, MAXBSIZE);
2111558Srgrimes			break;
2121558Srgrimes		case 'c':
213204919Ssobomax			rval = expand_number_int(optarg, &maxblkspercg);
214204615Ssobomax			if (rval < 0 || maxblkspercg <= 0)
21598542Smckusick				errx(1, "%s: bad blocks per cylinder group",
21698542Smckusick				    optarg);
2171558Srgrimes			break;
21898542Smckusick		case 'd':
219204919Ssobomax			rval = expand_number_int(optarg, &maxbsize);
220204615Ssobomax			if (rval < 0 || maxbsize < MINBSIZE)
22198542Smckusick				errx(1, "%s: bad extent block size", optarg);
22298542Smckusick			break;
2231558Srgrimes		case 'e':
224204919Ssobomax			rval = expand_number_int(optarg, &maxbpg);
225204615Ssobomax			if (rval < 0 || maxbpg <= 0)
22698542Smckusick			  errx(1, "%s: bad blocks per file in a cylinder group",
2271558Srgrimes				    optarg);
2281558Srgrimes			break;
2291558Srgrimes		case 'f':
230204919Ssobomax			rval = expand_number_int(optarg, &fsize);
231204615Ssobomax			if (rval < 0 || fsize <= 0)
23295357Sphk				errx(1, "%s: bad fragment size", optarg);
2331558Srgrimes			break;
23475377Smckusick		case 'g':
235204919Ssobomax			rval = expand_number_int(optarg, &avgfilesize);
236204615Ssobomax			if (rval < 0 || avgfilesize <= 0)
23795357Sphk				errx(1, "%s: bad average file size", optarg);
23875377Smckusick			break;
23975377Smckusick		case 'h':
240204919Ssobomax			rval = expand_number_int(optarg, &avgfilesperdir);
241204615Ssobomax			if (rval < 0 || avgfilesperdir <= 0)
24298542Smckusick			       errx(1, "%s: bad average files per dir", optarg);
24375377Smckusick			break;
2441558Srgrimes		case 'i':
245204919Ssobomax			rval = expand_number_int(optarg, &density);
246204615Ssobomax			if (rval < 0 || density <= 0)
24795357Sphk				errx(1, "%s: bad bytes per inode", optarg);
2481558Srgrimes			break;
249126254Srwatson		case 'l':
250126254Srwatson			lflag = 1;
251126254Srwatson			break;
252248623Smckusick		case 'k':
253248623Smckusick			if ((metaspace = atoi(optarg)) < 0)
254248623Smckusick				errx(1, "%s: bad metadata space %%", optarg);
255248623Smckusick			if (metaspace == 0)
256248623Smckusick				/* force to stay zero in mkfs */
257248623Smckusick				metaspace = -1;
258248623Smckusick			break;
2591558Srgrimes		case 'm':
2601558Srgrimes			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
26195357Sphk				errx(1, "%s: bad free space %%", optarg);
2621558Srgrimes			break;
263140603Swes		case 'n':
264140603Swes			nflag = 1;
265140603Swes			break;
2661558Srgrimes		case 'o':
26777420Sphk			if (strcmp(optarg, "space") == 0)
26877420Sphk				opt = FS_OPTSPACE;
26977420Sphk			else if (strcmp(optarg, "time") == 0)
27077420Sphk				opt = FS_OPTTIME;
27177420Sphk			else
27295357Sphk				errx(1,
27392533Sbde		"%s: unknown optimization preference: use `space' or `time'",
27492533Sbde				    optarg);
2751558Srgrimes			break;
276174011Syar		case 'r':
277174011Syar			errno = 0;
278174011Syar			reserved = strtoimax(optarg, &cp, 0);
279174011Syar			if (errno != 0 || cp == optarg ||
280174011Syar			    *cp != '\0' || reserved < 0)
281174011Syar				errx(1, "%s: bad reserved size", optarg);
282174011Syar			break;
283185588Sluigi		case 'p':
284185588Sluigi			is_file = 1;
285185588Sluigi			part_name = optarg[0];
286185588Sluigi			break;
287185588Sluigi
2881558Srgrimes		case 's':
289135460Spjd			errno = 0;
290174011Syar			fssize = strtoimax(optarg, &cp, 0);
291174011Syar			if (errno != 0 || cp == optarg ||
292174011Syar			    *cp != '\0' || fssize < 0)
293174011Syar				errx(1, "%s: bad file system size", optarg);
2941558Srgrimes			break;
295216798Skib		case 't':
296216798Skib			tflag = 1;
297216798Skib			break;
2981558Srgrimes		case '?':
2991558Srgrimes		default:
3001558Srgrimes			usage();
3011558Srgrimes		}
3021558Srgrimes	argc -= optind;
3031558Srgrimes	argv += optind;
3041558Srgrimes
30595357Sphk	if (argc != 1)
3061558Srgrimes		usage();
3071558Srgrimes
3081558Srgrimes	special = argv[0];
309185588Sluigi	if (!special[0])
310185588Sluigi		err(1, "empty file/special name");
31123682Speter	cp = strrchr(special, '/');
3121558Srgrimes	if (cp == 0) {
3131558Srgrimes		/*
31492533Sbde		 * No path prefix; try prefixing _PATH_DEV.
3151558Srgrimes		 */
31692533Sbde		snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
3171558Srgrimes		special = device;
3181558Srgrimes	}
3191558Srgrimes
320185588Sluigi	if (is_file) {
321185588Sluigi		/* bypass ufs_disk_fillout_blank */
322185588Sluigi		bzero( &disk, sizeof(disk));
323185588Sluigi		disk.d_bsize = 1;
324185588Sluigi		disk.d_name = special;
325185588Sluigi		disk.d_fd = open(special, O_RDONLY);
326185588Sluigi		if (disk.d_fd < 0 ||
327185588Sluigi		    (!Nflag && ufs_disk_write(&disk) == -1))
328185588Sluigi			errx(1, "%s: ", special);
329185588Sluigi	} else if (ufs_disk_fillout_blank(&disk, special) == -1 ||
330110671Sjmallett	    (!Nflag && ufs_disk_write(&disk) == -1)) {
331110671Sjmallett		if (disk.d_error != NULL)
332110671Sjmallett			errx(1, "%s: %s", special, disk.d_error);
333110671Sjmallett		else
334110671Sjmallett			err(1, "%s", special);
335110671Sjmallett	}
336110671Sjmallett	if (fstat(disk.d_fd, &st) < 0)
33795357Sphk		err(1, "%s", special);
338185588Sluigi	if ((st.st_mode & S_IFMT) != S_IFCHR) {
339185588Sluigi		warn("%s: not a character-special device", special);
340185588Sluigi		is_file = 1;	/* assume it is a file */
341185588Sluigi		dkname = special;
342185588Sluigi		if (sectorsize == 0)
343185588Sluigi			sectorsize = 512;
344185588Sluigi		mediasize = st.st_size;
345185588Sluigi		/* set fssize from the partition */
346185588Sluigi	} else {
347185588Sluigi	    if (sectorsize == 0)
348174012Syar		if (ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize) == -1)
349185588Sluigi		    sectorsize = 0;	/* back out on error for safety */
350185588Sluigi	    if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
351174011Syar		getfssize(&fssize, special, mediasize / sectorsize, reserved);
352185588Sluigi	}
35395357Sphk	pp = NULL;
35495357Sphk	lp = getdisklabel(special);
35595357Sphk	if (lp != NULL) {
356185990Sluigi		if (!is_file) /* already set for files */
357185990Sluigi			part_name = special[strlen(special) - 1];
358185990Sluigi		if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) &&
359185990Sluigi				!isdigit(part_name))
360185990Sluigi			errx(1, "%s: can't figure out file system partition",
361185990Sluigi					special);
362185588Sluigi		cp = &part_name;
36395360Sphk		if (isdigit(*cp))
36495360Sphk			pp = &lp->d_partitions[RAW_PART];
36595357Sphk		else
36695357Sphk			pp = &lp->d_partitions[*cp - 'a'];
36795357Sphk		oldpartition = *pp;
36895357Sphk		if (pp->p_size == 0)
36995357Sphk			errx(1, "%s: `%c' partition is unavailable",
37095357Sphk			    special, *cp);
37195357Sphk		if (pp->p_fstype == FS_BOOT)
37295357Sphk			errx(1, "%s: `%c' partition overlaps boot program",
37395357Sphk			    special, *cp);
374174011Syar		getfssize(&fssize, special, pp->p_size, reserved);
37595357Sphk		if (sectorsize == 0)
37695357Sphk			sectorsize = lp->d_secsize;
37795357Sphk		if (fsize == 0)
37895357Sphk			fsize = pp->p_fsize;
37995357Sphk		if (bsize == 0)
38095357Sphk			bsize = pp->p_frag * pp->p_fsize;
381185588Sluigi		if (is_file)
382185588Sluigi			part_ofs = pp->p_offset;
3831558Srgrimes	}
38495357Sphk	if (sectorsize <= 0)
38595357Sphk		errx(1, "%s: no default sector size", special);
38695357Sphk	if (fsize <= 0)
38795357Sphk		fsize = MAX(DFL_FRAGSIZE, sectorsize);
38895357Sphk	if (bsize <= 0)
38995357Sphk		bsize = MIN(DFL_BLKSIZE, 8 * fsize);
3901558Srgrimes	if (minfree < MINFREE && opt != FS_OPTSPACE) {
3911558Srgrimes		fprintf(stderr, "Warning: changing optimization to space ");
3921558Srgrimes		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
3931558Srgrimes		opt = FS_OPTSPACE;
3941558Srgrimes	}
3951558Srgrimes	realsectorsize = sectorsize;
3961558Srgrimes	if (sectorsize != DEV_BSIZE) {		/* XXX */
39720061Ssos		int secperblk = sectorsize / DEV_BSIZE;
39820061Ssos
39920061Ssos		sectorsize = DEV_BSIZE;
40020061Ssos		fssize *= secperblk;
401104308Sphk		if (pp != NULL)
40295357Sphk			pp->p_size *= secperblk;
40320061Ssos	}
40495357Sphk	mkfs(pp, special);
40595357Sphk	if (!unlabeled) {
40695357Sphk		if (realsectorsize != DEV_BSIZE)
40798542Smckusick			pp->p_size /= realsectorsize / DEV_BSIZE;
40895357Sphk		if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
40995357Sphk			rewritelabel(special, lp);
41095357Sphk	}
411110671Sjmallett	ufs_disk_close(&disk);
412218726Smckusick	if (!jflag)
413218726Smckusick		exit(0);
414218726Smckusick	if (execlp("tunefs", "newfs", "-j", "enable", special, NULL) < 0)
415218726Smckusick		err(1, "Cannot enable soft updates journaling, tunefs");
416218726Smckusick	/* NOT REACHED */
4171558Srgrimes}
4181558Srgrimes
419174011Syarvoid
420174011Syargetfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved)
421174011Syar{
422174011Syar	intmax_t available;
423174011Syar
424174011Syar	available = disksize - reserved;
425174011Syar	if (available <= 0)
426174011Syar		errx(1, "%s: reserved not less than device size %jd",
427174011Syar		    s, disksize);
428174011Syar	if (*fsz == 0)
429174011Syar		*fsz = available;
430174011Syar	else if (*fsz > available)
431174011Syar		errx(1, "%s: maximum file system size is %jd",
432174011Syar		    s, available);
433174011Syar}
434174011Syar
4351558Srgrimesstruct disklabel *
43695357Sphkgetdisklabel(char *s)
4371558Srgrimes{
4381558Srgrimes	static struct disklabel lab;
43995357Sphk	struct disklabel *lp;
4401558Srgrimes
441185588Sluigi	if (is_file) {
442185588Sluigi		if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
443185588Sluigi			err(4, "cannot read bootarea");
444185588Sluigi		if (bsd_disklabel_le_dec(
445185588Sluigi		    bootarea + (0 /* labeloffset */ +
446185588Sluigi				1 /* labelsoffset */ * sectorsize),
447185588Sluigi		    &lab, MAXPARTITIONS))
448185588Sluigi			errx(1, "no valid label found");
449185588Sluigi
450185588Sluigi		lp = &lab;
451185588Sluigi		return &lab;
452185588Sluigi	}
453185588Sluigi
454174012Syar	if (ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab) != -1)
45595357Sphk		return (&lab);
45695357Sphk	unlabeled++;
45795357Sphk	if (disktype) {
45895357Sphk		lp = getdiskbyname(disktype);
45995357Sphk		if (lp != NULL)
4601558Srgrimes			return (lp);
4611558Srgrimes	}
46295357Sphk	return (NULL);
4631558Srgrimes}
4641558Srgrimes
46577420Sphkvoid
46695357Sphkrewritelabel(char *s, struct disklabel *lp)
46737775Sbde{
46837775Sbde	if (unlabeled)
46937775Sbde		return;
47037775Sbde	lp->d_checksum = 0;
47137775Sbde	lp->d_checksum = dkcksum(lp);
472185588Sluigi	if (is_file) {
473185588Sluigi		bsd_disklabel_le_enc(bootarea + 0 /* labeloffset */ +
474185588Sluigi			1 /* labelsoffset */ * sectorsize, lp);
475185588Sluigi		lseek(disk.d_fd, 0, SEEK_SET);
476185588Sluigi		if (write(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
477185588Sluigi			errx(1, "cannot write label");
478185588Sluigi		return;
479185588Sluigi	}
480174012Syar	if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) == -1)
481103797Sphk		warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
48237775Sbde}
48337775Sbde
48437664Scharnierstatic void
485110065Sjmallettusage()
4861558Srgrimes{
48777420Sphk	fprintf(stderr,
48877420Sphk	    "usage: %s [ -fsoptions ] special-device%s\n",
48995357Sphk	    getprogname(),
49077420Sphk	    " [device-type]");
4911558Srgrimes	fprintf(stderr, "where fsoptions are:\n");
492203534Sdelphij	fprintf(stderr, "\t-E Erase previous disk content\n");
493167179Spjd	fprintf(stderr, "\t-J Enable journaling via gjournal\n");
494110174Sgordon	fprintf(stderr, "\t-L volume label to add to superblock\n");
4951558Srgrimes	fprintf(stderr,
496102231Strhodes	    "\t-N do not create file system, just print out parameters\n");
497102231Strhodes	fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
498203534Sdelphij	fprintf(stderr, "\t-R regression test, suppress random factors\n");
4991558Srgrimes	fprintf(stderr, "\t-S sector size\n");
5001558Srgrimes	fprintf(stderr, "\t-T disktype\n");
50175078Sobrien	fprintf(stderr, "\t-U enable soft updates\n");
5021558Srgrimes	fprintf(stderr, "\t-a maximum contiguous blocks\n");
5031558Srgrimes	fprintf(stderr, "\t-b block size\n");
50498542Smckusick	fprintf(stderr, "\t-c blocks per cylinders group\n");
50598542Smckusick	fprintf(stderr, "\t-d maximum extent size\n");
5061558Srgrimes	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
5071558Srgrimes	fprintf(stderr, "\t-f frag size\n");
50875377Smckusick	fprintf(stderr, "\t-g average file size\n");
50975377Smckusick	fprintf(stderr, "\t-h average files per directory\n");
5101558Srgrimes	fprintf(stderr, "\t-i number of bytes per inode\n");
511218726Smckusick	fprintf(stderr, "\t-j enable soft updates journaling\n");
512248623Smckusick	fprintf(stderr, "\t-k space to hold for metadata blocks\n");
513140611Sru	fprintf(stderr, "\t-l enable multilabel MAC\n");
514140611Sru	fprintf(stderr, "\t-n do not create .snap directory\n");
5151558Srgrimes	fprintf(stderr, "\t-m minimum free space %%\n");
5161558Srgrimes	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
517185588Sluigi	fprintf(stderr, "\t-p partition name (a..h)\n");
518174011Syar	fprintf(stderr, "\t-r reserved sectors at the end of device\n");
519174011Syar	fprintf(stderr, "\t-s file system size (sectors)\n");
520216798Skib	fprintf(stderr, "\t-t enable TRIM\n");
5211558Srgrimes	exit(1);
5221558Srgrimes}
523204909Ssobomax
524204909Ssobomaxstatic int
525204909Ssobomaxexpand_number_int(const char *buf, int *num)
526204909Ssobomax{
527204909Ssobomax	int64_t num64;
528204909Ssobomax	int rval;
529204909Ssobomax
530204909Ssobomax	rval = expand_number(buf, &num64);
531204919Ssobomax	if (rval < 0)
532204909Ssobomax		return (rval);
533204919Ssobomax	if (num64 > INT_MAX || num64 < INT_MIN) {
534204909Ssobomax		errno = ERANGE;
535204909Ssobomax		return (-1);
536204909Ssobomax	}
537204909Ssobomax	*num = (int)num64;
538204909Ssobomax	return (0);
539204909Ssobomax}
540