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