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