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