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 <libutil.h> 81 82 #include "newfs.h" 83 84 int Eflag; /* Erase previous disk contents */ 85 int Lflag; /* add a volume label */ 86 int Nflag; /* run without writing file system */ 87 int Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 88 int Rflag; /* regression test */ 89 int Uflag; /* enable soft updates for file system */ 90 int Xflag = 0; /* exit in middle of newfs for testing */ 91 int Jflag; /* enable gjournal for file system */ 92 int lflag; /* enable multilabel for file system */ 93 int nflag; /* do not create .snap directory */ 94 int tflag; /* enable TRIM */ 95 intmax_t fssize; /* file system size */ 96 int sectorsize; /* bytes/sector */ 97 int realsectorsize; /* bytes/sector in hardware */ 98 int fsize = 0; /* fragment size */ 99 int bsize = 0; /* block size */ 100 int maxbsize = 0; /* maximum clustering */ 101 int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ 102 int minfree = MINFREE; /* free space threshold */ 103 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 104 int density; /* number of bytes per inode */ 105 int maxcontig = 0; /* max contiguous blocks to allocate */ 106 int maxbpg; /* maximum blocks per file in a cyl group */ 107 int avgfilesize = AVFILESIZ;/* expected average file size */ 108 int avgfilesperdir = AFPDIR;/* expected number of files per directory */ 109 u_char *volumelabel = NULL; /* volume label for filesystem */ 110 struct uufsd disk; /* libufs disk structure */ 111 112 static char device[MAXPATHLEN]; 113 static u_char bootarea[BBSIZE]; 114 static int is_file; /* work on a file, not a device */ 115 static char *dkname; 116 static char *disktype; 117 static int unlabeled; 118 119 static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t); 120 static struct disklabel *getdisklabel(char *s); 121 static void rewritelabel(char *s, struct disklabel *lp); 122 static void usage(void); 123 static int expand_number_int(const char *buf, int *num); 124 125 ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */ 126 127 int 128 main(int argc, char *argv[]) 129 { 130 struct partition *pp; 131 struct disklabel *lp; 132 struct partition oldpartition; 133 struct stat st; 134 char *cp, *special; 135 intmax_t reserved; 136 int ch, i, rval; 137 off_t mediasize; 138 char part_name; /* partition name, default to full disk */ 139 140 part_name = 'c'; 141 reserved = 0; 142 while ((ch = getopt(argc, argv, 143 "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:lm:no:p:r:s:t")) != -1) 144 switch (ch) { 145 case 'E': 146 Eflag = 1; 147 break; 148 case 'J': 149 Jflag = 1; 150 break; 151 case 'L': 152 volumelabel = optarg; 153 i = -1; 154 while (isalnum(volumelabel[++i])); 155 if (volumelabel[i] != '\0') { 156 errx(1, "bad volume label. Valid characters are alphanumerics."); 157 } 158 if (strlen(volumelabel) >= MAXVOLLEN) { 159 errx(1, "bad volume label. Length is longer than %d.", 160 MAXVOLLEN); 161 } 162 Lflag = 1; 163 break; 164 case 'N': 165 Nflag = 1; 166 break; 167 case 'O': 168 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 169 errx(1, "%s: bad file system format value", 170 optarg); 171 break; 172 case 'R': 173 Rflag = 1; 174 break; 175 case 'S': 176 rval = expand_number_int(optarg, §orsize); 177 if (rval < 0 || sectorsize <= 0) 178 errx(1, "%s: bad sector size", optarg); 179 break; 180 case 'T': 181 disktype = optarg; 182 break; 183 case 'U': 184 Uflag = 1; 185 break; 186 case 'X': 187 Xflag++; 188 break; 189 case 'a': 190 rval = expand_number_int(optarg, &maxcontig); 191 if (rval < 0 || maxcontig <= 0) 192 errx(1, "%s: bad maximum contiguous blocks", 193 optarg); 194 break; 195 case 'b': 196 rval = expand_number_int(optarg, &bsize); 197 if (rval < 0) 198 errx(1, "%s: bad block size", 199 optarg); 200 if (bsize < MINBSIZE) 201 errx(1, "%s: block size too small, min is %d", 202 optarg, MINBSIZE); 203 if (bsize > MAXBSIZE) 204 errx(1, "%s: block size too large, max is %d", 205 optarg, MAXBSIZE); 206 break; 207 case 'c': 208 rval = expand_number_int(optarg, &maxblkspercg); 209 if (rval < 0 || maxblkspercg <= 0) 210 errx(1, "%s: bad blocks per cylinder group", 211 optarg); 212 break; 213 case 'd': 214 rval = expand_number_int(optarg, &maxbsize); 215 if (rval < 0 || maxbsize < MINBSIZE) 216 errx(1, "%s: bad extent block size", optarg); 217 break; 218 case 'e': 219 rval = expand_number_int(optarg, &maxbpg); 220 if (rval < 0 || maxbpg <= 0) 221 errx(1, "%s: bad blocks per file in a cylinder group", 222 optarg); 223 break; 224 case 'f': 225 rval = expand_number_int(optarg, &fsize); 226 if (rval < 0 || fsize <= 0) 227 errx(1, "%s: bad fragment size", optarg); 228 break; 229 case 'g': 230 rval = expand_number_int(optarg, &avgfilesize); 231 if (rval < 0 || avgfilesize <= 0) 232 errx(1, "%s: bad average file size", optarg); 233 break; 234 case 'h': 235 rval = expand_number_int(optarg, &avgfilesperdir); 236 if (rval < 0 || avgfilesperdir <= 0) 237 errx(1, "%s: bad average files per dir", optarg); 238 break; 239 case 'i': 240 rval = expand_number_int(optarg, &density); 241 if (rval < 0 || density <= 0) 242 errx(1, "%s: bad bytes per inode", optarg); 243 break; 244 case 'l': 245 lflag = 1; 246 break; 247 case 'm': 248 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 249 errx(1, "%s: bad free space %%", optarg); 250 break; 251 case 'n': 252 nflag = 1; 253 break; 254 case 'o': 255 if (strcmp(optarg, "space") == 0) 256 opt = FS_OPTSPACE; 257 else if (strcmp(optarg, "time") == 0) 258 opt = FS_OPTTIME; 259 else 260 errx(1, 261 "%s: unknown optimization preference: use `space' or `time'", 262 optarg); 263 break; 264 case 'r': 265 errno = 0; 266 reserved = strtoimax(optarg, &cp, 0); 267 if (errno != 0 || cp == optarg || 268 *cp != '\0' || reserved < 0) 269 errx(1, "%s: bad reserved size", optarg); 270 break; 271 case 'p': 272 is_file = 1; 273 part_name = optarg[0]; 274 break; 275 276 case 's': 277 errno = 0; 278 fssize = strtoimax(optarg, &cp, 0); 279 if (errno != 0 || cp == optarg || 280 *cp != '\0' || fssize < 0) 281 errx(1, "%s: bad file system size", optarg); 282 break; 283 case 't': 284 tflag = 1; 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 if (!special[0]) 298 err(1, "empty file/special name"); 299 cp = strrchr(special, '/'); 300 if (cp == 0) { 301 /* 302 * No path prefix; try prefixing _PATH_DEV. 303 */ 304 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); 305 special = device; 306 } 307 308 if (is_file) { 309 /* bypass ufs_disk_fillout_blank */ 310 bzero( &disk, sizeof(disk)); 311 disk.d_bsize = 1; 312 disk.d_name = special; 313 disk.d_fd = open(special, O_RDONLY); 314 if (disk.d_fd < 0 || 315 (!Nflag && ufs_disk_write(&disk) == -1)) 316 errx(1, "%s: ", special); 317 } else if (ufs_disk_fillout_blank(&disk, special) == -1 || 318 (!Nflag && ufs_disk_write(&disk) == -1)) { 319 if (disk.d_error != NULL) 320 errx(1, "%s: %s", special, disk.d_error); 321 else 322 err(1, "%s", special); 323 } 324 if (fstat(disk.d_fd, &st) < 0) 325 err(1, "%s", special); 326 if ((st.st_mode & S_IFMT) != S_IFCHR) { 327 warn("%s: not a character-special device", special); 328 is_file = 1; /* assume it is a file */ 329 dkname = special; 330 if (sectorsize == 0) 331 sectorsize = 512; 332 mediasize = st.st_size; 333 /* set fssize from the partition */ 334 } else { 335 if (sectorsize == 0) 336 if (ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize) == -1) 337 sectorsize = 0; /* back out on error for safety */ 338 if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1) 339 getfssize(&fssize, special, mediasize / sectorsize, reserved); 340 } 341 pp = NULL; 342 lp = getdisklabel(special); 343 if (lp != NULL) { 344 if (!is_file) /* already set for files */ 345 part_name = special[strlen(special) - 1]; 346 if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) && 347 !isdigit(part_name)) 348 errx(1, "%s: can't figure out file system partition", 349 special); 350 cp = &part_name; 351 if (isdigit(*cp)) 352 pp = &lp->d_partitions[RAW_PART]; 353 else 354 pp = &lp->d_partitions[*cp - 'a']; 355 oldpartition = *pp; 356 if (pp->p_size == 0) 357 errx(1, "%s: `%c' partition is unavailable", 358 special, *cp); 359 if (pp->p_fstype == FS_BOOT) 360 errx(1, "%s: `%c' partition overlaps boot program", 361 special, *cp); 362 getfssize(&fssize, special, pp->p_size, reserved); 363 if (sectorsize == 0) 364 sectorsize = lp->d_secsize; 365 if (fsize == 0) 366 fsize = pp->p_fsize; 367 if (bsize == 0) 368 bsize = pp->p_frag * pp->p_fsize; 369 if (is_file) 370 part_ofs = pp->p_offset; 371 } 372 if (sectorsize <= 0) 373 errx(1, "%s: no default sector size", special); 374 if (fsize <= 0) 375 fsize = MAX(DFL_FRAGSIZE, sectorsize); 376 if (bsize <= 0) 377 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 378 if (minfree < MINFREE && opt != FS_OPTSPACE) { 379 fprintf(stderr, "Warning: changing optimization to space "); 380 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 381 opt = FS_OPTSPACE; 382 } 383 realsectorsize = sectorsize; 384 if (sectorsize != DEV_BSIZE) { /* XXX */ 385 int secperblk = sectorsize / DEV_BSIZE; 386 387 sectorsize = DEV_BSIZE; 388 fssize *= secperblk; 389 if (pp != NULL) 390 pp->p_size *= secperblk; 391 } 392 mkfs(pp, special); 393 if (!unlabeled) { 394 if (realsectorsize != DEV_BSIZE) 395 pp->p_size /= realsectorsize / DEV_BSIZE; 396 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) 397 rewritelabel(special, lp); 398 } 399 ufs_disk_close(&disk); 400 exit(0); 401 } 402 403 void 404 getfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved) 405 { 406 intmax_t available; 407 408 available = disksize - reserved; 409 if (available <= 0) 410 errx(1, "%s: reserved not less than device size %jd", 411 s, disksize); 412 if (*fsz == 0) 413 *fsz = available; 414 else if (*fsz > available) 415 errx(1, "%s: maximum file system size is %jd", 416 s, available); 417 } 418 419 struct disklabel * 420 getdisklabel(char *s) 421 { 422 static struct disklabel lab; 423 struct disklabel *lp; 424 425 if (is_file) { 426 if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE) 427 err(4, "cannot read bootarea"); 428 if (bsd_disklabel_le_dec( 429 bootarea + (0 /* labeloffset */ + 430 1 /* labelsoffset */ * sectorsize), 431 &lab, MAXPARTITIONS)) 432 errx(1, "no valid label found"); 433 434 lp = &lab; 435 return &lab; 436 } 437 438 if (ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab) != -1) 439 return (&lab); 440 unlabeled++; 441 if (disktype) { 442 lp = getdiskbyname(disktype); 443 if (lp != NULL) 444 return (lp); 445 } 446 return (NULL); 447 } 448 449 void 450 rewritelabel(char *s, struct disklabel *lp) 451 { 452 if (unlabeled) 453 return; 454 lp->d_checksum = 0; 455 lp->d_checksum = dkcksum(lp); 456 if (is_file) { 457 bsd_disklabel_le_enc(bootarea + 0 /* labeloffset */ + 458 1 /* labelsoffset */ * sectorsize, lp); 459 lseek(disk.d_fd, 0, SEEK_SET); 460 if (write(disk.d_fd, bootarea, BBSIZE) != BBSIZE) 461 errx(1, "cannot write label"); 462 return; 463 } 464 if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) == -1) 465 warn("ioctl (WDINFO): %s: can't rewrite disk label", s); 466 } 467 468 static void 469 usage() 470 { 471 fprintf(stderr, 472 "usage: %s [ -fsoptions ] special-device%s\n", 473 getprogname(), 474 " [device-type]"); 475 fprintf(stderr, "where fsoptions are:\n"); 476 fprintf(stderr, "\t-E Erase previous disk content\n"); 477 fprintf(stderr, "\t-J Enable journaling via gjournal\n"); 478 fprintf(stderr, "\t-L volume label to add to superblock\n"); 479 fprintf(stderr, 480 "\t-N do not create file system, just print out parameters\n"); 481 fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n"); 482 fprintf(stderr, "\t-R regression test, suppress random factors\n"); 483 fprintf(stderr, "\t-S sector size\n"); 484 fprintf(stderr, "\t-T disktype\n"); 485 fprintf(stderr, "\t-U enable soft updates\n"); 486 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 487 fprintf(stderr, "\t-b block size\n"); 488 fprintf(stderr, "\t-c blocks per cylinders group\n"); 489 fprintf(stderr, "\t-d maximum extent size\n"); 490 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 491 fprintf(stderr, "\t-f frag size\n"); 492 fprintf(stderr, "\t-g average file size\n"); 493 fprintf(stderr, "\t-h average files per directory\n"); 494 fprintf(stderr, "\t-i number of bytes per inode\n"); 495 fprintf(stderr, "\t-l enable multilabel MAC\n"); 496 fprintf(stderr, "\t-n do not create .snap directory\n"); 497 fprintf(stderr, "\t-m minimum free space %%\n"); 498 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 499 fprintf(stderr, "\t-p partition name (a..h)\n"); 500 fprintf(stderr, "\t-r reserved sectors at the end of device\n"); 501 fprintf(stderr, "\t-s file system size (sectors)\n"); 502 fprintf(stderr, "\t-t enable TRIM\n"); 503 exit(1); 504 } 505 506 static int 507 expand_number_int(const char *buf, int *num) 508 { 509 int64_t num64; 510 int rval; 511 512 rval = expand_number(buf, &num64); 513 if (rval < 0) 514 return (rval); 515 if (num64 > INT_MAX || num64 < INT_MIN) { 516 errno = ERANGE; 517 return (-1); 518 } 519 *num = (int)num64; 520 return (0); 521 } 522