Home | History | Annotate | Line # | Download | only in fsirand
fsirand.c revision 1.16
      1 /*	$NetBSD: fsirand.c,v 1.16 2001/10/26 05:56:06 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.16 2001/10/26 05:56:06 lukem Exp $");
     42 #endif /* lint */
     43 
     44 #include <ctype.h>
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <fcntl.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.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/inode.h>
     61 #include <ufs/ufs/ufs_bswap.h>
     62 
     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 disklabel *, 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 disklabel *lab, struct fs *fs)
     94 {
     95 	struct partition *pp;
     96 	char p;
     97 
     98 	pp = NULL;
     99 
    100 	if (lab != NULL) {
    101 		p = name[strlen(name) - 1];
    102 		if (p >= 'a' && p <= 'h')
    103 			pp = &lab->d_partitions[p - 'a'];
    104 		else if (isdigit((unsigned char) p))
    105 			pp = &lab->d_partitions[0];
    106 		else
    107 			errx(1, "Invalid partition `%c'", p);
    108 
    109 		if (pp->p_fstype != FS_BSDFFS)
    110 			errx(1, "Not an FFS partition");
    111 	}
    112 
    113 	if (lseek(fd, (off_t) SBOFF , SEEK_SET) == (off_t) -1)
    114 		err(1, "Cannot seek to superblock");
    115 
    116 	if (read(fd, fs, SBSIZE) != SBSIZE)
    117 		err(1, "Cannot read superblock");
    118 
    119 	if (fs->fs_magic != FS_MAGIC)  {
    120 		if(fs->fs_magic == bswap32(FS_MAGIC)) {
    121 			needswap = 1;
    122 			ffs_sb_swap(fs, fs);
    123 		} else
    124 			errx(1, "Bad superblock magic number");
    125 	}
    126 
    127 	if (fs->fs_ncg < 1)
    128 		errx(1, "Bad ncg in superblock");
    129 
    130 	if (fs->fs_cpg < 1)
    131 		errx(1, "Bad cpg in superblock");
    132 
    133 	if (fs->fs_ncg * fs->fs_cpg < fs->fs_ncyl ||
    134 	    (fs->fs_ncg - 1) * fs->fs_cpg >= fs->fs_ncyl)
    135 		errx(1, "Bad number of cylinders in superblock");
    136 
    137 	if (fs->fs_sbsize > SBSIZE)
    138 		errx(1, "Superblock too large");
    139 }
    140 
    141 
    142 /*
    143  * fixinodes():
    144  *	Randomize the inode generation numbers
    145  */
    146 static void
    147 fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
    148 {
    149 	int inopb = INOPB(fs);
    150 	int size = inopb * DINODE_SIZE;
    151 	caddr_t buf;
    152 	struct dinode *dip;
    153 	int i;
    154 
    155 	if ((buf = malloc(size)) == NULL)
    156 		err(1, "Out of memory");
    157 
    158 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    159 		off_t sp;
    160 		sp = (off_t) fsbtodb(fs, ino_to_fsba(fs, ino)) *
    161 		     (off_t) lab->d_secsize;
    162 
    163 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    164 			err(1, "Seeking to inode %d failed", ino);
    165 
    166 		if (read(fd, buf, size) != size)
    167 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    168 
    169 		for (i = 0; i < inopb; i++) {
    170 			dip = (struct dinode *)(buf + (i * DINODE_SIZE));
    171 			if (pflag)
    172 				printf("inode %10d   gen 0x%08x\n", ino,
    173 					ufs_rw32(dip->di_gen, needswap));
    174 			else
    175 				dip->di_gen = ufs_rw32(random() ^ xorval,
    176 				    needswap);
    177 			if (++ino > imax)
    178 				errx(1, "Exceeded number of inodes");
    179 		}
    180 
    181 		if (pflag)
    182 			continue;
    183 
    184 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    185 			err(1, "Seeking to inode %d failed", ino);
    186 
    187 		if (write(fd, buf, size) != size)
    188 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    189 	}
    190 	free(buf);
    191 }
    192 
    193 /*
    194  * statussig():
    195  *	display current status
    196  */
    197 void
    198 statussig(int dummy)
    199 {
    200 	char	msgbuf[256];
    201 	int	len, deltat;
    202 	time_t	tnow, elapsed;
    203 
    204 	(void)time(&tnow);
    205 	elapsed = tnow - tstart;
    206 	len = snprintf(msgbuf, sizeof(msgbuf),
    207 	    "fsirand: completed inode %d of %d (%3.2f%%)",
    208 	    ino, imax, (ino * 100.0) / imax);
    209 	if (imax - ino) {
    210 		deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
    211 		len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
    212 		    ", finished in %d:%02d\n", deltat / 60, deltat % 60);
    213 	} else {
    214 		len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
    215 	}
    216 	write(STDERR_FILENO, msgbuf, len);
    217 }
    218 
    219 int
    220 main(int argc, char *argv[])
    221 {
    222 	char buf[SBSIZE];
    223 	struct fs *fs = (struct fs *) buf;
    224 	struct disklabel lab;
    225 	struct stat st;
    226 	int fd, c;
    227 	long xorval = 0;
    228 	char *ep;
    229 	struct timeval tv;
    230 	int Fflag, pflag;
    231 
    232 	Fflag = pflag = 0;
    233 
    234 	while ((c = getopt(argc, argv, "Fpx:")) != -1)
    235 		switch (c) {
    236 		case 'F':
    237 			Fflag++;
    238 			break;
    239 		case 'p':
    240 			pflag++;
    241 			break;
    242 		case 'x':
    243 			xorval = strtol(optarg, &ep, 0);
    244 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    245 			    errno == ERANGE)
    246 				err(1, "Out of range constant");
    247 			if (*ep)
    248 				errx(1, "Bad constant");
    249 			break;
    250 		default:
    251 			usage();
    252 		}
    253 
    254 	argv += optind;
    255 	argc -= optind;
    256 
    257 	if (argc != 1)
    258 		usage();
    259 
    260 	(void) gettimeofday(&tv, NULL);
    261 	srandom((unsigned) tv.tv_usec);
    262 
    263 	if ((fd = open(argv[0], pflag ? O_RDONLY : O_RDWR)) == -1)
    264 		err(1, "Cannot open `%s'", argv[0]);
    265 	if (fstat(fd, &st) == -1)
    266 		err(1, "Cannot stat `%s'", argv[0]);
    267 
    268 	if (Fflag) {
    269 		if (!S_ISREG(st.st_mode))
    270 			errx(1, "%s: not a regular file", argv[0]);
    271 		memset(&lab, 0, sizeof(lab));
    272 		lab.d_secsize = DEV_BSIZE;	/* XXX */
    273 	} else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
    274 		if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    275 			err(1, "%s: cannot get disklabel information", argv[0]);
    276 	} else
    277 		errx(1, "%s: not a block or character device", argv[0]);
    278 
    279 	time(&tstart);
    280 	(void)signal(SIGINFO, statussig);
    281 	getsblock(fd, argv[0], Fflag ? NULL : &lab, fs);
    282 	fixinodes(fd, fs, &lab, pflag, xorval);
    283 
    284 	(void) close(fd);
    285 	return 0;
    286 }
    287