Home | History | Annotate | Line # | Download | only in fsirand
fsirand.c revision 1.4
      1 /*	$NetBSD: fsirand.c,v 1.4 1997/06/24 17:47:03 kleink 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 #ifndef lint
     33 static char rcsid[] = "$NetBSD: fsirand.c,v 1.4 1997/06/24 17:47:03 kleink Exp $";
     34 #endif /* lint */
     35 
     36 #include <stdio.h>
     37 #include <string.h>
     38 #include <ctype.h>
     39 #include <fcntl.h>
     40 #include <errno.h>
     41 #include <err.h>
     42 #include <stdlib.h>
     43 #include <unistd.h>
     44 
     45 #include <sys/types.h>
     46 #include <sys/param.h>
     47 #include <sys/time.h>
     48 #include <sys/vnode.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/ioctl.h>
     51 
     52 #include <ufs/ufs/quota.h>
     53 #include <ufs/ufs/inode.h>
     54 
     55 #include <ufs/ffs/fs.h>
     56 
     57 static void usage __P((void));
     58 static void getsblock __P((int, const char *, struct disklabel *, struct fs *));
     59 static void fixinodes __P((int, struct fs *, struct disklabel *, int, long));
     60 
     61 int main __P((int, char *[]));
     62 
     63 static void
     64 usage()
     65 {
     66 	extern char *__progname;
     67 
     68 	(void) fprintf(stderr, "%s: [-x <constant>] [-p] <special>\n",
     69 	    __progname);
     70 	exit(1);
     71 }
     72 
     73 
     74 /* getsblock():
     75  *	Return the superblock
     76  */
     77 static void
     78 getsblock(fd, name, lab, fs)
     79 	int fd;
     80 	const char *name;
     81 	struct disklabel *lab;
     82 	struct fs *fs;
     83 {
     84 	struct partition *pp = NULL;
     85 	char p = name[strlen(name) - 1];
     86 
     87 	if (p >= 'a' && p <= 'h')
     88 		pp = &lab->d_partitions[p - 'a'];
     89 	else if (isdigit((unsigned char) p))
     90 		pp = &lab->d_partitions[0];
     91 	else
     92 		errx(1, "Invalid partition `%c'", p);
     93 
     94 	if (pp->p_fstype != FS_BSDFFS)
     95 		errx(1, "Not an FFS partition");
     96 
     97 	if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
     98 		err(1, "Cannot seek to superblock");
     99 
    100 	if (read(fd, fs, SBSIZE) != SBSIZE)
    101 		err(1, "Cannot read superblock");
    102 
    103 	if (fs->fs_magic != FS_MAGIC)
    104 		errx(1, "Bad superblock magic number");
    105 
    106 	if (fs->fs_ncg < 1)
    107 		errx(1, "Bad ncg in superblock");
    108 
    109 	if (fs->fs_cpg < 1)
    110 		errx(1, "Bad cpg in superblock");
    111 
    112 	if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
    113 	    (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
    114 		errx(1, "Bad number of cylinders in superblock");
    115 
    116 	if (fs->fs_sbsize > SBSIZE)
    117 		errx(1, "Superblock too large");
    118 }
    119 
    120 /* fixinodes():
    121  *	Randomize the inode generation numbers
    122  */
    123 static void
    124 fixinodes(fd, fs, lab, pflag, xorval)
    125 	int fd;
    126 	struct fs *fs;
    127 	struct disklabel *lab;
    128 	int pflag;
    129 	long xorval;
    130 {
    131 	int inopb = INOPB(fs);
    132 	int size = inopb * sizeof(struct dinode);
    133 	struct dinode *dibuf, *dip;
    134 	int ino, imax;
    135 
    136 	if ((dibuf = malloc(size)) == NULL)
    137 		err(1, "Out of memory");
    138 
    139 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    140 		off_t sp = fsbtodb(fs, ino_to_fsba(fs, ino)) * lab->d_secsize;
    141 
    142 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    143 			err(1, "Seeking to inode %d failed", ino);
    144 
    145 		if (read(fd, dibuf, size) != size)
    146 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    147 
    148 		for (dip = dibuf; dip < &dibuf[inopb]; dip++) {
    149 			if (pflag)
    150 				printf("ino %d gen %x\n", ino, dip->di_gen);
    151 			else
    152 				dip->di_gen = random() ^ xorval;
    153 			if (++ino > imax)
    154 				errx(1, "Exceeded number of inodes");
    155 		}
    156 
    157 		if (pflag)
    158 			continue;
    159 
    160 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    161 			err(1, "Seeking to inode %d failed", ino);
    162 
    163 		if (write(fd, dibuf, size) != size)
    164 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    165 	}
    166 	free(dibuf);
    167 }
    168 
    169 int
    170 main(argc, argv)
    171 	int	argc;
    172 	char	*argv[];
    173 {
    174 	char buf[SBSIZE];
    175 	struct fs *fs = (struct fs *) buf;
    176 	struct disklabel lab;
    177 	int fd, c;
    178 	long xorval = 0;
    179 	char *ep;
    180 	struct timeval tv;
    181 
    182 	int pflag = 0;
    183 
    184 	while ((c = getopt(argc, argv, "px:")) != -1)
    185 		switch (c) {
    186 		case 'p':
    187 			pflag++;
    188 			break;
    189 		case 'x':
    190 			xorval = strtol(optarg, &ep, 0);
    191 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    192 			    errno == ERANGE)
    193 				err(1, "Out of range constant");
    194 			if (*ep)
    195 				errx(1, "Bad constant");
    196 			break;
    197 		default:
    198 			usage();
    199 		}
    200 
    201 	argv += optind;
    202 	argc -= optind;
    203 
    204 	if (argc != 1)
    205 		usage();
    206 
    207 	(void) gettimeofday(&tv, NULL);
    208 	srandom((unsigned) tv.tv_usec);
    209 
    210 	if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
    211 		err(1, "Cannot open `%s'", argv[0]);
    212 
    213 	if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    214 		err(1, "Cannot get label information");
    215 
    216 	getsblock(fd, argv[0], &lab, fs);
    217 	fixinodes(fd, fs, &lab, pflag, xorval);
    218 
    219 	(void) close(fd);
    220 	return 0;
    221 }
    222