Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.11
      1 /*	$NetBSD: tunefs.c,v 1.11 1997/09/15 11:27:13 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.2 (Berkeley) 4/19/94";
     45 #else
     46 __RCSID("$NetBSD: tunefs.c,v 1.11 1997/09/15 11:27:13 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/ffs/fs.h>
     57 
     58 #include <errno.h>
     59 #include <err.h>
     60 #include <fcntl.h>
     61 #include <fstab.h>
     62 #include <stdio.h>
     63 #include <paths.h>
     64 #include <stdlib.h>
     65 #include <unistd.h>
     66 
     67 /* the optimization warning string template */
     68 #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
     69 
     70 union {
     71 	struct	fs sb;
     72 	char pad[MAXBSIZE];
     73 } sbun;
     74 #define	sblock sbun.sb
     75 
     76 int fi;
     77 long dev_bsize = 1;
     78 
     79 void bwrite __P((daddr_t, char *, int));
     80 int bread __P((daddr_t, char *, int));
     81 void getsb __P((struct fs *, char *));
     82 int main __P((int, char *[]));
     83 void usage __P((void));
     84 
     85 int
     86 main(argc, argv)
     87 	int argc;
     88 	char *argv[];
     89 {
     90 	char *cp, *special, *name;
     91 	struct stat st;
     92 	int i;
     93 	int Aflag = 0;
     94 	struct fstab *fs;
     95 	char *chg[2], device[MAXPATHLEN];
     96 
     97 	argc--, argv++;
     98 	if (argc < 2)
     99 		usage();
    100 	special = argv[argc - 1];
    101 	fs = getfsfile(special);
    102 	if (fs)
    103 		special = fs->fs_spec;
    104 again:
    105 	if (stat(special, &st) < 0) {
    106 		if (*special != '/') {
    107 			if (*special == 'r')
    108 				special++;
    109 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
    110 			special = device;
    111 			goto again;
    112 		}
    113 		err(1, "%s", special);
    114 	}
    115 	if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
    116 		errx(10, "%s: not a block or character device", special);
    117 	getsb(&sblock, special);
    118 	for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
    119 		for (cp = &argv[0][1]; *cp; cp++)
    120 			switch (*cp) {
    121 
    122 			case 'A':
    123 				Aflag++;
    124 				continue;
    125 
    126 			case 'a':
    127 				name = "maximum contiguous block count";
    128 				if (argc < 1)
    129 					errx(10, "-a: missing %s", name);
    130 				argc--, argv++;
    131 				i = atoi(*argv);
    132 				if (i < 1)
    133 					errx(10, "%s must be >= 1 (was %s)",
    134 					    name, *argv);
    135 				warnx("%s changes from %d to %d",
    136 				    name, sblock.fs_maxcontig, i);
    137 				sblock.fs_maxcontig = i;
    138 				continue;
    139 
    140 			case 'd':
    141 				name =
    142 				   "rotational delay between contiguous blocks";
    143 				if (argc < 1)
    144 					errx(10, "-d: missing %s", name);
    145 				argc--, argv++;
    146 				i = atoi(*argv);
    147 				warnx("%s changes from %dms to %dms",
    148 				    name, sblock.fs_rotdelay, i);
    149 				sblock.fs_rotdelay = i;
    150 				continue;
    151 
    152 			case 'e':
    153 				name =
    154 				  "maximum blocks per file in a cylinder group";
    155 				if (argc < 1)
    156 					errx(10, "-e: missing %s", name);
    157 				argc--, argv++;
    158 				i = atoi(*argv);
    159 				if (i < 1)
    160 					errx(10, "%s must be >= 1 (was %s)",
    161 					    name, *argv);
    162 				warnx("%s changes from %d to %d",
    163 				    name, sblock.fs_maxbpg, i);
    164 				sblock.fs_maxbpg = i;
    165 				continue;
    166 
    167 			case 'm':
    168 				name = "minimum percentage of free space";
    169 				if (argc < 1)
    170 					errx(10, "-m: missing %s", name);
    171 				argc--, argv++;
    172 				i = atoi(*argv);
    173 				if (i < 0 || i > 99)
    174 					errx(10, "bad %s (%s)", name, *argv);
    175 				warnx("%s changes from %d%% to %d%%",
    176 				    name, sblock.fs_minfree, i);
    177 				sblock.fs_minfree = i;
    178 				if (i >= MINFREE &&
    179 				    sblock.fs_optim == FS_OPTSPACE)
    180 					warnx(OPTWARN, "time", ">=", MINFREE);
    181 				if (i < MINFREE &&
    182 				    sblock.fs_optim == FS_OPTTIME)
    183 					warnx(OPTWARN, "space", "<", MINFREE);
    184 				continue;
    185 
    186 			case 'o':
    187 				name = "optimization preference";
    188 				if (argc < 1)
    189 					errx(10, "-o: missing %s", name);
    190 				argc--, argv++;
    191 				chg[FS_OPTSPACE] = "space";
    192 				chg[FS_OPTTIME] = "time";
    193 				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
    194 					i = FS_OPTSPACE;
    195 				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
    196 					i = FS_OPTTIME;
    197 				else
    198 					errx(10, "bad %s (options are `space' or `time')",
    199 					    name);
    200 				if (sblock.fs_optim == i) {
    201 					warnx("%s remains unchanged as %s",
    202 					    name, chg[i]);
    203 					continue;
    204 				}
    205 				warnx("%s changes from %s to %s",
    206 				    name, chg[sblock.fs_optim], chg[i]);
    207 				sblock.fs_optim = i;
    208 				if (sblock.fs_minfree >= MINFREE &&
    209 				    i == FS_OPTSPACE)
    210 					warnx(OPTWARN, "time", ">=", MINFREE);
    211 				if (sblock.fs_minfree < MINFREE &&
    212 				    i == FS_OPTTIME)
    213 					warnx(OPTWARN, "space", "<", MINFREE);
    214 				continue;
    215 
    216 			default:
    217 				usage();
    218 			}
    219 	}
    220 	if (argc != 1)
    221 		usage();
    222 	bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
    223 	if (Aflag)
    224 		for (i = 0; i < sblock.fs_ncg; i++)
    225 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
    226 			    (char *)&sblock, SBSIZE);
    227 	close(fi);
    228 	exit(0);
    229 }
    230 
    231 void
    232 usage()
    233 {
    234 
    235 	fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
    236 	fprintf(stderr, "where tuneup-options are:\n");
    237 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    238 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    239 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    240 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    241 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    242 	exit(2);
    243 }
    244 
    245 void
    246 getsb(fs, file)
    247 	struct fs *fs;
    248 	char *file;
    249 {
    250 
    251 	fi = open(file, 2);
    252 	if (fi < 0)
    253 		err(3, "cannot open %s", file);
    254 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
    255 		err(4, "%s: bad super block", file);
    256 	if (fs->fs_magic != FS_MAGIC)
    257 		err(5, "%s: bad magic number", file);
    258 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    259 }
    260 
    261 void
    262 bwrite(blk, buf, size)
    263 	daddr_t blk;
    264 	char *buf;
    265 	int size;
    266 {
    267 
    268 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
    269 		err(6, "FS SEEK");
    270 	if (write(fi, buf, size) != size)
    271 		err(7, "FS WRITE");
    272 }
    273 
    274 int
    275 bread(bno, buf, cnt)
    276 	daddr_t bno;
    277 	char *buf;
    278 	int cnt;
    279 {
    280 	int i;
    281 
    282 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
    283 		return(1);
    284 	if ((i = read(fi, buf, cnt)) != cnt) {
    285 		for(i=0; i<sblock.fs_bsize; i++)
    286 			buf[i] = 0;
    287 		return (1);
    288 	}
    289 	return (0);
    290 }
    291