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 2004 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 /*
28*7c478bd9Sstevel@tonic-gate * Creates and maintains a short-term cache of live upgrade slices.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <dirent.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <synch.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
40*7c478bd9Sstevel@tonic-gate #include <unistd.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
45*7c478bd9Sstevel@tonic-gate #include "disks_private.h"
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate #define TMPNM_SIZE 25
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate * The list of live upgrade slices in use.
51*7c478bd9Sstevel@tonic-gate */
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate struct lu_list {
54*7c478bd9Sstevel@tonic-gate struct lu_list *next;
55*7c478bd9Sstevel@tonic-gate char *slice;
56*7c478bd9Sstevel@tonic-gate char *name;
57*7c478bd9Sstevel@tonic-gate };
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate static struct lu_list *lu_listp = NULL;
60*7c478bd9Sstevel@tonic-gate static time_t timestamp = 0;
61*7c478bd9Sstevel@tonic-gate static mutex_t lu_lock = DEFAULTMUTEX;
62*7c478bd9Sstevel@tonic-gate
63*7c478bd9Sstevel@tonic-gate static int add_use_record(char *devname, char *name);
64*7c478bd9Sstevel@tonic-gate static void free_lu(struct lu_list *listp);
65*7c478bd9Sstevel@tonic-gate static int load_lu();
66*7c478bd9Sstevel@tonic-gate static int lustatus(int fd);
67*7c478bd9Sstevel@tonic-gate static int lufslist(int fd);
68*7c478bd9Sstevel@tonic-gate static int run_cmd(char *path, char *cmd, char *arg, int fd);
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate * Search the list of devices under live upgrade for the specified device.
72*7c478bd9Sstevel@tonic-gate */
73*7c478bd9Sstevel@tonic-gate int
inuse_lu(char * slice,nvlist_t * attrs,int * errp)74*7c478bd9Sstevel@tonic-gate inuse_lu(char *slice, nvlist_t *attrs, int *errp)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate int found = 0;
77*7c478bd9Sstevel@tonic-gate time_t curr_time;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate *errp = 0;
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate if (slice == NULL) {
82*7c478bd9Sstevel@tonic-gate return (found);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate * We don't want to have to re-read the live upgrade config for
87*7c478bd9Sstevel@tonic-gate * every slice, but we can't just cache it since there is no event
88*7c478bd9Sstevel@tonic-gate * when this changes. So, we'll keep the config in memory for
89*7c478bd9Sstevel@tonic-gate * a short time (1 minute) before reloading it.
90*7c478bd9Sstevel@tonic-gate */
91*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&lu_lock);
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate curr_time = time(NULL);
94*7c478bd9Sstevel@tonic-gate if (timestamp < curr_time && (curr_time - timestamp) > 60) {
95*7c478bd9Sstevel@tonic-gate free_lu(lu_listp); /* free old entries */
96*7c478bd9Sstevel@tonic-gate lu_listp = NULL;
97*7c478bd9Sstevel@tonic-gate *errp = load_lu(); /* load the cache */
98*7c478bd9Sstevel@tonic-gate timestamp = curr_time;
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate
101*7c478bd9Sstevel@tonic-gate if (*errp == 0) {
102*7c478bd9Sstevel@tonic-gate struct lu_list *listp;
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate listp = lu_listp;
105*7c478bd9Sstevel@tonic-gate while (listp != NULL) {
106*7c478bd9Sstevel@tonic-gate if (strcmp(slice, listp->slice) == 0) {
107*7c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_LU, errp);
108*7c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, listp->name, errp);
109*7c478bd9Sstevel@tonic-gate found = 1;
110*7c478bd9Sstevel@tonic-gate break;
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate listp = listp->next;
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lu_lock);
117*7c478bd9Sstevel@tonic-gate
118*7c478bd9Sstevel@tonic-gate return (found);
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate static int
add_use_record(char * devname,char * name)122*7c478bd9Sstevel@tonic-gate add_use_record(char *devname, char *name)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate struct lu_list *sp;
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate sp = (struct lu_list *)malloc(sizeof (struct lu_list));
127*7c478bd9Sstevel@tonic-gate if (sp == NULL) {
128*7c478bd9Sstevel@tonic-gate return (ENOMEM);
129*7c478bd9Sstevel@tonic-gate }
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gate if ((sp->slice = strdup(devname)) == NULL) {
132*7c478bd9Sstevel@tonic-gate free(sp);
133*7c478bd9Sstevel@tonic-gate return (ENOMEM);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate
136*7c478bd9Sstevel@tonic-gate if ((sp->name = strdup(name)) == NULL) {
137*7c478bd9Sstevel@tonic-gate free(sp->slice);
138*7c478bd9Sstevel@tonic-gate free(sp);
139*7c478bd9Sstevel@tonic-gate return (ENOMEM);
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate
142*7c478bd9Sstevel@tonic-gate sp->next = lu_listp;
143*7c478bd9Sstevel@tonic-gate lu_listp = sp;
144*7c478bd9Sstevel@tonic-gate
145*7c478bd9Sstevel@tonic-gate return (0);
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate
148*7c478bd9Sstevel@tonic-gate /*
149*7c478bd9Sstevel@tonic-gate * Free the list of liveupgrade entries.
150*7c478bd9Sstevel@tonic-gate */
151*7c478bd9Sstevel@tonic-gate static void
free_lu(struct lu_list * listp)152*7c478bd9Sstevel@tonic-gate free_lu(struct lu_list *listp) {
153*7c478bd9Sstevel@tonic-gate
154*7c478bd9Sstevel@tonic-gate struct lu_list *nextp;
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate while (listp != NULL) {
157*7c478bd9Sstevel@tonic-gate nextp = listp->next;
158*7c478bd9Sstevel@tonic-gate free((void *)listp->slice);
159*7c478bd9Sstevel@tonic-gate free((void *)listp->name);
160*7c478bd9Sstevel@tonic-gate free((void *)listp);
161*7c478bd9Sstevel@tonic-gate listp = nextp;
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate /*
166*7c478bd9Sstevel@tonic-gate * Create a list of live upgrade devices.
167*7c478bd9Sstevel@tonic-gate */
168*7c478bd9Sstevel@tonic-gate static int
load_lu()169*7c478bd9Sstevel@tonic-gate load_lu()
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate char tmpname[TMPNM_SIZE];
172*7c478bd9Sstevel@tonic-gate int fd;
173*7c478bd9Sstevel@tonic-gate int status = 0;
174*7c478bd9Sstevel@tonic-gate
175*7c478bd9Sstevel@tonic-gate (void) strlcpy(tmpname, "/var/run/dm_lu_XXXXXX", TMPNM_SIZE);
176*7c478bd9Sstevel@tonic-gate if ((fd = mkstemp(tmpname)) != -1) {
177*7c478bd9Sstevel@tonic-gate (void) unlink(tmpname);
178*7c478bd9Sstevel@tonic-gate if (run_cmd("/usr/sbin/lustatus", "lustatus", NULL, fd)) {
179*7c478bd9Sstevel@tonic-gate status = lustatus(fd);
180*7c478bd9Sstevel@tonic-gate } else {
181*7c478bd9Sstevel@tonic-gate (void) close(fd);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate }
184*7c478bd9Sstevel@tonic-gate
185*7c478bd9Sstevel@tonic-gate return (status);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate /*
189*7c478bd9Sstevel@tonic-gate * The XML generated by the live upgrade commands is not parseable by the
190*7c478bd9Sstevel@tonic-gate * standard Solaris XML parser, so we have to do it ourselves.
191*7c478bd9Sstevel@tonic-gate */
192*7c478bd9Sstevel@tonic-gate static int
lufslist(int fd)193*7c478bd9Sstevel@tonic-gate lufslist(int fd)
194*7c478bd9Sstevel@tonic-gate {
195*7c478bd9Sstevel@tonic-gate FILE *fp;
196*7c478bd9Sstevel@tonic-gate char line[MAXPATHLEN];
197*7c478bd9Sstevel@tonic-gate int status;
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate if ((fp = fdopen(fd, "r")) == NULL) {
200*7c478bd9Sstevel@tonic-gate (void) close(fd);
201*7c478bd9Sstevel@tonic-gate return (0);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate
204*7c478bd9Sstevel@tonic-gate (void) fseek(fp, 0L, SEEK_SET);
205*7c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) == line) {
206*7c478bd9Sstevel@tonic-gate char *devp;
207*7c478bd9Sstevel@tonic-gate char *nmp;
208*7c478bd9Sstevel@tonic-gate char *ep;
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate if (strncmp(line, "<beFsComponent ", 15) != 0) {
211*7c478bd9Sstevel@tonic-gate continue;
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate
214*7c478bd9Sstevel@tonic-gate if ((devp = strstr(line, "fsDevice=\"")) == NULL) {
215*7c478bd9Sstevel@tonic-gate continue;
216*7c478bd9Sstevel@tonic-gate }
217*7c478bd9Sstevel@tonic-gate
218*7c478bd9Sstevel@tonic-gate devp = devp + 10;
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate if ((ep = strchr(devp, '"')) == NULL) {
221*7c478bd9Sstevel@tonic-gate continue;
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate
224*7c478bd9Sstevel@tonic-gate *ep = 0;
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate /* try to get the mountpoint name */
227*7c478bd9Sstevel@tonic-gate if ((nmp = strstr(ep + 1, "mountPoint=\"")) != NULL) {
228*7c478bd9Sstevel@tonic-gate nmp = nmp + 12;
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate if ((ep = strchr(nmp, '"')) != NULL) {
231*7c478bd9Sstevel@tonic-gate *ep = 0;
232*7c478bd9Sstevel@tonic-gate } else {
233*7c478bd9Sstevel@tonic-gate nmp = "";
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate } else {
237*7c478bd9Sstevel@tonic-gate nmp = "";
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate
240*7c478bd9Sstevel@tonic-gate if ((status = add_use_record(devp, nmp)) != 0) {
241*7c478bd9Sstevel@tonic-gate break;
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate (void) fclose(fp);
246*7c478bd9Sstevel@tonic-gate
247*7c478bd9Sstevel@tonic-gate return (status);
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate
250*7c478bd9Sstevel@tonic-gate static int
lustatus(int fd)251*7c478bd9Sstevel@tonic-gate lustatus(int fd)
252*7c478bd9Sstevel@tonic-gate {
253*7c478bd9Sstevel@tonic-gate FILE *fp;
254*7c478bd9Sstevel@tonic-gate char line[MAXPATHLEN];
255*7c478bd9Sstevel@tonic-gate int status = 0;
256*7c478bd9Sstevel@tonic-gate
257*7c478bd9Sstevel@tonic-gate if ((fp = fdopen(fd, "r")) == NULL) {
258*7c478bd9Sstevel@tonic-gate (void) close(fd);
259*7c478bd9Sstevel@tonic-gate return (0);
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate
262*7c478bd9Sstevel@tonic-gate (void) fseek(fp, 0L, SEEK_SET);
263*7c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) == line) {
264*7c478bd9Sstevel@tonic-gate char *sp;
265*7c478bd9Sstevel@tonic-gate char *ep;
266*7c478bd9Sstevel@tonic-gate char tmpname[TMPNM_SIZE];
267*7c478bd9Sstevel@tonic-gate int ffd;
268*7c478bd9Sstevel@tonic-gate
269*7c478bd9Sstevel@tonic-gate if (strncmp(line, "<beStatus ", 10) != 0) {
270*7c478bd9Sstevel@tonic-gate continue;
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate if ((sp = strstr(line, "name=\"")) == NULL) {
274*7c478bd9Sstevel@tonic-gate continue;
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate
277*7c478bd9Sstevel@tonic-gate sp = sp + 6;
278*7c478bd9Sstevel@tonic-gate
279*7c478bd9Sstevel@tonic-gate if ((ep = strchr(sp, '"')) == NULL) {
280*7c478bd9Sstevel@tonic-gate continue;
281*7c478bd9Sstevel@tonic-gate }
282*7c478bd9Sstevel@tonic-gate
283*7c478bd9Sstevel@tonic-gate *ep = 0;
284*7c478bd9Sstevel@tonic-gate
285*7c478bd9Sstevel@tonic-gate (void) strlcpy(tmpname, "/var/run/dm_lu_XXXXXX", TMPNM_SIZE);
286*7c478bd9Sstevel@tonic-gate if ((ffd = mkstemp(tmpname)) != -1) {
287*7c478bd9Sstevel@tonic-gate (void) unlink(tmpname);
288*7c478bd9Sstevel@tonic-gate
289*7c478bd9Sstevel@tonic-gate if (run_cmd("/usr/sbin/lufslist", "lufslist", sp, ffd) == 0) {
290*7c478bd9Sstevel@tonic-gate (void) close(ffd);
291*7c478bd9Sstevel@tonic-gate break;
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate
294*7c478bd9Sstevel@tonic-gate if ((status = lufslist(ffd)) != 0) {
295*7c478bd9Sstevel@tonic-gate break;
296*7c478bd9Sstevel@tonic-gate }
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate
300*7c478bd9Sstevel@tonic-gate (void) fclose(fp);
301*7c478bd9Sstevel@tonic-gate
302*7c478bd9Sstevel@tonic-gate return (status);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate
305*7c478bd9Sstevel@tonic-gate static int
run_cmd(char * path,char * cmd,char * arg,int fd)306*7c478bd9Sstevel@tonic-gate run_cmd(char *path, char *cmd, char *arg, int fd)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate pid_t pid;
309*7c478bd9Sstevel@tonic-gate int loc;
310*7c478bd9Sstevel@tonic-gate
311*7c478bd9Sstevel@tonic-gate /* create the server process */
312*7c478bd9Sstevel@tonic-gate switch ((pid = fork1())) {
313*7c478bd9Sstevel@tonic-gate case 0:
314*7c478bd9Sstevel@tonic-gate /* child process */
315*7c478bd9Sstevel@tonic-gate (void) close(1);
316*7c478bd9Sstevel@tonic-gate (void) dup(fd);
317*7c478bd9Sstevel@tonic-gate (void) close(2);
318*7c478bd9Sstevel@tonic-gate (void) dup(fd);
319*7c478bd9Sstevel@tonic-gate closefrom(3);
320*7c478bd9Sstevel@tonic-gate (void) execl(path, cmd, "-X", arg, NULL);
321*7c478bd9Sstevel@tonic-gate _exit(1);
322*7c478bd9Sstevel@tonic-gate break;
323*7c478bd9Sstevel@tonic-gate
324*7c478bd9Sstevel@tonic-gate case -1:
325*7c478bd9Sstevel@tonic-gate return (0);
326*7c478bd9Sstevel@tonic-gate
327*7c478bd9Sstevel@tonic-gate default:
328*7c478bd9Sstevel@tonic-gate /* parent process */
329*7c478bd9Sstevel@tonic-gate break;
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate
332*7c478bd9Sstevel@tonic-gate (void) waitpid(pid, &loc, 0);
333*7c478bd9Sstevel@tonic-gate
334*7c478bd9Sstevel@tonic-gate /* printf("got 0x%x %d %d\n", loc, WIFEXITED(loc), WEXITSTATUS(loc)); */
335*7c478bd9Sstevel@tonic-gate
336*7c478bd9Sstevel@tonic-gate if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) {
337*7c478bd9Sstevel@tonic-gate return (1);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate
340*7c478bd9Sstevel@tonic-gate return (0);
341*7c478bd9Sstevel@tonic-gate }
342