xref: /freebsd/sbin/tunefs/tunefs.c (revision 1c2665d807e7cb19b790a9917bbce7003178c39c)
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 
79060ac658SSheldon Hearn void bwrite __P((daddr_t, char *, int));
80060ac658SSheldon Hearn int bread __P((daddr_t, char *, int));
81060ac658SSheldon Hearn void getsb __P((struct fs *, char *));
82060ac658SSheldon Hearn void putsb __P((struct fs *, 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 {
9177edab90SPhilippe Charnier 	char *special, *name;
928fae3551SRodney W. Grimes 	struct stat st;
937382c45aSLuoqi Chen 	int Aflag = 0, active = 0;
94a61ab64aSKirk McKusick 	int aflag = 0, dflag = 0, eflag = 0, fflag = 0, mflag = 0;
95a61ab64aSKirk McKusick 	int nflag = 0, oflag = 0, pflag = 0, sflag = 0;
96a61ab64aSKirk McKusick 	int avalue = 0, dvalue = 0, evalue = 0, fvalue = 0;
97a61ab64aSKirk McKusick 	int mvalue = 0, ovalue = 0, svalue = 0;
9877edab90SPhilippe Charnier 	char *nvalue = NULL;
998fae3551SRodney W. Grimes 	struct fstab *fs;
1008fae3551SRodney W. Grimes 	char *chg[2], device[MAXPATHLEN];
1017382c45aSLuoqi Chen 	struct ufs_args args;
1027382c45aSLuoqi Chen 	struct statfs stfs;
1032af14b60SPhilippe Charnier 	int found_arg, ch;
1048fae3551SRodney W. Grimes 
1052af14b60SPhilippe Charnier         if (argc < 3)
1068fae3551SRodney W. Grimes                 usage();
10777edab90SPhilippe Charnier 	found_arg = 0; /* at least one arg is required */
108a61ab64aSKirk McKusick 	while ((ch = getopt(argc, argv, "Aa:d:e:f:m:n:o:ps:")) != -1)
10977edab90SPhilippe Charnier 	  switch (ch) {
11077edab90SPhilippe Charnier 	  case 'A':
11177edab90SPhilippe Charnier 		found_arg = 1;
11277edab90SPhilippe Charnier 		Aflag++;
11377edab90SPhilippe Charnier 		break;
11477edab90SPhilippe Charnier 	  case 'a':
11577edab90SPhilippe Charnier 		found_arg = 1;
11677edab90SPhilippe Charnier 		name = "maximum contiguous block count";
11777edab90SPhilippe Charnier 		avalue = atoi(optarg);
11877edab90SPhilippe Charnier 		if (avalue < 1)
11977edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
12077edab90SPhilippe Charnier 		aflag = 1;
12177edab90SPhilippe Charnier 		break;
12277edab90SPhilippe Charnier 	  case 'd':
12377edab90SPhilippe Charnier 		found_arg = 1;
12477edab90SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
12577edab90SPhilippe Charnier 		dvalue = atoi(optarg);
12677edab90SPhilippe Charnier 		dflag = 1;
12777edab90SPhilippe Charnier 		break;
12877edab90SPhilippe Charnier 	  case 'e':
12977edab90SPhilippe Charnier 		found_arg = 1;
13077edab90SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
13177edab90SPhilippe Charnier 		evalue = atoi(optarg);
13277edab90SPhilippe Charnier 		if (evalue < 1)
13377edab90SPhilippe Charnier 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
13477edab90SPhilippe Charnier 		eflag = 1;
13577edab90SPhilippe Charnier 		break;
136a61ab64aSKirk McKusick 	  case 'f':
137a61ab64aSKirk McKusick 		found_arg = 1;
138a61ab64aSKirk McKusick 		name = "average file size";
139a61ab64aSKirk McKusick 		fvalue = atoi(optarg);
140a61ab64aSKirk McKusick 		if (fvalue < 1)
141a61ab64aSKirk McKusick 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
142a61ab64aSKirk McKusick 		fflag = 1;
143a61ab64aSKirk McKusick 		break;
14477edab90SPhilippe Charnier 	  case 'm':
14577edab90SPhilippe Charnier 		found_arg = 1;
14677edab90SPhilippe Charnier 		name = "minimum percentage of free space";
14777edab90SPhilippe Charnier 		mvalue = atoi(optarg);
14877edab90SPhilippe Charnier 		if (mvalue < 0 || mvalue > 99)
14977edab90SPhilippe Charnier 			errx(10, "bad %s (%s)", name, optarg);
15077edab90SPhilippe Charnier 		mflag = 1;
15177edab90SPhilippe Charnier 		break;
15277edab90SPhilippe Charnier 	  case 'n':
15377edab90SPhilippe Charnier 		found_arg = 1;
15477edab90SPhilippe Charnier  		name = "soft updates";
15577edab90SPhilippe Charnier  		nvalue = optarg;
15677edab90SPhilippe Charnier                 if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
15777edab90SPhilippe Charnier  			errx(10, "bad %s (options are %s)",
15877edab90SPhilippe Charnier  			    name, "`enable' or `disable'");
15977edab90SPhilippe Charnier  		}
16077edab90SPhilippe Charnier 		nflag = 1;
16177edab90SPhilippe Charnier  		break;
16277edab90SPhilippe Charnier 	  case 'o':
16377edab90SPhilippe Charnier 		found_arg = 1;
16477edab90SPhilippe Charnier 		name = "optimization preference";
16577edab90SPhilippe Charnier 		chg[FS_OPTSPACE] = "space";
16677edab90SPhilippe Charnier 		chg[FS_OPTTIME] = "time";
16777edab90SPhilippe Charnier 		if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
16877edab90SPhilippe Charnier 			ovalue = FS_OPTSPACE;
16977edab90SPhilippe Charnier 		else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
17077edab90SPhilippe Charnier 			ovalue = FS_OPTTIME;
17177edab90SPhilippe Charnier 		else
17277edab90SPhilippe Charnier 			errx(10, "bad %s (options are `space' or `time')",
17377edab90SPhilippe Charnier 					    name);
17477edab90SPhilippe Charnier 		oflag = 1;
17577edab90SPhilippe Charnier 		break;
17677edab90SPhilippe Charnier 	  case 'p':
177e50fa3d2SBen Smithurst 		found_arg = 1;
17877edab90SPhilippe Charnier 		pflag = 1;
17977edab90SPhilippe Charnier 		break;
180a61ab64aSKirk McKusick 	  case 's':
181a61ab64aSKirk McKusick 		found_arg = 1;
182a61ab64aSKirk McKusick 		name = "expected number of files per directory";
183a61ab64aSKirk McKusick 		svalue = atoi(optarg);
184a61ab64aSKirk McKusick 		if (svalue < 1)
185a61ab64aSKirk McKusick 			errx(10, "%s must be >= 1 (was %s)", name, optarg);
186a61ab64aSKirk McKusick 		sflag = 1;
187a61ab64aSKirk McKusick 		break;
18877edab90SPhilippe Charnier 	  default:
18977edab90SPhilippe Charnier 		usage();
19077edab90SPhilippe Charnier 	  }
19177edab90SPhilippe Charnier 	argc -= optind;
19277edab90SPhilippe Charnier 	argv += optind;
19377edab90SPhilippe Charnier 
19477edab90SPhilippe Charnier 	if (found_arg == 0 || argc != 1)
19577edab90SPhilippe Charnier 	  usage();
19677edab90SPhilippe Charnier 
19777edab90SPhilippe Charnier 	special = argv[0];
1988fae3551SRodney W. Grimes 	fs = getfsfile(special);
1997382c45aSLuoqi Chen 	if (fs) {
2009ea6e95eSLuoqi Chen 		if (statfs(special, &stfs) == 0 &&
2019ea6e95eSLuoqi Chen 		    strcmp(special, stfs.f_mntonname) == 0) {
2027382c45aSLuoqi Chen 			active = 1;
203b20ae6a0SLuoqi Chen 		}
2048fae3551SRodney W. Grimes 		special = fs->fs_spec;
2057382c45aSLuoqi Chen 	}
2068fae3551SRodney W. Grimes again:
2078fae3551SRodney W. Grimes 	if (stat(special, &st) < 0) {
2088fae3551SRodney W. Grimes 		if (*special != '/') {
2098fae3551SRodney W. Grimes 			if (*special == 'r')
2108fae3551SRodney W. Grimes 				special++;
2118fae3551SRodney W. Grimes 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
2128fae3551SRodney W. Grimes 			special = device;
2138fae3551SRodney W. Grimes 			goto again;
2148fae3551SRodney W. Grimes 		}
2158fae3551SRodney W. Grimes 		err(1, "%s", special);
2168fae3551SRodney W. Grimes 	}
2178fae3551SRodney W. Grimes 	if ((st.st_mode & S_IFMT) != S_IFBLK &&
2188fae3551SRodney W. Grimes 	    (st.st_mode & S_IFMT) != S_IFCHR)
2198fae3551SRodney W. Grimes 		errx(10, "%s: not a block or character device", special);
2208fae3551SRodney W. Grimes 	getsb(&sblock, special);
2218fae3551SRodney W. Grimes 
22277edab90SPhilippe Charnier 	if (pflag) {
22377edab90SPhilippe Charnier 		printfs();
22477edab90SPhilippe Charnier 		exit(0);
22577edab90SPhilippe Charnier 	}
22677edab90SPhilippe Charnier 	if (aflag) {
2278fae3551SRodney W. Grimes 		name = "maximum contiguous block count";
22877edab90SPhilippe Charnier 		if (sblock.fs_maxcontig == avalue) {
22977edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, avalue);
2302af14b60SPhilippe Charnier 		}
23177edab90SPhilippe Charnier 		else {
23277edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
23377edab90SPhilippe Charnier 					name, sblock.fs_maxcontig, avalue);
23477edab90SPhilippe Charnier 			sblock.fs_maxcontig = avalue;
23577edab90SPhilippe Charnier 		}
23677edab90SPhilippe Charnier 	}
23777edab90SPhilippe Charnier 	if (dflag) {
2382af14b60SPhilippe Charnier 		name = "rotational delay between contiguous blocks";
23977edab90SPhilippe Charnier 		if (sblock.fs_rotdelay == dvalue) {
24077edab90SPhilippe Charnier 			warnx("%s remains unchanged as %dms", name, dvalue);
2412af14b60SPhilippe Charnier 		}
24277edab90SPhilippe Charnier 		else {
2438fae3551SRodney W. Grimes 			warnx("%s changes from %dms to %dms",
24477edab90SPhilippe Charnier 				    name, sblock.fs_rotdelay, dvalue);
24577edab90SPhilippe Charnier 			sblock.fs_rotdelay = dvalue;
24677edab90SPhilippe Charnier 		}
24777edab90SPhilippe Charnier 	}
24877edab90SPhilippe Charnier 	if (eflag) {
2492af14b60SPhilippe Charnier 		name = "maximum blocks per file in a cylinder group";
25077edab90SPhilippe Charnier 		if (sblock.fs_maxbpg == evalue) {
25177edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d", name, evalue);
2522af14b60SPhilippe Charnier 		}
25377edab90SPhilippe Charnier 		else {
25477edab90SPhilippe Charnier 			warnx("%s changes from %d to %d",
25577edab90SPhilippe Charnier 					name, sblock.fs_maxbpg, evalue);
25677edab90SPhilippe Charnier 			sblock.fs_maxbpg = evalue;
25777edab90SPhilippe Charnier 		}
25877edab90SPhilippe Charnier 	}
259a61ab64aSKirk McKusick 	if (fflag) {
260a61ab64aSKirk McKusick 		name = "average file size";
261a61ab64aSKirk McKusick 		if (sblock.fs_avgfilesize == fvalue) {
262a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, fvalue);
263a61ab64aSKirk McKusick 		}
264a61ab64aSKirk McKusick 		else {
265a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
266a61ab64aSKirk McKusick 					name, sblock.fs_avgfilesize, fvalue);
267a61ab64aSKirk McKusick 			sblock.fs_avgfilesize = fvalue;
268a61ab64aSKirk McKusick 		}
269a61ab64aSKirk McKusick 	}
27077edab90SPhilippe Charnier 	if (mflag) {
2718fae3551SRodney W. Grimes 		name = "minimum percentage of free space";
27277edab90SPhilippe Charnier 		if (sblock.fs_minfree == mvalue) {
27377edab90SPhilippe Charnier 			warnx("%s remains unchanged as %d%%", name, mvalue);
2742af14b60SPhilippe Charnier 		}
27577edab90SPhilippe Charnier 		else {
2768fae3551SRodney W. Grimes 			warnx("%s changes from %d%% to %d%%",
27777edab90SPhilippe Charnier 				    name, sblock.fs_minfree, mvalue);
27877edab90SPhilippe Charnier 			sblock.fs_minfree = mvalue;
27977edab90SPhilippe Charnier 			if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
2808fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
28177edab90SPhilippe Charnier 			if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
2828fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
283b1897c19SJulian Elischer 		}
28477edab90SPhilippe Charnier 	}
28577edab90SPhilippe Charnier 	if (nflag) {
28677edab90SPhilippe Charnier  		name = "soft updates";
28777edab90SPhilippe Charnier  		if (strcmp(nvalue, "enable") == 0) {
28877edab90SPhilippe Charnier 			if (sblock.fs_flags & FS_DOSOFTDEP) {
28977edab90SPhilippe Charnier 				warnx("%s remains unchanged as enabled", name);
2901c2665d8SKirk McKusick 			} else if (sblock.fs_clean == 0) {
2911c2665d8SKirk McKusick 				warnx("%s cannot be enabled until fsck is run",
2921c2665d8SKirk McKusick 				    name);
29377edab90SPhilippe Charnier 			} else {
29477edab90SPhilippe Charnier  				sblock.fs_flags |= FS_DOSOFTDEP;
29577edab90SPhilippe Charnier  				warnx("%s set", name);
29677edab90SPhilippe Charnier 			}
29777edab90SPhilippe Charnier  		} else if (strcmp(nvalue, "disable") == 0) {
29877edab90SPhilippe Charnier 			if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
29977edab90SPhilippe Charnier 				warnx("%s remains unchanged as disabled", name);
30077edab90SPhilippe Charnier 			} else {
30177edab90SPhilippe Charnier  				sblock.fs_flags &= ~FS_DOSOFTDEP;
30277edab90SPhilippe Charnier  				warnx("%s cleared", name);
30377edab90SPhilippe Charnier 			}
30477edab90SPhilippe Charnier  		}
30577edab90SPhilippe Charnier 	}
30677edab90SPhilippe Charnier 	if (oflag) {
3078fae3551SRodney W. Grimes 		name = "optimization preference";
3088fae3551SRodney W. Grimes 		chg[FS_OPTSPACE] = "space";
3098fae3551SRodney W. Grimes 		chg[FS_OPTTIME] = "time";
31077edab90SPhilippe Charnier 		if (sblock.fs_optim == ovalue) {
31177edab90SPhilippe Charnier 			warnx("%s remains unchanged as %s", name, chg[ovalue]);
3128fae3551SRodney W. Grimes 		}
31377edab90SPhilippe Charnier 		else {
3148fae3551SRodney W. Grimes 			warnx("%s changes from %s to %s",
31577edab90SPhilippe Charnier 				    name, chg[sblock.fs_optim], chg[ovalue]);
31677edab90SPhilippe Charnier 			sblock.fs_optim = ovalue;
31777edab90SPhilippe Charnier 			if (sblock.fs_minfree >= MINFREE &&
31877edab90SPhilippe Charnier 					ovalue == FS_OPTSPACE)
3198fae3551SRodney W. Grimes 				warnx(OPTWARN, "time", ">=", MINFREE);
32077edab90SPhilippe Charnier 			if (sblock.fs_minfree < MINFREE &&
32177edab90SPhilippe Charnier 					ovalue == FS_OPTTIME)
3228fae3551SRodney W. Grimes 				warnx(OPTWARN, "space", "<", MINFREE);
3238fae3551SRodney W. Grimes 		}
32477edab90SPhilippe Charnier 	}
325a61ab64aSKirk McKusick 	if (sflag) {
326a61ab64aSKirk McKusick 		name = "expected number of files per directory";
327a61ab64aSKirk McKusick 		if (sblock.fs_avgfpdir == svalue) {
328a61ab64aSKirk McKusick 			warnx("%s remains unchanged as %d", name, svalue);
329a61ab64aSKirk McKusick 		}
330a61ab64aSKirk McKusick 		else {
331a61ab64aSKirk McKusick 			warnx("%s changes from %d to %d",
332a61ab64aSKirk McKusick 					name, sblock.fs_avgfpdir, svalue);
333a61ab64aSKirk McKusick 			sblock.fs_avgfpdir = svalue;
334a61ab64aSKirk McKusick 		}
335a61ab64aSKirk McKusick 	}
3362af14b60SPhilippe Charnier 
337060ac658SSheldon Hearn 	putsb(&sblock, special, Aflag);
3387382c45aSLuoqi Chen 	if (active) {
3397382c45aSLuoqi Chen 		bzero(&args, sizeof(args));
3407382c45aSLuoqi Chen 		if (mount("ufs", fs->fs_file,
3417382c45aSLuoqi Chen 		    stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
3427382c45aSLuoqi Chen 			err(9, "%s: reload", special);
3437382c45aSLuoqi Chen 		warnx("file system reloaded");
3447382c45aSLuoqi Chen 	}
3458fae3551SRodney W. Grimes 	exit(0);
3468fae3551SRodney W. Grimes }
3478fae3551SRodney W. Grimes 
3488fae3551SRodney W. Grimes void
3498fae3551SRodney W. Grimes usage()
3508fae3551SRodney W. Grimes {
3518679b1b4SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n%s\n",
352a61ab64aSKirk McKusick "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
353a61ab64aSKirk McKusick "              [-m minfree] [-p] [-n enable | disable] [-o space | time]",
354a61ab64aSKirk McKusick "              [-s filesperdir] special | filesystem");
3558fae3551SRodney W. Grimes 	exit(2);
3568fae3551SRodney W. Grimes }
3578fae3551SRodney W. Grimes 
3588fae3551SRodney W. Grimes void
3598fae3551SRodney W. Grimes getsb(fs, file)
3608fae3551SRodney W. Grimes 	register struct fs *fs;
3618fae3551SRodney W. Grimes 	char *file;
3628fae3551SRodney W. Grimes {
3638fae3551SRodney W. Grimes 
364060ac658SSheldon Hearn 	fi = open(file, O_RDONLY);
3658fae3551SRodney W. Grimes 	if (fi < 0)
3668fae3551SRodney W. Grimes 		err(3, "cannot open %s", file);
3678fae3551SRodney W. Grimes 	if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
3688fae3551SRodney W. Grimes 		err(4, "%s: bad super block", file);
3698fae3551SRodney W. Grimes 	if (fs->fs_magic != FS_MAGIC)
3708fae3551SRodney W. Grimes 		err(5, "%s: bad magic number", file);
3718fae3551SRodney W. Grimes 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
3728fae3551SRodney W. Grimes }
3738fae3551SRodney W. Grimes 
3748fae3551SRodney W. Grimes void
375060ac658SSheldon Hearn putsb(fs, file, all)
376060ac658SSheldon Hearn 	register struct fs *fs;
377060ac658SSheldon Hearn 	char *file;
378060ac658SSheldon Hearn 	int all;
379060ac658SSheldon Hearn {
380060ac658SSheldon Hearn 	int i;
381060ac658SSheldon Hearn 
382060ac658SSheldon Hearn 	/*
383060ac658SSheldon Hearn 	 * Re-open the device read-write. Use the read-only file
384060ac658SSheldon Hearn 	 * descriptor as an interlock to prevent the device from
385060ac658SSheldon Hearn 	 * being mounted while we are switching mode.
386060ac658SSheldon Hearn 	 */
387060ac658SSheldon Hearn 	i = fi;
388060ac658SSheldon Hearn 	fi = open(file, O_RDWR);
389060ac658SSheldon Hearn 	close(i);
390060ac658SSheldon Hearn 	if (fi < 0)
391060ac658SSheldon Hearn 		err(3, "cannot open %s", file);
392060ac658SSheldon Hearn 	bwrite((daddr_t)SBOFF / dev_bsize, (char *)fs, SBSIZE);
393060ac658SSheldon Hearn 	if (all)
394060ac658SSheldon Hearn 		for (i = 0; i < fs->fs_ncg; i++)
395060ac658SSheldon Hearn 			bwrite(fsbtodb(fs, cgsblock(fs, i)),
396060ac658SSheldon Hearn 			    (char *)fs, SBSIZE);
397060ac658SSheldon Hearn 	close(fi);
398060ac658SSheldon Hearn }
399060ac658SSheldon Hearn 
400060ac658SSheldon Hearn void
40116a7269eSJoerg Wunsch printfs()
40216a7269eSJoerg Wunsch {
403b1897c19SJulian Elischer 	warnx("soft updates:  (-n)                                %s",
404b1897c19SJulian Elischer 		(sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
40516a7269eSJoerg Wunsch 	warnx("maximum contiguous block count: (-a)               %d",
40616a7269eSJoerg Wunsch 	      sblock.fs_maxcontig);
40716a7269eSJoerg Wunsch 	warnx("rotational delay between contiguous blocks: (-d)   %d ms",
40816a7269eSJoerg Wunsch 	      sblock.fs_rotdelay);
40916a7269eSJoerg Wunsch 	warnx("maximum blocks per file in a cylinder group: (-e)  %d",
41016a7269eSJoerg Wunsch 	      sblock.fs_maxbpg);
411a61ab64aSKirk McKusick 	warnx("average file size: (-f)                            %d",
412a61ab64aSKirk McKusick 	      sblock.fs_avgfilesize);
413a61ab64aSKirk McKusick 	warnx("average number of files in a directory: (-s)       %d",
414a61ab64aSKirk McKusick 	      sblock.fs_avgfpdir);
41516a7269eSJoerg Wunsch 	warnx("minimum percentage of free space: (-m)             %d%%",
41616a7269eSJoerg Wunsch 	      sblock.fs_minfree);
41716a7269eSJoerg Wunsch 	warnx("optimization preference: (-o)                      %s",
41816a7269eSJoerg Wunsch 	      sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
41916a7269eSJoerg Wunsch 	if (sblock.fs_minfree >= MINFREE &&
42016a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTSPACE)
42116a7269eSJoerg Wunsch 		warnx(OPTWARN, "time", ">=", MINFREE);
42216a7269eSJoerg Wunsch 	if (sblock.fs_minfree < MINFREE &&
42316a7269eSJoerg Wunsch 	    sblock.fs_optim == FS_OPTTIME)
42416a7269eSJoerg Wunsch 		warnx(OPTWARN, "space", "<", MINFREE);
42516a7269eSJoerg Wunsch }
42616a7269eSJoerg Wunsch 
42716a7269eSJoerg Wunsch void
4288fae3551SRodney W. Grimes bwrite(blk, buf, size)
4298fae3551SRodney W. Grimes 	daddr_t blk;
4308fae3551SRodney W. Grimes 	char *buf;
4318fae3551SRodney W. Grimes 	int size;
4328fae3551SRodney W. Grimes {
4338fae3551SRodney W. Grimes 
4348fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
4358fae3551SRodney W. Grimes 		err(6, "FS SEEK");
4368fae3551SRodney W. Grimes 	if (write(fi, buf, size) != size)
4378fae3551SRodney W. Grimes 		err(7, "FS WRITE");
4388fae3551SRodney W. Grimes }
4398fae3551SRodney W. Grimes 
4408fae3551SRodney W. Grimes int
4418fae3551SRodney W. Grimes bread(bno, buf, cnt)
4428fae3551SRodney W. Grimes 	daddr_t bno;
4438fae3551SRodney W. Grimes 	char *buf;
4448fae3551SRodney W. Grimes 	int cnt;
4458fae3551SRodney W. Grimes {
4468fae3551SRodney W. Grimes 	int i;
4478fae3551SRodney W. Grimes 
4488fae3551SRodney W. Grimes 	if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
4498fae3551SRodney W. Grimes 		return(1);
4508fae3551SRodney W. Grimes 	if ((i = read(fi, buf, cnt)) != cnt) {
4518fae3551SRodney W. Grimes 		for(i=0; i<sblock.fs_bsize; i++)
4528fae3551SRodney W. Grimes 			buf[i] = 0;
4538fae3551SRodney W. Grimes 		return (1);
4548fae3551SRodney W. Grimes 	}
4558fae3551SRodney W. Grimes 	return (0);
4568fae3551SRodney W. Grimes }
457