xref: /freebsd/sbin/clri/clri.c (revision ec74b3f3ea80e3fd18cb267b7aa23ea7a17a7070)
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
47ec74b3f3SPhilippe Charnier static const char rcsid[] =
48ec74b3f3SPhilippe Charnier 	"$Id$";
498fae3551SRodney W. Grimes #endif /* not lint */
508fae3551SRodney W. Grimes 
518fae3551SRodney W. Grimes #include <sys/param.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>
578fae3551SRodney W. Grimes #include <fcntl.h>
588fae3551SRodney W. Grimes #include <stdlib.h>
598fae3551SRodney W. Grimes #include <string.h>
608fae3551SRodney W. Grimes #include <stdio.h>
618fae3551SRodney W. Grimes #include <unistd.h>
628fae3551SRodney W. Grimes 
63ec74b3f3SPhilippe Charnier static void
64ec74b3f3SPhilippe Charnier usage(void)
65ec74b3f3SPhilippe Charnier {
66ec74b3f3SPhilippe Charnier 	(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
67ec74b3f3SPhilippe Charnier 	exit(1);
68ec74b3f3SPhilippe Charnier }
69ec74b3f3SPhilippe Charnier 
708fae3551SRodney W. Grimes int
718fae3551SRodney W. Grimes main(argc, argv)
728fae3551SRodney W. Grimes 	int argc;
738fae3551SRodney W. Grimes 	char *argv[];
748fae3551SRodney W. Grimes {
758fae3551SRodney W. Grimes 	register struct fs *sbp;
768fae3551SRodney W. Grimes 	register struct dinode *ip;
778fae3551SRodney W. Grimes 	register int fd;
788fae3551SRodney W. Grimes 	struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
798fae3551SRodney W. Grimes 	long generation, bsize;
808fae3551SRodney W. Grimes 	off_t offset;
818fae3551SRodney W. Grimes 	int inonum;
828fae3551SRodney W. Grimes 	char *fs, sblock[SBSIZE];
838fae3551SRodney W. Grimes 
84ec74b3f3SPhilippe Charnier 	if (argc < 3)
85ec74b3f3SPhilippe Charnier 		usage();
868fae3551SRodney W. Grimes 
878fae3551SRodney W. Grimes 	fs = *++argv;
888fae3551SRodney W. Grimes 
898fae3551SRodney W. Grimes 	/* get the superblock. */
908fae3551SRodney W. Grimes 	if ((fd = open(fs, O_RDWR, 0)) < 0)
918fae3551SRodney W. Grimes 		err(1, "%s", fs);
928fae3551SRodney W. Grimes 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
938fae3551SRodney W. Grimes 		err(1, "%s", fs);
94a715c4a4SPhilippe Charnier 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
95a715c4a4SPhilippe Charnier 		errx(1, "%s: can't read superblock", fs);
968fae3551SRodney W. Grimes 
978fae3551SRodney W. Grimes 	sbp = (struct fs *)sblock;
98a715c4a4SPhilippe Charnier 	if (sbp->fs_magic != FS_MAGIC)
99ec74b3f3SPhilippe Charnier 		errx(1, "%s: superblock magic number 0x%x, not 0x%x",
1008fae3551SRodney W. Grimes 		    fs, sbp->fs_magic, FS_MAGIC);
1018fae3551SRodney W. Grimes 	bsize = sbp->fs_bsize;
1028fae3551SRodney W. Grimes 
1038fae3551SRodney W. Grimes 	/* remaining arguments are inode numbers. */
1048fae3551SRodney W. Grimes 	while (*++argv) {
1058fae3551SRodney W. Grimes 		/* get the inode number. */
106a715c4a4SPhilippe Charnier 		if ((inonum = atoi(*argv)) <= 0)
107ec74b3f3SPhilippe Charnier 			errx(1, "%s is not a valid inode number", *argv);
1088fae3551SRodney W. Grimes 		(void)printf("clearing %d\n", inonum);
1098fae3551SRodney W. Grimes 
1108fae3551SRodney W. Grimes 		/* read in the appropriate block. */
1118fae3551SRodney W. Grimes 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
1128fae3551SRodney W. Grimes 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
1138fae3551SRodney W. Grimes 		offset *= DEV_BSIZE;			/* disk blk to bytes */
1148fae3551SRodney W. Grimes 
1158fae3551SRodney W. Grimes 		/* seek and read the block */
1168fae3551SRodney W. Grimes 		if (lseek(fd, offset, SEEK_SET) < 0)
1178fae3551SRodney W. Grimes 			err(1, "%s", fs);
1188fae3551SRodney W. Grimes 		if (read(fd, ibuf, bsize) != bsize)
1198fae3551SRodney W. Grimes 			err(1, "%s", fs);
1208fae3551SRodney W. Grimes 
1218fae3551SRodney W. Grimes 		/* get the inode within the block. */
1228fae3551SRodney W. Grimes 		ip = &ibuf[ino_to_fsbo(sbp, inonum)];
1238fae3551SRodney W. Grimes 
1248fae3551SRodney W. Grimes 		/* clear the inode, and bump the generation count. */
1258fae3551SRodney W. Grimes 		generation = ip->di_gen + 1;
1268fae3551SRodney W. Grimes 		memset(ip, 0, sizeof(*ip));
1278fae3551SRodney W. Grimes 		ip->di_gen = generation;
1288fae3551SRodney W. Grimes 
1298fae3551SRodney W. Grimes 		/* backup and write the block */
1308fae3551SRodney W. Grimes 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
1318fae3551SRodney W. Grimes 			err(1, "%s", fs);
1328fae3551SRodney W. Grimes 		if (write(fd, ibuf, bsize) != bsize)
1338fae3551SRodney W. Grimes 			err(1, "%s", fs);
1348fae3551SRodney W. Grimes 		(void)fsync(fd);
1358fae3551SRodney W. Grimes 	}
1368fae3551SRodney W. Grimes 	(void)close(fd);
1378fae3551SRodney W. Grimes 	exit(0);
1388fae3551SRodney W. Grimes }
139