Home | History | Annotate | Line # | Download | only in fsirand
      1  1.33       chs /*	$NetBSD: fsirand.c,v 1.33 2022/11/17 06:40:39 chs Exp $	*/
      2   1.1  christos 
      3  1.11  christos /*-
      4  1.11  christos  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.11  christos  * All rights reserved.
      6  1.11  christos  *
      7  1.11  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.11  christos  * by Christos Zoulas.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  *
     19  1.11  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.11  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.11  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.11  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.11  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.11  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.11  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.11  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.11  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.11  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.11  christos  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  christos  */
     31   1.1  christos 
     32   1.6     lukem #include <sys/cdefs.h>
     33   1.1  christos #ifndef lint
     34  1.33       chs __RCSID("$NetBSD: fsirand.c,v 1.33 2022/11/17 06:40:39 chs Exp $");
     35   1.1  christos #endif /* lint */
     36   1.1  christos 
     37  1.17     lukem #include <sys/param.h>
     38  1.17     lukem #include <sys/time.h>
     39  1.17     lukem #include <sys/vnode.h>
     40  1.17     lukem #include <sys/disklabel.h>
     41  1.17     lukem #include <sys/ioctl.h>
     42  1.17     lukem 
     43   1.1  christos #include <ctype.h>
     44  1.15     lukem #include <err.h>
     45  1.15     lukem #include <errno.h>
     46   1.1  christos #include <fcntl.h>
     47  1.15     lukem #include <stdio.h>
     48   1.1  christos #include <stdlib.h>
     49  1.15     lukem #include <string.h>
     50   1.1  christos #include <unistd.h>
     51  1.17     lukem #include <util.h>
     52  1.27  drochner #include <signal.h>
     53   1.1  christos 
     54   1.8    bouyer #include <ufs/ufs/ufs_bswap.h>
     55   1.1  christos 
     56  1.19     lukem #include <ufs/ufs/dinode.h>
     57   1.1  christos #include <ufs/ffs/fs.h>
     58   1.8    bouyer #include <ufs/ffs/ffs_extern.h>
     59   1.1  christos 
     60  1.29     joerg __dead static void usage(void);
     61  1.17     lukem static void getsblock(int, const char *, struct fs *);
     62  1.13     lukem static void fixinodes(int, struct fs *, struct disklabel *, int, long);
     63  1.15     lukem static void statussig(int);
     64   1.1  christos 
     65  1.20      fvdl int	needswap, ino, imax, is_ufs2;
     66  1.29     joerg static time_t	tstart;
     67  1.15     lukem 
     68   1.1  christos static void
     69  1.13     lukem usage(void)
     70   1.1  christos {
     71  1.12       cgd 
     72  1.15     lukem 	(void) fprintf(stderr,
     73  1.23      jmmv 	    "usage: %s [-F] [-p] [-x <constant>] <special>\n",
     74  1.12       cgd 	    getprogname());
     75   1.1  christos 	exit(1);
     76   1.1  christos }
     77   1.1  christos 
     78   1.1  christos 
     79  1.20      fvdl static const off_t sblock_try[] = SBLOCKSEARCH;
     80  1.20      fvdl 
     81  1.13     lukem /*
     82  1.13     lukem  * getsblock():
     83   1.1  christos  *	Return the superblock
     84   1.1  christos  */
     85   1.1  christos static void
     86  1.17     lukem getsblock(int fd, const char *name, struct fs *fs)
     87   1.1  christos {
     88  1.20      fvdl 	int i;
     89  1.20      fvdl 
     90  1.24       dsl 	for (i = 0; ; i++) {
     91  1.24       dsl 		if (sblock_try[i] == -1)
     92  1.24       dsl 			errx(1, "%s: can't find superblock", name);
     93  1.24       dsl 		if (pread(fd, fs, SBLOCKSIZE, sblock_try[i]) != SBLOCKSIZE)
     94  1.20      fvdl 			continue;
     95   1.1  christos 
     96  1.20      fvdl 		switch(fs->fs_magic) {
     97  1.20      fvdl 		case FS_UFS2_MAGIC:
     98  1.33       chs 		case FS_UFS2EA_MAGIC:
     99  1.20      fvdl 			is_ufs2 = 1;
    100  1.20      fvdl 			/* FALLTHROUGH */
    101  1.20      fvdl 		case FS_UFS1_MAGIC:
    102  1.24       dsl 			break;
    103  1.20      fvdl 		case FS_UFS2_MAGIC_SWAPPED:
    104  1.33       chs 		case FS_UFS2EA_MAGIC_SWAPPED:
    105  1.20      fvdl 			is_ufs2 = 1;
    106  1.20      fvdl 			/* FALLTHROUGH */
    107  1.20      fvdl 		case FS_UFS1_MAGIC_SWAPPED:
    108   1.8    bouyer 			needswap = 1;
    109  1.24       dsl 			ffs_sb_swap(fs, fs);
    110  1.24       dsl 			break;
    111  1.20      fvdl 		default:
    112  1.20      fvdl 			continue;
    113  1.20      fvdl 		}
    114  1.24       dsl 
    115  1.24       dsl 		if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
    116  1.24       dsl 			continue;
    117  1.24       dsl 		break;
    118   1.9      ross 	}
    119   1.1  christos 
    120   1.1  christos 	if (fs->fs_ncg < 1)
    121  1.17     lukem 		errx(1, "%s: bad ncg in superblock", name);
    122   1.1  christos 
    123  1.20      fvdl 	if (fs->fs_sbsize > SBLOCKSIZE)
    124  1.17     lukem 		errx(1, "%s: superblock too large", name);
    125   1.1  christos }
    126   1.1  christos 
    127  1.15     lukem 
    128  1.13     lukem /*
    129  1.13     lukem  * fixinodes():
    130   1.1  christos  *	Randomize the inode generation numbers
    131   1.1  christos  */
    132   1.1  christos static void
    133  1.13     lukem fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
    134   1.1  christos {
    135  1.30  dholland 	int inopb = FFS_INOPB(fs);
    136  1.20      fvdl 	int size;
    137  1.10   thorpej 	caddr_t buf;
    138  1.26     lukem 	struct ufs1_dinode *dp1 = NULL;
    139  1.26     lukem 	struct ufs2_dinode *dp2 = NULL;
    140  1.15     lukem 	int i;
    141   1.1  christos 
    142  1.20      fvdl 	size = is_ufs2 ? inopb * sizeof (struct ufs2_dinode) :
    143  1.20      fvdl 	    inopb * sizeof (struct ufs1_dinode);
    144  1.20      fvdl 
    145  1.10   thorpej 	if ((buf = malloc(size)) == NULL)
    146   1.1  christos 		err(1, "Out of memory");
    147   1.1  christos 
    148  1.20      fvdl 	if (is_ufs2)
    149  1.20      fvdl 		dp2 = (struct ufs2_dinode *)buf;
    150  1.20      fvdl 	else
    151  1.20      fvdl 		dp1 = (struct ufs1_dinode *)buf;
    152  1.20      fvdl 
    153   1.1  christos 	for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
    154   1.5   mycroft 		off_t sp;
    155  1.31  dholland 		sp = (off_t) FFS_FSBTODB(fs, ino_to_fsba(fs, ino)) *
    156   1.5   mycroft 		     (off_t) lab->d_secsize;
    157   1.1  christos 
    158   1.1  christos 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    159   1.1  christos 			err(1, "Seeking to inode %d failed", ino);
    160   1.1  christos 
    161  1.10   thorpej 		if (read(fd, buf, size) != size)
    162   1.1  christos 			err(1, "Reading inodes %d+%d failed", ino, inopb);
    163   1.1  christos 
    164  1.10   thorpej 		for (i = 0; i < inopb; i++) {
    165  1.20      fvdl 			if (is_ufs2) {
    166  1.20      fvdl 				if (pflag)
    167  1.20      fvdl 					printf("inode %10d   gen 0x%08x\n",
    168  1.20      fvdl 					    ino,
    169  1.20      fvdl 					    ufs_rw32(dp2[i].di_gen, needswap));
    170  1.20      fvdl 				else
    171  1.20      fvdl 					dp2[i].di_gen =
    172  1.22    itojun 					    ufs_rw32((arc4random() & INT32_MAX)^ xorval,
    173  1.20      fvdl 						needswap);
    174  1.20      fvdl 			} else {
    175  1.20      fvdl 				if (pflag)
    176  1.20      fvdl 					printf("inode %10d   gen 0x%08x\n",
    177  1.20      fvdl 					    ino,
    178  1.20      fvdl 					    ufs_rw32(dp1[i].di_gen, needswap));
    179  1.20      fvdl 				else
    180  1.20      fvdl 					dp1[i].di_gen =
    181  1.22    itojun 					    ufs_rw32((arc4random() & INT32_MAX) ^ xorval,
    182  1.20      fvdl 						needswap);
    183  1.20      fvdl 			}
    184   1.1  christos 			if (++ino > imax)
    185   1.1  christos 				errx(1, "Exceeded number of inodes");
    186   1.1  christos 		}
    187   1.1  christos 
    188   1.1  christos 		if (pflag)
    189   1.1  christos 			continue;
    190   1.1  christos 
    191   1.1  christos 		if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
    192   1.1  christos 			err(1, "Seeking to inode %d failed", ino);
    193   1.1  christos 
    194  1.10   thorpej 		if (write(fd, buf, size) != size)
    195   1.1  christos 			err(1, "Writing inodes %d+%d failed", ino, inopb);
    196   1.1  christos 	}
    197  1.10   thorpej 	free(buf);
    198   1.1  christos }
    199   1.1  christos 
    200  1.15     lukem /*
    201  1.15     lukem  * statussig():
    202  1.15     lukem  *	display current status
    203  1.15     lukem  */
    204  1.29     joerg static void
    205  1.15     lukem statussig(int dummy)
    206  1.15     lukem {
    207  1.15     lukem 	char	msgbuf[256];
    208  1.15     lukem 	int	len, deltat;
    209  1.32  christos 	time_t	tnow;
    210  1.15     lukem 
    211  1.15     lukem 	(void)time(&tnow);
    212  1.15     lukem 	len = snprintf(msgbuf, sizeof(msgbuf),
    213  1.15     lukem 	    "fsirand: completed inode %d of %d (%3.2f%%)",
    214  1.15     lukem 	    ino, imax, (ino * 100.0) / imax);
    215  1.15     lukem 	if (imax - ino) {
    216  1.15     lukem 		deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
    217  1.15     lukem 		len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
    218  1.15     lukem 		    ", finished in %d:%02d\n", deltat / 60, deltat % 60);
    219  1.15     lukem 	} else {
    220  1.15     lukem 		len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
    221  1.15     lukem 	}
    222  1.15     lukem 	write(STDERR_FILENO, msgbuf, len);
    223  1.15     lukem }
    224  1.15     lukem 
    225   1.1  christos int
    226  1.13     lukem main(int argc, char *argv[])
    227   1.1  christos {
    228  1.17     lukem 	const char *special;
    229  1.20      fvdl 	char buf[SBLOCKSIZE], device[MAXPATHLEN];
    230   1.1  christos 	struct fs *fs = (struct fs *) buf;
    231   1.1  christos 	struct disklabel lab;
    232  1.17     lukem 	long xorval;
    233   1.1  christos 	char *ep;
    234  1.17     lukem 	int fd, c, Fflag, pflag, openflags;
    235   1.1  christos 
    236  1.17     lukem 	xorval = 0;
    237  1.15     lukem 	Fflag = pflag = 0;
    238   1.1  christos 
    239  1.15     lukem 	while ((c = getopt(argc, argv, "Fpx:")) != -1)
    240   1.1  christos 		switch (c) {
    241  1.15     lukem 		case 'F':
    242  1.15     lukem 			Fflag++;
    243  1.15     lukem 			break;
    244   1.1  christos 		case 'p':
    245   1.1  christos 			pflag++;
    246   1.1  christos 			break;
    247   1.1  christos 		case 'x':
    248  1.21     lukem 			errno = 0;
    249   1.1  christos 			xorval = strtol(optarg, &ep, 0);
    250   1.4    kleink 			if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
    251   1.1  christos 			    errno == ERANGE)
    252   1.1  christos 				err(1, "Out of range constant");
    253   1.1  christos 			if (*ep)
    254   1.1  christos 				errx(1, "Bad constant");
    255   1.1  christos 			break;
    256   1.1  christos 		default:
    257   1.1  christos 			usage();
    258   1.1  christos 		}
    259   1.1  christos 
    260   1.1  christos 	argv += optind;
    261   1.1  christos 	argc -= optind;
    262   1.1  christos 
    263   1.1  christos 	if (argc != 1)
    264   1.1  christos 		usage();
    265   1.1  christos 
    266  1.17     lukem 	special = argv[0];
    267  1.17     lukem 	openflags = pflag ? O_RDONLY : O_RDWR;
    268  1.17     lukem 	if (Fflag)
    269  1.17     lukem 		fd = open(special, openflags);
    270  1.17     lukem 	else {
    271  1.17     lukem 		fd = opendisk(special, openflags, device, sizeof(device), 0);
    272  1.17     lukem 		special = device;
    273  1.17     lukem 	}
    274  1.17     lukem 	if (fd == -1)
    275  1.17     lukem 		err(1, "Cannot open `%s'", special);
    276   1.1  christos 
    277  1.15     lukem 	if (Fflag) {
    278  1.15     lukem 		memset(&lab, 0, sizeof(lab));
    279  1.15     lukem 		lab.d_secsize = DEV_BSIZE;	/* XXX */
    280  1.17     lukem 	} else {
    281  1.15     lukem 		if (ioctl(fd, DIOCGDINFO, &lab) == -1)
    282  1.17     lukem 			err(1, "%s: cannot get disklabel information", special);
    283  1.17     lukem 	}
    284  1.15     lukem 
    285  1.15     lukem 	time(&tstart);
    286  1.15     lukem 	(void)signal(SIGINFO, statussig);
    287  1.17     lukem 	getsblock(fd, special, fs);
    288   1.1  christos 	fixinodes(fd, fs, &lab, pflag, xorval);
    289   1.1  christos 
    290   1.1  christos 	(void) close(fd);
    291   1.1  christos 	return 0;
    292   1.1  christos }
    293