1 /* $NetBSD: tunefs.c,v 1.33 2005/01/19 20:46:16 xtraeme 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)tunefs.c 8.3 (Berkeley) 5/3/95"; 41 #else 42 __RCSID("$NetBSD: tunefs.c,v 1.33 2005/01/19 20:46:16 xtraeme Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 /* 47 * tunefs: change layout parameters to an existing file system. 48 */ 49 #include <sys/param.h> 50 51 #include <ufs/ufs/dinode.h> 52 #include <ufs/ffs/fs.h> 53 #include <ufs/ffs/ffs_extern.h> 54 55 #include <machine/bswap.h> 56 57 #include <err.h> 58 #include <errno.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 #include <util.h> 67 68 /* the optimization warning string template */ 69 #define OPTWARN "should optimize for %s with minfree %s %d%%" 70 71 union { 72 struct fs sb; 73 char pad[MAXBSIZE]; 74 } sbun; 75 #define sblock sbun.sb 76 char buf[MAXBSIZE]; 77 78 int fi; 79 long dev_bsize = 512; 80 int needswap = 0; 81 int is_ufs2 = 0; 82 off_t sblockloc; 83 84 static off_t sblock_try[] = SBLOCKSEARCH; 85 86 static void bwrite(daddr_t, char *, int, const char *); 87 static void bread(daddr_t, char *, int, const char *); 88 static int getnum(const char *, const char *, int, int); 89 static void getsb(struct fs *, const char *); 90 static int openpartition(const char *, int, char *, size_t); 91 static void usage(void); 92 93 int 94 main(int argc, char *argv[]) 95 { 96 #define OPTSTRINGBASE "AFNe:g:h:m:o:" 97 #ifdef TUNEFS_SOFTDEP 98 int softdep; 99 #define OPTSTRING OPTSTRINGBASE ## "n:" 100 #else 101 #define OPTSTRING OPTSTRINGBASE 102 #endif 103 int i, ch, Aflag, Fflag, Nflag, openflags; 104 const char *special, *chg[2]; 105 char device[MAXPATHLEN]; 106 int maxbpg, minfree, optim; 107 int avgfilesize, avgfpdir; 108 109 Aflag = Fflag = Nflag = 0; 110 maxbpg = minfree = optim = -1; 111 avgfilesize = avgfpdir = -1; 112 #ifdef TUNEFS_SOFTDEP 113 softdep = -1; 114 #endif 115 chg[FS_OPTSPACE] = "space"; 116 chg[FS_OPTTIME] = "time"; 117 118 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) { 119 switch (ch) { 120 121 case 'A': 122 Aflag++; 123 break; 124 125 case 'F': 126 Fflag++; 127 break; 128 129 case 'N': 130 Nflag++; 131 break; 132 133 case 'e': 134 maxbpg = getnum(optarg, 135 "maximum blocks per file in a cylinder group", 136 1, INT_MAX); 137 break; 138 139 case 'g': 140 avgfilesize = getnum(optarg, 141 "average file size", 1, INT_MAX); 142 break; 143 144 case 'h': 145 avgfpdir = getnum(optarg, 146 "expected number of files per directory", 147 1, INT_MAX); 148 break; 149 150 case 'm': 151 minfree = getnum(optarg, 152 "minimum percentage of free space", 0, 99); 153 break; 154 155 #ifdef TUNEFS_SOFTDEP 156 case 'n': 157 if (strcmp(optarg, "enable") == 0) 158 softdep = 1; 159 else if (strcmp(optarg, "disable") == 0) 160 softdep = 0; 161 else { 162 errx(10, "bad soft dependencies " 163 "(options are `enable' or `disable')"); 164 } 165 break; 166 #endif 167 168 case 'o': 169 if (strcmp(optarg, chg[FS_OPTSPACE]) == 0) 170 optim = FS_OPTSPACE; 171 else if (strcmp(optarg, chg[FS_OPTTIME]) == 0) 172 optim = FS_OPTTIME; 173 else 174 errx(10, 175 "bad %s (options are `space' or `time')", 176 "optimization preference"); 177 break; 178 179 default: 180 usage(); 181 } 182 } 183 argc -= optind; 184 argv += optind; 185 if (argc != 1) 186 usage(); 187 188 special = argv[0]; 189 openflags = Nflag ? O_RDONLY : O_RDWR; 190 if (Fflag) 191 fi = open(special, openflags); 192 else { 193 fi = openpartition(special, openflags, device, sizeof(device)); 194 special = device; 195 } 196 if (fi == -1) 197 err(1, "%s", special); 198 getsb(&sblock, special); 199 200 #define CHANGEVAL(old, new, type, suffix) do \ 201 if ((new) != -1) { \ 202 if ((new) == (old)) \ 203 warnx("%s remains unchanged at %d%s", \ 204 (type), (old), (suffix)); \ 205 else { \ 206 warnx("%s changes from %d%s to %d%s", \ 207 (type), (old), (suffix), (new), (suffix)); \ 208 (old) = (new); \ 209 } \ 210 } while (/* CONSTCOND */0) 211 212 warnx("tuning %s", special); 213 CHANGEVAL(sblock.fs_maxbpg, maxbpg, 214 "maximum blocks per file in a cylinder group", ""); 215 CHANGEVAL(sblock.fs_minfree, minfree, 216 "minimum percentage of free space", "%"); 217 if (minfree != -1) { 218 if (minfree >= MINFREE && 219 sblock.fs_optim == FS_OPTSPACE) 220 warnx(OPTWARN, "time", ">=", MINFREE); 221 if (minfree < MINFREE && 222 sblock.fs_optim == FS_OPTTIME) 223 warnx(OPTWARN, "space", "<", MINFREE); 224 } 225 #ifdef TUNEFS_SOFTDEP 226 if (softdep == 1) { 227 sblock.fs_flags |= FS_DOSOFTDEP; 228 warnx("soft dependencies set"); 229 } else if (softdep == 0) { 230 sblock.fs_flags &= ~FS_DOSOFTDEP; 231 warnx("soft dependencies cleared"); 232 } 233 #endif 234 if (optim != -1) { 235 if (sblock.fs_optim == optim) { 236 warnx("%s remains unchanged as %s", 237 "optimization preference", 238 chg[optim]); 239 } else { 240 warnx("%s changes from %s to %s", 241 "optimization preference", 242 chg[sblock.fs_optim], chg[optim]); 243 sblock.fs_optim = optim; 244 if (sblock.fs_minfree >= MINFREE && 245 optim == FS_OPTSPACE) 246 warnx(OPTWARN, "time", ">=", MINFREE); 247 if (sblock.fs_minfree < MINFREE && 248 optim == FS_OPTTIME) 249 warnx(OPTWARN, "space", "<", MINFREE); 250 } 251 } 252 CHANGEVAL(sblock.fs_avgfilesize, avgfilesize, 253 "average file size", ""); 254 CHANGEVAL(sblock.fs_avgfpdir, avgfpdir, 255 "expected number of files per directory", ""); 256 257 if (Nflag) { 258 fprintf(stdout, "tunefs: current settings of %s\n", special); 259 fprintf(stdout, "\tmaximum contiguous block count %d\n", 260 sblock.fs_maxcontig); 261 fprintf(stdout, 262 "\tmaximum blocks per file in a cylinder group %d\n", 263 sblock.fs_maxbpg); 264 fprintf(stdout, "\tminimum percentage of free space %d%%\n", 265 sblock.fs_minfree); 266 #ifdef TUNEFS_SOFTDEP 267 fprintf(stdout, "\tsoft dependencies: %s\n", 268 (sblock.fs_flags & FS_DOSOFTDEP) ? "on" : "off"); 269 #endif 270 fprintf(stdout, "\toptimization preference: %s\n", 271 chg[sblock.fs_optim]); 272 fprintf(stdout, "\taverage file size: %d\n", 273 sblock.fs_avgfilesize); 274 fprintf(stdout, 275 "\texpected number of files per directory: %d\n", 276 sblock.fs_avgfpdir); 277 fprintf(stdout, "tunefs: no changes made\n"); 278 exit(0); 279 } 280 281 memcpy(buf, (char *)&sblock, SBLOCKSIZE); 282 if (needswap) 283 ffs_sb_swap((struct fs*)buf, (struct fs*)buf); 284 bwrite(sblockloc, buf, SBLOCKSIZE, special); 285 if (Aflag) 286 for (i = 0; i < sblock.fs_ncg; i++) 287 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)), 288 buf, SBLOCKSIZE, special); 289 close(fi); 290 exit(0); 291 } 292 293 static int 294 getnum(const char *num, const char *desc, int min, int max) 295 { 296 long n; 297 char *ep; 298 299 n = strtol(num, &ep, 10); 300 if (ep[0] != '\0') 301 errx(1, "Invalid number `%s' for %s", num, desc); 302 if ((int) n < min) 303 errx(1, "%s `%s' too small (minimum is %d)", desc, num, min); 304 if ((int) n > max) 305 errx(1, "%s `%s' too large (maximum is %d)", desc, num, max); 306 return ((int)n); 307 } 308 309 static void 310 usage(void) 311 { 312 313 fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n"); 314 fprintf(stderr, "where tuneup-options are:\n"); 315 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 316 fprintf(stderr, "\t-g average file size\n"); 317 fprintf(stderr, "\t-h expected number of files per directory\n"); 318 fprintf(stderr, "\t-m minimum percentage of free space\n"); 319 #ifdef TUNEFS_SOFTDEP 320 fprintf(stderr, "\t-n soft dependencies (`enable' or `disable')\n"); 321 #endif 322 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 323 exit(2); 324 } 325 326 static void 327 getsb(struct fs *fs, const char *file) 328 { 329 int i; 330 331 for (i = 0; ; i++) { 332 if (sblock_try[i] == -1) 333 errx(5, "cannot find filesystem superblock"); 334 bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file); 335 switch(fs->fs_magic) { 336 case FS_UFS2_MAGIC: 337 is_ufs2 = 1; 338 /*FALLTHROUGH*/ 339 case FS_UFS1_MAGIC: 340 break; 341 case FS_UFS2_MAGIC_SWAPPED: 342 is_ufs2 = 1; 343 /*FALLTHROUGH*/ 344 case FS_UFS1_MAGIC_SWAPPED: 345 warnx("%s: swapping byte order", file); 346 needswap = 1; 347 ffs_sb_swap(fs, fs); 348 break; 349 default: 350 continue; 351 } 352 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2) 353 continue; 354 if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED) 355 && fs->fs_sblockloc != sblock_try[i]) 356 continue; 357 break; 358 } 359 360 dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 361 sblockloc = sblock_try[i] / dev_bsize; 362 } 363 364 static void 365 bwrite(daddr_t blk, char *buffer, int size, const char *file) 366 { 367 off_t offset; 368 369 offset = (off_t)blk * dev_bsize; 370 if (lseek(fi, offset, SEEK_SET) == -1) 371 err(6, "%s: seeking to %lld", file, (long long)offset); 372 if (write(fi, buffer, size) != size) 373 err(7, "%s: writing %d bytes", file, size); 374 } 375 376 static void 377 bread(daddr_t blk, char *buffer, int cnt, const char *file) 378 { 379 off_t offset; 380 int i; 381 382 offset = (off_t)blk * dev_bsize; 383 if (lseek(fi, offset, SEEK_SET) == -1) 384 err(4, "%s: seeking to %lld", file, (long long)offset); 385 if ((i = read(fi, buffer, cnt)) != cnt) 386 errx(5, "%s: short read", file); 387 } 388 389 static int 390 openpartition(const char *name, int flags, char *device, size_t devicelen) 391 { 392 char rawspec[MAXPATHLEN], *p; 393 struct fstab *fs; 394 int fd, oerrno; 395 396 fs = getfsfile(name); 397 if (fs) { 398 if ((p = strrchr(fs->fs_spec, '/')) != NULL) { 399 snprintf(rawspec, sizeof(rawspec), "%.*s/r%s", 400 (int)(p - fs->fs_spec), fs->fs_spec, p + 1); 401 name = rawspec; 402 } else 403 name = fs->fs_spec; 404 } 405 fd = opendisk(name, flags, device, devicelen, 0); 406 if (fd == -1 && errno == ENOENT) { 407 oerrno = errno; 408 strlcpy(device, name, devicelen); 409 errno = oerrno; 410 } 411 return (fd); 412 } 413