]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsirand/fsirand.c
Add UPDATING entries and bump version.
[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 <libufs.h>
50 #include <stdio.h>
51 #include <stdint.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56
57 static void usage(void) __dead2;
58 int fsirand(char *);
59
60 static int printonly = 0, force = 0, ignorelabel = 0;
61
62 int
63 main(int argc, char *argv[])
64 {
65         int n, ex = 0;
66         struct rlimit rl;
67
68         while ((n = getopt(argc, argv, "bfp")) != -1) {
69                 switch (n) {
70                 case 'b':
71                         ignorelabel++;
72                         break;
73                 case 'p':
74                         printonly++;
75                         break;
76                 case 'f':
77                         force++;
78                         break;
79                 default:
80                         usage();
81                 }
82         }
83         if (argc - optind < 1)
84                 usage();
85
86         srandomdev();
87
88         /* Increase our data size to the max */
89         if (getrlimit(RLIMIT_DATA, &rl) == 0) {
90                 rl.rlim_cur = rl.rlim_max;
91                 if (setrlimit(RLIMIT_DATA, &rl) < 0)
92                         warn("can't get resource limit to max data size");
93         } else
94                 warn("can't get resource limit for data size");
95
96         for (n = optind; n < argc; n++) {
97                 if (argc - optind != 1)
98                         (void)puts(argv[n]);
99                 ex += fsirand(argv[n]);
100                 if (n < argc - 1)
101                         putchar('\n');
102         }
103
104         exit(ex);
105 }
106
107 int
108 fsirand(char *device)
109 {
110         struct ufs1_dinode *dp1;
111         struct ufs2_dinode *dp2;
112         caddr_t inodebuf;
113         ssize_t ibufsize;
114         struct fs *sblock;
115         ino_t inumber;
116         ufs2_daddr_t dblk;
117         int devfd, n, cg, ret;
118         u_int32_t bsize = DEV_BSIZE;
119
120         if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
121                 warn("can't open %s", device);
122                 return (1);
123         }
124
125         dp1 = NULL;
126         dp2 = NULL;
127
128         /* Read in master superblock */
129         if ((ret = sbget(devfd, &sblock, -1)) != 0) {
130                 switch (ret) {
131                 case ENOENT:
132                         warn("Cannot find file system superblock");
133                         return (1);
134                 default:
135                         warn("Unable to read file system superblock");
136                         return (1);
137                 }
138         }
139
140         if (sblock->fs_magic == FS_UFS1_MAGIC &&
141             sblock->fs_old_inodefmt < FS_44INODEFMT) {
142                 warnx("file system format is too old, sorry");
143                 return (1);
144         }
145         if (!force && !printonly && sblock->fs_clean != 1) {
146                 warnx("file system is not clean, fsck %s first", device);
147                 return (1);
148         }
149
150         /* XXX - should really cap buffer at 512kb or so */
151         if (sblock->fs_magic == FS_UFS1_MAGIC)
152                 ibufsize = sizeof(struct ufs1_dinode) * sblock->fs_ipg;
153         else
154                 ibufsize = sizeof(struct ufs2_dinode) * sblock->fs_ipg;
155         if ((inodebuf = malloc(ibufsize)) == NULL)
156                 errx(1, "can't allocate memory for inode buffer");
157
158         if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
159                 if (sblock->fs_id[0])
160                         (void)printf("%s was randomized on %s", device,
161                             ctime((void *)&(sblock->fs_id[0])));
162                 (void)printf("fsid: %x %x\n", sblock->fs_id[0],
163                             sblock->fs_id[1]);
164         }
165
166         /* Randomize fs_id unless old 4.2BSD file system */
167         if (!printonly) {
168                 /* Randomize fs_id and write out new sblock and backups */
169                 sblock->fs_id[0] = (u_int32_t)time(NULL);
170                 sblock->fs_id[1] = random();
171                 if (sbput(devfd, sblock, sblock->fs_ncg) != 0) {
172                         warn("could not write updated superblock");
173                         return (1);
174                 }
175         }
176
177         /* For each cylinder group, randomize inodes and update backup sblock */
178         for (cg = 0, inumber = 0; cg < (int)sblock->fs_ncg; cg++) {
179                 /* Read in inodes, then print or randomize generation nums */
180                 dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
181                 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
182                         warn("can't seek to %jd", (intmax_t)dblk * bsize);
183                         return (1);
184                 } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
185                         warnx("can't read inodes: %s",
186                              (n < ibufsize) ? "short read" : strerror(errno));
187                         return (1);
188                 }
189
190                 for (n = 0; n < (int)sblock->fs_ipg; n++, inumber++) {
191                         if (sblock->fs_magic == FS_UFS1_MAGIC)
192                                 dp1 = &((struct ufs1_dinode *)inodebuf)[n];
193                         else
194                                 dp2 = &((struct ufs2_dinode *)inodebuf)[n];
195                         if (inumber >= UFS_ROOTINO) {
196                                 if (printonly)
197                                         (void)printf("ino %ju gen %08x\n",
198                                             (uintmax_t)inumber,
199                                             sblock->fs_magic == FS_UFS1_MAGIC ?
200                                             dp1->di_gen : dp2->di_gen);
201                                 else if (sblock->fs_magic == FS_UFS1_MAGIC) 
202                                         dp1->di_gen = random(); 
203                                 else
204                                         dp2->di_gen = random();
205                         }
206                 }
207
208                 /* Write out modified inodes */
209                 if (!printonly) {
210                         if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
211                                 warn("can't seek to %jd",
212                                     (intmax_t)dblk * bsize);
213                                 return (1);
214                         } else if ((n = write(devfd, inodebuf, ibufsize)) !=
215                                  ibufsize) {
216                                 warnx("can't write inodes: %s",
217                                      (n != ibufsize) ? "short write" :
218                                      strerror(errno));
219                                 return (1);
220                         }
221                 }
222         }
223         (void)close(devfd);
224
225         return(0);
226 }
227
228 static void
229 usage(void)
230 {
231         (void)fprintf(stderr, 
232                 "usage: fsirand [-b] [-f] [-p] special [special ...]\n");
233         exit(1);
234 }