kernel.c (273464) | kernel.c (278037) |
---|---|
1/*- 2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp. 3 * Copyright (c) 1997-2007 Kenneth D. Merry 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. --- 21 unchanged lines hidden (view full) --- 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 */ 36 37#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp. 3 * Copyright (c) 1997-2007 Kenneth D. Merry 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. --- 21 unchanged lines hidden (view full) --- 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 */ 36 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: head/usr.sbin/ctld/kernel.c 273464 2014-10-22 09:17:17Z trasz $"); | 38__FBSDID("$FreeBSD: head/usr.sbin/ctld/kernel.c 278037 2015-02-01 21:50:28Z mav $"); |
39 40#include <sys/ioctl.h> 41#include <sys/types.h> 42#include <sys/stat.h> 43#include <sys/param.h> 44#include <sys/linker.h> 45#include <sys/queue.h> 46#include <sys/callout.h> --- 62 unchanged lines hidden (view full) --- 109 */ 110struct cctl_lun { 111 uint64_t lun_id; 112 char *backend_type; 113 uint64_t size_blocks; 114 uint32_t blocksize; 115 char *serial_number; 116 char *device_id; | 39 40#include <sys/ioctl.h> 41#include <sys/types.h> 42#include <sys/stat.h> 43#include <sys/param.h> 44#include <sys/linker.h> 45#include <sys/queue.h> 46#include <sys/callout.h> --- 62 unchanged lines hidden (view full) --- 109 */ 110struct cctl_lun { 111 uint64_t lun_id; 112 char *backend_type; 113 uint64_t size_blocks; 114 uint32_t blocksize; 115 char *serial_number; 116 char *device_id; |
117 char *cfiscsi_target; 118 int cfiscsi_lun; | 117 char *ctld_name; |
119 STAILQ_HEAD(,cctl_lun_nv) attr_list; 120 STAILQ_ENTRY(cctl_lun) links; 121}; 122 123struct cctl_port { 124 uint32_t port_id; 125 int cfiscsi_status; 126 char *cfiscsi_target; --- 97 unchanged lines hidden (view full) --- 224 } else if (strcmp(name, "blocksize") == 0) { 225 cur_lun->blocksize = strtoul(str, NULL, 0); 226 } else if (strcmp(name, "serial_number") == 0) { 227 cur_lun->serial_number = str; 228 str = NULL; 229 } else if (strcmp(name, "device_id") == 0) { 230 cur_lun->device_id = str; 231 str = NULL; | 118 STAILQ_HEAD(,cctl_lun_nv) attr_list; 119 STAILQ_ENTRY(cctl_lun) links; 120}; 121 122struct cctl_port { 123 uint32_t port_id; 124 int cfiscsi_status; 125 char *cfiscsi_target; --- 97 unchanged lines hidden (view full) --- 223 } else if (strcmp(name, "blocksize") == 0) { 224 cur_lun->blocksize = strtoul(str, NULL, 0); 225 } else if (strcmp(name, "serial_number") == 0) { 226 cur_lun->serial_number = str; 227 str = NULL; 228 } else if (strcmp(name, "device_id") == 0) { 229 cur_lun->device_id = str; 230 str = NULL; |
232 } else if (strcmp(name, "cfiscsi_target") == 0) { 233 cur_lun->cfiscsi_target = str; | 231 } else if (strcmp(name, "ctld_name") == 0) { 232 cur_lun->ctld_name = str; |
234 str = NULL; | 233 str = NULL; |
235 } else if (strcmp(name, "cfiscsi_lun") == 0) { 236 cur_lun->cfiscsi_lun = strtoul(str, NULL, 0); | |
237 } else if (strcmp(name, "lun") == 0) { 238 devlist->cur_lun = NULL; 239 } else if (strcmp(name, "ctllunlist") == 0) { 240 /* Nothing. */ 241 } else { 242 struct cctl_lun_nv *nv; 243 244 nv = calloc(1, sizeof(*nv)); --- 272 unchanged lines hidden (view full) --- 517 continue; 518 } 519 } 520 } 521 522 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 523 struct cctl_lun_nv *nv; 524 | 234 } else if (strcmp(name, "lun") == 0) { 235 devlist->cur_lun = NULL; 236 } else if (strcmp(name, "ctllunlist") == 0) { 237 /* Nothing. */ 238 } else { 239 struct cctl_lun_nv *nv; 240 241 nv = calloc(1, sizeof(*nv)); --- 272 unchanged lines hidden (view full) --- 514 continue; 515 } 516 } 517 } 518 519 STAILQ_FOREACH(lun, &devlist.lun_list, links) { 520 struct cctl_lun_nv *nv; 521 |
525 if (lun->cfiscsi_target == NULL) { | 522 if (lun->ctld_name == NULL) { |
526 log_debugx("CTL lun %ju wasn't managed by ctld; " 527 "ignoring", (uintmax_t)lun->lun_id); 528 continue; 529 } 530 | 523 log_debugx("CTL lun %ju wasn't managed by ctld; " 524 "ignoring", (uintmax_t)lun->lun_id); 525 continue; 526 } 527 |
531 targ = target_find(conf, lun->cfiscsi_target); 532 if (targ == NULL) { 533#if 0 534 log_debugx("found new kernel target %s for CTL lun %ld", 535 lun->cfiscsi_target, lun->lun_id); 536#endif 537 targ = target_new(conf, lun->cfiscsi_target); 538 if (targ == NULL) { 539 log_warnx("target_new failed"); 540 continue; 541 } 542 } 543 544 cl = lun_find(targ, lun->cfiscsi_lun); | 528 cl = lun_find(conf, lun->ctld_name); |
545 if (cl != NULL) { | 529 if (cl != NULL) { |
546 log_warnx("found CTL lun %ju, backing lun %d, target " 547 "%s, also backed by CTL lun %d; ignoring", 548 (uintmax_t) lun->lun_id, cl->l_lun, 549 cl->l_target->t_name, cl->l_ctl_lun); | 530 log_warnx("found CTL lun %ju \"%s\", " 531 "also backed by CTL lun %d; ignoring", 532 (uintmax_t)lun->lun_id, lun->ctld_name, 533 cl->l_ctl_lun); |
550 continue; 551 } 552 | 534 continue; 535 } 536 |
553 log_debugx("found CTL lun %ju, backing lun %d, target %s", 554 (uintmax_t)lun->lun_id, lun->cfiscsi_lun, lun->cfiscsi_target); | 537 log_debugx("found CTL lun %ju \"%s\"", 538 (uintmax_t)lun->lun_id, lun->ctld_name); |
555 | 539 |
556 cl = lun_new(targ, lun->cfiscsi_lun); | 540 cl = lun_new(conf, lun->ctld_name); |
557 if (cl == NULL) { 558 log_warnx("lun_new failed"); 559 continue; 560 } 561 lun_set_backend(cl, lun->backend_type); 562 lun_set_blocksize(cl, lun->blocksize); 563 lun_set_device_id(cl, lun->device_id); 564 lun_set_serial(cl, lun->serial_number); --- 4 unchanged lines hidden (view full) --- 569 if (strcmp(nv->name, "file") == 0 || 570 strcmp(nv->name, "dev") == 0) { 571 lun_set_path(cl, nv->value); 572 continue; 573 } 574 lo = lun_option_new(cl, nv->name, nv->value); 575 if (lo == NULL) 576 log_warnx("unable to add CTL lun option %s " | 541 if (cl == NULL) { 542 log_warnx("lun_new failed"); 543 continue; 544 } 545 lun_set_backend(cl, lun->backend_type); 546 lun_set_blocksize(cl, lun->blocksize); 547 lun_set_device_id(cl, lun->device_id); 548 lun_set_serial(cl, lun->serial_number); --- 4 unchanged lines hidden (view full) --- 553 if (strcmp(nv->name, "file") == 0 || 554 strcmp(nv->name, "dev") == 0) { 555 lun_set_path(cl, nv->value); 556 continue; 557 } 558 lo = lun_option_new(cl, nv->name, nv->value); 559 if (lo == NULL) 560 log_warnx("unable to add CTL lun option %s " |
577 "for CTL lun %ju for lun %d, target %s", | 561 "for CTL lun %ju \"%s\"", |
578 nv->name, (uintmax_t) lun->lun_id, | 562 nv->name, (uintmax_t) lun->lun_id, |
579 cl->l_lun, cl->l_target->t_name); | 563 cl->l_name); |
580 } 581 } 582 583 return (conf); 584} 585 586static void 587str_arg(struct ctl_be_arg *arg, const char *name, const char *value) --- 6 unchanged lines hidden (view full) --- 594 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD; 595} 596 597int 598kernel_lun_add(struct lun *lun) 599{ 600 struct lun_option *lo; 601 struct ctl_lun_req req; | 564 } 565 } 566 567 return (conf); 568} 569 570static void 571str_arg(struct ctl_be_arg *arg, const char *name, const char *value) --- 6 unchanged lines hidden (view full) --- 578 arg->flags = CTL_BEARG_ASCII | CTL_BEARG_RD; 579} 580 581int 582kernel_lun_add(struct lun *lun) 583{ 584 struct lun_option *lo; 585 struct ctl_lun_req req; |
602 char *tmp; | |
603 int error, i, num_options; 604 605 bzero(&req, sizeof(req)); 606 607 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 608 req.reqtype = CTL_LUNREQ_CREATE; 609 610 req.reqdata.create.blocksize_bytes = lun->l_blocksize; --- 21 unchanged lines hidden (view full) --- 632 if (lo != NULL) { 633 lun_option_set(lo, lun->l_path); 634 } else { 635 lo = lun_option_new(lun, "file", lun->l_path); 636 assert(lo != NULL); 637 } 638 } 639 | 586 int error, i, num_options; 587 588 bzero(&req, sizeof(req)); 589 590 strlcpy(req.backend, lun->l_backend, sizeof(req.backend)); 591 req.reqtype = CTL_LUNREQ_CREATE; 592 593 req.reqdata.create.blocksize_bytes = lun->l_blocksize; --- 21 unchanged lines hidden (view full) --- 615 if (lo != NULL) { 616 lun_option_set(lo, lun->l_path); 617 } else { 618 lo = lun_option_new(lun, "file", lun->l_path); 619 assert(lo != NULL); 620 } 621 } 622 |
640 lo = lun_option_find(lun, "cfiscsi_target"); | 623 lo = lun_option_find(lun, "ctld_name"); |
641 if (lo != NULL) { | 624 if (lo != NULL) { |
642 lun_option_set(lo, lun->l_target->t_name); | 625 lun_option_set(lo, lun->l_name); |
643 } else { | 626 } else { |
644 lo = lun_option_new(lun, "cfiscsi_target", 645 lun->l_target->t_name); | 627 lo = lun_option_new(lun, "ctld_name", lun->l_name); |
646 assert(lo != NULL); 647 } 648 | 628 assert(lo != NULL); 629 } 630 |
649 asprintf(&tmp, "%d", lun->l_lun); 650 if (tmp == NULL) 651 log_errx(1, "asprintf"); 652 lo = lun_option_find(lun, "cfiscsi_lun"); 653 if (lo != NULL) { 654 lun_option_set(lo, tmp); 655 free(tmp); 656 } else { 657 lo = lun_option_new(lun, "cfiscsi_lun", tmp); 658 free(tmp); 659 assert(lo != NULL); 660 } 661 662 asprintf(&tmp, "%s,lun,%d", lun->l_target->t_name, lun->l_lun); 663 if (tmp == NULL) 664 log_errx(1, "asprintf"); | |
665 lo = lun_option_find(lun, "scsiname"); | 631 lo = lun_option_find(lun, "scsiname"); |
666 if (lo != NULL) { 667 lun_option_set(lo, tmp); 668 free(tmp); 669 } else { 670 lo = lun_option_new(lun, "scsiname", tmp); 671 free(tmp); | 632 if (lo == NULL && lun->l_scsiname != NULL) { 633 lo = lun_option_new(lun, "scsiname", lun->l_scsiname); |
672 assert(lo != NULL); 673 } 674 675 num_options = 0; 676 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 677 num_options++; 678 679 req.num_be_args = num_options; --- 161 unchanged lines hidden (view full) --- 841 } 842} 843 844int 845kernel_port_add(struct target *targ) 846{ 847 struct ctl_port_entry entry; 848 struct ctl_req req; | 634 assert(lo != NULL); 635 } 636 637 num_options = 0; 638 TAILQ_FOREACH(lo, &lun->l_options, lo_next) 639 num_options++; 640 641 req.num_be_args = num_options; --- 161 unchanged lines hidden (view full) --- 803 } 804} 805 806int 807kernel_port_add(struct target *targ) 808{ 809 struct ctl_port_entry entry; 810 struct ctl_req req; |
811 struct ctl_lun_map lm; |
|
849 char tagstr[16]; | 812 char tagstr[16]; |
850 int error; 851 uint32_t port_id = -1; | 813 int error, i; |
852 | 814 |
815 /* Create iSCSI port. */ |
|
853 bzero(&req, sizeof(req)); 854 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 855 req.reqtype = CTL_REQ_CREATE; 856 req.num_args = 4; 857 req.args = malloc(req.num_args * sizeof(*req.args)); 858 req.args[0].namelen = sizeof("port_id"); 859 req.args[0].name = __DECONST(char *, "port_id"); | 816 bzero(&req, sizeof(req)); 817 strlcpy(req.driver, "iscsi", sizeof(req.driver)); 818 req.reqtype = CTL_REQ_CREATE; 819 req.num_args = 4; 820 req.args = malloc(req.num_args * sizeof(*req.args)); 821 req.args[0].namelen = sizeof("port_id"); 822 req.args[0].name = __DECONST(char *, "port_id"); |
860 req.args[0].vallen = sizeof(port_id); 861 req.args[0].value = &port_id; | 823 req.args[0].vallen = sizeof(targ->t_ctl_port); 824 req.args[0].value = &targ->t_ctl_port; |
862 req.args[0].flags = CTL_BEARG_WR; 863 str_arg(&req.args[1], "cfiscsi_target", targ->t_name); 864 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag); 865 str_arg(&req.args[2], "cfiscsi_portal_group_tag", tagstr); 866 if (targ->t_alias) 867 str_arg(&req.args[3], "cfiscsi_target_alias", targ->t_alias); 868 else 869 req.num_args--; | 825 req.args[0].flags = CTL_BEARG_WR; 826 str_arg(&req.args[1], "cfiscsi_target", targ->t_name); 827 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag); 828 str_arg(&req.args[2], "cfiscsi_portal_group_tag", tagstr); 829 if (targ->t_alias) 830 str_arg(&req.args[3], "cfiscsi_target_alias", targ->t_alias); 831 else 832 req.num_args--; |
870 | |
871 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 872 free(req.args); 873 if (error != 0) { 874 log_warn("error issuing CTL_PORT_REQ ioctl"); 875 return (1); 876 } | 833 error = ioctl(ctl_fd, CTL_PORT_REQ, &req); 834 free(req.args); 835 if (error != 0) { 836 log_warn("error issuing CTL_PORT_REQ ioctl"); 837 return (1); 838 } |
877 | |
878 if (req.status == CTL_LUN_ERROR) { 879 log_warnx("error returned from port creation request: %s", 880 req.error_str); 881 return (1); 882 } | 839 if (req.status == CTL_LUN_ERROR) { 840 log_warnx("error returned from port creation request: %s", 841 req.error_str); 842 return (1); 843 } |
883 | |
884 if (req.status != CTL_LUN_OK) { 885 log_warnx("unknown port creation request status %d", 886 req.status); 887 return (1); 888 } 889 | 844 if (req.status != CTL_LUN_OK) { 845 log_warnx("unknown port creation request status %d", 846 req.status); 847 return (1); 848 } 849 |
890 bzero(&entry, sizeof(entry)); 891 entry.targ_port = port_id; | 850 /* Explicitly enable mapping to block any access except allowed. */ 851 lm.port = targ->t_ctl_port; 852 lm.plun = UINT32_MAX; 853 lm.lun = 0; 854 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 855 if (error != 0) 856 log_warn("CTL_LUN_MAP ioctl failed"); |
892 | 857 |
858 /* Map configured LUNs */ 859 for (i = 0; i < MAX_LUNS; i++) { 860 if (targ->t_luns[i] == NULL) 861 continue; 862 lm.port = targ->t_ctl_port; 863 lm.plun = i; 864 lm.lun = targ->t_luns[i]->l_ctl_lun; 865 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 866 if (error != 0) 867 log_warn("CTL_LUN_MAP ioctl failed"); 868 } 869 870 /* Enable port */ 871 bzero(&entry, sizeof(entry)); 872 entry.targ_port = targ->t_ctl_port; |
|
893 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 894 if (error != 0) { 895 log_warn("CTL_ENABLE_PORT ioctl failed"); 896 return (-1); 897 } 898 899 return (0); 900} 901 902int | 873 error = ioctl(ctl_fd, CTL_ENABLE_PORT, &entry); 874 if (error != 0) { 875 log_warn("CTL_ENABLE_PORT ioctl failed"); 876 return (-1); 877 } 878 879 return (0); 880} 881 882int |
883kernel_port_update(struct target *targ) 884{ 885 struct ctl_lun_map lm; 886 int error, i; 887 888 /* Map configured LUNs and unmap others */ 889 for (i = 0; i < MAX_LUNS; i++) { 890 lm.port = targ->t_ctl_port; 891 lm.plun = i; 892 if (targ->t_luns[i] == NULL) 893 lm.lun = UINT32_MAX; 894 else 895 lm.lun = targ->t_luns[i]->l_ctl_lun; 896 error = ioctl(ctl_fd, CTL_LUN_MAP, &lm); 897 if (error != 0) 898 log_warn("CTL_LUN_MAP ioctl failed"); 899 } 900 return (0); 901} 902 903int |
|
903kernel_port_remove(struct target *targ) 904{ 905 struct ctl_req req; 906 char tagstr[16]; 907 int error; 908 909 bzero(&req, sizeof(req)); 910 strlcpy(req.driver, "iscsi", sizeof(req.driver)); --- 170 unchanged lines hidden --- | 904kernel_port_remove(struct target *targ) 905{ 906 struct ctl_req req; 907 char tagstr[16]; 908 int error; 909 910 bzero(&req, sizeof(req)); 911 strlcpy(req.driver, "iscsi", sizeof(req.driver)); --- 170 unchanged lines hidden --- |