xref: /netbsd-src/sbin/clri/clri.c (revision 08371645eea7fce89c0b1f52af7a1738d18acbee)
1 /*	$NetBSD: clri.c,v 1.12 1998/03/18 16:51:31 bouyer 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.3 (Berkeley) 4/28/95";
48 #else
49 __RCSID("$NetBSD: clri.c,v 1.12 1998/03/18 16:51:31 bouyer 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/ufs/ufs_bswap.h>
58 #include <ufs/ffs/fs.h>
59 #include <ufs/ffs/ffs_extern.h>
60 
61 #include <err.h>
62 #include <errno.h>
63 #include <fcntl.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <stdio.h>
67 #include <unistd.h>
68 
69 int	main __P((int, char *[]));
70 
71 int
72 main(argc, argv)
73 	int argc;
74 	char *argv[];
75 {
76 	struct fs *sbp;
77 	struct dinode *ip;
78 	int fd;
79 	struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
80 	int32_t generation;
81 	off_t offset;
82 	size_t bsize;
83 	int inonum;
84 	char *fs, sblock[SBSIZE];
85 	int needswap = 0;
86 	int i, imax;
87 
88 	if (argc < 3) {
89 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
90 		exit(1);
91 	}
92 
93 	fs = *++argv;
94 
95 
96 	/* get the superblock. */
97 	if ((fd = open(fs, O_RDWR, 0)) < 0)
98 		err(1, "%s", fs);
99 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
100 		err(1, "%s", fs);
101 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
102 		errx(1, "%s: can't read superblock", fs);
103 
104 	sbp = (struct fs *)sblock;
105 	if (sbp->fs_magic != FS_MAGIC)
106 		if (sbp->fs_magic == bswap32(FS_MAGIC))
107 			needswap = 1;
108 		else
109 			errx(1, "%s: superblock magic number 0x%x, not 0x%x",
110 		    	fs, sbp->fs_magic, FS_MAGIC);
111 
112 	/* check that inode numbers are valid */
113 	imax = ufs_rw32(sbp->fs_ncg, needswap) *
114 		ufs_rw32(sbp->fs_ipg, needswap);
115 	for (i = 1; i < (argc - 1); i++)
116 		if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax)
117 			errx(1, "%s is not a valid inode number", argv[i]);
118 
119 	/* delete clean flag in the superblok */
120 	sbp->fs_clean = ufs_rw32(
121 						ufs_rw32(sbp->fs_clean, needswap) << 1,
122 						needswap);
123 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
124 		err(1, "%s", fs);
125 	if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock))
126 		errx(1, "%s: can't rewrite superblock", fs);
127 	(void)fsync(fd);
128 
129 	if (needswap)
130 		ffs_sb_swap(sbp, sbp, 0);
131 	bsize = sbp->fs_bsize;
132 
133 	/* remaining arguments are inode numbers. */
134 	while (*++argv) {
135 		/* get the inode number. */
136 		inonum = atoi(*argv);
137 		(void)printf("clearing %d\n", inonum);
138 
139 		/* read in the appropriate block. */
140 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
141 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
142 		offset *= DEV_BSIZE;			/* disk blk to bytes */
143 
144 		/* seek and read the block */
145 		if (lseek(fd, offset, SEEK_SET) < 0)
146 			err(1, "%s", fs);
147 		if (read(fd, ibuf, bsize) != bsize)
148 			err(1, "%s", fs);
149 
150 		/* get the inode within the block. */
151 		ip = &ibuf[ino_to_fsbo(sbp, inonum)];
152 
153 		/* clear the inode, and bump the generation count. */
154 		generation = ip->di_gen + 1;
155 		memset(ip, 0, sizeof(*ip));
156 		ip->di_gen = generation;
157 
158 		/* backup and write the block */
159 		if (lseek(fd, offset, SEEK_SET) < 0)
160 			err(1, "%s", fs);
161 		if (write(fd, ibuf, bsize) != bsize)
162 			err(1, "%s", fs);
163 		(void)fsync(fd);
164 	}
165 	(void)close(fd);
166 	exit(0);
167 }
168