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