Home | History | Annotate | Line # | Download | only in tunefs
tunefs.c revision 1.6
      1 /*
      2  * Copyright (c) 1983 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)tunefs.c	5.11 (Berkeley) 6/1/90";*/
     42 static char rcsid[] = "$Id: tunefs.c,v 1.6 1994/04/12 05:03:24 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * tunefs: change layout parameters to an existing file system.
     47  */
     48 #include <sys/param.h>
     49 #include <sys/stat.h>
     50 #include <ufs/fs.h>
     51 #include <err.h>
     52 #include <fcntl.h>
     53 #include <fstab.h>
     54 #include <stdio.h>
     55 #include <paths.h>
     56 #include <stdlib.h>
     57 #include <unistd.h>
     58 
     59 /*
     60  * MINFREE gives the minimum acceptable percentage of file system
     61  * blocks which may be free. If the freelist drops below this level
     62  * only the superuser may continue to allocate blocks. This may
     63  * be set to 0 if no reserve of free blocks is deemed necessary,
     64  * however throughput drops by fifty percent if the file system
     65  * is run at between 95% and 100% full; thus the default value of
     66  * fs_minfree is 5%. With 5% free space, fragmentation is not a
     67  * problem, so we choose to optimize for time.
     68  *
     69  * XXX should be in some header common with newfs; snarfed from there.
     70  */
     71 #define	MINFREE		5
     72 
     73 /* the optimization warning string template */
     74 #define	OPTWARN		"should optimize for %s with minfree %s %d%%"
     75 
     76 union {
     77 	struct	fs sb;
     78 	char pad[MAXBSIZE];
     79 } sbun;
     80 #define	sblock sbun.sb
     81 
     82 int fi;
     83 long dev_bsize = 1;
     84 
     85 void bwrite(daddr_t, char *, int);
     86 int bread(daddr_t, char *, int);
     87 void getsb(struct fs *, char *);
     88 void usage __P((void));
     89 
     90 int
     91 main(argc, argv)
     92 	int argc;
     93 	char *argv[];
     94 {
     95 	char *cp, *special, *name;
     96 	struct stat st;
     97 	int i;
     98 	int Aflag = 0;
     99 	struct fstab *fs;
    100 	char *chg[2], device[MAXPATHLEN];
    101 
    102 	argc--, argv++;
    103 	if (argc < 2)
    104 		usage();
    105 	special = argv[argc - 1];
    106 	fs = getfsfile(special);
    107 	if (fs)
    108 		special = fs->fs_spec;
    109 again:
    110 	if (stat(special, &st) < 0) {
    111 		if (*special != '/') {
    112 			if (*special == 'r')
    113 				special++;
    114 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
    115 			special = device;
    116 			goto again;
    117 		}
    118 		err(1, "%s", special);
    119 	}
    120 	if ((st.st_mode & S_IFMT) != S_IFBLK &&
    121 	    (st.st_mode & S_IFMT) != S_IFCHR)
    122 		errx(1, "%s: not a block or character device", special);
    123 	getsb(&sblock, special);
    124 	for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
    125 		for (cp = &argv[0][1]; *cp; cp++)
    126 			switch (*cp) {
    127 
    128 			case 'A':
    129 				Aflag++;
    130 				continue;
    131 
    132 			case 'a':
    133 				name = "maximum contiguous block count";
    134 				if (argc < 1)
    135 					errx(1, "-a: missing %s", name);
    136 				argc--, argv++;
    137 				i = atoi(*argv);
    138 				if (i < 1)
    139 					errx(1, "%s must be >= 1 (was %s)",
    140 					    name, *argv);
    141 				warnx("%s changes from %d to %d",
    142 				    name, sblock.fs_maxcontig, i);
    143 				sblock.fs_maxcontig = i;
    144 				continue;
    145 
    146 			case 'd':
    147 				name =
    148 				   "rotational delay between contiguous blocks";
    149 				if (argc < 1)
    150 					errx(1, "-d: missing %s", name);
    151 				argc--, argv++;
    152 				i = atoi(*argv);
    153 				warnx("%s changes from %dms to %dms",
    154 				    name, sblock.fs_rotdelay, i);
    155 				sblock.fs_rotdelay = i;
    156 				continue;
    157 
    158 			case 'e':
    159 				name =
    160 				  "maximum blocks per file in a cylinder group";
    161 				if (argc < 1)
    162 					errx(1, "-e: missing %s", name);
    163 				argc--, argv++;
    164 				i = atoi(*argv);
    165 				if (i < 1)
    166 					errx(1, "%s must be >= 1 (was %s)",
    167 					    name, *argv);
    168 				warnx("%s changes from %d to %d",
    169 				    name, sblock.fs_maxbpg, i);
    170 				sblock.fs_maxbpg = i;
    171 				continue;
    172 
    173 			case 'm':
    174 				name = "minimum percentage of free space";
    175 				if (argc < 1)
    176 					errx(1, "-m: missing %s", name);
    177 				argc--, argv++;
    178 				i = atoi(*argv);
    179 				if (i < 0 || i > 99)
    180 					errx(1, "bad %s (%s)", name, *argv);
    181 				warnx("%s changes from %d%% to %d%%",
    182 				    name, sblock.fs_minfree, i);
    183 				sblock.fs_minfree = i;
    184 				if (i >= MINFREE &&
    185 				    sblock.fs_optim == FS_OPTSPACE)
    186 					warnx(OPTWARN, "time", ">=", MINFREE);
    187 				if (i < MINFREE &&
    188 				    sblock.fs_optim == FS_OPTTIME)
    189 					warnx(OPTWARN, "space", "<", MINFREE);
    190 				continue;
    191 
    192 			case 'o':
    193 				name = "optimization preference";
    194 				if (argc < 1)
    195 					errx(1, "-o: missing %s", name);
    196 				argc--, argv++;
    197 				chg[FS_OPTSPACE] = "space";
    198 				chg[FS_OPTTIME] = "time";
    199 				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
    200 					i = FS_OPTSPACE;
    201 				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
    202 					i = FS_OPTTIME;
    203 				else
    204 					errx(1, "bad %s (options are `space' or `time')",
    205 					    name);
    206 				if (sblock.fs_optim == i) {
    207 					warnx("%s remains unchanged as %s",
    208 					    name, chg[i]);
    209 					continue;
    210 				}
    211 				warnx("%s changes from %s to %s",
    212 				    name, chg[sblock.fs_optim], chg[i]);
    213 				sblock.fs_optim = i;
    214 				if (sblock.fs_minfree >= MINFREE &&
    215 				    i == FS_OPTSPACE)
    216 					warnx(OPTWARN, "time", ">=", MINFREE);
    217 				if (sblock.fs_minfree < MINFREE &&
    218 				    i == FS_OPTTIME)
    219 					warnx(OPTWARN, "space", "<", MINFREE);
    220 				continue;
    221 
    222 			default:
    223 				usage();
    224 			}
    225 	}
    226 	if (argc != 1)
    227 		usage();
    228 	bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
    229 	if (Aflag)
    230 		for (i = 0; i < sblock.fs_ncg; i++)
    231 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
    232 			    (char *)&sblock, SBSIZE);
    233 	close(fi);
    234 	exit(0);
    235 }
    236 
    237 void
    238 usage()
    239 {
    240 	fprintf(stderr, "usage: tunefs tuneup-options special-device\n");
    241 	fprintf(stderr, "where tuneup-options are:\n");
    242 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
    243 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
    244 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
    245 	fprintf(stderr, "\t-m minimum percentage of free space\n");
    246 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
    247 	exit(1);
    248 }
    249 
    250 void
    251 getsb(fs, file)
    252 	register struct fs *fs;
    253 	char *file;
    254 {
    255 
    256 	fi = open(file, 2);
    257 	if (fi < 0)
    258 		err(1, "cannot open %s", file);
    259 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
    260 		err(1, "%s: bad super block", file);
    261 	if (fs->fs_magic != FS_MAGIC)
    262 		err(1, "%s: bad magic number", file);
    263 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
    264 }
    265 
    266 void
    267 bwrite(bno, buf, size)
    268 	daddr_t bno;
    269 	char *buf;
    270 	int size;
    271 {
    272 	if (lseek(fi, bno * dev_bsize, 0) < 0)
    273 		err(1, "FS SEEK");
    274 	if (write(fi, buf, size) != size)
    275 		err(1, "FS WRITE");
    276 }
    277 
    278 int
    279 bread(bno, buf, cnt)
    280 	daddr_t bno;
    281 	char *buf;
    282 	int cnt;
    283 {
    284 	register i;
    285 
    286 	if (lseek(fi, bno * dev_bsize, 0) < 0)
    287 		return(1);
    288 	if ((i = read(fi, buf, cnt)) != cnt) {
    289 		for(i=0; i<sblock.fs_bsize; i++)
    290 			buf[i] = 0;
    291 		return (1);
    292 	}
    293 	return (0);
    294 }
    295