]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsirand/fsirand.c
MFC r326276:
[FreeBSD/FreeBSD.git] / sbin / fsirand / fsirand.c
1 /*      $OpenBSD: fsirand.c,v 1.9 1997/02/28 00:46:33 millert Exp $     */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5  *
6  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
22  *
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.
33  */
34
35 #ifndef lint
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39
40 #include <sys/param.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 <time.h>
54 #include <unistd.h>
55
56 static void usage(void) __dead2;
57 int fsirand(char *);
58
59 /*
60  * Possible superblock locations ordered from most to least likely.
61  */
62 static int sblock_try[] = SBLOCKSEARCH;
63
64 static int printonly = 0, force = 0, ignorelabel = 0;
65
66 int
67 main(int argc, char *argv[])
68 {
69         int n, ex = 0;
70         struct rlimit rl;
71
72         while ((n = getopt(argc, argv, "bfp")) != -1) {
73                 switch (n) {
74                 case 'b':
75                         ignorelabel++;
76                         break;
77                 case 'p':
78                         printonly++;
79                         break;
80                 case 'f':
81                         force++;
82                         break;
83                 default:
84                         usage();
85                 }
86         }
87         if (argc - optind < 1)
88                 usage();
89
90         srandomdev();
91
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");
97         } else
98                 warn("can't get resource limit for data size");
99
100         for (n = optind; n < argc; n++) {
101                 if (argc - optind != 1)
102                         (void)puts(argv[n]);
103                 ex += fsirand(argv[n]);
104                 if (n < argc - 1)
105                         putchar('\n');
106         }
107
108         exit(ex);
109 }
110
111 int
112 fsirand(char *device)
113 {
114         struct ufs1_dinode *dp1;
115         struct ufs2_dinode *dp2;
116         caddr_t inodebuf;
117         ssize_t ibufsize;
118         struct fs *sblock;
119         ino_t inumber;
120         ufs2_daddr_t sblockloc, dblk;
121         char sbuf[SBLOCKSIZE], sbuftmp[SBLOCKSIZE];
122         int i, devfd, n, cg;
123         u_int32_t bsize = DEV_BSIZE;
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         dp1 = NULL;
131         dp2 = NULL;
132
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);
141                         return (1);
142                 }
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));
146                         return (1);
147                 }
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))
153                         break;
154         }
155         if (sblock_try[i] == -1) {
156                 fprintf(stderr, "Cannot find file system superblock\n");
157                 return (1);
158         }
159
160         if (sblock->fs_magic == FS_UFS1_MAGIC &&
161             sblock->fs_old_inodefmt < FS_44INODEFMT) {
162                 warnx("file system format is too old, sorry");
163                 return (1);
164         }
165         if (!force && !printonly && sblock->fs_clean != 1) {
166                 warnx("file system is not clean, fsck %s first", device);
167                 return (1);
168         }
169
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);
176                         return (1);
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"
180                             : strerror(errno));
181                         return (1);
182                 }
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",
186                             cg + 1, device);
187                         return (1);
188                 }
189                 if (sblock->fs_sbsize > SBLOCKSIZE) {
190                         warnx("size of backup superblock %d on %s is preposterous",
191                             cg + 1, device);
192                         return (1);
193                 }
194         }
195         sblock = (struct fs *)&sbuf;
196
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;
200         else
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");
204
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],
210                             sblock->fs_id[1]);
211         }
212
213         /* Randomize fs_id unless old 4.2BSD file system */
214         if (!printonly) {
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();
218
219                 if (lseek(devfd, sblockloc, SEEK_SET) == -1) {
220                         warn("can't seek to superblock (%jd) on %s",
221                             (intmax_t)sblockloc, device);
222                         return (1);
223                 }
224                 if ((n = write(devfd, (void *)sblock, SBLOCKSIZE)) !=
225                     SBLOCKSIZE) {
226                         warn("can't write superblock on %s: %s", device,
227                             (n < SBLOCKSIZE) ? "short write" : strerror(errno));
228                         return (1);
229                 }
230         }
231
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 */
235                 if (!printonly) {
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);
240                                 return (1);
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));
246                                 return (1);
247                         }
248                 }
249
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);
254                         return (1);
255                 } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
256                         warnx("can't read inodes: %s",
257                              (n < ibufsize) ? "short read" : strerror(errno));
258                         return (1);
259                 }
260
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];
264                         else
265                                 dp2 = &((struct ufs2_dinode *)inodebuf)[n];
266                         if (inumber >= ROOTINO) {
267                                 if (printonly)
268                                         (void)printf("ino %ju gen %08x\n",
269                                             (uintmax_t)inumber,
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(); 
274                                 else
275                                         dp2->di_gen = random();
276                         }
277                 }
278
279                 /* Write out modified inodes */
280                 if (!printonly) {
281                         if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
282                                 warn("can't seek to %jd",
283                                     (intmax_t)dblk * bsize);
284                                 return (1);
285                         } else if ((n = write(devfd, inodebuf, ibufsize)) !=
286                                  ibufsize) {
287                                 warnx("can't write inodes: %s",
288                                      (n != ibufsize) ? "short write" :
289                                      strerror(errno));
290                                 return (1);
291                         }
292                 }
293         }
294         (void)close(devfd);
295
296         return(0);
297 }
298
299 static void
300 usage(void)
301 {
302         (void)fprintf(stderr, 
303                 "usage: fsirand [-b] [-f] [-p] special [special ...]\n");
304         exit(1);
305 }