Home | History | Annotate | Line # | Download | only in fsirand
fsirand.c revision 1.9
      1  1.9      ross /*	$NetBSD: fsirand.c,v 1.9 1998/08/25 19:18:16 ross Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
      5  1.1  christos  *
      6  1.1  christos  * Redistribution and use in source and binary forms, with or without
      7  1.1  christos  * modification, are permitted provided that the following conditions
      8  1.1  christos  * are met:
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     15  1.1  christos  *    must display the following acknowledgement:
     16  1.1  christos  *	This product includes software developed by Christos Zoulas.
     17  1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     18  1.1  christos  *    derived from this software without specific prior written permission.
     19  1.1  christos  *
     20  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos 
     32  1.6     lukem #include <sys/cdefs.h>
     33  1.1  christos #ifndef lint
     34  1.9      ross __RCSID("$NetBSD: fsirand.c,v 1.9 1998/08/25 19:18:16 ross Exp $");
     35  1.1  christos #endif /* lint */
     36  1.1  christos 
     37  1.1  christos #include <stdio.h>
     38  1.3       cgd #include <string.h>
     39  1.1  christos #include <ctype.h>
     40  1.1  christos #include <fcntl.h>
     41  1.1  christos #include <errno.h>
     42  1.1  christos #include <err.h>
     43  1.1  christos #include <stdlib.h>
     44  1.1  christos #include <unistd.h>
     45  1.1  christos 
     46  1.1  christos #include <sys/types.h>
     47  1.1  christos #include <sys/param.h>
     48  1.1  christos #include <sys/time.h>
     49  1.1  christos #include <sys/vnode.h>
     50  1.1  christos #include <sys/disklabel.h>
     51  1.1  christos #include <sys/ioctl.h>
     52  1.1  christos 
     53  1.1  christos #include <ufs/ufs/quota.h>
     54  1.1  christos #include <ufs/ufs/inode.h>
     55  1.8    bouyer #include <ufs/ufs/ufs_bswap.h>
     56  1.1  christos 
     57  1.1  christos #include <ufs/ffs/fs.h>
     58  1.8    bouyer #include <ufs/ffs/ffs_extern.h>
     59  1.1  christos 
     60  1.1  christos static void usage __P((void));
     61  1.1  christos static void getsblock __P((int, const char *, struct disklabel *, struct fs *));
     62  1.1  christos static void fixinodes __P((int, struct fs *, struct disklabel *, int, long));
     63  1.1  christos 
     64  1.1  christos int main __P((int, char *[]));
     65  1.1  christos 
     66  1.8    bouyer int needswap = 0;
     67  1.8    bouyer 
     68  1.1  christos static void
     69  1.1  christos usage()
     70  1.1  christos {
     71  1.1  christos 	extern char *__progname;
     72  1.1  christos 
     73  1.4    kleink 	(void) fprintf(stderr, "%s: [-x <constant>] [-p] <special>\n",
     74  1.4    kleink 	    __progname);
     75  1.1  christos 	exit(1);
     76  1.1  christos }
     77  1.1  christos 
     78  1.1  christos 
     79  1.1  christos /* getsblock():
     80  1.1  christos  *	Return the superblock
     81  1.1  christos  */
     82  1.1  christos static void
     83  1.1  christos getsblock(fd, name, lab, fs)
     84  1.1  christos 	int fd;
     85  1.1  christos 	const char *name;
     86  1.1  christos 	struct disklabel *lab;
     87  1.1  christos 	struct fs *fs;
     88  1.1  christos {
     89  1.1  christos 	struct partition *pp = NULL;
     90  1.1  christos 	char p = name[strlen(name) - 1];
     91  1.1  christos 
     92  1.1  christos 	if (p >= 'a' && p <= 'h')
     93  1.1  christos 		pp = &lab->d_partitions[p - 'a'];
     94  1.1  christos 	else if (isdigit((unsigned char) p))
     95  1.1  christos 		pp = &lab->d_partitions[0];
     96  1.1  christos 	else
     97  1.1  christos 		errx(1, "Invalid partition `%c'", p);
     98  1.1  christos 
     99  1.1  christos 	if (pp->p_fstype != FS_BSDFFS)
    100  1.1  christos 		errx(1, "Not an FFS partition");
    101  1.1  christos 
    102  1.1  christos 	if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
    103  1.1  christos 		err(1, "Cannot seek to superblock");
    104  1.1  christos 
    105  1.1  christos 	if (read(fd, fs, SBSIZE) != SBSIZE)
    106  1.1  christos 		err(1, "Cannot read superblock");
    107  1.1  christos 
    108  1.9      ross 	if (fs->fs_magic != FS_MAGIC)  {
    109  1.8    bouyer 		if(fs->fs_magic == bswap32(FS_MAGIC)) {
    110  1.8    bouyer 			needswap = 1;
    111  1.8    bouyer 			ffs_sb_swap(fs, fs, 0);
    112  1.8    bouyer 		} else
    113  1.8    bouyer 			errx(1, "Bad superblock magic number");
    114  1.9      ross 	}
    115  1.1  christos 
    116  1.1  christos 	if (fs->fs_ncg < 1)
    117  1.1  christos 		errx(1, "Bad ncg in superblock");
    118  1.1  christos 
    119  1.1  christos 	if (fs->fs_cpg < 1)
    120  1.1  christos 		errx(1, "Bad cpg in superblock");
    121  1.1  christos 
    122  1.1  christos 	if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
    123  1.1  christos 	    (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
    124  1.1  christos 		errx(1, "Bad number of cylinders in superblock");
    125  1.1  christos 
    126  1.1  christos 	if (fs->fs_sbsize > SBSIZE)
    127  1.1  christos 		errx(1, "Superblock too large");
    128  1.1  christos }
    129  1.1  christos 
    130  1.1  christos /* fixinodes():
    131  1.1  christos  *	Randomize the inode generation numbers
    132  1.1  christos  */
    133  1.1  christos static void
    134  1.1  christos fixinodes(fd, fs, lab, pflag, xorval)
    135  1.1  christos 	int fd;
    136  1.1  christos 	struct fs *fs;
    137  1.1  christos 	struct disklabel *lab;
    138  1.1  christos 	int pflag;
    139  1.1  christos 	long xorval;
    140  1.1  christos {
    141  1.1  christos 	int inopb = INOPB(fs);
    142  1.1  christos 	int size = inopb * sizeof(struct dinode);
    143  1.1  christos 	struct dinode *dibuf, *dip;
    144  1.1  christos 	int ino, imax;
    145  1.1  christos 
    146  1.1  christos 	if ((dibuf = malloc(size)) == NULL)
    147  1.1  christos 		err(1, "Out of memory");
    148  1.1  christos 
    149  1.1  christos 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    150  1.5   mycroft 		off_t sp;
    151  1.7  christos #if __GNUC__	/* XXX work around lame compiler problem (gcc 2.7.2) */
    152  1.7  christos 		(void)&sp;
    153  1.7  christos #endif
    154  1.5   mycroft 		sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
    155  1.5   mycroft 		     (off_t) lab->d_secsize;
    156  1.1  christos 
    157  1.1  christos 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    158  1.1  christos 			err(1, "Seeking to inode %d failed", ino);
    159  1.1  christos 
    160  1.1  christos 		if (read(fd, dibuf, size) != size)
    161  1.1  christos 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    162  1.1  christos 
    163  1.1  christos 		for (dip = dibuf; dip < &dibuf[inopb]; dip++) {
    164  1.1  christos 			if (pflag)
    165  1.8    bouyer 				printf("ino %d gen 0x%x\n", ino,
    166  1.8    bouyer 					ufs_rw32(dip->di_gen, needswap));
    167  1.1  christos 			else
    168  1.8    bouyer 				dip->di_gen = ufs_rw32(random() ^ xorval, needswap);
    169  1.1  christos 			if (++ino > imax)
    170  1.1  christos 				errx(1, "Exceeded number of inodes");
    171  1.1  christos 		}
    172  1.1  christos 
    173  1.1  christos 		if (pflag)
    174  1.1  christos 			continue;
    175  1.1  christos 
    176  1.1  christos 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    177  1.1  christos 			err(1, "Seeking to inode %d failed", ino);
    178  1.1  christos 
    179  1.1  christos 		if (write(fd, dibuf, size) != size)
    180  1.1  christos 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    181  1.1  christos 	}
    182  1.1  christos 	free(dibuf);
    183  1.1  christos }
    184  1.1  christos 
    185  1.1  christos int
    186  1.1  christos main(argc, argv)
    187  1.1  christos 	int	argc;
    188  1.1  christos 	char	*argv[];
    189  1.1  christos {
    190  1.1  christos 	char buf[SBSIZE];
    191  1.1  christos 	struct fs *fs = (struct fs *) buf;
    192  1.1  christos 	struct disklabel lab;
    193  1.1  christos 	int fd, c;
    194  1.1  christos 	long xorval = 0;
    195  1.1  christos 	char *ep;
    196  1.1  christos 	struct timeval tv;
    197  1.1  christos 
    198  1.1  christos 	int pflag = 0;
    199  1.1  christos 
    200  1.1  christos 	while ((c = getopt(argc, argv, "px:")) != -1)
    201  1.1  christos 		switch (c) {
    202  1.1  christos 		case 'p':
    203  1.1  christos 			pflag++;
    204  1.1  christos 			break;
    205  1.1  christos 		case 'x':
    206  1.1  christos 			xorval = strtol(optarg, &ep, 0);
    207  1.4    kleink 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    208  1.1  christos 			    errno == ERANGE)
    209  1.1  christos 				err(1, "Out of range constant");
    210  1.1  christos 			if (*ep)
    211  1.1  christos 				errx(1, "Bad constant");
    212  1.1  christos 			break;
    213  1.1  christos 		default:
    214  1.1  christos 			usage();
    215  1.1  christos 		}
    216  1.1  christos 
    217  1.1  christos 	argv += optind;
    218  1.1  christos 	argc -= optind;
    219  1.1  christos 
    220  1.1  christos 	if (argc != 1)
    221  1.1  christos 		usage();
    222  1.1  christos 
    223  1.1  christos 	(void) gettimeofday(&tv, NULL);
    224  1.1  christos 	srandom((unsigned) tv.tv_usec);
    225  1.1  christos 
    226  1.1  christos 	if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
    227  1.1  christos 		err(1, "Cannot open `%s'", argv[0]);
    228  1.1  christos 
    229  1.1  christos 	if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    230  1.1  christos 		err(1, "Cannot get label information");
    231  1.1  christos 
    232  1.1  christos 	getsblock(fd, argv[0], &lab, fs);
    233  1.1  christos 	fixinodes(fd, fs, &lab, pflag, xorval);
    234  1.1  christos 
    235  1.1  christos 	(void) close(fd);
    236  1.1  christos 	return 0;
    237  1.1  christos }
    238