fsirand.c revision 26625
124149Sguido/* $OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $ */ 224149Sguido 324149Sguido/* 424149Sguido * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 524149Sguido * All rights reserved. 624149Sguido * 724149Sguido * Redistribution and use in source and binary forms, with or without 824149Sguido * modification, are permitted provided that the following conditions 924149Sguido * are met: 1024149Sguido * 1. Redistributions of source code must retain the above copyright 1124149Sguido * notice, this list of conditions and the following disclaimer. 1224149Sguido * 2. Redistributions in binary form must reproduce the above copyright 1324149Sguido * notice, this list of conditions and the following disclaimer in the 1424149Sguido * documentation and/or other materials provided with the distribution. 1524149Sguido * 3. All advertising materials mentioning features or use of this software 1624149Sguido * must display the following acknowledgement: 1724149Sguido * This product includes software developed by Todd C. Miller. 1824149Sguido * 4. The name of the author may not be used to endorse or promote products 1924149Sguido * derived from this software without specific prior written permission. 2024149Sguido * 2124149Sguido * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 2224149Sguido * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2324149Sguido * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2424149Sguido * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2524149Sguido * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2624149Sguido * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2724149Sguido * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2824149Sguido * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 2924149Sguido * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3024149Sguido * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3124149Sguido */ 3224149Sguido 3324149Sguido#ifndef lint 3424149Sguidostatic char rcsid[] = "$OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $"; 3524149Sguido#endif /* not lint */ 3624149Sguido 3724149Sguido#include <sys/types.h> 3824149Sguido#include <sys/disklabel.h> 3924149Sguido#include <sys/ioctl.h> 4024149Sguido#include <sys/param.h> 4124149Sguido#include <sys/time.h> 4224149Sguido#include <sys/resource.h> 4324149Sguido 4424149Sguido#include <ufs/ffs/fs.h> 4524149Sguido#include <ufs/ufs/dinode.h> 4624149Sguido 4724149Sguido#include <err.h> 4824149Sguido#include <errno.h> 4924149Sguido#include <fcntl.h> 5024149Sguido#include <stdio.h> 5124149Sguido#include <stdlib.h> 5224149Sguido#include <string.h> 5324216Sache#include <time.h> 5424149Sguido#include <unistd.h> 5524149Sguido 5626558Scharnierstatic void usage __P((int)); 5724149Sguidoint fsirand __P((char *)); 5824149Sguido 5924149Sguidoint printonly = 0, force = 0, ignorelabel = 0; 6024149Sguido 6124149Sguidoint 6224149Sguidomain(argc, argv) 6324149Sguido int argc; 6424149Sguido char *argv[]; 6524149Sguido{ 6624149Sguido int n, ex = 0; 6724149Sguido struct rlimit rl; 6824149Sguido 6924149Sguido while ((n = getopt(argc, argv, "bfp")) != -1) { 7024149Sguido switch (n) { 7124149Sguido case 'b': 7224149Sguido ignorelabel++; 7324149Sguido break; 7424149Sguido case 'p': 7524149Sguido printonly++; 7624149Sguido break; 7724149Sguido case 'f': 7824149Sguido force++; 7924149Sguido break; 8024149Sguido default: 8124149Sguido usage(1); 8224149Sguido } 8324149Sguido } 8424149Sguido if (argc - optind < 1) 8524149Sguido usage(1); 8624149Sguido 8726625Sache srandomdev(); 8824216Sache 8924149Sguido /* Increase our data size to the max */ 9024149Sguido if (getrlimit(RLIMIT_DATA, &rl) == 0) { 9124149Sguido rl.rlim_cur = rl.rlim_max; 9224149Sguido if (setrlimit(RLIMIT_DATA, &rl) < 0) 9324149Sguido warn("Can't get resource limit to max data size"); 9424149Sguido } else 9524149Sguido warn("Can't get resource limit for data size"); 9624149Sguido 9724149Sguido for (n = optind; n < argc; n++) { 9824149Sguido if (argc - optind != 1) 9924149Sguido (void)puts(argv[n]); 10024149Sguido ex += fsirand(argv[n]); 10124149Sguido if (n < argc - 1) 10224149Sguido putchar('\n'); 10324149Sguido } 10424149Sguido 10524149Sguido exit(ex); 10624149Sguido} 10724149Sguido 10824149Sguidoint 10924149Sguidofsirand(device) 11024149Sguido char *device; 11124149Sguido{ 11224149Sguido static struct dinode *inodebuf; 11324149Sguido static size_t oldibufsize; 11424149Sguido size_t ibufsize; 11524149Sguido struct fs *sblock; 11624149Sguido ino_t inumber, maxino; 11724149Sguido daddr_t dblk; 11824149Sguido char sbuf[SBSIZE], sbuftmp[SBSIZE]; 11924149Sguido int devfd, n, cg; 12024149Sguido u_int32_t bsize = DEV_BSIZE; 12124149Sguido struct disklabel label; 12224149Sguido 12324149Sguido if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) { 12424149Sguido warn("Can't open %s", device); 12524149Sguido return (1); 12624149Sguido } 12724149Sguido 12824149Sguido /* Get block size (usually 512) from disklabel if possible */ 12924149Sguido if (!ignorelabel) { 13024149Sguido if (ioctl(devfd, DIOCGDINFO, &label) < 0) 13124149Sguido warn("Can't read disklabel, using sector size of %d", 13224149Sguido bsize); 13324149Sguido else 13424149Sguido bsize = label.d_secsize; 13524149Sguido } 13624149Sguido 13724149Sguido /* Read in master superblock */ 13824149Sguido (void)memset(&sbuf, 0, sizeof(sbuf)); 13924149Sguido sblock = (struct fs *)&sbuf; 14024149Sguido if (lseek(devfd, SBOFF, SEEK_SET) == -1) { 14124149Sguido warn("Can't seek to superblock (%qd) on %s", SBOFF, device); 14224149Sguido return (1); 14324149Sguido } 14424149Sguido if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 14524149Sguido warnx("Can't read superblock on %s: %s", device, 14624149Sguido (n < SBSIZE) ? "short read" : strerror(errno)); 14724149Sguido return (1); 14824149Sguido } 14924149Sguido maxino = sblock->fs_ncg * sblock->fs_ipg; 15024149Sguido 15124149Sguido /* Simple sanity checks on the superblock */ 15224149Sguido if (sblock->fs_magic != FS_MAGIC) { 15324149Sguido warnx("Bad magic number in superblock"); 15424149Sguido return (1); 15524149Sguido } 15624149Sguido if (sblock->fs_sbsize > SBSIZE) { 15724149Sguido warnx("Superblock size is preposterous"); 15824149Sguido return (1); 15924149Sguido } 16024149Sguido if (sblock->fs_postblformat == FS_42POSTBLFMT) { 16124149Sguido warnx("Filesystem format is too old, sorry"); 16224149Sguido return (1); 16324149Sguido } 16424149Sguido if (!force && !printonly && sblock->fs_clean != 1) { 16524149Sguido warnx("Filesystem is not clean, fsck %s first.", device); 16624149Sguido return (1); 16724149Sguido } 16824149Sguido 16924149Sguido /* Make sure backup superblocks are sane. */ 17024149Sguido sblock = (struct fs *)&sbuftmp; 17124149Sguido for (cg = 0; cg < sblock->fs_ncg; cg++) { 17224149Sguido dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 17324149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 17424149Sguido warn("Can't seek to %qd", (off_t)dblk * bsize); 17524149Sguido return (1); 17624149Sguido } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 17724149Sguido warn("Can't read backup superblock %d on %s: %s", 17824149Sguido cg + 1, device, (n < SBSIZE) ? "short write" 17924149Sguido : strerror(errno)); 18024149Sguido return (1); 18124149Sguido } 18224149Sguido if (sblock->fs_magic != FS_MAGIC) { 18324149Sguido warnx("Bad magic number in backup superblock %d on %s", 18424149Sguido cg + 1, device); 18524149Sguido return (1); 18624149Sguido } 18724149Sguido if (sblock->fs_sbsize > SBSIZE) { 18824149Sguido warnx("Size of backup superblock %d on %s is preposterous", 18924149Sguido cg + 1, device); 19024149Sguido return (1); 19124149Sguido } 19224149Sguido } 19324149Sguido sblock = (struct fs *)&sbuf; 19424149Sguido 19524149Sguido /* XXX - should really cap buffer at 512kb or so */ 19624149Sguido ibufsize = sizeof(struct dinode) * sblock->fs_ipg; 19724149Sguido if (oldibufsize < ibufsize) { 19824149Sguido if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL) 19924149Sguido errx(1, "Can't allocate memory for inode buffer"); 20024149Sguido oldibufsize = ibufsize; 20124149Sguido } 20224149Sguido 20324149Sguido if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) { 20424149Sguido if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0]) 20524149Sguido (void)printf("%s was randomized on %s", device, 20624149Sguido ctime((const time_t *)&(sblock->fs_id[0]))); 20724149Sguido (void)printf("fsid: %x %x\n", sblock->fs_id[0], 20824149Sguido sblock->fs_id[1]); 20924149Sguido } 21024149Sguido 21124149Sguido /* Randomize fs_id unless old 4.2BSD filesystem */ 21224149Sguido if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) { 21324149Sguido /* Randomize fs_id and write out new sblock and backups */ 21424149Sguido sblock->fs_id[0] = (u_int32_t)time(NULL); 21524149Sguido sblock->fs_id[1] = random(); 21624149Sguido 21724149Sguido if (lseek(devfd, SBOFF, SEEK_SET) == -1) { 21824149Sguido warn("Can't seek to superblock (%qd) on %s", SBOFF, 21924149Sguido device); 22024149Sguido return (1); 22124149Sguido } 22224149Sguido if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 22324149Sguido warn("Can't read superblock on %s: %s", device, 22424149Sguido (n < SBSIZE) ? "short write" : strerror(errno)); 22524149Sguido return (1); 22624149Sguido } 22724149Sguido } 22824149Sguido 22924149Sguido /* For each cylinder group, randomize inodes and update backup sblock */ 23024149Sguido for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) { 23124149Sguido /* Update superblock if appropriate */ 23224149Sguido if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) { 23324149Sguido dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 23424149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 23524149Sguido warn("Can't seek to %qd", (off_t)dblk * bsize); 23624149Sguido return (1); 23724149Sguido } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 23824149Sguido warn("Can't read backup superblock %d on %s: %s", 23924149Sguido cg + 1, device, (n < SBSIZE) ? "short write" 24024149Sguido : strerror(errno)); 24124149Sguido return (1); 24224149Sguido } 24324149Sguido } 24424149Sguido 24524149Sguido /* Read in inodes, then print or randomize generation nums */ 24624149Sguido dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber)); 24724149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 24824149Sguido warn("Can't seek to %qd", (off_t)dblk * bsize); 24924149Sguido return (1); 25024149Sguido } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) { 25124149Sguido warnx("Can't read inodes: %s", 25224149Sguido (n < ibufsize) ? "short read" : strerror(errno)); 25324149Sguido return (1); 25424149Sguido } 25524149Sguido 25624149Sguido for (n = 0; n < sblock->fs_ipg; n++, inumber++) { 25724149Sguido if (inumber >= ROOTINO) { 25824149Sguido if (printonly) 25924149Sguido (void)printf("ino %d gen %x\n", inumber, 26024149Sguido inodebuf[n].di_gen); 26124149Sguido else 26224149Sguido inodebuf[n].di_gen = random(); 26324149Sguido } 26424149Sguido } 26524149Sguido 26624149Sguido /* Write out modified inodes */ 26724149Sguido if (!printonly) { 26824149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 26924149Sguido warn("Can't seek to %qd", 27024149Sguido (off_t)dblk * bsize); 27124149Sguido return (1); 27224149Sguido } else if ((n = write(devfd, inodebuf, ibufsize)) != 27324149Sguido ibufsize) { 27424149Sguido warnx("Can't write inodes: %s", 27524149Sguido (n != ibufsize) ? "short write" : 27624149Sguido strerror(errno)); 27724149Sguido return (1); 27824149Sguido } 27924149Sguido } 28024149Sguido } 28124149Sguido (void)close(devfd); 28224149Sguido 28324149Sguido return(0); 28424149Sguido} 28524149Sguido 28626558Scharnierstatic void 28724149Sguidousage(ex) 28824149Sguido int ex; 28924149Sguido{ 29024303Sguido (void)fprintf(stderr, 29126558Scharnier"usage: fsirand [ -b ] [ -f ] [ -p ] special [special ...]\n"); 29224149Sguido exit(ex); 29324149Sguido} 294