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