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