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