1 /* 2 * Copyright (c) 2002 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 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 9 * research program 10 * 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. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 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. 32 * 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 43 * SUCH DAMAGE. 44 */ 45 46 #ifndef lint 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"; 50 #endif /* not lint */ 51 52 #ifndef lint 53 #if 0 54 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95"; 55 #endif 56 static const char rcsid[] = 57 "$FreeBSD$"; 58 #endif /* not lint */ 59 60 /* 61 * newfs: friendly front end to mkfs 62 */ 63 #include <sys/param.h> 64 #include <sys/stat.h> 65 #include <sys/disk.h> 66 #include <sys/disklabel.h> 67 #include <sys/file.h> 68 #include <sys/mount.h> 69 70 #include <ufs/ufs/dir.h> 71 #include <ufs/ufs/dinode.h> 72 #include <ufs/ffs/fs.h> 73 #include <ufs/ufs/ufsmount.h> 74 75 #include <ctype.h> 76 #include <err.h> 77 #include <errno.h> 78 #include <paths.h> 79 #include <stdarg.h> 80 #include <stdio.h> 81 #include <stdlib.h> 82 #include <string.h> 83 #include <syslog.h> 84 #include <unistd.h> 85 86 #include "newfs.h" 87 88 /* 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 94 */ 95 #define DFL_FRAGSIZE 2048 96 #define DFL_BLKSIZE 16384 97 98 /* 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. 103 */ 104 #define MAXBLKSPERCG 0x7fffffff /* desired fs_fpg ("infinity") */ 105 106 /* 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. 110 */ 111 #define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t)) 112 113 /* 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. 117 */ 118 #define NFPI 4 119 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 */ 139 140 static char device[MAXPATHLEN]; 141 static char *disktype; 142 static int unlabeled; 143 144 static struct disklabel *getdisklabel(char *s); 145 static void rewritelabel(char *s, struct disklabel *lp); 146 static void usage(void); 147 148 int 149 main(int argc, char *argv[]) 150 { 151 struct partition *pp; 152 struct disklabel *lp; 153 struct partition oldpartition; 154 struct stat st; 155 char *cp, *special; 156 int ch; 157 off_t mediasize; 158 159 while ((ch = getopt(argc, argv, 160 "NO:RS:T:Ua:b:c:d:e:f:g:h:i:m:o:s:")) != -1) 161 switch (ch) { 162 case 'N': 163 Nflag = 1; 164 break; 165 case 'O': 166 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 167 errx(1, "%s: bad file system format value", 168 optarg); 169 break; 170 case 'R': 171 Rflag = 1; 172 break; 173 case 'S': 174 if ((sectorsize = atoi(optarg)) <= 0) 175 errx(1, "%s: bad sector size", optarg); 176 break; 177 case 'T': 178 disktype = optarg; 179 break; 180 case 'U': 181 Uflag = 1; 182 break; 183 case 'a': 184 if ((maxcontig = atoi(optarg)) <= 0) 185 errx(1, "%s: bad maximum contiguous blocks", 186 optarg); 187 break; 188 case 'b': 189 if ((bsize = atoi(optarg)) < MINBSIZE) 190 errx(1, "%s: block size too small, min is %d", 191 optarg, MINBSIZE); 192 if (bsize > MAXBSIZE) 193 errx(1, "%s: block size too large, max is %d", 194 optarg, MAXBSIZE); 195 break; 196 case 'c': 197 if ((maxblkspercg = atoi(optarg)) <= 0) 198 errx(1, "%s: bad blocks per cylinder group", 199 optarg); 200 break; 201 case 'd': 202 if ((maxbsize = atoi(optarg)) < MINBSIZE) 203 errx(1, "%s: bad extent block size", optarg); 204 break; 205 case 'e': 206 if ((maxbpg = atoi(optarg)) <= 0) 207 errx(1, "%s: bad blocks per file in a cylinder group", 208 optarg); 209 break; 210 case 'f': 211 if ((fsize = atoi(optarg)) <= 0) 212 errx(1, "%s: bad fragment size", optarg); 213 break; 214 case 'g': 215 if ((avgfilesize = atoi(optarg)) <= 0) 216 errx(1, "%s: bad average file size", optarg); 217 break; 218 case 'h': 219 if ((avgfilesperdir = atoi(optarg)) <= 0) 220 errx(1, "%s: bad average files per dir", optarg); 221 break; 222 case 'i': 223 if ((density = atoi(optarg)) <= 0) 224 errx(1, "%s: bad bytes per inode", optarg); 225 break; 226 case 'm': 227 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 228 errx(1, "%s: bad free space %%", optarg); 229 break; 230 case 'o': 231 if (strcmp(optarg, "space") == 0) 232 opt = FS_OPTSPACE; 233 else if (strcmp(optarg, "time") == 0) 234 opt = FS_OPTTIME; 235 else 236 errx(1, 237 "%s: unknown optimization preference: use `space' or `time'", 238 optarg); 239 break; 240 case 's': 241 if ((fssize = atoi(optarg)) <= 0) 242 errx(1, "%s: bad file system size", optarg); 243 break; 244 case '?': 245 default: 246 usage(); 247 } 248 argc -= optind; 249 argv += optind; 250 251 if (argc != 1) 252 usage(); 253 254 special = argv[0]; 255 cp = strrchr(special, '/'); 256 if (cp == 0) { 257 /* 258 * No path prefix; try prefixing _PATH_DEV. 259 */ 260 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); 261 special = device; 262 } 263 264 fso = open(special, Nflag ? O_RDONLY : O_RDWR); 265 if (fso < 0) 266 err(1, "%s", special); 267 if (fstat(fso, &st) < 0) 268 err(1, "%s", special); 269 if ((st.st_mode & S_IFMT) != S_IFCHR) 270 errx(1, "%s: not a character-special device", special); 271 272 if (sectorsize == 0) 273 ioctl(fso, DIOCGSECTORSIZE, §orsize); 274 if (sectorsize && !ioctl(fso, DIOCGMEDIASIZE, &mediasize)) { 275 if (fssize == 0) 276 fssize = mediasize / sectorsize; 277 else if (fssize > mediasize / sectorsize) 278 errx(1, "%s: maximum file system size is %u", 279 special, (u_int)(mediasize / sectorsize)); 280 } 281 pp = NULL; 282 lp = getdisklabel(special); 283 if (lp != NULL) { 284 cp = strchr(special, '\0'); 285 cp--; 286 if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 287 errx(1, "%s: can't figure out file system partition", 288 special); 289 if (isdigit(*cp)) 290 pp = &lp->d_partitions[RAW_PART]; 291 else 292 pp = &lp->d_partitions[*cp - 'a']; 293 oldpartition = *pp; 294 if (pp->p_size == 0) 295 errx(1, "%s: `%c' partition is unavailable", 296 special, *cp); 297 if (pp->p_fstype == FS_BOOT) 298 errx(1, "%s: `%c' partition overlaps boot program", 299 special, *cp); 300 if (fssize == 0) 301 fssize = pp->p_size; 302 if (fssize > pp->p_size) 303 errx(1, 304 "%s: maximum file system size %d", special, pp->p_size); 305 if (sectorsize == 0) 306 sectorsize = lp->d_secsize; 307 if (fsize == 0) 308 fsize = pp->p_fsize; 309 if (bsize == 0) 310 bsize = pp->p_frag * pp->p_fsize; 311 } 312 if (sectorsize <= 0) 313 errx(1, "%s: no default sector size", special); 314 if (fsize <= 0) 315 fsize = MAX(DFL_FRAGSIZE, sectorsize); 316 if (bsize <= 0) 317 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 318 if (maxbsize == 0) 319 maxbsize = bsize; 320 /* 321 * Maxcontig sets the default for the maximum number of blocks 322 * that may be allocated sequentially. With file system clustering 323 * it is possible to allocate contiguous blocks up to the maximum 324 * transfer size permitted by the controller or buffering. 325 */ 326 if (maxcontig == 0) 327 maxcontig = MAX(1, MAXPHYS / bsize); 328 if (density == 0) 329 density = NFPI * fsize; 330 if (minfree < MINFREE && opt != FS_OPTSPACE) { 331 fprintf(stderr, "Warning: changing optimization to space "); 332 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 333 opt = FS_OPTSPACE; 334 } 335 if (maxbpg == 0) 336 maxbpg = MAXBLKPG(bsize); 337 realsectorsize = sectorsize; 338 if (sectorsize != DEV_BSIZE) { /* XXX */ 339 int secperblk = sectorsize / DEV_BSIZE; 340 341 sectorsize = DEV_BSIZE; 342 fssize *= secperblk; 343 if (pp != NULL) 344 pp->p_size *= secperblk; 345 } 346 mkfs(pp, special); 347 if (!unlabeled) { 348 if (realsectorsize != DEV_BSIZE) 349 pp->p_size /= realsectorsize / DEV_BSIZE; 350 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 351 rewritelabel(special, lp); 352 } 353 close(fso); 354 exit(0); 355 } 356 357 struct disklabel * 358 getdisklabel(char *s) 359 { 360 static struct disklabel lab; 361 struct disklabel *lp; 362 363 if (!ioctl(fso, DIOCGDINFO, (char *)&lab)) 364 return (&lab); 365 unlabeled++; 366 if (disktype) { 367 lp = getdiskbyname(disktype); 368 if (lp != NULL) 369 return (lp); 370 } 371 return (NULL); 372 } 373 374 void 375 rewritelabel(char *s, struct disklabel *lp) 376 { 377 if (unlabeled) 378 return; 379 lp->d_checksum = 0; 380 lp->d_checksum = dkcksum(lp); 381 if (ioctl(fso, DIOCWDINFO, (char *)lp) < 0) 382 warn("ioctl (WDINFO): %s: can't rewrite disk label", s); 383 } 384 385 static void 386 usage() 387 { 388 fprintf(stderr, 389 "usage: %s [ -fsoptions ] special-device%s\n", 390 getprogname(), 391 " [device-type]"); 392 fprintf(stderr, "where fsoptions are:\n"); 393 fprintf(stderr, 394 "\t-N do not create file system, just print out parameters\n"); 395 fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n"); 396 fprintf(stderr, "\t-R regression test, supress random factors\n"); 397 fprintf(stderr, "\t-S sector size\n"); 398 fprintf(stderr, "\t-T disktype\n"); 399 fprintf(stderr, "\t-U enable soft updates\n"); 400 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 401 fprintf(stderr, "\t-b block size\n"); 402 fprintf(stderr, "\t-c blocks per cylinders group\n"); 403 fprintf(stderr, "\t-d maximum extent size\n"); 404 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 405 fprintf(stderr, "\t-f frag size\n"); 406 fprintf(stderr, "\t-g average file size\n"); 407 fprintf(stderr, "\t-h average files per directory\n"); 408 fprintf(stderr, "\t-i number of bytes per inode\n"); 409 fprintf(stderr, "\t-m minimum free space %%\n"); 410 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 411 fprintf(stderr, "\t-s file systemsize (sectors)\n"); 412 exit(1); 413 } 414