tunefs.c revision 58047
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1983, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3538040Scharnierstatic const char copyright[] = 361558Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 371558Srgrimes The Regents of the University of California. All rights reserved.\n"; 381558Srgrimes#endif /* not lint */ 391558Srgrimes 401558Srgrimes#ifndef lint 4138040Scharnier#if 0 421558Srgrimesstatic char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94"; 4338040Scharnier#endif 4438040Scharnierstatic const char rcsid[] = 4550476Speter "$FreeBSD: head/sbin/tunefs/tunefs.c 58047 2000-03-14 07:44:32Z sheldonh $"; 461558Srgrimes#endif /* not lint */ 471558Srgrimes 481558Srgrimes/* 491558Srgrimes * tunefs: change layout parameters to an existing file system. 501558Srgrimes */ 511558Srgrimes#include <sys/param.h> 5242873Sluoqi#include <sys/mount.h> 531558Srgrimes#include <sys/stat.h> 541558Srgrimes 551558Srgrimes#include <ufs/ffs/fs.h> 5642873Sluoqi#include <ufs/ufs/ufsmount.h> 571558Srgrimes 581558Srgrimes#include <err.h> 591558Srgrimes#include <fcntl.h> 601558Srgrimes#include <fstab.h> 6138040Scharnier#include <paths.h> 621558Srgrimes#include <stdio.h> 631558Srgrimes#include <stdlib.h> 6458047Ssheldonh#include <string.h> 651558Srgrimes#include <unistd.h> 661558Srgrimes 671558Srgrimes/* the optimization warning string template */ 681558Srgrimes#define OPTWARN "should optimize for %s with minfree %s %d%%" 691558Srgrimes 701558Srgrimesunion { 711558Srgrimes struct fs sb; 721558Srgrimes char pad[MAXBSIZE]; 731558Srgrimes} sbun; 741558Srgrimes#define sblock sbun.sb 751558Srgrimes 761558Srgrimesint fi; 771558Srgrimeslong dev_bsize = 1; 781558Srgrimes 7958047Ssheldonhvoid bwrite __P((daddr_t, char *, int)); 8058047Ssheldonhint bread __P((daddr_t, char *, int)); 8158047Ssheldonhvoid getsb __P((struct fs *, char *)); 8258047Ssheldonhvoid putsb __P((struct fs *, char *, int)); 831558Srgrimesvoid usage __P((void)); 849315Sjoergvoid printfs __P((void)); 851558Srgrimes 861558Srgrimesint 871558Srgrimesmain(argc, argv) 881558Srgrimes int argc; 891558Srgrimes char *argv[]; 901558Srgrimes{ 9134266Sjulian char *cp, *special, *name, *action; 921558Srgrimes struct stat st; 931558Srgrimes int i; 9442873Sluoqi int Aflag = 0, active = 0; 951558Srgrimes struct fstab *fs; 961558Srgrimes char *chg[2], device[MAXPATHLEN]; 9742873Sluoqi struct ufs_args args; 9842873Sluoqi struct statfs stfs; 991558Srgrimes 1008871Srgrimes argc--, argv++; 1011558Srgrimes if (argc < 2) 1021558Srgrimes usage(); 1031558Srgrimes special = argv[argc - 1]; 1041558Srgrimes fs = getfsfile(special); 10542873Sluoqi if (fs) { 10648916Sluoqi if (statfs(special, &stfs) == 0 && 10748916Sluoqi strcmp(special, stfs.f_mntonname) == 0) { 10842873Sluoqi active = 1; 10956901Sluoqi } 11056901Sluoqi special = fs->fs_spec; 11142873Sluoqi } 1121558Srgrimesagain: 1131558Srgrimes if (stat(special, &st) < 0) { 1141558Srgrimes if (*special != '/') { 1151558Srgrimes if (*special == 'r') 1161558Srgrimes special++; 1171558Srgrimes (void)sprintf(device, "%s/%s", _PATH_DEV, special); 1181558Srgrimes special = device; 1191558Srgrimes goto again; 1201558Srgrimes } 1211558Srgrimes err(1, "%s", special); 1221558Srgrimes } 1231558Srgrimes if ((st.st_mode & S_IFMT) != S_IFBLK && 1241558Srgrimes (st.st_mode & S_IFMT) != S_IFCHR) 1251558Srgrimes errx(10, "%s: not a block or character device", special); 1261558Srgrimes getsb(&sblock, special); 1271558Srgrimes for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 1281558Srgrimes for (cp = &argv[0][1]; *cp; cp++) 1291558Srgrimes switch (*cp) { 1301558Srgrimes 1311558Srgrimes case 'A': 1321558Srgrimes Aflag++; 1331558Srgrimes continue; 1341558Srgrimes 1359315Sjoerg case 'p': 1369315Sjoerg printfs(); 1379315Sjoerg exit(0); 1389315Sjoerg 1391558Srgrimes case 'a': 1401558Srgrimes name = "maximum contiguous block count"; 1411558Srgrimes if (argc < 1) 1421558Srgrimes errx(10, "-a: missing %s", name); 1431558Srgrimes argc--, argv++; 1441558Srgrimes i = atoi(*argv); 1451558Srgrimes if (i < 1) 1461558Srgrimes errx(10, "%s must be >= 1 (was %s)", 1471558Srgrimes name, *argv); 1481558Srgrimes warnx("%s changes from %d to %d", 1491558Srgrimes name, sblock.fs_maxcontig, i); 1501558Srgrimes sblock.fs_maxcontig = i; 1511558Srgrimes continue; 1521558Srgrimes 1531558Srgrimes case 'd': 1541558Srgrimes name = 1551558Srgrimes "rotational delay between contiguous blocks"; 1561558Srgrimes if (argc < 1) 1571558Srgrimes errx(10, "-d: missing %s", name); 1581558Srgrimes argc--, argv++; 1591558Srgrimes i = atoi(*argv); 1601558Srgrimes warnx("%s changes from %dms to %dms", 1611558Srgrimes name, sblock.fs_rotdelay, i); 1621558Srgrimes sblock.fs_rotdelay = i; 1631558Srgrimes continue; 1641558Srgrimes 1651558Srgrimes case 'e': 1661558Srgrimes name = 1671558Srgrimes "maximum blocks per file in a cylinder group"; 1681558Srgrimes if (argc < 1) 1691558Srgrimes errx(10, "-e: missing %s", name); 1701558Srgrimes argc--, argv++; 1711558Srgrimes i = atoi(*argv); 1721558Srgrimes if (i < 1) 1731558Srgrimes errx(10, "%s must be >= 1 (was %s)", 1741558Srgrimes name, *argv); 1751558Srgrimes warnx("%s changes from %d to %d", 1761558Srgrimes name, sblock.fs_maxbpg, i); 1771558Srgrimes sblock.fs_maxbpg = i; 1781558Srgrimes continue; 1791558Srgrimes 1801558Srgrimes case 'm': 1811558Srgrimes name = "minimum percentage of free space"; 1821558Srgrimes if (argc < 1) 1831558Srgrimes errx(10, "-m: missing %s", name); 1841558Srgrimes argc--, argv++; 1851558Srgrimes i = atoi(*argv); 1861558Srgrimes if (i < 0 || i > 99) 1871558Srgrimes errx(10, "bad %s (%s)", name, *argv); 1881558Srgrimes warnx("%s changes from %d%% to %d%%", 1891558Srgrimes name, sblock.fs_minfree, i); 1901558Srgrimes sblock.fs_minfree = i; 1911558Srgrimes if (i >= MINFREE && 1921558Srgrimes sblock.fs_optim == FS_OPTSPACE) 1931558Srgrimes warnx(OPTWARN, "time", ">=", MINFREE); 1941558Srgrimes if (i < MINFREE && 1951558Srgrimes sblock.fs_optim == FS_OPTTIME) 1961558Srgrimes warnx(OPTWARN, "space", "<", MINFREE); 1971558Srgrimes continue; 1981558Srgrimes 19934266Sjulian case 'n': 20034266Sjulian name = "soft updates"; 20134266Sjulian if (argc < 1) 20256901Sluoqi errx(10, "-n: missing %s", name); 20334266Sjulian argc--, argv++; 20434266Sjulian if (strcmp(*argv, "enable") == 0) { 20534266Sjulian sblock.fs_flags |= FS_DOSOFTDEP; 20634266Sjulian action = "set"; 20734266Sjulian } else if (strcmp(*argv, "disable") == 0) { 20834266Sjulian sblock.fs_flags &= ~FS_DOSOFTDEP; 20934266Sjulian action = "cleared"; 21034266Sjulian } else { 21134266Sjulian errx(10, "bad %s (options are %s)", 21234266Sjulian name, "`enable' or `disable'"); 21334266Sjulian } 21434266Sjulian warnx("%s %s", name, action); 21534266Sjulian continue; 21634266Sjulian 2171558Srgrimes case 'o': 2181558Srgrimes name = "optimization preference"; 2191558Srgrimes if (argc < 1) 2201558Srgrimes errx(10, "-o: missing %s", name); 2211558Srgrimes argc--, argv++; 2221558Srgrimes chg[FS_OPTSPACE] = "space"; 2231558Srgrimes chg[FS_OPTTIME] = "time"; 2241558Srgrimes if (strcmp(*argv, chg[FS_OPTSPACE]) == 0) 2251558Srgrimes i = FS_OPTSPACE; 2261558Srgrimes else if (strcmp(*argv, chg[FS_OPTTIME]) == 0) 2271558Srgrimes i = FS_OPTTIME; 2281558Srgrimes else 2291558Srgrimes errx(10, "bad %s (options are `space' or `time')", 2301558Srgrimes name); 2311558Srgrimes if (sblock.fs_optim == i) { 2321558Srgrimes warnx("%s remains unchanged as %s", 2331558Srgrimes name, chg[i]); 2341558Srgrimes continue; 2351558Srgrimes } 2361558Srgrimes warnx("%s changes from %s to %s", 2371558Srgrimes name, chg[sblock.fs_optim], chg[i]); 2381558Srgrimes sblock.fs_optim = i; 2391558Srgrimes if (sblock.fs_minfree >= MINFREE && 2401558Srgrimes i == FS_OPTSPACE) 2411558Srgrimes warnx(OPTWARN, "time", ">=", MINFREE); 2421558Srgrimes if (sblock.fs_minfree < MINFREE && 2431558Srgrimes i == FS_OPTTIME) 2441558Srgrimes warnx(OPTWARN, "space", "<", MINFREE); 2451558Srgrimes continue; 2461558Srgrimes 2471558Srgrimes default: 2481558Srgrimes usage(); 2491558Srgrimes } 2501558Srgrimes } 2511558Srgrimes if (argc != 1) 2521558Srgrimes usage(); 25358047Ssheldonh putsb(&sblock, special, Aflag); 25442873Sluoqi if (active) { 25542873Sluoqi bzero(&args, sizeof(args)); 25642873Sluoqi if (mount("ufs", fs->fs_file, 25742873Sluoqi stfs.f_flags | MNT_UPDATE | MNT_RELOAD, &args) < 0) 25842873Sluoqi err(9, "%s: reload", special); 25942873Sluoqi warnx("file system reloaded"); 26042873Sluoqi } 2611558Srgrimes exit(0); 2621558Srgrimes} 2631558Srgrimes 2641558Srgrimesvoid 2651558Srgrimesusage() 2661558Srgrimes{ 26738040Scharnier fprintf(stderr, "%s\n%s\n%s\n", 26838040Scharnier"usage: tunefs [-A] [-a maxcontig] [-d rotdelay] [-e maxbpg] [-m minfree]", 26938040Scharnier" [-p] [-n enable | disable] [-o optimize_preference]", 27038040Scharnier" [special | filesystem]"); 2711558Srgrimes exit(2); 2721558Srgrimes} 2731558Srgrimes 2741558Srgrimesvoid 2751558Srgrimesgetsb(fs, file) 2761558Srgrimes register struct fs *fs; 2771558Srgrimes char *file; 2781558Srgrimes{ 2791558Srgrimes 28058047Ssheldonh fi = open(file, O_RDONLY); 2811558Srgrimes if (fi < 0) 2821558Srgrimes err(3, "cannot open %s", file); 2831558Srgrimes if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) 2841558Srgrimes err(4, "%s: bad super block", file); 2851558Srgrimes if (fs->fs_magic != FS_MAGIC) 2861558Srgrimes err(5, "%s: bad magic number", file); 2871558Srgrimes dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 2881558Srgrimes} 2891558Srgrimes 2901558Srgrimesvoid 29158047Ssheldonhputsb(fs, file, all) 29258047Ssheldonh register struct fs *fs; 29358047Ssheldonh char *file; 29458047Ssheldonh int all; 29558047Ssheldonh{ 29658047Ssheldonh int i; 29758047Ssheldonh 29858047Ssheldonh /* 29958047Ssheldonh * Re-open the device read-write. Use the read-only file 30058047Ssheldonh * descriptor as an interlock to prevent the device from 30158047Ssheldonh * being mounted while we are switching mode. 30258047Ssheldonh */ 30358047Ssheldonh i = fi; 30458047Ssheldonh fi = open(file, O_RDWR); 30558047Ssheldonh close(i); 30658047Ssheldonh if (fi < 0) 30758047Ssheldonh err(3, "cannot open %s", file); 30858047Ssheldonh bwrite((daddr_t)SBOFF / dev_bsize, (char *)fs, SBSIZE); 30958047Ssheldonh if (all) 31058047Ssheldonh for (i = 0; i < fs->fs_ncg; i++) 31158047Ssheldonh bwrite(fsbtodb(fs, cgsblock(fs, i)), 31258047Ssheldonh (char *)fs, SBSIZE); 31358047Ssheldonh close(fi); 31458047Ssheldonh} 31558047Ssheldonh 31658047Ssheldonhvoid 3179315Sjoergprintfs() 3189315Sjoerg{ 31934266Sjulian warnx("soft updates: (-n) %s", 32034266Sjulian (sblock.fs_flags & FS_DOSOFTDEP)? "enabled" : "disabled"); 3219315Sjoerg warnx("maximum contiguous block count: (-a) %d", 3229315Sjoerg sblock.fs_maxcontig); 3239315Sjoerg warnx("rotational delay between contiguous blocks: (-d) %d ms", 3249315Sjoerg sblock.fs_rotdelay); 3259315Sjoerg warnx("maximum blocks per file in a cylinder group: (-e) %d", 3269315Sjoerg sblock.fs_maxbpg); 3279315Sjoerg warnx("minimum percentage of free space: (-m) %d%%", 3289315Sjoerg sblock.fs_minfree); 3299315Sjoerg warnx("optimization preference: (-o) %s", 3309315Sjoerg sblock.fs_optim == FS_OPTSPACE ? "space" : "time"); 3319315Sjoerg if (sblock.fs_minfree >= MINFREE && 3329315Sjoerg sblock.fs_optim == FS_OPTSPACE) 3339315Sjoerg warnx(OPTWARN, "time", ">=", MINFREE); 3349315Sjoerg if (sblock.fs_minfree < MINFREE && 3359315Sjoerg sblock.fs_optim == FS_OPTTIME) 3369315Sjoerg warnx(OPTWARN, "space", "<", MINFREE); 3379315Sjoerg} 3389315Sjoerg 3399315Sjoergvoid 3401558Srgrimesbwrite(blk, buf, size) 3411558Srgrimes daddr_t blk; 3421558Srgrimes char *buf; 3431558Srgrimes int size; 3441558Srgrimes{ 3451558Srgrimes 3461558Srgrimes if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) 3471558Srgrimes err(6, "FS SEEK"); 3481558Srgrimes if (write(fi, buf, size) != size) 3491558Srgrimes err(7, "FS WRITE"); 3501558Srgrimes} 3511558Srgrimes 3521558Srgrimesint 3531558Srgrimesbread(bno, buf, cnt) 3541558Srgrimes daddr_t bno; 3551558Srgrimes char *buf; 3561558Srgrimes int cnt; 3571558Srgrimes{ 3581558Srgrimes int i; 3591558Srgrimes 3601558Srgrimes if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0) 3611558Srgrimes return(1); 3621558Srgrimes if ((i = read(fi, buf, cnt)) != cnt) { 3631558Srgrimes for(i=0; i<sblock.fs_bsize; i++) 3641558Srgrimes buf[i] = 0; 3651558Srgrimes return (1); 3661558Srgrimes } 3671558Srgrimes return (0); 3681558Srgrimes} 369