1 /* $NetBSD: clri.c,v 1.21 2009/03/16 13:01:21 lukem Exp $ */
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)clri.c 8.3 (Berkeley) 4/28/95";
45 __RCSID("$NetBSD: clri.c,v 1.21 2009/03/16 13:01:21 lukem Exp $");
49 #include <sys/param.h>
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ufs/ufs_bswap.h>
54 #include <ufs/ffs/fs.h>
55 #include <ufs/ffs/ffs_extern.h>
66 * Possible superblock locations ordered from most to least likely.
68 static off_t sblock_try
[] = SBLOCKSEARCH
;
73 main(int argc
, char *argv
[])
76 struct ufs1_dinode
*ip1
;
77 struct ufs2_dinode
*ip2
;
84 char *fs
, sblock
[SBLOCKSIZE
];
85 int needswap
= 0, is_ufs2
= 0;
89 (void)fprintf(stderr
, "usage: clri filesystem inode ...\n");
96 /* get the superblock. */
97 if ((fd
= open(fs
, O_RDWR
, 0)) < 0)
100 sblockloc
= sblock_try
[i
];
102 errx(1, "%s: can't find superblock", fs
);
103 if (pread(fd
, sblock
, sizeof(sblock
), sblockloc
) != sizeof(sblock
))
104 errx(1, "%s: can't read superblock", fs
);
106 sbp
= (struct fs
*)sblock
;
107 switch(sbp
->fs_magic
) {
113 case FS_UFS2_MAGIC_SWAPPED
:
116 case FS_UFS1_MAGIC_SWAPPED
:
123 /* check we haven't found an alternate */
124 if (is_ufs2
|| sbp
->fs_old_flags
& FS_FLAGS_UPDATED
) {
125 if ((uint64_t)sblockloc
!= ufs_rw64(sbp
->fs_sblockloc
, needswap
))
128 if (sblockloc
== SBLOCK_UFS2
)
135 /* check that inode numbers are valid */
136 imax
= ufs_rw32(sbp
->fs_ncg
, needswap
) *
137 ufs_rw32(sbp
->fs_ipg
, needswap
);
138 for (i
= 1; i
< (argc
- 1); i
++)
139 if (atoi(argv
[i
]) <= 0 || atoi(argv
[i
]) >= imax
)
140 errx(1, "%s is not a valid inode number", argv
[i
]);
142 /* delete clean flag in the superblok */
143 sbp
->fs_clean
= ufs_rw32(ufs_rw32(sbp
->fs_clean
, needswap
) << 1,
145 if (lseek(fd
, sblockloc
, SEEK_SET
) < 0)
147 if (write(fd
, sblock
, sizeof(sblock
)) != sizeof(sblock
))
148 errx(1, "%s: can't rewrite superblock", fs
);
152 ffs_sb_swap(sbp
, sbp
);
154 bsize
= sbp
->fs_bsize
;
155 ibuf
= malloc(bsize
);
160 /* remaining arguments are inode numbers. */
162 /* get the inode number. */
163 inonum
= atoi(*argv
);
164 (void)printf("clearing %d\n", inonum
);
166 /* read in the appropriate block. */
167 offset
= ino_to_fsba(sbp
, inonum
); /* inode to fs blk */
168 offset
= fsbtodb(sbp
, offset
); /* fs blk disk blk */
169 offset
*= DEV_BSIZE
; /* disk blk to bytes */
171 /* seek and read the block */
172 if (lseek(fd
, offset
, SEEK_SET
) < 0)
174 if ((size_t)read(fd
, ibuf
, bsize
) != bsize
)
177 /* get the inode within the block. */
179 ip2
= &((struct ufs2_dinode
*)ibuf
)
180 [ino_to_fsbo(sbp
, inonum
)];
181 /* clear the inode, and bump the generation count. */
182 generation
= ip2
->di_gen
+ 1;
183 memset(ip2
, 0, sizeof(*ip2
));
184 ip2
->di_gen
= generation
;
186 ip1
= &((struct ufs1_dinode
*)ibuf
)
187 [ino_to_fsbo(sbp
, inonum
)];
188 /* clear the inode, and bump the generation count. */
189 generation
= ip1
->di_gen
+ 1;
190 memset(ip1
, 0, sizeof(*ip1
));
191 ip1
->di_gen
= generation
;
194 /* backup and write the block */
195 if (lseek(fd
, offset
, SEEK_SET
) < 0)
197 if ((size_t)write(fd
, ibuf
, bsize
) != bsize
)