xref: /netbsd-src/sbin/clri/clri.c (revision 45db36d858778d4221f46da976b3e87f78d702f2)
1 /*	$NetBSD: clri.c,v 1.26 2023/02/26 22:55:02 andvar 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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38  The Regents of the University of California.  All rights reserved.");
39 #endif /* not lint */
40 
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)clri.c	8.3 (Berkeley) 4/28/95";
44 #else
45 __RCSID("$NetBSD: clri.c,v 1.26 2023/02/26 22:55:02 andvar Exp $");
46 #endif
47 #endif /* not lint */
48 
49 #include <sys/param.h>
50 #include <sys/time.h>
51 
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ufs/ufs_bswap.h>
54 #include <ufs/ffs/fs.h>
55 #include <ufs/ffs/ffs_extern.h>
56 
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdio.h>
63 #include <unistd.h>
64 
65 /*
66  * Possible superblock locations ordered from most to least likely.
67  */
68 static off_t sblock_try[] = SBLOCKSEARCH;
69 off_t sblockloc;
70 
71 
72 int
main(int argc,char * argv[])73 main(int argc, char *argv[])
74 {
75 	struct fs *sbp;
76 	struct ufs1_dinode *ip1;
77 	struct ufs2_dinode *ip2;
78 	int fd;
79 	void *ibuf;
80 	int32_t generation;
81 	off_t offset;
82 	size_t bsize;
83 	int inonum;
84 	char *fs, sblock[SBLOCKSIZE];
85 	int needswap = 0, is_ufs2 = 0;
86 	int i, imax;
87 	long dev_bsize;
88 
89 	if (argc < 3) {
90 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
91 		exit(1);
92 	}
93 
94 	fs = *++argv;
95 
96 
97 	/* get the superblock. */
98 	if ((fd = open(fs, O_RDWR, 0)) < 0)
99 		err(1, "%s", fs);
100 	for (i = 0;; i++) {
101 		sblockloc = sblock_try[i];
102 		if (sblockloc == -1)
103 			errx(1, "%s: can't find superblock", fs);
104 		if (pread(fd, sblock, sizeof(sblock), sblockloc) != sizeof(sblock))
105 			errx(1, "%s: can't read superblock", fs);
106 
107 		sbp = (struct fs *)sblock;
108 		switch(sbp->fs_magic) {
109 		case FS_UFS2_MAGIC:
110 		case FS_UFS2EA_MAGIC:
111 			is_ufs2 = 1;
112 			/*FALLTHROUGH*/
113 		case FS_UFS1_MAGIC:
114 			break;
115 		case FS_UFS2_MAGIC_SWAPPED:
116 		case FS_UFS2EA_MAGIC_SWAPPED:
117 			is_ufs2 = 1;
118 			/*FALLTHROUGH*/
119 		case FS_UFS1_MAGIC_SWAPPED:
120 			needswap = 1;
121 			break;
122 		default:
123 			continue;
124 		}
125 
126 		/* check we haven't found an alternate */
127 		if (is_ufs2 || sbp->fs_old_flags & FS_FLAGS_UPDATED) {
128 			if ((uint64_t)sblockloc != ufs_rw64(sbp->fs_sblockloc, needswap))
129 				continue;
130 		} else {
131 			if (sblockloc == SBLOCK_UFS2)
132 				continue;
133 		}
134 
135 		break;
136 	}
137 
138 	/* check that inode numbers are valid */
139 	imax = ufs_rw32(sbp->fs_ncg, needswap) *
140 		ufs_rw32(sbp->fs_ipg, needswap);
141 	for (i = 1; i < (argc - 1); i++)
142 		if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax)
143 			errx(1, "%s is not a valid inode number", argv[i]);
144 
145 	/* delete clean flag in the superblock */
146 	sbp->fs_clean = ufs_rw32(ufs_rw32(sbp->fs_clean, needswap) << 1,
147 				needswap);
148 	if (lseek(fd, sblockloc, SEEK_SET) < 0)
149 		err(1, "%s", fs);
150 	if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock))
151 		errx(1, "%s: can't rewrite superblock", fs);
152 	(void)fsync(fd);
153 
154 	if (needswap)
155 		ffs_sb_swap(sbp, sbp);
156 
157 	/* compute disk block size from superblock parameters */
158 	dev_bsize = sbp->fs_fsize / FFS_FSBTODB(sbp, 1);
159 
160 	bsize = sbp->fs_bsize;
161 	ibuf = malloc(bsize);
162 	if (ibuf == NULL) {
163 		err(1, "malloc");
164 	}
165 
166 	/* remaining arguments are inode numbers. */
167 	while (*++argv) {
168 		/* get the inode number. */
169 		inonum = atoi(*argv);
170 		(void)printf("clearing %d\n", inonum);
171 
172 		/* read in the appropriate block. */
173 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
174 		offset = FFS_FSBTODB(sbp, offset);	/* fs blk disk blk */
175 		offset *= dev_bsize;			/* disk blk to bytes */
176 
177 		/* seek and read the block */
178 		if (lseek(fd, offset, SEEK_SET) < 0)
179 			err(1, "%s", fs);
180 		if ((size_t)read(fd, ibuf, bsize) != bsize)
181 			err(1, "%s", fs);
182 
183 		/* get the inode within the block. */
184 		if (is_ufs2) {
185 			ip2 = &((struct ufs2_dinode *)ibuf)
186 			    [ino_to_fsbo(sbp, inonum)];
187 			/* clear the inode, and bump the generation count. */
188 			generation = ip2->di_gen + 1;
189 			memset(ip2, 0, sizeof(*ip2));
190 			ip2->di_gen = generation;
191 		} else {
192 			ip1 = &((struct ufs1_dinode *)ibuf)
193 			    [ino_to_fsbo(sbp, inonum)];
194 			/* clear the inode, and bump the generation count. */
195 			generation = ip1->di_gen + 1;
196 			memset(ip1, 0, sizeof(*ip1));
197 			ip1->di_gen = generation;
198 		}
199 
200 		/* backup and write the block */
201 		if (lseek(fd, offset, SEEK_SET) < 0)
202 			err(1, "%s", fs);
203 		if ((size_t)write(fd, ibuf, bsize) != bsize)
204 			err(1, "%s", fs);
205 		(void)fsync(fd);
206 	}
207 	free(ibuf);
208 	(void)close(fd);
209 	exit(0);
210 }
211