Debian Codegraph @

src/istgt_lu.c

raw symbols ◀ ▶ SCIP
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, int ignore_dup)
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 (ignore dup = %d)\n", sp->num, ignore_dup);
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 && !ignore_dup) {
1230 ISTGT_ERRLOG("LU%d: duplicate unit\n", sp->num);
1231 return -1;
1232 }
•1233 /* existing LUNs */
1234 if (istgt->logical_unit[sp->num] != NULL && ignore_dup) {
1235 istgt->logical_unit[sp->num]->to_add = 0;
1236 istgt->logical_unit[sp->num]->to_remove = 0;
1237 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "skip existing unit %d\n", sp->num);
1238 return 0;
1239 }
1240
1241 /* new LUNs */
1242 if (istgt->logical_unit[sp->num] == NULL && ignore_dup) {
1243 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "add NEW unit %d\n", sp->num);
1244 }
1245
1246 lu = xmalloc(sizeof *lu);
1247 memset(lu, 0, sizeof *lu);
1248 lu->num = sp->num;
1249 lu->istgt = istgt;
•1250
1251 /* tag new LUNs */
1252 if (ignore_dup) {
1253 lu->to_add = 1;
1254 lu->to_remove = 0;
1255 }
1256
1257 istgt_lu_set_state(lu, ISTGT_STATE_INVALID);
1258 nbs64 = istgt_lu_get_nbserial(istgt->nodebase);
1259 #if 0
1260 /* disabled now */
1261 nbs = (int) (nbs64 % 900) * 100000;
1262 #else
1263 nbs = 0;
1264 #endif
1265
1266 val = istgt_get_val(sp, "Comment");
1267 if (val != NULL) {
1268 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Comment %s\n", val);
1269 }
1270
1271 val = istgt_get_val(sp, "TargetName");
1272 if (val == NULL) {
1273 ISTGT_ERRLOG("LU%d: TargetName not found\n", lu->num);
1274 goto error_return;
1275 }
1276 if (strncasecmp(val, "iqn.", 4) != 0
1277 && strncasecmp(val, "eui.", 4) != 0
1278 && strncasecmp(val, "naa.", 4) != 0) {
1279 snprintf(buf, sizeof buf, "%s:%s", istgt->nodebase, val);
1280 } else {
1281 snprintf(buf, sizeof buf, "%s", val);
1282 }
1283 if (istgt_lu_check_iscsi_name(buf) != 0) {
1284 ISTGT_ERRLOG("TargetName %s contains an invalid character or format.\n",
1285 buf);
1286 #if 0
1287 goto error_return;
1288 #endif
1289 }
1290 lu->name = xstrdup(buf);
1291 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "TargetName %s\n",
1292 lu->name);
1293
1294 val = istgt_get_val(sp, "TargetAlias");
1295 if (val == NULL) {
1296 lu->alias = NULL;
1297 } else {
1298 lu->alias = xstrdup(val);
1299 }
1300 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "TargetAlias %s\n",
1301 lu->alias);
1302
1303 val = istgt_get_val(sp, "Mapping");
1304 if (val == NULL) {
1305 /* no map */
1306 lu->maxmap = 0;
1307 } else {
1308 lu->maxmap = 0;
1309 for (i = 0; ; i++) {
1310 val = istgt_get_nmval(sp, "Mapping", i, 0);
1311 if (val == NULL)
1312 break;
1313 if (lu->maxmap >= MAX_LU_MAP) {
1314 ISTGT_ERRLOG("LU%d: too many mapping\n", lu->num);
1315 goto error_return;
1316 }
1317 pg_tag = istgt_get_nmval(sp, "Mapping", i, 0);
1318 ig_tag = istgt_get_nmval(sp, "Mapping", i, 1);
1319 if (pg_tag == NULL || ig_tag == NULL) {
1320 ISTGT_ERRLOG("LU%d: mapping error\n", lu->num);
1321 goto error_return;
1322 }
1323 if (strncasecmp(pg_tag, "PortalGroup",
1324 strlen("PortalGroup")) != 0
1325 || sscanf(pg_tag, "%*[^0-9]%d", &pg_tag_i) != 1) {
1326 ISTGT_ERRLOG("LU%d: mapping portal error\n", lu->num);
1327 goto error_return;
1328 }
1329 if (strncasecmp(ig_tag, "InitiatorGroup",
1330 strlen("InitiatorGroup")) != 0
1331 || sscanf(ig_tag, "%*[^0-9]%d", &ig_tag_i) != 1) {
1332 ISTGT_ERRLOG("LU%d: mapping initiator error\n", lu->num);
1333 goto error_return;
1334 }
1335 if (pg_tag_i < 1 || ig_tag_i < 1) {
1336 ISTGT_ERRLOG("LU%d: invalid group tag\n", lu->num);
1337 goto error_return;
1338 }
1339 if (istgt_lu_find_portalgroup(istgt, pg_tag_i) == NULL) {
1340 ISTGT_ERRLOG("LU%d: PortalGroup%d not found\n",
1341 lu->num, pg_tag_i);
1342 goto error_return;
1343 }
1344 if (istgt_lu_find_initiatorgroup(istgt, ig_tag_i) == NULL) {
1345 ISTGT_ERRLOG("LU%d: InitiatorGroup%d not found\n",
1346 lu->num, ig_tag_i);
1347 goto error_return;
1348 }
1349 lu->map[i].pg_tag = pg_tag_i;
1350 lu->map[i].pg_aas = AAS_ACTIVE_OPTIMIZED;
1351 //lu->map[i].pg_aas = AAS_ACTIVE_NON_OPTIMIZED;
1352 lu->map[i].pg_aas |= AAS_STATUS_IMPLICIT;
1353 lu->map[i].ig_tag = ig_tag_i;
1354 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1355 "Mapping PortalGroup%d InitiatorGroup%d\n",
1356 lu->map[i].pg_tag, lu->map[i].ig_tag);
1357 lu->maxmap = i + 1;
1358 }
1359 }
1360 if (lu->maxmap == 0) {
1361 ISTGT_ERRLOG("LU%d: no Mapping\n", lu->num);
1362 goto error_return;
1363 }
1364
1365 val = istgt_get_val(sp, "AuthMethod");
1366 if (val == NULL) {
1367 /* none */
1368 lu->no_auth_chap = 0;
1369 lu->auth_chap = 0;
1370 lu->auth_chap_mutual = 0;
1371 } else {
1372 lu->no_auth_chap = 0;
1373 for (i = 0; ; i++) {
1374 val = istgt_get_nmval(sp, "AuthMethod", 0, i);
1375 if (val == NULL)
1376 break;
1377 if (strcasecmp(val, "CHAP") == 0) {
1378 lu->auth_chap = 1;
1379 } else if (strcasecmp(val, "Mutual") == 0) {
1380 lu->auth_chap_mutual = 1;
1381 } else if (strcasecmp(val, "Auto") == 0) {
1382 lu->auth_chap = 0;
1383 lu->auth_chap_mutual = 0;
1384 } else if (strcasecmp(val, "None") == 0) {
1385 lu->no_auth_chap = 1;
1386 lu->auth_chap = 0;
1387 lu->auth_chap_mutual = 0;
1388 } else {
1389 ISTGT_ERRLOG("LU%d: unknown auth\n", lu->num);
1390 goto error_return;
1391 }
1392 }
1393 if (lu->auth_chap_mutual && !lu->auth_chap) {
1394 ISTGT_ERRLOG("LU%d: Mutual but not CHAP\n", lu->num);
1395 goto error_return;
1396 }
1397 }
1398 if (lu->no_auth_chap != 0) {
1399 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthMethod None\n");
1400 } else if (lu->auth_chap == 0) {
1401 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthMethod Auto\n");
1402 } else {
1403 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthMethod %s %s\n",
1404 lu->auth_chap ? "CHAP" : "",
1405 lu->auth_chap_mutual ? "Mutual" : "");
1406 }
1407
1408 val = istgt_get_val(sp, "AuthGroup");
1409 if (val == NULL) {
1410 lu->auth_group = 0;
1411 } else {
1412 ag_tag = val;
1413 if (strcasecmp(ag_tag, "None") == 0) {
1414 ag_tag_i = 0;
1415 } else {
1416 if (strncasecmp(ag_tag, "AuthGroup",
1417 strlen("AuthGroup")) != 0
1418 || sscanf(ag_tag, "%*[^0-9]%d", &ag_tag_i) != 1) {
1419 ISTGT_ERRLOG("LU%d: auth group error\n", lu->num);
1420 goto error_return;
1421 }
1422 if (ag_tag_i == 0) {
1423 ISTGT_ERRLOG("LU%d: invalid auth group %d\n", ag_tag_i);
1424 goto error_return;
1425 }
1426 }
1427 lu->auth_group = ag_tag_i;
1428 }
1429 if (lu->auth_group == 0) {
1430 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthGroup None\n");
1431 } else {
1432 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "AuthGroup AuthGroup%d\n",
1433 lu->auth_group);
1434 }
1435
1436 val = istgt_get_val(sp, "UseDigest");
1437 if (val != NULL) {
1438 for (i = 0; ; i++) {
1439 val = istgt_get_nmval(sp, "UseDigest", 0, i);
1440 if (val == NULL)
1441 break;
1442 if (strcasecmp(val, "Header") == 0) {
1443 lu->header_digest = 1;
1444 } else if (strcasecmp(val, "Data") == 0) {
1445 lu->data_digest = 1;
1446 } else if (strcasecmp(val, "Auto") == 0) {
1447 lu->header_digest = 0;
1448 lu->data_digest = 0;
1449 } else {
1450 ISTGT_ERRLOG("LU%d: unknown digest\n", lu->num);
1451 goto error_return;
1452 }
1453 }
1454 }
1455 if (lu->header_digest == 0 && lu->data_digest == 0) {
1456 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UseDigest Auto\n");
1457 } else {
1458 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UseDigest %s %s\n",
1459 lu->header_digest ? "Header" : "",
1460 lu->data_digest ? "Data" : "");
1461 }
1462
1463 val = istgt_get_val(sp, "ReadOnly");
1464 if (val == NULL) {
1465 lu->readonly = 0;
1466 } else if (strcasecmp(val, "Yes") == 0) {
1467 lu->readonly = 1;
1468 }
1469 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "ReadOnly %s\n",
1470 lu->readonly ? "Yes" : "No");
1471
1472 val = istgt_get_val(sp, "UnitType");
1473 if (val == NULL) {
1474 ISTGT_ERRLOG("LU%d: unknown unit type\n", lu->num);
1475 goto error_return;
1476 }
1477 if (strcasecmp(val, "Pass") == 0) {
1478 lu->type = ISTGT_LU_TYPE_PASS;
1479 } else if (strcasecmp(val, "Disk") == 0) {
1480 lu->type = ISTGT_LU_TYPE_DISK;
1481 } else if (strcasecmp(val, "DVD") == 0) {
1482 lu->type = ISTGT_LU_TYPE_DVD;
1483 } else if (strcasecmp(val, "Tape") == 0) {
1484 lu->type = ISTGT_LU_TYPE_TAPE;
1485 } else {
1486 ISTGT_ERRLOG("LU%d: unknown unit type\n", lu->num);
1487 goto error_return;
1488 }
1489 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UnitType %d (%s)\n",
1490 lu->type, val);
1491
1492 val = istgt_get_val(sp, "UnitOnline");
1493 if (val == NULL) {
1494 lu->online = 1;
1495 } else if (strcasecmp(val, "Yes") == 0) {
1496 lu->online = 1;
1497 }
1498 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UnitOnline %s\n",
1499 lu->online ? "Yes" : "No");
1500
1501 vendor = istgt_get_nmval(sp, "UnitInquiry", 0, 0);
1502 product = istgt_get_nmval(sp, "UnitInquiry", 0, 1);
1503 revision = istgt_get_nmval(sp, "UnitInquiry", 0, 2);
1504 serial = istgt_get_nmval(sp, "UnitInquiry", 0, 3);
1505 switch (lu->type) {
1506 case ISTGT_LU_TYPE_DISK:
1507 if (vendor == NULL || strlen(vendor) == 0)
1508 vendor = DEFAULT_LU_VENDOR_DISK;
1509 if (product == NULL || strlen(product) == 0)
1510 product = DEFAULT_LU_PRODUCT_DISK;
1511 if (revision == NULL || strlen(revision) == 0)
1512 revision = DEFAULT_LU_REVISION_DISK;
1513 if (serial == NULL || strlen(serial) == 0) {
1514 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1515 serial = (const char *) &buf[0];
1516 }
1517 break;
1518 case ISTGT_LU_TYPE_DVD:
1519 if (vendor == NULL || strlen(vendor) == 0)
1520 vendor = DEFAULT_LU_VENDOR_DVD;
1521 if (product == NULL || strlen(product) == 0)
1522 product = DEFAULT_LU_PRODUCT_DVD;
1523 if (revision == NULL || strlen(revision) == 0)
1524 revision = DEFAULT_LU_REVISION_DVD;
1525 if (serial == NULL || strlen(serial) == 0) {
1526 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1527 serial = (const char *) &buf[0];
1528 }
1529 break;
1530 case ISTGT_LU_TYPE_TAPE:
1531 if (vendor == NULL || strlen(vendor) == 0)
1532 vendor = DEFAULT_LU_VENDOR_TAPE;
1533 if (product == NULL || strlen(product) == 0)
1534 product = DEFAULT_LU_PRODUCT_TAPE;
1535 if (revision == NULL || strlen(revision) == 0)
1536 revision = DEFAULT_LU_REVISION_TAPE;
1537 if (serial == NULL || strlen(serial) == 0) {
1538 #ifdef USE_LU_TAPE_DLT8000
1539 snprintf(buf, sizeof buf, "CX%.8d", 10000000 + nbs + lu->num);
1540 #else
1541 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1542 #endif /* USE_LU_TAPE_DLT8000 */
1543 serial = (const char *) &buf[0];
1544 }
1545 break;
1546 default:
1547 if (vendor == NULL || strlen(vendor) == 0)
1548 vendor = DEFAULT_LU_VENDOR;
1549 if (product == NULL || strlen(product) == 0)
1550 product = DEFAULT_LU_PRODUCT;
1551 if (revision == NULL || strlen(revision) == 0)
1552 revision = DEFAULT_LU_REVISION;
1553 if (serial == NULL || strlen(serial) == 0) {
1554 snprintf(buf, sizeof buf, "%.8d", 10000000 + nbs + lu->num);
1555 serial = (const char *) &buf[0];
1556 }
1557 break;
1558 }
1559 lu->inq_vendor = xstrdup(vendor);
1560 lu->inq_product = xstrdup(product);
1561 lu->inq_revision = xstrdup(revision);
1562 lu->inq_serial = xstrdup(serial);
1563 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "UnitInquiry %s %s %s %s\n",
1564 lu->inq_vendor, lu->inq_product, lu->inq_revision,
1565 lu->inq_serial);
1566
1567 val = istgt_get_val(sp, "BlockLength");
1568 if (val == NULL) {
1569 switch (lu->type) {
1570 case ISTGT_LU_TYPE_DISK:
1571 lu->blocklen = DEFAULT_LU_BLOCKLEN_DISK;
1572 break;
1573 case ISTGT_LU_TYPE_DVD:
1574 lu->blocklen = DEFAULT_LU_BLOCKLEN_DVD;
1575 break;
1576 case ISTGT_LU_TYPE_TAPE:
1577 lu->blocklen = DEFAULT_LU_BLOCKLEN_TAPE;
1578 break;
1579 default:
1580 lu->blocklen = DEFAULT_LU_BLOCKLEN;
1581 break;
1582 }
1583 } else {
1584 lu->blocklen = (int) strtol(val, NULL, 10);
1585 }
1586 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "BlockLength %d\n",
1587 lu->blocklen);
1588
1589 val = istgt_get_val(sp, "QueueDepth");
1590 if (val == NULL) {
1591 switch (lu->type) {
1592 case ISTGT_LU_TYPE_DISK:
1593 lu->queue_depth = DEFAULT_LU_QUEUE_DEPTH;
1594 //lu->queue_depth = 0;
1595 break;
1596 case ISTGT_LU_TYPE_DVD:
1597 case ISTGT_LU_TYPE_TAPE:
1598 default:
1599 lu->queue_depth = 0;
1600 break;
1601 }
1602 } else {
1603 lu->queue_depth = (int) strtol(val, NULL, 10);
1604 }
1605 if (lu->queue_depth < 0 || lu->queue_depth >= MAX_LU_QUEUE_DEPTH) {
1606 ISTGT_ERRLOG("LU%d: queue depth range error\n");
1607 goto error_return;
1608 }
1609 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "QueueDepth %d\n",
1610 lu->queue_depth);
1611
1612 lu->maxlun = 0;
1613 for (i = 0; i < MAX_LU_LUN; i++) {
1614 lu->lun[i].type = ISTGT_LU_LUN_TYPE_NONE;
1615 lu->lun[i].rotationrate = DEFAULT_LU_ROTATIONRATE;
1616 lu->lun[i].formfactor = DEFAULT_LU_FORMFACTOR;
1617 lu->lun[i].readcache = 1;
1618 lu->lun[i].writecache = 1;
1619 lu->lun[i].serial = NULL;
1620 lu->lun[i].spec = NULL;
1621 snprintf(buf, sizeof buf, "LUN%d", i);
1622 val = istgt_get_val(sp, buf);
1623 if (val == NULL)
1624 continue;
1625 if (i != 0) {
1626 /* default LUN serial (except LUN0) */
1627 snprintf(buf2, sizeof buf2, "%sL%d", lu->inq_serial, i);
1628 lu->lun[i].serial = xstrdup(buf2);
1629 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LUN%d Serial %s (default)\n",
1630 i, buf2);
1631 }
1632 for (j = 0; ; j++) {
1633 val = istgt_get_nmval(sp, buf, j, 0);
1634 if (val == NULL)
1635 break;
1636 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LUN%d\n", i);
1637 if (strcasecmp(val, "Device") == 0) {
1638 if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_NONE) {
1639 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1640 goto error_return;
1641 }
1642 lu->lun[i].type = ISTGT_LU_LUN_TYPE_DEVICE;
1643
1644 file = istgt_get_nmval(sp, buf, j, 1);
1645 if (file == NULL) {
1646 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1647 goto error_return;
1648 }
1649 lu->lun[i].u.device.file = xstrdup(file);
1650 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "Device file=%s\n",
1651 lu->lun[i].u.device.file);
1652 } else if (strcasecmp(val, "Storage") == 0) {
1653 if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_NONE) {
1654 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1655 goto error_return;
1656 }
1657 lu->lun[i].type = ISTGT_LU_LUN_TYPE_STORAGE;
1658
1659 file = istgt_get_nmval(sp, buf, j, 1);
1660 size = istgt_get_nmval(sp, buf, j, 2);
1661 if (file == NULL || size == NULL) {
1662 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1663 goto error_return;
1664 }
1665 if (strcasecmp(size, "Auto") == 0
1666 || strcasecmp(size, "Size") == 0) {
1667 lu->lun[i].u.storage.size = istgt_lu_get_filesize(file);
1668 } else {
1669 lu->lun[i].u.storage.size = istgt_lu_parse_size(size);
1670 }
1671 if (lu->lun[i].u.storage.size == 0) {
1672 ISTGT_ERRLOG("LU%d: LUN%d: Auto size error (%s)\n", lu->num, i, file);
1673 goto error_return;
1674 }
1675 lu->lun[i].u.storage.fd = -1;
1676 lu->lun[i].u.storage.file = xstrdup(file);
1677 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1678 "Storage file=%s, size=%"PRIu64"\n",
1679 lu->lun[i].u.storage.file,
1680 lu->lun[i].u.storage.size);
1681 } else if (strcasecmp(val, "Removable") == 0) {
1682 if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_NONE) {
1683 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1684 goto error_return;
1685 }
1686 lu->lun[i].type = ISTGT_LU_LUN_TYPE_REMOVABLE;
1687
1688 flags = istgt_get_nmval(sp, buf, j, 1);
1689 file = istgt_get_nmval(sp, buf, j, 2);
1690 size = istgt_get_nmval(sp, buf, j, 3);
1691 if (flags == NULL || file == NULL || size == NULL) {
1692 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1693 goto error_return;
1694 }
1695 mflags = istgt_lu_parse_media_flags(flags);
1696 msize = istgt_lu_parse_media_size(file, size, &mflags);
1697 if (msize == 0 && strcasecmp(file, "/dev/null") == 0) {
1698 /* empty media */
1699 } else if (msize == 0) {
1700 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1701 goto error_return;
1702 }
1703 lu->lun[i].u.removable.type = 0;
1704 lu->lun[i].u.removable.id = 0;
1705 lu->lun[i].u.removable.fd = -1;
1706 lu->lun[i].u.removable.flags = mflags;
1707 lu->lun[i].u.removable.file = xstrdup(file);
1708 lu->lun[i].u.removable.size = msize;
1709 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1710 "Removable file=%s, size=%"PRIu64", flags=%x\n",
1711 lu->lun[i].u.removable.file,
1712 lu->lun[i].u.removable.size,
1713 lu->lun[i].u.removable.flags);
1714 } else if (strncasecmp(val, "Slot", 4) == 0) {
1715 if (lu->lun[i].type == ISTGT_LU_LUN_TYPE_NONE) {
1716 lu->lun[i].u.slot.maxslot = 0;
1717 for (k = 0; k < MAX_LU_LUN_SLOT; k++) {
1718 lu->lun[i].u.slot.present[k] = 0;
1719 lu->lun[i].u.slot.flags[k] = 0;
1720 lu->lun[i].u.slot.file[k] = NULL;
1721 lu->lun[i].u.slot.size[k] = 0;
1722 }
1723 } else if (lu->lun[i].type != ISTGT_LU_LUN_TYPE_SLOT) {
1724 ISTGT_ERRLOG("LU%d: duplicate LUN%d\n", lu->num, i);
1725 goto error_return;
1726 }
1727 lu->lun[i].type = ISTGT_LU_LUN_TYPE_SLOT;
1728 if (sscanf(val, "%*[^0-9]%d", &slot) != 1) {
1729 ISTGT_ERRLOG("LU%d: slot number error\n", lu->num);
1730 goto error_return;
1731 }
1732 if (slot < 0 || slot >= MAX_LU_LUN_SLOT) {
1733 ISTGT_ERRLOG("LU%d: slot number range error\n", lu->num);
1734 goto error_return;
1735 }
1736 if (lu->lun[i].u.slot.present[slot]) {
1737 ISTGT_ERRLOG("LU%d: duplicate slot %d\n", lu->num, slot);
1738 goto error_return;
1739 }
1740 lu->lun[i].u.slot.present[slot] = 1;
1741 if (slot + 1 > lu->lun[i].u.slot.maxslot) {
1742 lu->lun[i].u.slot.maxslot = slot + 1;
1743 }
1744
1745 flags = istgt_get_nmval(sp, buf, j, 1);
1746 file = istgt_get_nmval(sp, buf, j, 2);
1747 size = istgt_get_nmval(sp, buf, j, 3);
1748 if (flags == NULL || file == NULL || size == NULL) {
1749 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1750 goto error_return;
1751 }
1752 mflags = istgt_lu_parse_media_flags(flags);
1753 msize = istgt_lu_parse_media_size(file, size, &mflags);
1754 if (msize == 0) {
1755 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1756 goto error_return;
1757 }
1758 lu->lun[i].u.slot.flags[slot] = mflags;
1759 lu->lun[i].u.slot.file[slot] = xstrdup(file);
1760 lu->lun[i].u.slot.size[slot] = msize;
1761 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
1762 "Slot%d file=%s, size=%"PRIu64", flags=%x\n",
1763 slot,
1764 lu->lun[i].u.slot.file[slot],
1765 lu->lun[i].u.slot.size[slot],
1766 lu->lun[i].u.slot.flags[slot]);
1767 } else if (strncasecmp(val, "Option", 6) == 0) {
1768 key = istgt_get_nmval(sp, buf, j, 1);
1769 val = istgt_get_nmval(sp, buf, j, 2);
1770 if (key == NULL || val == NULL) {
1771 ISTGT_ERRLOG("LU%d: LUN%d: format error\n", lu->num, i);
1772 goto error_return;
1773 }
1774 if (strcasecmp(key, "Serial") == 0) {
1775 /* set LUN serial */
1776 if (strlen(val) == 0) {
1777 ISTGT_ERRLOG("LU%d: LUN%d: no serial\n",
1778 lu->num, i);
1779 goto error_return;
1780 }
1781 xfree(lu->lun[i].serial);
1782 lu->lun[i].serial = xstrdup(val);
1783 } else if (strcasecmp(key, "RPM") == 0) {
1784 rpm = (int)strtol(val, NULL, 10);
1785 if (rpm < 0) {
1786 rpm = 0;
1787 } else if (rpm > 0xfffe) {
1788 rpm = 0xfffe;
1789 }
1790 lu->lun[i].rotationrate = rpm;
1791 } else if (strcasecmp(key, "FormFactor") == 0) {
1792 formfactor = (int)strtol(val, NULL, 10);
1793 if (formfactor < 0) {
1794 formfactor = 0;
1795 } else if (formfactor > 0x0f) {
1796 formfactor = 0xf;
1797 }
1798 lu->lun[i].formfactor = formfactor;
1799 } else if (strcasecmp(key, "ReadCache") == 0) {
1800 if (strcasecmp(val, "Enable") == 0) {
1801 lu->lun[i].readcache = 1;
1802 } else if (strcasecmp(val, "Disable") == 0) {
1803 lu->lun[i].readcache = 0;
1804 } else {
1805 ISTGT_ERRLOG("LU%d: LUN%d: unknown val(%s)\n",
1806 lu->num, i, val);
1807 }
1808 } else if (strcasecmp(key, "WriteCache") == 0) {
1809 if (strcasecmp(val, "Enable") == 0) {
1810 lu->lun[i].writecache = 1;
1811 } else if (strcasecmp(val, "Disable") == 0) {
1812 lu->lun[i].writecache = 0;
1813 } else {
1814 ISTGT_ERRLOG("LU%d: LUN%d: unknown val(%s)\n",
1815 lu->num, i, val);
1816 }
1817 } else {
1818 ISTGT_WARNLOG("LU%d: LUN%d: unknown key(%s)\n",
1819 lu->num, i, key);
1820 continue;
1821 }
1822 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LUN%d Option %s => %s\n",
1823 i, key, val);
1824 continue;
1825 } else {
1826 ISTGT_ERRLOG("LU%d: unknown lun type\n", lu->num);
1827 goto error_return;
1828 }
1829 }
1830 if (lu->lun[i].type == ISTGT_LU_LUN_TYPE_SLOT) {
1831 if (lu->lun[i].u.slot.maxslot == 0) {
1832 ISTGT_ERRLOG("LU%d: no slot\n", lu->num);
1833 goto error_return;
1834 }
1835 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "maxslot=%d\n",
1836 lu->lun[i].u.slot.maxslot);
1837 }
1838 lu->maxlun = i + 1;
1839 }
1840 if (lu->maxlun == 0) {
1841 ISTGT_ERRLOG("LU%d: no LUN\n", lu->num);
1842 goto error_return;
1843 }
1844 if (lu->lun[0].type == ISTGT_LU_LUN_TYPE_NONE) {
1845 ISTGT_ERRLOG("LU%d: no LUN0\n", lu->num);
1846 goto error_return;
1847 }
1848
1849 /* set local values if any */
1850 rc = istgt_lu_set_local_settings(istgt, sp, lu);
1851 if (rc < 0) {
1852 ISTGT_ERRLOG("LU%d: local setting error\n", lu->num);
1853 goto error_return;
1854 }
1855
1856 /* tsih 0 is reserved */
1857 for (i = 0; i < MAX_LU_TSIH; i++) {
1858 lu->tsih[i].tag = 0;
1859 lu->tsih[i].tsih = 0;
1860 lu->tsih[i].initiator_port = NULL;
1861 }
1862 lu->maxtsih = 1;
1863 lu->last_tsih = 0;
1864
1865 MTX_LOCK(&istgt->mutex);
1866 istgt->nlogical_unit++;
1867 istgt->logical_unit[lu->num] = lu;
1868 MTX_UNLOCK(&istgt->mutex);
1869 return 0;
1870
1871 error_return:
1872 xfree(lu->name);
1873 xfree(lu->alias);
1874 xfree(lu->inq_vendor);
1875 xfree(lu->inq_product);
1876 xfree(lu->inq_revision);
1877 for (i = 0; i < MAX_LU_LUN; i++) {
1878 switch (lu->lun[i].type) {
1879 case ISTGT_LU_LUN_TYPE_DEVICE:
1880 xfree(lu->lun[i].u.device.file);
1881 break;
1882 case ISTGT_LU_LUN_TYPE_STORAGE:
1883 xfree(lu->lun[i].u.storage.file);
1884 break;
1885 case ISTGT_LU_LUN_TYPE_REMOVABLE:
1886 xfree(lu->lun[i].u.removable.file);
1887 break;
1888 case ISTGT_LU_LUN_TYPE_SLOT:
1889 for (j = 0; j < lu->lun[i].u.slot.maxslot; j++) {
1890 xfree(lu->lun[i].u.slot.file[j]);
1891 }
1892 break;
1893 case ISTGT_LU_LUN_TYPE_NONE:
1894 default:
1895 break;
1896 }
1897 }
1898 for (i = 0; i < MAX_LU_TSIH; i++) {
1899 xfree(lu->tsih[i].initiator_port);
1900 }
1901 for (i = 0; i < lu->maxmap; i++) {
1902 /* nothing */
1903 }
1904
1905 xfree(lu);
1906 return -1;
1907 }
1908
1909 static int
1910 istgt_lu_del_unit(ISTGT_Ptr istgt, ISTGT_LU_Ptr lu)
1911 {
1912 int i, j;
1913
1914 if (lu ==NULL)
1915 return 0;
1916 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "del unit %d\n", lu->num);
1917
1918 MTX_LOCK(&istgt->mutex);
1919 istgt->nlogical_unit--;
1920 istgt->logical_unit[lu->num] = NULL;
1921 MTX_UNLOCK(&istgt->mutex);
1922
1923 xfree(lu->name);
1924 xfree(lu->alias);
1925 xfree(lu->inq_vendor);
1926 xfree(lu->inq_product);
1927 xfree(lu->inq_revision);
1928 xfree(lu->inq_serial);
1929 for (i = 0; i < MAX_LU_LUN; i++) {
1930 xfree(lu->lun[i].serial);
1931 switch (lu->lun[i].type) {
1932 case ISTGT_LU_LUN_TYPE_DEVICE:
1933 xfree(lu->lun[i].u.device.file);
1934 break;
1935 case ISTGT_LU_LUN_TYPE_STORAGE:
1936 xfree(lu->lun[i].u.storage.file);
1937 break;
1938 case ISTGT_LU_LUN_TYPE_REMOVABLE:
1939 xfree(lu->lun[i].u.removable.file);
1940 break;
1941 case ISTGT_LU_LUN_TYPE_SLOT:
1942 for (j = 0; j < lu->lun[i].u.slot.maxslot; j++) {
1943 xfree(lu->lun[i].u.slot.file[j]);
1944 }
1945 break;
1946 case ISTGT_LU_LUN_TYPE_NONE:
1947 default:
1948 break;
1949 }
1950 }
1951 for (i = 0; i < MAX_LU_TSIH; i++) {
1952 xfree(lu->tsih[i].initiator_port);
1953 }
1954 for (i = 0; i < lu->maxmap; i++) {
1955 /* nothing */
1956 }
1957
1958 return 0;
1959 }
1960
1961 static void *luworker(void *arg);
1962
1963 int
•1964 istgt_lu_init(ISTGT_Ptr istgt, int ignore_dup)
1965 {
1966 ISTGT_LU_Ptr lu;
1967 CF_SECTION *sp;
1968 int rc;
1969 int i;
•1970 int lun_map[MAX_LOGICAL_UNIT];
1971
1972 memset(lun_map, 0, sizeof(int) * MAX_LOGICAL_UNIT);
1973
1974 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_init\n");
1975 sp = istgt_find_cf_section(istgt->config, "Global");
1976 if (sp == NULL) {
1977 ISTGT_ERRLOG("find_cf_section failed()\n");
1978 return -1;
1979 }
1980
1981 sp = istgt->config->section;
1982 while (sp != NULL) {
1983 if (sp->type == ST_LOGICAL_UNIT) {
1984 if (sp->num == 0) {
1985 ISTGT_ERRLOG("Unit 0 is invalid\n");
1986 return -1;
1987 }
1988 if (sp->num > ISTGT_LU_TAG_MAX) {
1989 ISTGT_ERRLOG("tag %d is invalid\n", sp->num);
1990 return -1;
1991 }
•1992 rc = istgt_lu_add_unit(istgt, sp, ignore_dup);
1993 if (rc < 0) {
1994 ISTGT_ERRLOG("lu_add_unit() failed\n");
1995 return -1;
1996 }
•1997 lun_map[sp->num] = 1; /* in new LUN list */
1998 }
1999 sp = sp->next;
2000 }
2001
2002 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2003 lu = istgt->logical_unit[i];
2004 if (lu == NULL)
2005 continue;
•2006 /* tag to be removed LUNs */
2007 if (lun_map[i] == 0) {
2008 istgt->logical_unit[i]->to_add = 0;
2009 istgt->logical_unit[i]->to_remove = 1;
2010 continue;
2011 }
2012 /* only process newly added LUNs here beyond in loop */
2013 if (ignore_dup &&
2014 (istgt->logical_unit[i]->to_add == 1 &&
2015 istgt->logical_unit[i]->to_remove == 0)) {
2016 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "adding unit %d\n", i);
2017 } else if (ignore_dup &&
2018 !(istgt->logical_unit[i]->to_add == 1 &&
2019 istgt->logical_unit[i]->to_remove == 0)) {
2020 continue;
2021 }
2022
2023 rc = pthread_mutex_init(&lu->mutex, NULL);
2024 if (rc != 0) {
2025 ISTGT_ERRLOG("LU%d: mutex_init() failed\n", lu->num);
2026 return -1;
2027 }
2028 rc = pthread_mutex_init(&lu->state_mutex, NULL);
2029 if (rc != 0) {
2030 ISTGT_ERRLOG("LU%d: mutex_init() failed\n", lu->num);
2031 return -1;
2032 }
2033 rc = pthread_mutex_init(&lu->queue_mutex, NULL);
2034 if (rc != 0) {
2035 ISTGT_ERRLOG("LU%d: mutex_init() failed\n", lu->num);
2036 return -1;
2037 }
2038 rc = pthread_cond_init(&lu->queue_cond, NULL);
2039 if (rc != 0) {
2040 ISTGT_ERRLOG("LU%d: cond_init() failed\n", lu->num);
2041 return -1;
2042 }
2043
2044 switch (lu->type) {
2045 case ISTGT_LU_TYPE_PASS:
2046 rc = istgt_lu_pass_init(istgt, lu);
2047 if (rc < 0) {
2048 ISTGT_ERRLOG("LU%d: lu_pass_init() failed\n", lu->num);
2049 return -1;
2050 }
2051 break;
2052
2053 case ISTGT_LU_TYPE_DISK:
2054 rc = istgt_lu_disk_init(istgt, lu);
2055 if (rc < 0) {
2056 ISTGT_ERRLOG("LU%d: lu_disk_init() failed\n", lu->num);
2057 return -1;
2058 }
2059 break;
2060
2061 case ISTGT_LU_TYPE_DVD:
2062 rc = istgt_lu_dvd_init(istgt, lu);
2063 if (rc < 0) {
2064 ISTGT_ERRLOG("LU%d: lu_dvd_init() failed\n", lu->num);
2065 return -1;
2066 }
2067 break;
2068
2069 case ISTGT_LU_TYPE_TAPE:
2070 rc = istgt_lu_tape_init(istgt, lu);
2071 if (rc < 0) {
2072 ISTGT_ERRLOG("LU%d: lu_tape_init() failed\n", lu->num);
2073 return -1;
2074 }
2075 break;
2076
2077 case ISTGT_LU_TYPE_NONE:
2078 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2079 break;
2080 default:
2081 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2082 return -1;
2083 }
2084
2085 istgt_lu_set_state(lu, ISTGT_STATE_INITIALIZED);
2086 }
2087
2088 return 0;
2089 }
2090
2091 int
2092 istgt_lu_set_all_state(ISTGT_Ptr istgt, ISTGT_STATE state)
2093 {
2094 ISTGT_LU_Ptr lu;
2095 int i;
2096
2097 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2098 lu = istgt->logical_unit[i];
2099 if (lu == NULL)
2100 continue;
2101
2102 istgt_lu_set_state(lu, state);
2103 }
2104
2105 return 0;
2106 }
2107
2108 int
•2109 istgt_lu_create_threads(ISTGT_Ptr istgt, int new_only)
2110 {
2111 #ifdef HAVE_PTHREAD_SET_NAME_NP
2112 char buf[MAX_TMPBUF];
2113 #endif
2114 ISTGT_LU_Ptr lu;
2115 int rc;
2116 int i;
2117
2118 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_create_threads\n");
2119
2120 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2121 lu = istgt->logical_unit[i];
2122 if (lu == NULL)
2123 continue;
•2124 if (new_only && !lu->to_add) {
2125 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "skip existing LUN %d\n", i);
2126 continue;
2127 }
2128
2129 if (lu->queue_depth != 0) {
2130 /* create LU thread */
2131 #ifdef ISTGT_STACKSIZE
2132 rc = pthread_create(&lu->thread, &istgt->attr, &luworker, (void *)lu);
2133 #else
2134 rc = pthread_create(&lu->thread, NULL, &luworker, (void *)lu);
2135 #endif
2136 if (rc != 0) {
2137 ISTGT_ERRLOG("pthread_create() failed\n");
2138 return -1;
2139 }
2140 #if 0
2141 rc = pthread_detach(lu->thread);
2142 if (rc != 0) {
2143 ISTGT_ERRLOG("pthread_detach() failed\n");
2144 return -1;
2145 }
2146 #endif
2147 #ifdef HAVE_PTHREAD_SET_NAME_NP
2148 snprintf(buf, sizeof buf, "luthread #%d", i);
2149 pthread_set_name_np(lu->thread, buf);
2150 #endif
2151 }
2152 }
2153
2154 return 0;
2155 }
2156
2157 int
•2158 istgt_lu_shutdown(ISTGT_Ptr istgt, int removed_only)
2159 {
2160 ISTGT_LU_Ptr lu;
2161 int rc;
2162 int i;
2163
•2164 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_shutdown (removed_only = %d\n", removed_only);
2165
2166 for (i = 0; i < MAX_LOGICAL_UNIT; i++) {
2167 lu = istgt->logical_unit[i];
2168 if (lu == NULL)
2169 continue;
•2170 if (removed_only && !lu->to_remove) {
2171 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "skip existing LUNs %d\n", i);
2172 continue;
2173 }
2174 istgt_lu_set_state(lu, ISTGT_STATE_SHUTDOWN);
2175
2176 switch (lu->type) {
2177 case ISTGT_LU_TYPE_PASS:
2178 rc = istgt_lu_pass_shutdown(istgt, lu);
2179 if (rc < 0) {
2180 ISTGT_ERRLOG("LU%d: lu_pass_shutdown() failed\n", lu->num);
2181 /* ignore error */
2182 }
2183 break;
2184
2185 case ISTGT_LU_TYPE_DISK:
2186 rc = istgt_lu_disk_shutdown(istgt, lu);
2187 if (rc < 0) {
2188 ISTGT_ERRLOG("LU%d: lu_disk_shutdown() failed\n", lu->num);
2189 /* ignore error */
2190 }
2191 break;
2192
2193 case ISTGT_LU_TYPE_DVD:
2194 rc = istgt_lu_dvd_shutdown(istgt, lu);
2195 if (rc < 0) {
2196 ISTGT_ERRLOG("LU%d: lu_dvd_shutdown() failed\n", lu->num);
2197 /* ignore error */
2198 }
2199 break;
2200
2201 case ISTGT_LU_TYPE_TAPE:
2202 rc = istgt_lu_tape_shutdown(istgt, lu);
2203 if (rc < 0) {
2204 ISTGT_ERRLOG("LU%d: lu_tape_shutdown() failed\n", lu->num);
2205 /* ignore error */
2206 }
2207 break;
2208
2209 case ISTGT_LU_TYPE_NONE:
2210 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2211 break;
2212 default:
2213 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2214 return -1;
2215 }
2216
2217 rc = istgt_lu_del_unit(istgt, lu);
2218 if (rc < 0) {
2219 ISTGT_ERRLOG("LU%d: lu_del_unit() failed\n", lu->num);
2220 /* ignore error */
2221 }
2222
2223 if (lu->queue_depth != 0) {
2224 rc = pthread_cond_broadcast(&lu->queue_cond);
2225 if (rc != 0) {
2226 ISTGT_ERRLOG("LU%d: cond_broadcast() failed\n", lu->num);
2227 }
2228 rc = pthread_join(lu->thread, NULL);
2229 if (rc != 0) {
2230 ISTGT_ERRLOG("LU%d: pthread_join() failed\n", lu->num);
2231 }
2232 }
2233 rc = pthread_cond_destroy(&lu->queue_cond);
2234 if (rc != 0) {
2235 ISTGT_ERRLOG("LU%d: cond_destroy() failed\n", lu->num);
2236 /* ignore error */
2237 }
2238 rc = pthread_mutex_destroy(&lu->queue_mutex);
2239 if (rc != 0) {
2240 ISTGT_ERRLOG("LU%d: mutex_destroy() failed\n", lu->num);
2241 /* ignore error */
2242 }
2243 rc = pthread_mutex_destroy(&lu->state_mutex);
2244 if (rc != 0) {
2245 ISTGT_ERRLOG("LU%d: mutex_destroy() failed\n", lu->num);
2246 /* ignore error */
2247 }
2248 rc = pthread_mutex_destroy(&lu->mutex);
2249 if (rc != 0) {
2250 ISTGT_ERRLOG("LU%d: mutex_destroy() failed\n", lu->num);
2251 /* ignore error */
2252 }
2253 xfree(lu);
2254 istgt->logical_unit[i] = NULL;
2255 }
2256
2257 return 0;
2258 }
2259
2260 int
2261 istgt_lu_islun2lun(uint64_t islun)
2262 {
2263 uint64_t fmt_lun;
2264 uint64_t method;
2265 int lun_i;
2266
2267 fmt_lun = islun;
2268 method = (fmt_lun >> 62) & 0x03U;
2269 fmt_lun = fmt_lun >> 48;
2270 if (method == 0x00U) {
2271 lun_i = (int) (fmt_lun & 0x00ffU);
2272 } else if (method == 0x01U) {
2273 lun_i = (int) (fmt_lun & 0x3fffU);
2274 } else {
2275 lun_i = 0xffffU;
2276 }
2277 return lun_i;
2278 }
2279
2280 uint64_t
2281 istgt_lu_lun2islun(int lun, int maxlun)
2282 {
2283 uint64_t fmt_lun;
2284 uint64_t method;
2285 uint64_t islun;
2286
2287 islun = (uint64_t) lun;
2288 if (maxlun <= 0x0100) {
2289 /* below 256 */
2290 method = 0x00U;
2291 fmt_lun = (method & 0x03U) << 62;
2292 fmt_lun |= (islun & 0x00ffU) << 48;
2293 } else if (maxlun <= 0x4000U) {
2294 /* below 16384 */
2295 method = 0x01U;
2296 fmt_lun = (method & 0x03U) << 62;
2297 fmt_lun |= (islun & 0x3fffU) << 48;
2298 } else {
2299 /* XXX */
2300 fmt_lun = ~((uint64_t) 0);
2301 }
2302 return fmt_lun;
2303 }
2304
2305 int
2306 istgt_lu_reset(ISTGT_LU_Ptr lu, uint64_t lun)
2307 {
2308 int lun_i;
2309 int rc;
2310
2311 if (lu == NULL)
2312 return -1;
2313
2314 lun_i = istgt_lu_islun2lun(lun);
2315
2316 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LU%d: Name=%s, LUN=%d\n",
2317 lu->num, lu->name, lun_i);
2318
2319 switch (lu->type) {
2320 case ISTGT_LU_TYPE_PASS:
2321 MTX_LOCK(&lu->mutex);
2322 rc = istgt_lu_pass_reset(lu, lun_i);
2323 MTX_UNLOCK(&lu->mutex);
2324 if (rc < 0) {
2325 ISTGT_ERRLOG("LU%d: lu_pass_reset() failed\n", lu->num);
2326 return -1;
2327 }
2328 break;
2329
2330 case ISTGT_LU_TYPE_DISK:
2331 MTX_LOCK(&lu->mutex);
2332 rc = istgt_lu_disk_reset(lu, lun_i);
2333 MTX_UNLOCK(&lu->mutex);
2334 if (rc < 0) {
2335 ISTGT_ERRLOG("LU%d: lu_disk_reset() failed\n", lu->num);
2336 return -1;
2337 }
2338 break;
2339
2340 case ISTGT_LU_TYPE_DVD:
2341 MTX_LOCK(&lu->mutex);
2342 rc = istgt_lu_dvd_reset(lu, lun_i);
2343 MTX_UNLOCK(&lu->mutex);
2344 if (rc < 0) {
2345 ISTGT_ERRLOG("LU%d: lu_dvd_reset() failed\n", lu->num);
2346 return -1;
2347 }
2348 break;
2349
2350 case ISTGT_LU_TYPE_TAPE:
2351 MTX_LOCK(&lu->mutex);
2352 rc = istgt_lu_tape_reset(lu, lun_i);
2353 MTX_UNLOCK(&lu->mutex);
2354 if (rc < 0) {
2355 ISTGT_ERRLOG("LU%d: lu_tape_reset() failed\n", lu->num);
2356 return -1;
2357 }
2358 break;
2359
2360 case ISTGT_LU_TYPE_NONE:
2361 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2362 break;
2363 default:
2364 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2365 return -1;
2366 }
2367
2368 return 0;
2369 }
2370
2371 int
2372 istgt_lu_execute(CONN_Ptr conn, ISTGT_LU_CMD_Ptr lu_cmd)
2373 {
2374 ISTGT_LU_Ptr lu;
2375 int rc;
2376
2377 if (lu_cmd == NULL)
2378 return -1;
2379 lu = lu_cmd->lu;
2380 if (lu == NULL)
2381 return -1;
2382
2383 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG,
2384 "LU%d: PG=0x%4.4x, Name=%s, LUN=0x%16.16"PRIx64"\n",
2385 lu->num, conn->portal.tag, lu->name, lu_cmd->lun);
2386
2387 if (lu->online == 0) {
2388 ISTGT_TRACELOG(ISTGT_TRACE_SCSI, "LU%d: offline\n", lu->num);
2389 /* LOGICAL UNIT NOT READY, CAUSE NOT REPORTABLE */
2390 lu_cmd->sense_data_len
2391 = istgt_lu_scsi_build_sense_data(lu_cmd->sense_data,
2392 ISTGT_SCSI_SENSE_NOT_READY,
2393 0x04, 0x00);
2394 lu_cmd->status = ISTGT_SCSI_STATUS_CHECK_CONDITION;
2395 return 0;
2396 }
2397
2398 rc = 0;
2399 switch (lu->type) {
2400 case ISTGT_LU_TYPE_PASS:
2401 MTX_LOCK(&lu->mutex);
2402 rc = istgt_lu_pass_execute(conn, lu_cmd);
2403 MTX_UNLOCK(&lu->mutex);
2404 if (rc < 0) {
2405 ISTGT_ERRLOG("LU%d: lu_pass_execute() failed\n",
2406 lu->num);
2407 return -1;
2408 }
2409 break;
2410
2411 case ISTGT_LU_TYPE_DISK:
2412 if (lu->queue_depth != 0) {
2413 //MTX_LOCK(&lu->queue_mutex);
2414 rc = istgt_lu_disk_queue(conn, lu_cmd);
2415 //MTX_UNLOCK(&lu->queue_mutex);
2416 if (rc < 0) {
2417 ISTGT_ERRLOG("LU%d: lu_disk_queue() failed\n",
2418 lu->num);
2419 return -1;
2420 }
2421 } else {
2422 MTX_LOCK(&lu->mutex);
2423 rc = istgt_lu_disk_execute(conn, lu_cmd);
2424 MTX_UNLOCK(&lu->mutex);
2425 if (rc < 0) {
2426 ISTGT_ERRLOG("LU%d: lu_disk_execute() failed\n",
2427 lu->num);
2428 return -1;
2429 }
2430 }
2431 break;
2432
2433 case ISTGT_LU_TYPE_DVD:
2434 MTX_LOCK(&lu->mutex);
2435 rc = istgt_lu_dvd_execute(conn, lu_cmd);
2436 MTX_UNLOCK(&lu->mutex);
2437 if (rc < 0) {
2438 ISTGT_ERRLOG("LU%d: lu_dvd_execute() failed\n",
2439 lu->num);
2440 return -1;
2441 }
2442 break;
2443
2444 case ISTGT_LU_TYPE_TAPE:
2445 MTX_LOCK(&lu->mutex);
2446 rc = istgt_lu_tape_execute(conn, lu_cmd);
2447 MTX_UNLOCK(&lu->mutex);
2448 if (rc < 0) {
2449 ISTGT_ERRLOG("LU%d: lu_tape_execute() failed\n",
2450 lu->num);
2451 return -1;
2452 }
2453 break;
2454
2455 case ISTGT_LU_TYPE_NONE:
2456 //ISTGT_ERRLOG("LU%d: dummy type\n", lu->num);
2457 break;
2458 default:
2459 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2460 return -1;
2461 }
2462
2463 return rc;
2464 }
2465
2466 int
2467 istgt_lu_create_task(CONN_Ptr conn, ISTGT_LU_CMD_Ptr lu_cmd, ISTGT_LU_TASK_Ptr lu_task, int lun)
2468 {
2469 ISCSI_PDU_Ptr dst_pdu, src_pdu;
2470 uint8_t *cdb;
2471 int alloc_len;
2472 #if 0
2473 int rc;
2474 #endif
2475
2476 if (lu_task == NULL)
2477 return -1;
2478
2479 lu_task->type = ISTGT_LU_TASK_RESPONSE;
2480 lu_task->conn = conn;
2481 strncpy(lu_task->initiator_name, conn->initiator_name,
2482 sizeof lu_task->initiator_name);
2483 strncpy(lu_task->initiator_port, conn->initiator_port,
2484 sizeof lu_task->initiator_port);
2485
2486 lu_task->lun = (int) lun;
2487 lu_task->use_cond = 0;
2488 lu_task->dup_iobuf = 0;
2489 lu_task->iobuf = NULL;
2490 lu_task->data = NULL;
2491 lu_task->sense_data = NULL;
2492 lu_task->create_time = 0;
2493 lu_task->condwait = 0;
2494 lu_task->offset = 0;
2495 lu_task->req_execute = 0;
2496 lu_task->req_transfer_out = 0;
2497 lu_task->error = 0;
2498 lu_task->abort = 0;
2499 lu_task->execute = 0;
2500 lu_task->complete = 0;
2501 lu_task->lock = 0;
2502
2503 #if 0
2504 rc = pthread_mutex_init(&lu_task->trans_mutex, NULL);
2505 if (rc != 0) {
2506 ISTGT_ERRLOG("mutex_init() failed\n");
2507 return -1;
2508 }
2509 rc = pthread_cond_init(&lu_task->trans_cond, NULL);
2510 if (rc != 0) {
2511 ISTGT_ERRLOG("cond_init() failed\n");
2512 return -1;
2513 }
2514 rc = pthread_cond_init(&lu_task->exec_cond, NULL);
2515 if (rc != 0) {
2516 ISTGT_ERRLOG("cond_init() failed\n");
2517 return -1;
2518 }
2519 #endif
2520
2521 lu_task->lu_cmd.pdu = xmalloc(sizeof *lu_task->lu_cmd.pdu);
2522 memset(lu_task->lu_cmd.pdu, 0, sizeof *lu_task->lu_cmd.pdu);
2523
2524 /* copy PDU */
2525 dst_pdu = lu_task->lu_cmd.pdu;
2526 src_pdu = lu_cmd->pdu;
2527 memcpy(&dst_pdu->bhs, &src_pdu->bhs, ISCSI_BHS_LEN);
2528 dst_pdu->ahs = src_pdu->ahs;
2529 memcpy(dst_pdu->header_digest, src_pdu->header_digest, ISCSI_DIGEST_LEN);
2530 if (src_pdu->data == src_pdu->shortdata) {
2531 memcpy(dst_pdu->shortdata, src_pdu->shortdata,
2532 sizeof src_pdu->shortdata);
2533 dst_pdu->data = dst_pdu->shortdata;
2534 } else {
2535 dst_pdu->data = src_pdu->data;
2536 }
2537 memcpy(dst_pdu->data_digest, src_pdu->data_digest, ISCSI_DIGEST_LEN);
2538 dst_pdu->total_ahs_len = src_pdu->total_ahs_len;
2539 dst_pdu->data_segment_len = src_pdu->data_segment_len;
2540 dst_pdu->copy_pdu = 0;
2541 src_pdu->copy_pdu = 1;
2542
2543 /* copy other lu_cmd */
2544 lu_task->lu_cmd.lu = lu_cmd->lu;
2545 cdb = ((uint8_t *) &lu_task->lu_cmd.pdu->bhs) + 32;
2546 lu_task->lu_cmd.I_bit = lu_cmd->I_bit;
2547 lu_task->lu_cmd.F_bit = lu_cmd->F_bit;
2548 lu_task->lu_cmd.R_bit = lu_cmd->R_bit;
2549 lu_task->lu_cmd.W_bit = lu_cmd->W_bit;
2550 lu_task->lu_cmd.Attr_bit = lu_cmd->Attr_bit;
2551 lu_task->lu_cmd.lun = lu_cmd->lun;
2552 lu_task->lu_cmd.task_tag = lu_cmd->task_tag;
2553 lu_task->lu_cmd.transfer_len = lu_cmd->transfer_len;
2554 //lu_task->lu_cmd.cdb = lu_cmd->cdb;
2555 lu_task->lu_cmd.cdb = cdb;
2556 lu_task->lu_cmd.CmdSN = lu_cmd->CmdSN;
2557
2558 //lu_task->lu_cmd.iobuf = lu_cmd->iobuf;
2559 lu_task->lu_cmd.iobuf = NULL;
2560 lu_task->lu_cmd.iobufsize = lu_cmd->iobufsize;
2561 lu_task->lu_cmd.data = lu_cmd->data;
2562 lu_task->lu_cmd.data_len = lu_cmd->data_len;
2563 lu_task->lu_cmd.alloc_len = lu_cmd->alloc_len;
2564
2565 lu_task->lu_cmd.status = lu_cmd->status;
2566 lu_task->lu_cmd.sense_data = lu_cmd->sense_data;
2567 lu_task->lu_cmd.sense_data_len = lu_cmd->sense_data_len;
2568 lu_task->lu_cmd.sense_alloc_len = lu_cmd->sense_alloc_len;
2569
2570 /* pre allocate buffer */
2571 lu_task->lu_cmd.iobufsize = lu_cmd->transfer_len + 65536;
2572 #if 0
2573 lu_task->data = xmalloc(lu_cmd->alloc_len);
2574 lu_task->sense_data = xmalloc(lu_cmd->sense_alloc_len);
2575 lu_task->iobuf = xmalloc(lu_task->lu_cmd.iobufsize);
2576 #else
2577 alloc_len = ISCSI_ALIGN(lu_cmd->alloc_len);
2578 alloc_len += ISCSI_ALIGN(lu_cmd->sense_alloc_len);
2579 alloc_len += ISCSI_ALIGN(lu_task->lu_cmd.iobufsize);
2580 lu_task->data = xmalloc(alloc_len);
2581 lu_task->sense_data = lu_task->data + ISCSI_ALIGN(lu_cmd->alloc_len);
2582 lu_task->iobuf = lu_task->sense_data + ISCSI_ALIGN(lu_cmd->sense_alloc_len);
2583 #endif
2584
2585 /* creation time */
2586 lu_task->create_time = time(NULL);
2587 /* wait time */
2588 lu_task->condwait = conn->timeout * 1000;
2589 if (lu_task->condwait < ISTGT_CONDWAIT_MIN) {
2590 lu_task->condwait = ISTGT_CONDWAIT_MIN;
2591 }
2592
2593 return 0;
2594 }
2595
2596 int
2597 istgt_lu_destroy_task(ISTGT_LU_TASK_Ptr lu_task)
2598 {
2599 int rc;
2600
2601 if (lu_task == NULL)
2602 return -1;
2603
2604 if (lu_task->use_cond != 0) {
2605 rc = pthread_mutex_destroy(&lu_task->trans_mutex);
2606 if (rc != 0) {
2607 ISTGT_ERRLOG("mutex_destroy() failed\n");
2608 return -1;
2609 }
2610 rc = pthread_cond_destroy(&lu_task->trans_cond);
2611 if (rc != 0) {
2612 ISTGT_ERRLOG("cond_destroy() failed\n");
2613 return -1;
2614 }
2615 rc = pthread_cond_destroy(&lu_task->exec_cond);
2616 if (rc != 0) {
2617 ISTGT_ERRLOG("cond_destroy() failed\n");
2618 return -1;
2619 }
2620 }
2621 if (lu_task->lu_cmd.pdu != NULL) {
2622 if (lu_task->lu_cmd.pdu->copy_pdu == 0) {
2623 xfree(lu_task->lu_cmd.pdu->ahs);
2624 if (lu_task->lu_cmd.pdu->data
2625 != lu_task->lu_cmd.pdu->shortdata) {
2626 xfree(lu_task->lu_cmd.pdu->data);
2627 }
2628 }
2629 xfree(lu_task->lu_cmd.pdu);
2630 }
2631 #if 0
2632 if (lu_task->dup_iobuf == 0) {
2633 xfree(lu_task->iobuf);
2634 }
2635 xfree(lu_task->data);
2636 xfree(lu_task->sense_data);
2637 #else
2638 xfree(lu_task->data);
2639 #endif
2640 xfree(lu_task);
2641 return 0;
2642 }
2643
2644 int
2645 istgt_lu_clear_task_IT(CONN_Ptr conn, ISTGT_LU_Ptr lu)
2646 {
2647 int rc;
2648
2649 if (lu == NULL)
2650 return -1;
2651
2652 if (lu->queue_depth == 0)
2653 return 0;
2654
2655 rc = 0;
2656 switch (lu->type) {
2657 case ISTGT_LU_TYPE_DISK:
2658 MTX_LOCK(&lu->mutex);
2659 rc = istgt_lu_disk_queue_clear_IT(conn, lu);
2660 MTX_UNLOCK(&lu->mutex);
2661 if (rc < 0) {
2662 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_IT() failed\n", lu->num);
2663 return -1;
2664 }
2665 break;
2666
2667 case ISTGT_LU_TYPE_DVD:
2668 case ISTGT_LU_TYPE_TAPE:
2669 case ISTGT_LU_TYPE_NONE:
2670 default:
2671 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2672 return -1;
2673 }
2674
2675 return 0;
2676 }
2677
2678 int
2679 istgt_lu_clear_task_ITL(CONN_Ptr conn, ISTGT_LU_Ptr lu, uint64_t lun)
2680 {
2681 int lun_i;
2682 int rc;
2683
2684 if (lu == NULL)
2685 return -1;
2686
2687 if (lu->queue_depth == 0)
2688 return 0;
2689
2690 lun_i = istgt_lu_islun2lun(lun);
2691
2692 rc = 0;
2693 switch (lu->type) {
2694 case ISTGT_LU_TYPE_DISK:
2695 MTX_LOCK(&lu->mutex);
2696 rc = istgt_lu_disk_queue_clear_ITL(conn, lu, lun_i);
2697 MTX_UNLOCK(&lu->mutex);
2698 if (rc < 0) {
2699 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_ITL() failed\n", lu->num);
2700 return -1;
2701 }
2702 break;
2703
2704 case ISTGT_LU_TYPE_DVD:
2705 case ISTGT_LU_TYPE_TAPE:
2706 case ISTGT_LU_TYPE_NONE:
2707 default:
2708 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2709 return -1;
2710 }
2711
2712 return 0;
2713 }
2714
2715 int
2716 istgt_lu_clear_task_ITLQ(CONN_Ptr conn, ISTGT_LU_Ptr lu, uint64_t lun, uint32_t CmdSN)
2717 {
2718 int lun_i;
2719 int rc;
2720
2721 if (lu == NULL)
2722 return -1;
2723
2724 if (lu->queue_depth == 0)
2725 return 0;
2726
2727 lun_i = istgt_lu_islun2lun(lun);
2728
2729 rc = 0;
2730 switch (lu->type) {
2731 case ISTGT_LU_TYPE_DISK:
2732 MTX_LOCK(&lu->mutex);
2733 rc = istgt_lu_disk_queue_clear_ITLQ(conn, lu, lun_i, CmdSN);
2734 MTX_UNLOCK(&lu->mutex);
2735 if (rc < 0) {
2736 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_ITLQ() failed\n", lu->num);
2737 return -1;
2738 }
2739 break;
2740
2741 case ISTGT_LU_TYPE_DVD:
2742 case ISTGT_LU_TYPE_TAPE:
2743 case ISTGT_LU_TYPE_NONE:
2744 default:
2745 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2746 return -1;
2747 }
2748
2749 return 0;
2750 }
2751
2752 int
2753 istgt_lu_clear_all_task(ISTGT_LU_Ptr lu, uint64_t lun)
2754 {
2755 int rc;
2756
2757 if (lu == NULL)
2758 return -1;
2759
2760 if (lu->queue_depth == 0)
2761 return 0;
2762
2763 rc = 0;
2764 switch (lu->type) {
2765 case ISTGT_LU_TYPE_DISK:
2766 MTX_LOCK(&lu->mutex);
2767 rc = istgt_lu_disk_queue_clear_all(lu, lun);
2768 MTX_UNLOCK(&lu->mutex);
2769 if (rc < 0) {
2770 ISTGT_ERRLOG("LU%d: lu_disk_queue_clear_all() failed\n", lu->num);
2771 return -1;
2772 }
2773 break;
2774
2775 case ISTGT_LU_TYPE_DVD:
2776 case ISTGT_LU_TYPE_TAPE:
2777 case ISTGT_LU_TYPE_NONE:
2778 default:
2779 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2780 return -1;
2781 }
2782
2783 return 0;
2784 }
2785
2786 static void *
2787 luworker(void *arg)
2788 {
2789 ISTGT_LU_Ptr lu = (ISTGT_LU_Ptr) arg;
2790 sigset_t signew, sigold;
2791 #if 0
2792 struct timespec abstime;
2793 time_t now;
2794 int timeout = 20; /* XXX */
2795 #endif
2796 int qcnt;
2797 int lun;
2798 int rc;
2799
2800 sigemptyset(&signew);
2801 sigemptyset(&sigold);
2802 sigaddset(&signew, ISTGT_SIGWAKEUP);
2803 pthread_sigmask(SIG_UNBLOCK, &signew, &sigold);
2804
2805 while (istgt_get_state(lu->istgt) != ISTGT_STATE_RUNNING) {
2806 if (istgt_get_state(lu->istgt) == ISTGT_STATE_EXITING
2807 || istgt_get_state(lu->istgt) == ISTGT_STATE_SHUTDOWN) {
2808 ISTGT_ERRLOG("exit before running\n");
2809 return NULL;
2810 }
2811 //ISTGT_WARNLOG("Wait for running\n");
2812 sleep(1);
2813 continue;
2814 }
2815
2816 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LU%d loop start\n", lu->num);
2817 lun = 0;
2818 qcnt = 0;
2819 #if 0
2820 memset(&abstime, 0, sizeof abstime);
2821 #endif
2822 while (1) {
2823 switch (lu->type) {
2824 case ISTGT_LU_TYPE_DISK:
2825 while (1) {
2826 if (istgt_lu_get_state(lu) != ISTGT_STATE_RUNNING) {
2827 goto loop_exit;
2828 }
2829 MTX_LOCK(&lu->queue_mutex);
2830 qcnt = istgt_lu_disk_queue_count(lu, &lun);
2831 if (qcnt == 0) {
2832 #if 0
2833 now = time(NULL);
2834 abstime.tv_sec = now + timeout;
2835 abstime.tv_nsec = 0;
2836 rc = pthread_cond_timedwait(&lu->queue_cond,
2837 &lu->queue_mutex, &abstime);
2838 if (rc == ETIMEDOUT) {
2839 /* nothing */
2840 }
2841 #else
2842 pthread_cond_wait(&lu->queue_cond,
2843 &lu->queue_mutex);
2844 #endif
2845 qcnt = istgt_lu_disk_queue_count(lu, &lun);
2846 if (qcnt == 0) {
2847 MTX_UNLOCK(&lu->queue_mutex);
2848 continue;
2849 }
2850 }
2851 MTX_UNLOCK(&lu->queue_mutex);
2852 break;
2853 }
2854 if (qcnt < 0) {
2855 ISTGT_ERRLOG("LU%d: lu_disk_queue_count() failed\n",
2856 lu->num);
2857 break;
2858 }
2859 rc = istgt_lu_disk_queue_start(lu, lun);
2860 lun++;
2861 if (rc == -2) {
2862 ISTGT_WARNLOG("LU%d: lu_disk_queue_start() aborted\n",
2863 lu->num);
2864 break;
2865 }
2866 if (rc < 0) {
2867 ISTGT_ERRLOG("LU%d: lu_disk_queue_start() failed\n",
2868 lu->num);
2869 break;
2870 }
2871 break;
2872
2873 case ISTGT_LU_TYPE_DVD:
2874 case ISTGT_LU_TYPE_TAPE:
2875 case ISTGT_LU_TYPE_NONE:
2876 default:
2877 ISTGT_ERRLOG("LU%d: unsupported type\n", lu->num);
2878 return NULL;
2879 }
2880
2881 #if 0
2882 /* still running? */
2883 if (qcnt <= 1) {
2884 if (istgt_lu_get_state(lu) != ISTGT_STATE_RUNNING) {
2885 goto loop_exit;
2886 }
2887 }
2888 #endif
2889 }
2890 loop_exit:
2891 ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "LU%d loop ended\n", lu->num);
2892
2893 return NULL;
2894 }