fsirand.c revision 1.24 1 /* $NetBSD: fsirand.c,v 1.24 2004/03/21 20:12:16 dsl 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.24 2004/03/21 20:12:16 dsl Exp $");
42 #endif /* lint */
43
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/vnode.h>
47 #include <sys/disklabel.h>
48 #include <sys/ioctl.h>
49
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <util.h>
59
60 #include <ufs/ufs/ufs_bswap.h>
61
62 #include <ufs/ufs/dinode.h>
63 #include <ufs/ffs/fs.h>
64 #include <ufs/ffs/ffs_extern.h>
65
66 static void usage(void);
67 static void getsblock(int, const char *, struct fs *);
68 static void fixinodes(int, struct fs *, struct disklabel *, int, long);
69 static void statussig(int);
70
71 int main(int, char *[]);
72
73 int needswap, ino, imax, is_ufs2;
74 time_t tstart;
75
76
77 static void
78 usage(void)
79 {
80
81 (void) fprintf(stderr,
82 "usage: %s [-F] [-p] [-x <constant>] <special>\n",
83 getprogname());
84 exit(1);
85 }
86
87
88 static const off_t sblock_try[] = SBLOCKSEARCH;
89
90 /*
91 * getsblock():
92 * Return the superblock
93 */
94 static void
95 getsblock(int fd, const char *name, struct fs *fs)
96 {
97 int i;
98
99 for (i = 0; ; i++) {
100 if (sblock_try[i] == -1)
101 errx(1, "%s: can't find superblock", name);
102 if (pread(fd, fs, SBLOCKSIZE, sblock_try[i]) != SBLOCKSIZE)
103 continue;
104
105 switch(fs->fs_magic) {
106 case FS_UFS2_MAGIC:
107 is_ufs2 = 1;
108 /* FALLTHROUGH */
109 case FS_UFS1_MAGIC:
110 break;
111 case FS_UFS2_MAGIC_SWAPPED:
112 is_ufs2 = 1;
113 /* FALLTHROUGH */
114 case FS_UFS1_MAGIC_SWAPPED:
115 needswap = 1;
116 ffs_sb_swap(fs, fs);
117 break;
118 default:
119 continue;
120 }
121
122 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
123 continue;
124 break;
125 }
126
127 if (fs->fs_ncg < 1)
128 errx(1, "%s: bad ncg in superblock", name);
129
130 if (fs->fs_sbsize > SBLOCKSIZE)
131 errx(1, "%s: superblock too large", name);
132 }
133
134
135 /*
136 * fixinodes():
137 * Randomize the inode generation numbers
138 */
139 static void
140 fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
141 {
142 int inopb = INOPB(fs);
143 int size;
144 caddr_t buf;
145 struct ufs1_dinode *dp1;
146 struct ufs2_dinode *dp2;
147 int i;
148
149 size = is_ufs2 ? inopb * sizeof (struct ufs2_dinode) :
150 inopb * sizeof (struct ufs1_dinode);
151
152 if ((buf = malloc(size)) == NULL)
153 err(1, "Out of memory");
154
155 if (is_ufs2)
156 dp2 = (struct ufs2_dinode *)buf;
157 else
158 dp1 = (struct ufs1_dinode *)buf;
159
160 for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
161 off_t sp;
162 sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
163 (off_t) lab->d_secsize;
164
165 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
166 err(1, "Seeking to inode %d failed", ino);
167
168 if (read(fd, buf, size) != size)
169 err(1, "Reading inodes %d+%d failed", ino, inopb);
170
171 for (i = 0; i < inopb; i++) {
172 if (is_ufs2) {
173 if (pflag)
174 printf("inode %10d gen 0x%08x\n",
175 ino,
176 ufs_rw32(dp2[i].di_gen, needswap));
177 else
178 dp2[i].di_gen =
179 ufs_rw32((arc4random() & INT32_MAX)^ xorval,
180 needswap);
181 } else {
182 if (pflag)
183 printf("inode %10d gen 0x%08x\n",
184 ino,
185 ufs_rw32(dp1[i].di_gen, needswap));
186 else
187 dp1[i].di_gen =
188 ufs_rw32((arc4random() & INT32_MAX) ^ xorval,
189 needswap);
190 }
191 if (++ino > imax)
192 errx(1, "Exceeded number of inodes");
193 }
194
195 if (pflag)
196 continue;
197
198 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
199 err(1, "Seeking to inode %d failed", ino);
200
201 if (write(fd, buf, size) != size)
202 err(1, "Writing inodes %d+%d failed", ino, inopb);
203 }
204 free(buf);
205 }
206
207 /*
208 * statussig():
209 * display current status
210 */
211 void
212 statussig(int dummy)
213 {
214 char msgbuf[256];
215 int len, deltat;
216 time_t tnow, elapsed;
217
218 (void)time(&tnow);
219 elapsed = tnow - tstart;
220 len = snprintf(msgbuf, sizeof(msgbuf),
221 "fsirand: completed inode %d of %d (%3.2f%%)",
222 ino, imax, (ino * 100.0) / imax);
223 if (imax - ino) {
224 deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
225 len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
226 ", finished in %d:%02d\n", deltat / 60, deltat % 60);
227 } else {
228 len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
229 }
230 write(STDERR_FILENO, msgbuf, len);
231 }
232
233 int
234 main(int argc, char *argv[])
235 {
236 const char *special;
237 char buf[SBLOCKSIZE], device[MAXPATHLEN];
238 struct fs *fs = (struct fs *) buf;
239 struct disklabel lab;
240 long xorval;
241 char *ep;
242 int fd, c, Fflag, pflag, openflags;
243
244 xorval = 0;
245 Fflag = pflag = 0;
246
247 while ((c = getopt(argc, argv, "Fpx:")) != -1)
248 switch (c) {
249 case 'F':
250 Fflag++;
251 break;
252 case 'p':
253 pflag++;
254 break;
255 case 'x':
256 errno = 0;
257 xorval = strtol(optarg, &ep, 0);
258 if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
259 errno == ERANGE)
260 err(1, "Out of range constant");
261 if (*ep)
262 errx(1, "Bad constant");
263 break;
264 default:
265 usage();
266 }
267
268 argv += optind;
269 argc -= optind;
270
271 if (argc != 1)
272 usage();
273
274 special = argv[0];
275 openflags = pflag ? O_RDONLY : O_RDWR;
276 if (Fflag)
277 fd = open(special, openflags);
278 else {
279 fd = opendisk(special, openflags, device, sizeof(device), 0);
280 special = device;
281 }
282 if (fd == -1)
283 err(1, "Cannot open `%s'", special);
284
285 if (Fflag) {
286 memset(&lab, 0, sizeof(lab));
287 lab.d_secsize = DEV_BSIZE; /* XXX */
288 } else {
289 if (ioctl(fd, DIOCGDINFO, &lab) == -1)
290 err(1, "%s: cannot get disklabel information", special);
291 }
292
293 time(&tstart);
294 (void)signal(SIGINFO, statussig);
295 getsblock(fd, special, fs);
296 fixinodes(fd, fs, &lab, pflag, xorval);
297
298 (void) close(fd);
299 return 0;
300 }
301