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