Deleted Added
sdiff udiff text old ( 69314 ) new ( 69829 )
full compact
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/sbin/tunefs/tunefs.c 69829 2000-12-10 20:59:30Z charnier $";
46#endif /* not lint */
47
48/*
49 * tunefs: change layout parameters to an existing file system.
50 */
51#include <sys/param.h>
52#include <sys/mount.h>
53#include <sys/stat.h>
54
55#include <ufs/ffs/fs.h>
56#include <ufs/ufs/ufsmount.h>
57
58#include <err.h>
59#include <fcntl.h>
60#include <fstab.h>
61#include <paths.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66
67/* the optimization warning string template */
68#define OPTWARN "should optimize for %s with minfree %s %d%%"
69
70union {
71 struct fs sb;
72 char pad[MAXBSIZE];
73} sbun;
74#define sblock sbun.sb
75
76int fi;
77long dev_bsize = 1;
78
79void bwrite __P((daddr_t, char *, int));
80int bread __P((daddr_t, char *, int));
81void getsb __P((struct fs *, char *));
82void putsb __P((struct fs *, char *, int));
83void usage __P((void));
84void printfs __P((void));
85
86int
87main(argc, argv)
88 int argc;
89 char *argv[];
90{
91 char *special, *name;
92 struct stat st;
93 int Aflag = 0, active = 0;
94 int aflag = 0, dflag = 0, eflag = 0, mflag = 0;
95 int nflag = 0, oflag = 0, pflag = 0;
96 int avalue = 0, dvalue = 0, evalue = 0, mvalue = 0, ovalue = 0;
97 char *nvalue = NULL;
98 struct fstab *fs;
99 char *chg[2], device[MAXPATHLEN];
100 struct ufs_args args;
101 struct statfs stfs;
102 int found_arg, ch;
103
104 if (argc < 3)
105 usage();
106 found_arg = 0; /* at least one arg is required */
107 while ((ch = getopt(argc, argv, "Aa:d:e:m:n:o:p")) != -1)
108 switch (ch) {
109 case 'A':
110 found_arg = 1;
111 Aflag++;
112 break;
113 case 'a':
114 found_arg = 1;
115 name = "maximum contiguous block count";
116 avalue = atoi(optarg);
117 if (avalue < 1)
118 errx(10, "%s must be >= 1 (was %s)", name, optarg);
119 aflag = 1;
120 break;
121 case 'd':
122 found_arg = 1;
123 name = "rotational delay between contiguous blocks";
124 dvalue = atoi(optarg);
125 dflag = 1;
126 break;
127 case 'e':
128 found_arg = 1;
129 name = "maximum blocks per file in a cylinder group";
130 evalue = atoi(optarg);
131 if (evalue < 1)
132 errx(10, "%s must be >= 1 (was %s)", name, optarg);
133 eflag = 1;
134 break;
135 case 'm':
136 found_arg = 1;
137 name = "minimum percentage of free space";
138 mvalue = atoi(optarg);
139 if (mvalue < 0 || mvalue > 99)
140 errx(10, "bad %s (%s)", name, optarg);
141 mflag = 1;
142 break;
143 case 'n':
144 found_arg = 1;
145 name = "soft updates";
146 nvalue = optarg;
147 if (strcmp(nvalue, "enable") && strcmp(nvalue, "disable")) {
148 errx(10, "bad %s (options are %s)",
149 name, "`enable' or `disable'");
150 }
151 nflag = 1;
152 break;
153 case 'o':
154 found_arg = 1;
155 name = "optimization preference";
156 chg[FS_OPTSPACE] = "space";
157 chg[FS_OPTTIME] = "time";
158 if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
159 ovalue = FS_OPTSPACE;
160 else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
161 ovalue = FS_OPTTIME;
162 else
163 errx(10, "bad %s (options are `space' or `time')",
164 name);
165 oflag = 1;
166 break;
167 case 'p':
168 pflag = 1;
169 break;
170 default:
171 usage();
172 }
173 argc -= optind;
174 argv += optind;
175
176 if (found_arg == 0 || argc != 1)
177 usage();
178
179 special = argv[0];
180 fs = getfsfile(special);
181 if (fs) {
182 if (statfs(special, &stfs) == 0 &&
183 strcmp(special, stfs.f_mntonname) == 0) {
184 active = 1;
185 }
186 special = fs->fs_spec;
187 }
188again:
189 if (stat(special, &st) < 0) {
190 if (*special != '/') {
191 if (*special == 'r')
192 special++;
193 (void)sprintf(device, "%s/%s", _PATH_DEV, special);
194 special = device;
195 goto again;
196 }
197 err(1, "%s", special);
198 }
199 if ((st.st_mode & S_IFMT) != S_IFBLK &&
200 (st.st_mode & S_IFMT) != S_IFCHR)
201 errx(10, "%s: not a block or character device", special);
202 getsb(&sblock, special);
203
204 if (pflag) {
205 printfs();
206 exit(0);
207 }
208 if (aflag) {
209 name = "maximum contiguous block count";
210 if (sblock.fs_maxcontig == avalue) {
211 warnx("%s remains unchanged as %d", name, avalue);
212 }
213 else {
214 warnx("%s changes from %d to %d",
215 name, sblock.fs_maxcontig, avalue);
216 sblock.fs_maxcontig = avalue;
217 }
218 }
219 if (dflag) {
220 name = "rotational delay between contiguous blocks";
221 if (sblock.fs_rotdelay == dvalue) {
222 warnx("%s remains unchanged as %dms", name, dvalue);
223 }
224 else {
225 warnx("%s changes from %dms to %dms",
226 name, sblock.fs_rotdelay, dvalue);
227 sblock.fs_rotdelay = dvalue;
228 }
229 }
230 if (eflag) {
231 name = "maximum blocks per file in a cylinder group";
232 if (sblock.fs_maxbpg == evalue) {
233 warnx("%s remains unchanged as %d", name, evalue);
234 }
235 else {
236 warnx("%s changes from %d to %d",
237 name, sblock.fs_maxbpg, evalue);
238 sblock.fs_maxbpg = evalue;
239 }
240 }
241 if (mflag) {
242 name = "minimum percentage of free space";
243 if (sblock.fs_minfree == mvalue) {
244 warnx("%s remains unchanged as %d%%", name, mvalue);
245 }
246 else {
247 warnx("%s changes from %d%% to %d%%",
248 name, sblock.fs_minfree, mvalue);
249 sblock.fs_minfree = mvalue;
250 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE)
251 warnx(OPTWARN, "time", ">=", MINFREE);
252 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME)
253 warnx(OPTWARN, "space", "<", MINFREE);
254 }
255 }
256 if (nflag) {
257 name = "soft updates";
258 if (strcmp(nvalue, "enable") == 0) {
259 if ( sblock.fs_flags & FS_DOSOFTDEP ) {
260 warnx("%s remains unchanged as enabled", name);
261 } else {
262 sblock.fs_flags |= FS_DOSOFTDEP;
263 warnx("%s set", name);
264 }
265 } else if (strcmp(nvalue, "disable") == 0) {
266 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) {
267 warnx("%s remains unchanged as disabled", name);
268 } else {
269 sblock.fs_flags &= ~FS_DOSOFTDEP;
270 warnx("%s cleared", name);
271 }
272 }
273 }
274 if (oflag) {
275 name = "optimization preference";
276 chg[FS_OPTSPACE] = "space";
277 chg[FS_OPTTIME] = "time";
278 if (sblock.fs_optim == ovalue) {
279 warnx("%s remains unchanged as %s", name, chg[ovalue]);
280 }
281 else {
282 warnx("%s changes from %s to %s",
283 name, chg[sblock.fs_optim], chg[ovalue]);
284 sblock.fs_optim = ovalue;
285 if (sblock.fs_minfree >= MINFREE &&
286 ovalue == FS_OPTSPACE)
287 warnx(OPTWARN, "time", ">=", MINFREE);
288 if (sblock.fs_minfree < MINFREE &&
289 ovalue == FS_OPTTIME)
290 warnx(OPTWARN, "space", "<", MINFREE);
291 }
292 }
293
294 putsb(&sblock, special, Aflag);
295 if (active) {
296 bzero(&args, sizeof(args));
297 if (mount("ufs", fs->fs_file,
298 stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
299 err(9, "%s: reload", special);
300 warnx("file system reloaded");
301 }
302 exit(0);
303}
304
305void
306usage()
307{
308 fprintf(stderr, "%s\n%s\n%s\n",
309"usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-m minfree]",
310" [-p] [-n enable | disable] [-o space | time]",
311" special | filesystem");
312 exit(2);
313}
314
315void
316getsb(fs, file)
317 register struct fs *fs;
318 char *file;
319{
320
321 fi = open(file, O_RDONLY);
322 if (fi < 0)
323 err(3, "cannot open %s", file);
324 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
325 err(4, "%s: bad super block", file);
326 if (fs->fs_magic != FS_MAGIC)
327 err(5, "%s: bad magic number", file);
328 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
329}
330
331void
332putsb(fs, file, all)
333 register struct fs *fs;
334 char *file;
335 int all;
336{
337 int i;
338
339 /*
340 * Re-open the device read-write. Use the read-only file
341 * descriptor as an interlock to prevent the device from
342 * being mounted while we are switching mode.
343 */
344 i = fi;
345 fi = open(file, O_RDWR);
346 close(i);
347 if (fi < 0)
348 err(3, "cannot open %s", file);
349 bwrite((daddr_t)SBOFF / dev_bsize, (char *)fs, SBSIZE);
350 if (all)
351 for (i = 0; i < fs->fs_ncg; i++)
352 bwrite(fsbtodb(fs, cgsblock(fs, i)),
353 (char *)fs, SBSIZE);
354 close(fi);
355}
356
357void
358printfs()
359{
360 warnx("soft updates: (-n) %s",
361 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
362 warnx("maximum contiguous block count: (-a) %d",
363 sblock.fs_maxcontig);
364 warnx("rotational delay between contiguous blocks: (-d) %d ms",
365 sblock.fs_rotdelay);
366 warnx("maximum blocks per file in a cylinder group: (-e) %d",
367 sblock.fs_maxbpg);
368 warnx("minimum percentage of free space: (-m) %d%%",
369 sblock.fs_minfree);
370 warnx("optimization preference: (-o) %s",
371 sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
372 if (sblock.fs_minfree >= MINFREE &&
373 sblock.fs_optim == FS_OPTSPACE)
374 warnx(OPTWARN, "time", ">=", MINFREE);
375 if (sblock.fs_minfree < MINFREE &&
376 sblock.fs_optim == FS_OPTTIME)
377 warnx(OPTWARN, "space", "<", MINFREE);
378}
379
380void
381bwrite(blk, buf, size)
382 daddr_t blk;
383 char *buf;
384 int size;
385{
386
387 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
388 err(6, "FS SEEK");
389 if (write(fi, buf, size) != size)
390 err(7, "FS WRITE");
391}
392
393int
394bread(bno, buf, cnt)
395 daddr_t bno;
396 char *buf;
397 int cnt;
398{
399 int i;
400
401 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
402 return(1);
403 if ((i = read(fi, buf, cnt)) != cnt) {
404 for(i=0; i<sblock.fs_bsize; i++)
405 buf[i] = 0;
406 return (1);
407 }
408 return (0);
409}