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