tunefs.c revision 1.51
1/* $NetBSD: tunefs.c,v 1.51 2020/04/09 14:44:38 christos 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\ 35 The Regents of the University of California. All rights reserved."); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)tunefs.c 8.3 (Berkeley) 5/3/95"; 41#else 42__RCSID("$NetBSD: tunefs.c,v 1.51 2020/04/09 14:44:38 christos 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#include <sys/statvfs.h> 51 52#include <ufs/ffs/fs.h> 53#include <ufs/ffs/ffs_extern.h> 54#include <ufs/ufs/ufs_wapbl.h> 55#include <ufs/ufs/ufsmount.h> 56#include <ufs/ufs/quota2.h> 57 58#include <machine/bswap.h> 59 60#include <err.h> 61#include <errno.h> 62#include <fcntl.h> 63#include <fstab.h> 64#include <paths.h> 65#include <stdio.h> 66#include <stdlib.h> 67#include <string.h> 68#include <unistd.h> 69#include <util.h> 70 71/* the optimization warning string template */ 72#define OPTWARN "should optimize for %s with minfree %s %d%%" 73 74union { 75 struct fs sb; 76 char data[MAXBSIZE]; 77} sbun, buf; 78#define sblock sbun.sb 79 80int fi; 81long dev_bsize = 512; 82int needswap = 0; 83int is_ufs2 = 0; 84off_t sblockloc; 85int userquota = 0; 86int groupquota = 0; 87#define Q2_EN (1) 88#define Q2_IGN (0) 89#define Q2_DIS (-1) 90 91static off_t sblock_try[] = SBLOCKSEARCH; 92 93static void bwrite(daddr_t, char *, int, const char *); 94static void bread(daddr_t, char *, int, const char *); 95static void change_log_info(long long); 96static void getsb(struct fs *, const char *); 97static int openpartition(const char *, int, char *, size_t); 98static void show_log_info(void); 99__dead static void usage(void); 100 101int 102main(int argc, char *argv[]) 103{ 104 int i, ch, Aflag, Fflag, Nflag, openflags; 105 const char *special, *chg[2]; 106 char device[MAXPATHLEN]; 107 int maxbpg, minfree, optim, secsize; 108 int avgfilesize, avgfpdir, active; 109 long long logfilesize; 110 int secshift, fsbtodb; 111 struct statvfs sfs; 112 113 Aflag = Fflag = Nflag = 0; 114 maxbpg = minfree = optim = secsize = -1; 115 avgfilesize = avgfpdir = -1; 116 logfilesize = -1; 117 secshift = 0; 118 chg[FS_OPTSPACE] = "space"; 119 chg[FS_OPTTIME] = "time"; 120 121 while ((ch = getopt(argc, argv, "AFNe:g:h:l:m:o:q:S:")) != -1) { 122 switch (ch) { 123 124 case 'A': 125 Aflag++; 126 break; 127 128 case 'F': 129 Fflag++; 130 break; 131 132 case 'N': 133 Nflag++; 134 break; 135 136 case 'e': 137 maxbpg = strsuftoll( 138 "maximum blocks per file in a cylinder group", 139 optarg, 1, INT_MAX); 140 break; 141 142 case 'g': 143 avgfilesize = strsuftoll("average file size", optarg, 144 1, INT_MAX); 145 break; 146 147 case 'h': 148 avgfpdir = strsuftoll( 149 "expected number of files per directory", 150 optarg, 1, INT_MAX); 151 break; 152 153 case 'l': 154 logfilesize = strsuftoll("journal log file size", 155 optarg, 0, INT_MAX); 156 break; 157 158 case 'm': 159 minfree = strsuftoll("minimum percentage of free space", 160 optarg, 0, 99); 161 break; 162 163 case 'o': 164 if (strcmp(optarg, chg[FS_OPTSPACE]) == 0) 165 optim = FS_OPTSPACE; 166 else if (strcmp(optarg, chg[FS_OPTTIME]) == 0) 167 optim = FS_OPTTIME; 168 else 169 errx(10, 170 "bad %s (options are `space' or `time')", 171 "optimization preference"); 172 break; 173 case 'q': 174 if (strcmp(optarg, "user") == 0) 175 userquota = Q2_EN; 176 else if (strcmp(optarg, "group") == 0) 177 groupquota = Q2_EN; 178 else if (strcmp(optarg, "nouser") == 0) 179 userquota = Q2_DIS; 180 else if (strcmp(optarg, "nogroup") == 0) 181 groupquota = Q2_DIS; 182 else 183 errx(11, "invalid quota type %s", optarg); 184 break; 185 case 'S': 186 secsize = strsuftoll("physical sector size", 187 optarg, 0, INT_MAX); 188 secshift = ffs(secsize) - 1; 189 if (secsize != 0 && 1 << secshift != secsize) 190 errx(12, "sector size %d is not a power of two", secsize); 191 break; 192 default: 193 usage(); 194 } 195 } 196 argc -= optind; 197 argv += optind; 198 if (argc != 1) 199 usage(); 200 201 special = argv[0]; 202 openflags = Nflag ? O_RDONLY : O_RDWR; 203 if (Fflag) 204 fi = open(special, openflags); 205 else { 206 fi = openpartition(special, openflags, device, sizeof(device)); 207 special = device; 208 } 209 active = fstatvfs(fi, &sfs) != -1; 210 if (fi == -1) 211 err(1, "%s", special); 212 getsb(&sblock, special); 213 214#define CHANGEVAL(old, new, type, suffix) do \ 215 if ((new) != -1) { \ 216 if ((new) == (old)) \ 217 warnx("%s remains unchanged at %d%s", \ 218 (type), (old), (suffix)); \ 219 else { \ 220 warnx("%s changes from %d%s to %d%s", \ 221 (type), (old), (suffix), (new), (suffix)); \ 222 (old) = (new); \ 223 } \ 224 } while (/* CONSTCOND */0) 225 226 warnx("tuning %s", special); 227 CHANGEVAL(sblock.fs_maxbpg, maxbpg, 228 "maximum blocks per file in a cylinder group", ""); 229 CHANGEVAL(sblock.fs_minfree, minfree, 230 "minimum percentage of free space", "%"); 231 if (minfree != -1) { 232 if (minfree >= MINFREE && 233 sblock.fs_optim == FS_OPTSPACE) 234 warnx(OPTWARN, "time", ">=", MINFREE); 235 if (minfree < MINFREE && 236 sblock.fs_optim == FS_OPTTIME) 237 warnx(OPTWARN, "space", "<", MINFREE); 238 } 239 if (optim != -1) { 240 if (sblock.fs_optim == optim) { 241 warnx("%s remains unchanged as %s", 242 "optimization preference", 243 chg[optim]); 244 } else { 245 warnx("%s changes from %s to %s", 246 "optimization preference", 247 chg[sblock.fs_optim], chg[optim]); 248 sblock.fs_optim = optim; 249 if (sblock.fs_minfree >= MINFREE && 250 optim == FS_OPTSPACE) 251 warnx(OPTWARN, "time", ">=", MINFREE); 252 if (sblock.fs_minfree < MINFREE && 253 optim == FS_OPTTIME) 254 warnx(OPTWARN, "space", "<", MINFREE); 255 } 256 } 257 if (secsize != -1) { 258 if (secsize == 0) { 259 secsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1); 260 secshift = ffs(secsize) - 1; 261 } 262 263 if (secshift < DEV_BSHIFT) 264 warnx("sector size must be at least %d", DEV_BSIZE); 265 else if (secshift > sblock.fs_fshift) 266 warnx("sector size %d cannot be larger than fragment size %d", 267 secsize, sblock.fs_fsize); 268 else { 269 fsbtodb = sblock.fs_fshift - secshift; 270 if (fsbtodb == sblock.fs_fsbtodb) { 271 warnx("sector size remains unchanged as %d", 272 sblock.fs_fsize / FFS_FSBTODB(&sblock, 1)); 273 } else { 274 warnx("sector size changed from %d to %d", 275 sblock.fs_fsize / FFS_FSBTODB(&sblock, 1), 276 secsize); 277 sblock.fs_fsbtodb = fsbtodb; 278 } 279 } 280 } 281 CHANGEVAL(sblock.fs_avgfilesize, avgfilesize, 282 "average file size", ""); 283 CHANGEVAL(sblock.fs_avgfpdir, avgfpdir, 284 "expected number of files per directory", ""); 285 286 if (logfilesize >= 0) 287 change_log_info(logfilesize); 288 if (userquota == Q2_EN || groupquota == Q2_EN) 289 sblock.fs_flags |= FS_DOQUOTA2; 290 if (sblock.fs_flags & FS_DOQUOTA2) { 291 sblock.fs_quota_magic = Q2_HEAD_MAGIC; 292 switch(userquota) { 293 case Q2_EN: 294 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) 295 == 0) { 296 printf("enabling user quotas\n"); 297 sblock.fs_quota_flags |= 298 FS_Q2_DO_TYPE(USRQUOTA); 299 sblock.fs_quotafile[USRQUOTA] = 0; 300 } 301 break; 302 case Q2_DIS: 303 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) 304 != 0) { 305 printf("disabling user quotas\n"); 306 sblock.fs_quota_flags &= 307 ~FS_Q2_DO_TYPE(USRQUOTA); 308 } 309 } 310 switch(groupquota) { 311 case Q2_EN: 312 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA)) 313 == 0) { 314 printf("enabling group quotas\n"); 315 sblock.fs_quota_flags |= 316 FS_Q2_DO_TYPE(GRPQUOTA); 317 sblock.fs_quotafile[GRPQUOTA] = 0; 318 } 319 break; 320 case Q2_DIS: 321 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA)) 322 != 0) { 323 printf("disabling group quotas\n"); 324 sblock.fs_quota_flags &= 325 ~FS_Q2_DO_TYPE(GRPQUOTA); 326 } 327 } 328 } 329 /* 330 * if we disabled all quotas, FS_DOQUOTA2 and associated inode(s) will 331 * be cleared by kernel or fsck. 332 */ 333 334 if (Nflag) { 335 printf("tunefs: current settings of %s\n", special); 336 printf("\tmaximum contiguous block count %d\n", 337 sblock.fs_maxcontig); 338 printf("\tmaximum blocks per file in a cylinder group %d\n", 339 sblock.fs_maxbpg); 340 printf("\tminimum percentage of free space %d%%\n", 341 sblock.fs_minfree); 342 printf("\toptimization preference: %s\n", chg[sblock.fs_optim]); 343 printf("\taverage file size: %d\n", sblock.fs_avgfilesize); 344 printf("\texpected number of files per directory: %d\n", 345 sblock.fs_avgfpdir); 346 show_log_info(); 347 printf("\tquotas"); 348 if (sblock.fs_flags & FS_DOQUOTA2) { 349 if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) { 350 printf(" user"); 351 if (sblock.fs_quota_flags & 352 FS_Q2_DO_TYPE(GRPQUOTA)) 353 printf(","); 354 } 355 if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA)) 356 printf(" group"); 357 printf(" enabled\n"); 358 } else { 359 printf(" disabled\n"); 360 } 361 printf("tunefs: no changes made\n"); 362 exit(0); 363 } 364 365 memcpy(&buf, (char *)&sblock, SBLOCKSIZE); 366 if (needswap) 367 ffs_sb_swap((struct fs*)&buf, (struct fs*)&buf); 368 369 /* write superblock to original coordinates (use old dev_bsize!) */ 370 bwrite(sblockloc, buf.data, SBLOCKSIZE, special); 371 372 if (active) { 373 struct ufs_args args; 374 args.fspec = sfs.f_mntfromname; 375 if (mount(MOUNT_FFS, sfs.f_mntonname, sfs.f_flag | MNT_UPDATE, 376 &args, sizeof args) == -1) 377 warn("mount"); 378 else 379 printf("%s: mount of %s on %s updated\n", 380 getprogname(), sfs.f_mntfromname, sfs.f_mntonname); 381 } 382 383 384 /* correct dev_bsize from possibly changed superblock data */ 385 dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1); 386 387 if (Aflag) 388 for (i = 0; i < sblock.fs_ncg; i++) 389 bwrite(FFS_FSBTODB(&sblock, cgsblock(&sblock, i)), 390 buf.data, SBLOCKSIZE, special); 391 close(fi); 392 exit(0); 393} 394 395static void 396show_log_info(void) 397{ 398 const char *loc; 399 uint64_t size, blksize, logsize; 400 int print; 401 402 switch (sblock.fs_journal_location) { 403 case UFS_WAPBL_JOURNALLOC_NONE: 404 print = blksize = 0; 405 /* nothing */ 406 break; 407 case UFS_WAPBL_JOURNALLOC_END_PARTITION: 408 loc = "end of partition"; 409 size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT]; 410 blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ]; 411 print = 1; 412 break; 413 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM: 414 loc = "in filesystem"; 415 size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT]; 416 blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ]; 417 print = 1; 418 break; 419 default: 420 loc = "unknown"; 421 size = blksize = 0; 422 print = 1; 423 break; 424 } 425 426 if (print) { 427 logsize = size * blksize; 428 429 printf("\tjournal log file location: %s\n", loc); 430 printf("\tjournal log file size: "); 431 if (logsize == 0) 432 printf("0\n"); 433 else { 434 char sizebuf[8]; 435 humanize_number(sizebuf, 6, size * blksize, "B", 436 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 437 printf("%s (%" PRId64 " bytes)", sizebuf, logsize); 438 } 439 printf("\n"); 440 printf("\tjournal log flags:"); 441 if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG) 442 printf(" create-log"); 443 if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG) 444 printf(" clear-log"); 445 printf("\n"); 446 } 447} 448 449static void 450change_log_info(long long logfilesize) 451{ 452 /* 453 * NOTES: 454 * - only operate on in-filesystem log sizes 455 * - can't change size of existing log 456 * - if current is same, no action 457 * - if current is zero and new is non-zero, set flag to create log 458 * on next mount 459 * - if current is non-zero and new is zero, set flag to clear log 460 * on next mount 461 */ 462 int in_fs_log; 463 uint64_t old_size; 464 465 old_size = 0; 466 switch (sblock.fs_journal_location) { 467 case UFS_WAPBL_JOURNALLOC_END_PARTITION: 468 in_fs_log = 0; 469 old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] * 470 sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ]; 471 break; 472 473 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM: 474 in_fs_log = 1; 475 old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] * 476 sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ]; 477 break; 478 479 case UFS_WAPBL_JOURNALLOC_NONE: 480 default: 481 in_fs_log = 0; 482 old_size = 0; 483 break; 484 } 485 486 if (logfilesize == 0) { 487 /* 488 * Don't clear out the locators - the kernel might need 489 * these to find the log! Just set the "clear the log" 490 * flag and let the kernel do the rest. 491 */ 492 sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG; 493 sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG; 494 warnx("log file size cleared from %" PRIu64 "", old_size); 495 return; 496 } 497 498 if (!in_fs_log && logfilesize > 0 && old_size > 0) 499 errx(1, "Can't change size of non-in-filesystem log"); 500 501 if (old_size == (uint64_t)logfilesize && logfilesize > 0) { 502 /* no action */ 503 warnx("log file size remains unchanged at %lld", logfilesize); 504 return; 505 } 506 507 if (old_size == 0) { 508 /* create new log of desired size next mount */ 509 sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM; 510 sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0; 511 sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize; 512 sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0; 513 sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0; 514 sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG; 515 sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG; 516 warnx("log file size set to %lld", logfilesize); 517 } else { 518 errx(1, 519 "Can't change existing log size from %" PRIu64 " to %lld", 520 old_size, logfilesize); 521 } 522} 523 524static void 525usage(void) 526{ 527 528 fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n"); 529 fprintf(stderr, "where tuneup-options are:\n"); 530 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 531 fprintf(stderr, "\t-g average file size\n"); 532 fprintf(stderr, "\t-h expected number of files per directory\n"); 533 fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n"); 534 fprintf(stderr, "\t-m minimum percentage of free space\n"); 535 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 536 fprintf(stderr, "\t-q quota type (`[no]user' or `[no]group')\n"); 537 fprintf(stderr, "\t-S sector size\n"); 538 exit(2); 539} 540 541static void 542getsb(struct fs *fs, const char *file) 543{ 544 int i; 545 546 for (i = 0; ; i++) { 547 if (sblock_try[i] == -1) 548 errx(5, "cannot find filesystem superblock"); 549 bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file); 550 switch(fs->fs_magic) { 551 case FS_UFS2_MAGIC: 552 is_ufs2 = 1; 553 /*FALLTHROUGH*/ 554 case FS_UFS1_MAGIC: 555 break; 556 case FS_UFS2_MAGIC_SWAPPED: 557 is_ufs2 = 1; 558 /*FALLTHROUGH*/ 559 case FS_UFS1_MAGIC_SWAPPED: 560 warnx("%s: swapping byte order", file); 561 needswap = 1; 562 ffs_sb_swap(fs, fs); 563 break; 564 default: 565 continue; 566 } 567 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2) 568 continue; 569 if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED) 570 && fs->fs_sblockloc != sblock_try[i]) 571 continue; 572 break; 573 } 574 575 dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1); 576 sblockloc = sblock_try[i] / dev_bsize; 577} 578 579static void 580bwrite(daddr_t blk, char *buffer, int size, const char *file) 581{ 582 off_t offset; 583 584 offset = (off_t)blk * dev_bsize; 585 if (lseek(fi, offset, SEEK_SET) == -1) 586 err(6, "%s: seeking to %lld", file, (long long)offset); 587 if (write(fi, buffer, size) != size) 588 err(7, "%s: writing %d bytes", file, size); 589} 590 591static void 592bread(daddr_t blk, char *buffer, int cnt, const char *file) 593{ 594 off_t offset; 595 int i; 596 597 offset = (off_t)blk * dev_bsize; 598 if (lseek(fi, offset, SEEK_SET) == -1) 599 err(4, "%s: seeking to %lld", file, (long long)offset); 600 if ((i = read(fi, buffer, cnt)) != cnt) 601 errx(5, "%s: short read", file); 602} 603 604static int 605openpartition(const char *name, int flags, char *device, size_t devicelen) 606{ 607 char specname[MAXPATHLEN]; 608 char rawname[MAXPATHLEN]; 609 const char *special, *raw; 610 struct fstab *fs; 611 int fd, oerrno; 612 613 fs = getfsfile(name); 614 special = fs ? fs->fs_spec : name; 615 616 raw = getfsspecname(specname, sizeof(specname), special); 617 if (raw == NULL) 618 err(1, "%s: %s", name, specname); 619 special = getdiskrawname(rawname, sizeof(rawname), raw); 620 if (special == NULL) 621 special = raw; 622 623 fd = opendisk(special, flags, device, devicelen, 0); 624 if (fd == -1 && errno == ENOENT) { 625 oerrno = errno; 626 strlcpy(device, special, devicelen); 627 errno = oerrno; 628 } 629 return (fd); 630} 631