142864Smckusick /*
261478Sbostic * Copyright (c) 1990, 1993
361478Sbostic * The Regents of the University of California. All rights reserved.
442864Smckusick *
542864Smckusick * This code is derived from software contributed to Berkeley by
643088Sbostic * Rich $alz of BBN Inc.
742864Smckusick *
842864Smckusick * %sccs.include.redist.c%
942864Smckusick */
105341Smckusick
1142864Smckusick #ifndef lint
1261478Sbostic static char copyright[] =
1361478Sbostic "@(#) Copyright (c) 1990, 1993\n\
1461478Sbostic The Regents of the University of California. All rights reserved.\n";
1542864Smckusick #endif /* not lint */
165341Smckusick
1742864Smckusick #ifndef lint
18*68985Sbostic static char sccsid[] = "@(#)clri.c 8.3 (Berkeley) 04/28/95";
1942864Smckusick #endif /* not lint */
2042864Smckusick
216494Smckusick #include <sys/param.h>
2253730Smckusick #include <sys/time.h>
2360145Sbostic
2453730Smckusick #include <ufs/ufs/dinode.h>
2551614Sbostic #include <ufs/ffs/fs.h>
2660145Sbostic
2760145Sbostic #include <err.h>
2860145Sbostic #include <errno.h>
2960145Sbostic #include <fcntl.h>
3060145Sbostic #include <stdlib.h>
3160145Sbostic #include <string.h>
3260145Sbostic #include <stdio.h>
3342864Smckusick #include <unistd.h>
345341Smckusick
3560145Sbostic int
main(argc,argv)365341Smckusick main(argc, argv)
375345Smckusic int argc;
3860145Sbostic char *argv[];
395341Smckusick {
4042864Smckusick register struct fs *sbp;
4142864Smckusick register struct dinode *ip;
4242864Smckusick register int fd;
4342864Smckusick struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
4457995Sralph long generation, bsize;
4557995Sralph off_t offset;
4642864Smckusick int inonum;
4760145Sbostic char *fs, sblock[SBSIZE];
485341Smckusick
495345Smckusic if (argc < 3) {
5042864Smckusick (void)fprintf(stderr, "usage: clri filesystem inode ...\n");
5142864Smckusick exit(1);
525341Smckusick }
5342864Smckusick
5442864Smckusick fs = *++argv;
5542864Smckusick
5642864Smckusick /* get the superblock. */
5742864Smckusick if ((fd = open(fs, O_RDWR, 0)) < 0)
5860145Sbostic err(1, "%s", fs);
5957995Sralph if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
6060145Sbostic err(1, "%s", fs);
61*68985Sbostic if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
62*68985Sbostic errx(1, "%s: can't read superblock", fs);
6342864Smckusick
6442864Smckusick sbp = (struct fs *)sblock;
65*68985Sbostic if (sbp->fs_magic != FS_MAGIC)
66*68985Sbostic errx(1, "%s: superblock magic number 0x%x, not 0x%x",
6742864Smckusick fs, sbp->fs_magic, FS_MAGIC);
6842864Smckusick bsize = sbp->fs_bsize;
6942864Smckusick
7042864Smckusick /* remaining arguments are inode numbers. */
7142864Smckusick while (*++argv) {
7242864Smckusick /* get the inode number. */
73*68985Sbostic if ((inonum = atoi(*argv)) <= 0)
74*68985Sbostic errx(1, "%s is not a valid inode number", *argv);
7542864Smckusick (void)printf("clearing %d\n", inonum);
7642864Smckusick
7742864Smckusick /* read in the appropriate block. */
7864639Sbostic offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */
7964639Sbostic offset = fsbtodb(sbp, offset); /* fs blk disk blk */
8064639Sbostic offset *= DEV_BSIZE; /* disk blk to bytes */
8142864Smckusick
8242864Smckusick /* seek and read the block */
8342864Smckusick if (lseek(fd, offset, SEEK_SET) < 0)
8460145Sbostic err(1, "%s", fs);
8560145Sbostic if (read(fd, ibuf, bsize) != bsize)
8660145Sbostic err(1, "%s", fs);
8742864Smckusick
8842864Smckusick /* get the inode within the block. */
8964639Sbostic ip = &ibuf[ino_to_fsbo(sbp, inonum)];
9042864Smckusick
9142864Smckusick /* clear the inode, and bump the generation count. */
9242864Smckusick generation = ip->di_gen + 1;
9360145Sbostic memset(ip, 0, sizeof(*ip));
9442864Smckusick ip->di_gen = generation;
9542864Smckusick
9642864Smckusick /* backup and write the block */
9757995Sralph if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
9860145Sbostic err(1, "%s", fs);
9960145Sbostic if (write(fd, ibuf, bsize) != bsize)
10060145Sbostic err(1, "%s", fs);
10142864Smckusick (void)fsync(fd);
1025341Smckusick }
10342864Smckusick (void)close(fd);
10442864Smckusick exit(0);
1055341Smckusick }
106