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