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 #if 0 35 #ifndef lint 36 static const char copyright[] = 37 "@(#) Copyright (c) 1983, 1993\n\ 38 The Regents of the University of California. All rights reserved.\n"; 39 #endif /* not lint */ 40 41 #ifndef lint 42 static char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94"; 43 #endif /* not lint */ 44 #endif 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 /* 49 * tunefs: change layout parameters to an existing file system. 50 */ 51 #include <sys/param.h> 52 #include <sys/mount.h> 53 #include <sys/disklabel.h> 54 #include <sys/stat.h> 55 56 #include <ufs/ufs/ufsmount.h> 57 #include <ufs/ufs/dinode.h> 58 #include <ufs/ffs/fs.h> 59 60 #include <ctype.h> 61 #include <err.h> 62 #include <fcntl.h> 63 #include <fstab.h> 64 #include <libufs.h> 65 #include <paths.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <unistd.h> 70 71 /* the optimization warning string template */ 72 #define OPTWARN "should optimize for %s with minfree %s %d%%" 73 74 struct uufsd disk; 75 #define sblock disk.d_fs 76 77 void usage(void); 78 void printfs(void); 79 80 int 81 main(int argc, char *argv[]) 82 { 83 char *avalue, *Lvalue, *lvalue, *nvalue; 84 const char *special, *on; 85 const char *name; 86 int active; 87 int Aflag, aflag, eflag, evalue, fflag, fvalue, Lflag, lflag; 88 int mflag, mvalue, nflag, oflag, ovalue, pflag, sflag, svalue; 89 int ch, found_arg, i; 90 const char *chg[2]; 91 struct ufs_args args; 92 struct statfs stfs; 93 94 if (argc < 3) 95 usage(); 96 Aflag = aflag = eflag = fflag = Lflag = lflag = mflag = 0; 97 nflag = oflag = pflag = sflag = 0; 98 avalue = Lvalue = lvalue = nvalue = NULL; 99 evalue = fvalue = mvalue = ovalue = svalue = 0; 100 active = 0; 101 found_arg = 0; /* At least one arg is required. */ 102 while ((ch = getopt(argc, argv, "Aa:e:f:L:l:m:n:o:ps:")) != -1) 103 switch (ch) { 104 105 case 'A': 106 found_arg = 1; 107 Aflag++; 108 break; 109 110 case 'a': 111 found_arg = 1; 112 name = "ACLs"; 113 avalue = optarg; 114 if (strcmp(avalue, "enable") && 115 strcmp(avalue, "disable")) { 116 errx(10, "bad %s (options are %s)", 117 name, "`enable' or `disable'"); 118 } 119 aflag = 1; 120 break; 121 122 case 'e': 123 found_arg = 1; 124 name = "maximum blocks per file in a cylinder group"; 125 evalue = atoi(optarg); 126 if (evalue < 1) 127 errx(10, "%s must be >= 1 (was %s)", 128 name, optarg); 129 eflag = 1; 130 break; 131 132 case 'f': 133 found_arg = 1; 134 name = "average file size"; 135 fvalue = atoi(optarg); 136 if (fvalue < 1) 137 errx(10, "%s must be >= 1 (was %s)", 138 name, optarg); 139 fflag = 1; 140 break; 141 142 case 'L': 143 found_arg = 1; 144 name = "volume label"; 145 Lvalue = optarg; 146 i = -1; 147 while (isalnum(Lvalue[++i])); 148 if (Lvalue[i] != '\0') { 149 errx(10, 150 "bad %s. Valid characters are alphanumerics.", 151 name); 152 } 153 if (strlen(Lvalue) >= MAXVOLLEN) { 154 errx(10, "bad %s. Length is longer than %d.", 155 name, MAXVOLLEN - 1); 156 } 157 Lflag = 1; 158 break; 159 160 case 'l': 161 found_arg = 1; 162 name = "multilabel MAC file system"; 163 lvalue = optarg; 164 if (strcmp(lvalue, "enable") && 165 strcmp(lvalue, "disable")) { 166 errx(10, "bad %s (options are %s)", 167 name, "`enable' or `disable'"); 168 } 169 lflag = 1; 170 break; 171 172 case 'm': 173 found_arg = 1; 174 name = "minimum percentage of free space"; 175 mvalue = atoi(optarg); 176 if (mvalue < 0 || mvalue > 99) 177 errx(10, "bad %s (%s)", name, optarg); 178 mflag = 1; 179 break; 180 181 case 'n': 182 found_arg = 1; 183 name = "soft updates"; 184 nvalue = optarg; 185 if (strcmp(nvalue, "enable") != 0 && 186 strcmp(nvalue, "disable") != 0) { 187 errx(10, "bad %s (options are %s)", 188 name, "`enable' or `disable'"); 189 } 190 nflag = 1; 191 break; 192 193 case 'o': 194 found_arg = 1; 195 name = "optimization preference"; 196 if (strcmp(optarg, "space") == 0) 197 ovalue = FS_OPTSPACE; 198 else if (strcmp(optarg, "time") == 0) 199 ovalue = FS_OPTTIME; 200 else 201 errx(10, 202 "bad %s (options are `space' or `time')", 203 name); 204 oflag = 1; 205 break; 206 207 case 'p': 208 found_arg = 1; 209 pflag = 1; 210 break; 211 212 case 's': 213 found_arg = 1; 214 name = "expected number of files per directory"; 215 svalue = atoi(optarg); 216 if (svalue < 1) 217 errx(10, "%s must be >= 1 (was %s)", 218 name, optarg); 219 sflag = 1; 220 break; 221 222 default: 223 usage(); 224 } 225 argc -= optind; 226 argv += optind; 227 if (found_arg == 0 || argc != 1) 228 usage(); 229 230 on = special = argv[0]; 231 if (ufs_disk_fillout(&disk, special) == -1) 232 goto err; 233 if (disk.d_name != special) { 234 special = disk.d_name; 235 if (statfs(special, &stfs) == 0 && 236 strcmp(special, stfs.f_mntonname) == 0) 237 active = 1; 238 } 239 240 if (pflag) { 241 printfs(); 242 exit(0); 243 } 244 if (Lflag) { 245 name = "volume label"; 246 strlcpy(sblock.fs_volname, Lvalue, MAXVOLLEN); 247 } 248 if (aflag) { 249 name = "ACLs"; 250 if (strcmp(avalue, "enable") == 0) { 251 if (sblock.fs_flags & FS_ACLS) { 252 warnx("%s remains unchanged as enabled", name); 253 } else { 254 sblock.fs_flags |= FS_ACLS; 255 warnx("%s set", name); 256 } 257 } else if (strcmp(avalue, "disable") == 0) { 258 if ((~sblock.fs_flags & FS_ACLS) == 259 FS_ACLS) { 260 warnx("%s remains unchanged as disabled", 261 name); 262 } else { 263 sblock.fs_flags &= ~FS_ACLS; 264 warnx("%s cleared", name); 265 } 266 } 267 } 268 if (eflag) { 269 name = "maximum blocks per file in a cylinder group"; 270 if (sblock.fs_maxbpg == evalue) 271 warnx("%s remains unchanged as %d", name, evalue); 272 else { 273 warnx("%s changes from %d to %d", 274 name, sblock.fs_maxbpg, evalue); 275 sblock.fs_maxbpg = evalue; 276 } 277 } 278 if (fflag) { 279 name = "average file size"; 280 if (sblock.fs_avgfilesize == fvalue) { 281 warnx("%s remains unchanged as %d", name, fvalue); 282 } 283 else { 284 warnx("%s changes from %d to %d", 285 name, sblock.fs_avgfilesize, fvalue); 286 sblock.fs_avgfilesize = fvalue; 287 } 288 } 289 if (lflag) { 290 name = "multilabel"; 291 if (strcmp(lvalue, "enable") == 0) { 292 if (sblock.fs_flags & FS_MULTILABEL) { 293 warnx("%s remains unchanged as enabled", name); 294 } else { 295 sblock.fs_flags |= FS_MULTILABEL; 296 warnx("%s set", name); 297 } 298 } else if (strcmp(lvalue, "disable") == 0) { 299 if ((~sblock.fs_flags & FS_MULTILABEL) == 300 FS_MULTILABEL) { 301 warnx("%s remains unchanged as disabled", 302 name); 303 } else { 304 sblock.fs_flags &= ~FS_MULTILABEL; 305 warnx("%s cleared", name); 306 } 307 } 308 } 309 if (mflag) { 310 name = "minimum percentage of free space"; 311 if (sblock.fs_minfree == mvalue) 312 warnx("%s remains unchanged as %d%%", name, mvalue); 313 else { 314 warnx("%s changes from %d%% to %d%%", 315 name, sblock.fs_minfree, mvalue); 316 sblock.fs_minfree = mvalue; 317 if (mvalue >= MINFREE && sblock.fs_optim == FS_OPTSPACE) 318 warnx(OPTWARN, "time", ">=", MINFREE); 319 if (mvalue < MINFREE && sblock.fs_optim == FS_OPTTIME) 320 warnx(OPTWARN, "space", "<", MINFREE); 321 } 322 } 323 if (nflag) { 324 name = "soft updates"; 325 if (strcmp(nvalue, "enable") == 0) { 326 if (sblock.fs_flags & FS_DOSOFTDEP) 327 warnx("%s remains unchanged as enabled", name); 328 else if (sblock.fs_clean == 0) { 329 warnx("%s cannot be enabled until fsck is run", 330 name); 331 } else { 332 sblock.fs_flags |= FS_DOSOFTDEP; 333 warnx("%s set", name); 334 } 335 } else if (strcmp(nvalue, "disable") == 0) { 336 if ((~sblock.fs_flags & FS_DOSOFTDEP) == FS_DOSOFTDEP) 337 warnx("%s remains unchanged as disabled", name); 338 else { 339 sblock.fs_flags &= ~FS_DOSOFTDEP; 340 warnx("%s cleared", name); 341 } 342 } 343 } 344 if (oflag) { 345 name = "optimization preference"; 346 chg[FS_OPTSPACE] = "space"; 347 chg[FS_OPTTIME] = "time"; 348 if (sblock.fs_optim == ovalue) 349 warnx("%s remains unchanged as %s", name, chg[ovalue]); 350 else { 351 warnx("%s changes from %s to %s", 352 name, chg[sblock.fs_optim], chg[ovalue]); 353 sblock.fs_optim = ovalue; 354 if (sblock.fs_minfree >= MINFREE && 355 ovalue == FS_OPTSPACE) 356 warnx(OPTWARN, "time", ">=", MINFREE); 357 if (sblock.fs_minfree < MINFREE && ovalue == FS_OPTTIME) 358 warnx(OPTWARN, "space", "<", MINFREE); 359 } 360 } 361 if (sflag) { 362 name = "expected number of files per directory"; 363 if (sblock.fs_avgfpdir == svalue) { 364 warnx("%s remains unchanged as %d", name, svalue); 365 } 366 else { 367 warnx("%s changes from %d to %d", 368 name, sblock.fs_avgfpdir, svalue); 369 sblock.fs_avgfpdir = svalue; 370 } 371 } 372 373 if (sbwrite(&disk, Aflag) == -1) 374 goto err; 375 ufs_disk_close(&disk); 376 if (active) { 377 bzero(&args, sizeof(args)); 378 if (mount("ufs", on, 379 stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0) 380 err(9, "%s: reload", special); 381 warnx("file system reloaded"); 382 } 383 exit(0); 384 err: 385 if (disk.d_error != NULL) 386 errx(11, "%s: %s", special, disk.d_error); 387 else 388 err(12, "%s", special); 389 } 390 391 void 392 usage(void) 393 { 394 fprintf(stderr, "%s\n%s\n%s\n%s\n", 395 "usage: tunefs [-A] [-a enable | disable] [-e maxbpg] [-f avgfilesize]", 396 " [-L volname] [-l enable | disable] [-m minfree]", 397 " [-n enable | disable] [-o space | time] [-p]", 398 " [-s avgfpdir] special | filesystem"); 399 exit(2); 400 } 401 402 void 403 printfs(void) 404 { 405 warnx("ACLs: (-a) %s", 406 (sblock.fs_flags & FS_ACLS)? "enabled" : "disabled"); 407 warnx("MAC multilabel: (-l) %s", 408 (sblock.fs_flags & FS_MULTILABEL)? "enabled" : "disabled"); 409 warnx("soft updates: (-n) %s", 410 (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled"); 411 warnx("maximum blocks per file in a cylinder group: (-e) %d", 412 sblock.fs_maxbpg); 413 warnx("average file size: (-f) %d", 414 sblock.fs_avgfilesize); 415 warnx("average number of files in a directory: (-s) %d", 416 sblock.fs_avgfpdir); 417 warnx("minimum percentage of free space: (-m) %d%%", 418 sblock.fs_minfree); 419 warnx("optimization preference: (-o) %s", 420 sblock.fs_optim == FS_OPTSPACE ? "space" : "time"); 421 if (sblock.fs_minfree >= MINFREE && 422 sblock.fs_optim == FS_OPTSPACE) 423 warnx(OPTWARN, "time", ">=", MINFREE); 424 if (sblock.fs_minfree < MINFREE && 425 sblock.fs_optim == FS_OPTTIME) 426 warnx(OPTWARN, "space", "<", MINFREE); 427 warnx("volume label: (-L) %s", 428 sblock.fs_volname); 429 } 430