update for SETOPT/GETOPT (from karels)
[unix-history] / usr / src / sbin / newfs / newfs.c
... / ...
CommitLineData
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
9static char sccsid[] = "@(#)newfs.c 8.11 (Berkeley) %G%";
10#endif /* not lint */
11
12#ifndef lint
13static 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/ffs/fs.h>
30
31#include <ctype.h>
32#include <errno.h>
33#include <paths.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <syslog.h>
38#include <unistd.h>
39
40#if __STDC__
41#include <stdarg.h>
42#else
43#include <varargs.h>
44#endif
45
46#include "mntopts.h"
47
48struct mntopt mopts[] = {
49 MOPT_STDOPTS,
50 MOPT_ASYNC,
51 { NULL },
52};
53
54#if __STDC__
55void fatal(const char *fmt, ...);
56#else
57void fatal();
58#endif
59
60#define COMPAT /* allow non-labeled disks */
61
62/*
63 * The following two constants set the default block and fragment sizes.
64 * Both constants must be a power of 2 and meet the following constraints:
65 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
66 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
67 * DESBLKSIZE / DESFRAGSIZE <= 8
68 */
69#define DFL_FRAGSIZE 1024
70#define DFL_BLKSIZE 8192
71
72/*
73 * Cylinder groups may have up to many cylinders. The actual
74 * number used depends upon how much information can be stored
75 * on a single cylinder. The default is to use 16 cylinders
76 * per group.
77 */
78#define DESCPG 16 /* desired fs_cpg */
79
80/*
81 * ROTDELAY gives the minimum number of milliseconds to initiate
82 * another disk transfer on the same cylinder. It is used in
83 * determining the rotationally optimal layout for disk blocks
84 * within a file; the default of fs_rotdelay is 4ms.
85 */
86#define ROTDELAY 4
87
88/*
89 * MAXBLKPG determines the maximum number of data blocks which are
90 * placed in a single cylinder group. The default is one indirect
91 * block worth of data blocks.
92 */
93#define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
94
95/*
96 * Each file system has a number of inodes statically allocated.
97 * We allocate one inode slot per NFPI fragments, expecting this
98 * to be far more than we will ever need.
99 */
100#define NFPI 4
101
102/*
103 * For each cylinder we keep track of the availability of blocks at different
104 * rotational positions, so that we can lay out the data to be picked
105 * up with minimum rotational latency. NRPOS is the default number of
106 * rotational positions that we distinguish. With NRPOS of 8 the resolution
107 * of our summary information is 2ms for a typical 3600 rpm drive.
108 */
109#define NRPOS 8 /* number distinct rotational positions */
110
111
112int mfs; /* run as the memory based filesystem */
113int Nflag; /* run without writing file system */
114int Oflag; /* format as an 4.3BSD file system */
115int fssize; /* file system size */
116int ntracks; /* # tracks/cylinder */
117int nsectors; /* # sectors/track */
118int nphyssectors; /* # sectors/track including spares */
119int secpercyl; /* sectors per cylinder */
120int trackspares = -1; /* spare sectors per track */
121int cylspares = -1; /* spare sectors per cylinder */
122int sectorsize; /* bytes/sector */
123#ifdef tahoe
124int realsectorsize; /* bytes/sector in hardware */
125#endif
126int rpm; /* revolutions/minute of drive */
127int interleave; /* hardware sector interleave */
128int trackskew = -1; /* sector 0 skew, per track */
129int headswitch; /* head switch time, usec */
130int trackseek; /* track-to-track seek, usec */
131int fsize = 0; /* fragment size */
132int bsize = 0; /* block size */
133int cpg = DESCPG; /* cylinders/cylinder group */
134int cpgflg; /* cylinders/cylinder group flag was given */
135int minfree = MINFREE; /* free space threshold */
136int opt = DEFAULTOPT; /* optimization preference (space or time) */
137int density; /* number of bytes per inode */
138int maxcontig = 0; /* max contiguous blocks to allocate */
139int rotdelay = ROTDELAY; /* rotational delay between blocks */
140int maxbpg; /* maximum blocks per file in a cyl group */
141int nrpos = NRPOS; /* # of distinguished rotational positions */
142int bbsize = BBSIZE; /* boot block size */
143int sbsize = SBSIZE; /* superblock size */
144int mntflags = MNT_ASYNC; /* flags to be passed to mount */
145u_long memleft; /* virtual memory available */
146caddr_t membase; /* start address of memory based filesystem */
147#ifdef COMPAT
148char *disktype;
149int unlabeled;
150#endif
151
152char device[MAXPATHLEN];
153char *progname;
154
155int
156main(argc, argv)
157 int argc;
158 char *argv[];
159{
160 extern char *optarg;
161 extern int optind;
162 register int ch;
163 register struct partition *pp;
164 register struct disklabel *lp;
165 struct disklabel *getdisklabel();
166 struct partition oldpartition;
167 struct stat st;
168 struct statfs *mp;
169 int fsi, fso, len, n;
170 char *cp, *s1, *s2, *special, *opstring, buf[BUFSIZ];
171
172 if (progname = strrchr(*argv, '/'))
173 ++progname;
174 else
175 progname = *argv;
176
177 if (strstr(progname, "mfs")) {
178 mfs = 1;
179 Nflag++;
180 }
181
182 opstring = mfs ?
183 "NT:a:b:c:d:e:f:i:m:o:s:" :
184 "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:x:";
185 while ((ch = getopt(argc, argv, opstring)) != EOF)
186 switch (ch) {
187 case 'N':
188 Nflag = 1;
189 break;
190 case 'O':
191 Oflag = 1;
192 break;
193 case 'S':
194 if ((sectorsize = atoi(optarg)) <= 0)
195 fatal("%s: bad sector size", optarg);
196 break;
197#ifdef COMPAT
198 case 'T':
199 disktype = optarg;
200 break;
201#endif
202 case 'a':
203 if ((maxcontig = atoi(optarg)) <= 0)
204 fatal("%s: bad maximum contiguous blocks\n",
205 optarg);
206 break;
207 case 'b':
208 if ((bsize = atoi(optarg)) < MINBSIZE)
209 fatal("%s: bad block size", optarg);
210 break;
211 case 'c':
212 if ((cpg = atoi(optarg)) <= 0)
213 fatal("%s: bad cylinders/group", optarg);
214 cpgflg++;
215 break;
216 case 'd':
217 if ((rotdelay = atoi(optarg)) < 0)
218 fatal("%s: bad rotational delay\n", optarg);
219 break;
220 case 'e':
221 if ((maxbpg = atoi(optarg)) <= 0)
222 fatal("%s: bad blocks per file in a cylinder group\n",
223 optarg);
224 break;
225 case 'f':
226 if ((fsize = atoi(optarg)) <= 0)
227 fatal("%s: bad fragment size", optarg);
228 break;
229 case 'i':
230 if ((density = atoi(optarg)) <= 0)
231 fatal("%s: bad bytes per inode\n", optarg);
232 break;
233 case 'k':
234 if ((trackskew = atoi(optarg)) < 0)
235 fatal("%s: bad track skew", optarg);
236 break;
237 case 'l':
238 if ((interleave = atoi(optarg)) <= 0)
239 fatal("%s: bad interleave", optarg);
240 break;
241 case 'm':
242 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
243 fatal("%s: bad free space %%\n", optarg);
244 break;
245 case 'n':
246 if ((nrpos = atoi(optarg)) <= 0)
247 fatal("%s: bad rotational layout count\n",
248 optarg);
249 break;
250 case 'o':
251 if (mfs)
252 getmntopts(optarg, mopts, &mntflags);
253 else {
254 if (strcmp(optarg, "space") == 0)
255 opt = FS_OPTSPACE;
256 else if (strcmp(optarg, "time") == 0)
257 opt = FS_OPTTIME;
258 else
259 fatal("%s: unknown optimization preference: use `space' or `time'.");
260 }
261 break;
262 case 'p':
263 if ((trackspares = atoi(optarg)) < 0)
264 fatal("%s: bad spare sectors per track",
265 optarg);
266 break;
267 case 'r':
268 if ((rpm = atoi(optarg)) <= 0)
269 fatal("%s: bad revolutions/minute\n", optarg);
270 break;
271 case 's':
272 if ((fssize = atoi(optarg)) <= 0)
273 fatal("%s: bad file system size", optarg);
274 break;
275 case 't':
276 if ((ntracks = atoi(optarg)) <= 0)
277 fatal("%s: bad total tracks", optarg);
278 break;
279 case 'u':
280 if ((nsectors = atoi(optarg)) <= 0)
281 fatal("%s: bad sectors/track", optarg);
282 break;
283 case 'x':
284 if ((cylspares = atoi(optarg)) < 0)
285 fatal("%s: bad spare sectors per cylinder",
286 optarg);
287 break;
288 case '?':
289 default:
290 usage();
291 }
292 argc -= optind;
293 argv += optind;
294
295 if (argc != 2 && (mfs || argc != 1))
296 usage();
297
298 special = argv[0];
299 cp = strrchr(special, '/');
300 if (cp == 0) {
301 /*
302 * No path prefix; try /dev/r%s then /dev/%s.
303 */
304 (void)sprintf(device, "%sr%s", _PATH_DEV, special);
305 if (stat(device, &st) == -1)
306 (void)sprintf(device, "%s%s", _PATH_DEV, special);
307 special = device;
308 }
309 if (Nflag) {
310 fso = -1;
311 } else {
312 fso = open(special, O_WRONLY);
313 if (fso < 0)
314 fatal("%s: %s", special, strerror(errno));
315
316 /* Bail if target special is mounted */
317 n = getmntinfo(&mp, MNT_NOWAIT);
318 if (n == 0)
319 fatal("%s: getmntinfo: %s", special, strerror(errno));
320
321 len = sizeof(_PATH_DEV) - 1;
322 s1 = special;
323 if (strncmp(_PATH_DEV, s1, len) == 0)
324 s1 += len;
325
326 while (--n >= 0) {
327 s2 = mp->f_mntfromname;
328 if (strncmp(_PATH_DEV, s2, len) == 0) {
329 s2 += len - 1;
330 *s2 = 'r';
331 }
332 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
333 fatal("%s is mounted on %s",
334 special, mp->f_mntonname);
335 ++mp;
336 }
337 }
338 if (mfs && disktype != NULL) {
339 lp = (struct disklabel *)getdiskbyname(disktype);
340 if (lp == NULL)
341 fatal("%s: unknown disk type", disktype);
342 pp = &lp->d_partitions[1];
343 } else {
344 fsi = open(special, O_RDONLY);
345 if (fsi < 0)
346 fatal("%s: %s", special, strerror(errno));
347 if (fstat(fsi, &st) < 0)
348 fatal("%s: %s", special, strerror(errno));
349 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
350 printf("%s: %s: not a character-special device\n",
351 progname, special);
352 cp = strchr(argv[0], '\0') - 1;
353 if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
354 fatal("%s: can't figure out file system partition",
355 argv[0]);
356#ifdef COMPAT
357 if (!mfs && disktype == NULL)
358 disktype = argv[1];
359#endif
360 lp = getdisklabel(special, fsi);
361 if (isdigit(*cp))
362 pp = &lp->d_partitions[0];
363 else
364 pp = &lp->d_partitions[*cp - 'a'];
365 if (pp->p_size == 0)
366 fatal("%s: `%c' partition is unavailable",
367 argv[0], *cp);
368 if (pp->p_fstype == FS_BOOT)
369 fatal("%s: `%c' partition overlaps boot program",
370 argv[0], *cp);
371 }
372 if (fssize == 0)
373 fssize = pp->p_size;
374 if (fssize > pp->p_size && !mfs)
375 fatal("%s: maximum file system size on the `%c' partition is %d",
376 argv[0], *cp, pp->p_size);
377 if (rpm == 0) {
378 rpm = lp->d_rpm;
379 if (rpm <= 0)
380 rpm = 3600;
381 }
382 if (ntracks == 0) {
383 ntracks = lp->d_ntracks;
384 if (ntracks <= 0)
385 fatal("%s: no default #tracks", argv[0]);
386 }
387 if (nsectors == 0) {
388 nsectors = lp->d_nsectors;
389 if (nsectors <= 0)
390 fatal("%s: no default #sectors/track", argv[0]);
391 }
392 if (sectorsize == 0) {
393 sectorsize = lp->d_secsize;
394 if (sectorsize <= 0)
395 fatal("%s: no default sector size", argv[0]);
396 }
397 if (trackskew == -1) {
398 trackskew = lp->d_trackskew;
399 if (trackskew < 0)
400 trackskew = 0;
401 }
402 if (interleave == 0) {
403 interleave = lp->d_interleave;
404 if (interleave <= 0)
405 interleave = 1;
406 }
407 if (fsize == 0) {
408 fsize = pp->p_fsize;
409 if (fsize <= 0)
410 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
411 }
412 if (bsize == 0) {
413 bsize = pp->p_frag * pp->p_fsize;
414 if (bsize <= 0)
415 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
416 }
417 /*
418 * Maxcontig sets the default for the maximum number of blocks
419 * that may be allocated sequentially. With filesystem clustering
420 * it is possible to allocate contiguous blocks up to the maximum
421 * transfer size permitted by the controller or buffering.
422 */
423 if (maxcontig == 0)
424 maxcontig = MAX(1, MIN(MAXPHYS, MAXBSIZE) / bsize);
425 if (density == 0)
426 density = NFPI * fsize;
427 if (minfree < MINFREE && opt != FS_OPTSPACE) {
428 fprintf(stderr, "Warning: changing optimization to space ");
429 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
430 opt = FS_OPTSPACE;
431 }
432 if (trackspares == -1) {
433 trackspares = lp->d_sparespertrack;
434 if (trackspares < 0)
435 trackspares = 0;
436 }
437 nphyssectors = nsectors + trackspares;
438 if (cylspares == -1) {
439 cylspares = lp->d_sparespercyl;
440 if (cylspares < 0)
441 cylspares = 0;
442 }
443 secpercyl = nsectors * ntracks - cylspares;
444 if (secpercyl != lp->d_secpercyl)
445 fprintf(stderr, "%s (%d) %s (%lu)\n",
446 "Warning: calculated sectors per cylinder", secpercyl,
447 "disagrees with disk label", lp->d_secpercyl);
448 if (maxbpg == 0)
449 maxbpg = MAXBLKPG(bsize);
450 headswitch = lp->d_headswitch;
451 trackseek = lp->d_trkseek;
452 /* Reno fix: label may be 0 if faked up by kernel */
453#ifdef notdef
454#ifdef notdef /* label may be 0 if faked up by kernel */
455 bbsize = lp->d_bbsize;
456 sbsize = lp->d_sbsize;
457#endif
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(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
504char lmsg[] = "%s: can't read disk label; disk type must be specified";
505#else
506char lmsg[] = "%s: can't read disk label";
507#endif
508
509struct disklabel *
510getdisklabel(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
534rewritelabel(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*/
585void
586#if __STDC__
587fatal(const char *fmt, ...)
588#else
589fatal(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
613usage()
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}