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