xref: /freebsd/sbin/clri/clri.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
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.
16*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
178fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
188fae3551SRodney W. Grimes  *    without specific prior written permission.
198fae3551SRodney W. Grimes  *
208fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
218fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
228fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
238fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
248fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
258fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
268fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
278fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
288fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308fae3551SRodney W. Grimes  * SUCH DAMAGE.
318fae3551SRodney W. Grimes  */
328fae3551SRodney W. Grimes 
33c69284caSDavid E. O'Brien #if 0
348fae3551SRodney W. Grimes #ifndef lint
35ec74b3f3SPhilippe Charnier static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1990, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
418fae3551SRodney W. Grimes static char sccsid[] = "@(#)clri.c	8.2 (Berkeley) 9/23/93";
428fae3551SRodney W. Grimes #endif /* not lint */
43c69284caSDavid E. O'Brien #endif
448a1dff0cSPhilippe Charnier 
4506e8d954SMark Murray #include <sys/cdefs.h>
4606e8d954SMark Murray __FBSDID("$FreeBSD$");
4706e8d954SMark Murray 
488fae3551SRodney W. Grimes #include <sys/param.h>
4975766e17SPoul-Henning Kamp #include <sys/disklabel.h>
508fae3551SRodney W. Grimes 
518fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
528fae3551SRodney W. Grimes #include <ufs/ffs/fs.h>
538fae3551SRodney W. Grimes 
548fae3551SRodney W. Grimes #include <err.h>
558fae3551SRodney W. Grimes #include <fcntl.h>
568fae3551SRodney W. Grimes #include <stdlib.h>
578fae3551SRodney W. Grimes #include <string.h>
588fae3551SRodney W. Grimes #include <stdio.h>
598fae3551SRodney W. Grimes #include <unistd.h>
608fae3551SRodney W. Grimes 
611c85e6a3SKirk McKusick /*
621c85e6a3SKirk McKusick  * Possible superblock locations ordered from most to least likely.
631c85e6a3SKirk McKusick  */
641c85e6a3SKirk McKusick static int sblock_try[] = SBLOCKSEARCH;
651c85e6a3SKirk McKusick 
66ec74b3f3SPhilippe Charnier static void
67ec74b3f3SPhilippe Charnier usage(void)
68ec74b3f3SPhilippe Charnier {
69a48ea640STom Rhodes 	(void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
70ec74b3f3SPhilippe Charnier 	exit(1);
71ec74b3f3SPhilippe Charnier }
72ec74b3f3SPhilippe Charnier 
738fae3551SRodney W. Grimes int
7406e8d954SMark Murray main(int argc, char *argv[])
758fae3551SRodney W. Grimes {
76582236c5SWarner Losh 	struct fs *sbp;
771c85e6a3SKirk McKusick 	struct ufs1_dinode *dp1;
781c85e6a3SKirk McKusick 	struct ufs2_dinode *dp2;
791c85e6a3SKirk McKusick 	char *ibuf[MAXBSIZE];
808fae3551SRodney W. Grimes 	long generation, bsize;
818fae3551SRodney W. Grimes 	off_t offset;
821c85e6a3SKirk McKusick 	int i, fd, inonum;
831c85e6a3SKirk McKusick 	char *fs, sblock[SBLOCKSIZE];
8456589eb7SJohn Birrell 	void *v = ibuf;
858fae3551SRodney W. Grimes 
86ec74b3f3SPhilippe Charnier 	if (argc < 3)
87ec74b3f3SPhilippe Charnier 		usage();
888fae3551SRodney W. Grimes 
898fae3551SRodney W. Grimes 	fs = *++argv;
908a1dff0cSPhilippe Charnier 	sbp = NULL;
918fae3551SRodney W. Grimes 
928fae3551SRodney W. Grimes 	/* get the superblock. */
938fae3551SRodney W. Grimes 	if ((fd = open(fs, O_RDWR, 0)) < 0)
948fae3551SRodney W. Grimes 		err(1, "%s", fs);
951c85e6a3SKirk McKusick 	for (i = 0; sblock_try[i] != -1; i++) {
961c85e6a3SKirk McKusick 		if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0)
978fae3551SRodney W. Grimes 			err(1, "%s", fs);
98a715c4a4SPhilippe Charnier 		if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
99a715c4a4SPhilippe Charnier 			errx(1, "%s: can't read superblock", fs);
1008fae3551SRodney W. Grimes 		sbp = (struct fs *)sblock;
1011c85e6a3SKirk McKusick 		if ((sbp->fs_magic == FS_UFS1_MAGIC ||
1021c85e6a3SKirk McKusick 		     (sbp->fs_magic == FS_UFS2_MAGIC &&
103ada981b2SKirk McKusick 		      sbp->fs_sblockloc == sblock_try[i])) &&
1041c85e6a3SKirk McKusick 		    sbp->fs_bsize <= MAXBSIZE &&
10506e8d954SMark Murray 		    sbp->fs_bsize >= (int)sizeof(struct fs))
1061c85e6a3SKirk McKusick 			break;
1071c85e6a3SKirk McKusick 	}
1088a1dff0cSPhilippe Charnier 	if (sblock_try[i] == -1)
1098a1dff0cSPhilippe Charnier 		errx(2, "cannot find file system superblock");
1108fae3551SRodney W. Grimes 	bsize = sbp->fs_bsize;
1118fae3551SRodney W. Grimes 
1128fae3551SRodney W. Grimes 	/* remaining arguments are inode numbers. */
1138fae3551SRodney W. Grimes 	while (*++argv) {
1148fae3551SRodney W. Grimes 		/* get the inode number. */
115a715c4a4SPhilippe Charnier 		if ((inonum = atoi(*argv)) <= 0)
116ec74b3f3SPhilippe Charnier 			errx(1, "%s is not a valid inode number", *argv);
1178fae3551SRodney W. Grimes 		(void)printf("clearing %d\n", inonum);
1188fae3551SRodney W. Grimes 
1198fae3551SRodney W. Grimes 		/* read in the appropriate block. */
1208fae3551SRodney W. Grimes 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
1218fae3551SRodney W. Grimes 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
1228fae3551SRodney W. Grimes 		offset *= DEV_BSIZE;			/* disk blk to bytes */
1238fae3551SRodney W. Grimes 
1248fae3551SRodney W. Grimes 		/* seek and read the block */
1258fae3551SRodney W. Grimes 		if (lseek(fd, offset, SEEK_SET) < 0)
1268fae3551SRodney W. Grimes 			err(1, "%s", fs);
1278fae3551SRodney W. Grimes 		if (read(fd, ibuf, bsize) != bsize)
1288fae3551SRodney W. Grimes 			err(1, "%s", fs);
1298fae3551SRodney W. Grimes 
1301c85e6a3SKirk McKusick 		if (sbp->fs_magic == FS_UFS2_MAGIC) {
1318fae3551SRodney W. Grimes 			/* get the inode within the block. */
13256589eb7SJohn Birrell 			dp2 = &(((struct ufs2_dinode *)v)
1331c85e6a3SKirk McKusick 			    [ino_to_fsbo(sbp, inonum)]);
1348fae3551SRodney W. Grimes 
1358fae3551SRodney W. Grimes 			/* clear the inode, and bump the generation count. */
1361c85e6a3SKirk McKusick 			generation = dp2->di_gen + 1;
1371c85e6a3SKirk McKusick 			memset(dp2, 0, sizeof(*dp2));
1381c85e6a3SKirk McKusick 			dp2->di_gen = generation;
1391c85e6a3SKirk McKusick 		} else {
1401c85e6a3SKirk McKusick 			/* get the inode within the block. */
14156589eb7SJohn Birrell 			dp1 = &(((struct ufs1_dinode *)v)
1421c85e6a3SKirk McKusick 			    [ino_to_fsbo(sbp, inonum)]);
1431c85e6a3SKirk McKusick 
1441c85e6a3SKirk McKusick 			/* clear the inode, and bump the generation count. */
1451c85e6a3SKirk McKusick 			generation = dp1->di_gen + 1;
1461c85e6a3SKirk McKusick 			memset(dp1, 0, sizeof(*dp1));
1471c85e6a3SKirk McKusick 			dp1->di_gen = generation;
1481c85e6a3SKirk McKusick 		}
1498fae3551SRodney W. Grimes 
1508fae3551SRodney W. Grimes 		/* backup and write the block */
1518fae3551SRodney W. Grimes 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
1528fae3551SRodney W. Grimes 			err(1, "%s", fs);
1538fae3551SRodney W. Grimes 		if (write(fd, ibuf, bsize) != bsize)
1548fae3551SRodney W. Grimes 			err(1, "%s", fs);
1558fae3551SRodney W. Grimes 		(void)fsync(fd);
1568fae3551SRodney W. Grimes 	}
1578fae3551SRodney W. Grimes 	(void)close(fd);
1588fae3551SRodney W. Grimes 	exit(0);
1598fae3551SRodney W. Grimes }
160