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 * 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
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>
53 fprintf(stderr
, "usage: clri filesystem inode ...\n");
58 main(int argc
, char **argv
)
61 struct ufs1_dinode
*ip
;
63 struct ufs1_dinode ibuf
[MAXBSIZE
/ sizeof (struct ufs1_dinode
)];
64 long generation
, bsize
;
67 char *fs
, sblock
[SBSIZE
];
74 /* get the superblock. */
75 if ((fd
= open(fs
, O_RDWR
, 0)) < 0)
77 if (lseek(fd
, (off_t
)SBOFF
, SEEK_SET
) < 0)
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. */
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)
103 if (read(fd
, ibuf
, bsize
) != bsize
)
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)
117 if (write(fd
, ibuf
, bsize
) != bsize
)