fsirand.c revision 1.18 1 1.18 lukem /* $NetBSD: fsirand.c,v 1.18 2001/11/16 14:33:37 lukem 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.18 lukem __RCSID("$NetBSD: fsirand.c,v 1.18 2001/11/16 14:33:37 lukem 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.1 christos #include <ufs/ffs/fs.h>
63 1.8 bouyer #include <ufs/ffs/ffs_extern.h>
64 1.1 christos
65 1.13 lukem static void usage(void);
66 1.17 lukem static void getsblock(int, const char *, struct fs *);
67 1.13 lukem static void fixinodes(int, struct fs *, struct disklabel *, int, long);
68 1.15 lukem static void statussig(int);
69 1.1 christos
70 1.13 lukem int main(int, char *[]);
71 1.1 christos
72 1.15 lukem int needswap, ino, imax;
73 1.15 lukem time_t tstart;
74 1.15 lukem
75 1.8 bouyer
76 1.1 christos static void
77 1.13 lukem usage(void)
78 1.1 christos {
79 1.12 cgd
80 1.15 lukem (void) fprintf(stderr,
81 1.15 lukem "Usage: %s [-F] [-p] [-x <constant>] <special>\n",
82 1.12 cgd getprogname());
83 1.1 christos exit(1);
84 1.1 christos }
85 1.1 christos
86 1.1 christos
87 1.13 lukem /*
88 1.13 lukem * getsblock():
89 1.1 christos * Return the superblock
90 1.1 christos */
91 1.1 christos static void
92 1.17 lukem getsblock(int fd, const char *name, struct fs *fs)
93 1.1 christos {
94 1.1 christos
95 1.1 christos if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
96 1.17 lukem err(1, "%s: cannot seek to superblock", name);
97 1.1 christos
98 1.1 christos if (read(fd, fs, SBSIZE) != SBSIZE)
99 1.17 lukem err(1, "%s: cannot read superblock", name);
100 1.1 christos
101 1.9 ross if (fs->fs_magic != FS_MAGIC) {
102 1.8 bouyer if(fs->fs_magic == bswap32(FS_MAGIC)) {
103 1.8 bouyer needswap = 1;
104 1.14 lukem ffs_sb_swap(fs, fs);
105 1.8 bouyer } else
106 1.17 lukem errx(1, "%s: bad superblock magic number", name);
107 1.9 ross }
108 1.1 christos
109 1.1 christos if (fs->fs_ncg < 1)
110 1.17 lukem errx(1, "%s: bad ncg in superblock", name);
111 1.1 christos
112 1.1 christos if (fs->fs_cpg < 1)
113 1.17 lukem errx(1, "%s: bad cpg in superblock", name);
114 1.1 christos
115 1.1 christos if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
116 1.1 christos (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
117 1.17 lukem errx(1, "%s: bad number of cylinders in superblock", name);
118 1.1 christos
119 1.1 christos if (fs->fs_sbsize > SBSIZE)
120 1.17 lukem errx(1, "%s: superblock too large", name);
121 1.1 christos }
122 1.1 christos
123 1.15 lukem
124 1.13 lukem /*
125 1.13 lukem * fixinodes():
126 1.1 christos * Randomize the inode generation numbers
127 1.1 christos */
128 1.1 christos static void
129 1.13 lukem fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
130 1.1 christos {
131 1.1 christos int inopb = INOPB(fs);
132 1.10 thorpej int size = inopb * DINODE_SIZE;
133 1.10 thorpej caddr_t buf;
134 1.10 thorpej struct dinode *dip;
135 1.15 lukem int i;
136 1.1 christos
137 1.10 thorpej if ((buf = malloc(size)) == NULL)
138 1.1 christos err(1, "Out of memory");
139 1.1 christos
140 1.1 christos for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
141 1.5 mycroft off_t sp;
142 1.5 mycroft sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
143 1.5 mycroft (off_t) lab->d_secsize;
144 1.1 christos
145 1.1 christos if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
146 1.1 christos err(1, "Seeking to inode %d failed", ino);
147 1.1 christos
148 1.10 thorpej if (read(fd, buf, size) != size)
149 1.1 christos err(1, "Reading inodes %d+%d failed", ino, inopb);
150 1.1 christos
151 1.10 thorpej for (i = 0; i < inopb; i++) {
152 1.10 thorpej dip = (struct dinode *)(buf + (i * DINODE_SIZE));
153 1.1 christos if (pflag)
154 1.15 lukem printf("inode %10d gen 0x%08x\n", ino,
155 1.8 bouyer ufs_rw32(dip->di_gen, needswap));
156 1.1 christos else
157 1.13 lukem dip->di_gen = ufs_rw32(random() ^ xorval,
158 1.13 lukem needswap);
159 1.1 christos if (++ino > imax)
160 1.1 christos errx(1, "Exceeded number of inodes");
161 1.1 christos }
162 1.1 christos
163 1.1 christos if (pflag)
164 1.1 christos continue;
165 1.1 christos
166 1.1 christos if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
167 1.1 christos err(1, "Seeking to inode %d failed", ino);
168 1.1 christos
169 1.10 thorpej if (write(fd, buf, size) != size)
170 1.1 christos err(1, "Writing inodes %d+%d failed", ino, inopb);
171 1.1 christos }
172 1.10 thorpej free(buf);
173 1.1 christos }
174 1.1 christos
175 1.15 lukem /*
176 1.15 lukem * statussig():
177 1.15 lukem * display current status
178 1.15 lukem */
179 1.15 lukem void
180 1.15 lukem statussig(int dummy)
181 1.15 lukem {
182 1.15 lukem char msgbuf[256];
183 1.15 lukem int len, deltat;
184 1.15 lukem time_t tnow, elapsed;
185 1.15 lukem
186 1.15 lukem (void)time(&tnow);
187 1.15 lukem elapsed = tnow - tstart;
188 1.15 lukem len = snprintf(msgbuf, sizeof(msgbuf),
189 1.15 lukem "fsirand: completed inode %d of %d (%3.2f%%)",
190 1.15 lukem ino, imax, (ino * 100.0) / imax);
191 1.15 lukem if (imax - ino) {
192 1.15 lukem deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
193 1.15 lukem len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
194 1.15 lukem ", finished in %d:%02d\n", deltat / 60, deltat % 60);
195 1.15 lukem } else {
196 1.15 lukem len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
197 1.15 lukem }
198 1.15 lukem write(STDERR_FILENO, msgbuf, len);
199 1.15 lukem }
200 1.15 lukem
201 1.1 christos int
202 1.13 lukem main(int argc, char *argv[])
203 1.1 christos {
204 1.17 lukem const char *special;
205 1.17 lukem char buf[SBSIZE], device[MAXPATHLEN];
206 1.1 christos struct fs *fs = (struct fs *) buf;
207 1.1 christos struct disklabel lab;
208 1.17 lukem long xorval;
209 1.1 christos char *ep;
210 1.1 christos struct timeval tv;
211 1.17 lukem int fd, c, Fflag, pflag, openflags;
212 1.1 christos
213 1.17 lukem xorval = 0;
214 1.15 lukem Fflag = pflag = 0;
215 1.1 christos
216 1.15 lukem while ((c = getopt(argc, argv, "Fpx:")) != -1)
217 1.1 christos switch (c) {
218 1.15 lukem case 'F':
219 1.15 lukem Fflag++;
220 1.15 lukem break;
221 1.1 christos case 'p':
222 1.1 christos pflag++;
223 1.1 christos break;
224 1.1 christos case 'x':
225 1.1 christos xorval = strtol(optarg, &ep, 0);
226 1.4 kleink if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
227 1.1 christos errno == ERANGE)
228 1.1 christos err(1, "Out of range constant");
229 1.1 christos if (*ep)
230 1.1 christos errx(1, "Bad constant");
231 1.1 christos break;
232 1.1 christos default:
233 1.1 christos usage();
234 1.1 christos }
235 1.1 christos
236 1.1 christos argv += optind;
237 1.1 christos argc -= optind;
238 1.1 christos
239 1.1 christos if (argc != 1)
240 1.1 christos usage();
241 1.1 christos
242 1.1 christos (void) gettimeofday(&tv, NULL);
243 1.1 christos srandom((unsigned) tv.tv_usec);
244 1.1 christos
245 1.17 lukem special = argv[0];
246 1.17 lukem openflags = pflag ? O_RDONLY : O_RDWR;
247 1.17 lukem if (Fflag)
248 1.17 lukem fd = open(special, openflags);
249 1.17 lukem else {
250 1.17 lukem fd = opendisk(special, openflags, device, sizeof(device), 0);
251 1.17 lukem special = device;
252 1.17 lukem }
253 1.17 lukem if (fd == -1)
254 1.17 lukem err(1, "Cannot open `%s'", special);
255 1.1 christos
256 1.15 lukem if (Fflag) {
257 1.15 lukem memset(&lab, 0, sizeof(lab));
258 1.15 lukem lab.d_secsize = DEV_BSIZE; /* XXX */
259 1.17 lukem } else {
260 1.15 lukem if (ioctl(fd, DIOCGDINFO, &lab) == -1)
261 1.17 lukem err(1, "%s: cannot get disklabel information", special);
262 1.17 lukem }
263 1.15 lukem
264 1.15 lukem time(&tstart);
265 1.15 lukem (void)signal(SIGINFO, statussig);
266 1.17 lukem getsblock(fd, special, fs);
267 1.1 christos fixinodes(fd, fs, &lab, pflag, xorval);
268 1.1 christos
269 1.1 christos (void) close(fd);
270 1.1 christos return 0;
271 1.1 christos }
272