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