1 /* $OpenBSD: clri.c,v 1.5 2001/06/04 14:59:47 mickey Exp $ */ 2 /* $NetBSD: clri.c,v 1.9 1995/03/18 14:54:33 cgd 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. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #ifndef lint 41 static char copyright[] = 42 "@(#) Copyright (c) 1990, 1993\n\ 43 The Regents of the University of California. All rights reserved.\n"; 44 #endif /* not lint */ 45 46 #ifndef lint 47 #if 0 48 static char sccsid[] = "@(#)clri.c 8.2 (Berkeley) 9/23/93"; 49 #else 50 static char rcsid[] = "$OpenBSD: clri.c,v 1.5 2001/06/04 14:59:47 mickey Exp $"; 51 #endif 52 #endif /* not lint */ 53 54 #include <sys/param.h> 55 #include <sys/time.h> 56 57 #include <ufs/ufs/dinode.h> 58 #include <ufs/ffs/fs.h> 59 60 #include <err.h> 61 #include <errno.h> 62 #include <fcntl.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <stdio.h> 66 #include <unistd.h> 67 68 int 69 main(argc, argv) 70 int argc; 71 char *argv[]; 72 { 73 register struct fs *sbp; 74 register struct dinode *ip; 75 register int fd; 76 struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)]; 77 int32_t generation; 78 off_t offset; 79 size_t bsize; 80 int inonum; 81 char *fs, sblock[SBSIZE]; 82 83 if (argc < 3) { 84 fprintf(stderr, "usage: clri filesystem inode ...\n"); 85 return (1); 86 } 87 88 fs = *++argv; 89 90 /* get the superblock. */ 91 if ((fd = open(fs, O_RDWR, 0)) < 0) 92 err(1, "%s", fs); 93 if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), 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 98 sbp = (struct fs *)sblock; 99 if (sbp->fs_magic != FS_MAGIC) 100 errx(1, "%s: superblock magic number 0x%x, not 0x%x", 101 fs, sbp->fs_magic, FS_MAGIC); 102 bsize = sbp->fs_bsize; 103 104 /* remaining arguments are inode numbers. */ 105 while (*++argv) { 106 /* get the inode number. */ 107 if ((inonum = atoi(*argv)) <= 0) 108 errx(1, "%s is not a valid inode number", *argv); 109 (void)printf("clearing %d\n", inonum); 110 111 /* read in the appropriate block. */ 112 offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */ 113 offset = fsbtodb(sbp, offset); /* fs blk disk blk */ 114 offset *= DEV_BSIZE; /* disk blk to bytes */ 115 116 /* seek and read the block */ 117 if (lseek(fd, offset, SEEK_SET) < 0) 118 err(1, "%s", fs); 119 if (read(fd, ibuf, bsize) != bsize) 120 err(1, "%s", fs); 121 122 /* get the inode within the block. */ 123 ip = &ibuf[ino_to_fsbo(sbp, inonum)]; 124 125 /* clear the inode, and bump the generation count. */ 126 generation = ip->di_gen + 1; 127 memset(ip, 0, sizeof(*ip)); 128 ip->di_gen = generation; 129 130 /* backup and write the block */ 131 if (lseek(fd, offset, SEEK_SET) < 0) 132 err(1, "%s", fs); 133 if (write(fd, ibuf, bsize) != bsize) 134 err(1, "%s", fs); 135 (void)fsync(fd); 136 137 if (sbp->fs_inodefmt >= FS_44INODEFMT) { 138 /* update after each inode cleared */ 139 sbp->fs_clean = 0; 140 if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0) 141 err(1, "%s", fs); 142 if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock)) 143 errx(1, "%s: can't update superblock", fs); 144 } 145 } 146 147 return close(fd); 148 } 149