1 /* 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rich $alz of BBN Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 19 * 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 30 * SUCH DAMAGE. 31 */ 32 33 #if 0 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1990, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)clri.c 8.2 (Berkeley) 9/23/93"; 42 #endif /* not lint */ 43 #endif 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/disklabel.h> 50 51 #include <ufs/ufs/dinode.h> 52 #include <ufs/ffs/fs.h> 53 54 #include <err.h> 55 #include <fcntl.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <stdio.h> 59 #include <unistd.h> 60 61 /* 62 * Possible superblock locations ordered from most to least likely. 63 */ 64 static int sblock_try[] = SBLOCKSEARCH; 65 66 static void 67 usage(void) 68 { 69 (void)fprintf(stderr, "usage: clri special_device inode_number ...\n"); 70 exit(1); 71 } 72 73 int 74 main(int argc, char *argv[]) 75 { 76 struct fs *sbp; 77 struct ufs1_dinode *dp1; 78 struct ufs2_dinode *dp2; 79 char *ibuf[MAXBSIZE]; 80 long generation, bsize; 81 off_t offset; 82 int i, fd, inonum; 83 char *fs, sblock[SBLOCKSIZE]; 84 85 if (argc < 3) 86 usage(); 87 88 fs = *++argv; 89 sbp = NULL; 90 91 /* get the superblock. */ 92 if ((fd = open(fs, O_RDWR, 0)) < 0) 93 err(1, "%s", fs); 94 for (i = 0; sblock_try[i] != -1; i++) { 95 if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0) 96 err(1, "%s", fs); 97 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) 98 errx(1, "%s: can't read superblock", fs); 99 sbp = (struct fs *)sblock; 100 if ((sbp->fs_magic == FS_UFS1_MAGIC || 101 (sbp->fs_magic == FS_UFS2_MAGIC && 102 sbp->fs_sblockloc == sblock_try[i])) && 103 sbp->fs_bsize <= MAXBSIZE && 104 sbp->fs_bsize >= (int)sizeof(struct fs)) 105 break; 106 } 107 if (sblock_try[i] == -1) 108 errx(2, "cannot find file system superblock"); 109 bsize = sbp->fs_bsize; 110 111 /* remaining arguments are inode numbers. */ 112 while (*++argv) { 113 /* get the inode number. */ 114 if ((inonum = atoi(*argv)) <= 0) 115 errx(1, "%s is not a valid inode number", *argv); 116 (void)printf("clearing %d\n", inonum); 117 118 /* read in the appropriate block. */ 119 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */ 120 offset = fsbtodb(sbp, offset); /* fs blk disk blk */ 121 offset *= DEV_BSIZE; /* disk blk to bytes */ 122 123 /* seek and read the block */ 124 if (lseek(fd, offset, SEEK_SET) < 0) 125 err(1, "%s", fs); 126 if (read(fd, ibuf, bsize) != bsize) 127 err(1, "%s", fs); 128 129 if (sbp->fs_magic == FS_UFS2_MAGIC) { 130 /* get the inode within the block. */ 131 dp2 = &(((struct ufs2_dinode *)ibuf) 132 [ino_to_fsbo(sbp, inonum)]); 133 134 /* clear the inode, and bump the generation count. */ 135 generation = dp2->di_gen + 1; 136 memset(dp2, 0, sizeof(*dp2)); 137 dp2->di_gen = generation; 138 } else { 139 /* get the inode within the block. */ 140 dp1 = &(((struct ufs1_dinode *)ibuf) 141 [ino_to_fsbo(sbp, inonum)]); 142 143 /* clear the inode, and bump the generation count. */ 144 generation = dp1->di_gen + 1; 145 memset(dp1, 0, sizeof(*dp1)); 146 dp1->di_gen = generation; 147 } 148 149 /* backup and write the block */ 150 if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0) 151 err(1, "%s", fs); 152 if (write(fd, ibuf, bsize) != bsize) 153 err(1, "%s", fs); 154 (void)fsync(fd); 155 } 156 (void)close(fd); 157 exit(0); 158 } 159