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