summaryrefslogtreecommitdiff
path: root/usr/iscsi_sysfs.c
blob: aef766b18de12a2d76e7b97f2fec5667153c9a97 (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
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
/*
 * iSCSI sysfs
 *
 * Copyright (C) 2006 Mike Christie
 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 */
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>

#include "log.h"
#include "initiator.h"
#include "transport.h"
#include "idbm.h"
#include "version.h"
#include "iscsi_sysfs.h"
#include "iscsi_settings.h"
#include "iface.h"

#define ISCSI_TRANSPORT_DIR	"/sys/class/iscsi_transport"
#define ISCSI_SESSION_DIR	"/sys/class/iscsi_session"
#define ISCSI_CONN_DIR		"/sys/class/iscsi_connection"
#define ISCSI_HOST_DIR		"/sys/class/iscsi_host"
#define SCSI_HOST_DIR		"/sys/class/scsi_host"
#define SCSI_DEVICE_DIR		"/sys/bus/scsi/devices"

#define ISCSI_MAX_SYSFS_BUFFER NI_MAXHOST

/*
 * TODO: make this into a real API and check inputs better and add doc.
 * We should also use a common lib and search sysfs according to the sysfs
 * doc in the kernel documetnation.
 */

/* tmp buffer used by sysfs functions */
static char sysfs_file[PATH_MAX];
static int num_transports;
LIST_HEAD(transports);

/* mini implementation of versionsort for uclibc compatility */
int direntcmp(const void *d1, const void *d2)
{
	const char *a = (*(const struct dirent **)d1)->d_name;
	const char *b = (*(const struct dirent **)d2)->d_name;
	while (*a && *b) {
		int mask = (isdigit(*a) != 0) |  ((isdigit(*b) != 0) << 1);
		switch (mask) {
		case 0: /* none are digit */
			if (*a == *b)
				break;
			return (*a - *b);
		case 1: /* a is digit but not b */
			return -1;
		case 2: /* b is digit but not a */
			return 1;
		case 3: /* both ar digits */
			return atoi(a) - atoi(b);
		}
		a++; b++;
	}
	return *a - *b;
}

int read_sysfs_file(char *filename, void *value, char *format)
{
	FILE *file;
	char buffer[ISCSI_MAX_SYSFS_BUFFER + 1], *line;
	int err = 0;

	file = fopen(filename, "r");
	if (file) {
		line = fgets(buffer, sizeof(buffer), file);
		if (line && strncmp(line, "<NULL>", 6))
			sscanf(buffer, format, value);
		else {
			log_debug(5, "Could not read %s.\n", filename);
			err = ENODATA;
		}
		fclose(file);
	} else {
		log_debug(5, "Could not open %s.\n", filename);
		err = errno;
	}
	return err;
}

void free_transports(void)
{
	struct iscsi_transport *t, *tmp;

	list_for_each_entry_safe(t, tmp, &transports, list) {
		list_del(&t->list);
		free(t);
	}
}

static int trans_filter(const struct dirent *dir)
{
	return strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..");
}

static int read_transports(void)
{
	struct dirent **namelist;
	char filename[64];
	int i, n, found, err = 0;
	struct iscsi_transport *t;

	log_debug(7, "in %s", __FUNCTION__);

	n = scandir(ISCSI_TRANSPORT_DIR, &namelist, trans_filter,
		    direntcmp);
	if (n < 0) {
		log_error("Could not scan %s.", ISCSI_TRANSPORT_DIR);
		return n;
	}

	for (i = 0; i < n; i++) {
		found = 0;

		list_for_each_entry(t, &transports, list) {
			if (!strcmp(t->name, namelist[i]->d_name)) {
				found = 1;
				break;
			}
		}

		if (found)
			continue;

		/* copy new transport */
		t = malloc(sizeof(*t));
		if (!t)
			continue;
		log_debug(7, "Adding new transport %s", namelist[i]->d_name);

		INIT_LIST_HEAD(&t->sessions);
		INIT_LIST_HEAD(&t->list);
		strncpy(t->name, namelist[i]->d_name,
			ISCSI_TRANSPORT_NAME_MAXLEN);

		sprintf(filename, ISCSI_TRANSPORT_DIR"/%s/handle", t->name);
		err = read_sysfs_file(filename, &t->handle, "%llu\n");
		if (err)
			continue;

		sprintf(filename, ISCSI_TRANSPORT_DIR"/%s/caps", t->name);
		err = read_sysfs_file(filename, &t->caps, "0x%x");
		if (err)
			continue;

		/*
		 * tmp hack for qla4xx compat
		 */
		if (!strcmp(t->name, "qla4xxx")) {
			t->caps |= CAP_DATA_PATH_OFFLOAD;
			t->caps |= CAP_FW_DB;
		}

		list_add_tail(&t->list, &transports);
	}

	for (i = 0; i < n; i++)
		free(namelist[i]);
	free(namelist);
	num_transports = n;

	return 0;
}

static void get_session_param(int sid, char *param, void *value, char *format)
{
	/* set to invalid */
	if (!strcmp(format, "%s\n"))
		((char *)value)[0] = '\0';
	else
		*((int *)value) = -1;

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_SESSION_DIR"/session%d/%s", sid, param);
	read_sysfs_file(sysfs_file, value, format);
}

static void get_negotiated_conn_param(int sid, char *param, int *value)
{
	/* set to invalid */
	*value = -1;

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_CONN_DIR"/connection%d:0/%s", sid, param);
	read_sysfs_file(sysfs_file, value, "%d\n");
}

/* caller must check lengths */
void get_auth_conf(int sid, struct iscsi_auth_config *conf)
{
	memset(conf, 0, sizeof(*conf));

	get_session_param(sid, "username", conf->username, "%s\n");
	get_session_param(sid, "username_in", conf->username_in, "%s\n");
	get_session_param(sid, "password", conf->password, "%s\n");
	if (strlen((char *)conf->password))
		conf->password_length = strlen((char *)conf->password);
	get_session_param(sid, "password_in", conf->password_in, "%s\n");
	if (strlen((char *)conf->password_in))
		conf->password_in_length = strlen((char *)conf->password_in);
}

/* called must check for -1=invalid value */
void get_negotiated_conn_conf(int sid,
			      struct iscsi_conn_operational_config *conf)
{
	memset(conf, 0, sizeof(*conf));

	get_negotiated_conn_param(sid, "data_digest",
				  &conf->DataDigest);
	get_negotiated_conn_param(sid, "header_digest",
				  &conf->HeaderDigest);
	get_negotiated_conn_param(sid, "max_xmit_dlength",
				  &conf->MaxXmitDataSegmentLength);
	get_negotiated_conn_param(sid, "max_recv_dlength",
				  &conf->MaxRecvDataSegmentLength);
}

/* called must check for -1=invalid value */
void get_negotiated_session_conf(int sid,
				 struct iscsi_session_operational_config *conf)
{
	memset(conf, 0, sizeof(*conf));

	get_session_param(sid, "data_pdu_in_order",
			  &conf->DataPDUInOrder, "%d\n");
	get_session_param(sid, "data_seq_in_order",
			  &conf->DataSequenceInOrder, "%d\n");
	get_session_param(sid, "erl",
			  &conf->ERL, "%d\n");
	get_session_param(sid, "first_burst_len",
			  &conf->FirstBurstLength, "%d\n");
	get_session_param(sid, "max_burst_len",
			  &conf->MaxBurstLength, "%d\n");
	get_session_param(sid, "immediate_data",
			  &conf->ImmediateData, "%d\n");
	get_session_param(sid, "initial_r2t",
			  &conf->InitialR2T, "%d\n");
	get_session_param(sid, "max_outstanding_r2t",
			  &conf->MaxOutstandingR2T, "%d\n");
}

uint32_t get_host_no_from_sid(uint32_t sid, int *err)
{
	char *buf, *path, *tmp;
	uint32_t host_no;

	*err = 0;

	buf = calloc(2, PATH_MAX);
	if (!buf) {
		*err = ENOMEM;
		return 0;
	}
	path = buf + PATH_MAX;

	sprintf(path, ISCSI_SESSION_DIR"/session%d/device", sid);
	if (readlink(path, buf, PATH_MAX) < 0) {
		log_error("Could not get link for %s.", path);
		*err = errno;
		goto free_buf;
	}

	/* buf will be .....bus_info/hostX/sessionY */

	/* find hostX */
	tmp = strrchr(buf, '/');
	*tmp = '\0';

	/* find bus and increment past it */
	tmp = strrchr(buf, '/');
	tmp++;

	if (sscanf(tmp, "host%u", &host_no) != 1) {
		log_error("Could not get host for sid %u.", sid);
		*err = ENXIO;
		goto free_buf;
	}

free_buf:
	free(buf);
	return host_no;
}

static int __get_host_no_from_hwaddress(void *data, struct host_info *info)
{
	struct host_info *ret_info = data;

	if (!strcmp(ret_info->iface.hwaddress, info->iface.hwaddress)) {
		ret_info->host_no = info->host_no;
		return 1;
	}
	return 0;
}

static uint32_t get_host_no_from_hwaddress(char *address, int *rc)
{
	uint32_t host_no = -1;
	struct host_info *info;
	int nr_found, local_rc;

	*rc = 0;

	info = calloc(1, sizeof(*info));
	if (!info) {
		*rc = ENOMEM;
		return -1;
	}
	strcpy(info->iface.hwaddress, address);

	local_rc = sysfs_for_each_host(info, &nr_found,
					__get_host_no_from_hwaddress);
	if (local_rc == 1)
		host_no = info->host_no;
	else
		*rc = ENODEV;
	free(info);
	return host_no;
}

static int __get_host_no_from_ipaddress(void *data, struct host_info *info)
{
	struct host_info *ret_info = data;

	if (!strcmp(ret_info->iface.ipaddress, info->iface.ipaddress)) {
		ret_info->host_no = info->host_no;
		return 1;
	}
	return 0;
}

static uint32_t get_host_no_from_ipaddress(char *address, int *rc)
{
	uint32_t host_no = -1;
	struct host_info *info;
	int nr_found;
	int local_rc;

	*rc = 0;

	info = calloc(1, sizeof(*info));
	if (!info) {
		*rc = ENOMEM;
		return -1;
	}
	strcpy(info->iface.ipaddress, address);

	local_rc = sysfs_for_each_host(info, &nr_found,
					__get_host_no_from_ipaddress);
	if (local_rc == 1)
		host_no = info->host_no;
	else
		*rc = ENODEV;
	free(info);
	return host_no;
}

uint32_t get_host_no_from_iface(struct iface_rec *iface, int *rc)
{
	int tmp_rc;
	uint32_t host_no = -1;

	if (strlen(iface->hwaddress) &&
	    strcasecmp(iface->hwaddress, DEFAULT_HWADDRESS))
		host_no = get_host_no_from_hwaddress(iface->hwaddress, &tmp_rc);
	else if (strlen(iface->ipaddress) &&
		 strcasecmp(iface->ipaddress, DEFAULT_IPADDRESS))
		host_no = get_host_no_from_ipaddress(iface->ipaddress, &tmp_rc);
	else
		tmp_rc = EINVAL;

	*rc = tmp_rc;
	return host_no;
}

static int sysfs_read_iface(struct iface_rec *iface, int host_no, int sid)
{
	struct iscsi_transport *t;
	int ret;

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_HOST_DIR"/host%u/hwaddress", host_no);
	/*
	 * backward compat
	 * If we cannot get the address we assume we are doing the old
	 * style and use default.
	 */
	sprintf(iface->hwaddress, DEFAULT_HWADDRESS);
	ret = read_sysfs_file(sysfs_file, iface->hwaddress, "%s\n");
	if (ret)
		log_debug(7, "could not read hwaddress for %s", sysfs_file);

	t = get_transport_by_hba(host_no);
	if (!t)
		log_debug(7, "could not get transport name for host %d",
			  host_no);
	else
		strcpy(iface->transport_name, t->name);

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_HOST_DIR"/host%u/ipaddress", host_no);
	/* if not found just print out default */
	sprintf(iface->ipaddress, DEFAULT_IPADDRESS);
	ret = read_sysfs_file(sysfs_file, iface->ipaddress, "%s\n");
	if (ret)
		log_debug(7, "could not read local address for %s",
			 sysfs_file);

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_HOST_DIR"/host%u/netdev", host_no);
	/* if not found just print out default */
	sprintf(iface->netdev, DEFAULT_NETDEV);
	ret = read_sysfs_file(sysfs_file, iface->netdev, "%s\n");
	if (ret)
		log_debug(7, "could not read netdev for %s",
			 sysfs_file);

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_HOST_DIR"/host%d/initiatorname", host_no);
	ret = read_sysfs_file(sysfs_file, iface->iname, "%s\n");
	if (ret)
		log_debug(7, "Could not read initiatorname for %s",
			 sysfs_file);

	/*
	 * this is on the session, because we support multiple bindings
	 * per device.
	 */
	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_SESSION_DIR"/session%u/ifacename", sid);
	/*
	 * this was added after 2.0.869 so we could be doing iscsi_tcp
	 * session binding, but there may not be a ifacename set
	 */
	memset(iface->name, 0, sizeof(iface->name));
	ret = read_sysfs_file(sysfs_file, iface->name, "%s\n");
	if (ret) {
		log_debug(7, "could not read iface name for %s",
			 sysfs_file);
		/*
 		 * if the ifacename file is not there then we are using a older
 		 * kernel and can try to find the binding by the net info
 		 * which was used on these older kernels.
 		 */
		if (!iface_is_bound_by_hwaddr(iface) &&
		    !iface_is_bound_by_netdev(iface))
			sprintf(iface->name, DEFAULT_IFACENAME);
		else if (iface_get_by_net_binding(iface, iface))
			log_debug(7, "Could not find iface for session bound "
				  "to:" iface_fmt "\n", iface_str(iface));
	}
	return ret;
}

int sysfs_for_each_host(void *data, int *nr_found, sysfs_host_op_fn *fn)
{
	struct dirent **namelist;
	int rc = 0, i, n;
	struct host_info *info;

	info = calloc(1, sizeof(*info));
	if (!info)
		return ENOMEM;

	n = scandir(ISCSI_HOST_DIR, &namelist, trans_filter,
		    direntcmp);
	if (n <= 0)
		goto free_info;

	for (i = 0; i < n; i++) {
		if (sscanf(namelist[i]->d_name, "host%u", &info->host_no) !=
			   1) {
			log_error("Invalid iscsi host dir: %s",
				  namelist[i]->d_name);
			break;
		}

		sysfs_read_iface(&info->iface, info->host_no, -1);

		rc = fn(data, info);
		if (rc != 0)
			break;
		(*nr_found)++;
	}

	for (i = 0; i < n; i++)
		free(namelist[i]);
	free(namelist);

free_info:
	free(info);
	return rc;
}

/**
 * sysfs_session_has_leadconn - checks if session has lead conn in kernel
 * @sid: session id
 *
 * return 1 if session has lead conn and 0 if not.
 */
int sysfs_session_has_leadconn(uint32_t sid)
{
	struct stat statb;

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_CONN_DIR"/connection%u:0", sid);
	if (!stat(sysfs_file, &statb))
		return 1;
	else
		return 0;
}

int get_sessioninfo_by_sysfs_id(struct session_info *info, char *session)
{
	int ret, pers_failed = 0;
	uint32_t host_no;

	if (sscanf(session, "session%d", &info->sid) != 1) {
		log_error("invalid session '%s'", session);
		return EINVAL;
	}

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_SESSION_DIR"/%s/targetname", session);
	ret = read_sysfs_file(sysfs_file, info->targetname, "%s\n");
	if (ret) {
		log_error("could not read session targetname: %d", ret);
		return ret;
	}

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_SESSION_DIR"/%s/tpgt", session);
	ret = read_sysfs_file(sysfs_file, &info->tpgt, "%u\n");
	if (ret) {
		log_error("could not read session tpgt: %d", ret);
		return ret;
	}

	/* some HW drivers do not export addr and port */
	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_CONN_DIR"/connection%d:0/"
		"persistent_address", info->sid);
	memset(info->persistent_address, 0, NI_MAXHOST);
	ret = read_sysfs_file(sysfs_file, info->persistent_address, "%s\n");
	if (ret) {
		pers_failed = 1;
		/* older qlogic does not support this */
		log_debug(5, "could not read pers conn addr: %d", ret);
	}

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_CONN_DIR"/connection%d:0/address",
		 info->sid);
	memset(info->address, 0, NI_MAXHOST);
	ret = read_sysfs_file(sysfs_file, info->address, "%s\n");
	if (ret) {
		log_debug(5, "could not read curr addr: %d", ret);
		/* iser did not export this */
		if (!pers_failed)
			strcpy(info->address, info->persistent_address);
	} else if (pers_failed)
		/*
		 * for qla if we could not get the persistent addr
		 * we will use the current for both addrs
		 */
		strcpy(info->persistent_address, info->address);
	pers_failed = 0;

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_CONN_DIR"/connection%d:0/"
		"persistent_port", info->sid);
	info->persistent_port = -1;
	ret = read_sysfs_file(sysfs_file, &info->persistent_port, "%u\n");
	if (ret) {
		pers_failed = 1;
		log_debug(5, "Could not read pers conn port %d", ret);
	}

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_CONN_DIR"/connection%d:0/port",
		info->sid);
	info->port = -1;
	ret = read_sysfs_file(sysfs_file, &info->port, "%u\n");
	if (ret) {
		/* iser did not export this */
		if (!pers_failed)
			info->port = info->persistent_port;
		log_debug(5, "Could not read curr conn port %d", ret);
	} else if (pers_failed)
		/*
		 * for qla if we could not get the persistent addr
		 * we will use the current for both addrs
		 */
		info->persistent_port = info->port;

	ret = 0;
	host_no = get_host_no_from_sid(info->sid, &ret);
	if (ret) {
		log_error("could not get host_no for session %d.", ret);
		return ret;
	}

	sysfs_read_iface(&info->iface, host_no, info->sid);
 
	log_debug(7, "found targetname %s address %s pers address %s port %d "
		 "pers port %d driver %s iface name %s ipaddress %s "
		 "netdev %s hwaddress %s iname %s",
		  info->targetname, info->address ? info->address : "NA",
		  info->persistent_address ? info->persistent_address : "NA",
		  info->port, info->persistent_port, info->iface.transport_name,
		  info->iface.name, info->iface.ipaddress,
		  info->iface.netdev, info->iface.hwaddress,
		  info->iface.iname);
	return 0;
}
 
int sysfs_for_each_session(void *data, int *nr_found, sysfs_session_op_fn *fn)
{
	struct dirent **namelist;
	int rc = 0, n, i;
	struct session_info *info;

	info = calloc(1, sizeof(*info));
	if (!info)
		return ENOMEM;

	sprintf(sysfs_file, ISCSI_SESSION_DIR);
	n = scandir(sysfs_file, &namelist, trans_filter,
		    direntcmp);
	if (n <= 0)
		goto free_info;

	for (i = 0; i < n; i++) {
		rc = get_sessioninfo_by_sysfs_id(info, namelist[i]->d_name);
		if (rc) {
			log_error("could not find session info for %s",
				   namelist[i]->d_name);
			continue;
		}

		rc = fn(data, info);
		if (rc > 0)
			break;
		else if (rc == 0)
			(*nr_found)++;
		else
			/* if less than zero it means it was not a match */
			rc = 0;
	}

	for (i = 0; i < n; i++)
		free(namelist[i]);
	free(namelist);

free_info:
	free(info);
	return rc;
}

int get_session_state(char *state, int sid)
{
	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, ISCSI_SESSION_DIR"/session%d/state", sid);
	return read_sysfs_file(sysfs_file, state, "%s\n");
}

int get_host_state(char *state, int host_no)
{
	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, SCSI_HOST_DIR"/host%d/state", host_no);
	return read_sysfs_file(sysfs_file, state, "%s\n");
}

int get_device_state(char *state, int host_no, int target, int lun)
{
	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, SCSI_DEVICE_DIR"/%d:0:%d:%d/state",
		host_no, target, lun);
	return read_sysfs_file(sysfs_file, state, "%s\n");
}

char *get_blockdev_from_lun(int host_no, int target, int lun)
{
	DIR *dirfd;
	struct dirent *dent;
	char *blockdev, *blockdup = NULL;

	sprintf(sysfs_file, SCSI_DEVICE_DIR"/%d:0:%d:%d",
		host_no, target, lun);
	dirfd = opendir(sysfs_file);
	if (!dirfd)
		return NULL;

	while ((dent = readdir(dirfd))) {
		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
			continue;

		/* not sure what tape looks like */
		if (strncmp(dent->d_name, "block:", 5))
			continue;

		blockdev = strchr(dent->d_name, ':');
		if (!blockdev)
			break;
		/* increment past colon */
		blockdev++;

		blockdup = strdup(blockdev);
		break;

	}
	closedir(dirfd);
	return blockdup;
}

static uint32_t get_target_no_from_sid(uint32_t sid, int *err)
{
	DIR *dirfd;
	struct dirent *dent;
	uint32_t host, bus, target = 0;

	*err = ENODEV;

	sprintf(sysfs_file, ISCSI_SESSION_DIR"/session%u/device/", sid);
	dirfd = opendir(sysfs_file);
	if (!dirfd)
		return 0;

	while ((dent = readdir(dirfd))) {
		if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
			continue;

		if (strncmp(dent->d_name, "target", 6))
			continue;

		if (sscanf(dent->d_name, "target%u:%u:%u",
			   &host, &bus, &target) != 3)
			break;

		*err = 0;
		break;

	}
	closedir(dirfd);
	return target;

}

struct iscsi_transport *get_transport_by_name(char *transport_name)
{
	struct iscsi_transport *t;

	/* sync up kernel and userspace */
	read_transports();

	/* check if the transport is loaded and matches */
	list_for_each_entry(t, &transports, list) {
		if (t->handle && !strncmp(t->name, transport_name,
					  ISCSI_TRANSPORT_NAME_MAXLEN))
			return t;
	}
	return NULL;
}

/* TODO: replace the following functions with some decent sysfs links */
struct iscsi_transport *get_transport_by_hba(long host_no)
{
	char name[ISCSI_TRANSPORT_NAME_MAXLEN];
	int rc;

	if (host_no == -1)
		return NULL;

	memset(sysfs_file, 0, PATH_MAX);
	sprintf(sysfs_file, SCSI_HOST_DIR"/host%lu/proc_name", host_no);
	rc = read_sysfs_file(sysfs_file, name, "%s\n");
	if (rc) {
		log_error("Could not read %s rc %d.", sysfs_file, rc);
		return NULL;
	}

	/*
	 * stupid, stupid. We named the transports tcp or iser, but the
	 * the modules are named iscsi_tcp and iscsi_iser
	 */
	if (strstr(name, "iscsi_"))
		return get_transport_by_name(name + 6);
	else
		return get_transport_by_name(name);
}

struct iscsi_transport *get_transport_by_sid(uint32_t sid)
{
	uint32_t host_no;
	int err;

	host_no = get_host_no_from_sid(sid, &err);
	if (err)
		return NULL;
	return get_transport_by_hba(host_no);
}

/*
 * For connection reinstatement we need to send the exp_statsn from
 * the previous connection
 *
 * This is only called when the connection is halted so exp_statsn is safe
 * to read without racing.
 */
int set_exp_statsn(iscsi_conn_t *conn)
{
	sprintf(sysfs_file,
		ISCSI_CONN_DIR"/connection%d:%d/exp_statsn",
		conn->session->id, conn->id);
	if (read_sysfs_file(sysfs_file, &conn->exp_statsn, "%u\n")) {
		log_error("Could not read %s. Using zero fpr exp_statsn.",
			  sysfs_file);
		conn->exp_statsn = 0;
	}
	return 0;
}

int sysfs_for_each_device(int host_no, uint32_t sid,
			  void (* fn)(int host_no, int target, int lun))
{
	struct dirent **namelist;
	int h, b, t, l, i, n, err = 0, target;

	target = get_target_no_from_sid(sid, &err);
	if (err)
		return err;

	sprintf(sysfs_file, ISCSI_SESSION_DIR"/session%d/device/target%d:0:%d",
		sid, host_no, target);
	n = scandir(sysfs_file, &namelist, trans_filter,
		    direntcmp);
	if (n <= 0)
		return 0;

	for (i = 0; i < n; i++) {
		if (sscanf(namelist[i]->d_name, "%d:%d:%d:%d\n",
			   &h, &b, &t, &l) != 4)
			continue;
		fn(h, t, l);
	}

	for (i = 0; i < n; i++)
		free(namelist[i]);
	free(namelist);

	return 0;
}

void set_device_online(int hostno, int target, int lun)
{
	int fd;

	sprintf(sysfs_file, SCSI_DEVICE_DIR"/%d:0:%d:%d/state",
		hostno, target, lun);
	fd = open(sysfs_file, O_WRONLY);
	if (fd < 0)
		return;
	log_debug(4, "online device using %s", sysfs_file);
	if (write(fd, "running\n", 8) == -1 && errno != EINVAL)
		/* we should read the state */
		log_error("Could not online LUN %d err %d.", lun, errno);
	close(fd);
}

/* TODO: remove this when we fix shutdown */
void delete_device(int hostno, int target, int lun)
{
	int fd;

	sprintf(sysfs_file, SCSI_DEVICE_DIR"/%d:0:%d:%d/delete",
		hostno, target, lun);
	fd = open(sysfs_file, O_WRONLY);
	if (fd < 0)
		return;
	log_debug(4, "deleting device using %s", sysfs_file);
	write(fd, "1", 1);
	close(fd);
}

void rescan_device(int hostno, int target, int lun)
{
	int fd;

	sprintf(sysfs_file, SCSI_DEVICE_DIR"/%d:0:%d:%d/rescan",
		hostno, target, lun);
	fd = open(sysfs_file, O_WRONLY);
	if (fd < 0)
		return;
	log_debug(4, "rescanning device using %s", sysfs_file);
	write(fd, "1", 1);
	close(fd);
}

pid_t scan_host(int hostno, int async)
{
	pid_t pid = 0;
	int fd;

	sprintf(sysfs_file, SCSI_HOST_DIR"/host%d/scan",
		hostno);
	fd = open(sysfs_file, O_WRONLY);
	if (fd < 0) {
		log_error("could not scan scsi host%d.", hostno);
		return -1;
	}

	if (async)
		pid = fork();
	if (pid == 0) {
		/* child */
		log_debug(4, "scanning host%d using %s", hostno,
			  sysfs_file);
		write(fd, "- - -", 5);
		log_debug(4, "scanning host%d completed\n", hostno);
	} else if (pid > 0) {
		log_debug(4, "scanning host%d from pid %d", hostno, pid);
	} else
		/*
		 * Session is fine, so log the error and let the user
		 * scan by hand
		  */
		log_error("Could not start scanning process for host %d "
			  "err %d. Try scanning through sysfs.", hostno,
			  errno);

	close(fd);
	return pid;
}

struct iscsi_transport *get_transport_by_session(char *sys_session)
{
	uint32_t sid;

        if (sscanf(sys_session, "session%u", &sid) != 1) {
                log_error("invalid session '%s'.", sys_session);
                return NULL;
        }

	return get_transport_by_sid(sid);
}

int get_iscsi_kernel_version(char *buf)
{
	if (read_sysfs_file(ISCSI_VERSION_FILE, buf, "%s\n"))
		return ENODATA;
	else
		return 0;
}

int check_class_version(void)
{
	char version[20];
	int i;

	if (get_iscsi_kernel_version(version))
		goto fail;

	log_warning("transport class version %s. iscsid version %s",
		    version, ISCSI_VERSION_STR);

	for (i = 0; i < strlen(version); i++) {
		if (version[i] == '-')
			break;
	}

	if (i == strlen(version))
		goto fail;

	/*
	 * We want to make sure the release and interface are the same.
	 * It is ok for the svn versions to be different.
	 */
	if (!strncmp(version, ISCSI_VERSION_STR, i) ||
	   /* support 2.6.18 */
	    !strncmp(version, "1.1", 3))
		return 0;

fail:
	log_error( "Missing or Invalid version from %s. Make sure a up "
		"to date scsi_transport_iscsi module is loaded and a up to"
		"date version of iscsid is running. Exiting...",
		ISCSI_VERSION_FILE);
	return -1;
}