Home | History | Annotate | Line # | Download | only in fsirand
fsirand.c revision 1.10
      1  1.10   thorpej /*	$NetBSD: fsirand.c,v 1.10 1998/10/23 01:27:51 thorpej 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.10   thorpej __RCSID("$NetBSD: fsirand.c,v 1.10 1998/10/23 01:27:51 thorpej 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.10   thorpej 	int size = inopb * DINODE_SIZE;
    143  1.10   thorpej 	caddr_t buf;
    144  1.10   thorpej 	struct dinode *dip;
    145  1.10   thorpej 	int i, ino, imax;
    146   1.1  christos 
    147  1.10   thorpej 	if ((buf = malloc(size)) == NULL)
    148   1.1  christos 		err(1, "Out of memory");
    149   1.1  christos 
    150   1.1  christos 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    151   1.5   mycroft 		off_t sp;
    152   1.7  christos #if __GNUC__	/* XXX work around lame compiler problem (gcc 2.7.2) */
    153   1.7  christos 		(void)&sp;
    154   1.7  christos #endif
    155   1.5   mycroft 		sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
    156   1.5   mycroft 		     (off_t) lab->d_secsize;
    157   1.1  christos 
    158   1.1  christos 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    159   1.1  christos 			err(1, "Seeking to inode %d failed", ino);
    160   1.1  christos 
    161  1.10   thorpej 		if (read(fd, buf, size) != size)
    162   1.1  christos 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    163   1.1  christos 
    164  1.10   thorpej 		for (i = 0; i < inopb; i++) {
    165  1.10   thorpej 			dip = (struct dinode *)(buf + (i * DINODE_SIZE));
    166   1.1  christos 			if (pflag)
    167   1.8    bouyer 				printf("ino %d gen 0x%x\n", ino,
    168   1.8    bouyer 					ufs_rw32(dip->di_gen, needswap));
    169   1.1  christos 			else
    170   1.8    bouyer 				dip->di_gen = ufs_rw32(random() ^ xorval, needswap);
    171   1.1  christos 			if (++ino > imax)
    172   1.1  christos 				errx(1, "Exceeded number of inodes");
    173   1.1  christos 		}
    174   1.1  christos 
    175   1.1  christos 		if (pflag)
    176   1.1  christos 			continue;
    177   1.1  christos 
    178   1.1  christos 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    179   1.1  christos 			err(1, "Seeking to inode %d failed", ino);
    180   1.1  christos 
    181  1.10   thorpej 		if (write(fd, buf, size) != size)
    182   1.1  christos 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    183   1.1  christos 	}
    184  1.10   thorpej 	free(buf);
    185   1.1  christos }
    186   1.1  christos 
    187   1.1  christos int
    188   1.1  christos main(argc, argv)
    189   1.1  christos 	int	argc;
    190   1.1  christos 	char	*argv[];
    191   1.1  christos {
    192   1.1  christos 	char buf[SBSIZE];
    193   1.1  christos 	struct fs *fs = (struct fs *) buf;
    194   1.1  christos 	struct disklabel lab;
    195   1.1  christos 	int fd, c;
    196   1.1  christos 	long xorval = 0;
    197   1.1  christos 	char *ep;
    198   1.1  christos 	struct timeval tv;
    199   1.1  christos 
    200   1.1  christos 	int pflag = 0;
    201   1.1  christos 
    202   1.1  christos 	while ((c = getopt(argc, argv, "px:")) != -1)
    203   1.1  christos 		switch (c) {
    204   1.1  christos 		case 'p':
    205   1.1  christos 			pflag++;
    206   1.1  christos 			break;
    207   1.1  christos 		case 'x':
    208   1.1  christos 			xorval = strtol(optarg, &ep, 0);
    209   1.4    kleink 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    210   1.1  christos 			    errno == ERANGE)
    211   1.1  christos 				err(1, "Out of range constant");
    212   1.1  christos 			if (*ep)
    213   1.1  christos 				errx(1, "Bad constant");
    214   1.1  christos 			break;
    215   1.1  christos 		default:
    216   1.1  christos 			usage();
    217   1.1  christos 		}
    218   1.1  christos 
    219   1.1  christos 	argv += optind;
    220   1.1  christos 	argc -= optind;
    221   1.1  christos 
    222   1.1  christos 	if (argc != 1)
    223   1.1  christos 		usage();
    224   1.1  christos 
    225   1.1  christos 	(void) gettimeofday(&tv, NULL);
    226   1.1  christos 	srandom((unsigned) tv.tv_usec);
    227   1.1  christos 
    228   1.1  christos 	if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
    229   1.1  christos 		err(1, "Cannot open `%s'", argv[0]);
    230   1.1  christos 
    231   1.1  christos 	if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    232   1.1  christos 		err(1, "Cannot get label information");
    233   1.1  christos 
    234   1.1  christos 	getsblock(fd, argv[0], &lab, fs);
    235   1.1  christos 	fixinodes(fd, fs, &lab, pflag, xorval);
    236   1.1  christos 
    237   1.1  christos 	(void) close(fd);
    238   1.1  christos 	return 0;
    239   1.1  christos }
    240