Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*******************************************************************************
0003  * This file contains iSCSI Target Portal Group related functions.
0004  *
0005  * (c) Copyright 2007-2013 Datera, Inc.
0006  *
0007  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
0008  *
0009  ******************************************************************************/
0010 
0011 #include <linux/slab.h>
0012 #include <target/target_core_base.h>
0013 #include <target/target_core_fabric.h>
0014 #include <target/iscsi/iscsi_target_core.h>
0015 #include "iscsi_target_erl0.h"
0016 #include "iscsi_target_login.h"
0017 #include "iscsi_target_nodeattrib.h"
0018 #include "iscsi_target_tpg.h"
0019 #include "iscsi_target_util.h"
0020 #include "iscsi_target.h"
0021 #include "iscsi_target_parameters.h"
0022 
0023 #include <target/iscsi/iscsi_transport.h>
0024 
0025 struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
0026 {
0027     struct iscsi_portal_group *tpg;
0028 
0029     tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
0030     if (!tpg) {
0031         pr_err("Unable to allocate struct iscsi_portal_group\n");
0032         return NULL;
0033     }
0034 
0035     tpg->tpgt = tpgt;
0036     tpg->tpg_state = TPG_STATE_FREE;
0037     tpg->tpg_tiqn = tiqn;
0038     INIT_LIST_HEAD(&tpg->tpg_gnp_list);
0039     INIT_LIST_HEAD(&tpg->tpg_list);
0040     mutex_init(&tpg->tpg_access_lock);
0041     sema_init(&tpg->np_login_sem, 1);
0042     spin_lock_init(&tpg->tpg_state_lock);
0043     spin_lock_init(&tpg->tpg_np_lock);
0044 
0045     return tpg;
0046 }
0047 
0048 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);
0049 
0050 int iscsit_load_discovery_tpg(void)
0051 {
0052     struct iscsi_param *param;
0053     struct iscsi_portal_group *tpg;
0054     int ret;
0055 
0056     tpg = iscsit_alloc_portal_group(NULL, 1);
0057     if (!tpg) {
0058         pr_err("Unable to allocate struct iscsi_portal_group\n");
0059         return -1;
0060     }
0061     /*
0062      * Save iscsi_ops pointer for special case discovery TPG that
0063      * doesn't exist as se_wwn->wwn_group within configfs.
0064      */
0065     tpg->tpg_se_tpg.se_tpg_tfo = &iscsi_ops;
0066     ret = core_tpg_register(NULL, &tpg->tpg_se_tpg, -1);
0067     if (ret < 0) {
0068         kfree(tpg);
0069         return -1;
0070     }
0071 
0072     tpg->sid = 1; /* First Assigned LIO Session ID */
0073     iscsit_set_default_tpg_attribs(tpg);
0074 
0075     if (iscsi_create_default_params(&tpg->param_list) < 0)
0076         goto out;
0077     /*
0078      * By default we disable authentication for discovery sessions,
0079      * this can be changed with:
0080      *
0081      * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
0082      */
0083     param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
0084     if (!param)
0085         goto free_pl_out;
0086 
0087     if (iscsi_update_param_value(param, "CHAP,None") < 0)
0088         goto free_pl_out;
0089 
0090     tpg->tpg_attrib.authentication = 0;
0091 
0092     spin_lock(&tpg->tpg_state_lock);
0093     tpg->tpg_state  = TPG_STATE_ACTIVE;
0094     spin_unlock(&tpg->tpg_state_lock);
0095 
0096     iscsit_global->discovery_tpg = tpg;
0097     pr_debug("CORE[0] - Allocated Discovery TPG\n");
0098 
0099     return 0;
0100 free_pl_out:
0101     iscsi_release_param_list(tpg->param_list);
0102 out:
0103     if (tpg->sid == 1)
0104         core_tpg_deregister(&tpg->tpg_se_tpg);
0105     kfree(tpg);
0106     return -1;
0107 }
0108 
0109 void iscsit_release_discovery_tpg(void)
0110 {
0111     struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;
0112 
0113     if (!tpg)
0114         return;
0115 
0116     iscsi_release_param_list(tpg->param_list);
0117     core_tpg_deregister(&tpg->tpg_se_tpg);
0118 
0119     kfree(tpg);
0120     iscsit_global->discovery_tpg = NULL;
0121 }
0122 
0123 struct iscsi_portal_group *iscsit_get_tpg_from_np(
0124     struct iscsi_tiqn *tiqn,
0125     struct iscsi_np *np,
0126     struct iscsi_tpg_np **tpg_np_out)
0127 {
0128     struct iscsi_portal_group *tpg = NULL;
0129     struct iscsi_tpg_np *tpg_np;
0130 
0131     spin_lock(&tiqn->tiqn_tpg_lock);
0132     list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
0133 
0134         spin_lock(&tpg->tpg_state_lock);
0135         if (tpg->tpg_state != TPG_STATE_ACTIVE) {
0136             spin_unlock(&tpg->tpg_state_lock);
0137             continue;
0138         }
0139         spin_unlock(&tpg->tpg_state_lock);
0140 
0141         spin_lock(&tpg->tpg_np_lock);
0142         list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
0143             if (tpg_np->tpg_np == np) {
0144                 *tpg_np_out = tpg_np;
0145                 kref_get(&tpg_np->tpg_np_kref);
0146                 spin_unlock(&tpg->tpg_np_lock);
0147                 spin_unlock(&tiqn->tiqn_tpg_lock);
0148                 return tpg;
0149             }
0150         }
0151         spin_unlock(&tpg->tpg_np_lock);
0152     }
0153     spin_unlock(&tiqn->tiqn_tpg_lock);
0154 
0155     return NULL;
0156 }
0157 
0158 int iscsit_get_tpg(
0159     struct iscsi_portal_group *tpg)
0160 {
0161     return mutex_lock_interruptible(&tpg->tpg_access_lock);
0162 }
0163 
0164 void iscsit_put_tpg(struct iscsi_portal_group *tpg)
0165 {
0166     mutex_unlock(&tpg->tpg_access_lock);
0167 }
0168 
0169 static void iscsit_clear_tpg_np_login_thread(
0170     struct iscsi_tpg_np *tpg_np,
0171     struct iscsi_portal_group *tpg,
0172     bool shutdown)
0173 {
0174     if (!tpg_np->tpg_np) {
0175         pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
0176         return;
0177     }
0178 
0179     if (shutdown)
0180         tpg_np->tpg_np->enabled = false;
0181     iscsit_reset_np_thread(tpg_np->tpg_np, tpg_np, tpg, shutdown);
0182 }
0183 
0184 static void iscsit_clear_tpg_np_login_threads(
0185     struct iscsi_portal_group *tpg,
0186     bool shutdown)
0187 {
0188     struct iscsi_tpg_np *tpg_np;
0189 
0190     spin_lock(&tpg->tpg_np_lock);
0191     list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
0192         if (!tpg_np->tpg_np) {
0193             pr_err("struct iscsi_tpg_np->tpg_np is NULL!\n");
0194             continue;
0195         }
0196         spin_unlock(&tpg->tpg_np_lock);
0197         iscsit_clear_tpg_np_login_thread(tpg_np, tpg, shutdown);
0198         spin_lock(&tpg->tpg_np_lock);
0199     }
0200     spin_unlock(&tpg->tpg_np_lock);
0201 }
0202 
0203 void iscsit_tpg_dump_params(struct iscsi_portal_group *tpg)
0204 {
0205     iscsi_print_params(tpg->param_list);
0206 }
0207 
0208 static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *tpg)
0209 {
0210     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0211 
0212     a->authentication = TA_AUTHENTICATION;
0213     a->login_timeout = TA_LOGIN_TIMEOUT;
0214     a->netif_timeout = TA_NETIF_TIMEOUT;
0215     a->default_cmdsn_depth = TA_DEFAULT_CMDSN_DEPTH;
0216     a->generate_node_acls = TA_GENERATE_NODE_ACLS;
0217     a->cache_dynamic_acls = TA_CACHE_DYNAMIC_ACLS;
0218     a->demo_mode_write_protect = TA_DEMO_MODE_WRITE_PROTECT;
0219     a->prod_mode_write_protect = TA_PROD_MODE_WRITE_PROTECT;
0220     a->demo_mode_discovery = TA_DEMO_MODE_DISCOVERY;
0221     a->default_erl = TA_DEFAULT_ERL;
0222     a->t10_pi = TA_DEFAULT_T10_PI;
0223     a->fabric_prot_type = TA_DEFAULT_FABRIC_PROT_TYPE;
0224     a->tpg_enabled_sendtargets = TA_DEFAULT_TPG_ENABLED_SENDTARGETS;
0225     a->login_keys_workaround = TA_DEFAULT_LOGIN_KEYS_WORKAROUND;
0226 }
0227 
0228 int iscsit_tpg_add_portal_group(struct iscsi_tiqn *tiqn, struct iscsi_portal_group *tpg)
0229 {
0230     if (tpg->tpg_state != TPG_STATE_FREE) {
0231         pr_err("Unable to add iSCSI Target Portal Group: %d"
0232             " while not in TPG_STATE_FREE state.\n", tpg->tpgt);
0233         return -EEXIST;
0234     }
0235     iscsit_set_default_tpg_attribs(tpg);
0236 
0237     if (iscsi_create_default_params(&tpg->param_list) < 0)
0238         goto err_out;
0239 
0240     tpg->tpg_attrib.tpg = tpg;
0241 
0242     spin_lock(&tpg->tpg_state_lock);
0243     tpg->tpg_state  = TPG_STATE_INACTIVE;
0244     spin_unlock(&tpg->tpg_state_lock);
0245 
0246     spin_lock(&tiqn->tiqn_tpg_lock);
0247     list_add_tail(&tpg->tpg_list, &tiqn->tiqn_tpg_list);
0248     tiqn->tiqn_ntpgs++;
0249     pr_debug("CORE[%s]_TPG[%hu] - Added iSCSI Target Portal Group\n",
0250             tiqn->tiqn, tpg->tpgt);
0251     spin_unlock(&tiqn->tiqn_tpg_lock);
0252 
0253     return 0;
0254 err_out:
0255     if (tpg->param_list) {
0256         iscsi_release_param_list(tpg->param_list);
0257         tpg->param_list = NULL;
0258     }
0259     return -ENOMEM;
0260 }
0261 
0262 int iscsit_tpg_del_portal_group(
0263     struct iscsi_tiqn *tiqn,
0264     struct iscsi_portal_group *tpg,
0265     int force)
0266 {
0267     u8 old_state = tpg->tpg_state;
0268 
0269     spin_lock(&tpg->tpg_state_lock);
0270     tpg->tpg_state = TPG_STATE_INACTIVE;
0271     spin_unlock(&tpg->tpg_state_lock);
0272 
0273     if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
0274         pr_err("Unable to delete iSCSI Target Portal Group:"
0275             " %hu while active sessions exist, and force=0\n",
0276             tpg->tpgt);
0277         tpg->tpg_state = old_state;
0278         return -EPERM;
0279     }
0280 
0281     if (tpg->param_list) {
0282         iscsi_release_param_list(tpg->param_list);
0283         tpg->param_list = NULL;
0284     }
0285 
0286     core_tpg_deregister(&tpg->tpg_se_tpg);
0287 
0288     spin_lock(&tpg->tpg_state_lock);
0289     tpg->tpg_state = TPG_STATE_FREE;
0290     spin_unlock(&tpg->tpg_state_lock);
0291 
0292     spin_lock(&tiqn->tiqn_tpg_lock);
0293     tiqn->tiqn_ntpgs--;
0294     list_del(&tpg->tpg_list);
0295     spin_unlock(&tiqn->tiqn_tpg_lock);
0296 
0297     pr_debug("CORE[%s]_TPG[%hu] - Deleted iSCSI Target Portal Group\n",
0298             tiqn->tiqn, tpg->tpgt);
0299 
0300     kfree(tpg);
0301     return 0;
0302 }
0303 
0304 int iscsit_tpg_enable_portal_group(struct iscsi_portal_group *tpg)
0305 {
0306     struct iscsi_param *param;
0307     struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
0308     int ret;
0309 
0310     if (tpg->tpg_state == TPG_STATE_ACTIVE) {
0311         pr_err("iSCSI target portal group: %hu is already"
0312             " active, ignoring request.\n", tpg->tpgt);
0313         return -EINVAL;
0314     }
0315     /*
0316      * Make sure that AuthMethod does not contain None as an option
0317      * unless explictly disabled.  Set the default to CHAP if authentication
0318      * is enforced (as per default), and remove the NONE option.
0319      */
0320     param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
0321     if (!param)
0322         return -EINVAL;
0323 
0324     if (tpg->tpg_attrib.authentication) {
0325         if (!strcmp(param->value, NONE)) {
0326             ret = iscsi_update_param_value(param, CHAP);
0327             if (ret)
0328                 goto err;
0329         }
0330 
0331         ret = iscsit_ta_authentication(tpg, 1);
0332         if (ret < 0)
0333             goto err;
0334     }
0335 
0336     spin_lock(&tpg->tpg_state_lock);
0337     tpg->tpg_state = TPG_STATE_ACTIVE;
0338     spin_unlock(&tpg->tpg_state_lock);
0339 
0340     spin_lock(&tiqn->tiqn_tpg_lock);
0341     tiqn->tiqn_active_tpgs++;
0342     pr_debug("iSCSI_TPG[%hu] - Enabled iSCSI Target Portal Group\n",
0343             tpg->tpgt);
0344     spin_unlock(&tiqn->tiqn_tpg_lock);
0345 
0346     return 0;
0347 
0348 err:
0349     return ret;
0350 }
0351 
0352 int iscsit_tpg_disable_portal_group(struct iscsi_portal_group *tpg, int force)
0353 {
0354     struct iscsi_tiqn *tiqn;
0355     u8 old_state = tpg->tpg_state;
0356 
0357     spin_lock(&tpg->tpg_state_lock);
0358     if (tpg->tpg_state == TPG_STATE_INACTIVE) {
0359         pr_err("iSCSI Target Portal Group: %hu is already"
0360             " inactive, ignoring request.\n", tpg->tpgt);
0361         spin_unlock(&tpg->tpg_state_lock);
0362         return -EINVAL;
0363     }
0364     tpg->tpg_state = TPG_STATE_INACTIVE;
0365     spin_unlock(&tpg->tpg_state_lock);
0366 
0367     iscsit_clear_tpg_np_login_threads(tpg, false);
0368 
0369     if (iscsit_release_sessions_for_tpg(tpg, force) < 0) {
0370         spin_lock(&tpg->tpg_state_lock);
0371         tpg->tpg_state = old_state;
0372         spin_unlock(&tpg->tpg_state_lock);
0373         pr_err("Unable to disable iSCSI Target Portal Group:"
0374             " %hu while active sessions exist, and force=0\n",
0375             tpg->tpgt);
0376         return -EPERM;
0377     }
0378 
0379     tiqn = tpg->tpg_tiqn;
0380     if (!tiqn || (tpg == iscsit_global->discovery_tpg))
0381         return 0;
0382 
0383     spin_lock(&tiqn->tiqn_tpg_lock);
0384     tiqn->tiqn_active_tpgs--;
0385     pr_debug("iSCSI_TPG[%hu] - Disabled iSCSI Target Portal Group\n",
0386             tpg->tpgt);
0387     spin_unlock(&tiqn->tiqn_tpg_lock);
0388 
0389     return 0;
0390 }
0391 
0392 struct iscsi_node_attrib *iscsit_tpg_get_node_attrib(
0393     struct iscsit_session *sess)
0394 {
0395     struct se_session *se_sess = sess->se_sess;
0396     struct se_node_acl *se_nacl = se_sess->se_node_acl;
0397     struct iscsi_node_acl *acl = to_iscsi_nacl(se_nacl);
0398 
0399     return &acl->node_attrib;
0400 }
0401 
0402 struct iscsi_tpg_np *iscsit_tpg_locate_child_np(
0403     struct iscsi_tpg_np *tpg_np,
0404     int network_transport)
0405 {
0406     struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
0407 
0408     spin_lock(&tpg_np->tpg_np_parent_lock);
0409     list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
0410             &tpg_np->tpg_np_parent_list, tpg_np_child_list) {
0411         if (tpg_np_child->tpg_np->np_network_transport ==
0412                 network_transport) {
0413             spin_unlock(&tpg_np->tpg_np_parent_lock);
0414             return tpg_np_child;
0415         }
0416     }
0417     spin_unlock(&tpg_np->tpg_np_parent_lock);
0418 
0419     return NULL;
0420 }
0421 
0422 static bool iscsit_tpg_check_network_portal(
0423     struct iscsi_tiqn *tiqn,
0424     struct sockaddr_storage *sockaddr,
0425     int network_transport)
0426 {
0427     struct iscsi_portal_group *tpg;
0428     struct iscsi_tpg_np *tpg_np;
0429     struct iscsi_np *np;
0430     bool match = false;
0431 
0432     spin_lock(&tiqn->tiqn_tpg_lock);
0433     list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
0434 
0435         spin_lock(&tpg->tpg_np_lock);
0436         list_for_each_entry(tpg_np, &tpg->tpg_gnp_list, tpg_np_list) {
0437             np = tpg_np->tpg_np;
0438 
0439             match = iscsit_check_np_match(sockaddr, np,
0440                         network_transport);
0441             if (match)
0442                 break;
0443         }
0444         spin_unlock(&tpg->tpg_np_lock);
0445 
0446         if (match)
0447             break;
0448     }
0449     spin_unlock(&tiqn->tiqn_tpg_lock);
0450 
0451     return match;
0452 }
0453 
0454 struct iscsi_tpg_np *iscsit_tpg_add_network_portal(
0455     struct iscsi_portal_group *tpg,
0456     struct sockaddr_storage *sockaddr,
0457     struct iscsi_tpg_np *tpg_np_parent,
0458     int network_transport)
0459 {
0460     struct iscsi_np *np;
0461     struct iscsi_tpg_np *tpg_np;
0462 
0463     if (!tpg_np_parent) {
0464         if (iscsit_tpg_check_network_portal(tpg->tpg_tiqn, sockaddr,
0465                 network_transport)) {
0466             pr_err("Network Portal: %pISc already exists on a"
0467                 " different TPG on %s\n", sockaddr,
0468                 tpg->tpg_tiqn->tiqn);
0469             return ERR_PTR(-EEXIST);
0470         }
0471     }
0472 
0473     tpg_np = kzalloc(sizeof(struct iscsi_tpg_np), GFP_KERNEL);
0474     if (!tpg_np) {
0475         pr_err("Unable to allocate memory for"
0476                 " struct iscsi_tpg_np.\n");
0477         return ERR_PTR(-ENOMEM);
0478     }
0479 
0480     np = iscsit_add_np(sockaddr, network_transport);
0481     if (IS_ERR(np)) {
0482         kfree(tpg_np);
0483         return ERR_CAST(np);
0484     }
0485 
0486     INIT_LIST_HEAD(&tpg_np->tpg_np_list);
0487     INIT_LIST_HEAD(&tpg_np->tpg_np_child_list);
0488     INIT_LIST_HEAD(&tpg_np->tpg_np_parent_list);
0489     spin_lock_init(&tpg_np->tpg_np_parent_lock);
0490     init_completion(&tpg_np->tpg_np_comp);
0491     kref_init(&tpg_np->tpg_np_kref);
0492     tpg_np->tpg_np      = np;
0493     tpg_np->tpg     = tpg;
0494 
0495     spin_lock(&tpg->tpg_np_lock);
0496     list_add_tail(&tpg_np->tpg_np_list, &tpg->tpg_gnp_list);
0497     tpg->num_tpg_nps++;
0498     if (tpg->tpg_tiqn)
0499         tpg->tpg_tiqn->tiqn_num_tpg_nps++;
0500     spin_unlock(&tpg->tpg_np_lock);
0501 
0502     if (tpg_np_parent) {
0503         tpg_np->tpg_np_parent = tpg_np_parent;
0504         spin_lock(&tpg_np_parent->tpg_np_parent_lock);
0505         list_add_tail(&tpg_np->tpg_np_child_list,
0506             &tpg_np_parent->tpg_np_parent_list);
0507         spin_unlock(&tpg_np_parent->tpg_np_parent_lock);
0508     }
0509 
0510     pr_debug("CORE[%s] - Added Network Portal: %pISpc,%hu on %s\n",
0511         tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
0512         np->np_transport->name);
0513 
0514     return tpg_np;
0515 }
0516 
0517 static int iscsit_tpg_release_np(
0518     struct iscsi_tpg_np *tpg_np,
0519     struct iscsi_portal_group *tpg,
0520     struct iscsi_np *np)
0521 {
0522     iscsit_clear_tpg_np_login_thread(tpg_np, tpg, true);
0523 
0524     pr_debug("CORE[%s] - Removed Network Portal: %pISpc,%hu on %s\n",
0525         tpg->tpg_tiqn->tiqn, &np->np_sockaddr, tpg->tpgt,
0526         np->np_transport->name);
0527 
0528     tpg_np->tpg_np = NULL;
0529     tpg_np->tpg = NULL;
0530     kfree(tpg_np);
0531     /*
0532      * iscsit_del_np() will shutdown struct iscsi_np when last TPG reference is released.
0533      */
0534     return iscsit_del_np(np);
0535 }
0536 
0537 int iscsit_tpg_del_network_portal(
0538     struct iscsi_portal_group *tpg,
0539     struct iscsi_tpg_np *tpg_np)
0540 {
0541     struct iscsi_np *np;
0542     struct iscsi_tpg_np *tpg_np_child, *tpg_np_child_tmp;
0543     int ret = 0;
0544 
0545     np = tpg_np->tpg_np;
0546     if (!np) {
0547         pr_err("Unable to locate struct iscsi_np from"
0548                 " struct iscsi_tpg_np\n");
0549         return -EINVAL;
0550     }
0551 
0552     if (!tpg_np->tpg_np_parent) {
0553         /*
0554          * We are the parent tpg network portal.  Release all of the
0555          * child tpg_np's (eg: the non ISCSI_TCP ones) on our parent
0556          * list first.
0557          */
0558         list_for_each_entry_safe(tpg_np_child, tpg_np_child_tmp,
0559                 &tpg_np->tpg_np_parent_list,
0560                 tpg_np_child_list) {
0561             ret = iscsit_tpg_del_network_portal(tpg, tpg_np_child);
0562             if (ret < 0)
0563                 pr_err("iscsit_tpg_del_network_portal()"
0564                     " failed: %d\n", ret);
0565         }
0566     } else {
0567         /*
0568          * We are not the parent ISCSI_TCP tpg network portal.  Release
0569          * our own network portals from the child list.
0570          */
0571         spin_lock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
0572         list_del(&tpg_np->tpg_np_child_list);
0573         spin_unlock(&tpg_np->tpg_np_parent->tpg_np_parent_lock);
0574     }
0575 
0576     spin_lock(&tpg->tpg_np_lock);
0577     list_del(&tpg_np->tpg_np_list);
0578     tpg->num_tpg_nps--;
0579     if (tpg->tpg_tiqn)
0580         tpg->tpg_tiqn->tiqn_num_tpg_nps--;
0581     spin_unlock(&tpg->tpg_np_lock);
0582 
0583     return iscsit_tpg_release_np(tpg_np, tpg, np);
0584 }
0585 
0586 int iscsit_ta_authentication(struct iscsi_portal_group *tpg, u32 authentication)
0587 {
0588     unsigned char buf1[256], buf2[256], *none = NULL;
0589     int len;
0590     struct iscsi_param *param;
0591     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0592 
0593     if ((authentication != 1) && (authentication != 0)) {
0594         pr_err("Illegal value for authentication parameter:"
0595             " %u, ignoring request.\n", authentication);
0596         return -EINVAL;
0597     }
0598 
0599     memset(buf1, 0, sizeof(buf1));
0600     memset(buf2, 0, sizeof(buf2));
0601 
0602     param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
0603     if (!param)
0604         return -EINVAL;
0605 
0606     if (authentication) {
0607         snprintf(buf1, sizeof(buf1), "%s", param->value);
0608         none = strstr(buf1, NONE);
0609         if (!none)
0610             goto out;
0611         if (!strncmp(none + 4, ",", 1)) {
0612             if (!strcmp(buf1, none))
0613                 sprintf(buf2, "%s", none+5);
0614             else {
0615                 none--;
0616                 *none = '\0';
0617                 len = sprintf(buf2, "%s", buf1);
0618                 none += 5;
0619                 sprintf(buf2 + len, "%s", none);
0620             }
0621         } else {
0622             none--;
0623             *none = '\0';
0624             sprintf(buf2, "%s", buf1);
0625         }
0626         if (iscsi_update_param_value(param, buf2) < 0)
0627             return -EINVAL;
0628     } else {
0629         snprintf(buf1, sizeof(buf1), "%s", param->value);
0630         none = strstr(buf1, NONE);
0631         if (none)
0632             goto out;
0633         strlcat(buf1, "," NONE, sizeof(buf1));
0634         if (iscsi_update_param_value(param, buf1) < 0)
0635             return -EINVAL;
0636     }
0637 
0638 out:
0639     a->authentication = authentication;
0640     pr_debug("%s iSCSI Authentication Methods for TPG: %hu.\n",
0641         a->authentication ? "Enforcing" : "Disabling", tpg->tpgt);
0642 
0643     return 0;
0644 }
0645 
0646 int iscsit_ta_login_timeout(
0647     struct iscsi_portal_group *tpg,
0648     u32 login_timeout)
0649 {
0650     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0651 
0652     if (login_timeout > TA_LOGIN_TIMEOUT_MAX) {
0653         pr_err("Requested Login Timeout %u larger than maximum"
0654             " %u\n", login_timeout, TA_LOGIN_TIMEOUT_MAX);
0655         return -EINVAL;
0656     } else if (login_timeout < TA_LOGIN_TIMEOUT_MIN) {
0657         pr_err("Requested Logout Timeout %u smaller than"
0658             " minimum %u\n", login_timeout, TA_LOGIN_TIMEOUT_MIN);
0659         return -EINVAL;
0660     }
0661 
0662     a->login_timeout = login_timeout;
0663     pr_debug("Set Logout Timeout to %u for Target Portal Group"
0664         " %hu\n", a->login_timeout, tpg->tpgt);
0665 
0666     return 0;
0667 }
0668 
0669 int iscsit_ta_netif_timeout(
0670     struct iscsi_portal_group *tpg,
0671     u32 netif_timeout)
0672 {
0673     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0674 
0675     if (netif_timeout > TA_NETIF_TIMEOUT_MAX) {
0676         pr_err("Requested Network Interface Timeout %u larger"
0677             " than maximum %u\n", netif_timeout,
0678                 TA_NETIF_TIMEOUT_MAX);
0679         return -EINVAL;
0680     } else if (netif_timeout < TA_NETIF_TIMEOUT_MIN) {
0681         pr_err("Requested Network Interface Timeout %u smaller"
0682             " than minimum %u\n", netif_timeout,
0683                 TA_NETIF_TIMEOUT_MIN);
0684         return -EINVAL;
0685     }
0686 
0687     a->netif_timeout = netif_timeout;
0688     pr_debug("Set Network Interface Timeout to %u for"
0689         " Target Portal Group %hu\n", a->netif_timeout, tpg->tpgt);
0690 
0691     return 0;
0692 }
0693 
0694 int iscsit_ta_generate_node_acls(
0695     struct iscsi_portal_group *tpg,
0696     u32 flag)
0697 {
0698     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0699 
0700     if ((flag != 0) && (flag != 1)) {
0701         pr_err("Illegal value %d\n", flag);
0702         return -EINVAL;
0703     }
0704 
0705     a->generate_node_acls = flag;
0706     pr_debug("iSCSI_TPG[%hu] - Generate Initiator Portal Group ACLs: %s\n",
0707         tpg->tpgt, (a->generate_node_acls) ? "Enabled" : "Disabled");
0708 
0709     if (flag == 1 && a->cache_dynamic_acls == 0) {
0710         pr_debug("Explicitly setting cache_dynamic_acls=1 when "
0711             "generate_node_acls=1\n");
0712         a->cache_dynamic_acls = 1;
0713     }
0714 
0715     return 0;
0716 }
0717 
0718 int iscsit_ta_default_cmdsn_depth(
0719     struct iscsi_portal_group *tpg,
0720     u32 tcq_depth)
0721 {
0722     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0723 
0724     if (tcq_depth > TA_DEFAULT_CMDSN_DEPTH_MAX) {
0725         pr_err("Requested Default Queue Depth: %u larger"
0726             " than maximum %u\n", tcq_depth,
0727                 TA_DEFAULT_CMDSN_DEPTH_MAX);
0728         return -EINVAL;
0729     } else if (tcq_depth < TA_DEFAULT_CMDSN_DEPTH_MIN) {
0730         pr_err("Requested Default Queue Depth: %u smaller"
0731             " than minimum %u\n", tcq_depth,
0732                 TA_DEFAULT_CMDSN_DEPTH_MIN);
0733         return -EINVAL;
0734     }
0735 
0736     a->default_cmdsn_depth = tcq_depth;
0737     pr_debug("iSCSI_TPG[%hu] - Set Default CmdSN TCQ Depth to %u\n",
0738         tpg->tpgt, a->default_cmdsn_depth);
0739 
0740     return 0;
0741 }
0742 
0743 int iscsit_ta_cache_dynamic_acls(
0744     struct iscsi_portal_group *tpg,
0745     u32 flag)
0746 {
0747     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0748 
0749     if ((flag != 0) && (flag != 1)) {
0750         pr_err("Illegal value %d\n", flag);
0751         return -EINVAL;
0752     }
0753 
0754     if (a->generate_node_acls == 1 && flag == 0) {
0755         pr_debug("Skipping cache_dynamic_acls=0 when"
0756             " generate_node_acls=1\n");
0757         return 0;
0758     }
0759 
0760     a->cache_dynamic_acls = flag;
0761     pr_debug("iSCSI_TPG[%hu] - Cache Dynamic Initiator Portal Group"
0762         " ACLs %s\n", tpg->tpgt, (a->cache_dynamic_acls) ?
0763         "Enabled" : "Disabled");
0764 
0765     return 0;
0766 }
0767 
0768 int iscsit_ta_demo_mode_write_protect(
0769     struct iscsi_portal_group *tpg,
0770     u32 flag)
0771 {
0772     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0773 
0774     if ((flag != 0) && (flag != 1)) {
0775         pr_err("Illegal value %d\n", flag);
0776         return -EINVAL;
0777     }
0778 
0779     a->demo_mode_write_protect = flag;
0780     pr_debug("iSCSI_TPG[%hu] - Demo Mode Write Protect bit: %s\n",
0781         tpg->tpgt, (a->demo_mode_write_protect) ? "ON" : "OFF");
0782 
0783     return 0;
0784 }
0785 
0786 int iscsit_ta_prod_mode_write_protect(
0787     struct iscsi_portal_group *tpg,
0788     u32 flag)
0789 {
0790     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0791 
0792     if ((flag != 0) && (flag != 1)) {
0793         pr_err("Illegal value %d\n", flag);
0794         return -EINVAL;
0795     }
0796 
0797     a->prod_mode_write_protect = flag;
0798     pr_debug("iSCSI_TPG[%hu] - Production Mode Write Protect bit:"
0799         " %s\n", tpg->tpgt, (a->prod_mode_write_protect) ?
0800         "ON" : "OFF");
0801 
0802     return 0;
0803 }
0804 
0805 int iscsit_ta_demo_mode_discovery(
0806     struct iscsi_portal_group *tpg,
0807     u32 flag)
0808 {
0809     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0810 
0811     if ((flag != 0) && (flag != 1)) {
0812         pr_err("Illegal value %d\n", flag);
0813         return -EINVAL;
0814     }
0815 
0816     a->demo_mode_discovery = flag;
0817     pr_debug("iSCSI_TPG[%hu] - Demo Mode Discovery bit:"
0818         " %s\n", tpg->tpgt, (a->demo_mode_discovery) ?
0819         "ON" : "OFF");
0820 
0821     return 0;
0822 }
0823 
0824 int iscsit_ta_default_erl(
0825     struct iscsi_portal_group *tpg,
0826     u32 default_erl)
0827 {
0828     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0829 
0830     if ((default_erl != 0) && (default_erl != 1) && (default_erl != 2)) {
0831         pr_err("Illegal value for default_erl: %u\n", default_erl);
0832         return -EINVAL;
0833     }
0834 
0835     a->default_erl = default_erl;
0836     pr_debug("iSCSI_TPG[%hu] - DefaultERL: %u\n", tpg->tpgt, a->default_erl);
0837 
0838     return 0;
0839 }
0840 
0841 int iscsit_ta_t10_pi(
0842     struct iscsi_portal_group *tpg,
0843     u32 flag)
0844 {
0845     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0846 
0847     if ((flag != 0) && (flag != 1)) {
0848         pr_err("Illegal value %d\n", flag);
0849         return -EINVAL;
0850     }
0851 
0852     a->t10_pi = flag;
0853     pr_debug("iSCSI_TPG[%hu] - T10 Protection information bit:"
0854         " %s\n", tpg->tpgt, (a->t10_pi) ?
0855         "ON" : "OFF");
0856 
0857     return 0;
0858 }
0859 
0860 int iscsit_ta_fabric_prot_type(
0861     struct iscsi_portal_group *tpg,
0862     u32 prot_type)
0863 {
0864     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0865 
0866     if ((prot_type != 0) && (prot_type != 1) && (prot_type != 3)) {
0867         pr_err("Illegal value for fabric_prot_type: %u\n", prot_type);
0868         return -EINVAL;
0869     }
0870 
0871     a->fabric_prot_type = prot_type;
0872     pr_debug("iSCSI_TPG[%hu] - T10 Fabric Protection Type: %u\n",
0873          tpg->tpgt, prot_type);
0874 
0875     return 0;
0876 }
0877 
0878 int iscsit_ta_tpg_enabled_sendtargets(
0879     struct iscsi_portal_group *tpg,
0880     u32 flag)
0881 {
0882     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0883 
0884     if ((flag != 0) && (flag != 1)) {
0885         pr_err("Illegal value %d\n", flag);
0886         return -EINVAL;
0887     }
0888 
0889     a->tpg_enabled_sendtargets = flag;
0890     pr_debug("iSCSI_TPG[%hu] - TPG enabled bit required for SendTargets:"
0891         " %s\n", tpg->tpgt, (a->tpg_enabled_sendtargets) ? "ON" : "OFF");
0892 
0893     return 0;
0894 }
0895 
0896 int iscsit_ta_login_keys_workaround(
0897     struct iscsi_portal_group *tpg,
0898     u32 flag)
0899 {
0900     struct iscsi_tpg_attrib *a = &tpg->tpg_attrib;
0901 
0902     if ((flag != 0) && (flag != 1)) {
0903         pr_err("Illegal value %d\n", flag);
0904         return -EINVAL;
0905     }
0906 
0907     a->login_keys_workaround = flag;
0908     pr_debug("iSCSI_TPG[%hu] - TPG enabled bit for login keys workaround: %s ",
0909         tpg->tpgt, (a->login_keys_workaround) ? "ON" : "OFF");
0910 
0911     return 0;
0912 }