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