2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)tunefs.c 8.2 (Berkeley) 4/19/94
35 * $FreeBSD: src/sbin/tunefs/tunefs.c,v 1.11.2.5 2001/10/14 21:50:39 iedowse Exp $
36 * $DragonFly: src/sbin/tunefs/tunefs.c,v 1.5 2003/11/01 17:16:02 drhodus Exp $
40 * tunefs: change layout parameters to an existing file system.
42 #include <sys/param.h>
43 #include <sys/mount.h>
46 #include <vfs/ufs/fs.h>
47 #include <vfs/ufs/ufsmount.h>
58 /* the optimization warning string template */
59 #define OPTWARN "should optimize for %s with minfree %s %d%%"
65 #define sblock sbun.sb
70 void bwrite(daddr_t, const char *, int);
71 int bread(daddr_t, char *, int);
72 void getsb(struct fs *, const char *);
73 void putsb(struct fs *, const char *, int);
78 main(int argc, char **argv)
81 const char *name, *action;
84 int Aflag = 0, active = 0;
87 char device[MAXPATHLEN];
94 special = argv[argc - 1];
95 fs = getfsfile(special);
97 if (statfs(special, &stfs) == 0 &&
98 strcmp(special, stfs.f_mntonname) == 0) {
101 special = fs->fs_spec;
104 if (stat(special, &st) < 0) {
105 if (*special != '/') {
108 (void)snprintf(device, sizeof(device), "%s%s",
113 err(1, "%s", special);
115 if (fs == NULL && (st.st_mode & S_IFMT) == S_IFDIR)
116 errx(10, "%s: unknown file system", special);
117 getsb(&sblock, special);
118 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
119 for (cp = &argv[0][1]; *cp; cp++)
131 name = "maximum contiguous block count";
133 errx(10, "-a: missing %s", name);
137 errx(10, "%s must be >= 1 (was %s)",
139 warnx("%s changes from %d to %d",
140 name, sblock.fs_maxcontig, i);
141 sblock.fs_maxcontig = i;
146 "rotational delay between contiguous blocks";
148 errx(10, "-d: missing %s", name);
151 warnx("%s changes from %dms to %dms",
152 name, sblock.fs_rotdelay, i);
153 sblock.fs_rotdelay = i;
158 "maximum blocks per file in a cylinder group";
160 errx(10, "-e: missing %s", name);
164 errx(10, "%s must be >= 1 (was %s)",
166 warnx("%s changes from %d to %d",
167 name, sblock.fs_maxbpg, i);
168 sblock.fs_maxbpg = i;
172 name = "average file size";
174 errx(10, "-a: missing %s", name);
178 errx(10, "%s must be >= 1 (was %s)", name, *argv);
179 if (sblock.fs_avgfilesize == i) {
180 warnx("%s remains unchanged as %d",
183 warnx("%s changes from %d to %d",
184 name, sblock.fs_avgfilesize, i);
185 sblock.fs_avgfilesize = i;
190 name = "minimum percentage of free space";
192 errx(10, "-m: missing %s", name);
196 errx(10, "bad %s (%s)", name, *argv);
197 warnx("%s changes from %d%% to %d%%",
198 name, sblock.fs_minfree, i);
199 sblock.fs_minfree = i;
201 sblock.fs_optim == FS_OPTSPACE)
202 warnx(OPTWARN, "time", ">=", MINFREE);
204 sblock.fs_optim == FS_OPTTIME)
205 warnx(OPTWARN, "space", "<", MINFREE);
209 name = "soft updates";
211 errx(10, "-n: missing %s", name);
213 if (strcmp(*argv, "enable") == 0) {
214 sblock.fs_flags |= FS_DOSOFTDEP;
216 } else if (strcmp(*argv, "disable") == 0) {
217 sblock.fs_flags &= ~FS_DOSOFTDEP;
220 errx(10, "bad %s (options are %s)",
221 name, "`enable' or `disable'");
223 warnx("%s %s", name, action);
227 name = "optimization preference";
229 errx(10, "-o: missing %s", name);
231 chg[FS_OPTSPACE] = "space";
232 chg[FS_OPTTIME] = "time";
233 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
235 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
238 errx(10, "bad %s (options are `space' or `time')",
240 if (sblock.fs_optim == i) {
241 warnx("%s remains unchanged as %s",
245 warnx("%s changes from %s to %s",
246 name, chg[sblock.fs_optim], chg[i]);
248 if (sblock.fs_minfree >= MINFREE &&
250 warnx(OPTWARN, "time", ">=", MINFREE);
251 if (sblock.fs_minfree < MINFREE &&
253 warnx(OPTWARN, "space", "<", MINFREE);
257 name = "expected number of files per directory";
259 errx(10, "-a: missing %s", name);
263 errx(10, "%s must be >= 1 (was %s)", name, *argv);
264 if (sblock.fs_avgfpdir == i) {
265 warnx("%s remains unchanged as %d",
268 warnx("%s changes from %d to %d",
269 name, sblock.fs_avgfpdir, i);
270 sblock.fs_avgfpdir = i;
280 putsb(&sblock, special, Aflag);
282 bzero(&args, sizeof(args));
283 if (mount("ufs", fs->fs_file,
284 stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
285 err(9, "%s: reload", special);
286 warnx("file system reloaded");
294 fprintf(stderr, "%s\n%s\n%s\n",
295 "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
296 " [-m minfree] [-p] [-n enable | disable] [-o space | time]",
297 " [-s filesperdir] [special | filesystem]");
302 getsb(struct fs *fs, const char *file)
305 fi = open(file, O_RDONLY);
307 err(3, "cannot open %s", file);
308 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
309 err(4, "%s: bad super block", file);
310 if (fs->fs_magic != FS_MAGIC)
311 errx(5, "%s: bad magic number", file);
312 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
316 putsb(struct fs *fs, const char *file, int all)
321 * Re-open the device read-write. Use the read-only file
322 * descriptor as an interlock to prevent the device from
323 * being mounted while we are switching mode.
326 fi = open(file, O_RDWR);
329 err(3, "cannot open %s", file);
330 bwrite((daddr_t)SBOFF / dev_bsize, (const char *)fs, SBSIZE);
332 for (i = 0; i < fs->fs_ncg; i++)
333 bwrite(fsbtodb(fs, cgsblock(fs, i)),
334 (const char *)fs, SBSIZE);
341 warnx("soft updates: (-n) %s",
342 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
343 warnx("maximum contiguous block count: (-a) %d",
344 sblock.fs_maxcontig);
345 warnx("rotational delay between contiguous blocks: (-d) %d ms",
347 warnx("maximum blocks per file in a cylinder group: (-e) %d",
349 warnx("average file size: (-f) %d",
350 sblock.fs_avgfilesize);
351 warnx("average number of files in a directory: (-s) %d",
353 warnx("minimum percentage of free space: (-m) %d%%",
355 warnx("optimization preference: (-o) %s",
356 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
357 if (sblock.fs_minfree >= MINFREE &&
358 sblock.fs_optim == FS_OPTSPACE)
359 warnx(OPTWARN, "time", ">=", MINFREE);
360 if (sblock.fs_minfree < MINFREE &&
361 sblock.fs_optim == FS_OPTTIME)
362 warnx(OPTWARN, "space", "<", MINFREE);
366 bwrite(daddr_t blk, const char *buf, int size)
369 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
371 if (write(fi, buf, size) != size)
376 bread(daddr_t bno, char *buf, int cnt)
380 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
382 if ((i = read(fi, buf, cnt)) != cnt) {
383 for(i=0; i<sblock.fs_bsize; i++)