fsirand.c revision 24303
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
59int printonly = 0, force = 0, ignorelabel = 0;
60
61int
62main(argc, argv)
63	int	argc;
64	char	*argv[];
65{
66	int n, ex = 0;
67	struct rlimit rl;
68
69	while ((n = getopt(argc, argv, "bfp")) != -1) {
70		switch (n) {
71		case 'b':
72			ignorelabel++;
73			break;
74		case 'p':
75			printonly++;
76			break;
77		case 'f':
78			force++;
79			break;
80		default:
81			usage(1);
82		}
83	}
84	if (argc - optind < 1)
85		usage(1);
86
87	if (srandomdev() < 0)
88		srandom(time(NULL) ^ getpid());
89
90	/* Increase our data size to the max */
91	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
92		rl.rlim_cur = rl.rlim_max;
93		if (setrlimit(RLIMIT_DATA, &rl) < 0)
94			warn("Can't get resource limit to max data size");
95	} else
96		warn("Can't get resource limit for data size");
97
98	for (n = optind; n < argc; n++) {
99		if (argc - optind != 1)
100			(void)puts(argv[n]);
101		ex += fsirand(argv[n]);
102		if (n < argc - 1)
103			putchar('\n');
104	}
105
106	exit(ex);
107}
108
109int
110fsirand(device)
111	char *device;
112{
113	static struct dinode *inodebuf;
114	static size_t oldibufsize;
115	size_t ibufsize;
116	struct fs *sblock;
117	ino_t inumber, maxino;
118	daddr_t dblk;
119	char sbuf[SBSIZE], sbuftmp[SBSIZE];
120	int devfd, n, cg;
121	u_int32_t bsize = DEV_BSIZE;
122	struct disklabel label;
123
124	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
125		warn("Can't open %s", device);
126		return (1);
127	}
128
129	/* Get block size (usually 512) from disklabel if possible */
130	if (!ignorelabel) {
131		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
132			warn("Can't read disklabel, using sector size of %d",
133			    bsize);
134		else
135			bsize = label.d_secsize;
136	}
137
138	/* Read in master superblock */
139	(void)memset(&sbuf, 0, sizeof(sbuf));
140	sblock = (struct fs *)&sbuf;
141	if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
142		warn("Can't seek to superblock (%qd) on %s", SBOFF, device);
143		return (1);
144	}
145	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
146		warnx("Can't read superblock on %s: %s", device,
147		    (n < SBSIZE) ? "short read" : strerror(errno));
148		return (1);
149	}
150	maxino = sblock->fs_ncg * sblock->fs_ipg;
151
152	/* Simple sanity checks on the superblock */
153	if (sblock->fs_magic != FS_MAGIC) {
154		warnx("Bad magic number in superblock");
155		return (1);
156	}
157	if (sblock->fs_sbsize > SBSIZE) {
158		warnx("Superblock size is preposterous");
159		return (1);
160	}
161	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
162		warnx("Filesystem format is too old, sorry");
163		return (1);
164	}
165	if (!force && !printonly && sblock->fs_clean != 1) {
166		warnx("Filesystem is not clean, fsck %s first.", device);
167		return (1);
168	}
169
170	/* Make sure backup superblocks are sane. */
171	sblock = (struct fs *)&sbuftmp;
172	for (cg = 0; cg < sblock->fs_ncg; cg++) {
173		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
174		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
175			warn("Can't seek to %qd", (off_t)dblk * bsize);
176			return (1);
177		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
178			warn("Can't read backup superblock %d on %s: %s",
179			    cg + 1, device, (n < SBSIZE) ? "short write"
180			    : strerror(errno));
181			return (1);
182		}
183		if (sblock->fs_magic != FS_MAGIC) {
184			warnx("Bad magic number in backup superblock %d on %s",
185			    cg + 1, device);
186			return (1);
187		}
188		if (sblock->fs_sbsize > SBSIZE) {
189			warnx("Size of backup superblock %d on %s is preposterous",
190			    cg + 1, device);
191			return (1);
192		}
193	}
194	sblock = (struct fs *)&sbuf;
195
196	/* XXX - should really cap buffer at 512kb or so */
197	ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
198	if (oldibufsize < ibufsize) {
199		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
200			errx(1, "Can't allocate memory for inode buffer");
201		oldibufsize = ibufsize;
202	}
203
204	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
205		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
206			(void)printf("%s was randomized on %s", device,
207			    ctime((const time_t *)&(sblock->fs_id[0])));
208		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
209			    sblock->fs_id[1]);
210	}
211
212	/* Randomize fs_id unless old 4.2BSD filesystem */
213	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
214		/* Randomize fs_id and write out new sblock and backups */
215		sblock->fs_id[0] = (u_int32_t)time(NULL);
216		sblock->fs_id[1] = random();
217
218		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
219			warn("Can't seek to superblock (%qd) on %s", SBOFF,
220			    device);
221			return (1);
222		}
223		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
224			warn("Can't read superblock on %s: %s", device,
225			    (n < SBSIZE) ? "short write" : strerror(errno));
226			return (1);
227		}
228	}
229
230	/* For each cylinder group, randomize inodes and update backup sblock */
231	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
232		/* Update superblock if appropriate */
233		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
234			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
235			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
236				warn("Can't seek to %qd", (off_t)dblk * bsize);
237				return (1);
238			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
239				warn("Can't read backup superblock %d on %s: %s",
240				    cg + 1, device, (n < SBSIZE) ? "short write"
241				    : strerror(errno));
242				return (1);
243			}
244		}
245
246		/* Read in inodes, then print or randomize generation nums */
247		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
248		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
249			warn("Can't seek to %qd", (off_t)dblk * bsize);
250			return (1);
251		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
252			warnx("Can't read inodes: %s",
253			     (n < ibufsize) ? "short read" : strerror(errno));
254			return (1);
255		}
256
257		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
258			if (inumber >= ROOTINO) {
259				if (printonly)
260					(void)printf("ino %d gen %x\n", inumber,
261						     inodebuf[n].di_gen);
262				else
263					inodebuf[n].di_gen = random();
264			}
265		}
266
267		/* Write out modified inodes */
268		if (!printonly) {
269			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
270				warn("Can't seek to %qd",
271				    (off_t)dblk * bsize);
272				return (1);
273			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
274				 ibufsize) {
275				warnx("Can't write inodes: %s",
276				     (n != ibufsize) ? "short write" :
277				     strerror(errno));
278				return (1);
279			}
280		}
281	}
282	(void)close(devfd);
283
284	return(0);
285}
286
287void
288usage(ex)
289	int ex;
290{
291	(void)fprintf(stderr,
292"Usage: fsirand [ -b ] [ -f ] [ -p ] special [special ...]\n");
293	exit(ex);
294}
295