fsirand.c revision 229917
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[] =
3550476Speter  "$FreeBSD: head/sbin/fsirand/fsirand.c 229917 2012-01-10 02:59:43Z eadler $";
3637275Scharnier#endif /* not lint */
3724149Sguido
3824149Sguido#include <sys/param.h>
3924149Sguido#include <sys/disklabel.h>
4024149Sguido#include <sys/resource.h>
4124149Sguido
4224149Sguido#include <ufs/ufs/dinode.h>
4324149Sguido#include <ufs/ffs/fs.h>
4424149Sguido
4524149Sguido#include <err.h>
4624149Sguido#include <errno.h>
4724149Sguido#include <fcntl.h>
4824149Sguido#include <stdio.h>
4924149Sguido#include <stdint.h>
5024149Sguido#include <stdlib.h>
5124149Sguido#include <string.h>
5224149Sguido#include <time.h>
5324149Sguido#include <unistd.h>
5424149Sguido
5537275Scharnierstatic void usage(void) __dead2;
5624149Sguidoint fsirand(char *);
5724149Sguido
5824149Sguido/*
5924149Sguido * Possible superblock locations ordered from most to least likely.
6024149Sguido */
6124149Sguidostatic int sblock_try[] = SBLOCKSEARCH;
6224149Sguido
6324149Sguidostatic int printonly = 0, force = 0, ignorelabel = 0;
6424149Sguido
6524149Sguidoint
6624149Sguidomain(int argc, 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();
8437275Scharnier		}
8524149Sguido	}
8626625Sache	if (argc - optind < 1)
8724216Sache		usage();
8824149Sguido
8924149Sguido	srandomdev();
9024149Sguido
9124149Sguido	/* Increase our data size to the max */
9237275Scharnier	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
9324149Sguido		rl.rlim_cur = rl.rlim_max;
9437275Scharnier		if (setrlimit(RLIMIT_DATA, &rl) < 0)
9524149Sguido			warn("can't get resource limit to max data size");
9624149Sguido	} else
9724149Sguido		warn("can't get resource limit for data size");
9824149Sguido
9924149Sguido	for (n = optind; n < argc; n++) {
10024149Sguido		if (argc - optind != 1)
10124149Sguido			(void)puts(argv[n]);
10224149Sguido		ex += fsirand(argv[n]);
10324149Sguido		if (n < argc - 1)
10424149Sguido			putchar('\n');
10524149Sguido	}
10624149Sguido
10724149Sguido	exit(ex);
10824149Sguido}
10924149Sguido
11024149Sguidoint
11124149Sguidofsirand(char *device)
11224149Sguido{
11324149Sguido	struct ufs1_dinode *dp1;
11424149Sguido	struct ufs2_dinode *dp2;
11524149Sguido	caddr_t inodebuf;
11624149Sguido	ssize_t ibufsize;
11724149Sguido	struct fs *sblock;
11824149Sguido	ino_t inumber;
11924149Sguido	ufs2_daddr_t sblockloc, dblk;
12024149Sguido	char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
12124149Sguido	int i, devfd, n, cg;
12224149Sguido	u_int32_t bsize = DEV_BSIZE;
12337275Scharnier	struct disklabel label;
12424149Sguido
12524149Sguido	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
12624149Sguido		warn("can't open %s", device);
12724149Sguido		return (1);
12824149Sguido	}
12924149Sguido
13037275Scharnier	/* Get block size (usually 512) from disklabel if possible */
13124149Sguido	if (!ignorelabel) {
13224149Sguido		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
13324149Sguido			warn("can't read disklabel, using sector size of %d",
13424149Sguido			    bsize);
13524149Sguido		else
13624149Sguido			bsize = label.d_secsize;
13724149Sguido	}
13824149Sguido
13924149Sguido	dp1 = NULL;
14037275Scharnier	dp2 = NULL;
14124149Sguido
14224149Sguido	/* Read in master superblock */
14324149Sguido	(void)memset(&sbuf, 0, sizeof(sbuf));
14437275Scharnier	sblock = (struct fs *)&sbuf;
14524149Sguido	for (i = 0; sblock_try[i] != -1; i++) {
14624149Sguido		sblockloc = sblock_try[i];
14724149Sguido		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
14824149Sguido			warn("can't seek to superblock (%jd) on %s",
14924149Sguido			    (intmax_t)sblockloc, device);
15024149Sguido			return (1);
15124149Sguido		}
15237275Scharnier		if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) {
15324149Sguido			warnx("can't read superblock on %s: %s", device,
15424149Sguido			    (n < SBLOCKSIZE) ? "short read" : strerror(errno));
15524149Sguido			return (1);
15637275Scharnier		}
15724149Sguido		if ((sblock->fs_magic == FS_UFS1_MAGIC ||
15824149Sguido		     (sblock->fs_magic == FS_UFS2_MAGIC &&
15924149Sguido		      sblock->fs_sblockloc == sblock_try[i])) &&
16037275Scharnier		    sblock->fs_bsize <= MAXBSIZE &&
16124149Sguido		    sblock->fs_bsize >= (ssize_t)sizeof(struct fs))
16224149Sguido			break;
16324149Sguido	}
16437275Scharnier	if (sblock_try[i] == -1) {
16524149Sguido		fprintf(stderr, "Cannot find file system superblock\n");
16624149Sguido		return (1);
16724149Sguido	}
16824149Sguido
16924149Sguido	if (sblock->fs_magic == FS_UFS1_MAGIC &&
17024149Sguido	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
17124149Sguido		warnx("file system format is too old, sorry");
17224149Sguido		return (1);
17337275Scharnier	}
17424149Sguido	if (!force && !printonly && sblock->fs_clean != 1) {
17524149Sguido		warnx("file system is not clean, fsck %s first", device);
17637275Scharnier		return (1);
17724149Sguido	}
17824149Sguido
17924149Sguido	/* Make sure backup superblocks are sane. */
18024149Sguido	sblock = (struct fs *)&sbuftmp;
18124149Sguido	for (cg = 0; cg < (int)sblock->fs_ncg; cg++) {
18237275Scharnier		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
18324149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
18424149Sguido			warn("can't seek to %jd", (intmax_t)dblk * bsize);
18524149Sguido			return (1);
18624149Sguido		} else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) {
18737275Scharnier			warn("can't read backup superblock %d on %s: %s",
18824149Sguido			    cg + 1, device, (n < SBLOCKSIZE) ? "short write"
18924149Sguido			    : strerror(errno));
19024149Sguido			return (1);
19124149Sguido		}
19224149Sguido		if (sblock->fs_magic != FS_UFS1_MAGIC &&
19324149Sguido		    sblock->fs_magic != FS_UFS2_MAGIC) {
19424149Sguido			warnx("bad magic number in backup superblock %d on %s",
19524149Sguido			    cg + 1, device);
19624149Sguido			return (1);
19724149Sguido		}
19837275Scharnier		if (sblock->fs_sbsize > SBLOCKSIZE) {
19924149Sguido			warnx("size of backup superblock %d on %s is preposterous",
20024149Sguido			    cg + 1, device);
20124149Sguido			return (1);
20224149Sguido		}
20324149Sguido	}
20424149Sguido	sblock = (struct fs *)&sbuf;
20524149Sguido
20624149Sguido	/* XXX - should really cap buffer at 512kb or so */
20724149Sguido	if (sblock->fs_magic == FS_UFS1_MAGIC)
20824149Sguido		ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
20924149Sguido	else
21024149Sguido		ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
21124149Sguido	if ((inodebuf = malloc(ibufsize)) == NULL)
21224149Sguido		errx(1, "can't allocate memory for inode buffer");
21324149Sguido
21424149Sguido	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
21524149Sguido		if (sblock->fs_id[0])
21624149Sguido			(void)printf("%s was randomized on %s", device,
21737275Scharnier			    ctime((void *)&(sblock->fs_id[0])));
21824149Sguido		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
21924149Sguido			    sblock->fs_id[1]);
22024149Sguido	}
22124149Sguido
22237275Scharnier	/* Randomize fs_id unless old 4.2BSD file system */
22324149Sguido	if (!printonly) {
22424149Sguido		/* Randomize fs_id and write out new sblock and backups */
22524149Sguido		sblock->fs_id[0] = (u_int32_t)time(NULL);
22624149Sguido		sblock->fs_id[1] = random();
22724149Sguido
22824149Sguido		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
22924149Sguido			warn("can't seek to superblock (%jd) on %s",
23024149Sguido			    (intmax_t)sblockloc, device);
23124149Sguido			return (1);
23224149Sguido		}
23324149Sguido		if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
23437275Scharnier		    SBLOCKSIZE) {
23524149Sguido			warn("can't write superblock on %s: %s", device,
23624149Sguido			    (n < SBLOCKSIZE) ? "short write" : strerror(errno));
23737275Scharnier			return (1);
23824149Sguido		}
23924149Sguido	}
24024149Sguido
24124149Sguido	/* For each cylinder group, randomize inodes and update backup sblock */
24224149Sguido	for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) {
24324149Sguido		/* Update superblock if appropriate */
24424149Sguido		if (!printonly) {
24524149Sguido			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
24624149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
24737275Scharnier				warn("can't seek to %jd",
24824149Sguido				    (intmax_t)dblk * bsize);
24924149Sguido				return (1);
25037275Scharnier			} else if ((n = write(devfd, (void *)sblock,
25124149Sguido			    SBLOCKSIZE)) != SBLOCKSIZE) {
25224149Sguido			      warn("can't write backup superblock %d on %s: %s",
25324149Sguido				    cg + 1, device, (n < SBLOCKSIZE) ?
25424149Sguido				    "short write" : strerror(errno));
25524149Sguido				return (1);
25624149Sguido			}
25724149Sguido		}
25824149Sguido
25924149Sguido		/* Read in inodes, then print or randomize generation nums */
26024149Sguido		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
26124149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
26224149Sguido			warn("can't seek to %jd", (intmax_t)dblk * bsize);
26324149Sguido			return (1);
26424149Sguido		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
26524149Sguido			warnx("can't read inodes: %s",
26624149Sguido			     (n < ibufsize) ? "short read" : strerror(errno));
26724149Sguido			return (1);
26837275Scharnier		}
26924149Sguido
27024149Sguido		for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) {
27124149Sguido			if (sblock->fs_magic == FS_UFS1_MAGIC)
27224149Sguido				dp1 = &((struct ufs1_dinode *)inodebuf)[n];
27337275Scharnier			else
27424149Sguido				dp2 = &((struct ufs2_dinode *)inodebuf)[n];
27524149Sguido			if (inumber >= ROOTINO) {
27624149Sguido				if (printonly)
27724149Sguido					(void)printf("ino %d gen %08x\n",
27824149Sguido					    inumber,
27924149Sguido					    sblock->fs_magic == FS_UFS1_MAGIC ?
28024149Sguido					    dp1->di_gen : dp2->di_gen);
28124149Sguido				else if (sblock->fs_magic == FS_UFS1_MAGIC)
28224149Sguido					dp1->di_gen = random();
28324149Sguido				else
28424149Sguido					dp2->di_gen = random();
28526558Scharnier			}
28637275Scharnier		}
28724149Sguido
28824303Sguido		/* Write out modified inodes */
28937275Scharnier		if (!printonly) {
29037275Scharnier			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
29124149Sguido				warn("can't seek to %jd",
292				    (intmax_t)dblk * bsize);
293				return (1);
294			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
295				 ibufsize) {
296				warnx("can't write inodes: %s",
297				     (n != ibufsize) ? "short write" :
298				     strerror(errno));
299				return (1);
300			}
301		}
302	}
303	(void)close(devfd);
304
305	return(0);
306}
307
308static void
309usage(void)
310{
311	(void)fprintf(stderr,
312		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
313	exit(1);
314}
315