18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1990, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 68fae3551SRodney W. Grimes * Rich $alz of BBN Inc. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 98fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 108fae3551SRodney W. Grimes * are met: 118fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 128fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 138fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 158fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 168fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 178fae3551SRodney W. Grimes * must display the following acknowledgement: 188fae3551SRodney W. Grimes * This product includes software developed by the University of 198fae3551SRodney W. Grimes * California, Berkeley and its contributors. 208fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 218fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 228fae3551SRodney W. Grimes * without specific prior written permission. 238fae3551SRodney W. Grimes * 248fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 258fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 288fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 298fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 308fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 318fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 328fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 338fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 348fae3551SRodney W. Grimes * SUCH DAMAGE. 358fae3551SRodney W. Grimes */ 368fae3551SRodney W. Grimes 378fae3551SRodney W. Grimes #ifndef lint 388fae3551SRodney W. Grimes static char copyright[] = 398fae3551SRodney W. Grimes "@(#) Copyright (c) 1990, 1993\n\ 408fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 418fae3551SRodney W. Grimes #endif /* not lint */ 428fae3551SRodney W. Grimes 438fae3551SRodney W. Grimes #ifndef lint 448fae3551SRodney W. Grimes static char sccsid[] = "@(#)clri.c 8.2 (Berkeley) 9/23/93"; 458fae3551SRodney W. Grimes #endif /* not lint */ 468fae3551SRodney W. Grimes 478fae3551SRodney W. Grimes #include <sys/param.h> 488fae3551SRodney W. Grimes #include <sys/time.h> 498fae3551SRodney W. Grimes 508fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h> 518fae3551SRodney W. Grimes #include <ufs/ffs/fs.h> 528fae3551SRodney W. Grimes 538fae3551SRodney W. Grimes #include <err.h> 548fae3551SRodney W. Grimes #include <errno.h> 558fae3551SRodney W. Grimes #include <fcntl.h> 568fae3551SRodney W. Grimes #include <stdlib.h> 578fae3551SRodney W. Grimes #include <string.h> 588fae3551SRodney W. Grimes #include <stdio.h> 598fae3551SRodney W. Grimes #include <unistd.h> 608fae3551SRodney W. Grimes 618fae3551SRodney W. Grimes int 628fae3551SRodney W. Grimes main(argc, argv) 638fae3551SRodney W. Grimes int argc; 648fae3551SRodney W. Grimes char *argv[]; 658fae3551SRodney W. Grimes { 668fae3551SRodney W. Grimes register struct fs *sbp; 678fae3551SRodney W. Grimes register struct dinode *ip; 688fae3551SRodney W. Grimes register int fd; 698fae3551SRodney W. Grimes struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)]; 708fae3551SRodney W. Grimes long generation, bsize; 718fae3551SRodney W. Grimes off_t offset; 728fae3551SRodney W. Grimes int inonum; 738fae3551SRodney W. Grimes char *fs, sblock[SBSIZE]; 748fae3551SRodney W. Grimes 758fae3551SRodney W. Grimes if (argc < 3) { 768fae3551SRodney W. Grimes (void)fprintf(stderr, "usage: clri filesystem inode ...\n"); 778fae3551SRodney W. Grimes exit(1); 788fae3551SRodney W. Grimes } 798fae3551SRodney W. Grimes 808fae3551SRodney W. Grimes fs = *++argv; 818fae3551SRodney W. Grimes 828fae3551SRodney W. Grimes /* get the superblock. */ 838fae3551SRodney W. Grimes if ((fd = open(fs, O_RDWR, 0)) < 0) 848fae3551SRodney W. Grimes err(1, "%s", fs); 858fae3551SRodney W. Grimes if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0) 868fae3551SRodney W. Grimes err(1, "%s", fs); 878fae3551SRodney W. Grimes if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) { 888fae3551SRodney W. Grimes (void)fprintf(stderr, 898fae3551SRodney W. Grimes "clri: %s: can't read the superblock.\n", fs); 908fae3551SRodney W. Grimes exit(1); 918fae3551SRodney W. Grimes } 928fae3551SRodney W. Grimes 938fae3551SRodney W. Grimes sbp = (struct fs *)sblock; 948fae3551SRodney W. Grimes if (sbp->fs_magic != FS_MAGIC) { 958fae3551SRodney W. Grimes (void)fprintf(stderr, 968fae3551SRodney W. Grimes "clri: %s: superblock magic number 0x%x, not 0x%x.\n", 978fae3551SRodney W. Grimes fs, sbp->fs_magic, FS_MAGIC); 988fae3551SRodney W. Grimes exit(1); 998fae3551SRodney W. Grimes } 1008fae3551SRodney W. Grimes bsize = sbp->fs_bsize; 1018fae3551SRodney W. Grimes 1028fae3551SRodney W. Grimes /* remaining arguments are inode numbers. */ 1038fae3551SRodney W. Grimes while (*++argv) { 1048fae3551SRodney W. Grimes /* get the inode number. */ 1058fae3551SRodney W. Grimes if ((inonum = atoi(*argv)) <= 0) { 1068fae3551SRodney W. Grimes (void)fprintf(stderr, 1078fae3551SRodney W. Grimes "clri: %s is not a valid inode number.\n", *argv); 1088fae3551SRodney W. Grimes exit(1); 1098fae3551SRodney W. Grimes } 1108fae3551SRodney W. Grimes (void)printf("clearing %d\n", inonum); 1118fae3551SRodney W. Grimes 1128fae3551SRodney W. Grimes /* read in the appropriate block. */ 1138fae3551SRodney W. Grimes offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */ 1148fae3551SRodney W. Grimes offset = fsbtodb(sbp, offset); /* fs blk disk blk */ 1158fae3551SRodney W. Grimes offset *= DEV_BSIZE; /* disk blk to bytes */ 1168fae3551SRodney W. Grimes 1178fae3551SRodney W. Grimes /* seek and read the block */ 1188fae3551SRodney W. Grimes if (lseek(fd, offset, SEEK_SET) < 0) 1198fae3551SRodney W. Grimes err(1, "%s", fs); 1208fae3551SRodney W. Grimes if (read(fd, ibuf, bsize) != bsize) 1218fae3551SRodney W. Grimes err(1, "%s", fs); 1228fae3551SRodney W. Grimes 1238fae3551SRodney W. Grimes /* get the inode within the block. */ 1248fae3551SRodney W. Grimes ip = &ibuf[ino_to_fsbo(sbp, inonum)]; 1258fae3551SRodney W. Grimes 1268fae3551SRodney W. Grimes /* clear the inode, and bump the generation count. */ 1278fae3551SRodney W. Grimes generation = ip->di_gen + 1; 1288fae3551SRodney W. Grimes memset(ip, 0, sizeof(*ip)); 1298fae3551SRodney W. Grimes ip->di_gen = generation; 1308fae3551SRodney W. Grimes 1318fae3551SRodney W. Grimes /* backup and write the block */ 1328fae3551SRodney W. Grimes if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0) 1338fae3551SRodney W. Grimes err(1, "%s", fs); 1348fae3551SRodney W. Grimes if (write(fd, ibuf, bsize) != bsize) 1358fae3551SRodney W. Grimes err(1, "%s", fs); 1368fae3551SRodney W. Grimes (void)fsync(fd); 1378fae3551SRodney W. Grimes } 1388fae3551SRodney W. Grimes (void)close(fd); 1398fae3551SRodney W. Grimes exit(0); 1408fae3551SRodney W. Grimes } 141