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