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