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