Deleted Added
sdiff udiff text old ( 279003 ) new ( 279006 )
full compact
1/*-
2 * Copyright (c) 2012 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

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

24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/10/usr.sbin/ctld/ctld.c 279003 2015-02-19 14:33:46Z mav $");
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/socket.h>
37#include <sys/wait.h>
38#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <assert.h>

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

86 struct conf *conf;
87
88 conf = calloc(1, sizeof(*conf));
89 if (conf == NULL)
90 log_err(1, "calloc");
91 TAILQ_INIT(&conf->conf_luns);
92 TAILQ_INIT(&conf->conf_targets);
93 TAILQ_INIT(&conf->conf_auth_groups);
94 TAILQ_INIT(&conf->conf_portal_groups);
95 TAILQ_INIT(&conf->conf_isns);
96
97 conf->conf_isns_period = 900;
98 conf->conf_isns_timeout = 5;
99 conf->conf_debug = 0;
100 conf->conf_timeout = 60;
101 conf->conf_maxproc = 30;

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

119 TAILQ_FOREACH_SAFE(targ, &conf->conf_targets, t_next, tmp)
120 target_delete(targ);
121 TAILQ_FOREACH_SAFE(ag, &conf->conf_auth_groups, ag_next, cagtmp)
122 auth_group_delete(ag);
123 TAILQ_FOREACH_SAFE(pg, &conf->conf_portal_groups, pg_next, cpgtmp)
124 portal_group_delete(pg);
125 TAILQ_FOREACH_SAFE(is, &conf->conf_isns, i_next, istmp)
126 isns_delete(is);
127 free(conf->conf_pidfile_path);
128 free(conf);
129}
130
131static struct auth *
132auth_new(struct auth_group *ag)
133{
134 struct auth *auth;

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

604 return (NULL);
605 }
606
607 pg = calloc(1, sizeof(*pg));
608 if (pg == NULL)
609 log_err(1, "calloc");
610 pg->pg_name = checked_strdup(name);
611 TAILQ_INIT(&pg->pg_portals);
612 pg->pg_conf = conf;
613 pg->pg_tag = 0; /* Assigned later in conf_apply(). */
614 TAILQ_INSERT_TAIL(&conf->conf_portal_groups, pg, pg_next);
615
616 return (pg);
617}
618
619void
620portal_group_delete(struct portal_group *pg)
621{
622 struct portal *portal, *tmp;
623
624 TAILQ_REMOVE(&pg->pg_conf->conf_portal_groups, pg, pg_next);
625
626 TAILQ_FOREACH_SAFE(portal, &pg->pg_portals, p_next, tmp)
627 portal_delete(portal);
628 free(pg->pg_name);
629 free(pg->pg_redirection);
630 free(pg);
631}

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

779
780static int
781isns_do_register(struct isns *isns, int s, const char *hostname)
782{
783 struct conf *conf = isns->i_conf;
784 struct target *target;
785 struct portal *portal;
786 struct portal_group *pg;
787 struct isns_req *req;
788 int res = 0;
789 uint32_t error;
790
791 req = isns_req_create(ISNS_FUNC_DEVATTRREG, ISNS_FLAG_CLIENT);
792 isns_req_add_str(req, 32, TAILQ_FIRST(&conf->conf_targets)->t_name);
793 isns_req_add_delim(req);
794 isns_req_add_str(req, 1, hostname);

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

802 isns_req_add_port(req, 17, portal->p_ai);
803 }
804 }
805 TAILQ_FOREACH(target, &conf->conf_targets, t_next) {
806 isns_req_add_str(req, 32, target->t_name);
807 isns_req_add_32(req, 33, 1); /* 1 -- Target*/
808 if (target->t_alias != NULL)
809 isns_req_add_str(req, 34, target->t_alias);
810 pg = target->t_portal_group;
811 isns_req_add_32(req, 51, pg->pg_tag);
812 TAILQ_FOREACH(portal, &pg->pg_portals, p_next) {
813 isns_req_add_addr(req, 49, portal->p_ai);
814 isns_req_add_port(req, 50, portal->p_ai);
815 }
816 }
817 res = isns_req_send(s, req);
818 if (res < 0) {
819 log_warn("send(2) failed for %s", isns->i_addr);
820 goto quit;
821 }
822 res = isns_req_receive(s, req);

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

1118 } else {
1119 log_warnx("invalid target name \"%s\"; should start with "
1120 "either \".iqn\", \"eui.\", or \"naa.\"",
1121 name);
1122 }
1123 return (true);
1124}
1125
1126struct target *
1127target_new(struct conf *conf, const char *name)
1128{
1129 struct target *targ;
1130 int i, len;
1131
1132 targ = target_find(conf, name);
1133 if (targ != NULL) {

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

1146 /*
1147 * RFC 3722 requires us to normalize the name to lowercase.
1148 */
1149 len = strlen(name);
1150 for (i = 0; i < len; i++)
1151 targ->t_name[i] = tolower(targ->t_name[i]);
1152
1153 targ->t_conf = conf;
1154 TAILQ_INSERT_TAIL(&conf->conf_targets, targ, t_next);
1155
1156 return (targ);
1157}
1158
1159void
1160target_delete(struct target *targ)
1161{
1162
1163 TAILQ_REMOVE(&targ->t_conf->conf_targets, targ, t_next);
1164
1165 free(targ->t_name);
1166 free(targ->t_redirection);
1167 free(targ);
1168}
1169
1170struct target *

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

1191 return (1);
1192 }
1193
1194 target->t_redirection = checked_strdup(addr);
1195
1196 return (0);
1197}
1198
1199void
1200target_set_ctl_port(struct target *target, uint32_t value)
1201{
1202
1203 target->t_ctl_port = value;
1204}
1205
1206struct lun *
1207lun_new(struct conf *conf, const char *name)
1208{
1209 struct lun *lun;
1210
1211 lun = lun_find(conf, name);
1212 if (lun != NULL) {
1213 log_warnx("duplicated lun \"%s\"", name);

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

1503 return (0);
1504}
1505
1506int
1507conf_verify(struct conf *conf)
1508{
1509 struct auth_group *ag;
1510 struct portal_group *pg;
1511 struct target *targ;
1512 struct lun *lun;
1513 bool found;
1514 int error, i;
1515
1516 if (conf->conf_pidfile_path == NULL)
1517 conf->conf_pidfile_path = checked_strdup(DEFAULT_PIDFILE);
1518
1519 TAILQ_FOREACH(lun, &conf->conf_luns, l_next) {
1520 error = conf_verify_lun(lun);
1521 if (error != 0)
1522 return (error);
1523 }
1524 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1525 if (targ->t_auth_group == NULL) {
1526 targ->t_auth_group = auth_group_find(conf,
1527 "default");
1528 assert(targ->t_auth_group != NULL);
1529 }
1530 if (targ->t_portal_group == NULL) {
1531 targ->t_portal_group = portal_group_find(conf,
1532 "default");
1533 assert(targ->t_portal_group != NULL);
1534 }
1535 found = false;
1536 for (i = 0; i < MAX_LUNS; i++) {
1537 if (targ->t_luns[i] != NULL)
1538 found = true;
1539 }
1540 if (!found && targ->t_redirection == NULL) {
1541 log_warnx("no LUNs defined for target \"%s\"",

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

1553 pg->pg_discovery_auth_group =
1554 auth_group_find(conf, "default");
1555 assert(pg->pg_discovery_auth_group != NULL);
1556 }
1557
1558 if (pg->pg_discovery_filter == PG_FILTER_UNKNOWN)
1559 pg->pg_discovery_filter = PG_FILTER_NONE;
1560
1561 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1562 if (targ->t_portal_group == pg)
1563 break;
1564 }
1565 if (pg->pg_redirection != NULL) {
1566 if (targ != NULL) {
1567 log_debugx("portal-group \"%s\" assigned "
1568 "to target \"%s\", but configured "
1569 "for redirection",
1570 pg->pg_name, targ->t_name);
1571 }
1572 pg->pg_unassigned = false;
1573 } else if (targ != NULL) {
1574 pg->pg_unassigned = false;
1575 } else {
1576 if (strcmp(pg->pg_name, "default") != 0)
1577 log_warnx("portal-group \"%s\" not assigned "
1578 "to any target", pg->pg_name);
1579 pg->pg_unassigned = true;
1580 }
1581 }
1582 TAILQ_FOREACH(ag, &conf->conf_auth_groups, ag_next) {

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

1587
1588 found = false;
1589 TAILQ_FOREACH(targ, &conf->conf_targets, t_next) {
1590 if (targ->t_auth_group == ag) {
1591 found = true;
1592 break;
1593 }
1594 }
1595 TAILQ_FOREACH(pg, &conf->conf_portal_groups, pg_next) {
1596 if (pg->pg_discovery_auth_group == ag) {
1597 found = true;
1598 break;
1599 }
1600 }
1601 if (!found && ag->ag_name != NULL &&
1602 strcmp(ag->ag_name, "default") != 0 &&

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

1608 }
1609
1610 return (0);
1611}
1612
1613static int
1614conf_apply(struct conf *oldconf, struct conf *newconf)
1615{
1616 struct target *oldtarg, *newtarg, *tmptarg;
1617 struct lun *oldlun, *newlun, *tmplun;
1618 struct portal_group *oldpg, *newpg;
1619 struct portal *oldp, *newp;
1620 struct isns *oldns, *newns;
1621 pid_t otherpid;
1622 int changed, cumulated_error = 0, error, sockbuf;
1623 int one = 1;
1624
1625 if (oldconf->conf_debug != newconf->conf_debug) {
1626 log_debugx("changing debug level to %d", newconf->conf_debug);
1627 log_init(newconf->conf_debug);

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

1679 /*
1680 * XXX: If target or lun removal fails, we should somehow "move"
1681 * the old lun or target into newconf, so that subsequent
1682 * conf_apply() would try to remove them again. That would
1683 * be somewhat hairy, though, and lun deletion failures don't
1684 * really happen, so leave it as it is for now.
1685 */
1686 /*
1687 * First, remove any targets present in the old configuration
1688 * and missing in the new one.
1689 */
1690 TAILQ_FOREACH_SAFE(oldtarg, &oldconf->conf_targets, t_next, tmptarg) {
1691 newtarg = target_find(newconf, oldtarg->t_name);
1692 if (newtarg != NULL)
1693 continue;
1694 error = kernel_port_remove(oldtarg);
1695 if (error != 0) {
1696 log_warnx("failed to remove target %s",
1697 oldtarg->t_name);
1698 /*
1699 * XXX: Uncomment after fixing the root cause.
1700 *
1701 * cumulated_error++;
1702 */
1703 }
1704 }
1705

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

1804 if (error != 0) {
1805 log_warnx("failed to add lun \"%s\"", newlun->l_name);
1806 lun_delete(newlun);
1807 cumulated_error++;
1808 }
1809 }
1810
1811 /*
1812 * Now add new targets or modify existing ones.
1813 */
1814 TAILQ_FOREACH(newtarg, &newconf->conf_targets, t_next) {
1815 oldtarg = target_find(oldconf, newtarg->t_name);
1816
1817 if (oldtarg == NULL)
1818 error = kernel_port_add(newtarg);
1819 else {
1820 target_set_ctl_port(newtarg, oldtarg->t_ctl_port);
1821 error = kernel_port_update(newtarg);
1822 }
1823 if (error != 0) {
1824 log_warnx("failed to %s target %s",
1825 (oldtarg == NULL) ? "add" : "update",
1826 newtarg->t_name);
1827 /*
1828 * XXX: Uncomment after fixing the root cause.
1829 *
1830 * cumulated_error++;
1831 */
1832 }
1833 }
1834

--- 584 unchanged lines hidden ---