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