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