new subnets, interface addressing
[unix-history] / usr / src / sbin / tunefs / tunefs.c
CommitLineData
8c5eec2f
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
029a3235 13#ifndef lint
8c5eec2f
DF
14static char sccsid[] = "@(#)tunefs.c 5.1 (Berkeley) %G%";
15#endif not lint
029a3235
KM
16
17/*
18 * tunefs: change layout parameters to an existing file system.
19 */
20
029a3235
KM
21#include <sys/param.h>
22#include <sys/stat.h>
23#include <sys/fs.h>
24#include <sys/inode.h>
25
90354149
SL
26#include <stdio.h>
27#include <fstab.h>
28
029a3235
KM
29union {
30 struct fs sb;
31 char pad[MAXBSIZE];
32} sbun;
33#define sblock sbun.sb
34
35int fi;
36
37main(argc, argv)
38 int argc;
39 char *argv[];
40{
41 char *cp, *special, *name;
42 struct stat st;
43 int i;
44 int Aflag = 0;
45 char device[MAXPATHLEN];
46 extern char *sprintf();
90354149 47 struct fstab *fs;
029a3235
KM
48
49 argc--, argv++;
50 if (argc < 2)
51 goto usage;
52 special = argv[argc - 1];
90354149
SL
53 fs = getfsfile(special);
54 if (fs)
55 special = fs->fs_spec;
029a3235
KM
56again:
57 if (stat(special, &st) < 0) {
58 if (*special != '/') {
59 if (*special == 'r')
60 special++;
61 special = sprintf(device, "/dev/%s", special);
62 goto again;
63 }
64 fprintf(stderr, "tunefs: "); perror(special);
65 exit(1);
66 }
67 if ((st.st_mode & S_IFMT) != S_IFBLK &&
68 (st.st_mode & S_IFMT) != S_IFCHR)
69 fatal("%s: not a block or character device", special);
70 getsb(&sblock, special);
71 for (; argc > 0 && argv[0][0] == '-'; argc--, argv++) {
72 for (cp = &argv[0][1]; *cp; cp++)
73 switch (*cp) {
74
75 case 'A':
76 Aflag++;
77 continue;
78
79 case 'a':
80 name = "maximum contiguous block count";
81 if (argc < 1)
82 fatal("-a: missing %s", name);
83 argc--, argv++;
84 i = atoi(*argv);
85 if (i < 1)
86 fatal("%s: %s must be >= 1",
87 *argv, name);
88 fprintf(stdout, "%s changes from %d to %d\n",
89 name, sblock.fs_maxcontig, i);
90 sblock.fs_maxcontig = i;
91 continue;
92
93 case 'd':
94 name =
95 "rotational delay between contiguous blocks";
96 if (argc < 1)
97 fatal("-d: missing %s", name);
98 argc--, argv++;
99 i = atoi(*argv);
100 if (i < 0)
101 fatal("%s: bad %s", *argv, name);
102 fprintf(stdout,
103 "%s changes from %dms to %dms\n",
104 name, sblock.fs_rotdelay, i);
105 sblock.fs_rotdelay = i;
106 continue;
107
108 case 'e':
109 name =
110 "maximum blocks per file in a cylinder group";
111 if (argc < 1)
112 fatal("-e: missing %s", name);
113 argc--, argv++;
114 i = atoi(*argv);
115 if (i < 1)
116 fatal("%s: %s must be >= 1",
117 *argv, name);
118 fprintf(stdout, "%s changes from %d to %d\n",
119 name, sblock.fs_maxbpg, i);
120 sblock.fs_maxbpg = i;
121 continue;
122
123 case 'm':
124 name = "minimum percentage of free space";
125 if (argc < 1)
126 fatal("-m: missing %s", name);
127 argc--, argv++;
128 i = atoi(*argv);
129 if (i < 0 || i > 99)
130 fatal("%s: bad %s", *argv, name);
131 fprintf(stdout,
132 "%s changes from %d%% to %d%%\n",
133 name, sblock.fs_minfree, i);
134 sblock.fs_minfree = i;
135 continue;
136
137 default:
138 fatal("-%c: unknown flag", *cp);
139 }
140 }
141 if (argc != 1)
142 goto usage;
143 bwrite(SBLOCK, (char *)&sblock, SBSIZE);
144 if (Aflag)
145 for (i = 0; i < sblock.fs_ncg; i++)
146 bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
147 (char *)&sblock, SBSIZE);
148 close(fi);
149 exit(0);
150usage:
151 fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
152 fprintf(stderr, "where tuneup-options are:\n");
1c04f08d 153 fprintf(stderr, "\t-a maximum contiguous blocks\n");
029a3235
KM
154 fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
155 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
156 fprintf(stderr, "\t-m minimum percentage of free space\n");
157 exit(2);
158}
159
160getsb(fs, file)
161 register struct fs *fs;
162 char *file;
163{
164
165 fi = open(file, 2);
166 if (fi < 0) {
167 fprintf(stderr, "cannot open");
168 perror(file);
169 exit(3);
170 }
171 if (bread(SBLOCK, (char *)fs, SBSIZE)) {
172 fprintf(stderr, "bad super block");
173 perror(file);
174 exit(4);
175 }
176 if (fs->fs_magic != FS_MAGIC) {
177 fprintf(stderr, "%s: bad magic number\n", file);
178 exit(5);
179 }
180}
181
182bwrite(blk, buf, size)
183 char *buf;
184 daddr_t blk;
185 register size;
186{
187 if (lseek(fi, blk * DEV_BSIZE, 0) < 0) {
188 perror("FS SEEK");
189 exit(6);
190 }
191 if (write(fi, buf, size) != size) {
192 perror("FS WRITE");
193 exit(7);
194 }
195}
196
197bread(bno, buf, cnt)
198 daddr_t bno;
199 char *buf;
200{
201 register i;
202
203 if (lseek(fi, bno * DEV_BSIZE, 0) < 0)
204 return(1);
205 if ((i = read(fi, buf, cnt)) != cnt) {
206 for(i=0; i<sblock.fs_bsize; i++)
207 buf[i] = 0;
208 return (1);
209 }
210 return (0);
211}
212
213/* VARARGS1 */
214fatal(fmt, arg1, arg2)
215 char *fmt, *arg1, *arg2;
216{
217
fbe74678 218 fprintf(stderr, "tunefs: ");
029a3235
KM
219 fprintf(stderr, fmt, arg1, arg2);
220 putc('\n', stderr);
221 exit(10);
222}