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