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