Home | History | Annotate | Line # | Download | only in clri
clri.c revision 1.17
      1 /*	$NetBSD: clri.c,v 1.17 2004/03/21 19:35:23 dsl 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
     38 	The Regents of the University of California.  All rights reserved.\n");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)clri.c	8.3 (Berkeley) 4/28/95";
     44 #else
     45 __RCSID("$NetBSD: clri.c,v 1.17 2004/03/21 19:35:23 dsl Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #include <sys/time.h>
     51 
     52 #include <ufs/ufs/dinode.h>
     53 #include <ufs/ufs/ufs_bswap.h>
     54 #include <ufs/ffs/fs.h>
     55 #include <ufs/ffs/ffs_extern.h>
     56 
     57 #include <err.h>
     58 #include <errno.h>
     59 #include <fcntl.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <stdio.h>
     63 #include <unistd.h>
     64 
     65 /*
     66  * Possible superblock locations ordered from most to least likely.
     67  */
     68 static off_t sblock_try[] = SBLOCKSEARCH;
     69 off_t sblockloc;
     70 
     71 
     72 int	main __P((int, char *[]));
     73 
     74 int
     75 main(argc, argv)
     76 	int argc;
     77 	char *argv[];
     78 {
     79 	struct fs *sbp;
     80 	struct ufs1_dinode *ip1;
     81 	struct ufs2_dinode *ip2;
     82 	int fd;
     83 	char *ibuf[MAXBSIZE];
     84 	int32_t generation;
     85 	off_t offset;
     86 	size_t bsize;
     87 	int inonum;
     88 	char *fs, sblock[SBLOCKSIZE];
     89 	int needswap = 0, is_ufs2 = 0;
     90 	int i, imax;
     91 
     92 	if (argc < 3) {
     93 		(void)fprintf(stderr, "usage: clri filesystem inode ...\n");
     94 		exit(1);
     95 	}
     96 
     97 	fs = *++argv;
     98 
     99 
    100 	/* get the superblock. */
    101 	if ((fd = open(fs, O_RDWR, 0)) < 0)
    102 		err(1, "%s", fs);
    103 	for (i = 0;; i++) {
    104 		sblockloc = sblock_try[i];
    105 		if (sblockloc == -1)
    106 			errx(1, "%s: can't find superblock", fs);
    107 		if (pread(fd, sblock, sizeof(sblock), sblockloc) != sizeof(sblock))
    108 			errx(1, "%s: can't read superblock", fs);
    109 
    110 		sbp = (struct fs *)sblock;
    111 		switch(sbp->fs_magic) {
    112 		case FS_UFS2_MAGIC:
    113 			is_ufs2 = 1;
    114 			/*FALLTHROUGH*/
    115 		case FS_UFS1_MAGIC:
    116 			break;
    117 		case FS_UFS2_MAGIC_SWAPPED:
    118 			is_ufs2 = 1;
    119 			/*FALLTHROUGH*/
    120 		case FS_UFS1_MAGIC_SWAPPED:
    121 			needswap = 1;
    122 			break;
    123 		default:
    124 			continue;
    125 		}
    126 
    127 		/* check we haven't found an alternate */
    128 		if (sbp->fs_old_flags & FS_FLAGS_UPDATED) {
    129 			if (sblockloc != ufs_rw64(sbp->fs_sblockloc, needswap))
    130 				continue;
    131 		} else {
    132 			if (sblockloc == SBLOCK_UFS2)
    133 				continue;
    134 		}
    135 
    136 		break;
    137 	}
    138 
    139 	/* check that inode numbers are valid */
    140 	imax = ufs_rw32(sbp->fs_ncg, needswap) *
    141 		ufs_rw32(sbp->fs_ipg, needswap);
    142 	for (i = 1; i < (argc - 1); i++)
    143 		if (atoi(argv[i]) <= 0 || atoi(argv[i]) >= imax)
    144 			errx(1, "%s is not a valid inode number", argv[i]);
    145 
    146 	/* delete clean flag in the superblok */
    147 	sbp->fs_clean = ufs_rw32(ufs_rw32(sbp->fs_clean, needswap) << 1,
    148 				needswap);
    149 	if (lseek(fd, sblockloc, SEEK_SET) < 0)
    150 		err(1, "%s", fs);
    151 	if (write(fd, sblock, sizeof(sblock)) != sizeof(sblock))
    152 		errx(1, "%s: can't rewrite superblock", fs);
    153 	(void)fsync(fd);
    154 
    155 	if (needswap)
    156 		ffs_sb_swap(sbp, sbp);
    157 	bsize = sbp->fs_bsize;
    158 
    159 	/* remaining arguments are inode numbers. */
    160 	while (*++argv) {
    161 		/* get the inode number. */
    162 		inonum = atoi(*argv);
    163 		(void)printf("clearing %d\n", inonum);
    164 
    165 		/* read in the appropriate block. */
    166 		offset = ino_to_fsba(sbp, inonum);	/* inode to fs blk */
    167 		offset = fsbtodb(sbp, offset);		/* fs blk disk blk */
    168 		offset *= DEV_BSIZE;			/* disk blk to bytes */
    169 
    170 		/* seek and read the block */
    171 		if (lseek(fd, offset, SEEK_SET) < 0)
    172 			err(1, "%s", fs);
    173 		if (read(fd, ibuf, bsize) != bsize)
    174 			err(1, "%s", fs);
    175 
    176 		/* get the inode within the block. */
    177 		if (is_ufs2) {
    178 			ip2 = &((struct ufs2_dinode *)ibuf)
    179 			    [ino_to_fsbo(sbp, inonum)];
    180 			/* clear the inode, and bump the generation count. */
    181 			generation = ip2->di_gen + 1;
    182 			memset(ip2, 0, sizeof(*ip2));
    183 			ip2->di_gen = generation;
    184 		} else {
    185 			ip1 = &((struct ufs1_dinode *)ibuf)
    186 			    [ino_to_fsbo(sbp, inonum)];
    187 			/* clear the inode, and bump the generation count. */
    188 			generation = ip1->di_gen + 1;
    189 			memset(ip1, 0, sizeof(*ip1));
    190 			ip1->di_gen = generation;
    191 		}
    192 
    193 		/* backup and write the block */
    194 		if (lseek(fd, offset, SEEK_SET) < 0)
    195 			err(1, "%s", fs);
    196 		if (write(fd, ibuf, bsize) != bsize)
    197 			err(1, "%s", fs);
    198 		(void)fsync(fd);
    199 	}
    200 	(void)close(fd);
    201 	exit(0);
    202 }
    203