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