tunefs.c revision 1.24 1 /* $NetBSD: tunefs.c,v 1.24 2001/09/06 02:16:01 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.24 2001/09/06 02:16:01 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 #include <sys/stat.h>
55
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ffs/fs.h>
58 #include <ufs/ffs/ffs_extern.h>
59
60 #include <machine/bswap.h>
61
62 #include <err.h>
63 #include <errno.h>
64 #include <fcntl.h>
65 #include <fstab.h>
66 #include <paths.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.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);
87 static int bread(daddr_t, char *, int);
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 #ifdef TUNEFS_SOFTDEP
97 int softdep;
98 #define OPTSTRING "AFNa:d:e:g:h:k:m:n:o:t:"
99 #else
100 #define OPTSTRING "AFNa:d:e:g:h:k:m:o:t:"
101 #endif
102 struct stat st;
103 int i, ch, Aflag, Fflag, Nflag;
104 struct fstab *fs;
105 const char *special, *chg[2];
106 char device[MAXPATHLEN];
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 fs = getfsfile(special);
208 if (fs)
209 special = fs->fs_spec;
210 again:
211 if (stat(special, &st) < 0) {
212 if (!Fflag && *special != '/') {
213 if (*special == 'r')
214 special++;
215 (void)snprintf(device, sizeof(device), "%s/%s",
216 _PATH_DEV, special);
217 special = device;
218 goto again;
219 }
220 err(1, "%s", special);
221 }
222 if (Fflag) {
223 if (!S_ISREG(st.st_mode))
224 errx(10, "%s: not a regular file", special);
225 } else if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
226 errx(10, "%s: not a block or character device", special);
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 CHANGEVAL(sblock.fs_maxcontig, maxcontig,
242 "maximum contiguous block count", "");
243 CHANGEVAL(sblock.fs_rotdelay, rotdelay,
244 "rotational delay between contiguous blocks", "ms");
245 CHANGEVAL(sblock.fs_maxbpg, maxbpg,
246 "maximum blocks per file in a cylinder group", "");
247 CHANGEVAL(sblock.fs_minfree, minfree,
248 "minimum percentage of free space", "%");
249 if (minfree != -1) {
250 if (minfree >= MINFREE &&
251 sblock.fs_optim == FS_OPTSPACE)
252 warnx(OPTWARN, "time", ">=", MINFREE);
253 if (minfree < MINFREE &&
254 sblock.fs_optim == FS_OPTTIME)
255 warnx(OPTWARN, "space", "<", MINFREE);
256 }
257 #ifdef TUNEFS_SOFTDEP
258 if (softdep == 1) {
259 sblock.fs_flags |= FS_DOSOFTDEP;
260 warnx("soft dependencies set");
261 } else if (softdep == 0) {
262 sblock.fs_flags &= ~FS_DOSOFTDEP;
263 warnx("soft dependencies cleared");
264 }
265 #endif
266 if (optim != -1) {
267 if (sblock.fs_optim == optim) {
268 warnx("%s remains unchanged as %s",
269 "optimization preference",
270 chg[optim]);
271 } else {
272 warnx("%s changes from %s to %s",
273 "optimization preference",
274 chg[sblock.fs_optim], chg[optim]);
275 sblock.fs_optim = optim;
276 if (sblock.fs_minfree >= MINFREE &&
277 optim == FS_OPTSPACE)
278 warnx(OPTWARN, "time", ">=", MINFREE);
279 if (sblock.fs_minfree < MINFREE &&
280 optim == FS_OPTTIME)
281 warnx(OPTWARN, "space", "<", MINFREE);
282 }
283 }
284 CHANGEVAL(sblock.fs_trackskew, trackskew,
285 "track skew in sectors", "");
286 CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
287 "average file size", "");
288 CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
289 "expected number of files per directory", "");
290
291 if (Nflag) {
292 fprintf(stdout, "tunefs: current settings of %s\n", special);
293 fprintf(stdout, "\tmaximum contiguous block count %d\n",
294 sblock.fs_maxcontig);
295 fprintf(stdout,
296 "\trotational delay between contiguous blocks %dms\n",
297 sblock.fs_rotdelay);
298 fprintf(stdout,
299 "\tmaximum blocks per file in a cylinder group %d\n",
300 sblock.fs_maxbpg);
301 fprintf(stdout, "\tminimum percentage of free space %d%%\n",
302 sblock.fs_minfree);
303 #ifdef TUNEFS_SOFTDEP
304 fprintf(stdout, "\tsoft dependencies: %s\n",
305 (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
306 #endif
307 fprintf(stdout, "\toptimization preference: %s\n",
308 chg[sblock.fs_optim]);
309 fprintf(stdout, "\taverage file size: %d\n",
310 sblock.fs_avgfilesize);
311 fprintf(stdout,
312 "\texpected number of files per directory: %d\n",
313 sblock.fs_avgfpdir);
314 fprintf(stdout, "\ttrack skew %d sectors\n",
315 sblock.fs_trackskew);
316 fprintf(stdout, "tunefs: no changes made\n");
317 exit(0);
318 }
319
320 fi = open(special, 1);
321 if (fi < 0)
322 err(3, "cannot open %s for writing", special);
323 memcpy(buf, (char *)&sblock, SBSIZE);
324 if (needswap)
325 ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
326 bwrite((daddr_t)SBOFF / dev_bsize, buf, SBSIZE);
327 if (Aflag)
328 for (i = 0; i < sblock.fs_ncg; i++)
329 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
330 buf, SBSIZE);
331 close(fi);
332 exit(0);
333 }
334
335 static int
336 getnum(const char *num, const char *desc, int min, int max)
337 {
338 long n;
339 char *ep;
340
341 n = strtol(num, &ep, 10);
342 if (ep[0] != '\0')
343 errx(1, "Invalid number `%s' for %s", num, desc);
344 if ((int) n < min)
345 errx(1, "%s `%s' too small (minimum is %d)", desc, num, min);
346 if ((int) n > max)
347 errx(1, "%s `%s' too large (maximum is %d)", desc, num, max);
348 return ((int)n);
349 }
350
351 static void
352 usage(void)
353 {
354
355 fprintf(stderr, "Usage: tunefs [-AFN] tuneup-options special-device\n");
356 fprintf(stderr, "where tuneup-options are:\n");
357 fprintf(stderr, "\t-a maximum contiguous blocks\n");
358 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
359 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
360 fprintf(stderr, "\t-g average file size\n");
361 fprintf(stderr, "\t-h expected number of files per directory\n");
362 fprintf(stderr, "\t-k track skew in sectors\n");
363 fprintf(stderr, "\t-m minimum percentage of free space\n");
364 #ifdef TUNEFS_SOFTDEP
365 fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
366 #endif
367 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
368 exit(2);
369 }
370
371 static void
372 getsb(struct fs *fs, const char *file)
373 {
374
375 fi = open(file, 0);
376 if (fi < 0)
377 err(3, "cannot open %s for reading", file);
378 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
379 err(4, "%s: bad super block", file);
380 if (fs->fs_magic != FS_MAGIC) {
381 if (fs->fs_magic == bswap32(FS_MAGIC)) {
382 warnx("%s: swapping byte order", file);
383 needswap = 1;
384 ffs_sb_swap(fs, fs);
385 } else
386 err(5, "%s: bad magic number", file);
387 }
388 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
389 close(fi);
390 }
391
392 static void
393 bwrite(daddr_t blk, char *buffer, int size)
394 {
395
396 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
397 err(6, "FS SEEK");
398 if (write(fi, buffer, size) != size)
399 err(7, "FS WRITE");
400 }
401
402 static int
403 bread(daddr_t bno, char *buffer, int cnt)
404 {
405 int i;
406
407 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
408 return(1);
409 if ((i = read(fi, buffer, cnt)) != cnt) {
410 for(i=0; i<sblock.fs_bsize; i++)
411 buf[i] = 0;
412 return (1);
413 }
414 return (0);
415 }
416