summaryrefslogtreecommitdiff
path: root/mpathpersist/main.c
blob: b6617902f20ea49ff169bfc0d6f1286358681973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include "checkers.h"
#include "vector.h"
#include "config.h"
#include "structs.h"
#include <getopt.h>
#include <libudev.h>
#include "mpath_persist.h"
#include "mpath_persist_int.h"
#include "main.h"
#include "debug.h"
#include <pthread.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include "version.h"

static const char * pr_type_strs[] = {
	"obsolete [0]",
	"Write Exclusive",
	"obsolete [2]",
	"Exclusive Access",
	"obsolete [4]",
	"Write Exclusive, registrants only",
	"Exclusive Access, registrants only",
	"Write Exclusive, all registrants",
	"Exclusive Access, all registrants",
	"obsolete [9]", "obsolete [0xa]", "obsolete [0xb]", "obsolete [0xc]",
	"obsolete [0xd]", "obsolete [0xe]", "obsolete [0xf]",
};

int get_transportids_length(unsigned char * transportid_arr, int max_transportid, int num_transportids);
void mpath_print_buf_readcap(struct prin_resp *pr_buff);
void mpath_print_buf_readfullstat(struct prin_resp *pr_buff);
void mpath_print_buf_readresv(struct prin_resp *pr_buff);
void mpath_print_buf_readkeys(struct prin_resp *pr_buff);
void dumpHex(const char* str, int len, int no_ascii);
void * mpath_alloc_prin_response(int prin_sa);
void mpath_print_transport_id(struct prin_fulldescr *fdesc);
int construct_transportid(const char * inp, struct transportid transid[], int num_transportids);

void rcu_register_thread_memb(void) {}

void rcu_unregister_thread_memb(void) {}


static int verbose, loglevel, noisy;

static int handle_args(int argc, char * argv[], int line);

static int do_batch_file(const char *batch_fn)
{
	char command[] = "mpathpersist";
	const int ARGV_CHUNK = 2;
	const char delims[] = " \t\n";
	size_t len = 0;
	char *line = NULL;
	ssize_t n;
	int nline = 0;
	int argl = ARGV_CHUNK;
	FILE *fl;
	char **argv = calloc(argl, sizeof(*argv));
	int ret = MPATH_PR_SUCCESS;

	if (argv == NULL)
		return MPATH_PR_OTHER;

	fl = fopen(batch_fn, "r");
	if (fl == NULL) {
		fprintf(stderr, "unable to open %s: %s\n",
			batch_fn, strerror(errno));
		free(argv);
		return MPATH_PR_SYNTAX_ERROR;
	} else {
		if (verbose >= 2)
			fprintf(stderr, "running batch file %s\n",
				batch_fn);
	}

	while ((n = getline(&line, &len, fl)) != -1) {
		char *_token, *token;
		int argc = 0;
		int rv;

		nline++;
		argv[argc++] = command;

		if (line[n-1] == '\n')
			line[n-1] = '\0';
		if (verbose >= 3)
			fprintf(stderr, "processing line %d: %s\n",
				nline, line);

		for (token = strtok_r(line, delims, &_token);
		     token != NULL && *token != '#';
		     token = strtok_r(NULL, delims, &_token)) {

			if (argc >= argl) {
				int argn = argl + ARGV_CHUNK;
				char **tmp;

				tmp = realloc(argv, argn * sizeof(*argv));
				if (tmp == NULL)
					break;
				argv = tmp;
				argl = argn;
			}

			if (argc == 1 && !strcmp(token, command))
				continue;

			argv[argc++] = token;
		}

		if (argc <= 1)
			continue;

		if (verbose >= 2) {
			int i;

			fprintf(stderr, "## file %s line %d:", batch_fn, nline);
			for (i = 0; i < argc; i++)
				fprintf(stderr, " %s", argv[i]);
			fprintf(stderr, "\n");
		}

		optind = 0;
		rv = handle_args(argc, argv, nline);
		if (rv != MPATH_PR_SUCCESS)
			ret = rv;
	}

	fclose(fl);
	free(argv);
	free(line);
	return ret;
}

static struct prout_param_descriptor *
alloc_prout_param_descriptor(int num_transportid)
{
	struct prout_param_descriptor *paramp;

	if (num_transportid < 0 || num_transportid > MPATH_MX_TIDS)
		return NULL;

	paramp= malloc(sizeof(struct prout_param_descriptor) +
				(sizeof(struct transportid *) * num_transportid));

	if (!paramp)
		return NULL;

	memset(paramp, 0, sizeof(struct prout_param_descriptor) +
			(sizeof(struct transportid *) * num_transportid));
	return paramp;
}

static void free_prout_param_descriptor(struct prout_param_descriptor *paramp)
{
	unsigned int i;
	if (!paramp)
		return;

	for (i = 0; i < paramp->num_transportid; i++)
		free(paramp->trnptid_list[i]);

	free(paramp);
}

static int handle_args(int argc, char * argv[], int nline)
{
	int c;
	int fd = -1;
	const char *device_name = NULL;
	int num_prin_sa = 0;
	int num_prout_sa = 0;
	int prin_flag = 0;
	int prout_flag = 0;
	int ret = 0;
	int hex = 0;
	uint64_t param_sark = 0;
	unsigned int prout_type = 0;
	int param_alltgpt = 0;
	int param_aptpl = 0;
	uint64_t param_rk = 0;
	unsigned int param_rtp = 0;
	int num_transportids = 0;
	struct transportid transportids[MPATH_MX_TIDS];
	int prout = 1;
	int prin = 1;
	int prin_sa = -1;
	int prout_sa = -1;
	char *batch_fn = NULL;
	void *resp = NULL;

	memset(transportids, 0, MPATH_MX_TIDS * sizeof(struct transportid));

	while (1)
	{
		int option_index = 0;

		c = getopt_long (argc, argv, "v:Cd:hHioYZK:S:PAT:skrGILcRX:l:f:",
				long_options, &option_index);
		if (c == -1)
			break;

		switch (c)
		{
			case 'f':
				if (nline != 0) {
					fprintf(stderr,
						"ERROR: -f option not allowed in batch file\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				if (batch_fn != NULL) {
					fprintf(stderr,
						"ERROR: -f option can be used at most once\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				batch_fn = strdup(optarg);
				break;
			case 'v':
				if (nline == 0 && 1 != sscanf (optarg, "%d", &loglevel))
				{
					fprintf (stderr, "bad argument to '--verbose'\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				break;

			case 'C':
				prout_sa = MPATH_PROUT_CLEAR_SA;
				++num_prout_sa;
				break;

			case 'd':
				device_name = optarg;
				break;

			case 'h':
				usage ();
				free(batch_fn);
				return 0;

			case 'H':
				hex=1;
				break;

			case 'i':
				prin_flag = 1;
				break;

			case 'o':
				prout_flag = 1;
				break;

			case 'Y':
				param_alltgpt = 1;
				break;
			case 'Z':
				param_aptpl = 1;
				break;
			case 'K':
				if (1 != sscanf (optarg, "%" SCNx64 "", &param_rk))
				{
					fprintf (stderr, "bad argument to '--param-rk'\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				break;

			case 'S':
				if (1 != sscanf (optarg, "%" SCNx64 "", &param_sark))
				{
					fprintf (stderr, "bad argument to '--param-sark'\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				break;

			case 'P':
				prout_sa = MPATH_PROUT_PREE_SA;
				++num_prout_sa;
				break;

			case 'A':
				prout_sa = MPATH_PROUT_PREE_AB_SA;
				++num_prout_sa;
				break;

			case 'T':
				if (1 != sscanf (optarg, "%x", &prout_type))
				{
					fprintf (stderr, "bad argument to '--prout-type'\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				break;

			case 's':
				prin_sa = MPATH_PRIN_RFSTAT_SA;
				++num_prin_sa;
				break;

			case 'k':
				prin_sa = MPATH_PRIN_RKEY_SA;
				++num_prin_sa;
				break;

			case 'r':
				prin_sa = MPATH_PRIN_RRES_SA;
				++num_prin_sa;
				break;

			case 'G':
				prout_sa = MPATH_PROUT_REG_SA;
				++num_prout_sa;
				break;

			case 'I':
				prout_sa = MPATH_PROUT_REG_IGN_SA;
				++num_prout_sa;
				break;

			case 'L':
				prout_sa = MPATH_PROUT_REL_SA;
				++num_prout_sa;
				break;

			case 'c':
				prin_sa = MPATH_PRIN_RCAP_SA;
				++num_prin_sa;
				break;

			case 'R':
				prout_sa = MPATH_PROUT_RES_SA;
				++num_prout_sa;
				break;

			case 'X':
				if (0 != construct_transportid(optarg, transportids, num_transportids)) {
					fprintf(stderr, "bad argument to '--transport-id'\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}

				++num_transportids;
				break;

			case 'l':
				if (1 != sscanf(optarg, "%u", &mpath_mx_alloc_len)) {
					fprintf(stderr, "bad argument to '--alloc-length'\n");
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				} else if (MPATH_MAX_PARAM_LEN < mpath_mx_alloc_len) {
					fprintf(stderr, "'--alloc-length' argument exceeds maximum"
							" limit(%d)\n", MPATH_MAX_PARAM_LEN);
					ret = MPATH_PR_SYNTAX_ERROR;
					goto out;
				}
				break;

			default:
				fprintf(stderr, "unrecognised switch " "code 0x%x ??\n", c);
				ret = MPATH_PR_SYNTAX_ERROR;
				goto out;
		}
	}

	if (optind < argc)
	{

		if (NULL == device_name)
		{
			device_name = argv[optind];
			++optind;
		}
		if (optind < argc)
		{
			for (; optind < argc; ++optind)
				fprintf (stderr, "Unexpected extra argument: %s\n", argv[optind]);
			ret = MPATH_PR_SYNTAX_ERROR;
			goto out;
		}
	}

	if (nline == 0) {
		/* set verbosity */
		noisy = (loglevel >= 3) ? 1 : hex;
		verbose	= (loglevel >= 3)? 3: loglevel;
		ret = mpath_persistent_reserve_init_vecs(verbose);
		if (ret != MPATH_PR_SUCCESS)
			goto out;
	}

	if ((prout_flag + prin_flag) == 0 && batch_fn == NULL)
	{
		fprintf (stderr, "choose either '--in' or '--out' \n");
		ret = MPATH_PR_SYNTAX_ERROR;
		goto out;
	}
	if ((prout_flag + prin_flag) > 1)
	{
		fprintf (stderr, "choose either '--in' or '--out' \n");
		ret = MPATH_PR_SYNTAX_ERROR;
		goto out;
	}
	else if (prout_flag)
	{				/* syntax check on PROUT arguments */
		prin = 0;
		if ((1 != num_prout_sa) || (0 != num_prin_sa))
		{
			fprintf (stderr, " For Persistent Reserve Out only one "
					"appropriate\n service action must be "
					"chosen \n");
			ret = MPATH_PR_SYNTAX_ERROR;
			goto out;
		}
	}
	else if (prin_flag)
	{				/* syntax check on PRIN arguments */
		prout = 0;
		if (num_prout_sa > 0)
		{
			fprintf (stderr, " When a service action for Persistent "
					"Reserve Out is chosen the\n"
					" '--out' option must be given \n");
			ret = MPATH_PR_SYNTAX_ERROR;
			goto out;
		}
		if (0 == num_prin_sa)
		{
			fprintf (stderr,
					" No service action given for Persistent Reserve IN\n");
			ret = MPATH_PR_SYNTAX_ERROR;
			goto out;
		}
		else if (num_prin_sa > 1)
		{
			fprintf (stderr, " Too many service actions given; choose "
					"one only\n");
			ret = MPATH_PR_SYNTAX_ERROR;
			goto out;
		}
	}
	else
	{
		if (batch_fn == NULL)
			ret = MPATH_PR_SYNTAX_ERROR;
		goto out;
	}

	if ((param_rtp) && (MPATH_PROUT_REG_MOV_SA != prout_sa))
	{
		fprintf (stderr, " --relative-target-port"
				" only useful with --register-move\n");
		ret = MPATH_PR_SYNTAX_ERROR;
		goto out;
	}

	if (((MPATH_PROUT_RES_SA == prout_sa) ||
				(MPATH_PROUT_REL_SA == prout_sa) ||
				(MPATH_PROUT_PREE_SA == prout_sa) ||
				(MPATH_PROUT_PREE_AB_SA == prout_sa)) &&
			(0 == prout_type)) {
		fprintf(stderr, "Warning: --prout-type probably needs to be "
				"given\n");
	}
	if ((verbose > 2) && num_transportids)
	{
		fprintf (stderr, "number of transport-ids decoded from "
				"command line : %d\n", num_transportids);
	}

	if (device_name == NULL)
	{
		fprintf (stderr, "No device name given \n");
		ret = MPATH_PR_SYNTAX_ERROR;
		goto out;
	}

	/* open device */
	if ((fd = open (device_name, O_RDONLY)) < 0)
	{
		fprintf (stderr, "%s: error opening file (rw) fd=%d\n",
				device_name, fd);
		ret = MPATH_PR_FILE_ERROR;
		goto out;
	}


	if (prin)
	{
		resp = mpath_alloc_prin_response(prin_sa);
		if (!resp)
		{
			fprintf (stderr, "failed to allocate PRIN response buffer\n");
			ret = MPATH_PR_OTHER;
			goto out_fd;
		}

		ret = __mpath_persistent_reserve_in (fd, prin_sa, resp, noisy);
		if (ret != MPATH_PR_SUCCESS )
		{
			fprintf (stderr, "Persistent Reserve IN command failed\n");
			free(resp);
			goto out_fd;
		}

		switch(prin_sa)
		{
			case MPATH_PRIN_RKEY_SA:
				mpath_print_buf_readkeys(resp);
				break;
			case MPATH_PRIN_RRES_SA:
				mpath_print_buf_readresv(resp);
				break;
			case MPATH_PRIN_RCAP_SA:
				mpath_print_buf_readcap(resp);
				break;
			case MPATH_PRIN_RFSTAT_SA:
				mpath_print_buf_readfullstat(resp);
				break;
		}
		free(resp);
	}
	else if (prout)
	{
		int j;
		struct prout_param_descriptor *paramp;

		paramp = alloc_prout_param_descriptor(num_transportids);
		if (!paramp) {
			fprintf(stderr, "malloc paramp failed\n");
			ret = MPATH_PR_OTHER;
			goto out_fd;
		}

		for (j = 7; j >= 0; --j) {
			paramp->key[j] = (param_rk & 0xff);
			param_rk >>= 8;
		}

		for (j = 7; j >= 0; --j) {
			paramp->sa_key[j] = (param_sark & 0xff);
			param_sark >>= 8;
		}

		if (param_alltgpt)
			paramp->sa_flags |= MPATH_F_ALL_TG_PT_MASK;
		if (param_aptpl)
			paramp->sa_flags |= MPATH_F_APTPL_MASK;

		if (num_transportids)
		{
			paramp->sa_flags |= MPATH_F_SPEC_I_PT_MASK;
			paramp->num_transportid = num_transportids;
			for (j = 0 ; j < num_transportids; j++)
			{
				paramp->trnptid_list[j] = (struct transportid *)malloc(sizeof(struct transportid));
				if (!paramp->trnptid_list[j]) {
					fprintf(stderr, "malloc paramp->trnptid_list[%d] failed.\n", j);
					ret = MPATH_PR_OTHER;
					free_prout_param_descriptor(paramp);
					goto out_fd;
				}
				memcpy(paramp->trnptid_list[j], &transportids[j],sizeof(struct transportid));
			}
		}

		/* PROUT commands other than 'register and move' */
		ret = __mpath_persistent_reserve_out (fd, prout_sa, 0, prout_type,
				paramp, noisy);
		free_prout_param_descriptor(paramp);
	}

	if (ret != MPATH_PR_SUCCESS)
	{
		switch(ret)
		{
			case MPATH_PR_SENSE_UNIT_ATTENTION:
				printf("persistent reserve out: scsi status: Unit Attention\n");
				break;
			case MPATH_PR_RESERV_CONFLICT:
				printf("persistent reserve out: scsi status: Reservation Conflict\n");
				break;
		}
		printf("PR out: command failed\n");
	}

out_fd:
	close (fd);
out :
	if (ret == MPATH_PR_SYNTAX_ERROR) {
		free(batch_fn);
		if (nline == 0)
			usage();
		else
			fprintf(stderr, "syntax error on line %d in batch file\n",
				nline);
	} else if (batch_fn != NULL) {
		int rv = do_batch_file(batch_fn);

		free(batch_fn);
		ret = ret == 0 ? rv : ret;
	}
	if (nline == 0)
		mpath_persistent_reserve_free_vecs();
	return (ret >= 0) ? ret : MPATH_PR_OTHER;
}

int main(int argc, char *argv[])
{
	int ret;

	if (optind == argc)
	{

		fprintf (stderr, "No parameter used\n");
		usage ();
		exit (1);
	}

	if (getuid () != 0)
	{
		fprintf (stderr, "need to be root\n");
		exit (1);
	}

	if (libmpathpersist_init()) {
		exit(1);
	}
	if (atexit((void(*)(void))libmpathpersist_exit))
		fprintf(stderr, "failed to register cleanup handler for libmpathpersist: %m");

	ret = handle_args(argc, argv, 0);
	return (ret >= 0) ? ret : MPATH_PR_OTHER;
}

int
get_transportids_length(unsigned char * transportid_arr, int max_transportid, int num_transportids)
{
	int compact_len = 0;
	unsigned char * ucp = transportid_arr;
	int k, off, protocol_id, len;
	for (k = 0, off = 0; ((k < num_transportids) && (k < max_transportid));
			++k, off += MPATH_MX_TID_LEN) {
		protocol_id = ucp[off] & 0xf;
		if (5 == protocol_id) {
			len = (ucp[off + 2] << 8) + ucp[off + 3] + 4;
			if (len < 24)
				len = 24;
			if (off > compact_len)
				memmove(ucp + compact_len, ucp + off, len);
			compact_len += len;

		} else {
			if (off > compact_len)
				memmove(ucp + compact_len, ucp + off, 24);
			compact_len += 24;
		}
	}

	return compact_len;
}

void mpath_print_buf_readkeys( struct prin_resp *pr_buff)
{
	int i,j,k, num;
	unsigned char *keyp;
	uint64_t prkey;
	printf("  PR generation=0x%x, ", pr_buff->prin_descriptor.prin_readkeys.prgeneration);

	num = pr_buff->prin_descriptor.prin_readkeys.additional_length / 8;
	if (0 == num) {
		printf("	0 registered reservation key.\n");
		return;
	}
	else if (1 == num)
		printf("	1 registered reservation key follows:\n");
	else
		printf("	%d registered reservation keys follow:\n", num);


	keyp = (unsigned char *)&pr_buff->prin_descriptor.prin_readkeys.key_list[0];
	for (i = 0; i < num ; i++)
	{
		prkey = 0;
		for (j = 0; j < 8; ++j) {

			if (j > 0)
				prkey <<= 8;
			prkey |= keyp[j];
		}
		printf("    0x%" PRIx64 "\n", prkey);
		k=8*i+j;
		keyp = (unsigned char *)&pr_buff->prin_descriptor.prin_readkeys.key_list[k];
	}
}

void mpath_print_buf_readresv( struct prin_resp *pr_buff)
{
	int j, num, scope=0, type=0;
	unsigned char *keyp;
	uint64_t prkey;

	num = pr_buff->prin_descriptor.prin_readresv.additional_length / 8;
	if (0 == num)
	{
		printf("  PR generation=0x%x, there is NO reservation held \n", pr_buff->prin_descriptor.prin_readresv.prgeneration);
		return ;
	}
	else
		printf("  PR generation=0x%x, Reservation follows:\n", pr_buff->prin_descriptor.prin_readresv.prgeneration);
	keyp = (unsigned  char *)&pr_buff->prin_descriptor.prin_readkeys.key_list[0];
	prkey = 0;
	for (j = 0; j < 8; ++j) {
		if (j > 0)
			prkey <<= 8;
		prkey |= keyp[j];
	}

	printf("   Key = 0x%" PRIx64 "\n", prkey);

	scope = (pr_buff->prin_descriptor.prin_readresv.scope_type >> 4) &  0x0f;
	type = pr_buff->prin_descriptor.prin_readresv.scope_type & 0x0f;

	if (scope == 0)
		printf("  scope = LU_SCOPE, type = %s", pr_type_strs[type]);
	else
		printf("  scope = %d, type = %s", scope, pr_type_strs[type]);

	printf("\n");

}

void mpath_print_buf_readcap( struct prin_resp *pr_buff)
{
	if ( pr_buff->prin_descriptor.prin_readcap.length <= 2 ) {
		fprintf(stderr, "Unexpected response for PRIN Report "
				"Capabilities\n");
		return; //MALFORMED;
	}

	printf("Report capabilities response:\n");

	printf("  Compatible Reservation Handling(CRH): %d\n", !!(pr_buff->prin_descriptor.prin_readcap.flags[0] & 0x10));
	printf("  Specify Initiator Ports Capable(SIP_C): %d\n",!!(pr_buff->prin_descriptor.prin_readcap.flags[0] & 0x8));
	printf("  All Target Ports Capable(ATP_C): %d\n",!!(pr_buff->prin_descriptor.prin_readcap.flags[0] & 0x4 ));
	printf("  Persist Through Power Loss Capable(PTPL_C): %d\n",!!(pr_buff->prin_descriptor.prin_readcap.flags[0]));
	printf("  Type Mask Valid(TMV): %d\n", !!(pr_buff->prin_descriptor.prin_readcap.flags[1] & 0x80));
	printf("  Allow Commands: %d\n", !!(( pr_buff->prin_descriptor.prin_readcap.flags[1] >> 4) & 0x7));
	printf("  Persist Through Power Loss Active(PTPL_A): %d\n",
			!!(pr_buff->prin_descriptor.prin_readcap.flags[1] & 0x1));

	if(pr_buff->prin_descriptor.prin_readcap.flags[1] & 0x80)
	{
		printf("    Support indicated in Type mask:\n");

		printf("      %s: %d\n", pr_type_strs[7], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x80);
		printf("      %s: %d\n", pr_type_strs[6], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x40);
		printf("      %s: %d\n", pr_type_strs[5], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x20);
		printf("      %s: %d\n", pr_type_strs[3], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x8);
		printf("      %s: %d\n", pr_type_strs[1], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x2);
		printf("      %s: %d\n", pr_type_strs[8], pr_buff->prin_descriptor.prin_readcap.pr_type_mask & 0x100);
	}
}

void mpath_print_buf_readfullstat( struct prin_resp *pr_buff)
{

	int i,j, num;
	uint64_t  prkey;
	uint16_t  rel_pt_addr;
	unsigned char * keyp;

	num = pr_buff->prin_descriptor.prin_readfd.number_of_descriptor;
	if (0 == num)
	{
		printf("  PR generation=0x%x \n", pr_buff->prin_descriptor.prin_readfd.prgeneration);
		return ;
	}
	else
		printf("  PR generation=0x%x \n", pr_buff->prin_descriptor.prin_readfd.prgeneration);

	for (i = 0 ; i < num; i++)
	{
		keyp = (unsigned  char *)&pr_buff->prin_descriptor.prin_readfd.descriptors[i]->key;

		prkey = 0;
		for (j = 0; j < 8; ++j) {
			if (j > 0)
				prkey <<= 8;
			prkey |= *keyp;
			++keyp;
		}
		printf("   Key = 0x%" PRIx64 "\n", prkey);

		if (pr_buff->prin_descriptor.prin_readfd.descriptors[i]->flag & 0x02)
			printf("      All target ports bit set\n");
		else {
			printf("      All target ports bit clear\n");

			rel_pt_addr = pr_buff->prin_descriptor.prin_readfd.descriptors[i]->rtpi;
			printf("      Relative port address: 0x%x\n",
					rel_pt_addr);
		}

		if (pr_buff->prin_descriptor.prin_readfd.descriptors[i]->flag & 0x1) {
			printf("      << Reservation holder >>\n");
			j = ((pr_buff->prin_descriptor.prin_readfd.descriptors[i]->scope_type >> 4) & 0xf);
			if (0 == j)
				printf("      scope: LU_SCOPE, ");
			else
				printf("      scope: %d ", j);
			j = (pr_buff->prin_descriptor.prin_readfd.descriptors[i]->scope_type & 0xf);
			printf(" type: %s\n", pr_type_strs[j]);
		} else
			printf("      not reservation holder\n");
		mpath_print_transport_id(pr_buff->prin_descriptor.prin_readfd.descriptors[i]);
	}
}

static void usage(void)
{
	fprintf(stderr, VERSION_STRING);
	fprintf(stderr,
			"Usage: mpathpersist [OPTIONS] [DEVICE]\n"
			" Options:\n"
			"    --verbose|-v level         verbosity level\n"
			"                   0           Critical messages\n"
			"                   1           Error messages\n"
			"                   2           Warning messages\n"
			"                   3           Informational messages\n"
			"                   4           Informational messages with trace enabled\n"
			"    --clear|-C                 PR Out: Clear\n"
			"    --device=DEVICE|-d DEVICE  query or change DEVICE\n"
			"    --batch-file|-f FILE       run commands from FILE\n"
			"    --help|-h                  output this usage message\n"
			"    --hex|-H                   output response in hex\n"
			"    --in|-i                    request PR In command \n"
			"    --out|-o                   request PR Out command\n"
			"    --param-alltgpt|-Y         PR Out parameter 'ALL_TG_PT\n"
			"    --param-aptpl|-Z           PR Out parameter 'APTPL'\n"
			"    --read-keys|-k             PR In: Read Keys\n"
			"    --param-rk=RK|-K RK        PR Out parameter reservation key\n"
			"    --param-sark=SARK|-S SARK  PR Out parameter service action\n"
			"                               reservation key (SARK is in hex)\n"
			"    --preempt|-P               PR Out: Preempt\n"
			"    --preempt-abort|-A         PR Out: Preempt and Abort\n"
			"    --prout-type=TYPE|-T TYPE  PR Out command type\n"
			"    --read-full-status|-s      PR In: Read Full Status\n"
			"    --read-keys|-k             PR In: Read Keys\n"
			"    --read-reservation|-r      PR In: Read Reservation\n"
			"    --register|-G              PR Out: Register\n"
			"    --register-ignore|-I       PR Out: Register and Ignore\n"
			"    --release|-L               PR Out: Release\n"
			"    --report-capabilities|-c   PR In: Report Capabilities\n"
			"    --reserve|-R               PR Out: Reserve\n"
			"    --transport-id=TIDS|-X TIDS  TransportIDs can be mentioned\n"
			"                                 in several forms\n"
			"    --alloc-length=LEN|-l LEN  PR In: maximum allocation length\n");
}

void
mpath_print_transport_id(struct prin_fulldescr *fdesc)
{
	switch (fdesc->trnptid.protocol_id) {
	case MPATH_PROTOCOL_ID_FC:
		printf("   FCP-2 ");
		if (0 != fdesc->trnptid.format_code)
			printf(" [Unexpected format code: %d]\n",
					fdesc->trnptid.format_code);
		dumpHex((const char *)fdesc->trnptid.n_port_name, 8, 0);
		break;
	case MPATH_PROTOCOL_ID_ISCSI:
		printf("   iSCSI ");
		if (0 == fdesc->trnptid.format_code) {
			printf("name: %.*s\n", (int)sizeof(fdesc->trnptid.iscsi_name),
				fdesc->trnptid.iscsi_name);
		}else if (1 == fdesc->trnptid.format_code){
			printf("world wide unique port id: %.*s\n",
					(int)sizeof(fdesc->trnptid.iscsi_name),
					fdesc->trnptid.iscsi_name);
		}else {
			printf("  [Unexpected format code: %d]\n", fdesc->trnptid.format_code);
			dumpHex((const char *)fdesc->trnptid.iscsi_name,
				 (int)sizeof(fdesc->trnptid.iscsi_name), 0);
		}
		break;
	case MPATH_PROTOCOL_ID_SAS:
		printf("   SAS ");
		if (0 != fdesc->trnptid.format_code)
			printf(" [Unexpected format code: %d]\n",
					fdesc->trnptid.format_code);
		dumpHex((const char *)fdesc->trnptid.sas_address, 8, 0);
		break;
	default:
		return;
	}
}

int
construct_transportid(const char * lcp, struct transportid transid[], int num_transportids)
{
	int k = 0;
	int j, n, b, c, len, alen;
	const char * ecp;
	const char * isip;

	if ((0 == memcmp("fcp,", lcp, 4)) ||
			(0 == memcmp("FCP,", lcp, 4))) {
		lcp += 4;
		k = strspn(lcp, "0123456789aAbBcCdDeEfF");

		len = strlen(lcp);
		if (len != k) {
			fprintf(stderr, "badly formed symbolic FCP TransportID: %s\n",
					lcp);
			return 1;
		}
		transid[num_transportids].format_code = MPATH_PROTOCOL_ID_FC;
		transid[num_transportids].protocol_id = MPATH_WWUI_DEVICE_NAME;
		for (k = 0, j = 0, b = 0; k < 16; ++k) {
			c = lcp[k];
			if (isdigit(c))
				n = c - 0x30;
			else if (isupper(c))
				n = c - 0x37;
			else
				n = c - 0x57;
			if (k & 1) {
				transid[num_transportids].n_port_name[j] = b | n;
				++j;
			} else
				b = n << 4;
		}
		goto my_cont_b;
	}
	if ((0 == memcmp("sas,", lcp, 4)) || (0 == memcmp("SAS,", lcp, 4))) {
		lcp += 4;
		k = strspn(lcp, "0123456789aAbBcCdDeEfF");
		len =strlen(lcp);
		if (len != k) {
			fprintf(stderr, "badly formed symbolic SAS TransportID: %s\n",
					lcp);
			return 1;
		}
		transid[num_transportids].format_code = MPATH_PROTOCOL_ID_SAS;
		transid[num_transportids].protocol_id = MPATH_WWUI_DEVICE_NAME;
		memcpy(&transid[num_transportids].sas_address, lcp, 8);

		goto my_cont_b;
	}
	if (0 == memcmp("iqn.", lcp, 4)) {
		ecp = strpbrk(lcp, " \t");
		isip = strstr(lcp, ",i,0x");
		if (ecp && (isip > ecp))
			isip = NULL;
		len = ecp ? (ecp - lcp) : (int)strlen(lcp);
		transid[num_transportids].format_code = (isip ? MPATH_WWUI_PORT_IDENTIFIER:MPATH_WWUI_DEVICE_NAME);
		transid[num_transportids].protocol_id = MPATH_PROTOCOL_ID_ISCSI;
		alen = len + 1; /* at least one trailing null */
		if (alen < 20)
			alen = 20;
		else if (0 != (alen % 4))
			alen = ((alen / 4) + 1) * 4;
		if (alen > 241) { /* sam5r02.pdf A.2 (Annex) */
			fprintf(stderr, "iSCSI name too long, alen=%d\n", alen);
			return 0;
		}
		transid[num_transportids].iscsi_name[1] = alen & 0xff;
		memcpy(&transid[num_transportids].iscsi_name[2], lcp, len);
		goto my_cont_b;
	}
my_cont_b:
	if (k >= MPATH_MAX_PARAM_LEN) {
		fprintf(stderr, "build_transportid: array length exceeded\n");
		return 1;
	}
	return 0;
}