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