xref: /onnv-gate/usr/src/cmd/fs.d/ufs/clri/clri.c (revision 757:f4be52b91377)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
2328Sprabahar  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * clri filsys inumber ...
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <fcntl.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <sys/param.h>
490Sstevel@tonic-gate #include <sys/types.h>
500Sstevel@tonic-gate #include <sys/mntent.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <sys/vnode.h>
530Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
540Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include "roll_log.h"
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	ISIZE	(sizeof (struct dinode))
590Sstevel@tonic-gate #define	NI	(MAXBSIZE/ISIZE)
600Sstevel@tonic-gate 
610Sstevel@tonic-gate static struct dinode buf[NI];
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static union {
640Sstevel@tonic-gate 	char		dummy[SBSIZE];
650Sstevel@tonic-gate 	struct fs	sblk;
660Sstevel@tonic-gate } sb_un;
670Sstevel@tonic-gate #define	sblock sb_un.sblk
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static int status;
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static int read_sb(int fd, const char *dev);
720Sstevel@tonic-gate static int isnumber(const char *s);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate int
main(int argc,char * argv[])750Sstevel@tonic-gate main(int argc, char *argv[])
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	int		i, f;
780Sstevel@tonic-gate 	unsigned int	n;
790Sstevel@tonic-gate 	int		j;
800Sstevel@tonic-gate 	offset_t	off;
810Sstevel@tonic-gate 	int32_t		gen;
820Sstevel@tonic-gate 	time_t		t;
830Sstevel@tonic-gate 	int		sbrr;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (argc < 3) {
860Sstevel@tonic-gate 		(void) printf("ufs usage: clri filsys inumber ...\n");
870Sstevel@tonic-gate 		return (35);
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 	f = open64(argv[1], 2);
900Sstevel@tonic-gate 	if (f < 0) {
910Sstevel@tonic-gate 		(void) printf("cannot open %s\n", argv[1]);
920Sstevel@tonic-gate 		return (35);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	if ((sbrr = read_sb(f, argv[1])) != 0) {
960Sstevel@tonic-gate 		return (sbrr);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
9928Sprabahar 	if ((sblock.fs_magic != FS_MAGIC) &&
10028Sprabahar 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
10128Sprabahar 		(void) printf("bad super block magic number\n");
10228Sprabahar 		return (35);
10328Sprabahar 	}
10428Sprabahar 
105*757Svsakar 	if (sblock.fs_magic == FS_MAGIC &&
106*757Svsakar 	    (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
107*757Svsakar 	    sblock.fs_version != UFS_VERSION_MIN)) {
108*757Svsakar 		(void) printf(
109*757Svsakar 		    "unrecognized version of UFS on-disk format: %d\n",
110*757Svsakar 		    sblock.fs_version);
111*757Svsakar 		return (35);
112*757Svsakar 	}
113*757Svsakar 
11428Sprabahar 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
11528Sprabahar 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
11628Sprabahar 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
11728Sprabahar 		(void) printf(
11828Sprabahar 		    "unrecognized version of UFS on-disk format: %d\n",
11928Sprabahar 		    sblock.fs_version);
12028Sprabahar 		return (35);
12128Sprabahar 	}
12228Sprabahar 
1230Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
1240Sstevel@tonic-gate 	if (sblock.fs_logbno) {
1250Sstevel@tonic-gate 		switch (rl_roll_log(argv[1])) {
1260Sstevel@tonic-gate 		case RL_SUCCESS:
1270Sstevel@tonic-gate 			/*
1280Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
1290Sstevel@tonic-gate 			 * changed it.
1300Sstevel@tonic-gate 			 */
1310Sstevel@tonic-gate 			if ((sbrr = read_sb(f, argv[1])) != 0) {
1320Sstevel@tonic-gate 				return (sbrr);
1330Sstevel@tonic-gate 			}
1340Sstevel@tonic-gate 			break;
1350Sstevel@tonic-gate 		case RL_SYSERR:
1360Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.  %s.  "
1370Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1380Sstevel@tonic-gate 				argv[1], strerror(errno));
1390Sstevel@tonic-gate 			break;
1400Sstevel@tonic-gate 		default:
1410Sstevel@tonic-gate 			(void) printf("Cannot roll log for %s.  "
1420Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1430Sstevel@tonic-gate 				argv[1]);
1440Sstevel@tonic-gate 			break;
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1490Sstevel@tonic-gate 		if (!isnumber(argv[i])) {
1500Sstevel@tonic-gate 			(void) printf("%s: is not a number\n", argv[i]);
1510Sstevel@tonic-gate 			status = 1;
1520Sstevel@tonic-gate 			continue;
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		n = atoi(argv[i]);
1550Sstevel@tonic-gate 		if (n == 0) {
1560Sstevel@tonic-gate 			(void) printf("%s: is zero\n", argv[i]);
1570Sstevel@tonic-gate 			status = 1;
1580Sstevel@tonic-gate 			continue;
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1610Sstevel@tonic-gate 		off *= DEV_BSIZE;
1620Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1630Sstevel@tonic-gate 		if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
1640Sstevel@tonic-gate 			(void) printf("%s: read error\n", argv[i]);
1650Sstevel@tonic-gate 			status = 1;
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 	if (status)
1690Sstevel@tonic-gate 		return (status+31);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/*
1720Sstevel@tonic-gate 	 * Update the time in superblock, so fsck will check this filesystem.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	(void) llseek(f, (offset_t)(SBLOCK * DEV_BSIZE), 0);
1750Sstevel@tonic-gate 	(void) time(&t);
1760Sstevel@tonic-gate 	sblock.fs_time = (time32_t)t;
1770Sstevel@tonic-gate 	if (write(f, &sblock, SBSIZE) != SBSIZE) {
1780Sstevel@tonic-gate 		(void) printf("cannot update %s\n", argv[1]);
1790Sstevel@tonic-gate 		return (35);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1830Sstevel@tonic-gate 		n = atoi(argv[i]);
1840Sstevel@tonic-gate 		(void) printf("clearing %u\n", n);
1850Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1860Sstevel@tonic-gate 		off *= DEV_BSIZE;
1870Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1880Sstevel@tonic-gate 		(void) read(f, (char *)buf, sblock.fs_bsize);
1890Sstevel@tonic-gate 		j = itoo(&sblock, n);
1900Sstevel@tonic-gate 		gen = buf[j].di_gen;
1910Sstevel@tonic-gate 		memset(&buf[j], 0, ISIZE);
1920Sstevel@tonic-gate 		buf[j].di_gen = gen + 1;
1930Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1940Sstevel@tonic-gate 		(void) write(f, (char *)buf, sblock.fs_bsize);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 	if (status)
1970Sstevel@tonic-gate 		return (status+31);
1980Sstevel@tonic-gate 	(void) close(f);
1990Sstevel@tonic-gate 	return (0);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate static int
isnumber(const char * s)2030Sstevel@tonic-gate isnumber(const char *s)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	int c;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	while ((c = *s++) != '\0')
2080Sstevel@tonic-gate 		if (c < '0' || c > '9')
2090Sstevel@tonic-gate 			return (0);
2100Sstevel@tonic-gate 	return (1);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate static int
read_sb(int fd,const char * dev)2140Sstevel@tonic-gate read_sb(int fd, const char *dev)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	(void) llseek(fd, (offset_t)(SBLOCK * DEV_BSIZE), 0);
2170Sstevel@tonic-gate 	if (read(fd, &sblock, SBSIZE) != SBSIZE) {
2180Sstevel@tonic-gate 		(void) printf("cannot read %s\n", dev);
2190Sstevel@tonic-gate 		return (35);
2200Sstevel@tonic-gate 	} else {
2210Sstevel@tonic-gate 		return (0);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate }
224