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
33 * @(#) Copyright (c) 1983, 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)newfs.c 8.13 (Berkeley) 5/1/95
35 * $FreeBSD: src/sbin/newfs/newfs.c,v 1.30.2.9 2003/05/13 12:03:55 joerg Exp $
36 * $DragonFly: src/sbin/newfs/newfs.c,v 1.17 2007/06/19 19:18:20 dillon Exp $
40 * newfs: friendly front end to mkfs
42 #include <sys/param.h>
44 #include <sys/diskslice.h>
46 #include <sys/mount.h>
48 #include <vfs/ufs/dir.h>
49 #include <vfs/ufs/dinode.h>
50 #include <vfs/ufs/fs.h>
51 #include <vfs/ufs/ufsmount.h>
65 #include <sys/types.h>
78 struct mntopt mopts[] = {
84 void fatal(const char *fmt, ...);
86 #define COMPAT /* allow non-labeled disks */
89 * The following two constants set the default block and fragment sizes.
90 * Both constants must be a power of 2 and meet the following constraints:
91 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE
92 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE
93 * DESBLKSIZE / DESFRAGSIZE <= 8
95 #define DFL_FRAGSIZE 2048
96 #define DFL_BLKSIZE 16384
99 * Cylinder groups may have up to many cylinders. The actual
100 * number used depends upon how much information can be stored
101 * on a single cylinder. The default is to use as many as possible
102 * cylinders per group.
104 #define DESCPG 65536 /* desired fs_cpg ("infinity") */
107 * Once upon a time...
108 * ROTDELAY gives the minimum number of milliseconds to initiate
109 * another disk transfer on the same cylinder. It is used in
110 * determining the rotationally optimal layout for disk blocks
111 * within a file; the default of fs_rotdelay is 4ms.
113 * ...but now we make this 0 to disable the rotdelay delay because
114 * modern drives with read/write-behind achieve higher performance
120 * MAXBLKPG determines the maximum number of data blocks which are
121 * placed in a single cylinder group. The default is one indirect
122 * block worth of data blocks.
124 #define MAXBLKPG(bsize) ((bsize) / sizeof(daddr_t))
127 * Each file system has a number of inodes statically allocated.
128 * We allocate one inode slot per NFPI fragments, expecting this
129 * to be far more than we will ever need.
134 * Once upon a time...
135 * For each cylinder we keep track of the availability of blocks at different
136 * rotational positions, so that we can lay out the data to be picked
137 * up with minimum rotational latency. NRPOS is the default number of
138 * rotational positions that we distinguish. With NRPOS of 8 the resolution
139 * of our summary information is 2ms for a typical 3600 rpm drive.
141 * ...but now we make this 1 (which essentially disables the rotational
142 * position table because modern drives with read-ahead and write-behind do
143 * better without the rotational position table.
145 #define NRPOS 1 /* number distinct rotational positions */
148 * About the same time as the above, we knew what went where on the disks.
149 * no longer so, so kill the code which finds the different platters too...
150 * We do this by saying one head, with a lot of sectors on it.
151 * The number of sectors are used to determine the size of a cyl-group.
152 * Kirk suggested one or two meg per "cylinder" so we say two.
154 #define NTRACKS 1 /* number of heads */
155 #define NSECTORS 4096 /* number of sectors */
157 int mfs; /* run as the memory based filesystem */
158 char *mfs_mtpt; /* mount point for mfs */
159 struct stat mfs_mtstat; /* stat prior to mount */
160 int Nflag; /* run without writing file system */
161 int Oflag; /* format as an 4.3BSD file system */
162 int Cflag; /* copy underlying filesystem (mfs only) */
163 int Uflag; /* enable soft updates for file system */
164 u_long fssize; /* file system size */
165 int ntracks = NTRACKS; /* # tracks/cylinder */
166 int nsectors = NSECTORS; /* # sectors/track */
167 int nphyssectors; /* # sectors/track including spares */
168 int secpercyl; /* sectors per cylinder */
169 int trackspares = -1; /* spare sectors per track */
170 int cylspares = -1; /* spare sectors per cylinder */
171 int sectorsize; /* bytes/sector */
172 int realsectorsize; /* bytes/sector in hardware */
173 int rpm; /* revolutions/minute of drive */
174 int interleave; /* hardware sector interleave */
175 int trackskew = -1; /* sector 0 skew, per track */
176 int headswitch; /* head switch time, usec */
177 int trackseek; /* track-to-track seek, usec */
178 int fsize = 0; /* fragment size */
179 int bsize = 0; /* block size */
180 int cpg = DESCPG; /* cylinders/cylinder group */
181 int cpgflg; /* cylinders/cylinder group flag was given */
182 int minfree = MINFREE; /* free space threshold */
183 int opt = DEFAULTOPT; /* optimization preference (space or time) */
184 int density; /* number of bytes per inode */
185 int maxcontig = 0; /* max contiguous blocks to allocate */
186 int rotdelay = ROTDELAY; /* rotational delay between blocks */
187 int maxbpg; /* maximum blocks per file in a cyl group */
188 int nrpos = NRPOS; /* # of distinguished rotational positions */
189 int avgfilesize = AVFILESIZ;/* expected average file size */
190 int avgfilesperdir = AFPDIR;/* expected number of files per directory */
191 int bbsize = BBSIZE; /* boot block size */
192 int sbsize = SBSIZE; /* superblock size */
193 int mntflags = MNT_ASYNC; /* flags to be passed to mount */
194 int t_or_u_flag = 0; /* user has specified -t or -u */
195 caddr_t membase; /* start address of memory based filesystem */
202 char mfsdevname[256];
205 static void usage(void);
206 static void mfsintr(int signo);
209 main(int argc, char **argv)
212 struct disktab geom; /* disk geometry data */
215 int fsi = -1, fso = -1, len, n, vflag;
216 char *s1, *s2, *special;
217 const char *opstring;
223 bzero(&geom, sizeof(geom));
225 if ((progname = strrchr(*argv, '/')))
230 if (strstr(progname, "mfs")) {
236 "NCF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:v" :
237 "NOS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
238 while ((ch = getopt(argc, argv, opstring)) != -1) {
247 Cflag = 1; /* MFS only */
250 if ((sectorsize = atoi(optarg)) <= 0)
251 fatal("%s: bad sector size", optarg);
265 if ((maxcontig = atoi(optarg)) <= 0)
266 fatal("%s: bad maximum contiguous blocks",
270 if ((bsize = atoi(optarg)) < MINBSIZE)
271 fatal("%s: bad block size", optarg);
274 if ((cpg = atoi(optarg)) <= 0)
275 fatal("%s: bad cylinders/group", optarg);
279 if ((rotdelay = atoi(optarg)) < 0)
280 fatal("%s: bad rotational delay", optarg);
283 if ((maxbpg = atoi(optarg)) <= 0)
284 fatal("%s: bad blocks per file in a cylinder group",
288 if ((fsize = atoi(optarg)) <= 0)
289 fatal("%s: bad fragment size", optarg);
292 if ((avgfilesize = atoi(optarg)) <= 0)
293 fatal("%s: bad average file size", optarg);
296 if ((avgfilesperdir = atoi(optarg)) <= 0)
297 fatal("%s: bad average files per dir", optarg);
300 if ((density = atoi(optarg)) <= 0)
301 fatal("%s: bad bytes per inode", optarg);
304 if ((trackskew = atoi(optarg)) < 0)
305 fatal("%s: bad track skew", optarg);
308 if ((interleave = atoi(optarg)) <= 0)
309 fatal("%s: bad interleave", optarg);
312 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
313 fatal("%s: bad free space %%", optarg);
316 if ((nrpos = atoi(optarg)) < 0)
317 fatal("%s: bad rotational layout count",
324 getmntopts(optarg, mopts, &mntflags, 0);
326 if (strcmp(optarg, "space") == 0)
328 else if (strcmp(optarg, "time") == 0)
331 fatal("%s: unknown optimization preference: use `space' or `time'", optarg);
335 if ((trackspares = atoi(optarg)) < 0)
336 fatal("%s: bad spare sectors per track",
340 if ((rpm = atoi(optarg)) <= 0)
341 fatal("%s: bad revolutions/minute", optarg);
345 * Unsigned long but limit to long. On 32 bit a
346 * tad under 2G, on 64 bit the upper bound is more
347 * swap space then operand size.
349 * NOTE: fssize is converted from 512 byte sectors
350 * to filesystem block-sized sectors by mkfs XXX.
352 fssize = strtoul(optarg, NULL, 10);
353 if (fssize == 0 || fssize > LONG_MAX)
354 fatal("%s: bad file system size", optarg);
358 if ((ntracks = atoi(optarg)) < 0)
359 fatal("%s: bad total tracks", optarg);
363 if ((nsectors = atoi(optarg)) < 0)
364 fatal("%s: bad sectors/track", optarg);
370 if ((cylspares = atoi(optarg)) < 0)
371 fatal("%s: bad spare sectors per cylinder",
382 if (argc != 2 && (mfs || argc != 1))
386 /* Copy the NetBSD way of faking up a disk label */
387 if (mfs && !strcmp(special, "swap")) {
389 * it's an MFS, mounted on "swap." fake up a label.
392 fso = -1; /* XXX; normally done below. */
394 geom.d_media_blksize = 512;
396 geom.d_secpertrack = 64;
397 /* geom.d_ncylinders not used */
398 geom.d_secpercyl = 1024;
399 geom.d_media_blocks = 16384;
401 geom.d_interleave = 1;
407 * If we can't stat the device and the path is relative, try
410 if (stat(special, &st) < 0 && special[0] && special[0] != '/')
411 asprintf(&special, "/dev/%s", special);
416 fso = open(special, O_WRONLY);
418 fatal("%s: %s", special, strerror(errno));
420 /* Bail if target special is mounted */
421 n = getmntinfo(&mp, MNT_NOWAIT);
423 fatal("%s: getmntinfo: %s", special, strerror(errno));
425 len = sizeof(_PATH_DEV) - 1;
427 if (strncmp(_PATH_DEV, s1, len) == 0)
431 s2 = mp->f_mntfromname;
432 if (strncmp(_PATH_DEV, s2, len) == 0) {
436 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
437 fatal("%s is mounted on %s",
438 special, mp->f_mntonname);
442 if (mfs && disktype != NULL) {
445 if ((dt = getdisktabbyname(disktype)) == NULL)
446 fatal("%s: unknown disk type", disktype);
449 struct partinfo pinfo;
452 fatal("null special file name");
453 fsi = open(special, O_RDONLY);
455 fatal("%s: %s", special, strerror(errno));
456 if (fstat(fsi, &st) < 0)
457 fatal("%s: %s", special, strerror(errno));
458 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs && !vflag)
459 printf("%s: %s: not a character-special device\n",
462 if (!mfs && disktype == NULL)
465 if (ioctl(fsi, DIOCGPART, &pinfo) < 0) {
467 fatal("%s: unable to retrieve geometry "
468 "information", argv[0]);
471 * fake up geometry data
473 geom.d_media_blksize = 512;
475 geom.d_secpertrack = 64;
476 geom.d_secpercyl = 1024;
477 geom.d_media_blocks = st.st_size /
478 geom.d_media_blksize;
479 geom.d_media_size = geom.d_media_blocks *
480 geom.d_media_blksize;
481 /* geom.d_ncylinders not used */
484 * extract geometry from pinfo
486 geom.d_media_blksize = pinfo.media_blksize;
487 geom.d_nheads = pinfo.d_nheads;
488 geom.d_secpertrack = pinfo.d_secpertrack;
489 geom.d_secpercyl = pinfo.d_secpercyl;
490 /* geom.d_ncylinders not used */
491 geom.d_media_blocks = pinfo.media_blocks;
492 geom.d_media_size = pinfo.media_size;
494 if (geom.d_media_blocks == 0 || geom.d_media_size == 0) {
495 fatal("%s: is unavailable", argv[0]);
497 printf("%s: media size %6.2fMB\n",
498 argv[0], geom.d_media_size / 1024.0 / 1024.0);
499 if (geom.d_media_size / 512 >= 0x80000000ULL)
500 fatal("%s: media size is too large for newfs to handle",
505 fssize = geom.d_media_blocks;
506 if ((u_long)fssize > geom.d_media_blocks && !mfs) {
507 fatal("%s: maximum file system size is %lld blocks",
508 argv[0], geom.d_media_blocks);
516 ntracks = geom.d_nheads;
518 fatal("%s: no default #tracks", argv[0]);
521 nsectors = geom.d_secpertrack;
523 fatal("%s: no default #sectors/track", argv[0]);
525 if (sectorsize == 0) {
526 sectorsize = geom.d_media_blksize;
528 fatal("%s: no default sector size", argv[0]);
530 if (trackskew == -1) {
531 trackskew = geom.d_trackskew;
535 if (interleave == 0) {
536 interleave = geom.d_interleave;
541 fsize = MAX(DFL_FRAGSIZE, geom.d_media_blksize);
543 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
545 * Maxcontig sets the default for the maximum number of blocks
546 * that may be allocated sequentially. With filesystem clustering
547 * it is possible to allocate contiguous blocks up to the maximum
548 * transfer size permitted by the controller or buffering.
551 maxcontig = MAX(1, MAXPHYS / bsize - 1);
553 density = NFPI * fsize;
554 if (minfree < MINFREE && opt != FS_OPTSPACE) {
555 fprintf(stderr, "Warning: changing optimization to space ");
556 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
559 if (trackspares == -1)
561 nphyssectors = nsectors + trackspares;
564 secpercyl = nsectors * ntracks - cylspares;
566 * Only complain if -t or -u have been specified; the default
567 * case (4096 sectors per cylinder) is intended to disagree
568 * with the disklabel.
570 if (t_or_u_flag && (uint32_t)secpercyl != geom.d_secpercyl)
571 fprintf(stderr, "%s (%d) %s (%u)\n",
572 "Warning: calculated sectors per cylinder", secpercyl,
573 "disagrees with disk label", geom.d_secpercyl);
575 maxbpg = MAXBLKPG(bsize);
576 headswitch = geom.d_headswitch;
577 trackseek = geom.d_trkseek;
578 realsectorsize = sectorsize;
579 if (sectorsize != DEV_BSIZE) { /* XXX */
580 int secperblk = sectorsize / DEV_BSIZE;
582 sectorsize = DEV_BSIZE;
583 nsectors *= secperblk;
584 nphyssectors *= secperblk;
585 secpercyl *= secperblk;
591 stat(mfs_mtpt, &mfs_mtstat) < 0 ||
592 !S_ISDIR(mfs_mtstat.st_mode)
594 fatal("mount point not dir: %s", mfs_mtpt);
597 mkfs(special, fsi, fso, (Cflag && mfs) ? argv[1] : NULL);
600 * NOTE: Newfs no longer accesses or attempts to update the
601 * filesystem disklabel.
603 * NOTE: fssize is converted from 512 byte sectors
604 * to filesystem block-sized sectors by mkfs XXX.
611 struct mfs_args args;
613 bzero(&args, sizeof(args));
615 snprintf(mfsdevname, sizeof(mfsdevname), "/dev/mfs%d",
617 args.fspec = mfsdevname;
618 args.export.ex_root = -2;
619 if (mntflags & MNT_RDONLY)
620 args.export.ex_flags = MNT_EXRDONLY;
622 args.export.ex_flags = 0;
624 args.size = fssize * fsize;
626 error = getvfsbyname("mfs", &vfc);
627 if (error && vfsisloadable("mfs")) {
629 fatal("vfsload(mfs)");
630 endvfsent(); /* flush cache */
631 error = getvfsbyname("mfs", &vfc);
634 fatal("mfs filesystem not available");
638 udev = (253 << 8) | (getpid() & 255) |
639 ((getpid() & ~0xFF) << 8);
640 if (mknod(mfsdevname, S_IFCHR | 0700, udev) < 0)
641 printf("Warning: unable to create %s\n", mfsdevname);
643 signal(SIGINT, mfsintr);
644 if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
645 fatal("%s: %s", argv[1], strerror(errno));
646 signal(SIGINT, SIG_DFL);
656 mfsintr(__unused int signo)
659 munmap(membase, fssize * fsize);
669 fatal(const char *fmt, ...)
674 if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
675 openlog(progname, LOG_CONS, LOG_DAEMON);
676 vsyslog(LOG_ERR, fmt, ap);
691 "usage: %s [ -fsoptions ] special-device mount-point\n",
695 "usage: %s [ -fsoptions ] special-device%s\n",
702 fprintf(stderr, "where fsoptions are:\n");
703 fprintf(stderr, "\t-C (mfs) Copy the underlying filesystem to the MFS mount\n");
705 "\t-N do not create file system, just print out parameters\n");
706 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
707 fprintf(stderr, "\t-S sector size\n");
709 fprintf(stderr, "\t-T disktype\n");
711 fprintf(stderr, "\t-U enable soft updates\n");
712 fprintf(stderr, "\t-a maximum contiguous blocks\n");
713 fprintf(stderr, "\t-b block size\n");
714 fprintf(stderr, "\t-c cylinders/group\n");
715 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
716 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
717 fprintf(stderr, "\t-f frag size\n");
718 fprintf(stderr, "\t-g average file size\n");
719 fprintf(stderr, "\t-h average files per directory\n");
720 fprintf(stderr, "\t-i number of bytes per inode\n");
721 fprintf(stderr, "\t-k sector 0 skew, per track\n");
722 fprintf(stderr, "\t-l hardware sector interleave\n");
723 fprintf(stderr, "\t-m minimum free space %%\n");
724 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
725 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
726 fprintf(stderr, "\t-p spare sectors per track\n");
727 fprintf(stderr, "\t-s file system size (sectors)\n");
728 fprintf(stderr, "\t-r revolutions/minute\n");
729 fprintf(stderr, "\t-t tracks/cylinder\n");
730 fprintf(stderr, "\t-u sectors/track\n");
732 "\t-v do not attempt to determine partition name from device name\n");
733 fprintf(stderr, "\t-x spare sectors per cylinder\n");