xref: /illumos-gate/usr/src/cmd/fs.d/ufs/clri/clri.c (revision 68d61a2a)
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 /*
23*68d61a2aSprabahar  * 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
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 
99*68d61a2aSprabahar 	if ((sblock.fs_magic != FS_MAGIC) &&
100*68d61a2aSprabahar 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
101*68d61a2aSprabahar 		(void) printf("bad super block magic number\n");
102*68d61a2aSprabahar 		return (35);
103*68d61a2aSprabahar 	}
104*68d61a2aSprabahar 
105*68d61a2aSprabahar 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
106*68d61a2aSprabahar 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
107*68d61a2aSprabahar 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
108*68d61a2aSprabahar 		(void) printf(
109*68d61a2aSprabahar 		    "unrecognized version of UFS on-disk format: %d\n",
110*68d61a2aSprabahar 		    sblock.fs_version);
111*68d61a2aSprabahar 		return (35);
112*68d61a2aSprabahar 	}
113*68d61a2aSprabahar 
1147c478bd9Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
1157c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno) {
1167c478bd9Sstevel@tonic-gate 		switch (rl_roll_log(argv[1])) {
1177c478bd9Sstevel@tonic-gate 		case RL_SUCCESS:
1187c478bd9Sstevel@tonic-gate 			/*
1197c478bd9Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
1207c478bd9Sstevel@tonic-gate 			 * changed it.
1217c478bd9Sstevel@tonic-gate 			 */
1227c478bd9Sstevel@tonic-gate 			if ((sbrr = read_sb(f, argv[1])) != 0) {
1237c478bd9Sstevel@tonic-gate 				return (sbrr);
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 			break;
1267c478bd9Sstevel@tonic-gate 		case RL_SYSERR:
1277c478bd9Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.  %s.  "
1287c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1297c478bd9Sstevel@tonic-gate 				argv[1], strerror(errno));
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		default:
1327c478bd9Sstevel@tonic-gate 			(void) printf("Cannot roll log for %s.  "
1337c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
1347c478bd9Sstevel@tonic-gate 				argv[1]);
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1407c478bd9Sstevel@tonic-gate 		if (!isnumber(argv[i])) {
1417c478bd9Sstevel@tonic-gate 			(void) printf("%s: is not a number\n", argv[i]);
1427c478bd9Sstevel@tonic-gate 			status = 1;
1437c478bd9Sstevel@tonic-gate 			continue;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
1467c478bd9Sstevel@tonic-gate 		if (n == 0) {
1477c478bd9Sstevel@tonic-gate 			(void) printf("%s: is zero\n", argv[i]);
1487c478bd9Sstevel@tonic-gate 			status = 1;
1497c478bd9Sstevel@tonic-gate 			continue;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1527c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
1537c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1547c478bd9Sstevel@tonic-gate 		if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
1557c478bd9Sstevel@tonic-gate 			(void) printf("%s: read error\n", argv[i]);
1567c478bd9Sstevel@tonic-gate 			status = 1;
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 	if (status)
1607c478bd9Sstevel@tonic-gate 		return (status+31);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/*
1637c478bd9Sstevel@tonic-gate 	 * Update the time in superblock, so fsck will check this filesystem.
1647c478bd9Sstevel@tonic-gate 	 */
1657c478bd9Sstevel@tonic-gate 	(void) llseek(f, (offset_t)(SBLOCK * DEV_BSIZE), 0);
1667c478bd9Sstevel@tonic-gate 	(void) time(&t);
1677c478bd9Sstevel@tonic-gate 	sblock.fs_time = (time32_t)t;
1687c478bd9Sstevel@tonic-gate 	if (write(f, &sblock, SBSIZE) != SBSIZE) {
1697c478bd9Sstevel@tonic-gate 		(void) printf("cannot update %s\n", argv[1]);
1707c478bd9Sstevel@tonic-gate 		return (35);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
1747c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
1757c478bd9Sstevel@tonic-gate 		(void) printf("clearing %u\n", n);
1767c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
1777c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
1787c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1797c478bd9Sstevel@tonic-gate 		(void) read(f, (char *)buf, sblock.fs_bsize);
1807c478bd9Sstevel@tonic-gate 		j = itoo(&sblock, n);
1817c478bd9Sstevel@tonic-gate 		gen = buf[j].di_gen;
1827c478bd9Sstevel@tonic-gate 		memset(&buf[j], 0, ISIZE);
1837c478bd9Sstevel@tonic-gate 		buf[j].di_gen = gen + 1;
1847c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
1857c478bd9Sstevel@tonic-gate 		(void) write(f, (char *)buf, sblock.fs_bsize);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	if (status)
1887c478bd9Sstevel@tonic-gate 		return (status+31);
1897c478bd9Sstevel@tonic-gate 	(void) close(f);
1907c478bd9Sstevel@tonic-gate 	return (0);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static int
1947c478bd9Sstevel@tonic-gate isnumber(const char *s)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	int c;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
1997c478bd9Sstevel@tonic-gate 		if (c < '0' || c > '9')
2007c478bd9Sstevel@tonic-gate 			return (0);
2017c478bd9Sstevel@tonic-gate 	return (1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static int
2057c478bd9Sstevel@tonic-gate read_sb(int fd, const char *dev)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	(void) llseek(fd, (offset_t)(SBLOCK * DEV_BSIZE), 0);
2087c478bd9Sstevel@tonic-gate 	if (read(fd, &sblock, SBSIZE) != SBSIZE) {
2097c478bd9Sstevel@tonic-gate 		(void) printf("cannot read %s\n", dev);
2107c478bd9Sstevel@tonic-gate 		return (35);
2117c478bd9Sstevel@tonic-gate 	} else {
2127c478bd9Sstevel@tonic-gate 		return (0);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate }
215