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>
64 * Possible superblock locations ordered from most to least likely.
66 static int sblock_try[] = SBLOCKSEARCH;
71 (void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
76 main(int argc, char *argv[])
79 struct ufs1_dinode *dp1;
80 struct ufs2_dinode *dp2;
82 long generation, bsize;
85 char *fs, sblock[SBLOCKSIZE];
94 /* get the superblock. */
95 if ((fd = open(fs, O_RDWR, 0)) < 0)
97 for (i = 0; sblock_try[i] != -1; i++) {
98 if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0)
100 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
101 errx(1, "%s: can't read superblock", fs);
102 sbp = (struct fs *)sblock;
103 if ((sbp->fs_magic == FS_UFS1_MAGIC ||
104 (sbp->fs_magic == FS_UFS2_MAGIC &&
105 sbp->fs_sblockloc == sblock_try[i])) &&
106 sbp->fs_bsize <= MAXBSIZE &&
107 sbp->fs_bsize >= (int)sizeof(struct fs))
110 if (sblock_try[i] == -1)
111 errx(2, "cannot find file system superblock");
112 bsize = sbp->fs_bsize;
114 /* remaining arguments are inode numbers. */
116 /* get the inode number. */
117 if ((inonum = atoi(*argv)) <= 0)
118 errx(1, "%s is not a valid inode number", *argv);
119 (void)printf("clearing %d\n", inonum);
121 /* read in the appropriate block. */
122 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */
123 offset = fsbtodb(sbp, offset); /* fs blk disk blk */
124 offset *= DEV_BSIZE; /* disk blk to bytes */
126 /* seek and read the block */
127 if (lseek(fd, offset, SEEK_SET) < 0)
129 if (read(fd, ibuf, bsize) != bsize)
132 if (sbp->fs_magic == FS_UFS2_MAGIC) {
133 /* get the inode within the block. */
134 dp2 = &(((struct ufs2_dinode *)v)
135 [ino_to_fsbo(sbp, inonum)]);
137 /* clear the inode, and bump the generation count. */
138 generation = dp2->di_gen + 1;
139 memset(dp2, 0, sizeof(*dp2));
140 dp2->di_gen = generation;
142 /* get the inode within the block. */
143 dp1 = &(((struct ufs1_dinode *)v)
144 [ino_to_fsbo(sbp, inonum)]);
146 /* clear the inode, and bump the generation count. */
147 generation = dp1->di_gen + 1;
148 memset(dp1, 0, sizeof(*dp1));
149 dp1->di_gen = generation;
152 /* backup and write the block */
153 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
155 if (write(fd, ibuf, bsize) != bsize)