tunefs.c revision 1.1 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1983 The Regents of the University of California.
3 1.1 cgd * 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.1 cgd char copyright[] =
36 1.1 cgd "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
37 1.1 cgd All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1 cgd static char sccsid[] = "@(#)tunefs.c 5.11 (Berkeley) 6/1/90";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * tunefs: change layout parameters to an existing file system.
46 1.1 cgd */
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #include <sys/stat.h>
49 1.1 cgd #include <ufs/fs.h>
50 1.1 cgd #include <fstab.h>
51 1.1 cgd #include <stdio.h>
52 1.1 cgd #include <paths.h>
53 1.1 cgd
54 1.1 cgd union {
55 1.1 cgd struct fs sb;
56 1.1 cgd char pad[MAXBSIZE];
57 1.1 cgd } sbun;
58 1.1 cgd #define sblock sbun.sb
59 1.1 cgd
60 1.1 cgd int fi;
61 1.1 cgd long dev_bsize = 1;
62 1.1 cgd
63 1.1 cgd main(argc, argv)
64 1.1 cgd int argc;
65 1.1 cgd char *argv[];
66 1.1 cgd {
67 1.1 cgd char *cp, *special, *name;
68 1.1 cgd struct stat st;
69 1.1 cgd int i;
70 1.1 cgd int Aflag = 0;
71 1.1 cgd struct fstab *fs;
72 1.1 cgd char *chg[2], device[MAXPATHLEN];
73 1.1 cgd
74 1.1 cgd argc--, argv++;
75 1.1 cgd if (argc < 2)
76 1.1 cgd goto usage;
77 1.1 cgd special = argv[argc - 1];
78 1.1 cgd fs = getfsfile(special);
79 1.1 cgd if (fs)
80 1.1 cgd special = fs->fs_spec;
81 1.1 cgd again:
82 1.1 cgd if (stat(special, &st) < 0) {
83 1.1 cgd if (*special != '/') {
84 1.1 cgd if (*special == 'r')
85 1.1 cgd special++;
86 1.1 cgd (void)sprintf(device, "%s/%s", _PATH_DEV, special);
87 1.1 cgd special = device;
88 1.1 cgd goto again;
89 1.1 cgd }
90 1.1 cgd fprintf(stderr, "tunefs: "); perror(special);
91 1.1 cgd exit(1);
92 1.1 cgd }
93 1.1 cgd if ((st.st_mode & S_IFMT) != S_IFBLK &&
94 1.1 cgd (st.st_mode & S_IFMT) != S_IFCHR)
95 1.1 cgd fatal("%s: not a block or character device", special);
96 1.1 cgd getsb(&sblock, special);
97 1.1 cgd for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
98 1.1 cgd for (cp = &argv[0][1]; *cp; cp++)
99 1.1 cgd switch (*cp) {
100 1.1 cgd
101 1.1 cgd case 'A':
102 1.1 cgd Aflag++;
103 1.1 cgd continue;
104 1.1 cgd
105 1.1 cgd case 'a':
106 1.1 cgd name = "maximum contiguous block count";
107 1.1 cgd if (argc < 1)
108 1.1 cgd fatal("-a: missing %s", name);
109 1.1 cgd argc--, argv++;
110 1.1 cgd i = atoi(*argv);
111 1.1 cgd if (i < 1)
112 1.1 cgd fatal("%s: %s must be >= 1",
113 1.1 cgd *argv, name);
114 1.1 cgd fprintf(stdout, "%s changes from %d to %d\n",
115 1.1 cgd name, sblock.fs_maxcontig, i);
116 1.1 cgd sblock.fs_maxcontig = i;
117 1.1 cgd continue;
118 1.1 cgd
119 1.1 cgd case 'd':
120 1.1 cgd name =
121 1.1 cgd "rotational delay between contiguous blocks";
122 1.1 cgd if (argc < 1)
123 1.1 cgd fatal("-d: missing %s", name);
124 1.1 cgd argc--, argv++;
125 1.1 cgd i = atoi(*argv);
126 1.1 cgd fprintf(stdout,
127 1.1 cgd "%s changes from %dms to %dms\n",
128 1.1 cgd name, sblock.fs_rotdelay, i);
129 1.1 cgd sblock.fs_rotdelay = i;
130 1.1 cgd continue;
131 1.1 cgd
132 1.1 cgd case 'e':
133 1.1 cgd name =
134 1.1 cgd "maximum blocks per file in a cylinder group";
135 1.1 cgd if (argc < 1)
136 1.1 cgd fatal("-e: missing %s", name);
137 1.1 cgd argc--, argv++;
138 1.1 cgd i = atoi(*argv);
139 1.1 cgd if (i < 1)
140 1.1 cgd fatal("%s: %s must be >= 1",
141 1.1 cgd *argv, name);
142 1.1 cgd fprintf(stdout, "%s changes from %d to %d\n",
143 1.1 cgd name, sblock.fs_maxbpg, i);
144 1.1 cgd sblock.fs_maxbpg = i;
145 1.1 cgd continue;
146 1.1 cgd
147 1.1 cgd case 'm':
148 1.1 cgd name = "minimum percentage of free space";
149 1.1 cgd if (argc < 1)
150 1.1 cgd fatal("-m: missing %s", name);
151 1.1 cgd argc--, argv++;
152 1.1 cgd i = atoi(*argv);
153 1.1 cgd if (i < 0 || i > 99)
154 1.1 cgd fatal("%s: bad %s", *argv, name);
155 1.1 cgd fprintf(stdout,
156 1.1 cgd "%s changes from %d%% to %d%%\n",
157 1.1 cgd name, sblock.fs_minfree, i);
158 1.1 cgd sblock.fs_minfree = i;
159 1.1 cgd if (i >= 10 && sblock.fs_optim == FS_OPTSPACE)
160 1.1 cgd fprintf(stdout, "should optimize %s",
161 1.1 cgd "for time with minfree >= 10%\n");
162 1.1 cgd if (i < 10 && sblock.fs_optim == FS_OPTTIME)
163 1.1 cgd fprintf(stdout, "should optimize %s",
164 1.1 cgd "for space with minfree < 10%\n");
165 1.1 cgd continue;
166 1.1 cgd
167 1.1 cgd case 'o':
168 1.1 cgd name = "optimization preference";
169 1.1 cgd if (argc < 1)
170 1.1 cgd fatal("-o: missing %s", name);
171 1.1 cgd argc--, argv++;
172 1.1 cgd chg[FS_OPTSPACE] = "space";
173 1.1 cgd chg[FS_OPTTIME] = "time";
174 1.1 cgd if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
175 1.1 cgd i = FS_OPTSPACE;
176 1.1 cgd else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
177 1.1 cgd i = FS_OPTTIME;
178 1.1 cgd else
179 1.1 cgd fatal("%s: bad %s (options are `space' or `time')",
180 1.1 cgd *argv, name);
181 1.1 cgd if (sblock.fs_optim == i) {
182 1.1 cgd fprintf(stdout,
183 1.1 cgd "%s remains unchanged as %s\n",
184 1.1 cgd name, chg[i]);
185 1.1 cgd continue;
186 1.1 cgd }
187 1.1 cgd fprintf(stdout,
188 1.1 cgd "%s changes from %s to %s\n",
189 1.1 cgd name, chg[sblock.fs_optim], chg[i]);
190 1.1 cgd sblock.fs_optim = i;
191 1.1 cgd if (sblock.fs_minfree >= 10 && i == FS_OPTSPACE)
192 1.1 cgd fprintf(stdout, "should optimize %s",
193 1.1 cgd "for time with minfree >= 10%\n");
194 1.1 cgd if (sblock.fs_minfree < 10 && i == FS_OPTTIME)
195 1.1 cgd fprintf(stdout, "should optimize %s",
196 1.1 cgd "for space with minfree < 10%\n");
197 1.1 cgd continue;
198 1.1 cgd
199 1.1 cgd default:
200 1.1 cgd fatal("-%c: unknown flag", *cp);
201 1.1 cgd }
202 1.1 cgd }
203 1.1 cgd if (argc != 1)
204 1.1 cgd goto usage;
205 1.1 cgd bwrite(SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
206 1.1 cgd if (Aflag)
207 1.1 cgd for (i = 0; i < sblock.fs_ncg; i++)
208 1.1 cgd bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
209 1.1 cgd (char *)&sblock, SBSIZE);
210 1.1 cgd close(fi);
211 1.1 cgd exit(0);
212 1.1 cgd usage:
213 1.1 cgd fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
214 1.1 cgd fprintf(stderr, "where tuneup-options are:\n");
215 1.1 cgd fprintf(stderr, "\t-a maximum contiguous blocks\n");
216 1.1 cgd fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
217 1.1 cgd fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
218 1.1 cgd fprintf(stderr, "\t-m minimum percentage of free space\n");
219 1.1 cgd fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
220 1.1 cgd exit(2);
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd getsb(fs, file)
224 1.1 cgd register struct fs *fs;
225 1.1 cgd char *file;
226 1.1 cgd {
227 1.1 cgd
228 1.1 cgd fi = open(file, 2);
229 1.1 cgd if (fi < 0) {
230 1.1 cgd fprintf(stderr, "cannot open");
231 1.1 cgd perror(file);
232 1.1 cgd exit(3);
233 1.1 cgd }
234 1.1 cgd if (bread(SBOFF, (char *)fs, SBSIZE)) {
235 1.1 cgd fprintf(stderr, "bad super block");
236 1.1 cgd perror(file);
237 1.1 cgd exit(4);
238 1.1 cgd }
239 1.1 cgd if (fs->fs_magic != FS_MAGIC) {
240 1.1 cgd fprintf(stderr, "%s: bad magic number\n", file);
241 1.1 cgd exit(5);
242 1.1 cgd }
243 1.1 cgd dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd bwrite(blk, buf, size)
247 1.1 cgd char *buf;
248 1.1 cgd daddr_t blk;
249 1.1 cgd register size;
250 1.1 cgd {
251 1.1 cgd if (lseek(fi, blk * dev_bsize, 0) < 0) {
252 1.1 cgd perror("FS SEEK");
253 1.1 cgd exit(6);
254 1.1 cgd }
255 1.1 cgd if (write(fi, buf, size) != size) {
256 1.1 cgd perror("FS WRITE");
257 1.1 cgd exit(7);
258 1.1 cgd }
259 1.1 cgd }
260 1.1 cgd
261 1.1 cgd bread(bno, buf, cnt)
262 1.1 cgd daddr_t bno;
263 1.1 cgd char *buf;
264 1.1 cgd {
265 1.1 cgd register i;
266 1.1 cgd
267 1.1 cgd if (lseek(fi, bno * dev_bsize, 0) < 0)
268 1.1 cgd return(1);
269 1.1 cgd if ((i = read(fi, buf, cnt)) != cnt) {
270 1.1 cgd for(i=0; i<sblock.fs_bsize; i++)
271 1.1 cgd buf[i] = 0;
272 1.1 cgd return (1);
273 1.1 cgd }
274 1.1 cgd return (0);
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd /* VARARGS1 */
278 1.1 cgd fatal(fmt, arg1, arg2)
279 1.1 cgd char *fmt, *arg1, *arg2;
280 1.1 cgd {
281 1.1 cgd
282 1.1 cgd fprintf(stderr, "tunefs: ");
283 1.1 cgd fprintf(stderr, fmt, arg1, arg2);
284 1.1 cgd putc('\n', stderr);
285 1.1 cgd exit(10);
286 1.1 cgd }
287