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