Debian Codegraph @

src/istgt_lu.c

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (C) 2008-2011 Daisuke Aoyama <[email protected]>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <inttypes.h>
33 #include <stdint.h>
34
35 #include <ctype.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <pthread.h>
43 #ifdef HAVE_PTHREAD_NP_H
44 #include <pthread_np.h>
45 #endif
46 #include <time.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
51
52 #include <fcntl.h>
53 #include <unistd.h>
54 #include <sys/ioctl.h>
55 #ifdef HAVE_SYS_DISK_H
56 #include <sys/disk.h>
57 #endif
58 #ifdef HAVE_SYS_DISKLABEL_H
59 #include <sys/disklabel.h>
60 #endif
61 #ifdef __linux__
62 #include <linux/fs.h>
63 #endif
64
65 #include "istgt.h"
66 #include "istgt_ver.h"
67 #include "istgt_log.h"
68 #include "istgt_conf.h"
69 #include "istgt_sock.h"
70 #include "istgt_misc.h"
71 #include "istgt_md5.h"
72 #include "istgt_iscsi.h"
73 #include "istgt_lu.h"
74 #include "istgt_proto.h"
75 #include "istgt_scsi.h"
76
77 #define MAX_MASKBUF 128
78 static int
79 istgt_lu_allow_ipv6(const char *netmask, const char *addr)
80 {
81 struct in6_addr in6_mask;
82 struct in6_addr in6_addr;
83 char mask[MAX_MASKBUF];
84 const char *p;
85 int bits, bmask;
86 int n;
87 int i;
88
89 if (netmask[0] != '[')
90 return 0;
91 p = strchr(netmask, ']');
92 if (p == NULL)
93 return 0;
94 n = p - (netmask + 1);
95 if (n + 1 > sizeof mask)
96 return 0;
97
98 memcpy(mask, netmask + 1, n);
99 mask[n] = '\0';
100 p++;
101
102 if (p[0] == '/') {
103 bits = (int) strtol(p + 1, NULL, 10);
104 if (bits < 0 || bits > 128)
105 return 0;
106 } else {
107 bits = 128;
108 }
109
110 #if 0
111 printf("input %s\n", addr);
112 printf("mask %s / %d\n", mask, bits);
113 #endif
114
115 /* presentation to network order binary */
116 if (inet_pton(AF_INET6, mask, &in6_mask) <= 0
117 || inet_pton(AF_INET6, addr, &in6_addr) <= 0) {
118 return 0;
119 }
120
121 /* check 128bits */
122 for (i = 0; i < (bits / 8); i++) {
123 if (in6_mask.s6_addr[i] != in6_addr.s6_addr[i])
124 return 0;
125 }
126 if (bits % 8) {
127 bmask = (0xffU << (8 - (bits % 8))) & 0xffU;
128 if ((in6_mask.s6_addr[i] & bmask) != (in6_addr.s6_addr[i] & bmask))
129 return 0;
130 }
131
132 /* match */
133 return 1;
134 }
135
136 static int
137 istgt_lu_allow_ipv4(const char *netmask, const char *addr)
138 {
139 struct in_addr in4_mask;
140 struct in_addr in4_addr;
141 char mask[MAX_MASKBUF];
142 const char *p;
143 uint32_t bmask;
144 int bits;
145 int n;
146
147 p = strchr(netmask, '/');
148 if (p == NULL) {
149 p = netmask + strlen(netmask);
150 }
151 n = p - netmask;
152 if (n + 1 > sizeof mask)
153 return 0;
154
155 memcpy(mask, netmask, n);
156 mask[n] = '\0';
157
158 if (p[0] == '/') {
159 bits = (int) strtol(p + 1, NULL, 10);
160 if (bits < 0 || bits > 32)
161 return 0;
162 } else {
163 bits = 32;
164 }
165
166 #if 0
167 printf("input %s\n", addr);
168 printf("mask %s / %d\n", mask, bits);
169 #endif
170
171 /* presentation to network order binary */
172 if (inet_pton(AF_INET, mask, &in4_mask) <= 0
173 || inet_pton(AF_INET, addr, &in4_addr) <= 0) {
174 return 0;
175 }
176
177 /* check 32bits */
178 bmask = (0xffffffffU << (32 - bits)) & 0xffffffffU;
179 if ((ntohl(in4_mask.s_addr) & bmask) != (ntohl(in4_addr.s_addr) & bmask))
180 return 0;
181
182 /* match */
183 return 1;
184 }
185
186 int
187 istgt_lu_allow_netmask(const char *netmask, const char *addr)
188 {
189 if (netmask == NULL || addr == NULL)
190 return 0;
191 if (strcasecmp(netmask, "ALL") == 0)
192 return 1;
193 if (netmask[0] == '[') {
194 /* IPv6 */
195 if (istgt_lu_allow_ipv6(netmask, addr))
196 return 1;
197 } else {
198 /* IPv4 */
199 if (istgt_lu_allow_ipv4(netmask, addr))
200 return 1;
201 }
202 return 0;
203 }
204
205 int
206 istgt_lu_access(CONN_Ptr conn, ISTGT_LU_Ptr lu, const char *iqn, const char *addr)
207 {
208 ISTGT_Ptr istgt;
209 INITIATOR_GROUP *igp;
210 int pg_tag;
211 int ig_tag;
212 int rc;
213 int i, j, k;
214
215 if (conn == NULL || lu == NULL || iqn == NULL || addr == NULL)
216 return 0;
217 istgt = conn->istgt;
218 pg_tag = conn->portal.tag;
219
220 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "pg=%d, iqn=%s, addr=%s\n",
221 pg_tag, iqn, addr);
222 for (i = 0; i < lu->maxmap; i++) {
223 /* skip excluding self portal group tag */
224 if (pg_tag != lu->map[i].pg_tag)
225 continue;
226 /* iqn is initiator group? */
227 ig_tag = lu->map[i].ig_tag;
228 igp = istgt_lu_find_initiatorgroup(istgt, ig_tag);
229 if (igp == NULL) {
230 ISTGT_ERRLOG("LU%d: ig_tag not found\n", lu->num);
231 continue;
232 }
233 for (j = 0; j < igp->ninitiators; j++) {
234 /* deny initiators */
235 if (igp->initiators[j][0] == '!'
236 && (strcasecmp(&igp->initiators[j][1], "ALL") == 0
237 || strcasecmp(&igp->initiators[j][1], iqn) == 0)) {
238 /* NG */
239 ISTGT_WARNLOG("access denied from %s (%s) to %s (%s:%s,%d)\n",
240 iqn, addr, conn->target_name, conn->portal.host,
241 conn->portal.port, conn->portal.tag);
242 return 0;
243 }
244 /* allow initiators */
245 if (strcasecmp(igp->initiators[j], "ALL") == 0
246 || strcasecmp(igp->initiators[j], iqn) == 0) {
247 /* OK iqn, check netmask */
248 if (igp->nnetmasks == 0) {
249 /* OK, empty netmask as ALL */
250 return 1;
251 }
252 for (k = 0; k < igp->nnetmasks; k++) {
253 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
254 "netmask=%s, addr=%s\n",
255 igp->netmasks[k], addr);
256 rc = istgt_lu_allow_netmask(igp->netmasks[k], addr);
257 if (rc > 0) {
258 /* OK netmask */
259 return 1;
260 }
261 }
262 /* NG netmask in this group */
263 }
264 }
265 }
266
267 /* NG */
268 ISTGT_WARNLOG("access denied from %s (%s) to %s (%s:%s,%d)\n",
269 iqn, addr, conn->target_name, conn->portal.host,
270 conn->portal.port, conn->portal.tag);
271 return 0;
272 }
273
274 int
275 istgt_lu_visible(ISTGT_Ptr istgt, ISTGT_LU_Ptr lu, const char *iqn, int pg_tag)
276 {
277 INITIATOR_GROUP *igp;
278 int match_pg_tag;
279 int ig_tag;
280 int i, j;
281
282 if (istgt == NULL || lu == NULL || iqn == NULL)
283 return 0;
284 /* pg_tag exist map? */
285 match_pg_tag = 0;
286 for (i = 0; i < lu->maxmap; i++) {
287 if (lu->map[i].pg_tag == pg_tag) {
288 match_pg_tag = 1;
289 break;
290 }
291 }
292 if (match_pg_tag == 0) {
293 /* cat't access from pg_tag */
294 return 0;
295 }
296 for (i = 0; i < lu->maxmap; i++) {
297 /* iqn is initiator group? */
298 ig_tag = lu->map[i].ig_tag;
299 igp = istgt_lu_find_initiatorgroup(istgt, ig_tag);
300 if (igp == NULL) {
301 ISTGT_ERRLOG("LU%d: ig_tag not found\n", lu->num);
302 continue;
303 }
304 for (j = 0; j < igp->ninitiators; j++) {
305 if (igp->initiators[j][0] == '!'
306 && (strcasecmp(&igp->initiators[j][1], "ALL") == 0
307 || strcasecmp(&igp->initiators[j][1], iqn) == 0)) {
308 /* NG */
309 return 0;
310 }
311 if (strcasecmp(igp->initiators[j], "ALL") == 0
312 || strcasecmp(igp->initiators[j], iqn) == 0) {
313 /* OK iqn, no check addr */
314 return 1;
315 }
316 }
317 }
318
319 /* NG */
320 return 0;
321 }
322
323 int
324 istgt_lu_sendtargets(CONN_Ptr conn, const char *iiqn, const char *iaddr, const char *tiqn, uint8_t *data, int alloc_len, int data_len)
325 {
326 char buf[MAX_TMPBUF];
327 ISTGT_Ptr istgt;
328 ISTGT_LU_Ptr lu;
329 char *host;
330 int total;
331 int len;
332 int rc;
333 int pg_tag;
334 int i, j, k;
335
336 if (conn == NULL)
337 return 0;
338 istgt = conn->istgt;
339
340 total = data_len;
341 if (alloc_len < 1) {
342 return 0;
343 }
344 if (total > alloc_len) {
345 total = alloc_len;
346 data[total - 1] = '\0';
347 return total;
348 }
349
350 if (alloc_len - total < 1) {
351 ISTGT_ERRLOG("data space small %d\n", alloc_len);
352 return total;
353 }
354
355 MTX_LOCK(&istgt->mutex);
356 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
357 lu = istgt->logical_unit[i];
358 if (lu == NULL)
359 continue;
360 if (strcasecmp(tiqn, "ALL") != 0
361 && strcasecmp(tiqn, lu->name) != 0) {
362 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
363 "SKIP iqn=%s for %s from %s (%s)\n",
364 tiqn, lu->name, iiqn, iaddr);
365 continue;
366 }
367 rc = istgt_lu_visible(istgt, lu, iiqn, conn->portal.tag);
368 if (rc == 0) {
369 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
370 "SKIP iqn=%s for %s from %s (%s)\n",
371 tiqn, lu->name, iiqn, iaddr);
372 continue;
373 }
374
375 /* DO SENDTARGETS */
376 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
377 "OK iqn=%s for %s from %s (%s)\n",
378 tiqn, lu->name, iiqn, iaddr);
379
380 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
381 "TargetName=%s\n", lu->name);
382 len = snprintf((char *) data + total, alloc_len - total,
383 "TargetName=%s", lu->name);
384 total += len + 1;
385
386 for (j = 0; j < lu->maxmap; j++) {
387 pg_tag = lu->map[j].pg_tag;
388 /* skip same pg_tag */
389 for (k = 0; k < j; k++) {
390 if (lu->map[k].pg_tag == pg_tag) {
391 goto skip_pg_tag;
392 }
393 }
394 /* write to data */
395 for (k = 0; k < istgt->nportal; k++) {
396 if (istgt->portal[k].tag == pg_tag) {
397 if (alloc_len - total < 1) {
398 MTX_UNLOCK(&istgt->mutex);
399 ISTGT_ERRLOG("data space small %d\n", alloc_len);
400 return total;
401 }
402 host = istgt->portal[k].host;
403 /* wildcard? */
404 if (strcasecmp(host, "[::]") == 0
405 || strcasecmp(host, "[*]") == 0
406 || strcasecmp(host, "0.0.0.0") == 0
407 || strcasecmp(host, "*") == 0) {
408 if ((strcasecmp(host, "[::]") == 0
409 || strcasecmp(host, "[*]") == 0)
410 && conn->initiator_family == AF_INET6) {
411 snprintf(buf, sizeof buf, "[%s]",
412 conn->target_addr);
413 host = buf;
414 } else if ((strcasecmp(host, "0.0.0.0") == 0
415 || strcasecmp(host, "*") == 0)
416 && conn->initiator_family == AF_INET) {
417 snprintf(buf, sizeof buf, "%s",
418 conn->target_addr);
419 host = buf;
420 } else {
421 /* skip portal for the family */
422 continue;
423 }
424 }
425 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
426 "TargetAddress=%s:%s,%d\n",
427 host,
428 istgt->portal[k].port,
429 istgt->portal[k].tag);
430 len = snprintf((char *) data + total,
431 alloc_len - total,
432 "TargetAddress=%s:%s,%d",
433 host,
434 istgt->portal[k].port,
435 istgt->portal[k].tag);
436 total += len + 1;
437 }
438 }
439 skip_pg_tag:
440 ;
441 }
442 }
443 MTX_UNLOCK(&istgt->mutex);
444
445 return total;
446 }
447
448 ISTGT_LU_Ptr
449 istgt_lu_find_target(ISTGT_Ptr istgt, const char *target_name)
450 {
451 ISTGT_LU_Ptr lu;
452 int i;
453
454 if (istgt == NULL || target_name == NULL)
455 return NULL;
456 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
457 lu = istgt->logical_unit[i];
458 if (lu == NULL)
459 continue;
460 if (strcasecmp(target_name, lu->name) == 0) {
461 return lu;
462 }
463 }
464 ISTGT_WARNLOG("can't find target %s\n",
465 target_name);
466 return NULL;
467 }
468
469 uint16_t
470 istgt_lu_allocate_tsih(ISTGT_LU_Ptr lu, const char *initiator_port, int tag)
471 {
472 uint16_t tsih;
473 int retry = 10;
474 int i;
475
476 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_allocate_tsih\n");
477 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "initiator_port=%s, tag=%d\n",
478 initiator_port, tag);
479 if (lu == NULL || initiator_port == NULL || tag == 0)
480 return 0;
481 /* tsih 0 is reserved */
482 tsih = 0;
483 MTX_LOCK(&lu->mutex);
484 #if 0
485 for (i = 1; i < MAX_LU_TSIH; i++) {
486 if (lu->tsih[i].initiator_port == NULL)
487 continue;
488 if (tag != lu->tsih[i].tag)
489 continue;
490 if (strcasecmp(initiator_port, lu->tsih[i].initiator_port) == 0) {
491 tsih = lu->tsih[i].tsih;
492 break;
493 }
494 }
495 #endif
496 if (tsih == 0) {
497 if (lu->maxtsih >= MAX_LU_TSIH) {
498 ISTGT_ERRLOG("LU%d: tsih is maximum\n", lu->num);
499 MTX_UNLOCK(&lu->mutex);
500 return 0;
501 }
502 retry:
503 lu->last_tsih++;
504 tsih = lu->last_tsih;
505 if (tsih == 0) {
506 if (retry > 0) {
507 retry--;
508 goto retry;
509 }
510 ISTGT_ERRLOG("LU%d: retry error\n", lu->num);
511 MTX_UNLOCK(&lu->mutex);
512 return 0;
513 }
514 for (i = 1; i < MAX_LU_TSIH; i++) {
515 if (lu->tsih[i].initiator_port != NULL
516 && lu->tsih[i].tsih == tsih) {
517 ISTGT_ERRLOG("tsih is found in list\n");
518 if (retry > 0) {
519 retry--;
520 goto retry;
521 }
522 ISTGT_ERRLOG("LU%d: retry error\n", lu->num);
523 MTX_UNLOCK(&lu->mutex);
524 return 0;
525 }
526 }
527 for (i = 1; i < MAX_LU_TSIH; i++) {
528 if (lu->tsih[i].initiator_port == NULL) {
529 lu->tsih[i].tag = tag;
530 lu->tsih[i].tsih = tsih;
531 lu->tsih[i].initiator_port = xstrdup(initiator_port);
532 lu->maxtsih++;
533 break;
534 }
535 }
536 }
537 MTX_UNLOCK(&lu->mutex);
538 return tsih;
539 }
540
541 void
542 istgt_lu_free_tsih(ISTGT_LU_Ptr lu, uint16_t tsih, char *initiator_port)
543 {
544 int i;
545
546 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_free_tsih\n");
547 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "tsih=%u, initiator_port=%s\n",
548 tsih, initiator_port);
549 if (lu == NULL || initiator_port == NULL)
550 return;
551 if (tsih == 0)
552 return;
553
554 MTX_LOCK(&lu->mutex);
555 for (i = 1; i < MAX_LU_TSIH; i++) {
556 if (lu->tsih[i].initiator_port == NULL)
557 continue;
558 if (lu->tsih[i].tsih != tsih)
559 continue;
560
561 if (strcasecmp(initiator_port, lu->tsih[i].initiator_port) == 0) {
562 lu->tsih[i].tag = 0;
563 lu->tsih[i].tsih = 0;
564 xfree(lu->tsih[i].initiator_port);
565 lu->tsih[i].initiator_port = NULL;
566 lu->maxtsih--;
567 break;
568 }
569 }
570 MTX_UNLOCK(&lu->mutex);
571 return;
572 }
573
574 char *
575 istgt_lu_get_media_flags_string(int flags, char *buf, size_t len)
576 {
577 char *p;
578 size_t rest;
579
580 p = buf;
581 rest = len;
582 if (flags & ISTGT_LU_FLAG_MEDIA_READONLY) {
583 snprintf(p, rest, "%s", "ro");
584 } else {
585 snprintf(p, rest, "%s", "rw");
586 }
587 p = buf + strlen(buf);
588 rest = len - strlen(buf);
589 if (flags & ISTGT_LU_FLAG_MEDIA_EXTEND) {
590 snprintf(p, rest, ",%s", "extend");
591 }
592 p = buf + strlen(buf);
593 rest = len - strlen(buf);
594 if (flags & ISTGT_LU_FLAG_MEDIA_DYNAMIC) {
595 snprintf(p, rest, ",%s", "dynamic");
596 }
597 return buf;
598 }
599
600 uint64_t
601 istgt_lu_get_devsize(const char *file)
602 {
603 uint64_t val;
604 struct stat st;
605 int fd;
606 int rc;
607
608 val = 0ULL;
609 rc = stat(file, &st);
610 if (rc != 0)
611 return val;
612 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode))
613 return val;
614
615 fd = open(file, O_RDONLY, 0);
616 if (fd >= 0) {
617 #ifdef DIOCGMEDIASIZE
618 if (val == 0) {
619 off_t offset;
620 rc = ioctl(fd, DIOCGMEDIASIZE, &offset);
621 if (rc != -1) {
622 val = (uint64_t) offset;
623 }
624 }
625 #endif /* DIOCGMEDIASIZE */
626 #ifdef DIOCGDINFO
627 if (val == 0) {
628 struct disklabel dl;
629 rc = ioctl(fd, DIOCGDINFO, &dl);
630 if (rc != -1) {
631 val = (uint64_t) dl.d_secperunit;
632 val *= (uint64_t) dl.d_secsize;
633 }
634 }
635 #endif /* DIOCGDINFO */
636 #if defined(DKIOCGETBLOCKSIZE) && defined(DKIOCGETBLOCKCOUNT)
637 if (val == 0) {
638 uint32_t blocklen;
639 uint64_t blockcnt;
640 rc = ioctl(fd, DKIOCGETBLOCKSIZE, &blocklen);
641 if (rc != -1) {
642 rc = ioctl(fd, DKIOCGETBLOCKCOUNT, &blockcnt);
643 if (rc != -1) {
644 val = (uint64_t) blocklen;
645 val *= (uint64_t) blockcnt;
646 }
647 }
648 }
649 #endif /* DKIOCGETBLOCKSIZE && DKIOCGETBLOCKCOUNT */
650 #ifdef __linux__
651 #ifdef BLKGETSIZE64
652 if (val == 0) {
653 uint64_t blocksize;
654 rc = ioctl(fd, BLKGETSIZE64, &blocksize);
655 if (rc != -1) {
656 val = (uint64_t) blocksize;
657 }
658 }
659 #endif /* BLKGETSIZE64 */
660 #ifdef BLKGETSIZE
661 if (val == 0) {
662 uint32_t blocksize;
663 rc = ioctl(fd, BLKGETSIZE, &blocksize);
664 if (rc != -1) {
665 val = (uint64_t) 512;
666 val *= (uint64_t) blocksize;
667 }
668 }
669 #endif /* BLKGETSIZE */
670 #endif /* __linux__ */
671 if (val == 0) {
672 ISTGT_ERRLOG("unknown device size\n");
673 }
674 (void) close(fd);
675 } else {
676 if (g_trace_flag) {
677 ISTGT_WARNLOG("open error %s (errno=%d)\n", file, errno);
678 }
679 val = 0ULL;
680 }
681 return val;
682 }
683
684 uint64_t
685 istgt_lu_get_filesize(const char *file)
686 {
687 uint64_t val;
688 struct stat st;
689 int rc;
690
691 val = 0ULL;
692 rc = stat(file, &st);
693 if (rc < 0)
694 return val;
695
696 if (S_ISCHR(st.st_mode)) {
697 val = istgt_lu_get_devsize(file);
698 } else if (S_ISBLK(st.st_mode)) {
699 val = istgt_lu_get_devsize(file);
700 } else if (S_ISREG(st.st_mode)) {
701 val = st.st_size;
702 } else {
703 ISTGT_ERRLOG("stat is neither REG, CHR nor BLK\n");
704 val = 0ULL;
705 }
706 return val;
707 }
708
709 uint64_t
710 istgt_lu_parse_size(const char *size)
711 {
712 uint64_t val, val1, val2;
713 char *endp, *p;
714 size_t idx;
715 int sign;
716
717 val1 = (uint64_t) strtoull(size, &endp, 10);
718 val = val1;
719 val2 = 0;
720 if (endp != NULL) {
721 p = endp;
722 switch (toupper((int) *p)) {
723 case 'Z': val1 *= (uint64_t) 1024ULL;
724 case 'E': val1 *= (uint64_t) 1024ULL;
725 case 'P': val1 *= (uint64_t) 1024ULL;
726 case 'T': val1 *= (uint64_t) 1024ULL;
727 case 'G': val1 *= (uint64_t) 1024ULL;
728 case 'M': val1 *= (uint64_t) 1024ULL;
729 case 'K': val1 *= (uint64_t) 1024ULL;
730 break;
731 }
732 val = val1;
733 p++;
734 idx = strspn(p, "Bb \t");
735 p += idx;
736 if (*p == '-' || *p == '+') {
737 sign = (int) *p++;
738 idx = strspn(p, " \t");
739 p += idx;
740 val2 = (uint64_t) strtoull(p, &endp, 10);
741 if (endp != NULL) {
742 p = endp;
743 switch (toupper((int) *p)) {
744 case 'Z': val2 *= (uint64_t) 1024ULL;
745 case 'E': val2 *= (uint64_t) 1024ULL;
746 case 'P': val2 *= (uint64_t) 1024ULL;
747 case 'T': val2 *= (uint64_t) 1024ULL;
748 case 'G': val2 *= (uint64_t) 1024ULL;
749 case 'M': val2 *= (uint64_t) 1024ULL;
750 case 'K': val2 *= (uint64_t) 1024ULL;
751 break;
752 }
753 }
754 if (sign == '-') {
755 if (val2 > val1) {
756 /* underflow */
757 val = (uint64_t) 0ULL;
758 } else {
759 val = val1 - val2;
760 }
761 } else {
762 if (val2 > (UINT64_MAX - val1)) {
763 /* overflow */
764 val = UINT64_MAX;
765 } else {
766 val = val1 + val2;
767 }
768 }
769 }
770 }
771 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
772 "size=%s, val=%"PRIu64", val1=%"PRIu64", val2=%"PRIu64"\n",
773 size, val, val1, val2);
774 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
775 "size=%s, val=%"PRIx64", val1=%"PRIx64", val2=%"PRIx64"\n",
776 size, val, val1, val2);
777
778 return val;
779 }
780
781 int
782 istgt_lu_parse_media_flags(const char *flags)
783 {
784 char buf[MAX_TMPBUF];
785 const char *delim = ",";
786 char *next_p;
787 char *p;
788 int mflags;
789
790 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "mflags=%s\n", flags);
791 mflags = 0;
792 strlcpy(buf, flags, MAX_TMPBUF);
793 next_p = buf;
794 while ((p = strsep(&next_p, delim)) != NULL) {
795 if (strcasecmp(p, "ro") == 0) {
796 mflags |= ISTGT_LU_FLAG_MEDIA_READONLY;
797 } else if (strcasecmp(p, "rw") == 0) {
798 mflags &= ~ISTGT_LU_FLAG_MEDIA_READONLY;
799 } else if (strcasecmp(p, "extend") == 0) {
800 mflags |= ISTGT_LU_FLAG_MEDIA_EXTEND;
801 } else if (strcasecmp(p, "dynamic") == 0) {
802 mflags |= ISTGT_LU_FLAG_MEDIA_DYNAMIC;
803 } else {
804 ISTGT_ERRLOG("unknown media flag %.64s\n", p);
805 }
806 }
807
808 return mflags;
809 }
810
811 uint64_t
812 istgt_lu_parse_media_size(const char *file, const char *size, int *flags)
813 {
814 uint64_t msize, fsize;
815
816 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "msize=%s\n", size);
817 if (strcasecmp(file, "/dev/null") == 0) {
818 return 0;
819 }
820 if (strcasecmp(size, "Auto") == 0
821 || strcasecmp(size, "Size") == 0) {
822 msize = istgt_lu_get_filesize(file);
823 if (msize == 0) {
824 msize = ISTGT_LU_MEDIA_SIZE_MIN;
825 }
826 *flags |= ISTGT_LU_FLAG_MEDIA_AUTOSIZE;
827 } else {
828 msize = istgt_lu_parse_size(size);
829 if (*flags & ISTGT_LU_FLAG_MEDIA_EXTEND) {
830 fsize = istgt_lu_get_filesize(file);
831 if (fsize > msize) {
832 msize = fsize;
833 }
834 }
835 }
836
837 if (*flags & ISTGT_LU_FLAG_MEDIA_DYNAMIC) {
838 if (msize < ISTGT_LU_MEDIA_SIZE_MIN) {
839 msize = ISTGT_LU_MEDIA_SIZE_MIN;
840 }
841 } else {
842 if (msize < ISTGT_LU_MEDIA_SIZE_MIN) {
843 ISTGT_ERRLOG("media size too small\n");
844 return 0ULL;
845 }
846 }
847
848 return msize;
849 }
850
851 PORTAL *
852 istgt_lu_find_portalgroup(ISTGT_Ptr istgt, int tag)
853 {
854 PORTAL *pp;
855 int i;
856
857 for (i = 0; i < istgt->nportal; i++) {
858 if (istgt->portal[i].tag == tag) {
859 pp = &istgt->portal[i];
860 return pp;
861 }
862 }
863 return NULL;
864 }
865
866 INITIATOR_GROUP *
867 istgt_lu_find_initiatorgroup(ISTGT_Ptr istgt, int tag)
868 {
869 INITIATOR_GROUP *igp;
870 int i;
871
872 for (i = 0; i < istgt->ninitiator_group; i++) {
873 if (istgt->initiator_group[i].tag == tag) {
874 igp = &istgt->initiator_group[i];
875 return igp;
876 }
877 }
878 return NULL;
879 }
880
881 static int
882 istgt_lu_check_iscsi_name(const char *name)
883 {
884 const unsigned char *up = (const unsigned char *) name;
885 size_t n;
886
887 /* valid iSCSI name? */
888 for (n = 0; up[n] != 0; n++) {
889 if (up[n] > 0x00U && up[n] <= 0x2cU)
890 return -1;
891 if (up[n] == 0x2fU)
892 return -1;
893 if (up[n] >= 0x3bU && up[n] <= 0x40U)
894 return -1;
895 if (up[n] >= 0x5bU && up[n] <= 0x60U)
896 return -1;
897 if (up[n] >= 0x7bU && up[n] <= 0x7fU)
898 return -1;
899 if (isspace(up[n]))
900 return -1;
901 }
902 /* valid format? */
903 if (strncasecmp(name, "iqn.", 4) == 0) {
904 /* iqn.YYYY-MM.reversed.domain.name */
905 if (!isdigit(up[4]) || !isdigit(up[5]) || !isdigit(up[6])
906 || !isdigit(up[7]) || up[8] != '-' || !isdigit(up[9])
907 || !isdigit(up[10]) || up[11] != '.') {
908 ISTGT_ERRLOG("invalid iqn format. "
909 "expect \"iqn.YYYY-MM.reversed.domain.name\"\n");
910 return -1;
911 }
912 } else if (strncasecmp(name, "eui.", 4) == 0) {
913 /* EUI-64 -> 16bytes */
914 /* XXX */
915 } else if (strncasecmp(name, "naa.", 4) == 0) {
916 /* 64bit -> 16bytes, 128bit -> 32bytes */
917 /* XXX */
918 }
919 /* OK */
920 return 0;
921 }
922
923 static uint64_t
924 istgt_lu_get_nbserial(const char *nodebase)
925 {
926 ISTGT_MD5CTX md5ctx;
927 uint8_t nbsmd5[ISTGT_MD5DIGEST_LEN];
928 char buf[MAX_TMPBUF];
929 uint64_t nbs;
930 int idx;
931 int i;
932
933 snprintf(buf, sizeof buf, "%s", nodebase);
934 if (strcasecmp(buf, "iqn.2007-09.jp.ne.peach.istgt") == 0
935 || strcasecmp(buf, "iqn.2007-09.jp.ne.peach") == 0) {
936 /* always zero */
937 return 0;
938 }
939
940 istgt_md5init(&md5ctx);
941 istgt_md5update(&md5ctx, buf, strlen(buf));
942 istgt_md5final(nbsmd5, &md5ctx);
943
944 nbs = 0U;
945 idx = ISTGT_MD5DIGEST_LEN - 8;
946 if (idx < 0) {
947 ISTGT_WARNLOG("missing MD5 length\n");
948 idx = 0;
949 }
950 for (i = idx; i < ISTGT_MD5DIGEST_LEN; i++) {
951 nbs |= (uint64_t) nbsmd5[i];
952 nbs = nbs << 8;
953 }
954 return nbs;
955 }
956
957 static int
958 istgt_lu_set_local_settings(ISTGT_Ptr istgt, CF_SECTION *sp, ISTGT_LU_Ptr lu)
959 {
960 const char *val;
961
962 val = istgt_get_val(sp, "MaxOutstandingR2T");
963 if (val == NULL) {
964 lu->MaxOutstandingR2T = lu->istgt->MaxOutstandingR2T;
965 } else {
966 lu->MaxOutstandingR2T = (int)strtol(val, NULL, 10);
967 if (lu->MaxOutstandingR2T < 1) {
968 lu->MaxOutstandingR2T = DEFAULT_MAXOUTSTANDINGR2T;
969 }
970 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxOutstandingR2T %d\n",
971 lu->MaxOutstandingR2T);
972 }
973
974 val = istgt_get_val(sp, "DefaultTime2Wait");
975 if (val == NULL) {
976 lu->DefaultTime2Wait = lu->istgt->DefaultTime2Wait;
977 } else {
978 lu->DefaultTime2Wait = (int)strtol(val, NULL, 10);
979 if (lu->DefaultTime2Wait < 0) {
980 lu->DefaultTime2Wait = DEFAULT_DEFAULTTIME2WAIT;
981 }
982 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DefaultTime2Wait %d\n",
983 lu->DefaultTime2Wait);
984 }
985
986 val = istgt_get_val(sp, "DefaultTime2Retain");
987 if (val == NULL) {
988 lu->DefaultTime2Retain = lu->istgt->DefaultTime2Retain;
989 } else {
990 lu->DefaultTime2Retain = (int)strtol(val, NULL, 10);
991 if (lu->DefaultTime2Retain < 0) {
992 lu->DefaultTime2Retain = DEFAULT_DEFAULTTIME2RETAIN;
993 }
994 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DefaultTime2Retain %d\n",
995 lu->DefaultTime2Retain);
996 }
997
998 /* check size limit - RFC3720(12.15, 12.16, 12.17) */
999 if (lu->MaxOutstandingR2T > 65535) {
1000 ISTGT_ERRLOG("MaxOutstandingR2T(%d) > 65535\n",
1001 lu->MaxOutstandingR2T);
1002 return -1;
1003 }
1004 if (lu->DefaultTime2Wait > 3600) {
1005 ISTGT_ERRLOG("DefaultTime2Wait(%d) > 3600\n",
1006 lu->DefaultTime2Wait);
1007 return -1;
1008 }
1009 if (lu->DefaultTime2Retain > 3600) {
1010 ISTGT_ERRLOG("DefaultTime2Retain(%d) > 3600\n",
1011 lu->DefaultTime2Retain);
1012 return -1;
1013 }
1014
1015 val = istgt_get_val(sp, "FirstBurstLength");
1016 if (val == NULL) {
1017 lu->FirstBurstLength = lu->istgt->FirstBurstLength;
1018 } else {
1019 lu->FirstBurstLength = (int)strtol(val, NULL, 10);
1020 if (lu->FirstBurstLength < 0) {
1021 lu->FirstBurstLength = DEFAULT_FIRSTBURSTLENGTH;
1022 }
1023 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "FirstBurstLength %d\n",
1024 lu->FirstBurstLength);
1025 }
1026
1027 val = istgt_get_val(sp, "MaxBurstLength");
1028 if (val == NULL) {
1029 lu->MaxBurstLength = lu->istgt->MaxBurstLength;
1030 } else {
1031 lu->MaxBurstLength = (int)strtol(val, NULL, 10);
1032 if (lu->MaxBurstLength < 0) {
1033 lu->MaxBurstLength = DEFAULT_MAXBURSTLENGTH;
1034 }
1035 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "MaxBurstLength %d\n",
1036 lu->MaxBurstLength);
1037 }
1038
1039 val = istgt_get_val(sp, "MaxRecvDataSegmentLength");
1040 if (val == NULL) {
1041 lu->MaxRecvDataSegmentLength
1042 = lu->istgt->MaxRecvDataSegmentLength;
1043 } else {
1044 lu->MaxRecvDataSegmentLength = (int)strtol(val, NULL, 10);
1045 if (lu->MaxRecvDataSegmentLength < 0) {
1046 lu->MaxRecvDataSegmentLength
1047 = DEFAULT_MAXRECVDATASEGMENTLENGTH;
1048 }
1049 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1050 "MaxRecvDataSegmentLength %d\n",
1051 lu->MaxRecvDataSegmentLength);
1052 }
1053
1054 /* check size limit (up to 24bits - RFC3720(12.12)) */
1055 if (lu->MaxBurstLength < 512) {
1056 ISTGT_ERRLOG("MaxBurstLength(%d) < 512\n",
1057 lu->MaxBurstLength);
1058 return -1;
1059 }
1060 if (lu->FirstBurstLength < 512) {
1061 ISTGT_ERRLOG("FirstBurstLength(%d) < 512\n",
1062 lu->FirstBurstLength);
1063 return -1;
1064 }
1065 if (lu->FirstBurstLength > lu->MaxBurstLength) {
1066 ISTGT_ERRLOG("FirstBurstLength(%d) > MaxBurstLength(%d)\n",
1067 lu->FirstBurstLength, istgt->MaxBurstLength);
1068 return -1;
1069 }
1070 if (lu->MaxBurstLength > 0x00ffffff) {
1071 ISTGT_ERRLOG("MaxBurstLength(%d) > 0x00ffffff\n",
1072 lu->MaxBurstLength);
1073 return -1;
1074 }
1075 if (lu->MaxRecvDataSegmentLength < 512) {
1076 ISTGT_ERRLOG("MaxRecvDataSegmentLength(%d) < 512\n",
1077 lu->MaxRecvDataSegmentLength);
1078 return -1;
1079 }
1080 if (lu->MaxRecvDataSegmentLength > 0x00ffffff) {
1081 ISTGT_ERRLOG("MaxRecvDataSegmentLength(%d) > 0x00ffffff\n",
1082 lu->MaxRecvDataSegmentLength);
1083 return -1;
1084 }
1085
1086 val = istgt_get_val(sp, "InitialR2T");
1087 if (val == NULL) {
1088 lu->InitialR2T = lu->istgt->InitialR2T;
1089 } else {
1090 if (strcasecmp(val, "Yes") == 0) {
1091 lu->InitialR2T = 1;
1092 } else if (strcasecmp(val, "No") == 0) {
1093 #if 0
1094 lu->InitialR2T = 0;
1095 #else
1096 ISTGT_ERRLOG("not supported value %s\n", val);
1097 return -1;
1098 #endif
1099 } else {
1100 ISTGT_ERRLOG("unknown value %s\n", val);
1101 return -1;
1102 }
1103 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "InitialR2T %s\n",
1104 lu->InitialR2T ? "Yes" : "No");
1105 }
1106
1107 val = istgt_get_val(sp, "ImmediateData");
1108 if (val == NULL) {
1109 lu->ImmediateData = lu->istgt->ImmediateData;
1110 } else {
1111 if (strcasecmp(val, "Yes") == 0) {
1112 lu->ImmediateData = 1;
1113 } else if (strcasecmp(val, "No") == 0) {
1114 lu->ImmediateData = 0;
1115 } else {
1116 ISTGT_ERRLOG("unknown value %s\n", val);
1117 return -1;
1118 }
1119 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "ImmediateData %s\n",
1120 lu->ImmediateData ? "Yes" : "No");
1121 }
1122
1123 val = istgt_get_val(sp, "DataPDUInOrder");
1124 if (val == NULL) {
1125 lu->DataPDUInOrder = lu->istgt->DataPDUInOrder;
1126 } else {
1127 if (strcasecmp(val, "Yes") == 0) {
1128 lu->DataPDUInOrder = 1;
1129 } else if (strcasecmp(val, "No") == 0) {
1130 #if 0
1131 lu->DataPDUInOrder = 0;
1132 #else
1133 ISTGT_ERRLOG("not supported value %s\n", val);
1134 return -1;
1135 #endif
1136 } else {
1137 ISTGT_ERRLOG("unknown value %s\n", val);
1138 return -1;
1139 }
1140 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DataPDUInOrder %s\n",
1141 lu->DataPDUInOrder ? "Yes" : "No");
1142 }
1143
1144 val = istgt_get_val(sp, "DataSequenceInOrder");
1145 if (val == NULL) {
1146 lu->DataSequenceInOrder = lu->istgt->DataSequenceInOrder;
1147 } else {
1148 if (strcasecmp(val, "Yes") == 0) {
1149 lu->DataSequenceInOrder = 1;
1150 } else if (strcasecmp(val, "No") == 0) {
1151 #if 0
1152 lu->DataSequenceInOrder = 0;
1153 #else
1154 ISTGT_ERRLOG("not supported value %s\n", val);
1155 return -1;
1156 #endif
1157 } else {
1158 ISTGT_ERRLOG("unknown value %s\n", val);
1159 return -1;
1160 }
1161 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "DataSequenceInOrder %s\n",
1162 lu->DataSequenceInOrder ? "Yes" : "No");
1163 }
1164
1165 val = istgt_get_val(sp, "ErrorRecoveryLevel");
1166 if (val == NULL) {
1167 lu->ErrorRecoveryLevel = lu->istgt->ErrorRecoveryLevel;
1168 } else {
1169 lu->ErrorRecoveryLevel = (int)strtol(val, NULL, 10);
1170 if (lu->ErrorRecoveryLevel < 0) {
1171 lu->ErrorRecoveryLevel = DEFAULT_ERRORRECOVERYLEVEL;
1172 } else if (lu->ErrorRecoveryLevel == 0) {
1173 lu->ErrorRecoveryLevel = 0;
1174 } else if (lu->ErrorRecoveryLevel == 1) {
1175 #if 0
1176 lu->ErrorRecoveryLevel = 1;
1177 #else
1178 ISTGT_ERRLOG("not supported value %d\n",
1179 lu->ErrorRecoveryLevel);
1180 return -1;
1181 #endif
1182 } else if (lu->ErrorRecoveryLevel == 2) {
1183 #if 0
1184 lu->ErrorRecoveryLevel = 2;
1185 #else
1186 ISTGT_ERRLOG("not supported value %d\n",
1187 lu->ErrorRecoveryLevel);
1188 return -1;
1189 #endif
1190 } else {
1191 ISTGT_ERRLOG("not supported value %d\n",
1192 lu->ErrorRecoveryLevel);
1193 return -1;
1194 }
1195 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "ErrorRecoveryLevel %d\n",
1196 istgt->ErrorRecoveryLevel);
1197 }
1198
1199 return 0;
1200 }
1201
1202 static int
1203 istgt_lu_add_unit(ISTGT_Ptr istgt, CF_SECTION *sp)
1204 {
1205 char buf[MAX_TMPBUF], buf2[MAX_TMPBUF];
1206 ISTGT_LU_Ptr lu;
1207 const char *vendor, *product, *revision, *serial;
1208 const char *pg_tag, *ig_tag;
1209 const char *ag_tag;
1210 const char *flags, *file, *size;
1211 const char *key, *val;
1212 uint64_t msize;
1213 uint64_t nbs64;
1214 int pg_tag_i, ig_tag_i;
1215 int ag_tag_i;
1216 int rpm, formfactor;
1217 int mflags;
1218 int slot;
1219 int nbs;
1220 int i, j, k;
1221 int rc;
1222
1223 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "add unit %d\n", sp->num);
1224
1225 if (sp->num >= MAX_LOGICAL_UNIT) {
1226 ISTGT_ERRLOG("LU%d: over maximum unit number\n", sp->num);
1227 return -1;
1228 }
1229 if (istgt->logical_unit[sp->num] != NULL) {
1230 ISTGT_ERRLOG("LU%d: duplicate unit\n", sp->num);
1231 return -1;
1232 }
1233
1234 lu = xmalloc(sizeof *lu);
1235 memset(lu, 0, sizeof *lu);
1236 lu->num = sp->num;
1237 lu->istgt = istgt;
1238 istgt_lu_set_state(lu, ISTGT_STATE_INVALID);
1239 nbs64 = istgt_lu_get_nbserial(istgt->nodebase);
1240 #if 0
1241 /* disabled now */
1242 nbs = (int) (nbs64 % 900) * 100000;
1243 #else
1244 nbs = 0;
1245 #endif
1246
1247 val = istgt_get_val(sp, "Comment");
1248 if (val != NULL) {
1249 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Comment %s\n", val);
1250 }
1251
1252 val = istgt_get_val(sp, "TargetName");
1253 if (val == NULL) {
1254 ISTGT_ERRLOG("LU%d: TargetName not found\n", lu->num);
1255 goto error_return;
1256 }
1257 if (strncasecmp(val, "iqn.", 4) != 0
1258 && strncasecmp(val, "eui.", 4) != 0
1259 && strncasecmp(val, "naa.", 4) != 0) {
1260 snprintf(buf, sizeof buf, "%s:%s", istgt->nodebase, val);
1261 } else {
1262 snprintf(buf, sizeof buf, "%s", val);
1263 }
1264 if (istgt_lu_check_iscsi_name(buf) != 0) {
1265 ISTGT_ERRLOG("TargetName %s contains an invalid character or format.\n",
1266 buf);
1267 #if 0
1268 goto error_return;
1269 #endif
1270 }
1271 lu->name = xstrdup(buf);
1272 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "TargetName %s\n",
1273 lu->name);
1274
1275 val = istgt_get_val(sp, "TargetAlias");
1276 if (val == NULL) {
1277 lu->alias = NULL;
1278 } else {
1279 lu->alias = xstrdup(val);
1280 }
1281 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "TargetAlias %s\n",
1282 lu->alias);
1283
1284 val = istgt_get_val(sp, "Mapping");
1285 if (val == NULL) {
1286 /* no map */
1287 lu->maxmap = 0;
1288 } else {
1289 lu->maxmap = 0;
1290 for (i = 0; ; i++) {
1291 val = istgt_get_nmval(sp, "Mapping", i, 0);
1292 if (val == NULL)
1293 break;
1294 if (lu->maxmap >= MAX_LU_MAP) {
1295 ISTGT_ERRLOG("LU%d: too many mapping\n", lu->num);
1296 goto error_return;
1297 }
1298 pg_tag = istgt_get_nmval(sp, "Mapping", i, 0);
1299 ig_tag = istgt_get_nmval(sp, "Mapping", i, 1);
1300 if (pg_tag == NULL || ig_tag == NULL) {
1301 ISTGT_ERRLOG("LU%d: mapping error\n", lu->num);
1302 goto error_return;
1303 }
1304 if (strncasecmp(pg_tag, "PortalGroup",
1305 strlen("PortalGroup")) != 0
1306 || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1307 ISTGT_ERRLOG("LU%d: mapping portal error\n", lu->num);
1308 goto error_return;
1309 }
1310 if (strncasecmp(ig_tag, "InitiatorGroup",
1311 strlen("InitiatorGroup")) != 0
1312 || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1313 ISTGT_ERRLOG("LU%d: mapping initiator error\n", lu->num);
1314 goto error_return;
1315 }
1316 if (pg_tag_i < 1 || ig_tag_i < 1) {
1317 ISTGT_ERRLOG("LU%d: invalid group tag\n", lu->num);
1318 goto error_return;
1319 }
1320 if (istgt_lu_find_portalgroup(istgt, pg_tag_i) == NULL) {
1321 ISTGT_ERRLOG("LU%d: PortalGroup%d not found\n",
1322 lu->num, pg_tag_i);
1323 goto error_return;
1324 }
1325 if (istgt_lu_find_initiatorgroup(istgt, ig_tag_i) == NULL) {
1326 ISTGT_ERRLOG("LU%d: InitiatorGroup%d not found\n",
1327 lu->num, ig_tag_i);
1328 goto error_return;
1329 }
1330 lu->map[i].pg_tag = pg_tag_i;
1331 lu->map[i].pg_aas = AAS_ACTIVE_OPTIMIZED;
1332 //lu->map[i].pg_aas = AAS_ACTIVE_NON_OPTIMIZED;
1333 lu->map[i].pg_aas |= AAS_STATUS_IMPLICIT;
1334 lu->map[i].ig_tag = ig_tag_i;
1335 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1336 "Mapping PortalGroup%d InitiatorGroup%d\n",
1337 lu->map[i].pg_tag, lu->map[i].ig_tag);
1338 lu->maxmap = i + 1;
1339 }
1340 }
1341 if (lu->maxmap == 0) {
1342 ISTGT_ERRLOG("LU%d: no Mapping\n", lu->num);
1343 goto error_return;
1344 }
1345
1346 val = istgt_get_val(sp, "AuthMethod");
1347 if (val == NULL) {
1348 /* none */
1349 lu->no_auth_chap = 0;
1350 lu->auth_chap = 0;
1351 lu->auth_chap_mutual = 0;
1352 } else {
1353 lu->no_auth_chap = 0;
1354 for (i = 0; ; i++) {
1355 val = istgt_get_nmval(sp, "AuthMethod", 0, i);
1356 if (val == NULL)
1357 break;
1358 if (strcasecmp(val, "CHAP") == 0) {
1359 lu->auth_chap = 1;
1360 } else if (strcasecmp(val, "Mutual") == 0) {
1361 lu->auth_chap_mutual = 1;
1362 } else if (strcasecmp(val, "Auto") == 0) {
1363 lu->auth_chap = 0;
1364 lu->auth_chap_mutual = 0;
1365 } else if (strcasecmp(val, "None") == 0) {
1366 lu->no_auth_chap = 1;
1367 lu->auth_chap = 0;
1368 lu->auth_chap_mutual = 0;
1369 } else {
1370 ISTGT_ERRLOG("LU%d: unknown auth\n", lu->num);
1371 goto error_return;
1372 }
1373 }
1374 if (lu->auth_chap_mutual && !lu->auth_chap) {
1375 ISTGT_ERRLOG("LU%d: Mutual but not CHAP\n", lu->num);
1376 goto error_return;
1377 }
1378 }
1379 if (lu->no_auth_chap != 0) {
1380 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthMethod None\n");
1381 } else if (lu->auth_chap == 0) {
1382 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthMethod Auto\n");
1383 } else {
1384 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthMethod %s %s\n",
1385 lu->auth_chap ? "CHAP" : "",
1386 lu->auth_chap_mutual ? "Mutual" : "");
1387 }
1388
1389 val = istgt_get_val(sp, "AuthGroup");
1390 if (val == NULL) {
1391 lu->auth_group = 0;
1392 } else {
1393 ag_tag = val;
1394 if (strcasecmp(ag_tag, "None") == 0) {
1395 ag_tag_i = 0;
1396 } else {
1397 if (strncasecmp(ag_tag, "AuthGroup",
1398 strlen("AuthGroup")) != 0
1399 || sscanf(ag_tag, "%*[^0-9]%d", &ag_tag_i) != 1) {
1400 ISTGT_ERRLOG("LU%d: auth group error\n", lu->num);
1401 goto error_return;
1402 }
1403 if (ag_tag_i == 0) {
1404 ISTGT_ERRLOG("LU%d: invalid auth group %d\n", ag_tag_i);
1405 goto error_return;
1406 }
1407 }
1408 lu->auth_group = ag_tag_i;
1409 }
1410 if (lu->auth_group == 0) {
1411 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthGroup None\n");
1412 } else {
1413 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthGroup AuthGroup%d\n",
1414 lu->auth_group);
1415 }
1416
1417 val = istgt_get_val(sp, "UseDigest");
1418 if (val != NULL) {
1419 for (i = 0; ; i++) {
1420 val = istgt_get_nmval(sp, "UseDigest", 0, i);
1421 if (val == NULL)
1422 break;
1423 if (strcasecmp(val, "Header") == 0) {
1424 lu->header_digest = 1;
1425 } else if (strcasecmp(val, "Data") == 0) {
1426 lu->data_digest = 1;
1427 } else if (strcasecmp(val, "Auto") == 0) {
1428 lu->header_digest = 0;
1429 lu->data_digest = 0;
1430 } else {
1431 ISTGT_ERRLOG("LU%d: unknown digest\n", lu->num);
1432 goto error_return;
1433 }
1434 }
1435 }
1436 if (lu->header_digest == 0 && lu->data_digest == 0) {
1437 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UseDigest Auto\n");
1438 } else {
1439 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UseDigest %s %s\n",
1440 lu->header_digest ? "Header" : "",
1441 lu->data_digest ? "Data" : "");
1442 }
1443
1444 val = istgt_get_val(sp, "ReadOnly");
1445 if (val == NULL) {
1446 lu->readonly = 0;
1447 } else if (strcasecmp(val, "Yes") == 0) {
1448 lu->readonly = 1;
1449 }
1450 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "ReadOnly %s\n",
1451 lu->readonly ? "Yes" : "No");
1452
1453 val = istgt_get_val(sp, "UnitType");
1454 if (val == NULL) {
1455 ISTGT_ERRLOG("LU%d: unknown unit type\n", lu->num);
1456 goto error_return;
1457 }
1458 if (strcasecmp(val, "Pass") == 0) {
1459 lu->type = ISTGT_LU_TYPE_PASS;
1460 } else if (strcasecmp(val, "Disk") == 0) {
1461 lu->type = ISTGT_LU_TYPE_DISK;
1462 } else if (strcasecmp(val, "DVD") == 0) {
1463 lu->type = ISTGT_LU_TYPE_DVD;
1464 } else if (strcasecmp(val, "Tape") == 0) {
1465 lu->type = ISTGT_LU_TYPE_TAPE;
1466 } else {
1467 ISTGT_ERRLOG("LU%d: unknown unit type\n", lu->num);
1468 goto error_return;
1469 }
1470 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UnitType %d (%s)\n",
1471 lu->type, val);
1472
1473 val = istgt_get_val(sp, "UnitOnline");
1474 if (val == NULL) {
1475 lu->online = 1;
1476 } else if (strcasecmp(val, "Yes") == 0) {
1477 lu->online = 1;
1478 }
1479 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UnitOnline %s\n",
1480 lu->online ? "Yes" : "No");
1481
1482 vendor = istgt_get_nmval(sp, "UnitInquiry", 0, 0);
1483 product = istgt_get_nmval(sp, "UnitInquiry", 0, 1);
1484 revision = istgt_get_nmval(sp, "UnitInquiry", 0, 2);
1485 serial = istgt_get_nmval(sp, "UnitInquiry", 0, 3);
1486 switch (lu->type) {
1487 case ISTGT_LU_TYPE_DISK:
1488 if (vendor == NULL || strlen(vendor) == 0)
1489 vendor = DEFAULT_LU_VENDOR_DISK;
1490 if (product == NULL || strlen(product) == 0)
1491 product = DEFAULT_LU_PRODUCT_DISK;
1492 if (revision == NULL || strlen(revision) == 0)
1493 revision = DEFAULT_LU_REVISION_DISK;
1494 if (serial == NULL || strlen(serial) == 0) {
1495 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1496 serial = (const char *) &buf[0];
1497 }
1498 break;
1499 case ISTGT_LU_TYPE_DVD:
1500 if (vendor == NULL || strlen(vendor) == 0)
1501 vendor = DEFAULT_LU_VENDOR_DVD;
1502 if (product == NULL || strlen(product) == 0)
1503 product = DEFAULT_LU_PRODUCT_DVD;
1504 if (revision == NULL || strlen(revision) == 0)
1505 revision = DEFAULT_LU_REVISION_DVD;
1506 if (serial == NULL || strlen(serial) == 0) {
1507 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1508 serial = (const char *) &buf[0];
1509 }
1510 break;
1511 case ISTGT_LU_TYPE_TAPE:
1512 if (vendor == NULL || strlen(vendor) == 0)
1513 vendor = DEFAULT_LU_VENDOR_TAPE;
1514 if (product == NULL || strlen(product) == 0)
1515 product = DEFAULT_LU_PRODUCT_TAPE;
1516 if (revision == NULL || strlen(revision) == 0)
1517 revision = DEFAULT_LU_REVISION_TAPE;
1518 if (serial == NULL || strlen(serial) == 0) {
1519 #ifdef USE_LU_TAPE_DLT8000
1520 snprintf(buf, sizeof buf, "CX%.8d", 10000000 + nbs + lu->num);
1521 #else
1522 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1523 #endif /* USE_LU_TAPE_DLT8000 */
1524 serial = (const char *) &buf[0];
1525 }
1526 break;
1527 default:
1528 if (vendor == NULL || strlen(vendor) == 0)
1529 vendor = DEFAULT_LU_VENDOR;
1530 if (product == NULL || strlen(product) == 0)
1531 product = DEFAULT_LU_PRODUCT;
1532 if (revision == NULL || strlen(revision) == 0)
1533 revision = DEFAULT_LU_REVISION;
1534 if (serial == NULL || strlen(serial) == 0) {
1535 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1536 serial = (const char *) &buf[0];
1537 }
1538 break;
1539 }
1540 lu->inq_vendor = xstrdup(vendor);
1541 lu->inq_product = xstrdup(product);
1542 lu->inq_revision = xstrdup(revision);
1543 lu->inq_serial = xstrdup(serial);
1544 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UnitInquiry %s %s %s %s\n",
1545 lu->inq_vendor, lu->inq_product, lu->inq_revision,
1546 lu->inq_serial);
1547
1548 val = istgt_get_val(sp, "BlockLength");
1549 if (val == NULL) {
1550 switch (lu->type) {
1551 case ISTGT_LU_TYPE_DISK:
1552 lu->blocklen = DEFAULT_LU_BLOCKLEN_DISK;
1553 break;
1554 case ISTGT_LU_TYPE_DVD:
1555 lu->blocklen = DEFAULT_LU_BLOCKLEN_DVD;
1556 break;
1557 case ISTGT_LU_TYPE_TAPE:
1558 lu->blocklen = DEFAULT_LU_BLOCKLEN_TAPE;
1559 break;
1560 default:
1561 lu->blocklen = DEFAULT_LU_BLOCKLEN;
1562 break;
1563 }
1564 } else {
1565 lu->blocklen = (int) strtol(val, NULL, 10);
1566 }
1567 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "BlockLength %d\n",
1568 lu->blocklen);
1569
1570 val = istgt_get_val(sp, "QueueDepth");
1571 if (val == NULL) {
1572 switch (lu->type) {
1573 case ISTGT_LU_TYPE_DISK:
1574 lu->queue_depth = DEFAULT_LU_QUEUE_DEPTH;
1575 //lu->queue_depth = 0;
1576 break;
1577 case ISTGT_LU_TYPE_DVD:
1578 case ISTGT_LU_TYPE_TAPE:
1579 default:
1580 lu->queue_depth = 0;
1581 break;
1582 }
1583 } else {
1584 lu->queue_depth = (int) strtol(val, NULL, 10);
1585 }
1586 if (lu->queue_depth < 0 || lu->queue_depth >= MAX_LU_QUEUE_DEPTH) {
1587 ISTGT_ERRLOG("LU%d: queue depth range error\n");
1588 goto error_return;
1589 }
1590 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "QueueDepth %d\n",
1591 lu->queue_depth);
1592
1593 lu->maxlun = 0;
1594 for (i = 0; i < MAX_LU_LUN; i++) {
1595 lu->lun[i].type = ISTGT_LU_LUN_TYPE_NONE;
1596 lu->lun[i].rotationrate = DEFAULT_LU_ROTATIONRATE;
1597 lu->lun[i].formfactor = DEFAULT_LU_FORMFACTOR;
1598 lu->lun[i].readcache = 1;
1599 lu->lun[i].writecache = 1;
1600 lu->lun[i].serial = NULL;
1601 lu->lun[i].spec = NULL;
1602 snprintf(buf, sizeof buf, "LUN%d", i);
1603 val = istgt_get_val(sp, buf);
1604 if (val == NULL)
1605 continue;
1606 if (i != 0) {
1607 /* default LUN serial (except LUN0) */
1608 snprintf(buf2, sizeof buf2, "%sL%d", lu->inq_serial, i);
1609 lu->lun[i].serial = xstrdup(buf2);
1610 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LUN%d Serial %s (default)\n",
1611 i, buf2);
1612 }
1613 for (j = 0; ; j++) {
1614 val = istgt_get_nmval(sp, buf, j, 0);
1615 if (val == NULL)
1616 break;
1617 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LUN%d\n", i);
1618 if (strcasecmp(val, "Device") == 0) {
1619 if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_NONE) {
1620 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1621 goto error_return;
1622 }
1623 lu->lun[i].type = ISTGT_LU_LUN_TYPE_DEVICE;
1624
1625 file = istgt_get_nmval(sp, buf, j, 1);
1626 if (file == NULL) {
1627 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1628 goto error_return;
1629 }
1630 lu->lun[i].u.device.file = xstrdup(file);
1631 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Device file=%s\n",
1632 lu->lun[i].u.device.file);
1633 } else if (strcasecmp(val, "Storage") == 0) {
1634 if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_NONE) {
1635 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1636 goto error_return;
1637 }
1638 lu->lun[i].type = ISTGT_LU_LUN_TYPE_STORAGE;
1639
1640 file = istgt_get_nmval(sp, buf, j, 1);
1641 size = istgt_get_nmval(sp, buf, j, 2);
1642 if (file == NULL || size == NULL) {
1643 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1644 goto error_return;
1645 }
1646 if (strcasecmp(size, "Auto") == 0
1647 || strcasecmp(size, "Size") == 0) {
1648 lu->lun[i].u.storage.size = istgt_lu_get_filesize(file);
1649 } else {
1650 lu->lun[i].u.storage.size = istgt_lu_parse_size(size);
1651 }
1652 if (lu->lun[i].u.storage.size == 0) {
1653 ISTGT_ERRLOG("LU%d: LUN%d: Auto size error (%s)\n", lu->num, i, file);
1654 goto error_return;
1655 }
1656 lu->lun[i].u.storage.fd = -1;
1657 lu->lun[i].u.storage.file = xstrdup(file);
1658 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1659 "Storage file=%s, size=%"PRIu64"\n",
1660 lu->lun[i].u.storage.file,
1661 lu->lun[i].u.storage.size);
1662 } else if (strcasecmp(val, "Removable") == 0) {
1663 if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_NONE) {
1664 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1665 goto error_return;
1666 }
1667 lu->lun[i].type = ISTGT_LU_LUN_TYPE_REMOVABLE;
1668
1669 flags = istgt_get_nmval(sp, buf, j, 1);
1670 file = istgt_get_nmval(sp, buf, j, 2);
1671 size = istgt_get_nmval(sp, buf, j, 3);
1672 if (flags == NULL || file == NULL || size == NULL) {
1673 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1674 goto error_return;
1675 }
1676 mflags = istgt_lu_parse_media_flags(flags);
1677 msize = istgt_lu_parse_media_size(file, size, &mflags);
1678 if (msize == 0 && strcasecmp(file, "/dev/null") == 0) {
1679 /* empty media */
1680 } else if (msize == 0) {
1681 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1682 goto error_return;
1683 }
1684 lu->lun[i].u.removable.type = 0;
1685 lu->lun[i].u.removable.id = 0;
1686 lu->lun[i].u.removable.fd = -1;
1687 lu->lun[i].u.removable.flags = mflags;
1688 lu->lun[i].u.removable.file = xstrdup(file);
1689 lu->lun[i].u.removable.size = msize;
1690 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1691 "Removable file=%s, size=%"PRIu64", flags=%x\n",
1692 lu->lun[i].u.removable.file,
1693 lu->lun[i].u.removable.size,
1694 lu->lun[i].u.removable.flags);
1695 } else if (strncasecmp(val, "Slot", 4) == 0) {
1696 if (lu->lun[i].type == ISTGT_LU_LUN_TYPE_NONE) {
1697 lu->lun[i].u.slot.maxslot = 0;
1698 for (k = 0; k < MAX_LU_LUN_SLOT; k++) {
1699 lu->lun[i].u.slot.present[k] = 0;
1700 lu->lun[i].u.slot.flags[k] = 0;
1701 lu->lun[i].u.slot.file[k] = NULL;
1702 lu->lun[i].u.slot.size[k] = 0;
1703 }
1704 } else if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_SLOT) {
1705 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1706 goto error_return;
1707 }
1708 lu->lun[i].type = ISTGT_LU_LUN_TYPE_SLOT;
1709 if (sscanf(val, "%*[^0-9]%d", &slot) != 1) {
1710 ISTGT_ERRLOG("LU%d: slot number error\n", lu->num);
1711 goto error_return;
1712 }
1713 if (slot < 0 || slot >= MAX_LU_LUN_SLOT) {
1714 ISTGT_ERRLOG("LU%d: slot number range error\n", lu->num);
1715 goto error_return;
1716 }
1717 if (lu->lun[i].u.slot.present[slot]) {
1718 ISTGT_ERRLOG("LU%d: duplicate slot %d\n", lu->num, slot);
1719 goto error_return;
1720 }
1721 lu->lun[i].u.slot.present[slot] = 1;
1722 if (slot + 1 > lu->lun[i].u.slot.maxslot) {
1723 lu->lun[i].u.slot.maxslot = slot + 1;
1724 }
1725
1726 flags = istgt_get_nmval(sp, buf, j, 1);
1727 file = istgt_get_nmval(sp, buf, j, 2);
1728 size = istgt_get_nmval(sp, buf, j, 3);
1729 if (flags == NULL || file == NULL || size == NULL) {
1730 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1731 goto error_return;
1732 }
1733 mflags = istgt_lu_parse_media_flags(flags);
1734 msize = istgt_lu_parse_media_size(file, size, &mflags);
1735 if (msize == 0) {
1736 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1737 goto error_return;
1738 }
1739 lu->lun[i].u.slot.flags[slot] = mflags;
1740 lu->lun[i].u.slot.file[slot] = xstrdup(file);
1741 lu->lun[i].u.slot.size[slot] = msize;
1742 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1743 "Slot%d file=%s, size=%"PRIu64", flags=%x\n",
1744 slot,
1745 lu->lun[i].u.slot.file[slot],
1746 lu->lun[i].u.slot.size[slot],
1747 lu->lun[i].u.slot.flags[slot]);
1748 } else if (strncasecmp(val, "Option", 6) == 0) {
1749 key = istgt_get_nmval(sp, buf, j, 1);
1750 val = istgt_get_nmval(sp, buf, j, 2);
1751 if (key == NULL || val == NULL) {
1752 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1753 goto error_return;
1754 }
1755 if (strcasecmp(key, "Serial") == 0) {
1756 /* set LUN serial */
1757 if (strlen(val) == 0) {
1758 ISTGT_ERRLOG("LU%d: LUN%d: no serial\n",
1759 lu->num, i);
1760 goto error_return;
1761 }
1762 xfree(lu->lun[i].serial);
1763 lu->lun[i].serial = xstrdup(val);
1764 } else if (strcasecmp(key, "RPM") == 0) {
1765 rpm = (int)strtol(val, NULL, 10);
1766 if (rpm < 0) {
1767 rpm = 0;
1768 } else if (rpm > 0xfffe) {
1769 rpm = 0xfffe;
1770 }
1771 lu->lun[i].rotationrate = rpm;
1772 } else if (strcasecmp(key, "FormFactor") == 0) {
1773 formfactor = (int)strtol(val, NULL, 10);
1774 if (formfactor < 0) {
1775 formfactor = 0;
1776 } else if (formfactor > 0x0f) {
1777 formfactor = 0xf;
1778 }
1779 lu->lun[i].formfactor = formfactor;
1780 } else if (strcasecmp(key, "ReadCache") == 0) {
1781 if (strcasecmp(val, "Enable") == 0) {
1782 lu->lun[i].readcache = 1;
1783 } else if (strcasecmp(val, "Disable") == 0) {
1784 lu->lun[i].readcache = 0;
1785 } else {
1786 ISTGT_ERRLOG("LU%d: LUN%d: unknown val(%s)\n",
1787 lu->num, i, val);
1788 }
1789 } else if (strcasecmp(key, "WriteCache") == 0) {
1790 if (strcasecmp(val, "Enable") == 0) {
1791 lu->lun[i].writecache = 1;
1792 } else if (strcasecmp(val, "Disable") == 0) {
1793 lu->lun[i].writecache = 0;
1794 } else {
1795 ISTGT_ERRLOG("LU%d: LUN%d: unknown val(%s)\n",
1796 lu->num, i, val);
1797 }
1798 } else {
1799 ISTGT_WARNLOG("LU%d: LUN%d: unknown key(%s)\n",
1800 lu->num, i, key);
1801 continue;
1802 }
1803 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LUN%d Option %s => %s\n",
1804 i, key, val);
1805 continue;
1806 } else {
1807 ISTGT_ERRLOG("LU%d: unknown lun type\n", lu->num);
1808 goto error_return;
1809 }
1810 }
1811 if (lu->lun[i].type == ISTGT_LU_LUN_TYPE_SLOT) {
1812 if (lu->lun[i].u.slot.maxslot == 0) {
1813 ISTGT_ERRLOG("LU%d: no slot\n", lu->num);
1814 goto error_return;
1815 }
1816 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "maxslot=%d\n",
1817 lu->lun[i].u.slot.maxslot);
1818 }
1819 lu->maxlun = i + 1;
1820 }
1821 if (lu->maxlun == 0) {
1822 ISTGT_ERRLOG("LU%d: no LUN\n", lu->num);
1823 goto error_return;
1824 }
1825 if (lu->lun[0].type == ISTGT_LU_LUN_TYPE_NONE) {
1826 ISTGT_ERRLOG("LU%d: no LUN0\n", lu->num);
1827 goto error_return;
1828 }
1829
1830 /* set local values if any */
1831 rc = istgt_lu_set_local_settings(istgt, sp, lu);
1832 if (rc < 0) {
1833 ISTGT_ERRLOG("LU%d: local setting error\n", lu->num);
1834 goto error_return;
1835 }
1836
1837 /* tsih 0 is reserved */
1838 for (i = 0; i < MAX_LU_TSIH; i++) {
1839 lu->tsih[i].tag = 0;
1840 lu->tsih[i].tsih = 0;
1841 lu->tsih[i].initiator_port = NULL;
1842 }
1843 lu->maxtsih = 1;
1844 lu->last_tsih = 0;
1845
1846 MTX_LOCK(&istgt->mutex);
1847 istgt->nlogical_unit++;
1848 istgt->logical_unit[lu->num] = lu;
1849 MTX_UNLOCK(&istgt->mutex);
1850 return 0;
1851
1852 error_return:
1853 xfree(lu->name);
1854 xfree(lu->alias);
1855 xfree(lu->inq_vendor);
1856 xfree(lu->inq_product);
1857 xfree(lu->inq_revision);
1858 for (i = 0; i < MAX_LU_LUN; i++) {
1859 switch (lu->lun[i].type) {
1860 case ISTGT_LU_LUN_TYPE_DEVICE:
1861 xfree(lu->lun[i].u.device.file);
1862 break;
1863 case ISTGT_LU_LUN_TYPE_STORAGE:
1864 xfree(lu->lun[i].u.storage.file);
1865 break;
1866 case ISTGT_LU_LUN_TYPE_REMOVABLE:
1867 xfree(lu->lun[i].u.removable.file);
1868 break;
1869 case ISTGT_LU_LUN_TYPE_SLOT:
1870 for (j = 0; j < lu->lun[i].u.slot.maxslot; j++) {
1871 xfree(lu->lun[i].u.slot.file[j]);
1872 }
1873 break;
1874 case ISTGT_LU_LUN_TYPE_NONE:
1875 default:
1876 break;
1877 }
1878 }
1879 for (i = 0; i < MAX_LU_TSIH; i++) {
1880 xfree(lu->tsih[i].initiator_port);
1881 }
1882 for (i = 0; i < lu->maxmap; i++) {
1883 /* nothing */
1884 }
1885
1886 xfree(lu);
1887 return -1;
1888 }
1889
1890 static int
1891 istgt_lu_del_unit(ISTGT_Ptr istgt, ISTGT_LU_Ptr lu)
1892 {
1893 int i, j;
1894
1895 if (lu ==NULL)
1896 return 0;
1897 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "del unit %d\n", lu->num);
1898
1899 MTX_LOCK(&istgt->mutex);
1900 istgt->nlogical_unit--;
1901 istgt->logical_unit[lu->num] = NULL;
1902 MTX_UNLOCK(&istgt->mutex);
1903
1904 xfree(lu->name);
1905 xfree(lu->alias);
1906 xfree(lu->inq_vendor);
1907 xfree(lu->inq_product);
1908 xfree(lu->inq_revision);
1909 xfree(lu->inq_serial);
1910 for (i = 0; i < MAX_LU_LUN; i++) {
1911 xfree(lu->lun[i].serial);
1912 switch (lu->lun[i].type) {
1913 case ISTGT_LU_LUN_TYPE_DEVICE:
1914 xfree(lu->lun[i].u.device.file);
1915 break;
1916 case ISTGT_LU_LUN_TYPE_STORAGE:
1917 xfree(lu->lun[i].u.storage.file);
1918 break;
1919 case ISTGT_LU_LUN_TYPE_REMOVABLE:
1920 xfree(lu->lun[i].u.removable.file);
1921 break;
1922 case ISTGT_LU_LUN_TYPE_SLOT:
1923 for (j = 0; j < lu->lun[i].u.slot.maxslot; j++) {
1924 xfree(lu->lun[i].u.slot.file[j]);
1925 }
1926 break;
1927 case ISTGT_LU_LUN_TYPE_NONE:
1928 default:
1929 break;
1930 }
1931 }
1932 for (i = 0; i < MAX_LU_TSIH; i++) {
1933 xfree(lu->tsih[i].initiator_port);
1934 }
1935 for (i = 0; i < lu->maxmap; i++) {
1936 /* nothing */
1937 }
1938
1939 return 0;
1940 }
1941
1942 static void *luworker(void *arg);
1943
1944 int
1945 istgt_lu_init(ISTGT_Ptr istgt)
1946 {
1947 ISTGT_LU_Ptr lu;
1948 CF_SECTION *sp;
1949 int rc;
1950 int i;
1951
1952 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_init\n");
1953 sp = istgt_find_cf_section(istgt->config, "Global");
1954 if (sp == NULL) {
1955 ISTGT_ERRLOG("find_cf_section failed()\n");
1956 return -1;
1957 }
1958
1959 sp = istgt->config->section;
1960 while (sp != NULL) {
1961 if (sp->type == ST_LOGICAL_UNIT) {
1962 if (sp->num == 0) {
1963 ISTGT_ERRLOG("Unit 0 is invalid\n");
1964 return -1;
1965 }
1966 if (sp->num > ISTGT_LU_TAG_MAX) {
1967 ISTGT_ERRLOG("tag %d is invalid\n", sp->num);
1968 return -1;
1969 }
1970 rc = istgt_lu_add_unit(istgt, sp);
1971 if (rc < 0) {
1972 ISTGT_ERRLOG("lu_add_unit() failed\n");
1973 return -1;
1974 }
1975 }
1976 sp = sp->next;
1977 }
1978
1979 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
1980 lu = istgt->logical_unit[i];
1981 if (lu == NULL)
1982 continue;
1983
1984 rc = pthread_mutex_init(&lu->mutex, NULL);
1985 if (rc != 0) {
1986 ISTGT_ERRLOG("LU%d: mutex_init() failed\n", lu->num);
1987 return -1;
1988 }
1989 rc = pthread_mutex_init(&lu->state_mutex, NULL);
1990 if (rc != 0) {
1991 ISTGT_ERRLOG("LU%d: mutex_init() failed\n", lu->num);
1992 return -1;
1993 }
1994 rc = pthread_mutex_init(&lu->queue_mutex, NULL);
1995 if (rc != 0) {
1996 ISTGT_ERRLOG("LU%d: mutex_init() failed\n", lu->num);
1997 return -1;
1998 }
1999 rc = pthread_cond_init(&lu->queue_cond, NULL);
2000 if (rc != 0) {
2001 ISTGT_ERRLOG("LU%d: cond_init() failed\n", lu->num);
2002 return -1;
2003 }
2004
2005 switch (lu->type) {
2006 case ISTGT_LU_TYPE_PASS:
2007 rc = istgt_lu_pass_init(istgt, lu);
2008 if (rc < 0) {
2009 ISTGT_ERRLOG("LU%d: lu_pass_init() failed\n", lu->num);
2010 return -1;
2011 }
2012 break;
2013
2014 case ISTGT_LU_TYPE_DISK:
2015 rc = istgt_lu_disk_init(istgt, lu);
2016 if (rc < 0) {
2017 ISTGT_ERRLOG("LU%d: lu_disk_init() failed\n", lu->num);
2018 return -1;
2019 }
2020 break;
2021
2022 case ISTGT_LU_TYPE_DVD:
2023 rc = istgt_lu_dvd_init(istgt, lu);
2024 if (rc < 0) {
2025 ISTGT_ERRLOG("LU%d: lu_dvd_init() failed\n", lu->num);
2026 return -1;
2027 }
2028 break;
2029
2030 case ISTGT_LU_TYPE_TAPE:
2031 rc = istgt_lu_tape_init(istgt, lu);
2032 if (rc < 0) {
2033 ISTGT_ERRLOG("LU%d: lu_tape_init() failed\n", lu->num);
2034 return -1;
2035 }
2036 break;
2037
2038 case ISTGT_LU_TYPE_NONE:
2039 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2040 break;
2041 default:
2042 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2043 return -1;
2044 }
2045
2046 istgt_lu_set_state(lu, ISTGT_STATE_INITIALIZED);
2047 }
2048
2049 return 0;
2050 }
2051
2052 int
2053 istgt_lu_set_all_state(ISTGT_Ptr istgt, ISTGT_STATE state)
2054 {
2055 ISTGT_LU_Ptr lu;
2056 int i;
2057
2058 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2059 lu = istgt->logical_unit[i];
2060 if (lu == NULL)
2061 continue;
2062
2063 istgt_lu_set_state(lu, state);
2064 }
2065
2066 return 0;
2067 }
2068
2069 int
2070 istgt_lu_create_threads(ISTGT_Ptr istgt)
2071 {
2072 #ifdef HAVE_PTHREAD_SET_NAME_NP
2073 char buf[MAX_TMPBUF];
2074 #endif
2075 ISTGT_LU_Ptr lu;
2076 int rc;
2077 int i;
2078
2079 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_create_threads\n");
2080
2081 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2082 lu = istgt->logical_unit[i];
2083 if (lu == NULL)
2084 continue;
2085
2086 if (lu->queue_depth != 0) {
2087 /* create LU thread */
2088 #ifdef ISTGT_STACKSIZE
2089 rc = pthread_create(&lu->thread, &istgt->attr, &luworker, (void *)lu);
2090 #else
2091 rc = pthread_create(&lu->thread, NULL, &luworker, (void *)lu);
2092 #endif
2093 if (rc != 0) {
2094 ISTGT_ERRLOG("pthread_create() failed\n");
2095 return -1;
2096 }
2097 #if 0
2098 rc = pthread_detach(lu->thread);
2099 if (rc != 0) {
2100 ISTGT_ERRLOG("pthread_detach() failed\n");
2101 return -1;
2102 }
2103 #endif
2104 #ifdef HAVE_PTHREAD_SET_NAME_NP
2105 snprintf(buf, sizeof buf, "luthread #%d", i);
2106 pthread_set_name_np(lu->thread, buf);
2107 #endif
2108 }
2109 }
2110
2111 return 0;
2112 }
2113
2114 int
2115 istgt_lu_shutdown(ISTGT_Ptr istgt)
2116 {
2117 ISTGT_LU_Ptr lu;
2118 int rc;
2119 int i;
2120
2121 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_shutdown\n");
2122
2123 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2124 lu = istgt->logical_unit[i];
2125 if (lu == NULL)
2126 continue;
2127 istgt_lu_set_state(lu, ISTGT_STATE_SHUTDOWN);
2128
2129 switch (lu->type) {
2130 case ISTGT_LU_TYPE_PASS:
2131 rc = istgt_lu_pass_shutdown(istgt, lu);
2132 if (rc < 0) {
2133 ISTGT_ERRLOG("LU%d: lu_pass_shutdown() failed\n", lu->num);
2134 /* ignore error */
2135 }
2136 break;
2137
2138 case ISTGT_LU_TYPE_DISK:
2139 rc = istgt_lu_disk_shutdown(istgt, lu);
2140 if (rc < 0) {
2141 ISTGT_ERRLOG("LU%d: lu_disk_shutdown() failed\n", lu->num);
2142 /* ignore error */
2143 }
2144 break;
2145
2146 case ISTGT_LU_TYPE_DVD:
2147 rc = istgt_lu_dvd_shutdown(istgt, lu);
2148 if (rc < 0) {
2149 ISTGT_ERRLOG("LU%d: lu_dvd_shutdown() failed\n", lu->num);
2150 /* ignore error */
2151 }
2152 break;
2153
2154 case ISTGT_LU_TYPE_TAPE:
2155 rc = istgt_lu_tape_shutdown(istgt, lu);
2156 if (rc < 0) {
2157 ISTGT_ERRLOG("LU%d: lu_tape_shutdown() failed\n", lu->num);
2158 /* ignore error */
2159 }
2160 break;
2161
2162 case ISTGT_LU_TYPE_NONE:
2163 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2164 break;
2165 default:
2166 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2167 return -1;
2168 }
2169
2170 rc = istgt_lu_del_unit(istgt, lu);
2171 if (rc < 0) {
2172 ISTGT_ERRLOG("LU%d: lu_del_unit() failed\n", lu->num);
2173 /* ignore error */
2174 }
2175
2176 if (lu->queue_depth != 0) {
2177 rc = pthread_cond_broadcast(&lu->queue_cond);
2178 if (rc != 0) {
2179 ISTGT_ERRLOG("LU%d: cond_broadcast() failed\n", lu->num);
2180 }
2181 rc = pthread_join(lu->thread, NULL);
2182 if (rc != 0) {
2183 ISTGT_ERRLOG("LU%d: pthread_join() failed\n", lu->num);
2184 }
2185 }
2186 rc = pthread_cond_destroy(&lu->queue_cond);
2187 if (rc != 0) {
2188 ISTGT_ERRLOG("LU%d: cond_destroy() failed\n", lu->num);
2189 /* ignore error */
2190 }
2191 rc = pthread_mutex_destroy(&lu->queue_mutex);
2192 if (rc != 0) {
2193 ISTGT_ERRLOG("LU%d: mutex_destroy() failed\n", lu->num);
2194 /* ignore error */
2195 }
2196 rc = pthread_mutex_destroy(&lu->state_mutex);
2197 if (rc != 0) {
2198 ISTGT_ERRLOG("LU%d: mutex_destroy() failed\n", lu->num);
2199 /* ignore error */
2200 }
2201 rc = pthread_mutex_destroy(&lu->mutex);
2202 if (rc != 0) {
2203 ISTGT_ERRLOG("LU%d: mutex_destroy() failed\n", lu->num);
2204 /* ignore error */
2205 }
2206 xfree(lu);
2207 istgt->logical_unit[i] = NULL;
2208 }
2209
2210 return 0;
2211 }
2212
2213 int
2214 istgt_lu_islun2lun(uint64_t islun)
2215 {
2216 uint64_t fmt_lun;
2217 uint64_t method;
2218 int lun_i;
2219
2220 fmt_lun = islun;
2221 method = (fmt_lun >> 62) & 0x03U;
2222 fmt_lun = fmt_lun >> 48;
2223 if (method == 0x00U) {
2224 lun_i = (int) (fmt_lun & 0x00ffU);
2225 } else if (method == 0x01U) {
2226 lun_i = (int) (fmt_lun & 0x3fffU);
2227 } else {
2228 lun_i = 0xffffU;
2229 }
2230 return lun_i;
2231 }
2232
2233 uint64_t
2234 istgt_lu_lun2islun(int lun, int maxlun)
2235 {
2236 uint64_t fmt_lun;
2237 uint64_t method;
2238 uint64_t islun;
2239
2240 islun = (uint64_t) lun;
2241 if (maxlun <= 0x0100) {
2242 /* below 256 */
2243 method = 0x00U;
2244 fmt_lun = (method & 0x03U) << 62;
2245 fmt_lun |= (islun & 0x00ffU) << 48;
2246 } else if (maxlun <= 0x4000U) {
2247 /* below 16384 */
2248 method = 0x01U;
2249 fmt_lun = (method & 0x03U) << 62;
2250 fmt_lun |= (islun & 0x3fffU) << 48;
2251 } else {
2252 /* XXX */
2253 fmt_lun = ~((uint64_t) 0);
2254 }
2255 return fmt_lun;
2256 }
2257
2258 int
2259 istgt_lu_reset(ISTGT_LU_Ptr lu, uint64_t lun)
2260 {
2261 int lun_i;
2262 int rc;
2263
2264 if (lu == NULL)
2265 return -1;
2266
2267 lun_i = istgt_lu_islun2lun(lun);
2268
2269 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LU%d: Name=%s, LUN=%d\n",
2270 lu->num, lu->name, lun_i);
2271
2272 switch (lu->type) {
2273 case ISTGT_LU_TYPE_PASS:
2274 MTX_LOCK(&lu->mutex);
2275 rc = istgt_lu_pass_reset(lu, lun_i);
2276 MTX_UNLOCK(&lu->mutex);
2277 if (rc < 0) {
2278 ISTGT_ERRLOG("LU%d: lu_pass_reset() failed\n", lu->num);
2279 return -1;
2280 }
2281 break;
2282
2283 case ISTGT_LU_TYPE_DISK:
2284 MTX_LOCK(&lu->mutex);
2285 rc = istgt_lu_disk_reset(lu, lun_i);
2286 MTX_UNLOCK(&lu->mutex);
2287 if (rc < 0) {
2288 ISTGT_ERRLOG("LU%d: lu_disk_reset() failed\n", lu->num);
2289 return -1;
2290 }
2291 break;
2292
2293 case ISTGT_LU_TYPE_DVD:
2294 MTX_LOCK(&lu->mutex);
2295 rc = istgt_lu_dvd_reset(lu, lun_i);
2296 MTX_UNLOCK(&lu->mutex);
2297 if (rc < 0) {
2298 ISTGT_ERRLOG("LU%d: lu_dvd_reset() failed\n", lu->num);
2299 return -1;
2300 }
2301 break;
2302
2303 case ISTGT_LU_TYPE_TAPE:
2304 MTX_LOCK(&lu->mutex);
2305 rc = istgt_lu_tape_reset(lu, lun_i);
2306 MTX_UNLOCK(&lu->mutex);
2307 if (rc < 0) {
2308 ISTGT_ERRLOG("LU%d: lu_tape_reset() failed\n", lu->num);
2309 return -1;
2310 }
2311 break;
2312
2313 case ISTGT_LU_TYPE_NONE:
2314 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2315 break;
2316 default:
2317 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2318 return -1;
2319 }
2320
2321 return 0;
2322 }
2323
2324 int
2325 istgt_lu_execute(CONN_Ptr conn, ISTGT_LU_CMD_Ptr lu_cmd)
2326 {
2327 ISTGT_LU_Ptr lu;
2328 int rc;
2329
2330 if (lu_cmd == NULL)
2331 return -1;
2332 lu = lu_cmd->lu;
2333 if (lu == NULL)
2334 return -1;
2335
2336 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
2337 "LU%d: PG=0x%4.4x, Name=%s, LUN=0x%16.16"PRIx64"\n",
2338 lu->num, conn->portal.tag, lu->name, lu_cmd->lun);
2339
2340 if (lu->online == 0) {
2341 ISTGT_TRACELOG(ISTGT_TRACE_SCSI, "LU%d: offline\n", lu->num);
2342 /* LOGICAL UNIT NOT READY, CAUSE NOT REPORTABLE */
2343 lu_cmd->sense_data_len
2344 = istgt_lu_scsi_build_sense_data(lu_cmd->sense_data,
2345 ISTGT_SCSI_SENSE_NOT_READY,
2346 0x04, 0x00);
2347 lu_cmd->status = ISTGT_SCSI_STATUS_CHECK_CONDITION;
2348 return 0;
2349 }
2350
2351 rc = 0;
2352 switch (lu->type) {
2353 case ISTGT_LU_TYPE_PASS:
2354 MTX_LOCK(&lu->mutex);
2355 rc = istgt_lu_pass_execute(conn, lu_cmd);
2356 MTX_UNLOCK(&lu->mutex);
2357 if (rc < 0) {
2358 ISTGT_ERRLOG("LU%d: lu_pass_execute() failed\n",
2359 lu->num);
2360 return -1;
2361 }
2362 break;
2363
2364 case ISTGT_LU_TYPE_DISK:
2365 if (lu->queue_depth != 0) {
2366 //MTX_LOCK(&lu->queue_mutex);
2367 rc = istgt_lu_disk_queue(conn, lu_cmd);
2368 //MTX_UNLOCK(&lu->queue_mutex);
2369 if (rc < 0) {
2370 ISTGT_ERRLOG("LU%d: lu_disk_queue() failed\n",
2371 lu->num);
2372 return -1;
2373 }
2374 } else {
2375 MTX_LOCK(&lu->mutex);
2376 rc = istgt_lu_disk_execute(conn, lu_cmd);
2377 MTX_UNLOCK(&lu->mutex);
2378 if (rc < 0) {
2379 ISTGT_ERRLOG("LU%d: lu_disk_execute() failed\n",
2380 lu->num);
2381 return -1;
2382 }
2383 }
2384 break;
2385
2386 case ISTGT_LU_TYPE_DVD:
2387 MTX_LOCK(&lu->mutex);
2388 rc = istgt_lu_dvd_execute(conn, lu_cmd);
2389 MTX_UNLOCK(&lu->mutex);
2390 if (rc < 0) {
2391 ISTGT_ERRLOG("LU%d: lu_dvd_execute() failed\n",
2392 lu->num);
2393 return -1;
2394 }
2395 break;
2396
2397 case ISTGT_LU_TYPE_TAPE:
2398 MTX_LOCK(&lu->mutex);
2399 rc = istgt_lu_tape_execute(conn, lu_cmd);
2400 MTX_UNLOCK(&lu->mutex);
2401 if (rc < 0) {
2402 ISTGT_ERRLOG("LU%d: lu_tape_execute() failed\n",
2403 lu->num);
2404 return -1;
2405 }
2406 break;
2407
2408 case ISTGT_LU_TYPE_NONE:
2409 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2410 break;
2411 default:
2412 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2413 return -1;
2414 }
2415
2416 return rc;
2417 }
2418
2419 int
2420 istgt_lu_create_task(CONN_Ptr conn, ISTGT_LU_CMD_Ptr lu_cmd, ISTGT_LU_TASK_Ptr lu_task, int lun)
2421 {
2422 ISCSI_PDU_Ptr dst_pdu, src_pdu;
2423 uint8_t *cdb;
2424 int alloc_len;
2425 #if 0
2426 int rc;
2427 #endif
2428
2429 if (lu_task == NULL)
2430 return -1;
2431
2432 lu_task->type = ISTGT_LU_TASK_RESPONSE;
2433 lu_task->conn = conn;
2434 strncpy(lu_task->initiator_name, conn->initiator_name,
2435 sizeof lu_task->initiator_name);
2436 strncpy(lu_task->initiator_port, conn->initiator_port,
2437 sizeof lu_task->initiator_port);
2438
2439 lu_task->lun = (int) lun;
2440 lu_task->use_cond = 0;
2441 lu_task->dup_iobuf = 0;
2442 lu_task->iobuf = NULL;
2443 lu_task->data = NULL;
2444 lu_task->sense_data = NULL;
2445 lu_task->create_time = 0;
2446 lu_task->condwait = 0;
2447 lu_task->offset = 0;
2448 lu_task->req_execute = 0;
2449 lu_task->req_transfer_out = 0;
2450 lu_task->error = 0;
2451 lu_task->abort = 0;
2452 lu_task->execute = 0;
2453 lu_task->complete = 0;
2454 lu_task->lock = 0;
2455
2456 #if 0
2457 rc = pthread_mutex_init(&lu_task->trans_mutex, NULL);
2458 if (rc != 0) {
2459 ISTGT_ERRLOG("mutex_init() failed\n");
2460 return -1;
2461 }
2462 rc = pthread_cond_init(&lu_task->trans_cond, NULL);
2463 if (rc != 0) {
2464 ISTGT_ERRLOG("cond_init() failed\n");
2465 return -1;
2466 }
2467 rc = pthread_cond_init(&lu_task->exec_cond, NULL);
2468 if (rc != 0) {
2469 ISTGT_ERRLOG("cond_init() failed\n");
2470 return -1;
2471 }
2472 #endif
2473
2474 lu_task->lu_cmd.pdu = xmalloc(sizeof *lu_task->lu_cmd.pdu);
2475 memset(lu_task->lu_cmd.pdu, 0, sizeof *lu_task->lu_cmd.pdu);
2476
2477 /* copy PDU */
2478 dst_pdu = lu_task->lu_cmd.pdu;
2479 src_pdu = lu_cmd->pdu;
2480 memcpy(&dst_pdu->bhs, &src_pdu->bhs, ISCSI_BHS_LEN);
2481 dst_pdu->ahs = src_pdu->ahs;
2482 memcpy(dst_pdu->header_digest, src_pdu->header_digest, ISCSI_DIGEST_LEN);
2483 if (src_pdu->data == src_pdu->shortdata) {
2484 memcpy(dst_pdu->shortdata, src_pdu->shortdata,
2485 sizeof src_pdu->shortdata);
2486 dst_pdu->data = dst_pdu->shortdata;
2487 } else {
2488 dst_pdu->data = src_pdu->data;
2489 }
2490 memcpy(dst_pdu->data_digest, src_pdu->data_digest, ISCSI_DIGEST_LEN);
2491 dst_pdu->total_ahs_len = src_pdu->total_ahs_len;
2492 dst_pdu->data_segment_len = src_pdu->data_segment_len;
2493 dst_pdu->copy_pdu = 0;
2494 src_pdu->copy_pdu = 1;
2495
2496 /* copy other lu_cmd */
2497 lu_task->lu_cmd.lu = lu_cmd->lu;
2498 cdb = ((uint8_t *) &lu_task->lu_cmd.pdu->bhs) + 32;
2499 lu_task->lu_cmd.I_bit = lu_cmd->I_bit;
2500 lu_task->lu_cmd.F_bit = lu_cmd->F_bit;
2501 lu_task->lu_cmd.R_bit = lu_cmd->R_bit;
2502 lu_task->lu_cmd.W_bit = lu_cmd->W_bit;
2503 lu_task->lu_cmd.Attr_bit = lu_cmd->Attr_bit;
2504 lu_task->lu_cmd.lun = lu_cmd->lun;
2505 lu_task->lu_cmd.task_tag = lu_cmd->task_tag;
2506 lu_task->lu_cmd.transfer_len = lu_cmd->transfer_len;
2507 //lu_task->lu_cmd.cdb = lu_cmd->cdb;
2508 lu_task->lu_cmd.cdb = cdb;
2509 lu_task->lu_cmd.CmdSN = lu_cmd->CmdSN;
2510
2511 //lu_task->lu_cmd.iobuf = lu_cmd->iobuf;
2512 lu_task->lu_cmd.iobuf = NULL;
2513 lu_task->lu_cmd.iobufsize = lu_cmd->iobufsize;
2514 lu_task->lu_cmd.data = lu_cmd->data;
2515 lu_task->lu_cmd.data_len = lu_cmd->data_len;
2516 lu_task->lu_cmd.alloc_len = lu_cmd->alloc_len;
2517
2518 lu_task->lu_cmd.status = lu_cmd->status;
2519 lu_task->lu_cmd.sense_data = lu_cmd->sense_data;
2520 lu_task->lu_cmd.sense_data_len = lu_cmd->sense_data_len;
2521 lu_task->lu_cmd.sense_alloc_len = lu_cmd->sense_alloc_len;
2522
2523 /* pre allocate buffer */
2524 lu_task->lu_cmd.iobufsize = lu_cmd->transfer_len + 65536;
2525 #if 0
2526 lu_task->data = xmalloc(lu_cmd->alloc_len);
2527 lu_task->sense_data = xmalloc(lu_cmd->sense_alloc_len);
2528 lu_task->iobuf = xmalloc(lu_task->lu_cmd.iobufsize);
2529 #else
2530 alloc_len = ISCSI_ALIGN(lu_cmd->alloc_len);
2531 alloc_len += ISCSI_ALIGN(lu_cmd->sense_alloc_len);
2532 alloc_len += ISCSI_ALIGN(lu_task->lu_cmd.iobufsize);
2533 lu_task->data = xmalloc(alloc_len);
2534 lu_task->sense_data = lu_task->data + ISCSI_ALIGN(lu_cmd->alloc_len);
2535 lu_task->iobuf = lu_task->sense_data + ISCSI_ALIGN(lu_cmd->sense_alloc_len);
2536 #endif
2537
2538 /* creation time */
2539 lu_task->create_time = time(NULL);
2540 /* wait time */
2541 lu_task->condwait = conn->timeout * 1000;
2542 if (lu_task->condwait < ISTGT_CONDWAIT_MIN) {
2543 lu_task->condwait = ISTGT_CONDWAIT_MIN;
2544 }
2545
2546 return 0;
2547 }
2548
2549 int
2550 istgt_lu_destroy_task(ISTGT_LU_TASK_Ptr lu_task)
2551 {
2552 int rc;
2553
2554 if (lu_task == NULL)
2555 return -1;
2556
2557 if (lu_task->use_cond != 0) {
2558 rc = pthread_mutex_destroy(&lu_task->trans_mutex);
2559 if (rc != 0) {
2560 ISTGT_ERRLOG("mutex_destroy() failed\n");
2561 return -1;
2562 }
2563 rc = pthread_cond_destroy(&lu_task->trans_cond);
2564 if (rc != 0) {
2565 ISTGT_ERRLOG("cond_destroy() failed\n");
2566 return -1;
2567 }
2568 rc = pthread_cond_destroy(&lu_task->exec_cond);
2569 if (rc != 0) {
2570 ISTGT_ERRLOG("cond_destroy() failed\n");
2571 return -1;
2572 }
2573 }
2574 if (lu_task->lu_cmd.pdu != NULL) {
2575 if (lu_task->lu_cmd.pdu->copy_pdu == 0) {
2576 xfree(lu_task->lu_cmd.pdu->ahs);
2577 if (lu_task->lu_cmd.pdu->data
2578 != lu_task->lu_cmd.pdu->shortdata) {
2579 xfree(lu_task->lu_cmd.pdu->data);
2580 }
2581 }
2582 xfree(lu_task->lu_cmd.pdu);
2583 }
2584 #if 0
2585 if (lu_task->dup_iobuf == 0) {
2586 xfree(lu_task->iobuf);
2587 }
2588 xfree(lu_task->data);
2589 xfree(lu_task->sense_data);
2590 #else
2591 xfree(lu_task->data);
2592 #endif
2593 xfree(lu_task);
2594 return 0;
2595 }
2596
2597 int
2598 istgt_lu_clear_task_IT(CONN_Ptr conn, ISTGT_LU_Ptr lu)
2599 {
2600 int rc;
2601
2602 if (lu == NULL)
2603 return -1;
2604
2605 if (lu->queue_depth == 0)
2606 return 0;
2607
2608 rc = 0;
2609 switch (lu->type) {
2610 case ISTGT_LU_TYPE_DISK:
2611 MTX_LOCK(&lu->mutex);
2612 rc = istgt_lu_disk_queue_clear_IT(conn, lu);
2613 MTX_UNLOCK(&lu->mutex);
2614 if (rc < 0) {
2615 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_IT() failed\n", lu->num);
2616 return -1;
2617 }
2618 break;
2619
2620 case ISTGT_LU_TYPE_DVD:
2621 case ISTGT_LU_TYPE_TAPE:
2622 case ISTGT_LU_TYPE_NONE:
2623 default:
2624 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2625 return -1;
2626 }
2627
2628 return 0;
2629 }
2630
2631 int
2632 istgt_lu_clear_task_ITL(CONN_Ptr conn, ISTGT_LU_Ptr lu, uint64_t lun)
2633 {
2634 int lun_i;
2635 int rc;
2636
2637 if (lu == NULL)
2638 return -1;
2639
2640 if (lu->queue_depth == 0)
2641 return 0;
2642
2643 lun_i = istgt_lu_islun2lun(lun);
2644
2645 rc = 0;
2646 switch (lu->type) {
2647 case ISTGT_LU_TYPE_DISK:
2648 MTX_LOCK(&lu->mutex);
2649 rc = istgt_lu_disk_queue_clear_ITL(conn, lu, lun_i);
2650 MTX_UNLOCK(&lu->mutex);
2651 if (rc < 0) {
2652 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_ITL() failed\n", lu->num);
2653 return -1;
2654 }
2655 break;
2656
2657 case ISTGT_LU_TYPE_DVD:
2658 case ISTGT_LU_TYPE_TAPE:
2659 case ISTGT_LU_TYPE_NONE:
2660 default:
2661 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2662 return -1;
2663 }
2664
2665 return 0;
2666 }
2667
2668 int
2669 istgt_lu_clear_task_ITLQ(CONN_Ptr conn, ISTGT_LU_Ptr lu, uint64_t lun, uint32_t CmdSN)
2670 {
2671 int lun_i;
2672 int rc;
2673
2674 if (lu == NULL)
2675 return -1;
2676
2677 if (lu->queue_depth == 0)
2678 return 0;
2679
2680 lun_i = istgt_lu_islun2lun(lun);
2681
2682 rc = 0;
2683 switch (lu->type) {
2684 case ISTGT_LU_TYPE_DISK:
2685 MTX_LOCK(&lu->mutex);
2686 rc = istgt_lu_disk_queue_clear_ITLQ(conn, lu, lun_i, CmdSN);
2687 MTX_UNLOCK(&lu->mutex);
2688 if (rc < 0) {
2689 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_ITLQ() failed\n", lu->num);
2690 return -1;
2691 }
2692 break;
2693
2694 case ISTGT_LU_TYPE_DVD:
2695 case ISTGT_LU_TYPE_TAPE:
2696 case ISTGT_LU_TYPE_NONE:
2697 default:
2698 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2699 return -1;
2700 }
2701
2702 return 0;
2703 }
2704
2705 int
2706 istgt_lu_clear_all_task(ISTGT_LU_Ptr lu, uint64_t lun)
2707 {
2708 int rc;
2709
2710 if (lu == NULL)
2711 return -1;
2712
2713 if (lu->queue_depth == 0)
2714 return 0;
2715
2716 rc = 0;
2717 switch (lu->type) {
2718 case ISTGT_LU_TYPE_DISK:
2719 MTX_LOCK(&lu->mutex);
2720 rc = istgt_lu_disk_queue_clear_all(lu, lun);
2721 MTX_UNLOCK(&lu->mutex);
2722 if (rc < 0) {
2723 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_all() failed\n", lu->num);
2724 return -1;
2725 }
2726 break;
2727
2728 case ISTGT_LU_TYPE_DVD:
2729 case ISTGT_LU_TYPE_TAPE:
2730 case ISTGT_LU_TYPE_NONE:
2731 default:
2732 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2733 return -1;
2734 }
2735
2736 return 0;
2737 }
2738
2739 static void *
2740 luworker(void *arg)
2741 {
2742 ISTGT_LU_Ptr lu = (ISTGT_LU_Ptr) arg;
2743 sigset_t signew, sigold;
2744 #if 0
2745 struct timespec abstime;
2746 time_t now;
2747 int timeout = 20; /* XXX */
2748 #endif
2749 int qcnt;
2750 int lun;
2751 int rc;
2752
2753 sigemptyset(&signew);
2754 sigemptyset(&sigold);
2755 sigaddset(&signew, ISTGT_SIGWAKEUP);
2756 pthread_sigmask(SIG_UNBLOCK, &signew, &sigold);
2757
2758 while (istgt_get_state(lu->istgt) != ISTGT_STATE_RUNNING) {
2759 if (istgt_get_state(lu->istgt) == ISTGT_STATE_EXITING
2760 || istgt_get_state(lu->istgt) == ISTGT_STATE_SHUTDOWN) {
2761 ISTGT_ERRLOG("exit before running\n");
2762 return NULL;
2763 }
2764 //ISTGT_WARNLOG("Wait for running\n");
2765 sleep(1);
2766 continue;
2767 }
2768
2769 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LU%d loop start\n", lu->num);
2770 lun = 0;
2771 qcnt = 0;
2772 #if 0
2773 memset(&abstime, 0, sizeof abstime);
2774 #endif
2775 while (1) {
2776 switch (lu->type) {
2777 case ISTGT_LU_TYPE_DISK:
2778 while (1) {
2779 if (istgt_lu_get_state(lu) != ISTGT_STATE_RUNNING) {
2780 goto loop_exit;
2781 }
2782 MTX_LOCK(&lu->queue_mutex);
2783 qcnt = istgt_lu_disk_queue_count(lu, &lun);
2784 if (qcnt == 0) {
2785 #if 0
2786 now = time(NULL);
2787 abstime.tv_sec = now + timeout;
2788 abstime.tv_nsec = 0;
2789 rc = pthread_cond_timedwait(&lu->queue_cond,
2790 &lu->queue_mutex, &abstime);
2791 if (rc == ETIMEDOUT) {
2792 /* nothing */
2793 }
2794 #else
2795 pthread_cond_wait(&lu->queue_cond,
2796 &lu->queue_mutex);
2797 #endif
2798 qcnt = istgt_lu_disk_queue_count(lu, &lun);
2799 if (qcnt == 0) {
2800 MTX_UNLOCK(&lu->queue_mutex);
2801 continue;
2802 }
2803 }
2804 MTX_UNLOCK(&lu->queue_mutex);
2805 break;
2806 }
2807 if (qcnt < 0) {
2808 ISTGT_ERRLOG("LU%d: lu_disk_queue_count() failed\n",
2809 lu->num);
2810 break;
2811 }
2812 rc = istgt_lu_disk_queue_start(lu, lun);
2813 lun++;
2814 if (rc == -2) {
2815 ISTGT_WARNLOG("LU%d: lu_disk_queue_start() aborted\n",
2816 lu->num);
2817 break;
2818 }
2819 if (rc < 0) {
2820 ISTGT_ERRLOG("LU%d: lu_disk_queue_start() failed\n",
2821 lu->num);
2822 break;
2823 }
2824 break;
2825
2826 case ISTGT_LU_TYPE_DVD:
2827 case ISTGT_LU_TYPE_TAPE:
2828 case ISTGT_LU_TYPE_NONE:
2829 default:
2830 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2831 return NULL;
2832 }
2833
2834 #if 0
2835 /* still running? */
2836 if (qcnt <= 1) {
2837 if (istgt_lu_get_state(lu) != ISTGT_STATE_RUNNING) {
2838 goto loop_exit;
2839 }
2840 }
2841 #endif
2842 }
2843 loop_exit:
2844 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LU%d loop ended\n", lu->num);
2845
2846 return NULL;
2847 }