2 * Copyright (c) 1983, 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
44 static const char rcsid[] =
49 * newfs: friendly front end to mkfs
51 #include <sys/param.h>
53 #include <sys/disklabel.h>
55 #include <sys/mount.h>
57 #include <ufs/ufs/dir.h>
58 #include <ufs/ufs/dinode.h>
59 #include <ufs/ffs/fs.h>
60 #include <ufs/ufs/ufsmount.h>
73 #include <sys/types.h>
85 struct mntopt mopts[] = {
92 void fatal(const char *fmt, ...);
97 #define COMPAT /* allow non-labeled disks */
100 * The following two constants set the default block and fragment sizes.
101 * Both constants must be a power of 2 and meet the following constraints:
102 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
103 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
104 * DESBLKSIZE / DESFRAGSIZE <= 8
106 #define DFL_FRAGSIZE 1024
107 #define DFL_BLKSIZE 8192
110 * Cylinder groups may have up to many cylinders. The actual
111 * number used depends upon how much information can be stored
112 * on a single cylinder. The default is to use 16 cylinders
115 #define DESCPG 16 /* desired fs_cpg */
118 * Once upon a time...
119 * ROTDELAY gives the minimum number of milliseconds to initiate
120 * another disk transfer on the same cylinder. It is used in
121 * determining the rotationally optimal layout for disk blocks
122 * within a file; the default of fs_rotdelay is 4ms.
124 * ...but now we make this 0 to disable the rotdelay delay because
125 * modern drives with read/write-behind achieve higher performance
131 * MAXBLKPG determines the maximum number of data blocks which are
132 * placed in a single cylinder group. The default is one indirect
133 * block worth of data blocks.
135 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
138 * Each file system has a number of inodes statically allocated.
139 * We allocate one inode slot per NFPI fragments, expecting this
140 * to be far more than we will ever need.
145 * Once upon a time...
146 * For each cylinder we keep track of the availability of blocks at different
147 * rotational positions, so that we can lay out the data to be picked
148 * up with minimum rotational latency. NRPOS is the default number of
149 * rotational positions that we distinguish. With NRPOS of 8 the resolution
150 * of our summary information is 2ms for a typical 3600 rpm drive.
152 * ...but now we make this 1 (which essentially disables the rotational
153 * position table because modern drives with read-ahead and write-behind do
154 * better without the rotational position table.
156 #define NRPOS 1 /* number distinct rotational positions */
159 * About the same time as the above, we knew what went where on the disks.
160 * no longer so, so kill the code which finds the different platters too...
161 * We do this by saying one head, with a lot of sectors on it.
162 * The number of sectors are used to determine the size of a cyl-group.
163 * Kirk suggested one or two meg per "cylinder" so we say two.
166 #define NTRACKS 1 /* number of heads */
168 #define NSECTORS 4096 /* number of sectors */
170 int mfs; /* run as the memory based filesystem */
171 char *mfs_mtpt; /* mount point for mfs */
172 struct stat mfs_mtstat; /* stat prior to mount */
173 int Nflag; /* run without writing file system */
174 int Oflag; /* format as an 4.3BSD file system */
175 int fssize; /* file system size */
176 int ntracks = NTRACKS; /* # tracks/cylinder */
177 int nsectors = NSECTORS; /* # sectors/track */
178 int nphyssectors; /* # sectors/track including spares */
179 int secpercyl; /* sectors per cylinder */
180 int trackspares = -1; /* spare sectors per track */
181 int cylspares = -1; /* spare sectors per cylinder */
182 int sectorsize; /* bytes/sector */
183 int realsectorsize; /* bytes/sector in hardware */
184 int rpm; /* revolutions/minute of drive */
185 int interleave; /* hardware sector interleave */
186 int trackskew = -1; /* sector 0 skew, per track */
187 int headswitch; /* head switch time, usec */
188 int trackseek; /* track-to-track seek, usec */
189 int fsize = 0; /* fragment size */
190 int bsize = 0; /* block size */
191 int cpg = DESCPG; /* cylinders/cylinder group */
192 int cpgflg; /* cylinders/cylinder group flag was given */
193 int minfree = MINFREE; /* free space threshold */
194 int opt = DEFAULTOPT; /* optimization preference (space or time) */
195 int density; /* number of bytes per inode */
196 int maxcontig = 0; /* max contiguous blocks to allocate */
197 int rotdelay = ROTDELAY; /* rotational delay between blocks */
198 int maxbpg; /* maximum blocks per file in a cyl group */
199 int nrpos = NRPOS; /* # of distinguished rotational positions */
200 int bbsize = BBSIZE; /* boot block size */
201 int sbsize = SBSIZE; /* superblock size */
202 int mntflags = MNT_ASYNC; /* flags to be passed to mount */
203 int t_or_u_flag = 0; /* user has specified -t or -u */
204 u_long memleft; /* virtual memory available */
205 caddr_t membase; /* start address of memory based filesystem */
212 char device[MAXPATHLEN];
215 extern void mkfs __P((struct partition *, char *, int, int));
216 static void usage __P((void));
224 register struct partition *pp;
225 register struct disklabel *lp;
226 struct disklabel mfsfakelabel;
227 struct disklabel *getdisklabel();
228 struct partition oldpartition;
231 int fsi, fso, len, n, vflag;
232 char *cp, *s1, *s2, *special, *opstring;
240 if ((progname = strrchr(*argv, '/')))
245 if (strstr(progname, "mfs")) {
251 "NF:T:a:b:c:d:e:f:i:m:o:s:" :
252 "NOS:T:a:b:c:d:e:f:i:k:l:m:n:o:p:r:s:t:u:vx:";
253 while ((ch = getopt(argc, argv, opstring)) != -1)
262 if ((sectorsize = atoi(optarg)) <= 0)
263 fatal("%s: bad sector size", optarg);
274 if ((maxcontig = atoi(optarg)) <= 0)
275 fatal("%s: bad maximum contiguous blocks",
279 if ((bsize = atoi(optarg)) < MINBSIZE)
280 fatal("%s: bad block size", optarg);
283 if ((cpg = atoi(optarg)) <= 0)
284 fatal("%s: bad cylinders/group", optarg);
288 if ((rotdelay = atoi(optarg)) < 0)
289 fatal("%s: bad rotational delay", optarg);
292 if ((maxbpg = atoi(optarg)) <= 0)
293 fatal("%s: bad blocks per file in a cylinder group",
297 if ((fsize = atoi(optarg)) <= 0)
298 fatal("%s: bad fragment size", optarg);
301 if ((density = atoi(optarg)) <= 0)
302 fatal("%s: bad bytes per inode", optarg);
305 if ((trackskew = atoi(optarg)) < 0)
306 fatal("%s: bad track skew", optarg);
309 if ((interleave = atoi(optarg)) <= 0)
310 fatal("%s: bad interleave", optarg);
313 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
314 fatal("%s: bad free space %%", optarg);
317 if ((nrpos = atoi(optarg)) < 0)
318 fatal("%s: bad rotational layout count",
325 getmntopts(optarg, mopts, &mntflags, 0);
327 if (strcmp(optarg, "space") == 0)
329 else if (strcmp(optarg, "time") == 0)
332 fatal("%s: unknown optimization preference: use `space' or `time'");
336 if ((trackspares = atoi(optarg)) < 0)
337 fatal("%s: bad spare sectors per track",
341 if ((rpm = atoi(optarg)) <= 0)
342 fatal("%s: bad revolutions/minute", optarg);
345 if ((fssize = atoi(optarg)) <= 0)
346 fatal("%s: bad file system size", optarg);
350 if ((ntracks = atoi(optarg)) < 0)
351 fatal("%s: bad total tracks", optarg);
355 if ((nsectors = atoi(optarg)) < 0)
356 fatal("%s: bad sectors/track", optarg);
362 if ((cylspares = atoi(optarg)) < 0)
363 fatal("%s: bad spare sectors per cylinder",
373 if (argc != 2 && (mfs || argc != 1))
377 /* Copy the NetBSD way of faking up a disk label */
378 if (mfs && !strcmp(special, "swap")) {
380 * it's an MFS, mounted on "swap." fake up a label.
383 fso = -1; /* XXX; normally done below. */
385 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
386 mfsfakelabel.d_secsize = 512;
387 mfsfakelabel.d_nsectors = 64;
388 mfsfakelabel.d_ntracks = 16;
389 mfsfakelabel.d_ncylinders = 16;
390 mfsfakelabel.d_secpercyl = 1024;
391 mfsfakelabel.d_secperunit = 16384;
392 mfsfakelabel.d_rpm = 3600;
393 mfsfakelabel.d_interleave = 1;
394 mfsfakelabel.d_npartitions = 1;
395 mfsfakelabel.d_partitions[0].p_size = 16384;
396 mfsfakelabel.d_partitions[0].p_fsize = 1024;
397 mfsfakelabel.d_partitions[0].p_frag = 8;
398 mfsfakelabel.d_partitions[0].p_cpg = 16;
401 pp = &mfsfakelabel.d_partitions[0];
406 cp = strrchr(special, '/');
409 * No path prefix; try /dev/%s.
411 (void)sprintf(device, "%s%s", _PATH_DEV, special);
417 fso = open(special, O_WRONLY);
419 fatal("%s: %s", special, strerror(errno));
421 /* Bail if target special is mounted */
422 n = getmntinfo(&mp, MNT_NOWAIT);
424 fatal("%s: getmntinfo: %s", special, strerror(errno));
426 len = sizeof(_PATH_DEV) - 1;
428 if (strncmp(_PATH_DEV, s1, len) == 0)
432 s2 = mp->f_mntfromname;
433 if (strncmp(_PATH_DEV, s2, len) == 0) {
437 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
438 fatal("%s is mounted on %s",
439 special, mp->f_mntonname);
443 if (mfs && disktype != NULL) {
444 lp = (struct disklabel *)getdiskbyname(disktype);
446 fatal("%s: unknown disk type", disktype);
447 pp = &lp->d_partitions[1];
449 fsi = open(special, O_RDONLY);
451 fatal("%s: %s", special, strerror(errno));
452 if (fstat(fsi, &st) < 0)
453 fatal("%s: %s", special, strerror(errno));
454 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
455 printf("%s: %s: not a character-special device\n",
457 cp = strchr(argv[0], '\0');
459 fatal("null special file name");
461 if (!vflag && (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
462 fatal("%s: can't figure out file system partition",
465 if (!mfs && disktype == NULL)
468 lp = getdisklabel(special, fsi);
469 if (vflag || isdigit(*cp))
470 pp = &lp->d_partitions[0];
472 pp = &lp->d_partitions[*cp - 'a'];
474 fatal("%s: `%c' partition is unavailable",
476 if (pp->p_fstype == FS_BOOT)
477 fatal("%s: `%c' partition overlaps boot program",
483 if (fssize > pp->p_size && !mfs)
484 fatal("%s: maximum file system size on the `%c' partition is %d",
485 argv[0], *cp, pp->p_size);
492 ntracks = lp->d_ntracks;
494 fatal("%s: no default #tracks", argv[0]);
497 nsectors = lp->d_nsectors;
499 fatal("%s: no default #sectors/track", argv[0]);
501 if (sectorsize == 0) {
502 sectorsize = lp->d_secsize;
504 fatal("%s: no default sector size", argv[0]);
506 if (trackskew == -1) {
507 trackskew = lp->d_trackskew;
511 if (interleave == 0) {
512 interleave = lp->d_interleave;
519 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
522 bsize = pp->p_frag * pp->p_fsize;
524 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
527 * Maxcontig sets the default for the maximum number of blocks
528 * that may be allocated sequentially. With filesystem clustering
529 * it is possible to allocate contiguous blocks up to the maximum
530 * transfer size permitted by the controller or buffering.
533 maxcontig = MAX(1, MAXPHYS / bsize - 1);
535 density = NFPI * fsize;
536 if (minfree < MINFREE && opt != FS_OPTSPACE) {
537 fprintf(stderr, "Warning: changing optimization to space ");
538 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
541 if (trackspares == -1) {
542 trackspares = lp->d_sparespertrack;
546 nphyssectors = nsectors + trackspares;
547 if (cylspares == -1) {
548 cylspares = lp->d_sparespercyl;
552 secpercyl = nsectors * ntracks - cylspares;
554 * Only complain if -t or -u have been specified; the default
555 * case (4096 sectors per cylinder) is intended to disagree
556 * with the disklabel.
558 if (t_or_u_flag && secpercyl != lp->d_secpercyl)
559 fprintf(stderr, "%s (%d) %s (%lu)\n",
560 "Warning: calculated sectors per cylinder", secpercyl,
561 "disagrees with disk label", (u_long)lp->d_secpercyl);
563 maxbpg = MAXBLKPG(bsize);
564 headswitch = lp->d_headswitch;
565 trackseek = lp->d_trkseek;
566 #ifdef notdef /* label may be 0 if faked up by kernel */
567 bbsize = lp->d_bbsize;
568 sbsize = lp->d_sbsize;
572 realsectorsize = sectorsize;
573 if (sectorsize != DEV_BSIZE) { /* XXX */
574 int secperblk = DEV_BSIZE / sectorsize;
576 sectorsize = DEV_BSIZE;
577 nsectors /= secperblk;
578 nphyssectors /= secperblk;
579 secpercyl /= secperblk;
581 pp->p_size /= secperblk;
584 realsectorsize = sectorsize;
585 if (sectorsize != DEV_BSIZE) { /* XXX */
586 int secperblk = sectorsize / DEV_BSIZE;
588 sectorsize = DEV_BSIZE;
589 nsectors *= secperblk;
590 nphyssectors *= secperblk;
591 secpercyl *= secperblk;
593 pp->p_size *= secperblk;
599 stat(mfs_mtpt, &mfs_mtstat) < 0 ||
600 !S_ISDIR(mfs_mtstat.st_mode)
602 fatal("mount point not dir: %s", mfs_mtpt);
605 mkfs(pp, special, fsi, fso);
607 if (realsectorsize != DEV_BSIZE)
608 pp->p_size *= DEV_BSIZE / realsectorsize;
610 if (realsectorsize != DEV_BSIZE)
611 pp->p_size /= realsectorsize /DEV_BSIZE;
613 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
614 rewritelabel(special, fso, lp);
620 struct mfs_args args;
622 sprintf(buf, "mfs:%d", getpid());
624 args.export.ex_root = -2;
625 if (mntflags & MNT_RDONLY)
626 args.export.ex_flags = MNT_EXRDONLY;
628 args.export.ex_flags = 0;
630 args.size = fssize * sectorsize;
632 error = getvfsbyname("mfs", &vfc);
633 if (error && vfsisloadable("mfs")) {
635 fatal("vfsload(mfs)");
636 endvfsent(); /* flush cache */
637 error = getvfsbyname("mfs", &vfc);
640 fatal("mfs filesystem not available");
642 if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
643 fatal("%s: %s", argv[1], strerror(errno));
645 munmap(membase,fssize * sectorsize);
653 char lmsg[] = "%s: can't read disk label; disk type must be specified";
655 char lmsg[] = "%s: can't read disk label";
663 static struct disklabel lab;
665 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
668 struct disklabel *lp, *getdiskbyname();
671 lp = getdiskbyname(disktype);
673 fatal("%s: unknown disk type", disktype);
677 warn("ioctl (GDINFO)");
683 rewritelabel(s, fd, lp)
686 register struct disklabel *lp;
693 lp->d_checksum = dkcksum(lp);
694 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
695 warn("ioctl (WDINFO)");
696 fatal("%s: can't rewrite disk label", s);
703 fatal(const char *fmt, ...)
717 if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
718 openlog(progname, LOG_CONS, LOG_DAEMON);
719 vsyslog(LOG_ERR, fmt, ap);
734 "usage: %s [ -fsoptions ] special-device mount-point\n",
738 "usage: %s [ -fsoptions ] special-device%s\n",
745 fprintf(stderr, "where fsoptions are:\n");
747 "\t-N do not create file system, just print out parameters\n");
748 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
749 fprintf(stderr, "\t-S sector size\n");
751 fprintf(stderr, "\t-T disktype\n");
753 fprintf(stderr, "\t-a maximum contiguous blocks\n");
754 fprintf(stderr, "\t-b block size\n");
755 fprintf(stderr, "\t-c cylinders/group\n");
756 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
757 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
758 fprintf(stderr, "\t-f frag size\n");
759 fprintf(stderr, "\t-i number of bytes per inode\n");
760 fprintf(stderr, "\t-k sector 0 skew, per track\n");
761 fprintf(stderr, "\t-l hardware sector interleave\n");
762 fprintf(stderr, "\t-m minimum free space %%\n");
763 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
764 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
765 fprintf(stderr, "\t-p spare sectors per track\n");
766 fprintf(stderr, "\t-s file system size (sectors)\n");
767 fprintf(stderr, "\t-r revolutions/minute\n");
768 fprintf(stderr, "\t-t tracks/cylinder\n");
769 fprintf(stderr, "\t-u sectors/track\n");
771 "\t-v do not attempt to determine partition name from device name\n");
772 fprintf(stderr, "\t-x spare sectors per cylinder\n");