xref: /freebsd/sbin/tunefs/tunefs.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 const 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 #if 0
42 static char sccsid[] = "@(#)tunefs.c	8.2 (Berkeley) 4/19/94";
43 #endif
44 static const char rcsid[] =
45 	"$Id: tunefs.c,v 1.6 1998/08/03 06:41:20 charnier Exp $";
46 #endif /* not lint */
47 
48 /*
49  * tunefs: change layout parameters to an existing file system.
50  */
51 #include <sys/param.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
54 
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ufs/ufsmount.h>
57 
58 #include <err.h>
59 #include <fcntl.h>
60 #include <fstab.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66 
67 /* the optimization warning string template */
68 #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
69 
70 union {
71 	struct	fs sb;
72 	char pad[MAXBSIZE];
73 } sbun;
74 #define	sblock sbun.sb
75 
76 int fi;
77 long dev_bsize = 1;
78 
79 void bwrite(daddr_t, char *, int);
80 int bread(daddr_t, char *, int);
81 void getsb(struct fs *, char *);
82 void usage __P((void));
83 void printfs __P((void));
84 char *rawname __P((char *, char *));
85 
86 int
87 main(argc, argv)
88 	int argc;
89 	char *argv[];
90 {
91 	char *cp, *special, *name, *action;
92 	struct stat st;
93 	int i;
94 	int Aflag = 0, active = 0;
95 	struct fstab *fs;
96 	char *chg[2], device[MAXPATHLEN];
97 	struct ufs_args args;
98 	struct statfs stfs;
99 
100 	argc--, argv++;
101 	if (argc < 2)
102 		usage();
103 	special = argv[argc - 1];
104 	fs = getfsfile(special);
105 	if (fs) {
106 		if (statfs(special, &stfs) == 0) {
107 		    	if ((stfs.f_flags & MNT_RDONLY) == 0) {
108 				errx(1, "cannot work on read-write mounted file system");
109 			}
110 			active = 1;
111 			special = rawname(fs->fs_spec, device);
112 		} else
113 			special = fs->fs_spec;
114 	}
115 again:
116 	if (stat(special, &st) < 0) {
117 		if (*special != '/') {
118 			if (*special == 'r')
119 				special++;
120 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
121 			special = device;
122 			goto again;
123 		}
124 		err(1, "%s", special);
125 	}
126 	if ((st.st_mode & S_IFMT) != S_IFBLK &&
127 	    (st.st_mode & S_IFMT) != S_IFCHR)
128 		errx(10, "%s: not a block or character device", special);
129 	getsb(&sblock, special);
130 	for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
131 		for (cp = &argv[0][1]; *cp; cp++)
132 			switch (*cp) {
133 
134 			case 'A':
135 				Aflag++;
136 				continue;
137 
138 			case 'p':
139 				printfs();
140 				exit(0);
141 
142 			case 'a':
143 				name = "maximum contiguous block count";
144 				if (argc < 1)
145 					errx(10, "-a: missing %s", name);
146 				argc--, argv++;
147 				i = atoi(*argv);
148 				if (i < 1)
149 					errx(10, "%s must be >= 1 (was %s)",
150 					    name, *argv);
151 				warnx("%s changes from %d to %d",
152 				    name, sblock.fs_maxcontig, i);
153 				sblock.fs_maxcontig = i;
154 				continue;
155 
156 			case 'd':
157 				name =
158 				   "rotational delay between contiguous blocks";
159 				if (argc < 1)
160 					errx(10, "-d: missing %s", name);
161 				argc--, argv++;
162 				i = atoi(*argv);
163 				warnx("%s changes from %dms to %dms",
164 				    name, sblock.fs_rotdelay, i);
165 				sblock.fs_rotdelay = i;
166 				continue;
167 
168 			case 'e':
169 				name =
170 				  "maximum blocks per file in a cylinder group";
171 				if (argc < 1)
172 					errx(10, "-e: missing %s", name);
173 				argc--, argv++;
174 				i = atoi(*argv);
175 				if (i < 1)
176 					errx(10, "%s must be >= 1 (was %s)",
177 					    name, *argv);
178 				warnx("%s changes from %d to %d",
179 				    name, sblock.fs_maxbpg, i);
180 				sblock.fs_maxbpg = i;
181 				continue;
182 
183 			case 'm':
184 				name = "minimum percentage of free space";
185 				if (argc < 1)
186 					errx(10, "-m: missing %s", name);
187 				argc--, argv++;
188 				i = atoi(*argv);
189 				if (i < 0 || i > 99)
190 					errx(10, "bad %s (%s)", name, *argv);
191 				warnx("%s changes from %d%% to %d%%",
192 				    name, sblock.fs_minfree, i);
193 				sblock.fs_minfree = i;
194 				if (i >= MINFREE &&
195 				    sblock.fs_optim == FS_OPTSPACE)
196 					warnx(OPTWARN, "time", ">=", MINFREE);
197 				if (i < MINFREE &&
198 				    sblock.fs_optim == FS_OPTTIME)
199 					warnx(OPTWARN, "space", "<", MINFREE);
200 				continue;
201 
202 			case 'n':
203  				name = "soft updates";
204  				if (argc < 1)
205  					errx(10, "-s: missing %s", name);
206  				argc--, argv++;
207  				if (strcmp(*argv, "enable") == 0) {
208  					sblock.fs_flags |= FS_DOSOFTDEP;
209  					action = "set";
210  				} else if (strcmp(*argv, "disable") == 0) {
211  					sblock.fs_flags &= ~FS_DOSOFTDEP;
212  					action = "cleared";
213  				} else {
214  					errx(10, "bad %s (options are %s)",
215  					    name, "`enable' or `disable'");
216  				}
217  				warnx("%s %s", name, action);
218  				continue;
219 
220 			case 'o':
221 				name = "optimization preference";
222 				if (argc < 1)
223 					errx(10, "-o: missing %s", name);
224 				argc--, argv++;
225 				chg[FS_OPTSPACE] = "space";
226 				chg[FS_OPTTIME] = "time";
227 				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
228 					i = FS_OPTSPACE;
229 				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
230 					i = FS_OPTTIME;
231 				else
232 					errx(10, "bad %s (options are `space' or `time')",
233 					    name);
234 				if (sblock.fs_optim == i) {
235 					warnx("%s remains unchanged as %s",
236 					    name, chg[i]);
237 					continue;
238 				}
239 				warnx("%s changes from %s to %s",
240 				    name, chg[sblock.fs_optim], chg[i]);
241 				sblock.fs_optim = i;
242 				if (sblock.fs_minfree >= MINFREE &&
243 				    i == FS_OPTSPACE)
244 					warnx(OPTWARN, "time", ">=", MINFREE);
245 				if (sblock.fs_minfree < MINFREE &&
246 				    i == FS_OPTTIME)
247 					warnx(OPTWARN, "space", "<", MINFREE);
248 				continue;
249 
250 			default:
251 				usage();
252 			}
253 	}
254 	if (argc != 1)
255 		usage();
256 	bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
257 	if (Aflag)
258 		for (i = 0; i < sblock.fs_ncg; i++)
259 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
260 			    (char *)&sblock, SBSIZE);
261 	close(fi);
262 	if (active) {
263 		bzero(&args, sizeof(args));
264 		if (mount("ufs", fs->fs_file,
265 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
266 			err(9, "%s: reload", special);
267 		warnx("file system reloaded");
268 	}
269 	exit(0);
270 }
271 
272 void
273 usage()
274 {
275 	fprintf(stderr, "%s\n%s\n%s\n",
276 "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-m minfree]",
277 "              [-p] [-n enable | disable] [-o optimize_preference]",
278 "              [special | filesystem]");
279 	exit(2);
280 }
281 
282 void
283 getsb(fs, file)
284 	register struct fs *fs;
285 	char *file;
286 {
287 
288 	fi = open(file, 2);
289 	if (fi < 0)
290 		err(3, "cannot open %s", file);
291 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
292 		err(4, "%s: bad super block", file);
293 	if (fs->fs_magic != FS_MAGIC)
294 		err(5, "%s: bad magic number", file);
295 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
296 }
297 
298 void
299 printfs()
300 {
301 	warnx("soft updates:  (-n)                                %s",
302 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
303 	warnx("maximum contiguous block count: (-a)               %d",
304 	      sblock.fs_maxcontig);
305 	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
306 	      sblock.fs_rotdelay);
307 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
308 	      sblock.fs_maxbpg);
309 	warnx("minimum percentage of free space: (-m)             %d%%",
310 	      sblock.fs_minfree);
311 	warnx("optimization preference: (-o)                      %s",
312 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
313 	if (sblock.fs_minfree >= MINFREE &&
314 	    sblock.fs_optim == FS_OPTSPACE)
315 		warnx(OPTWARN, "time", ">=", MINFREE);
316 	if (sblock.fs_minfree < MINFREE &&
317 	    sblock.fs_optim == FS_OPTTIME)
318 		warnx(OPTWARN, "space", "<", MINFREE);
319 }
320 
321 void
322 bwrite(blk, buf, size)
323 	daddr_t blk;
324 	char *buf;
325 	int size;
326 {
327 
328 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
329 		err(6, "FS SEEK");
330 	if (write(fi, buf, size) != size)
331 		err(7, "FS WRITE");
332 }
333 
334 int
335 bread(bno, buf, cnt)
336 	daddr_t bno;
337 	char *buf;
338 	int cnt;
339 {
340 	int i;
341 
342 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
343 		return(1);
344 	if ((i = read(fi, buf, cnt)) != cnt) {
345 		for(i=0; i<sblock.fs_bsize; i++)
346 			buf[i] = 0;
347 		return (1);
348 	}
349 	return (0);
350 }
351 
352 char *
353 rawname(special, pathbuf)
354 	char *special;
355 	char *pathbuf;
356 {
357 	char *p;
358 	int n;
359 
360 	p = strrchr(special, '/');
361 	if (p) {
362 		n = ++p - special;
363 		bcopy(special, pathbuf, n);
364 	} else {
365 		strcpy(pathbuf, _PATH_DEV);
366 		n = strlen(pathbuf);
367 		p = special;
368 	}
369 	pathbuf[n++] = 'r';
370 	strcpy(pathbuf + n, p);
371 	return pathbuf;
372 }
373