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