fsirand.c revision 37275
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
3337275Scharnier#ifndef lint
3437275Scharnierstatic const char rcsid[] =
3537275Scharnier	"$Id$";
3637275Scharnier#endif /* not lint */
3724149Sguido
3824149Sguido#include <sys/types.h>
3924149Sguido#include <sys/disklabel.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>
5324149Sguido#include <unistd.h>
5424149Sguido
5537275Scharnierstatic void usage __P((void));
5624149Sguidoint fsirand __P((char *));
5724149Sguido
5824149Sguidoint printonly = 0, force = 0, ignorelabel = 0;
5924149Sguido
6024149Sguidoint
6124149Sguidomain(argc, argv)
6224149Sguido	int	argc;
6324149Sguido	char	*argv[];
6424149Sguido{
6524149Sguido	int n, ex = 0;
6624149Sguido	struct rlimit rl;
6724149Sguido
6824149Sguido	while ((n = getopt(argc, argv, "bfp")) != -1) {
6924149Sguido		switch (n) {
7024149Sguido		case 'b':
7124149Sguido			ignorelabel++;
7224149Sguido			break;
7324149Sguido		case 'p':
7424149Sguido			printonly++;
7524149Sguido			break;
7624149Sguido		case 'f':
7724149Sguido			force++;
7824149Sguido			break;
7924149Sguido		default:
8037275Scharnier			usage();
8124149Sguido		}
8224149Sguido	}
8324149Sguido	if (argc - optind < 1)
8437275Scharnier		usage();
8524149Sguido
8626625Sache	srandomdev();
8724216Sache
8824149Sguido	/* Increase our data size to the max */
8924149Sguido	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
9024149Sguido		rl.rlim_cur = rl.rlim_max;
9124149Sguido		if (setrlimit(RLIMIT_DATA, &rl) < 0)
9237275Scharnier			warn("can't get resource limit to max data size");
9324149Sguido	} else
9437275Scharnier		warn("can't get resource limit for data size");
9524149Sguido
9624149Sguido	for (n = optind; n < argc; n++) {
9724149Sguido		if (argc - optind != 1)
9824149Sguido			(void)puts(argv[n]);
9924149Sguido		ex += fsirand(argv[n]);
10024149Sguido		if (n < argc - 1)
10124149Sguido			putchar('\n');
10224149Sguido	}
10324149Sguido
10424149Sguido	exit(ex);
10524149Sguido}
10624149Sguido
10724149Sguidoint
10824149Sguidofsirand(device)
10924149Sguido	char *device;
11024149Sguido{
11124149Sguido	static struct dinode *inodebuf;
11224149Sguido	static size_t oldibufsize;
11324149Sguido	size_t ibufsize;
11424149Sguido	struct fs *sblock;
11524149Sguido	ino_t inumber, maxino;
11624149Sguido	daddr_t dblk;
11724149Sguido	char sbuf[SBSIZE], sbuftmp[SBSIZE];
11824149Sguido	int devfd, n, cg;
11924149Sguido	u_int32_t bsize = DEV_BSIZE;
12024149Sguido	struct disklabel label;
12124149Sguido
12224149Sguido	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
12337275Scharnier		warn("can't open %s", device);
12424149Sguido		return (1);
12524149Sguido	}
12624149Sguido
12724149Sguido	/* Get block size (usually 512) from disklabel if possible */
12824149Sguido	if (!ignorelabel) {
12924149Sguido		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
13037275Scharnier			warn("can't read disklabel, using sector size of %d",
13124149Sguido			    bsize);
13224149Sguido		else
13324149Sguido			bsize = label.d_secsize;
13424149Sguido	}
13524149Sguido
13624149Sguido	/* Read in master superblock */
13724149Sguido	(void)memset(&sbuf, 0, sizeof(sbuf));
13824149Sguido	sblock = (struct fs *)&sbuf;
13924149Sguido	if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
14037275Scharnier		warn("can't seek to superblock (%qd) on %s", SBOFF, device);
14124149Sguido		return (1);
14224149Sguido	}
14324149Sguido	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
14437275Scharnier		warnx("can't read superblock on %s: %s", device,
14524149Sguido		    (n < SBSIZE) ? "short read" : strerror(errno));
14624149Sguido		return (1);
14724149Sguido	}
14824149Sguido	maxino = sblock->fs_ncg * sblock->fs_ipg;
14924149Sguido
15024149Sguido	/* Simple sanity checks on the superblock */
15124149Sguido	if (sblock->fs_magic != FS_MAGIC) {
15237275Scharnier		warnx("bad magic number in superblock");
15324149Sguido		return (1);
15424149Sguido	}
15524149Sguido	if (sblock->fs_sbsize > SBSIZE) {
15637275Scharnier		warnx("superblock size is preposterous");
15724149Sguido		return (1);
15824149Sguido	}
15924149Sguido	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
16037275Scharnier		warnx("filesystem format is too old, sorry");
16124149Sguido		return (1);
16224149Sguido	}
16324149Sguido	if (!force && !printonly && sblock->fs_clean != 1) {
16437275Scharnier		warnx("filesystem is not clean, fsck %s first", device);
16524149Sguido		return (1);
16624149Sguido	}
16724149Sguido
16824149Sguido	/* Make sure backup superblocks are sane. */
16924149Sguido	sblock = (struct fs *)&sbuftmp;
17024149Sguido	for (cg = 0; cg < sblock->fs_ncg; cg++) {
17124149Sguido		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
17224149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
17337275Scharnier			warn("can't seek to %qd", (off_t)dblk * bsize);
17424149Sguido			return (1);
17524149Sguido		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
17637275Scharnier			warn("can't read backup superblock %d on %s: %s",
17724149Sguido			    cg + 1, device, (n < SBSIZE) ? "short write"
17824149Sguido			    : strerror(errno));
17924149Sguido			return (1);
18024149Sguido		}
18124149Sguido		if (sblock->fs_magic != FS_MAGIC) {
18237275Scharnier			warnx("bad magic number in backup superblock %d on %s",
18324149Sguido			    cg + 1, device);
18424149Sguido			return (1);
18524149Sguido		}
18624149Sguido		if (sblock->fs_sbsize > SBSIZE) {
18737275Scharnier			warnx("size of backup superblock %d on %s is preposterous",
18824149Sguido			    cg + 1, device);
18924149Sguido			return (1);
19024149Sguido		}
19124149Sguido	}
19224149Sguido	sblock = (struct fs *)&sbuf;
19324149Sguido
19424149Sguido	/* XXX - should really cap buffer at 512kb or so */
19524149Sguido	ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
19624149Sguido	if (oldibufsize < ibufsize) {
19724149Sguido		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
19837275Scharnier			errx(1, "can't allocate memory for inode buffer");
19924149Sguido		oldibufsize = ibufsize;
20024149Sguido	}
20124149Sguido
20224149Sguido	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
20324149Sguido		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
20424149Sguido			(void)printf("%s was randomized on %s", device,
20524149Sguido			    ctime((const time_t *)&(sblock->fs_id[0])));
20624149Sguido		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
20724149Sguido			    sblock->fs_id[1]);
20824149Sguido	}
20924149Sguido
21024149Sguido	/* Randomize fs_id unless old 4.2BSD filesystem */
21124149Sguido	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
21224149Sguido		/* Randomize fs_id and write out new sblock and backups */
21324149Sguido		sblock->fs_id[0] = (u_int32_t)time(NULL);
21424149Sguido		sblock->fs_id[1] = random();
21524149Sguido
21624149Sguido		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
21737275Scharnier			warn("can't seek to superblock (%qd) on %s", SBOFF,
21824149Sguido			    device);
21924149Sguido			return (1);
22024149Sguido		}
22124149Sguido		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
22237275Scharnier			warn("can't read superblock on %s: %s", device,
22324149Sguido			    (n < SBSIZE) ? "short write" : strerror(errno));
22424149Sguido			return (1);
22524149Sguido		}
22624149Sguido	}
22724149Sguido
22824149Sguido	/* For each cylinder group, randomize inodes and update backup sblock */
22924149Sguido	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
23024149Sguido		/* Update superblock if appropriate */
23124149Sguido		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
23224149Sguido			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
23324149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
23437275Scharnier				warn("can't seek to %qd", (off_t)dblk * bsize);
23524149Sguido				return (1);
23624149Sguido			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
23737275Scharnier				warn("can't read backup superblock %d on %s: %s",
23824149Sguido				    cg + 1, device, (n < SBSIZE) ? "short write"
23924149Sguido				    : strerror(errno));
24024149Sguido				return (1);
24124149Sguido			}
24224149Sguido		}
24324149Sguido
24424149Sguido		/* Read in inodes, then print or randomize generation nums */
24524149Sguido		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
24624149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
24737275Scharnier			warn("can't seek to %qd", (off_t)dblk * bsize);
24824149Sguido			return (1);
24924149Sguido		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
25037275Scharnier			warnx("can't read inodes: %s",
25124149Sguido			     (n < ibufsize) ? "short read" : strerror(errno));
25224149Sguido			return (1);
25324149Sguido		}
25424149Sguido
25524149Sguido		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
25624149Sguido			if (inumber >= ROOTINO) {
25724149Sguido				if (printonly)
25824149Sguido					(void)printf("ino %d gen %x\n", inumber,
25924149Sguido						     inodebuf[n].di_gen);
26024149Sguido				else
26124149Sguido					inodebuf[n].di_gen = random();
26224149Sguido			}
26324149Sguido		}
26424149Sguido
26524149Sguido		/* Write out modified inodes */
26624149Sguido		if (!printonly) {
26724149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
26837275Scharnier				warn("can't seek to %qd",
26924149Sguido				    (off_t)dblk * bsize);
27024149Sguido				return (1);
27124149Sguido			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
27224149Sguido				 ibufsize) {
27337275Scharnier				warnx("can't write inodes: %s",
27424149Sguido				     (n != ibufsize) ? "short write" :
27524149Sguido				     strerror(errno));
27624149Sguido				return (1);
27724149Sguido			}
27824149Sguido		}
27924149Sguido	}
28024149Sguido	(void)close(devfd);
28124149Sguido
28224149Sguido	return(0);
28324149Sguido}
28424149Sguido
28526558Scharnierstatic void
28637275Scharnierusage()
28724149Sguido{
28824303Sguido	(void)fprintf(stderr,
28937275Scharnier		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
29037275Scharnier	exit(1);
29124149Sguido}
292