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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)tunefs.c 8.2 (Berkeley) 4/19/94
31 * $FreeBSD: src/sbin/tunefs/tunefs.c,v 1.11.2.5 2001/10/14 21:50:39 iedowse Exp $
35 * tunefs: change layout parameters to an existing file system.
37 #include <sys/param.h>
38 #include <sys/mount.h>
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/ufs/fs.h>
43 #include <vfs/ufs/ufsmount.h>
54 /* the optimization warning string template */
55 #define OPTWARN "should optimize for %s with minfree %s %d%%"
61 #define sblock sbun.sb
66 void bwrite(daddr_t
, const char *, int);
67 int bread(daddr_t
, char *, int);
68 void getsb(struct fs
*, const char *);
69 void putsb(struct fs
*, const char *, int);
70 void usage(void) __dead2
;
74 main(int argc
, char **argv
)
77 const char *name
, *action
;
80 int Aflag
= 0, active
= 0;
83 char device
[MAXPATHLEN
];
90 special
= argv
[argc
- 1];
91 fs
= getfsfile(special
);
93 if (statfs(special
, &stfs
) == 0 &&
94 strcmp(special
, stfs
.f_mntonname
) == 0) {
97 special
= fs
->fs_spec
;
100 if (stat(special
, &st
) < 0) {
101 if (*special
!= '/') {
104 snprintf(device
, sizeof(device
), "%s%s",
109 err(1, "%s", special
);
111 if (fs
== NULL
&& (st
.st_mode
& S_IFMT
) == S_IFDIR
)
112 errx(10, "%s: unknown file system", special
);
113 getsb(&sblock
, special
);
114 for (; argc
> 0 && argv
[0][0] == '-'; argc
--, argv
++) {
115 for (cp
= &argv
[0][1]; *cp
; cp
++)
127 name
= "maximum contiguous block count";
129 errx(10, "-a: missing %s", name
);
133 errx(10, "%s must be >= 1 (was %s)",
135 warnx("%s changes from %d to %d",
136 name
, sblock
.fs_maxcontig
, i
);
137 sblock
.fs_maxcontig
= i
;
142 "rotational delay between contiguous blocks";
144 errx(10, "-d: missing %s", name
);
147 warnx("%s changes from %dms to %dms",
148 name
, sblock
.fs_rotdelay
, i
);
149 sblock
.fs_rotdelay
= i
;
154 "maximum blocks per file in a cylinder group";
156 errx(10, "-e: missing %s", name
);
160 errx(10, "%s must be >= 1 (was %s)",
162 warnx("%s changes from %d to %d",
163 name
, sblock
.fs_maxbpg
, i
);
164 sblock
.fs_maxbpg
= i
;
168 name
= "average file size";
170 errx(10, "-a: missing %s", name
);
174 errx(10, "%s must be >= 1 (was %s)", name
, *argv
);
175 if (sblock
.fs_avgfilesize
== i
) {
176 warnx("%s remains unchanged as %d",
179 warnx("%s changes from %d to %d",
180 name
, sblock
.fs_avgfilesize
, i
);
181 sblock
.fs_avgfilesize
= i
;
186 name
= "minimum percentage of free space";
188 errx(10, "-m: missing %s", name
);
192 errx(10, "bad %s (%s)", name
, *argv
);
193 warnx("%s changes from %d%% to %d%%",
194 name
, sblock
.fs_minfree
, i
);
195 sblock
.fs_minfree
= i
;
197 sblock
.fs_optim
== FS_OPTSPACE
)
198 warnx(OPTWARN
, "time", ">=", MINFREE
);
200 sblock
.fs_optim
== FS_OPTTIME
)
201 warnx(OPTWARN
, "space", "<", MINFREE
);
205 name
= "soft updates";
207 errx(10, "-n: missing %s", name
);
209 if (strcmp(*argv
, "enable") == 0) {
210 sblock
.fs_flags
|= FS_DOSOFTDEP
;
212 } else if (strcmp(*argv
, "disable") == 0) {
213 sblock
.fs_flags
&= ~FS_DOSOFTDEP
;
216 errx(10, "bad %s (options are %s)",
217 name
, "`enable' or `disable'");
219 warnx("%s %s", name
, action
);
223 name
= "optimization preference";
225 errx(10, "-o: missing %s", name
);
227 chg
[FS_OPTSPACE
] = "space";
228 chg
[FS_OPTTIME
] = "time";
229 if (strcmp(*argv
, chg
[FS_OPTSPACE
]) == 0)
231 else if (strcmp(*argv
, chg
[FS_OPTTIME
]) == 0)
234 errx(10, "bad %s (options are `space' or `time')",
236 if (sblock
.fs_optim
== i
) {
237 warnx("%s remains unchanged as %s",
241 warnx("%s changes from %s to %s",
242 name
, chg
[sblock
.fs_optim
], chg
[i
]);
244 if (sblock
.fs_minfree
>= MINFREE
&&
246 warnx(OPTWARN
, "time", ">=", MINFREE
);
247 if (sblock
.fs_minfree
< MINFREE
&&
249 warnx(OPTWARN
, "space", "<", MINFREE
);
253 name
= "expected number of files per directory";
255 errx(10, "-a: missing %s", name
);
259 errx(10, "%s must be >= 1 (was %s)", name
, *argv
);
260 if (sblock
.fs_avgfpdir
== i
) {
261 warnx("%s remains unchanged as %d",
264 warnx("%s changes from %d to %d",
265 name
, sblock
.fs_avgfpdir
, i
);
266 sblock
.fs_avgfpdir
= i
;
276 putsb(&sblock
, special
, Aflag
);
278 bzero(&args
, sizeof(args
));
279 if (mount("ufs", fs
->fs_file
,
280 stfs
.f_flags
| MNT_UPDATE
| MNT_RELOAD
, &args
) < 0)
281 err(9, "%s: reload", special
);
282 warnx("file system reloaded");
290 fprintf(stderr
, "%s\n%s\n%s\n",
291 "usage: tunefs [-Ap] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
292 " [-m minfree] [-n enable | disable] [-o space | time]",
293 " [-s filesperdir] {special | filesystem}");
298 getsb(struct fs
*fs
, const char *file
)
301 fi
= open(file
, O_RDONLY
);
303 err(3, "cannot open %s", file
);
304 if (bread((daddr_t
)SBOFF
, (char *)fs
, SBSIZE
))
305 err(4, "%s: bad super block", file
);
306 if (fs
->fs_magic
!= FS_MAGIC
)
307 errx(5, "%s: bad magic number", file
);
308 dev_bsize
= fs
->fs_fsize
/ fsbtodb(fs
, 1);
312 putsb(struct fs
*fs
, const char *file
, int all
)
317 * Re-open the device read-write. Use the read-only file
318 * descriptor as an interlock to prevent the device from
319 * being mounted while we are switching mode.
322 fi
= open(file
, O_RDWR
);
325 err(3, "cannot open %s", file
);
326 bwrite((daddr_t
)SBOFF
/ dev_bsize
, (const char *)fs
, SBSIZE
);
328 for (i
= 0; i
< fs
->fs_ncg
; i
++)
329 bwrite(fsbtodb(fs
, cgsblock(fs
, i
)),
330 (const char *)fs
, SBSIZE
);
337 warnx("soft updates: (-n) %s",
338 (sblock
.fs_flags
& FS_DOSOFTDEP
)? "enabled" : "disabled");
339 warnx("maximum contiguous block count: (-a) %d",
340 sblock
.fs_maxcontig
);
341 warnx("rotational delay between contiguous blocks: (-d) %d ms",
343 warnx("maximum blocks per file in a cylinder group: (-e) %d",
345 warnx("average file size: (-f) %d",
346 sblock
.fs_avgfilesize
);
347 warnx("average number of files in a directory: (-s) %d",
349 warnx("minimum percentage of free space: (-m) %d%%",
351 warnx("optimization preference: (-o) %s",
352 sblock
.fs_optim
== FS_OPTSPACE
? "space" : "time");
353 if (sblock
.fs_minfree
>= MINFREE
&&
354 sblock
.fs_optim
== FS_OPTSPACE
)
355 warnx(OPTWARN
, "time", ">=", MINFREE
);
356 if (sblock
.fs_minfree
< MINFREE
&&
357 sblock
.fs_optim
== FS_OPTTIME
)
358 warnx(OPTWARN
, "space", "<", MINFREE
);
362 bwrite(daddr_t blk
, const char *buf
, int size
)
365 if (lseek(fi
, (off_t
)blk
* dev_bsize
, SEEK_SET
) < 0)
367 if (write(fi
, buf
, size
) != size
)
372 bread(daddr_t bno
, char *buf
, int cnt
)
376 if (lseek(fi
, (off_t
)bno
* dev_bsize
, SEEK_SET
) < 0)
378 if ((i
= read(fi
, buf
, cnt
)) != cnt
) {
379 for(i
=0; i
<sblock
.fs_bsize
; i
++)