4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * fsirand installs random inode generation numbers on all the inodes on
39 * device <special>, and also installs a file system ID in the superblock.
40 * This helps increase the security of file systems exported by NFS.
49 #include <sys/param.h>
50 #include <sys/types.h>
52 #include <sys/fs/ufs_fs.h>
53 #include <sys/vnode.h>
54 #include <sys/fs/ufs_inode.h>
56 long fsbuf
[(SBSIZE
/ sizeof (long))];
57 struct dinode dibuf
[8192/sizeof (struct dinode
)];
60 main(int argc
, char *argv
[])
72 struct timeval timeval
;
76 if (argc
> 0 && strcmp(*argv
, "-p") == 0) {
82 (void) fprintf(stderr
, "Usage: fsirand [-p] special\n");
86 fd
= open64(dev
, pflag
? O_RDONLY
: O_RDWR
);
88 (void) fprintf(stderr
, "fsirand: Cannot open %s: %s\n", dev
,
92 if (llseek(fd
, (offset_t
)SBLOCK
* DEV_BSIZE
, 0) == -1) {
93 (void) fprintf(stderr
,
94 "fsirand: Seek to superblock failed: %s\n",
98 fs
= (struct fs
*)fsbuf
;
99 if ((n
= read(fd
, (char *)fs
, SBSIZE
)) != SBSIZE
) {
100 (void) fprintf(stderr
,
101 "fsirand: Read of superblock failed: %s\n",
102 n
== -1 ? strerror(errno
) : "Short read");
105 if ((fs
->fs_magic
!= FS_MAGIC
) &&
106 (fs
->fs_magic
!= MTB_UFS_MAGIC
)) {
107 (void) fprintf(stderr
,
108 "fsirand: Not a file system (bad magic number in superblock)\n");
111 if (fs
->fs_magic
== FS_MAGIC
&&
112 (fs
->fs_version
!= UFS_EFISTYLE4NONEFI_VERSION_2
&&
113 fs
->fs_version
!= UFS_VERSION_MIN
)) {
114 (void) fprintf(stderr
,
115 "fsirand: Unrecognized UFS format version number %d (in superblock)\n",
119 if (fs
->fs_magic
== MTB_UFS_MAGIC
&&
120 (fs
->fs_version
> MTB_UFS_VERSION_1
||
121 fs
->fs_version
< MTB_UFS_VERSION_MIN
)) {
122 (void) fprintf(stderr
,
123 "fsirand: Unrecognized UFS format version number %d (in superblock)\n",
128 (void) printf("fsid: %x %x\n", fs
->fs_id
[0], fs
->fs_id
[1]);
131 (void) gettimeofday(&timeval
, NULL
);
132 srand48((long)(timeval
.tv_sec
+ timeval
.tv_usec
+ n
));
134 bsize
= INOPB(fs
) * sizeof (struct dinode
);
136 imax
= fs
->fs_ipg
* fs
->fs_ncg
;
137 while (inum
< imax
) {
138 bno
= itod(fs
, inum
);
139 seekaddr
= (offset_t
)fsbtodb(fs
, bno
) * DEV_BSIZE
;
140 if (llseek(fd
, seekaddr
, 0) == -1) {
141 (void) fprintf(stderr
,
142 "fsirand: Seek to %ld %ld failed: %s\n",
143 ((off_t
*)&seekaddr
)[0], ((off_t
*)&seekaddr
)[1],
147 n
= read(fd
, (char *)dibuf
, bsize
);
149 (void) fprintf(stderr
,
150 "fsirand: Read of ilist block failed: %s\n",
151 n
== -1 ? strerror(errno
) : "Short read");
154 for (dip
= dibuf
; dip
< &dibuf
[INOPB(fs
)]; dip
++) {
156 (void) printf("ino %d gen %x\n", inum
,
159 dip
->di_gen
= lrand48();
164 if (llseek(fd
, seekaddr
, 0) == -1) {
165 (void) fprintf(stderr
,
166 "fsirand: Seek to %ld %ld failed: %s\n",
167 ((off_t
*)&seekaddr
)[0],
168 ((off_t
*)&seekaddr
)[1],
172 n
= write(fd
, (char *)dibuf
, bsize
);
174 (void) fprintf(stderr
,
175 "fsirand: Write of ilist block failed: %s\n",
176 n
== -1 ? strerror(errno
) : "Short write");
182 (void) gettimeofday(&timeval
, NULL
);
183 fs
->fs_id
[0] = timeval
.tv_sec
;
184 fs
->fs_id
[1] = timeval
.tv_usec
+ getpid();
185 if (llseek(fd
, (offset_t
)SBLOCK
* DEV_BSIZE
, 0) == -1) {
186 (void) fprintf(stderr
,
187 "fsirand: Seek to superblock failed: %s\n",
191 if ((n
= write(fd
, (char *)fs
, SBSIZE
)) != SBSIZE
) {
192 (void) fprintf(stderr
,
193 "fsirand: Write of superblock failed: %s\n",
194 n
== -1 ? strerror(errno
) : "Short write");
198 for (i
= 0; i
< fs
->fs_ncg
; i
++) {
199 seekaddr
= (offset_t
)fsbtodb(fs
, cgsblock(fs
, i
)) * DEV_BSIZE
;
200 if (llseek(fd
, seekaddr
, 0) == -1) {
201 (void) fprintf(stderr
,
202 "fsirand: Seek to alternate superblock failed: %s\n",
207 if ((n
= read(fd
, (char *)fs
, SBSIZE
)) != SBSIZE
) {
208 (void) fprintf(stderr
,
209 "fsirand: Read of alternate superblock failed: %s\n",
210 n
== -1 ? strerror(errno
) : "Short read");
213 if ((fs
->fs_magic
!= FS_MAGIC
) &&
214 (fs
->fs_magic
!= MTB_UFS_MAGIC
)) {
215 (void) fprintf(stderr
,
216 "fsirand: Not a valid file system (bad "
217 "magic number in alternate superblock)\n");
221 if ((n
= write(fd
, (char *)fs
, SBSIZE
)) != SBSIZE
) {
222 (void) fprintf(stderr
,
223 "fsirand: Write of alternate superblock failed: %s\n",
224 n
== -1 ? strerror(errno
) : "Short write");