xref: /illumos-gate/usr/src/cmd/fs.d/ufs/clri/clri.c (revision 2a8bcb4e)
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 /*
367c478bd9Sstevel@tonic-gate  * clri filsys inumber ...
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
517c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
527c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include "roll_log.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	ISIZE	(sizeof (struct dinode))
577c478bd9Sstevel@tonic-gate #define	NI	(MAXBSIZE/ISIZE)
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct dinode buf[NI];
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static union {
627c478bd9Sstevel@tonic-gate 	char		dummy[SBSIZE];
637c478bd9Sstevel@tonic-gate 	struct fs	sblk;
647c478bd9Sstevel@tonic-gate } sb_un;
657c478bd9Sstevel@tonic-gate #define	sblock sb_un.sblk
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static int status;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static int read_sb(int fd, const char *dev);
707c478bd9Sstevel@tonic-gate static int isnumber(const char *s);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])737c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	int		i, f;
767c478bd9Sstevel@tonic-gate 	unsigned int	n;
777c478bd9Sstevel@tonic-gate 	int		j;
787c478bd9Sstevel@tonic-gate 	offset_t	off;
797c478bd9Sstevel@tonic-gate 	int32_t		gen;
807c478bd9Sstevel@tonic-gate 	time_t		t;
817c478bd9Sstevel@tonic-gate 	int		sbrr;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (argc < 3) {
847c478bd9Sstevel@tonic-gate 		(void) printf("ufs usage: clri filsys inumber ...\n");
857c478bd9Sstevel@tonic-gate 		return (35);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	f = open64(argv[1], 2);
887c478bd9Sstevel@tonic-gate 	if (f < 0) {
897c478bd9Sstevel@tonic-gate 		(void) printf("cannot open %s\n", argv[1]);
907c478bd9Sstevel@tonic-gate 		return (35);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if ((sbrr = read_sb(f, argv[1])) != 0) {
947c478bd9Sstevel@tonic-gate 		return (sbrr);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
9768d61a2aSprabahar 	if ((sblock.fs_magic != FS_MAGIC) &&
9868d61a2aSprabahar 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
9968d61a2aSprabahar 		(void) printf("bad super block magic number\n");
10068d61a2aSprabahar 		return (35);
10168d61a2aSprabahar 	}
10268d61a2aSprabahar 
103*6451fdbcSvsakar 	if (sblock.fs_magic == FS_MAGIC &&
104*6451fdbcSvsakar 	    (sblock.fs_version != UFS_EFISTYLE4NONEFI_VERSION_2 &&
105*6451fdbcSvsakar 	    sblock.fs_version != UFS_VERSION_MIN)) {
106*6451fdbcSvsakar 		(void) printf(
107*6451fdbcSvsakar 		    "unrecognized version of UFS on-disk format: %d\n",
108*6451fdbcSvsakar 		    sblock.fs_version);
109*6451fdbcSvsakar 		return (35);
110*6451fdbcSvsakar 	}
111*6451fdbcSvsakar 
11268d61a2aSprabahar 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
11368d61a2aSprabahar 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
11468d61a2aSprabahar 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
11568d61a2aSprabahar 		(void) printf(
11668d61a2aSprabahar 		    "unrecognized version of UFS on-disk format: %d\n",
11768d61a2aSprabahar 		    sblock.fs_version);
11868d61a2aSprabahar 		return (35);
11968d61a2aSprabahar 	}
12068d61a2aSprabahar 
1217c478bd9Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
1227c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno) {
1237c478bd9Sstevel@tonic-gate 		switch (rl_roll_log(argv[1])) {
1247c478bd9Sstevel@tonic-gate 		case RL_SUCCESS:
1257c478bd9Sstevel@tonic-gate 			/*
1267c478bd9Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
1277c478bd9Sstevel@tonic-gate 			 * changed it.
1287c478bd9Sstevel@tonic-gate 			 */
1297c478bd9Sstevel@tonic-gate 			if ((sbrr = read_sb(f, argv[1])) != 0) {
1307c478bd9Sstevel@tonic-gate 				return (sbrr);
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 		case RL_SYSERR:
1347c478bd9Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.  %s.  "
1357c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1367c478bd9Sstevel@tonic-gate 				argv[1], strerror(errno));
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		default:
1397c478bd9Sstevel@tonic-gate 			(void) printf("Cannot roll log for %s.  "
1407c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1417c478bd9Sstevel@tonic-gate 				argv[1]);
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		}
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1477c478bd9Sstevel@tonic-gate 		if (!isnumber(argv[i])) {
1487c478bd9Sstevel@tonic-gate 			(void) printf("%s: is not a number\n", argv[i]);
1497c478bd9Sstevel@tonic-gate 			status = 1;
1507c478bd9Sstevel@tonic-gate 			continue;
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
1537c478bd9Sstevel@tonic-gate 		if (n == 0) {
1547c478bd9Sstevel@tonic-gate 			(void) printf("%s: is zero\n", argv[i]);
1557c478bd9Sstevel@tonic-gate 			status = 1;
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1597c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
1607c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1617c478bd9Sstevel@tonic-gate 		if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
1627c478bd9Sstevel@tonic-gate 			(void) printf("%s: read error\n", argv[i]);
1637c478bd9Sstevel@tonic-gate 			status = 1;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	if (status)
1677c478bd9Sstevel@tonic-gate 		return (status+31);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Update the time in superblock, so fsck will check this filesystem.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	(void) llseek(f, (offset_t)(SBLOCK * DEV_BSIZE), 0);
1737c478bd9Sstevel@tonic-gate 	(void) time(&t);
1747c478bd9Sstevel@tonic-gate 	sblock.fs_time = (time32_t)t;
1757c478bd9Sstevel@tonic-gate 	if (write(f, &sblock, SBSIZE) != SBSIZE) {
1767c478bd9Sstevel@tonic-gate 		(void) printf("cannot update %s\n", argv[1]);
1777c478bd9Sstevel@tonic-gate 		return (35);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1817c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
1827c478bd9Sstevel@tonic-gate 		(void) printf("clearing %u\n", n);
1837c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1847c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
1857c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1867c478bd9Sstevel@tonic-gate 		(void) read(f, (char *)buf, sblock.fs_bsize);
1877c478bd9Sstevel@tonic-gate 		j = itoo(&sblock, n);
1887c478bd9Sstevel@tonic-gate 		gen = buf[j].di_gen;
1897c478bd9Sstevel@tonic-gate 		memset(&buf[j], 0, ISIZE);
1907c478bd9Sstevel@tonic-gate 		buf[j].di_gen = gen + 1;
1917c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1927c478bd9Sstevel@tonic-gate 		(void) write(f, (char *)buf, sblock.fs_bsize);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	if (status)
1957c478bd9Sstevel@tonic-gate 		return (status+31);
1967c478bd9Sstevel@tonic-gate 	(void) close(f);
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
isnumber(const char * s)2017c478bd9Sstevel@tonic-gate isnumber(const char *s)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	int c;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
2067c478bd9Sstevel@tonic-gate 		if (c < '0' || c > '9')
2077c478bd9Sstevel@tonic-gate 			return (0);
2087c478bd9Sstevel@tonic-gate 	return (1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static int
read_sb(int fd,const char * dev)2127c478bd9Sstevel@tonic-gate read_sb(int fd, const char *dev)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	(void) llseek(fd, (offset_t)(SBLOCK * DEV_BSIZE), 0);
2157c478bd9Sstevel@tonic-gate 	if (read(fd, &sblock, SBSIZE) != SBSIZE) {
2167c478bd9Sstevel@tonic-gate 		(void) printf("cannot read %s\n", dev);
2177c478bd9Sstevel@tonic-gate 		return (35);
2187c478bd9Sstevel@tonic-gate 	} else {
2197c478bd9Sstevel@tonic-gate 		return (0);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate }
222