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