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) 1983, 1989, 1993, 1994 12 * The Regents of the University of California. All rights reserved. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 */ 42 43 #if 0 44 #ifndef lint 45 static const char copyright[] = 46 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\ 47 The Regents of the University of California. All rights reserved.\n"; 48 #endif /* not lint */ 49 50 #ifndef lint 51 static char sccsid[] = "@(#)newfs.c 8.13 (Berkeley) 5/1/95"; 52 #endif /* not lint */ 53 #endif 54 #include <sys/cdefs.h> 55 __FBSDID("$FreeBSD$"); 56 57 /* 58 * newfs: friendly front end to mkfs 59 */ 60 #include <sys/param.h> 61 #include <sys/stat.h> 62 #include <sys/disk.h> 63 #include <sys/disklabel.h> 64 #include <sys/file.h> 65 #include <sys/mount.h> 66 67 #include <ufs/ufs/dir.h> 68 #include <ufs/ufs/dinode.h> 69 #include <ufs/ffs/fs.h> 70 #include <ufs/ufs/ufsmount.h> 71 72 #include <ctype.h> 73 #include <err.h> 74 #include <errno.h> 75 #include <paths.h> 76 #include <stdarg.h> 77 #include <stdio.h> 78 #include <stdlib.h> 79 #include <string.h> 80 #include <syslog.h> 81 #include <unistd.h> 82 83 #include "newfs.h" 84 85 /* 86 * The following two constants set the default block and fragment sizes. 87 * Both constants must be a power of 2 and meet the following constraints: 88 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 89 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 90 * DESBLKSIZE / DESFRAGSIZE <= 8 91 */ 92 #define DFL_FRAGSIZE 2048 93 #define DFL_BLKSIZE 16384 94 95 /* 96 * Cylinder groups may have up to MAXBLKSPERCG blocks. The actual 97 * number used depends upon how much information can be stored 98 * in a cylinder group map which must fit in a single file system 99 * block. The default is to use as many as possible blocks per group. 100 */ 101 #define MAXBLKSPERCG 0x7fffffff /* desired fs_fpg ("infinity") */ 102 103 /* 104 * MAXBLKPG determines the maximum number of data blocks which are 105 * placed in a single cylinder group. The default is one indirect 106 * block worth of data blocks. 107 */ 108 #define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t)) 109 110 /* 111 * Each file system has a number of inodes statically allocated. 112 * We allocate one inode slot per NFPI fragments, expecting this 113 * to be far more than we will ever need. 114 */ 115 #define NFPI 4 116 117 int Lflag; /* add a volume label */ 118 int Nflag; /* run without writing file system */ 119 int Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 120 int Rflag; /* regression test */ 121 int Uflag; /* enable soft updates for file system */ 122 int Eflag = 0; /* exit in middle of newfs for testing */ 123 quad_t fssize; /* file system size */ 124 int sectorsize; /* bytes/sector */ 125 int realsectorsize; /* bytes/sector in hardware */ 126 int fsize = 0; /* fragment size */ 127 int bsize = 0; /* block size */ 128 int maxbsize = 0; /* maximum clustering */ 129 int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ 130 int minfree = MINFREE; /* free space threshold */ 131 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 132 int density; /* number of bytes per inode */ 133 int maxcontig = 0; /* max contiguous blocks to allocate */ 134 int maxbpg; /* maximum blocks per file in a cyl group */ 135 int avgfilesize = AVFILESIZ;/* expected average file size */ 136 int avgfilesperdir = AFPDIR;/* expected number of files per directory */ 137 u_char *volumelabel = NULL; /* volume label for filesystem */ 138 struct uufsd disk; /* libufs disk structure */ 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, i; 157 off_t mediasize; 158 159 while ((ch = getopt(argc, argv, 160 "EL:NO:RS:T:Ua:b:c:d:e:f:g:h:i:m:o:s:")) != -1) 161 switch (ch) { 162 case 'E': 163 Eflag++; 164 break; 165 case 'L': 166 volumelabel = optarg; 167 i = -1; 168 while (isalnum(volumelabel[++i])); 169 if (volumelabel[i] != '\0') { 170 errx(1, "bad volume label. Valid characters are alphanumerics."); 171 } 172 if (strlen(volumelabel) >= MAXVOLLEN) { 173 errx(1, "bad volume label. Length is longer than %d.", 174 MAXVOLLEN); 175 } 176 Lflag = 1; 177 break; 178 case 'N': 179 Nflag = 1; 180 break; 181 case 'O': 182 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 183 errx(1, "%s: bad file system format value", 184 optarg); 185 break; 186 case 'R': 187 Rflag = 1; 188 break; 189 case 'S': 190 if ((sectorsize = atoi(optarg)) <= 0) 191 errx(1, "%s: bad sector size", optarg); 192 break; 193 case 'T': 194 disktype = optarg; 195 break; 196 case 'U': 197 Uflag = 1; 198 break; 199 case 'a': 200 if ((maxcontig = atoi(optarg)) <= 0) 201 errx(1, "%s: bad maximum contiguous blocks", 202 optarg); 203 break; 204 case 'b': 205 if ((bsize = atoi(optarg)) < MINBSIZE) 206 errx(1, "%s: block size too small, min is %d", 207 optarg, MINBSIZE); 208 if (bsize > MAXBSIZE) 209 errx(1, "%s: block size too large, max is %d", 210 optarg, MAXBSIZE); 211 break; 212 case 'c': 213 if ((maxblkspercg = atoi(optarg)) <= 0) 214 errx(1, "%s: bad blocks per cylinder group", 215 optarg); 216 break; 217 case 'd': 218 if ((maxbsize = atoi(optarg)) < MINBSIZE) 219 errx(1, "%s: bad extent block size", optarg); 220 break; 221 case 'e': 222 if ((maxbpg = atoi(optarg)) <= 0) 223 errx(1, "%s: bad blocks per file in a cylinder group", 224 optarg); 225 break; 226 case 'f': 227 if ((fsize = atoi(optarg)) <= 0) 228 errx(1, "%s: bad fragment size", optarg); 229 break; 230 case 'g': 231 if ((avgfilesize = atoi(optarg)) <= 0) 232 errx(1, "%s: bad average file size", optarg); 233 break; 234 case 'h': 235 if ((avgfilesperdir = atoi(optarg)) <= 0) 236 errx(1, "%s: bad average files per dir", optarg); 237 break; 238 case 'i': 239 if ((density = atoi(optarg)) <= 0) 240 errx(1, "%s: bad bytes per inode", optarg); 241 break; 242 case 'm': 243 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 244 errx(1, "%s: bad free space %%", optarg); 245 break; 246 case 'o': 247 if (strcmp(optarg, "space") == 0) 248 opt = FS_OPTSPACE; 249 else if (strcmp(optarg, "time") == 0) 250 opt = FS_OPTTIME; 251 else 252 errx(1, 253 "%s: unknown optimization preference: use `space' or `time'", 254 optarg); 255 break; 256 case 's': 257 if ((fssize = atoi(optarg)) <= 0) 258 errx(1, "%s: bad file system size", optarg); 259 break; 260 case '?': 261 default: 262 usage(); 263 } 264 argc -= optind; 265 argv += optind; 266 267 if (argc != 1) 268 usage(); 269 270 special = argv[0]; 271 cp = strrchr(special, '/'); 272 if (cp == 0) { 273 /* 274 * No path prefix; try prefixing _PATH_DEV. 275 */ 276 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); 277 special = device; 278 } 279 280 if (ufs_disk_fillout_blank(&disk, special) == -1 || 281 (!Nflag && ufs_disk_write(&disk) == -1)) { 282 if (disk.d_error != NULL) 283 errx(1, "%s: %s", special, disk.d_error); 284 else 285 err(1, "%s", special); 286 } 287 if (fstat(disk.d_fd, &st) < 0) 288 err(1, "%s", special); 289 if ((st.st_mode & S_IFMT) != S_IFCHR) 290 errx(1, "%s: not a character-special device", special); 291 292 if (sectorsize == 0) 293 ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize); 294 if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) { 295 if (fssize == 0) 296 fssize = mediasize / sectorsize; 297 else if (fssize > mediasize / sectorsize) 298 errx(1, "%s: maximum file system size is %u", 299 special, (u_int)(mediasize / sectorsize)); 300 } 301 pp = NULL; 302 lp = getdisklabel(special); 303 if (lp != NULL) { 304 cp = strchr(special, '\0'); 305 cp--; 306 if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp)) 307 errx(1, "%s: can't figure out file system partition", 308 special); 309 if (isdigit(*cp)) 310 pp = &lp->d_partitions[RAW_PART]; 311 else 312 pp = &lp->d_partitions[*cp - 'a']; 313 oldpartition = *pp; 314 if (pp->p_size == 0) 315 errx(1, "%s: `%c' partition is unavailable", 316 special, *cp); 317 if (pp->p_fstype == FS_BOOT) 318 errx(1, "%s: `%c' partition overlaps boot program", 319 special, *cp); 320 if (fssize == 0) 321 fssize = pp->p_size; 322 if (fssize > pp->p_size) 323 errx(1, 324 "%s: maximum file system size %d", special, pp->p_size); 325 if (sectorsize == 0) 326 sectorsize = lp->d_secsize; 327 if (fsize == 0) 328 fsize = pp->p_fsize; 329 if (bsize == 0) 330 bsize = pp->p_frag * pp->p_fsize; 331 } 332 if (sectorsize <= 0) 333 errx(1, "%s: no default sector size", special); 334 if (fsize <= 0) 335 fsize = MAX(DFL_FRAGSIZE, sectorsize); 336 if (bsize <= 0) 337 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 338 if (maxbsize == 0) 339 maxbsize = bsize; 340 /* 341 * Maxcontig sets the default for the maximum number of blocks 342 * that may be allocated sequentially. With file system clustering 343 * it is possible to allocate contiguous blocks up to the maximum 344 * transfer size permitted by the controller or buffering. 345 */ 346 if (maxcontig == 0) 347 maxcontig = MAX(1, MAXPHYS / bsize); 348 if (density == 0) 349 density = NFPI * fsize; 350 if (minfree < MINFREE && opt != FS_OPTSPACE) { 351 fprintf(stderr, "Warning: changing optimization to space "); 352 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 353 opt = FS_OPTSPACE; 354 } 355 if (maxbpg == 0) 356 maxbpg = MAXBLKPG(bsize); 357 realsectorsize = sectorsize; 358 if (sectorsize != DEV_BSIZE) { /* XXX */ 359 int secperblk = sectorsize / DEV_BSIZE; 360 361 sectorsize = DEV_BSIZE; 362 fssize *= secperblk; 363 if (pp != NULL) 364 pp->p_size *= secperblk; 365 } 366 mkfs(pp, special); 367 if (!unlabeled) { 368 if (realsectorsize != DEV_BSIZE) 369 pp->p_size /= realsectorsize / DEV_BSIZE; 370 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 371 rewritelabel(special, lp); 372 } 373 ufs_disk_close(&disk); 374 exit(0); 375 } 376 377 struct disklabel * 378 getdisklabel(char *s) 379 { 380 static struct disklabel lab; 381 struct disklabel *lp; 382 383 if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab)) 384 return (&lab); 385 unlabeled++; 386 if (disktype) { 387 lp = getdiskbyname(disktype); 388 if (lp != NULL) 389 return (lp); 390 } 391 return (NULL); 392 } 393 394 void 395 rewritelabel(char *s, struct disklabel *lp) 396 { 397 if (unlabeled) 398 return; 399 lp->d_checksum = 0; 400 lp->d_checksum = dkcksum(lp); 401 if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0) 402 warn("ioctl (WDINFO): %s: can't rewrite disk label", s); 403 } 404 405 static void 406 usage() 407 { 408 fprintf(stderr, 409 "usage: %s [ -fsoptions ] special-device%s\n", 410 getprogname(), 411 " [device-type]"); 412 fprintf(stderr, "where fsoptions are:\n"); 413 fprintf(stderr, "\t-L volume label to add to superblock\n"); 414 fprintf(stderr, 415 "\t-N do not create file system, just print out parameters\n"); 416 fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n"); 417 fprintf(stderr, "\t-R regression test, supress random factors\n"); 418 fprintf(stderr, "\t-S sector size\n"); 419 fprintf(stderr, "\t-T disktype\n"); 420 fprintf(stderr, "\t-U enable soft updates\n"); 421 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 422 fprintf(stderr, "\t-b block size\n"); 423 fprintf(stderr, "\t-c blocks per cylinders group\n"); 424 fprintf(stderr, "\t-d maximum extent size\n"); 425 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 426 fprintf(stderr, "\t-f frag size\n"); 427 fprintf(stderr, "\t-g average file size\n"); 428 fprintf(stderr, "\t-h average files per directory\n"); 429 fprintf(stderr, "\t-i number of bytes per inode\n"); 430 fprintf(stderr, "\t-m minimum free space %%\n"); 431 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 432 fprintf(stderr, "\t-s file systemsize (sectors)\n"); 433 exit(1); 434 } 435