tunefs.c revision 98542
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 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
42static char sccsid[] = "@(#)tunefs.c	8.2 (Berkeley) 4/19/94";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/sbin/tunefs/tunefs.c 98542 2002-06-21 06:18:05Z mckusick $";
46#endif /* not lint */
47
48/*
49 * tunefs: change layout parameters to an existing filesystem.
50 */
51#include <sys/param.h>
52#include <sys/mount.h>
53#include <sys/disklabel.h>
54#include <sys/stat.h>
55
56#include <ufs/ufs/ufsmount.h>
57#include <ufs/ufs/dinode.h>
58#include <ufs/ffs/fs.h>
59
60#include <err.h>
61#include <fcntl.h>
62#include <fstab.h>
63#include <paths.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68
69/* the optimization warning string template */
70#define	OPTWARN	"should optimize for %s with minfree %s %d%%"
71
72union {
73	struct	fs sb;
74	char pad[MAXBSIZE];
75} sbun;
76#define	sblock sbun.sb
77
78int fi;
79long dev_bsize = 1;
80
81void bwrite(ufs2_daddr_t, const char *, int);
82int bread(ufs2_daddr_t, char *, int);
83void getsb(struct fs *, const char *);
84void putsb(struct fs *, const char *, int);
85void usage(void);
86void printfs(void);
87
88int
89main(argc, argv)
90	int argc;
91	char *argv[];
92{
93	char *special;
94	const char *name;
95	struct stat st;
96	int Aflag = 0, active = 0;
97	int aflag = 0, eflag = 0, fflag = 0, mflag = 0;
98	int nflag = 0, oflag = 0, pflag = 0, sflag = 0;
99	int avalue = 0, evalue = 0, fvalue = 0;
100	int mvalue = 0, ovalue = 0, svalue = 0;
101	char *nvalue = NULL;
102	struct fstab *fs;
103	const char *chg[2];
104	char device[MAXPATHLEN];
105	struct ufs_args args;
106	struct statfs stfs;
107	int found_arg, ch;
108
109        if (argc < 3)
110                usage();
111	found_arg = 0; /* at least one arg is required */
112	while ((ch = getopt(argc, argv, "Aa:e:f:m:n:o:ps:")) != -1)
113	  switch (ch) {
114	  case 'A':
115		found_arg = 1;
116		Aflag++;
117		break;
118	  case 'a':
119		found_arg = 1;
120		name = "maximum contiguous block count";
121		avalue = atoi(optarg);
122		if (avalue < 1)
123			errx(10, "%s must be >= 1 (was %s)", name, optarg);
124		aflag = 1;
125		break;
126	  case 'e':
127		found_arg = 1;
128		name = "maximum blocks per file in a cylinder group";
129		evalue = atoi(optarg);
130		if (evalue < 1)
131			errx(10, "%s must be >= 1 (was %s)", name, optarg);
132		eflag = 1;
133		break;
134	  case 'f':
135		found_arg = 1;
136		name = "average file size";
137		fvalue = atoi(optarg);
138		if (fvalue < 1)
139			errx(10, "%s must be >= 1 (was %s)", name, optarg);
140		fflag = 1;
141		break;
142	  case 'm':
143		found_arg = 1;
144		name = "minimum percentage of free space";
145		mvalue = atoi(optarg);
146		if (mvalue < 0 || mvalue > 99)
147			errx(10, "bad %s (%s)", name, optarg);
148		mflag = 1;
149		break;
150	  case 'n':
151		found_arg = 1;
152 		name = "soft updates";
153 		nvalue = optarg;
154                if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
155 			errx(10, "bad %s (options are %s)",
156 			    name, "`enable' or `disable'");
157 		}
158		nflag = 1;
159 		break;
160	  case 'o':
161		found_arg = 1;
162		name = "optimization preference";
163		chg[FS_OPTSPACE] = "space";
164		chg[FS_OPTTIME] = "time";
165		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
166			ovalue = FS_OPTSPACE;
167		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
168			ovalue = FS_OPTTIME;
169		else
170			errx(10, "bad %s (options are `space' or `time')",
171					    name);
172		oflag = 1;
173		break;
174	  case 'p':
175		found_arg = 1;
176		pflag = 1;
177		break;
178	  case 's':
179		found_arg = 1;
180		name = "expected number of files per directory";
181		svalue = atoi(optarg);
182		if (svalue < 1)
183			errx(10, "%s must be >= 1 (was %s)", name, optarg);
184		sflag = 1;
185		break;
186	  default:
187		usage();
188	  }
189	argc -= optind;
190	argv += optind;
191
192	if (found_arg == 0 || argc != 1)
193	  usage();
194
195	special = argv[0];
196	fs = getfsfile(special);
197	if (fs) {
198		if (statfs(special, &stfs) == 0 &&
199		    strcmp(special, stfs.f_mntonname) == 0) {
200			active = 1;
201		}
202		special = fs->fs_spec;
203	}
204again:
205	if (stat(special, &st) < 0) {
206		if (*special != '/') {
207			if (*special == 'r')
208				special++;
209			(void)snprintf(device, sizeof(device), "%s%s",
210				       _PATH_DEV, special);
211			special = device;
212			goto again;
213		}
214		err(1, "%s", special);
215	}
216	if (fs == NULL && (st.st_mode & S_IFMT) == S_IFDIR)
217		errx(10, "%s: unknown filesystem", special);
218	getsb(&sblock, special);
219
220	if (pflag) {
221		printfs();
222		exit(0);
223	}
224	if (aflag) {
225		name = "maximum contiguous block count";
226		if (sblock.fs_maxcontig == avalue) {
227			warnx("%s remains unchanged as %d", name, avalue);
228		}
229		else {
230			warnx("%s changes from %d to %d",
231					name, sblock.fs_maxcontig, avalue);
232			sblock.fs_maxcontig = avalue;
233		}
234	}
235	if (eflag) {
236		name = "maximum blocks per file in a cylinder group";
237		if (sblock.fs_maxbpg == evalue) {
238			warnx("%s remains unchanged as %d", name, evalue);
239		}
240		else {
241			warnx("%s changes from %d to %d",
242					name, sblock.fs_maxbpg, evalue);
243			sblock.fs_maxbpg = evalue;
244		}
245	}
246	if (fflag) {
247		name = "average file size";
248		if (sblock.fs_avgfilesize == fvalue) {
249			warnx("%s remains unchanged as %d", name, fvalue);
250		}
251		else {
252			warnx("%s changes from %d to %d",
253					name, sblock.fs_avgfilesize, fvalue);
254			sblock.fs_avgfilesize = fvalue;
255		}
256	}
257	if (mflag) {
258		name = "minimum percentage of free space";
259		if (sblock.fs_minfree == mvalue) {
260			warnx("%s remains unchanged as %d%%", name, mvalue);
261		}
262		else {
263			warnx("%s changes from %d%% to %d%%",
264				    name, sblock.fs_minfree, mvalue);
265			sblock.fs_minfree = mvalue;
266			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
267				warnx(OPTWARN, "time", ">=", MINFREE);
268			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
269				warnx(OPTWARN, "space", "<", MINFREE);
270		}
271	}
272	if (nflag) {
273 		name = "soft updates";
274 		if (strcmp(nvalue, "enable") == 0) {
275			if (sblock.fs_flags & FS_DOSOFTDEP) {
276				warnx("%s remains unchanged as enabled", name);
277			} else if (sblock.fs_clean == 0) {
278				warnx("%s cannot be enabled until fsck is run",
279				    name);
280			} else {
281 				sblock.fs_flags |= FS_DOSOFTDEP;
282 				warnx("%s set", name);
283			}
284 		} else if (strcmp(nvalue, "disable") == 0) {
285			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
286				warnx("%s remains unchanged as disabled", name);
287			} else {
288 				sblock.fs_flags &= ~FS_DOSOFTDEP;
289 				warnx("%s cleared", name);
290			}
291 		}
292	}
293	if (oflag) {
294		name = "optimization preference";
295		chg[FS_OPTSPACE] = "space";
296		chg[FS_OPTTIME] = "time";
297		if (sblock.fs_optim == ovalue) {
298			warnx("%s remains unchanged as %s", name, chg[ovalue]);
299		}
300		else {
301			warnx("%s changes from %s to %s",
302				    name, chg[sblock.fs_optim], chg[ovalue]);
303			sblock.fs_optim = ovalue;
304			if (sblock.fs_minfree >= MINFREE &&
305					ovalue == FS_OPTSPACE)
306				warnx(OPTWARN, "time", ">=", MINFREE);
307			if (sblock.fs_minfree < MINFREE &&
308					ovalue == FS_OPTTIME)
309				warnx(OPTWARN, "space", "<", MINFREE);
310		}
311	}
312	if (sflag) {
313		name = "expected number of files per directory";
314		if (sblock.fs_avgfpdir == svalue) {
315			warnx("%s remains unchanged as %d", name, svalue);
316		}
317		else {
318			warnx("%s changes from %d to %d",
319					name, sblock.fs_avgfpdir, svalue);
320			sblock.fs_avgfpdir = svalue;
321		}
322	}
323
324	putsb(&sblock, special, Aflag);
325	if (active) {
326		bzero(&args, sizeof(args));
327		if (mount("ufs", fs->fs_file,
328		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
329			err(9, "%s: reload", special);
330		warnx("filesystem reloaded");
331	}
332	exit(0);
333}
334
335void
336usage()
337{
338	fprintf(stderr, "%s\n%s\n%s\n",
339"usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
340"              [-m minfree] [-p] [-n enable | disable] [-o space | time]",
341"              [-s filesperdir] special | filesystem");
342	exit(2);
343}
344
345/*
346 * Possible superblock locations ordered from most to least likely.
347 */
348static int sblock_try[] = SBLOCKSEARCH;
349static ufs2_daddr_t sblockloc;
350
351void
352getsb(fs, file)
353	struct fs *fs;
354	const char *file;
355{
356	int i;
357
358	fi = open(file, O_RDONLY);
359	if (fi < 0)
360		err(3, "cannot open %s", file);
361	for (i = 0; sblock_try[i] != -1; i++) {
362		if (bread(sblock_try[i], (char *)fs, SBLOCKSIZE))
363			err(4, "%s: bad super block", file);
364		if ((fs->fs_magic == FS_UFS1_MAGIC ||
365		     (fs->fs_magic == FS_UFS2_MAGIC &&
366		      fs->fs_sblockloc == numfrags(fs, sblock_try[i]))) &&
367		    fs->fs_bsize <= MAXBSIZE &&
368		    fs->fs_bsize >= sizeof(struct fs))
369			break;
370	}
371	if (sblock_try[i] == -1)
372		err(5, "Cannot find filesystem superblock");
373	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
374	sblockloc = sblock_try[i] / dev_bsize;
375}
376
377void
378putsb(fs, file, all)
379	struct fs *fs;
380	const char *file;
381	int all;
382{
383	int i;
384
385	/*
386	 * Re-open the device read-write. Use the read-only file
387	 * descriptor as an interlock to prevent the device from
388	 * being mounted while we are switching mode.
389	 */
390	i = fi;
391	fi = open(file, O_RDWR);
392	close(i);
393	if (fi < 0)
394		err(3, "cannot open %s", file);
395	bwrite(sblockloc, (const char *)fs, SBLOCKSIZE);
396	if (all)
397		for (i = 0; i < fs->fs_ncg; i++)
398			bwrite(fsbtodb(fs, cgsblock(fs, i)),
399			    (const char *)fs, SBLOCKSIZE);
400	close(fi);
401}
402
403void
404printfs()
405{
406	warnx("soft updates:  (-n)                                %s",
407		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
408	warnx("maximum contiguous block count: (-a)               %d",
409	      sblock.fs_maxcontig);
410	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
411	      sblock.fs_maxbpg);
412	warnx("average file size: (-f)                            %d",
413	      sblock.fs_avgfilesize);
414	warnx("average number of files in a directory: (-s)       %d",
415	      sblock.fs_avgfpdir);
416	warnx("minimum percentage of free space: (-m)             %d%%",
417	      sblock.fs_minfree);
418	warnx("optimization preference: (-o)                      %s",
419	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
420	if (sblock.fs_minfree >= MINFREE &&
421	    sblock.fs_optim == FS_OPTSPACE)
422		warnx(OPTWARN, "time", ">=", MINFREE);
423	if (sblock.fs_minfree < MINFREE &&
424	    sblock.fs_optim == FS_OPTTIME)
425		warnx(OPTWARN, "space", "<", MINFREE);
426}
427
428void
429bwrite(blk, buf, size)
430	ufs2_daddr_t blk;
431	const char *buf;
432	int size;
433{
434
435	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
436		err(6, "FS SEEK");
437	if (write(fi, buf, size) != size)
438		err(7, "FS WRITE");
439}
440
441int
442bread(bno, buf, cnt)
443	ufs2_daddr_t bno;
444	char *buf;
445	int cnt;
446{
447	int i;
448
449	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
450		return(1);
451	if ((i = read(fi, buf, cnt)) != cnt) {
452		for(i=0; i<sblock.fs_bsize; i++)
453			buf[i] = 0;
454		return (1);
455	}
456	return (0);
457}
458