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