tunefs.c revision 1558
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 351558Srgrimesstatic 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 411558Srgrimesstatic char sccsid[] = "@(#)tunefs.c 8.2 (Berkeley) 4/19/94"; 421558Srgrimes#endif /* not lint */ 431558Srgrimes 441558Srgrimes/* 451558Srgrimes * tunefs: change layout parameters to an existing file system. 461558Srgrimes */ 471558Srgrimes#include <sys/param.h> 481558Srgrimes#include <sys/stat.h> 491558Srgrimes 501558Srgrimes#include <ufs/ffs/fs.h> 511558Srgrimes 521558Srgrimes#include <errno.h> 531558Srgrimes#include <err.h> 541558Srgrimes#include <fcntl.h> 551558Srgrimes#include <fstab.h> 561558Srgrimes#include <stdio.h> 571558Srgrimes#include <paths.h> 581558Srgrimes#include <stdlib.h> 591558Srgrimes#include <unistd.h> 601558Srgrimes 611558Srgrimes/* the optimization warning string template */ 621558Srgrimes#define OPTWARN "should optimize for %s with minfree %s %d%%" 631558Srgrimes 641558Srgrimesunion { 651558Srgrimes struct fs sb; 661558Srgrimes char pad[MAXBSIZE]; 671558Srgrimes} sbun; 681558Srgrimes#define sblock sbun.sb 691558Srgrimes 701558Srgrimesint fi; 711558Srgrimeslong dev_bsize = 1; 721558Srgrimes 731558Srgrimesvoid bwrite(daddr_t, char *, int); 741558Srgrimesint bread(daddr_t, char *, int); 751558Srgrimesvoid getsb(struct fs *, char *); 761558Srgrimesvoid usage __P((void)); 771558Srgrimes 781558Srgrimesint 791558Srgrimesmain(argc, argv) 801558Srgrimes int argc; 811558Srgrimes char *argv[]; 821558Srgrimes{ 831558Srgrimes char *cp, *special, *name; 841558Srgrimes struct stat st; 851558Srgrimes int i; 861558Srgrimes int Aflag = 0; 871558Srgrimes struct fstab *fs; 881558Srgrimes char *chg[2], device[MAXPATHLEN]; 891558Srgrimes 901558Srgrimes argc--, argv++; 911558Srgrimes if (argc < 2) 921558Srgrimes usage(); 931558Srgrimes special = argv[argc - 1]; 941558Srgrimes fs = getfsfile(special); 951558Srgrimes if (fs) 961558Srgrimes special = fs->fs_spec; 971558Srgrimesagain: 981558Srgrimes if (stat(special, &st) < 0) { 991558Srgrimes if (*special != '/') { 1001558Srgrimes if (*special == 'r') 1011558Srgrimes special++; 1021558Srgrimes (void)sprintf(device, "%s/%s", _PATH_DEV, special); 1031558Srgrimes special = device; 1041558Srgrimes goto again; 1051558Srgrimes } 1061558Srgrimes err(1, "%s", special); 1071558Srgrimes } 1081558Srgrimes if ((st.st_mode & S_IFMT) != S_IFBLK && 1091558Srgrimes (st.st_mode & S_IFMT) != S_IFCHR) 1101558Srgrimes errx(10, "%s: not a block or character device", special); 1111558Srgrimes getsb(&sblock, special); 1121558Srgrimes for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 1131558Srgrimes for (cp = &argv[0][1]; *cp; cp++) 1141558Srgrimes switch (*cp) { 1151558Srgrimes 1161558Srgrimes case 'A': 1171558Srgrimes Aflag++; 1181558Srgrimes continue; 1191558Srgrimes 1201558Srgrimes case 'a': 1211558Srgrimes name = "maximum contiguous block count"; 1221558Srgrimes if (argc < 1) 1231558Srgrimes errx(10, "-a: missing %s", name); 1241558Srgrimes argc--, argv++; 1251558Srgrimes i = atoi(*argv); 1261558Srgrimes if (i < 1) 1271558Srgrimes errx(10, "%s must be >= 1 (was %s)", 1281558Srgrimes name, *argv); 1291558Srgrimes warnx("%s changes from %d to %d", 1301558Srgrimes name, sblock.fs_maxcontig, i); 1311558Srgrimes sblock.fs_maxcontig = i; 1321558Srgrimes continue; 1331558Srgrimes 1341558Srgrimes case 'd': 1351558Srgrimes name = 1361558Srgrimes "rotational delay between contiguous blocks"; 1371558Srgrimes if (argc < 1) 1381558Srgrimes errx(10, "-d: missing %s", name); 1391558Srgrimes argc--, argv++; 1401558Srgrimes i = atoi(*argv); 1411558Srgrimes warnx("%s changes from %dms to %dms", 1421558Srgrimes name, sblock.fs_rotdelay, i); 1431558Srgrimes sblock.fs_rotdelay = i; 1441558Srgrimes continue; 1451558Srgrimes 1461558Srgrimes case 'e': 1471558Srgrimes name = 1481558Srgrimes "maximum blocks per file in a cylinder group"; 1491558Srgrimes if (argc < 1) 1501558Srgrimes errx(10, "-e: missing %s", name); 1511558Srgrimes argc--, argv++; 1521558Srgrimes i = atoi(*argv); 1531558Srgrimes if (i < 1) 1541558Srgrimes errx(10, "%s must be >= 1 (was %s)", 1551558Srgrimes name, *argv); 1561558Srgrimes warnx("%s changes from %d to %d", 1571558Srgrimes name, sblock.fs_maxbpg, i); 1581558Srgrimes sblock.fs_maxbpg = i; 1591558Srgrimes continue; 1601558Srgrimes 1611558Srgrimes case 'm': 1621558Srgrimes name = "minimum percentage of free space"; 1631558Srgrimes if (argc < 1) 1641558Srgrimes errx(10, "-m: missing %s", name); 1651558Srgrimes argc--, argv++; 1661558Srgrimes i = atoi(*argv); 1671558Srgrimes if (i < 0 || i > 99) 1681558Srgrimes errx(10, "bad %s (%s)", name, *argv); 1691558Srgrimes warnx("%s changes from %d%% to %d%%", 1701558Srgrimes name, sblock.fs_minfree, i); 1711558Srgrimes sblock.fs_minfree = i; 1721558Srgrimes if (i >= MINFREE && 1731558Srgrimes sblock.fs_optim == FS_OPTSPACE) 1741558Srgrimes warnx(OPTWARN, "time", ">=", MINFREE); 1751558Srgrimes if (i < MINFREE && 1761558Srgrimes sblock.fs_optim == FS_OPTTIME) 1771558Srgrimes warnx(OPTWARN, "space", "<", MINFREE); 1781558Srgrimes continue; 1791558Srgrimes 1801558Srgrimes case 'o': 1811558Srgrimes name = "optimization preference"; 1821558Srgrimes if (argc < 1) 1831558Srgrimes errx(10, "-o: missing %s", name); 1841558Srgrimes argc--, argv++; 1851558Srgrimes chg[FS_OPTSPACE] = "space"; 1861558Srgrimes chg[FS_OPTTIME] = "time"; 1871558Srgrimes if (strcmp(*argv, chg[FS_OPTSPACE]) == 0) 1881558Srgrimes i = FS_OPTSPACE; 1891558Srgrimes else if (strcmp(*argv, chg[FS_OPTTIME]) == 0) 1901558Srgrimes i = FS_OPTTIME; 1911558Srgrimes else 1921558Srgrimes errx(10, "bad %s (options are `space' or `time')", 1931558Srgrimes name); 1941558Srgrimes if (sblock.fs_optim == i) { 1951558Srgrimes warnx("%s remains unchanged as %s", 1961558Srgrimes name, chg[i]); 1971558Srgrimes continue; 1981558Srgrimes } 1991558Srgrimes warnx("%s changes from %s to %s", 2001558Srgrimes name, chg[sblock.fs_optim], chg[i]); 2011558Srgrimes sblock.fs_optim = i; 2021558Srgrimes if (sblock.fs_minfree >= MINFREE && 2031558Srgrimes i == FS_OPTSPACE) 2041558Srgrimes warnx(OPTWARN, "time", ">=", MINFREE); 2051558Srgrimes if (sblock.fs_minfree < MINFREE && 2061558Srgrimes i == FS_OPTTIME) 2071558Srgrimes warnx(OPTWARN, "space", "<", MINFREE); 2081558Srgrimes continue; 2091558Srgrimes 2101558Srgrimes default: 2111558Srgrimes usage(); 2121558Srgrimes } 2131558Srgrimes } 2141558Srgrimes if (argc != 1) 2151558Srgrimes usage(); 2161558Srgrimes bwrite((daddr_t)SBOFF / dev_bsize, (char *)&sblock, SBSIZE); 2171558Srgrimes if (Aflag) 2181558Srgrimes for (i = 0; i < sblock.fs_ncg; i++) 2191558Srgrimes bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)), 2201558Srgrimes (char *)&sblock, SBSIZE); 2211558Srgrimes close(fi); 2221558Srgrimes exit(0); 2231558Srgrimes} 2241558Srgrimes 2251558Srgrimesvoid 2261558Srgrimesusage() 2271558Srgrimes{ 2281558Srgrimes 2291558Srgrimes fprintf(stderr, "Usage: tunefs tuneup-options special-device\n"); 2301558Srgrimes fprintf(stderr, "where tuneup-options are:\n"); 2311558Srgrimes fprintf(stderr, "\t-a maximum contiguous blocks\n"); 2321558Srgrimes fprintf(stderr, "\t-d rotational delay between contiguous blocks\n"); 2331558Srgrimes fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 2341558Srgrimes fprintf(stderr, "\t-m minimum percentage of free space\n"); 2351558Srgrimes fprintf(stderr, "\t-o optimization preference (`space' or `time')\n"); 2361558Srgrimes exit(2); 2371558Srgrimes} 2381558Srgrimes 2391558Srgrimesvoid 2401558Srgrimesgetsb(fs, file) 2411558Srgrimes register struct fs *fs; 2421558Srgrimes char *file; 2431558Srgrimes{ 2441558Srgrimes 2451558Srgrimes fi = open(file, 2); 2461558Srgrimes if (fi < 0) 2471558Srgrimes err(3, "cannot open %s", file); 2481558Srgrimes if (bread((daddr_t)SBOFF, (char *)fs, SBSIZE)) 2491558Srgrimes err(4, "%s: bad super block", file); 2501558Srgrimes if (fs->fs_magic != FS_MAGIC) 2511558Srgrimes err(5, "%s: bad magic number", file); 2521558Srgrimes dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); 2531558Srgrimes} 2541558Srgrimes 2551558Srgrimesvoid 2561558Srgrimesbwrite(blk, buf, size) 2571558Srgrimes daddr_t blk; 2581558Srgrimes char *buf; 2591558Srgrimes int size; 2601558Srgrimes{ 2611558Srgrimes 2621558Srgrimes if (lseek(fi, (off_t)blk * dev_bsize, SEEK_SET) < 0) 2631558Srgrimes err(6, "FS SEEK"); 2641558Srgrimes if (write(fi, buf, size) != size) 2651558Srgrimes err(7, "FS WRITE"); 2661558Srgrimes} 2671558Srgrimes 2681558Srgrimesint 2691558Srgrimesbread(bno, buf, cnt) 2701558Srgrimes daddr_t bno; 2711558Srgrimes char *buf; 2721558Srgrimes int cnt; 2731558Srgrimes{ 2741558Srgrimes int i; 2751558Srgrimes 2761558Srgrimes if (lseek(fi, (off_t)bno * dev_bsize, SEEK_SET) < 0) 2771558Srgrimes return(1); 2781558Srgrimes if ((i = read(fi, buf, cnt)) != cnt) { 2791558Srgrimes for(i=0; i<sblock.fs_bsize; i++) 2801558Srgrimes buf[i] = 0; 2811558Srgrimes return (1); 2821558Srgrimes } 2831558Srgrimes return (0); 2841558Srgrimes} 285