Home | History | Annotate | Line # | Download | only in fsirand
fsirand.c revision 1.8
      1 /*	$NetBSD: fsirand.c,v 1.8 1998/03/18 17:07:14 bouyer 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.8 1998/03/18 17:07:14 bouyer 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 	if (fs->fs_ncg < 1)
    116 		errx(1, "Bad ncg in superblock");
    117 
    118 	if (fs->fs_cpg < 1)
    119 		errx(1, "Bad cpg in superblock");
    120 
    121 	if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
    122 	    (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
    123 		errx(1, "Bad number of cylinders in superblock");
    124 
    125 	if (fs->fs_sbsize > SBSIZE)
    126 		errx(1, "Superblock too large");
    127 }
    128 
    129 /* fixinodes():
    130  *	Randomize the inode generation numbers
    131  */
    132 static void
    133 fixinodes(fd, fs, lab, pflag, xorval)
    134 	int fd;
    135 	struct fs *fs;
    136 	struct disklabel *lab;
    137 	int pflag;
    138 	long xorval;
    139 {
    140 	int inopb = INOPB(fs);
    141 	int size = inopb * sizeof(struct dinode);
    142 	struct dinode *dibuf, *dip;
    143 	int ino, imax;
    144 
    145 	if ((dibuf = malloc(size)) == NULL)
    146 		err(1, "Out of memory");
    147 
    148 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    149 		off_t sp;
    150 #if __GNUC__	/* XXX work around lame compiler problem (gcc 2.7.2) */
    151 		(void)&sp;
    152 #endif
    153 		sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
    154 		     (off_t) lab->d_secsize;
    155 
    156 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    157 			err(1, "Seeking to inode %d failed", ino);
    158 
    159 		if (read(fd, dibuf, size) != size)
    160 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    161 
    162 		for (dip = dibuf; dip < &dibuf[inopb]; dip++) {
    163 			if (pflag)
    164 				printf("ino %d gen 0x%x\n", ino,
    165 					ufs_rw32(dip->di_gen, needswap));
    166 			else
    167 				dip->di_gen = ufs_rw32(random() ^ xorval, needswap);
    168 			if (++ino > imax)
    169 				errx(1, "Exceeded number of inodes");
    170 		}
    171 
    172 		if (pflag)
    173 			continue;
    174 
    175 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    176 			err(1, "Seeking to inode %d failed", ino);
    177 
    178 		if (write(fd, dibuf, size) != size)
    179 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    180 	}
    181 	free(dibuf);
    182 }
    183 
    184 int
    185 main(argc, argv)
    186 	int	argc;
    187 	char	*argv[];
    188 {
    189 	char buf[SBSIZE];
    190 	struct fs *fs = (struct fs *) buf;
    191 	struct disklabel lab;
    192 	int fd, c;
    193 	long xorval = 0;
    194 	char *ep;
    195 	struct timeval tv;
    196 
    197 	int pflag = 0;
    198 
    199 	while ((c = getopt(argc, argv, "px:")) != -1)
    200 		switch (c) {
    201 		case 'p':
    202 			pflag++;
    203 			break;
    204 		case 'x':
    205 			xorval = strtol(optarg, &ep, 0);
    206 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    207 			    errno == ERANGE)
    208 				err(1, "Out of range constant");
    209 			if (*ep)
    210 				errx(1, "Bad constant");
    211 			break;
    212 		default:
    213 			usage();
    214 		}
    215 
    216 	argv += optind;
    217 	argc -= optind;
    218 
    219 	if (argc != 1)
    220 		usage();
    221 
    222 	(void) gettimeofday(&tv, NULL);
    223 	srandom((unsigned) tv.tv_usec);
    224 
    225 	if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
    226 		err(1, "Cannot open `%s'", argv[0]);
    227 
    228 	if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    229 		err(1, "Cannot get label information");
    230 
    231 	getsblock(fd, argv[0], &lab, fs);
    232 	fixinodes(fd, fs, &lab, pflag, xorval);
    233 
    234 	(void) close(fd);
    235 	return 0;
    236 }
    237