1 /* $OpenBSD: clri.c,v 1.12 2009/10/27 23:59:32 deraadt 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 #include <sys/param.h> 37 38 #include <ufs/ufs/dinode.h> 39 #include <ufs/ffs/fs.h> 40 41 #include <err.h> 42 #include <fcntl.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <stdio.h> 46 #include <unistd.h> 47 48 /* 49 * Possible superblock locations ordered from most to least likely. 50 */ 51 static int sblock_try[] = SBLOCKSEARCH; 52 53 static void 54 usage(void) 55 { 56 (void)fprintf(stderr, "usage: clri special_device inode_number ...\n"); 57 exit(1); 58 } 59 60 int 61 main(int argc, char *argv[]) 62 { 63 struct ufs1_dinode *dp1; 64 struct ufs2_dinode *dp2; 65 struct fs *sbp; 66 char *ibuf[MAXBSIZE]; 67 char *fs, sblock[SBLOCKSIZE]; 68 size_t bsize; 69 off_t offset; 70 int i, fd, imax, inonum; 71 72 if (argc < 3) 73 usage(); 74 75 fs = *++argv; 76 sbp = NULL; 77 78 /* get the superblock. */ 79 if ((fd = open(fs, O_RDWR, 0)) < 0) 80 err(1, "%s", fs); 81 for (i = 0; sblock_try[i] != -1; i++) { 82 offset = (off_t)(sblock_try[i]); 83 if (pread(fd, sblock, sizeof(sblock), offset) != sizeof(sblock)) 84 err(1, "%s: can't read superblock", fs); 85 sbp = (struct fs *)sblock; 86 if ((sbp->fs_magic == FS_UFS1_MAGIC || 87 (sbp->fs_magic == FS_UFS2_MAGIC && 88 sbp->fs_sblockloc == sblock_try[i])) && 89 sbp->fs_bsize <= MAXBSIZE && 90 sbp->fs_bsize >= (int)sizeof(struct fs)) 91 break; 92 } 93 if (sblock_try[i] == -1) 94 errx(2, "cannot find file system superblock"); 95 bsize = sbp->fs_bsize; 96 97 /* check that inode numbers are valid */ 98 imax = sbp->fs_ncg * sbp->fs_ipg; 99 for (i = 1; i < (argc - 1); i++) { 100 if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax) 101 errx(1, "%s is not a valid inode number", argv[i]); 102 } 103 104 /* clear the clean flag in the superblock */ 105 if (sbp->fs_inodefmt >= FS_44INODEFMT) { 106 sbp->fs_clean = 0; 107 if (pwrite(fd, sblock, sizeof(sblock), offset) != sizeof(sblock)) 108 err(1, "%s: can't update superblock", fs); 109 (void)fsync(fd); 110 } 111 112 /* remaining arguments are inode numbers. */ 113 while (*++argv) { 114 /* get the inode number. */ 115 inonum = atoi(*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 (pread(fd, ibuf, bsize, offset) != bsize) 125 err(1, "%s", fs); 126 127 if (sbp->fs_magic == FS_UFS2_MAGIC) { 128 /* get the inode within the block. */ 129 dp2 = &(((struct ufs2_dinode *)ibuf) 130 [ino_to_fsbo(sbp, inonum)]); 131 132 /* clear the inode, and bump the generation count. */ 133 memset(dp2, 0, sizeof(*dp2)); 134 dp2->di_gen = arc4random(); 135 } else { 136 /* get the inode within the block. */ 137 dp1 = &(((struct ufs1_dinode *)ibuf) 138 [ino_to_fsbo(sbp, inonum)]); 139 140 /* clear the inode, and bump the generation count. */ 141 memset(dp1, 0, sizeof(*dp1)); 142 dp1->di_gen = arc4random(); 143 } 144 145 /* backup and write the block */ 146 if (pwrite(fd, ibuf, bsize, offset) != bsize) 147 err(1, "%s", fs); 148 (void)fsync(fd); 149 } 150 (void)close(fd); 151 exit(0); 152 } 153