xref: /titanic_41/usr/src/lib/lvm/libmeta/common/meta_reset.c (revision 6dfee4834394825da35b977ca71cdc965bc7b6a4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * clear metadevices
31  */
32 
33 #include <meta.h>
34 
35 /*
36  * clear a metadevice.
37  */
38 int
39 meta_reset(
40 	mdsetname_t	*sp,
41 	mdname_t	*np,
42 	mdcmdopts_t	options,
43 	md_error_t	*ep
44 )
45 {
46 	char		*miscname;
47 	md_i_reset_t	mir;
48 
49 	/* should have a set */
50 	assert(sp != NULL);
51 	assert(sp->setno == MD_MIN2SET(meta_getminor(np->dev)));
52 	/* clear device */
53 	if ((miscname = metagetmiscname(np, ep)) == NULL)
54 		return (-1);
55 	if (meta_isopen(sp, np, ep, options) != 0) {
56 		return (mdmderror(ep, MDE_IS_OPEN, meta_getminor(np->dev),
57 			np->cname));
58 	}
59 	(void) memset(&mir, '\0', sizeof (mir));
60 	MD_SETDRIVERNAME(&mir, miscname, sp->setno);
61 	mir.mnum = meta_getminor(np->dev);
62 	mir.force = (options & MDCMD_FORCE) ? 1 : 0;
63 	if (metaioctl(MD_IOCRESET, &mir, &mir.mde, np->cname) != 0)
64 		return (mdstealerror(ep, &mir.mde));
65 
66 	/* return success */
67 	return (0);
68 }
69 
70 /*
71  * reset all the metadevice and hotspares
72  */
73 int
74 meta_reset_all(
75 	mdsetname_t	*sp,
76 	mdcmdopts_t	options,
77 	md_error_t	*ep
78 )
79 {
80 	options |= MDCMD_RECURSE;
81 
82 	/*
83 	 * since soft partitions can appear at the top and bottom
84 	 * of the stack, we call meta_sp_reset twice to handle all
85 	 * cases.
86 	 */
87 	if (meta_trans_reset(sp, NULL, options, ep) != 0)
88 		return (-1);
89 	if (meta_sp_reset(sp, NULL, options, ep) != 0)
90 		return (-1);
91 	if (meta_raid_reset(sp, NULL, options, ep) != 0)
92 		return (-1);
93 	if (meta_mirror_reset(sp, NULL, options, ep) != 0)
94 		return (-1);
95 	if (meta_stripe_reset(sp, NULL, options, ep) != 0)
96 		return (-1);
97 	if (meta_hsp_reset(sp, NULL, options, ep) != 0)
98 		return (-1);
99 	if (meta_sp_reset(sp, NULL, options, ep) != 0)
100 		return (-1);
101 
102 	return (0);
103 }
104 
105 /*
106  * reset named device
107  */
108 int
109 meta_reset_by_name(
110 	mdsetname_t	*sp,
111 	mdname_t	*np,
112 	mdcmdopts_t	options,
113 	md_error_t	*ep
114 )
115 {
116 	char		*miscname;
117 	int		rval = 0;
118 
119 	/* should have a set */
120 	assert(sp != NULL);
121 	assert(sp->setno == MD_MIN2SET(meta_getminor(np->dev)));
122 
123 	/* get type */
124 	if (metachkmeta(np, ep) != 0)
125 		return (-1);
126 	if ((miscname = metagetmiscname(np, ep)) == NULL)
127 		return (-1);
128 	/* dispatch */
129 	if (strcmp(miscname, MD_STRIPE) == 0) {
130 		rval = meta_stripe_reset(sp, np, options, ep);
131 	} else if (strcmp(miscname, MD_MIRROR) == 0) {
132 		rval = meta_mirror_reset(sp, np, options, ep);
133 	} else if (strcmp(miscname, MD_TRANS) == 0) {
134 		rval = meta_trans_reset(sp, np, options, ep);
135 	} else if (strcmp(miscname, MD_RAID) == 0) {
136 		rval = meta_raid_reset(sp, np, options, ep);
137 	} else if (strcmp(miscname, MD_SP) == 0) {
138 		rval = meta_sp_reset(sp, np, options, ep);
139 	} else {
140 		rval = mdmderror(ep, MDE_UNKNOWN_TYPE, meta_getminor(np->dev),
141 		    np->cname);
142 	}
143 
144 	/* cleanup */
145 	return (rval);
146 }
147