xref: /dragonfly/sbin/fsirand/fsirand.c (revision dca3c15d)
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.10 2007/05/20 23:21:36 dillon Exp $
34  */
35 
36 #include <sys/diskslice.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/resource.h>
40 
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/ufs/fs.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(void);
53 int fsirand(char *);
54 
55 int printonly = 0, force = 0, ignorelabel = 0;
56 
57 int
58 main(int argc, char **argv)
59 {
60 	int n, ex = 0;
61 	struct rlimit rl;
62 
63 	while ((n = getopt(argc, argv, "bfp")) != -1) {
64 		switch (n) {
65 		case 'b':
66 			ignorelabel++;
67 			break;
68 		case 'p':
69 			printonly++;
70 			break;
71 		case 'f':
72 			force++;
73 			break;
74 		default:
75 			usage();
76 		}
77 	}
78 	if (argc - optind < 1)
79 		usage();
80 
81 	srandomdev();
82 
83 	/* Increase our data size to the max */
84 	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
85 		rl.rlim_cur = rl.rlim_max;
86 		if (setrlimit(RLIMIT_DATA, &rl) < 0)
87 			warn("can't get resource limit to max data size");
88 	} else
89 		warn("can't get resource limit for data size");
90 
91 	for (n = optind; n < argc; n++) {
92 		if (argc - optind != 1)
93 			puts(argv[n]);
94 		ex += fsirand(argv[n]);
95 		if (n < argc - 1)
96 			putchar('\n');
97 	}
98 
99 	exit(ex);
100 }
101 
102 int
103 fsirand(char *device)
104 {
105 	static struct ufs1_dinode *inodebuf;
106 	static ssize_t oldibufsize = 0;
107 	ssize_t ibufsize;
108 	struct fs *sblock;
109 	ino_t inumber, maxino;
110 	daddr_t dblk;
111 	char sbuf[SBSIZE], sbuftmp[SBSIZE];
112 	int devfd, n, cg;
113 	u_int32_t bsize = DEV_BSIZE;
114 
115 	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
116 		warn("can't open %s", device);
117 		return (1);
118 	}
119 
120 	/* Get block size (usually 512) from partinfo if possible */
121 	if (!ignorelabel) {
122 		struct partinfo pinfo;
123 
124 		if (ioctl(devfd, DIOCGPART, &pinfo) < 0) {
125 			fprintf(stderr,
126 			     "can't read partition info, assuming sector "
127 			     "size of %d\n", bsize);
128 		} else {
129 			bsize = pinfo.media_blksize;
130 		}
131 	}
132 
133 	/* Read in master superblock */
134 	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 (%jd) on %s",
138 		    (intmax_t)SBOFF, device);
139 		return (1);
140 	}
141 	if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
142 		warnx("can't read superblock on %s: %s", device,
143 		    (n < SBSIZE) ? "short read" : strerror(errno));
144 		return (1);
145 	}
146 	maxino = sblock->fs_ncg * sblock->fs_ipg;
147 
148 	/* Simple sanity checks on the superblock */
149 	if (sblock->fs_magic != FS_MAGIC) {
150 		warnx("bad magic number in superblock");
151 		return (1);
152 	}
153 	if (sblock->fs_sbsize > SBSIZE) {
154 		warnx("superblock size is preposterous");
155 		return (1);
156 	}
157 	if (sblock->fs_postblformat == FS_42POSTBLFMT) {
158 		warnx("filesystem format is too old, sorry");
159 		return (1);
160 	}
161 	if (!force && !printonly && sblock->fs_clean != 1) {
162 		warnx("filesystem is not clean, fsck %s first", device);
163 		return (1);
164 	}
165 
166 	/* Make sure backup superblocks are sane. */
167 	sblock = (struct fs *)&sbuftmp;
168 	for (cg = 0; cg < sblock->fs_ncg; cg++) {
169 		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
170 		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
171 			warn("can't seek to %jd", (intmax_t)dblk * bsize);
172 			return (1);
173 		} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
174 			warn("can't read backup superblock %d on %s: %s",
175 			    cg + 1, device, (n < SBSIZE) ? "short write"
176 			    : strerror(errno));
177 			return (1);
178 		}
179 		if (sblock->fs_magic != FS_MAGIC) {
180 			warnx("bad magic number in backup superblock %d on %s",
181 			    cg + 1, device);
182 			return (1);
183 		}
184 		if (sblock->fs_sbsize > SBSIZE) {
185 			warnx("size of backup superblock %d on %s is preposterous",
186 			    cg + 1, device);
187 			return (1);
188 		}
189 	}
190 	sblock = (struct fs *)&sbuf;
191 
192 	/* XXX - should really cap buffer at 512kb or so */
193 	ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
194 	if (oldibufsize < ibufsize) {
195 		if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
196 			errx(1, "can't allocate memory for inode buffer");
197 		oldibufsize = ibufsize;
198 	}
199 
200 	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
201 		if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
202 			printf("%s was randomized on %s", device,
203 			    ctime((const time_t *)&(sblock->fs_id[0])));
204 		printf("fsid: %x %x\n", sblock->fs_id[0],
205 		    sblock->fs_id[1]);
206 	}
207 
208 	/* Randomize fs_id unless old 4.2BSD filesystem */
209 	if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
210 		/* Randomize fs_id and write out new sblock and backups */
211 		sblock->fs_id[0] = (u_int32_t)time(NULL);
212 		sblock->fs_id[1] = random();
213 
214 		if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
215 			warn("can't seek to superblock (%jd) on %s",
216 			     (intmax_t)SBOFF, device);
217 			return (1);
218 		}
219 		if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
220 			warn("can't read superblock on %s: %s", device,
221 			    (n < SBSIZE) ? "short write" : strerror(errno));
222 			return (1);
223 		}
224 	}
225 
226 	/* For each cylinder group, randomize inodes and update backup sblock */
227 	for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
228 		/* Update superblock if appropriate */
229 		if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
230 			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
231 			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
232 				warn("can't seek to %jd",
233 				     (intmax_t)dblk * bsize);
234 				return (1);
235 			} else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
236 				warn("can't read backup superblock %d on %s: %s",
237 				    cg + 1, device, (n < SBSIZE) ? "short write"
238 				    : strerror(errno));
239 				return (1);
240 			}
241 		}
242 
243 		/* Read in inodes, then print or randomize generation nums */
244 		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
245 		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
246 			warn("can't seek to %jd", (intmax_t)dblk * bsize);
247 			return (1);
248 		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
249 			warnx("can't read inodes: %s",
250 			     (n < ibufsize) ? "short read" : strerror(errno));
251 			return (1);
252 		}
253 
254 		for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
255 			if (inumber >= ROOTINO) {
256 				if (printonly) {
257 					printf("ino %jd gen %x\n",
258 					       (intmax_t)inumber,
259 					       inodebuf[n].di_gen);
260 				} else {
261 					inodebuf[n].di_gen = random();
262 				}
263 			}
264 		}
265 
266 		/* Write out modified inodes */
267 		if (!printonly) {
268 			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
269 				warn("can't seek to %jd",
270 				    (intmax_t)dblk * bsize);
271 				return (1);
272 			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
273 				 ibufsize) {
274 				warnx("can't write inodes: %s",
275 				     (n != ibufsize) ? "short write" :
276 				     strerror(errno));
277 				return (1);
278 			}
279 		}
280 	}
281 	close(devfd);
282 
283 	return(0);
284 }
285 
286 static void
287 usage(void)
288 {
289 	fprintf(stderr,
290 	    "usage: fsirand [-b] [-f] [-p] special [special ...]\n");
291 	exit(1);
292 }
293