mpathpersist: no need to treat error close() as fatal
[multipath-tools/.git] / mpathpersist / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <fcntl.h>
6 #include "checkers.h"
7 #include "vector.h"
8 #include "config.h"
9 #include "structs.h"
10 #include <getopt.h>
11 #include <libudev.h>
12 #include "mpath_persist.h"
13 #include "main.h"
14 #include "debug.h"
15 #include <pthread.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <errno.h>
19 #include "version.h"
20
21 static const char * pr_type_strs[] = {
22         "obsolete [0]",
23         "Write Exclusive",
24         "obsolete [2]",
25         "Exclusive Access",
26         "obsolete [4]",
27         "Write Exclusive, registrants only",
28         "Exclusive Access, registrants only",
29         "Write Exclusive, all registrants",
30         "Exclusive Access, all registrants",
31         "obsolete [9]", "obsolete [0xa]", "obsolete [0xb]", "obsolete [0xc]",
32         "obsolete [0xd]", "obsolete [0xe]", "obsolete [0xf]",
33 };
34
35 int get_transportids_length(unsigned char * transportid_arr, int max_transportid, int num_transportids);
36 void mpath_print_buf_readcap(struct prin_resp *pr_buff);
37 void mpath_print_buf_readfullstat(struct prin_resp *pr_buff);
38 void mpath_print_buf_readresv(struct prin_resp *pr_buff);
39 void mpath_print_buf_readkeys(struct prin_resp *pr_buff);
40 void dumpHex(const char* str, int len, int no_ascii);
41 void * mpath_alloc_prin_response(int prin_sa);
42 void mpath_print_transport_id(struct prin_fulldescr *fdesc);
43 int construct_transportid(const char * inp, struct transportid transid[], int num_transportids);
44
45 int logsink;
46 struct config *multipath_conf;
47
48 struct config *get_multipath_config(void)
49 {
50         return multipath_conf;
51 }
52
53 void put_multipath_config(void * arg)
54 {
55         /* Noop for now */
56 }
57
58 void rcu_register_thread_memb(void) {}
59
60 void rcu_unregister_thread_memb(void) {}
61
62 struct udev *udev;
63
64 static int verbose, loglevel, noisy;
65
66 static int handle_args(int argc, char * argv[], int line);
67
68 static int do_batch_file(const char *batch_fn)
69 {
70         char command[] = "mpathpersist";
71         const int ARGV_CHUNK = 2;
72         const char delims[] = " \t\n";
73         size_t len = 0;
74         char *line = NULL;
75         ssize_t n;
76         int nline = 0;
77         int argl = ARGV_CHUNK;
78         FILE *fl;
79         char **argv = calloc(argl, sizeof(*argv));
80         int ret = MPATH_PR_SUCCESS;
81
82         if (argv == NULL)
83                 return MPATH_PR_OTHER;
84
85         fl = fopen(batch_fn, "r");
86         if (fl == NULL) {
87                 fprintf(stderr, "unable to open %s: %s\n",
88                         batch_fn, strerror(errno));
89                 free(argv);
90                 return MPATH_PR_SYNTAX_ERROR;
91         } else {
92                 if (verbose >= 2)
93                         fprintf(stderr, "running batch file %s\n",
94                                 batch_fn);
95         }
96
97         while ((n = getline(&line, &len, fl)) != -1) {
98                 char *_token, *token;
99                 int argc = 0;
100                 int rv;
101
102                 nline++;
103                 argv[argc++] = command;
104
105                 if (line[n-1] == '\n')
106                         line[n-1] = '\0';
107                 if (verbose >= 3)
108                         fprintf(stderr, "processing line %d: %s\n",
109                                 nline, line);
110
111                 for (token = strtok_r(line, delims, &_token);
112                      token != NULL && *token != '#';
113                      token = strtok_r(NULL, delims, &_token)) {
114
115                         if (argc >= argl) {
116                                 int argn = argl + ARGV_CHUNK;
117                                 char **tmp;
118
119                                 tmp = realloc(argv, argn * sizeof(*argv));
120                                 if (tmp == NULL)
121                                         break;
122                                 argv = tmp;
123                                 argl = argn;
124                         }
125
126                         if (argc == 1 && !strcmp(token, command))
127                                 continue;
128
129                         argv[argc++] = token;
130                 }
131
132                 if (argc <= 1)
133                         continue;
134
135                 if (verbose >= 2) {
136                         int i;
137
138                         fprintf(stderr, "## file %s line %d:", batch_fn, nline);
139                         for (i = 0; i < argc; i++)
140                                 fprintf(stderr, " %s", argv[i]);
141                         fprintf(stderr, "\n");
142                 }
143
144                 optind = 0;
145                 rv = handle_args(argc, argv, nline);
146                 if (rv != MPATH_PR_SUCCESS)
147                         ret = rv;
148         }
149
150         fclose(fl);
151         free(argv);
152         free(line);
153         return ret;
154 }
155
156 static int handle_args(int argc, char * argv[], int nline)
157 {
158         int fd, c;
159         const char *device_name = NULL;
160         int num_prin_sa = 0;
161         int num_prout_sa = 0;
162         int num_prout_param = 0;
163         int prin_flag = 0;
164         int prout_flag = 0;
165         int ret = 0;
166         int hex = 0;
167         uint64_t param_sark = 0;
168         unsigned int prout_type = 0;
169         int param_alltgpt = 0;
170         int param_aptpl = 0;
171         uint64_t param_rk = 0;
172         unsigned int param_rtp = 0;
173         int num_transportids = 0;
174         struct transportid transportids[MPATH_MX_TIDS];
175         int prout = 1;
176         int prin = 1;
177         int prin_sa = -1;
178         int prout_sa = -1;
179         int num_transport =0;
180         char *batch_fn = NULL;
181         void *resp = NULL;
182         struct transportid * tmp;
183
184         memset(transportids, 0, MPATH_MX_TIDS * sizeof(struct transportid));
185
186         while (1)
187         {
188                 int option_index = 0;
189
190                 c = getopt_long (argc, argv, "v:Cd:hHioYZK:S:PAT:skrGILcRX:l:f:",
191                                 long_options, &option_index);
192                 if (c == -1)
193                         break;
194
195                 switch (c)
196                 {
197                         case 'f':
198                                 if (nline != 0) {
199                                         fprintf(stderr,
200                                                 "ERROR: -f option not allowed in batch file\n");
201                                         ret = MPATH_PR_SYNTAX_ERROR;
202                                         goto out;
203                                 }
204                                 if (batch_fn != NULL) {
205                                         fprintf(stderr,
206                                                 "ERROR: -f option can be used at most once\n");
207                                         ret = MPATH_PR_SYNTAX_ERROR;
208                                         goto out;
209                                 }
210                                 batch_fn = strdup(optarg);
211                                 break;
212                         case 'v':
213                                 if (nline == 0 && 1 != sscanf (optarg, "%d", &loglevel))
214                                 {
215                                         fprintf (stderr, "bad argument to '--verbose'\n");
216                                         return MPATH_PR_SYNTAX_ERROR;
217                                 }
218                                 break;
219
220                         case 'C':
221                                 prout_sa = MPATH_PROUT_CLEAR_SA;
222                                 ++num_prout_sa;
223                                 break;
224
225                         case 'd':
226                                 device_name = optarg;
227                                 break;
228
229                         case 'h':
230                                 usage ();
231                                 return 0;
232
233                         case 'H':
234                                 hex=1;
235                                 break;
236
237                         case 'i':
238                                 prin_flag = 1;
239                                 break;
240
241                         case 'o':
242                                 prout_flag = 1;
243                                 break;
244
245                         case 'Y':
246                                 param_alltgpt = 1;
247                                 ++num_prout_param;
248                                 break;
249                         case 'Z':
250                                 param_aptpl = 1;
251                                 ++num_prout_param;
252                                 break;
253                         case 'K':
254                                 if (1 != sscanf (optarg, "%" SCNx64 "", &param_rk))
255                                 {
256                                         fprintf (stderr, "bad argument to '--param-rk'\n");
257                                         return MPATH_PR_SYNTAX_ERROR;
258                                 }
259                                 ++num_prout_param;
260                                 break;
261
262                         case 'S':
263                                 if (1 != sscanf (optarg, "%" SCNx64 "", &param_sark))
264                                 {
265                                         fprintf (stderr, "bad argument to '--param-sark'\n");
266                                         return MPATH_PR_SYNTAX_ERROR;
267                                 }
268                                 ++num_prout_param;
269                                 break;
270
271                         case 'P':
272                                 prout_sa = MPATH_PROUT_PREE_SA;
273                                 ++num_prout_sa;
274                                 break;
275
276                         case 'A':
277                                 prout_sa = MPATH_PROUT_PREE_AB_SA;
278                                 ++num_prout_sa;
279                                 break;
280
281                         case 'T':
282                                 if (1 != sscanf (optarg, "%x", &prout_type))
283                                 {
284                                         fprintf (stderr, "bad argument to '--prout-type'\n");
285                                         return MPATH_PR_SYNTAX_ERROR;
286                                 }
287                                 ++num_prout_param;
288                                 break;
289
290                         case 's':
291                                 prin_sa = MPATH_PRIN_RFSTAT_SA;
292                                 ++num_prin_sa;
293                                 break;
294
295                         case 'k':
296                                 prin_sa = MPATH_PRIN_RKEY_SA;
297                                 ++num_prin_sa;
298                                 break;
299
300                         case 'r':
301                                 prin_sa = MPATH_PRIN_RRES_SA;
302                                 ++num_prin_sa;
303                                 break;
304
305                         case 'G':
306                                 prout_sa = MPATH_PROUT_REG_SA;
307                                 ++num_prout_sa;
308                                 break;
309
310                         case 'I':
311                                 prout_sa = MPATH_PROUT_REG_IGN_SA;
312                                 ++num_prout_sa;
313                                 break;
314
315                         case 'L':
316                                 prout_sa = MPATH_PROUT_REL_SA;
317                                 ++num_prout_sa;
318                                 break;
319
320                         case 'c':
321                                 prin_sa = MPATH_PRIN_RCAP_SA;
322                                 ++num_prin_sa;
323                                 break;
324
325                         case 'R':
326                                 prout_sa = MPATH_PROUT_RES_SA;
327                                 ++num_prout_sa;
328                                 break;
329
330                         case 'X':
331                                 if (0 != construct_transportid(optarg, transportids, num_transport)) {
332                                         fprintf(stderr, "bad argument to '--transport-id'\n");
333                                         return MPATH_PR_SYNTAX_ERROR;
334                                 }
335
336                                 ++num_transport;
337                                 break;
338
339                         case 'l':
340                                 if (1 != sscanf(optarg, "%u", &mpath_mx_alloc_len)) {
341                                         fprintf(stderr, "bad argument to '--alloc-length'\n");
342                                         return MPATH_PR_SYNTAX_ERROR;
343                                 } else if (MPATH_MAX_PARAM_LEN < mpath_mx_alloc_len) {
344                                         fprintf(stderr, "'--alloc-length' argument exceeds maximum"
345                                                         " limit(%d)\n", MPATH_MAX_PARAM_LEN);
346                                         return MPATH_PR_SYNTAX_ERROR;
347                                 }
348                                 break;
349
350                         default:
351                                 fprintf(stderr, "unrecognised switch " "code 0x%x ??\n", c);
352                                 ret = MPATH_PR_SYNTAX_ERROR;
353                                 goto out;
354                 }
355         }
356
357         if (optind < argc)
358         {
359
360                 if (NULL == device_name)
361                 {
362                         device_name = argv[optind];
363                         ++optind;
364                 }
365                 if (optind < argc)
366                 {
367                         for (; optind < argc; ++optind)
368                                 fprintf (stderr, "Unexpected extra argument: %s\n", argv[optind]);
369                         ret = MPATH_PR_SYNTAX_ERROR;
370                         goto out;
371                 }
372         }
373
374         if (nline == 0) {
375                 /* set verbosity */
376                 noisy = (loglevel >= 3) ? 1 : hex;
377                 verbose = (loglevel >= 3)? 3: loglevel;
378         }
379
380         if ((prout_flag + prin_flag) == 0 && batch_fn == NULL)
381         {
382                 fprintf (stderr, "choose either '--in' or '--out' \n");
383                 ret = MPATH_PR_SYNTAX_ERROR;
384                 goto out;
385         }
386         if ((prout_flag + prin_flag) > 1)
387         {
388                 fprintf (stderr, "choose either '--in' or '--out' \n");
389                 ret = MPATH_PR_SYNTAX_ERROR;
390                 goto out;
391         }
392         else if (prout_flag)
393         {                               /* syntax check on PROUT arguments */
394                 prin = 0;
395                 if ((1 != num_prout_sa) || (0 != num_prin_sa))
396                 {
397                         fprintf (stderr, " For Persistent Reserve Out only one "
398                                         "appropriate\n service action must be "
399                                         "chosen \n");
400                         ret = MPATH_PR_SYNTAX_ERROR;
401                         goto out;
402                 }
403         }
404         else if (prin_flag)
405         {                               /* syntax check on PRIN arguments */
406                 prout = 0;
407                 if (num_prout_sa > 0)
408                 {
409                         fprintf (stderr, " When a service action for Persistent "
410                                         "Reserve Out is chosen the\n"
411                                         " '--out' option must be given \n");
412                         ret = MPATH_PR_SYNTAX_ERROR;
413                         goto out;
414                 }
415                 if (0 == num_prin_sa)
416                 {
417                         fprintf (stderr,
418                                         " No service action given for Persistent Reserve IN\n");
419                         ret = MPATH_PR_SYNTAX_ERROR;
420                 }
421                 else if (num_prin_sa > 1)
422                 {
423                         fprintf (stderr, " Too many service actions given; choose "
424                                         "one only\n");
425                         ret = MPATH_PR_SYNTAX_ERROR;
426                 }
427         }
428         else
429         {
430                 if (batch_fn == NULL)
431                         ret = MPATH_PR_SYNTAX_ERROR;
432                 goto out;
433         }
434
435         if ((param_rtp) && (MPATH_PROUT_REG_MOV_SA != prout_sa))
436         {
437                 fprintf (stderr, " --relative-target-port"
438                                 " only useful with --register-move\n");
439                 ret = MPATH_PR_SYNTAX_ERROR;
440                 goto out;
441         }
442
443         if (((MPATH_PROUT_RES_SA == prout_sa) ||
444                                 (MPATH_PROUT_REL_SA == prout_sa) ||
445                                 (MPATH_PROUT_PREE_SA == prout_sa) ||
446                                 (MPATH_PROUT_PREE_AB_SA == prout_sa)) &&
447                         (0 == prout_type)) {
448                 fprintf(stderr, "Warning: --prout-type probably needs to be "
449                                 "given\n");
450         }
451         if ((verbose > 2) && num_transportids)
452         {
453                 fprintf (stderr, "number of tranport-ids decoded from "
454                                 "command line : %d\n", num_transportids);
455         }
456
457         if (device_name == NULL)
458         {
459                 fprintf (stderr, "No device name given \n");
460                 ret = MPATH_PR_SYNTAX_ERROR;
461                 goto out;
462         }
463
464         /* open device */
465         if ((fd = open (device_name, O_RDONLY)) < 0)
466         {
467                 fprintf (stderr, "%s: error opening file (rw) fd=%d\n",
468                                 device_name, fd);
469                 ret = MPATH_PR_FILE_ERROR;
470                 goto out;
471         }
472
473
474         if (prin)
475         {
476                 resp = mpath_alloc_prin_response(prin_sa);
477                 if (!resp)
478                 {
479                         fprintf (stderr, "failed to allocate PRIN response buffer\n");
480                         ret = MPATH_PR_OTHER;
481                         goto out;
482                 }
483
484                 ret = mpath_persistent_reserve_in (fd, prin_sa, resp, noisy, verbose);
485                 if (ret != MPATH_PR_SUCCESS )
486                 {
487                         fprintf (stderr, "Persistent Reserve IN command failed\n");
488                         goto out;
489                 }
490
491                 switch(prin_sa)
492                 {
493                         case MPATH_PRIN_RKEY_SA:
494                                 mpath_print_buf_readkeys(resp);
495                                 break;
496                         case MPATH_PRIN_RRES_SA:
497                                 mpath_print_buf_readresv(resp);
498                                 break;
499                         case MPATH_PRIN_RCAP_SA:
500                                 mpath_print_buf_readcap(resp);
501                                 break;
502                         case MPATH_PRIN_RFSTAT_SA:
503                                 mpath_print_buf_readfullstat(resp);
504                                 break;
505                 }
506                 free(resp);
507         }
508         else if (prout)
509         {
510                 int j;
511                 struct prout_param_descriptor *paramp;
512
513                 paramp= malloc(sizeof(struct prout_param_descriptor) + (sizeof(struct transportid *)*(MPATH_MX_TIDS )));
514
515                 memset(paramp, 0, sizeof(struct prout_param_descriptor) + (sizeof(struct transportid *)*(MPATH_MX_TIDS)));
516
517                 for (j = 7; j >= 0; --j) {
518                         paramp->key[j] = (param_rk & 0xff);
519                         param_rk >>= 8;
520                 }
521
522                 for (j = 7; j >= 0; --j) {
523                         paramp->sa_key[j] = (param_sark & 0xff);
524                         param_sark >>= 8;
525                 }
526
527                 if (param_alltgpt)
528                         paramp->sa_flags |= MPATH_F_ALL_TG_PT_MASK;
529                 if (param_aptpl)
530                         paramp->sa_flags |= MPATH_F_APTPL_MASK;
531
532                 if (num_transport)
533                 {
534                         paramp->sa_flags |= MPATH_F_SPEC_I_PT_MASK;
535                         paramp->num_transportid = num_transport;
536                         for (j = 0 ; j < num_transport; j++)
537                         {
538                                 paramp->trnptid_list[j] = (struct transportid *)malloc(sizeof(struct transportid));
539                                 memcpy(paramp->trnptid_list[j], &transportids[j],sizeof(struct transportid));
540                         }
541                 }
542
543                 /* PROUT commands other than 'register and move' */
544                 ret = mpath_persistent_reserve_out (fd, prout_sa, 0, prout_type,
545                                 paramp, noisy, verbose);
546                 for (j = 0 ; j < num_transport; j++)
547                 {
548                         tmp = paramp->trnptid_list[j];
549                         free(tmp);
550                 }
551                 free(paramp);
552         }
553
554         if (ret != MPATH_PR_SUCCESS)
555         {
556                 switch(ret)
557                 {
558                         case MPATH_PR_SENSE_UNIT_ATTENTION:
559                                 printf("persistent reserve out: scsi status: Unit Attention\n");
560                                 break;
561                         case MPATH_PR_RESERV_CONFLICT:
562                                 printf("persistent reserve out: scsi status: Reservation Conflict\n");
563                                 break;
564                 }
565                 printf("PR out: command failed\n");
566         }
567
568         close (fd);
569
570 out :
571         if (ret == MPATH_PR_SYNTAX_ERROR) {
572                 free(batch_fn);
573                 if (nline == 0)
574                         usage();
575                 else
576                         fprintf(stderr, "syntax error on line %d in batch file\n",
577                                 nline);
578         } else if (batch_fn != NULL) {
579                 int rv = do_batch_file(batch_fn);
580
581                 free(batch_fn);
582                 ret = ret == 0 ? rv : ret;
583         }
584         return (ret >= 0) ? ret : MPATH_PR_OTHER;
585 }
586
587 int main(int argc, char *argv[])
588 {
589         int ret;
590
591         if (optind == argc)
592         {
593
594                 fprintf (stderr, "No parameter used\n");
595                 usage ();
596                 exit (1);
597         }
598
599         if (getuid () != 0)
600         {
601                 fprintf (stderr, "need to be root\n");
602                 exit (1);
603         }
604
605         udev = udev_new();
606         multipath_conf = mpath_lib_init();
607         if(!multipath_conf) {
608                 udev_unref(udev);
609                 exit(1);
610         }
611
612         ret = handle_args(argc, argv, 0);
613
614         mpath_lib_exit(multipath_conf);
615         udev_unref(udev);
616
617         return (ret >= 0) ? ret : MPATH_PR_OTHER;
618 }
619
620 int
621 get_transportids_length(unsigned char * transportid_arr, int max_transportid, int num_transportids)
622 {
623         int compact_len = 0;
624         unsigned char * ucp = transportid_arr;
625         int k, off, protocol_id, len;
626         for (k = 0, off = 0; ((k < num_transportids) && (k < max_transportid));
627                         ++k, off += MPATH_MX_TID_LEN) {
628                 protocol_id = ucp[off] & 0xf;
629                 if (5 == protocol_id) {
630                         len = (ucp[off + 2] << 8) + ucp[off + 3] + 4;
631                         if (len < 24)
632                                 len = 24;
633                         if (off > compact_len)
634                                 memmove(ucp + compact_len, ucp + off, len);
635                         compact_len += len;
636
637                 } else {
638                         if (off > compact_len)
639                                 memmove(ucp + compact_len, ucp + off, 24);
640                         compact_len += 24;
641                 }
642         }
643
644         return compact_len;
645 }
646
647 void mpath_print_buf_readkeys( struct prin_resp *pr_buff)
648 {
649         int i,j,k, num;
650         unsigned char *keyp;
651         uint64_t prkey;
652         printf("  PR generation=0x%x, ", pr_buff->prin_descriptor.prin_readkeys.prgeneration);
653
654         num = pr_buff->prin_descriptor.prin_readkeys.additional_length / 8;
655         if (0 == num) {
656                 printf("        0 registered reservation key.\n");
657                 return;
658         }
659         else if (1 == num)
660                 printf("        1 registered reservation key follows:\n");
661         else
662                 printf("        %d registered reservation keys follow:\n", num);
663
664
665         keyp = (unsigned char *)&pr_buff->prin_descriptor.prin_readkeys.key_list[0];
666         for (i = 0; i < num ; i++)
667         {
668                 prkey = 0;
669                 for (j = 0; j < 8; ++j) {
670
671                         if (j > 0)
672                                 prkey <<= 8;
673                         prkey |= keyp[j];
674                 }
675                 printf("    0x%" PRIx64 "\n", prkey);
676                 k=8*i+j;
677                 keyp = (unsigned char *)&pr_buff->prin_descriptor.prin_readkeys.key_list[k];
678         }
679 }
680
681 void mpath_print_buf_readresv( struct prin_resp *pr_buff)
682 {
683         int j, num, scope=0, type=0;
684         unsigned char *keyp;
685         uint64_t prkey;
686
687         num = pr_buff->prin_descriptor.prin_readresv.additional_length / 8;
688         if (0 == num)
689         {
690                 printf("  PR generation=0x%x, there is NO reservation held \n", pr_buff->prin_descriptor.prin_readresv.prgeneration);
691                 return ;
692         }
693         else
694                 printf("  PR generation=0x%x, Reservation follows:\n", pr_buff->prin_descriptor.prin_readresv.prgeneration);
695         keyp = (unsigned  char *)&pr_buff->prin_descriptor.prin_readkeys.key_list[0];
696         prkey = 0;
697         for (j = 0; j < 8; ++j) {
698                 if (j > 0)
699                         prkey <<= 8;
700                 prkey |= keyp[j];
701         }
702
703         printf("   Key = 0x%" PRIx64 "\n", prkey);
704
705         scope = (pr_buff->prin_descriptor.prin_readresv.scope_type >> 4) &  0x0f;
706         type = pr_buff->prin_descriptor.prin_readresv.scope_type & 0x0f;
707
708         if (scope == 0)
709                 printf("  scope = LU_SCOPE, type = %s", pr_type_strs[type]);
710         else
711                 printf("  scope = %d, type = %s", scope, pr_type_strs[type]);
712
713         printf("\n");
714
715 }
716
717 void mpath_print_buf_readcap( struct prin_resp *pr_buff)
718 {
719         if ( pr_buff->prin_descriptor.prin_readcap.length <= 2 ) {
720                 fprintf(stderr, "Unexpected response for PRIN Report "
721                                 "Capabilities\n");
722                 return; //MALFORMED;
723         }
724
725         printf("Report capabilities response:\n");
726
727         printf("  Compatible Reservation Handling(CRH): %d\n", !!(pr_buff->prin_descriptor.prin_readcap.flags[0] & 0x10));
728         printf("  Specify Initiator Ports Capable(SIP_C): %d\n",!!(pr_buff->prin_descriptor.prin_readcap.flags[0] & 0x8));
729         printf("  All Target Ports Capable(ATP_C): %d\n",!!(pr_buff->prin_descriptor.prin_readcap.flags[0] & 0x4 ));
730         printf("  Persist Through Power Loss Capable(PTPL_C): %d\n",!!(pr_buff->prin_descriptor.prin_readcap.flags[0]));
731         printf("  Type Mask Valid(TMV): %d\n", !!(pr_buff->prin_descriptor.prin_readcap.flags[1] & 0x80));
732         printf("  Allow Commands: %d\n", !!(( pr_buff->prin_descriptor.prin_readcap.flags[1] >> 4) & 0x7));
733         printf("  Persist Through Power Loss Active(PTPL_A): %d\n",
734                         !!(pr_buff->prin_descriptor.prin_readcap.flags[1] & 0x1));
735
736         if(pr_buff->prin_descriptor.prin_readcap.flags[1] & 0x80)
737         {
738                 printf("    Support indicated in Type mask:\n");
739
740                 printf("      %s: %d\n", pr_type_strs[7], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x80);
741                 printf("      %s: %d\n", pr_type_strs[6], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x40);
742                 printf("      %s: %d\n", pr_type_strs[5], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x20);
743                 printf("      %s: %d\n", pr_type_strs[3], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x8);
744                 printf("      %s: %d\n", pr_type_strs[1], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x2);
745                 printf("      %s: %d\n", pr_type_strs[8], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x100);
746         }
747 }
748
749 void mpath_print_buf_readfullstat( struct prin_resp *pr_buff)
750 {
751
752         int i,j, num;
753         uint64_t  prkey;
754         uint16_t  rel_pt_addr;
755         unsigned char * keyp;
756
757         num = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
758         if (0 == num)
759         {
760                 printf("  PR generation=0x%x \n", pr_buff->prin_descriptor.prin_readfd.prgeneration);
761                 return ;
762         }
763         else
764                 printf("  PR generation=0x%x \n", pr_buff->prin_descriptor.prin_readfd.prgeneration);
765
766         for (i = 0 ; i < num; i++)
767         {
768                 keyp = (unsigned  char *)&pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key;
769
770                 prkey = 0;
771                 for (j = 0; j < 8; ++j) {
772                         if (j > 0)
773                                 prkey <<= 8;
774                         prkey |= *keyp;
775                         ++keyp;
776                 }
777                 printf("   Key = 0x%" PRIx64 "\n", prkey);
778
779                 if (pr_buff->prin_descriptor.prin_readfd.descriptors[i]->flag & 0x02)
780                         printf("      All target ports bit set\n");
781                 else {
782                         printf("      All target ports bit clear\n");
783
784                         rel_pt_addr = pr_buff->prin_descriptor.prin_readfd.descriptors[i]->rtpi;
785                         printf("      Relative port address: 0x%x\n",
786                                         rel_pt_addr);
787                 }
788
789                 if (pr_buff->prin_descriptor.prin_readfd.descriptors[i]->flag & 0x1) {
790                         printf("      << Reservation holder >>\n");
791                         j = ((pr_buff->prin_descriptor.prin_readfd.descriptors[i]->scope_type >> 4) & 0xf);
792                         if (0 == j)
793                                 printf("      scope: LU_SCOPE, ");
794                         else
795                                 printf("      scope: %d ", j);
796                         j = (pr_buff->prin_descriptor.prin_readfd.descriptors[i]->scope_type & 0xf);
797                         printf(" type: %s\n", pr_type_strs[j]);
798                 } else
799                         printf("      not reservation holder\n");
800                 mpath_print_transport_id(pr_buff->prin_descriptor.prin_readfd.descriptors[i]);
801         }
802 }
803
804 static void usage(void)
805 {
806         fprintf(stderr, VERSION_STRING);
807         fprintf(stderr,
808                         "Usage: mpathpersist [OPTIONS] [DEVICE]\n"
809                         " Options:\n"
810                         "    --verbose|-v level         verbosity level\n"
811                         "                   0           Critical messages\n"
812                         "                   1           Error messages\n"
813                         "                   2           Warning messages\n"
814                         "                   3           Informational messages\n"
815                         "                   4           Informational messages with trace enabled\n"
816                         "    --clear|-C                 PR Out: Clear\n"
817                         "    --device=DEVICE|-d DEVICE  query or change DEVICE\n"
818                         "    --batch-file|-f FILE       run commands from FILE\n"
819                         "    --help|-h                  output this usage message\n"
820                         "    --hex|-H                   output response in hex\n"
821                         "    --in|-i                    request PR In command \n"
822                         "    --out|-o                   request PR Out command\n"
823                         "    --param-alltgpt|-Y         PR Out parameter 'ALL_TG_PT\n"
824                         "    --param-aptpl|-Z           PR Out parameter 'APTPL'\n"
825                         "    --read-keys|-k             PR In: Read Keys\n"
826                         "    --param-rk=RK|-K RK        PR Out parameter reservation key\n"
827                         "    --param-sark=SARK|-S SARK  PR Out parameter service action\n"
828                         "                               reservation key (SARK is in hex)\n"
829                         "    --preempt|-P               PR Out: Preempt\n"
830                         "    --preempt-abort|-A         PR Out: Preempt and Abort\n"
831                         "    --prout-type=TYPE|-T TYPE  PR Out command type\n"
832                         "    --read-full-status|-s      PR In: Read Full Status\n"
833                         "    --read-keys|-k             PR In: Read Keys\n"
834                         "    --read-reservation|-r      PR In: Read Reservation\n"
835                         "    --register|-G              PR Out: Register\n"
836                         "    --register-ignore|-I       PR Out: Register and Ignore\n"
837                         "    --release|-L               PR Out: Release\n"
838                         "    --report-capabilities|-c   PR In: Report Capabilities\n"
839                         "    --reserve|-R               PR Out: Reserve\n"
840                         "    --transport-id=TIDS|-X TIDS  TransportIDs can be mentioned\n"
841                         "                                 in several forms\n"
842                         "    --alloc-length=LEN|-l LEN  PR In: maximum allocation length\n"
843                         " Examples:\n"
844                         "     mpathpersist --out --register --param-sark=123abc --prout-type=5 /dev/mapper/mpath9\n"
845                         "     mpathpersist -i -k /dev/mapper/mpath9\n"
846                         "     mpathpersist --out --reserve --param-sark=123abc --prout-type=8 -d /dev/mapper/mpath9\n"
847                         "     mpathpersist -i -s -d /dev/mapper/mpath9\n");
848 }
849
850 void
851 mpath_print_transport_id(struct prin_fulldescr *fdesc)
852 {
853         switch (fdesc->trnptid.protocol_id) {
854         case MPATH_PROTOCOL_ID_FC:
855                 printf("   FCP-2 ");
856                 if (0 != fdesc->trnptid.format_code)
857                         printf(" [Unexpected format code: %d]\n",
858                                         fdesc->trnptid.format_code);
859                 dumpHex((const char *)fdesc->trnptid.n_port_name, 8, 0);
860                 break;
861         case MPATH_PROTOCOL_ID_ISCSI:
862                 printf("   iSCSI ");
863                 if (0 == fdesc->trnptid.format_code) {
864                         printf("name: %.*s\n", (int)sizeof(fdesc->trnptid.iscsi_name),
865                                 fdesc->trnptid.iscsi_name);
866                 }else if (1 == fdesc->trnptid.format_code){
867                         printf("world wide unique port id: %.*s\n",
868                                         (int)sizeof(fdesc->trnptid.iscsi_name),
869                                         fdesc->trnptid.iscsi_name);
870                 }else {
871                         printf("  [Unexpected format code: %d]\n", fdesc->trnptid.format_code);
872                         dumpHex((const char *)fdesc->trnptid.iscsi_name,
873                                  (int)sizeof(fdesc->trnptid.iscsi_name), 0);
874                 }
875                 break;
876         case MPATH_PROTOCOL_ID_SAS:
877                 printf("   SAS ");
878                 if (0 != fdesc->trnptid.format_code)
879                         printf(" [Unexpected format code: %d]\n",
880                                         fdesc->trnptid.format_code);
881                 dumpHex((const char *)fdesc->trnptid.sas_address, 8, 0);
882                 break;
883         default:
884                 return;
885         }
886 }
887
888 int
889 construct_transportid(const char * lcp, struct transportid transid[], int num_transportids)
890 {
891         int k = 0;
892         int j, n, b, c, len, alen;
893         const char * ecp;
894         const char * isip;
895
896         if ((0 == memcmp("fcp,", lcp, 4)) ||
897                         (0 == memcmp("FCP,", lcp, 4))) {
898                 lcp += 4;
899                 k = strspn(lcp, "0123456789aAbBcCdDeEfF");
900
901                 len = strlen(lcp);
902                 if (len != k) {
903                         fprintf(stderr, "badly formed symbolic FCP TransportID: %s\n",
904                                         lcp);
905                         return 1;
906                 }
907                 transid[num_transportids].format_code = MPATH_PROTOCOL_ID_FC;
908                 transid[num_transportids].protocol_id = MPATH_WWUI_DEVICE_NAME;
909                 for (k = 0, j = 0, b = 0; k < 16; ++k) {
910                         c = lcp[k];
911                         if (isdigit(c))
912                                 n = c - 0x30;
913                         else if (isupper(c))
914                                 n = c - 0x37;
915                         else
916                                 n = c - 0x57;
917                         if (k & 1) {
918                                 transid[num_transportids].n_port_name[j] = b | n;
919                                 ++j;
920                         } else
921                                 b = n << 4;
922                 }
923                 goto my_cont_b;
924         }
925         if ((0 == memcmp("sas,", lcp, 4)) || (0 == memcmp("SAS,", lcp, 4))) {
926                 lcp += 4;
927                 k = strspn(lcp, "0123456789aAbBcCdDeEfF");
928                 len =strlen(lcp);
929                 if (len != k) {
930                         fprintf(stderr, "badly formed symbolic SAS TransportID: %s\n",
931                                         lcp);
932                         return 1;
933                 }
934                 transid[num_transportids].format_code = MPATH_PROTOCOL_ID_SAS;
935                 transid[num_transportids].protocol_id = MPATH_WWUI_DEVICE_NAME;
936                 memcpy(&transid[num_transportids].sas_address, lcp, 8);
937
938                 goto my_cont_b;
939         }
940         if (0 == memcmp("iqn.", lcp, 4)) {
941                 ecp = strpbrk(lcp, " \t");
942                 isip = strstr(lcp, ",i,0x");
943                 if (ecp && (isip > ecp))
944                         isip = NULL;
945                 len = ecp ? (ecp - lcp) : (int)strlen(lcp);
946                 transid[num_transportids].format_code = (isip ? MPATH_WWUI_PORT_IDENTIFIER:MPATH_WWUI_DEVICE_NAME);
947                 transid[num_transportids].protocol_id = MPATH_PROTOCOL_ID_ISCSI;
948                 alen = len + 1; /* at least one trailing null */
949                 if (alen < 20)
950                         alen = 20;
951                 else if (0 != (alen % 4))
952                         alen = ((alen / 4) + 1) * 4;
953                 if (alen > 241) { /* sam5r02.pdf A.2 (Annex) */
954                         fprintf(stderr, "iSCSI name too long, alen=%d\n", alen);
955                         return 0;
956                 }
957                 transid[num_transportids].iscsi_name[1] = alen & 0xff;
958                 memcpy(&transid[num_transportids].iscsi_name[2], lcp, len);
959                 goto my_cont_b;
960         }
961 my_cont_b:
962         if (k >= MPATH_MAX_PARAM_LEN) {
963                 fprintf(stderr, "build_transportid: array length exceeded\n");
964                 return 1;
965         }
966         return 0;
967 }