tunefs.c revision 34266
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
35static 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
41static char sccsid[] = "@(#)tunefs.c	8.2 (Berkeley) 4/19/94";
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
50#include <ufs/ffs/fs.h>
51
52#include <errno.h>
53#include <err.h>
54#include <fcntl.h>
55#include <fstab.h>
56#include <stdio.h>
57#include <paths.h>
58#include <stdlib.h>
59#include <unistd.h>
60
61/* the optimization warning string template */
62#define	OPTWARN	"should optimize for %s with minfree %s %d%%"
63
64union {
65	struct	fs sb;
66	char pad[MAXBSIZE];
67} sbun;
68#define	sblock sbun.sb
69
70int fi;
71long dev_bsize = 1;
72
73void bwrite(daddr_t, char *, int);
74int bread(daddr_t, char *, int);
75void getsb(struct fs *, char *);
76void usage __P((void));
77void printfs __P((void));
78
79int
80main(argc, argv)
81	int argc;
82	char *argv[];
83{
84	char *cp, *special, *name, *action;
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;
98again:
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 'p':
122				printfs();
123				exit(0);
124
125			case 'a':
126				name = "maximum contiguous block count";
127				if (argc < 1)
128					errx(10, "-a: missing %s", name);
129				argc--, argv++;
130				i = atoi(*argv);
131				if (i < 1)
132					errx(10, "%s must be >= 1 (was %s)",
133					    name, *argv);
134				warnx("%s changes from %d to %d",
135				    name, sblock.fs_maxcontig, i);
136				sblock.fs_maxcontig = i;
137				continue;
138
139			case 'd':
140				name =
141				   "rotational delay between contiguous blocks";
142				if (argc < 1)
143					errx(10, "-d: missing %s", name);
144				argc--, argv++;
145				i = atoi(*argv);
146				warnx("%s changes from %dms to %dms",
147				    name, sblock.fs_rotdelay, i);
148				sblock.fs_rotdelay = i;
149				continue;
150
151			case 'e':
152				name =
153				  "maximum blocks per file in a cylinder group";
154				if (argc < 1)
155					errx(10, "-e: missing %s", name);
156				argc--, argv++;
157				i = atoi(*argv);
158				if (i < 1)
159					errx(10, "%s must be >= 1 (was %s)",
160					    name, *argv);
161				warnx("%s changes from %d to %d",
162				    name, sblock.fs_maxbpg, i);
163				sblock.fs_maxbpg = i;
164				continue;
165
166			case 'm':
167				name = "minimum percentage of free space";
168				if (argc < 1)
169					errx(10, "-m: missing %s", name);
170				argc--, argv++;
171				i = atoi(*argv);
172				if (i < 0 || i > 99)
173					errx(10, "bad %s (%s)", name, *argv);
174				warnx("%s changes from %d%% to %d%%",
175				    name, sblock.fs_minfree, i);
176				sblock.fs_minfree = i;
177				if (i >= MINFREE &&
178				    sblock.fs_optim == FS_OPTSPACE)
179					warnx(OPTWARN, "time", ">=", MINFREE);
180				if (i < MINFREE &&
181				    sblock.fs_optim == FS_OPTTIME)
182					warnx(OPTWARN, "space", "<", MINFREE);
183				continue;
184
185			case 'n':
186 				name = "soft updates";
187 				if (argc < 1)
188 					errx(10, "-s: missing %s", name);
189 				argc--, argv++;
190 				if (strcmp(*argv, "enable") == 0) {
191 					sblock.fs_flags |= FS_DOSOFTDEP;
192 					action = "set";
193 				} else if (strcmp(*argv, "disable") == 0) {
194 					sblock.fs_flags &= ~FS_DOSOFTDEP;
195 					action = "cleared";
196 				} else {
197 					errx(10, "bad %s (options are %s)",
198 					    name, "`enable' or `disable'");
199 				}
200 				warnx("%s %s", name, action);
201 				continue;
202
203			case 'o':
204				name = "optimization preference";
205				if (argc < 1)
206					errx(10, "-o: missing %s", name);
207				argc--, argv++;
208				chg[FS_OPTSPACE] = "space";
209				chg[FS_OPTTIME] = "time";
210				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
211					i = FS_OPTSPACE;
212				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
213					i = FS_OPTTIME;
214				else
215					errx(10, "bad %s (options are `space' or `time')",
216					    name);
217				if (sblock.fs_optim == i) {
218					warnx("%s remains unchanged as %s",
219					    name, chg[i]);
220					continue;
221				}
222				warnx("%s changes from %s to %s",
223				    name, chg[sblock.fs_optim], chg[i]);
224				sblock.fs_optim = i;
225				if (sblock.fs_minfree >= MINFREE &&
226				    i == FS_OPTSPACE)
227					warnx(OPTWARN, "time", ">=", MINFREE);
228				if (sblock.fs_minfree < MINFREE &&
229				    i == FS_OPTTIME)
230					warnx(OPTWARN, "space", "<", MINFREE);
231				continue;
232
233			default:
234				usage();
235			}
236	}
237	if (argc != 1)
238		usage();
239	bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
240	if (Aflag)
241		for (i = 0; i < sblock.fs_ncg; i++)
242			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
243			    (char *)&sblock, SBSIZE);
244	close(fi);
245	exit(0);
246}
247
248void
249usage()
250{
251
252	fprintf(stderr, "usage: tunefs tuneup-options special-device\n");
253	fprintf(stderr, "where tuneup-options are:\n");
254	fprintf(stderr, "\t-a maximum contiguous blocks\n");
255	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
256	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
257	fprintf(stderr, "\t-m minimum percentage of free space\n");
258	fprintf(stderr, "\t-n soft updates (`enable' or `disable')\n");
259	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
260	fprintf(stderr, "\t-p no change - just prints current tuneable settings\n");
261	exit(2);
262}
263
264void
265getsb(fs, file)
266	register struct fs *fs;
267	char *file;
268{
269
270	fi = open(file, 2);
271	if (fi < 0)
272		err(3, "cannot open %s", file);
273	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
274		err(4, "%s: bad super block", file);
275	if (fs->fs_magic != FS_MAGIC)
276		err(5, "%s: bad magic number", file);
277	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
278}
279
280void
281printfs()
282{
283	warnx("soft updates:  (-n)                                %s",
284		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
285	warnx("maximum contiguous block count: (-a)               %d",
286	      sblock.fs_maxcontig);
287	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
288	      sblock.fs_rotdelay);
289	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
290	      sblock.fs_maxbpg);
291	warnx("minimum percentage of free space: (-m)             %d%%",
292	      sblock.fs_minfree);
293	warnx("optimization preference: (-o)                      %s",
294	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
295	if (sblock.fs_minfree >= MINFREE &&
296	    sblock.fs_optim == FS_OPTSPACE)
297		warnx(OPTWARN, "time", ">=", MINFREE);
298	if (sblock.fs_minfree < MINFREE &&
299	    sblock.fs_optim == FS_OPTTIME)
300		warnx(OPTWARN, "space", "<", MINFREE);
301}
302
303void
304bwrite(blk, buf, size)
305	daddr_t blk;
306	char *buf;
307	int size;
308{
309
310	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
311		err(6, "FS SEEK");
312	if (write(fi, buf, size) != size)
313		err(7, "FS WRITE");
314}
315
316int
317bread(bno, buf, cnt)
318	daddr_t bno;
319	char *buf;
320	int cnt;
321{
322	int i;
323
324	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
325		return(1);
326	if ((i = read(fi, buf, cnt)) != cnt) {
327		for(i=0; i<sblock.fs_bsize; i++)
328			buf[i] = 0;
329		return (1);
330	}
331	return (0);
332}
333