Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.20
      1  1.20     fvdl /*	$NetBSD: tunefs.c,v 1.20 2000/06/15 22:37:17 fvdl 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.20     fvdl __RCSID("$NetBSD: tunefs.c,v 1.20 2000/06/15 22:37:17 fvdl 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.8  mycroft #include <errno.h>
     63   1.6      cgd #include <err.h>
     64   1.6      cgd #include <fcntl.h>
     65   1.1      cgd #include <fstab.h>
     66   1.1      cgd #include <stdio.h>
     67   1.1      cgd #include <paths.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.1      cgd int fi;
     83   1.1      cgd long dev_bsize = 1;
     84  1.13   bouyer int needswap = 0;
     85   1.1      cgd 
     86  1.11    lukem void bwrite __P((daddr_t, char *, int));
     87  1.11    lukem int bread __P((daddr_t, char *, int));
     88  1.15  mycroft void getsb __P((struct fs *, const char *));
     89  1.11    lukem int main __P((int, char *[]));
     90   1.6      cgd void usage __P((void));
     91   1.6      cgd 
     92   1.6      cgd int
     93   1.1      cgd main(argc, argv)
     94   1.1      cgd 	int argc;
     95   1.1      cgd 	char *argv[];
     96   1.1      cgd {
     97  1.20     fvdl 	char *cp, *name;
     98  1.20     fvdl #ifdef TUNEFS_SOFTDEP
     99  1.20     fvdl 	char *action;
    100  1.20     fvdl #endif
    101  1.15  mycroft 	const char *special;
    102   1.1      cgd 	struct stat st;
    103   1.1      cgd 	int i;
    104  1.12    lukem 	int Aflag = 0, Nflag = 0;
    105   1.1      cgd 	struct fstab *fs;
    106   1.1      cgd 	char *chg[2], device[MAXPATHLEN];
    107   1.1      cgd 
    108   1.1      cgd 	argc--, argv++;
    109   1.1      cgd 	if (argc < 2)
    110   1.6      cgd 		usage();
    111   1.1      cgd 	special = argv[argc - 1];
    112   1.1      cgd 	fs = getfsfile(special);
    113   1.1      cgd 	if (fs)
    114   1.1      cgd 		special = fs->fs_spec;
    115   1.1      cgd again:
    116   1.1      cgd 	if (stat(special, &st) < 0) {
    117   1.1      cgd 		if (*special != '/') {
    118   1.1      cgd 			if (*special == 'r')
    119   1.1      cgd 				special++;
    120  1.16  mycroft 			(void)snprintf(device, sizeof(device), "%s/%s",
    121  1.16  mycroft 			    _PATH_DEV, special);
    122   1.1      cgd 			special = device;
    123   1.1      cgd 			goto again;
    124   1.1      cgd 		}
    125   1.6      cgd 		err(1, "%s", special);
    126   1.1      cgd 	}
    127   1.9  mycroft 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    128   1.8  mycroft 		errx(10, "%s: not a block or character device", special);
    129   1.1      cgd 	getsb(&sblock, special);
    130  1.12    lukem 	chg[FS_OPTSPACE] = "space";
    131  1.12    lukem 	chg[FS_OPTTIME] = "time";
    132   1.1      cgd 	for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
    133   1.1      cgd 		for (cp = &argv[0][1]; *cp; cp++)
    134   1.1      cgd 			switch (*cp) {
    135   1.1      cgd 
    136   1.1      cgd 			case 'A':
    137   1.1      cgd 				Aflag++;
    138   1.1      cgd 				continue;
    139   1.1      cgd 
    140  1.12    lukem 			case 'N':
    141  1.12    lukem 				Nflag++;
    142  1.12    lukem 				continue;
    143  1.12    lukem 
    144   1.1      cgd 			case 'a':
    145   1.1      cgd 				name = "maximum contiguous block count";
    146   1.1      cgd 				if (argc < 1)
    147   1.8  mycroft 					errx(10, "-a: missing %s", name);
    148   1.1      cgd 				argc--, argv++;
    149   1.1      cgd 				i = atoi(*argv);
    150   1.1      cgd 				if (i < 1)
    151   1.8  mycroft 					errx(10, "%s must be >= 1 (was %s)",
    152   1.6      cgd 					    name, *argv);
    153   1.6      cgd 				warnx("%s changes from %d to %d",
    154   1.6      cgd 				    name, sblock.fs_maxcontig, i);
    155   1.1      cgd 				sblock.fs_maxcontig = i;
    156   1.1      cgd 				continue;
    157   1.1      cgd 
    158   1.1      cgd 			case 'd':
    159   1.1      cgd 				name =
    160   1.1      cgd 				   "rotational delay between contiguous blocks";
    161   1.1      cgd 				if (argc < 1)
    162   1.8  mycroft 					errx(10, "-d: missing %s", name);
    163   1.1      cgd 				argc--, argv++;
    164   1.1      cgd 				i = atoi(*argv);
    165   1.6      cgd 				warnx("%s changes from %dms to %dms",
    166   1.6      cgd 				    name, sblock.fs_rotdelay, i);
    167   1.1      cgd 				sblock.fs_rotdelay = i;
    168   1.1      cgd 				continue;
    169   1.1      cgd 
    170   1.1      cgd 			case 'e':
    171   1.1      cgd 				name =
    172   1.1      cgd 				  "maximum blocks per file in a cylinder group";
    173   1.1      cgd 				if (argc < 1)
    174   1.8  mycroft 					errx(10, "-e: missing %s", name);
    175   1.1      cgd 				argc--, argv++;
    176   1.1      cgd 				i = atoi(*argv);
    177   1.1      cgd 				if (i < 1)
    178   1.8  mycroft 					errx(10, "%s must be >= 1 (was %s)",
    179   1.6      cgd 					    name, *argv);
    180   1.6      cgd 				warnx("%s changes from %d to %d",
    181   1.6      cgd 				    name, sblock.fs_maxbpg, i);
    182   1.1      cgd 				sblock.fs_maxbpg = i;
    183   1.1      cgd 				continue;
    184   1.1      cgd 
    185   1.1      cgd 			case 'm':
    186   1.1      cgd 				name = "minimum percentage of free space";
    187   1.1      cgd 				if (argc < 1)
    188   1.8  mycroft 					errx(10, "-m: missing %s", name);
    189   1.1      cgd 				argc--, argv++;
    190   1.1      cgd 				i = atoi(*argv);
    191   1.1      cgd 				if (i < 0 || i > 99)
    192   1.8  mycroft 					errx(10, "bad %s (%s)", name, *argv);
    193   1.6      cgd 				warnx("%s changes from %d%% to %d%%",
    194   1.6      cgd 				    name, sblock.fs_minfree, i);
    195   1.1      cgd 				sblock.fs_minfree = i;
    196   1.6      cgd 				if (i >= MINFREE &&
    197   1.6      cgd 				    sblock.fs_optim == FS_OPTSPACE)
    198   1.6      cgd 					warnx(OPTWARN, "time", ">=", MINFREE);
    199   1.6      cgd 				if (i < MINFREE &&
    200   1.6      cgd 				    sblock.fs_optim == FS_OPTTIME)
    201   1.6      cgd 					warnx(OPTWARN, "space", "<", MINFREE);
    202   1.1      cgd 				continue;
    203  1.20     fvdl #ifdef TUNEFS_SOFTDEP
    204  1.19     fvdl 			case 'n':
    205  1.19     fvdl 				name = "soft dependencies";
    206  1.19     fvdl 				if (argc < 1)
    207  1.19     fvdl 					errx(10, "-n: missing %s", name);
    208  1.19     fvdl 				argc--, argv++;
    209  1.19     fvdl 				if (strcmp(*argv, "enable") == 0) {
    210  1.19     fvdl 					sblock.fs_flags |= FS_DOSOFTDEP;
    211  1.19     fvdl 					action = "set";
    212  1.19     fvdl 				} else if (strcmp(*argv, "disable") == 0) {
    213  1.19     fvdl 					sblock.fs_flags &= ~FS_DOSOFTDEP;
    214  1.19     fvdl 					action = "cleared";
    215  1.19     fvdl 				} else {
    216  1.19     fvdl 					errx(10, "bad %s (options are %s)",
    217  1.19     fvdl 					    name, "`enable' or `disable'");
    218  1.19     fvdl 				}
    219  1.19     fvdl 				warnx("%s %s", name, action);
    220  1.19     fvdl 				continue;
    221  1.20     fvdl #endif
    222   1.1      cgd 
    223   1.1      cgd 			case 'o':
    224   1.1      cgd 				name = "optimization preference";
    225   1.1      cgd 				if (argc < 1)
    226   1.8  mycroft 					errx(10, "-o: missing %s", name);
    227   1.1      cgd 				argc--, argv++;
    228   1.1      cgd 				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
    229   1.1      cgd 					i = FS_OPTSPACE;
    230   1.1      cgd 				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
    231   1.1      cgd 					i = FS_OPTTIME;
    232   1.1      cgd 				else
    233   1.8  mycroft 					errx(10, "bad %s (options are `space' or `time')",
    234   1.6      cgd 					    name);
    235   1.1      cgd 				if (sblock.fs_optim == i) {
    236   1.6      cgd 					warnx("%s remains unchanged as %s",
    237   1.6      cgd 					    name, chg[i]);
    238   1.1      cgd 					continue;
    239   1.1      cgd 				}
    240   1.6      cgd 				warnx("%s changes from %s to %s",
    241   1.6      cgd 				    name, chg[sblock.fs_optim], chg[i]);
    242   1.1      cgd 				sblock.fs_optim = i;
    243   1.6      cgd 				if (sblock.fs_minfree >= MINFREE &&
    244   1.6      cgd 				    i == FS_OPTSPACE)
    245   1.6      cgd 					warnx(OPTWARN, "time", ">=", MINFREE);
    246   1.6      cgd 				if (sblock.fs_minfree < MINFREE &&
    247   1.6      cgd 				    i == FS_OPTTIME)
    248   1.6      cgd 					warnx(OPTWARN, "space", "<", MINFREE);
    249   1.1      cgd 				continue;
    250   1.1      cgd 
    251  1.12    lukem 			case 't':
    252  1.12    lukem 				name = "track skew in sectors";
    253  1.12    lukem 				if (argc < 1)
    254  1.12    lukem 					errx(10, "-t: missing %s", name);
    255  1.12    lukem 				argc--, argv++;
    256  1.12    lukem 				i = atoi(*argv);
    257  1.12    lukem 				if (i < 0)
    258  1.12    lukem 					errx(10, "%s: %s must be >= 0",
    259  1.12    lukem 						*argv, name);
    260  1.12    lukem 				warnx("%s changes from %d to %d",
    261  1.12    lukem 					name, sblock.fs_trackskew, i);
    262  1.12    lukem 				sblock.fs_trackskew = i;
    263  1.12    lukem 				continue;
    264  1.12    lukem 
    265   1.1      cgd 			default:
    266   1.6      cgd 				usage();
    267   1.1      cgd 			}
    268   1.1      cgd 	}
    269   1.1      cgd 	if (argc != 1)
    270   1.6      cgd 		usage();
    271  1.12    lukem 	if (Nflag) {
    272  1.12    lukem 		fprintf(stdout, "tunefs: current settings\n");
    273  1.12    lukem 		fprintf(stdout, "\tmaximum contiguous block count %d\n",
    274  1.12    lukem 		    sblock.fs_maxcontig);
    275  1.12    lukem 		fprintf(stdout,
    276  1.12    lukem 		    "\trotational delay between contiguous blocks %dms\n",
    277  1.12    lukem 		    sblock.fs_rotdelay);
    278  1.12    lukem 		fprintf(stdout,
    279  1.12    lukem 		    "\tmaximum blocks per file in a cylinder group %d\n",
    280  1.12    lukem 		    sblock.fs_maxbpg);
    281  1.12    lukem 		fprintf(stdout, "\tminimum percentage of free space %d%%\n",
    282  1.12    lukem 		    sblock.fs_minfree);
    283  1.20     fvdl #ifdef TUNEFS_SOFTDEP
    284  1.19     fvdl 		fprintf(stdout, "\tsoft dependencies: %s\n",
    285  1.19     fvdl 		    (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
    286  1.20     fvdl #endif
    287  1.12    lukem 		fprintf(stdout, "\toptimization preference: %s\n",
    288  1.12    lukem 		    chg[sblock.fs_optim]);
    289  1.12    lukem 		fprintf(stdout, "\ttrack skew %d sectors\n",
    290  1.12    lukem 			sblock.fs_trackskew);
    291  1.12    lukem 		fprintf(stdout, "tunefs: no changes made\n");
    292  1.12    lukem 		exit(0);
    293  1.12    lukem 	}
    294  1.12    lukem 	fi = open(special, 1);
    295  1.12    lukem 	if (fi < 0)
    296  1.12    lukem 		err(3, "cannot open %s for writing", special);
    297  1.13   bouyer 	memcpy(buf, (char *)&sblock, SBSIZE);
    298  1.13   bouyer 	if (needswap)
    299  1.13   bouyer 		ffs_sb_swap((struct fs*)buf, (struct fs*)buf, 1);
    300  1.13   bouyer 	bwrite((daddr_t)SBOFF / dev_bsize, buf, SBSIZE);
    301   1.1      cgd 	if (Aflag)
    302   1.1      cgd 		for (i = 0; i < sblock.fs_ncg; i++)
    303   1.1      cgd 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
    304  1.13   bouyer 			    buf, SBSIZE);
    305   1.1      cgd 	close(fi);
    306   1.1      cgd 	exit(0);
    307   1.6      cgd }
    308   1.6      cgd 
    309   1.6      cgd void
    310   1.6      cgd usage()
    311   1.6      cgd {
    312   1.8  mycroft 
    313  1.12    lukem 	fprintf(stderr, "Usage: tunefs [-AN] tuneup-options special-device\n");
    314   1.1      cgd 	fprintf(stderr, "where tuneup-options are:\n");
    315  1.12    lukem 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    316   1.1      cgd 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    317   1.1      cgd 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    318   1.1      cgd 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    319   1.1      cgd 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    320  1.20     fvdl #ifdef TUNEFS_SOFTDEP
    321  1.19     fvdl 	fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
    322  1.20     fvdl #endif
    323  1.12    lukem 	fprintf(stderr, "\t-t track skew in sectors\n");
    324   1.8  mycroft 	exit(2);
    325   1.1      cgd }
    326   1.1      cgd 
    327   1.6      cgd void
    328   1.1      cgd getsb(fs, file)
    329  1.11    lukem 	struct fs *fs;
    330  1.15  mycroft 	const char *file;
    331   1.1      cgd {
    332   1.1      cgd 
    333  1.12    lukem 	fi = open(file, 0);
    334   1.6      cgd 	if (fi < 0)
    335  1.12    lukem 		err(3, "cannot open %s for reading", file);
    336   1.6      cgd 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
    337   1.8  mycroft 		err(4, "%s: bad super block", file);
    338  1.17     ross 	if (fs->fs_magic != FS_MAGIC) {
    339  1.13   bouyer 		if (fs->fs_magic == bswap32(FS_MAGIC)) {
    340  1.13   bouyer 			needswap = 1;
    341  1.13   bouyer 			ffs_sb_swap(fs, fs, 0);
    342  1.13   bouyer 		} else
    343  1.13   bouyer 			err(5, "%s: bad magic number", file);
    344  1.17     ross 	}
    345   1.1      cgd 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    346  1.12    lukem 	close(fi);
    347   1.1      cgd }
    348   1.1      cgd 
    349   1.6      cgd void
    350   1.8  mycroft bwrite(blk, buf, size)
    351   1.8  mycroft 	daddr_t blk;
    352   1.1      cgd 	char *buf;
    353   1.6      cgd 	int size;
    354   1.1      cgd {
    355   1.8  mycroft 
    356   1.8  mycroft 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
    357   1.8  mycroft 		err(6, "FS SEEK");
    358   1.6      cgd 	if (write(fi, buf, size) != size)
    359   1.8  mycroft 		err(7, "FS WRITE");
    360   1.1      cgd }
    361   1.1      cgd 
    362   1.6      cgd int
    363   1.1      cgd bread(bno, buf, cnt)
    364   1.1      cgd 	daddr_t bno;
    365   1.1      cgd 	char *buf;
    366   1.6      cgd 	int cnt;
    367   1.1      cgd {
    368   1.8  mycroft 	int i;
    369   1.1      cgd 
    370   1.8  mycroft 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
    371   1.1      cgd 		return(1);
    372   1.1      cgd 	if ((i = read(fi, buf, cnt)) != cnt) {
    373   1.1      cgd 		for(i=0; i<sblock.fs_bsize; i++)
    374   1.1      cgd 			buf[i] = 0;
    375   1.1      cgd 		return (1);
    376   1.1      cgd 	}
    377   1.1      cgd 	return (0);
    378   1.1      cgd }
    379