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