xref: /original-bsd/sbin/newfs/newfs.c (revision 0842ddeb)
1 /*
2  * Copyright (c) 1983, 1989, 1993, 1994
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.13 (Berkeley) 05/01/95";
10 #endif /* not lint */
11 
12 #ifndef lint
13 static char copyright[] =
14 "@(#) Copyright (c) 1983, 1989, 1993, 1994\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 
28 #include <ufs/ufs/dir.h>
29 #include <ufs/ufs/dinode.h>
30 #include <ufs/ffs/fs.h>
31 #include <ufs/ufs/ufsmount.h>
32 
33 #include <ctype.h>
34 #include <errno.h>
35 #include <paths.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <syslog.h>
40 #include <unistd.h>
41 
42 #if __STDC__
43 #include <stdarg.h>
44 #else
45 #include <varargs.h>
46 #endif
47 
48 #include "mntopts.h"
49 
50 struct mntopt mopts[] = {
51 	MOPT_STDOPTS,
52 	MOPT_ASYNC,
53 	{ NULL },
54 };
55 
56 #if __STDC__
57 void	fatal(const char *fmt, ...);
58 #else
59 void	fatal();
60 #endif
61 
62 #define	COMPAT			/* allow non-labeled disks */
63 
64 /*
65  * The following two constants set the default block and fragment sizes.
66  * Both constants must be a power of 2 and meet the following constraints:
67  *	MINBSIZE <= DESBLKSIZE <= MAXBSIZE
68  *	sectorsize <= DESFRAGSIZE <= DESBLKSIZE
69  *	DESBLKSIZE / DESFRAGSIZE <= 8
70  */
71 #define	DFL_FRAGSIZE	1024
72 #define	DFL_BLKSIZE	8192
73 
74 /*
75  * Cylinder groups may have up to many cylinders. The actual
76  * number used depends upon how much information can be stored
77  * on a single cylinder. The default is to use 16 cylinders
78  * per group.
79  */
80 #define	DESCPG		16	/* desired fs_cpg */
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 = MNT_ASYNC;	/* 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 int
158 main(argc, argv)
159 	int argc;
160 	char *argv[];
161 {
162 	extern char *optarg;
163 	extern int optind;
164 	register int ch;
165 	register struct partition *pp;
166 	register struct disklabel *lp;
167 	struct disklabel *getdisklabel();
168 	struct partition oldpartition;
169 	struct stat st;
170 	struct statfs *mp;
171 	int fsi, fso, len, n;
172 	char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
173 
174 	if (progname = strrchr(*argv, '/'))
175 		++progname;
176 	else
177 		progname = *argv;
178 
179 	if (strstr(progname, "mfs")) {
180 		mfs = 1;
181 		Nflag++;
182 	}
183 
184 	opstring = mfs ?
185 	    "NT:a:b:c:d:e:f:i:m:o:s:" :
186 	    "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
187 	while ((ch = getopt(argc, argv, opstring)) != EOF)
188 		switch (ch) {
189 		case 'N':
190 			Nflag = 1;
191 			break;
192 		case 'O':
193 			Oflag = 1;
194 			break;
195 		case 'S':
196 			if ((sectorsize = atoi(optarg)) <= 0)
197 				fatal("%s: bad sector size", optarg);
198 			break;
199 #ifdef COMPAT
200 		case 'T':
201 			disktype = optarg;
202 			break;
203 #endif
204 		case 'a':
205 			if ((maxcontig = atoi(optarg)) <= 0)
206 				fatal("%s: bad maximum contiguous blocks\n",
207 				    optarg);
208 			break;
209 		case 'b':
210 			if ((bsize = atoi(optarg)) < MINBSIZE)
211 				fatal("%s: bad block size", optarg);
212 			break;
213 		case 'c':
214 			if ((cpg = atoi(optarg)) <= 0)
215 				fatal("%s: bad cylinders/group", optarg);
216 			cpgflg++;
217 			break;
218 		case 'd':
219 			if ((rotdelay = atoi(optarg)) < 0)
220 				fatal("%s: bad rotational delay\n", optarg);
221 			break;
222 		case 'e':
223 			if ((maxbpg = atoi(optarg)) <= 0)
224 		fatal("%s: bad blocks per file in a cylinder group\n",
225 				    optarg);
226 			break;
227 		case 'f':
228 			if ((fsize = atoi(optarg)) <= 0)
229 				fatal("%s: bad fragment size", optarg);
230 			break;
231 		case 'i':
232 			if ((density = atoi(optarg)) <= 0)
233 				fatal("%s: bad bytes per inode\n", optarg);
234 			break;
235 		case 'k':
236 			if ((trackskew = atoi(optarg)) < 0)
237 				fatal("%s: bad track skew", optarg);
238 			break;
239 		case 'l':
240 			if ((interleave = atoi(optarg)) <= 0)
241 				fatal("%s: bad interleave", optarg);
242 			break;
243 		case 'm':
244 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
245 				fatal("%s: bad free space %%\n", optarg);
246 			break;
247 		case 'n':
248 			if ((nrpos = atoi(optarg)) <= 0)
249 				fatal("%s: bad rotational layout count\n",
250 				    optarg);
251 			break;
252 		case 'o':
253 			if (mfs)
254 				getmntopts(optarg, mopts, &mntflags, 0);
255 			else {
256 				if (strcmp(optarg, "space") == 0)
257 					opt = FS_OPTSPACE;
258 				else if (strcmp(optarg, "time") == 0)
259 					opt = FS_OPTTIME;
260 				else
261 	fatal("%s: unknown optimization preference: use `space' or `time'.");
262 			}
263 			break;
264 		case 'p':
265 			if ((trackspares = atoi(optarg)) < 0)
266 				fatal("%s: bad spare sectors per track",
267 				    optarg);
268 			break;
269 		case 'r':
270 			if ((rpm = atoi(optarg)) <= 0)
271 				fatal("%s: bad revolutions/minute\n", optarg);
272 			break;
273 		case 's':
274 			if ((fssize = atoi(optarg)) <= 0)
275 				fatal("%s: bad file system size", optarg);
276 			break;
277 		case 't':
278 			if ((ntracks = atoi(optarg)) <= 0)
279 				fatal("%s: bad total tracks", optarg);
280 			break;
281 		case 'u':
282 			if ((nsectors = atoi(optarg)) <= 0)
283 				fatal("%s: bad sectors/track", optarg);
284 			break;
285 		case 'x':
286 			if ((cylspares = atoi(optarg)) < 0)
287 				fatal("%s: bad spare sectors per cylinder",
288 				    optarg);
289 			break;
290 		case '?':
291 		default:
292 			usage();
293 		}
294 	argc -= optind;
295 	argv += optind;
296 
297 	if (argc != 2 && (mfs || argc != 1))
298 		usage();
299 
300 	special = argv[0];
301 	cp = strrchr(special, '/');
302 	if (cp == 0) {
303 		/*
304 		 * No path prefix; try /dev/r%s then /dev/%s.
305 		 */
306 		(void)sprintf(device, "%sr%s", _PATH_DEV, special);
307 		if (stat(device, &st) == -1)
308 			(void)sprintf(device, "%s%s", _PATH_DEV, special);
309 		special = device;
310 	}
311 	if (Nflag) {
312 		fso = -1;
313 	} else {
314 		fso = open(special, O_WRONLY);
315 		if (fso < 0)
316 			fatal("%s: %s", special, strerror(errno));
317 
318 		/* Bail if target special is mounted */
319 		n = getmntinfo(&mp, MNT_NOWAIT);
320 		if (n == 0)
321 			fatal("%s: getmntinfo: %s", special, strerror(errno));
322 
323 		len = sizeof(_PATH_DEV) - 1;
324 		s1 = special;
325 		if (strncmp(_PATH_DEV, s1, len) == 0)
326 			s1 += len;
327 
328 		while (--n >= 0) {
329 			s2 = mp->f_mntfromname;
330 			if (strncmp(_PATH_DEV, s2, len) == 0) {
331 				s2 += len - 1;
332 				*s2 = 'r';
333 			}
334 			if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
335 				fatal("%s is mounted on %s",
336 				    special, mp->f_mntonname);
337 			++mp;
338 		}
339 	}
340 	if (mfs && disktype != NULL) {
341 		lp = (struct disklabel *)getdiskbyname(disktype);
342 		if (lp == NULL)
343 			fatal("%s: unknown disk type", disktype);
344 		pp = &lp->d_partitions[1];
345 	} else {
346 		fsi = open(special, O_RDONLY);
347 		if (fsi < 0)
348 			fatal("%s: %s", special, strerror(errno));
349 		if (fstat(fsi, &st) < 0)
350 			fatal("%s: %s", special, strerror(errno));
351 		if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
352 			printf("%s: %s: not a character-special device\n",
353 			    progname, special);
354 		cp = strchr(argv[0], '\0') - 1;
355 		if (cp == (char *)-1 ||
356 		    (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
357 			fatal("%s: can't figure out file system partition",
358 			    argv[0]);
359 #ifdef COMPAT
360 		if (!mfs && disktype == NULL)
361 			disktype = argv[1];
362 #endif
363 		lp = getdisklabel(special, fsi);
364 		if (isdigit(*cp))
365 			pp = &lp->d_partitions[0];
366 		else
367 			pp = &lp->d_partitions[*cp - 'a'];
368 		if (pp->p_size == 0)
369 			fatal("%s: `%c' partition is unavailable",
370 			    argv[0], *cp);
371 		if (pp->p_fstype == FS_BOOT)
372 			fatal("%s: `%c' partition overlaps boot program",
373 			      argv[0], *cp);
374 	}
375 	if (fssize == 0)
376 		fssize = pp->p_size;
377 	if (fssize > pp->p_size && !mfs)
378 	       fatal("%s: maximum file system size on the `%c' partition is %d",
379 			argv[0], *cp, pp->p_size);
380 	if (rpm == 0) {
381 		rpm = lp->d_rpm;
382 		if (rpm <= 0)
383 			rpm = 3600;
384 	}
385 	if (ntracks == 0) {
386 		ntracks = lp->d_ntracks;
387 		if (ntracks <= 0)
388 			fatal("%s: no default #tracks", argv[0]);
389 	}
390 	if (nsectors == 0) {
391 		nsectors = lp->d_nsectors;
392 		if (nsectors <= 0)
393 			fatal("%s: no default #sectors/track", argv[0]);
394 	}
395 	if (sectorsize == 0) {
396 		sectorsize = lp->d_secsize;
397 		if (sectorsize <= 0)
398 			fatal("%s: no default sector size", argv[0]);
399 	}
400 	if (trackskew == -1) {
401 		trackskew = lp->d_trackskew;
402 		if (trackskew < 0)
403 			trackskew = 0;
404 	}
405 	if (interleave == 0) {
406 		interleave = lp->d_interleave;
407 		if (interleave <= 0)
408 			interleave = 1;
409 	}
410 	if (fsize == 0) {
411 		fsize = pp->p_fsize;
412 		if (fsize <= 0)
413 			fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
414 	}
415 	if (bsize == 0) {
416 		bsize = pp->p_frag * pp->p_fsize;
417 		if (bsize <= 0)
418 			bsize = MIN(DFL_BLKSIZE, 8 * fsize);
419 	}
420 	/*
421 	 * Maxcontig sets the default for the maximum number of blocks
422 	 * that may be allocated sequentially. With filesystem clustering
423 	 * it is possible to allocate contiguous blocks up to the maximum
424 	 * transfer size permitted by the controller or buffering.
425 	 */
426 	if (maxcontig == 0)
427 		maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
428 	if (density == 0)
429 		density = NFPI * fsize;
430 	if (minfree < MINFREE && opt != FS_OPTSPACE) {
431 		fprintf(stderr, "Warning: changing optimization to space ");
432 		fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
433 		opt = FS_OPTSPACE;
434 	}
435 	if (trackspares == -1) {
436 		trackspares = lp->d_sparespertrack;
437 		if (trackspares < 0)
438 			trackspares = 0;
439 	}
440 	nphyssectors = nsectors + trackspares;
441 	if (cylspares == -1) {
442 		cylspares = lp->d_sparespercyl;
443 		if (cylspares < 0)
444 			cylspares = 0;
445 	}
446 	secpercyl = nsectors * ntracks - cylspares;
447 	if (secpercyl != lp->d_secpercyl)
448 		fprintf(stderr, "%s (%d) %s (%lu)\n",
449 			"Warning: calculated sectors per cylinder", secpercyl,
450 			"disagrees with disk label", lp->d_secpercyl);
451 	if (maxbpg == 0)
452 		maxbpg = MAXBLKPG(bsize);
453 	headswitch = lp->d_headswitch;
454 	trackseek = lp->d_trkseek;
455 #ifdef notdef /* label may be 0 if faked up by kernel */
456 	bbsize = lp->d_bbsize;
457 	sbsize = lp->d_sbsize;
458 #endif
459 	oldpartition = *pp;
460 #ifdef tahoe
461 	realsectorsize = sectorsize;
462 	if (sectorsize != DEV_BSIZE) {		/* XXX */
463 		int secperblk = DEV_BSIZE / sectorsize;
464 
465 		sectorsize = DEV_BSIZE;
466 		nsectors /= secperblk;
467 		nphyssectors /= secperblk;
468 		secpercyl /= secperblk;
469 		fssize /= secperblk;
470 		pp->p_size /= secperblk;
471 	}
472 #endif
473 	mkfs(pp, special, fsi, fso);
474 #ifdef tahoe
475 	if (realsectorsize != DEV_BSIZE)
476 		pp->p_size *= DEV_BSIZE / realsectorsize;
477 #endif
478 	if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition)))
479 		rewritelabel(special, fso, lp);
480 	if (!Nflag)
481 		close(fso);
482 	close(fsi);
483 #ifdef MFS
484 	if (mfs) {
485 		struct mfs_args args;
486 
487 		sprintf(buf, "mfs:%d", getpid());
488 		args.fspec = buf;
489 		args.export.ex_root = -2;
490 		if (mntflags & MNT_RDONLY)
491 			args.export.ex_flags = MNT_EXRDONLY;
492 		else
493 			args.export.ex_flags = 0;
494 		args.base = membase;
495 		args.size = fssize * sectorsize;
496 		if (mount("mfs", argv[1], mntflags, &args) < 0)
497 			fatal("%s: %s", argv[1], strerror(errno));
498 	}
499 #endif
500 	exit(0);
501 }
502 
503 #ifdef COMPAT
504 char lmsg[] = "%s: can't read disk label; disk type must be specified";
505 #else
506 char lmsg[] = "%s: can't read disk label";
507 #endif
508 
509 struct disklabel *
510 getdisklabel(s, fd)
511 	char *s;
512 	int fd;
513 {
514 	static struct disklabel lab;
515 
516 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
517 #ifdef COMPAT
518 		if (disktype) {
519 			struct disklabel *lp, *getdiskbyname();
520 
521 			unlabeled++;
522 			lp = getdiskbyname(disktype);
523 			if (lp == NULL)
524 				fatal("%s: unknown disk type", disktype);
525 			return (lp);
526 		}
527 #endif
528 		warn("ioctl (GDINFO)");
529 		fatal(lmsg, s);
530 	}
531 	return (&lab);
532 }
533 
534 rewritelabel(s, fd, lp)
535 	char *s;
536 	int fd;
537 	register struct disklabel *lp;
538 {
539 #ifdef COMPAT
540 	if (unlabeled)
541 		return;
542 #endif
543 	lp->d_checksum = 0;
544 	lp->d_checksum = dkcksum(lp);
545 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
546 		warn("ioctl (WDINFO)");
547 		fatal("%s: can't rewrite disk label", s);
548 	}
549 #if vax
550 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
551 		register i;
552 		int cfd;
553 		daddr_t alt;
554 		char specname[64];
555 		char blk[1024];
556 		char *cp;
557 
558 		/*
559 		 * Make name for 'c' partition.
560 		 */
561 		strcpy(specname, s);
562 		cp = specname + strlen(specname) - 1;
563 		if (!isdigit(*cp))
564 			*cp = 'c';
565 		cfd = open(specname, O_WRONLY);
566 		if (cfd < 0)
567 			fatal("%s: %s", specname, strerror(errno));
568 		memset(blk, 0, sizeof(blk));
569 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
570 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
571 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
572 			if (lseek(cfd, (off_t)(alt + i) * lp->d_secsize,
573 			    L_SET) == -1)
574 				fatal("lseek to badsector area: %s",
575 				    strerror(errno));
576 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
577 				warn("alternate label %d write", i/2);
578 		}
579 		close(cfd);
580 	}
581 #endif
582 }
583 
584 /*VARARGS*/
585 void
586 #if __STDC__
587 fatal(const char *fmt, ...)
588 #else
589 fatal(fmt, va_alist)
590 	char *fmt;
591 	va_dcl
592 #endif
593 {
594 	va_list ap;
595 
596 #if __STDC__
597 	va_start(ap, fmt);
598 #else
599 	va_start(ap);
600 #endif
601 	if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
602 		openlog(progname, LOG_CONS, LOG_DAEMON);
603 		vsyslog(LOG_ERR, fmt, ap);
604 		closelog();
605 	} else {
606 		vwarnx(fmt, ap);
607 	}
608 	va_end(ap);
609 	exit(1);
610 	/*NOTREACHED*/
611 }
612 
613 usage()
614 {
615 	if (mfs) {
616 		fprintf(stderr,
617 		    "usage: %s [ -fsoptions ] special-device mount-point\n",
618 			progname);
619 	} else
620 		fprintf(stderr,
621 		    "usage: %s [ -fsoptions ] special-device%s\n",
622 		    progname,
623 #ifdef COMPAT
624 		    " [device-type]");
625 #else
626 		    "");
627 #endif
628 	fprintf(stderr, "where fsoptions are:\n");
629 	fprintf(stderr,
630 	    "\t-N do not create file system, just print out parameters\n");
631 	fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
632 	fprintf(stderr, "\t-S sector size\n");
633 #ifdef COMPAT
634 	fprintf(stderr, "\t-T disktype\n");
635 #endif
636 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
637 	fprintf(stderr, "\t-b block size\n");
638 	fprintf(stderr, "\t-c cylinders/group\n");
639 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
640 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
641 	fprintf(stderr, "\t-f frag size\n");
642 	fprintf(stderr, "\t-i number of bytes per inode\n");
643 	fprintf(stderr, "\t-k sector 0 skew, per track\n");
644 	fprintf(stderr, "\t-l hardware sector interleave\n");
645 	fprintf(stderr, "\t-m minimum free space %%\n");
646 	fprintf(stderr, "\t-n number of distinguished rotational positions\n");
647 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
648 	fprintf(stderr, "\t-p spare sectors per track\n");
649 	fprintf(stderr, "\t-s file system size (sectors)\n");
650 	fprintf(stderr, "\t-r revolutions/minute\n");
651 	fprintf(stderr, "\t-t tracks/cylinder\n");
652 	fprintf(stderr, "\t-u sectors/track\n");
653 	fprintf(stderr, "\t-x spare sectors per cylinder\n");
654 	exit(1);
655 }
656