fsirand.c revision 132762
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 3337275Scharnier#ifndef lint 3437275Scharnierstatic const char rcsid[] = 3550476Speter "$FreeBSD: head/sbin/fsirand/fsirand.c 132762 2004-07-28 05:59:22Z kan $"; 3637275Scharnier#endif /* not lint */ 3724149Sguido 3824149Sguido#include <sys/disklabel.h> 3924149Sguido#include <sys/param.h> 4024149Sguido#include <sys/time.h> 4124149Sguido#include <sys/resource.h> 4224149Sguido 4398542Smckusick#include <ufs/ufs/dinode.h> 4424149Sguido#include <ufs/ffs/fs.h> 4524149Sguido 4624149Sguido#include <err.h> 4724149Sguido#include <errno.h> 4824149Sguido#include <fcntl.h> 4924149Sguido#include <stdio.h> 5024149Sguido#include <stdlib.h> 5124149Sguido#include <string.h> 5224149Sguido#include <unistd.h> 5324149Sguido 5492881Simpstatic void usage(void) __dead2; 5592881Simpint fsirand(char *); 5624149Sguido 5798542Smckusick/* 5898542Smckusick * Possible superblock locations ordered from most to least likely. 5998542Smckusick */ 6098542Smckusickstatic int sblock_try[] = SBLOCKSEARCH; 6198542Smckusick 6224149Sguidoint printonly = 0, force = 0, ignorelabel = 0; 6324149Sguido 6424149Sguidoint 6592881Simpmain(int argc, char *argv[]) 6624149Sguido{ 6724149Sguido int n, ex = 0; 6824149Sguido struct rlimit rl; 6924149Sguido 7024149Sguido while ((n = getopt(argc, argv, "bfp")) != -1) { 7124149Sguido switch (n) { 7224149Sguido case 'b': 7324149Sguido ignorelabel++; 7424149Sguido break; 7524149Sguido case 'p': 7624149Sguido printonly++; 7724149Sguido break; 7824149Sguido case 'f': 7924149Sguido force++; 8024149Sguido break; 8124149Sguido default: 8237275Scharnier usage(); 8324149Sguido } 8424149Sguido } 8524149Sguido if (argc - optind < 1) 8637275Scharnier usage(); 8724149Sguido 8826625Sache srandomdev(); 8924216Sache 9024149Sguido /* Increase our data size to the max */ 9124149Sguido if (getrlimit(RLIMIT_DATA, &rl) == 0) { 9224149Sguido rl.rlim_cur = rl.rlim_max; 9324149Sguido if (setrlimit(RLIMIT_DATA, &rl) < 0) 9437275Scharnier warn("can't get resource limit to max data size"); 9524149Sguido } else 9637275Scharnier warn("can't get resource limit for data size"); 9724149Sguido 9824149Sguido for (n = optind; n < argc; n++) { 9924149Sguido if (argc - optind != 1) 10024149Sguido (void)puts(argv[n]); 10124149Sguido ex += fsirand(argv[n]); 10224149Sguido if (n < argc - 1) 10324149Sguido putchar('\n'); 10424149Sguido } 10524149Sguido 10624149Sguido exit(ex); 10724149Sguido} 10824149Sguido 10924149Sguidoint 11092881Simpfsirand(char *device) 11124149Sguido{ 11298542Smckusick struct ufs1_dinode *dp1; 11398542Smckusick struct ufs2_dinode *dp2; 11498542Smckusick caddr_t inodebuf; 11524149Sguido size_t ibufsize; 11624149Sguido struct fs *sblock; 11724149Sguido ino_t inumber, maxino; 11898542Smckusick ufs2_daddr_t sblockloc, dblk; 11998542Smckusick char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE]; 12098542Smckusick int i, devfd, n, cg; 12124149Sguido u_int32_t bsize = DEV_BSIZE; 12224149Sguido struct disklabel label; 12324149Sguido 12424149Sguido if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) { 12537275Scharnier warn("can't open %s", device); 12624149Sguido return (1); 12724149Sguido } 12824149Sguido 12924149Sguido /* Get block size (usually 512) from disklabel if possible */ 13024149Sguido if (!ignorelabel) { 13124149Sguido if (ioctl(devfd, DIOCGDINFO, &label) < 0) 13237275Scharnier warn("can't read disklabel, using sector size of %d", 13324149Sguido bsize); 13424149Sguido else 13524149Sguido bsize = label.d_secsize; 13624149Sguido } 13724149Sguido 13824149Sguido /* Read in master superblock */ 13924149Sguido (void)memset(&sbuf, 0, sizeof(sbuf)); 14024149Sguido sblock = (struct fs *)&sbuf; 14198542Smckusick for (i = 0; sblock_try[i] != -1; i++) { 14298542Smckusick sblockloc = sblock_try[i]; 14398542Smckusick if (lseek(devfd, sblockloc, SEEK_SET) == -1) { 14498542Smckusick warn("can't seek to superblock (%qd) on %s", 14598542Smckusick sblockloc, device); 14698542Smckusick return (1); 14798542Smckusick } 14898542Smckusick if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) { 14998542Smckusick warnx("can't read superblock on %s: %s", device, 15098542Smckusick (n < SBLOCKSIZE) ? "short read" : strerror(errno)); 15198542Smckusick return (1); 15298542Smckusick } 15398542Smckusick if ((sblock->fs_magic == FS_UFS1_MAGIC || 15498542Smckusick (sblock->fs_magic == FS_UFS2_MAGIC && 155107294Smckusick sblock->fs_sblockloc == sblock_try[i])) && 15698542Smckusick sblock->fs_bsize <= MAXBSIZE && 15798542Smckusick sblock->fs_bsize >= sizeof(struct fs)) 15898542Smckusick break; 15924149Sguido } 16098542Smckusick if (sblock_try[i] == -1) { 161102231Strhodes fprintf(stderr, "Cannot find file system superblock\n"); 16224149Sguido return (1); 16324149Sguido } 16424149Sguido maxino = sblock->fs_ncg * sblock->fs_ipg; 16524149Sguido 16698542Smckusick if (sblock->fs_magic == FS_UFS1_MAGIC && 16798542Smckusick sblock->fs_old_inodefmt < FS_44INODEFMT) { 168102231Strhodes warnx("file system format is too old, sorry"); 16924149Sguido return (1); 17024149Sguido } 17124149Sguido if (!force && !printonly && sblock->fs_clean != 1) { 172102231Strhodes warnx("file system is not clean, fsck %s first", device); 17324149Sguido return (1); 17424149Sguido } 17524149Sguido 17624149Sguido /* Make sure backup superblocks are sane. */ 17724149Sguido sblock = (struct fs *)&sbuftmp; 17824149Sguido for (cg = 0; cg < sblock->fs_ncg; cg++) { 17924149Sguido dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 18024149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 18137275Scharnier warn("can't seek to %qd", (off_t)dblk * bsize); 18224149Sguido return (1); 18398542Smckusick } else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) { 18437275Scharnier warn("can't read backup superblock %d on %s: %s", 18598542Smckusick cg + 1, device, (n < SBLOCKSIZE) ? "short write" 18624149Sguido : strerror(errno)); 18724149Sguido return (1); 18824149Sguido } 18998542Smckusick if (sblock->fs_magic != FS_UFS1_MAGIC && 19098542Smckusick sblock->fs_magic != FS_UFS2_MAGIC) { 19137275Scharnier warnx("bad magic number in backup superblock %d on %s", 19224149Sguido cg + 1, device); 19324149Sguido return (1); 19424149Sguido } 19598542Smckusick if (sblock->fs_sbsize > SBLOCKSIZE) { 19637275Scharnier warnx("size of backup superblock %d on %s is preposterous", 19724149Sguido cg + 1, device); 19824149Sguido return (1); 19924149Sguido } 20024149Sguido } 20124149Sguido sblock = (struct fs *)&sbuf; 20224149Sguido 20324149Sguido /* XXX - should really cap buffer at 512kb or so */ 20498542Smckusick if (sblock->fs_magic == FS_UFS1_MAGIC) 20598542Smckusick ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg; 20698542Smckusick else 20798542Smckusick ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg; 20898542Smckusick if ((inodebuf = malloc(ibufsize)) == NULL) 20998542Smckusick errx(1, "can't allocate memory for inode buffer"); 21024149Sguido 21124149Sguido if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) { 21298542Smckusick if (sblock->fs_id[0]) 21324149Sguido (void)printf("%s was randomized on %s", device, 21424149Sguido ctime((const time_t *)&(sblock->fs_id[0]))); 21524149Sguido (void)printf("fsid: %x %x\n", sblock->fs_id[0], 21624149Sguido sblock->fs_id[1]); 21724149Sguido } 21824149Sguido 219102231Strhodes /* Randomize fs_id unless old 4.2BSD file system */ 22098542Smckusick if (!printonly) { 22124149Sguido /* Randomize fs_id and write out new sblock and backups */ 22224149Sguido sblock->fs_id[0] = (u_int32_t)time(NULL); 22324149Sguido sblock->fs_id[1] = random(); 22424149Sguido 22598542Smckusick if (lseek(devfd, sblockloc, SEEK_SET) == -1) { 22698542Smckusick warn("can't seek to superblock (%qd) on %s", sblockloc, 22724149Sguido device); 22824149Sguido return (1); 22924149Sguido } 23098542Smckusick if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != 23198542Smckusick SBLOCKSIZE) { 23298542Smckusick warn("can't write superblock on %s: %s", device, 23398542Smckusick (n < SBLOCKSIZE) ? "short write" : strerror(errno)); 23424149Sguido return (1); 23524149Sguido } 23624149Sguido } 23724149Sguido 23824149Sguido /* For each cylinder group, randomize inodes and update backup sblock */ 23924149Sguido for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) { 24024149Sguido /* Update superblock if appropriate */ 24198542Smckusick if (!printonly) { 24224149Sguido dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 24324149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 24437275Scharnier warn("can't seek to %qd", (off_t)dblk * bsize); 24524149Sguido return (1); 24698542Smckusick } else if ((n = write(devfd, (void *)sblock, 24798542Smckusick SBLOCKSIZE)) != SBLOCKSIZE) { 24898542Smckusick warn("can't write backup superblock %d on %s: %s", 24998542Smckusick cg + 1, device, (n < SBLOCKSIZE) ? 25098542Smckusick "short write" : strerror(errno)); 25124149Sguido return (1); 25224149Sguido } 25324149Sguido } 25424149Sguido 25524149Sguido /* Read in inodes, then print or randomize generation nums */ 25624149Sguido dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber)); 25724149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 25837275Scharnier warn("can't seek to %qd", (off_t)dblk * bsize); 25924149Sguido return (1); 26024149Sguido } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) { 26137275Scharnier warnx("can't read inodes: %s", 26224149Sguido (n < ibufsize) ? "short read" : strerror(errno)); 26324149Sguido return (1); 26424149Sguido } 26524149Sguido 26624149Sguido for (n = 0; n < sblock->fs_ipg; n++, inumber++) { 26798542Smckusick if (sblock->fs_magic == FS_UFS1_MAGIC) 26898542Smckusick dp1 = &((struct ufs1_dinode *)inodebuf)[n]; 26998542Smckusick else 27098542Smckusick dp2 = &((struct ufs2_dinode *)inodebuf)[n]; 27124149Sguido if (inumber >= ROOTINO) { 27224149Sguido if (printonly) 27398542Smckusick (void)printf("ino %d gen %qx\n", 27498542Smckusick inumber, 27598542Smckusick sblock->fs_magic == FS_UFS1_MAGIC ? 27698542Smckusick (quad_t)dp1->di_gen : dp2->di_gen); 277132762Skan else if (sblock->fs_magic == FS_UFS1_MAGIC) 278132762Skan dp1->di_gen = random(); 27924149Sguido else 280132762Skan dp2->di_gen = random(); 28124149Sguido } 28224149Sguido } 28324149Sguido 28424149Sguido /* Write out modified inodes */ 28524149Sguido if (!printonly) { 28624149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 28737275Scharnier warn("can't seek to %qd", 28824149Sguido (off_t)dblk * bsize); 28924149Sguido return (1); 29024149Sguido } else if ((n = write(devfd, inodebuf, ibufsize)) != 29124149Sguido ibufsize) { 29237275Scharnier warnx("can't write inodes: %s", 29324149Sguido (n != ibufsize) ? "short write" : 29424149Sguido strerror(errno)); 29524149Sguido return (1); 29624149Sguido } 29724149Sguido } 29824149Sguido } 29924149Sguido (void)close(devfd); 30024149Sguido 30124149Sguido return(0); 30224149Sguido} 30324149Sguido 30426558Scharnierstatic void 30592881Simpusage(void) 30624149Sguido{ 30724303Sguido (void)fprintf(stderr, 30837275Scharnier "usage: fsirand [-b] [-f] [-p] special [special ...]\n"); 30937275Scharnier exit(1); 31024149Sguido} 311