xref: /freebsd/sbin/tunefs/tunefs.c (revision 75766e179d86dc47c602150fec8e95ba40d9abd5)
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>
5375766e17SPoul-Henning Kamp #include <sys/disklabel.h>
548fae3551SRodney W. Grimes #include <sys/stat.h>
558fae3551SRodney W. Grimes 
568fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
577382c45aSLuoqi Chen #include <ufs/ufs/ufsmount.h>
588fae3551SRodney W. Grimes 
598fae3551SRodney W. Grimes #include <err.h>
608fae3551SRodney W. Grimes #include <fcntl.h>
618fae3551SRodney W. Grimes #include <fstab.h>
628fae3551SRodney W. Grimes #include <paths.h>
638679b1b4SPhilippe Charnier #include <stdio.h>
648fae3551SRodney W. Grimes #include <stdlib.h>
65060ac658SSheldon Hearn #include <string.h>
668fae3551SRodney W. Grimes #include <unistd.h>
678fae3551SRodney W. Grimes 
688fae3551SRodney W. Grimes /* the optimization warning string template */
698fae3551SRodney W. Grimes #define	OPTWARN	"should optimize for %s with minfree %s %d%%"
708fae3551SRodney W. Grimes 
718fae3551SRodney W. Grimes union {
728fae3551SRodney W. Grimes 	struct	fs sb;
738fae3551SRodney W. Grimes 	char pad[MAXBSIZE];
748fae3551SRodney W. Grimes } sbun;
758fae3551SRodney W. Grimes #define	sblock sbun.sb
768fae3551SRodney W. Grimes 
778fae3551SRodney W. Grimes int fi;
788fae3551SRodney W. Grimes long dev_bsize = 1;
798fae3551SRodney W. Grimes 
80d476a036SWarner Losh void bwrite(daddr_t, const char *, int);
81d476a036SWarner Losh int bread(daddr_t, char *, int);
82d476a036SWarner Losh void getsb(struct fs *, const char *);
83d476a036SWarner Losh void putsb(struct fs *, const char *, int);
84d476a036SWarner Losh void usage(void);
85d476a036SWarner Losh void printfs(void);
868fae3551SRodney W. Grimes 
878fae3551SRodney W. Grimes int
888fae3551SRodney W. Grimes main(argc, argv)
898fae3551SRodney W. Grimes 	int argc;
908fae3551SRodney W. Grimes 	char *argv[];
918fae3551SRodney W. Grimes {
92c33fa91fSDima Dorfman 	char *special;
93c33fa91fSDima Dorfman 	const char *name;
948fae3551SRodney W. Grimes 	struct stat st;
957382c45aSLuoqi Chen 	int Aflag = 0, active = 0;
96a61ab64aSKirk McKusick 	int aflag = 0, dflag = 0, eflag = 0, fflag = 0, mflag = 0;
97a61ab64aSKirk McKusick 	int nflag = 0, oflag = 0, pflag = 0, sflag = 0;
98a61ab64aSKirk McKusick 	int avalue = 0, dvalue = 0, evalue = 0, fvalue = 0;
99a61ab64aSKirk McKusick 	int mvalue = 0, ovalue = 0, svalue = 0;
10077edab90SPhilippe Charnier 	char *nvalue = NULL;
1018fae3551SRodney W. Grimes 	struct fstab *fs;
102c33fa91fSDima Dorfman 	const char *chg[2];
103c33fa91fSDima Dorfman 	char device[MAXPATHLEN];
1047382c45aSLuoqi Chen 	struct ufs_args args;
1057382c45aSLuoqi Chen 	struct statfs stfs;
1062af14b60SPhilippe Charnier 	int found_arg, ch;
1078fae3551SRodney W. Grimes 
1082af14b60SPhilippe Charnier         if (argc < 3)
1098fae3551SRodney W. Grimes                 usage();
11077edab90SPhilippe Charnier 	found_arg = 0; /* at least one arg is required */
111a61ab64aSKirk McKusick 	while ((ch = getopt(argc, argv, "Aa:d:e:f:m:n:o:ps:")) != -1)
11277edab90SPhilippe Charnier 	  switch (ch) {
11377edab90SPhilippe Charnier 	  case 'A':
11477edab90SPhilippe Charnier 		found_arg = 1;
11577edab90SPhilippe Charnier 		Aflag++;
11677edab90SPhilippe Charnier 		break;
11777edab90SPhilippe Charnier 	  case 'a':
11877edab90SPhilippe Charnier 		found_arg = 1;
11977edab90SPhilippe Charnier 		name = "maximum contiguous block count";
12077edab90SPhilippe Charnier 		avalue = atoi(optarg);
12177edab90SPhilippe Charnier 		if (avalue < 1)
12277edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
12377edab90SPhilippe Charnier 		aflag = 1;
12477edab90SPhilippe Charnier 		break;
12577edab90SPhilippe Charnier 	  case 'd':
12677edab90SPhilippe Charnier 		found_arg = 1;
12777edab90SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
12877edab90SPhilippe Charnier 		dvalue = atoi(optarg);
12977edab90SPhilippe Charnier 		dflag = 1;
13077edab90SPhilippe Charnier 		break;
13177edab90SPhilippe Charnier 	  case 'e':
13277edab90SPhilippe Charnier 		found_arg = 1;
13377edab90SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
13477edab90SPhilippe Charnier 		evalue = atoi(optarg);
13577edab90SPhilippe Charnier 		if (evalue < 1)
13677edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
13777edab90SPhilippe Charnier 		eflag = 1;
13877edab90SPhilippe Charnier 		break;
139a61ab64aSKirk McKusick 	  case 'f':
140a61ab64aSKirk McKusick 		found_arg = 1;
141a61ab64aSKirk McKusick 		name = "average file size";
142a61ab64aSKirk McKusick 		fvalue = atoi(optarg);
143a61ab64aSKirk McKusick 		if (fvalue < 1)
144a61ab64aSKirk McKusick 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
145a61ab64aSKirk McKusick 		fflag = 1;
146a61ab64aSKirk McKusick 		break;
14777edab90SPhilippe Charnier 	  case 'm':
14877edab90SPhilippe Charnier 		found_arg = 1;
14977edab90SPhilippe Charnier 		name = "minimum percentage of free space";
15077edab90SPhilippe Charnier 		mvalue = atoi(optarg);
15177edab90SPhilippe Charnier 		if (mvalue < 0 || mvalue > 99)
15277edab90SPhilippe Charnier 			errx(10, "bad %s (%s)", name, optarg);
15377edab90SPhilippe Charnier 		mflag = 1;
15477edab90SPhilippe Charnier 		break;
15577edab90SPhilippe Charnier 	  case 'n':
15677edab90SPhilippe Charnier 		found_arg = 1;
15777edab90SPhilippe Charnier  		name = "soft updates";
15877edab90SPhilippe Charnier  		nvalue = optarg;
15977edab90SPhilippe Charnier                 if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
16077edab90SPhilippe Charnier  			errx(10, "bad %s (options are %s)",
16177edab90SPhilippe Charnier  			    name, "`enable' or `disable'");
16277edab90SPhilippe Charnier  		}
16377edab90SPhilippe Charnier 		nflag = 1;
16477edab90SPhilippe Charnier  		break;
16577edab90SPhilippe Charnier 	  case 'o':
16677edab90SPhilippe Charnier 		found_arg = 1;
16777edab90SPhilippe Charnier 		name = "optimization preference";
16877edab90SPhilippe Charnier 		chg[FS_OPTSPACE] = "space";
16977edab90SPhilippe Charnier 		chg[FS_OPTTIME] = "time";
17077edab90SPhilippe Charnier 		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
17177edab90SPhilippe Charnier 			ovalue = FS_OPTSPACE;
17277edab90SPhilippe Charnier 		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
17377edab90SPhilippe Charnier 			ovalue = FS_OPTTIME;
17477edab90SPhilippe Charnier 		else
17577edab90SPhilippe Charnier 			errx(10, "bad %s (options are `space' or `time')",
17677edab90SPhilippe Charnier 					    name);
17777edab90SPhilippe Charnier 		oflag = 1;
17877edab90SPhilippe Charnier 		break;
17977edab90SPhilippe Charnier 	  case 'p':
180e50fa3d2SBen Smithurst 		found_arg = 1;
18177edab90SPhilippe Charnier 		pflag = 1;
18277edab90SPhilippe Charnier 		break;
183a61ab64aSKirk McKusick 	  case 's':
184a61ab64aSKirk McKusick 		found_arg = 1;
185a61ab64aSKirk McKusick 		name = "expected number of files per directory";
186a61ab64aSKirk McKusick 		svalue = atoi(optarg);
187a61ab64aSKirk McKusick 		if (svalue < 1)
188a61ab64aSKirk McKusick 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
189a61ab64aSKirk McKusick 		sflag = 1;
190a61ab64aSKirk McKusick 		break;
19177edab90SPhilippe Charnier 	  default:
19277edab90SPhilippe Charnier 		usage();
19377edab90SPhilippe Charnier 	  }
19477edab90SPhilippe Charnier 	argc -= optind;
19577edab90SPhilippe Charnier 	argv += optind;
19677edab90SPhilippe Charnier 
19777edab90SPhilippe Charnier 	if (found_arg == 0 || argc != 1)
19877edab90SPhilippe Charnier 	  usage();
19977edab90SPhilippe Charnier 
20077edab90SPhilippe Charnier 	special = argv[0];
2018fae3551SRodney W. Grimes 	fs = getfsfile(special);
2027382c45aSLuoqi Chen 	if (fs) {
2039ea6e95eSLuoqi Chen 		if (statfs(special, &stfs) == 0 &&
2049ea6e95eSLuoqi Chen 		    strcmp(special, stfs.f_mntonname) == 0) {
2057382c45aSLuoqi Chen 			active = 1;
206b20ae6a0SLuoqi Chen 		}
2078fae3551SRodney W. Grimes 		special = fs->fs_spec;
2087382c45aSLuoqi Chen 	}
2098fae3551SRodney W. Grimes again:
2108fae3551SRodney W. Grimes 	if (stat(special, &st) < 0) {
2118fae3551SRodney W. Grimes 		if (*special != '/') {
2128fae3551SRodney W. Grimes 			if (*special == 'r')
2138fae3551SRodney W. Grimes 				special++;
21447f07d95SIan Dowse 			(void)snprintf(device, sizeof(device), "%s%s",
21555fd28c8SKris Kennaway 				       _PATH_DEV, special);
2168fae3551SRodney W. Grimes 			special = device;
2178fae3551SRodney W. Grimes 			goto again;
2188fae3551SRodney W. Grimes 		}
2198fae3551SRodney W. Grimes 		err(1, "%s", special);
2208fae3551SRodney W. Grimes 	}
22147f07d95SIan Dowse 	if (fs == NULL && (st.st_mode & S_IFMT) == S_IFDIR)
22247f07d95SIan Dowse 		errx(10, "%s: unknown file system", special);
2238fae3551SRodney W. Grimes 	getsb(&sblock, special);
2248fae3551SRodney W. Grimes 
22577edab90SPhilippe Charnier 	if (pflag) {
22677edab90SPhilippe Charnier 		printfs();
22777edab90SPhilippe Charnier 		exit(0);
22877edab90SPhilippe Charnier 	}
22977edab90SPhilippe Charnier 	if (aflag) {
2308fae3551SRodney W. Grimes 		name = "maximum contiguous block count";
23177edab90SPhilippe Charnier 		if (sblock.fs_maxcontig == avalue) {
23277edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, avalue);
2332af14b60SPhilippe Charnier 		}
23477edab90SPhilippe Charnier 		else {
23577edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
23677edab90SPhilippe Charnier 					name, sblock.fs_maxcontig, avalue);
23777edab90SPhilippe Charnier 			sblock.fs_maxcontig = avalue;
23877edab90SPhilippe Charnier 		}
23977edab90SPhilippe Charnier 	}
24077edab90SPhilippe Charnier 	if (dflag) {
2412af14b60SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
24277edab90SPhilippe Charnier 		if (sblock.fs_rotdelay == dvalue) {
24377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %dms", name, dvalue);
2442af14b60SPhilippe Charnier 		}
24577edab90SPhilippe Charnier 		else {
2468fae3551SRodney W. Grimes 			warnx("%s changes from %dms to %dms",
24777edab90SPhilippe Charnier 				    name, sblock.fs_rotdelay, dvalue);
24877edab90SPhilippe Charnier 			sblock.fs_rotdelay = dvalue;
24977edab90SPhilippe Charnier 		}
25077edab90SPhilippe Charnier 	}
25177edab90SPhilippe Charnier 	if (eflag) {
2522af14b60SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
25377edab90SPhilippe Charnier 		if (sblock.fs_maxbpg == evalue) {
25477edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, evalue);
2552af14b60SPhilippe Charnier 		}
25677edab90SPhilippe Charnier 		else {
25777edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
25877edab90SPhilippe Charnier 					name, sblock.fs_maxbpg, evalue);
25977edab90SPhilippe Charnier 			sblock.fs_maxbpg = evalue;
26077edab90SPhilippe Charnier 		}
26177edab90SPhilippe Charnier 	}
262a61ab64aSKirk McKusick 	if (fflag) {
263a61ab64aSKirk McKusick 		name = "average file size";
264a61ab64aSKirk McKusick 		if (sblock.fs_avgfilesize == fvalue) {
265a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, fvalue);
266a61ab64aSKirk McKusick 		}
267a61ab64aSKirk McKusick 		else {
268a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
269a61ab64aSKirk McKusick 					name, sblock.fs_avgfilesize, fvalue);
270a61ab64aSKirk McKusick 			sblock.fs_avgfilesize = fvalue;
271a61ab64aSKirk McKusick 		}
272a61ab64aSKirk McKusick 	}
27377edab90SPhilippe Charnier 	if (mflag) {
2748fae3551SRodney W. Grimes 		name = "minimum percentage of free space";
27577edab90SPhilippe Charnier 		if (sblock.fs_minfree == mvalue) {
27677edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d%%", name, mvalue);
2772af14b60SPhilippe Charnier 		}
27877edab90SPhilippe Charnier 		else {
2798fae3551SRodney W. Grimes 			warnx("%s changes from %d%% to %d%%",
28077edab90SPhilippe Charnier 				    name, sblock.fs_minfree, mvalue);
28177edab90SPhilippe Charnier 			sblock.fs_minfree = mvalue;
28277edab90SPhilippe Charnier 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
2838fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
28477edab90SPhilippe Charnier 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
2858fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
286b1897c19SJulian Elischer 		}
28777edab90SPhilippe Charnier 	}
28877edab90SPhilippe Charnier 	if (nflag) {
28977edab90SPhilippe Charnier  		name = "soft updates";
29077edab90SPhilippe Charnier  		if (strcmp(nvalue, "enable") == 0) {
29177edab90SPhilippe Charnier 			if (sblock.fs_flags & FS_DOSOFTDEP) {
29277edab90SPhilippe Charnier 				warnx("%s remains unchanged as enabled", name);
2931c2665d8SKirk McKusick 			} else if (sblock.fs_clean == 0) {
2941c2665d8SKirk McKusick 				warnx("%s cannot be enabled until fsck is run",
2951c2665d8SKirk McKusick 				    name);
29677edab90SPhilippe Charnier 			} else {
29777edab90SPhilippe Charnier  				sblock.fs_flags |= FS_DOSOFTDEP;
29877edab90SPhilippe Charnier  				warnx("%s set", name);
29977edab90SPhilippe Charnier 			}
30077edab90SPhilippe Charnier  		} else if (strcmp(nvalue, "disable") == 0) {
30177edab90SPhilippe Charnier 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
30277edab90SPhilippe Charnier 				warnx("%s remains unchanged as disabled", name);
30377edab90SPhilippe Charnier 			} else {
30477edab90SPhilippe Charnier  				sblock.fs_flags &= ~FS_DOSOFTDEP;
30577edab90SPhilippe Charnier  				warnx("%s cleared", name);
30677edab90SPhilippe Charnier 			}
30777edab90SPhilippe Charnier  		}
30877edab90SPhilippe Charnier 	}
30977edab90SPhilippe Charnier 	if (oflag) {
3108fae3551SRodney W. Grimes 		name = "optimization preference";
3118fae3551SRodney W. Grimes 		chg[FS_OPTSPACE] = "space";
3128fae3551SRodney W. Grimes 		chg[FS_OPTTIME] = "time";
31377edab90SPhilippe Charnier 		if (sblock.fs_optim == ovalue) {
31477edab90SPhilippe Charnier 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
3158fae3551SRodney W. Grimes 		}
31677edab90SPhilippe Charnier 		else {
3178fae3551SRodney W. Grimes 			warnx("%s changes from %s to %s",
31877edab90SPhilippe Charnier 				    name, chg[sblock.fs_optim], chg[ovalue]);
31977edab90SPhilippe Charnier 			sblock.fs_optim = ovalue;
32077edab90SPhilippe Charnier 			if (sblock.fs_minfree >= MINFREE &&
32177edab90SPhilippe Charnier 					ovalue == FS_OPTSPACE)
3228fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
32377edab90SPhilippe Charnier 			if (sblock.fs_minfree < MINFREE &&
32477edab90SPhilippe Charnier 					ovalue == FS_OPTTIME)
3258fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
3268fae3551SRodney W. Grimes 		}
32777edab90SPhilippe Charnier 	}
328a61ab64aSKirk McKusick 	if (sflag) {
329a61ab64aSKirk McKusick 		name = "expected number of files per directory";
330a61ab64aSKirk McKusick 		if (sblock.fs_avgfpdir == svalue) {
331a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, svalue);
332a61ab64aSKirk McKusick 		}
333a61ab64aSKirk McKusick 		else {
334a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
335a61ab64aSKirk McKusick 					name, sblock.fs_avgfpdir, svalue);
336a61ab64aSKirk McKusick 			sblock.fs_avgfpdir = svalue;
337a61ab64aSKirk McKusick 		}
338a61ab64aSKirk McKusick 	}
3392af14b60SPhilippe Charnier 
340060ac658SSheldon Hearn 	putsb(&sblock, special, Aflag);
3417382c45aSLuoqi Chen 	if (active) {
3427382c45aSLuoqi Chen 		bzero(&args, sizeof(args));
3437382c45aSLuoqi Chen 		if (mount("ufs", fs->fs_file,
3447382c45aSLuoqi Chen 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
3457382c45aSLuoqi Chen 			err(9, "%s: reload", special);
3467382c45aSLuoqi Chen 		warnx("file system reloaded");
3477382c45aSLuoqi Chen 	}
3488fae3551SRodney W. Grimes 	exit(0);
3498fae3551SRodney W. Grimes }
3508fae3551SRodney W. Grimes 
3518fae3551SRodney W. Grimes void
3528fae3551SRodney W. Grimes usage()
3538fae3551SRodney W. Grimes {
3548679b1b4SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n%s\n",
355a61ab64aSKirk McKusick "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
356a61ab64aSKirk McKusick "              [-m minfree] [-p] [-n enable | disable] [-o space | time]",
357a61ab64aSKirk McKusick "              [-s filesperdir] special | filesystem");
3588fae3551SRodney W. Grimes 	exit(2);
3598fae3551SRodney W. Grimes }
3608fae3551SRodney W. Grimes 
3618fae3551SRodney W. Grimes void
3628fae3551SRodney W. Grimes getsb(fs, file)
363c33fa91fSDima Dorfman 	struct fs *fs;
364c33fa91fSDima Dorfman 	const char *file;
3658fae3551SRodney W. Grimes {
3668fae3551SRodney W. Grimes 
367060ac658SSheldon Hearn 	fi = open(file, O_RDONLY);
3688fae3551SRodney W. Grimes 	if (fi < 0)
3698fae3551SRodney W. Grimes 		err(3, "cannot open %s", file);
3708fae3551SRodney W. Grimes 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
3718fae3551SRodney W. Grimes 		err(4, "%s: bad super block", file);
3728fae3551SRodney W. Grimes 	if (fs->fs_magic != FS_MAGIC)
37347f07d95SIan Dowse 		errx(5, "%s: bad magic number", file);
3748fae3551SRodney W. Grimes 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
3758fae3551SRodney W. Grimes }
3768fae3551SRodney W. Grimes 
3778fae3551SRodney W. Grimes void
378060ac658SSheldon Hearn putsb(fs, file, all)
379c33fa91fSDima Dorfman 	struct fs *fs;
380c33fa91fSDima Dorfman 	const char *file;
381060ac658SSheldon Hearn 	int all;
382060ac658SSheldon Hearn {
383060ac658SSheldon Hearn 	int i;
384060ac658SSheldon Hearn 
385060ac658SSheldon Hearn 	/*
386060ac658SSheldon Hearn 	 * Re-open the device read-write. Use the read-only file
387060ac658SSheldon Hearn 	 * descriptor as an interlock to prevent the device from
388060ac658SSheldon Hearn 	 * being mounted while we are switching mode.
389060ac658SSheldon Hearn 	 */
390060ac658SSheldon Hearn 	i = fi;
391060ac658SSheldon Hearn 	fi = open(file, O_RDWR);
392060ac658SSheldon Hearn 	close(i);
393060ac658SSheldon Hearn 	if (fi < 0)
394060ac658SSheldon Hearn 		err(3, "cannot open %s", file);
395c33fa91fSDima Dorfman 	bwrite((daddr_t)SBOFF / dev_bsize, (const char *)fs, SBSIZE);
396060ac658SSheldon Hearn 	if (all)
397060ac658SSheldon Hearn 		for (i = 0; i < fs->fs_ncg; i++)
398060ac658SSheldon Hearn 			bwrite(fsbtodb(fs, cgsblock(fs, i)),
399c33fa91fSDima Dorfman 			    (const char *)fs, SBSIZE);
400060ac658SSheldon Hearn 	close(fi);
401060ac658SSheldon Hearn }
402060ac658SSheldon Hearn 
403060ac658SSheldon Hearn void
40416a7269eSJoerg Wunsch printfs()
40516a7269eSJoerg Wunsch {
406b1897c19SJulian Elischer 	warnx("soft updates:  (-n)                                %s",
407b1897c19SJulian Elischer 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
40816a7269eSJoerg Wunsch 	warnx("maximum contiguous block count: (-a)               %d",
40916a7269eSJoerg Wunsch 	      sblock.fs_maxcontig);
41016a7269eSJoerg Wunsch 	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
41116a7269eSJoerg Wunsch 	      sblock.fs_rotdelay);
41216a7269eSJoerg Wunsch 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
41316a7269eSJoerg Wunsch 	      sblock.fs_maxbpg);
414a61ab64aSKirk McKusick 	warnx("average file size: (-f)                            %d",
415a61ab64aSKirk McKusick 	      sblock.fs_avgfilesize);
416a61ab64aSKirk McKusick 	warnx("average number of files in a directory: (-s)       %d",
417a61ab64aSKirk McKusick 	      sblock.fs_avgfpdir);
41816a7269eSJoerg Wunsch 	warnx("minimum percentage of free space: (-m)             %d%%",
41916a7269eSJoerg Wunsch 	      sblock.fs_minfree);
42016a7269eSJoerg Wunsch 	warnx("optimization preference: (-o)                      %s",
42116a7269eSJoerg Wunsch 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
42216a7269eSJoerg Wunsch 	if (sblock.fs_minfree >= MINFREE &&
42316a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTSPACE)
42416a7269eSJoerg Wunsch 		warnx(OPTWARN, "time", ">=", MINFREE);
42516a7269eSJoerg Wunsch 	if (sblock.fs_minfree < MINFREE &&
42616a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTTIME)
42716a7269eSJoerg Wunsch 		warnx(OPTWARN, "space", "<", MINFREE);
42816a7269eSJoerg Wunsch }
42916a7269eSJoerg Wunsch 
43016a7269eSJoerg Wunsch void
4318fae3551SRodney W. Grimes bwrite(blk, buf, size)
4328fae3551SRodney W. Grimes 	daddr_t blk;
433c33fa91fSDima Dorfman 	const char *buf;
4348fae3551SRodney W. Grimes 	int size;
4358fae3551SRodney W. Grimes {
4368fae3551SRodney W. Grimes 
4378fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
4388fae3551SRodney W. Grimes 		err(6, "FS SEEK");
4398fae3551SRodney W. Grimes 	if (write(fi, buf, size) != size)
4408fae3551SRodney W. Grimes 		err(7, "FS WRITE");
4418fae3551SRodney W. Grimes }
4428fae3551SRodney W. Grimes 
4438fae3551SRodney W. Grimes int
4448fae3551SRodney W. Grimes bread(bno, buf, cnt)
4458fae3551SRodney W. Grimes 	daddr_t bno;
4468fae3551SRodney W. Grimes 	char *buf;
4478fae3551SRodney W. Grimes 	int cnt;
4488fae3551SRodney W. Grimes {
4498fae3551SRodney W. Grimes 	int i;
4508fae3551SRodney W. Grimes 
4518fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
4528fae3551SRodney W. Grimes 		return(1);
4538fae3551SRodney W. Grimes 	if ((i = read(fi, buf, cnt)) != cnt) {
4548fae3551SRodney W. Grimes 		for(i=0; i<sblock.fs_bsize; i++)
4558fae3551SRodney W. Grimes 			buf[i] = 0;
4568fae3551SRodney W. Grimes 		return (1);
4578fae3551SRodney W. Grimes 	}
4588fae3551SRodney W. Grimes 	return (0);
4598fae3551SRodney W. Grimes }
460