xref: /openbsd-src/sbin/clri/clri.c (revision 342ef31aa933482941a09fddec51e63e4fa4094e)
1 /*	$OpenBSD: clri.c,v 1.22 2020/06/24 05:46:07 otto Exp $	*/
2 /*	$NetBSD: clri.c,v 1.19 2005/01/20 15:50:47 xtraeme 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 #include <sys/param.h>	/* MAXBSIZE DEV_BSIZE */
37 
38 #include <ufs/ufs/dinode.h>
39 #include <ufs/ffs/fs.h>
40 
41 #include <err.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <util.h>
48 
49 /*
50  * Possible superblock locations ordered from most to least likely.
51  */
52 static int sblock_try[] = SBLOCKSEARCH;
53 
54 static void
usage(void)55 usage(void)
56 {
57 	(void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
58 	exit(1);
59 }
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	struct ufs1_dinode *dp1;
65 	struct ufs2_dinode *dp2;
66 	struct fs *sbp;
67 	char *ibuf[MAXBSIZE];
68 	char *fs, sblock[SBLOCKSIZE];
69 	size_t bsize;
70 	off_t offset;
71 	int i, fd;
72 	ino_t imax, inonum;
73 
74 	if (argc < 3)
75 		usage();
76 
77 	fs = *++argv;
78 	sbp = NULL;
79 
80 	/* get the superblock. */
81 	if ((fd = opendev(fs, O_RDWR, 0, NULL)) == -1)
82 		err(1, "%s", fs);
83 
84 	if (pledge("stdio", NULL) == -1)
85 		err(1, "pledge");
86 
87 	for (i = 0; sblock_try[i] != -1; i++) {
88 		offset = (off_t)(sblock_try[i]);
89 		if (pread(fd, sblock, sizeof(sblock), offset) != sizeof(sblock))
90 			err(1, "%s: can't read superblock", fs);
91 		sbp = (struct fs *)sblock;
92 		if ((sbp->fs_magic == FS_UFS1_MAGIC ||
93 		     (sbp->fs_magic == FS_UFS2_MAGIC &&
94 		      sbp->fs_sblockloc == sblock_try[i])) &&
95 		    sbp->fs_bsize <= MAXBSIZE &&
96 		    sbp->fs_bsize >= (int)sizeof(struct fs))
97 			break;
98 	}
99 	if (sblock_try[i] == -1)
100 		errx(2, "cannot find file system superblock");
101 	bsize = sbp->fs_bsize;
102 
103 	/* check that inode numbers are valid */
104 	imax = sbp->fs_ncg * sbp->fs_ipg;
105 	for (i = 1; i < (argc - 1); i++) {
106 		const char *errstr;
107 		strtonum(argv[i], 1, imax, &errstr);
108 		if (errstr)
109 			errx(1, "%s is not a valid inode number: %s", argv[i], errstr);
110 	}
111 
112 	/* clear the clean flag in the superblock */
113 	if (sbp->fs_inodefmt >= FS_44INODEFMT) {
114 		sbp->fs_clean = 0;
115 		if (pwrite(fd, sblock, sizeof(sblock), offset) != sizeof(sblock))
116 			err(1, "%s: can't update superblock", fs);
117 		(void)fsync(fd);
118 	}
119 
120 	/* remaining arguments are inode numbers. */
121 	while (*++argv) {
122 		/* get the inode number. */
123 		inonum = strtonum(*argv, 1, imax, NULL);
124 		(void)printf("clearing %llu\n", inonum);
125 
126 		/* read in the appropriate block. */
127 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
128 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
129 		offset *= DEV_BSIZE;			/* disk blk to bytes */
130 
131 		/* seek and read the block */
132 		if (pread(fd, ibuf, bsize, offset) != bsize)
133 			err(1, "%s", fs);
134 
135 		if (sbp->fs_magic == FS_UFS2_MAGIC) {
136 			/* get the inode within the block. */
137 			dp2 = &(((struct ufs2_dinode *)ibuf)
138 			    [ino_to_fsbo(sbp, inonum)]);
139 
140 			/* clear the inode, and bump the generation count. */
141 			memset(dp2, 0, sizeof(*dp2));
142 			dp2->di_gen = arc4random();
143 		} else {
144 			/* get the inode within the block. */
145 			dp1 = &(((struct ufs1_dinode *)ibuf)
146 			    [ino_to_fsbo(sbp, inonum)]);
147 
148 			/* clear the inode, and bump the generation count. */
149 			memset(dp1, 0, sizeof(*dp1));
150 			dp1->di_gen = arc4random();
151 		}
152 
153 		/* backup and write the block */
154 		if (pwrite(fd, ibuf, bsize, offset) != bsize)
155 			err(1, "%s", fs);
156 		(void)fsync(fd);
157 	}
158 	(void)close(fd);
159 	exit(0);
160 }
161