Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sbin / fsirand / fsirand.c
blob36e36ba678f4407439accb8bf991caaeae7746e3
1 /* $NetBSD: fsirand.c,v 1.27 2007/02/08 21:36:58 drochner Exp $ */
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsirand.c,v 1.27 2007/02/08 21:36:58 drochner Exp $");
35 #endif /* lint */
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/vnode.h>
40 #include <sys/disklabel.h>
41 #include <sys/ioctl.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <util.h>
52 #include <signal.h>
54 #include <ufs/ufs/ufs_bswap.h>
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ffs/fs.h>
58 #include <ufs/ffs/ffs_extern.h>
60 static void usage(void);
61 static void getsblock(int, const char *, struct fs *);
62 static void fixinodes(int, struct fs *, struct disklabel *, int, long);
63 static void statussig(int);
65 int needswap, ino, imax, is_ufs2;
66 time_t tstart;
68 static void
69 usage(void)
72 (void) fprintf(stderr,
73 "usage: %s [-F] [-p] [-x <constant>] <special>\n",
74 getprogname());
75 exit(1);
79 static const off_t sblock_try[] = SBLOCKSEARCH;
82 * getsblock():
83 * Return the superblock
85 static void
86 getsblock(int fd, const char *name, struct fs *fs)
88 int i;
90 for (i = 0; ; i++) {
91 if (sblock_try[i] == -1)
92 errx(1, "%s: can't find superblock", name);
93 if (pread(fd, fs, SBLOCKSIZE, sblock_try[i]) != SBLOCKSIZE)
94 continue;
96 switch(fs->fs_magic) {
97 case FS_UFS2_MAGIC:
98 is_ufs2 = 1;
99 /* FALLTHROUGH */
100 case FS_UFS1_MAGIC:
101 break;
102 case FS_UFS2_MAGIC_SWAPPED:
103 is_ufs2 = 1;
104 /* FALLTHROUGH */
105 case FS_UFS1_MAGIC_SWAPPED:
106 needswap = 1;
107 ffs_sb_swap(fs, fs);
108 break;
109 default:
110 continue;
113 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
114 continue;
115 break;
118 if (fs->fs_ncg < 1)
119 errx(1, "%s: bad ncg in superblock", name);
121 if (fs->fs_sbsize > SBLOCKSIZE)
122 errx(1, "%s: superblock too large", name);
127 * fixinodes():
128 * Randomize the inode generation numbers
130 static void
131 fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
133 int inopb = INOPB(fs);
134 int size;
135 caddr_t buf;
136 struct ufs1_dinode *dp1 = NULL;
137 struct ufs2_dinode *dp2 = NULL;
138 int i;
140 size = is_ufs2 ? inopb * sizeof (struct ufs2_dinode) :
141 inopb * sizeof (struct ufs1_dinode);
143 if ((buf = malloc(size)) == NULL)
144 err(1, "Out of memory");
146 if (is_ufs2)
147 dp2 = (struct ufs2_dinode *)buf;
148 else
149 dp1 = (struct ufs1_dinode *)buf;
151 for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
152 off_t sp;
153 sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
154 (off_t) lab->d_secsize;
156 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
157 err(1, "Seeking to inode %d failed", ino);
159 if (read(fd, buf, size) != size)
160 err(1, "Reading inodes %d+%d failed", ino, inopb);
162 for (i = 0; i < inopb; i++) {
163 if (is_ufs2) {
164 if (pflag)
165 printf("inode %10d gen 0x%08x\n",
166 ino,
167 ufs_rw32(dp2[i].di_gen, needswap));
168 else
169 dp2[i].di_gen =
170 ufs_rw32((arc4random() & INT32_MAX)^ xorval,
171 needswap);
172 } else {
173 if (pflag)
174 printf("inode %10d gen 0x%08x\n",
175 ino,
176 ufs_rw32(dp1[i].di_gen, needswap));
177 else
178 dp1[i].di_gen =
179 ufs_rw32((arc4random() & INT32_MAX) ^ xorval,
180 needswap);
182 if (++ino > imax)
183 errx(1, "Exceeded number of inodes");
186 if (pflag)
187 continue;
189 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
190 err(1, "Seeking to inode %d failed", ino);
192 if (write(fd, buf, size) != size)
193 err(1, "Writing inodes %d+%d failed", ino, inopb);
195 free(buf);
199 * statussig():
200 * display current status
202 void
203 statussig(int dummy)
205 char msgbuf[256];
206 int len, deltat;
207 time_t tnow, elapsed;
209 (void)time(&tnow);
210 elapsed = tnow - tstart;
211 len = snprintf(msgbuf, sizeof(msgbuf),
212 "fsirand: completed inode %d of %d (%3.2f%%)",
213 ino, imax, (ino * 100.0) / imax);
214 if (imax - ino) {
215 deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
216 len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
217 ", finished in %d:%02d\n", deltat / 60, deltat % 60);
218 } else {
219 len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
221 write(STDERR_FILENO, msgbuf, len);
225 main(int argc, char *argv[])
227 const char *special;
228 char buf[SBLOCKSIZE], device[MAXPATHLEN];
229 struct fs *fs = (struct fs *) buf;
230 struct disklabel lab;
231 long xorval;
232 char *ep;
233 int fd, c, Fflag, pflag, openflags;
235 xorval = 0;
236 Fflag = pflag = 0;
238 while ((c = getopt(argc, argv, "Fpx:")) != -1)
239 switch (c) {
240 case 'F':
241 Fflag++;
242 break;
243 case 'p':
244 pflag++;
245 break;
246 case 'x':
247 errno = 0;
248 xorval = strtol(optarg, &ep, 0);
249 if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
250 errno == ERANGE)
251 err(1, "Out of range constant");
252 if (*ep)
253 errx(1, "Bad constant");
254 break;
255 default:
256 usage();
259 argv += optind;
260 argc -= optind;
262 if (argc != 1)
263 usage();
265 special = argv[0];
266 openflags = pflag ? O_RDONLY : O_RDWR;
267 if (Fflag)
268 fd = open(special, openflags);
269 else {
270 fd = opendisk(special, openflags, device, sizeof(device), 0);
271 special = device;
273 if (fd == -1)
274 err(1, "Cannot open `%s'", special);
276 if (Fflag) {
277 memset(&lab, 0, sizeof(lab));
278 lab.d_secsize = DEV_BSIZE; /* XXX */
279 } else {
280 if (ioctl(fd, DIOCGDINFO, &lab) == -1)
281 err(1, "%s: cannot get disklabel information", special);
284 time(&tstart);
285 (void)signal(SIGINFO, statussig);
286 getsblock(fd, special, fs);
287 fixinodes(fd, fs, &lab, pflag, xorval);
289 (void) close(fd);
290 return 0;