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