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