xref: /freebsd/sbin/newfs/newfs.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 #ifndef lint
44 static const char copyright[] =
45 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
46 	The Regents of the University of California.  All rights reserved.\n";
47 #endif /* not lint */
48 
49 #ifndef lint
50 #if 0
51 static char sccsid[] = "@(#)newfs.c	8.13 (Berkeley) 5/1/95";
52 #endif
53 static const char rcsid[] =
54   "$FreeBSD$";
55 #endif /* not lint */
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 = 1;		/* file system format (1 => UFS1, 2 => UFS2) */
120 int	Rflag;			/* regression test */
121 int	Uflag;			/* enable soft updates for file system */
122 quad_t	fssize;			/* file system size */
123 int	sectorsize;		/* bytes/sector */
124 int	realsectorsize;		/* bytes/sector in hardware */
125 int	fsize = 0;		/* fragment size */
126 int	bsize = 0;		/* block size */
127 int	maxbsize = 0;		/* maximum clustering */
128 int	maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
129 int	minfree = MINFREE;	/* free space threshold */
130 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
131 int	density;		/* number of bytes per inode */
132 int	maxcontig = 0;		/* max contiguous blocks to allocate */
133 int	maxbpg;			/* maximum blocks per file in a cyl group */
134 int	avgfilesize = AVFILESIZ;/* expected average file size */
135 int	avgfilesperdir = AFPDIR;/* expected number of files per directory */
136 u_char	*volumelabel = NULL;	/* volume label for filesystem */
137 struct uufsd disk;		/* libufs disk structure */
138 
139 static char	device[MAXPATHLEN];
140 static char	*disktype;
141 static int	unlabeled;
142 
143 static struct disklabel *getdisklabel(char *s);
144 static void rewritelabel(char *s, struct disklabel *lp);
145 static void usage(void);
146 
147 int
148 main(int argc, char *argv[])
149 {
150 	struct partition *pp;
151 	struct disklabel *lp;
152 	struct partition oldpartition;
153 	struct stat st;
154 	char *cp, *special;
155 	int ch, i;
156 	off_t mediasize;
157 
158 	while ((ch = getopt(argc, argv,
159 	    "L:NO:RS:T:Ua:b:c:d:e:f:g:h:i:m:o:s:")) != -1)
160 		switch (ch) {
161 		case 'L':
162 			volumelabel = optarg;
163 			i = -1;
164 			while (isalnum(volumelabel[++i]));
165 			if (volumelabel[i] != '\0') {
166 				errx(1, "bad volume label. Valid characters are alphanumerics.");
167 			}
168 			if (strlen(volumelabel) >= MAXVOLLEN) {
169 				errx(1, "bad volume label. Length is longer than %d.",
170 				    MAXVOLLEN);
171 			}
172 			Lflag = 1;
173 			break;
174 		case 'N':
175 			Nflag = 1;
176 			break;
177 		case 'O':
178 			if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
179 				errx(1, "%s: bad file system format value",
180 				    optarg);
181 			break;
182 		case 'R':
183 			Rflag = 1;
184 			break;
185 		case 'S':
186 			if ((sectorsize = atoi(optarg)) <= 0)
187 				errx(1, "%s: bad sector size", optarg);
188 			break;
189 		case 'T':
190 			disktype = optarg;
191 			break;
192 		case 'U':
193 			Uflag = 1;
194 			break;
195 		case 'a':
196 			if ((maxcontig = atoi(optarg)) <= 0)
197 				errx(1, "%s: bad maximum contiguous blocks",
198 				    optarg);
199 			break;
200 		case 'b':
201 			if ((bsize = atoi(optarg)) < MINBSIZE)
202 				errx(1, "%s: block size too small, min is %d",
203 				    optarg, MINBSIZE);
204 			if (bsize > MAXBSIZE)
205 				errx(1, "%s: block size too large, max is %d",
206 				    optarg, MAXBSIZE);
207 			break;
208 		case 'c':
209 			if ((maxblkspercg = atoi(optarg)) <= 0)
210 				errx(1, "%s: bad blocks per cylinder group",
211 				    optarg);
212 			break;
213 		case 'd':
214 			if ((maxbsize = atoi(optarg)) < MINBSIZE)
215 				errx(1, "%s: bad extent block size", optarg);
216 			break;
217 		case 'e':
218 			if ((maxbpg = atoi(optarg)) <= 0)
219 			  errx(1, "%s: bad blocks per file in a cylinder group",
220 				    optarg);
221 			break;
222 		case 'f':
223 			if ((fsize = atoi(optarg)) <= 0)
224 				errx(1, "%s: bad fragment size", optarg);
225 			break;
226 		case 'g':
227 			if ((avgfilesize = atoi(optarg)) <= 0)
228 				errx(1, "%s: bad average file size", optarg);
229 			break;
230 		case 'h':
231 			if ((avgfilesperdir = atoi(optarg)) <= 0)
232 			       errx(1, "%s: bad average files per dir", optarg);
233 			break;
234 		case 'i':
235 			if ((density = atoi(optarg)) <= 0)
236 				errx(1, "%s: bad bytes per inode", optarg);
237 			break;
238 		case 'm':
239 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
240 				errx(1, "%s: bad free space %%", optarg);
241 			break;
242 		case 'o':
243 			if (strcmp(optarg, "space") == 0)
244 				opt = FS_OPTSPACE;
245 			else if (strcmp(optarg, "time") == 0)
246 				opt = FS_OPTTIME;
247 			else
248 				errx(1,
249 		"%s: unknown optimization preference: use `space' or `time'",
250 				    optarg);
251 			break;
252 		case 's':
253 			if ((fssize = atoi(optarg)) <= 0)
254 				errx(1, "%s: bad file system size", optarg);
255 			break;
256 		case '?':
257 		default:
258 			usage();
259 		}
260 	argc -= optind;
261 	argv += optind;
262 
263 	if (argc != 1)
264 		usage();
265 
266 	special = argv[0];
267 	cp = strrchr(special, '/');
268 	if (cp == 0) {
269 		/*
270 		 * No path prefix; try prefixing _PATH_DEV.
271 		 */
272 		snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
273 		special = device;
274 	}
275 
276 	if (ufs_disk_fillout_blank(&disk, special) == -1 ||
277 	    (!Nflag && ufs_disk_write(&disk) == -1)) {
278 		if (disk.d_error != NULL)
279 			errx(1, "%s: %s", special, disk.d_error);
280 		else
281 			err(1, "%s", special);
282 	}
283 	if (fstat(disk.d_fd, &st) < 0)
284 		err(1, "%s", special);
285 	if ((st.st_mode & S_IFMT) != S_IFCHR)
286 		errx(1, "%s: not a character-special device", special);
287 
288 	if (sectorsize == 0)
289 		ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize);
290 	if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) {
291 		if (fssize == 0)
292 			fssize = mediasize / sectorsize;
293 		else if (fssize > mediasize / sectorsize)
294 			errx(1, "%s: maximum file system size is %u",
295 			    special, (u_int)(mediasize / sectorsize));
296 	}
297 	pp = NULL;
298 	lp = getdisklabel(special);
299 	if (lp != NULL) {
300 		cp = strchr(special, '\0');
301 		cp--;
302 		if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))
303 			errx(1, "%s: can't figure out file system partition",
304 			    special);
305 		if (isdigit(*cp))
306 			pp = &lp->d_partitions[RAW_PART];
307 		else
308 			pp = &lp->d_partitions[*cp - 'a'];
309 		oldpartition = *pp;
310 		if (pp->p_size == 0)
311 			errx(1, "%s: `%c' partition is unavailable",
312 			    special, *cp);
313 		if (pp->p_fstype == FS_BOOT)
314 			errx(1, "%s: `%c' partition overlaps boot program",
315 			    special, *cp);
316 		if (fssize == 0)
317 			fssize = pp->p_size;
318 		if (fssize > pp->p_size)
319 			errx(1,
320 		    "%s: maximum file system size %d", special, pp->p_size);
321 		if (sectorsize == 0)
322 			sectorsize = lp->d_secsize;
323 		if (fsize == 0)
324 			fsize = pp->p_fsize;
325 		if (bsize == 0)
326 			bsize = pp->p_frag * pp->p_fsize;
327 	}
328 	if (sectorsize <= 0)
329 		errx(1, "%s: no default sector size", special);
330 	if (fsize <= 0)
331 		fsize = MAX(DFL_FRAGSIZE, sectorsize);
332 	if (bsize <= 0)
333 		bsize = MIN(DFL_BLKSIZE, 8 * fsize);
334 	if (maxbsize == 0)
335 		maxbsize = bsize;
336 	/*
337 	 * Maxcontig sets the default for the maximum number of blocks
338 	 * that may be allocated sequentially. With file system clustering
339 	 * it is possible to allocate contiguous blocks up to the maximum
340 	 * transfer size permitted by the controller or buffering.
341 	 */
342 	if (maxcontig == 0)
343 		maxcontig = MAX(1, MAXPHYS / bsize);
344 	if (density == 0)
345 		density = NFPI * fsize;
346 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
347 		fprintf(stderr, "Warning: changing optimization to space ");
348 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
349 		opt = FS_OPTSPACE;
350 	}
351 	if (maxbpg == 0)
352 		maxbpg = MAXBLKPG(bsize);
353 	realsectorsize = sectorsize;
354 	if (sectorsize != DEV_BSIZE) {		/* XXX */
355 		int secperblk = sectorsize / DEV_BSIZE;
356 
357 		sectorsize = DEV_BSIZE;
358 		fssize *= secperblk;
359 		if (pp != NULL)
360 			pp->p_size *= secperblk;
361 	}
362 	mkfs(pp, special);
363 	if (!unlabeled) {
364 		if (realsectorsize != DEV_BSIZE)
365 			pp->p_size /= realsectorsize / DEV_BSIZE;
366 		if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
367 			rewritelabel(special, lp);
368 	}
369 	ufs_disk_close(&disk);
370 	exit(0);
371 }
372 
373 struct disklabel *
374 getdisklabel(char *s)
375 {
376 	static struct disklabel lab;
377 	struct disklabel *lp;
378 
379 	if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab))
380 		return (&lab);
381 	unlabeled++;
382 	if (disktype) {
383 		lp = getdiskbyname(disktype);
384 		if (lp != NULL)
385 			return (lp);
386 	}
387 	return (NULL);
388 }
389 
390 void
391 rewritelabel(char *s, struct disklabel *lp)
392 {
393 	if (unlabeled)
394 		return;
395 	lp->d_checksum = 0;
396 	lp->d_checksum = dkcksum(lp);
397 	if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0)
398 		warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
399 }
400 
401 static void
402 usage()
403 {
404 	fprintf(stderr,
405 	    "usage: %s [ -fsoptions ] special-device%s\n",
406 	    getprogname(),
407 	    " [device-type]");
408 	fprintf(stderr, "where fsoptions are:\n");
409 	fprintf(stderr, "\t-L volume label to add to superblock\n");
410 	fprintf(stderr,
411 	    "\t-N do not create file system, just print out parameters\n");
412 	fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
413 	fprintf(stderr, "\t-R regression test, supress random factors\n");
414 	fprintf(stderr, "\t-S sector size\n");
415 	fprintf(stderr, "\t-T disktype\n");
416 	fprintf(stderr, "\t-U enable soft updates\n");
417 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
418 	fprintf(stderr, "\t-b block size\n");
419 	fprintf(stderr, "\t-c blocks per cylinders group\n");
420 	fprintf(stderr, "\t-d maximum extent size\n");
421 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
422 	fprintf(stderr, "\t-f frag size\n");
423 	fprintf(stderr, "\t-g average file size\n");
424 	fprintf(stderr, "\t-h average files per directory\n");
425 	fprintf(stderr, "\t-i number of bytes per inode\n");
426 	fprintf(stderr, "\t-m minimum free space %%\n");
427 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
428 	fprintf(stderr, "\t-s file systemsize (sectors)\n");
429 	exit(1);
430 }
431