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