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