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: stable/10/sbin/fsirand/fsirand.c 322860 2017-08-24 21:44:23Z mckusick $"; 3637275Scharnier#endif /* not lint */ 3724149Sguido 38217769Smckusick#include <sys/param.h> 3924149Sguido#include <sys/resource.h> 4024149Sguido 4198542Smckusick#include <ufs/ufs/dinode.h> 4224149Sguido#include <ufs/ffs/fs.h> 4324149Sguido 4424149Sguido#include <err.h> 4524149Sguido#include <errno.h> 4624149Sguido#include <fcntl.h> 4724149Sguido#include <stdio.h> 48208074Suqs#include <stdint.h> 4924149Sguido#include <stdlib.h> 5024149Sguido#include <string.h> 51217769Smckusick#include <time.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 62227081Sedstatic int 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; 115208074Suqs ssize_t ibufsize; 11624149Sguido struct fs *sblock; 117229917Seadler ino_t inumber; 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 12324149Sguido if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) { 12437275Scharnier warn("can't open %s", device); 12524149Sguido return (1); 12624149Sguido } 12724149Sguido 128208074Suqs dp1 = NULL; 129208074Suqs dp2 = NULL; 130208074Suqs 13124149Sguido /* Read in master superblock */ 13224149Sguido (void)memset(&sbuf, 0, sizeof(sbuf)); 13324149Sguido sblock = (struct fs *)&sbuf; 13498542Smckusick for (i = 0; sblock_try[i] != -1; i++) { 13598542Smckusick sblockloc = sblock_try[i]; 13698542Smckusick if (lseek(devfd, sblockloc, SEEK_SET) == -1) { 137208074Suqs warn("can't seek to superblock (%jd) on %s", 138208074Suqs (intmax_t)sblockloc, device); 13998542Smckusick return (1); 14098542Smckusick } 14198542Smckusick if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) { 14298542Smckusick warnx("can't read superblock on %s: %s", device, 14398542Smckusick (n < SBLOCKSIZE) ? "short read" : strerror(errno)); 14498542Smckusick return (1); 14598542Smckusick } 14698542Smckusick if ((sblock->fs_magic == FS_UFS1_MAGIC || 14798542Smckusick (sblock->fs_magic == FS_UFS2_MAGIC && 148107294Smckusick sblock->fs_sblockloc == sblock_try[i])) && 14998542Smckusick sblock->fs_bsize <= MAXBSIZE && 150208074Suqs sblock->fs_bsize >= (ssize_t)sizeof(struct fs)) 15198542Smckusick break; 15224149Sguido } 15398542Smckusick if (sblock_try[i] == -1) { 154102231Strhodes fprintf(stderr, "Cannot find file system superblock\n"); 15524149Sguido return (1); 15624149Sguido } 15724149Sguido 15898542Smckusick if (sblock->fs_magic == FS_UFS1_MAGIC && 15998542Smckusick sblock->fs_old_inodefmt < FS_44INODEFMT) { 160102231Strhodes warnx("file system format is too old, sorry"); 16124149Sguido return (1); 16224149Sguido } 16324149Sguido if (!force && !printonly && sblock->fs_clean != 1) { 164102231Strhodes warnx("file system is not clean, fsck %s first", device); 16524149Sguido return (1); 16624149Sguido } 16724149Sguido 16824149Sguido /* Make sure backup superblocks are sane. */ 16924149Sguido sblock = (struct fs *)&sbuftmp; 170208074Suqs for (cg = 0; cg < (int)sblock->fs_ncg; cg++) { 17124149Sguido dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 17224149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 173208074Suqs warn("can't seek to %jd", (intmax_t)dblk * bsize); 17424149Sguido return (1); 17598542Smckusick } else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) { 17637275Scharnier warn("can't read backup superblock %d on %s: %s", 17798542Smckusick cg + 1, device, (n < SBLOCKSIZE) ? "short write" 17824149Sguido : strerror(errno)); 17924149Sguido return (1); 18024149Sguido } 18198542Smckusick if (sblock->fs_magic != FS_UFS1_MAGIC && 18298542Smckusick sblock->fs_magic != FS_UFS2_MAGIC) { 18337275Scharnier warnx("bad magic number in backup superblock %d on %s", 18424149Sguido cg + 1, device); 18524149Sguido return (1); 18624149Sguido } 18798542Smckusick if (sblock->fs_sbsize > SBLOCKSIZE) { 18837275Scharnier 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 */ 19698542Smckusick if (sblock->fs_magic == FS_UFS1_MAGIC) 19798542Smckusick ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg; 19898542Smckusick else 19998542Smckusick ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg; 20098542Smckusick if ((inodebuf = malloc(ibufsize)) == NULL) 20198542Smckusick errx(1, "can't allocate memory for inode buffer"); 20224149Sguido 20324149Sguido if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) { 20498542Smckusick if (sblock->fs_id[0]) 20524149Sguido (void)printf("%s was randomized on %s", device, 206208074Suqs ctime((void *)&(sblock->fs_id[0]))); 20724149Sguido (void)printf("fsid: %x %x\n", sblock->fs_id[0], 20824149Sguido sblock->fs_id[1]); 20924149Sguido } 21024149Sguido 211102231Strhodes /* Randomize fs_id unless old 4.2BSD file system */ 21298542Smckusick if (!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 21798542Smckusick if (lseek(devfd, sblockloc, SEEK_SET) == -1) { 218208074Suqs warn("can't seek to superblock (%jd) on %s", 219208074Suqs (intmax_t)sblockloc, device); 22024149Sguido return (1); 22124149Sguido } 22298542Smckusick if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != 22398542Smckusick SBLOCKSIZE) { 22498542Smckusick warn("can't write superblock on %s: %s", device, 22598542Smckusick (n < SBLOCKSIZE) ? "short write" : strerror(errno)); 22624149Sguido return (1); 22724149Sguido } 22824149Sguido } 22924149Sguido 23024149Sguido /* For each cylinder group, randomize inodes and update backup sblock */ 231208074Suqs for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) { 23224149Sguido /* Update superblock if appropriate */ 23398542Smckusick if (!printonly) { 23424149Sguido dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 23524149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 236208074Suqs warn("can't seek to %jd", 237208074Suqs (intmax_t)dblk * bsize); 23824149Sguido return (1); 23998542Smckusick } else if ((n = write(devfd, (void *)sblock, 24098542Smckusick SBLOCKSIZE)) != SBLOCKSIZE) { 24198542Smckusick warn("can't write backup superblock %d on %s: %s", 24298542Smckusick cg + 1, device, (n < SBLOCKSIZE) ? 24398542Smckusick "short write" : strerror(errno)); 24424149Sguido return (1); 24524149Sguido } 24624149Sguido } 24724149Sguido 24824149Sguido /* Read in inodes, then print or randomize generation nums */ 24924149Sguido dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber)); 25024149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 251208074Suqs warn("can't seek to %jd", (intmax_t)dblk * bsize); 25224149Sguido return (1); 25324149Sguido } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) { 25437275Scharnier warnx("can't read inodes: %s", 25524149Sguido (n < ibufsize) ? "short read" : strerror(errno)); 25624149Sguido return (1); 25724149Sguido } 25824149Sguido 259208074Suqs for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) { 26098542Smckusick if (sblock->fs_magic == FS_UFS1_MAGIC) 26198542Smckusick dp1 = &((struct ufs1_dinode *)inodebuf)[n]; 26298542Smckusick else 26398542Smckusick dp2 = &((struct ufs2_dinode *)inodebuf)[n]; 26424149Sguido if (inumber >= ROOTINO) { 26524149Sguido if (printonly) 266241013Smdf (void)printf("ino %ju gen %08x\n", 267241013Smdf (uintmax_t)inumber, 26898542Smckusick sblock->fs_magic == FS_UFS1_MAGIC ? 269208074Suqs dp1->di_gen : dp2->di_gen); 270132762Skan else if (sblock->fs_magic == FS_UFS1_MAGIC) 271132762Skan dp1->di_gen = random(); 27224149Sguido else 273132762Skan dp2->di_gen = random(); 27424149Sguido } 27524149Sguido } 27624149Sguido 27724149Sguido /* Write out modified inodes */ 27824149Sguido if (!printonly) { 27924149Sguido if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 280208074Suqs warn("can't seek to %jd", 281208074Suqs (intmax_t)dblk * bsize); 28224149Sguido return (1); 28324149Sguido } else if ((n = write(devfd, inodebuf, ibufsize)) != 28424149Sguido ibufsize) { 28537275Scharnier warnx("can't write inodes: %s", 28624149Sguido (n != ibufsize) ? "short write" : 28724149Sguido strerror(errno)); 28824149Sguido return (1); 28924149Sguido } 29024149Sguido } 29124149Sguido } 29224149Sguido (void)close(devfd); 29324149Sguido 29424149Sguido return(0); 29524149Sguido} 29624149Sguido 29726558Scharnierstatic void 29892881Simpusage(void) 29924149Sguido{ 30024303Sguido (void)fprintf(stderr, 30137275Scharnier "usage: fsirand [-b] [-f] [-p] special [special ...]\n"); 30237275Scharnier exit(1); 30324149Sguido} 304