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 78 int 79 main(argc, argv) 80 int argc; 81 char *argv[]; 82 { 83 char *cp, *special, *name; 84 struct stat st; 85 int i; 86 int Aflag = 0; 87 struct fstab *fs; 88 char *chg[2], device[MAXPATHLEN]; 89 90 argc--, argv++; 91 if (argc < 2) 92 usage(); 93 special = argv[argc - 1]; 94 fs = getfsfile(special); 95 if (fs) 96 special = fs->fs_spec; 97 again: 98 if (stat(special, &st) < 0) { 99 if (*special != '/') { 100 if (*special == 'r') 101 special++; 102 (void)sprintf(device, "%s/%s", _PATH_DEV, special); 103 special = device; 104 goto again; 105 } 106 err(1, "%s", special); 107 } 108 if ((st.st_mode & S_IFMT) != S_IFBLK && 109 (st.st_mode & S_IFMT) != S_IFCHR) 110 errx(10, "%s: not a block or character device", special); 111 getsb(&sblock, special); 112 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 113 for (cp = &argv[0][1]; *cp; cp++) 114 switch (*cp) { 115 116 case 'A': 117 Aflag++; 118 continue; 119 120 case 'a': 121 name = "maximum contiguous block count"; 122 if (argc < 1) 123 errx(10, "-a: missing %s", name); 124 argc--, argv++; 125 i = atoi(*argv); 126 if (i < 1) 127 errx(10, "%s must be >= 1 (was %s)", 128 name, *argv); 129 warnx("%s changes from %d to %d", 130 name, sblock.fs_maxcontig, i); 131 sblock.fs_maxcontig = i; 132 continue; 133 134 case 'd': 135 name = 136 "rotational delay between contiguous blocks"; 137 if (argc < 1) 138 errx(10, "-d: missing %s", name); 139 argc--, argv++; 140 i = atoi(*argv); 141 warnx("%s changes from %dms to %dms", 142 name, sblock.fs_rotdelay, i); 143 sblock.fs_rotdelay = i; 144 continue; 145 146 case 'e': 147 name = 148 "maximum blocks per file in a cylinder group"; 149 if (argc < 1) 150 errx(10, "-e: missing %s", name); 151 argc--, argv++; 152 i = atoi(*argv); 153 if (i < 1) 154 errx(10, "%s must be >= 1 (was %s)", 155 name, *argv); 156 warnx("%s changes from %d to %d", 157 name, sblock.fs_maxbpg, i); 158 sblock.fs_maxbpg = i; 159 continue; 160 161 case 'm': 162 name = "minimum percentage of free space"; 163 if (argc < 1) 164 errx(10, "-m: missing %s", name); 165 argc--, argv++; 166 i = atoi(*argv); 167 if (i < 0 || i > 99) 168 errx(10, "bad %s (%s)", name, *argv); 169 warnx("%s changes from %d%% to %d%%", 170 name, sblock.fs_minfree, i); 171 sblock.fs_minfree = i; 172 if (i >= MINFREE && 173 sblock.fs_optim == FS_OPTSPACE) 174 warnx(OPTWARN, "time", ">=", MINFREE); 175 if (i < MINFREE && 176 sblock.fs_optim == FS_OPTTIME) 177 warnx(OPTWARN, "space", "<", MINFREE); 178 continue; 179 180 case 'o': 181 name = "optimization preference"; 182 if (argc < 1) 183 errx(10, "-o: missing %s", name); 184 argc--, argv++; 185 chg[FS_OPTSPACE] = "space"; 186 chg[FS_OPTTIME] = "time"; 187 if (strcmp(*argv, chg[FS_OPTSPACE]) == 0) 188 i = FS_OPTSPACE; 189 else if (strcmp(*argv, chg[FS_OPTTIME]) == 0) 190 i = FS_OPTTIME; 191 else 192 errx(10, "bad %s (options are `space' or `time')", 193 name); 194 if (sblock.fs_optim == i) { 195 warnx("%s remains unchanged as %s", 196 name, chg[i]); 197 continue; 198 } 199 warnx("%s changes from %s to %s", 200 name, chg[sblock.fs_optim], chg[i]); 201 sblock.fs_optim = i; 202 if (sblock.fs_minfree >= MINFREE && 203 i == FS_OPTSPACE) 204 warnx(OPTWARN, "time", ">=", MINFREE); 205 if (sblock.fs_minfree < MINFREE && 206 i == FS_OPTTIME) 207 warnx(OPTWARN, "space", "<", MINFREE); 208 continue; 209 210 default: 211 usage(); 212 } 213 } 214 if (argc != 1) 215 usage(); 216 bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE); 217 if (Aflag) 218 for (i = 0; i < sblock.fs_ncg; i++) 219 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)), 220 (char *)&sblock, SBSIZE); 221 close(fi); 222 exit(0); 223 } 224 225 void 226 usage() 227 { 228 229 fprintf(stderr, "Usage: tunefs tuneup-options special-device\n"); 230 fprintf(stderr, "where tuneup-options are:\n"); 231 fprintf(stderr, "\t-a maximum contiguous blocks\n"); 232 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n"); 233 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 234 fprintf(stderr, "\t-m minimum percentage of free space\n"); 235 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 236 exit(2); 237 } 238 239 void 240 getsb(fs, file) 241 register struct fs *fs; 242 char *file; 243 { 244 245 fi = open(file, 2); 246 if (fi < 0) 247 err(3, "cannot open %s", file); 248 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) 249 err(4, "%s: bad super block", file); 250 if (fs->fs_magic != FS_MAGIC) 251 err(5, "%s: bad magic number", file); 252 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 253 } 254 255 void 256 bwrite(blk, buf, size) 257 daddr_t blk; 258 char *buf; 259 int size; 260 { 261 262 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) 263 err(6, "FS SEEK"); 264 if (write(fi, buf, size) != size) 265 err(7, "FS WRITE"); 266 } 267 268 int 269 bread(bno, buf, cnt) 270 daddr_t bno; 271 char *buf; 272 int cnt; 273 { 274 int i; 275 276 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0) 277 return(1); 278 if ((i = read(fi, buf, cnt)) != cnt) { 279 for(i=0; i<sblock.fs_bsize; i++) 280 buf[i] = 0; 281 return (1); 282 } 283 return (0); 284 } 285