1 /* $OpenBSD: tunefs.c,v 1.34 2014/05/20 21:11:16 krw Exp $ */ 2 /* $NetBSD: tunefs.c,v 1.33 2005/01/19 20:46:16 xtraeme Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * tunefs: change layout parameters to an existing file system. 35 */ 36 #include <sys/param.h> 37 38 #include <ufs/ufs/dinode.h> 39 #include <ufs/ffs/fs.h> 40 #include <ufs/ffs/ffs_extern.h> 41 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <fstab.h> 46 #include <paths.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <util.h> 52 53 /* the optimization warning string template */ 54 #define OPTWARN "should optimize for %s with minfree %s %d%%" 55 56 union { 57 struct fs sb; 58 char pad[MAXBSIZE]; 59 } sbun; 60 #define sblock sbun.sb 61 char buf[MAXBSIZE]; 62 63 int fi; 64 int is_ufs2 = 0; 65 off_t sblockloc; 66 67 static off_t sblock_try[] = SBLOCKSEARCH; 68 69 static void bwrite(daddr_t, char *, int, const char *); 70 static void bread(daddr_t, char *, int, const char *); 71 static int getnum(const char *, const char *, int, int); 72 static void getsb(struct fs *, const char *); 73 static int openpartition(char *, int, char **); 74 static void usage(void); 75 76 int 77 main(int argc, char *argv[]) 78 { 79 #define OPTSTRING "AFNe:g:h:m:o:" 80 int i, ch, Aflag, Fflag, Nflag, openflags; 81 char *special; 82 const char *chg[2]; 83 int maxbpg, minfree, optim; 84 int avgfilesize, avgfpdir; 85 86 Aflag = Fflag = Nflag = 0; 87 maxbpg = minfree = optim = -1; 88 avgfilesize = avgfpdir = -1; 89 chg[FS_OPTSPACE] = "space"; 90 chg[FS_OPTTIME] = "time"; 91 92 while ((ch = getopt(argc, argv, OPTSTRING)) != -1) { 93 switch (ch) { 94 95 case 'A': 96 Aflag++; 97 break; 98 99 case 'F': 100 Fflag++; 101 break; 102 103 case 'N': 104 Nflag++; 105 break; 106 107 case 'e': 108 maxbpg = getnum(optarg, 109 "maximum blocks per file in a cylinder group", 110 1, INT_MAX); 111 break; 112 113 case 'g': 114 avgfilesize = getnum(optarg, 115 "average file size", 1, INT_MAX); 116 break; 117 118 case 'h': 119 avgfpdir = getnum(optarg, 120 "expected number of files per directory", 121 1, INT_MAX); 122 break; 123 124 case 'm': 125 minfree = getnum(optarg, 126 "minimum percentage of free space", 0, 99); 127 break; 128 129 case 'o': 130 if (strcmp(optarg, chg[FS_OPTSPACE]) == 0) 131 optim = FS_OPTSPACE; 132 else if (strcmp(optarg, chg[FS_OPTTIME]) == 0) 133 optim = FS_OPTTIME; 134 else 135 errx(10, 136 "bad %s (options are `space' or `time')", 137 "optimization preference"); 138 break; 139 140 default: 141 usage(); 142 } 143 } 144 argc -= optind; 145 argv += optind; 146 if (argc != 1) 147 usage(); 148 149 special = argv[0]; 150 openflags = Nflag ? O_RDONLY : O_RDWR; 151 if (Fflag) 152 fi = open(special, openflags); 153 else 154 fi = openpartition(special, openflags, &special); 155 if (fi == -1) 156 err(1, "%s", special); 157 getsb(&sblock, special); 158 159 #define CHANGEVAL(old, new, type, suffix) do \ 160 if ((new) != -1) { \ 161 if ((new) == (old)) \ 162 warnx("%s remains unchanged at %d%s", \ 163 (type), (old), (suffix)); \ 164 else { \ 165 warnx("%s changes from %d%s to %d%s", \ 166 (type), (old), (suffix), (new), (suffix)); \ 167 (old) = (new); \ 168 } \ 169 } while (/* CONSTCOND */0) 170 171 warnx("tuning %s", special); 172 CHANGEVAL(sblock.fs_maxbpg, maxbpg, 173 "maximum blocks per file in a cylinder group", ""); 174 CHANGEVAL(sblock.fs_minfree, minfree, 175 "minimum percentage of free space", "%"); 176 if (minfree != -1) { 177 if (minfree >= MINFREE && 178 sblock.fs_optim == FS_OPTSPACE) 179 warnx(OPTWARN, "time", ">=", MINFREE); 180 if (minfree < MINFREE && 181 sblock.fs_optim == FS_OPTTIME) 182 warnx(OPTWARN, "space", "<", MINFREE); 183 } 184 if (optim != -1) { 185 if (sblock.fs_optim == optim) { 186 warnx("%s remains unchanged as %s", 187 "optimization preference", 188 chg[optim]); 189 } else { 190 warnx("%s changes from %s to %s", 191 "optimization preference", 192 chg[sblock.fs_optim], chg[optim]); 193 sblock.fs_optim = optim; 194 if (sblock.fs_minfree >= MINFREE && 195 optim == FS_OPTSPACE) 196 warnx(OPTWARN, "time", ">=", MINFREE); 197 if (sblock.fs_minfree < MINFREE && 198 optim == FS_OPTTIME) 199 warnx(OPTWARN, "space", "<", MINFREE); 200 } 201 } 202 CHANGEVAL(sblock.fs_avgfilesize, avgfilesize, 203 "average file size", ""); 204 CHANGEVAL(sblock.fs_avgfpdir, avgfpdir, 205 "expected number of files per directory", ""); 206 207 if (Nflag) { 208 fprintf(stdout, "tunefs: current settings of %s\n", special); 209 fprintf(stdout, "\tmaximum contiguous block count %d\n", 210 sblock.fs_maxcontig); 211 fprintf(stdout, 212 "\tmaximum blocks per file in a cylinder group %d\n", 213 sblock.fs_maxbpg); 214 fprintf(stdout, "\tminimum percentage of free space %d%%\n", 215 sblock.fs_minfree); 216 fprintf(stdout, "\toptimization preference: %s\n", 217 chg[sblock.fs_optim]); 218 fprintf(stdout, "\taverage file size: %d\n", 219 sblock.fs_avgfilesize); 220 fprintf(stdout, 221 "\texpected number of files per directory: %d\n", 222 sblock.fs_avgfpdir); 223 fprintf(stdout, "tunefs: no changes made\n"); 224 exit(0); 225 } 226 227 memcpy(buf, (char *)&sblock, SBLOCKSIZE); 228 bwrite(sblockloc, buf, SBLOCKSIZE, special); 229 if (Aflag) 230 for (i = 0; i < sblock.fs_ncg; i++) 231 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)), 232 buf, SBLOCKSIZE, special); 233 close(fi); 234 exit(0); 235 } 236 237 static int 238 getnum(const char *num, const char *desc, int min, int max) 239 { 240 int n; 241 const char *errstr; 242 243 n = strtonum(num, min, max, &errstr); 244 if (errstr != NULL) 245 errx(1, "Invalid number `%s' for %s: %s", num, desc, errstr); 246 return (n); 247 } 248 249 static void 250 usage(void) 251 { 252 extern char *__progname; 253 254 fprintf(stderr, 255 "usage: %s [-AFN] [-e maxbpg] [-g avgfilesize] " 256 "[-h avgfpdir] [-m minfree]\n" 257 "\t[-o optimize_preference] special | filesys\n", 258 __progname); 259 260 exit(2); 261 } 262 263 static void 264 getsb(struct fs *fs, const char *file) 265 { 266 int i; 267 268 for (i = 0; ; i++) { 269 if (sblock_try[i] == -1) 270 errx(5, "cannot find filesystem superblock"); 271 bread(sblock_try[i] / DEV_BSIZE, (char *)fs, SBLOCKSIZE, file); 272 switch(fs->fs_magic) { 273 case FS_UFS2_MAGIC: 274 is_ufs2 = 1; 275 /*FALLTHROUGH*/ 276 case FS_UFS1_MAGIC: 277 break; 278 default: 279 continue; 280 } 281 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2) 282 continue; 283 if ((is_ufs2 || fs->fs_flags & FS_FLAGS_UPDATED) 284 && fs->fs_sblockloc != sblock_try[i]) 285 continue; 286 break; 287 } 288 289 sblockloc = sblock_try[i] / DEV_BSIZE; 290 } 291 292 static void 293 bwrite(daddr_t blk, char *buffer, int size, const char *file) 294 { 295 if (pwrite(fi, buffer, size, blk * DEV_BSIZE) != size) 296 err(7, "%s: writing %d bytes @ %lld", file, size, 297 (long long)(blk * DEV_BSIZE)); 298 } 299 300 static void 301 bread(daddr_t blk, char *buffer, int cnt, const char *file) 302 { 303 if ((pread(fi, buffer, cnt, (off_t)blk * DEV_BSIZE)) != cnt) 304 errx(5, "%s: reading %d bytes @ %lld", file, cnt, 305 (long long)(blk * DEV_BSIZE)); 306 } 307 308 static int 309 openpartition(char *name, int flags, char **devicep) 310 { 311 char rawspec[MAXPATHLEN], *p; 312 struct fstab *fs; 313 int fd; 314 315 fs = getfsfile(name); 316 if (fs) { 317 if ((p = strrchr(fs->fs_spec, '/')) != NULL) { 318 snprintf(rawspec, sizeof(rawspec), "%.*s/r%s", 319 (int)(p - fs->fs_spec), fs->fs_spec, p + 1); 320 name = rawspec; 321 } else 322 name = fs->fs_spec; 323 } 324 fd = opendev(name, flags, 0, devicep); 325 if (fd == -1 && errno == ENOENT) 326 devicep = &name; 327 return (fd); 328 } 329