xref: /original-bsd/sbin/newfs/newfs.c (revision 7b7c12b4)
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.33 (Berkeley) 05/03/93";
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 	if (mfs && disktype != NULL) {
348 		lp = (struct disklabel *)getdiskbyname(disktype);
349 		if (lp == NULL)
350 			fatal("%s: unknown disk type", disktype);
351 		pp = &lp->d_partitions[1];
352 	} else {
353 		fsi = open(special, O_RDONLY);
354 		if (fsi < 0)
355 			fatal("%s: %s", special, strerror(errno));
356 		if (fstat(fsi, &st) < 0)
357 			fatal("%s: %s", special, strerror(errno));
358 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
359 			printf("%s: %s: not a character-special device\n",
360 			    progname, special);
361 		cp = index(argv[0], '\0') - 1;
362 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
363 			fatal("%s: can't figure out file system partition",
364 			    argv[0]);
365 #ifdef COMPAT
366 		if (!mfs && disktype == NULL)
367 			disktype = argv[1];
368 #endif
369 		lp = getdisklabel(special, fsi);
370 		if (isdigit(*cp))
371 			pp = &lp->d_partitions[0];
372 		else
373 			pp = &lp->d_partitions[*cp - 'a'];
374 		if (pp->p_size == 0)
375 			fatal("%s: `%c' partition is unavailable",
376 			    argv[0], *cp);
377 		if (pp->p_fstype == FS_BOOT)
378 			fatal("%s: `%c' partition overlaps boot program",
379 			      argv[0], *cp);
380 	}
381 	if (fssize == 0)
382 		fssize = pp->p_size;
383 	if (fssize > pp->p_size && !mfs)
384 	       fatal("%s: maximum file system size on the `%c' partition is %d",
385 			argv[0], *cp, pp->p_size);
386 	if (rpm == 0) {
387 		rpm = lp->d_rpm;
388 		if (rpm <= 0)
389 			rpm = 3600;
390 	}
391 	if (ntracks == 0) {
392 		ntracks = lp->d_ntracks;
393 		if (ntracks <= 0)
394 			fatal("%s: no default #tracks", argv[0]);
395 	}
396 	if (nsectors == 0) {
397 		nsectors = lp->d_nsectors;
398 		if (nsectors <= 0)
399 			fatal("%s: no default #sectors/track", argv[0]);
400 	}
401 	if (sectorsize == 0) {
402 		sectorsize = lp->d_secsize;
403 		if (sectorsize <= 0)
404 			fatal("%s: no default sector size", argv[0]);
405 	}
406 	if (trackskew == -1) {
407 		trackskew = lp->d_trackskew;
408 		if (trackskew < 0)
409 			trackskew = 0;
410 	}
411 	if (interleave == 0) {
412 		interleave = lp->d_interleave;
413 		if (interleave <= 0)
414 			interleave = 1;
415 	}
416 	if (fsize == 0) {
417 		fsize = pp->p_fsize;
418 		if (fsize <= 0)
419 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
420 	}
421 	if (bsize == 0) {
422 		bsize = pp->p_frag * pp->p_fsize;
423 		if (bsize <= 0)
424 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
425 	}
426 	if (density == 0)
427 		density = NFPI * fsize;
428 	if (minfree < 10 && opt != FS_OPTSPACE) {
429 		fprintf(stderr, "Warning: changing optimization to space ");
430 		fprintf(stderr, "because minfree is less than 10%%\n");
431 		opt = FS_OPTSPACE;
432 	}
433 	if (trackspares == -1) {
434 		trackspares = lp->d_sparespertrack;
435 		if (trackspares < 0)
436 			trackspares = 0;
437 	}
438 	nphyssectors = nsectors + trackspares;
439 	if (cylspares == -1) {
440 		cylspares = lp->d_sparespercyl;
441 		if (cylspares < 0)
442 			cylspares = 0;
443 	}
444 	secpercyl = nsectors * ntracks - cylspares;
445 	if (secpercyl != lp->d_secpercyl)
446 		fprintf(stderr, "%s (%d) %s (%lu)\n",
447 			"Warning: calculated sectors per cylinder", secpercyl,
448 			"disagrees with disk label", lp->d_secpercyl);
449 	if (maxbpg == 0)
450 		maxbpg = MAXBLKPG(bsize);
451 	headswitch = lp->d_headswitch;
452 	trackseek = lp->d_trkseek;
453 #ifdef notdef /* label may be 0 if faked up by kernel */
454 	bbsize = lp->d_bbsize;
455 	sbsize = lp->d_sbsize;
456 #endif
457 	oldpartition = *pp;
458 #ifdef tahoe
459 	realsectorsize = sectorsize;
460 	if (sectorsize != DEV_BSIZE) {		/* XXX */
461 		int secperblk = DEV_BSIZE / sectorsize;
462 
463 		sectorsize = DEV_BSIZE;
464 		nsectors /= secperblk;
465 		nphyssectors /= secperblk;
466 		secpercyl /= secperblk;
467 		fssize /= secperblk;
468 		pp->p_size /= secperblk;
469 	}
470 #endif
471 	mkfs(pp, special, fsi, fso);
472 #ifdef tahoe
473 	if (realsectorsize != DEV_BSIZE)
474 		pp->p_size *= DEV_BSIZE / realsectorsize;
475 #endif
476 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
477 		rewritelabel(special, fso, lp);
478 	if (!Nflag)
479 		close(fso);
480 	close(fsi);
481 #ifdef MFS
482 	if (mfs) {
483 		struct mfs_args args;
484 
485 		sprintf(buf, "mfs:%d", getpid());
486 		args.name = buf;
487 		args.base = membase;
488 		args.size = fssize * sectorsize;
489 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
490 			fatal("%s: %s", argv[1], strerror(errno));
491 	}
492 #endif
493 	exit(0);
494 }
495 
496 #ifdef COMPAT
497 char lmsg[] = "%s: can't read disk label; disk type must be specified";
498 #else
499 char lmsg[] = "%s: can't read disk label";
500 #endif
501 
502 struct disklabel *
503 getdisklabel(s, fd)
504 	char *s;
505 	int fd;
506 {
507 	static struct disklabel lab;
508 
509 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
510 #ifdef COMPAT
511 		if (disktype) {
512 			struct disklabel *lp, *getdiskbyname();
513 
514 			unlabeled++;
515 			lp = getdiskbyname(disktype);
516 			if (lp == NULL)
517 				fatal("%s: unknown disk type", disktype);
518 			return (lp);
519 		}
520 #endif
521 		(void)fprintf(stderr,
522 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
523 		fatal(lmsg, s);
524 	}
525 	return (&lab);
526 }
527 
528 rewritelabel(s, fd, lp)
529 	char *s;
530 	int fd;
531 	register struct disklabel *lp;
532 {
533 #ifdef COMPAT
534 	if (unlabeled)
535 		return;
536 #endif
537 	lp->d_checksum = 0;
538 	lp->d_checksum = dkcksum(lp);
539 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
540 		(void)fprintf(stderr,
541 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
542 		fatal("%s: can't rewrite disk label", s);
543 	}
544 #if vax
545 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
546 		register i;
547 		int cfd;
548 		daddr_t alt;
549 		char specname[64];
550 		char blk[1024];
551 		char *cp;
552 
553 		/*
554 		 * Make name for 'c' partition.
555 		 */
556 		strcpy(specname, s);
557 		cp = specname + strlen(specname) - 1;
558 		if (!isdigit(*cp))
559 			*cp = 'c';
560 		cfd = open(specname, O_WRONLY);
561 		if (cfd < 0)
562 			fatal("%s: %s", specname, strerror(errno));
563 		bzero(blk, sizeof(blk));
564 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
565 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
566 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
567 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
568 			    L_SET) == -1)
569 				fatal("lseek to badsector area: %s",
570 				    strerror(errno));
571 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
572 				fprintf(stderr,
573 				    "%s: alternate label %d write: %s\n",
574 				    progname, i/2, strerror(errno));
575 		}
576 		close(cfd);
577 	}
578 #endif
579 }
580 
581 /*VARARGS*/
582 void
583 #if __STDC__
584 fatal(const char *fmt, ...)
585 #else
586 fatal(fmt, va_alist)
587 	char *fmt;
588 	va_dcl
589 #endif
590 {
591 	va_list ap;
592 
593 #if __STDC__
594 	va_start(ap, fmt);
595 #else
596 	va_start(ap);
597 #endif
598 	fprintf(stderr, "%s: ", progname);
599 	(void)vfprintf(stderr, fmt, ap);
600 	va_end(ap);
601 	putc('\n', stderr);
602 	exit(1);
603 }
604 
605 usage()
606 {
607 	if (mfs) {
608 		fprintf(stderr,
609 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
610 			progname);
611 	} else
612 		fprintf(stderr,
613 		    "usage: %s [ -fsoptions ] special-device%s\n",
614 		    progname,
615 #ifdef COMPAT
616 		    " [device-type]");
617 #else
618 		    "");
619 #endif
620 	fprintf(stderr, "where fsoptions are:\n");
621 	fprintf(stderr,
622 	    "\t-N do not create file system, just print out parameters\n");
623 	fprintf(stderr, "\t-S sector size\n");
624 #ifdef COMPAT
625 	fprintf(stderr, "\t-T disktype\n");
626 #endif
627 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
628 	fprintf(stderr, "\t-b block size\n");
629 	fprintf(stderr, "\t-c cylinders/group\n");
630 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
631 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
632 	fprintf(stderr, "\t-f frag size\n");
633 	fprintf(stderr, "\t-i number of bytes per inode\n");
634 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
635 	fprintf(stderr, "\t-l hardware sector interleave\n");
636 	fprintf(stderr, "\t-m minimum free space %%\n");
637 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
638 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
639 	fprintf(stderr, "\t-p spare sectors per track\n");
640 	fprintf(stderr, "\t-s file system size (sectors)\n");
641 	fprintf(stderr, "\t-r revolutions/minute\n");
642 	fprintf(stderr, "\t-t tracks/cylinder\n");
643 	fprintf(stderr, "\t-u sectors/track\n");
644 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
645 	exit(1);
646 }
647