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