-/* $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>
*/
#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>
#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;
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;
{
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)
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++) {
/* 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);
}