fsirand.c revision 1.7 1 /* $NetBSD: fsirand.c,v 1.7 1997/09/20 16:28:00 christos Exp $ */
2
3 /*
4 * Copyright (c) 1997 Christos Zoulas. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christos Zoulas.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsirand.c,v 1.7 1997/09/20 16:28:00 christos Exp $");
35 #endif /* lint */
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <err.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/vnode.h>
50 #include <sys/disklabel.h>
51 #include <sys/ioctl.h>
52
53 #include <ufs/ufs/quota.h>
54 #include <ufs/ufs/inode.h>
55
56 #include <ufs/ffs/fs.h>
57
58 static void usage __P((void));
59 static void getsblock __P((int, const char *, struct disklabel *, struct fs *));
60 static void fixinodes __P((int, struct fs *, struct disklabel *, int, long));
61
62 int main __P((int, char *[]));
63
64 static void
65 usage()
66 {
67 extern char *__progname;
68
69 (void) fprintf(stderr, "%s: [-x <constant>] [-p] <special>\n",
70 __progname);
71 exit(1);
72 }
73
74
75 /* getsblock():
76 * Return the superblock
77 */
78 static void
79 getsblock(fd, name, lab, fs)
80 int fd;
81 const char *name;
82 struct disklabel *lab;
83 struct fs *fs;
84 {
85 struct partition *pp = NULL;
86 char p = name[strlen(name) - 1];
87
88 if (p >= 'a' && p <= 'h')
89 pp = &lab->d_partitions[p - 'a'];
90 else if (isdigit((unsigned char) p))
91 pp = &lab->d_partitions[0];
92 else
93 errx(1, "Invalid partition `%c'", p);
94
95 if (pp->p_fstype != FS_BSDFFS)
96 errx(1, "Not an FFS partition");
97
98 if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
99 err(1, "Cannot seek to superblock");
100
101 if (read(fd, fs, SBSIZE) != SBSIZE)
102 err(1, "Cannot read superblock");
103
104 if (fs->fs_magic != FS_MAGIC)
105 errx(1, "Bad superblock magic number");
106
107 if (fs->fs_ncg < 1)
108 errx(1, "Bad ncg in superblock");
109
110 if (fs->fs_cpg < 1)
111 errx(1, "Bad cpg in superblock");
112
113 if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
114 (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
115 errx(1, "Bad number of cylinders in superblock");
116
117 if (fs->fs_sbsize > SBSIZE)
118 errx(1, "Superblock too large");
119 }
120
121 /* fixinodes():
122 * Randomize the inode generation numbers
123 */
124 static void
125 fixinodes(fd, fs, lab, pflag, xorval)
126 int fd;
127 struct fs *fs;
128 struct disklabel *lab;
129 int pflag;
130 long xorval;
131 {
132 int inopb = INOPB(fs);
133 int size = inopb * sizeof(struct dinode);
134 struct dinode *dibuf, *dip;
135 int ino, imax;
136
137 if ((dibuf = malloc(size)) == NULL)
138 err(1, "Out of memory");
139
140 for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
141 off_t sp;
142 #if __GNUC__ /* XXX work around lame compiler problem (gcc 2.7.2) */
143 (void)&sp;
144 #endif
145 sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
146 (off_t) lab->d_secsize;
147
148 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
149 err(1, "Seeking to inode %d failed", ino);
150
151 if (read(fd, dibuf, size) != size)
152 err(1, "Reading inodes %d+%d failed", ino, inopb);
153
154 for (dip = dibuf; dip < &dibuf[inopb]; dip++) {
155 if (pflag)
156 printf("ino %d gen 0x%x\n", ino, dip->di_gen);
157 else
158 dip->di_gen = random() ^ xorval;
159 if (++ino > imax)
160 errx(1, "Exceeded number of inodes");
161 }
162
163 if (pflag)
164 continue;
165
166 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
167 err(1, "Seeking to inode %d failed", ino);
168
169 if (write(fd, dibuf, size) != size)
170 err(1, "Writing inodes %d+%d failed", ino, inopb);
171 }
172 free(dibuf);
173 }
174
175 int
176 main(argc, argv)
177 int argc;
178 char *argv[];
179 {
180 char buf[SBSIZE];
181 struct fs *fs = (struct fs *) buf;
182 struct disklabel lab;
183 int fd, c;
184 long xorval = 0;
185 char *ep;
186 struct timeval tv;
187
188 int pflag = 0;
189
190 while ((c = getopt(argc, argv, "px:")) != -1)
191 switch (c) {
192 case 'p':
193 pflag++;
194 break;
195 case 'x':
196 xorval = strtol(optarg, &ep, 0);
197 if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
198 errno == ERANGE)
199 err(1, "Out of range constant");
200 if (*ep)
201 errx(1, "Bad constant");
202 break;
203 default:
204 usage();
205 }
206
207 argv += optind;
208 argc -= optind;
209
210 if (argc != 1)
211 usage();
212
213 (void) gettimeofday(&tv, NULL);
214 srandom((unsigned) tv.tv_usec);
215
216 if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
217 err(1, "Cannot open `%s'", argv[0]);
218
219 if (ioctl(fd, DIOCGDINFO, &lab) == -1)
220 err(1, "Cannot get label information");
221
222 getsblock(fd, argv[0], &lab, fs);
223 fixinodes(fd, fs, &lab, pflag, xorval);
224
225 (void) close(fd);
226 return 0;
227 }
228