fsirand.c revision 24216
1/*	$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $	*/
2
3/*
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Todd C. Miller.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
24 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef lint
34static char rcsid[] = "$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $";
35#endif /* not lint */
36
37#include <sys/types.h>
38#include <sys/disklabel.h>
39#include <sys/ioctl.h>
40#include <sys/param.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43
44#include <ufs/ffs/fs.h>
45#include <ufs/ufs/dinode.h>
46
47#include <err.h>
48#include <errno.h>
49#include <fcntl.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <time.h>
54#include <unistd.h>
55
56void usage __P((int));
57int fsirand __P((char *));
58
59extern char *__progname;
60
61int printonly = 0, force = 0, ignorelabel = 0;
62
63int
64main(argc, argv)
65	int	argc;
66	char	*argv[];
67{
68	int n, ex = 0;
69	struct rlimit rl;
70
71	while ((n = getopt(argc, argv, "bfp")) != -1) {
72		switch (n) {
73		case 'b':
74			ignorelabel++;
75			break;
76		case 'p':
77			printonly++;
78			break;
79		case 'f':
80			force++;
81			break;
82		default:
83			usage(1);
84		}
85	}
86	if (argc - optind < 1)
87		usage(1);
88
89	if (srandomdev() < 0)
90		srandom(time(NULL) ^ getpid());
91
92	/* Increase our data size to the max */
93	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
94		rl.rlim_cur = rl.rlim_max;
95		if (setrlimit(RLIMIT_DATA, &rl) < 0)
96			warn("Can't get resource limit to max data size");
97	} else
98		warn("Can't get resource limit for data size");
99
100	for (n = optind; n < argc; n++) {
101		if (argc - optind != 1)
102			(void)puts(argv[n]);
103		ex += fsirand(argv[n]);
104		if (n < argc - 1)
105			putchar('\n');
106	}
107
108	exit(ex);
109}
110
111int
112fsirand(device)
113	char *device;
114{
115	static struct dinode *inodebuf;
116	static size_t oldibufsize;
117	size_t ibufsize;
118	struct fs *sblock;
119	ino_t inumber, maxino;
120	daddr_t dblk;
121	char sbuf[SBSIZE], sbuftmp[SBSIZE];
122	int devfd, n, cg;
123	u_int32_t bsize = DEV_BSIZE;
124	struct disklabel label;
125
126	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
127		warn("Can't open %s", device);
128		return (1);
129	}
130
131	/* Get block size (usually 512) from disklabel if possible */
132	if (!ignorelabel) {
133		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
134			warn("Can't read disklabel, using sector size of %d",
135			    bsize);
136		else
137			bsize = label.d_secsize;
138	}
139
140	/* Read in master superblock */
141	(void)memset(&sbuf, 0, sizeof(sbuf));
142	sblock = (struct fs *)&sbuf;
143	if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
144		warn("Can't seek to superblock (%qd) on %s", SBOFF, device);
145		return (1);
146	}
147	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
148		warnx("Can't read superblock on %s: %s", device,
149		    (n < SBSIZE) ? "short read" : strerror(errno));
150		return (1);
151	}
152	maxino = sblock->fs_ncg * sblock->fs_ipg;
153
154	/* Simple sanity checks on the superblock */
155	if (sblock->fs_magic != FS_MAGIC) {
156		warnx("Bad magic number in superblock");
157		return (1);
158	}
159	if (sblock->fs_sbsize > SBSIZE) {
160		warnx("Superblock size is preposterous");
161		return (1);
162	}
163	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
164		warnx("Filesystem format is too old, sorry");
165		return (1);
166	}
167	if (!force && !printonly && sblock->fs_clean != 1) {
168		warnx("Filesystem is not clean, fsck %s first.", device);
169		return (1);
170	}
171
172	/* Make sure backup superblocks are sane. */
173	sblock = (struct fs *)&sbuftmp;
174	for (cg = 0; cg < sblock->fs_ncg; cg++) {
175		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
176		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
177			warn("Can't seek to %qd", (off_t)dblk * bsize);
178			return (1);
179		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
180			warn("Can't read backup superblock %d on %s: %s",
181			    cg + 1, device, (n < SBSIZE) ? "short write"
182			    : strerror(errno));
183			return (1);
184		}
185		if (sblock->fs_magic != FS_MAGIC) {
186			warnx("Bad magic number in backup superblock %d on %s",
187			    cg + 1, device);
188			return (1);
189		}
190		if (sblock->fs_sbsize > SBSIZE) {
191			warnx("Size of backup superblock %d on %s is preposterous",
192			    cg + 1, device);
193			return (1);
194		}
195	}
196	sblock = (struct fs *)&sbuf;
197
198	/* XXX - should really cap buffer at 512kb or so */
199	ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
200	if (oldibufsize < ibufsize) {
201		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
202			errx(1, "Can't allocate memory for inode buffer");
203		oldibufsize = ibufsize;
204	}
205
206	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
207		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
208			(void)printf("%s was randomized on %s", device,
209			    ctime((const time_t *)&(sblock->fs_id[0])));
210		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
211			    sblock->fs_id[1]);
212	}
213
214	/* Randomize fs_id unless old 4.2BSD filesystem */
215	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
216		/* Randomize fs_id and write out new sblock and backups */
217		sblock->fs_id[0] = (u_int32_t)time(NULL);
218		sblock->fs_id[1] = random();
219
220		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
221			warn("Can't seek to superblock (%qd) on %s", SBOFF,
222			    device);
223			return (1);
224		}
225		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
226			warn("Can't read superblock on %s: %s", device,
227			    (n < SBSIZE) ? "short write" : strerror(errno));
228			return (1);
229		}
230	}
231
232	/* For each cylinder group, randomize inodes and update backup sblock */
233	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
234		/* Update superblock if appropriate */
235		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
236			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
237			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
238				warn("Can't seek to %qd", (off_t)dblk * bsize);
239				return (1);
240			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
241				warn("Can't read backup superblock %d on %s: %s",
242				    cg + 1, device, (n < SBSIZE) ? "short write"
243				    : strerror(errno));
244				return (1);
245			}
246		}
247
248		/* Read in inodes, then print or randomize generation nums */
249		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
250		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
251			warn("Can't seek to %qd", (off_t)dblk * bsize);
252			return (1);
253		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
254			warnx("Can't read inodes: %s",
255			     (n < ibufsize) ? "short read" : strerror(errno));
256			return (1);
257		}
258
259		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
260			if (inumber >= ROOTINO) {
261				if (printonly)
262					(void)printf("ino %d gen %x\n", inumber,
263						     inodebuf[n].di_gen);
264				else
265					inodebuf[n].di_gen = random();
266			}
267		}
268
269		/* Write out modified inodes */
270		if (!printonly) {
271			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
272				warn("Can't seek to %qd",
273				    (off_t)dblk * bsize);
274				return (1);
275			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
276				 ibufsize) {
277				warnx("Can't write inodes: %s",
278				     (n != ibufsize) ? "short write" :
279				     strerror(errno));
280				return (1);
281			}
282		}
283	}
284	(void)close(devfd);
285
286	return(0);
287}
288
289void
290usage(ex)
291	int ex;
292{
293	(void)fprintf(stderr, "Usage: %s [ -b ] [ -f ] [ -p ] special [special ...]\n",
294		      __progname);
295	exit(ex);
296}
297