1 /* $OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Todd C. Miller. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 24 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 * $FreeBSD: src/sbin/fsirand/fsirand.c,v 1.7.2.1 2000/07/01 06:23:36 ps Exp $ 33 * $DragonFly: src/sbin/fsirand/fsirand.c,v 1.2 2003/06/17 04:27:32 dillon Exp $ 34 */ 35 36 #include <sys/disklabel.h> 37 #include <sys/param.h> 38 #include <sys/time.h> 39 #include <sys/resource.h> 40 41 #include <ufs/ffs/fs.h> 42 #include <ufs/ufs/dinode.h> 43 44 #include <err.h> 45 #include <errno.h> 46 #include <fcntl.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 static void usage __P((void)); 53 int fsirand __P((char *)); 54 55 int printonly = 0, force = 0, ignorelabel = 0; 56 57 int 58 main(argc, argv) 59 int argc; 60 char *argv[]; 61 { 62 int n, ex = 0; 63 struct rlimit rl; 64 65 while ((n = getopt(argc, argv, "bfp")) != -1) { 66 switch (n) { 67 case 'b': 68 ignorelabel++; 69 break; 70 case 'p': 71 printonly++; 72 break; 73 case 'f': 74 force++; 75 break; 76 default: 77 usage(); 78 } 79 } 80 if (argc - optind < 1) 81 usage(); 82 83 srandomdev(); 84 85 /* Increase our data size to the max */ 86 if (getrlimit(RLIMIT_DATA, &rl) == 0) { 87 rl.rlim_cur = rl.rlim_max; 88 if (setrlimit(RLIMIT_DATA, &rl) < 0) 89 warn("can't get resource limit to max data size"); 90 } else 91 warn("can't get resource limit for data size"); 92 93 for (n = optind; n < argc; n++) { 94 if (argc - optind != 1) 95 (void)puts(argv[n]); 96 ex += fsirand(argv[n]); 97 if (n < argc - 1) 98 putchar('\n'); 99 } 100 101 exit(ex); 102 } 103 104 int 105 fsirand(device) 106 char *device; 107 { 108 static struct dinode *inodebuf; 109 static size_t oldibufsize; 110 size_t ibufsize; 111 struct fs *sblock; 112 ino_t inumber, maxino; 113 daddr_t dblk; 114 char sbuf[SBSIZE], sbuftmp[SBSIZE]; 115 int devfd, n, cg; 116 u_int32_t bsize = DEV_BSIZE; 117 struct disklabel label; 118 119 if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) { 120 warn("can't open %s", device); 121 return (1); 122 } 123 124 /* Get block size (usually 512) from disklabel if possible */ 125 if (!ignorelabel) { 126 if (ioctl(devfd, DIOCGDINFO, &label) < 0) 127 warn("can't read disklabel, using sector size of %d", 128 bsize); 129 else 130 bsize = label.d_secsize; 131 } 132 133 /* Read in master superblock */ 134 (void)memset(&sbuf, 0, sizeof(sbuf)); 135 sblock = (struct fs *)&sbuf; 136 if (lseek(devfd, SBOFF, SEEK_SET) == -1) { 137 warn("can't seek to superblock (%qd) on %s", SBOFF, device); 138 return (1); 139 } 140 if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 141 warnx("can't read superblock on %s: %s", device, 142 (n < SBSIZE) ? "short read" : strerror(errno)); 143 return (1); 144 } 145 maxino = sblock->fs_ncg * sblock->fs_ipg; 146 147 /* Simple sanity checks on the superblock */ 148 if (sblock->fs_magic != FS_MAGIC) { 149 warnx("bad magic number in superblock"); 150 return (1); 151 } 152 if (sblock->fs_sbsize > SBSIZE) { 153 warnx("superblock size is preposterous"); 154 return (1); 155 } 156 if (sblock->fs_postblformat == FS_42POSTBLFMT) { 157 warnx("filesystem format is too old, sorry"); 158 return (1); 159 } 160 if (!force && !printonly && sblock->fs_clean != 1) { 161 warnx("filesystem is not clean, fsck %s first", device); 162 return (1); 163 } 164 165 /* Make sure backup superblocks are sane. */ 166 sblock = (struct fs *)&sbuftmp; 167 for (cg = 0; cg < sblock->fs_ncg; cg++) { 168 dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 169 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 170 warn("can't seek to %qd", (off_t)dblk * bsize); 171 return (1); 172 } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 173 warn("can't read backup superblock %d on %s: %s", 174 cg + 1, device, (n < SBSIZE) ? "short write" 175 : strerror(errno)); 176 return (1); 177 } 178 if (sblock->fs_magic != FS_MAGIC) { 179 warnx("bad magic number in backup superblock %d on %s", 180 cg + 1, device); 181 return (1); 182 } 183 if (sblock->fs_sbsize > SBSIZE) { 184 warnx("size of backup superblock %d on %s is preposterous", 185 cg + 1, device); 186 return (1); 187 } 188 } 189 sblock = (struct fs *)&sbuf; 190 191 /* XXX - should really cap buffer at 512kb or so */ 192 ibufsize = sizeof(struct dinode) * sblock->fs_ipg; 193 if (oldibufsize < ibufsize) { 194 if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL) 195 errx(1, "can't allocate memory for inode buffer"); 196 oldibufsize = ibufsize; 197 } 198 199 if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) { 200 if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0]) 201 (void)printf("%s was randomized on %s", device, 202 ctime((const time_t *)&(sblock->fs_id[0]))); 203 (void)printf("fsid: %x %x\n", sblock->fs_id[0], 204 sblock->fs_id[1]); 205 } 206 207 /* Randomize fs_id unless old 4.2BSD filesystem */ 208 if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) { 209 /* Randomize fs_id and write out new sblock and backups */ 210 sblock->fs_id[0] = (u_int32_t)time(NULL); 211 sblock->fs_id[1] = random(); 212 213 if (lseek(devfd, SBOFF, SEEK_SET) == -1) { 214 warn("can't seek to superblock (%qd) on %s", SBOFF, 215 device); 216 return (1); 217 } 218 if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 219 warn("can't read superblock on %s: %s", device, 220 (n < SBSIZE) ? "short write" : strerror(errno)); 221 return (1); 222 } 223 } 224 225 /* For each cylinder group, randomize inodes and update backup sblock */ 226 for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) { 227 /* Update superblock if appropriate */ 228 if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) { 229 dblk = fsbtodb(sblock, cgsblock(sblock, cg)); 230 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 231 warn("can't seek to %qd", (off_t)dblk * bsize); 232 return (1); 233 } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) { 234 warn("can't read backup superblock %d on %s: %s", 235 cg + 1, device, (n < SBSIZE) ? "short write" 236 : strerror(errno)); 237 return (1); 238 } 239 } 240 241 /* Read in inodes, then print or randomize generation nums */ 242 dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber)); 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 = read(devfd, inodebuf, ibufsize)) != ibufsize) { 247 warnx("can't read inodes: %s", 248 (n < ibufsize) ? "short read" : strerror(errno)); 249 return (1); 250 } 251 252 for (n = 0; n < sblock->fs_ipg; n++, inumber++) { 253 if (inumber >= ROOTINO) { 254 if (printonly) 255 (void)printf("ino %d gen %x\n", inumber, 256 inodebuf[n].di_gen); 257 else 258 inodebuf[n].di_gen = random(); 259 } 260 } 261 262 /* Write out modified inodes */ 263 if (!printonly) { 264 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) { 265 warn("can't seek to %qd", 266 (off_t)dblk * bsize); 267 return (1); 268 } else if ((n = write(devfd, inodebuf, ibufsize)) != 269 ibufsize) { 270 warnx("can't write inodes: %s", 271 (n != ibufsize) ? "short write" : 272 strerror(errno)); 273 return (1); 274 } 275 } 276 } 277 (void)close(devfd); 278 279 return(0); 280 } 281 282 static void 283 usage() 284 { 285 (void)fprintf(stderr, 286 "usage: fsirand [-b] [-f] [-p] special [special ...]\n"); 287 exit(1); 288 } 289