xref: /illumos-gate/usr/src/cmd/fs.d/ufs/clri/clri.c (revision 6451fdbc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
2368d61a2aSprabahar  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * clri filsys inumber ...
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <unistd.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <stdio.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <fcntl.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
537c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
547c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include "roll_log.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	ISIZE	(sizeof (struct dinode))
597c478bd9Sstevel@tonic-gate #define	NI	(MAXBSIZE/ISIZE)
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static struct dinode buf[NI];
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static union {
647c478bd9Sstevel@tonic-gate 	char		dummy[SBSIZE];
657c478bd9Sstevel@tonic-gate 	struct fs	sblk;
667c478bd9Sstevel@tonic-gate } sb_un;
677c478bd9Sstevel@tonic-gate #define	sblock sb_un.sblk
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static int status;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static int read_sb(int fd, const char *dev);
727c478bd9Sstevel@tonic-gate static int isnumber(const char *s);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])757c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	int		i, f;
787c478bd9Sstevel@tonic-gate 	unsigned int	n;
797c478bd9Sstevel@tonic-gate 	int		j;
807c478bd9Sstevel@tonic-gate 	offset_t	off;
817c478bd9Sstevel@tonic-gate 	int32_t		gen;
827c478bd9Sstevel@tonic-gate 	time_t		t;
837c478bd9Sstevel@tonic-gate 	int		sbrr;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (argc < 3) {
867c478bd9Sstevel@tonic-gate 		(void) printf("ufs usage: clri filsys inumber ...\n");
877c478bd9Sstevel@tonic-gate 		return (35);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	f = open64(argv[1], 2);
907c478bd9Sstevel@tonic-gate 	if (f < 0) {
917c478bd9Sstevel@tonic-gate 		(void) printf("cannot open %s\n", argv[1]);
927c478bd9Sstevel@tonic-gate 		return (35);
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if ((sbrr = read_sb(f, argv[1])) != 0) {
967c478bd9Sstevel@tonic-gate 		return (sbrr);
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
9968d61a2aSprabahar 	if ((sblock.fs_magic != FS_MAGIC) &&
10068d61a2aSprabahar 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
10168d61a2aSprabahar 		(void) printf("bad super block magic number\n");
10268d61a2aSprabahar 		return (35);
10368d61a2aSprabahar 	}
10468d61a2aSprabahar 
105*6451fdbcSvsakar 	if (sblock.fs_magic == FS_MAGIC &&
106*6451fdbcSvsakar 	    (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
107*6451fdbcSvsakar 	    sblock.fs_version != UFS_VERSION_MIN)) {
108*6451fdbcSvsakar 		(void) printf(
109*6451fdbcSvsakar 		    "unrecognized version of UFS on-disk format: %d\n",
110*6451fdbcSvsakar 		    sblock.fs_version);
111*6451fdbcSvsakar 		return (35);
112*6451fdbcSvsakar 	}
113*6451fdbcSvsakar 
11468d61a2aSprabahar 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
11568d61a2aSprabahar 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
11668d61a2aSprabahar 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
11768d61a2aSprabahar 		(void) printf(
11868d61a2aSprabahar 		    "unrecognized version of UFS on-disk format: %d\n",
11968d61a2aSprabahar 		    sblock.fs_version);
12068d61a2aSprabahar 		return (35);
12168d61a2aSprabahar 	}
12268d61a2aSprabahar 
1237c478bd9Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
1247c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno) {
1257c478bd9Sstevel@tonic-gate 		switch (rl_roll_log(argv[1])) {
1267c478bd9Sstevel@tonic-gate 		case RL_SUCCESS:
1277c478bd9Sstevel@tonic-gate 			/*
1287c478bd9Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
1297c478bd9Sstevel@tonic-gate 			 * changed it.
1307c478bd9Sstevel@tonic-gate 			 */
1317c478bd9Sstevel@tonic-gate 			if ((sbrr = read_sb(f, argv[1])) != 0) {
1327c478bd9Sstevel@tonic-gate 				return (sbrr);
1337c478bd9Sstevel@tonic-gate 			}
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		case RL_SYSERR:
1367c478bd9Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.  %s.  "
1377c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1387c478bd9Sstevel@tonic-gate 				argv[1], strerror(errno));
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		default:
1417c478bd9Sstevel@tonic-gate 			(void) printf("Cannot roll log for %s.  "
1427c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1437c478bd9Sstevel@tonic-gate 				argv[1]);
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1497c478bd9Sstevel@tonic-gate 		if (!isnumber(argv[i])) {
1507c478bd9Sstevel@tonic-gate 			(void) printf("%s: is not a number\n", argv[i]);
1517c478bd9Sstevel@tonic-gate 			status = 1;
1527c478bd9Sstevel@tonic-gate 			continue;
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
1557c478bd9Sstevel@tonic-gate 		if (n == 0) {
1567c478bd9Sstevel@tonic-gate 			(void) printf("%s: is zero\n", argv[i]);
1577c478bd9Sstevel@tonic-gate 			status = 1;
1587c478bd9Sstevel@tonic-gate 			continue;
1597c478bd9Sstevel@tonic-gate 		}
1607c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1617c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
1627c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1637c478bd9Sstevel@tonic-gate 		if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
1647c478bd9Sstevel@tonic-gate 			(void) printf("%s: read error\n", argv[i]);
1657c478bd9Sstevel@tonic-gate 			status = 1;
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	if (status)
1697c478bd9Sstevel@tonic-gate 		return (status+31);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Update the time in superblock, so fsck will check this filesystem.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	(void) llseek(f, (offset_t)(SBLOCK * DEV_BSIZE), 0);
1757c478bd9Sstevel@tonic-gate 	(void) time(&t);
1767c478bd9Sstevel@tonic-gate 	sblock.fs_time = (time32_t)t;
1777c478bd9Sstevel@tonic-gate 	if (write(f, &sblock, SBSIZE) != SBSIZE) {
1787c478bd9Sstevel@tonic-gate 		(void) printf("cannot update %s\n", argv[1]);
1797c478bd9Sstevel@tonic-gate 		return (35);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1837c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
1847c478bd9Sstevel@tonic-gate 		(void) printf("clearing %u\n", n);
1857c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1867c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
1877c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1887c478bd9Sstevel@tonic-gate 		(void) read(f, (char *)buf, sblock.fs_bsize);
1897c478bd9Sstevel@tonic-gate 		j = itoo(&sblock, n);
1907c478bd9Sstevel@tonic-gate 		gen = buf[j].di_gen;
1917c478bd9Sstevel@tonic-gate 		memset(&buf[j], 0, ISIZE);
1927c478bd9Sstevel@tonic-gate 		buf[j].di_gen = gen + 1;
1937c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1947c478bd9Sstevel@tonic-gate 		(void) write(f, (char *)buf, sblock.fs_bsize);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	if (status)
1977c478bd9Sstevel@tonic-gate 		return (status+31);
1987c478bd9Sstevel@tonic-gate 	(void) close(f);
1997c478bd9Sstevel@tonic-gate 	return (0);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static int
isnumber(const char * s)2037c478bd9Sstevel@tonic-gate isnumber(const char *s)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	int c;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
2087c478bd9Sstevel@tonic-gate 		if (c < '0' || c > '9')
2097c478bd9Sstevel@tonic-gate 			return (0);
2107c478bd9Sstevel@tonic-gate 	return (1);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static int
read_sb(int fd,const char * dev)2147c478bd9Sstevel@tonic-gate read_sb(int fd, const char *dev)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	(void) llseek(fd, (offset_t)(SBLOCK * DEV_BSIZE), 0);
2177c478bd9Sstevel@tonic-gate 	if (read(fd, &sblock, SBSIZE) != SBSIZE) {
2187c478bd9Sstevel@tonic-gate 		(void) printf("cannot read %s\n", dev);
2197c478bd9Sstevel@tonic-gate 		return (35);
2207c478bd9Sstevel@tonic-gate 	} else {
2217c478bd9Sstevel@tonic-gate 		return (0);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate }
224