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