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[] =
45 "$FreeBSD: src/sbin/newfs/newfs.c,v 1.30.2.9 2003/05/13 12:03:55 joerg Exp $";
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 2048
107 #define DFL_BLKSIZE 16384
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 as many as possible
113 * cylinders per group.
115 #define DESCPG 65536 /* desired fs_cpg ("infinity") */
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.
165 #define NTRACKS 1 /* number of heads */
166 #define NSECTORS 4096 /* number of sectors */
168 int mfs; /* run as the memory based filesystem */
169 char *mfs_mtpt; /* mount point for mfs */
170 struct stat mfs_mtstat; /* stat prior to mount */
171 int Nflag; /* run without writing file system */
172 int Oflag; /* format as an 4.3BSD file system */
173 int Uflag; /* enable soft updates for file system */
174 int fssize; /* file system size */
175 int ntracks = NTRACKS; /* # tracks/cylinder */
176 int nsectors = NSECTORS; /* # sectors/track */
177 int nphyssectors; /* # sectors/track including spares */
178 int secpercyl; /* sectors per cylinder */
179 int trackspares = -1; /* spare sectors per track */
180 int cylspares = -1; /* spare sectors per cylinder */
181 int sectorsize; /* bytes/sector */
182 int realsectorsize; /* bytes/sector in hardware */
183 int rpm; /* revolutions/minute of drive */
184 int interleave; /* hardware sector interleave */
185 int trackskew = -1; /* sector 0 skew, per track */
186 int headswitch; /* head switch time, usec */
187 int trackseek; /* track-to-track seek, usec */
188 int fsize = 0; /* fragment size */
189 int bsize = 0; /* block size */
190 int cpg = DESCPG; /* cylinders/cylinder group */
191 int cpgflg; /* cylinders/cylinder group flag was given */
192 int minfree = MINFREE; /* free space threshold */
193 int opt = DEFAULTOPT; /* optimization preference (space or time) */
194 int density; /* number of bytes per inode */
195 int maxcontig = 0; /* max contiguous blocks to allocate */
196 int rotdelay = ROTDELAY; /* rotational delay between blocks */
197 int maxbpg; /* maximum blocks per file in a cyl group */
198 int nrpos = NRPOS; /* # of distinguished rotational positions */
199 int avgfilesize = AVFILESIZ;/* expected average file size */
200 int avgfilesperdir = AFPDIR;/* expected number of files per directory */
201 int bbsize = BBSIZE; /* boot block size */
202 int sbsize = SBSIZE; /* superblock size */
203 int mntflags = MNT_ASYNC; /* flags to be passed to mount */
204 int t_or_u_flag = 0; /* user has specified -t or -u */
205 u_long memleft; /* virtual memory available */
206 caddr_t membase; /* start address of memory based filesystem */
213 char device[MAXPATHLEN];
216 extern void mkfs __P((struct partition *, char *, int, int));
217 static void usage __P((void));
225 register struct partition *pp;
226 register struct disklabel *lp;
227 struct disklabel mfsfakelabel;
228 struct disklabel *getdisklabel();
229 struct partition oldpartition;
232 int fsi, fso, len, n, vflag;
233 char *cp, *s1, *s2, *special, *opstring;
241 if ((progname = strrchr(*argv, '/')))
246 if (strstr(progname, "mfs")) {
252 "NF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:v" :
253 "NOS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
254 while ((ch = getopt(argc, argv, opstring)) != -1)
263 if ((sectorsize = atoi(optarg)) <= 0)
264 fatal("%s: bad sector size", optarg);
278 if ((maxcontig = atoi(optarg)) <= 0)
279 fatal("%s: bad maximum contiguous blocks",
283 if ((bsize = atoi(optarg)) < MINBSIZE)
284 fatal("%s: bad block size", optarg);
287 if ((cpg = atoi(optarg)) <= 0)
288 fatal("%s: bad cylinders/group", optarg);
292 if ((rotdelay = atoi(optarg)) < 0)
293 fatal("%s: bad rotational delay", optarg);
296 if ((maxbpg = atoi(optarg)) <= 0)
297 fatal("%s: bad blocks per file in a cylinder group",
301 if ((fsize = atoi(optarg)) <= 0)
302 fatal("%s: bad fragment size", optarg);
305 if ((avgfilesize = atoi(optarg)) <= 0)
306 fatal("%s: bad average file size", optarg);
309 if ((avgfilesperdir = atoi(optarg)) <= 0)
310 fatal("%s: bad average files per dir", optarg);
313 if ((density = atoi(optarg)) <= 0)
314 fatal("%s: bad bytes per inode", optarg);
317 if ((trackskew = atoi(optarg)) < 0)
318 fatal("%s: bad track skew", optarg);
321 if ((interleave = atoi(optarg)) <= 0)
322 fatal("%s: bad interleave", optarg);
325 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
326 fatal("%s: bad free space %%", optarg);
329 if ((nrpos = atoi(optarg)) < 0)
330 fatal("%s: bad rotational layout count",
337 getmntopts(optarg, mopts, &mntflags, 0);
339 if (strcmp(optarg, "space") == 0)
341 else if (strcmp(optarg, "time") == 0)
344 fatal("%s: unknown optimization preference: use `space' or `time'", optarg);
348 if ((trackspares = atoi(optarg)) < 0)
349 fatal("%s: bad spare sectors per track",
353 if ((rpm = atoi(optarg)) <= 0)
354 fatal("%s: bad revolutions/minute", optarg);
357 if ((fssize = atoi(optarg)) <= 0)
358 fatal("%s: bad file system size", optarg);
362 if ((ntracks = atoi(optarg)) < 0)
363 fatal("%s: bad total tracks", optarg);
367 if ((nsectors = atoi(optarg)) < 0)
368 fatal("%s: bad sectors/track", optarg);
374 if ((cylspares = atoi(optarg)) < 0)
375 fatal("%s: bad spare sectors per cylinder",
385 if (argc != 2 && (mfs || argc != 1))
389 /* Copy the NetBSD way of faking up a disk label */
390 if (mfs && !strcmp(special, "swap")) {
392 * it's an MFS, mounted on "swap." fake up a label.
395 fso = -1; /* XXX; normally done below. */
397 memset(&mfsfakelabel, 0, sizeof(mfsfakelabel));
398 mfsfakelabel.d_secsize = 512;
399 mfsfakelabel.d_nsectors = 64;
400 mfsfakelabel.d_ntracks = 16;
401 mfsfakelabel.d_ncylinders = 16;
402 mfsfakelabel.d_secpercyl = 1024;
403 mfsfakelabel.d_secperunit = 16384;
404 mfsfakelabel.d_rpm = 3600;
405 mfsfakelabel.d_interleave = 1;
406 mfsfakelabel.d_npartitions = 1;
407 mfsfakelabel.d_partitions[0].p_size = 16384;
408 mfsfakelabel.d_partitions[0].p_fsize = 1024;
409 mfsfakelabel.d_partitions[0].p_frag = 8;
410 mfsfakelabel.d_partitions[0].p_cpg = 16;
413 pp = &mfsfakelabel.d_partitions[0];
418 cp = strrchr(special, '/');
421 * No path prefix; try /dev/%s.
423 (void)snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
429 fso = open(special, O_WRONLY);
431 fatal("%s: %s", special, strerror(errno));
433 /* Bail if target special is mounted */
434 n = getmntinfo(&mp, MNT_NOWAIT);
436 fatal("%s: getmntinfo: %s", special, strerror(errno));
438 len = sizeof(_PATH_DEV) - 1;
440 if (strncmp(_PATH_DEV, s1, len) == 0)
444 s2 = mp->f_mntfromname;
445 if (strncmp(_PATH_DEV, s2, len) == 0) {
449 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
450 fatal("%s is mounted on %s",
451 special, mp->f_mntonname);
455 if (mfs && disktype != NULL) {
456 lp = (struct disklabel *)getdiskbyname(disktype);
458 fatal("%s: unknown disk type", disktype);
459 pp = &lp->d_partitions[1];
461 fsi = open(special, O_RDONLY);
463 fatal("%s: %s", special, strerror(errno));
464 if (fstat(fsi, &st) < 0)
465 fatal("%s: %s", special, strerror(errno));
466 if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
467 printf("%s: %s: not a character-special device\n",
469 cp = strchr(argv[0], '\0');
471 fatal("null special file name");
473 if (!vflag && (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
474 fatal("%s: can't figure out file system partition",
477 if (!mfs && disktype == NULL)
480 lp = getdisklabel(special, fsi);
481 if (vflag || isdigit(*cp))
482 pp = &lp->d_partitions[0];
484 pp = &lp->d_partitions[*cp - 'a'];
486 fatal("%s: `%c' partition is unavailable",
488 if (pp->p_fstype == FS_BOOT)
489 fatal("%s: `%c' partition overlaps boot program",
495 if (fssize > pp->p_size && !mfs)
496 fatal("%s: maximum file system size on the `%c' partition is %d",
497 argv[0], *cp, pp->p_size);
504 ntracks = lp->d_ntracks;
506 fatal("%s: no default #tracks", argv[0]);
509 nsectors = lp->d_nsectors;
511 fatal("%s: no default #sectors/track", argv[0]);
513 if (sectorsize == 0) {
514 sectorsize = lp->d_secsize;
516 fatal("%s: no default sector size", argv[0]);
518 if (trackskew == -1) {
519 trackskew = lp->d_trackskew;
523 if (interleave == 0) {
524 interleave = lp->d_interleave;
531 fsize = MAX(DFL_FRAGSIZE, lp->d_secsize);
534 bsize = pp->p_frag * pp->p_fsize;
536 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
539 * Maxcontig sets the default for the maximum number of blocks
540 * that may be allocated sequentially. With filesystem clustering
541 * it is possible to allocate contiguous blocks up to the maximum
542 * transfer size permitted by the controller or buffering.
545 maxcontig = MAX(1, MAXPHYS / bsize - 1);
547 density = NFPI * fsize;
548 if (minfree < MINFREE && opt != FS_OPTSPACE) {
549 fprintf(stderr, "Warning: changing optimization to space ");
550 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
553 if (trackspares == -1) {
554 trackspares = lp->d_sparespertrack;
558 nphyssectors = nsectors + trackspares;
559 if (cylspares == -1) {
560 cylspares = lp->d_sparespercyl;
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 && secpercyl != lp->d_secpercyl)
571 fprintf(stderr, "%s (%d) %s (%lu)\n",
572 "Warning: calculated sectors per cylinder", secpercyl,
573 "disagrees with disk label", (u_long)lp->d_secpercyl);
575 maxbpg = MAXBLKPG(bsize);
576 headswitch = lp->d_headswitch;
577 trackseek = lp->d_trkseek;
578 #ifdef notdef /* label may be 0 if faked up by kernel */
579 bbsize = lp->d_bbsize;
580 sbsize = lp->d_sbsize;
584 realsectorsize = sectorsize;
585 if (sectorsize != DEV_BSIZE) { /* XXX */
586 int secperblk = DEV_BSIZE / sectorsize;
588 sectorsize = DEV_BSIZE;
589 nsectors /= secperblk;
590 nphyssectors /= secperblk;
591 secpercyl /= secperblk;
593 pp->p_size /= secperblk;
596 realsectorsize = sectorsize;
597 if (sectorsize != DEV_BSIZE) { /* XXX */
598 int secperblk = sectorsize / DEV_BSIZE;
600 sectorsize = DEV_BSIZE;
601 nsectors *= secperblk;
602 nphyssectors *= secperblk;
603 secpercyl *= secperblk;
605 pp->p_size *= secperblk;
611 stat(mfs_mtpt, &mfs_mtstat) < 0 ||
612 !S_ISDIR(mfs_mtstat.st_mode)
614 fatal("mount point not dir: %s", mfs_mtpt);
617 mkfs(pp, special, fsi, fso);
619 if (realsectorsize != DEV_BSIZE)
620 pp->p_size *= DEV_BSIZE / realsectorsize;
622 if (realsectorsize != DEV_BSIZE)
623 pp->p_size /= realsectorsize /DEV_BSIZE;
625 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
626 rewritelabel(special, fso, lp);
632 struct mfs_args args;
634 snprintf(buf, sizeof(buf), "mfs:%d", getpid());
636 args.export.ex_root = -2;
637 if (mntflags & MNT_RDONLY)
638 args.export.ex_flags = MNT_EXRDONLY;
640 args.export.ex_flags = 0;
642 args.size = fssize * sectorsize;
644 error = getvfsbyname("mfs", &vfc);
645 if (error && vfsisloadable("mfs")) {
647 fatal("vfsload(mfs)");
648 endvfsent(); /* flush cache */
649 error = getvfsbyname("mfs", &vfc);
652 fatal("mfs filesystem not available");
654 if (mount(vfc.vfc_name, argv[1], mntflags, &args) < 0)
655 fatal("%s: %s", argv[1], strerror(errno));
657 munmap(membase,fssize * sectorsize);
665 char lmsg[] = "%s: can't read disk label; disk type must be specified";
667 char lmsg[] = "%s: can't read disk label";
675 static struct disklabel lab;
677 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
680 struct disklabel *lp, *getdiskbyname();
683 lp = getdiskbyname(disktype);
685 fatal("%s: unknown disk type", disktype);
689 warn("ioctl (GDINFO)");
695 rewritelabel(s, fd, lp)
698 register struct disklabel *lp;
705 lp->d_checksum = dkcksum(lp);
706 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
707 warn("ioctl (WDINFO)");
708 fatal("%s: can't rewrite disk label", s);
715 fatal(const char *fmt, ...)
729 if (fcntl(STDERR_FILENO, F_GETFL) < 0) {
730 openlog(progname, LOG_CONS, LOG_DAEMON);
731 vsyslog(LOG_ERR, fmt, ap);
746 "usage: %s [ -fsoptions ] special-device mount-point\n",
750 "usage: %s [ -fsoptions ] special-device%s\n",
757 fprintf(stderr, "where fsoptions are:\n");
759 "\t-N do not create file system, just print out parameters\n");
760 fprintf(stderr, "\t-O create a 4.3BSD format filesystem\n");
761 fprintf(stderr, "\t-S sector size\n");
763 fprintf(stderr, "\t-T disktype\n");
765 fprintf(stderr, "\t-U enable soft updates\n");
766 fprintf(stderr, "\t-a maximum contiguous blocks\n");
767 fprintf(stderr, "\t-b block size\n");
768 fprintf(stderr, "\t-c cylinders/group\n");
769 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
770 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
771 fprintf(stderr, "\t-f frag size\n");
772 fprintf(stderr, "\t-g average file size\n");
773 fprintf(stderr, "\t-h average files per directory\n");
774 fprintf(stderr, "\t-i number of bytes per inode\n");
775 fprintf(stderr, "\t-k sector 0 skew, per track\n");
776 fprintf(stderr, "\t-l hardware sector interleave\n");
777 fprintf(stderr, "\t-m minimum free space %%\n");
778 fprintf(stderr, "\t-n number of distinguished rotational positions\n");
779 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
780 fprintf(stderr, "\t-p spare sectors per track\n");
781 fprintf(stderr, "\t-s file system size (sectors)\n");
782 fprintf(stderr, "\t-r revolutions/minute\n");
783 fprintf(stderr, "\t-t tracks/cylinder\n");
784 fprintf(stderr, "\t-u sectors/track\n");
786 "\t-v do not attempt to determine partition name from device name\n");
787 fprintf(stderr, "\t-x spare sectors per cylinder\n");