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 <paths.h> 72 #include <stdarg.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <syslog.h> 77 #include <unistd.h> 78 79 #include "newfs.h" 80 81 /* 82 * The following two constants set the default block and fragment sizes. 83 * Both constants must be a power of 2 and meet the following constraints: 84 * MINBSIZE <= DESBLKSIZE <= MAXBSIZE 85 * sectorsize <= DESFRAGSIZE <= DESBLKSIZE 86 * DESBLKSIZE / DESFRAGSIZE <= 8 87 */ 88 #define DFL_FRAGSIZE 2048 89 #define DFL_BLKSIZE 16384 90 91 /* 92 * Cylinder groups may have up to MAXBLKSPERCG blocks. The actual 93 * number used depends upon how much information can be stored 94 * in a cylinder group map which must fit in a single file system 95 * block. The default is to use as many as possible blocks per group. 96 */ 97 #define MAXBLKSPERCG 0x7fffffff /* desired fs_fpg ("infinity") */ 98 99 /* 100 * MAXBLKPG determines the maximum number of data blocks which are 101 * placed in a single cylinder group. The default is one indirect 102 * block worth of data blocks. 103 */ 104 #define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t)) 105 106 /* 107 * Each file system has a number of inodes statically allocated. 108 * We allocate one inode slot per NFPI fragments, expecting this 109 * to be far more than we will ever need. 110 */ 111 #define NFPI 4 112 113 int Lflag; /* add a volume label */ 114 int Nflag; /* run without writing file system */ 115 int Oflag = 2; /* file system format (1 => UFS1, 2 => UFS2) */ 116 int Rflag; /* regression test */ 117 int Uflag; /* enable soft updates for file system */ 118 int Eflag = 0; /* exit in middle of newfs for testing */ 119 int lflag; /* enable multilabel for file system */ 120 quad_t fssize; /* file system size */ 121 int sectorsize; /* bytes/sector */ 122 int realsectorsize; /* bytes/sector in hardware */ 123 int fsize = 0; /* fragment size */ 124 int bsize = 0; /* block size */ 125 int maxbsize = 0; /* maximum clustering */ 126 int maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */ 127 int minfree = MINFREE; /* free space threshold */ 128 int opt = DEFAULTOPT; /* optimization preference (space or time) */ 129 int density; /* number of bytes per inode */ 130 int maxcontig = 0; /* max contiguous blocks to allocate */ 131 int maxbpg; /* maximum blocks per file in a cyl group */ 132 int avgfilesize = AVFILESIZ;/* expected average file size */ 133 int avgfilesperdir = AFPDIR;/* expected number of files per directory */ 134 u_char *volumelabel = NULL; /* volume label for filesystem */ 135 struct uufsd disk; /* libufs disk structure */ 136 137 static char device[MAXPATHLEN]; 138 static char *disktype; 139 static int unlabeled; 140 141 static struct disklabel *getdisklabel(char *s); 142 static void rewritelabel(char *s, struct disklabel *lp); 143 static void usage(void); 144 145 int 146 main(int argc, char *argv[]) 147 { 148 struct partition *pp; 149 struct disklabel *lp; 150 struct partition oldpartition; 151 struct stat st; 152 char *cp, *special; 153 int ch, i; 154 off_t mediasize; 155 156 while ((ch = getopt(argc, argv, 157 "EL:NO:RS:T:Ua:b:c:d:e:f:g:h:i:lm:o:s:")) != -1) 158 switch (ch) { 159 case 'E': 160 Eflag++; 161 break; 162 case 'L': 163 volumelabel = optarg; 164 i = -1; 165 while (isalnum(volumelabel[++i])); 166 if (volumelabel[i] != '\0') { 167 errx(1, "bad volume label. Valid characters are alphanumerics."); 168 } 169 if (strlen(volumelabel) >= MAXVOLLEN) { 170 errx(1, "bad volume label. Length is longer than %d.", 171 MAXVOLLEN); 172 } 173 Lflag = 1; 174 break; 175 case 'N': 176 Nflag = 1; 177 break; 178 case 'O': 179 if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) 180 errx(1, "%s: bad file system format value", 181 optarg); 182 break; 183 case 'R': 184 Rflag = 1; 185 break; 186 case 'S': 187 if ((sectorsize = atoi(optarg)) <= 0) 188 errx(1, "%s: bad sector size", optarg); 189 break; 190 case 'T': 191 disktype = optarg; 192 break; 193 case 'U': 194 Uflag = 1; 195 break; 196 case 'a': 197 if ((maxcontig = atoi(optarg)) <= 0) 198 errx(1, "%s: bad maximum contiguous blocks", 199 optarg); 200 break; 201 case 'b': 202 if ((bsize = atoi(optarg)) < MINBSIZE) 203 errx(1, "%s: block size too small, min is %d", 204 optarg, MINBSIZE); 205 if (bsize > MAXBSIZE) 206 errx(1, "%s: block size too large, max is %d", 207 optarg, MAXBSIZE); 208 break; 209 case 'c': 210 if ((maxblkspercg = atoi(optarg)) <= 0) 211 errx(1, "%s: bad blocks per cylinder group", 212 optarg); 213 break; 214 case 'd': 215 if ((maxbsize = atoi(optarg)) < MINBSIZE) 216 errx(1, "%s: bad extent block size", optarg); 217 break; 218 case 'e': 219 if ((maxbpg = atoi(optarg)) <= 0) 220 errx(1, "%s: bad blocks per file in a cylinder group", 221 optarg); 222 break; 223 case 'f': 224 if ((fsize = atoi(optarg)) <= 0) 225 errx(1, "%s: bad fragment size", optarg); 226 break; 227 case 'g': 228 if ((avgfilesize = atoi(optarg)) <= 0) 229 errx(1, "%s: bad average file size", optarg); 230 break; 231 case 'h': 232 if ((avgfilesperdir = atoi(optarg)) <= 0) 233 errx(1, "%s: bad average files per dir", optarg); 234 break; 235 case 'i': 236 if ((density = atoi(optarg)) <= 0) 237 errx(1, "%s: bad bytes per inode", optarg); 238 break; 239 case 'l': 240 lflag = 1; 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