1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2002 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * This software was developed for the FreeBSD Project by Marshall 8 * Kirk McKusick and Network Associates Laboratories, the Security 9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 11 * research program. 12 * 13 * Copyright (c) 1983, 1989, 1993, 1994 14 * The Regents of the University of California. All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 /* 42 * newfs: friendly front end to mkfs 43 */ 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/disk.h> 47 #include <sys/disklabel.h> 48 #include <sys/file.h> 49 #include <sys/mount.h> 50 51 #include <ufs/ufs/dir.h> 52 #include <ufs/ufs/dinode.h> 53 #include <ufs/ffs/fs.h> 54 #include <ufs/ufs/extattr.h> 55 #include <ufs/ufs/quota.h> 56 #include <ufs/ufs/ufsmount.h> 57 58 #include <ctype.h> 59 #include <err.h> 60 #include <errno.h> 61 #include <inttypes.h> 62 #include <paths.h> 63 #include <stdarg.h> 64 #include <stdio.h> 65 #include <stdlib.h> 66 #include <string.h> 67 #include <syslog.h> 68 #include <unistd.h> 69 70 #include <libutil.h> 71 72 #include "newfs.h" 73 74 int Eflag; /* Erase previous disk contents */ 75 int Lflag; /* add a volume label */ 76 int Nflag; /* run without writing file system */ 77 int Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 78 int Rflag; /* regression test */ 79 int Uflag; /* enable soft updates for file system */ 80 int jflag; /* enable soft updates journaling for filesys */ 81 int Xflag = 0; /* exit in middle of newfs for testing */ 82 int Jflag; /* enable gjournal for file system */ 83 int lflag; /* enable multilabel for file system */ 84 int nflag; /* do not create .snap directory */ 85 int tflag; /* enable TRIM */ 86 intmax_t fssize; /* file system size */ 87 off_t mediasize; /* device size */ 88 int sectorsize; /* bytes/sector */ 89 int realsectorsize; /* bytes/sector in hardware */ 90 int fsize = 0; /* fragment size */ 91 int bsize = 0; /* block size */ 92 int maxbsize = 0; /* maximum clustering */ 93 int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ 94 int minfree = MINFREE; /* free space threshold */ 95 int metaspace; /* space held for metadata blocks */ 96 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 97 int density; /* number of bytes per inode */ 98 int maxcontig = 0; /* max contiguous blocks to allocate */ 99 int maxbpg; /* maximum blocks per file in a cyl group */ 100 int avgfilesize = AVFILESIZ;/* expected average file size */ 101 int avgfilesperdir = AFPDIR;/* expected number of files per directory */ 102 u_char *volumelabel = NULL; /* volume label for filesystem */ 103 struct uufsd disk; /* libufs disk structure */ 104 105 static char device[MAXPATHLEN]; 106 static u_char bootarea[BBSIZE]; 107 static int is_file; /* work on a file, not a device */ 108 static char *disktype; 109 110 static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t); 111 static struct disklabel *getdisklabel(void); 112 static void usage(void) __dead2; 113 static int expand_number_int(const char *buf, int *num); 114 115 ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */ 116 117 int 118 main(int argc, char *argv[]) 119 { 120 struct partition *pp; 121 struct disklabel *lp; 122 struct stat st; 123 char *cp, *special; 124 intmax_t reserved; 125 int ch, rval; 126 size_t i; 127 char part_name; /* partition name, default to full disk */ 128 129 part_name = 'c'; 130 reserved = 0; 131 while ((ch = getopt(argc, argv, 132 "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:jk:lm:no:p:r:s:t")) != -1) 133 switch (ch) { 134 case 'E': 135 Eflag = 1; 136 break; 137 case 'J': 138 Jflag = 1; 139 break; 140 case 'L': 141 volumelabel = optarg; 142 for (i = 0; isalnum(volumelabel[i]) || 143 volumelabel[i] == '_' || volumelabel[i] == '-'; 144 i++) 145 continue; 146 if (volumelabel[i] != '\0') { 147 errx(1, "bad volume label. Valid characters " 148 "are alphanumerics, dashes, and underscores."); 149 } 150 if (strlen(volumelabel) >= MAXVOLLEN) { 151 errx(1, "bad volume label. Length is longer than %d.", 152 MAXVOLLEN); 153 } 154 Lflag = 1; 155 break; 156 case 'N': 157 Nflag = 1; 158 break; 159 case 'O': 160 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 161 errx(1, "%s: bad file system format value", 162 optarg); 163 break; 164 case 'R': 165 Rflag = 1; 166 break; 167 case 'S': 168 rval = expand_number_int(optarg, §orsize); 169 if (rval < 0 || sectorsize <= 0) 170 errx(1, "%s: bad sector size", optarg); 171 break; 172 case 'T': 173 disktype = optarg; 174 break; 175 case 'j': 176 jflag = 1; 177 /* fall through to enable soft updates */ 178 /* FALLTHROUGH */ 179 case 'U': 180 Uflag = 1; 181 break; 182 case 'X': 183 Xflag++; 184 break; 185 case 'a': 186 rval = expand_number_int(optarg, &maxcontig); 187 if (rval < 0 || maxcontig <= 0) 188 errx(1, "%s: bad maximum contiguous blocks", 189 optarg); 190 break; 191 case 'b': 192 rval = expand_number_int(optarg, &bsize); 193 if (rval < 0) 194 errx(1, "%s: bad block size", 195 optarg); 196 if (bsize < MINBSIZE) 197 errx(1, "%s: block size too small, min is %d", 198 optarg, MINBSIZE); 199 if (bsize > MAXBSIZE) 200 errx(1, "%s: block size too large, max is %d", 201 optarg, MAXBSIZE); 202 break; 203 case 'c': 204 rval = expand_number_int(optarg, &maxblkspercg); 205 if (rval < 0 || maxblkspercg <= 0) 206 errx(1, "%s: bad blocks per cylinder group", 207 optarg); 208 break; 209 case 'd': 210 rval = expand_number_int(optarg, &maxbsize); 211 if (rval < 0 || maxbsize < MINBSIZE) 212 errx(1, "%s: bad extent block size", optarg); 213 break; 214 case 'e': 215 rval = expand_number_int(optarg, &maxbpg); 216 if (rval < 0 || maxbpg <= 0) 217 errx(1, "%s: bad blocks per file in a cylinder group", 218 optarg); 219 break; 220 case 'f': 221 rval = expand_number_int(optarg, &fsize); 222 if (rval < 0 || fsize <= 0) 223 errx(1, "%s: bad fragment size", optarg); 224 break; 225 case 'g': 226 rval = expand_number_int(optarg, &avgfilesize); 227 if (rval < 0 || avgfilesize <= 0) 228 errx(1, "%s: bad average file size", optarg); 229 break; 230 case 'h': 231 rval = expand_number_int(optarg, &avgfilesperdir); 232 if (rval < 0 || avgfilesperdir <= 0) 233 errx(1, "%s: bad average files per dir", optarg); 234 break; 235 case 'i': 236 rval = expand_number_int(optarg, &density); 237 if (rval < 0 || density <= 0) 238 errx(1, "%s: bad bytes per inode", optarg); 239 break; 240 case 'l': 241 lflag = 1; 242 break; 243 case 'k': 244 if ((metaspace = atoi(optarg)) < 0) 245 errx(1, "%s: bad metadata space %%", optarg); 246 if (metaspace == 0) 247 /* force to stay zero in mkfs */ 248 metaspace = -1; 249 break; 250 case 'm': 251 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 252 errx(1, "%s: bad free space %%", optarg); 253 break; 254 case 'n': 255 nflag = 1; 256 break; 257 case 'o': 258 if (strcmp(optarg, "space") == 0) 259 opt = FS_OPTSPACE; 260 else if (strcmp(optarg, "time") == 0) 261 opt = FS_OPTTIME; 262 else 263 errx(1, 264 "%s: unknown optimization preference: use `space' or `time'", 265 optarg); 266 break; 267 case 'r': 268 errno = 0; 269 reserved = strtoimax(optarg, &cp, 0); 270 if (errno != 0 || cp == optarg || 271 *cp != '\0' || reserved < 0) 272 errx(1, "%s: bad reserved size", optarg); 273 break; 274 case 'p': 275 is_file = 1; 276 part_name = optarg[0]; 277 break; 278 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 't': 287 tflag = 1; 288 break; 289 case '?': 290 default: 291 usage(); 292 } 293 argc -= optind; 294 argv += optind; 295 296 if (argc != 1) 297 usage(); 298 299 special = argv[0]; 300 if (!special[0]) 301 err(1, "empty file/special name"); 302 cp = strrchr(special, '/'); 303 if (cp == NULL) { 304 /* 305 * No path prefix; try prefixing _PATH_DEV. 306 */ 307 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); 308 special = device; 309 } 310 311 if (is_file) { 312 /* bypass ufs_disk_fillout_blank */ 313 bzero( &disk, sizeof(disk)); 314 disk.d_bsize = 1; 315 disk.d_name = special; 316 disk.d_fd = open(special, O_RDONLY); 317 if (disk.d_fd < 0 || 318 (!Nflag && ufs_disk_write(&disk) == -1)) 319 errx(1, "%s: ", special); 320 } else if (ufs_disk_fillout_blank(&disk, special) == -1 || 321 (!Nflag && ufs_disk_write(&disk) == -1)) { 322 if (disk.d_error != NULL) 323 errx(1, "%s: %s", special, disk.d_error); 324 else 325 err(1, "%s", special); 326 } 327 if (fstat(disk.d_fd, &st) < 0) 328 err(1, "%s", special); 329 if ((st.st_mode & S_IFMT) != S_IFCHR) { 330 warn("%s: not a character-special device", special); 331 is_file = 1; /* assume it is a file */ 332 if (sectorsize == 0) 333 sectorsize = 512; 334 mediasize = st.st_size; 335 /* set fssize from the partition */ 336 } else { 337 if (sectorsize == 0) 338 if (ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize) == -1) 339 sectorsize = 0; /* back out on error for safety */ 340 if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1) 341 getfssize(&fssize, special, mediasize / sectorsize, reserved); 342 } 343 pp = NULL; 344 lp = getdisklabel(); 345 if (lp != NULL) { 346 if (!is_file) /* already set for files */ 347 part_name = special[strlen(special) - 1]; 348 if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) && 349 !isdigit(part_name)) 350 errx(1, "%s: can't figure out file system partition", 351 special); 352 cp = &part_name; 353 if (isdigit(*cp)) 354 pp = &lp->d_partitions[RAW_PART]; 355 else 356 pp = &lp->d_partitions[*cp - 'a']; 357 if (pp->p_size == 0) 358 errx(1, "%s: `%c' partition is unavailable", 359 special, *cp); 360 if (pp->p_fstype == FS_BOOT) 361 errx(1, "%s: `%c' partition overlaps boot program", 362 special, *cp); 363 getfssize(&fssize, special, pp->p_size, reserved); 364 if (sectorsize == 0) 365 sectorsize = lp->d_secsize; 366 if (fsize == 0) 367 fsize = pp->p_fsize; 368 if (bsize == 0) 369 bsize = pp->p_frag * pp->p_fsize; 370 if (is_file) 371 part_ofs = pp->p_offset; 372 } 373 if (sectorsize <= 0) 374 errx(1, "%s: no default sector size", special); 375 if (fsize <= 0) 376 fsize = MAX(DFL_FRAGSIZE, sectorsize); 377 if (bsize <= 0) 378 bsize = MIN(DFL_BLKSIZE, 8 * fsize); 379 if (minfree < MINFREE && opt != FS_OPTSPACE) { 380 fprintf(stderr, "Warning: changing optimization to space "); 381 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); 382 opt = FS_OPTSPACE; 383 } 384 /* Use soft updates by default for UFS2 and above */ 385 if (Oflag > 1) 386 Uflag = 1; 387 realsectorsize = sectorsize; 388 if (sectorsize != DEV_BSIZE) { /* XXX */ 389 int secperblk = sectorsize / DEV_BSIZE; 390 391 sectorsize = DEV_BSIZE; 392 fssize *= secperblk; 393 if (pp != NULL) 394 pp->p_size *= secperblk; 395 } 396 mkfs(pp, special); 397 ufs_disk_close(&disk); 398 if (!jflag) 399 exit(0); 400 if (execlp("tunefs", "newfs", "-j", "enable", special, NULL) < 0) 401 err(1, "Cannot enable soft updates journaling, tunefs"); 402 /* NOT REACHED */ 403 } 404 405 void 406 getfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved) 407 { 408 intmax_t available; 409 410 available = disksize - reserved; 411 if (available <= 0) 412 errx(1, "%s: reserved not less than device size %jd", 413 s, disksize); 414 if (*fsz == 0) 415 *fsz = available; 416 else if (*fsz > available) 417 errx(1, "%s: maximum file system size is %jd", 418 s, available); 419 } 420 421 struct disklabel * 422 getdisklabel(void) 423 { 424 static struct disklabel lab; 425 struct disklabel *lp; 426 427 if (is_file) { 428 if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE) 429 err(4, "cannot read bootarea"); 430 if (bsd_disklabel_le_dec( 431 bootarea + (0 /* labeloffset */ + 432 1 /* labelsoffset */ * sectorsize), 433 &lab, MAXPARTITIONS)) 434 errx(1, "no valid label found"); 435 436 lp = &lab; 437 return &lab; 438 } 439 440 if (disktype) { 441 lp = getdiskbyname(disktype); 442 if (lp != NULL) 443 return (lp); 444 } 445 return (NULL); 446 } 447 448 static void 449 usage(void) 450 { 451 fprintf(stderr, 452 "usage: %s [ -fsoptions ] special-device%s\n", 453 getprogname(), 454 " [device-type]"); 455 fprintf(stderr, "where fsoptions are:\n"); 456 fprintf(stderr, "\t-E Erase previous disk content\n"); 457 fprintf(stderr, "\t-J Enable journaling via gjournal\n"); 458 fprintf(stderr, "\t-L volume label to add to superblock\n"); 459 fprintf(stderr, 460 "\t-N do not create file system, just print out parameters\n"); 461 fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n"); 462 fprintf(stderr, "\t-R regression test, suppress random factors\n"); 463 fprintf(stderr, "\t-S sector size\n"); 464 fprintf(stderr, "\t-T disktype\n"); 465 fprintf(stderr, "\t-U enable soft updates\n"); 466 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 467 fprintf(stderr, "\t-b block size\n"); 468 fprintf(stderr, "\t-c blocks per cylinders group\n"); 469 fprintf(stderr, "\t-d maximum extent size\n"); 470 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 471 fprintf(stderr, "\t-f frag size\n"); 472 fprintf(stderr, "\t-g average file size\n"); 473 fprintf(stderr, "\t-h average files per directory\n"); 474 fprintf(stderr, "\t-i number of bytes per inode\n"); 475 fprintf(stderr, "\t-j enable soft updates journaling\n"); 476 fprintf(stderr, "\t-k space to hold for metadata blocks\n"); 477 fprintf(stderr, "\t-l enable multilabel MAC\n"); 478 fprintf(stderr, "\t-n do not create .snap directory\n"); 479 fprintf(stderr, "\t-m minimum free space %%\n"); 480 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 481 fprintf(stderr, "\t-p partition name (a..h)\n"); 482 fprintf(stderr, "\t-r reserved sectors at the end of device\n"); 483 fprintf(stderr, "\t-s file system size (sectors)\n"); 484 fprintf(stderr, "\t-t enable TRIM\n"); 485 exit(1); 486 } 487 488 static int 489 expand_number_int(const char *buf, int *num) 490 { 491 int64_t num64; 492 int rval; 493 494 rval = expand_number(buf, &num64); 495 if (rval < 0) 496 return (rval); 497 if (num64 > INT_MAX || num64 < INT_MIN) { 498 errno = ERANGE; 499 return (-1); 500 } 501 *num = (int)num64; 502 return (0); 503 } 504