1 /* $NetBSD: tunefs.c,v 1.10 1995/03/18 15:01:31 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1983, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94"; 45 #else 46 static char rcsid[] = "$NetBSD: tunefs.c,v 1.10 1995/03/18 15:01:31 cgd Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 /* 51 * tunefs: change layout parameters to an existing file system. 52 */ 53 #include <sys/param.h> 54 #include <sys/stat.h> 55 56 #include <ufs/ffs/fs.h> 57 58 #include <errno.h> 59 #include <err.h> 60 #include <fcntl.h> 61 #include <fstab.h> 62 #include <stdio.h> 63 #include <paths.h> 64 #include <stdlib.h> 65 #include <unistd.h> 66 67 /* the optimization warning string template */ 68 #define OPTWARN "should optimize for %s with minfree %s %d%%" 69 70 union { 71 struct fs sb; 72 char pad[MAXBSIZE]; 73 } sbun; 74 #define sblock sbun.sb 75 76 int fi; 77 long dev_bsize = 1; 78 79 void bwrite(daddr_t, char *, int); 80 int bread(daddr_t, char *, int); 81 void getsb(struct fs *, char *); 82 void usage __P((void)); 83 84 int 85 main(argc, argv) 86 int argc; 87 char *argv[]; 88 { 89 char *cp, *special, *name; 90 struct stat st; 91 int i; 92 int Aflag = 0; 93 struct fstab *fs; 94 char *chg[2], device[MAXPATHLEN]; 95 96 argc--, argv++; 97 if (argc < 2) 98 usage(); 99 special = argv[argc - 1]; 100 fs = getfsfile(special); 101 if (fs) 102 special = fs->fs_spec; 103 again: 104 if (stat(special, &st) < 0) { 105 if (*special != '/') { 106 if (*special == 'r') 107 special++; 108 (void)sprintf(device, "%s/%s", _PATH_DEV, special); 109 special = device; 110 goto again; 111 } 112 err(1, "%s", special); 113 } 114 if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) 115 errx(10, "%s: not a block or character device", special); 116 getsb(&sblock, special); 117 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 118 for (cp = &argv[0][1]; *cp; cp++) 119 switch (*cp) { 120 121 case 'A': 122 Aflag++; 123 continue; 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 exit(2); 242 } 243 244 void 245 getsb(fs, file) 246 register struct fs *fs; 247 char *file; 248 { 249 250 fi = open(file, 2); 251 if (fi < 0) 252 err(3, "cannot open %s", file); 253 if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) 254 err(4, "%s: bad super block", file); 255 if (fs->fs_magic != FS_MAGIC) 256 err(5, "%s: bad magic number", file); 257 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 258 } 259 260 void 261 bwrite(blk, buf, size) 262 daddr_t blk; 263 char *buf; 264 int size; 265 { 266 267 if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) 268 err(6, "FS SEEK"); 269 if (write(fi, buf, size) != size) 270 err(7, "FS WRITE"); 271 } 272 273 int 274 bread(bno, buf, cnt) 275 daddr_t bno; 276 char *buf; 277 int cnt; 278 { 279 int i; 280 281 if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0) 282 return(1); 283 if ((i = read(fi, buf, cnt)) != cnt) { 284 for(i=0; i<sblock.fs_bsize; i++) 285 buf[i] = 0; 286 return (1); 287 } 288 return (0); 289 } 290