fsirand.c revision 24303
124149Sguido/*	$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $	*/
224149Sguido
324149Sguido/*
424149Sguido * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
524149Sguido * All rights reserved.
624149Sguido *
724149Sguido * Redistribution and use in source and binary forms, with or without
824149Sguido * modification, are permitted provided that the following conditions
924149Sguido * are met:
1024149Sguido * 1. Redistributions of source code must retain the above copyright
1124149Sguido *    notice, this list of conditions and the following disclaimer.
1224149Sguido * 2. Redistributions in binary form must reproduce the above copyright
1324149Sguido *    notice, this list of conditions and the following disclaimer in the
1424149Sguido *    documentation and/or other materials provided with the distribution.
1524149Sguido * 3. All advertising materials mentioning features or use of this software
1624149Sguido *    must display the following acknowledgement:
1724149Sguido *	This product includes software developed by Todd C. Miller.
1824149Sguido * 4. The name of the author may not be used to endorse or promote products
1924149Sguido *    derived from this software without specific prior written permission.
2024149Sguido *
2124149Sguido * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2224149Sguido * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2324149Sguido * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2424149Sguido * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2524149Sguido * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2624149Sguido * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2724149Sguido * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2824149Sguido * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2924149Sguido * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3024149Sguido * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3124149Sguido */
3224149Sguido
3324149Sguido#ifndef lint
3424149Sguidostatic char rcsid[] = "$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $";
3524149Sguido#endif /* not lint */
3624149Sguido
3724149Sguido#include <sys/types.h>
3824149Sguido#include <sys/disklabel.h>
3924149Sguido#include <sys/ioctl.h>
4024149Sguido#include <sys/param.h>
4124149Sguido#include <sys/time.h>
4224149Sguido#include <sys/resource.h>
4324149Sguido
4424149Sguido#include <ufs/ffs/fs.h>
4524149Sguido#include <ufs/ufs/dinode.h>
4624149Sguido
4724149Sguido#include <err.h>
4824149Sguido#include <errno.h>
4924149Sguido#include <fcntl.h>
5024149Sguido#include <stdio.h>
5124149Sguido#include <stdlib.h>
5224149Sguido#include <string.h>
5324216Sache#include <time.h>
5424149Sguido#include <unistd.h>
5524149Sguido
5624149Sguidovoid usage __P((int));
5724149Sguidoint fsirand __P((char *));
5824149Sguido
5924149Sguidoint printonly = 0, force = 0, ignorelabel = 0;
6024149Sguido
6124149Sguidoint
6224149Sguidomain(argc, argv)
6324149Sguido	int	argc;
6424149Sguido	char	*argv[];
6524149Sguido{
6624149Sguido	int n, ex = 0;
6724149Sguido	struct rlimit rl;
6824149Sguido
6924149Sguido	while ((n = getopt(argc, argv, "bfp")) != -1) {
7024149Sguido		switch (n) {
7124149Sguido		case 'b':
7224149Sguido			ignorelabel++;
7324149Sguido			break;
7424149Sguido		case 'p':
7524149Sguido			printonly++;
7624149Sguido			break;
7724149Sguido		case 'f':
7824149Sguido			force++;
7924149Sguido			break;
8024149Sguido		default:
8124149Sguido			usage(1);
8224149Sguido		}
8324149Sguido	}
8424149Sguido	if (argc - optind < 1)
8524149Sguido		usage(1);
8624149Sguido
8724216Sache	if (srandomdev() < 0)
8824216Sache		srandom(time(NULL) ^ getpid());
8924216Sache
9024149Sguido	/* Increase our data size to the max */
9124149Sguido	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
9224149Sguido		rl.rlim_cur = rl.rlim_max;
9324149Sguido		if (setrlimit(RLIMIT_DATA, &rl) < 0)
9424149Sguido			warn("Can't get resource limit to max data size");
9524149Sguido	} else
9624149Sguido		warn("Can't get resource limit for data size");
9724149Sguido
9824149Sguido	for (n = optind; n < argc; n++) {
9924149Sguido		if (argc - optind != 1)
10024149Sguido			(void)puts(argv[n]);
10124149Sguido		ex += fsirand(argv[n]);
10224149Sguido		if (n < argc - 1)
10324149Sguido			putchar('\n');
10424149Sguido	}
10524149Sguido
10624149Sguido	exit(ex);
10724149Sguido}
10824149Sguido
10924149Sguidoint
11024149Sguidofsirand(device)
11124149Sguido	char *device;
11224149Sguido{
11324149Sguido	static struct dinode *inodebuf;
11424149Sguido	static size_t oldibufsize;
11524149Sguido	size_t ibufsize;
11624149Sguido	struct fs *sblock;
11724149Sguido	ino_t inumber, maxino;
11824149Sguido	daddr_t dblk;
11924149Sguido	char sbuf[SBSIZE], sbuftmp[SBSIZE];
12024149Sguido	int devfd, n, cg;
12124149Sguido	u_int32_t bsize = DEV_BSIZE;
12224149Sguido	struct disklabel label;
12324149Sguido
12424149Sguido	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
12524149Sguido		warn("Can't open %s", device);
12624149Sguido		return (1);
12724149Sguido	}
12824149Sguido
12924149Sguido	/* Get block size (usually 512) from disklabel if possible */
13024149Sguido	if (!ignorelabel) {
13124149Sguido		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
13224149Sguido			warn("Can't read disklabel, using sector size of %d",
13324149Sguido			    bsize);
13424149Sguido		else
13524149Sguido			bsize = label.d_secsize;
13624149Sguido	}
13724149Sguido
13824149Sguido	/* Read in master superblock */
13924149Sguido	(void)memset(&sbuf, 0, sizeof(sbuf));
14024149Sguido	sblock = (struct fs *)&sbuf;
14124149Sguido	if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
14224149Sguido		warn("Can't seek to superblock (%qd) on %s", SBOFF, device);
14324149Sguido		return (1);
14424149Sguido	}
14524149Sguido	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
14624149Sguido		warnx("Can't read superblock on %s: %s", device,
14724149Sguido		    (n < SBSIZE) ? "short read" : strerror(errno));
14824149Sguido		return (1);
14924149Sguido	}
15024149Sguido	maxino = sblock->fs_ncg * sblock->fs_ipg;
15124149Sguido
15224149Sguido	/* Simple sanity checks on the superblock */
15324149Sguido	if (sblock->fs_magic != FS_MAGIC) {
15424149Sguido		warnx("Bad magic number in superblock");
15524149Sguido		return (1);
15624149Sguido	}
15724149Sguido	if (sblock->fs_sbsize > SBSIZE) {
15824149Sguido		warnx("Superblock size is preposterous");
15924149Sguido		return (1);
16024149Sguido	}
16124149Sguido	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
16224149Sguido		warnx("Filesystem format is too old, sorry");
16324149Sguido		return (1);
16424149Sguido	}
16524149Sguido	if (!force && !printonly && sblock->fs_clean != 1) {
16624149Sguido		warnx("Filesystem is not clean, fsck %s first.", device);
16724149Sguido		return (1);
16824149Sguido	}
16924149Sguido
17024149Sguido	/* Make sure backup superblocks are sane. */
17124149Sguido	sblock = (struct fs *)&sbuftmp;
17224149Sguido	for (cg = 0; cg < sblock->fs_ncg; cg++) {
17324149Sguido		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
17424149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
17524149Sguido			warn("Can't seek to %qd", (off_t)dblk * bsize);
17624149Sguido			return (1);
17724149Sguido		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
17824149Sguido			warn("Can't read backup superblock %d on %s: %s",
17924149Sguido			    cg + 1, device, (n < SBSIZE) ? "short write"
18024149Sguido			    : strerror(errno));
18124149Sguido			return (1);
18224149Sguido		}
18324149Sguido		if (sblock->fs_magic != FS_MAGIC) {
18424149Sguido			warnx("Bad magic number in backup superblock %d on %s",
18524149Sguido			    cg + 1, device);
18624149Sguido			return (1);
18724149Sguido		}
18824149Sguido		if (sblock->fs_sbsize > SBSIZE) {
18924149Sguido			warnx("Size of backup superblock %d on %s is preposterous",
19024149Sguido			    cg + 1, device);
19124149Sguido			return (1);
19224149Sguido		}
19324149Sguido	}
19424149Sguido	sblock = (struct fs *)&sbuf;
19524149Sguido
19624149Sguido	/* XXX - should really cap buffer at 512kb or so */
19724149Sguido	ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
19824149Sguido	if (oldibufsize < ibufsize) {
19924149Sguido		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
20024149Sguido			errx(1, "Can't allocate memory for inode buffer");
20124149Sguido		oldibufsize = ibufsize;
20224149Sguido	}
20324149Sguido
20424149Sguido	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
20524149Sguido		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
20624149Sguido			(void)printf("%s was randomized on %s", device,
20724149Sguido			    ctime((const time_t *)&(sblock->fs_id[0])));
20824149Sguido		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
20924149Sguido			    sblock->fs_id[1]);
21024149Sguido	}
21124149Sguido
21224149Sguido	/* Randomize fs_id unless old 4.2BSD filesystem */
21324149Sguido	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
21424149Sguido		/* Randomize fs_id and write out new sblock and backups */
21524149Sguido		sblock->fs_id[0] = (u_int32_t)time(NULL);
21624149Sguido		sblock->fs_id[1] = random();
21724149Sguido
21824149Sguido		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
21924149Sguido			warn("Can't seek to superblock (%qd) on %s", SBOFF,
22024149Sguido			    device);
22124149Sguido			return (1);
22224149Sguido		}
22324149Sguido		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
22424149Sguido			warn("Can't read superblock on %s: %s", device,
22524149Sguido			    (n < SBSIZE) ? "short write" : strerror(errno));
22624149Sguido			return (1);
22724149Sguido		}
22824149Sguido	}
22924149Sguido
23024149Sguido	/* For each cylinder group, randomize inodes and update backup sblock */
23124149Sguido	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
23224149Sguido		/* Update superblock if appropriate */
23324149Sguido		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
23424149Sguido			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
23524149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
23624149Sguido				warn("Can't seek to %qd", (off_t)dblk * bsize);
23724149Sguido				return (1);
23824149Sguido			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
23924149Sguido				warn("Can't read backup superblock %d on %s: %s",
24024149Sguido				    cg + 1, device, (n < SBSIZE) ? "short write"
24124149Sguido				    : strerror(errno));
24224149Sguido				return (1);
24324149Sguido			}
24424149Sguido		}
24524149Sguido
24624149Sguido		/* Read in inodes, then print or randomize generation nums */
24724149Sguido		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
24824149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
24924149Sguido			warn("Can't seek to %qd", (off_t)dblk * bsize);
25024149Sguido			return (1);
25124149Sguido		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
25224149Sguido			warnx("Can't read inodes: %s",
25324149Sguido			     (n < ibufsize) ? "short read" : strerror(errno));
25424149Sguido			return (1);
25524149Sguido		}
25624149Sguido
25724149Sguido		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
25824149Sguido			if (inumber >= ROOTINO) {
25924149Sguido				if (printonly)
26024149Sguido					(void)printf("ino %d gen %x\n", inumber,
26124149Sguido						     inodebuf[n].di_gen);
26224149Sguido				else
26324149Sguido					inodebuf[n].di_gen = random();
26424149Sguido			}
26524149Sguido		}
26624149Sguido
26724149Sguido		/* Write out modified inodes */
26824149Sguido		if (!printonly) {
26924149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
27024149Sguido				warn("Can't seek to %qd",
27124149Sguido				    (off_t)dblk * bsize);
27224149Sguido				return (1);
27324149Sguido			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
27424149Sguido				 ibufsize) {
27524149Sguido				warnx("Can't write inodes: %s",
27624149Sguido				     (n != ibufsize) ? "short write" :
27724149Sguido				     strerror(errno));
27824149Sguido				return (1);
27924149Sguido			}
28024149Sguido		}
28124149Sguido	}
28224149Sguido	(void)close(devfd);
28324149Sguido
28424149Sguido	return(0);
28524149Sguido}
28624149Sguido
28724149Sguidovoid
28824149Sguidousage(ex)
28924149Sguido	int ex;
29024149Sguido{
29124303Sguido	(void)fprintf(stderr,
29224303Sguido"Usage: fsirand [ -b ] [ -f ] [ -p ] special [special ...]\n");
29324149Sguido	exit(ex);
29424149Sguido}
295