tunefs.c revision 1.25 1 /* $NetBSD: tunefs.c,v 1.25 2001/11/09 09:05:52 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)tunefs.c 8.3 (Berkeley) 5/3/95";
45 #else
46 __RCSID("$NetBSD: tunefs.c,v 1.25 2001/11/09 09:05:52 lukem Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * tunefs: change layout parameters to an existing file system.
52 */
53 #include <sys/param.h>
54
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 #include <ufs/ffs/ffs_extern.h>
58
59 #include <machine/bswap.h>
60
61 #include <err.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <fstab.h>
65 #include <paths.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <util.h>
71
72 /* the optimization warning string template */
73 #define OPTWARN "should optimize for %s with minfree %s %d%%"
74
75 union {
76 struct fs sb;
77 char pad[MAXBSIZE];
78 } sbun;
79 #define sblock sbun.sb
80 char buf[MAXBSIZE];
81
82 int fi;
83 long dev_bsize = 1;
84 int needswap = 0;
85
86 static void bwrite(daddr_t, char *, int, const char *);
87 static void bread(daddr_t, char *, int, const char *);
88 static int getnum(const char *, const char *, int, int);
89 static void getsb(struct fs *, const char *);
90 static void usage(void);
91 int main(int, char *[]);
92
93 int
94 main(int argc, char *argv[])
95 {
96 #define OPTSTRINGBASE "AFNa:d:e:g:h:k:m:o:t:"
97 #ifdef TUNEFS_SOFTDEP
98 int softdep;
99 #define OPTSTRING OPTSTRINGBASE ## "n:"
100 #else
101 #define OPTSTRING OPTSTRINGBASE
102 #endif
103 int i, ch, Aflag, Fflag, Nflag, openflags;
104 struct fstab *fs;
105 const char *special, *chg[2];
106 char device[MAXPATHLEN], rawspec[MAXPATHLEN], *p;
107 int maxbpg, maxcontig, minfree, rotdelay, optim, trackskew;
108 int avgfilesize, avgfpdir;
109
110 Aflag = Fflag = Nflag = 0;
111 maxbpg = maxcontig = minfree = rotdelay = optim = trackskew = -1;
112 avgfilesize = avgfpdir = -1;
113 #ifdef TUNEFS_SOFTDEP
114 softdep = -1;
115 #endif
116 chg[FS_OPTSPACE] = "space";
117 chg[FS_OPTTIME] = "time";
118
119 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) {
120 switch (ch) {
121
122 case 'A':
123 Aflag++;
124 break;
125
126 case 'F':
127 Fflag++;
128 break;
129
130 case 'N':
131 Nflag++;
132 break;
133
134 case 'a':
135 maxcontig = getnum(optarg,
136 "maximum contiguous block count", 1, INT_MAX);
137 break;
138
139 case 'd':
140 rotdelay = getnum(optarg,
141 "rotational delay between contiguous blocks",
142 0, INT_MAX);
143 break;
144
145 case 'e':
146 maxbpg = getnum(optarg,
147 "maximum blocks per file in a cylinder group",
148 1, INT_MAX);
149 break;
150
151 case 'g':
152 avgfilesize = getnum(optarg,
153 "average file size", 1, INT_MAX);
154 break;
155
156 case 'h':
157 avgfpdir = getnum(optarg,
158 "expected number of files per directory",
159 1, INT_MAX);
160 break;
161
162 case 'm':
163 minfree = getnum(optarg,
164 "minimum percentage of free space", 0, 99);
165 break;
166
167 #ifdef TUNEFS_SOFTDEP
168 case 'n':
169 if (strcmp(optarg, "enable") == 0)
170 softdep = 1;
171 else if (strcmp(optarg, "disable") == 0)
172 softdep = 0;
173 else {
174 errx(10, "bad soft dependencies "
175 "(options are `enable' or `disable')");
176 }
177 break;
178 #endif
179
180 case 'o':
181 if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
182 optim = FS_OPTSPACE;
183 else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
184 optim = FS_OPTTIME;
185 else
186 errx(10,
187 "bad %s (options are `space' or `time')",
188 "optimization preference");
189 break;
190
191 case 'k':
192 case 't': /* for compatibility with old syntax */
193 trackskew = getnum(optarg,
194 "track skew in sectors", 0, INT_MAX);
195 break;
196
197 default:
198 usage();
199 }
200 }
201 argc -= optind;
202 argv += optind;
203 if (argc != 1)
204 usage();
205
206 special = argv[0];
207 openflags = Nflag ? O_RDONLY : O_RDWR;
208 if (Fflag) {
209 fi = open(special, openflags);
210 if (fi == -1)
211 err(1, "%s", special);
212 } else {
213 fs = getfsfile(special);
214 if (fs) {
215 if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
216 snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
217 (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
218 special = rawspec;
219 } else
220 special = fs->fs_spec;
221 }
222 fi = opendisk(special, openflags, device, sizeof(device), 0);
223 if (fi == -1)
224 err(1, "%s", errno == ENOENT ? special : device);
225 special = device;
226 }
227 getsb(&sblock, special);
228
229 #define CHANGEVAL(old, new, type, suffix) do \
230 if ((new) != -1) { \
231 if ((new) == (old)) \
232 warnx("%s remains unchanged at %d%s", \
233 (type), (old), (suffix)); \
234 else { \
235 warnx("%s changes from %d%s to %d%s", \
236 (type), (old), (suffix), (new), (suffix)); \
237 (old) = (new); \
238 } \
239 } while (/* CONSTCOND */0)
240
241 warnx("tuning %s", special);
242 CHANGEVAL(sblock.fs_maxcontig, maxcontig,
243 "maximum contiguous block count", "");
244 CHANGEVAL(sblock.fs_rotdelay, rotdelay,
245 "rotational delay between contiguous blocks", "ms");
246 CHANGEVAL(sblock.fs_maxbpg, maxbpg,
247 "maximum blocks per file in a cylinder group", "");
248 CHANGEVAL(sblock.fs_minfree, minfree,
249 "minimum percentage of free space", "%");
250 if (minfree != -1) {
251 if (minfree >= MINFREE &&
252 sblock.fs_optim == FS_OPTSPACE)
253 warnx(OPTWARN, "time", ">=", MINFREE);
254 if (minfree < MINFREE &&
255 sblock.fs_optim == FS_OPTTIME)
256 warnx(OPTWARN, "space", "<", MINFREE);
257 }
258 #ifdef TUNEFS_SOFTDEP
259 if (softdep == 1) {
260 sblock.fs_flags |= FS_DOSOFTDEP;
261 warnx("soft dependencies set");
262 } else if (softdep == 0) {
263 sblock.fs_flags &= ~FS_DOSOFTDEP;
264 warnx("soft dependencies cleared");
265 }
266 #endif
267 if (optim != -1) {
268 if (sblock.fs_optim == optim) {
269 warnx("%s remains unchanged as %s",
270 "optimization preference",
271 chg[optim]);
272 } else {
273 warnx("%s changes from %s to %s",
274 "optimization preference",
275 chg[sblock.fs_optim], chg[optim]);
276 sblock.fs_optim = optim;
277 if (sblock.fs_minfree >= MINFREE &&
278 optim == FS_OPTSPACE)
279 warnx(OPTWARN, "time", ">=", MINFREE);
280 if (sblock.fs_minfree < MINFREE &&
281 optim == FS_OPTTIME)
282 warnx(OPTWARN, "space", "<", MINFREE);
283 }
284 }
285 CHANGEVAL(sblock.fs_trackskew, trackskew,
286 "track skew in sectors", "");
287 CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
288 "average file size", "");
289 CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
290 "expected number of files per directory", "");
291
292 if (Nflag) {
293 fprintf(stdout, "tunefs: current settings of %s\n", special);
294 fprintf(stdout, "\tmaximum contiguous block count %d\n",
295 sblock.fs_maxcontig);
296 fprintf(stdout,
297 "\trotational delay between contiguous blocks %dms\n",
298 sblock.fs_rotdelay);
299 fprintf(stdout,
300 "\tmaximum blocks per file in a cylinder group %d\n",
301 sblock.fs_maxbpg);
302 fprintf(stdout, "\tminimum percentage of free space %d%%\n",
303 sblock.fs_minfree);
304 #ifdef TUNEFS_SOFTDEP
305 fprintf(stdout, "\tsoft dependencies: %s\n",
306 (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
307 #endif
308 fprintf(stdout, "\toptimization preference: %s\n",
309 chg[sblock.fs_optim]);
310 fprintf(stdout, "\taverage file size: %d\n",
311 sblock.fs_avgfilesize);
312 fprintf(stdout,
313 "\texpected number of files per directory: %d\n",
314 sblock.fs_avgfpdir);
315 fprintf(stdout, "\ttrack skew %d sectors\n",
316 sblock.fs_trackskew);
317 fprintf(stdout, "tunefs: no changes made\n");
318 exit(0);
319 }
320
321 memcpy(buf, (char *)&sblock, SBSIZE);
322 if (needswap)
323 ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
324 bwrite((daddr_t)SBOFF / dev_bsize, buf, SBSIZE, special);
325 if (Aflag)
326 for (i = 0; i < sblock.fs_ncg; i++)
327 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
328 buf, SBSIZE, special);
329 close(fi);
330 exit(0);
331 }
332
333 static int
334 getnum(const char *num, const char *desc, int min, int max)
335 {
336 long n;
337 char *ep;
338
339 n = strtol(num, &ep, 10);
340 if (ep[0] != '\0')
341 errx(1, "Invalid number `%s' for %s", num, desc);
342 if ((int) n < min)
343 errx(1, "%s `%s' too small (minimum is %d)", desc, num, min);
344 if ((int) n > max)
345 errx(1, "%s `%s' too large (maximum is %d)", desc, num, max);
346 return ((int)n);
347 }
348
349 static void
350 usage(void)
351 {
352
353 fprintf(stderr, "Usage: tunefs [-AFN] tuneup-options special-device\n");
354 fprintf(stderr, "where tuneup-options are:\n");
355 fprintf(stderr, "\t-a maximum contiguous blocks\n");
356 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
357 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
358 fprintf(stderr, "\t-g average file size\n");
359 fprintf(stderr, "\t-h expected number of files per directory\n");
360 fprintf(stderr, "\t-k track skew in sectors\n");
361 fprintf(stderr, "\t-m minimum percentage of free space\n");
362 #ifdef TUNEFS_SOFTDEP
363 fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
364 #endif
365 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
366 exit(2);
367 }
368
369 static void
370 getsb(struct fs *fs, const char *file)
371 {
372
373 bread((daddr_t)SBOFF, (char *)fs, SBSIZE, file);
374 if (fs->fs_magic != FS_MAGIC) {
375 if (fs->fs_magic == bswap32(FS_MAGIC)) {
376 warnx("%s: swapping byte order", file);
377 needswap = 1;
378 ffs_sb_swap(fs, fs);
379 } else
380 err(5, "%s: bad magic number", file);
381 }
382 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
383 }
384
385 static void
386 bwrite(daddr_t blk, char *buffer, int size, const char *file)
387 {
388 off_t offset;
389
390 offset = (off_t)blk * dev_bsize;
391 if (lseek(fi, offset, SEEK_SET) == -1)
392 err(6, "%s: seeking to %lld", file, (long long)offset);
393 if (write(fi, buffer, size) != size)
394 err(7, "%s: writing %d bytes", file, size);
395 }
396
397 static void
398 bread(daddr_t blk, char *buffer, int cnt, const char *file)
399 {
400 off_t offset;
401 int i;
402
403 offset = (off_t)blk * dev_bsize;
404 if (lseek(fi, offset, SEEK_SET) == -1)
405 err(4, "%s: seeking to %lld", file, (long long)offset);
406 if ((i = read(fi, buffer, cnt)) != cnt)
407 errx(5, "%s: short read", file);
408 }
409