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