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