xref: /original-bsd/sbin/newfs/newfs.c (revision 1b11f3a3)
1 /*
2  * Copyright (c) 1983, 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)newfs.c	6.29 (Berkeley) 11/04/91";
10 #endif /* not lint */
11 
12 #ifndef lint
13 char copyright[] =
14 "@(#) Copyright (c) 1983, 1989 Regents of the University of California.\n\
15  All rights reserved.\n";
16 #endif /* not lint */
17 
18 /*
19  * newfs: friendly front end to mkfs
20  */
21 #include <sys/param.h>
22 #include <sys/stat.h>
23 #include <sys/ioctl.h>
24 #include <sys/disklabel.h>
25 #include <sys/file.h>
26 #include <sys/mount.h>
27 #include <ufs/ufs/dir.h>
28 #include <ufs/ffs/fs.h>
29 
30 #include <errno.h>
31 #if __STDC__
32 #include <stdarg.h>
33 #else
34 #include <varargs.h>
35 #endif
36 #include <stdio.h>
37 #include <ctype.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <paths.h>
41 
42 #if __STDC__
43 void	fatal(const char *fmt, ...);
44 #else
45 void	fatal();
46 #endif
47 
48 #define	COMPAT			/* allow non-labeled disks */
49 
50 /*
51  * The following two constants set the default block and fragment sizes.
52  * Both constants must be a power of 2 and meet the following constraints:
53  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
54  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
55  *	DESBLKSIZE / DESFRAGSIZE <= 8
56  */
57 #define	DFL_FRAGSIZE	1024
58 #define	DFL_BLKSIZE	8192
59 
60 /*
61  * Cylinder groups may have up to many cylinders. The actual
62  * number used depends upon how much information can be stored
63  * on a single cylinder. The default is to use 16 cylinders
64  * per group.
65  */
66 #define	DESCPG		16	/* desired fs_cpg */
67 
68 /*
69  * MINFREE gives the minimum acceptable percentage of file system
70  * blocks which may be free. If the freelist drops below this level
71  * only the superuser may continue to allocate blocks. This may
72  * be set to 0 if no reserve of free blocks is deemed necessary,
73  * however throughput drops by fifty percent if the file system
74  * is run at between 90% and 100% full; thus the default value of
75  * fs_minfree is 10%. With 10% free space, fragmentation is not a
76  * problem, so we choose to optimize for time.
77  */
78 #define MINFREE		10
79 #define DEFAULTOPT	FS_OPTTIME
80 
81 /*
82  * ROTDELAY gives the minimum number of milliseconds to initiate
83  * another disk transfer on the same cylinder. It is used in
84  * determining the rotationally optimal layout for disk blocks
85  * within a file; the default of fs_rotdelay is 4ms.
86  */
87 #define ROTDELAY	4
88 
89 /*
90  * MAXCONTIG sets the default for the maximum number of blocks
91  * that may be allocated sequentially. Since UNIX drivers are
92  * not capable of scheduling multi-block transfers, this defaults
93  * to 1 (ie no contiguous blocks are allocated).
94  */
95 #define MAXCONTIG	1
96 
97 /*
98  * MAXBLKPG determines the maximum number of data blocks which are
99  * placed in a single cylinder group. The default is one indirect
100  * block worth of data blocks.
101  */
102 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
103 
104 /*
105  * Each file system has a number of inodes statically allocated.
106  * We allocate one inode slot per NFPI fragments, expecting this
107  * to be far more than we will ever need.
108  */
109 #define	NFPI		4
110 
111 /*
112  * For each cylinder we keep track of the availability of blocks at different
113  * rotational positions, so that we can lay out the data to be picked
114  * up with minimum rotational latency.  NRPOS is the default number of
115  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
116  * of our summary information is 2ms for a typical 3600 rpm drive.
117  */
118 #define	NRPOS		8	/* number distinct rotational positions */
119 
120 
121 int	mfs;			/* run as the memory based filesystem */
122 int	Nflag;			/* run without writing file system */
123 int	fssize;			/* file system size */
124 int	ntracks;		/* # tracks/cylinder */
125 int	nsectors;		/* # sectors/track */
126 int	nphyssectors;		/* # sectors/track including spares */
127 int	secpercyl;		/* sectors per cylinder */
128 int	trackspares = -1;	/* spare sectors per track */
129 int	cylspares = -1;		/* spare sectors per cylinder */
130 int	sectorsize;		/* bytes/sector */
131 #ifdef tahoe
132 int	realsectorsize;		/* bytes/sector in hardware */
133 #endif
134 int	rpm;			/* revolutions/minute of drive */
135 int	interleave;		/* hardware sector interleave */
136 int	trackskew = -1;		/* sector 0 skew, per track */
137 int	headswitch;		/* head switch time, usec */
138 int	trackseek;		/* track-to-track seek, usec */
139 int	fsize = 0;		/* fragment size */
140 int	bsize = 0;		/* block size */
141 int	cpg = DESCPG;		/* cylinders/cylinder group */
142 int	cpgflg;			/* cylinders/cylinder group flag was given */
143 int	minfree = MINFREE;	/* free space threshold */
144 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
145 int	density;		/* number of bytes per inode */
146 int	maxcontig = MAXCONTIG;	/* max contiguous blocks to allocate */
147 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
148 int	maxbpg;			/* maximum blocks per file in a cyl group */
149 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
150 int	bbsize = BBSIZE;	/* boot block size */
151 int	sbsize = SBSIZE;	/* superblock size */
152 int	mntflags;		/* flags to be passed to mount */
153 u_long	memleft;		/* virtual memory available */
154 caddr_t	membase;		/* start address of memory based filesystem */
155 #ifdef COMPAT
156 char	*disktype;
157 int	unlabeled;
158 #endif
159 
160 char	device[MAXPATHLEN];
161 char	*progname;
162 
163 main(argc, argv)
164 	int argc;
165 	char *argv[];
166 {
167 	extern char *optarg;
168 	extern int optind;
169 	register int ch;
170 	register struct partition *pp;
171 	register struct disklabel *lp;
172 	struct disklabel *getdisklabel();
173 	struct partition oldpartition;
174 	struct stat st;
175 	int fsi, fso;
176 	char *cp, *special, *opstring, buf[BUFSIZ];
177 
178 	if (progname = rindex(*argv, '/'))
179 		++progname;
180 	else
181 		progname = *argv;
182 
183 	if (strstr(progname, "mfs")) {
184 		mfs = 1;
185 		Nflag++;
186 	}
187 
188 	opstring = "F:NS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
189 	if (!mfs)
190 		opstring += 2;		/* -F is mfs only */
191 
192 	while ((ch = getopt(argc, argv, opstring)) != EOF)
193 		switch(ch) {
194 		case 'F':
195 			if ((mntflags = atoi(optarg)) == 0)
196 				fatal("%s: bad mount flags", optarg);
197 			break;
198 		case 'N':
199 			Nflag++;
200 			break;
201 		case 'S':
202 			if ((sectorsize = atoi(optarg)) <= 0)
203 				fatal("%s: bad sector size", optarg);
204 			break;
205 #ifdef COMPAT
206 		case 'T':
207 			disktype = optarg;
208 			break;
209 #endif
210 		case 'a':
211 			if ((maxcontig = atoi(optarg)) <= 0)
212 				fatal("%s: bad max contiguous blocks\n",
213 				    optarg);
214 			break;
215 		case 'b':
216 			if ((bsize = atoi(optarg)) < MINBSIZE)
217 				fatal("%s: bad block size", optarg);
218 			break;
219 		case 'c':
220 			if ((cpg = atoi(optarg)) <= 0)
221 				fatal("%s: bad cylinders/group", optarg);
222 			cpgflg++;
223 			break;
224 		case 'd':
225 			if ((rotdelay = atoi(optarg)) < 0)
226 				fatal("%s: bad rotational delay\n", optarg);
227 			break;
228 		case 'e':
229 			if ((maxbpg = atoi(optarg)) <= 0)
230 				fatal("%s: bad blocks per file in a cyl group\n",
231 				    optarg);
232 			break;
233 		case 'f':
234 			if ((fsize = atoi(optarg)) <= 0)
235 				fatal("%s: bad frag size", optarg);
236 			break;
237 		case 'i':
238 			if ((density = atoi(optarg)) <= 0)
239 				fatal("%s: bad bytes per inode\n", optarg);
240 			break;
241 		case 'k':
242 			if ((trackskew = atoi(optarg)) < 0)
243 				fatal("%s: bad track skew", optarg);
244 			break;
245 		case 'l':
246 			if ((interleave = atoi(optarg)) <= 0)
247 				fatal("%s: bad interleave", optarg);
248 			break;
249 		case 'm':
250 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
251 				fatal("%s: bad free space %%\n", optarg);
252 			break;
253 		case 'n':
254 			if ((nrpos = atoi(optarg)) <= 0)
255 				fatal("%s: bad rotational layout count\n",
256 				    optarg);
257 			break;
258 		case 'o':
259 			if (strcmp(optarg, "space") == 0)
260 				opt = FS_OPTSPACE;
261 			else if (strcmp(optarg, "time") == 0)
262 				opt = FS_OPTTIME;
263 			else
264 				fatal("%s: bad optimization preference %s",
265 				    optarg, "(options are `space' or `time')");
266 			break;
267 		case 'p':
268 			if ((trackspares = atoi(optarg)) < 0)
269 				fatal("%s: bad spare sectors per track",
270 				    optarg);
271 			break;
272 		case 'r':
273 			if ((rpm = atoi(optarg)) <= 0)
274 				fatal("%s: bad revs/minute\n", optarg);
275 			break;
276 		case 's':
277 			if ((fssize = atoi(optarg)) <= 0)
278 				fatal("%s: bad file system size", optarg);
279 			break;
280 		case 't':
281 			if ((ntracks = atoi(optarg)) <= 0)
282 				fatal("%s: bad total tracks", optarg);
283 			break;
284 		case 'u':
285 			if ((nsectors = atoi(optarg)) <= 0)
286 				fatal("%s: bad sectors/track", optarg);
287 			break;
288 		case 'x':
289 			if ((cylspares = atoi(optarg)) < 0)
290 				fatal("%s: bad spare sectors per cylinder",
291 				    optarg);
292 			break;
293 		case '?':
294 		default:
295 			usage();
296 		}
297 	argc -= optind;
298 	argv += optind;
299 
300 	if (argc != 2 && (mfs || argc != 1))
301 		usage();
302 
303 	special = argv[0];
304 	cp = rindex(special, '/');
305 	if (cp == 0) {
306 		/*
307 		 * No path prefix; try /dev/r%s then /dev/%s.
308 		 */
309 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
310 		if (stat(device, &st) == -1)
311 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
312 		special = device;
313 	}
314 	if (!Nflag) {
315 		fso = open(special, O_WRONLY);
316 		if (fso < 0)
317 			fatal("%s: %s", special, strerror(errno));
318 	} else
319 		fso = -1;
320 	fsi = open(special, O_RDONLY);
321 	if (fsi < 0)
322 		fatal("%s: %s", special, strerror(errno));
323 	if (fstat(fsi, &st) < 0)
324 		fatal("%s: %s", special, strerror(errno));
325 	if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
326 		printf("%s: %s: not a character-special device\n",
327 		    progname, special);
328 	cp = index(argv[0], '\0') - 1;
329 	if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
330 		fatal("%s: can't figure out file system partition", argv[0]);
331 #ifdef COMPAT
332 	if (!mfs && disktype == NULL)
333 		disktype = argv[1];
334 #endif
335 	lp = getdisklabel(special, fsi);
336 	if (isdigit(*cp))
337 		pp = &lp->d_partitions[0];
338 	else
339 		pp = &lp->d_partitions[*cp - 'a'];
340 	if (pp->p_size == 0)
341 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
342 	if (fssize == 0)
343 		fssize = pp->p_size;
344 	if (fssize > pp->p_size && !mfs)
345 	       fatal("%s: maximum file system size on the `%c' partition is %d",
346 			argv[0], *cp, pp->p_size);
347 	if (rpm == 0) {
348 		rpm = lp->d_rpm;
349 		if (rpm <= 0)
350 			rpm = 3600;
351 	}
352 	if (ntracks == 0) {
353 		ntracks = lp->d_ntracks;
354 		if (ntracks <= 0)
355 			fatal("%s: no default #tracks", argv[0]);
356 	}
357 	if (nsectors == 0) {
358 		nsectors = lp->d_nsectors;
359 		if (nsectors <= 0)
360 			fatal("%s: no default #sectors/track", argv[0]);
361 	}
362 	if (sectorsize == 0) {
363 		sectorsize = lp->d_secsize;
364 		if (sectorsize <= 0)
365 			fatal("%s: no default sector size", argv[0]);
366 	}
367 	if (trackskew == -1) {
368 		trackskew = lp->d_trackskew;
369 		if (trackskew < 0)
370 			trackskew = 0;
371 	}
372 	if (interleave == 0) {
373 		interleave = lp->d_interleave;
374 		if (interleave <= 0)
375 			interleave = 1;
376 	}
377 	if (fsize == 0) {
378 		fsize = pp->p_fsize;
379 		if (fsize <= 0)
380 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
381 	}
382 	if (bsize == 0) {
383 		bsize = pp->p_frag * pp->p_fsize;
384 		if (bsize <= 0)
385 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
386 	}
387 	if (density == 0)
388 		density = NFPI * fsize;
389 	if (minfree < 10 && opt != FS_OPTSPACE) {
390 		fprintf(stderr, "Warning: changing optimization to space ");
391 		fprintf(stderr, "because minfree is less than 10%%\n");
392 		opt = FS_OPTSPACE;
393 	}
394 	if (trackspares == -1) {
395 		trackspares = lp->d_sparespertrack;
396 		if (trackspares < 0)
397 			trackspares = 0;
398 	}
399 	nphyssectors = nsectors + trackspares;
400 	if (cylspares == -1) {
401 		cylspares = lp->d_sparespercyl;
402 		if (cylspares < 0)
403 			cylspares = 0;
404 	}
405 	secpercyl = nsectors * ntracks - cylspares;
406 	if (secpercyl != lp->d_secpercyl)
407 		fprintf(stderr, "%s (%d) %s (%lu)\n",
408 			"Warning: calculated sectors per cylinder", secpercyl,
409 			"disagrees with disk label", lp->d_secpercyl);
410 	if (maxbpg == 0)
411 		maxbpg = MAXBLKPG(bsize);
412 	headswitch = lp->d_headswitch;
413 	trackseek = lp->d_trkseek;
414 #ifdef notdef /* label may be 0 if faked up by kernel */
415 	bbsize = lp->d_bbsize;
416 	sbsize = lp->d_sbsize;
417 #endif
418 	oldpartition = *pp;
419 #ifdef tahoe
420 	realsectorsize = sectorsize;
421 	if (sectorsize != DEV_BSIZE) {		/* XXX */
422 		int secperblk = DEV_BSIZE / sectorsize;
423 
424 		sectorsize = DEV_BSIZE;
425 		nsectors /= secperblk;
426 		nphyssectors /= secperblk;
427 		secpercyl /= secperblk;
428 		fssize /= secperblk;
429 		pp->p_size /= secperblk;
430 	}
431 #endif
432 	mkfs(pp, special, fsi, fso);
433 #ifdef tahoe
434 	if (realsectorsize != DEV_BSIZE)
435 		pp->p_size *= DEV_BSIZE / realsectorsize;
436 #endif
437 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
438 		rewritelabel(special, fso, lp);
439 	if (!Nflag)
440 		close(fso);
441 	close(fsi);
442 #ifdef MFS
443 	if (mfs) {
444 		struct mfs_args args;
445 
446 		sprintf(buf, "mfs:%d", getpid());
447 		args.name = buf;
448 		args.base = membase;
449 		args.size = fssize * sectorsize;
450 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
451 			fatal("%s: %s", argv[1], strerror(errno));
452 	}
453 #endif
454 	exit(0);
455 }
456 
457 #ifdef COMPAT
458 char lmsg[] = "%s: can't read disk label; disk type must be specified";
459 #else
460 char lmsg[] = "%s: can't read disk label";
461 #endif
462 
463 struct disklabel *
464 getdisklabel(s, fd)
465 	char *s;
466 	int fd;
467 {
468 	static struct disklabel lab;
469 
470 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
471 #ifdef COMPAT
472 		if (disktype) {
473 			struct disklabel *lp, *getdiskbyname();
474 
475 			unlabeled++;
476 			lp = getdiskbyname(disktype);
477 			if (lp == NULL)
478 				fatal("%s: unknown disk type", disktype);
479 			return (lp);
480 		}
481 #endif
482 		(void)fprintf(stderr,
483 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
484 		fatal(lmsg, s);
485 	}
486 	return (&lab);
487 }
488 
489 rewritelabel(s, fd, lp)
490 	char *s;
491 	int fd;
492 	register struct disklabel *lp;
493 {
494 #ifdef COMPAT
495 	if (unlabeled)
496 		return;
497 #endif
498 	lp->d_checksum = 0;
499 	lp->d_checksum = dkcksum(lp);
500 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
501 		(void)fprintf(stderr,
502 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
503 		fatal("%s: can't rewrite disk label", s);
504 	}
505 #if vax
506 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
507 		register i;
508 		int cfd;
509 		daddr_t alt;
510 		char specname[64];
511 		char blk[1024];
512 		char *cp;
513 
514 		/*
515 		 * Make name for 'c' partition.
516 		 */
517 		strcpy(specname, s);
518 		cp = specname + strlen(specname) - 1;
519 		if (!isdigit(*cp))
520 			*cp = 'c';
521 		cfd = open(specname, O_WRONLY);
522 		if (cfd < 0)
523 			fatal("%s: %s", specname, strerror(errno));
524 		bzero(blk, sizeof(blk));
525 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
526 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
527 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
528 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
529 			    L_SET) == -1)
530 				fatal("lseek to badsector area: %s",
531 				    strerror(errno));
532 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
533 				fprintf(stderr,
534 				    "%s: alternate label %d write: %s\n",
535 				    progname, i/2, strerror(errno));
536 		}
537 		close(cfd);
538 	}
539 #endif
540 }
541 
542 /*VARARGS*/
543 void
544 #if __STDC__
545 fatal(const char *fmt, ...)
546 #else
547 fatal(fmt, va_alist)
548 	char *fmt;
549 	va_dcl
550 #endif
551 {
552 	va_list ap;
553 
554 #if __STDC__
555 	va_start(ap, fmt);
556 #else
557 	va_start(ap);
558 #endif
559 	fprintf(stderr, "%s: ", progname);
560 	(void)vfprintf(stderr, fmt, ap);
561 	va_end(ap);
562 	putc('\n', stderr);
563 	exit(1);
564 }
565 
566 usage()
567 {
568 	if (mfs) {
569 		fprintf(stderr,
570 		    "usage: mfs [ -fsoptions ] special-device mount-point\n");
571 	} else
572 		fprintf(stderr,
573 		    "usage: newfs [ -fsoptions ] special-device%s\n",
574 #ifdef COMPAT
575 		    " [device-type]");
576 #else
577 		    "");
578 #endif
579 	fprintf(stderr, "where fsoptions are:\n");
580 	fprintf(stderr,
581 	    "\t-N do not create file system, just print out parameters\n");
582 	fprintf(stderr, "\t-S sector size\n");
583 #ifdef COMPAT
584 	fprintf(stderr, "\t-T disktype\n");
585 #endif
586 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
587 	fprintf(stderr, "\t-b block size\n");
588 	fprintf(stderr, "\t-c cylinders/group\n");
589 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
590 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
591 	fprintf(stderr, "\t-f frag size\n");
592 	fprintf(stderr, "\t-i number of bytes per inode\n");
593 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
594 	fprintf(stderr, "\t-l hardware sector interleave\n");
595 	fprintf(stderr, "\t-m minimum free space %%\n");
596 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
597 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
598 	fprintf(stderr, "\t-p spare sectors per track\n");
599 	fprintf(stderr, "\t-s file system size (sectors)\n");
600 	fprintf(stderr, "\t-r revolutions/minute\n");
601 	fprintf(stderr, "\t-t tracks/cylinder\n");
602 	fprintf(stderr, "\t-u sectors/track\n");
603 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
604 	exit(1);
605 }
606