fsirand.c revision 102231
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 102231 2002-08-21 18:11:48Z trhodes $";
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/ufs/dinode.h>
44#include <ufs/ffs/fs.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(void) __dead2;
55int fsirand(char *);
56
57/*
58 * Possible superblock locations ordered from most to least likely.
59 */
60static int sblock_try[] = SBLOCKSEARCH;
61
62int printonly = 0, force = 0, ignorelabel = 0;
63
64int
65main(int argc, char *argv[])
66{
67	int n, ex = 0;
68	struct rlimit rl;
69
70	while ((n = getopt(argc, argv, "bfp")) != -1) {
71		switch (n) {
72		case 'b':
73			ignorelabel++;
74			break;
75		case 'p':
76			printonly++;
77			break;
78		case 'f':
79			force++;
80			break;
81		default:
82			usage();
83		}
84	}
85	if (argc - optind < 1)
86		usage();
87
88	srandomdev();
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(char *device)
111{
112	struct ufs1_dinode *dp1;
113	struct ufs2_dinode *dp2;
114	caddr_t inodebuf;
115	size_t ibufsize;
116	struct fs *sblock;
117	ino_t inumber, maxino;
118	ufs2_daddr_t sblockloc, dblk;
119	char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
120	int i, 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	for (i = 0; sblock_try[i] != -1; i++) {
142		sblockloc = sblock_try[i];
143		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
144			warn("can't seek to superblock (%qd) on %s",
145			    sblockloc, device);
146			return (1);
147		}
148		if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) {
149			warnx("can't read superblock on %s: %s", device,
150			    (n < SBLOCKSIZE) ? "short read" : strerror(errno));
151			return (1);
152		}
153		if ((sblock->fs_magic == FS_UFS1_MAGIC ||
154		     (sblock->fs_magic == FS_UFS2_MAGIC &&
155		      sblock->fs_sblockloc ==
156			  numfrags(sblock, sblock_try[i]))) &&
157		    sblock->fs_bsize <= MAXBSIZE &&
158		    sblock->fs_bsize >= sizeof(struct fs))
159			break;
160	}
161	if (sblock_try[i] == -1) {
162		fprintf(stderr, "Cannot find file system superblock\n");
163		return (1);
164	}
165	maxino = sblock->fs_ncg * sblock->fs_ipg;
166
167	if (sblock->fs_magic == FS_UFS1_MAGIC &&
168	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
169		warnx("file system format is too old, sorry");
170		return (1);
171	}
172	if (!force && !printonly && sblock->fs_clean != 1) {
173		warnx("file system is not clean, fsck %s first", device);
174		return (1);
175	}
176
177	/* Make sure backup superblocks are sane. */
178	sblock = (struct fs *)&sbuftmp;
179	for (cg = 0; cg < sblock->fs_ncg; cg++) {
180		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
181		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
182			warn("can't seek to %qd", (off_t)dblk * bsize);
183			return (1);
184		} else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) {
185			warn("can't read backup superblock %d on %s: %s",
186			    cg + 1, device, (n < SBLOCKSIZE) ? "short write"
187			    : strerror(errno));
188			return (1);
189		}
190		if (sblock->fs_magic != FS_UFS1_MAGIC &&
191		    sblock->fs_magic != FS_UFS2_MAGIC) {
192			warnx("bad magic number in backup superblock %d on %s",
193			    cg + 1, device);
194			return (1);
195		}
196		if (sblock->fs_sbsize > SBLOCKSIZE) {
197			warnx("size of backup superblock %d on %s is preposterous",
198			    cg + 1, device);
199			return (1);
200		}
201	}
202	sblock = (struct fs *)&sbuf;
203
204	/* XXX - should really cap buffer at 512kb or so */
205	if (sblock->fs_magic == FS_UFS1_MAGIC)
206		ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
207	else
208		ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
209	if ((inodebuf = malloc(ibufsize)) == NULL)
210		errx(1, "can't allocate memory for inode buffer");
211
212	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
213		if (sblock->fs_id[0])
214			(void)printf("%s was randomized on %s", device,
215			    ctime((const time_t *)&(sblock->fs_id[0])));
216		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
217			    sblock->fs_id[1]);
218	}
219
220	/* Randomize fs_id unless old 4.2BSD file system */
221	if (!printonly) {
222		/* Randomize fs_id and write out new sblock and backups */
223		sblock->fs_id[0] = (u_int32_t)time(NULL);
224		sblock->fs_id[1] = random();
225
226		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
227			warn("can't seek to superblock (%qd) on %s", sblockloc,
228			    device);
229			return (1);
230		}
231		if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
232		    SBLOCKSIZE) {
233			warn("can't write superblock on %s: %s", device,
234			    (n < SBLOCKSIZE) ? "short write" : strerror(errno));
235			return (1);
236		}
237	}
238
239	/* For each cylinder group, randomize inodes and update backup sblock */
240	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
241		/* Update superblock if appropriate */
242		if (!printonly) {
243			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
244			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
245				warn("can't seek to %qd", (off_t)dblk * bsize);
246				return (1);
247			} else if ((n = write(devfd, (void *)sblock,
248			    SBLOCKSIZE)) != SBLOCKSIZE) {
249			      warn("can't write backup superblock %d on %s: %s",
250				    cg + 1, device, (n < SBLOCKSIZE) ?
251				    "short write" : strerror(errno));
252				return (1);
253			}
254		}
255
256		/* Read in inodes, then print or randomize generation nums */
257		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
258		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
259			warn("can't seek to %qd", (off_t)dblk * bsize);
260			return (1);
261		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
262			warnx("can't read inodes: %s",
263			     (n < ibufsize) ? "short read" : strerror(errno));
264			return (1);
265		}
266
267		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
268			if (sblock->fs_magic == FS_UFS1_MAGIC)
269				dp1 = &((struct ufs1_dinode *)inodebuf)[n];
270			else
271				dp2 = &((struct ufs2_dinode *)inodebuf)[n];
272			if (inumber >= ROOTINO) {
273				if (printonly)
274					(void)printf("ino %d gen %qx\n",
275					    inumber,
276					    sblock->fs_magic == FS_UFS1_MAGIC ?
277					    (quad_t)dp1->di_gen : dp2->di_gen);
278				else
279					(sblock->fs_magic == FS_UFS1_MAGIC ?
280					dp1->di_gen : dp2->di_gen) = random();
281			}
282		}
283
284		/* Write out modified inodes */
285		if (!printonly) {
286			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
287				warn("can't seek to %qd",
288				    (off_t)dblk * bsize);
289				return (1);
290			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
291				 ibufsize) {
292				warnx("can't write inodes: %s",
293				     (n != ibufsize) ? "short write" :
294				     strerror(errno));
295				return (1);
296			}
297		}
298	}
299	(void)close(devfd);
300
301	return(0);
302}
303
304static void
305usage(void)
306{
307	(void)fprintf(stderr,
308		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
309	exit(1);
310}
311