xref: /original-bsd/sbin/newfs/newfs.c (revision b4971bb3)
1 /*
2  * Copyright (c) 1983, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)newfs.c	8.1 (Berkeley) 06/05/93";
10 #endif /* not lint */
11 
12 #ifndef lint
13 static char copyright[] =
14 "@(#) Copyright (c) 1983, 1989, 1993\n\
15 	The Regents of the University of California.  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 95% and 100% full; thus the default value of
76  * fs_minfree is 5%. With 5% free space, fragmentation is not a
77  * problem, so we choose to optimize for time.
78  */
79 #define MINFREE		5
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  * MAXBLKPG determines the maximum number of data blocks which are
92  * placed in a single cylinder group. The default is one indirect
93  * block worth of data blocks.
94  */
95 #define MAXBLKPG(bsize)	((bsize) / sizeof(daddr_t))
96 
97 /*
98  * Each file system has a number of inodes statically allocated.
99  * We allocate one inode slot per NFPI fragments, expecting this
100  * to be far more than we will ever need.
101  */
102 #define	NFPI		4
103 
104 /*
105  * For each cylinder we keep track of the availability of blocks at different
106  * rotational positions, so that we can lay out the data to be picked
107  * up with minimum rotational latency.  NRPOS is the default number of
108  * rotational positions that we distinguish.  With NRPOS of 8 the resolution
109  * of our summary information is 2ms for a typical 3600 rpm drive.
110  */
111 #define	NRPOS		8	/* number distinct rotational positions */
112 
113 
114 int	mfs;			/* run as the memory based filesystem */
115 int	Nflag;			/* run without writing file system */
116 int	Oflag;			/* format as an 4.3BSD file system */
117 int	fssize;			/* file system size */
118 int	ntracks;		/* # tracks/cylinder */
119 int	nsectors;		/* # sectors/track */
120 int	nphyssectors;		/* # sectors/track including spares */
121 int	secpercyl;		/* sectors per cylinder */
122 int	trackspares = -1;	/* spare sectors per track */
123 int	cylspares = -1;		/* spare sectors per cylinder */
124 int	sectorsize;		/* bytes/sector */
125 #ifdef tahoe
126 int	realsectorsize;		/* bytes/sector in hardware */
127 #endif
128 int	rpm;			/* revolutions/minute of drive */
129 int	interleave;		/* hardware sector interleave */
130 int	trackskew = -1;		/* sector 0 skew, per track */
131 int	headswitch;		/* head switch time, usec */
132 int	trackseek;		/* track-to-track seek, usec */
133 int	fsize = 0;		/* fragment size */
134 int	bsize = 0;		/* block size */
135 int	cpg = DESCPG;		/* cylinders/cylinder group */
136 int	cpgflg;			/* cylinders/cylinder group flag was given */
137 int	minfree = MINFREE;	/* free space threshold */
138 int	opt = DEFAULTOPT;	/* optimization preference (space or time) */
139 int	density;		/* number of bytes per inode */
140 int	maxcontig = 0;		/* max contiguous blocks to allocate */
141 int	rotdelay = ROTDELAY;	/* rotational delay between blocks */
142 int	maxbpg;			/* maximum blocks per file in a cyl group */
143 int	nrpos = NRPOS;		/* # of distinguished rotational positions */
144 int	bbsize = BBSIZE;	/* boot block size */
145 int	sbsize = SBSIZE;	/* superblock size */
146 int	mntflags;		/* flags to be passed to mount */
147 u_long	memleft;		/* virtual memory available */
148 caddr_t	membase;		/* start address of memory based filesystem */
149 #ifdef COMPAT
150 char	*disktype;
151 int	unlabeled;
152 #endif
153 
154 char	device[MAXPATHLEN];
155 char	*progname;
156 
157 main(argc, argv)
158 	int argc;
159 	char *argv[];
160 {
161 	extern char *optarg;
162 	extern int optind;
163 	register int ch;
164 	register struct partition *pp;
165 	register struct disklabel *lp;
166 	struct disklabel *getdisklabel();
167 	struct partition oldpartition;
168 	struct stat st;
169 	int fsi, fso;
170 	char *cp, *special, *opstring, buf[BUFSIZ];
171 	struct statfs *mp;
172 	char *s1, *s2;
173 	int len, n;
174 
175 	if (progname = rindex(*argv, '/'))
176 		++progname;
177 	else
178 		progname = *argv;
179 
180 	if (strstr(progname, "mfs")) {
181 		mfs = 1;
182 		Nflag++;
183 	}
184 
185 	opstring = "F:NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
186 	if (!mfs)
187 		opstring += 2;		/* -F is mfs only */
188 
189 	while ((ch = getopt(argc, argv, opstring)) != EOF)
190 		switch(ch) {
191 		case 'F':
192 			if ((mntflags = atoi(optarg)) == 0)
193 				fatal("%s: bad mount flags", optarg);
194 			break;
195 		case 'N':
196 			Nflag++;
197 			break;
198 		case 'O':
199 			Oflag++;
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 = -1;
316 	} else {
317 		fso = open(special, O_WRONLY);
318 		if (fso < 0)
319 			fatal("%s: %s", special, strerror(errno));
320 
321 		/* Bail if target special is mounted */
322 		n = getmntinfo(&mp, MNT_NOWAIT);
323 		if (n == 0)
324 			fatal("%s: getmntinfo: %s", special, strerror(errno));
325 
326 		len = sizeof(_PATH_DEV) - 1;
327 		s1 = special;
328 		if (strncmp(_PATH_DEV, s1, len) == 0)
329 			s1 += len;
330 
331 		while (--n >= 0) {
332 			s2 = mp->f_mntfromname;
333 			if (strncmp(_PATH_DEV, s2, len) == 0) {
334 				s2 += len - 1;
335 				*s2 = 'r';
336 			}
337 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
338 				fatal("%s is mounted on %s",
339 				    special, mp->f_mntonname);
340 			++mp;
341 		}
342 	}
343 	if (mfs && disktype != NULL) {
344 		lp = (struct disklabel *)getdiskbyname(disktype);
345 		if (lp == NULL)
346 			fatal("%s: unknown disk type", disktype);
347 		pp = &lp->d_partitions[1];
348 	} else {
349 		fsi = open(special, O_RDONLY);
350 		if (fsi < 0)
351 			fatal("%s: %s", special, strerror(errno));
352 		if (fstat(fsi, &st) < 0)
353 			fatal("%s: %s", special, strerror(errno));
354 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
355 			printf("%s: %s: not a character-special device\n",
356 			    progname, special);
357 		cp = index(argv[0], '\0') - 1;
358 		if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
359 			fatal("%s: can't figure out file system partition",
360 			    argv[0]);
361 #ifdef COMPAT
362 		if (!mfs && disktype == NULL)
363 			disktype = argv[1];
364 #endif
365 		lp = getdisklabel(special, fsi);
366 		if (isdigit(*cp))
367 			pp = &lp->d_partitions[0];
368 		else
369 			pp = &lp->d_partitions[*cp - 'a'];
370 		if (pp->p_size == 0)
371 			fatal("%s: `%c' partition is unavailable",
372 			    argv[0], *cp);
373 		if (pp->p_fstype == FS_BOOT)
374 			fatal("%s: `%c' partition overlaps boot program",
375 			      argv[0], *cp);
376 	}
377 	if (fssize == 0)
378 		fssize = pp->p_size;
379 	if (fssize > pp->p_size && !mfs)
380 	       fatal("%s: maximum file system size on the `%c' partition is %d",
381 			argv[0], *cp, pp->p_size);
382 	if (rpm == 0) {
383 		rpm = lp->d_rpm;
384 		if (rpm <= 0)
385 			rpm = 3600;
386 	}
387 	if (ntracks == 0) {
388 		ntracks = lp->d_ntracks;
389 		if (ntracks <= 0)
390 			fatal("%s: no default #tracks", argv[0]);
391 	}
392 	if (nsectors == 0) {
393 		nsectors = lp->d_nsectors;
394 		if (nsectors <= 0)
395 			fatal("%s: no default #sectors/track", argv[0]);
396 	}
397 	if (sectorsize == 0) {
398 		sectorsize = lp->d_secsize;
399 		if (sectorsize <= 0)
400 			fatal("%s: no default sector size", argv[0]);
401 	}
402 	if (trackskew == -1) {
403 		trackskew = lp->d_trackskew;
404 		if (trackskew < 0)
405 			trackskew = 0;
406 	}
407 	if (interleave == 0) {
408 		interleave = lp->d_interleave;
409 		if (interleave <= 0)
410 			interleave = 1;
411 	}
412 	if (fsize == 0) {
413 		fsize = pp->p_fsize;
414 		if (fsize <= 0)
415 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
416 	}
417 	if (bsize == 0) {
418 		bsize = pp->p_frag * pp->p_fsize;
419 		if (bsize <= 0)
420 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
421 	}
422 	/*
423 	 * Maxcontig sets the default for the maximum number of blocks
424 	 * that may be allocated sequentially. With filesystem clustering
425 	 * it is possible to allocate contiguous blocks up to the maximum
426 	 * transfer size permitted by the controller or buffering.
427 	 */
428 	if (maxcontig == 0)
429 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
430 	if (density == 0)
431 		density = NFPI * fsize;
432 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
433 		fprintf(stderr, "Warning: changing optimization to space ");
434 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
435 		opt = FS_OPTSPACE;
436 	}
437 	if (trackspares == -1) {
438 		trackspares = lp->d_sparespertrack;
439 		if (trackspares < 0)
440 			trackspares = 0;
441 	}
442 	nphyssectors = nsectors + trackspares;
443 	if (cylspares == -1) {
444 		cylspares = lp->d_sparespercyl;
445 		if (cylspares < 0)
446 			cylspares = 0;
447 	}
448 	secpercyl = nsectors * ntracks - cylspares;
449 	if (secpercyl != lp->d_secpercyl)
450 		fprintf(stderr, "%s (%d) %s (%lu)\n",
451 			"Warning: calculated sectors per cylinder", secpercyl,
452 			"disagrees with disk label", lp->d_secpercyl);
453 	if (maxbpg == 0)
454 		maxbpg = MAXBLKPG(bsize);
455 	headswitch = lp->d_headswitch;
456 	trackseek = lp->d_trkseek;
457 #ifdef notdef /* label may be 0 if faked up by kernel */
458 	bbsize = lp->d_bbsize;
459 	sbsize = lp->d_sbsize;
460 #endif
461 	oldpartition = *pp;
462 #ifdef tahoe
463 	realsectorsize = sectorsize;
464 	if (sectorsize != DEV_BSIZE) {		/* XXX */
465 		int secperblk = DEV_BSIZE / sectorsize;
466 
467 		sectorsize = DEV_BSIZE;
468 		nsectors /= secperblk;
469 		nphyssectors /= secperblk;
470 		secpercyl /= secperblk;
471 		fssize /= secperblk;
472 		pp->p_size /= secperblk;
473 	}
474 #endif
475 	mkfs(pp, special, fsi, fso);
476 #ifdef tahoe
477 	if (realsectorsize != DEV_BSIZE)
478 		pp->p_size *= DEV_BSIZE / realsectorsize;
479 #endif
480 	if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
481 		rewritelabel(special, fso, lp);
482 	if (!Nflag)
483 		close(fso);
484 	close(fsi);
485 #ifdef MFS
486 	if (mfs) {
487 		struct mfs_args args;
488 
489 		sprintf(buf, "mfs:%d", getpid());
490 		args.name = buf;
491 		args.base = membase;
492 		args.size = fssize * sectorsize;
493 		if (mount(MOUNT_MFS, argv[1], mntflags, &args) < 0)
494 			fatal("%s: %s", argv[1], strerror(errno));
495 	}
496 #endif
497 	exit(0);
498 }
499 
500 #ifdef COMPAT
501 char lmsg[] = "%s: can't read disk label; disk type must be specified";
502 #else
503 char lmsg[] = "%s: can't read disk label";
504 #endif
505 
506 struct disklabel *
507 getdisklabel(s, fd)
508 	char *s;
509 	int fd;
510 {
511 	static struct disklabel lab;
512 
513 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
514 #ifdef COMPAT
515 		if (disktype) {
516 			struct disklabel *lp, *getdiskbyname();
517 
518 			unlabeled++;
519 			lp = getdiskbyname(disktype);
520 			if (lp == NULL)
521 				fatal("%s: unknown disk type", disktype);
522 			return (lp);
523 		}
524 #endif
525 		(void)fprintf(stderr,
526 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
527 		fatal(lmsg, s);
528 	}
529 	return (&lab);
530 }
531 
532 rewritelabel(s, fd, lp)
533 	char *s;
534 	int fd;
535 	register struct disklabel *lp;
536 {
537 #ifdef COMPAT
538 	if (unlabeled)
539 		return;
540 #endif
541 	lp->d_checksum = 0;
542 	lp->d_checksum = dkcksum(lp);
543 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
544 		(void)fprintf(stderr,
545 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
546 		fatal("%s: can't rewrite disk label", s);
547 	}
548 #if vax
549 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
550 		register i;
551 		int cfd;
552 		daddr_t alt;
553 		char specname[64];
554 		char blk[1024];
555 		char *cp;
556 
557 		/*
558 		 * Make name for 'c' partition.
559 		 */
560 		strcpy(specname, s);
561 		cp = specname + strlen(specname) - 1;
562 		if (!isdigit(*cp))
563 			*cp = 'c';
564 		cfd = open(specname, O_WRONLY);
565 		if (cfd < 0)
566 			fatal("%s: %s", specname, strerror(errno));
567 		bzero(blk, sizeof(blk));
568 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
569 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
570 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
571 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
572 			    L_SET) == -1)
573 				fatal("lseek to badsector area: %s",
574 				    strerror(errno));
575 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
576 				fprintf(stderr,
577 				    "%s: alternate label %d write: %s\n",
578 				    progname, i/2, strerror(errno));
579 		}
580 		close(cfd);
581 	}
582 #endif
583 }
584 
585 /*VARARGS*/
586 void
587 #if __STDC__
588 fatal(const char *fmt, ...)
589 #else
590 fatal(fmt, va_alist)
591 	char *fmt;
592 	va_dcl
593 #endif
594 {
595 	va_list ap;
596 
597 #if __STDC__
598 	va_start(ap, fmt);
599 #else
600 	va_start(ap);
601 #endif
602 	fprintf(stderr, "%s: ", progname);
603 	(void)vfprintf(stderr, fmt, ap);
604 	va_end(ap);
605 	putc('\n', stderr);
606 	exit(1);
607 }
608 
609 usage()
610 {
611 	if (mfs) {
612 		fprintf(stderr,
613 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
614 			progname);
615 	} else
616 		fprintf(stderr,
617 		    "usage: %s [ -fsoptions ] special-device%s\n",
618 		    progname,
619 #ifdef COMPAT
620 		    " [device-type]");
621 #else
622 		    "");
623 #endif
624 	fprintf(stderr, "where fsoptions are:\n");
625 	fprintf(stderr,
626 	    "\t-N do not create file system, just print out parameters\n");
627 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
628 	fprintf(stderr, "\t-S sector size\n");
629 #ifdef COMPAT
630 	fprintf(stderr, "\t-T disktype\n");
631 #endif
632 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
633 	fprintf(stderr, "\t-b block size\n");
634 	fprintf(stderr, "\t-c cylinders/group\n");
635 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
636 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
637 	fprintf(stderr, "\t-f frag size\n");
638 	fprintf(stderr, "\t-i number of bytes per inode\n");
639 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
640 	fprintf(stderr, "\t-l hardware sector interleave\n");
641 	fprintf(stderr, "\t-m minimum free space %%\n");
642 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
643 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
644 	fprintf(stderr, "\t-p spare sectors per track\n");
645 	fprintf(stderr, "\t-s file system size (sectors)\n");
646 	fprintf(stderr, "\t-r revolutions/minute\n");
647 	fprintf(stderr, "\t-t tracks/cylinder\n");
648 	fprintf(stderr, "\t-u sectors/track\n");
649 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
650 	exit(1);
651 }
652