tunefs.c revision 1.20 1 /* $NetBSD: tunefs.c,v 1.20 2000/06/15 22:37:17 fvdl 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.20 2000/06/15 22:37:17 fvdl 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 <errno.h>
63 #include <err.h>
64 #include <fcntl.h>
65 #include <fstab.h>
66 #include <stdio.h>
67 #include <paths.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 void bwrite __P((daddr_t, char *, int));
87 int bread __P((daddr_t, char *, int));
88 void getsb __P((struct fs *, const char *));
89 int main __P((int, char *[]));
90 void usage __P((void));
91
92 int
93 main(argc, argv)
94 int argc;
95 char *argv[];
96 {
97 char *cp, *name;
98 #ifdef TUNEFS_SOFTDEP
99 char *action;
100 #endif
101 const char *special;
102 struct stat st;
103 int i;
104 int Aflag = 0, Nflag = 0;
105 struct fstab *fs;
106 char *chg[2], device[MAXPATHLEN];
107
108 argc--, argv++;
109 if (argc < 2)
110 usage();
111 special = argv[argc - 1];
112 fs = getfsfile(special);
113 if (fs)
114 special = fs->fs_spec;
115 again:
116 if (stat(special, &st) < 0) {
117 if (*special != '/') {
118 if (*special == 'r')
119 special++;
120 (void)snprintf(device, sizeof(device), "%s/%s",
121 _PATH_DEV, special);
122 special = device;
123 goto again;
124 }
125 err(1, "%s", special);
126 }
127 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode))
128 errx(10, "%s: not a block or character device", special);
129 getsb(&sblock, special);
130 chg[FS_OPTSPACE] = "space";
131 chg[FS_OPTTIME] = "time";
132 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
133 for (cp = &argv[0][1]; *cp; cp++)
134 switch (*cp) {
135
136 case 'A':
137 Aflag++;
138 continue;
139
140 case 'N':
141 Nflag++;
142 continue;
143
144 case 'a':
145 name = "maximum contiguous block count";
146 if (argc < 1)
147 errx(10, "-a: missing %s", name);
148 argc--, argv++;
149 i = atoi(*argv);
150 if (i < 1)
151 errx(10, "%s must be >= 1 (was %s)",
152 name, *argv);
153 warnx("%s changes from %d to %d",
154 name, sblock.fs_maxcontig, i);
155 sblock.fs_maxcontig = i;
156 continue;
157
158 case 'd':
159 name =
160 "rotational delay between contiguous blocks";
161 if (argc < 1)
162 errx(10, "-d: missing %s", name);
163 argc--, argv++;
164 i = atoi(*argv);
165 warnx("%s changes from %dms to %dms",
166 name, sblock.fs_rotdelay, i);
167 sblock.fs_rotdelay = i;
168 continue;
169
170 case 'e':
171 name =
172 "maximum blocks per file in a cylinder group";
173 if (argc < 1)
174 errx(10, "-e: missing %s", name);
175 argc--, argv++;
176 i = atoi(*argv);
177 if (i < 1)
178 errx(10, "%s must be >= 1 (was %s)",
179 name, *argv);
180 warnx("%s changes from %d to %d",
181 name, sblock.fs_maxbpg, i);
182 sblock.fs_maxbpg = i;
183 continue;
184
185 case 'm':
186 name = "minimum percentage of free space";
187 if (argc < 1)
188 errx(10, "-m: missing %s", name);
189 argc--, argv++;
190 i = atoi(*argv);
191 if (i < 0 || i > 99)
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;
196 if (i >= MINFREE &&
197 sblock.fs_optim == FS_OPTSPACE)
198 warnx(OPTWARN, "time", ">=", MINFREE);
199 if (i < MINFREE &&
200 sblock.fs_optim == FS_OPTTIME)
201 warnx(OPTWARN, "space", "<", MINFREE);
202 continue;
203 #ifdef TUNEFS_SOFTDEP
204 case 'n':
205 name = "soft dependencies";
206 if (argc < 1)
207 errx(10, "-n: missing %s", name);
208 argc--, argv++;
209 if (strcmp(*argv, "enable") == 0) {
210 sblock.fs_flags |= FS_DOSOFTDEP;
211 action = "set";
212 } else if (strcmp(*argv, "disable") == 0) {
213 sblock.fs_flags &= ~FS_DOSOFTDEP;
214 action = "cleared";
215 } else {
216 errx(10, "bad %s (options are %s)",
217 name, "`enable' or `disable'");
218 }
219 warnx("%s %s", name, action);
220 continue;
221 #endif
222
223 case 'o':
224 name = "optimization preference";
225 if (argc < 1)
226 errx(10, "-o: missing %s", name);
227 argc--, argv++;
228 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
229 i = FS_OPTSPACE;
230 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
231 i = FS_OPTTIME;
232 else
233 errx(10, "bad %s (options are `space' or `time')",
234 name);
235 if (sblock.fs_optim == i) {
236 warnx("%s remains unchanged as %s",
237 name, chg[i]);
238 continue;
239 }
240 warnx("%s changes from %s to %s",
241 name, chg[sblock.fs_optim], chg[i]);
242 sblock.fs_optim = i;
243 if (sblock.fs_minfree >= MINFREE &&
244 i == FS_OPTSPACE)
245 warnx(OPTWARN, "time", ">=", MINFREE);
246 if (sblock.fs_minfree < MINFREE &&
247 i == FS_OPTTIME)
248 warnx(OPTWARN, "space", "<", MINFREE);
249 continue;
250
251 case 't':
252 name = "track skew in sectors";
253 if (argc < 1)
254 errx(10, "-t: missing %s", name);
255 argc--, argv++;
256 i = atoi(*argv);
257 if (i < 0)
258 errx(10, "%s: %s must be >= 0",
259 *argv, name);
260 warnx("%s changes from %d to %d",
261 name, sblock.fs_trackskew, i);
262 sblock.fs_trackskew = i;
263 continue;
264
265 default:
266 usage();
267 }
268 }
269 if (argc != 1)
270 usage();
271 if (Nflag) {
272 fprintf(stdout, "tunefs: current settings\n");
273 fprintf(stdout, "\tmaximum contiguous block count %d\n",
274 sblock.fs_maxcontig);
275 fprintf(stdout,
276 "\trotational delay between contiguous blocks %dms\n",
277 sblock.fs_rotdelay);
278 fprintf(stdout,
279 "\tmaximum blocks per file in a cylinder group %d\n",
280 sblock.fs_maxbpg);
281 fprintf(stdout, "\tminimum percentage of free space %d%%\n",
282 sblock.fs_minfree);
283 #ifdef TUNEFS_SOFTDEP
284 fprintf(stdout, "\tsoft dependencies: %s\n",
285 (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off");
286 #endif
287 fprintf(stdout, "\toptimization preference: %s\n",
288 chg[sblock.fs_optim]);
289 fprintf(stdout, "\ttrack skew %d sectors\n",
290 sblock.fs_trackskew);
291 fprintf(stdout, "tunefs: no changes made\n");
292 exit(0);
293 }
294 fi = open(special, 1);
295 if (fi < 0)
296 err(3, "cannot open %s for writing", special);
297 memcpy(buf, (char *)&sblock, SBSIZE);
298 if (needswap)
299 ffs_sb_swap((struct fs*)buf, (struct fs*)buf, 1);
300 bwrite((daddr_t)SBOFF / dev_bsize, buf, SBSIZE);
301 if (Aflag)
302 for (i = 0; i < sblock.fs_ncg; i++)
303 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
304 buf, SBSIZE);
305 close(fi);
306 exit(0);
307 }
308
309 void
310 usage()
311 {
312
313 fprintf(stderr, "Usage: tunefs [-AN] tuneup-options special-device\n");
314 fprintf(stderr, "where tuneup-options are:\n");
315 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
316 fprintf(stderr, "\t-a maximum contiguous blocks\n");
317 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
318 fprintf(stderr, "\t-m minimum percentage of free space\n");
319 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
320 #ifdef TUNEFS_SOFTDEP
321 fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n");
322 #endif
323 fprintf(stderr, "\t-t track skew in sectors\n");
324 exit(2);
325 }
326
327 void
328 getsb(fs, file)
329 struct fs *fs;
330 const char *file;
331 {
332
333 fi = open(file, 0);
334 if (fi < 0)
335 err(3, "cannot open %s for reading", file);
336 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
337 err(4, "%s: bad super block", file);
338 if (fs->fs_magic != FS_MAGIC) {
339 if (fs->fs_magic == bswap32(FS_MAGIC)) {
340 needswap = 1;
341 ffs_sb_swap(fs, fs, 0);
342 } else
343 err(5, "%s: bad magic number", file);
344 }
345 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
346 close(fi);
347 }
348
349 void
350 bwrite(blk, buf, size)
351 daddr_t blk;
352 char *buf;
353 int size;
354 {
355
356 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
357 err(6, "FS SEEK");
358 if (write(fi, buf, size) != size)
359 err(7, "FS WRITE");
360 }
361
362 int
363 bread(bno, buf, cnt)
364 daddr_t bno;
365 char *buf;
366 int cnt;
367 {
368 int i;
369
370 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
371 return(1);
372 if ((i = read(fi, buf, cnt)) != cnt) {
373 for(i=0; i<sblock.fs_bsize; i++)
374 buf[i] = 0;
375 return (1);
376 }
377 return (0);
378 }
379