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