xref: /freebsd/sbin/newfs/newfs.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program.
10  *
11  * Copyright (c) 1983, 1989, 1993, 1994
12  *	The Regents of the University of California.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the University of
25  *	California, Berkeley and its contributors.
26  * 4. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 #if 0
44 #ifndef lint
45 static const char copyright[] =
46 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
47 	The Regents of the University of California.  All rights reserved.\n";
48 #endif /* not lint */
49 
50 #ifndef lint
51 static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
52 #endif /* not lint */
53 #endif
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
56 
57 /*
58  * newfs: friendly front end to mkfs
59  */
60 #include <sys/param.h>
61 #include <sys/stat.h>
62 #include <sys/disk.h>
63 #include <sys/disklabel.h>
64 #include <sys/file.h>
65 #include <sys/mount.h>
66 
67 #include <ufs/ufs/dir.h>
68 #include <ufs/ufs/dinode.h>
69 #include <ufs/ffs/fs.h>
70 #include <ufs/ufs/ufsmount.h>
71 
72 #include <ctype.h>
73 #include <err.h>
74 #include <errno.h>
75 #include <paths.h>
76 #include <stdarg.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <syslog.h>
81 #include <unistd.h>
82 
83 #include "newfs.h"
84 
85 /*
86  * The following two constants set the default block and fragment sizes.
87  * Both constants must be a power of 2 and meet the following constraints:
88  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
89  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
90  *	DESBLKSIZE / DESFRAGSIZE <= 8
91  */
92 #define	DFL_FRAGSIZE	2048
93 #define	DFL_BLKSIZE	16384
94 
95 /*
96  * Cylinder groups may have up to MAXBLKSPERCG blocks. The actual
97  * number used depends upon how much information can be stored
98  * in a cylinder group map which must fit in a single file system
99  * block. The default is to use as many as possible blocks per group.
100  */
101 #define	MAXBLKSPERCG	0x7fffffff	/* desired fs_fpg ("infinity") */
102 
103 /*
104  * MAXBLKPG determines the maximum number of data blocks which are
105  * placed in a single cylinder group. The default is one indirect
106  * block worth of data blocks.
107  */
108 #define MAXBLKPG(bsize)	((bsize) / sizeof(ufs2_daddr_t))
109 
110 /*
111  * Each file system has a number of inodes statically allocated.
112  * We allocate one inode slot per NFPI fragments, expecting this
113  * to be far more than we will ever need.
114  */
115 #define	NFPI		4
116 
117 int	Lflag;			/* add a volume label */
118 int	Nflag;			/* run without writing file system */
119 int	Oflag = 2;		/* file system format (1 => UFS1, 2 => UFS2) */
120 int	Rflag;			/* regression test */
121 int	Uflag;			/* enable soft updates for file system */
122 int	Eflag = 0;		/* exit in middle of newfs for testing */
123 int	lflag;			/* enable multilabel for file system */
124 quad_t	fssize;			/* file system size */
125 int	sectorsize;		/* bytes/sector */
126 int	realsectorsize;		/* bytes/sector in hardware */
127 int	fsize = 0;		/* fragment size */
128 int	bsize = 0;		/* block size */
129 int	maxbsize = 0;		/* maximum clustering */
130 int	maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
131 int	minfree = MINFREE;	/* free space threshold */
132 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
133 int	density;		/* number of bytes per inode */
134 int	maxcontig = 0;		/* max contiguous blocks to allocate */
135 int	maxbpg;			/* maximum blocks per file in a cyl group */
136 int	avgfilesize = AVFILESIZ;/* expected average file size */
137 int	avgfilesperdir = AFPDIR;/* expected number of files per directory */
138 u_char	*volumelabel = NULL;	/* volume label for filesystem */
139 struct uufsd disk;		/* libufs disk structure */
140 
141 static char	device[MAXPATHLEN];
142 static char	*disktype;
143 static int	unlabeled;
144 
145 static struct disklabel *getdisklabel(char *s);
146 static void rewritelabel(char *s, struct disklabel *lp);
147 static void usage(void);
148 
149 int
150 main(int argc, char *argv[])
151 {
152 	struct partition *pp;
153 	struct disklabel *lp;
154 	struct partition oldpartition;
155 	struct stat st;
156 	char *cp, *special;
157 	int ch, i;
158 	off_t mediasize;
159 
160 	while ((ch = getopt(argc, argv,
161 	    "EL:NO:RS:T:Ua:b:c:d:e:f:g:h:i:lm:o:s:")) != -1)
162 		switch (ch) {
163 		case 'E':
164 			Eflag++;
165 			break;
166 		case 'L':
167 			volumelabel = optarg;
168 			i = -1;
169 			while (isalnum(volumelabel[++i]));
170 			if (volumelabel[i] != '\0') {
171 				errx(1, "bad volume label. Valid characters are alphanumerics.");
172 			}
173 			if (strlen(volumelabel) >= MAXVOLLEN) {
174 				errx(1, "bad volume label. Length is longer than %d.",
175 				    MAXVOLLEN);
176 			}
177 			Lflag = 1;
178 			break;
179 		case 'N':
180 			Nflag = 1;
181 			break;
182 		case 'O':
183 			if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
184 				errx(1, "%s: bad file system format value",
185 				    optarg);
186 			break;
187 		case 'R':
188 			Rflag = 1;
189 			break;
190 		case 'S':
191 			if ((sectorsize = atoi(optarg)) <= 0)
192 				errx(1, "%s: bad sector size", optarg);
193 			break;
194 		case 'T':
195 			disktype = optarg;
196 			break;
197 		case 'U':
198 			Uflag = 1;
199 			break;
200 		case 'a':
201 			if ((maxcontig = atoi(optarg)) <= 0)
202 				errx(1, "%s: bad maximum contiguous blocks",
203 				    optarg);
204 			break;
205 		case 'b':
206 			if ((bsize = atoi(optarg)) < MINBSIZE)
207 				errx(1, "%s: block size too small, min is %d",
208 				    optarg, MINBSIZE);
209 			if (bsize > MAXBSIZE)
210 				errx(1, "%s: block size too large, max is %d",
211 				    optarg, MAXBSIZE);
212 			break;
213 		case 'c':
214 			if ((maxblkspercg = atoi(optarg)) <= 0)
215 				errx(1, "%s: bad blocks per cylinder group",
216 				    optarg);
217 			break;
218 		case 'd':
219 			if ((maxbsize = atoi(optarg)) < MINBSIZE)
220 				errx(1, "%s: bad extent block size", optarg);
221 			break;
222 		case 'e':
223 			if ((maxbpg = atoi(optarg)) <= 0)
224 			  errx(1, "%s: bad blocks per file in a cylinder group",
225 				    optarg);
226 			break;
227 		case 'f':
228 			if ((fsize = atoi(optarg)) <= 0)
229 				errx(1, "%s: bad fragment size", optarg);
230 			break;
231 		case 'g':
232 			if ((avgfilesize = atoi(optarg)) <= 0)
233 				errx(1, "%s: bad average file size", optarg);
234 			break;
235 		case 'h':
236 			if ((avgfilesperdir = atoi(optarg)) <= 0)
237 			       errx(1, "%s: bad average files per dir", optarg);
238 			break;
239 		case 'i':
240 			if ((density = atoi(optarg)) <= 0)
241 				errx(1, "%s: bad bytes per inode", optarg);
242 			break;
243 		case 'l':
244 			lflag = 1;
245 			break;
246 		case 'm':
247 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
248 				errx(1, "%s: bad free space %%", optarg);
249 			break;
250 		case 'o':
251 			if (strcmp(optarg, "space") == 0)
252 				opt = FS_OPTSPACE;
253 			else if (strcmp(optarg, "time") == 0)
254 				opt = FS_OPTTIME;
255 			else
256 				errx(1,
257 		"%s: unknown optimization preference: use `space' or `time'",
258 				    optarg);
259 			break;
260 		case 's':
261 			if ((fssize = atoi(optarg)) <= 0)
262 				errx(1, "%s: bad file system size", optarg);
263 			break;
264 		case '?':
265 		default:
266 			usage();
267 		}
268 	argc -= optind;
269 	argv += optind;
270 
271 	if (argc != 1)
272 		usage();
273 
274 	special = argv[0];
275 	cp = strrchr(special, '/');
276 	if (cp == 0) {
277 		/*
278 		 * No path prefix; try prefixing _PATH_DEV.
279 		 */
280 		snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
281 		special = device;
282 	}
283 
284 	if (ufs_disk_fillout_blank(&disk, special) == -1 ||
285 	    (!Nflag && ufs_disk_write(&disk) == -1)) {
286 		if (disk.d_error != NULL)
287 			errx(1, "%s: %s", special, disk.d_error);
288 		else
289 			err(1, "%s", special);
290 	}
291 	if (fstat(disk.d_fd, &st) < 0)
292 		err(1, "%s", special);
293 	if ((st.st_mode & S_IFMT) != S_IFCHR)
294 		errx(1, "%s: not a character-special device", special);
295 
296 	if (sectorsize == 0)
297 		ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize);
298 	if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) {
299 		if (fssize == 0)
300 			fssize = mediasize / sectorsize;
301 		else if (fssize > mediasize / sectorsize)
302 			errx(1, "%s: maximum file system size is %u",
303 			    special, (u_int)(mediasize / sectorsize));
304 	}
305 	pp = NULL;
306 	lp = getdisklabel(special);
307 	if (lp != NULL) {
308 		cp = strchr(special, '\0');
309 		cp--;
310 		if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))
311 			errx(1, "%s: can't figure out file system partition",
312 			    special);
313 		if (isdigit(*cp))
314 			pp = &lp->d_partitions[RAW_PART];
315 		else
316 			pp = &lp->d_partitions[*cp - 'a'];
317 		oldpartition = *pp;
318 		if (pp->p_size == 0)
319 			errx(1, "%s: `%c' partition is unavailable",
320 			    special, *cp);
321 		if (pp->p_fstype == FS_BOOT)
322 			errx(1, "%s: `%c' partition overlaps boot program",
323 			    special, *cp);
324 		if (fssize == 0)
325 			fssize = pp->p_size;
326 		if (fssize > pp->p_size)
327 			errx(1,
328 		    "%s: maximum file system size %d", special, pp->p_size);
329 		if (sectorsize == 0)
330 			sectorsize = lp->d_secsize;
331 		if (fsize == 0)
332 			fsize = pp->p_fsize;
333 		if (bsize == 0)
334 			bsize = pp->p_frag * pp->p_fsize;
335 	}
336 	if (sectorsize <= 0)
337 		errx(1, "%s: no default sector size", special);
338 	if (fsize <= 0)
339 		fsize = MAX(DFL_FRAGSIZE, sectorsize);
340 	if (bsize <= 0)
341 		bsize = MIN(DFL_BLKSIZE, 8 * fsize);
342 	if (maxbsize == 0)
343 		maxbsize = bsize;
344 	/*
345 	 * Maxcontig sets the default for the maximum number of blocks
346 	 * that may be allocated sequentially. With file system clustering
347 	 * it is possible to allocate contiguous blocks up to the maximum
348 	 * transfer size permitted by the controller or buffering.
349 	 */
350 	if (maxcontig == 0)
351 		maxcontig = MAX(1, MAXPHYS / bsize);
352 	if (density == 0)
353 		density = NFPI * fsize;
354 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
355 		fprintf(stderr, "Warning: changing optimization to space ");
356 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
357 		opt = FS_OPTSPACE;
358 	}
359 	if (maxbpg == 0)
360 		maxbpg = MAXBLKPG(bsize);
361 	realsectorsize = sectorsize;
362 	if (sectorsize != DEV_BSIZE) {		/* XXX */
363 		int secperblk = sectorsize / DEV_BSIZE;
364 
365 		sectorsize = DEV_BSIZE;
366 		fssize *= secperblk;
367 		if (pp != NULL)
368 			pp->p_size *= secperblk;
369 	}
370 	mkfs(pp, special);
371 	if (!unlabeled) {
372 		if (realsectorsize != DEV_BSIZE)
373 			pp->p_size /= realsectorsize / DEV_BSIZE;
374 		if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
375 			rewritelabel(special, lp);
376 	}
377 	ufs_disk_close(&disk);
378 	exit(0);
379 }
380 
381 struct disklabel *
382 getdisklabel(char *s)
383 {
384 	static struct disklabel lab;
385 	struct disklabel *lp;
386 
387 	if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab))
388 		return (&lab);
389 	unlabeled++;
390 	if (disktype) {
391 		lp = getdiskbyname(disktype);
392 		if (lp != NULL)
393 			return (lp);
394 	}
395 	return (NULL);
396 }
397 
398 void
399 rewritelabel(char *s, struct disklabel *lp)
400 {
401 	if (unlabeled)
402 		return;
403 	lp->d_checksum = 0;
404 	lp->d_checksum = dkcksum(lp);
405 	if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0)
406 		warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
407 }
408 
409 static void
410 usage()
411 {
412 	fprintf(stderr,
413 	    "usage: %s [ -fsoptions ] special-device%s\n",
414 	    getprogname(),
415 	    " [device-type]");
416 	fprintf(stderr, "where fsoptions are:\n");
417 	fprintf(stderr, "\t-L volume label to add to superblock\n");
418 	fprintf(stderr,
419 	    "\t-N do not create file system, just print out parameters\n");
420 	fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
421 	fprintf(stderr, "\t-R regression test, supress random factors\n");
422 	fprintf(stderr, "\t-S sector size\n");
423 	fprintf(stderr, "\t-T disktype\n");
424 	fprintf(stderr, "\t-U enable soft updates\n");
425 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
426 	fprintf(stderr, "\t-b block size\n");
427 	fprintf(stderr, "\t-c blocks per cylinders group\n");
428 	fprintf(stderr, "\t-d maximum extent size\n");
429 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
430 	fprintf(stderr, "\t-f frag size\n");
431 	fprintf(stderr, "\t-g average file size\n");
432 	fprintf(stderr, "\t-h average files per directory\n");
433 	fprintf(stderr, "\t-i number of bytes per inode\n");
434 	fprintf(stderr, "\t-m minimum free space %%\n");
435 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
436 	fprintf(stderr, "\t-s file systemsize (sectors)\n");
437 	exit(1);
438 }
439