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