Home | History | Annotate | Line # | Download | only in fsirand
fsirand.c revision 1.19
      1 /*	$NetBSD: fsirand.c,v 1.19 2002/01/08 05:01:50 lukem 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.19 2002/01/08 05:01:50 lukem Exp $");
     42 #endif /* lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/time.h>
     46 #include <sys/vnode.h>
     47 #include <sys/disklabel.h>
     48 #include <sys/ioctl.h>
     49 
     50 #include <ctype.h>
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <fcntl.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 #include <util.h>
     59 
     60 #include <ufs/ufs/ufs_bswap.h>
     61 
     62 #include <ufs/ufs/dinode.h>
     63 #include <ufs/ffs/fs.h>
     64 #include <ufs/ffs/ffs_extern.h>
     65 
     66 static void usage(void);
     67 static void getsblock(int, const char *, struct fs *);
     68 static void fixinodes(int, struct fs *, struct disklabel *, int, long);
     69 static void statussig(int);
     70 
     71 int main(int, char *[]);
     72 
     73 int	needswap, ino, imax;
     74 time_t	tstart;
     75 
     76 
     77 static void
     78 usage(void)
     79 {
     80 
     81 	(void) fprintf(stderr,
     82 	    "Usage: %s [-F] [-p] [-x <constant>] <special>\n",
     83 	    getprogname());
     84 	exit(1);
     85 }
     86 
     87 
     88 /*
     89  * getsblock():
     90  *	Return the superblock
     91  */
     92 static void
     93 getsblock(int fd, const char *name, struct fs *fs)
     94 {
     95 
     96 	if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
     97 		err(1, "%s: cannot seek to superblock", name);
     98 
     99 	if (read(fd, fs, SBSIZE) != SBSIZE)
    100 		err(1, "%s: cannot read superblock", name);
    101 
    102 	if (fs->fs_magic != FS_MAGIC)  {
    103 		if(fs->fs_magic == bswap32(FS_MAGIC)) {
    104 			needswap = 1;
    105 			ffs_sb_swap(fs, fs);
    106 		} else
    107 			errx(1, "%s: bad superblock magic number", name);
    108 	}
    109 
    110 	if (fs->fs_ncg < 1)
    111 		errx(1, "%s: bad ncg in superblock", name);
    112 
    113 	if (fs->fs_cpg < 1)
    114 		errx(1, "%s: bad cpg in superblock", name);
    115 
    116 	if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
    117 	    (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
    118 		errx(1, "%s: bad number of cylinders in superblock", name);
    119 
    120 	if (fs->fs_sbsize > SBSIZE)
    121 		errx(1, "%s: superblock too large", name);
    122 }
    123 
    124 
    125 /*
    126  * fixinodes():
    127  *	Randomize the inode generation numbers
    128  */
    129 static void
    130 fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
    131 {
    132 	int inopb = INOPB(fs);
    133 	int size = inopb * DINODE_SIZE;
    134 	caddr_t buf;
    135 	struct dinode *dip;
    136 	int i;
    137 
    138 	if ((buf = malloc(size)) == NULL)
    139 		err(1, "Out of memory");
    140 
    141 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    142 		off_t sp;
    143 		sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
    144 		     (off_t) lab->d_secsize;
    145 
    146 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    147 			err(1, "Seeking to inode %d failed", ino);
    148 
    149 		if (read(fd, buf, size) != size)
    150 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    151 
    152 		for (i = 0; i < inopb; i++) {
    153 			dip = (struct dinode *)(buf + (i * DINODE_SIZE));
    154 			if (pflag)
    155 				printf("inode %10d   gen 0x%08x\n", ino,
    156 					ufs_rw32(dip->di_gen, needswap));
    157 			else
    158 				dip->di_gen = ufs_rw32(random() ^ xorval,
    159 				    needswap);
    160 			if (++ino > imax)
    161 				errx(1, "Exceeded number of inodes");
    162 		}
    163 
    164 		if (pflag)
    165 			continue;
    166 
    167 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    168 			err(1, "Seeking to inode %d failed", ino);
    169 
    170 		if (write(fd, buf, size) != size)
    171 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    172 	}
    173 	free(buf);
    174 }
    175 
    176 /*
    177  * statussig():
    178  *	display current status
    179  */
    180 void
    181 statussig(int dummy)
    182 {
    183 	char	msgbuf[256];
    184 	int	len, deltat;
    185 	time_t	tnow, elapsed;
    186 
    187 	(void)time(&tnow);
    188 	elapsed = tnow - tstart;
    189 	len = snprintf(msgbuf, sizeof(msgbuf),
    190 	    "fsirand: completed inode %d of %d (%3.2f%%)",
    191 	    ino, imax, (ino * 100.0) / imax);
    192 	if (imax - ino) {
    193 		deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
    194 		len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
    195 		    ", finished in %d:%02d\n", deltat / 60, deltat % 60);
    196 	} else {
    197 		len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
    198 	}
    199 	write(STDERR_FILENO, msgbuf, len);
    200 }
    201 
    202 int
    203 main(int argc, char *argv[])
    204 {
    205 	const char *special;
    206 	char buf[SBSIZE], device[MAXPATHLEN];
    207 	struct fs *fs = (struct fs *) buf;
    208 	struct disklabel lab;
    209 	long xorval;
    210 	char *ep;
    211 	struct timeval tv;
    212 	int fd, c, Fflag, pflag, openflags;
    213 
    214 	xorval = 0;
    215 	Fflag = pflag = 0;
    216 
    217 	while ((c = getopt(argc, argv, "Fpx:")) != -1)
    218 		switch (c) {
    219 		case 'F':
    220 			Fflag++;
    221 			break;
    222 		case 'p':
    223 			pflag++;
    224 			break;
    225 		case 'x':
    226 			xorval = strtol(optarg, &ep, 0);
    227 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    228 			    errno == ERANGE)
    229 				err(1, "Out of range constant");
    230 			if (*ep)
    231 				errx(1, "Bad constant");
    232 			break;
    233 		default:
    234 			usage();
    235 		}
    236 
    237 	argv += optind;
    238 	argc -= optind;
    239 
    240 	if (argc != 1)
    241 		usage();
    242 
    243 	(void) gettimeofday(&tv, NULL);
    244 	srandom((unsigned) tv.tv_usec);
    245 
    246 	special = argv[0];
    247 	openflags = pflag ? O_RDONLY : O_RDWR;
    248 	if (Fflag)
    249 		fd = open(special, openflags);
    250 	else {
    251 		fd = opendisk(special, openflags, device, sizeof(device), 0);
    252 		special = device;
    253 	}
    254 	if (fd == -1)
    255 		err(1, "Cannot open `%s'", special);
    256 
    257 	if (Fflag) {
    258 		memset(&lab, 0, sizeof(lab));
    259 		lab.d_secsize = DEV_BSIZE;	/* XXX */
    260 	} else {
    261 		if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    262 			err(1, "%s: cannot get disklabel information", special);
    263 	}
    264 
    265 	time(&tstart);
    266 	(void)signal(SIGINFO, statussig);
    267 	getsblock(fd, special, fs);
    268 	fixinodes(fd, fs, &lab, pflag, xorval);
    269 
    270 	(void) close(fd);
    271 	return 0;
    272 }
    273