1 /* $NetBSD: clri.c,v 1.15 2003/04/02 10:39:23 fvdl Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rich $alz of BBN Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\ 42 The Regents of the University of California. All rights reserved.\n"); 43 #endif /* not lint */ 44 45 #ifndef lint 46 #if 0 47 static char sccsid[] = "@(#)clri.c 8.3 (Berkeley) 4/28/95"; 48 #else 49 __RCSID("$NetBSD: clri.c,v 1.15 2003/04/02 10:39:23 fvdl Exp $"); 50 #endif 51 #endif /* not lint */ 52 53 #include <sys/param.h> 54 #include <sys/time.h> 55 56 #include <ufs/ufs/dinode.h> 57 #include <ufs/ufs/ufs_bswap.h> 58 #include <ufs/ffs/fs.h> 59 #include <ufs/ffs/ffs_extern.h> 60 61 #include <err.h> 62 #include <errno.h> 63 #include <fcntl.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <stdio.h> 67 #include <unistd.h> 68 69 /* 70 * Possible superblock locations ordered from most to least likely. 71 */ 72 static off_t sblock_try[] = SBLOCKSEARCH; 73 off_t sblockloc; 74 75 76 int main __P((int, char *[])); 77 78 int 79 main(argc, argv) 80 int argc; 81 char *argv[]; 82 { 83 struct fs *sbp; 84 struct ufs1_dinode *ip1; 85 struct ufs2_dinode *ip2; 86 int fd; 87 char *ibuf[MAXBSIZE]; 88 int32_t generation; 89 off_t offset; 90 size_t bsize; 91 int inonum; 92 char *fs, sblock[SBLOCKSIZE]; 93 int needswap = 0, is_ufs2 = 0; 94 int i, imax; 95 96 if (argc < 3) { 97 (void)fprintf(stderr, "usage: clri filesystem inode ...\n"); 98 exit(1); 99 } 100 101 fs = *++argv; 102 103 104 /* get the superblock. */ 105 if ((fd = open(fs, O_RDWR, 0)) < 0) 106 err(1, "%s", fs); 107 for (i = 0; i < sblock_try[i] != -1; i++) { 108 if (lseek(fd, sblock_try[i], SEEK_SET) < 0) 109 err(1, "%s", fs); 110 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) 111 errx(1, "%s: can't read superblock", fs); 112 113 sbp = (struct fs *)sblock; 114 switch(sbp->fs_magic) { 115 case FS_UFS2_MAGIC: 116 is_ufs2 = 1; 117 /*FALLTHROUGH*/ 118 case FS_UFS1_MAGIC: 119 goto found; 120 case FS_UFS2_MAGIC_SWAPPED: 121 is_ufs2 = 1; 122 /*FALLTHROUGH*/ 123 case FS_UFS1_MAGIC_SWAPPED: 124 needswap = 1; 125 goto found; 126 default: 127 continue; 128 } 129 } 130 errx(1, "%s: can't find superblock", fs); 131 132 found: 133 sblockloc = sblock_try[i]; 134 /* check that inode numbers are valid */ 135 imax = ufs_rw32(sbp->fs_ncg, needswap) * 136 ufs_rw32(sbp->fs_ipg, needswap); 137 for (i = 1; i < (argc - 1); i++) 138 if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax) 139 errx(1, "%s is not a valid inode number", argv[i]); 140 141 /* delete clean flag in the superblok */ 142 sbp->fs_clean = ufs_rw32( 143 ufs_rw32(sbp->fs_clean, needswap) << 1, 144 needswap); 145 if (lseek(fd, sblockloc, SEEK_SET) < 0) 146 err(1, "%s", fs); 147 if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock)) 148 errx(1, "%s: can't rewrite superblock", fs); 149 (void)fsync(fd); 150 151 if (needswap) 152 ffs_sb_swap(sbp, sbp); 153 bsize = sbp->fs_bsize; 154 155 /* remaining arguments are inode numbers. */ 156 while (*++argv) { 157 /* get the inode number. */ 158 inonum = atoi(*argv); 159 (void)printf("clearing %d\n", inonum); 160 161 /* read in the appropriate block. */ 162 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */ 163 offset = fsbtodb(sbp, offset); /* fs blk disk blk */ 164 offset *= DEV_BSIZE; /* disk blk to bytes */ 165 166 /* seek and read the block */ 167 if (lseek(fd, offset, SEEK_SET) < 0) 168 err(1, "%s", fs); 169 if (read(fd, ibuf, bsize) != bsize) 170 err(1, "%s", fs); 171 172 /* get the inode within the block. */ 173 if (is_ufs2) { 174 ip2 = &((struct ufs2_dinode *)ibuf) 175 [ino_to_fsbo(sbp, inonum)]; 176 /* clear the inode, and bump the generation count. */ 177 generation = ip2->di_gen + 1; 178 memset(ip2, 0, sizeof(*ip2)); 179 ip2->di_gen = generation; 180 } else { 181 ip1 = &((struct ufs1_dinode *)ibuf) 182 [ino_to_fsbo(sbp, inonum)]; 183 /* clear the inode, and bump the generation count. */ 184 generation = ip1->di_gen + 1; 185 memset(ip1, 0, sizeof(*ip1)); 186 ip1->di_gen = generation; 187 } 188 189 /* backup and write the block */ 190 if (lseek(fd, offset, SEEK_SET) < 0) 191 err(1, "%s", fs); 192 if (write(fd, ibuf, bsize) != bsize) 193 err(1, "%s", fs); 194 (void)fsync(fd); 195 } 196 (void)close(fd); 197 exit(0); 198 } 199