fsirand.c revision 50476
1139825Simp/*	$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $	*/
21541Srgrimes
31541Srgrimes/*
41541Srgrimes * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
51541Srgrimes * All rights reserved.
61541Srgrimes *
71541Srgrimes * Redistribution and use in source and binary forms, with or without
81541Srgrimes * modification, are permitted provided that the following conditions
91541Srgrimes * are met:
101541Srgrimes * 1. Redistributions of source code must retain the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer.
121541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
131541Srgrimes *    notice, this list of conditions and the following disclaimer in the
141541Srgrimes *    documentation and/or other materials provided with the distribution.
151541Srgrimes * 3. All advertising materials mentioning features or use of this software
161541Srgrimes *    must display the following acknowledgement:
171541Srgrimes *	This product includes software developed by Todd C. Miller.
181541Srgrimes * 4. The name of the author may not be used to endorse or promote products
191541Srgrimes *    derived from this software without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
221541Srgrimes * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
231541Srgrimes * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
241541Srgrimes * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
251541Srgrimes * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
261541Srgrimes * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
271541Srgrimes * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
281541Srgrimes * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
291541Srgrimes * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
301541Srgrimes * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311541Srgrimes */
321541Srgrimes
331541Srgrimes#ifndef lint
3422521Sdysonstatic const char rcsid[] =
3550477Speter  "$FreeBSD: head/sbin/fsirand/fsirand.c 50476 1999-08-28 00:22:10Z peter $";
361541Srgrimes#endif /* not lint */
371541Srgrimes
382177Spaul#include <sys/types.h>
395247Sbde#include <sys/disklabel.h>
402177Spaul#include <sys/param.h>
4131557Sjkh#include <sys/time.h>
4244512Sbde#include <sys/resource.h>
431541Srgrimes
441541Srgrimes#include <ufs/ffs/fs.h>
451541Srgrimes#include <ufs/ufs/dinode.h>
4624477Sbde
4724477Sbde#include <err.h>
4824477Sbde#include <errno.h>
4924477Sbde#include <fcntl.h>
5024477Sbde#include <stdio.h>
5122521Sdyson#include <stdlib.h>
5222521Sdyson#include <string.h>
5322521Sdyson#include <unistd.h>
5422521Sdyson
5522521Sdysonstatic void usage __P((void));
5622521Sdysonint fsirand __P((char *));
5722521Sdyson
58163194Skibint printonly = 0, force = 0, ignorelabel = 0;
59163194Skib
60163194Skibint
61163194Skibmain(argc, argv)
62163194Skib	int	argc;
63163194Skib	char	*argv[];
64163194Skib{
651541Srgrimes	int n, ex = 0;
6622521Sdyson	struct rlimit rl;
67104702Smckusick
6822521Sdyson	while ((n = getopt(argc, argv, "bfp")) != -1) {
6998542Smckusick		switch (n) {
7022521Sdyson		case 'b':
71104052Sphk			ignorelabel++;
7222521Sdyson			break;
7334266Sjulian		case 'p':
741541Srgrimes			printonly++;
7596873Siedowse			break;
7622521Sdyson		case 'f':
771541Srgrimes			force++;
781541Srgrimes			break;
791541Srgrimes		default:
8022521Sdyson			usage();
8122521Sdyson		}
8222521Sdyson	}
8322521Sdyson	if (argc - optind < 1)
8479561Siedowse		usage();
85107915Smckusick
86107915Smckusick	srandomdev();
87107915Smckusick
88107915Smckusick	/* Increase our data size to the max */
89102991Sphk	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
901541Srgrimes		rl.rlim_cur = rl.rlim_max;
91102991Sphk		if (setrlimit(RLIMIT_DATA, &rl) < 0)
92102991Sphk			warn("can't get resource limit to max data size");
93102991Sphk	} else
94102991Sphk		warn("can't get resource limit for data size");
95102991Sphk
96189737Skib	for (n = optind; n < argc; n++) {
97102991Sphk		if (argc - optind != 1)
98102991Sphk			(void)puts(argv[n]);
9998542Smckusick		ex += fsirand(argv[n]);
1001541Srgrimes		if (n < argc - 1)
10198542Smckusick			putchar('\n');
10298542Smckusick	}
10398542Smckusick
10498542Smckusick	exit(ex);
105252435Spfg}
10698542Smckusick
10798542Smckusickint
10898542Smckusickfsirand(device)
10998542Smckusick	char *device;
11098542Smckusick{
11198542Smckusick	static struct dinode *inodebuf;
11298542Smckusick	static size_t oldibufsize;
11398542Smckusick	size_t ibufsize;
11498542Smckusick	struct fs *sblock;
1151541Srgrimes	ino_t inumber, maxino;
11698542Smckusick	daddr_t dblk;
11798542Smckusick	char sbuf[SBSIZE], sbuftmp[SBSIZE];
11898542Smckusick	int devfd, n, cg;
1191541Srgrimes	u_int32_t bsize = DEV_BSIZE;
1201541Srgrimes	struct disklabel label;
12122521Sdyson
12222521Sdyson	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
123207141Sjeff		warn("can't open %s", device);
12476357Smckusick		return (1);
125163194Skib	}
126163194Skib
127189737Skib	/* Get block size (usually 512) from disklabel if possible */
128189737Skib	if (!ignorelabel) {
1291541Srgrimes		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
130222958Sjeff			warn("can't read disklabel, using sector size of %d",
131222958Sjeff			    bsize);
132262779Spfg		else
133262779Spfg			bsize = label.d_secsize;
134262779Spfg	}
135262779Spfg
136262779Spfg	/* Read in master superblock */
137262779Spfg	(void)memset(&sbuf, 0, sizeof(sbuf));
13898542Smckusick	sblock = (struct fs *)&sbuf;
13955206Speter	if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
1401541Srgrimes		warn("can't seek to superblock (%qd) on %s", SBOFF, device);
14198542Smckusick		return (1);
14298542Smckusick	}
14398542Smckusick	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
14498542Smckusick		warnx("can't read superblock on %s: %s", device,
14598542Smckusick		    (n < SBSIZE) ? "short read" : strerror(errno));
14698542Smckusick		return (1);
147132775Skan	}
148132775Skan	maxino = sblock->fs_ncg * sblock->fs_ipg;
149132775Skan
150132775Skan	/* Simple sanity checks on the superblock */
151132775Skan	if (sblock->fs_magic != FS_MAGIC) {
152132775Skan		warnx("bad magic number in superblock");
15398542Smckusick		return (1);
15498542Smckusick	}
15598542Smckusick	if (sblock->fs_sbsize > SBSIZE) {
15698542Smckusick		warnx("superblock size is preposterous");
157262779Spfg		return (1);
15898542Smckusick	}
15998542Smckusick	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
1601541Srgrimes		warnx("filesystem format is too old, sorry");
1611541Srgrimes		return (1);
1621541Srgrimes	}
1631541Srgrimes	if (!force && !printonly && sblock->fs_clean != 1) {
16498542Smckusick		warnx("filesystem is not clean, fsck %s first", device);
1651541Srgrimes		return (1);
1661541Srgrimes	}
1671541Srgrimes
1681541Srgrimes	/* Make sure backup superblocks are sane. */
169262779Spfg	sblock = (struct fs *)&sbuftmp;
170262779Spfg	for (cg = 0; cg < sblock->fs_ncg; cg++) {
1711541Srgrimes		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
17234266Sjulian		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
173262779Spfg			warn("can't seek to %qd", (off_t)dblk * bsize);
174262779Spfg			return (1);
175262779Spfg		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
176262779Spfg			warn("can't read backup superblock %d on %s: %s",
17734266Sjulian			    cg + 1, device, (n < SBSIZE) ? "short write"
1781541Srgrimes			    : strerror(errno));
1791541Srgrimes			return (1);
18022521Sdyson		}
18122521Sdyson		if (sblock->fs_magic != FS_MAGIC) {
182241011Smdf			warnx("bad magic number in backup superblock %d on %s",
183253163Spfg			    cg + 1, device);
1841541Srgrimes			return (1);
18555206Speter		}
1862177Spaul		if (sblock->fs_sbsize > SBSIZE) {
1875247Sbde			warnx("size of backup superblock %d on %s is preposterous",
188			    cg + 1, device);
189			return (1);
190		}
191	}
192	sblock = (struct fs *)&sbuf;
193
194	/* XXX - should really cap buffer at 512kb or so */
195	ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
196	if (oldibufsize < ibufsize) {
197		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
198			errx(1, "can't allocate memory for inode buffer");
199		oldibufsize = ibufsize;
200	}
201
202	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
203		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
204			(void)printf("%s was randomized on %s", device,
205			    ctime((const time_t *)&(sblock->fs_id[0])));
206		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
207			    sblock->fs_id[1]);
208	}
209
210	/* Randomize fs_id unless old 4.2BSD filesystem */
211	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
212		/* Randomize fs_id and write out new sblock and backups */
213		sblock->fs_id[0] = (u_int32_t)time(NULL);
214		sblock->fs_id[1] = random();
215
216		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
217			warn("can't seek to superblock (%qd) on %s", SBOFF,
218			    device);
219			return (1);
220		}
221		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
222			warn("can't read superblock on %s: %s", device,
223			    (n < SBSIZE) ? "short write" : strerror(errno));
224			return (1);
225		}
226	}
227
228	/* For each cylinder group, randomize inodes and update backup sblock */
229	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
230		/* Update superblock if appropriate */
231		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
232			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
233			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
234				warn("can't seek to %qd", (off_t)dblk * bsize);
235				return (1);
236			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
237				warn("can't read backup superblock %d on %s: %s",
238				    cg + 1, device, (n < SBSIZE) ? "short write"
239				    : strerror(errno));
240				return (1);
241			}
242		}
243
244		/* Read in inodes, then print or randomize generation nums */
245		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
246		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
247			warn("can't seek to %qd", (off_t)dblk * bsize);
248			return (1);
249		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
250			warnx("can't read inodes: %s",
251			     (n < ibufsize) ? "short read" : strerror(errno));
252			return (1);
253		}
254
255		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
256			if (inumber >= ROOTINO) {
257				if (printonly)
258					(void)printf("ino %d gen %x\n", inumber,
259						     inodebuf[n].di_gen);
260				else
261					inodebuf[n].di_gen = random();
262			}
263		}
264
265		/* Write out modified inodes */
266		if (!printonly) {
267			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
268				warn("can't seek to %qd",
269				    (off_t)dblk * bsize);
270				return (1);
271			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
272				 ibufsize) {
273				warnx("can't write inodes: %s",
274				     (n != ibufsize) ? "short write" :
275				     strerror(errno));
276				return (1);
277			}
278		}
279	}
280	(void)close(devfd);
281
282	return(0);
283}
284
285static void
286usage()
287{
288	(void)fprintf(stderr,
289		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
290	exit(1);
291}
292