xref: /freebsd/sbin/tunefs/tunefs.c (revision c33fa91f6135f676cf5f6a789f74872f26154d8a)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1983, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
138fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
348fae3551SRodney W. Grimes #ifndef lint
358679b1b4SPhilippe Charnier static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1983, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
418679b1b4SPhilippe Charnier #if 0
428fae3551SRodney W. Grimes static char sccsid[] = "@(#)tunefs.c	8.2 (Berkeley) 4/19/94";
438679b1b4SPhilippe Charnier #endif
448679b1b4SPhilippe Charnier static const char rcsid[] =
457f3dea24SPeter Wemm   "$FreeBSD$";
468fae3551SRodney W. Grimes #endif /* not lint */
478fae3551SRodney W. Grimes 
488fae3551SRodney W. Grimes /*
498fae3551SRodney W. Grimes  * tunefs: change layout parameters to an existing file system.
508fae3551SRodney W. Grimes  */
518fae3551SRodney W. Grimes #include <sys/param.h>
527382c45aSLuoqi Chen #include <sys/mount.h>
538fae3551SRodney W. Grimes #include <sys/stat.h>
548fae3551SRodney W. Grimes 
558fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
567382c45aSLuoqi Chen #include <ufs/ufs/ufsmount.h>
578fae3551SRodney W. Grimes 
588fae3551SRodney W. Grimes #include <err.h>
598fae3551SRodney W. Grimes #include <fcntl.h>
608fae3551SRodney W. Grimes #include <fstab.h>
618fae3551SRodney W. Grimes #include <paths.h>
628679b1b4SPhilippe Charnier #include <stdio.h>
638fae3551SRodney W. Grimes #include <stdlib.h>
64060ac658SSheldon Hearn #include <string.h>
658fae3551SRodney W. Grimes #include <unistd.h>
668fae3551SRodney W. Grimes 
678fae3551SRodney W. Grimes /* the optimization warning string template */
688fae3551SRodney W. Grimes #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
698fae3551SRodney W. Grimes 
708fae3551SRodney W. Grimes union {
718fae3551SRodney W. Grimes 	struct	fs sb;
728fae3551SRodney W. Grimes 	char pad[MAXBSIZE];
738fae3551SRodney W. Grimes } sbun;
748fae3551SRodney W. Grimes #define	sblock sbun.sb
758fae3551SRodney W. Grimes 
768fae3551SRodney W. Grimes int fi;
778fae3551SRodney W. Grimes long dev_bsize = 1;
788fae3551SRodney W. Grimes 
79c33fa91fSDima Dorfman void bwrite __P((daddr_t, const char *, int));
80060ac658SSheldon Hearn int bread __P((daddr_t, char *, int));
81c33fa91fSDima Dorfman void getsb __P((struct fs *, const char *));
82c33fa91fSDima Dorfman void putsb __P((struct fs *, const char *, int));
838fae3551SRodney W. Grimes void usage __P((void));
8416a7269eSJoerg Wunsch void printfs __P((void));
858fae3551SRodney W. Grimes 
868fae3551SRodney W. Grimes int
878fae3551SRodney W. Grimes main(argc, argv)
888fae3551SRodney W. Grimes 	int argc;
898fae3551SRodney W. Grimes 	char *argv[];
908fae3551SRodney W. Grimes {
91c33fa91fSDima Dorfman 	char *special;
92c33fa91fSDima Dorfman 	const char *name;
938fae3551SRodney W. Grimes 	struct stat st;
947382c45aSLuoqi Chen 	int Aflag = 0, active = 0;
95a61ab64aSKirk McKusick 	int aflag = 0, dflag = 0, eflag = 0, fflag = 0, mflag = 0;
96a61ab64aSKirk McKusick 	int nflag = 0, oflag = 0, pflag = 0, sflag = 0;
97a61ab64aSKirk McKusick 	int avalue = 0, dvalue = 0, evalue = 0, fvalue = 0;
98a61ab64aSKirk McKusick 	int mvalue = 0, ovalue = 0, svalue = 0;
9977edab90SPhilippe Charnier 	char *nvalue = NULL;
1008fae3551SRodney W. Grimes 	struct fstab *fs;
101c33fa91fSDima Dorfman 	const char *chg[2];
102c33fa91fSDima Dorfman 	char device[MAXPATHLEN];
1037382c45aSLuoqi Chen 	struct ufs_args args;
1047382c45aSLuoqi Chen 	struct statfs stfs;
1052af14b60SPhilippe Charnier 	int found_arg, ch;
1068fae3551SRodney W. Grimes 
1072af14b60SPhilippe Charnier         if (argc < 3)
1088fae3551SRodney W. Grimes                 usage();
10977edab90SPhilippe Charnier 	found_arg = 0; /* at least one arg is required */
110a61ab64aSKirk McKusick 	while ((ch = getopt(argc, argv, "Aa:d:e:f:m:n:o:ps:")) != -1)
11177edab90SPhilippe Charnier 	  switch (ch) {
11277edab90SPhilippe Charnier 	  case 'A':
11377edab90SPhilippe Charnier 		found_arg = 1;
11477edab90SPhilippe Charnier 		Aflag++;
11577edab90SPhilippe Charnier 		break;
11677edab90SPhilippe Charnier 	  case 'a':
11777edab90SPhilippe Charnier 		found_arg = 1;
11877edab90SPhilippe Charnier 		name = "maximum contiguous block count";
11977edab90SPhilippe Charnier 		avalue = atoi(optarg);
12077edab90SPhilippe Charnier 		if (avalue < 1)
12177edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
12277edab90SPhilippe Charnier 		aflag = 1;
12377edab90SPhilippe Charnier 		break;
12477edab90SPhilippe Charnier 	  case 'd':
12577edab90SPhilippe Charnier 		found_arg = 1;
12677edab90SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
12777edab90SPhilippe Charnier 		dvalue = atoi(optarg);
12877edab90SPhilippe Charnier 		dflag = 1;
12977edab90SPhilippe Charnier 		break;
13077edab90SPhilippe Charnier 	  case 'e':
13177edab90SPhilippe Charnier 		found_arg = 1;
13277edab90SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
13377edab90SPhilippe Charnier 		evalue = atoi(optarg);
13477edab90SPhilippe Charnier 		if (evalue < 1)
13577edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
13677edab90SPhilippe Charnier 		eflag = 1;
13777edab90SPhilippe Charnier 		break;
138a61ab64aSKirk McKusick 	  case 'f':
139a61ab64aSKirk McKusick 		found_arg = 1;
140a61ab64aSKirk McKusick 		name = "average file size";
141a61ab64aSKirk McKusick 		fvalue = atoi(optarg);
142a61ab64aSKirk McKusick 		if (fvalue < 1)
143a61ab64aSKirk McKusick 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
144a61ab64aSKirk McKusick 		fflag = 1;
145a61ab64aSKirk McKusick 		break;
14677edab90SPhilippe Charnier 	  case 'm':
14777edab90SPhilippe Charnier 		found_arg = 1;
14877edab90SPhilippe Charnier 		name = "minimum percentage of free space";
14977edab90SPhilippe Charnier 		mvalue = atoi(optarg);
15077edab90SPhilippe Charnier 		if (mvalue < 0 || mvalue > 99)
15177edab90SPhilippe Charnier 			errx(10, "bad %s (%s)", name, optarg);
15277edab90SPhilippe Charnier 		mflag = 1;
15377edab90SPhilippe Charnier 		break;
15477edab90SPhilippe Charnier 	  case 'n':
15577edab90SPhilippe Charnier 		found_arg = 1;
15677edab90SPhilippe Charnier  		name = "soft updates";
15777edab90SPhilippe Charnier  		nvalue = optarg;
15877edab90SPhilippe Charnier                 if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
15977edab90SPhilippe Charnier  			errx(10, "bad %s (options are %s)",
16077edab90SPhilippe Charnier  			    name, "`enable' or `disable'");
16177edab90SPhilippe Charnier  		}
16277edab90SPhilippe Charnier 		nflag = 1;
16377edab90SPhilippe Charnier  		break;
16477edab90SPhilippe Charnier 	  case 'o':
16577edab90SPhilippe Charnier 		found_arg = 1;
16677edab90SPhilippe Charnier 		name = "optimization preference";
16777edab90SPhilippe Charnier 		chg[FS_OPTSPACE] = "space";
16877edab90SPhilippe Charnier 		chg[FS_OPTTIME] = "time";
16977edab90SPhilippe Charnier 		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
17077edab90SPhilippe Charnier 			ovalue = FS_OPTSPACE;
17177edab90SPhilippe Charnier 		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
17277edab90SPhilippe Charnier 			ovalue = FS_OPTTIME;
17377edab90SPhilippe Charnier 		else
17477edab90SPhilippe Charnier 			errx(10, "bad %s (options are `space' or `time')",
17577edab90SPhilippe Charnier 					    name);
17677edab90SPhilippe Charnier 		oflag = 1;
17777edab90SPhilippe Charnier 		break;
17877edab90SPhilippe Charnier 	  case 'p':
179e50fa3d2SBen Smithurst 		found_arg = 1;
18077edab90SPhilippe Charnier 		pflag = 1;
18177edab90SPhilippe Charnier 		break;
182a61ab64aSKirk McKusick 	  case 's':
183a61ab64aSKirk McKusick 		found_arg = 1;
184a61ab64aSKirk McKusick 		name = "expected number of files per directory";
185a61ab64aSKirk McKusick 		svalue = atoi(optarg);
186a61ab64aSKirk McKusick 		if (svalue < 1)
187a61ab64aSKirk McKusick 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
188a61ab64aSKirk McKusick 		sflag = 1;
189a61ab64aSKirk McKusick 		break;
19077edab90SPhilippe Charnier 	  default:
19177edab90SPhilippe Charnier 		usage();
19277edab90SPhilippe Charnier 	  }
19377edab90SPhilippe Charnier 	argc -= optind;
19477edab90SPhilippe Charnier 	argv += optind;
19577edab90SPhilippe Charnier 
19677edab90SPhilippe Charnier 	if (found_arg == 0 || argc != 1)
19777edab90SPhilippe Charnier 	  usage();
19877edab90SPhilippe Charnier 
19977edab90SPhilippe Charnier 	special = argv[0];
2008fae3551SRodney W. Grimes 	fs = getfsfile(special);
2017382c45aSLuoqi Chen 	if (fs) {
2029ea6e95eSLuoqi Chen 		if (statfs(special, &stfs) == 0 &&
2039ea6e95eSLuoqi Chen 		    strcmp(special, stfs.f_mntonname) == 0) {
2047382c45aSLuoqi Chen 			active = 1;
205b20ae6a0SLuoqi Chen 		}
2068fae3551SRodney W. Grimes 		special = fs->fs_spec;
2077382c45aSLuoqi Chen 	}
2088fae3551SRodney W. Grimes again:
2098fae3551SRodney W. Grimes 	if (stat(special, &st) < 0) {
2108fae3551SRodney W. Grimes 		if (*special != '/') {
2118fae3551SRodney W. Grimes 			if (*special == 'r')
2128fae3551SRodney W. Grimes 				special++;
2138fae3551SRodney W. Grimes 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
2148fae3551SRodney W. Grimes 			special = device;
2158fae3551SRodney W. Grimes 			goto again;
2168fae3551SRodney W. Grimes 		}
2178fae3551SRodney W. Grimes 		err(1, "%s", special);
2188fae3551SRodney W. Grimes 	}
2198fae3551SRodney W. Grimes 	if ((st.st_mode & S_IFMT) != S_IFBLK &&
2208fae3551SRodney W. Grimes 	    (st.st_mode & S_IFMT) != S_IFCHR)
2218fae3551SRodney W. Grimes 		errx(10, "%s: not a block or character device", special);
2228fae3551SRodney W. Grimes 	getsb(&sblock, special);
2238fae3551SRodney W. Grimes 
22477edab90SPhilippe Charnier 	if (pflag) {
22577edab90SPhilippe Charnier 		printfs();
22677edab90SPhilippe Charnier 		exit(0);
22777edab90SPhilippe Charnier 	}
22877edab90SPhilippe Charnier 	if (aflag) {
2298fae3551SRodney W. Grimes 		name = "maximum contiguous block count";
23077edab90SPhilippe Charnier 		if (sblock.fs_maxcontig == avalue) {
23177edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, avalue);
2322af14b60SPhilippe Charnier 		}
23377edab90SPhilippe Charnier 		else {
23477edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
23577edab90SPhilippe Charnier 					name, sblock.fs_maxcontig, avalue);
23677edab90SPhilippe Charnier 			sblock.fs_maxcontig = avalue;
23777edab90SPhilippe Charnier 		}
23877edab90SPhilippe Charnier 	}
23977edab90SPhilippe Charnier 	if (dflag) {
2402af14b60SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
24177edab90SPhilippe Charnier 		if (sblock.fs_rotdelay == dvalue) {
24277edab90SPhilippe Charnier 			warnx("%s remains unchanged as %dms", name, dvalue);
2432af14b60SPhilippe Charnier 		}
24477edab90SPhilippe Charnier 		else {
2458fae3551SRodney W. Grimes 			warnx("%s changes from %dms to %dms",
24677edab90SPhilippe Charnier 				    name, sblock.fs_rotdelay, dvalue);
24777edab90SPhilippe Charnier 			sblock.fs_rotdelay = dvalue;
24877edab90SPhilippe Charnier 		}
24977edab90SPhilippe Charnier 	}
25077edab90SPhilippe Charnier 	if (eflag) {
2512af14b60SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
25277edab90SPhilippe Charnier 		if (sblock.fs_maxbpg == evalue) {
25377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, evalue);
2542af14b60SPhilippe Charnier 		}
25577edab90SPhilippe Charnier 		else {
25677edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
25777edab90SPhilippe Charnier 					name, sblock.fs_maxbpg, evalue);
25877edab90SPhilippe Charnier 			sblock.fs_maxbpg = evalue;
25977edab90SPhilippe Charnier 		}
26077edab90SPhilippe Charnier 	}
261a61ab64aSKirk McKusick 	if (fflag) {
262a61ab64aSKirk McKusick 		name = "average file size";
263a61ab64aSKirk McKusick 		if (sblock.fs_avgfilesize == fvalue) {
264a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, fvalue);
265a61ab64aSKirk McKusick 		}
266a61ab64aSKirk McKusick 		else {
267a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
268a61ab64aSKirk McKusick 					name, sblock.fs_avgfilesize, fvalue);
269a61ab64aSKirk McKusick 			sblock.fs_avgfilesize = fvalue;
270a61ab64aSKirk McKusick 		}
271a61ab64aSKirk McKusick 	}
27277edab90SPhilippe Charnier 	if (mflag) {
2738fae3551SRodney W. Grimes 		name = "minimum percentage of free space";
27477edab90SPhilippe Charnier 		if (sblock.fs_minfree == mvalue) {
27577edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d%%", name, mvalue);
2762af14b60SPhilippe Charnier 		}
27777edab90SPhilippe Charnier 		else {
2788fae3551SRodney W. Grimes 			warnx("%s changes from %d%% to %d%%",
27977edab90SPhilippe Charnier 				    name, sblock.fs_minfree, mvalue);
28077edab90SPhilippe Charnier 			sblock.fs_minfree = mvalue;
28177edab90SPhilippe Charnier 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
2828fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
28377edab90SPhilippe Charnier 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
2848fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
285b1897c19SJulian Elischer 		}
28677edab90SPhilippe Charnier 	}
28777edab90SPhilippe Charnier 	if (nflag) {
28877edab90SPhilippe Charnier  		name = "soft updates";
28977edab90SPhilippe Charnier  		if (strcmp(nvalue, "enable") == 0) {
29077edab90SPhilippe Charnier 			if (sblock.fs_flags & FS_DOSOFTDEP) {
29177edab90SPhilippe Charnier 				warnx("%s remains unchanged as enabled", name);
2921c2665d8SKirk McKusick 			} else if (sblock.fs_clean == 0) {
2931c2665d8SKirk McKusick 				warnx("%s cannot be enabled until fsck is run",
2941c2665d8SKirk McKusick 				    name);
29577edab90SPhilippe Charnier 			} else {
29677edab90SPhilippe Charnier  				sblock.fs_flags |= FS_DOSOFTDEP;
29777edab90SPhilippe Charnier  				warnx("%s set", name);
29877edab90SPhilippe Charnier 			}
29977edab90SPhilippe Charnier  		} else if (strcmp(nvalue, "disable") == 0) {
30077edab90SPhilippe Charnier 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
30177edab90SPhilippe Charnier 				warnx("%s remains unchanged as disabled", name);
30277edab90SPhilippe Charnier 			} else {
30377edab90SPhilippe Charnier  				sblock.fs_flags &= ~FS_DOSOFTDEP;
30477edab90SPhilippe Charnier  				warnx("%s cleared", name);
30577edab90SPhilippe Charnier 			}
30677edab90SPhilippe Charnier  		}
30777edab90SPhilippe Charnier 	}
30877edab90SPhilippe Charnier 	if (oflag) {
3098fae3551SRodney W. Grimes 		name = "optimization preference";
3108fae3551SRodney W. Grimes 		chg[FS_OPTSPACE] = "space";
3118fae3551SRodney W. Grimes 		chg[FS_OPTTIME] = "time";
31277edab90SPhilippe Charnier 		if (sblock.fs_optim == ovalue) {
31377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
3148fae3551SRodney W. Grimes 		}
31577edab90SPhilippe Charnier 		else {
3168fae3551SRodney W. Grimes 			warnx("%s changes from %s to %s",
31777edab90SPhilippe Charnier 				    name, chg[sblock.fs_optim], chg[ovalue]);
31877edab90SPhilippe Charnier 			sblock.fs_optim = ovalue;
31977edab90SPhilippe Charnier 			if (sblock.fs_minfree >= MINFREE &&
32077edab90SPhilippe Charnier 					ovalue == FS_OPTSPACE)
3218fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
32277edab90SPhilippe Charnier 			if (sblock.fs_minfree < MINFREE &&
32377edab90SPhilippe Charnier 					ovalue == FS_OPTTIME)
3248fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
3258fae3551SRodney W. Grimes 		}
32677edab90SPhilippe Charnier 	}
327a61ab64aSKirk McKusick 	if (sflag) {
328a61ab64aSKirk McKusick 		name = "expected number of files per directory";
329a61ab64aSKirk McKusick 		if (sblock.fs_avgfpdir == svalue) {
330a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, svalue);
331a61ab64aSKirk McKusick 		}
332a61ab64aSKirk McKusick 		else {
333a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
334a61ab64aSKirk McKusick 					name, sblock.fs_avgfpdir, svalue);
335a61ab64aSKirk McKusick 			sblock.fs_avgfpdir = svalue;
336a61ab64aSKirk McKusick 		}
337a61ab64aSKirk McKusick 	}
3382af14b60SPhilippe Charnier 
339060ac658SSheldon Hearn 	putsb(&sblock, special, Aflag);
3407382c45aSLuoqi Chen 	if (active) {
3417382c45aSLuoqi Chen 		bzero(&args, sizeof(args));
3427382c45aSLuoqi Chen 		if (mount("ufs", fs->fs_file,
3437382c45aSLuoqi Chen 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
3447382c45aSLuoqi Chen 			err(9, "%s: reload", special);
3457382c45aSLuoqi Chen 		warnx("file system reloaded");
3467382c45aSLuoqi Chen 	}
3478fae3551SRodney W. Grimes 	exit(0);
3488fae3551SRodney W. Grimes }
3498fae3551SRodney W. Grimes 
3508fae3551SRodney W. Grimes void
3518fae3551SRodney W. Grimes usage()
3528fae3551SRodney W. Grimes {
3538679b1b4SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n%s\n",
354a61ab64aSKirk McKusick "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
355a61ab64aSKirk McKusick "              [-m minfree] [-p] [-n enable | disable] [-o space | time]",
356a61ab64aSKirk McKusick "              [-s filesperdir] special | filesystem");
3578fae3551SRodney W. Grimes 	exit(2);
3588fae3551SRodney W. Grimes }
3598fae3551SRodney W. Grimes 
3608fae3551SRodney W. Grimes void
3618fae3551SRodney W. Grimes getsb(fs, file)
362c33fa91fSDima Dorfman 	struct fs *fs;
363c33fa91fSDima Dorfman 	const char *file;
3648fae3551SRodney W. Grimes {
3658fae3551SRodney W. Grimes 
366060ac658SSheldon Hearn 	fi = open(file, O_RDONLY);
3678fae3551SRodney W. Grimes 	if (fi < 0)
3688fae3551SRodney W. Grimes 		err(3, "cannot open %s", file);
3698fae3551SRodney W. Grimes 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
3708fae3551SRodney W. Grimes 		err(4, "%s: bad super block", file);
3718fae3551SRodney W. Grimes 	if (fs->fs_magic != FS_MAGIC)
3728fae3551SRodney W. Grimes 		err(5, "%s: bad magic number", file);
3738fae3551SRodney W. Grimes 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
3748fae3551SRodney W. Grimes }
3758fae3551SRodney W. Grimes 
3768fae3551SRodney W. Grimes void
377060ac658SSheldon Hearn putsb(fs, file, all)
378c33fa91fSDima Dorfman 	struct fs *fs;
379c33fa91fSDima Dorfman 	const char *file;
380060ac658SSheldon Hearn 	int all;
381060ac658SSheldon Hearn {
382060ac658SSheldon Hearn 	int i;
383060ac658SSheldon Hearn 
384060ac658SSheldon Hearn 	/*
385060ac658SSheldon Hearn 	 * Re-open the device read-write. Use the read-only file
386060ac658SSheldon Hearn 	 * descriptor as an interlock to prevent the device from
387060ac658SSheldon Hearn 	 * being mounted while we are switching mode.
388060ac658SSheldon Hearn 	 */
389060ac658SSheldon Hearn 	i = fi;
390060ac658SSheldon Hearn 	fi = open(file, O_RDWR);
391060ac658SSheldon Hearn 	close(i);
392060ac658SSheldon Hearn 	if (fi < 0)
393060ac658SSheldon Hearn 		err(3, "cannot open %s", file);
394c33fa91fSDima Dorfman 	bwrite((daddr_t)SBOFF / dev_bsize, (const char *)fs, SBSIZE);
395060ac658SSheldon Hearn 	if (all)
396060ac658SSheldon Hearn 		for (i = 0; i < fs->fs_ncg; i++)
397060ac658SSheldon Hearn 			bwrite(fsbtodb(fs, cgsblock(fs, i)),
398c33fa91fSDima Dorfman 			    (const char *)fs, SBSIZE);
399060ac658SSheldon Hearn 	close(fi);
400060ac658SSheldon Hearn }
401060ac658SSheldon Hearn 
402060ac658SSheldon Hearn void
40316a7269eSJoerg Wunsch printfs()
40416a7269eSJoerg Wunsch {
405b1897c19SJulian Elischer 	warnx("soft updates:  (-n)                                %s",
406b1897c19SJulian Elischer 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
40716a7269eSJoerg Wunsch 	warnx("maximum contiguous block count: (-a)               %d",
40816a7269eSJoerg Wunsch 	      sblock.fs_maxcontig);
40916a7269eSJoerg Wunsch 	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
41016a7269eSJoerg Wunsch 	      sblock.fs_rotdelay);
41116a7269eSJoerg Wunsch 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
41216a7269eSJoerg Wunsch 	      sblock.fs_maxbpg);
413a61ab64aSKirk McKusick 	warnx("average file size: (-f)                            %d",
414a61ab64aSKirk McKusick 	      sblock.fs_avgfilesize);
415a61ab64aSKirk McKusick 	warnx("average number of files in a directory: (-s)       %d",
416a61ab64aSKirk McKusick 	      sblock.fs_avgfpdir);
41716a7269eSJoerg Wunsch 	warnx("minimum percentage of free space: (-m)             %d%%",
41816a7269eSJoerg Wunsch 	      sblock.fs_minfree);
41916a7269eSJoerg Wunsch 	warnx("optimization preference: (-o)                      %s",
42016a7269eSJoerg Wunsch 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
42116a7269eSJoerg Wunsch 	if (sblock.fs_minfree >= MINFREE &&
42216a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTSPACE)
42316a7269eSJoerg Wunsch 		warnx(OPTWARN, "time", ">=", MINFREE);
42416a7269eSJoerg Wunsch 	if (sblock.fs_minfree < MINFREE &&
42516a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTTIME)
42616a7269eSJoerg Wunsch 		warnx(OPTWARN, "space", "<", MINFREE);
42716a7269eSJoerg Wunsch }
42816a7269eSJoerg Wunsch 
42916a7269eSJoerg Wunsch void
4308fae3551SRodney W. Grimes bwrite(blk, buf, size)
4318fae3551SRodney W. Grimes 	daddr_t blk;
432c33fa91fSDima Dorfman 	const char *buf;
4338fae3551SRodney W. Grimes 	int size;
4348fae3551SRodney W. Grimes {
4358fae3551SRodney W. Grimes 
4368fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
4378fae3551SRodney W. Grimes 		err(6, "FS SEEK");
4388fae3551SRodney W. Grimes 	if (write(fi, buf, size) != size)
4398fae3551SRodney W. Grimes 		err(7, "FS WRITE");
4408fae3551SRodney W. Grimes }
4418fae3551SRodney W. Grimes 
4428fae3551SRodney W. Grimes int
4438fae3551SRodney W. Grimes bread(bno, buf, cnt)
4448fae3551SRodney W. Grimes 	daddr_t bno;
4458fae3551SRodney W. Grimes 	char *buf;
4468fae3551SRodney W. Grimes 	int cnt;
4478fae3551SRodney W. Grimes {
4488fae3551SRodney W. Grimes 	int i;
4498fae3551SRodney W. Grimes 
4508fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
4518fae3551SRodney W. Grimes 		return(1);
4528fae3551SRodney W. Grimes 	if ((i = read(fi, buf, cnt)) != cnt) {
4538fae3551SRodney W. Grimes 		for(i=0; i<sblock.fs_bsize; i++)
4548fae3551SRodney W. Grimes 			buf[i] = 0;
4558fae3551SRodney W. Grimes 		return (1);
4568fae3551SRodney W. Grimes 	}
4578fae3551SRodney W. Grimes 	return (0);
4588fae3551SRodney W. Grimes }
459