fsirand.c revision 1.6 1 /* $NetBSD: fsirand.c,v 1.6 1997/09/14 14:58:54 lukem 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.6 1997/09/14 14:58:54 lukem 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
143 sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
144 (off_t) lab->d_secsize;
145
146 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
147 err(1, "Seeking to inode %d failed", ino);
148
149 if (read(fd, dibuf, size) != size)
150 err(1, "Reading inodes %d+%d failed", ino, inopb);
151
152 for (dip = dibuf; dip < &dibuf[inopb]; dip++) {
153 if (pflag)
154 printf("ino %d gen 0x%x\n", ino, dip->di_gen);
155 else
156 dip->di_gen = random() ^ xorval;
157 if (++ino > imax)
158 errx(1, "Exceeded number of inodes");
159 }
160
161 if (pflag)
162 continue;
163
164 if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
165 err(1, "Seeking to inode %d failed", ino);
166
167 if (write(fd, dibuf, size) != size)
168 err(1, "Writing inodes %d+%d failed", ino, inopb);
169 }
170 free(dibuf);
171 }
172
173 int
174 main(argc, argv)
175 int argc;
176 char *argv[];
177 {
178 char buf[SBSIZE];
179 struct fs *fs = (struct fs *) buf;
180 struct disklabel lab;
181 int fd, c;
182 long xorval = 0;
183 char *ep;
184 struct timeval tv;
185
186 int pflag = 0;
187
188 while ((c = getopt(argc, argv, "px:")) != -1)
189 switch (c) {
190 case 'p':
191 pflag++;
192 break;
193 case 'x':
194 xorval = strtol(optarg, &ep, 0);
195 if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
196 errno == ERANGE)
197 err(1, "Out of range constant");
198 if (*ep)
199 errx(1, "Bad constant");
200 break;
201 default:
202 usage();
203 }
204
205 argv += optind;
206 argc -= optind;
207
208 if (argc != 1)
209 usage();
210
211 (void) gettimeofday(&tv, NULL);
212 srandom((unsigned) tv.tv_usec);
213
214 if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
215 err(1, "Cannot open `%s'", argv[0]);
216
217 if (ioctl(fd, DIOCGDINFO, &lab) == -1)
218 err(1, "Cannot get label information");
219
220 getsblock(fd, argv[0], &lab, fs);
221 fixinodes(fd, fs, &lab, pflag, xorval);
222
223 (void) close(fd);
224 return 0;
225 }
226