Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.23
      1  1.23    lukem /*	$NetBSD: tunefs.c,v 1.23 2001/09/03 15:04:39 lukem Exp $	*/
      2  1.10      cgd 
      3   1.1      cgd /*
      4   1.8  mycroft  * Copyright (c) 1983, 1993
      5   1.8  mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36  1.11    lukem #include <sys/cdefs.h>
     37   1.1      cgd #ifndef lint
     38  1.11    lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
     39  1.11    lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1      cgd #endif /* not lint */
     41   1.1      cgd 
     42   1.1      cgd #ifndef lint
     43  1.10      cgd #if 0
     44  1.12    lukem static char sccsid[] = "@(#)tunefs.c	8.3 (Berkeley) 5/3/95";
     45  1.10      cgd #else
     46  1.23    lukem __RCSID("$NetBSD: tunefs.c,v 1.23 2001/09/03 15:04:39 lukem Exp $");
     47  1.10      cgd #endif
     48   1.1      cgd #endif /* not lint */
     49   1.1      cgd 
     50   1.1      cgd /*
     51   1.1      cgd  * tunefs: change layout parameters to an existing file system.
     52   1.1      cgd  */
     53   1.1      cgd #include <sys/param.h>
     54   1.1      cgd #include <sys/stat.h>
     55   1.8  mycroft 
     56  1.12    lukem #include <ufs/ufs/dinode.h>
     57   1.8  mycroft #include <ufs/ffs/fs.h>
     58  1.13   bouyer #include <ufs/ffs/ffs_extern.h>
     59  1.18   bouyer 
     60  1.18   bouyer #include <machine/bswap.h>
     61   1.8  mycroft 
     62  1.22    lukem #include <err.h>
     63   1.8  mycroft #include <errno.h>
     64   1.6      cgd #include <fcntl.h>
     65   1.1      cgd #include <fstab.h>
     66  1.22    lukem #include <paths.h>
     67   1.1      cgd #include <stdio.h>
     68   1.6      cgd #include <stdlib.h>
     69  1.14  thorpej #include <string.h>
     70   1.6      cgd #include <unistd.h>
     71   1.6      cgd 
     72   1.6      cgd /* the optimization warning string template */
     73   1.8  mycroft #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
     74   1.1      cgd 
     75   1.1      cgd union {
     76   1.1      cgd 	struct	fs sb;
     77   1.1      cgd 	char pad[MAXBSIZE];
     78   1.1      cgd } sbun;
     79   1.1      cgd #define	sblock sbun.sb
     80  1.13   bouyer char buf[MAXBSIZE];
     81   1.1      cgd 
     82  1.22    lukem int	fi;
     83  1.22    lukem long	dev_bsize = 1;
     84  1.22    lukem int	needswap = 0;
     85  1.22    lukem 
     86  1.22    lukem static	void	bwrite(daddr_t, char *, int);
     87  1.22    lukem static	int	bread(daddr_t, char *, int);
     88  1.22    lukem static	int	getnum(const char *, const char *, int, int);
     89  1.22    lukem static	void	getsb(struct fs *, const char *);
     90  1.22    lukem static	void	usage(void);
     91  1.22    lukem int		main(int, char *[]);
     92   1.6      cgd 
     93   1.6      cgd int
     94  1.22    lukem main(int argc, char *argv[])
     95   1.1      cgd {
     96  1.20     fvdl #ifdef TUNEFS_SOFTDEP
     97  1.22    lukem 	int		softdep;
     98  1.23    lukem #define	OPTSTRING	"AFNa:d:e:k:m:n:o:t:"
     99  1.22    lukem #else
    100  1.23    lukem #define	OPTSTRING	"AFNa:d:e:k:m:o:t:"
    101  1.22    lukem #endif
    102  1.22    lukem 	struct stat	st;
    103  1.22    lukem 	int		i, ch, Aflag, Fflag, Nflag;
    104  1.22    lukem 	struct fstab	*fs;
    105  1.22    lukem 	const char	*special, *chg[2];
    106  1.22    lukem 	char		device[MAXPATHLEN];
    107  1.22    lukem 	int		maxbpg, maxcontig, minfree, rotdelay, optim, trackskew;
    108  1.22    lukem 
    109  1.22    lukem 	Aflag = Fflag = Nflag = 0;
    110  1.22    lukem 	maxbpg = maxcontig = minfree = rotdelay = optim = trackskew = -1;
    111  1.22    lukem #ifdef TUNEFS_SOFTDEP
    112  1.22    lukem 	softdep = -1;
    113  1.22    lukem #endif
    114  1.22    lukem 	chg[FS_OPTSPACE] = "space";
    115  1.22    lukem 	chg[FS_OPTTIME] = "time";
    116  1.22    lukem 
    117  1.22    lukem 	while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
    118  1.22    lukem 		switch (ch) {
    119  1.22    lukem 
    120  1.22    lukem 		case 'A':
    121  1.22    lukem 			Aflag++;
    122  1.22    lukem 			break;
    123  1.22    lukem 
    124  1.22    lukem 		case 'F':
    125  1.22    lukem 			Fflag++;
    126  1.22    lukem 			break;
    127  1.22    lukem 
    128  1.22    lukem 		case 'N':
    129  1.22    lukem 			Nflag++;
    130  1.22    lukem 			break;
    131  1.22    lukem 
    132  1.22    lukem 		case 'a':
    133  1.22    lukem 			maxcontig = getnum(optarg,
    134  1.22    lukem 			    "maximum contiguous block count", 1, INT_MAX);
    135  1.22    lukem 			break;
    136  1.22    lukem 
    137  1.22    lukem 		case 'd':
    138  1.22    lukem 			rotdelay = getnum(optarg,
    139  1.22    lukem 			    "rotational delay between contiguous blocks",
    140  1.22    lukem 			    0, INT_MAX);
    141  1.22    lukem 			break;
    142  1.22    lukem 
    143  1.22    lukem 		case 'e':
    144  1.22    lukem 			maxbpg = getnum(optarg,
    145  1.22    lukem 			    "maximum blocks per file in a cylinder group",
    146  1.22    lukem 			    1, INT_MAX);
    147  1.22    lukem 			break;
    148  1.22    lukem 
    149  1.22    lukem 
    150  1.22    lukem 		case 'm':
    151  1.22    lukem 			minfree = getnum(optarg,
    152  1.22    lukem 			    "minimum percentage of free space", 0, 99);
    153  1.22    lukem 			break;
    154  1.22    lukem 
    155  1.22    lukem #ifdef TUNEFS_SOFTDEP
    156  1.22    lukem 		case 'n':
    157  1.22    lukem 			if (strcmp(optarg, "enable") == 0)
    158  1.22    lukem 				softdep = 1;
    159  1.22    lukem 			else if (strcmp(optarg, "disable") == 0)
    160  1.22    lukem 				softdep = 0;
    161  1.22    lukem 			else {
    162  1.22    lukem 				errx(10, "bad soft dependencies "
    163  1.22    lukem 					"(options are `enable' or `disable')");
    164  1.22    lukem 			}
    165  1.22    lukem 			break;
    166  1.20     fvdl #endif
    167   1.1      cgd 
    168  1.22    lukem 		case 'o':
    169  1.22    lukem 			if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
    170  1.22    lukem 				optim = FS_OPTSPACE;
    171  1.22    lukem 			else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
    172  1.22    lukem 				optim = FS_OPTTIME;
    173  1.22    lukem 			else
    174  1.22    lukem 				errx(10,
    175  1.22    lukem 				    "bad %s (options are `space' or `time')",
    176  1.22    lukem 				    "optimization preference");
    177  1.22    lukem 			break;
    178  1.22    lukem 
    179  1.23    lukem 		case 'k':
    180  1.23    lukem 		case 't':	/* for compatibility with old syntax */
    181  1.22    lukem 			trackskew = getnum(optarg,
    182  1.22    lukem 			    "track skew in sectors", 0, INT_MAX);
    183  1.22    lukem 			break;
    184  1.22    lukem 
    185  1.22    lukem 		default:
    186  1.22    lukem 			usage();
    187  1.22    lukem 		}
    188  1.22    lukem 	}
    189  1.22    lukem 	argc -= optind;
    190  1.22    lukem 	argv += optind;
    191  1.22    lukem 	if (argc != 1)
    192   1.6      cgd 		usage();
    193  1.22    lukem 
    194  1.22    lukem 	special = argv[0];
    195   1.1      cgd 	fs = getfsfile(special);
    196   1.1      cgd 	if (fs)
    197   1.1      cgd 		special = fs->fs_spec;
    198  1.22    lukem  again:
    199   1.1      cgd 	if (stat(special, &st) < 0) {
    200  1.22    lukem 		if (!Fflag && *special != '/') {
    201   1.1      cgd 			if (*special == 'r')
    202   1.1      cgd 				special++;
    203  1.16  mycroft 			(void)snprintf(device, sizeof(device), "%s/%s",
    204  1.16  mycroft 			    _PATH_DEV, special);
    205   1.1      cgd 			special = device;
    206   1.1      cgd 			goto again;
    207   1.1      cgd 		}
    208   1.6      cgd 		err(1, "%s", special);
    209   1.1      cgd 	}
    210  1.22    lukem 	if (Fflag) {
    211  1.22    lukem 		if (!S_ISREG(st.st_mode))
    212  1.22    lukem 			errx(10, "%s: not a regular file", special);
    213  1.22    lukem 	} else if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    214   1.8  mycroft 		errx(10, "%s: not a block or character device", special);
    215   1.1      cgd 	getsb(&sblock, special);
    216  1.22    lukem 
    217  1.23    lukem #define CHANGEVAL(old, new, type, suffix) do				\
    218  1.23    lukem 	if ((new) != -1) {						\
    219  1.23    lukem 		if ((new) == (old))					\
    220  1.23    lukem 			warnx("%s remains unchanged at %d%s",		\
    221  1.23    lukem 			    (type), (old), (suffix));			\
    222  1.23    lukem 		else {							\
    223  1.23    lukem 			warnx("%s changes from %d%s to %d%s",		\
    224  1.23    lukem 			    (type), (old), (suffix), (new), (suffix));	\
    225  1.23    lukem 			(old) = (new);					\
    226  1.23    lukem 		}							\
    227  1.23    lukem 	} while (/* CONSTCOND */0)
    228  1.23    lukem 
    229  1.23    lukem 	CHANGEVAL(sblock.fs_maxcontig, maxcontig,
    230  1.23    lukem 	    "maximum contiguous block count", "");
    231  1.23    lukem 	CHANGEVAL(sblock.fs_rotdelay, rotdelay,
    232  1.23    lukem 	    "rotational delay between contiguous blocks", "ms");
    233  1.23    lukem 	CHANGEVAL(sblock.fs_maxbpg, maxbpg,
    234  1.23    lukem 	    "maximum blocks per file in a cylinder group", "");
    235  1.23    lukem 	CHANGEVAL(sblock.fs_minfree, minfree,
    236  1.23    lukem 	    "minimum percentage of free space", "%");
    237  1.22    lukem 	if (minfree != -1) {
    238  1.22    lukem 		if (minfree >= MINFREE &&
    239  1.22    lukem 		    sblock.fs_optim == FS_OPTSPACE)
    240  1.22    lukem 			warnx(OPTWARN, "time", ">=", MINFREE);
    241  1.22    lukem 		if (minfree < MINFREE &&
    242  1.22    lukem 		    sblock.fs_optim == FS_OPTTIME)
    243  1.22    lukem 			warnx(OPTWARN, "space", "<", MINFREE);
    244  1.22    lukem 	}
    245  1.20     fvdl #ifdef TUNEFS_SOFTDEP
    246  1.22    lukem 	if (softdep == 1) {
    247  1.22    lukem 		sblock.fs_flags |= FS_DOSOFTDEP;
    248  1.22    lukem 		warnx("soft dependencies set");
    249  1.22    lukem 	} else if (softdep == 0) {
    250  1.22    lukem 		sblock.fs_flags &= ~FS_DOSOFTDEP;
    251  1.22    lukem 		warnx("soft dependencies cleared");
    252  1.22    lukem 	}
    253  1.20     fvdl #endif
    254  1.22    lukem 	if (optim != -1) {
    255  1.22    lukem 		if (sblock.fs_optim == optim) {
    256  1.22    lukem 			warnx("%s remains unchanged as %s",
    257  1.22    lukem 			    "optimization preference",
    258  1.22    lukem 			    chg[optim]);
    259  1.22    lukem 		} else {
    260  1.22    lukem 			warnx("%s changes from %s to %s",
    261  1.22    lukem 			    "optimization preference",
    262  1.22    lukem 			    chg[sblock.fs_optim], chg[optim]);
    263  1.22    lukem 			sblock.fs_optim = optim;
    264  1.22    lukem 			if (sblock.fs_minfree >= MINFREE &&
    265  1.22    lukem 			    optim == FS_OPTSPACE)
    266  1.22    lukem 				warnx(OPTWARN, "time", ">=", MINFREE);
    267  1.22    lukem 			if (sblock.fs_minfree < MINFREE &&
    268  1.22    lukem 			    optim == FS_OPTTIME)
    269  1.22    lukem 				warnx(OPTWARN, "space", "<", MINFREE);
    270  1.22    lukem 		}
    271  1.22    lukem 	}
    272  1.23    lukem 	CHANGEVAL(sblock.fs_trackskew, trackskew,
    273  1.23    lukem 	    "track skew in sectors", "");
    274   1.1      cgd 
    275  1.12    lukem 	if (Nflag) {
    276  1.22    lukem 		fprintf(stdout, "tunefs: current settings of %s\n", special);
    277  1.12    lukem 		fprintf(stdout, "\tmaximum contiguous block count %d\n",
    278  1.12    lukem 		    sblock.fs_maxcontig);
    279  1.12    lukem 		fprintf(stdout,
    280  1.12    lukem 		    "\trotational delay between contiguous blocks %dms\n",
    281  1.12    lukem 		    sblock.fs_rotdelay);
    282  1.12    lukem 		fprintf(stdout,
    283  1.12    lukem 		    "\tmaximum blocks per file in a cylinder group %d\n",
    284  1.12    lukem 		    sblock.fs_maxbpg);
    285  1.12    lukem 		fprintf(stdout, "\tminimum percentage of free space %d%%\n",
    286  1.12    lukem 		    sblock.fs_minfree);
    287  1.20     fvdl #ifdef TUNEFS_SOFTDEP
    288  1.19     fvdl 		fprintf(stdout, "\tsoft dependencies: %s\n",
    289  1.19     fvdl 		    (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
    290  1.20     fvdl #endif
    291  1.12    lukem 		fprintf(stdout, "\toptimization preference: %s\n",
    292  1.12    lukem 		    chg[sblock.fs_optim]);
    293  1.12    lukem 		fprintf(stdout, "\ttrack skew %d sectors\n",
    294  1.23    lukem 		    sblock.fs_trackskew);
    295  1.12    lukem 		fprintf(stdout, "tunefs: no changes made\n");
    296  1.12    lukem 		exit(0);
    297  1.12    lukem 	}
    298  1.22    lukem 
    299  1.12    lukem 	fi = open(special, 1);
    300  1.12    lukem 	if (fi < 0)
    301  1.12    lukem 		err(3, "cannot open %s for writing", special);
    302  1.13   bouyer 	memcpy(buf, (char *)&sblock, SBSIZE);
    303  1.13   bouyer 	if (needswap)
    304  1.21    lukem 		ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
    305  1.13   bouyer 	bwrite((daddr_t)SBOFF / dev_bsize, buf, SBSIZE);
    306   1.1      cgd 	if (Aflag)
    307   1.1      cgd 		for (i = 0; i < sblock.fs_ncg; i++)
    308   1.1      cgd 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
    309  1.13   bouyer 			    buf, SBSIZE);
    310   1.1      cgd 	close(fi);
    311   1.1      cgd 	exit(0);
    312   1.6      cgd }
    313   1.6      cgd 
    314  1.22    lukem static int
    315  1.22    lukem getnum(const char *num, const char *desc, int min, int max)
    316   1.6      cgd {
    317  1.22    lukem 	long	n;
    318  1.22    lukem 	char	*ep;
    319   1.8  mycroft 
    320  1.22    lukem 	n = strtol(num, &ep, 10);
    321  1.22    lukem 	if (ep[0] != '\0')
    322  1.22    lukem 		errx(1, "Invalid number `%s' for %s", num, desc);
    323  1.22    lukem 	if ((int) n < min)
    324  1.22    lukem 		errx(1, "%s `%s' too small (minimum is %d)", desc, num, min);
    325  1.22    lukem 	if ((int) n > max)
    326  1.22    lukem 		errx(1, "%s `%s' too large (maximum is %d)", desc, num, max);
    327  1.22    lukem 	return ((int)n);
    328  1.22    lukem }
    329  1.22    lukem 
    330  1.22    lukem static void
    331  1.22    lukem usage(void)
    332  1.22    lukem {
    333  1.22    lukem 
    334  1.22    lukem 	fprintf(stderr, "Usage: tunefs [-AFN] tuneup-options special-device\n");
    335   1.1      cgd 	fprintf(stderr, "where tuneup-options are:\n");
    336  1.23    lukem 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    337  1.12    lukem 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    338   1.1      cgd 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    339  1.23    lukem 	fprintf(stderr, "\t-k track skew in sectors\n");
    340   1.1      cgd 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    341  1.20     fvdl #ifdef TUNEFS_SOFTDEP
    342  1.19     fvdl 	fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
    343  1.20     fvdl #endif
    344  1.23    lukem 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    345   1.8  mycroft 	exit(2);
    346   1.1      cgd }
    347   1.1      cgd 
    348  1.22    lukem static void
    349  1.22    lukem getsb(struct fs *fs, const char *file)
    350   1.1      cgd {
    351   1.1      cgd 
    352  1.12    lukem 	fi = open(file, 0);
    353   1.6      cgd 	if (fi < 0)
    354  1.12    lukem 		err(3, "cannot open %s for reading", file);
    355   1.6      cgd 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
    356   1.8  mycroft 		err(4, "%s: bad super block", file);
    357  1.17     ross 	if (fs->fs_magic != FS_MAGIC) {
    358  1.13   bouyer 		if (fs->fs_magic == bswap32(FS_MAGIC)) {
    359  1.22    lukem 			warnx("%s: swapping byte order", file);
    360  1.13   bouyer 			needswap = 1;
    361  1.21    lukem 			ffs_sb_swap(fs, fs);
    362  1.13   bouyer 		} else
    363  1.13   bouyer 			err(5, "%s: bad magic number", file);
    364  1.17     ross 	}
    365   1.1      cgd 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    366  1.12    lukem 	close(fi);
    367   1.1      cgd }
    368   1.1      cgd 
    369  1.22    lukem static void
    370  1.22    lukem bwrite(daddr_t blk, char *buffer, int size)
    371   1.1      cgd {
    372   1.8  mycroft 
    373   1.8  mycroft 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
    374   1.8  mycroft 		err(6, "FS SEEK");
    375  1.22    lukem 	if (write(fi, buffer, size) != size)
    376   1.8  mycroft 		err(7, "FS WRITE");
    377   1.1      cgd }
    378   1.1      cgd 
    379  1.22    lukem static int
    380  1.22    lukem bread(daddr_t bno, char *buffer, int cnt)
    381   1.1      cgd {
    382   1.8  mycroft 	int i;
    383   1.1      cgd 
    384   1.8  mycroft 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
    385   1.1      cgd 		return(1);
    386  1.22    lukem 	if ((i = read(fi, buffer, cnt)) != cnt) {
    387   1.1      cgd 		for(i=0; i<sblock.fs_bsize; i++)
    388   1.1      cgd 			buf[i] = 0;
    389   1.1      cgd 		return (1);
    390   1.1      cgd 	}
    391   1.1      cgd 	return (0);
    392   1.1      cgd }
    393