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