2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rich $alz of BBN Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 static const char copyright[] =
38 "@(#) Copyright (c) 1990, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)clri.c 8.2 (Berkeley) 9/23/93";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/disklabel.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
68 (void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
73 main(int argc, char *argv[])
76 struct ufs1_dinode *dp1;
77 struct ufs2_dinode *dp2;
79 long generation, bsize;
90 /* get the superblock. */
91 if ((fd = open(fsname, O_RDWR, 0)) < 0)
93 if ((ret = sbget(fd, &fs, -1)) != 0) {
96 warn("Cannot find file system superblock");
99 warn("Unable to read file system superblock");
103 bsize = fs->fs_bsize;
105 /* remaining arguments are inode numbers. */
107 /* get the inode number. */
108 if ((inonum = atoi(*argv)) <= 0)
109 errx(1, "%s is not a valid inode number", *argv);
110 (void)printf("clearing %d\n", inonum);
112 /* read in the appropriate block. */
113 offset = ino_to_fsba(fs, inonum); /* inode to fs blk */
114 offset = fsbtodb(fs, offset); /* fs blk disk blk */
115 offset *= DEV_BSIZE; /* disk blk to bytes */
117 /* seek and read the block */
118 if (lseek(fd, offset, SEEK_SET) < 0)
119 err(1, "%s", fsname);
120 if (read(fd, ibuf, bsize) != bsize)
121 err(1, "%s", fsname);
123 if (fs->fs_magic == FS_UFS2_MAGIC) {
124 /* get the inode within the block. */
125 dp2 = &(((struct ufs2_dinode *)v)
126 [ino_to_fsbo(fs, inonum)]);
128 /* clear the inode, and bump the generation count. */
129 generation = dp2->di_gen + 1;
130 memset(dp2, 0, sizeof(*dp2));
131 dp2->di_gen = generation;
133 /* get the inode within the block. */
134 dp1 = &(((struct ufs1_dinode *)v)
135 [ino_to_fsbo(fs, inonum)]);
137 /* clear the inode, and bump the generation count. */
138 generation = dp1->di_gen + 1;
139 memset(dp1, 0, sizeof(*dp1));
140 dp1->di_gen = generation;
143 /* backup and write the block */
144 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
145 err(1, "%s", fsname);
146 if (write(fd, ibuf, bsize) != bsize)
147 err(1, "%s", fsname);