Deleted Added
sdiff udiff text old ( 279003 ) new ( 279006 )
full compact
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: stable/10/usr.sbin/ctld/kernel.c 279003 2015-02-19 14:33:46Z 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>

--- 69 unchanged lines hidden (view full) ---

116 char *device_id;
117 char *ctld_name;
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;
126 uint16_t cfiscsi_portal_group_tag;
127 STAILQ_HEAD(,cctl_lun_nv) attr_list;
128 STAILQ_ENTRY(cctl_port) links;
129};
130
131struct cctl_devlist_data {
132 int num_luns;
133 STAILQ_HEAD(,cctl_lun) lun_list;
134 struct cctl_lun *cur_lun;

--- 192 unchanged lines hidden (view full) ---

327
328 sbuf_delete(devlist->cur_sb[devlist->level]);
329 devlist->cur_sb[devlist->level] = NULL;
330 devlist->level--;
331
332 if (strcmp(name, "cfiscsi_target") == 0) {
333 cur_port->cfiscsi_target = str;
334 str = NULL;
335 } else if (strcmp(name, "cfiscsi_status") == 0) {
336 cur_port->cfiscsi_status = strtoul(str, NULL, 0);
337 } else if (strcmp(name, "cfiscsi_portal_group_tag") == 0) {
338 cur_port->cfiscsi_portal_group_tag = strtoul(str, NULL, 0);
339 } else if (strcmp(name, "targ_port") == 0) {
340 devlist->cur_port = NULL;
341 } else if (strcmp(name, "ctlportlist") == 0) {
342 /* Nothing. */
343 } else {
344 struct cctl_lun_nv *nv;
345
346 nv = calloc(1, sizeof(*nv));

--- 21 unchanged lines hidden (view full) ---

368 sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
369}
370
371struct conf *
372conf_new_from_kernel(void)
373{
374 struct conf *conf = NULL;
375 struct target *targ;
376 struct lun *cl;
377 struct lun_option *lo;
378 struct ctl_lun_list list;
379 struct cctl_devlist_data devlist;
380 struct cctl_lun *lun;
381 struct cctl_port *port;
382 XML_Parser parser;
383 char *str;

--- 107 unchanged lines hidden (view full) ---

491
492 STAILQ_FOREACH(port, &devlist.port_list, links) {
493
494 if (port->cfiscsi_target == NULL) {
495 log_debugx("CTL port %ju wasn't managed by ctld; "
496 "ignoring", (uintmax_t)port->port_id);
497 continue;
498 }
499 if (port->cfiscsi_status != 1) {
500 log_debugx("CTL port %ju is not active (%d); ignoring",
501 (uintmax_t)port->port_id, port->cfiscsi_status);
502 continue;
503 }
504
505 targ = target_find(conf, port->cfiscsi_target);
506 if (targ == NULL) {
507#if 0
508 log_debugx("found new kernel target %s for CTL port %ld",
509 port->cfiscsi_target, port->port_id);
510#endif
511 targ = target_new(conf, port->cfiscsi_target);
512 if (targ == NULL) {
513 log_warnx("target_new failed");
514 continue;
515 }
516 }
517 }
518
519 STAILQ_FOREACH(lun, &devlist.lun_list, links) {
520 struct cctl_lun_nv *nv;
521
522 if (lun->ctld_name == NULL) {
523 log_debugx("CTL lun %ju wasn't managed by ctld; "
524 "ignoring", (uintmax_t)lun->lun_id);

--- 274 unchanged lines hidden (view full) ---

799
800 if (req.status != CTL_ISCSI_OK) {
801 log_errx(1, "error returned from CTL iSCSI handoff request: "
802 "%s; dropping connection", req.error_str);
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;
812 char tagstr[16];
813 int error, i;
814
815 /* Create iSCSI port. */
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");
823 req.args[0].vallen = sizeof(targ->t_ctl_port);
824 req.args[0].value = &targ->t_ctl_port;
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--;
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 }
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 }
844 if (req.status != CTL_LUN_OK) {
845 log_warnx("unknown port creation request status %d",
846 req.status);
847 return (1);
848 }
849
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");
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;
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
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));
912 req.reqtype = CTL_REQ_REMOVE;
913 req.num_args = 2;
914 req.args = malloc(req.num_args * sizeof(*req.args));
915 str_arg(&req.args[0], "cfiscsi_target", targ->t_name);
916 snprintf(tagstr, sizeof(tagstr), "%d", targ->t_portal_group->pg_tag);
917 str_arg(&req.args[1], "cfiscsi_portal_group_tag", tagstr);
918
919 error = ioctl(ctl_fd, CTL_PORT_REQ, &req);
920 free(req.args);
921 if (error != 0) {
922 log_warn("error issuing CTL_PORT_REQ ioctl");
923 return (1);
924 }

--- 153 unchanged lines hidden ---