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