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