newfs.c revision 1.6
1/* $NetBSD: newfs.c,v 1.6 2000/12/05 19:51:15 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. 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.6 2000/12/05 19:51:15 perseant 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 <errno.h> 69#include <unistd.h> 70#include <stdio.h> 71#include <stdlib.h> 72#include <ctype.h> 73#include <string.h> 74#include <paths.h> 75#include <util.h> 76#include "config.h" 77#include "extern.h" 78 79#define COMPAT /* allow non-labeled disks */ 80 81int Nflag; /* run without writing file system */ 82int fssize; /* file system size */ 83int sectorsize; /* bytes/sector */ 84int fsize = 0; /* fragment size */ 85int bsize = 0; /* block size */ 86int minfree = MINFREE; /* free space threshold */ 87int minfreeseg = 0; /* free segments reserved for the cleaner */ 88int bbsize = BBSIZE; /* boot block size */ 89int sbsize = SBSIZE; /* superblock size */ 90u_long memleft; /* virtual memory available */ 91caddr_t membase; /* start address of memory based filesystem */ 92#ifdef COMPAT 93char *disktype; 94int unlabeled; 95#endif 96 97char device[MAXPATHLEN]; 98char *progname, *special; 99 100int main __P((int, char **)); 101static struct disklabel *getdisklabel __P((char *, int)); 102static struct disklabel *debug_readlabel __P((int)); 103#ifdef notdef 104static void rewritelabel __P((char *, int, struct disklabel *)); 105#endif 106static void usage __P((void)); 107 108#define CHUNKSIZE 65536 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("bandwidth %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 152 /* Version 1 filesystems have po2 segment sizes */ 153 if (version == 1) { 154 for (i = 0; final; final >>= 1, i++) 155 ; 156 final = 1 << i; 157 } 158 159 printf("using initial segment size %ld\n", (long)final); 160 return final; 161} 162 163int 164main(argc, argv) 165 int argc; 166 char *argv[]; 167{ 168 int ch; 169 struct partition *pp; 170 struct disklabel *lp; 171 struct stat st; 172 int debug, force, fsi, fso, segsize, maxpartitions; 173 char *cp, *opstring; 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:DFLNb:f:M:m:s:"; 185 186 debug = force = segsize = 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 'L': /* Compatibility only */ 203 break; 204 case 'M': 205 minfreeseg = atoi(optarg); 206 break; 207 case 'N': 208 Nflag++; 209 break; 210#ifdef COMPAT 211 case 'T': 212 disktype = optarg; 213 break; 214#endif 215 case 'b': 216 if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE) 217 fatal("%s: bad block size", optarg); 218 break; 219 case 'f': 220 if ((fsize = atoi(optarg)) <= 0) 221 fatal("%s: bad frag size", optarg); 222 break; 223 case 'm': 224 if ((minfree = atoi(optarg)) < 0 || minfree > 99) 225 fatal("%s: bad free space %%\n", optarg); 226 break; 227 case 's': 228 if ((fssize = atoi(optarg)) <= 0) 229 fatal("%s: bad file system size", optarg); 230 break; 231 case '?': 232 default: 233 usage(); 234 } 235 argc -= optind; 236 argv += optind; 237 238 if (argc != 2 && argc != 1) 239 usage(); 240 241 /* 242 * If the -N flag isn't specified, open the output file. If no path 243 * prefix, try /dev/r%s and then /dev/%s. 244 */ 245 special = argv[0]; 246 if (strchr(special, '/') == NULL) { 247 (void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV, 248 special); 249 if (stat(device, &st) == -1) 250 (void)snprintf(device, sizeof(device), "%s%s", 251 _PATH_DEV, special); 252 special = device; 253 } 254 if (!Nflag) { 255 fso = open(special, 256 (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE); 257 if (fso < 0) 258 fatal("%s: %s", special, strerror(errno)); 259 } else 260 fso = -1; 261 262 /* Open the input file. */ 263 fsi = open(special, O_RDONLY); 264 if (fsi < 0) 265 fatal("%s: %s", special, strerror(errno)); 266 if (fstat(fsi, &st) < 0) 267 fatal("%s: %s", special, strerror(errno)); 268 269 270 if (!debug && !S_ISCHR(st.st_mode)) 271 (void)printf("%s: %s: not a character-special device\n", 272 progname, special); 273 cp = strchr(argv[0], '\0') - 1; 274 if (!debug 275 && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) 276 && !isdigit(*cp)))) 277 fatal("%s: can't figure out file system partition", argv[0]); 278 279#ifdef COMPAT 280 if (disktype == NULL) 281 disktype = argv[1]; 282#endif 283 if (debug) 284 lp = debug_readlabel(fsi); 285 else 286 lp = getdisklabel(special, fsi); 287 288 if (isdigit(*cp)) 289 pp = &lp->d_partitions[0]; 290 else 291 pp = &lp->d_partitions[*cp - 'a']; 292 if (pp->p_size == 0) 293 fatal("%s: `%c' partition is unavailable", argv[0], *cp); 294 295 /* If force, make the partition look like an LFS */ 296 if(force) { 297 pp->p_fstype = FS_BSDLFS; 298 /* 0 means to use defaults */ 299 pp->p_fsize = 0; 300 pp->p_frag = 0; 301 pp->p_sgs = 0; 302 } 303 304 /* Try autoconfiguring segment size, if asked to */ 305 if (segsize == -1) 306 segsize = auto_segsize(fsi, dbtob(pp->p_size), 1); 307 308 /* If we're making a LFS, we break out here */ 309 exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize, 310 minfreeseg)); 311} 312 313#ifdef COMPAT 314char lmsg[] = "%s: can't read disk label; disk type must be specified"; 315#else 316char lmsg[] = "%s: can't read disk label"; 317#endif 318 319static struct disklabel * 320getdisklabel(s, fd) 321 char *s; 322 int fd; 323{ 324 static struct disklabel lab; 325 326 if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) { 327#ifdef COMPAT 328 if (disktype) { 329 struct disklabel *lp; 330 331 unlabeled++; 332 lp = getdiskbyname(disktype); 333 if (lp == NULL) 334 fatal("%s: unknown disk type", disktype); 335 return (lp); 336 } 337#endif 338 (void)fprintf(stderr, 339 "%s: ioctl (GDINFO): %s\n", progname, strerror(errno)); 340 fatal(lmsg, s); 341 } 342 return (&lab); 343} 344 345 346static struct disklabel * 347debug_readlabel(fd) 348 int fd; 349{ 350 static struct disklabel lab; 351 int n; 352 353 if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0) 354 fatal("unable to read disk label: %s", strerror(errno)); 355 else if (n < sizeof(struct disklabel)) 356 fatal("short read of disklabel: %d of %ld bytes", n, 357 (u_long) sizeof(struct disklabel)); 358 return(&lab); 359} 360 361#ifdef notdef 362static void 363rewritelabel(s, fd, lp) 364 char *s; 365 int fd; 366 struct disklabel *lp; 367{ 368#ifdef COMPAT 369 if (unlabeled) 370 return; 371#endif 372 lp->d_checksum = 0; 373 lp->d_checksum = dkcksum(lp); 374 if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) { 375 (void)fprintf(stderr, 376 "%s: ioctl (WDINFO): %s\n", progname, strerror(errno)); 377 fatal("%s: can't rewrite disk label", s); 378 } 379#if __vax__ 380 if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) { 381 int i; 382 int cfd; 383 daddr_t alt; 384 char specname[64]; 385 char blk[1024]; 386 char *cp; 387 388 /* 389 * Make name for 'c' partition. 390 */ 391 strcpy(specname, s); 392 cp = specname + strlen(specname) - 1; 393 if (!isdigit(*cp)) 394 *cp = 'c'; 395 cfd = open(specname, O_WRONLY); 396 if (cfd < 0) 397 fatal("%s: %s", specname, strerror(errno)); 398 memset(blk, 0, sizeof(blk)); 399 *(struct disklabel *)(blk + LABELOFFSET) = *lp; 400 alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors; 401 for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) { 402 if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize), 403 SEEK_SET) == -1) 404 fatal("lseek to badsector area: %s", 405 strerror(errno)); 406 if (write(cfd, blk, lp->d_secsize) < lp->d_secsize) 407 fprintf(stderr, 408 "%s: alternate label %d write: %s\n", 409 progname, i/2, strerror(errno)); 410 } 411 close(cfd); 412 } 413#endif /* vax */ 414} 415#endif /* notdef */ 416 417void 418usage() 419{ 420 fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n"); 421 fprintf(stderr, "where fsoptions are:\n"); 422 fprintf(stderr, "\t-A (autoconfigure segment size)\n"); 423 fprintf(stderr, "\t-B segment size in bytes\n"); 424 fprintf(stderr, "\t-D debug\n"); 425 fprintf(stderr, 426 "\t-N (do not create file system, just print out parameters)\n"); 427 fprintf(stderr, "\t-b block size in bytes\n"); 428 fprintf(stderr, "\t-f frag size in bytes\n"); 429 fprintf(stderr, "\t-m minimum free space %%\n"); 430 fprintf(stderr, "\t-s file system size in sectors\n"); 431 exit(1); 432} 433