K&R style function removal. Update functions to ANSI style.
[dragonfly.git] / sbin / fsirand / fsirand.c
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.4 2003/09/28 14:39:18 hmp Exp $
34  */
35
36 #include <sys/disklabel.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/resource.h>
40
41 #include <vfs/ufs/fs.h>
42 #include <vfs/ufs/dinode.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 __P((void));
53 int fsirand __P((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                         (void)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 dinode *inodebuf;
106         static size_t oldibufsize;
107         size_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         struct disklabel label;
115
116         if ((devfd = open(device, printonly ? O_RDONLY : O_RDWR)) < 0) {
117                 warn("can't open %s", device);
118                 return (1);
119         }
120
121         /* Get block size (usually 512) from disklabel if possible */
122         if (!ignorelabel) {
123                 if (ioctl(devfd, DIOCGDINFO, &label) < 0)
124                         warn("can't read disklabel, using sector size of %d",
125                             bsize);
126                 else
127                         bsize = label.d_secsize;
128         }
129
130         /* Read in master superblock */
131         (void)memset(&sbuf, 0, sizeof(sbuf));
132         sblock = (struct fs *)&sbuf;
133         if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
134                 warn("can't seek to superblock (%qd) on %s", SBOFF, device);
135                 return (1);
136         }
137         if ((n = read(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
138                 warnx("can't read superblock on %s: %s", device,
139                     (n < SBSIZE) ? "short read" : strerror(errno));
140                 return (1);
141         }
142         maxino = sblock->fs_ncg * sblock->fs_ipg;
143
144         /* Simple sanity checks on the superblock */
145         if (sblock->fs_magic != FS_MAGIC) {
146                 warnx("bad magic number in superblock");
147                 return (1);
148         }
149         if (sblock->fs_sbsize > SBSIZE) {
150                 warnx("superblock size is preposterous");
151                 return (1);
152         }
153         if (sblock->fs_postblformat == FS_42POSTBLFMT) {
154                 warnx("filesystem format is too old, sorry");
155                 return (1);
156         }
157         if (!force && !printonly && sblock->fs_clean != 1) {
158                 warnx("filesystem is not clean, fsck %s first", device);
159                 return (1);
160         }
161
162         /* Make sure backup superblocks are sane. */
163         sblock = (struct fs *)&sbuftmp;
164         for (cg = 0; cg < sblock->fs_ncg; cg++) {
165                 dblk = fsbtodb(sblock, cgsblock(sblock, cg));
166                 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
167                         warn("can't seek to %qd", (off_t)dblk * bsize);
168                         return (1);
169                 } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
170                         warn("can't read backup superblock %d on %s: %s",
171                             cg + 1, device, (n < SBSIZE) ? "short write"
172                             : strerror(errno));
173                         return (1);
174                 }
175                 if (sblock->fs_magic != FS_MAGIC) {
176                         warnx("bad magic number in backup superblock %d on %s",
177                             cg + 1, device);
178                         return (1);
179                 }
180                 if (sblock->fs_sbsize > SBSIZE) {
181                         warnx("size of backup superblock %d on %s is preposterous",
182                             cg + 1, device);
183                         return (1);
184                 }
185         }
186         sblock = (struct fs *)&sbuf;
187
188         /* XXX - should really cap buffer at 512kb or so */
189         ibufsize = sizeof(struct dinode) * sblock->fs_ipg;
190         if (oldibufsize < ibufsize) {
191                 if ((inodebuf = realloc(inodebuf, ibufsize)) == NULL)
192                         errx(1, "can't allocate memory for inode buffer");
193                 oldibufsize = ibufsize;
194         }
195
196         if (printonly && (sblock->fs_id[0] || sblock->fs_id[1])) {
197                 if (sblock->fs_inodefmt >= FS_44INODEFMT && sblock->fs_id[0])
198                         (void)printf("%s was randomized on %s", device,
199                             ctime((const time_t *)&(sblock->fs_id[0])));
200                 (void)printf("fsid: %x %x\n", sblock->fs_id[0],
201                             sblock->fs_id[1]);
202         }
203
204         /* Randomize fs_id unless old 4.2BSD filesystem */
205         if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
206                 /* Randomize fs_id and write out new sblock and backups */
207                 sblock->fs_id[0] = (u_int32_t)time(NULL);
208                 sblock->fs_id[1] = random();
209
210                 if (lseek(devfd, SBOFF, SEEK_SET) == -1) {
211                         warn("can't seek to superblock (%qd) on %s", SBOFF,
212                             device);
213                         return (1);
214                 }
215                 if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
216                         warn("can't read superblock on %s: %s", device,
217                             (n < SBSIZE) ? "short write" : strerror(errno));
218                         return (1);
219                 }
220         }
221
222         /* For each cylinder group, randomize inodes and update backup sblock */
223         for (cg = 0, inumber = 0; cg < sblock->fs_ncg; cg++) {
224                 /* Update superblock if appropriate */
225                 if ((sblock->fs_inodefmt >= FS_44INODEFMT) && !printonly) {
226                         dblk = fsbtodb(sblock, cgsblock(sblock, cg));
227                         if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
228                                 warn("can't seek to %qd", (off_t)dblk * bsize);
229                                 return (1);
230                         } else if ((n = write(devfd, (void *)sblock, SBSIZE)) != SBSIZE) {
231                                 warn("can't read backup superblock %d on %s: %s",
232                                     cg + 1, device, (n < SBSIZE) ? "short write"
233                                     : strerror(errno));
234                                 return (1);
235                         }
236                 }
237
238                 /* Read in inodes, then print or randomize generation nums */
239                 dblk = fsbtodb(sblock, ino_to_fsba(sblock, inumber));
240                 if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
241                         warn("can't seek to %qd", (off_t)dblk * bsize);
242                         return (1);
243                 } else if ((n = read(devfd, inodebuf, ibufsize)) != ibufsize) {
244                         warnx("can't read inodes: %s",
245                              (n < ibufsize) ? "short read" : strerror(errno));
246                         return (1);
247                 }
248
249                 for (n = 0; n < sblock->fs_ipg; n++, inumber++) {
250                         if (inumber >= ROOTINO) {
251                                 if (printonly)
252                                         (void)printf("ino %d gen %x\n", inumber,
253                                                      inodebuf[n].di_gen);
254                                 else
255                                         inodebuf[n].di_gen = random();
256                         }
257                 }
258
259                 /* Write out modified inodes */
260                 if (!printonly) {
261                         if (lseek(devfd, (off_t)dblk * bsize, SEEK_SET) < 0) {
262                                 warn("can't seek to %qd",
263                                     (off_t)dblk * bsize);
264                                 return (1);
265                         } else if ((n = write(devfd, inodebuf, ibufsize)) !=
266                                  ibufsize) {
267                                 warnx("can't write inodes: %s",
268                                      (n != ibufsize) ? "short write" :
269                                      strerror(errno));
270                                 return (1);
271                         }
272                 }
273         }
274         (void)close(devfd);
275
276         return(0);
277 }
278
279 static void
280 usage(void)
281 {
282         (void)fprintf(stderr, 
283                 "usage: fsirand [-b] [-f] [-p] special [special ...]\n");
284         exit(1);
285 }