<commit_before>/*******************************************************************************
 * This file contains iSCSI Target Portal Group related functions.
 *
 * (c) Copyright 2007-2013 Datera, Inc.
 *
 * Author: <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 ******************************************************************************/

#include <target/target_core_base.h>
#include <target/target_core_fabric.h>
#include <target/target_core_configfs.h>

#include "iscsi_target_core.h"
#include "iscsi_target_erl0.h"
#include "iscsi_target_login.h"
#include "iscsi_target_nodeattrib.h"
#include "iscsi_target_tpg.h"
#include "iscsi_target_util.h"
#include "iscsi_target.h"
#include "iscsi_target_parameters.h"

#include <target/iscsi/iscsi_transport.h>

struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
{
	struct iscsi_portal_group *tpg;

	tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
	if (!tpg) {
		pr_err("Unable to allocate struct iscsi_portal_group\n");
		return NULL;
	}

	tpg->tpgt = tpgt;
	tpg->tpg_state = TPG_STATE_FREE;
	tpg->tpg_tiqn = tiqn;
	INIT_LIST_HEAD(&tpg->tpg_gnp_list);
	INIT_LIST_HEAD(&tpg->tpg_list);
	mutex_init(&tpg->tpg_access_lock);
	sema_init(&tpg->np_login_sem, 1);
	spin_lock_init(&tpg->tpg_state_lock);
	spin_lock_init(&tpg->tpg_np_lock);

	return tpg;
}

static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);

int iscsit_load_discovery_tpg(void)
{
	struct iscsi_param *param;
	struct iscsi_portal_group *tpg;
	int ret;

	tpg = iscsit_alloc_portal_group(NULL, 1);
	if (!tpg) {
		pr_err("Unable to allocate struct iscsi_portal_group\n");
		return -1;
	}

	ret = core_tpg_register(
			&lio_target_fabric_configfs->tf_ops,
			NULL, &tpg->tpg_se_tpg, tpg,
			TRANSPORT_TPG_TYPE_DISCOVERY);
	if (ret < 0) {
		kfree(tpg);
		return -1;
	}

	tpg->sid = 1; /* First Assigned LIO Session ID */
	iscsit_set_default_tpg_attribs(tpg);

	if (iscsi_create_default_params(&tpg->param_list) < 0)
		goto out;
	/*
	 * By default we disable authentication for discovery sessions,
	 * this can be changed with:
	 *
	 * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
	 */
	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
	if (!param)
		goto out;

	if (iscsi_update_param_value(param, "CHAP,None") < 0)
		goto out;

	tpg->tpg_attrib.authentication = 0;

	spin_lock(&tpg->tpg_state_lock);
	tpg->tpg_state  = TPG_STATE_ACTIVE;
	spin_unlock(&tpg->tpg_state_lock);

	iscsit_global->discovery_tpg = tpg;
	pr_debug("CORE[0] - Allocated Discovery TPG\n");

	return 0;
out:
	if (tpg->sid == 1)
		core_tpg_deregister(&tpg->tpg_se_tpg);
	kfree(tpg);
	return -1;
}

void iscsit_release_discovery_tpg(void)
{
	struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;

	if (!tpg)
		return;

	core_tpg_deregister(&tpg->tpg_se_tpg);

	kfree(tpg);
	iscsit_global->discovery_tpg = NULL;
}

struct iscsi_portal_group *iscsit_get_tpg_from_np(
	struct iscsi_tiqn *tiqn,
	struct iscsi_np *np,
	struct iscsi_tpg_np **tpg_np_out)
{
	struct iscsi_portal_group *tpg = NULL;
	struct iscsi_tpg_np *tpg_np;

	spin_lock(&tiqn->tiqn_tpg_lock);
	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {

		spin_lock(&tpg->tpg_state_lock);
		if (tpg->tpg_state == TPG_STATE_FREE) {
			spin_unlock(&tpg->tpg_state_lock);
			continue;
		}
		spin_unlock(&tpg->tpg_state_lock);<commit_msg>iscsi-target: Fix iscsit_get_tpg_from_np tpg_state bug<commit_after>/*******************************************************************************
 * This file contains iSCSI Target Portal Group related functions.
 *
 * (c) Copyright 2007-2013 Datera, Inc.
 *
 * Author: Nich<NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 ******************************************************************************/

#include <target/target_core_base.h>
#include <target/target_core_fabric.h>
#include <target/target_core_configfs.h>

#include "iscsi_target_core.h"
#include "iscsi_target_erl0.h"
#include "iscsi_target_login.h"
#include "iscsi_target_nodeattrib.h"
#include "iscsi_target_tpg.h"
#include "iscsi_target_util.h"
#include "iscsi_target.h"
#include "iscsi_target_parameters.h"

#include <target/iscsi/iscsi_transport.h>

struct iscsi_portal_group *iscsit_alloc_portal_group(struct iscsi_tiqn *tiqn, u16 tpgt)
{
	struct iscsi_portal_group *tpg;

	tpg = kzalloc(sizeof(struct iscsi_portal_group), GFP_KERNEL);
	if (!tpg) {
		pr_err("Unable to allocate struct iscsi_portal_group\n");
		return NULL;
	}

	tpg->tpgt = tpgt;
	tpg->tpg_state = TPG_STATE_FREE;
	tpg->tpg_tiqn = tiqn;
	INIT_LIST_HEAD(&tpg->tpg_gnp_list);
	INIT_LIST_HEAD(&tpg->tpg_list);
	mutex_init(&tpg->tpg_access_lock);
	sema_init(&tpg->np_login_sem, 1);
	spin_lock_init(&tpg->tpg_state_lock);
	spin_lock_init(&tpg->tpg_np_lock);

	return tpg;
}

static void iscsit_set_default_tpg_attribs(struct iscsi_portal_group *);

int iscsit_load_discovery_tpg(void)
{
	struct iscsi_param *param;
	struct iscsi_portal_group *tpg;
	int ret;

	tpg = iscsit_alloc_portal_group(NULL, 1);
	if (!tpg) {
		pr_err("Unable to allocate struct iscsi_portal_group\n");
		return -1;
	}

	ret = core_tpg_register(
			&lio_target_fabric_configfs->tf_ops,
			NULL, &tpg->tpg_se_tpg, tpg,
			TRANSPORT_TPG_TYPE_DISCOVERY);
	if (ret < 0) {
		kfree(tpg);
		return -1;
	}

	tpg->sid = 1; /* First Assigned LIO Session ID */
	iscsit_set_default_tpg_attribs(tpg);

	if (iscsi_create_default_params(&tpg->param_list) < 0)
		goto out;
	/*
	 * By default we disable authentication for discovery sessions,
	 * this can be changed with:
	 *
	 * /sys/kernel/config/target/iscsi/discovery_auth/enforce_discovery_auth
	 */
	param = iscsi_find_param_from_key(AUTHMETHOD, tpg->param_list);
	if (!param)
		goto out;

	if (iscsi_update_param_value(param, "CHAP,None") < 0)
		goto out;

	tpg->tpg_attrib.authentication = 0;

	spin_lock(&tpg->tpg_state_lock);
	tpg->tpg_state  = TPG_STATE_ACTIVE;
	spin_unlock(&tpg->tpg_state_lock);

	iscsit_global->discovery_tpg = tpg;
	pr_debug("CORE[0] - Allocated Discovery TPG\n");

	return 0;
out:
	if (tpg->sid == 1)
		core_tpg_deregister(&tpg->tpg_se_tpg);
	kfree(tpg);
	return -1;
}

void iscsit_release_discovery_tpg(void)
{
	struct iscsi_portal_group *tpg = iscsit_global->discovery_tpg;

	if (!tpg)
		return;

	core_tpg_deregister(&tpg->tpg_se_tpg);

	kfree(tpg);
	iscsit_global->discovery_tpg = NULL;
}

struct iscsi_portal_group *iscsit_get_tpg_from_np(
	struct iscsi_tiqn *tiqn,
	struct iscsi_np *np,
	struct iscsi_tpg_np **tpg_np_out)
{
	struct iscsi_portal_group *tpg = NULL;
	struct iscsi_tpg_np *tpg_np;

	spin_lock(&tiqn->tiqn_tpg_lock);
	list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {

		spin_lock(&tpg->tpg_state_lock);
		if (tpg->tpg_state != TPG_STATE_ACTIVE) {
			spin_unlock(&tpg->tpg_state_lock);
			continue;
		}
		spin_unlock(&tpg->tpg_state_lock);
{'fraction_non_alphanumeric': 0.14929975587819608, 'fraction_numerical': 0.0064242579982012075, 'mean_word_length': 5.585448392554992, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1545535', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3026, 'n_words': 609}