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