newfs.c revision 1.15
1/* $NetBSD: newfs.c,v 1.15 2005/02/26 05:45:54 perseant Exp $ */ 2 3/*- 4 * Copyright (c) 1989, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT("@(#) Copyright (c) 1989, 1992, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)newfs.c 8.5 (Berkeley) 5/24/95"; 41#else 42__RCSID("$NetBSD: newfs.c,v 1.15 2005/02/26 05:45:54 perseant Exp $"); 43#endif 44#endif /* not lint */ 45 46/* 47 * newfs: friendly front end to mkfs 48 */ 49#include <sys/param.h> 50#include <sys/ucred.h> 51#include <sys/stat.h> 52#include <sys/ioctl.h> 53#include <sys/disklabel.h> 54#include <sys/file.h> 55#include <sys/mount.h> 56#include <sys/sysctl.h> 57#include <sys/time.h> 58 59#include <ufs/ufs/dir.h> 60#include <ufs/ufs/dinode.h> 61#include <ufs/lfs/lfs.h> 62 63#include <disktab.h> 64#include <err.h> 65#include <errno.h> 66#include <unistd.h> 67#include <stdio.h> 68#include <stdlib.h> 69#include <ctype.h> 70#include <string.h> 71#include <paths.h> 72#include <util.h> 73#include "config.h" 74#include "extern.h" 75 76#define COMPAT /* allow non-labeled disks */ 77 78int Nflag = 0; /* run without writing file system */ 79int fssize; /* file system size */ 80int sectorsize; /* bytes/sector */ 81int fsize = 0; /* fragment size */ 82int bsize = 0; /* block size */ 83int ibsize = 0; /* inode block size */ 84int interleave = 0; /* segment interleave */ 85int minfree = MINFREE; /* free space threshold */ 86int minfreeseg = 0; /* free segments reserved for the cleaner */ 87u_int32_t roll_id = 0; /* roll-forward id */ 88u_long memleft; /* virtual memory available */ 89caddr_t membase; /* start address of memory based filesystem */ 90#ifdef COMPAT 91char *disktype; 92int unlabeled; 93#endif 94int preen = 0; /* Coexistence with fsck_lfs */ 95 96char device[MAXPATHLEN]; 97char *progname, *special; 98 99static struct disklabel *getdisklabel(char *, int); 100static struct disklabel *debug_readlabel(int); 101#ifdef notdef 102static void rewritelabel(char *, int, struct disklabel *); 103#endif 104static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *); 105static void usage(void); 106 107/* CHUNKSIZE should be larger than MAXPHYS */ 108#define CHUNKSIZE (1024 * 1024) 109 110static size_t 111auto_segsize(int fd, off_t len, int version) 112{ 113 off_t off, bw; 114 time_t start, finish; 115 char buf[CHUNKSIZE]; 116 long seeks; 117 size_t final; 118 int i; 119 120 /* First, get sequential access bandwidth */ 121 time(&start); 122 finish = start; 123 for (off = 0; finish - start < 10; off += CHUNKSIZE) { 124 if (pread(fd, buf, CHUNKSIZE, off) < 0) 125 break; 126 time(&finish); 127 } 128 /* Bandwidth = bytes / sec */ 129 /* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */ 130 bw = off / (finish - start); 131 132 /* Second, seek time */ 133 time(&start); 134 finish = start; /* structure copy */ 135 for (seeks = 0; finish - start < 10; ) { 136 off = (((double)rand()) * len) / (off_t)RAND_MAX; 137 if (pread(fd, buf, dbtob(1), off) < 0) 138 break; 139 time(&finish); 140 ++seeks; 141 } 142 /* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */ 143 /* Seek time in units/sec */ 144 seeks /= (finish - start); 145 if (seeks == 0) 146 seeks = 1; 147 148 printf("bw = %ld B/s, seek time %ld ms (%ld seeks/s)\n", 149 (long)bw, 1000/seeks, seeks); 150 final = dbtob(btodb(4 * bw / seeks)); 151 if (version == 1) { 152 for (i = 0; final; final >>= 1, i++) 153 ; 154 final = 1 << i; 155 } 156 printf("using initial segment size %ld\n", (long)final); 157 return final; 158} 159 160int 161main(int argc, char **argv) 162{ 163 int version, ch; 164 struct partition *pp; 165 struct disklabel *lp; 166 struct stat st; 167 int debug, force, fsi, fso, segsize, maxpartitions; 168 uint secsize = 0; 169 daddr_t start; 170 char *cp, *opstring; 171 int byte_sized = 0; 172 173 version = DFL_VERSION; /* what version of lfs to make */ 174 175 if ((progname = strrchr(*argv, '/')) != NULL) 176 ++progname; 177 else 178 progname = *argv; 179 180 maxpartitions = getmaxpartitions(); 181 if (maxpartitions > 26) 182 fatal("insane maxpartitions value %d", maxpartitions); 183 184 opstring = "AB:b:DFf:I:i:LM:m:NO:r:S:s:v:"; 185 186 debug = force = segsize = start = 0; 187 while ((ch = getopt(argc, argv, opstring)) != -1) 188 switch(ch) { 189 case 'A': /* Adaptively configure segment size */ 190 segsize = -1; 191 break; 192 case 'B': /* LFS segment size */ 193 segsize = strsuftoi64("segment size", optarg, LFS_MINSEGSIZE, INT64_MAX, NULL); 194 break; 195 case 'D': 196 debug = 1; 197 break; 198 case 'F': 199 force = 1; 200 break; 201 case 'I': 202 interleave = strsuftoi64("interleave", optarg, 0, INT64_MAX, NULL); 203 break; 204 case 'L': /* Compatibility only */ 205 break; 206 case 'M': 207 minfreeseg = strsuftoi64("minfreeseg", optarg, 0, INT64_MAX, NULL); 208 break; 209 case 'N': 210 Nflag++; 211 break; 212 case 'O': 213 start = strsuftoi64("start", optarg, 0, INT64_MAX, NULL); 214 break; 215 case 'S': 216 secsize = strsuftoi64("sector size", optarg, 1, INT64_MAX, NULL); 217 if (secsize <= 0 || (secsize & (secsize - 1))) 218 fatal("%s: bad sector size", optarg); 219 break; 220#ifdef COMPAT 221 case 'T': 222 disktype = optarg; 223 break; 224#endif 225 case 'b': 226 bsize = strsuftoi64("block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL); 227 break; 228 case 'f': 229 fsize = strsuftoi64("fragment size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL); 230 break; 231 case 'i': 232 ibsize = strsuftoi64("inode block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL); 233 break; 234 case 'm': 235 minfree = strsuftoi64("free space %", optarg, 0, 99, NULL); 236 break; 237 case 'r': 238 roll_id = strsuftoi64("roll-forward id", optarg, 1, UINT_MAX, NULL); 239 break; 240 case 's': 241 fssize = strsuftoi64("file system size", optarg, 0, INT64_MAX, &byte_sized); 242 break; 243 case 'v': 244 version = strsuftoi64("file system version", optarg, 1, LFS_VERSION, NULL); 245 break; 246 case '?': 247 default: 248 usage(); 249 } 250 argc -= optind; 251 argv += optind; 252 253 if (argc != 2 && argc != 1) 254 usage(); 255 256 /* 257 * If the -N flag isn't specified, open the output file. If no path 258 * prefix, try /dev/r%s and then /dev/%s. 259 */ 260 special = argv[0]; 261 if (strchr(special, '/') == NULL) { 262 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV, 263 special); 264 if (stat(device, &st) == -1) 265 (void)snprintf(device, sizeof(device), "%s%s", 266 _PATH_DEV, special); 267 special = device; 268 } 269 if (!Nflag) { 270 fso = open(special, 271 (debug ? O_CREAT : 0) | O_RDWR, DEFFILEMODE); 272 if (fso < 0) 273 fatal("%s: %s", special, strerror(errno)); 274 } else 275 fso = -1; 276 277 /* Open the input file. */ 278 fsi = open(special, O_RDONLY); 279 if (fsi < 0) 280 fatal("%s: %s", special, strerror(errno)); 281 if (fstat(fsi, &st) < 0) 282 fatal("%s: %s", special, strerror(errno)); 283 284 285 if (!S_ISCHR(st.st_mode)) { 286 if (debug) { 287 lp = debug_readlabel(fsi); 288 pp = &lp->d_partitions[0]; 289 } else { 290 static struct partition dummy_pp; 291 lp = NULL; 292 pp = &dummy_pp; 293 pp->p_fstype = FS_BSDLFS; 294 if (secsize == 0) 295 secsize = 512; 296 pp->p_size = st.st_size / secsize; 297 } 298 } else { 299 cp = strchr(argv[0], '\0') - 1; 300 if (!debug 301 && ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) 302 && !isdigit((unsigned char)*cp))) 303 fatal("%s: can't figure out file system partition", argv[0]); 304 305#ifdef COMPAT 306 if (disktype == NULL) 307 disktype = argv[1]; 308#endif 309 lp = getdisklabel(special, fsi); 310 311 if (isdigit((unsigned char)*cp)) 312 pp = &lp->d_partitions[0]; 313 else 314 pp = &lp->d_partitions[*cp - 'a']; 315 if (pp->p_size == 0) 316 fatal("%s: `%c' partition is unavailable", argv[0], *cp); 317 } 318 319 if (secsize == 0) 320 secsize = lp->d_secsize; 321 322 /* From here on out fssize is in sectors */ 323 if (byte_sized) { 324 fssize /= secsize; 325 } 326 327 /* If force, make the partition look like an LFS */ 328 if (force) { 329 pp->p_fstype = FS_BSDLFS; 330 pp->p_size = fssize; 331 /* 0 means to use defaults */ 332 pp->p_fsize = 0; 333 pp->p_frag = 0; 334 pp->p_sgs = 0; 335 } else 336 if (fssize != 0 && fssize < pp->p_size) 337 pp->p_size = fssize; 338 339 /* Try autoconfiguring segment size, if asked to */ 340 if (segsize == -1) { 341 if (!S_ISCHR(st.st_mode)) { 342 warnx("%s is not a character special device, ignoring -A", special); 343 segsize = 0; 344 } else 345 segsize = auto_segsize(fsi, dbtob(pp->p_size), version); 346 } 347 348 /* If we're making a LFS, we break out here */ 349 exit(make_lfs(fso, secsize, pp, minfree, bsize, fsize, segsize, 350 minfreeseg, version, start, ibsize, interleave, 351 roll_id)); 352} 353 354#ifdef COMPAT 355char lmsg[] = "%s: can't read disk label; disk type must be specified"; 356#else 357char lmsg[] = "%s: can't read disk label"; 358#endif 359 360static struct disklabel * 361getdisklabel(char *s, int fd) 362{ 363 static struct disklabel lab; 364 365 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 366#ifdef COMPAT 367 if (disktype) { 368 struct disklabel *lp; 369 370 unlabeled++; 371 lp = getdiskbyname(disktype); 372 if (lp == NULL) 373 fatal("%s: unknown disk type", disktype); 374 return (lp); 375 } 376#endif 377 (void)fprintf(stderr, 378 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno)); 379 fatal(lmsg, s); 380 } 381 return (&lab); 382} 383 384 385static struct disklabel * 386debug_readlabel(int fd) 387{ 388 static struct disklabel lab; 389 int n; 390 391 if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0) 392 fatal("unable to read disk label: %s", strerror(errno)); 393 else if (n < sizeof(struct disklabel)) 394 fatal("short read of disklabel: %d of %ld bytes", n, 395 (u_long) sizeof(struct disklabel)); 396 return(&lab); 397} 398 399#ifdef notdef 400static void 401rewritelabel(char *s, int fd, struct disklabel *lp) 402{ 403#ifdef COMPAT 404 if (unlabeled) 405 return; 406#endif 407 lp->d_checksum = 0; 408 lp->d_checksum = dkcksum(lp); 409 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) { 410 (void)fprintf(stderr, 411 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno)); 412 fatal("%s: can't rewrite disk label", s); 413 } 414#if __vax__ 415 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { 416 int i; 417 int cfd; 418 daddr_t alt; 419 off_t loff; 420 char specname[64]; 421 char blk[1024]; 422 char *cp; 423 424 /* 425 * Make name for 'c' partition. 426 */ 427 strlcpy(specname, s, sizeof(specname)); 428 cp = specname + strlen(specname) - 1; 429 if (!isdigit(*cp)) 430 *cp = 'c'; 431 cfd = open(specname, O_WRONLY); 432 if (cfd < 0) 433 fatal("%s: %s", specname, strerror(errno)); 434 if ((loff = getlabeloffset()) < 0) 435 fatal("getlabeloffset: %s", strerror(errno)); 436 memset(blk, 0, sizeof(blk)); 437 *(struct disklabel *)(blk + loff) = *lp; 438 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; 439 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { 440 if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize), 441 SEEK_SET) == -1) 442 fatal("lseek to badsector area: %s", 443 strerror(errno)); 444 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize) 445 fprintf(stderr, 446 "%s: alternate label %d write: %s\n", 447 progname, i/2, strerror(errno)); 448 } 449 close(cfd); 450 } 451#endif /* vax */ 452} 453#endif /* notdef */ 454 455static int64_t 456strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix) 457{ 458 int64_t result, r1; 459 int shift = 0; 460 char *ep; 461 462 errno = 0; 463 r1 = strtoll(arg, &ep, 10); 464 if (ep[0] != '\0' && ep[1] != '\0') 465 errx(1, "%s `%s' is not a valid number.", desc, arg); 466 switch (ep[0]) { 467 case '\0': 468 case 's': case 'S': 469 if (num_suffix != NULL) 470 *num_suffix = 0; 471 break; 472 case 'g': case 'G': 473 shift += 10; 474 /* FALLTHROUGH */ 475 case 'm': case 'M': 476 shift += 10; 477 /* FALLTHROUGH */ 478 case 'k': case 'K': 479 shift += 10; 480 /* FALLTHROUGH */ 481 case 'b': case 'B': 482 if (num_suffix != NULL) 483 *num_suffix = 1; 484 break; 485 default: 486 errx(1, "`%s' is not a valid suffix for %s.", ep, desc); 487 } 488 result = r1 << shift; 489 if (errno == ERANGE || result >> shift != r1) 490 errx(1, "%s `%s' is too large to convert.", desc, arg); 491 if (result < min) 492 errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").", 493 desc, arg, result, min); 494 if (result > max) 495 errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").", 496 desc, arg, result, max); 497 return result; 498} 499 500void 501usage() 502{ 503 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n"); 504 fprintf(stderr, "where fsoptions are:\n"); 505 fprintf(stderr, "\t-A (autoconfigure segment size)\n"); 506 fprintf(stderr, "\t-B segment size in bytes\n"); 507 fprintf(stderr, "\t-D (debug)\n"); 508 fprintf(stderr, 509 "\t-N (do not create file system, just print out parameters)\n"); 510 fprintf(stderr, "\t-O first segment offset in sectors\n"); 511 fprintf(stderr, "\t-b block size in bytes\n"); 512 fprintf(stderr, "\t-f frag size in bytes\n"); 513 fprintf(stderr, "\t-m minimum free space %%\n"); 514 fprintf(stderr, "\t-s file system size in sectors\n"); 515 fprintf(stderr, "\t-v version\n"); 516 exit(1); 517} 518