fsirand.c revision 227081
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 227081 2011-11-04 13:36:02Z ed $";
3637275Scharnier#endif /* not lint */
3724149Sguido
38217769Smckusick#include <sys/param.h>
3924149Sguido#include <sys/disklabel.h>
4024149Sguido#include <sys/resource.h>
4124149Sguido
4298542Smckusick#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>
49208074Suqs#include <stdint.h>
5024149Sguido#include <stdlib.h>
5124149Sguido#include <string.h>
52217769Smckusick#include <time.h>
5324149Sguido#include <unistd.h>
5424149Sguido
5592881Simpstatic void usage(void) __dead2;
5692881Simpint fsirand(char *);
5724149Sguido
5898542Smckusick/*
5998542Smckusick * Possible superblock locations ordered from most to least likely.
6098542Smckusick */
6198542Smckusickstatic int sblock_try[] = SBLOCKSEARCH;
6298542Smckusick
63227081Sedstatic int printonly = 0, force = 0, ignorelabel = 0;
6424149Sguido
6524149Sguidoint
6692881Simpmain(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':
8024149Sguido			force++;
8124149Sguido			break;
8224149Sguido		default:
8337275Scharnier			usage();
8424149Sguido		}
8524149Sguido	}
8624149Sguido	if (argc - optind < 1)
8737275Scharnier		usage();
8824149Sguido
8926625Sache	srandomdev();
9024216Sache
9124149Sguido	/* Increase our data size to the max */
9224149Sguido	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
9324149Sguido		rl.rlim_cur = rl.rlim_max;
9424149Sguido		if (setrlimit(RLIMIT_DATA, &rl) < 0)
9537275Scharnier			warn("can't get resource limit to max data size");
9624149Sguido	} else
9737275Scharnier		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
11192881Simpfsirand(char *device)
11224149Sguido{
11398542Smckusick	struct ufs1_dinode *dp1;
11498542Smckusick	struct ufs2_dinode *dp2;
11598542Smckusick	caddr_t inodebuf;
116208074Suqs	ssize_t ibufsize;
11724149Sguido	struct fs *sblock;
11824149Sguido	ino_t inumber, maxino;
11998542Smckusick	ufs2_daddr_t sblockloc, dblk;
12098542Smckusick	char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
12198542Smckusick	int i, devfd, n, cg;
12224149Sguido	u_int32_t bsize = DEV_BSIZE;
12324149Sguido	struct disklabel label;
12424149Sguido
12524149Sguido	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
12637275Scharnier		warn("can't open %s", device);
12724149Sguido		return (1);
12824149Sguido	}
12924149Sguido
13024149Sguido	/* Get block size (usually 512) from disklabel if possible */
13124149Sguido	if (!ignorelabel) {
13224149Sguido		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
13337275Scharnier			warn("can't read disklabel, using sector size of %d",
13424149Sguido			    bsize);
13524149Sguido		else
13624149Sguido			bsize = label.d_secsize;
13724149Sguido	}
13824149Sguido
139208074Suqs	dp1 = NULL;
140208074Suqs	dp2 = NULL;
141208074Suqs
14224149Sguido	/* Read in master superblock */
14324149Sguido	(void)memset(&sbuf, 0, sizeof(sbuf));
14424149Sguido	sblock = (struct fs *)&sbuf;
14598542Smckusick	for (i = 0; sblock_try[i] != -1; i++) {
14698542Smckusick		sblockloc = sblock_try[i];
14798542Smckusick		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
148208074Suqs			warn("can't seek to superblock (%jd) on %s",
149208074Suqs			    (intmax_t)sblockloc, device);
15098542Smckusick			return (1);
15198542Smckusick		}
15298542Smckusick		if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) {
15398542Smckusick			warnx("can't read superblock on %s: %s", device,
15498542Smckusick			    (n < SBLOCKSIZE) ? "short read" : strerror(errno));
15598542Smckusick			return (1);
15698542Smckusick		}
15798542Smckusick		if ((sblock->fs_magic == FS_UFS1_MAGIC ||
15898542Smckusick		     (sblock->fs_magic == FS_UFS2_MAGIC &&
159107294Smckusick		      sblock->fs_sblockloc == sblock_try[i])) &&
16098542Smckusick		    sblock->fs_bsize <= MAXBSIZE &&
161208074Suqs		    sblock->fs_bsize >= (ssize_t)sizeof(struct fs))
16298542Smckusick			break;
16324149Sguido	}
16498542Smckusick	if (sblock_try[i] == -1) {
165102231Strhodes		fprintf(stderr, "Cannot find file system superblock\n");
16624149Sguido		return (1);
16724149Sguido	}
16824149Sguido	maxino = sblock->fs_ncg * sblock->fs_ipg;
16924149Sguido
17098542Smckusick	if (sblock->fs_magic == FS_UFS1_MAGIC &&
17198542Smckusick	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
172102231Strhodes		warnx("file system format is too old, sorry");
17324149Sguido		return (1);
17424149Sguido	}
17524149Sguido	if (!force && !printonly && sblock->fs_clean != 1) {
176102231Strhodes		warnx("file system is not clean, fsck %s first", device);
17724149Sguido		return (1);
17824149Sguido	}
17924149Sguido
18024149Sguido	/* Make sure backup superblocks are sane. */
18124149Sguido	sblock = (struct fs *)&sbuftmp;
182208074Suqs	for (cg = 0; cg < (int)sblock->fs_ncg; cg++) {
18324149Sguido		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
18424149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
185208074Suqs			warn("can't seek to %jd", (intmax_t)dblk * bsize);
18624149Sguido			return (1);
18798542Smckusick		} else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) {
18837275Scharnier			warn("can't read backup superblock %d on %s: %s",
18998542Smckusick			    cg + 1, device, (n < SBLOCKSIZE) ? "short write"
19024149Sguido			    : strerror(errno));
19124149Sguido			return (1);
19224149Sguido		}
19398542Smckusick		if (sblock->fs_magic != FS_UFS1_MAGIC &&
19498542Smckusick		    sblock->fs_magic != FS_UFS2_MAGIC) {
19537275Scharnier			warnx("bad magic number in backup superblock %d on %s",
19624149Sguido			    cg + 1, device);
19724149Sguido			return (1);
19824149Sguido		}
19998542Smckusick		if (sblock->fs_sbsize > SBLOCKSIZE) {
20037275Scharnier			warnx("size of backup superblock %d on %s is preposterous",
20124149Sguido			    cg + 1, device);
20224149Sguido			return (1);
20324149Sguido		}
20424149Sguido	}
20524149Sguido	sblock = (struct fs *)&sbuf;
20624149Sguido
20724149Sguido	/* XXX - should really cap buffer at 512kb or so */
20898542Smckusick	if (sblock->fs_magic == FS_UFS1_MAGIC)
20998542Smckusick		ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
21098542Smckusick	else
21198542Smckusick		ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
21298542Smckusick	if ((inodebuf = malloc(ibufsize)) == NULL)
21398542Smckusick		errx(1, "can't allocate memory for inode buffer");
21424149Sguido
21524149Sguido	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
21698542Smckusick		if (sblock->fs_id[0])
21724149Sguido			(void)printf("%s was randomized on %s", device,
218208074Suqs			    ctime((void *)&(sblock->fs_id[0])));
21924149Sguido		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
22024149Sguido			    sblock->fs_id[1]);
22124149Sguido	}
22224149Sguido
223102231Strhodes	/* Randomize fs_id unless old 4.2BSD file system */
22498542Smckusick	if (!printonly) {
22524149Sguido		/* Randomize fs_id and write out new sblock and backups */
22624149Sguido		sblock->fs_id[0] = (u_int32_t)time(NULL);
22724149Sguido		sblock->fs_id[1] = random();
22824149Sguido
22998542Smckusick		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
230208074Suqs			warn("can't seek to superblock (%jd) on %s",
231208074Suqs			    (intmax_t)sblockloc, device);
23224149Sguido			return (1);
23324149Sguido		}
23498542Smckusick		if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
23598542Smckusick		    SBLOCKSIZE) {
23698542Smckusick			warn("can't write superblock on %s: %s", device,
23798542Smckusick			    (n < SBLOCKSIZE) ? "short write" : strerror(errno));
23824149Sguido			return (1);
23924149Sguido		}
24024149Sguido	}
24124149Sguido
24224149Sguido	/* For each cylinder group, randomize inodes and update backup sblock */
243208074Suqs	for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) {
24424149Sguido		/* Update superblock if appropriate */
24598542Smckusick		if (!printonly) {
24624149Sguido			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
24724149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
248208074Suqs				warn("can't seek to %jd",
249208074Suqs				    (intmax_t)dblk * bsize);
25024149Sguido				return (1);
25198542Smckusick			} else if ((n = write(devfd, (void *)sblock,
25298542Smckusick			    SBLOCKSIZE)) != SBLOCKSIZE) {
25398542Smckusick			      warn("can't write backup superblock %d on %s: %s",
25498542Smckusick				    cg + 1, device, (n < SBLOCKSIZE) ?
25598542Smckusick				    "short write" : strerror(errno));
25624149Sguido				return (1);
25724149Sguido			}
25824149Sguido		}
25924149Sguido
26024149Sguido		/* Read in inodes, then print or randomize generation nums */
26124149Sguido		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
26224149Sguido		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
263208074Suqs			warn("can't seek to %jd", (intmax_t)dblk * bsize);
26424149Sguido			return (1);
26524149Sguido		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
26637275Scharnier			warnx("can't read inodes: %s",
26724149Sguido			     (n < ibufsize) ? "short read" : strerror(errno));
26824149Sguido			return (1);
26924149Sguido		}
27024149Sguido
271208074Suqs		for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) {
27298542Smckusick			if (sblock->fs_magic == FS_UFS1_MAGIC)
27398542Smckusick				dp1 = &((struct ufs1_dinode *)inodebuf)[n];
27498542Smckusick			else
27598542Smckusick				dp2 = &((struct ufs2_dinode *)inodebuf)[n];
27624149Sguido			if (inumber >= ROOTINO) {
27724149Sguido				if (printonly)
278208074Suqs					(void)printf("ino %d gen %08x\n",
27998542Smckusick					    inumber,
28098542Smckusick					    sblock->fs_magic == FS_UFS1_MAGIC ?
281208074Suqs					    dp1->di_gen : dp2->di_gen);
282132762Skan				else if (sblock->fs_magic == FS_UFS1_MAGIC)
283132762Skan					dp1->di_gen = random();
28424149Sguido				else
285132762Skan					dp2->di_gen = random();
28624149Sguido			}
28724149Sguido		}
28824149Sguido
28924149Sguido		/* Write out modified inodes */
29024149Sguido		if (!printonly) {
29124149Sguido			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
292208074Suqs				warn("can't seek to %jd",
293208074Suqs				    (intmax_t)dblk * bsize);
29424149Sguido				return (1);
29524149Sguido			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
29624149Sguido				 ibufsize) {
29737275Scharnier				warnx("can't write inodes: %s",
29824149Sguido				     (n != ibufsize) ? "short write" :
29924149Sguido				     strerror(errno));
30024149Sguido				return (1);
30124149Sguido			}
30224149Sguido		}
30324149Sguido	}
30424149Sguido	(void)close(devfd);
30524149Sguido
30624149Sguido	return(0);
30724149Sguido}
30824149Sguido
30926558Scharnierstatic void
31092881Simpusage(void)
31124149Sguido{
31224303Sguido	(void)fprintf(stderr,
31337275Scharnier		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
31437275Scharnier	exit(1);
31524149Sguido}
316