Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.30
      1 /*	$NetBSD: tunefs.c,v 1.30 2004/03/21 20:38:08 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 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) 1983, 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[] = "@(#)tunefs.c	8.3 (Berkeley) 5/3/95";
     41 #else
     42 __RCSID("$NetBSD: tunefs.c,v 1.30 2004/03/21 20:38:08 dsl Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 /*
     47  * tunefs: change layout parameters to an existing file system.
     48  */
     49 #include <sys/param.h>
     50 
     51 #include <ufs/ufs/dinode.h>
     52 #include <ufs/ffs/fs.h>
     53 #include <ufs/ffs/ffs_extern.h>
     54 
     55 #include <machine/bswap.h>
     56 
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <fcntl.h>
     60 #include <fstab.h>
     61 #include <paths.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <unistd.h>
     66 #include <util.h>
     67 
     68 /* the optimization warning string template */
     69 #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
     70 
     71 union {
     72 	struct	fs sb;
     73 	char pad[MAXBSIZE];
     74 } sbun;
     75 #define	sblock sbun.sb
     76 char buf[MAXBSIZE];
     77 
     78 int	fi;
     79 long	dev_bsize = 512;
     80 int	needswap = 0;
     81 int	is_ufs2 = 0;
     82 off_t	sblockloc;
     83 
     84 static off_t sblock_try[] = SBLOCKSEARCH;
     85 
     86 static	void	bwrite(daddr_t, char *, int, const char *);
     87 static	void	bread(daddr_t, char *, int, const char *);
     88 static	int	getnum(const char *, const char *, int, int);
     89 static	void	getsb(struct fs *, const char *);
     90 static	int	openpartition(const char *, int, char *, size_t);
     91 static	void	usage(void);
     92 int		main(int, char *[]);
     93 
     94 int
     95 main(int argc, char *argv[])
     96 {
     97 #define	OPTSTRINGBASE	"AFNe:g:h:m:o:"
     98 #ifdef TUNEFS_SOFTDEP
     99 	int		softdep;
    100 #define	OPTSTRING	OPTSTRINGBASE ## "n:"
    101 #else
    102 #define	OPTSTRING	OPTSTRINGBASE
    103 #endif
    104 	int		i, ch, Aflag, Fflag, Nflag, openflags;
    105 	const char	*special, *chg[2];
    106 	char		device[MAXPATHLEN];
    107 	int		maxbpg, minfree, optim;
    108 	int		avgfilesize, avgfpdir;
    109 
    110 	Aflag = Fflag = Nflag = 0;
    111 	maxbpg = minfree = optim = -1;
    112 	avgfilesize = avgfpdir = -1;
    113 #ifdef TUNEFS_SOFTDEP
    114 	softdep = -1;
    115 #endif
    116 	chg[FS_OPTSPACE] = "space";
    117 	chg[FS_OPTTIME] = "time";
    118 
    119 	while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
    120 		switch (ch) {
    121 
    122 		case 'A':
    123 			Aflag++;
    124 			break;
    125 
    126 		case 'F':
    127 			Fflag++;
    128 			break;
    129 
    130 		case 'N':
    131 			Nflag++;
    132 			break;
    133 
    134 		case 'e':
    135 			maxbpg = getnum(optarg,
    136 			    "maximum blocks per file in a cylinder group",
    137 			    1, INT_MAX);
    138 			break;
    139 
    140 		case 'g':
    141 			avgfilesize = getnum(optarg,
    142 			    "average file size", 1, INT_MAX);
    143 			break;
    144 
    145 		case 'h':
    146 			avgfpdir = getnum(optarg,
    147 			    "expected number of files per directory",
    148 			    1, INT_MAX);
    149 			break;
    150 
    151 		case 'm':
    152 			minfree = getnum(optarg,
    153 			    "minimum percentage of free space", 0, 99);
    154 			break;
    155 
    156 #ifdef TUNEFS_SOFTDEP
    157 		case 'n':
    158 			if (strcmp(optarg, "enable") == 0)
    159 				softdep = 1;
    160 			else if (strcmp(optarg, "disable") == 0)
    161 				softdep = 0;
    162 			else {
    163 				errx(10, "bad soft dependencies "
    164 					"(options are `enable' or `disable')");
    165 			}
    166 			break;
    167 #endif
    168 
    169 		case 'o':
    170 			if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
    171 				optim = FS_OPTSPACE;
    172 			else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
    173 				optim = FS_OPTTIME;
    174 			else
    175 				errx(10,
    176 				    "bad %s (options are `space' or `time')",
    177 				    "optimization preference");
    178 			break;
    179 
    180 		default:
    181 			usage();
    182 		}
    183 	}
    184 	argc -= optind;
    185 	argv += optind;
    186 	if (argc != 1)
    187 		usage();
    188 
    189 	special = argv[0];
    190 	openflags = Nflag ? O_RDONLY : O_RDWR;
    191 	if (Fflag)
    192 		fi = open(special, openflags);
    193 	else {
    194 		fi = openpartition(special, openflags, device, sizeof(device));
    195 		special = device;
    196 	}
    197 	if (fi == -1)
    198 		err(1, "%s", special);
    199 	getsb(&sblock, special);
    200 
    201 #define CHANGEVAL(old, new, type, suffix) do				\
    202 	if ((new) != -1) {						\
    203 		if ((new) == (old))					\
    204 			warnx("%s remains unchanged at %d%s",		\
    205 			    (type), (old), (suffix));			\
    206 		else {							\
    207 			warnx("%s changes from %d%s to %d%s",		\
    208 			    (type), (old), (suffix), (new), (suffix));	\
    209 			(old) = (new);					\
    210 		}							\
    211 	} while (/* CONSTCOND */0)
    212 
    213 	warnx("tuning %s", special);
    214 	CHANGEVAL(sblock.fs_maxbpg, maxbpg,
    215 	    "maximum blocks per file in a cylinder group", "");
    216 	CHANGEVAL(sblock.fs_minfree, minfree,
    217 	    "minimum percentage of free space", "%");
    218 	if (minfree != -1) {
    219 		if (minfree >= MINFREE &&
    220 		    sblock.fs_optim == FS_OPTSPACE)
    221 			warnx(OPTWARN, "time", ">=", MINFREE);
    222 		if (minfree < MINFREE &&
    223 		    sblock.fs_optim == FS_OPTTIME)
    224 			warnx(OPTWARN, "space", "<", MINFREE);
    225 	}
    226 #ifdef TUNEFS_SOFTDEP
    227 	if (softdep == 1) {
    228 		sblock.fs_flags |= FS_DOSOFTDEP;
    229 		warnx("soft dependencies set");
    230 	} else if (softdep == 0) {
    231 		sblock.fs_flags &= ~FS_DOSOFTDEP;
    232 		warnx("soft dependencies cleared");
    233 	}
    234 #endif
    235 	if (optim != -1) {
    236 		if (sblock.fs_optim == optim) {
    237 			warnx("%s remains unchanged as %s",
    238 			    "optimization preference",
    239 			    chg[optim]);
    240 		} else {
    241 			warnx("%s changes from %s to %s",
    242 			    "optimization preference",
    243 			    chg[sblock.fs_optim], chg[optim]);
    244 			sblock.fs_optim = optim;
    245 			if (sblock.fs_minfree >= MINFREE &&
    246 			    optim == FS_OPTSPACE)
    247 				warnx(OPTWARN, "time", ">=", MINFREE);
    248 			if (sblock.fs_minfree < MINFREE &&
    249 			    optim == FS_OPTTIME)
    250 				warnx(OPTWARN, "space", "<", MINFREE);
    251 		}
    252 	}
    253 	CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
    254 	    "average file size", "");
    255 	CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
    256 	    "expected number of files per directory", "");
    257 
    258 	if (Nflag) {
    259 		fprintf(stdout, "tunefs: current settings of %s\n", special);
    260 		fprintf(stdout, "\tmaximum contiguous block count %d\n",
    261 		    sblock.fs_maxcontig);
    262 		fprintf(stdout,
    263 		    "\tmaximum blocks per file in a cylinder group %d\n",
    264 		    sblock.fs_maxbpg);
    265 		fprintf(stdout, "\tminimum percentage of free space %d%%\n",
    266 		    sblock.fs_minfree);
    267 #ifdef TUNEFS_SOFTDEP
    268 		fprintf(stdout, "\tsoft dependencies: %s\n",
    269 		    (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
    270 #endif
    271 		fprintf(stdout, "\toptimization preference: %s\n",
    272 		    chg[sblock.fs_optim]);
    273 		fprintf(stdout, "\taverage file size: %d\n",
    274 		    sblock.fs_avgfilesize);
    275 		fprintf(stdout,
    276 		    "\texpected number of files per directory: %d\n",
    277 		    sblock.fs_avgfpdir);
    278 		fprintf(stdout, "tunefs: no changes made\n");
    279 		exit(0);
    280 	}
    281 
    282 	memcpy(buf, (char *)&sblock, SBLOCKSIZE);
    283 	if (needswap)
    284 		ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
    285 	bwrite(sblockloc, buf, SBLOCKSIZE, special);
    286 	if (Aflag)
    287 		for (i = 0; i < sblock.fs_ncg; i++)
    288 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
    289 			    buf, SBLOCKSIZE, special);
    290 	close(fi);
    291 	exit(0);
    292 }
    293 
    294 static int
    295 getnum(const char *num, const char *desc, int min, int max)
    296 {
    297 	long	n;
    298 	char	*ep;
    299 
    300 	n = strtol(num, &ep, 10);
    301 	if (ep[0] != '\0')
    302 		errx(1, "Invalid number `%s' for %s", num, desc);
    303 	if ((int) n < min)
    304 		errx(1, "%s `%s' too small (minimum is %d)", desc, num, min);
    305 	if ((int) n > max)
    306 		errx(1, "%s `%s' too large (maximum is %d)", desc, num, max);
    307 	return ((int)n);
    308 }
    309 
    310 static void
    311 usage(void)
    312 {
    313 
    314 	fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
    315 	fprintf(stderr, "where tuneup-options are:\n");
    316 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    317 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    318 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    319 	fprintf(stderr, "\t-g average file size\n");
    320 	fprintf(stderr, "\t-h expected number of files per directory\n");
    321 	fprintf(stderr, "\t-k track skew in sectors\n");
    322 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    323 #ifdef TUNEFS_SOFTDEP
    324 	fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
    325 #endif
    326 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    327 	exit(2);
    328 }
    329 
    330 static void
    331 getsb(struct fs *fs, const char *file)
    332 {
    333 	int i;
    334 
    335 	for (i = 0; ; i++) {
    336 		if (sblock_try[i] == -1)
    337 			errx(5, "cannot find filesystem superblock");
    338 		bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
    339 		switch(fs->fs_magic) {
    340 		case FS_UFS2_MAGIC:
    341 			is_ufs2 = 1;
    342 			/*FALLTHROUGH*/
    343 		case FS_UFS1_MAGIC:
    344 			break;
    345 		case FS_UFS2_MAGIC_SWAPPED:
    346 			is_ufs2 = 1;
    347 			/*FALLTHROUGH*/
    348 		case FS_UFS1_MAGIC_SWAPPED:
    349 			warnx("%s: swapping byte order", file);
    350 			needswap = 1;
    351 			ffs_sb_swap(fs, fs);
    352 			break;
    353 		default:
    354 			continue;
    355 		}
    356 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    357 			continue;
    358 		if (fs->fs_old_flags & FS_FLAGS_UPDATED
    359 		    && fs->fs_sblockloc != sblock_try[i])
    360 			continue;
    361 		break;
    362 	}
    363 
    364 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    365 	sblockloc = sblock_try[i] / dev_bsize;
    366 }
    367 
    368 static void
    369 bwrite(daddr_t blk, char *buffer, int size, const char *file)
    370 {
    371 	off_t	offset;
    372 
    373 	offset = (off_t)blk * dev_bsize;
    374 	if (lseek(fi, offset, SEEK_SET) == -1)
    375 		err(6, "%s: seeking to %lld", file, (long long)offset);
    376 	if (write(fi, buffer, size) != size)
    377 		err(7, "%s: writing %d bytes", file, size);
    378 }
    379 
    380 static void
    381 bread(daddr_t blk, char *buffer, int cnt, const char *file)
    382 {
    383 	off_t	offset;
    384 	int	i;
    385 
    386 	offset = (off_t)blk * dev_bsize;
    387 	if (lseek(fi, offset, SEEK_SET) == -1)
    388 		err(4, "%s: seeking to %lld", file, (long long)offset);
    389 	if ((i = read(fi, buffer, cnt)) != cnt)
    390 		errx(5, "%s: short read", file);
    391 }
    392 
    393 static int
    394 openpartition(const char *name, int flags, char *device, size_t devicelen)
    395 {
    396 	char		rawspec[MAXPATHLEN], *p;
    397 	struct fstab	*fs;
    398 	int		fd, oerrno;
    399 
    400 	fs = getfsfile(name);
    401 	if (fs) {
    402 		if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
    403 			snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
    404 			    (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
    405 			name = rawspec;
    406 		} else
    407 			name = fs->fs_spec;
    408 	}
    409 	fd = opendisk(name, flags, device, devicelen, 0);
    410 	if (fd == -1 && errno == ENOENT) {
    411 		oerrno = errno;
    412 		strlcpy(device, name, devicelen);
    413 		errno = oerrno;
    414 	}
    415 	return (fd);
    416 }
    417