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