xref: /freebsd/sbin/fsirand/fsirand.c (revision 3157ba21)
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 
33 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37 
38 #include <sys/disklabel.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ffs/fs.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdio.h>
50 #include <stdint.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 static void usage(void) __dead2;
56 int fsirand(char *);
57 
58 /*
59  * Possible superblock locations ordered from most to least likely.
60  */
61 static int sblock_try[] = SBLOCKSEARCH;
62 
63 int printonly = 0, force = 0, ignorelabel = 0;
64 
65 int
66 main(int argc, char *argv[])
67 {
68 	int n, ex = 0;
69 	struct rlimit rl;
70 
71 	while ((n = getopt(argc, argv, "bfp")) != -1) {
72 		switch (n) {
73 		case 'b':
74 			ignorelabel++;
75 			break;
76 		case 'p':
77 			printonly++;
78 			break;
79 		case 'f':
80 			force++;
81 			break;
82 		default:
83 			usage();
84 		}
85 	}
86 	if (argc - optind < 1)
87 		usage();
88 
89 	srandomdev();
90 
91 	/* Increase our data size to the max */
92 	if (getrlimit(RLIMIT_DATA, &rl) == 0) {
93 		rl.rlim_cur = rl.rlim_max;
94 		if (setrlimit(RLIMIT_DATA, &rl) < 0)
95 			warn("can't get resource limit to max data size");
96 	} else
97 		warn("can't get resource limit for data size");
98 
99 	for (n = optind; n < argc; n++) {
100 		if (argc - optind != 1)
101 			(void)puts(argv[n]);
102 		ex += fsirand(argv[n]);
103 		if (n < argc - 1)
104 			putchar('\n');
105 	}
106 
107 	exit(ex);
108 }
109 
110 int
111 fsirand(char *device)
112 {
113 	struct ufs1_dinode *dp1;
114 	struct ufs2_dinode *dp2;
115 	caddr_t inodebuf;
116 	ssize_t ibufsize;
117 	struct fs *sblock;
118 	ino_t inumber, maxino;
119 	ufs2_daddr_t sblockloc, dblk;
120 	char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
121 	int i, devfd, n, cg;
122 	u_int32_t bsize = DEV_BSIZE;
123 	struct disklabel label;
124 
125 	if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
126 		warn("can't open %s", device);
127 		return (1);
128 	}
129 
130 	/* Get block size (usually 512) from disklabel if possible */
131 	if (!ignorelabel) {
132 		if (ioctl(devfd, DIOCGDINFO, &label) < 0)
133 			warn("can't read disklabel, using sector size of %d",
134 			    bsize);
135 		else
136 			bsize = label.d_secsize;
137 	}
138 
139 	dp1 = NULL;
140 	dp2 = NULL;
141 
142 	/* Read in master superblock */
143 	(void)memset(&sbuf, 0, sizeof(sbuf));
144 	sblock = (struct fs *)&sbuf;
145 	for (i = 0; sblock_try[i] != -1; i++) {
146 		sblockloc = sblock_try[i];
147 		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
148 			warn("can't seek to superblock (%jd) on %s",
149 			    (intmax_t)sblockloc, device);
150 			return (1);
151 		}
152 		if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) {
153 			warnx("can't read superblock on %s: %s", device,
154 			    (n < SBLOCKSIZE) ? "short read" : strerror(errno));
155 			return (1);
156 		}
157 		if ((sblock->fs_magic == FS_UFS1_MAGIC ||
158 		     (sblock->fs_magic == FS_UFS2_MAGIC &&
159 		      sblock->fs_sblockloc == sblock_try[i])) &&
160 		    sblock->fs_bsize <= MAXBSIZE &&
161 		    sblock->fs_bsize >= (ssize_t)sizeof(struct fs))
162 			break;
163 	}
164 	if (sblock_try[i] == -1) {
165 		fprintf(stderr, "Cannot find file system superblock\n");
166 		return (1);
167 	}
168 	maxino = sblock->fs_ncg * sblock->fs_ipg;
169 
170 	if (sblock->fs_magic == FS_UFS1_MAGIC &&
171 	    sblock->fs_old_inodefmt < FS_44INODEFMT) {
172 		warnx("file system format is too old, sorry");
173 		return (1);
174 	}
175 	if (!force && !printonly && sblock->fs_clean != 1) {
176 		warnx("file system is not clean, fsck %s first", device);
177 		return (1);
178 	}
179 
180 	/* Make sure backup superblocks are sane. */
181 	sblock = (struct fs *)&sbuftmp;
182 	for (cg = 0; cg < (int)sblock->fs_ncg; cg++) {
183 		dblk = fsbtodb(sblock, cgsblock(sblock, cg));
184 		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
185 			warn("can't seek to %jd", (intmax_t)dblk * bsize);
186 			return (1);
187 		} else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) {
188 			warn("can't read backup superblock %d on %s: %s",
189 			    cg + 1, device, (n < SBLOCKSIZE) ? "short write"
190 			    : strerror(errno));
191 			return (1);
192 		}
193 		if (sblock->fs_magic != FS_UFS1_MAGIC &&
194 		    sblock->fs_magic != FS_UFS2_MAGIC) {
195 			warnx("bad magic number in backup superblock %d on %s",
196 			    cg + 1, device);
197 			return (1);
198 		}
199 		if (sblock->fs_sbsize > SBLOCKSIZE) {
200 			warnx("size of backup superblock %d on %s is preposterous",
201 			    cg + 1, device);
202 			return (1);
203 		}
204 	}
205 	sblock = (struct fs *)&sbuf;
206 
207 	/* XXX - should really cap buffer at 512kb or so */
208 	if (sblock->fs_magic == FS_UFS1_MAGIC)
209 		ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
210 	else
211 		ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
212 	if ((inodebuf = malloc(ibufsize)) == NULL)
213 		errx(1, "can't allocate memory for inode buffer");
214 
215 	if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
216 		if (sblock->fs_id[0])
217 			(void)printf("%s was randomized on %s", device,
218 			    ctime((void *)&(sblock->fs_id[0])));
219 		(void)printf("fsid: %x %x\n", sblock->fs_id[0],
220 			    sblock->fs_id[1]);
221 	}
222 
223 	/* Randomize fs_id unless old 4.2BSD file system */
224 	if (!printonly) {
225 		/* Randomize fs_id and write out new sblock and backups */
226 		sblock->fs_id[0] = (u_int32_t)time(NULL);
227 		sblock->fs_id[1] = random();
228 
229 		if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
230 			warn("can't seek to superblock (%jd) on %s",
231 			    (intmax_t)sblockloc, device);
232 			return (1);
233 		}
234 		if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
235 		    SBLOCKSIZE) {
236 			warn("can't write superblock on %s: %s", device,
237 			    (n < SBLOCKSIZE) ? "short write" : strerror(errno));
238 			return (1);
239 		}
240 	}
241 
242 	/* For each cylinder group, randomize inodes and update backup sblock */
243 	for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) {
244 		/* Update superblock if appropriate */
245 		if (!printonly) {
246 			dblk = fsbtodb(sblock, cgsblock(sblock, cg));
247 			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
248 				warn("can't seek to %jd",
249 				    (intmax_t)dblk * bsize);
250 				return (1);
251 			} else if ((n = write(devfd, (void *)sblock,
252 			    SBLOCKSIZE)) != SBLOCKSIZE) {
253 			      warn("can't write backup superblock %d on %s: %s",
254 				    cg + 1, device, (n < SBLOCKSIZE) ?
255 				    "short write" : strerror(errno));
256 				return (1);
257 			}
258 		}
259 
260 		/* Read in inodes, then print or randomize generation nums */
261 		dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
262 		if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
263 			warn("can't seek to %jd", (intmax_t)dblk * bsize);
264 			return (1);
265 		} else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
266 			warnx("can't read inodes: %s",
267 			     (n < ibufsize) ? "short read" : strerror(errno));
268 			return (1);
269 		}
270 
271 		for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) {
272 			if (sblock->fs_magic == FS_UFS1_MAGIC)
273 				dp1 = &((struct ufs1_dinode *)inodebuf)[n];
274 			else
275 				dp2 = &((struct ufs2_dinode *)inodebuf)[n];
276 			if (inumber >= ROOTINO) {
277 				if (printonly)
278 					(void)printf("ino %d gen %08x\n",
279 					    inumber,
280 					    sblock->fs_magic == FS_UFS1_MAGIC ?
281 					    dp1->di_gen : dp2->di_gen);
282 				else if (sblock->fs_magic == FS_UFS1_MAGIC)
283 					dp1->di_gen = random();
284 				else
285 					dp2->di_gen = random();
286 			}
287 		}
288 
289 		/* Write out modified inodes */
290 		if (!printonly) {
291 			if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
292 				warn("can't seek to %jd",
293 				    (intmax_t)dblk * bsize);
294 				return (1);
295 			} else if ((n = write(devfd, inodebuf, ibufsize)) !=
296 				 ibufsize) {
297 				warnx("can't write inodes: %s",
298 				     (n != ibufsize) ? "short write" :
299 				     strerror(errno));
300 				return (1);
301 			}
302 		}
303 	}
304 	(void)close(devfd);
305 
306 	return(0);
307 }
308 
309 static void
310 usage(void)
311 {
312 	(void)fprintf(stderr,
313 		"usage: fsirand [-b] [-f] [-p] special [special ...]\n");
314 	exit(1);
315 }
316