xref: /original-bsd/sbin/clri/clri.c (revision 0842ddeb)
1 /*
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rich $alz of BBN Inc.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char copyright[] =
13 "@(#) Copyright (c) 1990, 1993\n\
14 	The Regents of the University of California.  All rights reserved.\n";
15 #endif /* not lint */
16 
17 #ifndef lint
18 static char sccsid[] = "@(#)clri.c	8.3 (Berkeley) 04/28/95";
19 #endif /* not lint */
20 
21 #include <sys/param.h>
22 #include <sys/time.h>
23 
24 #include <ufs/ufs/dinode.h>
25 #include <ufs/ffs/fs.h>
26 
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 int
36 main(argc, argv)
37 	int argc;
38 	char *argv[];
39 {
40 	register struct fs *sbp;
41 	register struct dinode *ip;
42 	register int fd;
43 	struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
44 	long generation, bsize;
45 	off_t offset;
46 	int inonum;
47 	char *fs, sblock[SBSIZE];
48 
49 	if (argc < 3) {
50 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
51 		exit(1);
52 	}
53 
54 	fs = *++argv;
55 
56 	/* get the superblock. */
57 	if ((fd = open(fs, O_RDWR, 0)) < 0)
58 		err(1, "%s", fs);
59 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
60 		err(1, "%s", fs);
61 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
62 		errx(1, "%s: can't read superblock", fs);
63 
64 	sbp = (struct fs *)sblock;
65 	if (sbp->fs_magic != FS_MAGIC)
66 		errx(1, "%s: superblock magic number 0x%x, not 0x%x",
67 		    fs, sbp->fs_magic, FS_MAGIC);
68 	bsize = sbp->fs_bsize;
69 
70 	/* remaining arguments are inode numbers. */
71 	while (*++argv) {
72 		/* get the inode number. */
73 		if ((inonum = atoi(*argv)) <= 0)
74 			errx(1, "%s is not a valid inode number", *argv);
75 		(void)printf("clearing %d\n", inonum);
76 
77 		/* read in the appropriate block. */
78 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
79 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
80 		offset *= DEV_BSIZE;			/* disk blk to bytes */
81 
82 		/* seek and read the block */
83 		if (lseek(fd, offset, SEEK_SET) < 0)
84 			err(1, "%s", fs);
85 		if (read(fd, ibuf, bsize) != bsize)
86 			err(1, "%s", fs);
87 
88 		/* get the inode within the block. */
89 		ip = &ibuf[ino_to_fsbo(sbp, inonum)];
90 
91 		/* clear the inode, and bump the generation count. */
92 		generation = ip->di_gen + 1;
93 		memset(ip, 0, sizeof(*ip));
94 		ip->di_gen = generation;
95 
96 		/* backup and write the block */
97 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
98 			err(1, "%s", fs);
99 		if (write(fd, ibuf, bsize) != bsize)
100 			err(1, "%s", fs);
101 		(void)fsync(fd);
102 	}
103 	(void)close(fd);
104 	exit(0);
105 }
106