1 /*	$OpenBSD: clri.c,v 1.10 2004/01/25 19:18:54 deraadt Exp $	*/
2 /*	$NetBSD: clri.c,v 1.9 1995/03/18 14:54:33 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rich $alz of BBN Inc.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1990, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)clri.c	8.2 (Berkeley) 9/23/93";
45 #else
46 static char rcsid[] = "$OpenBSD: clri.c,v 1.10 2004/01/25 19:18:54 deraadt Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/time.h>
52 
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
55 
56 #include <err.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <stdio.h>
62 #include <unistd.h>
63 
64 int
main(int argc,char * argv[])65 main(int argc, char *argv[])
66 {
67 	struct ufs1_dinode ibuf[MAXBSIZE / sizeof (struct ufs1_dinode)];
68 	char *fs, sblock[SBSIZE];
69 	struct ufs1_dinode *ip;
70 	struct fs *sbp;
71 	int fd, inonum;
72 	off_t offset;
73 	size_t bsize;
74 
75 	if (argc < 3) {
76 		fprintf(stderr, "usage: clri filesystem inode ...\n");
77 		return (1);
78 	}
79 
80 	fs = *++argv;
81 
82 	/* get the superblock. */
83 	if ((fd = open(fs, O_RDWR, 0)) < 0)
84 		err(1, "%s", fs);
85 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
86 		err(1, "%s", fs);
87 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
88 		errx(1, "%s: can't read superblock", fs);
89 
90 	sbp = (struct fs *)sblock;
91 	if (sbp->fs_magic != FS_MAGIC)
92 		errx(1, "%s: superblock magic number 0x%x, not 0x%x",
93 		    fs, sbp->fs_magic, FS_MAGIC);
94 	bsize = sbp->fs_bsize;
95 
96 	/* remaining arguments are inode numbers. */
97 	while (*++argv) {
98 		/* get the inode number. */
99 		if ((inonum = atoi(*argv)) <= 0)
100 			errx(1, "%s is not a valid inode number", *argv);
101 		(void)printf("clearing %d\n", inonum);
102 
103 		/* read in the appropriate block. */
104 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
105 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
106 		offset *= DEV_BSIZE;			/* disk blk to bytes */
107 
108 		/* seek and read the block */
109 		if (lseek(fd, offset, SEEK_SET) < 0)
110 			err(1, "%s", fs);
111 		if (read(fd, ibuf, bsize) != bsize)
112 			err(1, "%s", fs);
113 
114 		/* get the inode within the block. */
115 		ip = &ibuf[ino_to_fsbo(sbp, inonum)];
116 
117 		/* clear the inode, and bump the generation count. */
118 		memset(ip, 0, sizeof(*ip));
119 		ip->di_gen = arc4random();
120 
121 		/* backup and write the block */
122 		if (lseek(fd, offset, SEEK_SET) < 0)
123 			err(1, "%s", fs);
124 		if (write(fd, ibuf, bsize) != bsize)
125 			err(1, "%s", fs);
126 		(void)fsync(fd);
127 
128 		if (sbp->fs_inodefmt >= FS_44INODEFMT) {
129 			/* update after each inode cleared */
130 			sbp->fs_clean = 0;
131 			if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
132 				err(1, "%s", fs);
133 			if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock))
134 				errx(1, "%s: can't update superblock", fs);
135 		}
136 	}
137 
138 	return close(fd);
139 }
140