Annotation of 43BSDReno/sbin/tunefs/tunefs.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1983 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: char copyright[] =
                     22: "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
                     23:  All rights reserved.\n";
                     24: #endif /* not lint */
                     25: 
                     26: #ifndef lint
                     27: static char sccsid[] = "@(#)tunefs.c   5.11 (Berkeley) 6/1/90";
                     28: #endif /* not lint */
                     29: 
                     30: /*
                     31:  * tunefs: change layout parameters to an existing file system.
                     32:  */
                     33: #include <sys/param.h>
                     34: #include <sys/stat.h>
                     35: #include <ufs/fs.h>
                     36: #include <fstab.h>
                     37: #include <stdio.h>
                     38: #include <paths.h>
                     39: 
                     40: union {
                     41:        struct  fs sb;
                     42:        char pad[MAXBSIZE];
                     43: } sbun;
                     44: #define        sblock sbun.sb
                     45: 
                     46: int fi;
                     47: long dev_bsize = 1;
                     48: 
                     49: main(argc, argv)
                     50:        int argc;
                     51:        char *argv[];
                     52: {
                     53:        char *cp, *special, *name;
                     54:        struct stat st;
                     55:        int i;
                     56:        int Aflag = 0;
                     57:        struct fstab *fs;
                     58:        char *chg[2], device[MAXPATHLEN];
                     59: 
                     60:        argc--, argv++; 
                     61:        if (argc < 2)
                     62:                goto usage;
                     63:        special = argv[argc - 1];
                     64:        fs = getfsfile(special);
                     65:        if (fs)
                     66:                special = fs->fs_spec;
                     67: again:
                     68:        if (stat(special, &st) < 0) {
                     69:                if (*special != '/') {
                     70:                        if (*special == 'r')
                     71:                                special++;
                     72:                        (void)sprintf(device, "%s/%s", _PATH_DEV, special);
                     73:                        special = device;
                     74:                        goto again;
                     75:                }
                     76:                fprintf(stderr, "tunefs: "); perror(special);
                     77:                exit(1);
                     78:        }
                     79:        if ((st.st_mode & S_IFMT) != S_IFBLK &&
                     80:            (st.st_mode & S_IFMT) != S_IFCHR)
                     81:                fatal("%s: not a block or character device", special);
                     82:        getsb(&sblock, special);
                     83:        for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
                     84:                for (cp = &argv[0][1]; *cp; cp++)
                     85:                        switch (*cp) {
                     86: 
                     87:                        case 'A':
                     88:                                Aflag++;
                     89:                                continue;
                     90: 
                     91:                        case 'a':
                     92:                                name = "maximum contiguous block count";
                     93:                                if (argc < 1)
                     94:                                        fatal("-a: missing %s", name);
                     95:                                argc--, argv++;
                     96:                                i = atoi(*argv);
                     97:                                if (i < 1)
                     98:                                        fatal("%s: %s must be >= 1",
                     99:                                                *argv, name);
                    100:                                fprintf(stdout, "%s changes from %d to %d\n",
                    101:                                        name, sblock.fs_maxcontig, i);
                    102:                                sblock.fs_maxcontig = i;
                    103:                                continue;
                    104: 
                    105:                        case 'd':
                    106:                                name =
                    107:                                   "rotational delay between contiguous blocks";
                    108:                                if (argc < 1)
                    109:                                        fatal("-d: missing %s", name);
                    110:                                argc--, argv++;
                    111:                                i = atoi(*argv);
                    112:                                fprintf(stdout,
                    113:                                        "%s changes from %dms to %dms\n",
                    114:                                        name, sblock.fs_rotdelay, i);
                    115:                                sblock.fs_rotdelay = i;
                    116:                                continue;
                    117: 
                    118:                        case 'e':
                    119:                                name =
                    120:                                  "maximum blocks per file in a cylinder group";
                    121:                                if (argc < 1)
                    122:                                        fatal("-e: missing %s", name);
                    123:                                argc--, argv++;
                    124:                                i = atoi(*argv);
                    125:                                if (i < 1)
                    126:                                        fatal("%s: %s must be >= 1",
                    127:                                                *argv, name);
                    128:                                fprintf(stdout, "%s changes from %d to %d\n",
                    129:                                        name, sblock.fs_maxbpg, i);
                    130:                                sblock.fs_maxbpg = i;
                    131:                                continue;
                    132: 
                    133:                        case 'm':
                    134:                                name = "minimum percentage of free space";
                    135:                                if (argc < 1)
                    136:                                        fatal("-m: missing %s", name);
                    137:                                argc--, argv++;
                    138:                                i = atoi(*argv);
                    139:                                if (i < 0 || i > 99)
                    140:                                        fatal("%s: bad %s", *argv, name);
                    141:                                fprintf(stdout,
                    142:                                        "%s changes from %d%% to %d%%\n",
                    143:                                        name, sblock.fs_minfree, i);
                    144:                                sblock.fs_minfree = i;
                    145:                                if (i >= 10 && sblock.fs_optim == FS_OPTSPACE)
                    146:                                        fprintf(stdout, "should optimize %s",
                    147:                                            "for time with minfree >= 10%\n");
                    148:                                if (i < 10 && sblock.fs_optim == FS_OPTTIME)
                    149:                                        fprintf(stdout, "should optimize %s",
                    150:                                            "for space with minfree < 10%\n");
                    151:                                continue;
                    152: 
                    153:                        case 'o':
                    154:                                name = "optimization preference";
                    155:                                if (argc < 1)
                    156:                                        fatal("-o: missing %s", name);
                    157:                                argc--, argv++;
                    158:                                chg[FS_OPTSPACE] = "space";
                    159:                                chg[FS_OPTTIME] = "time";
                    160:                                if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
                    161:                                        i = FS_OPTSPACE;
                    162:                                else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
                    163:                                        i = FS_OPTTIME;
                    164:                                else
                    165:                                        fatal("%s: bad %s (options are `space' or `time')",
                    166:                                                *argv, name);
                    167:                                if (sblock.fs_optim == i) {
                    168:                                        fprintf(stdout,
                    169:                                                "%s remains unchanged as %s\n",
                    170:                                                name, chg[i]);
                    171:                                        continue;
                    172:                                }
                    173:                                fprintf(stdout,
                    174:                                        "%s changes from %s to %s\n",
                    175:                                        name, chg[sblock.fs_optim], chg[i]);
                    176:                                sblock.fs_optim = i;
                    177:                                if (sblock.fs_minfree >= 10 && i == FS_OPTSPACE)
                    178:                                        fprintf(stdout, "should optimize %s",
                    179:                                            "for time with minfree >= 10%\n");
                    180:                                if (sblock.fs_minfree < 10 && i == FS_OPTTIME)
                    181:                                        fprintf(stdout, "should optimize %s",
                    182:                                            "for space with minfree < 10%\n");
                    183:                                continue;
                    184: 
                    185:                        default:
                    186:                                fatal("-%c: unknown flag", *cp);
                    187:                        }
                    188:        }
                    189:        if (argc != 1)
                    190:                goto usage;
                    191:        bwrite(SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
                    192:        if (Aflag)
                    193:                for (i = 0; i < sblock.fs_ncg; i++)
                    194:                        bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
                    195:                            (char *)&sblock, SBSIZE);
                    196:        close(fi);
                    197:        exit(0);
                    198: usage:
                    199:        fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
                    200:        fprintf(stderr, "where tuneup-options are:\n");
                    201:        fprintf(stderr, "\t-a maximum contiguous blocks\n");
                    202:        fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
                    203:        fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
                    204:        fprintf(stderr, "\t-m minimum percentage of free space\n");
                    205:        fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
                    206:        exit(2);
                    207: }
                    208: 
                    209: getsb(fs, file)
                    210:        register struct fs *fs;
                    211:        char *file;
                    212: {
                    213: 
                    214:        fi = open(file, 2);
                    215:        if (fi < 0) {
                    216:                fprintf(stderr, "cannot open");
                    217:                perror(file);
                    218:                exit(3);
                    219:        }
                    220:        if (bread(SBOFF, (char *)fs, SBSIZE)) {
                    221:                fprintf(stderr, "bad super block");
                    222:                perror(file);
                    223:                exit(4);
                    224:        }
                    225:        if (fs->fs_magic != FS_MAGIC) {
                    226:                fprintf(stderr, "%s: bad magic number\n", file);
                    227:                exit(5);
                    228:        }
                    229:        dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
                    230: }
                    231: 
                    232: bwrite(blk, buf, size)
                    233:        char *buf;
                    234:        daddr_t blk;
                    235:        register size;
                    236: {
                    237:        if (lseek(fi, blk * dev_bsize, 0) < 0) {
                    238:                perror("FS SEEK");
                    239:                exit(6);
                    240:        }
                    241:        if (write(fi, buf, size) != size) {
                    242:                perror("FS WRITE");
                    243:                exit(7);
                    244:        }
                    245: }
                    246: 
                    247: bread(bno, buf, cnt)
                    248:        daddr_t bno;
                    249:        char *buf;
                    250: {
                    251:        register i;
                    252: 
                    253:        if (lseek(fi, bno * dev_bsize, 0) < 0)
                    254:                return(1);
                    255:        if ((i = read(fi, buf, cnt)) != cnt) {
                    256:                for(i=0; i<sblock.fs_bsize; i++)
                    257:                        buf[i] = 0;
                    258:                return (1);
                    259:        }
                    260:        return (0);
                    261: }
                    262: 
                    263: /* VARARGS1 */
                    264: fatal(fmt, arg1, arg2)
                    265:        char *fmt, *arg1, *arg2;
                    266: {
                    267: 
                    268:        fprintf(stderr, "tunefs: ");
                    269:        fprintf(stderr, fmt, arg1, arg2);
                    270:        putc('\n', stderr);
                    271:        exit(10);
                    272: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.