xref: /csrg-svn/sbin/tunefs/tunefs.c (revision 24700)
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
8 char copyright[] =
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)tunefs.c	5.2 (Berkeley) 09/11/85";
15 #endif not lint
16 
17 /*
18  * tunefs: change layout parameters to an existing file system.
19  */
20 
21 #include <sys/param.h>
22 #include <sys/stat.h>
23 #include <sys/fs.h>
24 #include <sys/inode.h>
25 
26 #include <stdio.h>
27 #include <fstab.h>
28 
29 union {
30 	struct	fs sb;
31 	char pad[MAXBSIZE];
32 } sbun;
33 #define	sblock sbun.sb
34 
35 int fi;
36 
37 main(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 	struct fstab *fs;
46 	char *chg[2], device[MAXPATHLEN];
47 	extern char *sprintf();
48 
49 	argc--, argv++;
50 	if (argc < 2)
51 		goto usage;
52 	special = argv[argc - 1];
53 	fs = getfsfile(special);
54 	if (fs)
55 		special = fs->fs_spec;
56 again:
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 			case 'o':
138 				name = "optimization preference";
139 				if (argc < 1)
140 					fatal("-o: missing %s", name);
141 				argc--, argv++;
142 				chg[FS_OPTSPACE] = "space";
143 				chg[FS_OPTTIME] = "time";
144 				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
145 					i = FS_OPTSPACE;
146 				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
147 					i = FS_OPTTIME;
148 				else
149 					fatal("%s: bad %s (options are `space' or `time')",
150 						*argv, name);
151 				if (sblock.fs_optim == i) {
152 					fprintf(stdout,
153 						"%s remains unchanged as %s\n",
154 						name, chg[i]);
155 					continue;
156 				}
157 				fprintf(stdout,
158 					"%s changes from %s to %s\n",
159 					name, chg[sblock.fs_optim], chg[i]);
160 				sblock.fs_optim = i;
161 				continue;
162 
163 			default:
164 				fatal("-%c: unknown flag", *cp);
165 			}
166 	}
167 	if (argc != 1)
168 		goto usage;
169 	bwrite(SBLOCK, (char *)&sblock, SBSIZE);
170 	if (Aflag)
171 		for (i = 0; i < sblock.fs_ncg; i++)
172 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
173 			    (char *)&sblock, SBSIZE);
174 	close(fi);
175 	exit(0);
176 usage:
177 	fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
178 	fprintf(stderr, "where tuneup-options are:\n");
179 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
180 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
181 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
182 	fprintf(stderr, "\t-m minimum percentage of free space\n");
183 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
184 	exit(2);
185 }
186 
187 getsb(fs, file)
188 	register struct fs *fs;
189 	char *file;
190 {
191 
192 	fi = open(file, 2);
193 	if (fi < 0) {
194 		fprintf(stderr, "cannot open");
195 		perror(file);
196 		exit(3);
197 	}
198 	if (bread(SBLOCK, (char *)fs, SBSIZE)) {
199 		fprintf(stderr, "bad super block");
200 		perror(file);
201 		exit(4);
202 	}
203 	if (fs->fs_magic != FS_MAGIC) {
204 		fprintf(stderr, "%s: bad magic number\n", file);
205 		exit(5);
206 	}
207 }
208 
209 bwrite(blk, buf, size)
210 	char *buf;
211 	daddr_t blk;
212 	register size;
213 {
214 	if (lseek(fi, blk * DEV_BSIZE, 0) < 0) {
215 		perror("FS SEEK");
216 		exit(6);
217 	}
218 	if (write(fi, buf, size) != size) {
219 		perror("FS WRITE");
220 		exit(7);
221 	}
222 }
223 
224 bread(bno, buf, cnt)
225 	daddr_t bno;
226 	char *buf;
227 {
228 	register i;
229 
230 	if (lseek(fi, bno * DEV_BSIZE, 0) < 0)
231 		return(1);
232 	if ((i = read(fi, buf, cnt)) != cnt) {
233 		for(i=0; i<sblock.fs_bsize; i++)
234 			buf[i] = 0;
235 		return (1);
236 	}
237 	return (0);
238 }
239 
240 /* VARARGS1 */
241 fatal(fmt, arg1, arg2)
242 	char *fmt, *arg1, *arg2;
243 {
244 
245 	fprintf(stderr, "tunefs: ");
246 	fprintf(stderr, fmt, arg1, arg2);
247 	putc('\n', stderr);
248 	exit(10);
249 }
250