xref: /original-bsd/sbin/clri/clri.c (revision 2bdcd748)
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.6 (Berkeley) 02/15/93";
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, bsize;
41 	off_t offset;
42 	int inonum;
43 	char sblock[SBSIZE];
44 
45 	if (argc < 3) {
46 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
47 		exit(1);
48 	}
49 
50 	fs = *++argv;
51 
52 	/* get the superblock. */
53 	if ((fd = open(fs, O_RDWR, 0)) < 0)
54 		error();
55 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
56 		error();
57 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) {
58 		(void)fprintf(stderr,
59 		    "clri: %s: can't read the superblock.\n", fs);
60 		exit(1);
61 	}
62 
63 	sbp = (struct fs *)sblock;
64 	if (sbp->fs_magic != FS_MAGIC) {
65 		(void)fprintf(stderr,
66 		    "clri: %s: superblock magic number 0x%x, not 0x%x.\n",
67 		    fs, sbp->fs_magic, FS_MAGIC);
68 		exit(1);
69 	}
70 	bsize = sbp->fs_bsize;
71 
72 	/* remaining arguments are inode numbers. */
73 	while (*++argv) {
74 		/* get the inode number. */
75 		if ((inonum = atoi(*argv)) <= 0) {
76 			(void)fprintf(stderr,
77 			    "clri: %s is not a valid inode number.\n", *argv);
78 			exit(1);
79 		}
80 		(void)printf("clearing %d\n", inonum);
81 
82 		/* read in the appropriate block. */
83 		offset = itod(sbp, inonum);	/* inode to fs block */
84 		offset = fsbtodb(sbp, offset);	/* fs block to disk block */
85 		offset *= DEV_BSIZE;		/* disk block to disk bytes */
86 
87 		/* seek and read the block */
88 		if (lseek(fd, offset, SEEK_SET) < 0)
89 			error();
90 		if (read(fd, (char *)ibuf, bsize) != bsize)
91 			error();
92 
93 		/* get the inode within the block. */
94 		ip = &ibuf[itoo(sbp, inonum)];
95 
96 		/* clear the inode, and bump the generation count. */
97 		generation = ip->di_gen + 1;
98 		bzero((char *)ip, sizeof *ip);
99 		ip->di_gen = generation;
100 
101 		/* backup and write the block */
102 		if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
103 			error();
104 		if (write(fd, (char *)ibuf, bsize) != bsize)
105 			error();
106 		(void)fsync(fd);
107 	}
108 	(void)close(fd);
109 	exit(0);
110 }
111 
112 error()
113 {
114 	(void)fprintf(stderr, "clri: %s: %s\n", fs, strerror(errno));
115 	exit(1);
116 }
117