fsirand.c revision 1.15 1 /* $NetBSD: fsirand.c,v 1.15 2001/08/19 14:59:39 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: fsirand.c,v 1.15 2001/08/19 14:59:39 lukem Exp $");
42 #endif /* lint */
43
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #include <sys/time.h>
56 #include <sys/vnode.h>
57 #include <sys/disklabel.h>
58 #include <sys/ioctl.h>
59
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_bswap.h>
63
64 #include <ufs/ffs/fs.h>
65 #include <ufs/ffs/ffs_extern.h>
66
67 static void usage(void);
68 static void getsblock(int, const char *, struct disklabel *, struct fs *);
69 static void fixinodes(int, struct fs *, struct disklabel *, int, long);
70 static void statussig(int);
71
72 int main(int, char *[]);
73
74 int needswap, ino, imax;
75 time_t tstart;
76
77
78 static void
79 usage(void)
80 {
81
82 (void) fprintf(stderr,
83 "Usage: %s [-F] [-p] [-x <constant>] <special>\n",
84 getprogname());
85 exit(1);
86 }
87
88
89 /*
90 * getsblock():
91 * Return the superblock
92 */
93 static void
94 getsblock(int fd, const char *name, struct disklabel *lab, struct fs *fs)
95 {
96 struct partition *pp;
97 char p;
98
99 pp = NULL;
100
101 if (lab != NULL) {
102 p = name[strlen(name) - 1];
103 if (p >= 'a' && p <= 'h')
104 pp = &lab->d_partitions[p - 'a'];
105 else if (isdigit((unsigned char) p))
106 pp = &lab->d_partitions[0];
107 else
108 errx(1, "Invalid partition `%c'", p);
109
110 if (pp->p_fstype != FS_BSDFFS)
111 errx(1, "Not an FFS partition");
112 }
113
114 if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
115 err(1, "Cannot seek to superblock");
116
117 if (read(fd, fs, SBSIZE) != SBSIZE)
118 err(1, "Cannot read superblock");
119
120 if (fs->fs_magic != FS_MAGIC) {
121 if(fs->fs_magic == bswap32(FS_MAGIC)) {
122 needswap = 1;
123 ffs_sb_swap(fs, fs);
124 } else
125 errx(1, "Bad superblock magic number");
126 }
127
128 if (fs->fs_ncg < 1)
129 errx(1, "Bad ncg in superblock");
130
131 if (fs->fs_cpg < 1)
132 errx(1, "Bad cpg in superblock");
133
134 if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
135 (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
136 errx(1, "Bad number of cylinders in superblock");
137
138 if (fs->fs_sbsize > SBSIZE)
139 errx(1, "Superblock too large");
140 }
141
142
143 /*
144 * fixinodes():
145 * Randomize the inode generation numbers
146 */
147 static void
148 fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
149 {
150 int inopb = INOPB(fs);
151 int size = inopb * DINODE_SIZE;
152 caddr_t buf;
153 struct dinode *dip;
154 int i;
155
156 if ((buf = malloc(size)) == NULL)
157 err(1, "Out of memory");
158
159 for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
160 off_t sp;
161 sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
162 (off_t) lab->d_secsize;
163
164 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
165 err(1, "Seeking to inode %d failed", ino);
166
167 if (read(fd, buf, size) != size)
168 err(1, "Reading inodes %d+%d failed", ino, inopb);
169
170 for (i = 0; i < inopb; i++) {
171 dip = (struct dinode *)(buf + (i * DINODE_SIZE));
172 if (pflag)
173 printf("inode %10d gen 0x%08x\n", ino,
174 ufs_rw32(dip->di_gen, needswap));
175 else
176 dip->di_gen = ufs_rw32(random() ^ xorval,
177 needswap);
178 if (++ino > imax)
179 errx(1, "Exceeded number of inodes");
180 }
181
182 if (pflag)
183 continue;
184
185 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
186 err(1, "Seeking to inode %d failed", ino);
187
188 if (write(fd, buf, size) != size)
189 err(1, "Writing inodes %d+%d failed", ino, inopb);
190 }
191 free(buf);
192 }
193
194 /*
195 * statussig():
196 * display current status
197 */
198 void
199 statussig(int dummy)
200 {
201 char msgbuf[256];
202 int len, deltat;
203 time_t tnow, elapsed;
204
205 (void)time(&tnow);
206 elapsed = tnow - tstart;
207 len = snprintf(msgbuf, sizeof(msgbuf),
208 "fsirand: completed inode %d of %d (%3.2f%%)",
209 ino, imax, (ino * 100.0) / imax);
210 if (imax - ino) {
211 deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
212 len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
213 ", finished in %d:%02d\n", deltat / 60, deltat % 60);
214 } else {
215 len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
216 }
217 write(STDERR_FILENO, msgbuf, len);
218 }
219
220 int
221 main(int argc, char *argv[])
222 {
223 char buf[SBSIZE];
224 struct fs *fs = (struct fs *) buf;
225 struct disklabel lab;
226 struct stat st;
227 int fd, c;
228 long xorval = 0;
229 char *ep;
230 struct timeval tv;
231 int Fflag, pflag;
232
233 Fflag = pflag = 0;
234
235 while ((c = getopt(argc, argv, "Fpx:")) != -1)
236 switch (c) {
237 case 'F':
238 Fflag++;
239 break;
240 case 'p':
241 pflag++;
242 break;
243 case 'x':
244 xorval = strtol(optarg, &ep, 0);
245 if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
246 errno == ERANGE)
247 err(1, "Out of range constant");
248 if (*ep)
249 errx(1, "Bad constant");
250 break;
251 default:
252 usage();
253 }
254
255 argv += optind;
256 argc -= optind;
257
258 if (argc != 1)
259 usage();
260
261 (void) gettimeofday(&tv, NULL);
262 srandom((unsigned) tv.tv_usec);
263
264 if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
265 err(1, "Cannot open `%s'", argv[0]);
266 if (fstat(fd, &st) == -1)
267 err(1, "Cannot stat `%s'", argv[0]);
268
269 if (Fflag) {
270 if (!S_ISREG(st.st_mode))
271 errx(1, "%s: not a regular file", argv[0]);
272 memset(&lab, 0, sizeof(lab));
273 lab.d_secsize = DEV_BSIZE; /* XXX */
274 } else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
275 if (ioctl(fd, DIOCGDINFO, &lab) == -1)
276 err(1, "%s: cannot get disklabel information", argv[0]);
277 } else
278 errx(1, "%s: not a block or character device", argv[0]);
279
280 time(&tstart);
281 (void)signal(SIGINFO, statussig);
282 getsblock(fd, argv[0], Fflag ? NULL : &lab, fs);
283 fixinodes(fd, fs, &lab, pflag, xorval);
284
285 (void) close(fd);
286 return 0;
287 }
288