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 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/disklabel.h> 49 50 #include <ufs/ufs/dinode.h> 51 #include <ufs/ffs/fs.h> 52 53 #include <err.h> 54 #include <fcntl.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <stdio.h> 58 #include <unistd.h> 59 60 /* 61 * Possible superblock locations ordered from most to least likely. 62 */ 63 static int sblock_try[] = SBLOCKSEARCH; 64 65 static void 66 usage(void) 67 { 68 (void)fprintf(stderr, "usage: clri special_device inode_number ...\n"); 69 exit(1); 70 } 71 72 int 73 main(int argc, char *argv[]) 74 { 75 struct fs *sbp; 76 struct ufs1_dinode *dp1; 77 struct ufs2_dinode *dp2; 78 char *ibuf[MAXBSIZE]; 79 long generation, bsize; 80 off_t offset; 81 int i, fd, inonum; 82 char *fs, sblock[SBLOCKSIZE]; 83 84 if (argc < 3) 85 usage(); 86 87 fs = *++argv; 88 89 /* get the superblock. */ 90 if ((fd = open(fs, O_RDWR, 0)) < 0) 91 err(1, "%s", fs); 92 for (i = 0; sblock_try[i] != -1; i++) { 93 if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0) 94 err(1, "%s", fs); 95 if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) 96 errx(1, "%s: can't read superblock", fs); 97 sbp = (struct fs *)sblock; 98 if ((sbp->fs_magic == FS_UFS1_MAGIC || 99 (sbp->fs_magic == FS_UFS2_MAGIC && 100 sbp->fs_sblockloc == sblock_try[i])) && 101 sbp->fs_bsize <= MAXBSIZE && 102 sbp->fs_bsize >= (int)sizeof(struct fs)) 103 break; 104 } 105 if (sblock_try[i] == -1) { 106 fprintf(stderr, "Cannot find file system superblock\n"); 107 exit(2); 108 } 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