non-AT&T version from Rich Salz
[unix-history] / usr / src / sbin / clri / clri.c
CommitLineData
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
6 * Rich Salz.
7 *
8 * %sccs.include.redist.c%
9 */
10
11#ifndef lint
12char 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
KM
17#ifndef lint
18static char sccsid[] = "@(#)clri.c 5.1 (Berkeley) %G%";
19#endif /* not lint */
45eb394d 20
be79e615 21/*
63ac1b03 22 * clri(8)
be79e615
KM
23 */
24
45eb394d 25#include <sys/param.h>
63ac1b03
KM
26#include <sys/time.h>
27#include <sys/vnode.h>
28#include <ufs/quota.h>
29#include <ufs/inode.h>
c0efa4d3 30#include <ufs/fs.h>
63ac1b03
KM
31#include <unistd.h>
32#include <stdio.h>
33#include <fcntl.h>
34#include <errno.h>
be79e615 35
63ac1b03 36char *fs;
be79e615
KM
37
38main(argc, argv)
e335baf5 39 int argc;
63ac1b03 40 char **argv;
be79e615 41{
63ac1b03
KM
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];
be79e615 49
e335baf5 50 if (argc < 3) {
63ac1b03
KM
51 (void)fprintf(stderr, "usage: clri filesystem inode ...\n");
52 exit(1);
be79e615 53 }
63ac1b03
KM
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);
e335baf5 66 }
63ac1b03
KM
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);
87ad4e58 74 }
63ac1b03
KM
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);
be79e615 84 }
63ac1b03
KM
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);
be79e615 112 }
63ac1b03
KM
113 (void)close(fd);
114 exit(0);
be79e615
KM
115}
116
63ac1b03 117error()
be79e615 118{
63ac1b03
KM
119 (void)fprintf(stderr, "clri: %s: %s\n", fs, strerror(errno));
120 exit(1);
be79e615 121}