xref: /freebsd/sbin/clri/clri.c (revision dffce2150eeac1c7f424324a5cade7a97b308979)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1990, 1993
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
88fae3551SRodney W. Grimes  * Rich $alz of BBN Inc.
98fae3551SRodney W. Grimes  *
108fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
118fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
128fae3551SRodney W. Grimes  * are met:
138fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
158fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
168fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
178fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
198fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
208fae3551SRodney W. Grimes  *    without specific prior written permission.
218fae3551SRodney W. Grimes  *
228fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
238fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
248fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
268fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
278fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
298fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
318fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328fae3551SRodney W. Grimes  * SUCH DAMAGE.
338fae3551SRodney W. Grimes  */
348fae3551SRodney W. Grimes 
35c69284caSDavid E. O'Brien #if 0
368fae3551SRodney W. Grimes #ifndef lint
37ec74b3f3SPhilippe Charnier static const char copyright[] =
388fae3551SRodney W. Grimes "@(#) Copyright (c) 1990, 1993\n\
398fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
408fae3551SRodney W. Grimes #endif /* not lint */
418fae3551SRodney W. Grimes 
428fae3551SRodney W. Grimes #ifndef lint
438fae3551SRodney W. Grimes static char sccsid[] = "@(#)clri.c	8.2 (Berkeley) 9/23/93";
448fae3551SRodney W. Grimes #endif /* not lint */
45c69284caSDavid E. O'Brien #endif
468a1dff0cSPhilippe Charnier 
4706e8d954SMark Murray #include <sys/cdefs.h>
4806e8d954SMark Murray __FBSDID("$FreeBSD$");
4906e8d954SMark Murray 
508fae3551SRodney W. Grimes #include <sys/param.h>
5175766e17SPoul-Henning Kamp #include <sys/disklabel.h>
528fae3551SRodney W. Grimes 
538fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
548fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
558fae3551SRodney W. Grimes 
568fae3551SRodney W. Grimes #include <err.h>
57*dffce215SKirk McKusick #include <errno.h>
588fae3551SRodney W. Grimes #include <fcntl.h>
59*dffce215SKirk McKusick #include <libufs.h>
608fae3551SRodney W. Grimes #include <stdlib.h>
618fae3551SRodney W. Grimes #include <string.h>
628fae3551SRodney W. Grimes #include <stdio.h>
638fae3551SRodney W. Grimes #include <unistd.h>
648fae3551SRodney W. Grimes 
65ec74b3f3SPhilippe Charnier static void
66ec74b3f3SPhilippe Charnier usage(void)
67ec74b3f3SPhilippe Charnier {
68a48ea640STom Rhodes 	(void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
69ec74b3f3SPhilippe Charnier 	exit(1);
70ec74b3f3SPhilippe Charnier }
71ec74b3f3SPhilippe Charnier 
728fae3551SRodney W. Grimes int
7306e8d954SMark Murray main(int argc, char *argv[])
748fae3551SRodney W. Grimes {
75*dffce215SKirk McKusick 	struct fs *fs;
761c85e6a3SKirk McKusick 	struct ufs1_dinode *dp1;
771c85e6a3SKirk McKusick 	struct ufs2_dinode *dp2;
781c85e6a3SKirk McKusick 	char *ibuf[MAXBSIZE];
798fae3551SRodney W. Grimes 	long generation, bsize;
808fae3551SRodney W. Grimes 	off_t offset;
81*dffce215SKirk McKusick 	int fd, ret, inonum;
82*dffce215SKirk McKusick 	char *fsname;
8356589eb7SJohn Birrell 	void *v = ibuf;
848fae3551SRodney W. Grimes 
85ec74b3f3SPhilippe Charnier 	if (argc < 3)
86ec74b3f3SPhilippe Charnier 		usage();
878fae3551SRodney W. Grimes 
88*dffce215SKirk McKusick 	fsname = *++argv;
898fae3551SRodney W. Grimes 
908fae3551SRodney W. Grimes 	/* get the superblock. */
91*dffce215SKirk McKusick 	if ((fd = open(fsname, O_RDWR, 0)) < 0)
92*dffce215SKirk McKusick 		err(1, "%s", fsname);
93*dffce215SKirk McKusick 	if ((ret = sbget(fd, &fs, -1)) != 0) {
94*dffce215SKirk McKusick 		switch (ret) {
95*dffce215SKirk McKusick 		case ENOENT:
96*dffce215SKirk McKusick 			warn("Cannot find file system superblock");
97*dffce215SKirk McKusick 			return (1);
98*dffce215SKirk McKusick 		default:
99*dffce215SKirk McKusick 			warn("Unable to read file system superblock");
100*dffce215SKirk McKusick 			return (1);
1011c85e6a3SKirk McKusick 		}
102*dffce215SKirk McKusick 	}
103*dffce215SKirk McKusick 	bsize = fs->fs_bsize;
1048fae3551SRodney W. Grimes 
1058fae3551SRodney W. Grimes 	/* remaining arguments are inode numbers. */
1068fae3551SRodney W. Grimes 	while (*++argv) {
1078fae3551SRodney W. Grimes 		/* get the inode number. */
108a715c4a4SPhilippe Charnier 		if ((inonum = atoi(*argv)) <= 0)
109ec74b3f3SPhilippe Charnier 			errx(1, "%s is not a valid inode number", *argv);
1108fae3551SRodney W. Grimes 		(void)printf("clearing %d\n", inonum);
1118fae3551SRodney W. Grimes 
1128fae3551SRodney W. Grimes 		/* read in the appropriate block. */
113*dffce215SKirk McKusick 		offset = ino_to_fsba(fs, inonum);	/* inode to fs blk */
114*dffce215SKirk McKusick 		offset = fsbtodb(fs, offset);		/* fs blk disk blk */
1158fae3551SRodney W. Grimes 		offset *= DEV_BSIZE;			/* disk blk to bytes */
1168fae3551SRodney W. Grimes 
1178fae3551SRodney W. Grimes 		/* seek and read the block */
1188fae3551SRodney W. Grimes 		if (lseek(fd, offset, SEEK_SET) < 0)
119*dffce215SKirk McKusick 			err(1, "%s", fsname);
1208fae3551SRodney W. Grimes 		if (read(fd, ibuf, bsize) != bsize)
121*dffce215SKirk McKusick 			err(1, "%s", fsname);
1228fae3551SRodney W. Grimes 
123*dffce215SKirk McKusick 		if (fs->fs_magic == FS_UFS2_MAGIC) {
1248fae3551SRodney W. Grimes 			/* get the inode within the block. */
12556589eb7SJohn Birrell 			dp2 = &(((struct ufs2_dinode *)v)
126*dffce215SKirk McKusick 			    [ino_to_fsbo(fs, inonum)]);
1278fae3551SRodney W. Grimes 
1288fae3551SRodney W. Grimes 			/* clear the inode, and bump the generation count. */
1291c85e6a3SKirk McKusick 			generation = dp2->di_gen + 1;
1301c85e6a3SKirk McKusick 			memset(dp2, 0, sizeof(*dp2));
1311c85e6a3SKirk McKusick 			dp2->di_gen = generation;
1321c85e6a3SKirk McKusick 		} else {
1331c85e6a3SKirk McKusick 			/* get the inode within the block. */
13456589eb7SJohn Birrell 			dp1 = &(((struct ufs1_dinode *)v)
135*dffce215SKirk McKusick 			    [ino_to_fsbo(fs, inonum)]);
1361c85e6a3SKirk McKusick 
1371c85e6a3SKirk McKusick 			/* clear the inode, and bump the generation count. */
1381c85e6a3SKirk McKusick 			generation = dp1->di_gen + 1;
1391c85e6a3SKirk McKusick 			memset(dp1, 0, sizeof(*dp1));
1401c85e6a3SKirk McKusick 			dp1->di_gen = generation;
1411c85e6a3SKirk McKusick 		}
1428fae3551SRodney W. Grimes 
1438fae3551SRodney W. Grimes 		/* backup and write the block */
1448fae3551SRodney W. Grimes 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
145*dffce215SKirk McKusick 			err(1, "%s", fsname);
1468fae3551SRodney W. Grimes 		if (write(fd, ibuf, bsize) != bsize)
147*dffce215SKirk McKusick 			err(1, "%s", fsname);
1488fae3551SRodney W. Grimes 		(void)fsync(fd);
1498fae3551SRodney W. Grimes 	}
1508fae3551SRodney W. Grimes 	(void)close(fd);
1518fae3551SRodney W. Grimes 	exit(0);
1528fae3551SRodney W. Grimes }
153