ca91c55e31b692e3dea398edd6d4a16893b03b5b
[multipath-tools/.git] / libmpathpersist / mpath_persist.c
1 #include <libdevmapper.h>
2 #include "defaults.h"
3 #include <sys/stat.h>
4 #include <sys/types.h>
5 #include <fcntl.h>
6 #include "vector.h"
7 #include "checkers.h"
8 #include "structs.h"
9 #include "structs_vec.h"
10 #include <libudev.h>
11
12 #include "prio.h"
13 #include <unistd.h>
14 #include "devmapper.h"
15 #include "debug.h"
16 #include "config.h"
17 #include "switchgroup.h"
18 #include "discovery.h"
19 #include "dmparser.h"
20 #include <ctype.h>
21 #include "propsel.h"
22 #include "util.h"
23
24 #include "mpath_persist.h"
25 #include "mpathpr.h"
26 #include "mpath_pr_ioctl.h"
27
28 #include <pthread.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/resource.h>
34
35 #define __STDC_FORMAT_MACROS 1
36
37 extern struct udev *udev;
38
39 struct config *
40 mpath_lib_init (void)
41 {
42         struct config *conf;
43
44         conf = load_config(DEFAULT_CONFIGFILE);
45         if (!conf) {
46                 condlog(0, "Failed to initialize multipath config.");
47                 return NULL;
48         }
49
50         if (conf->max_fds) {
51                 struct rlimit fd_limit;
52
53                 fd_limit.rlim_cur = conf->max_fds;
54                 fd_limit.rlim_max = conf->max_fds;
55                 if (setrlimit(RLIMIT_NOFILE, &fd_limit) < 0)
56                         condlog(0, "can't set open fds limit to %d : %s",
57                                    conf->max_fds, strerror(errno));
58         }
59
60         return conf;
61 }
62
63 int
64 mpath_lib_exit (struct config *conf)
65 {
66         dm_lib_release();
67         dm_lib_exit();
68         cleanup_prio();
69         cleanup_checkers();
70         free_config(conf);
71         conf = NULL;
72         return 0;
73 }
74
75 static int
76 updatepaths (struct multipath * mpp)
77 {
78         int i, j;
79         struct pathgroup * pgp;
80         struct path * pp;
81         struct config *conf;
82
83         if (!mpp->pg)
84                 return 0;
85
86         vector_foreach_slot (mpp->pg, pgp, i){
87                 if (!pgp->paths)
88                         continue;
89
90                 vector_foreach_slot (pgp->paths, pp, j){
91                         if (!strlen(pp->dev)){
92                                 if (devt2devname(pp->dev, FILE_NAME_SIZE,
93                                                  pp->dev_t)){
94                                         /*
95                                          * path is not in sysfs anymore
96                                          */
97                                         pp->state = PATH_DOWN;
98                                         continue;
99                                 }
100                                 pp->mpp = mpp;
101                                 conf = get_multipath_config();
102                                 pathinfo(pp, conf, DI_ALL);
103                                 put_multipath_config(conf);
104                                 continue;
105                         }
106                         pp->mpp = mpp;
107                         if (pp->state == PATH_UNCHECKED ||
108                                         pp->state == PATH_WILD) {
109                                 conf = get_multipath_config();
110                                 pathinfo(pp, conf, DI_CHECKER);
111                                 put_multipath_config(conf);
112                         }
113
114                         if (pp->priority == PRIO_UNDEF) {
115                                 conf = get_multipath_config();
116                                 pathinfo(pp, conf, DI_PRIO);
117                                 put_multipath_config(conf);
118                         }
119                 }
120         }
121         return 0;
122 }
123
124 int
125 mpath_prin_activepath (struct multipath *mpp, int rq_servact,
126         struct prin_resp * resp, int noisy)
127 {
128         int i,j, ret = MPATH_PR_DMMP_ERROR;
129         struct pathgroup *pgp = NULL;
130         struct path *pp = NULL;
131
132         vector_foreach_slot (mpp->pg, pgp, j){
133                 vector_foreach_slot (pgp->paths, pp, i){
134                         if (!((pp->state == PATH_UP) ||
135                               (pp->state == PATH_GHOST))){
136                                 condlog(2, "%s: %s not available. Skip.",
137                                         mpp->wwid, pp->dev);
138                                 condlog(3, "%s: status = %d.",
139                                         mpp->wwid, pp->state);
140                                 continue;
141                         }
142
143                         condlog(3, "%s: sending pr in command to %s ",
144                                 mpp->wwid, pp->dev);
145                         ret = mpath_send_prin_activepath(pp->dev, rq_servact,
146                                                          resp, noisy);
147                         switch(ret)
148                         {
149                                 case MPATH_PR_SUCCESS:
150                                 case MPATH_PR_SENSE_INVALID_OP:
151                                         return ret;
152                                 default:
153                                         continue;
154                         }
155                 }
156         }
157         return ret;
158 }
159
160 int mpath_persistent_reserve_in (int fd, int rq_servact,
161         struct prin_resp *resp, int noisy, int verbose)
162 {
163         struct stat info;
164         vector curmp = NULL;
165         vector pathvec = NULL;
166         char * alias;
167         struct multipath * mpp;
168         int map_present;
169         int major, minor;
170         int ret;
171         struct config *conf;
172
173         conf = get_multipath_config();
174         conf->verbosity = verbose;
175         put_multipath_config(conf);
176
177         if (fstat( fd, &info) != 0){
178                 condlog(0, "stat error %d", fd);
179                 return MPATH_PR_FILE_ERROR;
180         }
181         if(!S_ISBLK(info.st_mode)){
182                 condlog(0, "Failed to get major:minor. fd = %d", fd);
183                 return MPATH_PR_FILE_ERROR;
184         }
185
186         major = major(info.st_rdev);
187         minor = minor(info.st_rdev);
188         condlog(4, "Device %d:%d:  ", major, minor);
189
190         /* get alias from major:minor*/
191         alias = dm_mapname(major, minor);
192         if (!alias){
193                 condlog(0, "%d:%d failed to get device alias.", major, minor);
194                 return MPATH_PR_DMMP_ERROR;
195         }
196
197         condlog(3, "alias = %s", alias);
198         map_present = dm_map_present(alias);
199         if (map_present && !dm_is_mpath(alias)){
200                 condlog( 0, "%s: not a multipath device.", alias);
201                 ret = MPATH_PR_DMMP_ERROR;
202                 goto out;
203         }
204
205         /*
206          * allocate core vectors to store paths and multipaths
207          */
208         curmp = vector_alloc ();
209         pathvec = vector_alloc ();
210
211         if (!curmp || !pathvec){
212                 condlog (0, "%s: vector allocation failed.", alias);
213                 ret = MPATH_PR_DMMP_ERROR;
214                 if (curmp)
215                         vector_free(curmp);
216                 if (pathvec)
217                         vector_free(pathvec);
218                 goto out;
219         }
220
221         if (path_discovery(pathvec, DI_SYSFS | DI_CHECKER) < 0) {
222                 ret = MPATH_PR_DMMP_ERROR;
223                 goto out1;
224         }
225
226         /* get info of all paths from the dm device     */
227         if (get_mpvec (curmp, pathvec, alias)){
228                 condlog(0, "%s: failed to get device info.", alias);
229                 ret = MPATH_PR_DMMP_ERROR;
230                 goto out1;
231         }
232
233         mpp = find_mp_by_alias(curmp, alias);
234         if (!mpp){
235                 condlog(0, "%s: devmap not registered.", alias);
236                 ret = MPATH_PR_DMMP_ERROR;
237                 goto out1;
238         }
239
240         ret = mpath_prin_activepath(mpp, rq_servact, resp, noisy);
241
242 out1:
243         free_multipathvec(curmp, KEEP_PATHS);
244         free_pathvec(pathvec, FREE_PATHS);
245 out:
246         FREE(alias);
247         return ret;
248 }
249
250 int mpath_persistent_reserve_out ( int fd, int rq_servact, int rq_scope,
251         unsigned int rq_type, struct prout_param_descriptor *paramp, int noisy, int verbose)
252 {
253
254         struct stat info;
255
256         vector curmp = NULL;
257         vector pathvec = NULL;
258
259         char * alias;
260         struct multipath * mpp;
261         int map_present;
262         int major, minor;
263         int ret;
264         uint64_t prkey;
265         struct config *conf;
266
267         conf = get_multipath_config();
268         conf->verbosity = verbose;
269         put_multipath_config(conf);
270
271         if (fstat( fd, &info) != 0){
272                 condlog(0, "stat error fd=%d", fd);
273                 return MPATH_PR_FILE_ERROR;
274         }
275
276         if(!S_ISBLK(info.st_mode)){
277                 condlog(3, "Failed to get major:minor. fd=%d", fd);
278                 return MPATH_PR_FILE_ERROR;
279         }
280
281         major = major(info.st_rdev);
282         minor = minor(info.st_rdev);
283         condlog(4, "Device  %d:%d", major, minor);
284
285         /* get WWN of the device from major:minor*/
286         alias = dm_mapname(major, minor);
287         if (!alias){
288                 return MPATH_PR_DMMP_ERROR;
289         }
290
291         condlog(3, "alias = %s", alias);
292         map_present = dm_map_present(alias);
293
294         if (map_present && !dm_is_mpath(alias)){
295                 condlog(3, "%s: not a multipath device.", alias);
296                 ret = MPATH_PR_DMMP_ERROR;
297                 goto out;
298         }
299
300         /*
301          * allocate core vectors to store paths and multipaths
302          */
303         curmp = vector_alloc ();
304         pathvec = vector_alloc ();
305
306         if (!curmp || !pathvec){
307                 condlog (0, "%s: vector allocation failed.", alias);
308                 ret = MPATH_PR_DMMP_ERROR;
309                 if (curmp)
310                         vector_free(curmp);
311                 if (pathvec)
312                         vector_free(pathvec);
313                 goto out;
314         }
315
316         if (path_discovery(pathvec, DI_SYSFS | DI_CHECKER) < 0) {
317                 ret = MPATH_PR_DMMP_ERROR;
318                 goto out1;
319         }
320
321         /* get info of all paths from the dm device     */
322         if (get_mpvec(curmp, pathvec, alias)){
323                 condlog(0, "%s: failed to get device info.", alias);
324                 ret = MPATH_PR_DMMP_ERROR;
325                 goto out1;
326         }
327
328         mpp = find_mp_by_alias(curmp, alias);
329
330         if (!mpp) {
331                 condlog(0, "%s: devmap not registered.", alias);
332                 ret = MPATH_PR_DMMP_ERROR;
333                 goto out1;
334         }
335
336         conf = get_multipath_config();
337         select_reservation_key(conf, mpp);
338         select_all_tg_pt(conf, mpp);
339         put_multipath_config(conf);
340
341         memcpy(&prkey, paramp->sa_key, 8);
342         if (mpp->prkey_source == PRKEY_SOURCE_FILE && prkey &&
343             ((!get_be64(mpp->reservation_key) &&
344               rq_servact == MPATH_PROUT_REG_SA) ||
345              rq_servact == MPATH_PROUT_REG_IGN_SA)) {
346                 memcpy(&mpp->reservation_key, paramp->sa_key, 8);
347                 if (update_prkey(alias, get_be64(mpp->reservation_key))) {
348                         condlog(0, "%s: failed to set prkey for multipathd.",
349                                 alias);
350                         ret = MPATH_PR_DMMP_ERROR;
351                         goto out1;
352                 }
353         }
354
355         if (memcmp(paramp->key, &mpp->reservation_key, 8) &&
356             memcmp(paramp->sa_key, &mpp->reservation_key, 8)) {
357                 condlog(0, "%s: configured reservation key doesn't match: 0x%" PRIx64, alias, get_be64(mpp->reservation_key));
358                 ret = MPATH_PR_SYNTAX_ERROR;
359                 goto out1;
360         }
361
362         switch(rq_servact)
363         {
364         case MPATH_PROUT_REG_SA:
365         case MPATH_PROUT_REG_IGN_SA:
366                 ret= mpath_prout_reg(mpp, rq_servact, rq_scope, rq_type, paramp, noisy);
367                 break;
368         case MPATH_PROUT_RES_SA :
369         case MPATH_PROUT_PREE_SA :
370         case MPATH_PROUT_PREE_AB_SA :
371         case MPATH_PROUT_CLEAR_SA:
372                 ret = mpath_prout_common(mpp, rq_servact, rq_scope, rq_type, paramp, noisy);
373                 break;
374         case MPATH_PROUT_REL_SA:
375                 ret = mpath_prout_rel(mpp, rq_servact, rq_scope, rq_type, paramp, noisy);
376                 break;
377         default:
378                 ret = MPATH_PR_OTHER;
379                 goto out1;
380         }
381
382         if ((ret == MPATH_PR_SUCCESS) && ((rq_servact == MPATH_PROUT_REG_SA) ||
383                                 (rq_servact ==  MPATH_PROUT_REG_IGN_SA)))
384         {
385                 if (prkey == 0) {
386                         update_prflag(alias, 0);
387                         update_prkey(alias, 0);
388                 } else
389                         update_prflag(alias, 1);
390         } else if ((ret == MPATH_PR_SUCCESS) && (rq_servact == MPATH_PROUT_CLEAR_SA)) {
391                 update_prflag(alias, 0);
392                 update_prkey(alias, 0);
393         }
394 out1:
395         free_multipathvec(curmp, KEEP_PATHS);
396         free_pathvec(pathvec, FREE_PATHS);
397
398 out:
399         FREE(alias);
400         return ret;
401 }
402
403 int
404 get_mpvec (vector curmp, vector pathvec, char * refwwid)
405 {
406         int i;
407         struct multipath *mpp;
408         char params[PARAMS_SIZE], status[PARAMS_SIZE];
409
410         if (dm_get_maps (curmp)){
411                 return 1;
412         }
413
414         vector_foreach_slot (curmp, mpp, i){
415                 /*
416                  * discard out of scope maps
417                  */
418                 if (mpp->alias && refwwid &&
419                     strncmp (mpp->alias, refwwid, WWID_SIZE - 1)){
420                         free_multipath (mpp, KEEP_PATHS);
421                         vector_del_slot (curmp, i);
422                         i--;
423                         continue;
424                 }
425
426                 dm_get_map(mpp->alias, &mpp->size, params);
427                 condlog(3, "params = %s", params);
428                 dm_get_status(mpp->alias, status);
429                 condlog(3, "status = %s", status);
430                 disassemble_map (pathvec, params, mpp, 0);
431
432                 /*
433                  * disassemble_map() can add new paths to pathvec.
434                  * If not in "fast list mode", we need to fetch information
435                  * about them
436                  */
437                 updatepaths(mpp);
438                 mpp->bestpg = select_path_group (mpp);
439                 disassemble_status (status, mpp);
440
441         }
442         return MPATH_PR_SUCCESS ;
443 }
444
445 int mpath_send_prin_activepath (char * dev, int rq_servact,
446                                 struct prin_resp * resp, int noisy)
447 {
448
449         int rc;
450
451         rc = prin_do_scsi_ioctl(dev, rq_servact, resp,  noisy);
452
453         return (rc);
454 }
455
456 int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
457         unsigned int rq_type, struct prout_param_descriptor * paramp, int noisy)
458 {
459
460         int i, j, k;
461         struct pathgroup *pgp = NULL;
462         struct path *pp = NULL;
463         int rollback = 0;
464         int active_pathcount=0;
465         int rc;
466         int count=0;
467         int status = MPATH_PR_SUCCESS;
468         uint64_t sa_key = 0;
469
470         if (!mpp)
471                 return MPATH_PR_DMMP_ERROR;
472
473         active_pathcount = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
474
475         if (active_pathcount == 0) {
476                 condlog (0, "%s: no path available", mpp->wwid);
477                 return MPATH_PR_DMMP_ERROR;
478         }
479
480         if ( paramp->sa_flags & MPATH_F_ALL_TG_PT_MASK ) {
481                 condlog (1, "Warning: ALL_TG_PT is set. Configuration not supported");
482         }
483
484         struct threadinfo thread[active_pathcount];
485         int hosts[active_pathcount];
486
487         memset(thread, 0, sizeof(thread));
488
489         /* init thread parameter */
490         for (i =0; i< active_pathcount; i++){
491                 hosts[i] = -1;
492                 thread[i].param.rq_servact = rq_servact;
493                 thread[i].param.rq_scope = rq_scope;
494                 thread[i].param.rq_type = rq_type;
495                 thread[i].param.paramp = paramp;
496                 thread[i].param.noisy = noisy;
497                 thread[i].param.status = MPATH_PR_SKIP;
498
499                 condlog (3, "THREAD ID [%d] INFO]", i);
500                 condlog (3, "rq_servact=%d ", thread[i].param.rq_servact);
501                 condlog (3, "rq_scope=%d ", thread[i].param.rq_scope);
502                 condlog (3, "rq_type=%d ", thread[i].param.rq_type);
503                 condlog (3, "rkey=");
504                 condlog (3, "paramp->sa_flags =%02x ",
505                          thread[i].param.paramp->sa_flags);
506                 condlog (3, "noisy=%d ", thread[i].param.noisy);
507                 condlog (3, "status=%d ", thread[i].param.status);
508         }
509
510         pthread_attr_t attr;
511         pthread_attr_init(&attr);
512         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
513
514         vector_foreach_slot (mpp->pg, pgp, j){
515                 vector_foreach_slot (pgp->paths, pp, i){
516                         if (!((pp->state == PATH_UP) || (pp->state == PATH_GHOST))){
517                                 condlog (1, "%s: %s path not up. Skip.", mpp->wwid, pp->dev);
518                                 continue;
519                         }
520                         if (mpp->all_tg_pt == ALL_TG_PT_ON &&
521                             pp->sg_id.host_no != -1) {
522                                 for (k = 0; k < count; k++) {
523                                         if (pp->sg_id.host_no == hosts[k]) {
524                                                 condlog(3, "%s: %s host %d matches skip.", pp->wwid, pp->dev, pp->sg_id.host_no);
525                                                 break;
526                                         }
527                                 }
528                                 if (k < count)
529                                         continue;
530                         }
531                         strncpy(thread[count].param.dev, pp->dev,
532                                 FILE_NAME_SIZE - 1);
533
534                         if (count && (thread[count].param.paramp->sa_flags & MPATH_F_SPEC_I_PT_MASK)){
535                                 /*
536                                  * Clearing SPEC_I_PT as transportids are already registered by now.
537                                  */
538                                 thread[count].param.paramp->sa_flags &= (~MPATH_F_SPEC_I_PT_MASK);
539                         }
540
541                         condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
542
543                         rc = pthread_create(&thread[count].id, &attr, mpath_prout_pthread_fn, (void *)(&thread[count].param));
544                         if (rc){
545                                 condlog (0, "%s: failed to create thread %d", mpp->wwid, rc);
546                                 thread[count].param.status = MPATH_PR_THREAD_ERROR;
547                         }
548                         else
549                                 hosts[count] = pp->sg_id.host_no;
550                         count = count + 1;
551                 }
552         }
553         for( i=0; i < count ; i++){
554                 if (thread[i].param.status != MPATH_PR_THREAD_ERROR) {
555                         rc = pthread_join(thread[i].id, NULL);
556                         if (rc){
557                                 condlog (0, "%s: Thread[%d] failed to join thread %d", mpp->wwid, i, rc);
558                         }
559                 }
560                 if (!rollback && (thread[i].param.status == MPATH_PR_RESERV_CONFLICT)){
561                         rollback = 1;
562                         sa_key = 0;
563                         for (i = 0; i < 8; ++i){
564                                 if (i > 0)
565                                         sa_key <<= 8;
566                                 sa_key |= paramp->sa_key[i];
567                         }
568                         status = MPATH_PR_RESERV_CONFLICT ;
569                 }
570                 if (!rollback && (status == MPATH_PR_SUCCESS)){
571                         status = thread[i].param.status;
572                 }
573         }
574         if (rollback && ((rq_servact == MPATH_PROUT_REG_SA) && sa_key != 0 )){
575                 condlog (3, "%s: ERROR: initiating pr out rollback", mpp->wwid);
576                 for( i=0 ; i < count ; i++){
577                         if(thread[i].param.status == MPATH_PR_SUCCESS) {
578                                 memcpy(&thread[i].param.paramp->key, &thread[i].param.paramp->sa_key, 8);
579                                 memset(&thread[i].param.paramp->sa_key, 0, 8);
580                                 thread[i].param.status = MPATH_PR_SUCCESS;
581                                 rc = pthread_create(&thread[i].id, &attr, mpath_prout_pthread_fn,
582                                                 (void *)(&thread[i].param));
583                                 if (rc){
584                                         condlog (0, "%s: failed to create thread for rollback. %d",  mpp->wwid, rc);
585                                         thread[i].param.status = MPATH_PR_THREAD_ERROR;
586                                 }
587                         } else
588                                 thread[i].param.status = MPATH_PR_SKIP;
589                 }
590                 for(i=0; i < count ; i++){
591                         if (thread[i].param.status != MPATH_PR_SKIP &&
592                             thread[i].param.status != MPATH_PR_THREAD_ERROR) {
593                                 rc = pthread_join(thread[i].id, NULL);
594                                 if (rc){
595                                         condlog (3, "%s: failed to join thread while rolling back %d",
596                                                  mpp->wwid, i);
597                                 }
598                         }
599                 }
600         }
601
602         pthread_attr_destroy(&attr);
603         return (status);
604 }
605
606 void * mpath_prout_pthread_fn(void *p)
607 {
608         int ret;
609         struct prout_param * param = (struct prout_param *)p;
610
611         ret = prout_do_scsi_ioctl( param->dev,param->rq_servact, param->rq_scope,
612                         param->rq_type, param->paramp, param->noisy);
613         param->status = ret;
614         pthread_exit(NULL);
615 }
616
617 int mpath_prout_common(struct multipath *mpp,int rq_servact, int rq_scope,
618         unsigned int rq_type, struct prout_param_descriptor* paramp, int noisy)
619 {
620         int i,j, ret;
621         struct pathgroup *pgp = NULL;
622         struct path *pp = NULL;
623
624         vector_foreach_slot (mpp->pg, pgp, j){
625                 vector_foreach_slot (pgp->paths, pp, i){
626                         if (!((pp->state == PATH_UP) || (pp->state == PATH_GHOST))){
627                                 condlog (1, "%s: %s path not up. Skip",
628                                          mpp->wwid, pp->dev);
629                                 continue;
630                         }
631
632                         condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
633                         ret = send_prout_activepath(pp->dev, rq_servact,
634                                                     rq_scope, rq_type,
635                                                     paramp, noisy);
636                         return ret ;
637                 }
638         }
639         return MPATH_PR_SUCCESS;
640 }
641
642 int send_prout_activepath(char * dev, int rq_servact, int rq_scope,
643         unsigned int rq_type, struct prout_param_descriptor * paramp, int noisy)
644 {
645         struct prout_param param;
646         param.rq_servact = rq_servact;
647         param.rq_scope  = rq_scope;
648         param.rq_type   = rq_type;
649         param.paramp    = paramp;
650         param.noisy = noisy;
651         param.status = -1;
652
653         pthread_t thread;
654         pthread_attr_t attr;
655         int rc;
656
657         memset(&thread, 0, sizeof(thread));
658         strncpy(param.dev, dev, FILE_NAME_SIZE - 1);
659         /* Initialize and set thread joinable attribute */
660         pthread_attr_init(&attr);
661         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
662
663         rc = pthread_create(&thread, &attr, mpath_prout_pthread_fn, (void *)(&param));
664         if (rc){
665                 condlog (3, "%s: failed to create thread %d", dev, rc);
666                 return MPATH_PR_THREAD_ERROR;
667         }
668         /* Free attribute and wait for the other threads */
669         pthread_attr_destroy(&attr);
670         rc = pthread_join(thread, NULL);
671
672         return (param.status);
673 }
674
675 int mpath_prout_rel(struct multipath *mpp,int rq_servact, int rq_scope,
676         unsigned int rq_type, struct prout_param_descriptor * paramp, int noisy)
677 {
678         int i, j;
679         int num = 0;
680         struct pathgroup *pgp = NULL;
681         struct path *pp = NULL;
682         int active_pathcount = 0;
683         pthread_attr_t attr;
684         int rc, found = 0;
685         int count = 0;
686         int status = MPATH_PR_SUCCESS;
687         struct prin_resp resp;
688         struct prout_param_descriptor *pamp;
689         struct prin_resp *pr_buff;
690         int length;
691         struct transportid *pptr;
692
693         if (!mpp)
694                 return MPATH_PR_DMMP_ERROR;
695
696         active_pathcount = pathcount (mpp, PATH_UP) + pathcount (mpp, PATH_GHOST);
697
698         struct threadinfo thread[active_pathcount];
699         memset(thread, 0, sizeof(thread));
700         for (i = 0; i < active_pathcount; i++){
701                 thread[i].param.rq_servact = rq_servact;
702                 thread[i].param.rq_scope = rq_scope;
703                 thread[i].param.rq_type = rq_type;
704                 thread[i].param.paramp = paramp;
705                 thread[i].param.noisy = noisy;
706                 thread[i].param.status = MPATH_PR_SKIP;
707
708                 condlog (3, " path count = %d", i);
709                 condlog (3, "rq_servact=%d ", thread[i].param.rq_servact);
710                 condlog (3, "rq_scope=%d ", thread[i].param.rq_scope);
711                 condlog (3, "rq_type=%d ", thread[i].param.rq_type);
712                 condlog (3, "noisy=%d ", thread[i].param.noisy);
713                 condlog (3, "status=%d ", thread[i].param.status);
714         }
715
716         pthread_attr_init (&attr);
717         pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
718
719         vector_foreach_slot (mpp->pg, pgp, j){
720                 vector_foreach_slot (pgp->paths, pp, i){
721                         if (!((pp->state == PATH_UP) || (pp->state == PATH_GHOST))){
722                                 condlog (1, "%s: %s path not up.", mpp->wwid, pp->dev);
723                                 continue;
724                         }
725
726                         strncpy(thread[count].param.dev, pp->dev,
727                                 FILE_NAME_SIZE - 1);
728                         condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
729                         rc = pthread_create (&thread[count].id, &attr, mpath_prout_pthread_fn,
730                                         (void *) (&thread[count].param));
731                         if (rc) {
732                                 condlog (0, "%s: failed to create thread. %d",  mpp->wwid, rc);
733                                 thread[count].param.status = MPATH_PR_THREAD_ERROR;
734                         }
735                         count = count + 1;
736                 }
737         }
738         pthread_attr_destroy (&attr);
739         for (i = 0; i < count; i++){
740                 if (thread[i].param.status != MPATH_PR_THREAD_ERROR) {
741                         rc = pthread_join (thread[i].id, NULL);
742                         if (rc){
743                                 condlog (1, "%s: failed to join thread.  %d",  mpp->wwid,  rc);
744                         }
745                 }
746         }
747
748         for (i = 0; i < count; i++){
749                 /*  check thread status here and return the status */
750
751                 if (thread[i].param.status == MPATH_PR_RESERV_CONFLICT)
752                         status = MPATH_PR_RESERV_CONFLICT;
753                 else if (status == MPATH_PR_SUCCESS
754                                 && thread[i].param.status != MPATH_PR_RESERV_CONFLICT)
755                         status = thread[i].param.status;
756         }
757
758         status = mpath_prin_activepath (mpp, MPATH_PRIN_RRES_SA, &resp, noisy);
759         if (status != MPATH_PR_SUCCESS){
760                 condlog (0, "%s: pr in read reservation command failed.", mpp->wwid);
761                 return MPATH_PR_OTHER;
762         }
763
764         num = resp.prin_descriptor.prin_readresv.additional_length / 8;
765         if (num == 0){
766                 condlog (2, "%s: Path holding reservation is released.", mpp->wwid);
767                 return MPATH_PR_SUCCESS;
768         }
769         condlog (2, "%s: Path holding reservation is not avialable.", mpp->wwid);
770
771         pr_buff =  mpath_alloc_prin_response(MPATH_PRIN_RFSTAT_SA);
772         if (!pr_buff){
773                 condlog (0, "%s: failed to  alloc pr in response buffer.", mpp->wwid);
774                 return MPATH_PR_OTHER;
775         }
776
777         status = mpath_prin_activepath (mpp, MPATH_PRIN_RFSTAT_SA, pr_buff, noisy);
778
779         if (status != MPATH_PR_SUCCESS){
780                 condlog (0,  "%s: pr in read full status command failed.",  mpp->wwid);
781                 goto out;
782         }
783
784         num = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
785         if (0 == num){
786                 goto out;
787         }
788         length = sizeof (struct prout_param_descriptor) + (sizeof (struct transportid *));
789
790         pamp = (struct prout_param_descriptor *)malloc (length);
791         if (!pamp){
792                 condlog (0, "%s: failed to alloc pr out parameter.", mpp->wwid);
793                 goto out1;
794         }
795
796         memset(pamp, 0, length);
797
798         pamp->trnptid_list[0] = (struct transportid *) malloc (sizeof (struct transportid));
799         if (!pamp->trnptid_list[0]){
800                 condlog (0, "%s: failed to alloc pr out transportid.", mpp->wwid);
801                 goto out1;
802         }
803
804         if (get_be64(mpp->reservation_key)){
805                 memcpy (pamp->key, &mpp->reservation_key, 8);
806                 condlog (3, "%s: reservation key set.", mpp->wwid);
807         }
808
809         status = mpath_prout_common (mpp, MPATH_PROUT_CLEAR_SA,
810                                      rq_scope, rq_type, pamp, noisy);
811
812         if (status) {
813                 condlog(0, "%s: failed to send CLEAR_SA", mpp->wwid);
814                 goto out1;
815         }
816
817         pamp->num_transportid = 1;
818         pptr=pamp->trnptid_list[0];
819
820         for (i = 0; i < num; i++){
821                 if (get_be64(mpp->reservation_key) &&
822                         memcmp(pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key,
823                                &mpp->reservation_key, 8)){
824                         /*register with tarnsport id*/
825                         memset(pamp, 0, length);
826                         pamp->trnptid_list[0] = pptr;
827                         memset (pamp->trnptid_list[0], 0, sizeof (struct transportid));
828                         memcpy (pamp->sa_key,
829                                         pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key, 8);
830                         pamp->sa_flags = MPATH_F_SPEC_I_PT_MASK;
831                         pamp->num_transportid = 1;
832
833                         memcpy (pamp->trnptid_list[0],
834                                         &pr_buff->prin_descriptor.prin_readfd.descriptors[i]->trnptid,
835                                         sizeof (struct transportid));
836                         status = mpath_prout_common (mpp, MPATH_PROUT_REG_SA, 0, rq_type,
837                                         pamp, noisy);
838
839                         pamp->sa_flags = 0;
840                         memcpy (pamp->key, pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key, 8);
841                         memset (pamp->sa_key, 0, 8);
842                         pamp->num_transportid = 0;
843                         status = mpath_prout_common (mpp, MPATH_PROUT_REG_SA, 0, rq_type,
844                                         pamp, noisy);
845                 }
846                 else
847                 {
848                         if (get_be64(mpp->reservation_key))
849                                 found = 1;
850                 }
851
852
853         }
854
855         if (found){
856                 memset (pamp, 0, length);
857                 memcpy (pamp->sa_key, &mpp->reservation_key, 8);
858                 memset (pamp->key, 0, 8);
859                 status = mpath_prout_reg(mpp, MPATH_PROUT_REG_SA, rq_scope, rq_type, pamp, noisy);
860         }
861
862
863         free(pptr);
864 out1:
865         free (pamp);
866 out:
867         free (pr_buff);
868         return (status);
869 }
870
871 void * mpath_alloc_prin_response(int prin_sa)
872 {
873         void * ptr = NULL;
874         int size=0;
875         switch (prin_sa)
876         {
877                 case MPATH_PRIN_RKEY_SA:
878                         size = sizeof(struct prin_readdescr);
879                         break;
880                 case MPATH_PRIN_RRES_SA:
881                         size = sizeof(struct prin_resvdescr);
882                         break;
883                 case MPATH_PRIN_RCAP_SA:
884                         size=sizeof(struct prin_capdescr);
885                         break;
886                 case MPATH_PRIN_RFSTAT_SA:
887                         size = sizeof(struct print_fulldescr_list) +
888                                 sizeof(struct prin_fulldescr *)*MPATH_MX_TIDS;
889                         break;
890         }
891         if (size > 0)
892         {
893                 ptr = calloc(size, 1);
894         }
895         return ptr;
896 }
897
898 int update_map_pr(struct multipath *mpp)
899 {
900         int noisy=0;
901         struct prin_resp *resp;
902         int i, ret, isFound;
903
904         if (!get_be64(mpp->reservation_key))
905         {
906                 /* Nothing to do. Assuming pr mgmt feature is disabled*/
907                 condlog(3, "%s: reservation_key not set in multipath.conf", mpp->alias);
908                 return MPATH_PR_SUCCESS;
909         }
910
911         resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
912         if (!resp)
913         {
914                 condlog(0,"%s : failed to alloc resp in update_map_pr", mpp->alias);
915                 return MPATH_PR_OTHER;
916         }
917         ret = mpath_prin_activepath(mpp, MPATH_PRIN_RKEY_SA, resp, noisy);
918
919         if (ret != MPATH_PR_SUCCESS )
920         {
921                 condlog(0,"%s : pr in read keys service action failed Error=%d", mpp->alias, ret);
922                 free(resp);
923                 return  ret;
924         }
925
926         if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
927         {
928                 condlog(3,"%s: No key found. Device may not be registered. ", mpp->alias);
929                 free(resp);
930                 return MPATH_PR_SUCCESS;
931         }
932
933         condlog(2, "%s: Multipath  reservation_key: 0x%" PRIx64 " ", mpp->alias,
934                 get_be64(mpp->reservation_key));
935
936         isFound =0;
937         for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
938         {
939                 condlog(2, "%s: PR IN READKEYS[%d]  reservation key:", mpp->alias, i);
940                 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , 1);
941
942                 if (!memcmp(&mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
943                 {
944                         condlog(2, "%s: reservation key found in pr in readkeys response", mpp->alias);
945                         isFound =1;
946                 }
947         }
948
949         if (isFound)
950         {
951                 mpp->prflag = 1;
952                 condlog(2, "%s: prflag flag set.", mpp->alias );
953         }
954
955         free(resp);
956         return MPATH_PR_SUCCESS;
957 }