1 /* $OpenBSD: clri.c,v 1.11 2007/05/21 18:13:11 millert Exp $ */ 2 /* $NetBSD: clri.c,v 1.19 2005/01/20 15:50:47 xtraeme Exp $ */ 3 4 /* 5 * Copyright (c) 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Rich $alz of BBN Inc. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #if 0 37 #ifndef lint 38 static const char copyright[] = 39 "@(#) Copyright (c) 1990, 1993\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41 #endif /* not lint */ 42 43 #ifndef lint 44 static char sccsid[] = "@(#)clri.c 8.2 (Berkeley) 9/23/93"; 45 #endif /* not lint */ 46 #endif 47 48 #include <sys/param.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 ufs1_dinode *dp1; 76 struct ufs2_dinode *dp2; 77 struct fs *sbp; 78 char *ibuf[MAXBSIZE]; 79 char *fs, sblock[SBLOCKSIZE]; 80 size_t bsize; 81 off_t offset; 82 int i, fd, imax, inonum; 83 84 if (argc < 3) 85 usage(); 86 87 fs = *++argv; 88 sbp = NULL; 89 90 /* get the superblock. */ 91 if ((fd = open(fs, O_RDWR, 0)) < 0) 92 err(1, "%s", fs); 93 for (i = 0; sblock_try[i] != -1; i++) { 94 offset = (off_t)(sblock_try[i]); 95 if (pread(fd, sblock, sizeof(sblock), offset) != sizeof(sblock)) 96 err(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 errx(2, "cannot find file system superblock"); 107 bsize = sbp->fs_bsize; 108 109 /* check that inode numbers are valid */ 110 imax = sbp->fs_ncg * sbp->fs_ipg; 111 for (i = 1; i < (argc - 1); i++) { 112 if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax) 113 errx(1, "%s is not a valid inode number", argv[i]); 114 } 115 116 /* clear the clean flag in the superblock */ 117 if (sbp->fs_inodefmt >= FS_44INODEFMT) { 118 sbp->fs_clean = 0; 119 if (pwrite(fd, sblock, sizeof(sblock), offset) != sizeof(sblock)) 120 err(1, "%s: can't update superblock", fs); 121 (void)fsync(fd); 122 } 123 124 /* remaining arguments are inode numbers. */ 125 while (*++argv) { 126 /* get the inode number. */ 127 inonum = atoi(*argv); 128 (void)printf("clearing %d\n", inonum); 129 130 /* read in the appropriate block. */ 131 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */ 132 offset = fsbtodb(sbp, offset); /* fs blk disk blk */ 133 offset *= DEV_BSIZE; /* disk blk to bytes */ 134 135 /* seek and read the block */ 136 if (pread(fd, ibuf, bsize, offset) != bsize) 137 err(1, "%s", fs); 138 139 if (sbp->fs_magic == FS_UFS2_MAGIC) { 140 /* get the inode within the block. */ 141 dp2 = &(((struct ufs2_dinode *)ibuf) 142 [ino_to_fsbo(sbp, inonum)]); 143 144 /* clear the inode, and bump the generation count. */ 145 memset(dp2, 0, sizeof(*dp2)); 146 dp2->di_gen = arc4random(); 147 } else { 148 /* get the inode within the block. */ 149 dp1 = &(((struct ufs1_dinode *)ibuf) 150 [ino_to_fsbo(sbp, inonum)]); 151 152 /* clear the inode, and bump the generation count. */ 153 memset(dp1, 0, sizeof(*dp1)); 154 dp1->di_gen = arc4random(); 155 } 156 157 /* backup and write the block */ 158 if (pwrite(fd, ibuf, bsize, offset) != bsize) 159 err(1, "%s", fs); 160 (void)fsync(fd); 161 } 162 (void)close(fd); 163 exit(0); 164 } 165