fsirand.c revision 1.13 1 /* $NetBSD: fsirand.c,v 1.13 2001/07/29 11:15:29 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.13 2001/07/29 11:15:29 lukem Exp $");
42 #endif /* lint */
43
44 #include <stdio.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <err.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #include <sys/time.h>
56 #include <sys/vnode.h>
57 #include <sys/disklabel.h>
58 #include <sys/ioctl.h>
59
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/ufs_bswap.h>
63
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 disklabel *, struct fs *);
69 static void fixinodes(int, struct fs *, struct disklabel *, int, long);
70
71 int main(int, char *[]);
72
73 int needswap = 0;
74
75 static void
76 usage(void)
77 {
78
79 (void) fprintf(stderr, "Usage: %s [-x <constant>] [-p] <special>\n",
80 getprogname());
81 exit(1);
82 }
83
84
85 /*
86 * getsblock():
87 * Return the superblock
88 */
89 static void
90 getsblock(int fd, const char *name, struct disklabel *lab, struct fs *fs)
91 {
92 struct partition *pp;
93 char p;
94
95 pp = NULL;
96 p = name[strlen(name) - 1];
97 if (p >= 'a' && p <= 'h')
98 pp = &lab->d_partitions[p - 'a'];
99 else if (isdigit((unsigned char) p))
100 pp = &lab->d_partitions[0];
101 else
102 errx(1, "Invalid partition `%c'", p);
103
104 if (pp->p_fstype != FS_BSDFFS)
105 errx(1, "Not an FFS partition");
106
107 if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
108 err(1, "Cannot seek to superblock");
109
110 if (read(fd, fs, SBSIZE) != SBSIZE)
111 err(1, "Cannot read superblock");
112
113 if (fs->fs_magic != FS_MAGIC) {
114 if(fs->fs_magic == bswap32(FS_MAGIC)) {
115 needswap = 1;
116 ffs_sb_swap(fs, fs, 0);
117 } else
118 errx(1, "Bad superblock magic number");
119 }
120
121 if (fs->fs_ncg < 1)
122 errx(1, "Bad ncg in superblock");
123
124 if (fs->fs_cpg < 1)
125 errx(1, "Bad cpg in superblock");
126
127 if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
128 (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
129 errx(1, "Bad number of cylinders in superblock");
130
131 if (fs->fs_sbsize > SBSIZE)
132 errx(1, "Superblock too large");
133 }
134
135 /*
136 * fixinodes():
137 * Randomize the inode generation numbers
138 */
139 static void
140 fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
141 {
142 int inopb = INOPB(fs);
143 int size = inopb * DINODE_SIZE;
144 caddr_t buf;
145 struct dinode *dip;
146 int i, ino, imax;
147
148 if ((buf = malloc(size)) == NULL)
149 err(1, "Out of memory");
150
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;
155
156 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
157 err(1, "Seeking to inode %d failed", ino);
158
159 if (read(fd, buf, size) != size)
160 err(1, "Reading inodes %d+%d failed", ino, inopb);
161
162 for (i = 0; i < inopb; i++) {
163 dip = (struct dinode *)(buf + (i * DINODE_SIZE));
164 if (pflag)
165 printf("ino %d gen 0x%x\n", ino,
166 ufs_rw32(dip->di_gen, needswap));
167 else
168 dip->di_gen = ufs_rw32(random() ^ xorval,
169 needswap);
170 if (++ino > imax)
171 errx(1, "Exceeded number of inodes");
172 }
173
174 if (pflag)
175 continue;
176
177 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
178 err(1, "Seeking to inode %d failed", ino);
179
180 if (write(fd, buf, size) != size)
181 err(1, "Writing inodes %d+%d failed", ino, inopb);
182 }
183 free(buf);
184 }
185
186 int
187 main(int argc, char *argv[])
188 {
189 char buf[SBSIZE];
190 struct fs *fs = (struct fs *) buf;
191 struct disklabel lab;
192 int fd, c;
193 long xorval = 0;
194 char *ep;
195 struct timeval tv;
196
197 int pflag = 0;
198
199 while ((c = getopt(argc, argv, "px:")) != -1)
200 switch (c) {
201 case 'p':
202 pflag++;
203 break;
204 case 'x':
205 xorval = strtol(optarg, &ep, 0);
206 if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
207 errno == ERANGE)
208 err(1, "Out of range constant");
209 if (*ep)
210 errx(1, "Bad constant");
211 break;
212 default:
213 usage();
214 }
215
216 argv += optind;
217 argc -= optind;
218
219 if (argc != 1)
220 usage();
221
222 (void) gettimeofday(&tv, NULL);
223 srandom((unsigned) tv.tv_usec);
224
225 if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
226 err(1, "Cannot open `%s'", argv[0]);
227
228 if (ioctl(fd, DIOCGDINFO, &lab) == -1)
229 err(1, "Cannot get label information");
230
231 getsblock(fd, argv[0], &lab, fs);
232 fixinodes(fd, fs, &lab, pflag, xorval);
233
234 (void) close(fd);
235 return 0;
236 }
237