xref: /csrg-svn/sbin/tunefs/tunefs.c (revision 55659)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1983 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)tunefs.c	5.13 (Berkeley) 07/24/92";
16 #endif /* not lint */
17 
18 /*
19  * tunefs: change layout parameters to an existing file system.
20  */
21 #include <sys/param.h>
22 #include <sys/stat.h>
23 #include <ufs/ffs/fs.h>
24 #include <fstab.h>
25 #include <stdio.h>
26 #include <paths.h>
27 
28 union {
29 	struct	fs sb;
30 	char pad[MAXBSIZE];
31 } sbun;
32 #define	sblock sbun.sb
33 
34 int fi;
35 long dev_bsize = 1;
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 
48 	argc--, argv++;
49 	if (argc < 2)
50 		goto usage;
51 	special = argv[argc - 1];
52 	fs = getfsfile(special);
53 	if (fs)
54 		special = fs->fs_spec;
55 again:
56 	if (stat(special, &st) < 0) {
57 		if (*special != '/') {
58 			if (*special == 'r')
59 				special++;
60 			(void)sprintf(device, "%s/%s", _PATH_DEV, special);
61 			special = device;
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 				fprintf(stdout,
101 					"%s changes from %dms to %dms\n",
102 					name, sblock.fs_rotdelay, i);
103 				sblock.fs_rotdelay = i;
104 				continue;
105 
106 			case 'e':
107 				name =
108 				  "maximum blocks per file in a cylinder group";
109 				if (argc < 1)
110 					fatal("-e: missing %s", name);
111 				argc--, argv++;
112 				i = atoi(*argv);
113 				if (i < 1)
114 					fatal("%s: %s must be >= 1",
115 						*argv, name);
116 				fprintf(stdout, "%s changes from %d to %d\n",
117 					name, sblock.fs_maxbpg, i);
118 				sblock.fs_maxbpg = i;
119 				continue;
120 
121 			case 'm':
122 				name = "minimum percentage of free space";
123 				if (argc < 1)
124 					fatal("-m: missing %s", name);
125 				argc--, argv++;
126 				i = atoi(*argv);
127 				if (i < 0 || i > 99)
128 					fatal("%s: bad %s", *argv, name);
129 				fprintf(stdout,
130 					"%s changes from %d%% to %d%%\n",
131 					name, sblock.fs_minfree, i);
132 				sblock.fs_minfree = i;
133 				if (i >= 10 && sblock.fs_optim == FS_OPTSPACE)
134 					fprintf(stdout, "should optimize %s",
135 					    "for time with minfree >= 10%\n");
136 				if (i < 10 && sblock.fs_optim == FS_OPTTIME)
137 					fprintf(stdout, "should optimize %s",
138 					    "for space with minfree < 10%\n");
139 				continue;
140 
141 			case 'o':
142 				name = "optimization preference";
143 				if (argc < 1)
144 					fatal("-o: missing %s", name);
145 				argc--, argv++;
146 				chg[FS_OPTSPACE] = "space";
147 				chg[FS_OPTTIME] = "time";
148 				if (strcmp(*argv, chg[FS_OPTSPACE]) == 0)
149 					i = FS_OPTSPACE;
150 				else if (strcmp(*argv, chg[FS_OPTTIME]) == 0)
151 					i = FS_OPTTIME;
152 				else
153 					fatal("%s: bad %s (options are `space' or `time')",
154 						*argv, name);
155 				if (sblock.fs_optim == i) {
156 					fprintf(stdout,
157 						"%s remains unchanged as %s\n",
158 						name, chg[i]);
159 					continue;
160 				}
161 				fprintf(stdout,
162 					"%s changes from %s to %s\n",
163 					name, chg[sblock.fs_optim], chg[i]);
164 				sblock.fs_optim = i;
165 				if (sblock.fs_minfree >= 10 && i == FS_OPTSPACE)
166 					fprintf(stdout, "should optimize %s",
167 					    "for time with minfree >= 10%\n");
168 				if (sblock.fs_minfree < 10 && i == FS_OPTTIME)
169 					fprintf(stdout, "should optimize %s",
170 					    "for space with minfree < 10%\n");
171 				continue;
172 
173 			default:
174 				fatal("-%c: unknown flag", *cp);
175 			}
176 	}
177 	if (argc != 1)
178 		goto usage;
179 	bwrite(SBOFF / dev_bsize, (char *)&sblock, SBSIZE);
180 	if (Aflag)
181 		for (i = 0; i < sblock.fs_ncg; i++)
182 			bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
183 			    (char *)&sblock, SBSIZE);
184 	close(fi);
185 	exit(0);
186 usage:
187 	fprintf(stderr, "Usage: tunefs tuneup-options special-device\n");
188 	fprintf(stderr, "where tuneup-options are:\n");
189 	fprintf(stderr, "\t-a maximum contiguous blocks\n");
190 	fprintf(stderr, "\t-d rotational delay between contiguous blocks\n");
191 	fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
192 	fprintf(stderr, "\t-m minimum percentage of free space\n");
193 	fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
194 	exit(2);
195 }
196 
197 getsb(fs, file)
198 	register struct fs *fs;
199 	char *file;
200 {
201 
202 	fi = open(file, 2);
203 	if (fi < 0) {
204 		fprintf(stderr, "tunefs: %s: %s\n", file, strerror(errno));
205 		exit(3);
206 	}
207 	if (bread(SBOFF, (char *)fs, SBSIZE)) {
208 		fprintf(stderr, "tunefs: %s: bad super block\n", file);
209 		exit(4);
210 	}
211 	if (fs->fs_magic != FS_MAGIC) {
212 		fprintf(stderr, "tunefs: %s: bad magic number\n", file);
213 		exit(5);
214 	}
215 	dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
216 }
217 
218 bwrite(blk, buf, size)
219 	char *buf;
220 	daddr_t blk;
221 	register size;
222 {
223 	if (lseek(fi, blk * dev_bsize, 0) < 0) {
224 		perror("FS SEEK");
225 		exit(6);
226 	}
227 	if (write(fi, buf, size) != size) {
228 		perror("FS WRITE");
229 		exit(7);
230 	}
231 }
232 
233 bread(bno, buf, cnt)
234 	daddr_t bno;
235 	char *buf;
236 {
237 	register i;
238 
239 	if (lseek(fi, bno * dev_bsize, 0) < 0)
240 		return(1);
241 	if ((i = read(fi, buf, cnt)) != cnt) {
242 		for(i=0; i<sblock.fs_bsize; i++)
243 			buf[i] = 0;
244 		return (1);
245 	}
246 	return (0);
247 }
248 
249 /* VARARGS1 */
250 fatal(fmt, arg1, arg2)
251 	char *fmt, *arg1, *arg2;
252 {
253 
254 	fprintf(stderr, "tunefs: ");
255 	fprintf(stderr, fmt, arg1, arg2);
256 	putc('\n', stderr);
257 	exit(10);
258 }
259