fsirand.c revision 132762
1219820Sjeff/* $OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $ */ 2219820Sjeff 3219820Sjeff/* 4219820Sjeff * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5219820Sjeff * All rights reserved. 6219820Sjeff * 7219820Sjeff * Redistribution and use in source and binary forms, with or without 8219820Sjeff * modification, are permitted provided that the following conditions 9219820Sjeff * are met: 10219820Sjeff * 1. Redistributions of source code must retain the above copyright 11219820Sjeff * notice, this list of conditions and the following disclaimer. 12219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 13219820Sjeff * notice, this list of conditions and the following disclaimer in the 14219820Sjeff * documentation and/or other materials provided with the distribution. 15219820Sjeff * 3. All advertising materials mentioning features or use of this software 16219820Sjeff * must display the following acknowledgement: 17219820Sjeff * This product includes software developed by Todd C. Miller. 18219820Sjeff * 4. The name of the author may not be used to endorse or promote products 19219820Sjeff * derived from this software without specific prior written permission. 20219820Sjeff * 21219820Sjeff * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 22219820Sjeff * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 23219820Sjeff * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 24219820Sjeff * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25219820Sjeff * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26219820Sjeff * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27219820Sjeff * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28219820Sjeff * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29219820Sjeff * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30219820Sjeff * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31219820Sjeff */ 32219820Sjeff 33219820Sjeff#ifndef lint 34219820Sjeffstatic const char rcsid[] = 35219820Sjeff "$FreeBSD: head/sbin/fsirand/fsirand.c 132762 2004-07-28 05:59:22Z kan $"; 36219820Sjeff#endif /* not lint */ 37219820Sjeff 38219820Sjeff#include <sys/disklabel.h> 39219820Sjeff#include <sys/param.h> 40219820Sjeff#include <sys/time.h> 41219820Sjeff#include <sys/resource.h> 42219820Sjeff 43219820Sjeff#include <ufs/ufs/dinode.h> 44219820Sjeff#include <ufs/ffs/fs.h> 45219820Sjeff 46219820Sjeff#include <err.h> 47219820Sjeff#include <errno.h> 48219820Sjeff#include <fcntl.h> 49219820Sjeff#include <stdio.h> 50219820Sjeff#include <stdlib.h> 51219820Sjeff#include <string.h> 52219820Sjeff#include <unistd.h> 53219820Sjeff 54219820Sjeffstatic void usage(void) __dead2; 55219820Sjeffint fsirand(char *); 56219820Sjeff 57219820Sjeff/* 58219820Sjeff * Possible superblock locations ordered from most to least likely. 59219820Sjeff */ 60219820Sjeffstatic int sblock_try[] = SBLOCKSEARCH; 61219820Sjeff 62219820Sjeffint printonly = 0, force = 0, ignorelabel = 0; 63219820Sjeff 64219820Sjeffint 65219820Sjeffmain(int argc, char *argv[]) 66219820Sjeff{ 67219820Sjeff int n, ex = 0; 68219820Sjeff struct rlimit rl; 69219820Sjeff 70219820Sjeff while ((n = getopt(argc, argv, "bfp")) != -1) { 71219820Sjeff switch (n) { 72219820Sjeff case 'b': 73219820Sjeff ignorelabel++; 74219820Sjeff break; 75219820Sjeff case 'p': 76219820Sjeff printonly++; 77219820Sjeff break; 78219820Sjeff case 'f': 79219820Sjeff force++; 80219820Sjeff break; 81219820Sjeff default: 82219820Sjeff usage(); 83219820Sjeff } 84219820Sjeff } 85219820Sjeff if (argc - optind < 1) 86219820Sjeff usage(); 87219820Sjeff 88219820Sjeff srandomdev(); 89219820Sjeff 90219820Sjeff /* Increase our data size to the max */ 91219820Sjeff if (getrlimit(RLIMIT_DATA, &rl) == 0) { 92219820Sjeff rl.rlim_cur = rl.rlim_max; 93219820Sjeff if (setrlimit(RLIMIT_DATA, &rl) < 0) 94219820Sjeff warn("can't get resource limit to max data size"); 95219820Sjeff } else 96219820Sjeff warn("can't get resource limit for data size"); 97219820Sjeff 98219820Sjeff for (n = optind; n < argc; n++) { 99219820Sjeff if (argc - optind != 1) 100219820Sjeff (void)puts(argv[n]); 101219820Sjeff ex += fsirand(argv[n]); 102 if (n < argc - 1) 103 putchar('\n'); 104 } 105 106 exit(ex); 107} 108 109int 110fsirand(char *device) 111{ 112 struct ufs1_dinode *dp1; 113 struct ufs2_dinode *dp2; 114 caddr_t inodebuf; 115 size_t ibufsize; 116 struct fs *sblock; 117 ino_t inumber, maxino; 118 ufs2_daddr_t sblockloc, dblk; 119 char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE]; 120 int i, devfd, n, cg; 121 u_int32_t bsize = DEV_BSIZE; 122 struct disklabel label; 123 124 if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) { 125 warn("can't open %s", device); 126 return (1); 127 } 128 129 /* Get block size (usually 512) from disklabel if possible */ 130 if (!ignorelabel) { 131 if (ioctl(devfd, DIOCGDINFO, &label) < 0) 132 warn("can't read disklabel, using sector size of %d", 133 bsize); 134 else 135 bsize = label.d_secsize; 136 } 137 138 /* Read in master superblock */ 139 (void)memset(&sbuf, 0, sizeof(sbuf)); 140 sblock = (struct fs *)&sbuf; 141 for (i = 0; sblock_try[i] != -1; i++) { 142 sblockloc = sblock_try[i]; 143 if (lseek(devfd, sblockloc, SEEK_SET) == -1) { 144 warn("can't seek to superblock (%qd) on %s", 145 sblockloc, device); 146 return (1); 147 } 148 if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) { 149 warnx("can't read superblock on %s: %s", device, 150 (n < SBLOCKSIZE) ? "short read" : strerror(errno)); 151 return (1); 152 } 153 if ((sblock->fs_magic == FS_UFS1_MAGIC || 154 (sblock->fs_magic == FS_UFS2_MAGIC && 155 sblock->fs_sblockloc == sblock_try[i])) && 156 sblock->fs_bsize <= MAXBSIZE && 157 sblock->fs_bsize >= sizeof(struct fs)) 158 break; 159 } 160 if (sblock_try[i] == -1) { 161 fprintf(stderr, "Cannot find file system superblock\n"); 162 return (1); 163 } 164 maxino = sblock->fs_ncg * sblock->fs_ipg; 165 166 if (sblock->fs_magic == FS_UFS1_MAGIC && 167 sblock->fs_old_inodefmt < FS_44INODEFMT) { 168 warnx("file system format is too old, sorry"); 169 return (1); 170 } 171 if (!force && !printonly && sblock->fs_clean != 1) { 172 warnx("file system is not clean, fsck %s first", device); 173 return (1); 174 } 175 176 /* Make sure backup superblocks are sane. */ 177 sblock = (struct fs *)&sbuftmp; 178 for (cg = 0; cg < sblock->fs_ncg; cg++) { 179 dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 180 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 181 warn("can't seek to %qd", (off_t)dblk * bsize); 182 return (1); 183 } else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) { 184 warn("can't read backup superblock %d on %s: %s", 185 cg + 1, device, (n < SBLOCKSIZE) ? "short write" 186 : strerror(errno)); 187 return (1); 188 } 189 if (sblock->fs_magic != FS_UFS1_MAGIC && 190 sblock->fs_magic != FS_UFS2_MAGIC) { 191 warnx("bad magic number in backup superblock %d on %s", 192 cg + 1, device); 193 return (1); 194 } 195 if (sblock->fs_sbsize > SBLOCKSIZE) { 196 warnx("size of backup superblock %d on %s is preposterous", 197 cg + 1, device); 198 return (1); 199 } 200 } 201 sblock = (struct fs *)&sbuf; 202 203 /* XXX - should really cap buffer at 512kb or so */ 204 if (sblock->fs_magic == FS_UFS1_MAGIC) 205 ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg; 206 else 207 ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg; 208 if ((inodebuf = malloc(ibufsize)) == NULL) 209 errx(1, "can't allocate memory for inode buffer"); 210 211 if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) { 212 if (sblock->fs_id[0]) 213 (void)printf("%s was randomized on %s", device, 214 ctime((const time_t *)&(sblock->fs_id[0]))); 215 (void)printf("fsid: %x %x\n", sblock->fs_id[0], 216 sblock->fs_id[1]); 217 } 218 219 /* Randomize fs_id unless old 4.2BSD file system */ 220 if (!printonly) { 221 /* Randomize fs_id and write out new sblock and backups */ 222 sblock->fs_id[0] = (u_int32_t)time(NULL); 223 sblock->fs_id[1] = random(); 224 225 if (lseek(devfd, sblockloc, SEEK_SET) == -1) { 226 warn("can't seek to superblock (%qd) on %s", sblockloc, 227 device); 228 return (1); 229 } 230 if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != 231 SBLOCKSIZE) { 232 warn("can't write superblock on %s: %s", device, 233 (n < SBLOCKSIZE) ? "short write" : strerror(errno)); 234 return (1); 235 } 236 } 237 238 /* For each cylinder group, randomize inodes and update backup sblock */ 239 for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) { 240 /* Update superblock if appropriate */ 241 if (!printonly) { 242 dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 243 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 244 warn("can't seek to %qd", (off_t)dblk * bsize); 245 return (1); 246 } else if ((n = write(devfd, (void *)sblock, 247 SBLOCKSIZE)) != SBLOCKSIZE) { 248 warn("can't write backup superblock %d on %s: %s", 249 cg + 1, device, (n < SBLOCKSIZE) ? 250 "short write" : strerror(errno)); 251 return (1); 252 } 253 } 254 255 /* Read in inodes, then print or randomize generation nums */ 256 dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber)); 257 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 258 warn("can't seek to %qd", (off_t)dblk * bsize); 259 return (1); 260 } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) { 261 warnx("can't read inodes: %s", 262 (n < ibufsize) ? "short read" : strerror(errno)); 263 return (1); 264 } 265 266 for (n = 0; n < sblock->fs_ipg; n++, inumber++) { 267 if (sblock->fs_magic == FS_UFS1_MAGIC) 268 dp1 = &((struct ufs1_dinode *)inodebuf)[n]; 269 else 270 dp2 = &((struct ufs2_dinode *)inodebuf)[n]; 271 if (inumber >= ROOTINO) { 272 if (printonly) 273 (void)printf("ino %d gen %qx\n", 274 inumber, 275 sblock->fs_magic == FS_UFS1_MAGIC ? 276 (quad_t)dp1->di_gen : dp2->di_gen); 277 else if (sblock->fs_magic == FS_UFS1_MAGIC) 278 dp1->di_gen = random(); 279 else 280 dp2->di_gen = random(); 281 } 282 } 283 284 /* Write out modified inodes */ 285 if (!printonly) { 286 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 287 warn("can't seek to %qd", 288 (off_t)dblk * bsize); 289 return (1); 290 } else if ((n = write(devfd, inodebuf, ibufsize)) != 291 ibufsize) { 292 warnx("can't write inodes: %s", 293 (n != ibufsize) ? "short write" : 294 strerror(errno)); 295 return (1); 296 } 297 } 298 } 299 (void)close(devfd); 300 301 return(0); 302} 303 304static void 305usage(void) 306{ 307 (void)fprintf(stderr, 308 "usage: fsirand [-b] [-f] [-p] special [special ...]\n"); 309 exit(1); 310} 311