2 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * This software was developed for the FreeBSD Project by Marshall
6 * Kirk McKusick and Network Associates Laboratories, the Security
7 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * Copyright (c) 1982, 1989, 1993
12 * The Regents of the University of California. All rights reserved.
13 * (c) UNIX System Laboratories, Inc.
14 * Copyright (c) 1983, 1989, 1993, 1994
15 * The Regents of the University of California. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 static const char copyright[] =
48 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
49 The Regents of the University of California. All rights reserved.\n";
54 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95";
56 static const char rcsid[] =
61 * newfs: friendly front end to mkfs
63 #include <sys/param.h>
66 #include <sys/disklabel.h>
68 #include <sys/mount.h>
70 #include <ufs/ufs/dir.h>
71 #include <ufs/ufs/dinode.h>
72 #include <ufs/ffs/fs.h>
73 #include <ufs/ufs/ufsmount.h>
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 MAXBLKSPERCG blocks. The actual
100 * number used depends upon how much information can be stored
101 * in a cylinder group map which must fit in a single file system
102 * block. The default is to use as many as possible blocks per group.
104 #define MAXBLKSPERCG 0x7fffffff /* desired fs_fpg ("infinity") */
107 * MAXBLKPG determines the maximum number of data blocks which are
108 * placed in a single cylinder group. The default is one indirect
109 * block worth of data blocks.
111 #define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t))
114 * Each file system has a number of inodes statically allocated.
115 * We allocate one inode slot per NFPI fragments, expecting this
116 * to be far more than we will ever need.
120 int Nflag; /* run without writing file system */
121 int Oflag = 1; /* file system format (1 => UFS1, 2 => UFS2) */
122 int Rflag; /* regression test */
123 int Uflag; /* enable soft updates for file system */
124 quad_t fssize; /* file system size */
125 int sectorsize; /* bytes/sector */
126 int realsectorsize; /* bytes/sector in hardware */
127 int fsize = 0; /* fragment size */
128 int bsize = 0; /* block size */
129 int maxbsize = 0; /* maximum clustering */
130 int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
131 int minfree = MINFREE; /* free space threshold */
132 int opt = DEFAULTOPT; /* optimization preference (space or time) */
133 int density; /* number of bytes per inode */
134 int maxcontig = 0; /* max contiguous blocks to allocate */
135 int maxbpg; /* maximum blocks per file in a cyl group */
136 int avgfilesize = AVFILESIZ;/* expected average file size */
137 int avgfilesperdir = AFPDIR;/* expected number of files per directory */
138 int fso; /* filedescriptor to device */
140 static char device[MAXPATHLEN];
141 static char *disktype;
142 static int unlabeled;
144 static struct disklabel *getdisklabel(char *s);
145 static void rewritelabel(char *s, struct disklabel *lp);
146 static void usage(void);
149 main(int argc, char *argv[])
151 struct partition *pp;
152 struct disklabel *lp;
153 struct partition oldpartition;
159 while ((ch = getopt(argc, argv,
160 "NO:RS:T:Ua:b:c:d:e:f:g:h:i:m:o:s:")) != -1)
166 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
167 errx(1, "%s: bad file system format value",
174 if ((sectorsize = atoi(optarg)) <= 0)
175 errx(1, "%s: bad sector size", optarg);
184 if ((maxcontig = atoi(optarg)) <= 0)
185 errx(1, "%s: bad maximum contiguous blocks",
189 if ((bsize = atoi(optarg)) < MINBSIZE)
190 errx(1, "%s: bad block size", optarg);
193 if ((maxblkspercg = atoi(optarg)) <= 0)
194 errx(1, "%s: bad blocks per cylinder group",
198 if ((maxbsize = atoi(optarg)) < MINBSIZE)
199 errx(1, "%s: bad extent block size", optarg);
202 if ((maxbpg = atoi(optarg)) <= 0)
203 errx(1, "%s: bad blocks per file in a cylinder group",
207 if ((fsize = atoi(optarg)) <= 0)
208 errx(1, "%s: bad fragment size", optarg);
211 if ((avgfilesize = atoi(optarg)) <= 0)
212 errx(1, "%s: bad average file size", optarg);
215 if ((avgfilesperdir = atoi(optarg)) <= 0)
216 errx(1, "%s: bad average files per dir", optarg);
219 if ((density = atoi(optarg)) <= 0)
220 errx(1, "%s: bad bytes per inode", optarg);
223 if ((minfree = atoi(optarg)) < 0 || minfree > 99)
224 errx(1, "%s: bad free space %%", optarg);
227 if (strcmp(optarg, "space") == 0)
229 else if (strcmp(optarg, "time") == 0)
233 "%s: unknown optimization preference: use `space' or `time'",
237 if ((fssize = atoi(optarg)) <= 0)
238 errx(1, "%s: bad file system size", optarg);
251 cp = strrchr(special, '/');
254 * No path prefix; try prefixing _PATH_DEV.
256 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
260 fso = open(special, Nflag ? O_RDONLY : O_RDWR);
262 err(1, "%s", special);
263 if (fstat(fso, &st) < 0)
264 err(1, "%s", special);
265 if ((st.st_mode & S_IFMT) != S_IFCHR)
266 errx(1, "%s: not a character-special device", special);
269 ioctl(fso, DIOCGSECTORSIZE, §orsize);
270 if (sectorsize && !ioctl(fso, DIOCGMEDIASIZE, &mediasize)) {
272 fssize = mediasize / sectorsize;
273 else if (fssize > mediasize / sectorsize)
274 errx(1, "%s: maximum file system size is %u",
275 special, (u_int)(mediasize / sectorsize));
278 lp = getdisklabel(special);
280 cp = strchr(special, '\0');
282 if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))
283 errx(1, "%s: can't figure out file system partition",
286 pp = &lp->d_partitions[RAW_PART];
288 pp = &lp->d_partitions[*cp - 'a'];
291 errx(1, "%s: `%c' partition is unavailable",
293 if (pp->p_fstype == FS_BOOT)
294 errx(1, "%s: `%c' partition overlaps boot program",
298 if (fssize > pp->p_size)
300 "%s: maximum file system size %d", special, pp->p_size);
302 sectorsize = lp->d_secsize;
306 bsize = pp->p_frag * pp->p_fsize;
309 errx(1, "%s: no default sector size", special);
311 fsize = MAX(DFL_FRAGSIZE, sectorsize);
313 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
317 * Maxcontig sets the default for the maximum number of blocks
318 * that may be allocated sequentially. With file system clustering
319 * it is possible to allocate contiguous blocks up to the maximum
320 * transfer size permitted by the controller or buffering.
323 maxcontig = MAX(1, MAXPHYS / bsize);
325 density = NFPI * fsize;
326 if (minfree < MINFREE && opt != FS_OPTSPACE) {
327 fprintf(stderr, "Warning: changing optimization to space ");
328 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
332 maxbpg = MAXBLKPG(bsize);
333 realsectorsize = sectorsize;
334 if (sectorsize != DEV_BSIZE) { /* XXX */
335 int secperblk = sectorsize / DEV_BSIZE;
337 sectorsize = DEV_BSIZE;
340 pp->p_size *= secperblk;
344 if (realsectorsize != DEV_BSIZE)
345 pp->p_size /= realsectorsize / DEV_BSIZE;
346 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
347 rewritelabel(special, lp);
354 getdisklabel(char *s)
356 static struct disklabel lab;
357 struct disklabel *lp;
359 if (!ioctl(fso, DIOCGDINFO, (char *)&lab))
363 lp = getdiskbyname(disktype);
371 rewritelabel(char *s, struct disklabel *lp)
376 lp->d_checksum = dkcksum(lp);
377 if (ioctl(fso, DIOCWDINFO, (char *)lp) < 0)
378 warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
385 "usage: %s [ -fsoptions ] special-device%s\n",
388 fprintf(stderr, "where fsoptions are:\n");
390 "\t-N do not create file system, just print out parameters\n");
391 fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
392 fprintf(stderr, "\t-R regression test, supress random factors\n");
393 fprintf(stderr, "\t-S sector size\n");
394 fprintf(stderr, "\t-T disktype\n");
395 fprintf(stderr, "\t-U enable soft updates\n");
396 fprintf(stderr, "\t-a maximum contiguous blocks\n");
397 fprintf(stderr, "\t-b block size\n");
398 fprintf(stderr, "\t-c blocks per cylinders group\n");
399 fprintf(stderr, "\t-d maximum extent size\n");
400 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
401 fprintf(stderr, "\t-f frag size\n");
402 fprintf(stderr, "\t-g average file size\n");
403 fprintf(stderr, "\t-h average files per directory\n");
404 fprintf(stderr, "\t-i number of bytes per inode\n");
405 fprintf(stderr, "\t-m minimum free space %%\n");
406 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
407 fprintf(stderr, "\t-s file systemsize (sectors)\n");