111288Smckusick #ifndef lint 2*11295Ssam static char *sccsid = "@(#)tunefs.c 4.2 (Berkeley) 02/26/83"; 311288Smckusick #endif lint 411288Smckusick 511288Smckusick /* 611288Smckusick * tunefs: change layout parameters to an existing file system. 711288Smckusick */ 811288Smckusick 911288Smckusick #include <stdio.h> 1011288Smckusick #include <sys/param.h> 1111288Smckusick #include <sys/stat.h> 1211288Smckusick #include <sys/fs.h> 1311288Smckusick #include <sys/inode.h> 1411288Smckusick 1511288Smckusick union { 1611288Smckusick struct fs sb; 1711288Smckusick char pad[MAXBSIZE]; 1811288Smckusick } sbun; 1911288Smckusick #define sblock sbun.sb 2011288Smckusick 2111288Smckusick int fi; 2211288Smckusick 2311288Smckusick main(argc, argv) 2411288Smckusick int argc; 2511288Smckusick char *argv[]; 2611288Smckusick { 2711288Smckusick char *cp, *special, *name; 2811288Smckusick struct stat st; 2911288Smckusick int i; 3011288Smckusick int Aflag = 0; 3111288Smckusick char device[MAXPATHLEN]; 3211288Smckusick extern char *sprintf(); 3311288Smckusick 3411288Smckusick argc--, argv++; 3511288Smckusick if (argc < 2) 3611288Smckusick goto usage; 3711288Smckusick special = argv[argc - 1]; 3811288Smckusick again: 3911288Smckusick if (stat(special, &st) < 0) { 4011288Smckusick if (*special != '/') { 4111288Smckusick if (*special == 'r') 4211288Smckusick special++; 4311288Smckusick special = sprintf(device, "/dev/%s", special); 4411288Smckusick goto again; 4511288Smckusick } 4611288Smckusick fprintf(stderr, "tunefs: "); perror(special); 4711288Smckusick exit(1); 4811288Smckusick } 4911288Smckusick if ((st.st_mode & S_IFMT) != S_IFBLK && 5011288Smckusick (st.st_mode & S_IFMT) != S_IFCHR) 5111288Smckusick fatal("%s: not a block or character device", special); 5211288Smckusick getsb(&sblock, special); 5311288Smckusick for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) { 5411288Smckusick for (cp = &argv[0][1]; *cp; cp++) 5511288Smckusick switch (*cp) { 5611288Smckusick 5711288Smckusick case 'A': 5811288Smckusick Aflag++; 5911288Smckusick continue; 6011288Smckusick 6111288Smckusick case 'a': 6211288Smckusick name = "maximum contiguous block count"; 6311288Smckusick if (argc < 1) 6411288Smckusick fatal("-a: missing %s", name); 6511288Smckusick argc--, argv++; 6611288Smckusick i = atoi(*argv); 6711288Smckusick if (i < 1) 6811288Smckusick fatal("%s: %s must be >= 1", 6911288Smckusick *argv, name); 7011288Smckusick fprintf(stdout, "%s changes from %d to %d\n", 7111288Smckusick name, sblock.fs_maxcontig, i); 7211288Smckusick sblock.fs_maxcontig = i; 7311288Smckusick continue; 7411288Smckusick 7511288Smckusick case 'd': 7611288Smckusick name = 7711288Smckusick "rotational delay between contiguous blocks"; 7811288Smckusick if (argc < 1) 7911288Smckusick fatal("-d: missing %s", name); 8011288Smckusick argc--, argv++; 8111288Smckusick i = atoi(*argv); 8211288Smckusick if (i < 0) 8311288Smckusick fatal("%s: bad %s", *argv, name); 8411288Smckusick fprintf(stdout, 8511288Smckusick "%s changes from %dms to %dms\n", 8611288Smckusick name, sblock.fs_rotdelay, i); 8711288Smckusick sblock.fs_rotdelay = i; 8811288Smckusick continue; 8911288Smckusick 9011288Smckusick case 'e': 9111288Smckusick name = 9211288Smckusick "maximum blocks per file in a cylinder group"; 9311288Smckusick if (argc < 1) 9411288Smckusick fatal("-e: missing %s", name); 9511288Smckusick argc--, argv++; 9611288Smckusick i = atoi(*argv); 9711288Smckusick if (i < 1) 9811288Smckusick fatal("%s: %s must be >= 1", 9911288Smckusick *argv, name); 10011288Smckusick fprintf(stdout, "%s changes from %d to %d\n", 10111288Smckusick name, sblock.fs_maxbpg, i); 10211288Smckusick sblock.fs_maxbpg = i; 10311288Smckusick continue; 10411288Smckusick 10511288Smckusick case 'm': 10611288Smckusick name = "minimum percentage of free space"; 10711288Smckusick if (argc < 1) 10811288Smckusick fatal("-m: missing %s", name); 10911288Smckusick argc--, argv++; 11011288Smckusick i = atoi(*argv); 11111288Smckusick if (i < 0 || i > 99) 11211288Smckusick fatal("%s: bad %s", *argv, name); 11311288Smckusick fprintf(stdout, 11411288Smckusick "%s changes from %d%% to %d%%\n", 11511288Smckusick name, sblock.fs_minfree, i); 11611288Smckusick sblock.fs_minfree = i; 11711288Smckusick continue; 11811288Smckusick 11911288Smckusick default: 12011288Smckusick fatal("-%c: unknown flag", *cp); 12111288Smckusick } 12211288Smckusick } 12311288Smckusick if (argc != 1) 12411288Smckusick goto usage; 12511288Smckusick bwrite(SBLOCK, (char *)&sblock, SBSIZE); 12611288Smckusick if (Aflag) 12711288Smckusick for (i = 0; i < sblock.fs_ncg; i++) 12811288Smckusick bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)), 12911288Smckusick (char *)&sblock, SBSIZE); 13011288Smckusick close(fi); 13111288Smckusick exit(0); 13211288Smckusick usage: 13311288Smckusick fprintf(stderr, "Usage: tunefs tuneup-options special-device\n"); 13411288Smckusick fprintf(stderr, "where tuneup-options are:\n"); 13511288Smckusick fprintf(stderr, "\t-a maximum contigouous blocks\n"); 13611288Smckusick fprintf(stderr, "\t-d rotational delay between contiguous blocks\n"); 13711288Smckusick fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n"); 13811288Smckusick fprintf(stderr, "\t-m minimum percentage of free space\n"); 13911288Smckusick exit(2); 14011288Smckusick } 14111288Smckusick 14211288Smckusick getsb(fs, file) 14311288Smckusick register struct fs *fs; 14411288Smckusick char *file; 14511288Smckusick { 14611288Smckusick 14711288Smckusick fi = open(file, 2); 14811288Smckusick if (fi < 0) { 14911288Smckusick fprintf(stderr, "cannot open"); 15011288Smckusick perror(file); 15111288Smckusick exit(3); 15211288Smckusick } 15311288Smckusick if (bread(SBLOCK, (char *)fs, SBSIZE)) { 15411288Smckusick fprintf(stderr, "bad super block"); 15511288Smckusick perror(file); 15611288Smckusick exit(4); 15711288Smckusick } 15811288Smckusick if (fs->fs_magic != FS_MAGIC) { 15911288Smckusick fprintf(stderr, "%s: bad magic number\n", file); 16011288Smckusick exit(5); 16111288Smckusick } 16211288Smckusick } 16311288Smckusick 16411288Smckusick bwrite(blk, buf, size) 16511288Smckusick char *buf; 16611288Smckusick daddr_t blk; 16711288Smckusick register size; 16811288Smckusick { 16911288Smckusick if (lseek(fi, blk * DEV_BSIZE, 0) < 0) { 17011288Smckusick perror("FS SEEK"); 17111288Smckusick exit(6); 17211288Smckusick } 17311288Smckusick if (write(fi, buf, size) != size) { 17411288Smckusick perror("FS WRITE"); 17511288Smckusick exit(7); 17611288Smckusick } 17711288Smckusick } 17811288Smckusick 17911288Smckusick bread(bno, buf, cnt) 18011288Smckusick daddr_t bno; 18111288Smckusick char *buf; 18211288Smckusick { 18311288Smckusick register i; 18411288Smckusick 18511288Smckusick if (lseek(fi, bno * DEV_BSIZE, 0) < 0) 18611288Smckusick return(1); 18711288Smckusick if ((i = read(fi, buf, cnt)) != cnt) { 18811288Smckusick for(i=0; i<sblock.fs_bsize; i++) 18911288Smckusick buf[i] = 0; 19011288Smckusick return (1); 19111288Smckusick } 19211288Smckusick return (0); 19311288Smckusick } 19411288Smckusick 19511288Smckusick /* VARARGS1 */ 19611288Smckusick fatal(fmt, arg1, arg2) 19711288Smckusick char *fmt, *arg1, *arg2; 19811288Smckusick { 19911288Smckusick 200*11295Ssam fprintf(stderr, "tunefs: "); 20111288Smckusick fprintf(stderr, fmt, arg1, arg2); 20211288Smckusick putc('\n', stderr); 20311288Smckusick exit(10); 20411288Smckusick } 205