mpathpersist: fix aptpl support
[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_flags(alias, get_be64(mpp->reservation_key),
348                                        paramp->sa_flags)) {
349                         condlog(0, "%s: failed to set prkey for multipathd.",
350                                 alias);
351                         ret = MPATH_PR_DMMP_ERROR;
352                         goto out1;
353                 }
354         }
355
356         if (memcmp(paramp->key, &mpp->reservation_key, 8) &&
357             memcmp(paramp->sa_key, &mpp->reservation_key, 8)) {
358                 condlog(0, "%s: configured reservation key doesn't match: 0x%" PRIx64, alias, get_be64(mpp->reservation_key));
359                 ret = MPATH_PR_SYNTAX_ERROR;
360                 goto out1;
361         }
362
363         switch(rq_servact)
364         {
365         case MPATH_PROUT_REG_SA:
366         case MPATH_PROUT_REG_IGN_SA:
367                 ret= mpath_prout_reg(mpp, rq_servact, rq_scope, rq_type, paramp, noisy);
368                 break;
369         case MPATH_PROUT_RES_SA :
370         case MPATH_PROUT_PREE_SA :
371         case MPATH_PROUT_PREE_AB_SA :
372         case MPATH_PROUT_CLEAR_SA:
373                 ret = mpath_prout_common(mpp, rq_servact, rq_scope, rq_type, paramp, noisy);
374                 break;
375         case MPATH_PROUT_REL_SA:
376                 ret = mpath_prout_rel(mpp, rq_servact, rq_scope, rq_type, paramp, noisy);
377                 break;
378         default:
379                 ret = MPATH_PR_OTHER;
380                 goto out1;
381         }
382
383         if ((ret == MPATH_PR_SUCCESS) && ((rq_servact == MPATH_PROUT_REG_SA) ||
384                                 (rq_servact ==  MPATH_PROUT_REG_IGN_SA)))
385         {
386                 if (prkey == 0) {
387                         update_prflag(alias, 0);
388                         update_prkey(alias, 0);
389                 } else
390                         update_prflag(alias, 1);
391         } else if ((ret == MPATH_PR_SUCCESS) && (rq_servact == MPATH_PROUT_CLEAR_SA)) {
392                 update_prflag(alias, 0);
393                 update_prkey(alias, 0);
394         }
395 out1:
396         free_multipathvec(curmp, KEEP_PATHS);
397         free_pathvec(pathvec, FREE_PATHS);
398
399 out:
400         FREE(alias);
401         return ret;
402 }
403
404 int
405 get_mpvec (vector curmp, vector pathvec, char * refwwid)
406 {
407         int i;
408         struct multipath *mpp;
409         char params[PARAMS_SIZE], status[PARAMS_SIZE];
410
411         if (dm_get_maps (curmp)){
412                 return 1;
413         }
414
415         vector_foreach_slot (curmp, mpp, i){
416                 /*
417                  * discard out of scope maps
418                  */
419                 if (mpp->alias && refwwid &&
420                     strncmp (mpp->alias, refwwid, WWID_SIZE - 1)){
421                         free_multipath (mpp, KEEP_PATHS);
422                         vector_del_slot (curmp, i);
423                         i--;
424                         continue;
425                 }
426
427                 dm_get_map(mpp->alias, &mpp->size, params);
428                 condlog(3, "params = %s", params);
429                 dm_get_status(mpp->alias, status);
430                 condlog(3, "status = %s", status);
431                 disassemble_map (pathvec, params, mpp, 0);
432
433                 /*
434                  * disassemble_map() can add new paths to pathvec.
435                  * If not in "fast list mode", we need to fetch information
436                  * about them
437                  */
438                 updatepaths(mpp);
439                 mpp->bestpg = select_path_group (mpp);
440                 disassemble_status (status, mpp);
441
442         }
443         return MPATH_PR_SUCCESS ;
444 }
445
446 int mpath_send_prin_activepath (char * dev, int rq_servact,
447                                 struct prin_resp * resp, int noisy)
448 {
449
450         int rc;
451
452         rc = prin_do_scsi_ioctl(dev, rq_servact, resp,  noisy);
453
454         return (rc);
455 }
456
457 int mpath_prout_reg(struct multipath *mpp,int rq_servact, int rq_scope,
458         unsigned int rq_type, struct prout_param_descriptor * paramp, int noisy)
459 {
460
461         int i, j, k;
462         struct pathgroup *pgp = NULL;
463         struct path *pp = NULL;
464         int rollback = 0;
465         int active_pathcount=0;
466         int rc;
467         int count=0;
468         int status = MPATH_PR_SUCCESS;
469         uint64_t sa_key = 0;
470
471         if (!mpp)
472                 return MPATH_PR_DMMP_ERROR;
473
474         active_pathcount = pathcount(mpp, PATH_UP) + pathcount(mpp, PATH_GHOST);
475
476         if (active_pathcount == 0) {
477                 condlog (0, "%s: no path available", mpp->wwid);
478                 return MPATH_PR_DMMP_ERROR;
479         }
480
481         if ( paramp->sa_flags & MPATH_F_ALL_TG_PT_MASK ) {
482                 condlog (1, "Warning: ALL_TG_PT is set. Configuration not supported");
483         }
484
485         struct threadinfo thread[active_pathcount];
486         int hosts[active_pathcount];
487
488         memset(thread, 0, sizeof(thread));
489
490         /* init thread parameter */
491         for (i =0; i< active_pathcount; i++){
492                 hosts[i] = -1;
493                 thread[i].param.rq_servact = rq_servact;
494                 thread[i].param.rq_scope = rq_scope;
495                 thread[i].param.rq_type = rq_type;
496                 thread[i].param.paramp = paramp;
497                 thread[i].param.noisy = noisy;
498                 thread[i].param.status = MPATH_PR_SKIP;
499
500                 condlog (3, "THREAD ID [%d] INFO]", i);
501                 condlog (3, "rq_servact=%d ", thread[i].param.rq_servact);
502                 condlog (3, "rq_scope=%d ", thread[i].param.rq_scope);
503                 condlog (3, "rq_type=%d ", thread[i].param.rq_type);
504                 condlog (3, "rkey=");
505                 condlog (3, "paramp->sa_flags =%02x ",
506                          thread[i].param.paramp->sa_flags);
507                 condlog (3, "noisy=%d ", thread[i].param.noisy);
508                 condlog (3, "status=%d ", thread[i].param.status);
509         }
510
511         pthread_attr_t attr;
512         pthread_attr_init(&attr);
513         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
514
515         vector_foreach_slot (mpp->pg, pgp, j){
516                 vector_foreach_slot (pgp->paths, pp, i){
517                         if (!((pp->state == PATH_UP) || (pp->state == PATH_GHOST))){
518                                 condlog (1, "%s: %s path not up. Skip.", mpp->wwid, pp->dev);
519                                 continue;
520                         }
521                         if (mpp->all_tg_pt == ALL_TG_PT_ON &&
522                             pp->sg_id.host_no != -1) {
523                                 for (k = 0; k < count; k++) {
524                                         if (pp->sg_id.host_no == hosts[k]) {
525                                                 condlog(3, "%s: %s host %d matches skip.", pp->wwid, pp->dev, pp->sg_id.host_no);
526                                                 break;
527                                         }
528                                 }
529                                 if (k < count)
530                                         continue;
531                         }
532                         strncpy(thread[count].param.dev, pp->dev,
533                                 FILE_NAME_SIZE - 1);
534
535                         if (count && (thread[count].param.paramp->sa_flags & MPATH_F_SPEC_I_PT_MASK)){
536                                 /*
537                                  * Clearing SPEC_I_PT as transportids are already registered by now.
538                                  */
539                                 thread[count].param.paramp->sa_flags &= (~MPATH_F_SPEC_I_PT_MASK);
540                         }
541
542                         condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
543
544                         rc = pthread_create(&thread[count].id, &attr, mpath_prout_pthread_fn, (void *)(&thread[count].param));
545                         if (rc){
546                                 condlog (0, "%s: failed to create thread %d", mpp->wwid, rc);
547                                 thread[count].param.status = MPATH_PR_THREAD_ERROR;
548                         }
549                         else
550                                 hosts[count] = pp->sg_id.host_no;
551                         count = count + 1;
552                 }
553         }
554         for( i=0; i < count ; i++){
555                 if (thread[i].param.status != MPATH_PR_THREAD_ERROR) {
556                         rc = pthread_join(thread[i].id, NULL);
557                         if (rc){
558                                 condlog (0, "%s: Thread[%d] failed to join thread %d", mpp->wwid, i, rc);
559                         }
560                 }
561                 if (!rollback && (thread[i].param.status == MPATH_PR_RESERV_CONFLICT)){
562                         rollback = 1;
563                         sa_key = 0;
564                         for (i = 0; i < 8; ++i){
565                                 if (i > 0)
566                                         sa_key <<= 8;
567                                 sa_key |= paramp->sa_key[i];
568                         }
569                         status = MPATH_PR_RESERV_CONFLICT ;
570                 }
571                 if (!rollback && (status == MPATH_PR_SUCCESS)){
572                         status = thread[i].param.status;
573                 }
574         }
575         if (rollback && ((rq_servact == MPATH_PROUT_REG_SA) && sa_key != 0 )){
576                 condlog (3, "%s: ERROR: initiating pr out rollback", mpp->wwid);
577                 for( i=0 ; i < count ; i++){
578                         if(thread[i].param.status == MPATH_PR_SUCCESS) {
579                                 memcpy(&thread[i].param.paramp->key, &thread[i].param.paramp->sa_key, 8);
580                                 memset(&thread[i].param.paramp->sa_key, 0, 8);
581                                 thread[i].param.status = MPATH_PR_SUCCESS;
582                                 rc = pthread_create(&thread[i].id, &attr, mpath_prout_pthread_fn,
583                                                 (void *)(&thread[i].param));
584                                 if (rc){
585                                         condlog (0, "%s: failed to create thread for rollback. %d",  mpp->wwid, rc);
586                                         thread[i].param.status = MPATH_PR_THREAD_ERROR;
587                                 }
588                         } else
589                                 thread[i].param.status = MPATH_PR_SKIP;
590                 }
591                 for(i=0; i < count ; i++){
592                         if (thread[i].param.status != MPATH_PR_SKIP &&
593                             thread[i].param.status != MPATH_PR_THREAD_ERROR) {
594                                 rc = pthread_join(thread[i].id, NULL);
595                                 if (rc){
596                                         condlog (3, "%s: failed to join thread while rolling back %d",
597                                                  mpp->wwid, i);
598                                 }
599                         }
600                 }
601         }
602
603         pthread_attr_destroy(&attr);
604         return (status);
605 }
606
607 void * mpath_prout_pthread_fn(void *p)
608 {
609         int ret;
610         struct prout_param * param = (struct prout_param *)p;
611
612         ret = prout_do_scsi_ioctl( param->dev,param->rq_servact, param->rq_scope,
613                         param->rq_type, param->paramp, param->noisy);
614         param->status = ret;
615         pthread_exit(NULL);
616 }
617
618 int mpath_prout_common(struct multipath *mpp,int rq_servact, int rq_scope,
619         unsigned int rq_type, struct prout_param_descriptor* paramp, int noisy)
620 {
621         int i,j, ret;
622         struct pathgroup *pgp = NULL;
623         struct path *pp = NULL;
624
625         vector_foreach_slot (mpp->pg, pgp, j){
626                 vector_foreach_slot (pgp->paths, pp, i){
627                         if (!((pp->state == PATH_UP) || (pp->state == PATH_GHOST))){
628                                 condlog (1, "%s: %s path not up. Skip",
629                                          mpp->wwid, pp->dev);
630                                 continue;
631                         }
632
633                         condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
634                         ret = send_prout_activepath(pp->dev, rq_servact,
635                                                     rq_scope, rq_type,
636                                                     paramp, noisy);
637                         return ret ;
638                 }
639         }
640         return MPATH_PR_SUCCESS;
641 }
642
643 int send_prout_activepath(char * dev, int rq_servact, int rq_scope,
644         unsigned int rq_type, struct prout_param_descriptor * paramp, int noisy)
645 {
646         struct prout_param param;
647         param.rq_servact = rq_servact;
648         param.rq_scope  = rq_scope;
649         param.rq_type   = rq_type;
650         param.paramp    = paramp;
651         param.noisy = noisy;
652         param.status = -1;
653
654         pthread_t thread;
655         pthread_attr_t attr;
656         int rc;
657
658         memset(&thread, 0, sizeof(thread));
659         strncpy(param.dev, dev, FILE_NAME_SIZE - 1);
660         /* Initialize and set thread joinable attribute */
661         pthread_attr_init(&attr);
662         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
663
664         rc = pthread_create(&thread, &attr, mpath_prout_pthread_fn, (void *)(&param));
665         if (rc){
666                 condlog (3, "%s: failed to create thread %d", dev, rc);
667                 return MPATH_PR_THREAD_ERROR;
668         }
669         /* Free attribute and wait for the other threads */
670         pthread_attr_destroy(&attr);
671         rc = pthread_join(thread, NULL);
672
673         return (param.status);
674 }
675
676 int mpath_prout_rel(struct multipath *mpp,int rq_servact, int rq_scope,
677         unsigned int rq_type, struct prout_param_descriptor * paramp, int noisy)
678 {
679         int i, j;
680         int num = 0;
681         struct pathgroup *pgp = NULL;
682         struct path *pp = NULL;
683         int active_pathcount = 0;
684         pthread_attr_t attr;
685         int rc, found = 0;
686         int count = 0;
687         int status = MPATH_PR_SUCCESS;
688         struct prin_resp resp;
689         struct prout_param_descriptor *pamp;
690         struct prin_resp *pr_buff;
691         int length;
692         struct transportid *pptr;
693
694         if (!mpp)
695                 return MPATH_PR_DMMP_ERROR;
696
697         active_pathcount = pathcount (mpp, PATH_UP) + pathcount (mpp, PATH_GHOST);
698
699         struct threadinfo thread[active_pathcount];
700         memset(thread, 0, sizeof(thread));
701         for (i = 0; i < active_pathcount; i++){
702                 thread[i].param.rq_servact = rq_servact;
703                 thread[i].param.rq_scope = rq_scope;
704                 thread[i].param.rq_type = rq_type;
705                 thread[i].param.paramp = paramp;
706                 thread[i].param.noisy = noisy;
707                 thread[i].param.status = MPATH_PR_SKIP;
708
709                 condlog (3, " path count = %d", i);
710                 condlog (3, "rq_servact=%d ", thread[i].param.rq_servact);
711                 condlog (3, "rq_scope=%d ", thread[i].param.rq_scope);
712                 condlog (3, "rq_type=%d ", thread[i].param.rq_type);
713                 condlog (3, "noisy=%d ", thread[i].param.noisy);
714                 condlog (3, "status=%d ", thread[i].param.status);
715         }
716
717         pthread_attr_init (&attr);
718         pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);
719
720         vector_foreach_slot (mpp->pg, pgp, j){
721                 vector_foreach_slot (pgp->paths, pp, i){
722                         if (!((pp->state == PATH_UP) || (pp->state == PATH_GHOST))){
723                                 condlog (1, "%s: %s path not up.", mpp->wwid, pp->dev);
724                                 continue;
725                         }
726
727                         strncpy(thread[count].param.dev, pp->dev,
728                                 FILE_NAME_SIZE - 1);
729                         condlog (3, "%s: sending pr out command to %s", mpp->wwid, pp->dev);
730                         rc = pthread_create (&thread[count].id, &attr, mpath_prout_pthread_fn,
731                                         (void *) (&thread[count].param));
732                         if (rc) {
733                                 condlog (0, "%s: failed to create thread. %d",  mpp->wwid, rc);
734                                 thread[count].param.status = MPATH_PR_THREAD_ERROR;
735                         }
736                         count = count + 1;
737                 }
738         }
739         pthread_attr_destroy (&attr);
740         for (i = 0; i < count; i++){
741                 if (thread[i].param.status != MPATH_PR_THREAD_ERROR) {
742                         rc = pthread_join (thread[i].id, NULL);
743                         if (rc){
744                                 condlog (1, "%s: failed to join thread.  %d",  mpp->wwid,  rc);
745                         }
746                 }
747         }
748
749         for (i = 0; i < count; i++){
750                 /*  check thread status here and return the status */
751
752                 if (thread[i].param.status == MPATH_PR_RESERV_CONFLICT)
753                         status = MPATH_PR_RESERV_CONFLICT;
754                 else if (status == MPATH_PR_SUCCESS
755                                 && thread[i].param.status != MPATH_PR_RESERV_CONFLICT)
756                         status = thread[i].param.status;
757         }
758
759         status = mpath_prin_activepath (mpp, MPATH_PRIN_RRES_SA, &resp, noisy);
760         if (status != MPATH_PR_SUCCESS){
761                 condlog (0, "%s: pr in read reservation command failed.", mpp->wwid);
762                 return MPATH_PR_OTHER;
763         }
764
765         num = resp.prin_descriptor.prin_readresv.additional_length / 8;
766         if (num == 0){
767                 condlog (2, "%s: Path holding reservation is released.", mpp->wwid);
768                 return MPATH_PR_SUCCESS;
769         }
770         condlog (2, "%s: Path holding reservation is not avialable.", mpp->wwid);
771
772         pr_buff =  mpath_alloc_prin_response(MPATH_PRIN_RFSTAT_SA);
773         if (!pr_buff){
774                 condlog (0, "%s: failed to  alloc pr in response buffer.", mpp->wwid);
775                 return MPATH_PR_OTHER;
776         }
777
778         status = mpath_prin_activepath (mpp, MPATH_PRIN_RFSTAT_SA, pr_buff, noisy);
779
780         if (status != MPATH_PR_SUCCESS){
781                 condlog (0,  "%s: pr in read full status command failed.",  mpp->wwid);
782                 goto out;
783         }
784
785         num = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
786         if (0 == num){
787                 goto out;
788         }
789         length = sizeof (struct prout_param_descriptor) + (sizeof (struct transportid *));
790
791         pamp = (struct prout_param_descriptor *)malloc (length);
792         if (!pamp){
793                 condlog (0, "%s: failed to alloc pr out parameter.", mpp->wwid);
794                 goto out1;
795         }
796
797         memset(pamp, 0, length);
798
799         pamp->trnptid_list[0] = (struct transportid *) malloc (sizeof (struct transportid));
800         if (!pamp->trnptid_list[0]){
801                 condlog (0, "%s: failed to alloc pr out transportid.", mpp->wwid);
802                 goto out1;
803         }
804
805         if (get_be64(mpp->reservation_key)){
806                 memcpy (pamp->key, &mpp->reservation_key, 8);
807                 condlog (3, "%s: reservation key set.", mpp->wwid);
808         }
809
810         status = mpath_prout_common (mpp, MPATH_PROUT_CLEAR_SA,
811                                      rq_scope, rq_type, pamp, noisy);
812
813         if (status) {
814                 condlog(0, "%s: failed to send CLEAR_SA", mpp->wwid);
815                 goto out1;
816         }
817
818         pamp->num_transportid = 1;
819         pptr=pamp->trnptid_list[0];
820
821         for (i = 0; i < num; i++){
822                 if (get_be64(mpp->reservation_key) &&
823                         memcmp(pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key,
824                                &mpp->reservation_key, 8)){
825                         /*register with tarnsport id*/
826                         memset(pamp, 0, length);
827                         pamp->trnptid_list[0] = pptr;
828                         memset (pamp->trnptid_list[0], 0, sizeof (struct transportid));
829                         memcpy (pamp->sa_key,
830                                         pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key, 8);
831                         pamp->sa_flags = MPATH_F_SPEC_I_PT_MASK;
832                         pamp->num_transportid = 1;
833
834                         memcpy (pamp->trnptid_list[0],
835                                         &pr_buff->prin_descriptor.prin_readfd.descriptors[i]->trnptid,
836                                         sizeof (struct transportid));
837                         status = mpath_prout_common (mpp, MPATH_PROUT_REG_SA, 0, rq_type,
838                                         pamp, noisy);
839
840                         pamp->sa_flags = 0;
841                         memcpy (pamp->key, pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key, 8);
842                         memset (pamp->sa_key, 0, 8);
843                         pamp->num_transportid = 0;
844                         status = mpath_prout_common (mpp, MPATH_PROUT_REG_SA, 0, rq_type,
845                                         pamp, noisy);
846                 }
847                 else
848                 {
849                         if (get_be64(mpp->reservation_key))
850                                 found = 1;
851                 }
852
853
854         }
855
856         if (found){
857                 memset (pamp, 0, length);
858                 memcpy (pamp->sa_key, &mpp->reservation_key, 8);
859                 memset (pamp->key, 0, 8);
860                 status = mpath_prout_reg(mpp, MPATH_PROUT_REG_SA, rq_scope, rq_type, pamp, noisy);
861         }
862
863
864         free(pptr);
865 out1:
866         free (pamp);
867 out:
868         free (pr_buff);
869         return (status);
870 }
871
872 void * mpath_alloc_prin_response(int prin_sa)
873 {
874         void * ptr = NULL;
875         int size=0;
876         switch (prin_sa)
877         {
878                 case MPATH_PRIN_RKEY_SA:
879                         size = sizeof(struct prin_readdescr);
880                         break;
881                 case MPATH_PRIN_RRES_SA:
882                         size = sizeof(struct prin_resvdescr);
883                         break;
884                 case MPATH_PRIN_RCAP_SA:
885                         size=sizeof(struct prin_capdescr);
886                         break;
887                 case MPATH_PRIN_RFSTAT_SA:
888                         size = sizeof(struct print_fulldescr_list) +
889                                 sizeof(struct prin_fulldescr *)*MPATH_MX_TIDS;
890                         break;
891         }
892         if (size > 0)
893         {
894                 ptr = calloc(size, 1);
895         }
896         return ptr;
897 }
898
899 int update_map_pr(struct multipath *mpp)
900 {
901         int noisy=0;
902         struct prin_resp *resp;
903         int i, ret, isFound;
904
905         if (!get_be64(mpp->reservation_key))
906         {
907                 /* Nothing to do. Assuming pr mgmt feature is disabled*/
908                 condlog(3, "%s: reservation_key not set in multipath.conf", mpp->alias);
909                 return MPATH_PR_SUCCESS;
910         }
911
912         resp = mpath_alloc_prin_response(MPATH_PRIN_RKEY_SA);
913         if (!resp)
914         {
915                 condlog(0,"%s : failed to alloc resp in update_map_pr", mpp->alias);
916                 return MPATH_PR_OTHER;
917         }
918         ret = mpath_prin_activepath(mpp, MPATH_PRIN_RKEY_SA, resp, noisy);
919
920         if (ret != MPATH_PR_SUCCESS )
921         {
922                 condlog(0,"%s : pr in read keys service action failed Error=%d", mpp->alias, ret);
923                 free(resp);
924                 return  ret;
925         }
926
927         if (resp->prin_descriptor.prin_readkeys.additional_length == 0 )
928         {
929                 condlog(3,"%s: No key found. Device may not be registered. ", mpp->alias);
930                 free(resp);
931                 return MPATH_PR_SUCCESS;
932         }
933
934         condlog(2, "%s: Multipath  reservation_key: 0x%" PRIx64 " ", mpp->alias,
935                 get_be64(mpp->reservation_key));
936
937         isFound =0;
938         for (i = 0; i < resp->prin_descriptor.prin_readkeys.additional_length/8; i++ )
939         {
940                 condlog(2, "%s: PR IN READKEYS[%d]  reservation key:", mpp->alias, i);
941                 dumpHex((char *)&resp->prin_descriptor.prin_readkeys.key_list[i*8], 8 , 1);
942
943                 if (!memcmp(&mpp->reservation_key, &resp->prin_descriptor.prin_readkeys.key_list[i*8], 8))
944                 {
945                         condlog(2, "%s: reservation key found in pr in readkeys response", mpp->alias);
946                         isFound =1;
947                 }
948         }
949
950         if (isFound)
951         {
952                 mpp->prflag = 1;
953                 condlog(2, "%s: prflag flag set.", mpp->alias );
954         }
955
956         free(resp);
957         return MPATH_PR_SUCCESS;
958 }