Home | History | Annotate | Line # | Download | only in newfs_lfs
newfs.c revision 1.8
      1 /*	$NetBSD: newfs.c,v 1.8 2001/11/01 07:44:05 lukem 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
     44 static char sccsid[] = "@(#)newfs.c	8.5 (Berkeley) 5/24/95";
     45 #else
     46 __RCSID("$NetBSD: newfs.c,v 1.8 2001/11/01 07:44:05 lukem 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 
     81 int	Nflag = 0;		/* run without writing file system */
     82 int	fssize;			/* file system size */
     83 int	sectorsize;		/* bytes/sector */
     84 int	fsize = 0;		/* fragment size */
     85 int	bsize = 0;		/* block size */
     86 int	ibsize = 0;		/* inode block size */
     87 int	interleave = 0;		/* segment interleave */
     88 int	minfree = MINFREE;	/* free space threshold */
     89 int     minfreeseg = 0;         /* free segments reserved for the cleaner */
     90 u_int32_t roll_id = 0;		/* roll-forward id */
     91 u_long	memleft;		/* virtual memory available */
     92 caddr_t	membase;		/* start address of memory based filesystem */
     93 #ifdef COMPAT
     94 char	*disktype;
     95 int	unlabeled;
     96 #endif
     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 void usage(void);
    107 
    108 /* CHUNKSIZE should be larger than MAXPHYS */
    109 #define CHUNKSIZE (1024 * 1024)
    110 
    111 static size_t
    112 auto_segsize(int fd, off_t len, int version)
    113 {
    114 	off_t off, bw;
    115 	time_t start, finish;
    116 	char buf[CHUNKSIZE];
    117 	long seeks;
    118 	size_t final;
    119 	int i;
    120 
    121 	/* First, get sequential access bandwidth */
    122 	time(&start);
    123 	finish = start;
    124 	for (off = 0; finish - start < 10; off += CHUNKSIZE) {
    125 		if (pread(fd, buf, CHUNKSIZE, off) < 0)
    126 			break;
    127 		time(&finish);
    128 	}
    129 	/* Bandwidth = bytes / sec */
    130 	/* printf("%ld bytes in %ld seconds\n", (long)off, (long)(finish - start)); */
    131 	bw = off / (finish - start);
    132 
    133 	/* Second, seek time */
    134 	time(&start);
    135 	finish = start; /* structure copy */
    136 	for (seeks = 0; finish - start < 10; ) {
    137 		off = (((double)rand()) * len) / (off_t)RAND_MAX;
    138 		if (pread(fd, buf, dbtob(1), off) < 0)
    139 			break;
    140 		time(&finish);
    141 		++seeks;
    142 	}
    143 	/* printf("%ld seeks in %ld seconds\n", (long)seeks, (long)(finish - start)); */
    144 	/* Seek time in units/sec */
    145 	seeks /= (finish - start);
    146 	if (seeks == 0)
    147 		seeks = 1;
    148 
    149 	printf("bw = %ld B/s, seek time %ld ms (%ld seeks/s)\n",
    150 		(long)bw, 1000/seeks, seeks);
    151 	final = dbtob(btodb(4 * bw / seeks));
    152 	if (version == 1) {
    153 		for (i = 0; final; final >>= 1, i++)
    154 			;
    155 		final = 1 << i;
    156 	}
    157 	printf("using initial segment size %ld\n", (long)final);
    158 	return final;
    159 }
    160 
    161 int
    162 main(int argc, char **argv)
    163 {
    164 	int version, ch;
    165 	struct partition *pp;
    166 	struct disklabel *lp;
    167 	struct stat st;
    168 	int debug, force, fsi, fso, segsize, maxpartitions;
    169 	daddr_t start;
    170 	char *cp, *opstring;
    171 
    172 	version = DFL_VERSION;		/* what version of lfs to make */
    173 
    174 	if ((progname = strrchr(*argv, '/')) != NULL)
    175 		++progname;
    176 	else
    177 		progname = *argv;
    178 
    179 	maxpartitions = getmaxpartitions();
    180 	if (maxpartitions > 26)
    181 		fatal("insane maxpartitions value %d", maxpartitions);
    182 
    183 	opstring = "AB:b:DFf:I:i:LM:m:NO:r:s:v:";
    184 
    185 	debug = force = segsize = start = 0;
    186 	while ((ch = getopt(argc, argv, opstring)) != -1)
    187 		switch(ch) {
    188 		case 'A':	/* Adaptively configure segment size */
    189 			segsize = -1;
    190 			break;
    191 		case 'B':	/* LFS segment size */
    192 			if ((segsize = atoi(optarg)) < LFS_MINSEGSIZE)
    193 				fatal("%s: bad segment size", optarg);
    194 			break;
    195 		case 'D':
    196 			debug = 1;
    197 			break;
    198 		case 'F':
    199 			force = 1;
    200 			break;
    201 		case 'I':
    202 			interleave = atoi(optarg);
    203 			break;
    204 		case 'L':	/* Compatibility only */
    205 			break;
    206 		case 'M':
    207 			minfreeseg = atoi(optarg);
    208 			break;
    209 		case 'N':
    210 			Nflag++;
    211 			break;
    212 		case 'O':
    213 			start = atoi(optarg);
    214 			break;
    215 #ifdef COMPAT
    216 		case 'T':
    217 			disktype = optarg;
    218 			break;
    219 #endif
    220 		case 'b':
    221 			if ((bsize = atoi(optarg)) < LFS_MINBLOCKSIZE)
    222 				fatal("%s: bad block size", optarg);
    223 			break;
    224 		case 'f':
    225 			if ((fsize = atoi(optarg)) <= 0)
    226 				fatal("%s: bad frag size", optarg);
    227 			break;
    228 		case 'i':
    229 			if ((ibsize = atoi(optarg)) <= 0)
    230 				fatal("%s: bad inode block size", optarg);
    231 			break;
    232 		case 'm':
    233 			if ((minfree = atoi(optarg)) < 0 || minfree > 99)
    234 				fatal("%s: bad free space %%\n", optarg);
    235 			break;
    236 		case 'r':
    237 			if ((roll_id = strtoul(optarg, NULL, 0)) == 0)
    238 				fatal("%s: bad roll-forward id\n", optarg);
    239 			break;
    240 		case 's':
    241 			if ((fssize = atoi(optarg)) <= 0)
    242 				fatal("%s: bad file system size", optarg);
    243 			break;
    244 		case 'v':
    245 			version = atoi(optarg);
    246 			if (version <= 0 || version > LFS_VERSION)
    247 				fatal("%s: bad version", optarg);
    248 			break;
    249 		case '?':
    250 		default:
    251 			usage();
    252 		}
    253 	argc -= optind;
    254 	argv += optind;
    255 
    256 	if (argc != 2 && argc != 1)
    257 		usage();
    258 
    259 	/*
    260 	 * If the -N flag isn't specified, open the output file.  If no path
    261 	 * prefix, try /dev/r%s and then /dev/%s.
    262 	 */
    263 	special = argv[0];
    264 	if (strchr(special, '/') == NULL) {
    265 		(void)snprintf(device, sizeof(device), "%sr%s", _PATH_DEV,
    266 		    special);
    267 		if (stat(device, &st) == -1)
    268 			(void)snprintf(device, sizeof(device), "%s%s",
    269 			    _PATH_DEV, special);
    270 		special = device;
    271 	}
    272 	if (!Nflag) {
    273 		fso = open(special,
    274 		    (debug ? O_CREAT : 0) | O_WRONLY, DEFFILEMODE);
    275 		if (fso < 0)
    276 			fatal("%s: %s", special, strerror(errno));
    277 	} else
    278 		fso = -1;
    279 
    280 	/* Open the input file. */
    281 	fsi = open(special, O_RDONLY);
    282 	if (fsi < 0)
    283 		fatal("%s: %s", special, strerror(errno));
    284 	if (fstat(fsi, &st) < 0)
    285 		fatal("%s: %s", special, strerror(errno));
    286 
    287 
    288 	if (!debug && !S_ISCHR(st.st_mode))
    289 		(void)printf("%s: %s: not a character-special device\n",
    290 		    progname, special);
    291 	cp = strchr(argv[0], '\0') - 1;
    292 	if (!debug
    293 	    && (cp == 0 || ((*cp < 'a' || *cp > ('a' + maxpartitions - 1))
    294 	    && !isdigit(*cp))))
    295 		fatal("%s: can't figure out file system partition", argv[0]);
    296 
    297 #ifdef COMPAT
    298 	if (disktype == NULL)
    299 		disktype = argv[1];
    300 #endif
    301 	if (debug)
    302 		lp = debug_readlabel(fsi);
    303 	else
    304 		lp = getdisklabel(special, fsi);
    305 
    306 	if (isdigit(*cp))
    307 		pp = &lp->d_partitions[0];
    308 	else
    309 		pp = &lp->d_partitions[*cp - 'a'];
    310 	if (pp->p_size == 0)
    311 		fatal("%s: `%c' partition is unavailable", argv[0], *cp);
    312 
    313 	/* If force, make the partition look like an LFS */
    314 	if(force) {
    315 		pp->p_fstype = FS_BSDLFS;
    316 		/* 0 means to use defaults */
    317 		pp->p_fsize  = 0;
    318 		pp->p_frag   = 0;
    319 		pp->p_sgs    = 0;
    320 	}
    321 
    322 	/* Try autoconfiguring segment size, if asked to */
    323 	if (segsize == -1)
    324 		segsize = auto_segsize(fsi, dbtob(pp->p_size), version);
    325 
    326 	/* If we're making a LFS, we break out here */
    327 	exit(make_lfs(fso, lp, pp, minfree, bsize, fsize, segsize,
    328 		      minfreeseg, version, start, ibsize, interleave,
    329                       roll_id));
    330 }
    331 
    332 #ifdef COMPAT
    333 char lmsg[] = "%s: can't read disk label; disk type must be specified";
    334 #else
    335 char lmsg[] = "%s: can't read disk label";
    336 #endif
    337 
    338 static struct disklabel *
    339 getdisklabel(char *s, int fd)
    340 {
    341 	static struct disklabel lab;
    342 
    343 	if (ioctl(fd, DIOCGDINFO, (char *)&lab) < 0) {
    344 #ifdef COMPAT
    345 		if (disktype) {
    346 			struct disklabel *lp;
    347 
    348 			unlabeled++;
    349 			lp = getdiskbyname(disktype);
    350 			if (lp == NULL)
    351 				fatal("%s: unknown disk type", disktype);
    352 			return (lp);
    353 		}
    354 #endif
    355 		(void)fprintf(stderr,
    356 		    "%s: ioctl (GDINFO): %s\n", progname, strerror(errno));
    357 		fatal(lmsg, s);
    358 	}
    359 	return (&lab);
    360 }
    361 
    362 
    363 static struct disklabel *
    364 debug_readlabel(int fd)
    365 {
    366 	static struct disklabel lab;
    367 	int n;
    368 
    369 	if ((n = read(fd, &lab, sizeof(struct disklabel))) < 0)
    370 		fatal("unable to read disk label: %s", strerror(errno));
    371 	else if (n < sizeof(struct disklabel))
    372 		fatal("short read of disklabel: %d of %ld bytes", n,
    373 			(u_long) sizeof(struct disklabel));
    374 	return(&lab);
    375 }
    376 
    377 #ifdef notdef
    378 static void
    379 rewritelabel(char *s, int fd, struct disklabel *lp)
    380 {
    381 #ifdef COMPAT
    382 	if (unlabeled)
    383 		return;
    384 #endif
    385 	lp->d_checksum = 0;
    386 	lp->d_checksum = dkcksum(lp);
    387 	if (ioctl(fd, DIOCWDINFO, (char *)lp) < 0) {
    388 		(void)fprintf(stderr,
    389 		    "%s: ioctl (WDINFO): %s\n", progname, strerror(errno));
    390 		fatal("%s: can't rewrite disk label", s);
    391 	}
    392 #if __vax__
    393 	if (lp->d_type == DTYPE_SMD && lp->d_flags & D_BADSECT) {
    394 		int i;
    395 		int cfd;
    396 		daddr_t alt;
    397 		char specname[64];
    398 		char blk[1024];
    399 		char *cp;
    400 
    401 		/*
    402 		 * Make name for 'c' partition.
    403 		 */
    404 		strcpy(specname, s);
    405 		cp = specname + strlen(specname) - 1;
    406 		if (!isdigit(*cp))
    407 			*cp = 'c';
    408 		cfd = open(specname, O_WRONLY);
    409 		if (cfd < 0)
    410 			fatal("%s: %s", specname, strerror(errno));
    411 		memset(blk, 0, sizeof(blk));
    412 		*(struct disklabel *)(blk + LABELOFFSET) = *lp;
    413 		alt = lp->d_ncylinders * lp->d_secpercyl - lp->d_nsectors;
    414 		for (i = 1; i < 11 && i < lp->d_nsectors; i += 2) {
    415 			if (lseek(cfd, (off_t)((alt + i) * lp->d_secsize),
    416 			    SEEK_SET) == -1)
    417 				fatal("lseek to badsector area: %s",
    418 				    strerror(errno));
    419 			if (write(cfd, blk, lp->d_secsize) < lp->d_secsize)
    420 				fprintf(stderr,
    421 				    "%s: alternate label %d write: %s\n",
    422 				    progname, i/2, strerror(errno));
    423 		}
    424 		close(cfd);
    425 	}
    426 #endif /* vax */
    427 }
    428 #endif /* notdef */
    429 
    430 void
    431 usage()
    432 {
    433 	fprintf(stderr, "usage: newfs_lfs [ -fsoptions ] special-device\n");
    434 	fprintf(stderr, "where fsoptions are:\n");
    435 	fprintf(stderr, "\t-A (autoconfigure segment size)\n");
    436 	fprintf(stderr, "\t-B segment size in bytes\n");
    437 	fprintf(stderr, "\t-D (debug)\n");
    438 	fprintf(stderr,
    439 	    "\t-N (do not create file system, just print out parameters)\n");
    440 	fprintf(stderr, "\t-O first segment offset in sectors\n");
    441 	fprintf(stderr, "\t-b block size in bytes\n");
    442 	fprintf(stderr, "\t-f frag size in bytes\n");
    443 	fprintf(stderr, "\t-m minimum free space %%\n");
    444 	fprintf(stderr, "\t-s file system size in sectors\n");
    445 	fprintf(stderr, "\t-v version\n");
    446 	exit(1);
    447 }
    448