periodic(8): Sync with FreeBSD current
[dragonfly.git] / sbin / clri / clri.c
blob0e8a87b4641e0c8d3a2a59dbed48072c23bfd8bf
1 /*
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rich $alz of BBN Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)clri.c 8.2 (Berkeley) 9/23/93
34 * $FreeBSD: src/sbin/clri/clri.c,v 1.4 1999/08/28 00:12:32 peter Exp $
35 * $DragonFly: src/sbin/clri/clri.c,v 1.8 2006/04/03 01:58:48 dillon Exp $
38 #include <sys/param.h>
40 #include <vfs/ufs/dinode.h>
41 #include <vfs/ufs/fs.h>
43 #include <err.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <unistd.h>
50 static void
51 usage(void)
53 fprintf(stderr, "usage: clri filesystem inode ...\n");
54 exit(1);
57 int
58 main(int argc, char **argv)
60 struct fs *sbp;
61 struct ufs1_dinode *ip;
62 int fd;
63 struct ufs1_dinode ibuf[MAXBSIZE / sizeof (struct ufs1_dinode)];
64 long generation, bsize;
65 off_t offset;
66 int inonum;
67 char *fs, sblock[SBSIZE];
69 if (argc < 3)
70 usage();
72 fs = *++argv;
74 /* get the superblock. */
75 if ((fd = open(fs, O_RDWR, 0)) < 0)
76 err(1, "%s", fs);
77 if (lseek(fd, (off_t)SBOFF, SEEK_SET) < 0)
78 err(1, "%s", fs);
79 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
80 errx(1, "%s: can't read superblock", fs);
82 sbp = (struct fs *)sblock;
83 if (sbp->fs_magic != FS_MAGIC)
84 errx(1, "%s: superblock magic number 0x%x, not 0x%x",
85 fs, sbp->fs_magic, FS_MAGIC);
86 bsize = sbp->fs_bsize;
88 /* remaining arguments are inode numbers. */
89 while (*++argv) {
90 /* get the inode number. */
91 if ((inonum = atoi(*argv)) <= 0)
92 errx(1, "%s is not a valid inode number", *argv);
93 printf("clearing %d\n", inonum);
95 /* read in the appropriate block. */
96 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */
97 offset = fsbtodb(sbp, offset); /* fs blk disk blk */
98 offset *= DEV_BSIZE; /* disk blk to bytes */
100 /* seek and read the block */
101 if (lseek(fd, offset, SEEK_SET) < 0)
102 err(1, "%s", fs);
103 if (read(fd, ibuf, bsize) != bsize)
104 err(1, "%s", fs);
106 /* get the inode within the block. */
107 ip = &ibuf[ino_to_fsbo(sbp, inonum)];
109 /* clear the inode, and bump the generation count. */
110 generation = ip->di_gen + 1;
111 memset(ip, 0, sizeof(*ip));
112 ip->di_gen = generation;
114 /* backup and write the block */
115 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
116 err(1, "%s", fs);
117 if (write(fd, ibuf, bsize) != bsize)
118 err(1, "%s", fs);
119 fsync(fd);
121 close(fd);
122 exit(0);