Home | History | Annotate | Line # | Download | only in clri
clri.c revision 1.13
      1 /*	$NetBSD: clri.c,v 1.13 1998/08/25 19:18:13 ross 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 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) 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.3 (Berkeley) 4/28/95";
     48 #else
     49 __RCSID("$NetBSD: clri.c,v 1.13 1998/08/25 19:18:13 ross 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/ufs/ufs_bswap.h>
     58 #include <ufs/ffs/fs.h>
     59 #include <ufs/ffs/ffs_extern.h>
     60 
     61 #include <err.h>
     62 #include <errno.h>
     63 #include <fcntl.h>
     64 #include <stdlib.h>
     65 #include <string.h>
     66 #include <stdio.h>
     67 #include <unistd.h>
     68 
     69 int	main __P((int, char *[]));
     70 
     71 int
     72 main(argc, argv)
     73 	int argc;
     74 	char *argv[];
     75 {
     76 	struct fs *sbp;
     77 	struct dinode *ip;
     78 	int fd;
     79 	struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
     80 	int32_t generation;
     81 	off_t offset;
     82 	size_t bsize;
     83 	int inonum;
     84 	char *fs, sblock[SBSIZE];
     85 	int needswap = 0;
     86 	int i, imax;
     87 
     88 	if (argc < 3) {
     89 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
     90 		exit(1);
     91 	}
     92 
     93 	fs = *++argv;
     94 
     95 
     96 	/* get the superblock. */
     97 	if ((fd = open(fs, O_RDWR, 0)) < 0)
     98 		err(1, "%s", fs);
     99 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
    100 		err(1, "%s", fs);
    101 	if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
    102 		errx(1, "%s: can't read superblock", fs);
    103 
    104 	sbp = (struct fs *)sblock;
    105 	if (sbp->fs_magic != FS_MAGIC) {
    106 		if (sbp->fs_magic == bswap32(FS_MAGIC))
    107 			needswap = 1;
    108 		else
    109 			errx(1, "%s: superblock magic number 0x%x, not 0x%x",
    110 		    	fs, sbp->fs_magic, FS_MAGIC);
    111 	}
    112 
    113 	/* check that inode numbers are valid */
    114 	imax = ufs_rw32(sbp->fs_ncg, needswap) *
    115 		ufs_rw32(sbp->fs_ipg, needswap);
    116 	for (i = 1; i < (argc - 1); i++)
    117 		if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax)
    118 			errx(1, "%s is not a valid inode number", argv[i]);
    119 
    120 	/* delete clean flag in the superblok */
    121 	sbp->fs_clean = ufs_rw32(
    122 						ufs_rw32(sbp->fs_clean, needswap) << 1,
    123 						needswap);
    124 	if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
    125 		err(1, "%s", fs);
    126 	if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock))
    127 		errx(1, "%s: can't rewrite superblock", fs);
    128 	(void)fsync(fd);
    129 
    130 	if (needswap)
    131 		ffs_sb_swap(sbp, sbp, 0);
    132 	bsize = sbp->fs_bsize;
    133 
    134 	/* remaining arguments are inode numbers. */
    135 	while (*++argv) {
    136 		/* get the inode number. */
    137 		inonum = atoi(*argv);
    138 		(void)printf("clearing %d\n", inonum);
    139 
    140 		/* read in the appropriate block. */
    141 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
    142 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
    143 		offset *= DEV_BSIZE;			/* disk blk to bytes */
    144 
    145 		/* seek and read the block */
    146 		if (lseek(fd, offset, SEEK_SET) < 0)
    147 			err(1, "%s", fs);
    148 		if (read(fd, ibuf, bsize) != bsize)
    149 			err(1, "%s", fs);
    150 
    151 		/* get the inode within the block. */
    152 		ip = &ibuf[ino_to_fsbo(sbp, inonum)];
    153 
    154 		/* clear the inode, and bump the generation count. */
    155 		generation = ip->di_gen + 1;
    156 		memset(ip, 0, sizeof(*ip));
    157 		ip->di_gen = generation;
    158 
    159 		/* backup and write the block */
    160 		if (lseek(fd, offset, SEEK_SET) < 0)
    161 			err(1, "%s", fs);
    162 		if (write(fd, ibuf, bsize) != bsize)
    163 			err(1, "%s", fs);
    164 		(void)fsync(fd);
    165 	}
    166 	(void)close(fd);
    167 	exit(0);
    168 }
    169