Initial import from FreeBSD RELENG_4:
[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
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)tunefs.c    8.2 (Berkeley) 4/19/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/sbin/tunefs/tunefs.c,v 1.11.2.5 2001/10/14 21:50:39 iedowse Exp $";
46 #endif /* not lint */
47
48 /*
49  * tunefs: change layout parameters to an existing file system.
50  */
51 #include <sys/param.h>
52 #include <sys/mount.h>
53 #include <sys/stat.h>
54
55 #include <ufs/ffs/fs.h>
56 #include <ufs/ufs/ufsmount.h>
57
58 #include <err.h>
59 #include <fcntl.h>
60 #include <fstab.h>
61 #include <paths.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <unistd.h>
66
67 /* the optimization warning string template */
68 #define OPTWARN "should optimize for %s with minfree %s %d%%"
69
70 union {
71         struct  fs sb;
72         char pad[MAXBSIZE];
73 } sbun;
74 #define sblock sbun.sb
75
76 int fi;
77 long dev_bsize = 1;
78
79 void bwrite __P((daddr_t, const char *, int));
80 int bread __P((daddr_t, char *, int));
81 void getsb __P((struct fs *, const char *));
82 void putsb __P((struct fs *, const char *, int));
83 void usage __P((void));
84 void printfs __P((void));
85
86 int
87 main(argc, argv)
88         int argc;
89         char *argv[];
90 {
91         char *cp, *special;
92         const char *name, *action;
93         struct stat st;
94         int i;
95         int Aflag = 0, active = 0;
96         struct fstab *fs;
97         const char *chg[2];
98         char device[MAXPATHLEN];
99         struct ufs_args args;
100         struct statfs stfs;
101
102         argc--, argv++;
103         if (argc < 2)
104                 usage();
105         special = argv[argc - 1];
106         fs = getfsfile(special);
107         if (fs) {
108                 if (statfs(special, &stfs) == 0 &&
109                     strcmp(special, stfs.f_mntonname) == 0) {
110                         active = 1;
111                 }
112                 special = fs->fs_spec;
113         }
114 again:
115         if (stat(special, &st) < 0) {
116                 if (*special != '/') {
117                         if (*special == 'r')
118                                 special++;
119                         (void)snprintf(device, sizeof(device), "%s%s",
120                                        _PATH_DEV, special);
121                         special = device;
122                         goto again;
123                 }
124                 err(1, "%s", special);
125         }
126         if (fs == NULL && (st.st_mode & S_IFMT) == S_IFDIR)
127                 errx(10, "%s: unknown file system", special);
128         getsb(&sblock, special);
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 'p':
138                                 printfs();
139                                 exit(0);
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 'f':
183                                 name = "average file size";
184                                 if (argc < 1)
185                                         errx(10, "-a: missing %s", name);
186                                 argc--, argv++;
187                                 i = atoi(*argv);
188                                 if (i < 1)
189                                         errx(10, "%s must be >= 1 (was %s)", name, *argv);
190                                 if (sblock.fs_avgfilesize == i) {
191                                         warnx("%s remains unchanged as %d",
192                                                 name, i);
193                                 } else {
194                                         warnx("%s changes from %d to %d",
195                                                 name, sblock.fs_avgfilesize, i);
196                                         sblock.fs_avgfilesize = i;
197                                 }
198                                 break;
199
200                         case 'm':
201                                 name = "minimum percentage of free space";
202                                 if (argc < 1)
203                                         errx(10, "-m: missing %s", name);
204                                 argc--, argv++;
205                                 i = atoi(*argv);
206                                 if (i < 0 || i > 99)
207                                         errx(10, "bad %s (%s)", name, *argv);
208                                 warnx("%s changes from %d%% to %d%%",
209                                     name, sblock.fs_minfree, i);
210                                 sblock.fs_minfree = i;
211                                 if (i >= MINFREE &&
212                                     sblock.fs_optim == FS_OPTSPACE)
213                                         warnx(OPTWARN, "time", ">=", MINFREE);
214                                 if (i < MINFREE &&
215                                     sblock.fs_optim == FS_OPTTIME)
216                                         warnx(OPTWARN, "space", "<", MINFREE);
217                                 continue;
218
219                         case 'n':
220                                 name = "soft updates";
221                                 if (argc < 1)
222                                         errx(10, "-n: missing %s", name);
223                                 argc--, argv++;
224                                 if (strcmp(*argv, "enable") == 0) {
225                                         sblock.fs_flags |= FS_DOSOFTDEP;
226                                         action = "set";
227                                 } else if (strcmp(*argv, "disable") == 0) {
228                                         sblock.fs_flags &= ~FS_DOSOFTDEP;
229                                         action = "cleared";
230                                 } else {
231                                         errx(10, "bad %s (options are %s)",
232                                             name, "`enable' or `disable'");
233                                 }
234                                 warnx("%s %s", name, action);
235                                 continue;
236  
237                         case 'o':
238                                 name = "optimization preference";
239                                 if (argc < 1)
240                                         errx(10, "-o: missing %s", name);
241                                 argc--, argv++;
242                                 chg[FS_OPTSPACE] = "space";
243                                 chg[FS_OPTTIME] = "time";
244                                 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
245                                         i = FS_OPTSPACE;
246                                 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
247                                         i = FS_OPTTIME;
248                                 else
249                                         errx(10, "bad %s (options are `space' or `time')",
250                                             name);
251                                 if (sblock.fs_optim == i) {
252                                         warnx("%s remains unchanged as %s",
253                                             name, chg[i]);
254                                         continue;
255                                 }
256                                 warnx("%s changes from %s to %s",
257                                     name, chg[sblock.fs_optim], chg[i]);
258                                 sblock.fs_optim = i;
259                                 if (sblock.fs_minfree >= MINFREE &&
260                                     i == FS_OPTSPACE)
261                                         warnx(OPTWARN, "time", ">=", MINFREE);
262                                 if (sblock.fs_minfree < MINFREE &&
263                                     i == FS_OPTTIME)
264                                         warnx(OPTWARN, "space", "<", MINFREE);
265                                 continue;
266
267                         case 's':
268                                 name = "expected number of files per directory";
269                                 if (argc < 1)
270                                         errx(10, "-a: missing %s", name);
271                                 argc--, argv++;
272                                 i = atoi(*argv);
273                                 if (i < 1)
274                                         errx(10, "%s must be >= 1 (was %s)", name, *argv);
275                                 if (sblock.fs_avgfpdir == i) {
276                                         warnx("%s remains unchanged as %d",
277                                                 name, i);
278                                 } else {
279                                         warnx("%s changes from %d to %d",
280                                                 name, sblock.fs_avgfpdir, i);
281                                         sblock.fs_avgfpdir = i;
282                                 }
283                                 break;
284
285                         default:
286                                 usage();
287                         }
288         }
289         if (argc != 1)
290                 usage();
291         putsb(&sblock, special, Aflag);
292         if (active) {
293                 bzero(&args, sizeof(args));
294                 if (mount("ufs", fs->fs_file,
295                     stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0)
296                         err(9, "%s: reload", special);
297                 warnx("file system reloaded");
298         }
299         exit(0);
300 }
301
302 void
303 usage()
304 {
305         fprintf(stderr, "%s\n%s\n%s\n",
306 "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]",
307 "              [-m minfree] [-p] [-n enable | disable] [-o space | time]",
308 "              [-s filesperdir] [special | filesystem]");
309         exit(2);
310 }
311
312 void
313 getsb(fs, file)
314         struct fs *fs;
315         const char *file;
316 {
317
318         fi = open(file, O_RDONLY);
319         if (fi < 0)
320                 err(3, "cannot open %s", file);
321         if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE))
322                 err(4, "%s: bad super block", file);
323         if (fs->fs_magic != FS_MAGIC)
324                 errx(5, "%s: bad magic number", file);
325         dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
326 }
327
328 void
329 putsb(fs, file, all)
330         struct fs *fs;
331         const char *file;
332         int all;
333 {
334         int i;
335
336         /*
337          * Re-open the device read-write. Use the read-only file
338          * descriptor as an interlock to prevent the device from
339          * being mounted while we are switching mode.
340          */
341         i = fi;
342         fi = open(file, O_RDWR);
343         close(i);
344         if (fi < 0)
345                 err(3, "cannot open %s", file);
346         bwrite((daddr_t)SBOFF / dev_bsize, (const char *)fs, SBSIZE);
347         if (all)
348                 for (i = 0; i < fs->fs_ncg; i++)
349                         bwrite(fsbtodb(fs, cgsblock(fs, i)),
350                             (const char *)fs, SBSIZE);
351         close(fi);
352 }
353
354 void
355 printfs()
356 {
357         warnx("soft updates:  (-n)                                %s", 
358                 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled");
359         warnx("maximum contiguous block count: (-a)               %d",
360               sblock.fs_maxcontig);
361         warnx("rotational delay between contiguous blocks: (-d)   %d ms",
362               sblock.fs_rotdelay);
363         warnx("maximum blocks per file in a cylinder group: (-e)  %d",
364               sblock.fs_maxbpg);
365         warnx("average file size: (-f)                            %d",
366               sblock.fs_avgfilesize);
367         warnx("average number of files in a directory: (-s)       %d",
368               sblock.fs_avgfpdir);
369         warnx("minimum percentage of free space: (-m)             %d%%",
370               sblock.fs_minfree);
371         warnx("optimization preference: (-o)                      %s",
372               sblock.fs_optim == FS_OPTSPACE ? "space" : "time");
373         if (sblock.fs_minfree >= MINFREE &&
374             sblock.fs_optim == FS_OPTSPACE)
375                 warnx(OPTWARN, "time", ">=", MINFREE);
376         if (sblock.fs_minfree < MINFREE &&
377             sblock.fs_optim == FS_OPTTIME)
378                 warnx(OPTWARN, "space", "<", MINFREE);
379 }
380
381 void
382 bwrite(blk, buf, size)
383         daddr_t blk;
384         const char *buf;
385         int size;
386 {
387
388         if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0)
389                 err(6, "FS SEEK");
390         if (write(fi, buf, size) != size)
391                 err(7, "FS WRITE");
392 }
393
394 int
395 bread(bno, buf, cnt)
396         daddr_t bno;
397         char *buf;
398         int cnt;
399 {
400         int i;
401
402         if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0)
403                 return(1);
404         if ((i = read(fi, buf, cnt)) != cnt) {
405                 for(i=0; i<sblock.fs_bsize; i++)
406                         buf[i] = 0;
407                 return (1);
408         }
409         return (0);
410 }