xref: /illumos-gate/usr/src/cmd/fs.d/ufs/clri/clri.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*7c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate  * clri filsys inumber ...
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <unistd.h>
42*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
43*7c478bd9Sstevel@tonic-gate #include <stdio.h>
44*7c478bd9Sstevel@tonic-gate #include <errno.h>
45*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
46*7c478bd9Sstevel@tonic-gate #include <string.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include "roll_log.h"
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	ISIZE	(sizeof (struct dinode))
59*7c478bd9Sstevel@tonic-gate #define	NI	(MAXBSIZE/ISIZE)
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static struct dinode buf[NI];
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static union {
64*7c478bd9Sstevel@tonic-gate 	char		dummy[SBSIZE];
65*7c478bd9Sstevel@tonic-gate 	struct fs	sblk;
66*7c478bd9Sstevel@tonic-gate } sb_un;
67*7c478bd9Sstevel@tonic-gate #define	sblock sb_un.sblk
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static int status;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static int read_sb(int fd, const char *dev);
72*7c478bd9Sstevel@tonic-gate static int isnumber(const char *s);
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate int
75*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	int		i, f;
78*7c478bd9Sstevel@tonic-gate 	unsigned int	n;
79*7c478bd9Sstevel@tonic-gate 	int		j;
80*7c478bd9Sstevel@tonic-gate 	offset_t	off;
81*7c478bd9Sstevel@tonic-gate 	int32_t		gen;
82*7c478bd9Sstevel@tonic-gate 	time_t		t;
83*7c478bd9Sstevel@tonic-gate 	int		sbrr;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (argc < 3) {
86*7c478bd9Sstevel@tonic-gate 		(void) printf("ufs usage: clri filsys inumber ...\n");
87*7c478bd9Sstevel@tonic-gate 		return (35);
88*7c478bd9Sstevel@tonic-gate 	}
89*7c478bd9Sstevel@tonic-gate 	f = open64(argv[1], 2);
90*7c478bd9Sstevel@tonic-gate 	if (f < 0) {
91*7c478bd9Sstevel@tonic-gate 		(void) printf("cannot open %s\n", argv[1]);
92*7c478bd9Sstevel@tonic-gate 		return (35);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	if ((sbrr = read_sb(f, argv[1])) != 0) {
96*7c478bd9Sstevel@tonic-gate 		return (sbrr);
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	/* If fs is logged, roll the log. */
100*7c478bd9Sstevel@tonic-gate 	if (sblock.fs_logbno) {
101*7c478bd9Sstevel@tonic-gate 		switch (rl_roll_log(argv[1])) {
102*7c478bd9Sstevel@tonic-gate 		case RL_SUCCESS:
103*7c478bd9Sstevel@tonic-gate 			/*
104*7c478bd9Sstevel@tonic-gate 			 * Reread the superblock.  Rolling the log may have
105*7c478bd9Sstevel@tonic-gate 			 * changed it.
106*7c478bd9Sstevel@tonic-gate 			 */
107*7c478bd9Sstevel@tonic-gate 			if ((sbrr = read_sb(f, argv[1])) != 0) {
108*7c478bd9Sstevel@tonic-gate 				return (sbrr);
109*7c478bd9Sstevel@tonic-gate 			}
110*7c478bd9Sstevel@tonic-gate 			break;
111*7c478bd9Sstevel@tonic-gate 		case RL_SYSERR:
112*7c478bd9Sstevel@tonic-gate 			(void) printf("Warning: Cannot roll log for %s.  %s.  "
113*7c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
114*7c478bd9Sstevel@tonic-gate 				argv[1], strerror(errno));
115*7c478bd9Sstevel@tonic-gate 			break;
116*7c478bd9Sstevel@tonic-gate 		default:
117*7c478bd9Sstevel@tonic-gate 			(void) printf("Cannot roll log for %s.  "
118*7c478bd9Sstevel@tonic-gate 				"Inodes will be cleared anyway.\n",
119*7c478bd9Sstevel@tonic-gate 				argv[1]);
120*7c478bd9Sstevel@tonic-gate 			break;
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if ((sblock.fs_magic != FS_MAGIC) &&
125*7c478bd9Sstevel@tonic-gate 	    (sblock.fs_magic != MTB_UFS_MAGIC)) {
126*7c478bd9Sstevel@tonic-gate 		(void) printf("bad super block magic number\n");
127*7c478bd9Sstevel@tonic-gate 		return (35);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (sblock.fs_magic == MTB_UFS_MAGIC &&
131*7c478bd9Sstevel@tonic-gate 	    (sblock.fs_version > MTB_UFS_VERSION_1 ||
132*7c478bd9Sstevel@tonic-gate 	    sblock.fs_version < MTB_UFS_VERSION_MIN)) {
133*7c478bd9Sstevel@tonic-gate 		(void) printf(
134*7c478bd9Sstevel@tonic-gate 		    "unrecognized version of UFS on-disk format: %d\n",
135*7c478bd9Sstevel@tonic-gate 		    sblock.fs_version);
136*7c478bd9Sstevel@tonic-gate 		return (35);
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
140*7c478bd9Sstevel@tonic-gate 		if (!isnumber(argv[i])) {
141*7c478bd9Sstevel@tonic-gate 			(void) printf("%s: is not a number\n", argv[i]);
142*7c478bd9Sstevel@tonic-gate 			status = 1;
143*7c478bd9Sstevel@tonic-gate 			continue;
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
146*7c478bd9Sstevel@tonic-gate 		if (n == 0) {
147*7c478bd9Sstevel@tonic-gate 			(void) printf("%s: is zero\n", argv[i]);
148*7c478bd9Sstevel@tonic-gate 			status = 1;
149*7c478bd9Sstevel@tonic-gate 			continue;
150*7c478bd9Sstevel@tonic-gate 		}
151*7c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
152*7c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
153*7c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
154*7c478bd9Sstevel@tonic-gate 		if (read(f, (char *)buf, sblock.fs_bsize) != sblock.fs_bsize) {
155*7c478bd9Sstevel@tonic-gate 			(void) printf("%s: read error\n", argv[i]);
156*7c478bd9Sstevel@tonic-gate 			status = 1;
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 	if (status)
160*7c478bd9Sstevel@tonic-gate 		return (status+31);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	/*
163*7c478bd9Sstevel@tonic-gate 	 * Update the time in superblock, so fsck will check this filesystem.
164*7c478bd9Sstevel@tonic-gate 	 */
165*7c478bd9Sstevel@tonic-gate 	(void) llseek(f, (offset_t)(SBLOCK * DEV_BSIZE), 0);
166*7c478bd9Sstevel@tonic-gate 	(void) time(&t);
167*7c478bd9Sstevel@tonic-gate 	sblock.fs_time = (time32_t)t;
168*7c478bd9Sstevel@tonic-gate 	if (write(f, &sblock, SBSIZE) != SBSIZE) {
169*7c478bd9Sstevel@tonic-gate 		(void) printf("cannot update %s\n", argv[1]);
170*7c478bd9Sstevel@tonic-gate 		return (35);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	for (i = 2; i < argc; i++) {
174*7c478bd9Sstevel@tonic-gate 		n = atoi(argv[i]);
175*7c478bd9Sstevel@tonic-gate 		(void) printf("clearing %u\n", n);
176*7c478bd9Sstevel@tonic-gate 		off = fsbtodb(&sblock, itod(&sblock, n));
177*7c478bd9Sstevel@tonic-gate 		off *= DEV_BSIZE;
178*7c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
179*7c478bd9Sstevel@tonic-gate 		(void) read(f, (char *)buf, sblock.fs_bsize);
180*7c478bd9Sstevel@tonic-gate 		j = itoo(&sblock, n);
181*7c478bd9Sstevel@tonic-gate 		gen = buf[j].di_gen;
182*7c478bd9Sstevel@tonic-gate 		memset(&buf[j], 0, ISIZE);
183*7c478bd9Sstevel@tonic-gate 		buf[j].di_gen = gen + 1;
184*7c478bd9Sstevel@tonic-gate 		(void) llseek(f, off, 0);
185*7c478bd9Sstevel@tonic-gate 		(void) write(f, (char *)buf, sblock.fs_bsize);
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	if (status)
188*7c478bd9Sstevel@tonic-gate 		return (status+31);
189*7c478bd9Sstevel@tonic-gate 	(void) close(f);
190*7c478bd9Sstevel@tonic-gate 	return (0);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate static int
194*7c478bd9Sstevel@tonic-gate isnumber(const char *s)
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate 	int c;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
199*7c478bd9Sstevel@tonic-gate 		if (c < '0' || c > '9')
200*7c478bd9Sstevel@tonic-gate 			return (0);
201*7c478bd9Sstevel@tonic-gate 	return (1);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate static int
205*7c478bd9Sstevel@tonic-gate read_sb(int fd, const char *dev)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	(void) llseek(fd, (offset_t)(SBLOCK * DEV_BSIZE), 0);
208*7c478bd9Sstevel@tonic-gate 	if (read(fd, &sblock, SBSIZE) != SBSIZE) {
209*7c478bd9Sstevel@tonic-gate 		(void) printf("cannot read %s\n", dev);
210*7c478bd9Sstevel@tonic-gate 		return (35);
211*7c478bd9Sstevel@tonic-gate 	} else {
212*7c478bd9Sstevel@tonic-gate 		return (0);
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate }
215