xref: /freebsd/sbin/tunefs/tunefs.c (revision ee41f1b1cf5e3d4f586cb85b46123b416275862c)
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   "$FreeBSD$";
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 __P((daddr_t, char *, int));
80 int bread __P((daddr_t, char *, int));
81 void getsb __P((struct fs *, char *));
82 void putsb __P((struct fs *, char *, int));
83 void usage __P((void));
84 void printfs __P((void));
85 
86 int
87 main(argc, argv)
88 	int argc;
89 	char *argv[];
90 {
91 	char *special, *name;
92 	struct stat st;
93 	int Aflag = 0, active = 0;
94 	int aflag = 0, dflag = 0, eflag = 0, mflag = 0;
95 	int nflag = 0, oflag = 0, pflag = 0;
96 	int avalue = 0, dvalue = 0, evalue = 0, mvalue = 0, ovalue = 0;
97 	char *nvalue = NULL;
98 	struct fstab *fs;
99 	char *chg[2], device[MAXPATHLEN];
100 	struct ufs_args args;
101 	struct statfs stfs;
102 	int found_arg, ch;
103 
104         if (argc < 3)
105                 usage();
106 	found_arg = 0; /* at least one arg is required */
107 	while ((ch = getopt(argc, argv, "Aa:d:e:m:n:o:p")) != -1)
108 	  switch (ch) {
109 	  case 'A':
110 		found_arg = 1;
111 		Aflag++;
112 		break;
113 	  case 'a':
114 		found_arg = 1;
115 		name = "maximum contiguous block count";
116 		avalue = atoi(optarg);
117 		if (avalue < 1)
118 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
119 		aflag = 1;
120 		break;
121 	  case 'd':
122 		found_arg = 1;
123 		name = "rotational delay between contiguous blocks";
124 		dvalue = atoi(optarg);
125 		dflag = 1;
126 		break;
127 	  case 'e':
128 		found_arg = 1;
129 		name = "maximum blocks per file in a cylinder group";
130 		evalue = atoi(optarg);
131 		if (evalue < 1)
132 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
133 		eflag = 1;
134 		break;
135 	  case 'm':
136 		found_arg = 1;
137 		name = "minimum percentage of free space";
138 		mvalue = atoi(optarg);
139 		if (mvalue < 0 || mvalue > 99)
140 			errx(10, "bad %s (%s)", name, optarg);
141 		mflag = 1;
142 		break;
143 	  case 'n':
144 		found_arg = 1;
145  		name = "soft updates";
146  		nvalue = optarg;
147                 if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
148  			errx(10, "bad %s (options are %s)",
149  			    name, "`enable' or `disable'");
150  		}
151 		nflag = 1;
152  		break;
153 	  case 'o':
154 		found_arg = 1;
155 		name = "optimization preference";
156 		chg[FS_OPTSPACE] = "space";
157 		chg[FS_OPTTIME] = "time";
158 		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
159 			ovalue = FS_OPTSPACE;
160 		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
161 			ovalue = FS_OPTTIME;
162 		else
163 			errx(10, "bad %s (options are `space' or `time')",
164 					    name);
165 		oflag = 1;
166 		break;
167 	  case 'p':
168 		found_arg = 1;
169 		pflag = 1;
170 		break;
171 	  default:
172 		usage();
173 	  }
174 	argc -= optind;
175 	argv += optind;
176 
177 	if (found_arg == 0 || argc != 1)
178 	  usage();
179 
180 	special = argv[0];
181 	fs = getfsfile(special);
182 	if (fs) {
183 		if (statfs(special, &stfs) == 0 &&
184 		    strcmp(special, stfs.f_mntonname) == 0) {
185 			active = 1;
186 		}
187 		special = fs->fs_spec;
188 	}
189 again:
190 	if (stat(special, &st) < 0) {
191 		if (*special != '/') {
192 			if (*special == 'r')
193 				special++;
194 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
195 			special = device;
196 			goto again;
197 		}
198 		err(1, "%s", special);
199 	}
200 	if ((st.st_mode & S_IFMT) != S_IFBLK &&
201 	    (st.st_mode & S_IFMT) != S_IFCHR)
202 		errx(10, "%s: not a block or character device", special);
203 	getsb(&sblock, special);
204 
205 	if (pflag) {
206 		printfs();
207 		exit(0);
208 	}
209 	if (aflag) {
210 		name = "maximum contiguous block count";
211 		if (sblock.fs_maxcontig == avalue) {
212 			warnx("%s remains unchanged as %d", name, avalue);
213 		}
214 		else {
215 			warnx("%s changes from %d to %d",
216 					name, sblock.fs_maxcontig, avalue);
217 			sblock.fs_maxcontig = avalue;
218 		}
219 	}
220 	if (dflag) {
221 		name = "rotational delay between contiguous blocks";
222 		if (sblock.fs_rotdelay == dvalue) {
223 			warnx("%s remains unchanged as %dms", name, dvalue);
224 		}
225 		else {
226 			warnx("%s changes from %dms to %dms",
227 				    name, sblock.fs_rotdelay, dvalue);
228 			sblock.fs_rotdelay = dvalue;
229 		}
230 	}
231 	if (eflag) {
232 		name = "maximum blocks per file in a cylinder group";
233 		if (sblock.fs_maxbpg == evalue) {
234 			warnx("%s remains unchanged as %d", name, evalue);
235 		}
236 		else {
237 			warnx("%s changes from %d to %d",
238 					name, sblock.fs_maxbpg, evalue);
239 			sblock.fs_maxbpg = evalue;
240 		}
241 	}
242 	if (mflag) {
243 		name = "minimum percentage of free space";
244 		if (sblock.fs_minfree == mvalue) {
245 			warnx("%s remains unchanged as %d%%", name, mvalue);
246 		}
247 		else {
248 			warnx("%s changes from %d%% to %d%%",
249 				    name, sblock.fs_minfree, mvalue);
250 			sblock.fs_minfree = mvalue;
251 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
252 				warnx(OPTWARN, "time", ">=", MINFREE);
253 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
254 				warnx(OPTWARN, "space", "<", MINFREE);
255 		}
256 	}
257 	if (nflag) {
258  		name = "soft updates";
259  		if (strcmp(nvalue, "enable") == 0) {
260 			if ( sblock.fs_flags & FS_DOSOFTDEP ) {
261 				warnx("%s remains unchanged as enabled", name);
262 			} else {
263  				sblock.fs_flags |= FS_DOSOFTDEP;
264  				warnx("%s set", name);
265 			}
266  		} else if (strcmp(nvalue, "disable") == 0) {
267 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
268 				warnx("%s remains unchanged as disabled", name);
269 			} else {
270  				sblock.fs_flags &= ~FS_DOSOFTDEP;
271  				warnx("%s cleared", name);
272 			}
273  		}
274 	}
275 	if (oflag) {
276 		name = "optimization preference";
277 		chg[FS_OPTSPACE] = "space";
278 		chg[FS_OPTTIME] = "time";
279 		if (sblock.fs_optim == ovalue) {
280 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
281 		}
282 		else {
283 			warnx("%s changes from %s to %s",
284 				    name, chg[sblock.fs_optim], chg[ovalue]);
285 			sblock.fs_optim = ovalue;
286 			if (sblock.fs_minfree >= MINFREE &&
287 					ovalue == FS_OPTSPACE)
288 				warnx(OPTWARN, "time", ">=", MINFREE);
289 			if (sblock.fs_minfree < MINFREE &&
290 					ovalue == FS_OPTTIME)
291 				warnx(OPTWARN, "space", "<", MINFREE);
292 		}
293 	}
294 
295 	putsb(&sblock, special, Aflag);
296 	if (active) {
297 		bzero(&args, sizeof(args));
298 		if (mount("ufs", fs->fs_file,
299 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
300 			err(9, "%s: reload", special);
301 		warnx("file system reloaded");
302 	}
303 	exit(0);
304 }
305 
306 void
307 usage()
308 {
309 	fprintf(stderr, "%s\n%s\n%s\n",
310 "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-m minfree]",
311 "              [-p] [-n enable | disable] [-o space | time]",
312 "              special | filesystem");
313 	exit(2);
314 }
315 
316 void
317 getsb(fs, file)
318 	register struct fs *fs;
319 	char *file;
320 {
321 
322 	fi = open(file, O_RDONLY);
323 	if (fi < 0)
324 		err(3, "cannot open %s", file);
325 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
326 		err(4, "%s: bad super block", file);
327 	if (fs->fs_magic != FS_MAGIC)
328 		err(5, "%s: bad magic number", file);
329 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
330 }
331 
332 void
333 putsb(fs, file, all)
334 	register struct fs *fs;
335 	char *file;
336 	int all;
337 {
338 	int i;
339 
340 	/*
341 	 * Re-open the device read-write. Use the read-only file
342 	 * descriptor as an interlock to prevent the device from
343 	 * being mounted while we are switching mode.
344 	 */
345 	i = fi;
346 	fi = open(file, O_RDWR);
347 	close(i);
348 	if (fi < 0)
349 		err(3, "cannot open %s", file);
350 	bwrite((daddr_t)SBOFF / dev_bsize, (char *)fs, SBSIZE);
351 	if (all)
352 		for (i = 0; i < fs->fs_ncg; i++)
353 			bwrite(fsbtodb(fs, cgsblock(fs, i)),
354 			    (char *)fs, SBSIZE);
355 	close(fi);
356 }
357 
358 void
359 printfs()
360 {
361 	warnx("soft updates:  (-n)                                %s",
362 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
363 	warnx("maximum contiguous block count: (-a)               %d",
364 	      sblock.fs_maxcontig);
365 	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
366 	      sblock.fs_rotdelay);
367 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
368 	      sblock.fs_maxbpg);
369 	warnx("minimum percentage of free space: (-m)             %d%%",
370 	      sblock.fs_minfree);
371 	warnx("optimization preference: (-o)                      %s",
372 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
373 	if (sblock.fs_minfree >= MINFREE &&
374 	    sblock.fs_optim == FS_OPTSPACE)
375 		warnx(OPTWARN, "time", ">=", MINFREE);
376 	if (sblock.fs_minfree < MINFREE &&
377 	    sblock.fs_optim == FS_OPTTIME)
378 		warnx(OPTWARN, "space", "<", MINFREE);
379 }
380 
381 void
382 bwrite(blk, buf, size)
383 	daddr_t blk;
384 	char *buf;
385 	int size;
386 {
387 
388 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
389 		err(6, "FS SEEK");
390 	if (write(fi, buf, size) != size)
391 		err(7, "FS WRITE");
392 }
393 
394 int
395 bread(bno, buf, cnt)
396 	daddr_t bno;
397 	char *buf;
398 	int cnt;
399 {
400 	int i;
401 
402 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
403 		return(1);
404 	if ((i = read(fi, buf, cnt)) != cnt) {
405 		for(i=0; i<sblock.fs_bsize; i++)
406 			buf[i] = 0;
407 		return (1);
408 	}
409 	return (0);
410 }
411