tunefs.c revision 109963
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 109963 2003-01-28 02:42:01Z jmallett $";
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/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 <libufs.h>
64#include <paths.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <unistd.h>
69
70/* the optimization warning string template */
71#define	OPTWARN	"should optimize for %s with minfree %s %d%%"
72
73struct uufsd disk;
74#define	sblock disk.d_fs
75
76void usage(void);
77void printfs(void);
78
79int
80main(int argc, char *argv[])
81{
82	const char *special, *on;
83	const char *name;
84	int Aflag = 0, active = 0, aflag = 0;
85	int eflag = 0, fflag = 0, lflag = 0, mflag = 0;
86	int nflag = 0, oflag = 0, pflag = 0, sflag = 0;
87	int evalue = 0, fvalue = 0;
88	int mvalue = 0, ovalue = 0, svalue = 0;
89	char *avalue = NULL, *lvalue = NULL, *nvalue = NULL;
90	const char *chg[2];
91	struct ufs_args args;
92	struct statfs stfs;
93	int found_arg, ch;
94
95        if (argc < 3)
96                usage();
97	found_arg = 0; /* at least one arg is required */
98	while ((ch = getopt(argc, argv, "Aa:e:f:l:m:n:o:ps:")) != -1)
99	  switch (ch) {
100	  case 'A':
101		found_arg = 1;
102		Aflag++;
103		break;
104	  case 'a':
105		found_arg = 1;
106		name = "ACLs";
107		avalue = optarg;
108		if (strcmp(avalue, "enable") && strcmp(avalue, "disable")) {
109			errx(10, "bad %s (options are %s)", name,
110			    "`enable' or `disable'");
111		}
112		aflag = 1;
113		break;
114	  case 'e':
115		found_arg = 1;
116		name = "maximum blocks per file in a cylinder group";
117		evalue = atoi(optarg);
118		if (evalue < 1)
119			errx(10, "%s must be >= 1 (was %s)", name, optarg);
120		eflag = 1;
121		break;
122	  case 'f':
123		found_arg = 1;
124		name = "average file size";
125		fvalue = atoi(optarg);
126		if (fvalue < 1)
127			errx(10, "%s must be >= 1 (was %s)", name, optarg);
128		fflag = 1;
129		break;
130	  case 'l':
131		found_arg = 1;
132		name = "multilabel MAC file system";
133		lvalue = optarg;
134		if (strcmp(lvalue, "enable") && strcmp(lvalue, "disable")) {
135			errx(10, "bad %s (options are %s)", name,
136			    "`enable' or `disable'");
137		}
138		lflag = 1;
139		break;
140	  case 'm':
141		found_arg = 1;
142		name = "minimum percentage of free space";
143		mvalue = atoi(optarg);
144		if (mvalue < 0 || mvalue > 99)
145			errx(10, "bad %s (%s)", name, optarg);
146		mflag = 1;
147		break;
148	  case 'n':
149		found_arg = 1;
150 		name = "soft updates";
151 		nvalue = optarg;
152                if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
153 			errx(10, "bad %s (options are %s)",
154 			    name, "`enable' or `disable'");
155 		}
156		nflag = 1;
157 		break;
158	  case 'o':
159		found_arg = 1;
160		name = "optimization preference";
161		chg[FS_OPTSPACE] = "space";
162		chg[FS_OPTTIME] = "time";
163		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
164			ovalue = FS_OPTSPACE;
165		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
166			ovalue = FS_OPTTIME;
167		else
168			errx(10, "bad %s (options are `space' or `time')",
169					    name);
170		oflag = 1;
171		break;
172	  case 'p':
173		found_arg = 1;
174		pflag = 1;
175		break;
176	  case 's':
177		found_arg = 1;
178		name = "expected number of files per directory";
179		svalue = atoi(optarg);
180		if (svalue < 1)
181			errx(10, "%s must be >= 1 (was %s)", name, optarg);
182		sflag = 1;
183		break;
184	  default:
185		usage();
186	  }
187	argc -= optind;
188	argv += optind;
189
190	if (found_arg == 0 || argc != 1)
191	  usage();
192
193	on = special = argv[0];
194	if (ufs_disk_fillout(&disk, special) == -1)
195		goto err;
196	if (disk.d_name != special) {
197		special = disk.d_name;
198		if (statfs(special, &stfs) == 0 &&
199		    strcmp(special, stfs.f_mntonname) == 0)
200			active = 1;
201	}
202
203	if (pflag) {
204		printfs();
205		exit(0);
206	}
207	if (aflag) {
208		name = "ACLs";
209		if (strcmp(avalue, "enable") == 0) {
210			if (sblock.fs_flags & FS_ACLS) {
211				warnx("%s remains unchanged as enabled", name);
212			} else {
213				sblock.fs_flags |= FS_ACLS;
214				warnx("%s set", name);
215			}
216		} else if (strcmp(avalue, "disable") == 0) {
217			if ((~sblock.fs_flags & FS_ACLS) ==
218			    FS_ACLS) {
219				warnx("%s remains unchanged as disabled",
220				    name);
221			} else {
222				sblock.fs_flags &= ~FS_ACLS;
223				warnx("%s cleared", name);
224			}
225		}
226	}
227	if (eflag) {
228		name = "maximum blocks per file in a cylinder group";
229		if (sblock.fs_maxbpg == evalue) {
230			warnx("%s remains unchanged as %d", name, evalue);
231		}
232		else {
233			warnx("%s changes from %d to %d",
234					name, sblock.fs_maxbpg, evalue);
235			sblock.fs_maxbpg = evalue;
236		}
237	}
238	if (fflag) {
239		name = "average file size";
240		if (sblock.fs_avgfilesize == fvalue) {
241			warnx("%s remains unchanged as %d", name, fvalue);
242		}
243		else {
244			warnx("%s changes from %d to %d",
245					name, sblock.fs_avgfilesize, fvalue);
246			sblock.fs_avgfilesize = fvalue;
247		}
248	}
249	if (lflag) {
250		name = "multilabel";
251		if (strcmp(lvalue, "enable") == 0) {
252			if (sblock.fs_flags & FS_MULTILABEL) {
253				warnx("%s remains unchanged as enabled", name);
254			} else {
255				sblock.fs_flags |= FS_MULTILABEL;
256				warnx("%s set", name);
257			}
258		} else if (strcmp(lvalue, "disable") == 0) {
259			if ((~sblock.fs_flags & FS_MULTILABEL) ==
260			    FS_MULTILABEL) {
261				warnx("%s remains unchanged as disabled",
262				    name);
263			} else {
264				sblock.fs_flags &= ~FS_MULTILABEL;
265				warnx("%s cleared", name);
266			}
267		}
268	}
269	if (mflag) {
270		name = "minimum percentage of free space";
271		if (sblock.fs_minfree == mvalue) {
272			warnx("%s remains unchanged as %d%%", name, mvalue);
273		}
274		else {
275			warnx("%s changes from %d%% to %d%%",
276				    name, sblock.fs_minfree, mvalue);
277			sblock.fs_minfree = mvalue;
278			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
279				warnx(OPTWARN, "time", ">=", MINFREE);
280			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
281				warnx(OPTWARN, "space", "<", MINFREE);
282		}
283	}
284	if (nflag) {
285 		name = "soft updates";
286 		if (strcmp(nvalue, "enable") == 0) {
287			if (sblock.fs_flags & FS_DOSOFTDEP) {
288				warnx("%s remains unchanged as enabled", name);
289			} else if (sblock.fs_clean == 0) {
290				warnx("%s cannot be enabled until fsck is run",
291				    name);
292			} else {
293 				sblock.fs_flags |= FS_DOSOFTDEP;
294 				warnx("%s set", name);
295			}
296 		} else if (strcmp(nvalue, "disable") == 0) {
297			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
298				warnx("%s remains unchanged as disabled", name);
299			} else {
300 				sblock.fs_flags &= ~FS_DOSOFTDEP;
301 				warnx("%s cleared", name);
302			}
303 		}
304	}
305	if (oflag) {
306		name = "optimization preference";
307		chg[FS_OPTSPACE] = "space";
308		chg[FS_OPTTIME] = "time";
309		if (sblock.fs_optim == ovalue) {
310			warnx("%s remains unchanged as %s", name, chg[ovalue]);
311		}
312		else {
313			warnx("%s changes from %s to %s",
314				    name, chg[sblock.fs_optim], chg[ovalue]);
315			sblock.fs_optim = ovalue;
316			if (sblock.fs_minfree >= MINFREE &&
317					ovalue == FS_OPTSPACE)
318				warnx(OPTWARN, "time", ">=", MINFREE);
319			if (sblock.fs_minfree < MINFREE &&
320					ovalue == FS_OPTTIME)
321				warnx(OPTWARN, "space", "<", MINFREE);
322		}
323	}
324	if (sflag) {
325		name = "expected number of files per directory";
326		if (sblock.fs_avgfpdir == svalue) {
327			warnx("%s remains unchanged as %d", name, svalue);
328		}
329		else {
330			warnx("%s changes from %d to %d",
331					name, sblock.fs_avgfpdir, svalue);
332			sblock.fs_avgfpdir = svalue;
333		}
334	}
335
336	if (sbwrite(&disk, Aflag) == -1)
337		goto err;
338	ufs_disk_close(&disk);
339	if (active) {
340		bzero(&args, sizeof(args));
341		if (mount("ufs", on,
342		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
343			err(9, "%s: reload", special);
344		warnx("file system reloaded");
345	}
346	exit(0);
347err:
348	if (disk.d_error != NULL)
349		errx(11, "%s: %s", special, disk.d_error);
350	else
351		err(12, "%s", special);
352}
353
354void
355usage(void)
356{
357	fprintf(stderr, "%s\n%s\n%s\n",
358"usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]",
359"              [-l enable | disable] [-m minfree] [-n enable | disable]",
360"              [-o space | time] [-p] [-s avgfpdir] special | filesystem");
361	exit(2);
362}
363
364void
365printfs(void)
366{
367	warnx("ACLs: (-a)                                         %s",
368		(sblock.fs_flags & FS_ACLS)? "enabled" : "disabled");
369	warnx("MAC multilabel: (-l)                               %s",
370		(sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled");
371	warnx("soft updates: (-n)                                 %s",
372		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
373	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
374	      sblock.fs_maxbpg);
375	warnx("average file size: (-f)                            %d",
376	      sblock.fs_avgfilesize);
377	warnx("average number of files in a directory: (-s)       %d",
378	      sblock.fs_avgfpdir);
379	warnx("minimum percentage of free space: (-m)             %d%%",
380	      sblock.fs_minfree);
381	warnx("optimization preference: (-o)                      %s",
382	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
383	if (sblock.fs_minfree >= MINFREE &&
384	    sblock.fs_optim == FS_OPTSPACE)
385		warnx(OPTWARN, "time", ">=", MINFREE);
386	if (sblock.fs_minfree < MINFREE &&
387	    sblock.fs_optim == FS_OPTTIME)
388		warnx(OPTWARN, "space", "<", MINFREE);
389}
390