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