Commit | Line | Data |
---|---|---|
63ac1b03 KM |
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 | |
1b71c7c8 | 6 | * Rich $alz of BBN Inc. |
63ac1b03 KM |
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 */ | |
be79e615 | 16 | |
63ac1b03 | 17 | #ifndef lint |
b4c9333d | 18 | static char sccsid[] = "@(#)clri.c 5.4 (Berkeley) %G%"; |
63ac1b03 | 19 | #endif /* not lint */ |
45eb394d | 20 | |
45eb394d | 21 | #include <sys/param.h> |
b4c9333d KB |
22 | #include <ufs/ufs/quota.h> |
23 | #include <ufs/ufs/inode.h> | |
24 | #include <ufs/ffs/fs.h> | |
63ac1b03 KM |
25 | #include <unistd.h> |
26 | #include <stdio.h> | |
27 | #include <fcntl.h> | |
28 | #include <errno.h> | |
be79e615 | 29 | |
63ac1b03 | 30 | char *fs; |
be79e615 KM |
31 | |
32 | main(argc, argv) | |
e335baf5 | 33 | int argc; |
63ac1b03 | 34 | char **argv; |
be79e615 | 35 | { |
63ac1b03 KM |
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]; | |
be79e615 | 43 | |
e335baf5 | 44 | if (argc < 3) { |
63ac1b03 KM |
45 | (void)fprintf(stderr, "usage: clri filesystem inode ...\n"); |
46 | exit(1); | |
be79e615 | 47 | } |
63ac1b03 KM |
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); | |
e335baf5 | 60 | } |
63ac1b03 KM |
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); | |
87ad4e58 | 68 | } |
63ac1b03 KM |
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); | |
be79e615 | 78 | } |
63ac1b03 KM |
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); | |
be79e615 | 106 | } |
63ac1b03 KM |
107 | (void)close(fd); |
108 | exit(0); | |
be79e615 KM |
109 | } |
110 | ||
63ac1b03 | 111 | error() |
be79e615 | 112 | { |
63ac1b03 KM |
113 | (void)fprintf(stderr, "clri: %s: %s\n", fs, strerror(errno)); |
114 | exit(1); | |
be79e615 | 115 | } |