2013-10-19 05:09:58 +04:00
|
|
|
/* $NetBSD: fsirand.c,v 1.32 2013/10/19 01:09:58 christos Exp $ */
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2001-02-04 23:04:02 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
1997-03-07 23:16:39 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2001-02-04 23:04:02 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1997-03-07 23:16:39 +03:00
|
|
|
*/
|
|
|
|
|
1997-09-14 18:58:53 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
#ifndef lint
|
2013-10-19 05:09:58 +04:00
|
|
|
__RCSID("$NetBSD: fsirand.c,v 1.32 2013/10/19 01:09:58 christos Exp $");
|
1997-03-07 23:16:39 +03:00
|
|
|
#endif /* lint */
|
|
|
|
|
2001-11-16 07:25:42 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
1997-03-07 23:16:39 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2001-08-19 18:59:39 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
#include <stdlib.h>
|
2001-08-19 18:59:39 +04:00
|
|
|
#include <string.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
#include <unistd.h>
|
2001-11-16 07:25:42 +03:00
|
|
|
#include <util.h>
|
2007-02-09 00:36:58 +03:00
|
|
|
#include <signal.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
|
1998-03-18 20:07:14 +03:00
|
|
|
#include <ufs/ufs/ufs_bswap.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2002-01-08 08:01:50 +03:00
|
|
|
#include <ufs/ufs/dinode.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
#include <ufs/ffs/fs.h>
|
1998-03-18 20:07:14 +03:00
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2011-08-27 21:36:05 +04:00
|
|
|
__dead static void usage(void);
|
2001-11-16 07:25:42 +03:00
|
|
|
static void getsblock(int, const char *, struct fs *);
|
2001-07-29 15:15:29 +04:00
|
|
|
static void fixinodes(int, struct fs *, struct disklabel *, int, long);
|
2001-08-19 18:59:39 +04:00
|
|
|
static void statussig(int);
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
int needswap, ino, imax, is_ufs2;
|
2011-08-27 21:36:05 +04:00
|
|
|
static time_t tstart;
|
2001-08-19 18:59:39 +04:00
|
|
|
|
1997-03-07 23:16:39 +03:00
|
|
|
static void
|
2001-07-29 15:15:29 +04:00
|
|
|
usage(void)
|
1997-03-07 23:16:39 +03:00
|
|
|
{
|
2001-02-20 01:48:57 +03:00
|
|
|
|
2001-08-19 18:59:39 +04:00
|
|
|
(void) fprintf(stderr,
|
2004-01-06 02:23:32 +03:00
|
|
|
"usage: %s [-F] [-p] [-x <constant>] <special>\n",
|
2001-02-20 01:48:57 +03:00
|
|
|
getprogname());
|
1997-03-07 23:16:39 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
static const off_t sblock_try[] = SBLOCKSEARCH;
|
|
|
|
|
2001-07-29 15:15:29 +04:00
|
|
|
/*
|
|
|
|
* getsblock():
|
1997-03-07 23:16:39 +03:00
|
|
|
* Return the superblock
|
|
|
|
*/
|
|
|
|
static void
|
2001-11-16 07:25:42 +03:00
|
|
|
getsblock(int fd, const char *name, struct fs *fs)
|
1997-03-07 23:16:39 +03:00
|
|
|
{
|
2003-04-02 14:39:19 +04:00
|
|
|
int i;
|
|
|
|
|
2004-03-21 23:12:16 +03:00
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (sblock_try[i] == -1)
|
|
|
|
errx(1, "%s: can't find superblock", name);
|
|
|
|
if (pread(fd, fs, SBLOCKSIZE, sblock_try[i]) != SBLOCKSIZE)
|
2003-04-02 14:39:19 +04:00
|
|
|
continue;
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
switch(fs->fs_magic) {
|
|
|
|
case FS_UFS2_MAGIC:
|
|
|
|
is_ufs2 = 1;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FS_UFS1_MAGIC:
|
2004-03-21 23:12:16 +03:00
|
|
|
break;
|
2003-04-02 14:39:19 +04:00
|
|
|
case FS_UFS2_MAGIC_SWAPPED:
|
|
|
|
is_ufs2 = 1;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case FS_UFS1_MAGIC_SWAPPED:
|
1998-03-18 20:07:14 +03:00
|
|
|
needswap = 1;
|
2004-03-21 23:12:16 +03:00
|
|
|
ffs_sb_swap(fs, fs);
|
|
|
|
break;
|
2003-04-02 14:39:19 +04:00
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2004-03-21 23:12:16 +03:00
|
|
|
if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
2003-04-02 14:39:19 +04:00
|
|
|
|
1997-03-07 23:16:39 +03:00
|
|
|
if (fs->fs_ncg < 1)
|
2001-11-16 07:25:42 +03:00
|
|
|
errx(1, "%s: bad ncg in superblock", name);
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
if (fs->fs_sbsize > SBLOCKSIZE)
|
2001-11-16 07:25:42 +03:00
|
|
|
errx(1, "%s: superblock too large", name);
|
1997-03-07 23:16:39 +03:00
|
|
|
}
|
|
|
|
|
2001-08-19 18:59:39 +04:00
|
|
|
|
2001-07-29 15:15:29 +04:00
|
|
|
/*
|
|
|
|
* fixinodes():
|
1997-03-07 23:16:39 +03:00
|
|
|
* Randomize the inode generation numbers
|
|
|
|
*/
|
|
|
|
static void
|
2001-07-29 15:15:29 +04:00
|
|
|
fixinodes(int fd, struct fs *fs, struct disklabel *lab, int pflag, long xorval)
|
1997-03-07 23:16:39 +03:00
|
|
|
{
|
Rename ambiguous macros:
MAXDIRSIZE -> UFS_MAXDIRSIZE or LFS_MAXDIRSIZE
NINDIR -> FFS_NINDIR, EXT2_NINDIR, LFS_NINDIR, or MFS_NINDIR
INOPB -> FFS_INOPB, LFS_INOPB
INOPF -> FFS_INOPF, LFS_INOPF
blksize -> ffs_blksize, ext2_blksize, or lfs_blksize
sblksize -> ffs_blksize
These are not the only ambiguously defined filesystem macros, of
course, there's a pile more. I may not have found all the ambiguous
definitions of blksize(), too, as there are a lot of other things
called 'blksize' in the system.
2013-06-19 21:51:25 +04:00
|
|
|
int inopb = FFS_INOPB(fs);
|
2003-04-02 14:39:19 +04:00
|
|
|
int size;
|
1998-10-23 05:27:51 +04:00
|
|
|
caddr_t buf;
|
2005-06-02 04:01:47 +04:00
|
|
|
struct ufs1_dinode *dp1 = NULL;
|
|
|
|
struct ufs2_dinode *dp2 = NULL;
|
2001-08-19 18:59:39 +04:00
|
|
|
int i;
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
size = is_ufs2 ? inopb * sizeof (struct ufs2_dinode) :
|
|
|
|
inopb * sizeof (struct ufs1_dinode);
|
|
|
|
|
1998-10-23 05:27:51 +04:00
|
|
|
if ((buf = malloc(size)) == NULL)
|
1997-03-07 23:16:39 +03:00
|
|
|
err(1, "Out of memory");
|
|
|
|
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2)
|
|
|
|
dp2 = (struct ufs2_dinode *)buf;
|
|
|
|
else
|
|
|
|
dp1 = (struct ufs1_dinode *)buf;
|
|
|
|
|
1997-03-07 23:16:39 +03:00
|
|
|
for (ino = 0, imax = fs->fs_ipg * fs->fs_ncg; ino < imax;) {
|
1997-07-06 20:19:40 +04:00
|
|
|
off_t sp;
|
2013-06-23 06:06:04 +04:00
|
|
|
sp = (off_t) FFS_FSBTODB(fs, ino_to_fsba(fs, ino)) *
|
1997-07-06 20:19:40 +04:00
|
|
|
(off_t) lab->d_secsize;
|
1997-03-07 23:16:39 +03:00
|
|
|
|
|
|
|
if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
|
|
|
|
err(1, "Seeking to inode %d failed", ino);
|
|
|
|
|
1998-10-23 05:27:51 +04:00
|
|
|
if (read(fd, buf, size) != size)
|
1997-03-07 23:16:39 +03:00
|
|
|
err(1, "Reading inodes %d+%d failed", ino, inopb);
|
|
|
|
|
1998-10-23 05:27:51 +04:00
|
|
|
for (i = 0; i < inopb; i++) {
|
2003-04-02 14:39:19 +04:00
|
|
|
if (is_ufs2) {
|
|
|
|
if (pflag)
|
|
|
|
printf("inode %10d gen 0x%08x\n",
|
|
|
|
ino,
|
|
|
|
ufs_rw32(dp2[i].di_gen, needswap));
|
|
|
|
else
|
|
|
|
dp2[i].di_gen =
|
2003-09-06 16:42:00 +04:00
|
|
|
ufs_rw32((arc4random() & INT32_MAX)^ xorval,
|
2003-04-02 14:39:19 +04:00
|
|
|
needswap);
|
|
|
|
} else {
|
|
|
|
if (pflag)
|
|
|
|
printf("inode %10d gen 0x%08x\n",
|
|
|
|
ino,
|
|
|
|
ufs_rw32(dp1[i].di_gen, needswap));
|
|
|
|
else
|
|
|
|
dp1[i].di_gen =
|
2003-09-06 16:42:00 +04:00
|
|
|
ufs_rw32((arc4random() & INT32_MAX) ^ xorval,
|
2003-04-02 14:39:19 +04:00
|
|
|
needswap);
|
|
|
|
}
|
1997-03-07 23:16:39 +03:00
|
|
|
if (++ino > imax)
|
|
|
|
errx(1, "Exceeded number of inodes");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pflag)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (lseek(fd, sp, SEEK_SET) == (off_t) -1)
|
|
|
|
err(1, "Seeking to inode %d failed", ino);
|
|
|
|
|
1998-10-23 05:27:51 +04:00
|
|
|
if (write(fd, buf, size) != size)
|
1997-03-07 23:16:39 +03:00
|
|
|
err(1, "Writing inodes %d+%d failed", ino, inopb);
|
|
|
|
}
|
1998-10-23 05:27:51 +04:00
|
|
|
free(buf);
|
1997-03-07 23:16:39 +03:00
|
|
|
}
|
|
|
|
|
2001-08-19 18:59:39 +04:00
|
|
|
/*
|
|
|
|
* statussig():
|
|
|
|
* display current status
|
|
|
|
*/
|
2011-08-27 21:36:05 +04:00
|
|
|
static void
|
2001-08-19 18:59:39 +04:00
|
|
|
statussig(int dummy)
|
|
|
|
{
|
|
|
|
char msgbuf[256];
|
|
|
|
int len, deltat;
|
2013-10-19 05:09:58 +04:00
|
|
|
time_t tnow;
|
2001-08-19 18:59:39 +04:00
|
|
|
|
|
|
|
(void)time(&tnow);
|
|
|
|
len = snprintf(msgbuf, sizeof(msgbuf),
|
|
|
|
"fsirand: completed inode %d of %d (%3.2f%%)",
|
|
|
|
ino, imax, (ino * 100.0) / imax);
|
|
|
|
if (imax - ino) {
|
|
|
|
deltat = tstart - tnow + (1.0 * (tnow - tstart)) / ino * imax;
|
|
|
|
len += snprintf(msgbuf + len, sizeof(msgbuf) - len,
|
|
|
|
", finished in %d:%02d\n", deltat / 60, deltat % 60);
|
|
|
|
} else {
|
|
|
|
len += snprintf(msgbuf + len, sizeof(msgbuf) - len, "\n");
|
|
|
|
}
|
|
|
|
write(STDERR_FILENO, msgbuf, len);
|
|
|
|
}
|
|
|
|
|
1997-03-07 23:16:39 +03:00
|
|
|
int
|
2001-07-29 15:15:29 +04:00
|
|
|
main(int argc, char *argv[])
|
1997-03-07 23:16:39 +03:00
|
|
|
{
|
2001-11-16 07:25:42 +03:00
|
|
|
const char *special;
|
2003-04-02 14:39:19 +04:00
|
|
|
char buf[SBLOCKSIZE], device[MAXPATHLEN];
|
1997-03-07 23:16:39 +03:00
|
|
|
struct fs *fs = (struct fs *) buf;
|
|
|
|
struct disklabel lab;
|
2001-11-16 07:25:42 +03:00
|
|
|
long xorval;
|
1997-03-07 23:16:39 +03:00
|
|
|
char *ep;
|
2001-11-16 07:25:42 +03:00
|
|
|
int fd, c, Fflag, pflag, openflags;
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2001-11-16 07:25:42 +03:00
|
|
|
xorval = 0;
|
2001-08-19 18:59:39 +04:00
|
|
|
Fflag = pflag = 0;
|
1997-03-07 23:16:39 +03:00
|
|
|
|
2001-08-19 18:59:39 +04:00
|
|
|
while ((c = getopt(argc, argv, "Fpx:")) != -1)
|
1997-03-07 23:16:39 +03:00
|
|
|
switch (c) {
|
2001-08-19 18:59:39 +04:00
|
|
|
case 'F':
|
|
|
|
Fflag++;
|
|
|
|
break;
|
1997-03-07 23:16:39 +03:00
|
|
|
case 'p':
|
|
|
|
pflag++;
|
|
|
|
break;
|
|
|
|
case 'x':
|
2003-04-17 08:01:56 +04:00
|
|
|
errno = 0;
|
1997-03-07 23:16:39 +03:00
|
|
|
xorval = strtol(optarg, &ep, 0);
|
1997-06-24 21:47:03 +04:00
|
|
|
if ((xorval == LONG_MIN || xorval == LONG_MAX) &&
|
1997-03-07 23:16:39 +03:00
|
|
|
errno == ERANGE)
|
|
|
|
err(1, "Out of range constant");
|
|
|
|
if (*ep)
|
|
|
|
errx(1, "Bad constant");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
|
|
|
if (argc != 1)
|
|
|
|
usage();
|
|
|
|
|
2001-11-16 07:25:42 +03:00
|
|
|
special = argv[0];
|
|
|
|
openflags = pflag ? O_RDONLY : O_RDWR;
|
|
|
|
if (Fflag)
|
|
|
|
fd = open(special, openflags);
|
|
|
|
else {
|
|
|
|
fd = opendisk(special, openflags, device, sizeof(device), 0);
|
|
|
|
special = device;
|
|
|
|
}
|
|
|
|
if (fd == -1)
|
|
|
|
err(1, "Cannot open `%s'", special);
|
2001-08-19 18:59:39 +04:00
|
|
|
|
|
|
|
if (Fflag) {
|
|
|
|
memset(&lab, 0, sizeof(lab));
|
|
|
|
lab.d_secsize = DEV_BSIZE; /* XXX */
|
2001-11-16 07:25:42 +03:00
|
|
|
} else {
|
2001-08-19 18:59:39 +04:00
|
|
|
if (ioctl(fd, DIOCGDINFO, &lab) == -1)
|
2001-11-16 07:25:42 +03:00
|
|
|
err(1, "%s: cannot get disklabel information", special);
|
|
|
|
}
|
2001-08-19 18:59:39 +04:00
|
|
|
|
|
|
|
time(&tstart);
|
|
|
|
(void)signal(SIGINFO, statussig);
|
2001-11-16 07:25:42 +03:00
|
|
|
getsblock(fd, special, fs);
|
1997-03-07 23:16:39 +03:00
|
|
|
fixinodes(fd, fs, &lab, pflag, xorval);
|
|
|
|
|
|
|
|
(void) close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|