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