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
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 * 4. 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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1990, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)clri.c 8.2 (Berkeley) 9/23/93";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/disklabel.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ffs/fs.h>
62 * Possible superblock locations ordered from most to least likely.
64 static int sblock_try[] = SBLOCKSEARCH;
69 (void)fprintf(stderr, "usage: clri special_device inode_number ...\n");
74 main(int argc, char *argv[])
77 struct ufs1_dinode *dp1;
78 struct ufs2_dinode *dp2;
80 long generation, bsize;
83 char *fs, sblock[SBLOCKSIZE];
92 /* get the superblock. */
93 if ((fd = open(fs, O_RDWR, 0)) < 0)
95 for (i = 0; sblock_try[i] != -1; i++) {
96 if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0)
98 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
99 errx(1, "%s: can't read superblock", fs);
100 sbp = (struct fs *)sblock;
101 if ((sbp->fs_magic == FS_UFS1_MAGIC ||
102 (sbp->fs_magic == FS_UFS2_MAGIC &&
103 sbp->fs_sblockloc == sblock_try[i])) &&
104 sbp->fs_bsize <= MAXBSIZE &&
105 sbp->fs_bsize >= (int)sizeof(struct fs))
108 if (sblock_try[i] == -1)
109 errx(2, "cannot find file system superblock");
110 bsize = sbp->fs_bsize;
112 /* remaining arguments are inode numbers. */
114 /* get the inode number. */
115 if ((inonum = atoi(*argv)) <= 0)
116 errx(1, "%s is not a valid inode number", *argv);
117 (void)printf("clearing %d\n", inonum);
119 /* read in the appropriate block. */
120 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */
121 offset = fsbtodb(sbp, offset); /* fs blk disk blk */
122 offset *= DEV_BSIZE; /* disk blk to bytes */
124 /* seek and read the block */
125 if (lseek(fd, offset, SEEK_SET) < 0)
127 if (read(fd, ibuf, bsize) != bsize)
130 if (sbp->fs_magic == FS_UFS2_MAGIC) {
131 /* get the inode within the block. */
132 dp2 = &(((struct ufs2_dinode *)v)
133 [ino_to_fsbo(sbp, inonum)]);
135 /* clear the inode, and bump the generation count. */
136 generation = dp2->di_gen + 1;
137 memset(dp2, 0, sizeof(*dp2));
138 dp2->di_gen = generation;
140 /* get the inode within the block. */
141 dp1 = &(((struct ufs1_dinode *)v)
142 [ino_to_fsbo(sbp, inonum)]);
144 /* clear the inode, and bump the generation count. */
145 generation = dp1->di_gen + 1;
146 memset(dp1, 0, sizeof(*dp1));
147 dp1->di_gen = generation;
150 /* backup and write the block */
151 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
153 if (write(fd, ibuf, bsize) != bsize)