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 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 static char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94"; 42 #endif /* not lint */ 43 44 /* 45 * tunefs: change layout parameters to an existing file system. 46 */ 47 #include <sys/param.h> 48 #include <sys/stat.h> 49 50 #include <ufs/ffs/fs.h> 51 52 #include <errno.h> 53 #include <err.h> 54 #include <fcntl.h> 55 #include <fstab.h> 56 #include <stdio.h> 57 #include <paths.h> 58 #include <stdlib.h> 59 #include <unistd.h> 60 61 /* the optimization warning string template */ 62 #define OPTWARN "should optimize for %s with minfree %s %d%%" 63 64 union { 65 struct fs sb; 66 char pad[MAXBSIZE]; 67 } sbun; 68 #define sblock sbun.sb 69 70 int fi; 71 long dev_bsize = 1; 72 73 void bwrite(daddr_t, char *, int); 74 int bread(daddr_t, char *, int); 75 void getsb(struct fs *, char *); 76 void usage __P((void)); 77 void printfs __P((void)); 78 79 int 80 main(argc, argv) 81 int argc; 82 char *argv[]; 83 { 84 char *cp, *special, *name; 85 struct stat st; 86 int i; 87 int Aflag = 0; 88 struct fstab *fs; 89 char *chg[2], device[MAXPATHLEN]; 90 91 argc--, argv++; 92 if (argc < 2) 93 usage(); 94 special = argv[argc - 1]; 95 fs = getfsfile(special); 96 if (fs) 97 special = fs->fs_spec; 98 again: 99 if (stat(special, &st) < 0) { 100 if (*special != '/') { 101 if (*special == 'r') 102 special++; 103 (void)sprintf(device, "%s/%s", _PATH_DEV, special); 104 special = device; 105 goto again; 106 } 107 err(1, "%s", special); 108 } 109 if ((st.st_mode & S_IFMT) != S_IFBLK && 110 (st.st_mode & S_IFMT) != S_IFCHR) 111 errx(10, "%s: not a block or character device", special); 112 getsb(&sblock, special); 113 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 114 for (cp = &argv[0][1]; *cp; cp++) 115 switch (*cp) { 116 117 case 'A': 118 Aflag++; 119 continue; 120 121 case 'p': 122 printfs(); 123 exit(0); 124 125 case 'a': 126 name = "maximum contiguous block count"; 127 if (argc < 1) 128 errx(10, "-a: missing %s", name); 129 argc--, argv++; 130 i = atoi(*argv); 131 if (i < 1) 132 errx(10, "%s must be >= 1 (was %s)", 133 name, *argv); 134 warnx("%s changes from %d to %d", 135 name, sblock.fs_maxcontig, i); 136 sblock.fs_maxcontig = i; 137 continue; 138 139 case 'd': 140 name = 141 "rotational delay between contiguous blocks"; 142 if (argc < 1) 143 errx(10, "-d: missing %s", name); 144 argc--, argv++; 145 i = atoi(*argv); 146 warnx("%s changes from %dms to %dms", 147 name, sblock.fs_rotdelay, i); 148 sblock.fs_rotdelay = i; 149 continue; 150 151 case 'e': 152 name = 153 "maximum blocks per file in a cylinder group"; 154 if (argc < 1) 155 errx(10, "-e: missing %s", name); 156 argc--, argv++; 157 i = atoi(*argv); 158 if (i < 1) 159 errx(10, "%s must be >= 1 (was %s)", 160 name, *argv); 161 warnx("%s changes from %d to %d", 162 name, sblock.fs_maxbpg, i); 163 sblock.fs_maxbpg = i; 164 continue; 165 166 case 'm': 167 name = "minimum percentage of free space"; 168 if (argc < 1) 169 errx(10, "-m: missing %s", name); 170 argc--, argv++; 171 i = atoi(*argv); 172 if (i < 0 || i > 99) 173 errx(10, "bad %s (%s)", name, *argv); 174 warnx("%s changes from %d%% to %d%%", 175 name, sblock.fs_minfree, i); 176 sblock.fs_minfree = i; 177 if (i >= MINFREE && 178 sblock.fs_optim == FS_OPTSPACE) 179 warnx(OPTWARN, "time", ">=", MINFREE); 180 if (i < MINFREE && 181 sblock.fs_optim == FS_OPTTIME) 182 warnx(OPTWARN, "space", "<", MINFREE); 183 continue; 184 185 case 'o': 186 name = "optimization preference"; 187 if (argc < 1) 188 errx(10, "-o: missing %s", name); 189 argc--, argv++; 190 chg[FS_OPTSPACE] = "space"; 191 chg[FS_OPTTIME] = "time"; 192 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0) 193 i = FS_OPTSPACE; 194 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0) 195 i = FS_OPTTIME; 196 else 197 errx(10, "bad %s (options are `space' or `time')", 198 name); 199 if (sblock.fs_optim == i) { 200 warnx("%s remains unchanged as %s", 201 name, chg[i]); 202 continue; 203 } 204 warnx("%s changes from %s to %s", 205 name, chg[sblock.fs_optim], chg[i]); 206 sblock.fs_optim = i; 207 if (sblock.fs_minfree >= MINFREE && 208 i == FS_OPTSPACE) 209 warnx(OPTWARN, "time", ">=", MINFREE); 210 if (sblock.fs_minfree < MINFREE && 211 i == FS_OPTTIME) 212 warnx(OPTWARN, "space", "<", MINFREE); 213 continue; 214 215 default: 216 usage(); 217 } 218 } 219 if (argc != 1) 220 usage(); 221 bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE); 222 if (Aflag) 223 for (i = 0; i < sblock.fs_ncg; i++) 224 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)), 225 (char *)&sblock, SBSIZE); 226 close(fi); 227 exit(0); 228 } 229 230 void 231 usage() 232 { 233 234 fprintf(stderr, "Usage: tunefs tuneup-options special-device\n"); 235 fprintf(stderr, "where tuneup-options are:\n"); 236 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 237 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n"); 238 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 239 fprintf(stderr, "\t-m minimum percentage of free space\n"); 240 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 241 fprintf(stderr, "\t-p no change - just prints current tuneable settings\n"); 242 exit(2); 243 } 244 245 void 246 getsb(fs, file) 247 register struct fs *fs; 248 char *file; 249 { 250 251 fi = open(file, 2); 252 if (fi < 0) 253 err(3, "cannot open %s", file); 254 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) 255 err(4, "%s: bad super block", file); 256 if (fs->fs_magic != FS_MAGIC) 257 err(5, "%s: bad magic number", file); 258 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 259 } 260 261 void 262 printfs() 263 { 264 warnx("maximum contiguous block count: (-a) %d", 265 sblock.fs_maxcontig); 266 warnx("rotational delay between contiguous blocks: (-d) %d ms", 267 sblock.fs_rotdelay); 268 warnx("maximum blocks per file in a cylinder group: (-e) %d", 269 sblock.fs_maxbpg); 270 warnx("minimum percentage of free space: (-m) %d%%", 271 sblock.fs_minfree); 272 warnx("optimization preference: (-o) %s", 273 sblock.fs_optim == FS_OPTSPACE ? "space" : "time"); 274 if (sblock.fs_minfree >= MINFREE && 275 sblock.fs_optim == FS_OPTSPACE) 276 warnx(OPTWARN, "time", ">=", MINFREE); 277 if (sblock.fs_minfree < MINFREE && 278 sblock.fs_optim == FS_OPTTIME) 279 warnx(OPTWARN, "space", "<", MINFREE); 280 } 281 282 void 283 bwrite(blk, buf, size) 284 daddr_t blk; 285 char *buf; 286 int size; 287 { 288 289 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) 290 err(6, "FS SEEK"); 291 if (write(fi, buf, size) != size) 292 err(7, "FS WRITE"); 293 } 294 295 int 296 bread(bno, buf, cnt) 297 daddr_t bno; 298 char *buf; 299 int cnt; 300 { 301 int i; 302 303 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0) 304 return(1); 305 if ((i = read(fi, buf, cnt)) != cnt) { 306 for(i=0; i<sblock.fs_bsize; i++) 307 buf[i] = 0; 308 return (1); 309 } 310 return (0); 311 } 312