Kernel tree reorganization stage 2: Major cvs repository work.
[dragonfly.git] / sbin / tunefs / tunefs.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#) Copyright (c) 1983, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)tunefs.c 8.2 (Berkeley) 4/19/94
35  * $FreeBSD: src/sbin/tunefs/tunefs.c,v 1.11.2.5 2001/10/14 21:50:39 iedowse Exp $
36  * $DragonFly: src/sbin/tunefs/tunefs.c,v 1.3 2003/08/08 04:18:41 dillon Exp $
37  */
38
39 /*
40  * tunefs: change layout parameters to an existing file system.
41  */
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45
46 #include <vfs/ufs/fs.h>
47 #include <vfs/ufs/ufsmount.h>
48
49 #include <err.h>
50 #include <fcntl.h>
51 #include <fstab.h>
52 #include <paths.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 /* the optimization warning string template */
59 #define OPTWARN "should optimize for %s with minfree %s %d%%"
60
61 union {
62         struct  fs sb;
63         char pad[MAXBSIZE];
64 } sbun;
65 #define sblock sbun.sb
66
67 int fi;
68 long dev_bsize = 1;
69
70 void bwrite __P((daddr_t, const char *, int));
71 int bread __P((daddr_t, char *, int));
72 void getsb __P((struct fs *, const char *));
73 void putsb __P((struct fs *, const char *, int));
74 void usage __P((void));
75 void printfs __P((void));
76
77 int
78 main(argc, argv)
79         int argc;
80         char *argv[];
81 {
82         char *cp, *special;
83         const char *name, *action;
84         struct stat st;
85         int i;
86         int Aflag = 0, active = 0;
87         struct fstab *fs;
88         const char *chg[2];
89         char device[MAXPATHLEN];
90         struct ufs_args args;
91         struct statfs stfs;
92
93         argc--, argv++;
94         if (argc < 2)
95                 usage();
96         special = argv[argc - 1];
97         fs = getfsfile(special);
98         if (fs) {
99                 if (statfs(special, &stfs) == 0 &&
100                     strcmp(special, stfs.f_mntonname) == 0) {
101                         active = 1;
102                 }
103                 special = fs->fs_spec;
104         }
105 again:
106         if (stat(special, &st) < 0) {
107                 if (*special != '/') {
108                         if (*special == 'r')
109                                 special++;
110                         (void)snprintf(device, sizeof(device), "%s%s",
111                                        _PATH_DEV, special);
112                         special = device;
113                         goto again;
114                 }
115                 err(1, "%s", special);
116         }
117         if (fs == NULL && (st.st_mode & S_IFMT) == S_IFDIR)
118                 errx(10, "%s: unknown file system", special);
119         getsb(&sblock, special);
120         for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
121                 for (cp = &argv[0][1]; *cp; cp++)
122                         switch (*cp) {
123
124                         case 'A':
125                                 Aflag++;
126                                 continue;
127
128                         case 'p':
129                                 printfs();
130                                 exit(0);
131
132                         case 'a':
133                                 name = "maximum contiguous block count";
134                                 if (argc < 1)
135                                         errx(10, "-a: missing %s", name);
136                                 argc--, argv++;
137                                 i = atoi(*argv);
138                                 if (i < 1)
139                                         errx(10, "%s must be >= 1 (was %s)",
140                                             name, *argv);
141                                 warnx("%s changes from %d to %d",
142                                     name, sblock.fs_maxcontig, i);
143                                 sblock.fs_maxcontig = i;
144                                 continue;
145
146                         case 'd':
147                                 name =
148                                    "rotational delay between contiguous blocks";
149                                 if (argc < 1)
150                                         errx(10, "-d: missing %s", name);
151                                 argc--, argv++;
152                                 i = atoi(*argv);
153                                 warnx("%s changes from %dms to %dms",
154                                     name, sblock.fs_rotdelay, i);
155                                 sblock.fs_rotdelay = i;
156                                 continue;
157
158                         case 'e':
159                                 name =
160                                   "maximum blocks per file in a cylinder group";
161                                 if (argc < 1)
162                                         errx(10, "-e: missing %s", name);
163                                 argc--, argv++;
164                                 i = atoi(*argv);
165                                 if (i < 1)
166                                         errx(10, "%s must be >= 1 (was %s)",
167                                             name, *argv);
168                                 warnx("%s changes from %d to %d",
169                                     name, sblock.fs_maxbpg, i);
170                                 sblock.fs_maxbpg = i;
171                                 continue;
172
173                         case 'f':
174                                 name = "average file size";
175                                 if (argc < 1)
176                                         errx(10, "-a: missing %s", name);
177                                 argc--, argv++;
178                                 i = atoi(*argv);
179                                 if (i < 1)
180                                         errx(10, "%s must be >= 1 (was %s)", name, *argv);
181                                 if (sblock.fs_avgfilesize == i) {
182                                         warnx("%s remains unchanged as %d",
183                                                 name, i);
184                                 } else {
185                                         warnx("%s changes from %d to %d",
186                                                 name, sblock.fs_avgfilesize, i);
187                                         sblock.fs_avgfilesize = i;
188                                 }
189                                 break;
190
191                         case 'm':
192                                 name = "minimum percentage of free space";
193                                 if (argc < 1)
194                                         errx(10, "-m: missing %s", name);
195                                 argc--, argv++;
196                                 i = atoi(*argv);
197                                 if (i < 0 || i > 99)
198                                         errx(10, "bad %s (%s)", name, *argv);
199                                 warnx("%s changes from %d%% to %d%%",
200                                     name, sblock.fs_minfree, i);
201                                 sblock.fs_minfree = i;
202                                 if (i >= MINFREE &&
203                                     sblock.fs_optim == FS_OPTSPACE)
204                                         warnx(OPTWARN, "time", ">=", MINFREE);
205                                 if (i < MINFREE &&
206                                     sblock.fs_optim == FS_OPTTIME)
207                                         warnx(OPTWARN, "space", "<", MINFREE);
208                                 continue;
209
210                         case 'n':
211                                 name = "soft updates";
212                                 if (argc < 1)
213                                         errx(10, "-n: missing %s", name);
214                                 argc--, argv++;
215                                 if (strcmp(*argv, "enable") == 0) {
216                                         sblock.fs_flags |= FS_DOSOFTDEP;
217                                         action = "set";
218                                 } else if (strcmp(*argv, "disable") == 0) {
219                                         sblock.fs_flags &= ~FS_DOSOFTDEP;
220                                         action = "cleared";
221                                 } else {
222                                         errx(10, "bad %s (options are %s)",
223                                             name, "`enable' or `disable'");
224                                 }
225                                 warnx("%s %s", name, action);
226                                 continue;
227  
228                         case 'o':
229                                 name = "optimization preference";
230                                 if (argc < 1)
231                                         errx(10, "-o: missing %s", name);
232                                 argc--, argv++;
233                                 chg[FS_OPTSPACE] = "space";
234                                 chg[FS_OPTTIME] = "time";
235                                 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
236                                         i = FS_OPTSPACE;
237                                 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
238                                         i = FS_OPTTIME;
239                                 else
240                                         errx(10, "bad %s (options are `space' or `time')",
241                                             name);
242                                 if (sblock.fs_optim == i) {
243                                         warnx("%s remains unchanged as %s",
244                                             name, chg[i]);
245                                         continue;
246                                 }
247                                 warnx("%s changes from %s to %s",
248                                     name, chg[sblock.fs_optim], chg[i]);
249                                 sblock.fs_optim = i;
250                                 if (sblock.fs_minfree >= MINFREE &&
251                                     i == FS_OPTSPACE)
252                                         warnx(OPTWARN, "time", ">=", MINFREE);
253                                 if (sblock.fs_minfree < MINFREE &&
254                                     i == FS_OPTTIME)
255                                         warnx(OPTWARN, "space", "<", MINFREE);
256                                 continue;
257
258                         case 's':
259                                 name = "expected number of files per directory";
260                                 if (argc < 1)
261                                         errx(10, "-a: missing %s", name);
262                                 argc--, argv++;
263                                 i = atoi(*argv);
264                                 if (i < 1)
265                                         errx(10, "%s must be >= 1 (was %s)", name, *argv);
266                                 if (sblock.fs_avgfpdir == i) {
267                                         warnx("%s remains unchanged as %d",
268                                                 name, i);
269                                 } else {
270                                         warnx("%s changes from %d to %d",
271                                                 name, sblock.fs_avgfpdir, i);
272                                         sblock.fs_avgfpdir = i;
273                                 }
274                                 break;
275
276                         default:
277                                 usage();
278                         }
279         }
280         if (argc != 1)
281                 usage();
282         putsb(&sblock, special, Aflag);
283         if (active) {
284                 bzero(&args, sizeof(args));
285                 if (mount("ufs", fs->fs_file,
286                     stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
287                         err(9, "%s: reload", special);
288                 warnx("file system reloaded");
289         }
290         exit(0);
291 }
292
293 void
294 usage()
295 {
296         fprintf(stderr, "%s\n%s\n%s\n",
297 "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
298 "              [-m minfree] [-p] [-n enable | disable] [-o space | time]",
299 "              [-s filesperdir] [special | filesystem]");
300         exit(2);
301 }
302
303 void
304 getsb(fs, file)
305         struct fs *fs;
306         const char *file;
307 {
308
309         fi = open(file, O_RDONLY);
310         if (fi < 0)
311                 err(3, "cannot open %s", file);
312         if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
313                 err(4, "%s: bad super block", file);
314         if (fs->fs_magic != FS_MAGIC)
315                 errx(5, "%s: bad magic number", file);
316         dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
317 }
318
319 void
320 putsb(fs, file, all)
321         struct fs *fs;
322         const char *file;
323         int all;
324 {
325         int i;
326
327         /*
328          * Re-open the device read-write. Use the read-only file
329          * descriptor as an interlock to prevent the device from
330          * being mounted while we are switching mode.
331          */
332         i = fi;
333         fi = open(file, O_RDWR);
334         close(i);
335         if (fi < 0)
336                 err(3, "cannot open %s", file);
337         bwrite((daddr_t)SBOFF / dev_bsize, (const char *)fs, SBSIZE);
338         if (all)
339                 for (i = 0; i < fs->fs_ncg; i++)
340                         bwrite(fsbtodb(fs, cgsblock(fs, i)),
341                             (const char *)fs, SBSIZE);
342         close(fi);
343 }
344
345 void
346 printfs()
347 {
348         warnx("soft updates:  (-n)                                %s", 
349                 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
350         warnx("maximum contiguous block count: (-a)               %d",
351               sblock.fs_maxcontig);
352         warnx("rotational delay between contiguous blocks: (-d)   %d ms",
353               sblock.fs_rotdelay);
354         warnx("maximum blocks per file in a cylinder group: (-e)  %d",
355               sblock.fs_maxbpg);
356         warnx("average file size: (-f)                            %d",
357               sblock.fs_avgfilesize);
358         warnx("average number of files in a directory: (-s)       %d",
359               sblock.fs_avgfpdir);
360         warnx("minimum percentage of free space: (-m)             %d%%",
361               sblock.fs_minfree);
362         warnx("optimization preference: (-o)                      %s",
363               sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
364         if (sblock.fs_minfree >= MINFREE &&
365             sblock.fs_optim == FS_OPTSPACE)
366                 warnx(OPTWARN, "time", ">=", MINFREE);
367         if (sblock.fs_minfree < MINFREE &&
368             sblock.fs_optim == FS_OPTTIME)
369                 warnx(OPTWARN, "space", "<", MINFREE);
370 }
371
372 void
373 bwrite(blk, buf, size)
374         daddr_t blk;
375         const char *buf;
376         int size;
377 {
378
379         if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
380                 err(6, "FS SEEK");
381         if (write(fi, buf, size) != size)
382                 err(7, "FS WRITE");
383 }
384
385 int
386 bread(bno, buf, cnt)
387         daddr_t bno;
388         char *buf;
389         int cnt;
390 {
391         int i;
392
393         if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
394                 return(1);
395         if ((i = read(fi, buf, cnt)) != cnt) {
396                 for(i=0; i<sblock.fs_bsize; i++)
397                         buf[i] = 0;
398                 return (1);
399         }
400         return (0);
401 }