xref: /freebsd/sbin/clri/clri.c (revision a48ea640731d8581baa01dcf350f971f889cd4b8)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1990, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
68fae3551SRodney W. Grimes  * Rich $alz of BBN Inc.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
98fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
108fae3551SRodney W. Grimes  * are met:
118fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
128fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
138fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
158fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
168fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
178fae3551SRodney W. Grimes  *    must display the following acknowledgement:
188fae3551SRodney W. Grimes  *	This product includes software developed by the University of
198fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
208fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
218fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
228fae3551SRodney W. Grimes  *    without specific prior written permission.
238fae3551SRodney W. Grimes  *
248fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
258fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
288fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
298fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
308fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
318fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
328fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
338fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
348fae3551SRodney W. Grimes  * SUCH DAMAGE.
358fae3551SRodney W. Grimes  */
368fae3551SRodney W. Grimes 
378fae3551SRodney W. Grimes #ifndef lint
38ec74b3f3SPhilippe Charnier static const char copyright[] =
398fae3551SRodney W. Grimes "@(#) Copyright (c) 1990, 1993\n\
408fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
418fae3551SRodney W. Grimes #endif /* not lint */
428fae3551SRodney W. Grimes 
438fae3551SRodney W. Grimes #ifndef lint
44ec74b3f3SPhilippe Charnier #if 0
458fae3551SRodney W. Grimes static char sccsid[] = "@(#)clri.c	8.2 (Berkeley) 9/23/93";
46ec74b3f3SPhilippe Charnier #endif
478fae3551SRodney W. Grimes #endif /* not lint */
488fae3551SRodney W. Grimes 
4906e8d954SMark Murray #include <sys/cdefs.h>
5006e8d954SMark Murray __FBSDID("$FreeBSD$");
5106e8d954SMark Murray 
528fae3551SRodney W. Grimes #include <sys/param.h>
5375766e17SPoul-Henning Kamp #include <sys/disklabel.h>
548fae3551SRodney W. Grimes 
558fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
568fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
578fae3551SRodney W. Grimes 
588fae3551SRodney W. Grimes #include <err.h>
598fae3551SRodney W. Grimes #include <fcntl.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 
651c85e6a3SKirk McKusick /*
661c85e6a3SKirk McKusick  * Possible superblock locations ordered from most to least likely.
671c85e6a3SKirk McKusick  */
681c85e6a3SKirk McKusick static int sblock_try[] = SBLOCKSEARCH;
691c85e6a3SKirk McKusick 
70ec74b3f3SPhilippe Charnier static void
71ec74b3f3SPhilippe Charnier usage(void)
72ec74b3f3SPhilippe Charnier {
73a48ea640STom Rhodes 	(void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
74ec74b3f3SPhilippe Charnier 	exit(1);
75ec74b3f3SPhilippe Charnier }
76ec74b3f3SPhilippe Charnier 
778fae3551SRodney W. Grimes int
7806e8d954SMark Murray main(int argc, char *argv[])
798fae3551SRodney W. Grimes {
80582236c5SWarner Losh 	struct fs *sbp;
811c85e6a3SKirk McKusick 	struct ufs1_dinode *dp1;
821c85e6a3SKirk McKusick 	struct ufs2_dinode *dp2;
831c85e6a3SKirk McKusick 	char *ibuf[MAXBSIZE];
848fae3551SRodney W. Grimes 	long generation, bsize;
858fae3551SRodney W. Grimes 	off_t offset;
861c85e6a3SKirk McKusick 	int i, fd, inonum;
871c85e6a3SKirk McKusick 	char *fs, sblock[SBLOCKSIZE];
888fae3551SRodney W. Grimes 
89ec74b3f3SPhilippe Charnier 	if (argc < 3)
90ec74b3f3SPhilippe Charnier 		usage();
918fae3551SRodney W. Grimes 
928fae3551SRodney W. Grimes 	fs = *++argv;
938fae3551SRodney W. Grimes 
948fae3551SRodney W. Grimes 	/* get the superblock. */
958fae3551SRodney W. Grimes 	if ((fd = open(fs, O_RDWR, 0)) < 0)
968fae3551SRodney W. Grimes 		err(1, "%s", fs);
971c85e6a3SKirk McKusick 	for (i = 0; sblock_try[i] != -1; i++) {
981c85e6a3SKirk McKusick 		if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0)
998fae3551SRodney W. Grimes 			err(1, "%s", fs);
100a715c4a4SPhilippe Charnier 		if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
101a715c4a4SPhilippe Charnier 			errx(1, "%s: can't read superblock", fs);
1028fae3551SRodney W. Grimes 		sbp = (struct fs *)sblock;
1031c85e6a3SKirk McKusick 		if ((sbp->fs_magic == FS_UFS1_MAGIC ||
1041c85e6a3SKirk McKusick 		     (sbp->fs_magic == FS_UFS2_MAGIC &&
1051c85e6a3SKirk McKusick 		      sbp->fs_sblockloc == numfrags(sbp, sblock_try[i]))) &&
1061c85e6a3SKirk McKusick 		    sbp->fs_bsize <= MAXBSIZE &&
10706e8d954SMark Murray 		    sbp->fs_bsize >= (int)sizeof(struct fs))
1081c85e6a3SKirk McKusick 			break;
1091c85e6a3SKirk McKusick 	}
1101c85e6a3SKirk McKusick 	if (sblock_try[i] == -1) {
111a48ea640STom Rhodes 		fprintf(stderr, "Cannot find special_device\n");
1121c85e6a3SKirk McKusick 		exit(2);
1131c85e6a3SKirk McKusick 	}
1148fae3551SRodney W. Grimes 	bsize = sbp->fs_bsize;
1158fae3551SRodney W. Grimes 
1168fae3551SRodney W. Grimes 	/* remaining arguments are inode numbers. */
1178fae3551SRodney W. Grimes 	while (*++argv) {
1188fae3551SRodney W. Grimes 		/* get the inode number. */
119a715c4a4SPhilippe Charnier 		if ((inonum = atoi(*argv)) <= 0)
120ec74b3f3SPhilippe Charnier 			errx(1, "%s is not a valid inode number", *argv);
1218fae3551SRodney W. Grimes 		(void)printf("clearing %d\n", inonum);
1228fae3551SRodney W. Grimes 
1238fae3551SRodney W. Grimes 		/* read in the appropriate block. */
1248fae3551SRodney W. Grimes 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
1258fae3551SRodney W. Grimes 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
1268fae3551SRodney W. Grimes 		offset *= DEV_BSIZE;			/* disk blk to bytes */
1278fae3551SRodney W. Grimes 
1288fae3551SRodney W. Grimes 		/* seek and read the block */
1298fae3551SRodney W. Grimes 		if (lseek(fd, offset, SEEK_SET) < 0)
1308fae3551SRodney W. Grimes 			err(1, "%s", fs);
1318fae3551SRodney W. Grimes 		if (read(fd, ibuf, bsize) != bsize)
1328fae3551SRodney W. Grimes 			err(1, "%s", fs);
1338fae3551SRodney W. Grimes 
1341c85e6a3SKirk McKusick 		if (sbp->fs_magic == FS_UFS2_MAGIC) {
1358fae3551SRodney W. Grimes 			/* get the inode within the block. */
1361c85e6a3SKirk McKusick 			dp2 = &(((struct ufs2_dinode *)ibuf)
1371c85e6a3SKirk McKusick 			    [ino_to_fsbo(sbp, inonum)]);
1388fae3551SRodney W. Grimes 
1398fae3551SRodney W. Grimes 			/* clear the inode, and bump the generation count. */
1401c85e6a3SKirk McKusick 			generation = dp2->di_gen + 1;
1411c85e6a3SKirk McKusick 			memset(dp2, 0, sizeof(*dp2));
1421c85e6a3SKirk McKusick 			dp2->di_gen = generation;
1431c85e6a3SKirk McKusick 		} else {
1441c85e6a3SKirk McKusick 			/* get the inode within the block. */
1451c85e6a3SKirk McKusick 			dp1 = &(((struct ufs1_dinode *)ibuf)
1461c85e6a3SKirk McKusick 			    [ino_to_fsbo(sbp, inonum)]);
1471c85e6a3SKirk McKusick 
1481c85e6a3SKirk McKusick 			/* clear the inode, and bump the generation count. */
1491c85e6a3SKirk McKusick 			generation = dp1->di_gen + 1;
1501c85e6a3SKirk McKusick 			memset(dp1, 0, sizeof(*dp1));
1511c85e6a3SKirk McKusick 			dp1->di_gen = generation;
1521c85e6a3SKirk McKusick 		}
1538fae3551SRodney W. Grimes 
1548fae3551SRodney W. Grimes 		/* backup and write the block */
1558fae3551SRodney W. Grimes 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
1568fae3551SRodney W. Grimes 			err(1, "%s", fs);
1578fae3551SRodney W. Grimes 		if (write(fd, ibuf, bsize) != bsize)
1588fae3551SRodney W. Grimes 			err(1, "%s", fs);
1598fae3551SRodney W. Grimes 		(void)fsync(fd);
1608fae3551SRodney W. Grimes 	}
1618fae3551SRodney W. Grimes 	(void)close(fd);
1628fae3551SRodney W. Grimes 	exit(0);
1638fae3551SRodney W. Grimes }
164