xref: /titanic_50/usr/src/cmd/fs.d/ufs/fsirand/fsirand.c (revision 6451fdbca2f79129a3a09d2fe3f6dd4d062bebff)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23ace1a5f1Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
38ace1a5f1Sdp  * fsirand installs random inode generation numbers on all the inodes on
39ace1a5f1Sdp  * device <special>, and also installs a file system ID in the superblock.
40ace1a5f1Sdp  * This helps increase the security of  file systems exported by NFS.
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
46ace1a5f1Sdp #include <strings.h>
47ace1a5f1Sdp #include <unistd.h>
48ace1a5f1Sdp #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <sys/param.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/time.h>
527c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
537c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
547c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
557c478bd9Sstevel@tonic-gate 
56ace1a5f1Sdp long fsbuf[(SBSIZE / sizeof (long))];
577c478bd9Sstevel@tonic-gate struct dinode dibuf[8192/sizeof (struct dinode)];
587c478bd9Sstevel@tonic-gate 
59ace1a5f1Sdp int
main(int argc,char * argv[])60ace1a5f1Sdp main(int argc, char *argv[])
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	struct fs *fs;
637c478bd9Sstevel@tonic-gate 	int fd;
647c478bd9Sstevel@tonic-gate 	char *dev;
657c478bd9Sstevel@tonic-gate 	int bno;
667c478bd9Sstevel@tonic-gate 	struct dinode *dip;
677c478bd9Sstevel@tonic-gate 	int inum, imax;
687c478bd9Sstevel@tonic-gate 	int i, n;
697c478bd9Sstevel@tonic-gate 	offset_t seekaddr;
707c478bd9Sstevel@tonic-gate 	int bsize;
717c478bd9Sstevel@tonic-gate 	int pflag = 0;
727c478bd9Sstevel@tonic-gate 	struct timeval timeval;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	argv++;
757c478bd9Sstevel@tonic-gate 	argc--;
767c478bd9Sstevel@tonic-gate 	if (argc > 0 && strcmp(*argv, "-p") == 0) {
777c478bd9Sstevel@tonic-gate 		pflag++;
787c478bd9Sstevel@tonic-gate 		argv++;
797c478bd9Sstevel@tonic-gate 		argc--;
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	if (argc <= 0) {
827c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Usage: fsirand [-p] special\n");
837c478bd9Sstevel@tonic-gate 		exit(1);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	dev = *argv;
867c478bd9Sstevel@tonic-gate 	fd = open64(dev, pflag ? O_RDONLY : O_RDWR);
877c478bd9Sstevel@tonic-gate 	if (fd == -1) {
887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "fsirand: Cannot open %s: %s\n", dev,
897c478bd9Sstevel@tonic-gate 		    strerror(errno));
907c478bd9Sstevel@tonic-gate 		exit(1);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1) {
937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
947c478bd9Sstevel@tonic-gate 		    "fsirand: Seek to superblock failed: %s\n",
957c478bd9Sstevel@tonic-gate 		    strerror(errno));
967c478bd9Sstevel@tonic-gate 		exit(1);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 	fs = (struct fs *)fsbuf;
997c478bd9Sstevel@tonic-gate 	if ((n = read(fd, (char *)fs, SBSIZE)) != SBSIZE) {
1007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1017c478bd9Sstevel@tonic-gate 		    "fsirand: Read of superblock failed: %s\n",
1027c478bd9Sstevel@tonic-gate 		    n == -1 ? strerror(errno) : "Short read");
1037c478bd9Sstevel@tonic-gate 		exit(1);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	if ((fs->fs_magic != FS_MAGIC) &&
1067c478bd9Sstevel@tonic-gate 	    (fs->fs_magic != MTB_UFS_MAGIC)) {
1077c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1087c478bd9Sstevel@tonic-gate 	"fsirand: Not a file system (bad magic number in superblock)\n");
1097c478bd9Sstevel@tonic-gate 		exit(1);
1107c478bd9Sstevel@tonic-gate 	}
111*6451fdbcSvsakar 	if (fs->fs_magic == FS_MAGIC &&
112*6451fdbcSvsakar 	    (fs->fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
113*6451fdbcSvsakar 	    fs->fs_version != UFS_VERSION_MIN)) {
114*6451fdbcSvsakar 		(void) fprintf(stderr,
115*6451fdbcSvsakar 	"fsirand: Unrecognized UFS format version number %d (in superblock)\n",
116*6451fdbcSvsakar 		    fs->fs_version);
117*6451fdbcSvsakar 		exit(1);
118*6451fdbcSvsakar 	}
1197c478bd9Sstevel@tonic-gate 	if (fs->fs_magic == MTB_UFS_MAGIC &&
1207c478bd9Sstevel@tonic-gate 	    (fs->fs_version > MTB_UFS_VERSION_1 ||
1217c478bd9Sstevel@tonic-gate 	    fs->fs_version < MTB_UFS_VERSION_MIN)) {
1227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1237c478bd9Sstevel@tonic-gate 	"fsirand: Unrecognized UFS format version number %d (in superblock)\n",
1247c478bd9Sstevel@tonic-gate 		    fs->fs_version);
1257c478bd9Sstevel@tonic-gate 		exit(1);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	if (pflag) {
1287c478bd9Sstevel@tonic-gate 		(void) printf("fsid: %x %x\n", fs->fs_id[0], fs->fs_id[1]);
1297c478bd9Sstevel@tonic-gate 	} else {
1307c478bd9Sstevel@tonic-gate 		n = getpid();
1317c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&timeval, (struct timezone *)NULL);
1327c478bd9Sstevel@tonic-gate 		srand48((long)(timeval.tv_sec + timeval.tv_usec + n));
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	bsize = INOPB(fs) * sizeof (struct dinode);
1357c478bd9Sstevel@tonic-gate 	inum = 0;
1367c478bd9Sstevel@tonic-gate 	imax = fs->fs_ipg * fs->fs_ncg;
1377c478bd9Sstevel@tonic-gate 	while (inum < imax) {
1387c478bd9Sstevel@tonic-gate 		bno = itod(fs, inum);
1397c478bd9Sstevel@tonic-gate 		seekaddr = (offset_t)fsbtodb(fs, bno) * DEV_BSIZE;
1407c478bd9Sstevel@tonic-gate 		if (llseek(fd, seekaddr, 0) == -1) {
1417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1427c478bd9Sstevel@tonic-gate 			    "fsirand: Seek to %ld %ld failed: %s\n",
1437c478bd9Sstevel@tonic-gate 			    ((off_t *)&seekaddr)[0], ((off_t *)&seekaddr)[1],
1447c478bd9Sstevel@tonic-gate 			    strerror(errno));
1457c478bd9Sstevel@tonic-gate 			exit(1);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		n = read(fd, (char *)dibuf, bsize);
1487c478bd9Sstevel@tonic-gate 		if (n != bsize) {
1497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1507c478bd9Sstevel@tonic-gate 			    "fsirand: Read of ilist block failed: %s\n",
1517c478bd9Sstevel@tonic-gate 			    n == -1 ? strerror(errno) : "Short read");
1527c478bd9Sstevel@tonic-gate 			exit(1);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		for (dip = dibuf; dip < &dibuf[INOPB(fs)]; dip++) {
1557c478bd9Sstevel@tonic-gate 			if (pflag) {
1567c478bd9Sstevel@tonic-gate 				(void) printf("ino %d gen %x\n", inum,
1577c478bd9Sstevel@tonic-gate 				    dip->di_gen);
1587c478bd9Sstevel@tonic-gate 			} else {
1597c478bd9Sstevel@tonic-gate 				dip->di_gen = lrand48();
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 			inum++;
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 		if (!pflag) {
1647c478bd9Sstevel@tonic-gate 			if (llseek(fd, seekaddr, 0) == -1) {
1657c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1667c478bd9Sstevel@tonic-gate 				    "fsirand: Seek to %ld %ld failed: %s\n",
1677c478bd9Sstevel@tonic-gate 				    ((off_t *)&seekaddr)[0],
1687c478bd9Sstevel@tonic-gate 				    ((off_t *)&seekaddr)[1],
169ace1a5f1Sdp 				    strerror(errno));
1707c478bd9Sstevel@tonic-gate 				exit(1);
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			n = write(fd, (char *)dibuf, bsize);
1737c478bd9Sstevel@tonic-gate 			if (n != bsize) {
1747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
1757c478bd9Sstevel@tonic-gate 				"fsirand: Write of ilist block failed: %s\n",
1767c478bd9Sstevel@tonic-gate 				    n == -1 ? strerror(errno) : "Short write");
1777c478bd9Sstevel@tonic-gate 				exit(1);
1787c478bd9Sstevel@tonic-gate 			}
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	if (!pflag) {
1827c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&timeval, (struct timezone *)NULL);
1837c478bd9Sstevel@tonic-gate 		fs->fs_id[0] = timeval.tv_sec;
1847c478bd9Sstevel@tonic-gate 		fs->fs_id[1] = timeval.tv_usec + getpid();
1857c478bd9Sstevel@tonic-gate 		if (llseek(fd, (offset_t)SBLOCK * DEV_BSIZE, 0) == -1) {
1867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1877c478bd9Sstevel@tonic-gate 			    "fsirand: Seek to superblock failed: %s\n",
1887c478bd9Sstevel@tonic-gate 			    strerror(errno));
1897c478bd9Sstevel@tonic-gate 			exit(1);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		if ((n = write(fd, (char *)fs, SBSIZE)) != SBSIZE) {
1927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1937c478bd9Sstevel@tonic-gate 			    "fsirand: Write of superblock failed: %s\n",
1947c478bd9Sstevel@tonic-gate 			    n == -1 ? strerror(errno) : "Short write");
1957c478bd9Sstevel@tonic-gate 			exit(1);
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	for (i = 0; i < fs->fs_ncg; i++) {
1997c478bd9Sstevel@tonic-gate 		seekaddr = (offset_t)fsbtodb(fs, cgsblock(fs, i)) * DEV_BSIZE;
2007c478bd9Sstevel@tonic-gate 		if (llseek(fd,  seekaddr, 0) == -1) {
2017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2027c478bd9Sstevel@tonic-gate 			"fsirand: Seek to alternate superblock failed: %s\n",
2037c478bd9Sstevel@tonic-gate 			    strerror(errno));
2047c478bd9Sstevel@tonic-gate 			exit(1);
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 		if (pflag) {
2077c478bd9Sstevel@tonic-gate 			if ((n = read(fd, (char *)fs, SBSIZE)) != SBSIZE) {
2087c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2097c478bd9Sstevel@tonic-gate 			"fsirand: Read of alternate superblock failed: %s\n",
2107c478bd9Sstevel@tonic-gate 				    n == -1 ? strerror(errno) : "Short read");
2117c478bd9Sstevel@tonic-gate 				exit(1);
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 			if ((fs->fs_magic != FS_MAGIC) &&
2147c478bd9Sstevel@tonic-gate 			    (fs->fs_magic != MTB_UFS_MAGIC)) {
2157c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2167c478bd9Sstevel@tonic-gate 				    "fsirand: Not a valid file system (bad "
2177c478bd9Sstevel@tonic-gate 				    "magic number in alternate superblock)\n");
2187c478bd9Sstevel@tonic-gate 				exit(1);
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		} else {
2217c478bd9Sstevel@tonic-gate 			if ((n = write(fd, (char *)fs, SBSIZE)) != SBSIZE) {
2227c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2237c478bd9Sstevel@tonic-gate 			"fsirand: Write of alternate superblock failed: %s\n",
2247c478bd9Sstevel@tonic-gate 				    n == -1 ? strerror(errno) : "Short write");
2257c478bd9Sstevel@tonic-gate 				exit(1);
2267c478bd9Sstevel@tonic-gate 			}
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 	}
229ace1a5f1Sdp 	return (0);
2307c478bd9Sstevel@tonic-gate }
231