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