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 139 static char device[MAXPATHLEN]; 140 static char *disktype; 141 static int unlabeled; 142 143 static struct disklabel *getdisklabel(char *s); 144 static void rewritelabel(char *s, struct disklabel *lp); 145 static void usage(void); 146 147 int 148 main(int argc, char *argv[]) 149 { 150 struct partition *pp; 151 struct disklabel *lp; 152 struct partition oldpartition; 153 struct stat st; 154 char *cp, *special; 155 int ch; 156 off_t mediasize; 157 158 while ((ch = getopt(argc, argv, 159 "NO:RS:T:Ua:b:c:d:e:f:g:h:i:m:o:s:")) != -1) 160 switch (ch) { 161 case 'N': 162 Nflag = 1; 163 break; 164 case 'O': 165 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 166 errx(1, "%s: bad file system format value", 167 optarg); 168 break; 169 case 'R': 170 Rflag = 1; 171 break; 172 case 'S': 173 if ((sectorsize = atoi(optarg)) <= 0) 174 errx(1, "%s: bad sector size", optarg); 175 break; 176 case 'T': 177 disktype = optarg; 178 break; 179 case 'U': 180 Uflag = 1; 181 break; 182 case 'a': 183 if ((maxcontig = atoi(optarg)) <= 0) 184 errx(1, "%s: bad maximum contiguous blocks", 185 optarg); 186 break; 187 case 'b': 188 if ((bsize = atoi(optarg)) < MINBSIZE) 189 errx(1, "%s: block size too small, min is %d", 190 optarg, MINBSIZE); 191 if (bsize > MAXBSIZE) 192 errx(1, "%s: block size too large, max is %d", 193 optarg, MAXBSIZE); 194 break; 195 case 'c': 196 if ((maxblkspercg = atoi(optarg)) <= 0) 197 errx(1, "%s: bad blocks per cylinder group", 198 optarg); 199 break; 200 case 'd': 201 if ((maxbsize = atoi(optarg)) < MINBSIZE) 202 errx(1, "%s: bad extent block size", optarg); 203 break; 204 case 'e': 205 if ((maxbpg = atoi(optarg)) <= 0) 206 errx(1, "%s: bad blocks per file in a cylinder group", 207 optarg); 208 break; 209 case 'f': 210 if ((fsize = atoi(optarg)) <= 0) 211 errx(1, "%s: bad fragment size", optarg); 212 break; 213 case 'g': 214 if ((avgfilesize = atoi(optarg)) <= 0) 215 errx(1, "%s: bad average file size", optarg); 216 break; 217 case 'h': 218 if ((avgfilesperdir = atoi(optarg)) <= 0) 219 errx(1, "%s: bad average files per dir", optarg); 220 break; 221 case 'i': 222 if ((density = atoi(optarg)) <= 0) 223 errx(1, "%s: bad bytes per inode", optarg); 224 break; 225 case 'm': 226 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 227 errx(1, "%s: bad free space %%", optarg); 228 break; 229 case 'o': 230 if (strcmp(optarg, "space") == 0) 231 opt = FS_OPTSPACE; 232 else if (strcmp(optarg, "time") == 0) 233 opt = FS_OPTTIME; 234 else 235 errx(1, 236 "%s: unknown optimization preference: use `space' or `time'", 237 optarg); 238 break; 239 case 's': 240 if ((fssize = atoi(optarg)) <= 0) 241 errx(1, "%s: bad file system size", optarg); 242 break; 243 case '?': 244 default: 245 usage(); 246 } 247 argc -= optind; 248 argv += optind; 249 250 if (argc != 1) 251 usage(); 252 253 special = argv[0]; 254 cp = strrchr(special, '/'); 255 if (cp == 0) { 256 /* 257 * No path prefix; try prefixing _PATH_DEV. 258 */ 259 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); 260 special = device; 261 } 262 263 if (ufs_disk_fillout_blank(&disk, special) == -1) { 264 if (disk.d_error != NULL) 265 errx(1, "%s: %s", special, disk.d_error); 266 else 267 err(1, "%s", special); 268 } 269 if (fstat(disk.d_fd, &st) < 0) 270 err(1, "%s", special); 271 if ((st.st_mode & S_IFMT) != S_IFCHR) 272 errx(1, "%s: not a character-special device", special); 273 274 if (sectorsize == 0) 275 ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize); 276 if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) { 277 if (fssize == 0) 278 fssize = mediasize / sectorsize; 279 else if (fssize > mediasize / sectorsize) 280 errx(1, "%s: maximum file system size is %u", 281 special, (u_int)(mediasize / sectorsize)); 282 } 283 pp = NULL; 284 lp = getdisklabel(special); 285 if (lp != NULL) { 286 cp = strchr(special, '\0'); 287 cp--; 288 if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 289 errx(1, "%s: can't figure out file system partition", 290 special); 291 if (isdigit(*cp)) 292 pp = &lp->d_partitions[RAW_PART]; 293 else 294 pp = &lp->d_partitions[*cp - 'a']; 295 oldpartition = *pp; 296 if (pp->p_size == 0) 297 errx(1, "%s: `%c' partition is unavailable", 298 special, *cp); 299 if (pp->p_fstype == FS_BOOT) 300 errx(1, "%s: `%c' partition overlaps boot program", 301 special, *cp); 302 if (fssize == 0) 303 fssize = pp->p_size; 304 if (fssize > pp->p_size) 305 errx(1, 306 "%s: maximum file system size %d", special, pp->p_size); 307 if (sectorsize == 0) 308 sectorsize = lp->d_secsize; 309 if (fsize == 0) 310 fsize = pp->p_fsize; 311 if (bsize == 0) 312 bsize = pp->p_frag * pp->p_fsize; 313 } 314 if (sectorsize <= 0) 315 errx(1, "%s: no default sector size", special); 316 if (fsize <= 0) 317 fsize = MAX(DFL_FRAGSIZE, sectorsize); 318 if (bsize <= 0) 319 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 320 if (maxbsize == 0) 321 maxbsize = bsize; 322 /* 323 * Maxcontig sets the default for the maximum number of blocks 324 * that may be allocated sequentially. With file system clustering 325 * it is possible to allocate contiguous blocks up to the maximum 326 * transfer size permitted by the controller or buffering. 327 */ 328 if (maxcontig == 0) 329 maxcontig = MAX(1, MAXPHYS / bsize); 330 if (density == 0) 331 density = NFPI * fsize; 332 if (minfree < MINFREE && opt != FS_OPTSPACE) { 333 fprintf(stderr, "Warning: changing optimization to space "); 334 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 335 opt = FS_OPTSPACE; 336 } 337 if (maxbpg == 0) 338 maxbpg = MAXBLKPG(bsize); 339 realsectorsize = sectorsize; 340 if (sectorsize != DEV_BSIZE) { /* XXX */ 341 int secperblk = sectorsize / DEV_BSIZE; 342 343 sectorsize = DEV_BSIZE; 344 fssize *= secperblk; 345 if (pp != NULL) 346 pp->p_size *= secperblk; 347 } 348 mkfs(pp, special); 349 if (!unlabeled) { 350 if (realsectorsize != DEV_BSIZE) 351 pp->p_size /= realsectorsize / DEV_BSIZE; 352 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 353 rewritelabel(special, lp); 354 } 355 ufs_disk_close(&disk); 356 exit(0); 357 } 358 359 struct disklabel * 360 getdisklabel(char *s) 361 { 362 static struct disklabel lab; 363 struct disklabel *lp; 364 365 if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab)) 366 return (&lab); 367 unlabeled++; 368 if (disktype) { 369 lp = getdiskbyname(disktype); 370 if (lp != NULL) 371 return (lp); 372 } 373 return (NULL); 374 } 375 376 void 377 rewritelabel(char *s, struct disklabel *lp) 378 { 379 if (unlabeled) 380 return; 381 lp->d_checksum = 0; 382 lp->d_checksum = dkcksum(lp); 383 if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0) 384 warn("ioctl (WDINFO): %s: can't rewrite disk label", s); 385 } 386 387 static void 388 usage(void) 389 { 390 fprintf(stderr, 391 "usage: %s [ -fsoptions ] special-device%s\n", 392 getprogname(), 393 " [device-type]"); 394 fprintf(stderr, "where fsoptions are:\n"); 395 fprintf(stderr, 396 "\t-N do not create file system, just print out parameters\n"); 397 fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n"); 398 fprintf(stderr, "\t-R regression test, supress random factors\n"); 399 fprintf(stderr, "\t-S sector size\n"); 400 fprintf(stderr, "\t-T disktype\n"); 401 fprintf(stderr, "\t-U enable soft updates\n"); 402 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 403 fprintf(stderr, "\t-b block size\n"); 404 fprintf(stderr, "\t-c blocks per cylinders group\n"); 405 fprintf(stderr, "\t-d maximum extent size\n"); 406 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 407 fprintf(stderr, "\t-f frag size\n"); 408 fprintf(stderr, "\t-g average file size\n"); 409 fprintf(stderr, "\t-h average files per directory\n"); 410 fprintf(stderr, "\t-i number of bytes per inode\n"); 411 fprintf(stderr, "\t-m minimum free space %%\n"); 412 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 413 fprintf(stderr, "\t-s file systemsize (sectors)\n"); 414 exit(1); 415 } 416