xref: /titanic_41/usr/src/cmd/lvm/util/metadevadm.c (revision a31148363f598def767ac48c5d82e1572e44b935)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include	<stdio.h>
27 #include	<stdarg.h>
28 #include	<ctype.h>
29 #include	<sys/fcntl.h>
30 #include	<sys/types.h>
31 #include	<devid.h>
32 #include	<ftw.h>
33 #include	<string.h>
34 #include	<mdiox.h>
35 #include	<sys/lvm/mdio.h>
36 #include 	<meta.h>
37 #include 	<syslog.h>
38 #include	<sdssc.h>
39 
40 /*
41  * print usage message
42  */
43 static void
44 usage(char *myname)
45 {
46 	(void) fprintf(stderr, gettext(
47 	    "usage:  %s -h\n"
48 	    "	%s [-s setname] -r [-lnv]\n"
49 	    "	%s [-s setname] -u cxtxdx [-lnv]\n"),
50 	    myname, myname, myname);
51 }
52 
53 int
54 main(int argc, char **argv)
55 {
56 	char		c;
57 	char		*sname = MD_LOCAL_NAME;
58 	mddevopts_t	options = 0;
59 	md_error_t	status = mdnullerror;
60 	md_error_t	*ep = &status;
61 	mdsetname_t	*sp = NULL;
62 	mdsetname_t	*local_sp = NULL;
63 	char		*argname;
64 	int		todo = 0;
65 	int		ret = 0;
66 	int		md_upgd_stat = 0;
67 	int		error;
68 	md_set_desc	*sd;
69 
70 	/*
71 	 * Get the locale set up before calling any other routines
72 	 * with messages to ouput.  Just in case we're not in a build
73 	 * environment, make sure that TEXT_DOMAIN gets set to
74 	 * something.
75 	 */
76 #if !defined(TEXT_DOMAIN)
77 #define	TEXT_DOMAIN "SYS_TEST"
78 #endif
79 	(void) setlocale(LC_ALL, "");
80 	(void) textdomain(TEXT_DOMAIN);
81 
82 	if ((sdssc_bind_library() == SDSSC_OKAY) &&
83 	    (sdssc_cmd_proxy(argc, argv, SDSSC_PROXY_PRIMARY,
84 	    &error) == SDSSC_PROXY_DONE))
85 		exit(error);
86 
87 	openlog("metadevadm", LOG_ODELAY, LOG_USER);
88 
89 	/* initialize */
90 	if (md_init(argc, argv, 0, 1, ep) != 0 ||
91 	    meta_check_root(ep) != 0) {
92 		closelog();
93 		mde_perror(ep, "");
94 		md_exit(sp, 1);
95 	}
96 
97 	/* parse args */
98 	optind = 1;
99 	opterr = 1;
100 	while ((c = getopt(argc, argv, "vlhnrs:u:")) != -1) {
101 		switch (c) {
102 		case 'v':
103 			options |= DEV_VERBOSE;
104 			break;
105 		case 'n':
106 			options |= DEV_NOACTION;
107 			break;
108 		case 'r':
109 			options |= DEV_RELOAD;
110 			todo = 1;
111 			break;
112 		case 's':
113 			sname = optarg;
114 			break;
115 		case 'u':
116 			todo = 1;
117 			options |= DEV_UPDATE;
118 			argname = optarg;
119 			if (argname == NULL) {
120 				usage("metadevadm");
121 				closelog();
122 				md_exit(sp, 0);
123 			}
124 			break;
125 		case 'l':
126 			options |= DEV_LOG;
127 			break;
128 		case 'h':
129 		default:
130 			usage("metadevadm");
131 			closelog();
132 			md_exit(sp, 0);
133 		}
134 	}
135 
136 	if ((sp = metasetname(sname, ep)) == NULL) {
137 		mde_perror(ep, "");
138 		closelog();
139 		md_exit(sp, 1);
140 	}
141 
142 	if (!metaislocalset(sp)) {
143 		if ((sd = metaget_setdesc(sp, ep)) == NULL) {
144 			mde_perror(ep, "");
145 			closelog();
146 			md_exit(sp, 1);
147 		}
148 		if (MD_MNSET_DESC(sd)) {
149 			(void) printf("%s\n", gettext("metadevadm cannot be "
150 			    "run on multi-owner disksets\n"));
151 			closelog();
152 			md_exit(sp, 0);
153 		}
154 	}
155 
156 	if ((options & DEV_VERBOSE) && (todo != 1)) {
157 		usage("metadevadm");
158 		closelog();
159 		md_exit(sp, 0);
160 	}
161 
162 	if ((options & DEV_NOACTION) && (todo != 1)) {
163 		usage("metadevadm");
164 		closelog();
165 		md_exit(sp, 0);
166 	}
167 
168 	if (todo == 0) {
169 		usage("metadevadm");
170 		closelog();
171 		md_exit(sp, 0);
172 	}
173 
174 	if ((local_sp = metasetname(MD_LOCAL_NAME, ep)) == NULL) {
175 		mde_perror(ep, "");
176 		closelog();
177 		md_exit(local_sp, 1);
178 	}
179 
180 	/* lock the local set */
181 	if (meta_lock(local_sp, TRUE, ep) != 0) {
182 		mde_perror(ep, "");
183 		closelog();
184 		md_exit(local_sp, 1);
185 	}
186 
187 	/* grab set lock */
188 	if (meta_lock(sp, TRUE, ep)) {
189 		mde_perror(ep, "");
190 		closelog();
191 		md_exit(local_sp, 1);
192 	}
193 
194 	/* check for ownership */
195 	if (meta_check_ownership(sp, ep) != 0) {
196 		/*
197 		 * If the set is not owned by this node then only update the
198 		 * local set's replica.
199 		 */
200 		options |= DEV_LOCAL_SET;
201 	}
202 
203 	/*
204 	 * check for upgrade. If upgrade in progress then just exit.
205 	 */
206 	if (metaioctl(MD_UPGRADE_STAT, &md_upgd_stat, ep, NULL) != 0) {
207 			mde_perror(ep, "");
208 			closelog();
209 			(void) meta_unlock(sp, ep);
210 			md_exit(local_sp, 1);
211 	}
212 	if (md_upgd_stat == 0) {
213 		ret = meta_fixdevid(sp, options, argname, ep);
214 		if (ret == METADEVADM_ERR) {
215 			/*
216 			 * If the call failed, for a DEV_RELOAD still need to
217 			 * update the .conf file to provide the latest devid
218 			 * information so exit later.
219 			 */
220 			if (options & DEV_UPDATE) {
221 				closelog();
222 				(void) meta_unlock(sp, ep);
223 				md_exit(local_sp, 1);
224 			}
225 		}
226 	}
227 
228 	/*
229 	 * Sync replica list in kernel to replica list in conf files.
230 	 * This will update driver name and minor number in conf file
231 	 * if reload was run.  Will update device id in conf file if
232 	 * update was run.
233 	 */
234 	meta_sync_db_locations(sp, ep);
235 	closelog();
236 	(void) meta_unlock(sp, ep);
237 	md_exit(local_sp, ret);
238 	return (0);
239 }
240