Home | History | Annotate | Line # | Download | only in newfs_lfs
newfs.c revision 1.22
      1 /*	$NetBSD: newfs.c,v 1.22 2006/08/06 00:21:14 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
     40 static char sccsid[] = "@(#)newfs.c	8.5 (Berkeley) 5/24/95";
     41 #else
     42 __RCSID("$NetBSD: newfs.c,v 1.22 2006/08/06 00:21:14 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 #include "bufcache.h"
     76 
     77 #define	COMPAT			/* allow non-labeled disks */
     78 
     79 int	Nflag = 0;		/* run without writing file system */
     80 int	fssize;			/* file system size */
     81 int	sectorsize;		/* bytes/sector */
     82 int	fsize = 0;		/* fragment size */
     83 int	bsize = 0;		/* block size */
     84 int	ibsize = 0;		/* inode block size */
     85 int	interleave = 0;		/* segment interleave */
     86 int	minfree = MINFREE;	/* free space threshold */
     87 int     minfreeseg = 0;         /* segments not counted in bfree total */
     88 int     resvseg = 0;            /* free segments reserved for the cleaner */
     89 u_int32_t roll_id = 0;		/* roll-forward id */
     90 u_long	memleft;		/* virtual memory available */
     91 caddr_t	membase;		/* start address of memory based filesystem */
     92 #ifdef COMPAT
     93 char	*disktype;
     94 int	unlabeled;
     95 #endif
     96 int	preen = 0;		/* Coexistence with fsck_lfs */
     97 
     98 char	device[MAXPATHLEN];
     99 char	*progname, *special;
    100 
    101 static struct disklabel *getdisklabel(char *, int);
    102 static struct disklabel *debug_readlabel(int);
    103 #ifdef notdef
    104 static void rewritelabel(char *, int, struct disklabel *);
    105 #endif
    106 static int64_t strsuftoi64(const char *, const char *, int64_t, int64_t, int *);
    107 static void usage(void);
    108 
    109 /* CHUNKSIZE should be larger than MAXPHYS */
    110 #define CHUNKSIZE (1024 * 1024)
    111 
    112 static size_t
    113 auto_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()) * (btodb(len))) / ((off_t)RAND_MAX + 1);
    139 		if (pread(fd, buf, dbtob(1), dbtob(off)) < 0)
    140 			err(1, "pread");
    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 
    162 int
    163 main(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 	uint secsize = 0;
    171 	daddr_t start;
    172 	char *cp;
    173 	const char *opstring;
    174 	int byte_sized = 0;
    175 	int r;
    176 
    177 	version = DFL_VERSION;		/* what version of lfs to make */
    178 
    179 	if ((progname = strrchr(*argv, '/')) != NULL)
    180 		++progname;
    181 	else
    182 		progname = *argv;
    183 
    184 	maxpartitions = getmaxpartitions();
    185 	if (maxpartitions > 26)
    186 		fatal("insane maxpartitions value %d", maxpartitions);
    187 
    188 	opstring = "AB:b:DFf:I:i:LM:m:NO:R:r:S:s:v:";
    189 
    190 	debug = force = segsize = start = 0;
    191 	while ((ch = getopt(argc, argv, opstring)) != -1)
    192 		switch(ch) {
    193 		case 'A':	/* Adaptively configure segment size */
    194 			segsize = -1;
    195 			break;
    196 		case 'B':	/* LFS segment size */
    197 		        segsize = strsuftoi64("segment size", optarg, LFS_MINSEGSIZE, INT64_MAX, NULL);
    198 			break;
    199 		case 'D':
    200 			debug = 1;
    201 			break;
    202 		case 'F':
    203 			force = 1;
    204 			break;
    205 		case 'I':
    206 		        interleave = strsuftoi64("interleave", optarg, 0, INT64_MAX, NULL);
    207 			break;
    208 		case 'L':	/* Compatibility only */
    209 			break;
    210 		case 'M':
    211 		  	minfreeseg = strsuftoi64("minfreeseg", optarg, 0, INT64_MAX, NULL);
    212 			break;
    213 		case 'N':
    214 			Nflag++;
    215 			break;
    216 		case 'O':
    217 		  	start = strsuftoi64("start", optarg, 0, INT64_MAX, NULL);
    218 			break;
    219 		case 'R':
    220 		  	resvseg = strsuftoi64("resvseg", optarg, 0, INT64_MAX, NULL);
    221 			break;
    222 		case 'S':
    223 		  	secsize = strsuftoi64("sector size", optarg, 1, INT64_MAX, NULL);
    224 			if (secsize <= 0 || (secsize & (secsize - 1)))
    225 				fatal("%s: bad sector size", optarg);
    226 			break;
    227 #ifdef COMPAT
    228 		case 'T':
    229 			disktype = optarg;
    230 			break;
    231 #endif
    232 		case 'b':
    233 		  	bsize = strsuftoi64("block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
    234 			break;
    235 		case 'f':
    236 		  	fsize = strsuftoi64("fragment size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
    237 			break;
    238 		case 'i':
    239 		  	ibsize = strsuftoi64("inode block size", optarg, LFS_MINBLOCKSIZE, INT64_MAX, NULL);
    240 			break;
    241 		case 'm':
    242 		  	minfree = strsuftoi64("free space %", optarg, 0, 99, NULL);
    243 			break;
    244 		case 'r':
    245 		  	roll_id = strsuftoi64("roll-forward id", optarg, 1, UINT_MAX, NULL);
    246 			break;
    247 		case 's':
    248 		        fssize = strsuftoi64("file system size", optarg, 0, INT64_MAX, &byte_sized);
    249 			break;
    250 		case 'v':
    251 		        version = strsuftoi64("file system version", optarg, 1, LFS_VERSION, NULL);
    252 			break;
    253 		case '?':
    254 		default:
    255 			usage();
    256 		}
    257 	argc -= optind;
    258 	argv += optind;
    259 
    260 	if (argc != 2 && argc != 1)
    261 		usage();
    262 
    263 	/*
    264 	 * If the -N flag isn't specified, open the output file.  If no path
    265 	 * prefix, try /dev/r%s and then /dev/%s.
    266 	 */
    267 	special = argv[0];
    268 	if (strchr(special, '/') == NULL) {
    269 		(void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    270 		    special);
    271 		if (stat(device, &st) == -1)
    272 			(void)snprintf(device, sizeof(device), "%s%s",
    273 			    _PATH_DEV, special);
    274 		special = device;
    275 	}
    276 	if (!Nflag) {
    277 		fso = open(special,
    278 		    (debug ? O_CREAT : 0) | O_RDWR, DEFFILEMODE);
    279 		if (fso < 0)
    280 			fatal("%s: %s", special, strerror(errno));
    281 	} else
    282 		fso = -1;
    283 
    284 	/* Open the input file. */
    285 	fsi = open(special, O_RDONLY);
    286 	if (fsi < 0)
    287 		fatal("%s: %s", special, strerror(errno));
    288 	if (fstat(fsi, &st) < 0)
    289 		fatal("%s: %s", special, strerror(errno));
    290 
    291 
    292 	if (!S_ISCHR(st.st_mode)) {
    293 		if (!S_ISREG(st.st_mode)) {
    294 			fatal("%s: neither a character special device "
    295 			      "nor a regular file", special);
    296 		}
    297 		if (debug) {
    298 			lp = debug_readlabel(fsi);
    299 			pp = &lp->d_partitions[0];
    300 			if (secsize == 0)
    301 				secsize = 512;
    302 			pp->p_size = st.st_size / secsize;
    303 		} else {
    304 			static struct partition dummy_pp;
    305 			lp = NULL;
    306 			pp = &dummy_pp;
    307 			pp->p_fstype = FS_BSDLFS;
    308 			if (secsize == 0)
    309 				secsize = 512;
    310 			pp->p_size = st.st_size / secsize;
    311 		}
    312 	} else {
    313 		cp = strchr(argv[0], '\0') - 1;
    314 		if (!debug
    315 		    && ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    316 		    && !isdigit((unsigned char)*cp)))
    317 			fatal("%s: can't figure out file system partition", argv[0]);
    318 
    319 #ifdef COMPAT
    320 		if (disktype == NULL)
    321 			disktype = argv[1];
    322 #endif
    323 		lp = getdisklabel(special, fsi);
    324 
    325 		if (isdigit((unsigned char)*cp))
    326 			pp = &lp->d_partitions[0];
    327 		else
    328 			pp = &lp->d_partitions[*cp - 'a'];
    329 		if (pp->p_size == 0)
    330 			fatal("%s: `%c' partition is unavailable", argv[0], *cp);
    331 	}
    332 
    333 	if (secsize == 0)
    334 		secsize = lp->d_secsize;
    335 
    336 	/* From here on out fssize is in sectors */
    337 	if (byte_sized) {
    338 		fssize /= secsize;
    339 	}
    340 
    341 	/* If force, make the partition look like an LFS */
    342 	if (force) {
    343 		pp->p_fstype = FS_BSDLFS;
    344 		if (fssize) {
    345 			pp->p_size = fssize;
    346 		}
    347 		/* 0 means to use defaults */
    348 		pp->p_fsize  = 0;
    349 		pp->p_frag   = 0;
    350 		pp->p_sgs    = 0;
    351 	} else
    352 		if (fssize != 0 && fssize < pp->p_size)
    353 			pp->p_size = fssize;
    354 
    355 	/* Try autoconfiguring segment size, if asked to */
    356 	if (segsize == -1) {
    357 		if (!S_ISCHR(st.st_mode)) {
    358 			warnx("%s is not a character special device, ignoring -A", special);
    359 			segsize = 0;
    360 		} else
    361 			segsize = auto_segsize(fsi, dbtob(pp->p_size), version);
    362 	}
    363 
    364 	/* If we're making a LFS, we break out here */
    365 	r = make_lfs(fso, secsize, pp, minfree, bsize, fsize, segsize,
    366 		      minfreeseg, resvseg, version, start, ibsize, interleave,
    367                       roll_id);
    368 	if (debug)
    369 		bufstats();
    370 	exit(r);
    371 }
    372 
    373 #ifdef COMPAT
    374 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    375 #else
    376 char lmsg[] = "%s: can't read disk label";
    377 #endif
    378 
    379 static struct disklabel *
    380 getdisklabel(char *s, int fd)
    381 {
    382 	static struct disklabel lab;
    383 
    384 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    385 #ifdef COMPAT
    386 		if (disktype) {
    387 			struct disklabel *lp;
    388 
    389 			unlabeled++;
    390 			lp = getdiskbyname(disktype);
    391 			if (lp == NULL)
    392 				fatal("%s: unknown disk type", disktype);
    393 			return (lp);
    394 		}
    395 #endif
    396 		(void)fprintf(stderr,
    397 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
    398 		fatal(lmsg, s);
    399 	}
    400 	return (&lab);
    401 }
    402 
    403 
    404 static struct disklabel *
    405 debug_readlabel(int fd)
    406 {
    407 	static struct disklabel lab;
    408 	int n;
    409 
    410 	if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
    411 		fatal("unable to read disk label: %s", strerror(errno));
    412 	else if (n < sizeof(struct disklabel))
    413 		fatal("short read of disklabel: %d of %ld bytes", n,
    414 			(u_long) sizeof(struct disklabel));
    415 	return(&lab);
    416 }
    417 
    418 #ifdef notdef
    419 static void
    420 rewritelabel(char *s, int fd, struct disklabel *lp)
    421 {
    422 #ifdef COMPAT
    423 	if (unlabeled)
    424 		return;
    425 #endif
    426 	lp->d_checksum = 0;
    427 	lp->d_checksum = dkcksum(lp);
    428 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    429 		(void)fprintf(stderr,
    430 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
    431 		fatal("%s: can't rewrite disk label", s);
    432 	}
    433 #if __vax__
    434 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    435 		int i;
    436 		int cfd;
    437 		daddr_t alt;
    438 		off_t loff;
    439 		char specname[64];
    440 		char blk[1024];
    441 		char *cp;
    442 
    443 		/*
    444 		 * Make name for 'c' partition.
    445 		 */
    446 		strlcpy(specname, s, sizeof(specname));
    447 		cp = specname + strlen(specname) - 1;
    448 		if (!isdigit(*cp))
    449 			*cp = 'c';
    450 		cfd = open(specname, O_WRONLY);
    451 		if (cfd < 0)
    452 			fatal("%s: %s", specname, strerror(errno));
    453 		if ((loff = getlabeloffset()) < 0)
    454 			fatal("getlabeloffset: %s", strerror(errno));
    455 		memset(blk, 0, sizeof(blk));
    456 		*(struct disklabel *)(blk + loff) = *lp;
    457 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    458 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    459 			if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
    460 			    SEEK_SET) == -1)
    461 				fatal("lseek to badsector area: %s",
    462 				    strerror(errno));
    463 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    464 				fprintf(stderr,
    465 				    "%s: alternate label %d write: %s\n",
    466 				    progname, i/2, strerror(errno));
    467 		}
    468 		close(cfd);
    469 	}
    470 #endif /* vax */
    471 }
    472 #endif /* notdef */
    473 
    474 static int64_t
    475 strsuftoi64(const char *desc, const char *arg, int64_t min, int64_t max, int *num_suffix)
    476 {
    477 	int64_t result, r1;
    478 	int shift = 0;
    479 	char	*ep;
    480 
    481 	errno = 0;
    482 	r1 = strtoll(arg, &ep, 10);
    483 	if (ep[0] != '\0' && ep[1] != '\0')
    484 		errx(1, "%s `%s' is not a valid number.", desc, arg);
    485 	switch (ep[0]) {
    486 	case '\0':
    487 	case 's': case 'S':
    488 		if (num_suffix != NULL)
    489 			*num_suffix = 0;
    490 		break;
    491 	case 'g': case 'G':
    492 		shift += 10;
    493 		/* FALLTHROUGH */
    494 	case 'm': case 'M':
    495 		shift += 10;
    496 		/* FALLTHROUGH */
    497 	case 'k': case 'K':
    498 		shift += 10;
    499 		/* FALLTHROUGH */
    500 	case 'b': case 'B':
    501 		if (num_suffix != NULL)
    502 			*num_suffix = 1;
    503 		break;
    504 	default:
    505 		errx(1, "`%s' is not a valid suffix for %s.", ep, desc);
    506 	}
    507 	result = r1 << shift;
    508 	if (errno == ERANGE || result >> shift != r1)
    509 		errx(1, "%s `%s' is too large to convert.", desc, arg);
    510 	if (result < min)
    511 		errx(1, "%s `%s' (%" PRId64 ") is less than the minimum (%" PRId64 ").",
    512 		    desc, arg, result, min);
    513 	if (result > max)
    514 		errx(1, "%s `%s' (%" PRId64 ") is greater than the maximum (%" PRId64 ").",
    515 		    desc, arg, result, max);
    516 	return result;
    517 }
    518 
    519 void
    520 usage()
    521 {
    522 	fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
    523 	fprintf(stderr, "where fsoptions are:\n");
    524 	fprintf(stderr, "\t-A (autoconfigure segment size)\n");
    525 	fprintf(stderr, "\t-B segment size in bytes\n");
    526 	fprintf(stderr, "\t-D (debug)\n");
    527 	fprintf(stderr, "\t-M count of segments not counted in bfree\n");
    528 	fprintf(stderr,
    529 	    "\t-N (do not create file system, just print out parameters)\n");
    530 	fprintf(stderr, "\t-O first segment offset in sectors\n");
    531 	fprintf(stderr, "\t-R count of segments reserved for the cleaner\n");
    532 	fprintf(stderr, "\t-b block size in bytes\n");
    533 	fprintf(stderr, "\t-f frag size in bytes\n");
    534 	fprintf(stderr, "\t-m minimum free space %%\n");
    535 	fprintf(stderr, "\t-s file system size in sectors\n");
    536 	fprintf(stderr, "\t-v version\n");
    537 	exit(1);
    538 }
    539