1 /* $OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Todd C. Miller.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 static const char rcsid[] =
40 #include <sys/param.h>
41 #include <sys/resource.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ffs/fs.h>
56 static void usage(void) __dead2;
60 * Possible superblock locations ordered from most to least likely.
62 static int sblock_try[] = SBLOCKSEARCH;
64 static int printonly = 0, force = 0, ignorelabel = 0;
67 main(int argc, char *argv[])
72 while ((n = getopt(argc, argv, "bfp")) != -1) {
87 if (argc - optind < 1)
92 /* Increase our data size to the max */
93 if (getrlimit(RLIMIT_DATA, &rl) == 0) {
94 rl.rlim_cur = rl.rlim_max;
95 if (setrlimit(RLIMIT_DATA, &rl) < 0)
96 warn("can't get resource limit to max data size");
98 warn("can't get resource limit for data size");
100 for (n = optind; n < argc; n++) {
101 if (argc - optind != 1)
103 ex += fsirand(argv[n]);
112 fsirand(char *device)
114 struct ufs1_dinode *dp1;
115 struct ufs2_dinode *dp2;
120 ufs2_daddr_t sblockloc, dblk;
121 char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
123 u_int32_t bsize = DEV_BSIZE;
125 if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
126 warn("can't open %s", device);
133 /* Read in master superblock */
134 (void)memset(&sbuf, 0, sizeof(sbuf));
135 sblock = (struct fs *)&sbuf;
136 for (i = 0; sblock_try[i] != -1; i++) {
137 sblockloc = sblock_try[i];
138 if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
139 warn("can't seek to superblock (%jd) on %s",
140 (intmax_t)sblockloc, device);
143 if ((n = read(devfd, (void *)sblock, SBLOCKSIZE))!=SBLOCKSIZE) {
144 warnx("can't read superblock on %s: %s", device,
145 (n < SBLOCKSIZE) ? "short read" : strerror(errno));
148 if ((sblock->fs_magic == FS_UFS1_MAGIC ||
149 (sblock->fs_magic == FS_UFS2_MAGIC &&
150 sblock->fs_sblockloc == sblock_try[i])) &&
151 sblock->fs_bsize <= MAXBSIZE &&
152 sblock->fs_bsize >= (ssize_t)sizeof(struct fs))
155 if (sblock_try[i] == -1) {
156 fprintf(stderr, "Cannot find file system superblock\n");
160 if (sblock->fs_magic == FS_UFS1_MAGIC &&
161 sblock->fs_old_inodefmt < FS_44INODEFMT) {
162 warnx("file system format is too old, sorry");
165 if (!force && !printonly && sblock->fs_clean != 1) {
166 warnx("file system is not clean, fsck %s first", device);
170 /* Make sure backup superblocks are sane. */
171 sblock = (struct fs *)&sbuftmp;
172 for (cg = 0; cg < (int)sblock->fs_ncg; cg++) {
173 dblk = fsbtodb(sblock, cgsblock(sblock, cg));
174 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
175 warn("can't seek to %jd", (intmax_t)dblk * bsize);
177 } else if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) != SBLOCKSIZE) {
178 warn("can't read backup superblock %d on %s: %s",
179 cg + 1, device, (n < SBLOCKSIZE) ? "short write"
183 if (sblock->fs_magic != FS_UFS1_MAGIC &&
184 sblock->fs_magic != FS_UFS2_MAGIC) {
185 warnx("bad magic number in backup superblock %d on %s",
189 if (sblock->fs_sbsize > SBLOCKSIZE) {
190 warnx("size of backup superblock %d on %s is preposterous",
195 sblock = (struct fs *)&sbuf;
197 /* XXX - should really cap buffer at 512kb or so */
198 if (sblock->fs_magic == FS_UFS1_MAGIC)
199 ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
201 ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
202 if ((inodebuf = malloc(ibufsize)) == NULL)
203 errx(1, "can't allocate memory for inode buffer");
205 if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
206 if (sblock->fs_id[0])
207 (void)printf("%s was randomized on %s", device,
208 ctime((void *)&(sblock->fs_id[0])));
209 (void)printf("fsid: %x %x\n", sblock->fs_id[0],
213 /* Randomize fs_id unless old 4.2BSD file system */
215 /* Randomize fs_id and write out new sblock and backups */
216 sblock->fs_id[0] = (u_int32_t)time(NULL);
217 sblock->fs_id[1] = random();
219 if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
220 warn("can't seek to superblock (%jd) on %s",
221 (intmax_t)sblockloc, device);
224 if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
226 warn("can't write superblock on %s: %s", device,
227 (n < SBLOCKSIZE) ? "short write" : strerror(errno));
232 /* For each cylinder group, randomize inodes and update backup sblock */
233 for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) {
234 /* Update superblock if appropriate */
236 dblk = fsbtodb(sblock, cgsblock(sblock, cg));
237 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
238 warn("can't seek to %jd",
239 (intmax_t)dblk * bsize);
241 } else if ((n = write(devfd, (void *)sblock,
242 SBLOCKSIZE)) != SBLOCKSIZE) {
243 warn("can't write backup superblock %d on %s: %s",
244 cg + 1, device, (n < SBLOCKSIZE) ?
245 "short write" : strerror(errno));
250 /* Read in inodes, then print or randomize generation nums */
251 dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
252 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
253 warn("can't seek to %jd", (intmax_t)dblk * bsize);
255 } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
256 warnx("can't read inodes: %s",
257 (n < ibufsize) ? "short read" : strerror(errno));
261 for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) {
262 if (sblock->fs_magic == FS_UFS1_MAGIC)
263 dp1 = &((struct ufs1_dinode *)inodebuf)[n];
265 dp2 = &((struct ufs2_dinode *)inodebuf)[n];
266 if (inumber >= ROOTINO) {
268 (void)printf("ino %ju gen %08x\n",
270 sblock->fs_magic == FS_UFS1_MAGIC ?
271 dp1->di_gen : dp2->di_gen);
272 else if (sblock->fs_magic == FS_UFS1_MAGIC)
273 dp1->di_gen = random();
275 dp2->di_gen = random();
279 /* Write out modified inodes */
281 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
282 warn("can't seek to %jd",
283 (intmax_t)dblk * bsize);
285 } else if ((n = write(devfd, inodebuf, ibufsize)) !=
287 warnx("can't write inodes: %s",
288 (n != ibufsize) ? "short write" :
302 (void)fprintf(stderr,
303 "usage: fsirand [-b] [-f] [-p] special [special ...]\n");