summaryrefslogtreecommitdiff
path: root/sbin/fsirand
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1997-02-09 01:10:18 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1997-02-09 01:10:18 +0000
commitbdc8567913dc0824fb1f8864eb0612fcc849d21f (patch)
tree1c379edad31fe091cbb6400e0406557ac0877619 /sbin/fsirand
parent3e1d430c8b77edc6b34a1a98eaa82a65f9ad83d1 (diff)
Get block size from disklabel.
Adds support for setting fs_id for when when we have that in struct fs.
Diffstat (limited to 'sbin/fsirand')
-rw-r--r--sbin/fsirand/fsirand.86
-rw-r--r--sbin/fsirand/fsirand.c187
2 files changed, 150 insertions, 43 deletions
diff --git a/sbin/fsirand/fsirand.8 b/sbin/fsirand/fsirand.8
index 1fd4b39d1d8..cc8b7257b7a 100644
--- a/sbin/fsirand/fsirand.8
+++ b/sbin/fsirand/fsirand.8
@@ -26,7 +26,7 @@
.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.\" $OpenBSD: fsirand.8,v 1.4 1997/02/04 04:44:16 millert Exp $
+.\" $OpenBSD: fsirand.8,v 1.5 1997/02/09 01:10:16 millert Exp $
.\"
.Dd January 25, 1997
.Dt FSIRAND 8
@@ -36,6 +36,7 @@
.Nd randomize inode generation numbers
.Sh SYNOPSIS
.Nm fsirand
+.Op Fl b
.Op Fl f
.Op Fl p
.Ar special
@@ -60,6 +61,9 @@ but the system should be rebooted via ``reboot -n'' afterwards.
.Sh OPTIONS
.Bl -tag -width indent
The available options are as follows:
+.It Fl b
+Use the default block size (usuallyt 512 bytes) instead
+of the value gleaned from the disklabel.
.It Fl f
Force
.Nm
diff --git a/sbin/fsirand/fsirand.c b/sbin/fsirand/fsirand.c
index 9e5d9af6057..e102f2c5433 100644
--- a/sbin/fsirand/fsirand.c
+++ b/sbin/fsirand/fsirand.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsirand.c,v 1.4 1997/01/28 04:21:25 millert Exp $ */
+/* $OpenBSD: fsirand.c,v 1.5 1997/02/09 01:10:17 millert Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -31,11 +31,14 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: fsirand.c,v 1.4 1997/01/28 04:21:25 millert Exp $";
+static char rcsid[] = "$OpenBSD: fsirand.c,v 1.5 1997/02/09 01:10:17 millert Exp $";
#endif /* not lint */
-#include <sys/param.h>
#include <sys/types.h>
+#include <sys/disklabel.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#include <sys/time.h>
#include <ufs/ffs/fs.h>
#include <ufs/ufs/dinode.h>
@@ -50,21 +53,24 @@ static char rcsid[] = "$OpenBSD: fsirand.c,v 1.4 1997/01/28 04:21:25 millert Exp
#include <util.h>
void usage __P((int));
-void fsirand __P((char *));
+int fsirand __P((char *));
extern char *__progname;
-int printonly = 0, force = 0;
+int printonly = 0, force = 0, ignorelabel = 0;
int
main(argc, argv)
int argc;
char *argv[];
{
- int n;
+ int n, ex = 0;
- while ((n = getopt(argc, argv, "fp")) != -1) {
+ while ((n = getopt(argc, argv, "bfp")) != -1) {
switch (n) {
+ case 'b':
+ ignorelabel++;
+ break;
case 'p':
printonly++;
break;
@@ -81,15 +87,15 @@ main(argc, argv)
for (n = optind; n < argc; n++) {
if (argc - optind != 1)
(void)puts(argv[n]);
- fsirand(argv[n]);
+ ex += fsirand(argv[n]);
if (n < argc - 1)
putchar('\n');
}
- exit(0);
+ exit(ex);
}
-void
+int
fsirand(device)
char *device;
{
@@ -98,34 +104,87 @@ fsirand(device)
size_t ibufsize;
struct fs *sblock;
ino_t inumber, maxino;
- static daddr_t dblk;
- char sbuf[SBSIZE];
- int devfd, n;
+ daddr_t dblk;
+ char sbuf[SBSIZE], sbuftmp[SBSIZE];
+ int devfd, n, cg;
char *devpath;
+ u_int32_t bsize = DEV_BSIZE;
+ struct disklabel label;
- /* Open device and read in superblock */
if ((devfd = opendev(device, printonly ? O_RDONLY : O_RDWR,
- OPENDEV_PART, &devpath)) < 0)
- err(1, "Can't open %s", devpath);
+ OPENDEV_PART, &devpath)) < 0) {
+ warn("Can't open %s", devpath);
+ return (1);
+ }
+
+ /* Get block size (usually 512) from disklabel if possible */
+ if (!ignorelabel) {
+ if (ioctl(devfd, DIOCGDINFO, &label) < 0)
+ warn("Can't read disklabel, using sector size of %d",
+ bsize);
+ else
+ bsize = label.d_secsize;
+ }
+ /* Read in master superblock */
(void)memset(&sbuf, 0, sizeof(sbuf));
sblock = (struct fs *)&sbuf;
- if (lseek(devfd, SBOFF, SEEK_SET) == -1)
- err(1, "Can't seek to superblock (%qd) on %s", SBOFF, devpath);
- if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE)
- err(1, "Can't read superblock on %s: %s", devpath,
+ if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
+ warn("Can't seek to superblock (%qd) on %s", SBOFF, devpath);
+ return (1);
+ }
+ if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
+ warnx("Can't read superblock on %s: %s", devpath,
(n < SBSIZE) ? "short read" : strerror(errno));
+ return (1);
+ }
+ maxino = sblock->fs_ncg * sblock->fs_ipg;
/* Simple sanity checks on the superblock */
- if (sblock->fs_magic != FS_MAGIC)
- errx(1, "Wrong magic number in superblock");
- if (sblock->fs_sbsize > SBSIZE)
- errx(1, "Superblock size is preposterous");
- if (!force && !printonly && sblock->fs_clean != FS_ISCLEAN)
- errx(1, "Filesystem is not clean, fsck %s before running %s\n",
- devpath, __progname);
+ if (sblock->fs_magic != FS_MAGIC) {
+ warnx("Bad magic number in superblock");
+ return (1);
+ }
+ if (sblock->fs_sbsize > SBSIZE) {
+ warnx("Superblock size is preposterous");
+ return (1);
+ }
+ if (sblock->fs_postblformat == FS_42POSTBLFMT) {
+ warnx("Filesystem format is too old, sorry");
+ return (1);
+ }
+ if (!force && !printonly && sblock->fs_clean != FS_ISCLEAN) {
+ warnx("Filesystem is not clean, fsck %s first.", devpath);
+ return (1);
+ }
- maxino = sblock->fs_ncg * sblock->fs_ipg;
+ /* Make sure backup superblocks are sane. */
+ sblock = (struct fs *)&sbuftmp;
+ for (cg = 0; cg < sblock->fs_ncg; cg++) {
+ dblk = fsbtodb(sblock, cgsblock(sblock, cg));
+ if (lseek(devfd, (off_t)(dblk * bsize), SEEK_SET) < 0) {
+ warn("Can't seek to %qd", (off_t)(dblk * bsize));
+ return (1);
+ } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
+ warn("Can't read backup superblock %d on %s: %s",
+ cg + 1, devpath, (n < SBSIZE) ? "short write"
+ : strerror(errno));
+ return (1);
+ }
+ if (sblock->fs_magic != FS_MAGIC) {
+ warnx("Bad magic number in backup superblock %d on %s",
+ cg + 1, devpath);
+ return (1);
+ }
+ if (sblock->fs_sbsize > SBSIZE) {
+ warnx("Size of backup superblock %d on %s is preposterous",
+ cg + 1, devpath);
+ return (1);
+ }
+ }
+ sblock = (struct fs *)&sbuf;
+
+ /* XXX - should really cap buffer at 512kb or so */
ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
if (oldibufsize < ibufsize) {
if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
@@ -133,18 +192,61 @@ fsirand(device)
oldibufsize = ibufsize;
}
- /* Randomize inodes a cylinder group at a time */
- for (inumber = 0; inumber < maxino;) {
+#ifdef HAVE_FS_ID
+ if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
+ if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
+ (void)printf("%s was randomized on %s", devpath,
+ ctime((const time_t *)&(sblock->fs_id[0])));
+ (void)printf("fsid: %x %x\n", sblock->fs_id[0],
+ sblock->fs_id[1]);
+ }
+
+ /* Randomize fs_id unless old 4.2BSD filesystem */
+ if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
+ /* Randomize fs_id and write out new sblock and backups */
+ sblock->fs_id[0] = (u_int32_t)time(NULL);
+ sblock->fs_id[1] = arc4random();
+
+ if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
+ warn("Can't seek to superblock (%qd) on %s", SBOFF,
+ devpath);
+ return (1);
+ }
+ if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
+ warn("Can't read superblock on %s: %s", devpath,
+ (n < SBSIZE) ? "short write" : strerror(errno));
+ return (1);
+ }
+ }
+#endif
+
+ /* For each cylinder group, randomize inodes and update backup sblock */
+ for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
+#ifdef HAVE_FS_ID
+ /* Update superblock if appropriate */
+ if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
+ dblk = fsbtodb(sblock, cgsblock(sblock, cg));
+ if (lseek(devfd, (off_t)(dblk * bsize), SEEK_SET) < 0) {
+ warn("Can't seek to %qd", (off_t)(dblk * bsize));
+ return (1);
+ } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
+ warn("Can't read backup superblock %d on %s: %s",
+ cg + 1, devpath, (n < SBSIZE) ? "short write"
+ : strerror(errno));
+ return (1);
+ }
+ }
+#endif
+
/* Read in inodes, then print or randomize generation nums */
dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
- /* XXX - don't use DEV_BSIZE, get value from disklabel! */
- if (lseek(devfd, (off_t)(dblk * DEV_BSIZE), SEEK_SET) < 0) {
- warn("Can't seek to %qd", (off_t)(dblk * DEV_BSIZE));
- return;
+ if (lseek(devfd, (off_t)(dblk * bsize), SEEK_SET) < 0) {
+ warn("Can't seek to %qd", (off_t)(dblk * bsize));
+ return (1);
} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
warnx("Can't read inodes: %s",
(n < ibufsize) ? "short read" : strerror(errno));
- return;
+ return (1);
}
for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
@@ -159,28 +261,29 @@ fsirand(device)
/* Write out modified inodes */
if (!printonly) {
- /* XXX - don't use DEV_BSIZE, get from disklabel! */
- if (lseek(devfd, (off_t)(dblk * DEV_BSIZE), SEEK_SET) < 0) {
+ if (lseek(devfd, (off_t)(dblk * bsize), SEEK_SET) < 0) {
warn("Can't seek to %qd",
- (off_t)(dblk * DEV_BSIZE));
- return;
+ (off_t)(dblk * bsize));
+ return (1);
} else if ((n = write(devfd, inodebuf, ibufsize)) !=
ibufsize) {
- errx(1, "Can't write inodes: %s",
+ warnx("Can't write inodes: %s",
(n != ibufsize) ? "short write" :
strerror(errno));
- return;
+ return (1);
}
}
}
(void)close(devfd);
+
+ return(0);
}
void
usage(ex)
int ex;
{
- (void)fprintf(stderr, "Usage: %s [ -p ] special [special ...]\n",
+ (void)fprintf(stderr, "Usage: %s [ -b ] [ -f ] [ -p ] special [special ...]\n",
__progname);
exit(ex);
}