tunefs.c revision 1.8 1 1.1 cgd /*
2 1.8 mycroft * Copyright (c) 1983, 1993
3 1.8 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.8 mycroft static char copyright[] =
36 1.8 mycroft "@(#) Copyright (c) 1983, 1993\n\
37 1.8 mycroft The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.8 mycroft /*static char sccsid[] = "from: @(#)tunefs.c 8.2 (Berkeley) 4/19/94";*/
42 1.8 mycroft static char *rcsid = "$Id: tunefs.c,v 1.8 1994/06/08 19:36:13 mycroft Exp $";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * tunefs: change layout parameters to an existing file system.
47 1.1 cgd */
48 1.1 cgd #include <sys/param.h>
49 1.1 cgd #include <sys/stat.h>
50 1.8 mycroft
51 1.8 mycroft #include <ufs/ffs/fs.h>
52 1.8 mycroft
53 1.8 mycroft #include <errno.h>
54 1.6 cgd #include <err.h>
55 1.6 cgd #include <fcntl.h>
56 1.1 cgd #include <fstab.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <paths.h>
59 1.6 cgd #include <stdlib.h>
60 1.6 cgd #include <unistd.h>
61 1.6 cgd
62 1.6 cgd /* the optimization warning string template */
63 1.8 mycroft #define OPTWARN "should optimize for %s with minfree %s %d%%"
64 1.1 cgd
65 1.1 cgd union {
66 1.1 cgd struct fs sb;
67 1.1 cgd char pad[MAXBSIZE];
68 1.1 cgd } sbun;
69 1.1 cgd #define sblock sbun.sb
70 1.1 cgd
71 1.1 cgd int fi;
72 1.1 cgd long dev_bsize = 1;
73 1.1 cgd
74 1.6 cgd void bwrite(daddr_t, char *, int);
75 1.6 cgd int bread(daddr_t, char *, int);
76 1.6 cgd void getsb(struct fs *, char *);
77 1.6 cgd void usage __P((void));
78 1.6 cgd
79 1.6 cgd int
80 1.1 cgd main(argc, argv)
81 1.1 cgd int argc;
82 1.1 cgd char *argv[];
83 1.1 cgd {
84 1.1 cgd char *cp, *special, *name;
85 1.1 cgd struct stat st;
86 1.1 cgd int i;
87 1.1 cgd int Aflag = 0;
88 1.1 cgd struct fstab *fs;
89 1.1 cgd char *chg[2], device[MAXPATHLEN];
90 1.1 cgd
91 1.1 cgd argc--, argv++;
92 1.1 cgd if (argc < 2)
93 1.6 cgd usage();
94 1.1 cgd special = argv[argc - 1];
95 1.1 cgd fs = getfsfile(special);
96 1.1 cgd if (fs)
97 1.1 cgd special = fs->fs_spec;
98 1.1 cgd again:
99 1.1 cgd if (stat(special, &st) < 0) {
100 1.1 cgd if (*special != '/') {
101 1.1 cgd if (*special == 'r')
102 1.1 cgd special++;
103 1.1 cgd (void)sprintf(device, "%s/%s", _PATH_DEV, special);
104 1.1 cgd special = device;
105 1.1 cgd goto again;
106 1.1 cgd }
107 1.6 cgd err(1, "%s", special);
108 1.1 cgd }
109 1.1 cgd if ((st.st_mode & S_IFMT) != S_IFBLK &&
110 1.1 cgd (st.st_mode & S_IFMT) != S_IFCHR)
111 1.8 mycroft errx(10, "%s: not a block or character device", special);
112 1.1 cgd getsb(&sblock, special);
113 1.1 cgd for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
114 1.1 cgd for (cp = &argv[0][1]; *cp; cp++)
115 1.1 cgd switch (*cp) {
116 1.1 cgd
117 1.1 cgd case 'A':
118 1.1 cgd Aflag++;
119 1.1 cgd continue;
120 1.1 cgd
121 1.1 cgd case 'a':
122 1.1 cgd name = "maximum contiguous block count";
123 1.1 cgd if (argc < 1)
124 1.8 mycroft errx(10, "-a: missing %s", name);
125 1.1 cgd argc--, argv++;
126 1.1 cgd i = atoi(*argv);
127 1.1 cgd if (i < 1)
128 1.8 mycroft errx(10, "%s must be >= 1 (was %s)",
129 1.6 cgd name, *argv);
130 1.6 cgd warnx("%s changes from %d to %d",
131 1.6 cgd name, sblock.fs_maxcontig, i);
132 1.1 cgd sblock.fs_maxcontig = i;
133 1.1 cgd continue;
134 1.1 cgd
135 1.1 cgd case 'd':
136 1.1 cgd name =
137 1.1 cgd "rotational delay between contiguous blocks";
138 1.1 cgd if (argc < 1)
139 1.8 mycroft errx(10, "-d: missing %s", name);
140 1.1 cgd argc--, argv++;
141 1.1 cgd i = atoi(*argv);
142 1.6 cgd warnx("%s changes from %dms to %dms",
143 1.6 cgd name, sblock.fs_rotdelay, i);
144 1.1 cgd sblock.fs_rotdelay = i;
145 1.1 cgd continue;
146 1.1 cgd
147 1.1 cgd case 'e':
148 1.1 cgd name =
149 1.1 cgd "maximum blocks per file in a cylinder group";
150 1.1 cgd if (argc < 1)
151 1.8 mycroft errx(10, "-e: missing %s", name);
152 1.1 cgd argc--, argv++;
153 1.1 cgd i = atoi(*argv);
154 1.1 cgd if (i < 1)
155 1.8 mycroft errx(10, "%s must be >= 1 (was %s)",
156 1.6 cgd name, *argv);
157 1.6 cgd warnx("%s changes from %d to %d",
158 1.6 cgd name, sblock.fs_maxbpg, i);
159 1.1 cgd sblock.fs_maxbpg = i;
160 1.1 cgd continue;
161 1.1 cgd
162 1.1 cgd case 'm':
163 1.1 cgd name = "minimum percentage of free space";
164 1.1 cgd if (argc < 1)
165 1.8 mycroft errx(10, "-m: missing %s", name);
166 1.1 cgd argc--, argv++;
167 1.1 cgd i = atoi(*argv);
168 1.1 cgd if (i < 0 || i > 99)
169 1.8 mycroft errx(10, "bad %s (%s)", name, *argv);
170 1.6 cgd warnx("%s changes from %d%% to %d%%",
171 1.6 cgd name, sblock.fs_minfree, i);
172 1.1 cgd sblock.fs_minfree = i;
173 1.6 cgd if (i >= MINFREE &&
174 1.6 cgd sblock.fs_optim == FS_OPTSPACE)
175 1.6 cgd warnx(OPTWARN, "time", ">=", MINFREE);
176 1.6 cgd if (i < MINFREE &&
177 1.6 cgd sblock.fs_optim == FS_OPTTIME)
178 1.6 cgd warnx(OPTWARN, "space", "<", MINFREE);
179 1.1 cgd continue;
180 1.1 cgd
181 1.1 cgd case 'o':
182 1.1 cgd name = "optimization preference";
183 1.1 cgd if (argc < 1)
184 1.8 mycroft errx(10, "-o: missing %s", name);
185 1.1 cgd argc--, argv++;
186 1.1 cgd chg[FS_OPTSPACE] = "space";
187 1.1 cgd chg[FS_OPTTIME] = "time";
188 1.1 cgd if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
189 1.1 cgd i = FS_OPTSPACE;
190 1.1 cgd else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
191 1.1 cgd i = FS_OPTTIME;
192 1.1 cgd else
193 1.8 mycroft errx(10, "bad %s (options are `space' or `time')",
194 1.6 cgd name);
195 1.1 cgd if (sblock.fs_optim == i) {
196 1.6 cgd warnx("%s remains unchanged as %s",
197 1.6 cgd name, chg[i]);
198 1.1 cgd continue;
199 1.1 cgd }
200 1.6 cgd warnx("%s changes from %s to %s",
201 1.6 cgd name, chg[sblock.fs_optim], chg[i]);
202 1.1 cgd sblock.fs_optim = i;
203 1.6 cgd if (sblock.fs_minfree >= MINFREE &&
204 1.6 cgd i == FS_OPTSPACE)
205 1.6 cgd warnx(OPTWARN, "time", ">=", MINFREE);
206 1.6 cgd if (sblock.fs_minfree < MINFREE &&
207 1.6 cgd i == FS_OPTTIME)
208 1.6 cgd warnx(OPTWARN, "space", "<", MINFREE);
209 1.1 cgd continue;
210 1.1 cgd
211 1.1 cgd default:
212 1.6 cgd usage();
213 1.1 cgd }
214 1.1 cgd }
215 1.1 cgd if (argc != 1)
216 1.6 cgd usage();
217 1.5 cgd bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
218 1.1 cgd if (Aflag)
219 1.1 cgd for (i = 0; i < sblock.fs_ncg; i++)
220 1.1 cgd bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
221 1.1 cgd (char *)&sblock, SBSIZE);
222 1.1 cgd close(fi);
223 1.1 cgd exit(0);
224 1.6 cgd }
225 1.6 cgd
226 1.6 cgd void
227 1.6 cgd usage()
228 1.6 cgd {
229 1.8 mycroft
230 1.8 mycroft fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
231 1.1 cgd fprintf(stderr, "where tuneup-options are:\n");
232 1.1 cgd fprintf(stderr, "\t-a maximum contiguous blocks\n");
233 1.1 cgd fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
234 1.1 cgd fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
235 1.1 cgd fprintf(stderr, "\t-m minimum percentage of free space\n");
236 1.1 cgd fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
237 1.8 mycroft exit(2);
238 1.1 cgd }
239 1.1 cgd
240 1.6 cgd void
241 1.1 cgd getsb(fs, file)
242 1.1 cgd register struct fs *fs;
243 1.1 cgd char *file;
244 1.1 cgd {
245 1.1 cgd
246 1.1 cgd fi = open(file, 2);
247 1.6 cgd if (fi < 0)
248 1.8 mycroft err(3, "cannot open %s", file);
249 1.6 cgd if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
250 1.8 mycroft err(4, "%s: bad super block", file);
251 1.6 cgd if (fs->fs_magic != FS_MAGIC)
252 1.8 mycroft err(5, "%s: bad magic number", file);
253 1.1 cgd dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
254 1.1 cgd }
255 1.1 cgd
256 1.6 cgd void
257 1.8 mycroft bwrite(blk, buf, size)
258 1.8 mycroft daddr_t blk;
259 1.1 cgd char *buf;
260 1.6 cgd int size;
261 1.1 cgd {
262 1.8 mycroft
263 1.8 mycroft if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
264 1.8 mycroft err(6, "FS SEEK");
265 1.6 cgd if (write(fi, buf, size) != size)
266 1.8 mycroft err(7, "FS WRITE");
267 1.1 cgd }
268 1.1 cgd
269 1.6 cgd int
270 1.1 cgd bread(bno, buf, cnt)
271 1.1 cgd daddr_t bno;
272 1.1 cgd char *buf;
273 1.6 cgd int cnt;
274 1.1 cgd {
275 1.8 mycroft int i;
276 1.1 cgd
277 1.8 mycroft if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
278 1.1 cgd return(1);
279 1.1 cgd if ((i = read(fi, buf, cnt)) != cnt) {
280 1.1 cgd for(i=0; i<sblock.fs_bsize; i++)
281 1.1 cgd buf[i] = 0;
282 1.1 cgd return (1);
283 1.1 cgd }
284 1.1 cgd return (0);
285 1.1 cgd }
286