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