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.6 2004/12/18 21:43:46 swildner 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(daddr_t, const char *, int); 71 int bread(daddr_t, char *, int); 72 void getsb(struct fs *, const char *); 73 void putsb(struct fs *, const char *, int); 74 void usage(void); 75 void printfs(void); 76 77 int 78 main(int argc, char **argv) 79 { 80 char *cp, *special; 81 const char *name, *action; 82 struct stat st; 83 int i; 84 int Aflag = 0, active = 0; 85 struct fstab *fs; 86 const char *chg[2]; 87 char device[MAXPATHLEN]; 88 struct ufs_args args; 89 struct statfs stfs; 90 91 argc--, argv++; 92 if (argc < 2) 93 usage(); 94 special = argv[argc - 1]; 95 fs = getfsfile(special); 96 if (fs) { 97 if (statfs(special, &stfs) == 0 && 98 strcmp(special, stfs.f_mntonname) == 0) { 99 active = 1; 100 } 101 special = fs->fs_spec; 102 } 103 again: 104 if (stat(special, &st) < 0) { 105 if (*special != '/') { 106 if (*special == 'r') 107 special++; 108 snprintf(device, sizeof(device), "%s%s", 109 _PATH_DEV, special); 110 special = device; 111 goto again; 112 } 113 err(1, "%s", special); 114 } 115 if (fs == NULL && (st.st_mode & S_IFMT) == S_IFDIR) 116 errx(10, "%s: unknown file system", special); 117 getsb(&sblock, special); 118 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 119 for (cp = &argv[0][1]; *cp; cp++) 120 switch (*cp) { 121 122 case 'A': 123 Aflag++; 124 continue; 125 126 case 'p': 127 printfs(); 128 exit(0); 129 130 case 'a': 131 name = "maximum contiguous block count"; 132 if (argc < 1) 133 errx(10, "-a: missing %s", name); 134 argc--, argv++; 135 i = atoi(*argv); 136 if (i < 1) 137 errx(10, "%s must be >= 1 (was %s)", 138 name, *argv); 139 warnx("%s changes from %d to %d", 140 name, sblock.fs_maxcontig, i); 141 sblock.fs_maxcontig = i; 142 continue; 143 144 case 'd': 145 name = 146 "rotational delay between contiguous blocks"; 147 if (argc < 1) 148 errx(10, "-d: missing %s", name); 149 argc--, argv++; 150 i = atoi(*argv); 151 warnx("%s changes from %dms to %dms", 152 name, sblock.fs_rotdelay, i); 153 sblock.fs_rotdelay = i; 154 continue; 155 156 case 'e': 157 name = 158 "maximum blocks per file in a cylinder group"; 159 if (argc < 1) 160 errx(10, "-e: missing %s", name); 161 argc--, argv++; 162 i = atoi(*argv); 163 if (i < 1) 164 errx(10, "%s must be >= 1 (was %s)", 165 name, *argv); 166 warnx("%s changes from %d to %d", 167 name, sblock.fs_maxbpg, i); 168 sblock.fs_maxbpg = i; 169 continue; 170 171 case 'f': 172 name = "average file size"; 173 if (argc < 1) 174 errx(10, "-a: missing %s", name); 175 argc--, argv++; 176 i = atoi(*argv); 177 if (i < 1) 178 errx(10, "%s must be >= 1 (was %s)", name, *argv); 179 if (sblock.fs_avgfilesize == i) { 180 warnx("%s remains unchanged as %d", 181 name, i); 182 } else { 183 warnx("%s changes from %d to %d", 184 name, sblock.fs_avgfilesize, i); 185 sblock.fs_avgfilesize = i; 186 } 187 break; 188 189 case 'm': 190 name = "minimum percentage of free space"; 191 if (argc < 1) 192 errx(10, "-m: missing %s", name); 193 argc--, argv++; 194 i = atoi(*argv); 195 if (i < 0 || i > 99) 196 errx(10, "bad %s (%s)", name, *argv); 197 warnx("%s changes from %d%% to %d%%", 198 name, sblock.fs_minfree, i); 199 sblock.fs_minfree = i; 200 if (i >= MINFREE && 201 sblock.fs_optim == FS_OPTSPACE) 202 warnx(OPTWARN, "time", ">=", MINFREE); 203 if (i < MINFREE && 204 sblock.fs_optim == FS_OPTTIME) 205 warnx(OPTWARN, "space", "<", MINFREE); 206 continue; 207 208 case 'n': 209 name = "soft updates"; 210 if (argc < 1) 211 errx(10, "-n: missing %s", name); 212 argc--, argv++; 213 if (strcmp(*argv, "enable") == 0) { 214 sblock.fs_flags |= FS_DOSOFTDEP; 215 action = "set"; 216 } else if (strcmp(*argv, "disable") == 0) { 217 sblock.fs_flags &= ~FS_DOSOFTDEP; 218 action = "cleared"; 219 } else { 220 errx(10, "bad %s (options are %s)", 221 name, "`enable' or `disable'"); 222 } 223 warnx("%s %s", name, action); 224 continue; 225 226 case 'o': 227 name = "optimization preference"; 228 if (argc < 1) 229 errx(10, "-o: missing %s", name); 230 argc--, argv++; 231 chg[FS_OPTSPACE] = "space"; 232 chg[FS_OPTTIME] = "time"; 233 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0) 234 i = FS_OPTSPACE; 235 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0) 236 i = FS_OPTTIME; 237 else 238 errx(10, "bad %s (options are `space' or `time')", 239 name); 240 if (sblock.fs_optim == i) { 241 warnx("%s remains unchanged as %s", 242 name, chg[i]); 243 continue; 244 } 245 warnx("%s changes from %s to %s", 246 name, chg[sblock.fs_optim], chg[i]); 247 sblock.fs_optim = i; 248 if (sblock.fs_minfree >= MINFREE && 249 i == FS_OPTSPACE) 250 warnx(OPTWARN, "time", ">=", MINFREE); 251 if (sblock.fs_minfree < MINFREE && 252 i == FS_OPTTIME) 253 warnx(OPTWARN, "space", "<", MINFREE); 254 continue; 255 256 case 's': 257 name = "expected number of files per directory"; 258 if (argc < 1) 259 errx(10, "-a: missing %s", name); 260 argc--, argv++; 261 i = atoi(*argv); 262 if (i < 1) 263 errx(10, "%s must be >= 1 (was %s)", name, *argv); 264 if (sblock.fs_avgfpdir == i) { 265 warnx("%s remains unchanged as %d", 266 name, i); 267 } else { 268 warnx("%s changes from %d to %d", 269 name, sblock.fs_avgfpdir, i); 270 sblock.fs_avgfpdir = i; 271 } 272 break; 273 274 default: 275 usage(); 276 } 277 } 278 if (argc != 1) 279 usage(); 280 putsb(&sblock, special, Aflag); 281 if (active) { 282 bzero(&args, sizeof(args)); 283 if (mount("ufs", fs->fs_file, 284 stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0) 285 err(9, "%s: reload", special); 286 warnx("file system reloaded"); 287 } 288 exit(0); 289 } 290 291 void 292 usage(void) 293 { 294 fprintf(stderr, "%s\n%s\n%s\n", 295 "usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-f avgfilesize]", 296 " [-m minfree] [-p] [-n enable | disable] [-o space | time]", 297 " [-s filesperdir] [special | filesystem]"); 298 exit(2); 299 } 300 301 void 302 getsb(struct fs *fs, const char *file) 303 { 304 305 fi = open(file, O_RDONLY); 306 if (fi < 0) 307 err(3, "cannot open %s", file); 308 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) 309 err(4, "%s: bad super block", file); 310 if (fs->fs_magic != FS_MAGIC) 311 errx(5, "%s: bad magic number", file); 312 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 313 } 314 315 void 316 putsb(struct fs *fs, const char *file, int all) 317 { 318 int i; 319 320 /* 321 * Re-open the device read-write. Use the read-only file 322 * descriptor as an interlock to prevent the device from 323 * being mounted while we are switching mode. 324 */ 325 i = fi; 326 fi = open(file, O_RDWR); 327 close(i); 328 if (fi < 0) 329 err(3, "cannot open %s", file); 330 bwrite((daddr_t)SBOFF / dev_bsize, (const char *)fs, SBSIZE); 331 if (all) 332 for (i = 0; i < fs->fs_ncg; i++) 333 bwrite(fsbtodb(fs, cgsblock(fs, i)), 334 (const char *)fs, SBSIZE); 335 close(fi); 336 } 337 338 void 339 printfs(void) 340 { 341 warnx("soft updates: (-n) %s", 342 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled"); 343 warnx("maximum contiguous block count: (-a) %d", 344 sblock.fs_maxcontig); 345 warnx("rotational delay between contiguous blocks: (-d) %d ms", 346 sblock.fs_rotdelay); 347 warnx("maximum blocks per file in a cylinder group: (-e) %d", 348 sblock.fs_maxbpg); 349 warnx("average file size: (-f) %d", 350 sblock.fs_avgfilesize); 351 warnx("average number of files in a directory: (-s) %d", 352 sblock.fs_avgfpdir); 353 warnx("minimum percentage of free space: (-m) %d%%", 354 sblock.fs_minfree); 355 warnx("optimization preference: (-o) %s", 356 sblock.fs_optim == FS_OPTSPACE ? "space" : "time"); 357 if (sblock.fs_minfree >= MINFREE && 358 sblock.fs_optim == FS_OPTSPACE) 359 warnx(OPTWARN, "time", ">=", MINFREE); 360 if (sblock.fs_minfree < MINFREE && 361 sblock.fs_optim == FS_OPTTIME) 362 warnx(OPTWARN, "space", "<", MINFREE); 363 } 364 365 void 366 bwrite(daddr_t blk, const char *buf, int size) 367 { 368 369 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) 370 err(6, "FS SEEK"); 371 if (write(fi, buf, size) != size) 372 err(7, "FS WRITE"); 373 } 374 375 int 376 bread(daddr_t bno, char *buf, int cnt) 377 { 378 int i; 379 380 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0) 381 return(1); 382 if ((i = read(fi, buf, cnt)) != cnt) { 383 for(i=0; i<sblock.fs_bsize; i++) 384 buf[i] = 0; 385 return (1); 386 } 387 return (0); 388 } 389