target_core_configfs.c 88.7 KB
Newer Older
1 2 3 4 5
/*******************************************************************************
 * Filename:  target_core_configfs.c
 *
 * This file contains ConfigFS logic for the Generic Target Engine project.
 *
6
 * (c) Copyright 2008-2013 Datera, Inc.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Nicholas A. Bellinger <nab@kernel.org>
 *
 * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
 *
 * 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 <linux/module.h>
#include <linux/moduleparam.h>
#include <generated/utsrelease.h>
#include <linux/utsname.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/unistd.h>
#include <linux/string.h>
#include <linux/parser.h>
#include <linux/syscalls.h>
#include <linux/configfs.h>
38
#include <linux/spinlock.h>
39 40

#include <target/target_core_base.h>
41 42
#include <target/target_core_backend.h>
#include <target/target_core_fabric.h>
43

C
Christoph Hellwig 已提交
44
#include "target_core_internal.h"
45 46 47
#include "target_core_alua.h"
#include "target_core_pr.h"
#include "target_core_rd.h"
48
#include "target_core_xcopy.h"
49

50
#define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
51
static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
52
{									\
53
	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
54 55 56 57
									\
	cit->ct_item_ops = _item_ops;					\
	cit->ct_group_ops = _group_ops;					\
	cit->ct_attrs = _attrs;						\
58 59 60 61 62 63 64 65 66 67 68 69 70
	cit->ct_owner = tb->ops->owner;					\
	pr_debug("Setup generic %s\n", __stringify(_name));		\
}

#define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
{									\
	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
									\
	cit->ct_item_ops = _item_ops;					\
	cit->ct_group_ops = _group_ops;					\
	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
	cit->ct_owner = tb->ops->owner;					\
71 72 73
	pr_debug("Setup generic %s\n", __stringify(_name));		\
}

74 75
extern struct t10_alua_lu_gp *default_lu_gp;

76 77
static LIST_HEAD(g_tf_list);
static DEFINE_MUTEX(g_tf_lock);
78

79 80 81 82
static struct config_group target_core_hbagroup;
static struct config_group alua_group;
static struct config_group alua_lu_gps_group;

83 84 85 86 87 88 89 90 91
static inline struct se_hba *
item_to_hba(struct config_item *item)
{
	return container_of(to_config_group(item), struct se_hba, hba_group);
}

/*
 * Attributes for /sys/kernel/config/target/
 */
92 93
static ssize_t target_core_item_version_show(struct config_item *item,
		char *page)
94 95
{
	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
96
		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
97 98 99
		utsname()->sysname, utsname()->machine);
}

100
CONFIGFS_ATTR_RO(target_core_item_, version);
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
static char db_root_stage[DB_ROOT_LEN];

static ssize_t target_core_item_dbroot_show(struct config_item *item,
					    char *page)
{
	return sprintf(page, "%s\n", db_root);
}

static ssize_t target_core_item_dbroot_store(struct config_item *item,
					const char *page, size_t count)
{
	ssize_t read_bytes;
	struct file *fp;

	mutex_lock(&g_tf_lock);
	if (!list_empty(&g_tf_list)) {
		mutex_unlock(&g_tf_lock);
		pr_err("db_root: cannot be changed: target drivers registered");
		return -EINVAL;
	}

	if (count > (DB_ROOT_LEN - 1)) {
		mutex_unlock(&g_tf_lock);
		pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
		       (int)count, DB_ROOT_LEN - 1);
		return -EINVAL;
	}

	read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
	if (!read_bytes) {
		mutex_unlock(&g_tf_lock);
		return -EINVAL;
	}
	if (db_root_stage[read_bytes - 1] == '\n')
		db_root_stage[read_bytes - 1] = '\0';

	/* validate new db root before accepting it */
	fp = filp_open(db_root_stage, O_RDONLY, 0);
	if (IS_ERR(fp)) {
		mutex_unlock(&g_tf_lock);
		pr_err("db_root: cannot open: %s\n", db_root_stage);
		return -EINVAL;
	}
A
Al Viro 已提交
146
	if (!S_ISDIR(file_inode(fp)->i_mode)) {
147
		filp_close(fp, NULL);
148 149 150 151
		mutex_unlock(&g_tf_lock);
		pr_err("db_root: not a directory: %s\n", db_root_stage);
		return -EINVAL;
	}
152
	filp_close(fp, NULL);
153 154 155 156 157 158 159 160 161 162

	strncpy(db_root, db_root_stage, read_bytes);

	mutex_unlock(&g_tf_lock);

	return read_bytes;
}

CONFIGFS_ATTR(target_core_item_, dbroot);

163 164 165 166 167
static struct target_fabric_configfs *target_core_get_fabric(
	const char *name)
{
	struct target_fabric_configfs *tf;

168
	if (!name)
169 170 171 172
		return NULL;

	mutex_lock(&g_tf_lock);
	list_for_each_entry(tf, &g_tf_list, tf_list) {
173
		if (!strcmp(tf->tf_ops->name, name)) {
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
			atomic_inc(&tf->tf_access_cnt);
			mutex_unlock(&g_tf_lock);
			return tf;
		}
	}
	mutex_unlock(&g_tf_lock);

	return NULL;
}

/*
 * Called from struct target_core_group_ops->make_group()
 */
static struct config_group *target_core_register_fabric(
	struct config_group *group,
	const char *name)
{
	struct target_fabric_configfs *tf;
	int ret;

194
	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
195
			" %s\n", group, name);
196 197 198

	tf = target_core_get_fabric(name);
	if (!tf) {
199 200
		pr_debug("target_core_register_fabric() trying autoload for %s\n",
			 name);
201

202
		/*
203 204
		 * Below are some hardcoded request_module() calls to automatically
		 * local fabric modules when the following is called:
205
		 *
206
		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
207
		 *
208 209 210
		 * Note that this does not limit which TCM fabric module can be
		 * registered, but simply provids auto loading logic for modules with
		 * mkdir(2) system calls with known TCM fabric modules.
211
		 */
212 213 214 215 216 217 218 219 220 221

		if (!strncmp(name, "iscsi", 5)) {
			/*
			 * Automatically load the LIO Target fabric module when the
			 * following is called:
			 *
			 * mkdir -p $CONFIGFS/target/iscsi
			 */
			ret = request_module("iscsi_target_mod");
			if (ret < 0) {
222 223
				pr_debug("request_module() failed for"
				         " iscsi_target_mod.ko: %d\n", ret);
224 225 226 227 228 229 230 231 232 233 234
				return ERR_PTR(-EINVAL);
			}
		} else if (!strncmp(name, "loopback", 8)) {
			/*
			 * Automatically load the tcm_loop fabric module when the
			 * following is called:
			 *
			 * mkdir -p $CONFIGFS/target/loopback
			 */
			ret = request_module("tcm_loop");
			if (ret < 0) {
235 236
				pr_debug("request_module() failed for"
				         " tcm_loop.ko: %d\n", ret);
237 238
				return ERR_PTR(-EINVAL);
			}
239
		}
240 241

		tf = target_core_get_fabric(name);
242 243
	}

244
	if (!tf) {
245 246
		pr_debug("target_core_get_fabric() failed for %s\n",
		         name);
247 248
		return ERR_PTR(-EINVAL);
	}
249
	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
250
			" %s\n", tf->tf_ops->name);
251 252 253 254
	/*
	 * On a successful target_core_get_fabric() look, the returned
	 * struct target_fabric_configfs *tf will contain a usage reference.
	 */
255
	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
256
			&tf->tf_wwn_cit);
257

258
	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
259

260
	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
261
			&tf->tf_discovery_cit);
262
	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
263

264
	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
265 266 267 268 269 270 271 272 273 274 275 276 277 278
			" %s\n", tf->tf_group.cg_item.ci_name);
	return &tf->tf_group;
}

/*
 * Called from struct target_core_group_ops->drop_item()
 */
static void target_core_deregister_fabric(
	struct config_group *group,
	struct config_item *item)
{
	struct target_fabric_configfs *tf = container_of(
		to_config_group(item), struct target_fabric_configfs, tf_group);

279
	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
280 281
		" tf list\n", config_item_name(item));

282
	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
283
			" %s\n", tf->tf_ops->name);
284 285
	atomic_dec(&tf->tf_access_cnt);

286
	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
287 288
			" %s\n", config_item_name(item));

289
	configfs_remove_default_groups(&tf->tf_group);
290 291 292 293 294 295 296 297 298 299 300 301 302
	config_item_put(item);
}

static struct configfs_group_operations target_core_fabric_group_ops = {
	.make_group	= &target_core_register_fabric,
	.drop_item	= &target_core_deregister_fabric,
};

/*
 * All item attributes appearing in /sys/kernel/target/ appear here.
 */
static struct configfs_attribute *target_core_fabric_item_attrs[] = {
	&target_core_item_attr_version,
303
	&target_core_item_attr_dbroot,
304 305 306 307 308 309
	NULL,
};

/*
 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
 */
310
static const struct config_item_type target_core_fabrics_item = {
311 312 313 314 315 316 317 318 319 320 321 322 323 324
	.ct_group_ops	= &target_core_fabric_group_ops,
	.ct_attrs	= target_core_fabric_item_attrs,
	.ct_owner	= THIS_MODULE,
};

static struct configfs_subsystem target_core_fabrics = {
	.su_group = {
		.cg_item = {
			.ci_namebuf = "target",
			.ci_type = &target_core_fabrics_item,
		},
	},
};

325 326 327 328 329 330 331 332
int target_depend_item(struct config_item *item)
{
	return configfs_depend_item(&target_core_fabrics, item);
}
EXPORT_SYMBOL(target_depend_item);

void target_undepend_item(struct config_item *item)
{
333
	return configfs_undepend_item(item);
334 335
}
EXPORT_SYMBOL(target_undepend_item);
336 337 338 339 340

/*##############################################################################
// Start functions called by external Target Fabrics Modules
//############################################################################*/

341
static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
342
{
343 344 345
	if (!tfo->name) {
		pr_err("Missing tfo->name\n");
		return -EINVAL;
346
	}
347
	if (strlen(tfo->name) >= TARGET_FABRIC_NAME_SIZE) {
348
		pr_err("Passed name: %s exceeds TARGET_FABRIC"
349 350
			"_NAME_SIZE\n", tfo->name);
		return -EINVAL;
351
	}
352 353
	if (!tfo->get_fabric_name) {
		pr_err("Missing tfo->get_fabric_name()\n");
354 355
		return -EINVAL;
	}
356 357
	if (!tfo->tpg_get_wwn) {
		pr_err("Missing tfo->tpg_get_wwn()\n");
358 359
		return -EINVAL;
	}
360 361
	if (!tfo->tpg_get_tag) {
		pr_err("Missing tfo->tpg_get_tag()\n");
362 363
		return -EINVAL;
	}
364 365
	if (!tfo->tpg_check_demo_mode) {
		pr_err("Missing tfo->tpg_check_demo_mode()\n");
366 367
		return -EINVAL;
	}
368 369
	if (!tfo->tpg_check_demo_mode_cache) {
		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
370 371
		return -EINVAL;
	}
372 373
	if (!tfo->tpg_check_demo_mode_write_protect) {
		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
374 375
		return -EINVAL;
	}
376 377
	if (!tfo->tpg_check_prod_mode_write_protect) {
		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
378 379
		return -EINVAL;
	}
380 381
	if (!tfo->tpg_get_inst_index) {
		pr_err("Missing tfo->tpg_get_inst_index()\n");
382 383
		return -EINVAL;
	}
384
	if (!tfo->release_cmd) {
385
		pr_err("Missing tfo->release_cmd()\n");
386 387
		return -EINVAL;
	}
388 389
	if (!tfo->sess_get_index) {
		pr_err("Missing tfo->sess_get_index()\n");
390 391
		return -EINVAL;
	}
392 393
	if (!tfo->write_pending) {
		pr_err("Missing tfo->write_pending()\n");
394 395
		return -EINVAL;
	}
396 397
	if (!tfo->write_pending_status) {
		pr_err("Missing tfo->write_pending_status()\n");
398 399
		return -EINVAL;
	}
400 401
	if (!tfo->set_default_node_attributes) {
		pr_err("Missing tfo->set_default_node_attributes()\n");
402 403
		return -EINVAL;
	}
404 405
	if (!tfo->get_cmd_state) {
		pr_err("Missing tfo->get_cmd_state()\n");
406 407
		return -EINVAL;
	}
408 409
	if (!tfo->queue_data_in) {
		pr_err("Missing tfo->queue_data_in()\n");
410 411
		return -EINVAL;
	}
412 413
	if (!tfo->queue_status) {
		pr_err("Missing tfo->queue_status()\n");
414 415
		return -EINVAL;
	}
416 417
	if (!tfo->queue_tm_rsp) {
		pr_err("Missing tfo->queue_tm_rsp()\n");
418 419
		return -EINVAL;
	}
420 421 422 423
	if (!tfo->aborted_task) {
		pr_err("Missing tfo->aborted_task()\n");
		return -EINVAL;
	}
424 425 426 427
	if (!tfo->check_stop_free) {
		pr_err("Missing tfo->check_stop_free()\n");
		return -EINVAL;
	}
428 429 430 431 432
	/*
	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
	 * target_core_fabric_configfs.c WWN+TPG group context code.
	 */
433 434
	if (!tfo->fabric_make_wwn) {
		pr_err("Missing tfo->fabric_make_wwn()\n");
435 436
		return -EINVAL;
	}
437 438
	if (!tfo->fabric_drop_wwn) {
		pr_err("Missing tfo->fabric_drop_wwn()\n");
439 440
		return -EINVAL;
	}
441 442
	if (!tfo->fabric_make_tpg) {
		pr_err("Missing tfo->fabric_make_tpg()\n");
443 444
		return -EINVAL;
	}
445 446
	if (!tfo->fabric_drop_tpg) {
		pr_err("Missing tfo->fabric_drop_tpg()\n");
447 448 449 450 451 452
		return -EINVAL;
	}

	return 0;
}

453
int target_register_template(const struct target_core_fabric_ops *fo)
454
{
455
	struct target_fabric_configfs *tf;
456 457
	int ret;

458 459 460 461 462
	ret = target_fabric_tf_ops_check(fo);
	if (ret)
		return ret;

	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
463
	if (!tf) {
464 465
		pr_err("%s: could not allocate memory!\n", __func__);
		return -ENOMEM;
466 467
	}

468 469
	INIT_LIST_HEAD(&tf->tf_list);
	atomic_set(&tf->tf_access_cnt, 0);
470
	tf->tf_ops = fo;
471 472 473 474 475 476
	target_fabric_setup_cits(tf);

	mutex_lock(&g_tf_lock);
	list_add_tail(&tf->tf_list, &g_tf_list);
	mutex_unlock(&g_tf_lock);

477 478
	return 0;
}
479
EXPORT_SYMBOL(target_register_template);
480

481
void target_unregister_template(const struct target_core_fabric_ops *fo)
482
{
483
	struct target_fabric_configfs *t;
484 485

	mutex_lock(&g_tf_lock);
486
	list_for_each_entry(t, &g_tf_list, tf_list) {
487
		if (!strcmp(t->tf_ops->name, fo->name)) {
488 489
			BUG_ON(atomic_read(&t->tf_access_cnt));
			list_del(&t->tf_list);
490 491 492 493 494 495 496
			mutex_unlock(&g_tf_lock);
			/*
			 * Wait for any outstanding fabric se_deve_entry->rcu_head
			 * callbacks to complete post kfree_rcu(), before allowing
			 * fabric driver unload of TFO->module to proceed.
			 */
			rcu_barrier();
497
			kfree(t);
498
			return;
499
		}
500 501 502
	}
	mutex_unlock(&g_tf_lock);
}
503
EXPORT_SYMBOL(target_unregister_template);
504 505 506 507 508

/*##############################################################################
// Stop functions called by external Target Fabrics Modules
//############################################################################*/

509 510 511 512 513 514
static inline struct se_dev_attrib *to_attrib(struct config_item *item)
{
	return container_of(to_config_group(item), struct se_dev_attrib,
			da_group);
}

515
/* Start functions for struct config_item_type tb_dev_attrib_cit */
516 517
#define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
static ssize_t _name##_show(struct config_item *item, char *page)	\
518
{									\
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
}

DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_format);
536
DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
537 538 539 540 541 542 543 544 545 546 547 548 549 550
DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
DEF_CONFIGFS_ATTRIB_SHOW(block_size);
DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
551
DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
552 553 554 555
DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);

#define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
static ssize_t _name##_store(struct config_item *item, const char *page,\
556
		size_t count)						\
557
{									\
558
	struct se_dev_attrib *da = to_attrib(item);			\
559
	u32 val;							\
560 561
	int ret;							\
									\
562 563 564 565 566 567 568
	ret = kstrtou32(page, 0, &val);					\
	if (ret < 0)							\
		return ret;						\
	da->_name = val;						\
	return count;							\
}

569 570 571 572 573
DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
574

575 576
#define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
static ssize_t _name##_store(struct config_item *item, const char *page,	\
577 578
		size_t count)						\
{									\
579
	struct se_dev_attrib *da = to_attrib(item);			\
580 581
	bool flag;							\
	int ret;							\
582
									\
583 584 585 586 587 588 589
	ret = strtobool(page, &flag);					\
	if (ret < 0)							\
		return ret;						\
	da->_name = flag;						\
	return count;							\
}

590 591 592 593 594
DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
595

596 597
#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
static ssize_t _name##_store(struct config_item *item, const char *page,\
598 599 600
		size_t count)						\
{									\
	printk_once(KERN_WARNING					\
601 602
		"ignoring deprecated %s attribute\n",			\
		__stringify(_name));					\
603 604 605
	return count;							\
}

606 607
DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
608 609 610 611 612 613 614 615 616 617 618 619 620 621

static void dev_set_t10_wwn_model_alias(struct se_device *dev)
{
	const char *configname;

	configname = config_item_name(&dev->dev_group.cg_item);
	if (strlen(configname) >= 16) {
		pr_warn("dev[%p]: Backstore name '%s' is too long for "
			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
			configname);
	}
	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
}

622
static ssize_t emulate_model_alias_store(struct config_item *item,
623 624
		const char *page, size_t count)
{
625
	struct se_dev_attrib *da = to_attrib(item);
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	struct se_device *dev = da->da_dev;
	bool flag;
	int ret;

	if (dev->export_count) {
		pr_err("dev[%p]: Unable to change model alias"
			" while export_count is %d\n",
			dev, dev->export_count);
		return -EINVAL;
	}

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (flag) {
		dev_set_t10_wwn_model_alias(dev);
	} else {
		strncpy(&dev->t10_wwn.model[0],
			dev->transport->inquiry_prod, 16);
	}
	da->emulate_model_alias = flag;
	return count;
}

651
static ssize_t emulate_write_cache_store(struct config_item *item,
652 653
		const char *page, size_t count)
{
654
	struct se_dev_attrib *da = to_attrib(item);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (flag && da->da_dev->transport->get_write_cache) {
		pr_err("emulate_write_cache not supported for this device\n");
		return -EINVAL;
	}

	da->emulate_write_cache = flag;
	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
			da->da_dev, flag);
	return count;
}

673
static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
674 675
		const char *page, size_t count)
{
676
	struct se_dev_attrib *da = to_attrib(item);
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	u32 val;
	int ret;

	ret = kstrtou32(page, 0, &val);
	if (ret < 0)
		return ret;

	if (val != 0 && val != 1 && val != 2) {
		pr_err("Illegal value %d\n", val);
		return -EINVAL;
	}

	if (da->da_dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device"
			" UA_INTRLCK_CTRL while export_count is %d\n",
			da->da_dev, da->da_dev->export_count);
		return -EINVAL;
	}
	da->emulate_ua_intlck_ctrl = val;
	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
		da->da_dev, val);
	return count;
}

701
static ssize_t emulate_tas_store(struct config_item *item,
702 703
		const char *page, size_t count)
{
704
	struct se_dev_attrib *da = to_attrib(item);
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (da->da_dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device TAS while"
			" export_count is %d\n",
			da->da_dev, da->da_dev->export_count);
		return -EINVAL;
	}
	da->emulate_tas = flag;
	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
		da->da_dev, flag ? "Enabled" : "Disabled");

	return count;
}

725
static ssize_t emulate_tpu_store(struct config_item *item,
726 727
		const char *page, size_t count)
{
728
	struct se_dev_attrib *da = to_attrib(item);
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	/*
	 * We expect this value to be non-zero when generic Block Layer
	 * Discard supported is detected iblock_create_virtdevice().
	 */
	if (flag && !da->max_unmap_block_desc_count) {
		pr_err("Generic Block Discard not supported\n");
		return -ENOSYS;
	}

	da->emulate_tpu = flag;
	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
		da->da_dev, flag);
	return count;
}

751
static ssize_t emulate_tpws_store(struct config_item *item,
752 753
		const char *page, size_t count)
{
754
	struct se_dev_attrib *da = to_attrib(item);
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	/*
	 * We expect this value to be non-zero when generic Block Layer
	 * Discard supported is detected iblock_create_virtdevice().
	 */
	if (flag && !da->max_unmap_block_desc_count) {
		pr_err("Generic Block Discard not supported\n");
		return -ENOSYS;
	}

	da->emulate_tpws = flag;
	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
				da->da_dev, flag);
	return count;
}

777
static ssize_t pi_prot_type_store(struct config_item *item,
778 779
		const char *page, size_t count)
{
780
	struct se_dev_attrib *da = to_attrib(item);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
	int old_prot = da->pi_prot_type, ret;
	struct se_device *dev = da->da_dev;
	u32 flag;

	ret = kstrtou32(page, 0, &flag);
	if (ret < 0)
		return ret;

	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
		pr_err("Illegal value %d for pi_prot_type\n", flag);
		return -EINVAL;
	}
	if (flag == 2) {
		pr_err("DIF TYPE2 protection currently not supported\n");
		return -ENOSYS;
	}
	if (da->hw_pi_prot_type) {
		pr_warn("DIF protection enabled on underlying hardware,"
			" ignoring\n");
		return count;
	}
	if (!dev->transport->init_prot || !dev->transport->free_prot) {
		/* 0 is only allowed value for non-supporting backends */
		if (flag == 0)
805
			return count;
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

		pr_err("DIF protection not supported by backend: %s\n",
		       dev->transport->name);
		return -ENOSYS;
	}
	if (!(dev->dev_flags & DF_CONFIGURED)) {
		pr_err("DIF protection requires device to be configured\n");
		return -ENODEV;
	}
	if (dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device PROT type while"
		       " export_count is %d\n", dev, dev->export_count);
		return -EINVAL;
	}

	da->pi_prot_type = flag;

	if (flag && !old_prot) {
		ret = dev->transport->init_prot(dev);
		if (ret) {
			da->pi_prot_type = old_prot;
827
			da->pi_prot_verify = (bool) da->pi_prot_type;
828 829 830 831 832 833 834
			return ret;
		}

	} else if (!flag && old_prot) {
		dev->transport->free_prot(dev);
	}

835
	da->pi_prot_verify = (bool) da->pi_prot_type;
836 837 838 839
	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
	return count;
}

840
static ssize_t pi_prot_format_store(struct config_item *item,
841 842
		const char *page, size_t count)
{
843
	struct se_dev_attrib *da = to_attrib(item);
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
	struct se_device *dev = da->da_dev;
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (!flag)
		return count;

	if (!dev->transport->format_prot) {
		pr_err("DIF protection format not supported by backend %s\n",
		       dev->transport->name);
		return -ENOSYS;
	}
	if (!(dev->dev_flags & DF_CONFIGURED)) {
		pr_err("DIF protection format requires device to be configured\n");
		return -ENODEV;
	}
	if (dev->export_count) {
		pr_err("dev[%p]: Unable to format SE Device PROT type while"
		       " export_count is %d\n", dev, dev->export_count);
		return -EINVAL;
	}

	ret = dev->transport->format_prot(dev);
	if (ret)
		return ret;

	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
	return count;
}

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
static ssize_t pi_prot_verify_store(struct config_item *item,
		const char *page, size_t count)
{
	struct se_dev_attrib *da = to_attrib(item);
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (!flag) {
		da->pi_prot_verify = flag;
		return count;
	}
	if (da->hw_pi_prot_type) {
		pr_warn("DIF protection enabled on underlying hardware,"
			" ignoring\n");
		return count;
	}
	if (!da->pi_prot_type) {
		pr_warn("DIF protection not supported by backend, ignoring\n");
		return count;
	}
	da->pi_prot_verify = flag;

	return count;
}

907
static ssize_t force_pr_aptpl_store(struct config_item *item,
908 909
		const char *page, size_t count)
{
910
	struct se_dev_attrib *da = to_attrib(item);
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;
	if (da->da_dev->export_count) {
		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
		       " export_count is %d\n",
		       da->da_dev, da->da_dev->export_count);
		return -EINVAL;
	}

	da->force_pr_aptpl = flag;
	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
	return count;
}

929
static ssize_t emulate_rest_reord_store(struct config_item *item,
930 931
		const char *page, size_t count)
{
932
	struct se_dev_attrib *da = to_attrib(item);
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (flag != 0) {
		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
			" reordering not implemented\n", da->da_dev);
		return -ENOSYS;
	}
	da->emulate_rest_reord = flag;
	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
		da->da_dev, flag);
	return count;
}

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
static ssize_t unmap_zeroes_data_store(struct config_item *item,
		const char *page, size_t count)
{
	struct se_dev_attrib *da = to_attrib(item);
	bool flag;
	int ret;

	ret = strtobool(page, &flag);
	if (ret < 0)
		return ret;

	if (da->da_dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device"
		       " unmap_zeroes_data while export_count is %d\n",
		       da->da_dev, da->da_dev->export_count);
		return -EINVAL;
	}
	/*
	 * We expect this value to be non-zero when generic Block Layer
	 * Discard supported is detected iblock_configure_device().
	 */
	if (flag && !da->max_unmap_block_desc_count) {
		pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set"
		       " because max_unmap_block_desc_count is zero\n",
		       da->da_dev);
976
		return -ENOSYS;
977 978 979 980
	}
	da->unmap_zeroes_data = flag;
	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
		 da->da_dev, flag);
981
	return count;
982 983
}

984 985 986
/*
 * Note, this can only be called on unexported SE Device Object.
 */
987
static ssize_t queue_depth_store(struct config_item *item,
988 989
		const char *page, size_t count)
{
990
	struct se_dev_attrib *da = to_attrib(item);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
	struct se_device *dev = da->da_dev;
	u32 val;
	int ret;

	ret = kstrtou32(page, 0, &val);
	if (ret < 0)
		return ret;

	if (dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device TCQ while"
			" export_count is %d\n",
			dev, dev->export_count);
		return -EINVAL;
	}
	if (!val) {
		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
		return -EINVAL;
	}

	if (val > dev->dev_attrib.queue_depth) {
		if (val > dev->dev_attrib.hw_queue_depth) {
			pr_err("dev[%p]: Passed queue_depth:"
				" %u exceeds TCM/SE_Device MAX"
				" TCQ: %u\n", dev, val,
				dev->dev_attrib.hw_queue_depth);
			return -EINVAL;
		}
	}
	da->queue_depth = dev->queue_depth = val;
	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
	return count;
}

1024
static ssize_t optimal_sectors_store(struct config_item *item,
1025 1026
		const char *page, size_t count)
{
1027
	struct se_dev_attrib *da = to_attrib(item);
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	u32 val;
	int ret;

	ret = kstrtou32(page, 0, &val);
	if (ret < 0)
		return ret;

	if (da->da_dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device"
			" optimal_sectors while export_count is %d\n",
			da->da_dev, da->da_dev->export_count);
		return -EINVAL;
	}
	if (val > da->hw_max_sectors) {
		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
			" greater than hw_max_sectors: %u\n",
			da->da_dev, val, da->hw_max_sectors);
		return -EINVAL;
	}

	da->optimal_sectors = val;
	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
			da->da_dev, val);
	return count;
1052 1053
}

1054
static ssize_t block_size_store(struct config_item *item,
1055 1056
		const char *page, size_t count)
{
1057
	struct se_dev_attrib *da = to_attrib(item);
1058 1059 1060 1061 1062 1063
	u32 val;
	int ret;

	ret = kstrtou32(page, 0, &val);
	if (ret < 0)
		return ret;
1064

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	if (da->da_dev->export_count) {
		pr_err("dev[%p]: Unable to change SE Device block_size"
			" while export_count is %d\n",
			da->da_dev, da->da_dev->export_count);
		return -EINVAL;
	}

	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
		pr_err("dev[%p]: Illegal value for block_device: %u"
			" for SE device, must be 512, 1024, 2048 or 4096\n",
			da->da_dev, val);
		return -EINVAL;
	}

	da->block_size = val;
	if (da->max_bytes_per_io)
		da->hw_max_sectors = da->max_bytes_per_io / val;

	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
			da->da_dev, val);
	return count;
}
1087

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
static ssize_t alua_support_show(struct config_item *item, char *page)
{
	struct se_dev_attrib *da = to_attrib(item);
	u8 flags = da->da_dev->transport->transport_flags;

	return snprintf(page, PAGE_SIZE, "%d\n",
			flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
}

static ssize_t pgr_support_show(struct config_item *item, char *page)
{
	struct se_dev_attrib *da = to_attrib(item);
	u8 flags = da->da_dev->transport->transport_flags;

	return snprintf(page, PAGE_SIZE, "%d\n",
			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
CONFIGFS_ATTR(, emulate_model_alias);
CONFIGFS_ATTR(, emulate_dpo);
CONFIGFS_ATTR(, emulate_fua_write);
CONFIGFS_ATTR(, emulate_fua_read);
CONFIGFS_ATTR(, emulate_write_cache);
CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
CONFIGFS_ATTR(, emulate_tas);
CONFIGFS_ATTR(, emulate_tpu);
CONFIGFS_ATTR(, emulate_tpws);
CONFIGFS_ATTR(, emulate_caw);
CONFIGFS_ATTR(, emulate_3pc);
CONFIGFS_ATTR(, pi_prot_type);
CONFIGFS_ATTR_RO(, hw_pi_prot_type);
CONFIGFS_ATTR(, pi_prot_format);
1120
CONFIGFS_ATTR(, pi_prot_verify);
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
CONFIGFS_ATTR(, enforce_pr_isids);
CONFIGFS_ATTR(, is_nonrot);
CONFIGFS_ATTR(, emulate_rest_reord);
CONFIGFS_ATTR(, force_pr_aptpl);
CONFIGFS_ATTR_RO(, hw_block_size);
CONFIGFS_ATTR(, block_size);
CONFIGFS_ATTR_RO(, hw_max_sectors);
CONFIGFS_ATTR(, optimal_sectors);
CONFIGFS_ATTR_RO(, hw_queue_depth);
CONFIGFS_ATTR(, queue_depth);
CONFIGFS_ATTR(, max_unmap_lba_count);
CONFIGFS_ATTR(, max_unmap_block_desc_count);
CONFIGFS_ATTR(, unmap_granularity);
CONFIGFS_ATTR(, unmap_granularity_alignment);
1135
CONFIGFS_ATTR(, unmap_zeroes_data);
1136
CONFIGFS_ATTR(, max_write_same_len);
1137 1138
CONFIGFS_ATTR_RO(, alua_support);
CONFIGFS_ATTR_RO(, pgr_support);
1139

1140 1141 1142 1143 1144 1145
/*
 * dev_attrib attributes for devices using the target core SBC/SPC
 * interpreter.  Any backend using spc_parse_cdb should be using
 * these.
 */
struct configfs_attribute *sbc_attrib_attrs[] = {
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	&attr_emulate_model_alias,
	&attr_emulate_dpo,
	&attr_emulate_fua_write,
	&attr_emulate_fua_read,
	&attr_emulate_write_cache,
	&attr_emulate_ua_intlck_ctrl,
	&attr_emulate_tas,
	&attr_emulate_tpu,
	&attr_emulate_tpws,
	&attr_emulate_caw,
	&attr_emulate_3pc,
	&attr_pi_prot_type,
	&attr_hw_pi_prot_type,
	&attr_pi_prot_format,
1160
	&attr_pi_prot_verify,
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	&attr_enforce_pr_isids,
	&attr_is_nonrot,
	&attr_emulate_rest_reord,
	&attr_force_pr_aptpl,
	&attr_hw_block_size,
	&attr_block_size,
	&attr_hw_max_sectors,
	&attr_optimal_sectors,
	&attr_hw_queue_depth,
	&attr_queue_depth,
	&attr_max_unmap_lba_count,
	&attr_max_unmap_block_desc_count,
	&attr_unmap_granularity,
	&attr_unmap_granularity_alignment,
1175
	&attr_unmap_zeroes_data,
1176
	&attr_max_write_same_len,
1177 1178
	&attr_alua_support,
	&attr_pgr_support,
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	NULL,
};
EXPORT_SYMBOL(sbc_attrib_attrs);

/*
 * Minimal dev_attrib attributes for devices passing through CDBs.
 * In this case we only provide a few read-only attributes for
 * backwards compatibility.
 */
struct configfs_attribute *passthrough_attrib_attrs[] = {
1189 1190 1191 1192
	&attr_hw_pi_prot_type,
	&attr_hw_block_size,
	&attr_hw_max_sectors,
	&attr_hw_queue_depth,
1193 1194
	&attr_alua_support,
	&attr_pgr_support,
1195 1196 1197 1198
	NULL,
};
EXPORT_SYMBOL(passthrough_attrib_attrs);

1199
TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1200
TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1201

1202
/* End functions for struct config_item_type tb_dev_attrib_cit */
1203

1204
/*  Start functions for struct config_item_type tb_dev_wwn_cit */
1205

1206 1207 1208 1209
static struct t10_wwn *to_t10_wwn(struct config_item *item)
{
	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
}
1210 1211 1212 1213

/*
 * VPD page 0x80 Unit serial
 */
1214 1215
static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
		char *page)
1216 1217
{
	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
1218
		&to_t10_wwn(item)->unit_serial[0]);
1219 1220
}

1221 1222
static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
		const char *page, size_t count)
1223
{
1224
	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1225
	struct se_device *dev = t10_wwn->t10_dev;
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
	unsigned char buf[INQUIRY_VPD_SERIAL_LEN];

	/*
	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
	 * from the struct scsi_device level firmware, do not allow
	 * VPD Unit Serial to be emulated.
	 *
	 * Note this struct scsi_device could also be emulating VPD
	 * information from its drivers/scsi LLD.  But for now we assume
	 * it is doing 'the right thing' wrt a world wide unique
	 * VPD Unit Serial Number that OS dependent multipath can depend on.
	 */
1238
	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
1239
		pr_err("Underlying SCSI device firmware provided VPD"
1240 1241 1242 1243
			" Unit Serial, ignoring request\n");
		return -EOPNOTSUPP;
	}

1244
	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
1245
		pr_err("Emulated VPD Unit Serial exceeds"
1246 1247 1248 1249 1250 1251 1252 1253 1254
		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
		return -EOVERFLOW;
	}
	/*
	 * Check to see if any active $FABRIC_MOD exports exist.  If they
	 * do exist, fail here as changing this information on the fly
	 * (underneath the initiator side OS dependent multipath code)
	 * could cause negative effects.
	 */
1255 1256 1257 1258 1259
	if (dev->export_count) {
		pr_err("Unable to set VPD Unit Serial while"
			" active %d $FABRIC_MOD exports exist\n",
			dev->export_count);
		return -EINVAL;
1260
	}
1261

1262 1263 1264 1265 1266 1267 1268 1269
	/*
	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
	 *
	 * Also, strip any newline added from the userspace
	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
	 */
	memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
1270
	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1271
			"%s", strstrip(buf));
1272
	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1273

1274
	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
1275
			" %s\n", dev->t10_wwn.unit_serial);
1276 1277 1278 1279 1280 1281 1282

	return count;
}

/*
 * VPD page 0x83 Protocol Identifier
 */
1283 1284
static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
		char *page)
1285
{
1286
	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1287 1288 1289 1290 1291 1292 1293 1294
	struct t10_vpd *vpd;
	unsigned char buf[VPD_TMP_BUF_SIZE];
	ssize_t len = 0;

	memset(buf, 0, VPD_TMP_BUF_SIZE);

	spin_lock(&t10_wwn->t10_vpd_lock);
	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
1295
		if (!vpd->protocol_identifier_set)
1296 1297 1298 1299
			continue;

		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);

1300
		if (len + strlen(buf) >= PAGE_SIZE)
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
			break;

		len += sprintf(page+len, "%s", buf);
	}
	spin_unlock(&t10_wwn->t10_vpd_lock);

	return len;
}

/*
 * Generic wrapper for dumping VPD identifiers by association.
 */
#define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
1314 1315
static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
		char *page)						\
1316
{									\
1317 1318
	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
	struct t10_vpd *vpd;						\
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
	unsigned char buf[VPD_TMP_BUF_SIZE];				\
	ssize_t len = 0;						\
									\
	spin_lock(&t10_wwn->t10_vpd_lock);				\
	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
		if (vpd->association != _assoc)				\
			continue;					\
									\
		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
1329
		if (len + strlen(buf) >= PAGE_SIZE)			\
1330 1331 1332 1333 1334
			break;						\
		len += sprintf(page+len, "%s", buf);			\
									\
		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
1335
		if (len + strlen(buf) >= PAGE_SIZE)			\
1336 1337 1338 1339 1340
			break;						\
		len += sprintf(page+len, "%s", buf);			\
									\
		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1341
		if (len + strlen(buf) >= PAGE_SIZE)			\
1342 1343 1344 1345 1346 1347 1348 1349
			break;						\
		len += sprintf(page+len, "%s", buf);			\
	}								\
	spin_unlock(&t10_wwn->t10_vpd_lock);				\
									\
	return len;							\
}

1350
/* VPD page 0x83 Association: Logical Unit */
1351
DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1352
/* VPD page 0x83 Association: Target Port */
1353
DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1354
/* VPD page 0x83 Association: SCSI Target Device */
1355 1356
DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);

1357 1358 1359 1360 1361
CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1362 1363

static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1364 1365 1366 1367 1368
	&target_wwn_attr_vpd_unit_serial,
	&target_wwn_attr_vpd_protocol_identifier,
	&target_wwn_attr_vpd_assoc_logical_unit,
	&target_wwn_attr_vpd_assoc_target_port,
	&target_wwn_attr_vpd_assoc_scsi_target_device,
1369 1370 1371
	NULL,
};

1372
TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1373

1374
/*  End functions for struct config_item_type tb_dev_wwn_cit */
1375

1376
/*  Start functions for struct config_item_type tb_dev_pr_cit */
1377

1378 1379 1380 1381 1382
static struct se_device *pr_to_dev(struct config_item *item)
{
	return container_of(to_config_group(item), struct se_device,
			dev_pr_group);
}
1383

1384 1385
static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
		char *page)
1386 1387 1388 1389 1390 1391 1392 1393
{
	struct se_node_acl *se_nacl;
	struct t10_pr_registration *pr_reg;
	char i_buf[PR_REG_ISID_ID_LEN];

	memset(i_buf, 0, PR_REG_ISID_ID_LEN);

	pr_reg = dev->dev_pr_res_holder;
1394 1395 1396
	if (!pr_reg)
		return sprintf(page, "No SPC-3 Reservation holder\n");

1397
	se_nacl = pr_reg->pr_reg_nacl;
1398
	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1399

1400
	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1401
		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1402
		se_nacl->initiatorname, i_buf);
1403 1404
}

1405 1406
static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
		char *page)
1407 1408
{
	struct se_node_acl *se_nacl;
1409
	ssize_t len;
1410 1411

	se_nacl = dev->dev_reserved_node_acl;
1412 1413 1414 1415 1416 1417 1418
	if (se_nacl) {
		len = sprintf(page,
			      "SPC-2 Reservation: %s Initiator: %s\n",
			      se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
			      se_nacl->initiatorname);
	} else {
		len = sprintf(page, "No SPC-2 Reservation holder\n");
1419
	}
1420
	return len;
1421 1422
}

1423
static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1424
{
1425
	struct se_device *dev = pr_to_dev(item);
1426
	int ret;
1427

B
Bryant G. Ly 已提交
1428
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1429
		return sprintf(page, "Passthrough\n");
1430

1431 1432 1433 1434 1435 1436 1437
	spin_lock(&dev->dev_reservation_lock);
	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
		ret = target_core_dev_pr_show_spc2_res(dev, page);
	else
		ret = target_core_dev_pr_show_spc3_res(dev, page);
	spin_unlock(&dev->dev_reservation_lock);
	return ret;
1438 1439
}

1440 1441
static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
		char *page)
1442
{
1443
	struct se_device *dev = pr_to_dev(item);
1444 1445 1446
	ssize_t len = 0;

	spin_lock(&dev->dev_reservation_lock);
1447
	if (!dev->dev_pr_res_holder) {
1448
		len = sprintf(page, "No SPC-3 Reservation holder\n");
1449
	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1450 1451
		len = sprintf(page, "SPC-3 Reservation: All Target"
			" Ports registration\n");
1452
	} else {
1453 1454
		len = sprintf(page, "SPC-3 Reservation: Single"
			" Target Port registration\n");
1455
	}
1456

1457
	spin_unlock(&dev->dev_reservation_lock);
1458 1459 1460
	return len;
}

1461 1462
static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
		char *page)
1463
{
1464
	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1465 1466 1467
}


1468 1469
static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
		char *page)
1470
{
1471
	struct se_device *dev = pr_to_dev(item);
1472 1473 1474
	struct se_node_acl *se_nacl;
	struct se_portal_group *se_tpg;
	struct t10_pr_registration *pr_reg;
1475
	const struct target_core_fabric_ops *tfo;
1476 1477 1478 1479
	ssize_t len = 0;

	spin_lock(&dev->dev_reservation_lock);
	pr_reg = dev->dev_pr_res_holder;
1480
	if (!pr_reg) {
1481
		len = sprintf(page, "No SPC-3 Reservation holder\n");
1482
		goto out_unlock;
1483
	}
1484

1485 1486
	se_nacl = pr_reg->pr_reg_nacl;
	se_tpg = se_nacl->se_tpg;
1487
	tfo = se_tpg->se_tpg_tfo;
1488 1489 1490 1491 1492

	len += sprintf(page+len, "SPC-3 Reservation: %s"
		" Target Node Endpoint: %s\n", tfo->get_fabric_name(),
		tfo->tpg_get_wwn(se_tpg));
	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1493
		" Identifier Tag: %hu %s Portal Group Tag: %hu"
H
Hannes Reinecke 已提交
1494
		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1495
		tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1496
		tfo->get_fabric_name(), pr_reg->pr_aptpl_target_lun);
1497

1498 1499
out_unlock:
	spin_unlock(&dev->dev_reservation_lock);
1500 1501 1502 1503
	return len;
}


1504 1505
static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
		char *page)
1506
{
1507
	struct se_device *dev = pr_to_dev(item);
1508
	const struct target_core_fabric_ops *tfo;
1509 1510 1511 1512
	struct t10_pr_registration *pr_reg;
	unsigned char buf[384];
	char i_buf[PR_REG_ISID_ID_LEN];
	ssize_t len = 0;
1513
	int reg_count = 0;
1514 1515 1516

	len += sprintf(page+len, "SPC-3 PR Registrations:\n");

1517 1518
	spin_lock(&dev->t10_pr.registration_lock);
	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1519 1520 1521 1522 1523
			pr_reg_list) {

		memset(buf, 0, 384);
		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1524
		core_pr_dump_initiator_port(pr_reg, i_buf,
1525 1526 1527
					PR_REG_ISID_ID_LEN);
		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
			tfo->get_fabric_name(),
1528
			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1529 1530
			pr_reg->pr_res_generation);

1531
		if (len + strlen(buf) >= PAGE_SIZE)
1532 1533 1534 1535 1536
			break;

		len += sprintf(page+len, "%s", buf);
		reg_count++;
	}
1537
	spin_unlock(&dev->t10_pr.registration_lock);
1538

1539
	if (!reg_count)
1540 1541 1542 1543 1544
		len += sprintf(page+len, "None\n");

	return len;
}

1545
static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1546
{
1547
	struct se_device *dev = pr_to_dev(item);
1548 1549 1550 1551 1552
	struct t10_pr_registration *pr_reg;
	ssize_t len = 0;

	spin_lock(&dev->dev_reservation_lock);
	pr_reg = dev->dev_pr_res_holder;
1553 1554 1555 1556
	if (pr_reg) {
		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
	} else {
1557 1558 1559
		len = sprintf(page, "No SPC-3 Reservation holder\n");
	}

1560
	spin_unlock(&dev->dev_reservation_lock);
1561 1562 1563
	return len;
}

1564
static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1565
{
1566 1567
	struct se_device *dev = pr_to_dev(item);

B
Bryant G. Ly 已提交
1568
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1569 1570 1571 1572 1573
		return sprintf(page, "SPC_PASSTHROUGH\n");
	else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
		return sprintf(page, "SPC2_RESERVATIONS\n");
	else
		return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1574 1575
}

1576 1577
static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
		char *page)
1578
{
1579 1580
	struct se_device *dev = pr_to_dev(item);

B
Bryant G. Ly 已提交
1581
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1582 1583 1584
		return 0;

	return sprintf(page, "APTPL Bit Status: %s\n",
1585
		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1586 1587
}

1588 1589
static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
		char *page)
1590
{
1591 1592
	struct se_device *dev = pr_to_dev(item);

B
Bryant G. Ly 已提交
1593
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
		return 0;

	return sprintf(page, "Ready to process PR APTPL metadata..\n");
}

enum {
	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
};

static match_table_t tokens = {
	{Opt_initiator_fabric, "initiator_fabric=%s"},
	{Opt_initiator_node, "initiator_node=%s"},
	{Opt_initiator_sid, "initiator_sid=%s"},
	{Opt_sa_res_key, "sa_res_key=%s"},
	{Opt_res_holder, "res_holder=%d"},
	{Opt_res_type, "res_type=%d"},
	{Opt_res_scope, "res_scope=%d"},
	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1615
	{Opt_mapped_lun, "mapped_lun=%u"},
1616 1617 1618 1619
	{Opt_target_fabric, "target_fabric=%s"},
	{Opt_target_node, "target_node=%s"},
	{Opt_tpgt, "tpgt=%d"},
	{Opt_port_rtpi, "port_rtpi=%d"},
1620
	{Opt_target_lun, "target_lun=%u"},
1621 1622 1623
	{Opt_err, NULL}
};

1624 1625
static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
		const char *page, size_t count)
1626
{
1627
	struct se_device *dev = pr_to_dev(item);
1628 1629
	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
	unsigned char *t_fabric = NULL, *t_port = NULL;
1630
	char *orig, *ptr, *opts;
1631 1632 1633
	substring_t args[MAX_OPT_ARGS];
	unsigned long long tmp_ll;
	u64 sa_res_key = 0;
H
Hannes Reinecke 已提交
1634
	u64 mapped_lun = 0, target_lun = 0;
1635
	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1636 1637
	u16 tpgt = 0;
	u8 type = 0;
1638

B
Bryant G. Ly 已提交
1639
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1640
		return count;
1641
	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1642
		return count;
1643

1644
	if (dev->export_count) {
1645
		pr_debug("Unable to process APTPL metadata while"
1646 1647 1648 1649 1650 1651 1652 1653 1654
			" active fabric exports exist\n");
		return -EINVAL;
	}

	opts = kstrdup(page, GFP_KERNEL);
	if (!opts)
		return -ENOMEM;

	orig = opts;
1655
	while ((ptr = strsep(&opts, ",\n")) != NULL) {
1656 1657 1658 1659 1660 1661
		if (!*ptr)
			continue;

		token = match_token(ptr, tokens, args);
		switch (token) {
		case Opt_initiator_fabric:
1662
			i_fabric = match_strdup(args);
1663 1664 1665 1666
			if (!i_fabric) {
				ret = -ENOMEM;
				goto out;
			}
1667 1668
			break;
		case Opt_initiator_node:
1669
			i_port = match_strdup(args);
1670 1671 1672 1673
			if (!i_port) {
				ret = -ENOMEM;
				goto out;
			}
1674
			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1675
				pr_err("APTPL metadata initiator_node="
1676 1677 1678 1679 1680 1681 1682
					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
					PR_APTPL_MAX_IPORT_LEN);
				ret = -EINVAL;
				break;
			}
			break;
		case Opt_initiator_sid:
1683
			isid = match_strdup(args);
1684 1685 1686 1687
			if (!isid) {
				ret = -ENOMEM;
				goto out;
			}
1688
			if (strlen(isid) >= PR_REG_ISID_LEN) {
1689
				pr_err("APTPL metadata initiator_isid"
1690 1691 1692 1693 1694 1695 1696
					"= exceeds PR_REG_ISID_LEN: %d\n",
					PR_REG_ISID_LEN);
				ret = -EINVAL;
				break;
			}
			break;
		case Opt_sa_res_key:
1697
			ret = match_u64(args,  &tmp_ll);
1698
			if (ret < 0) {
1699
				pr_err("kstrtoull() failed for sa_res_key=\n");
1700 1701 1702 1703 1704 1705 1706 1707
				goto out;
			}
			sa_res_key = (u64)tmp_ll;
			break;
		/*
		 * PR APTPL Metadata for Reservation
		 */
		case Opt_res_holder:
1708 1709 1710
			ret = match_int(args, &arg);
			if (ret)
				goto out;
1711 1712 1713
			res_holder = arg;
			break;
		case Opt_res_type:
1714 1715 1716
			ret = match_int(args, &arg);
			if (ret)
				goto out;
1717 1718 1719
			type = (u8)arg;
			break;
		case Opt_res_scope:
1720 1721 1722
			ret = match_int(args, &arg);
			if (ret)
				goto out;
1723 1724
			break;
		case Opt_res_all_tg_pt:
1725 1726 1727
			ret = match_int(args, &arg);
			if (ret)
				goto out;
1728 1729 1730
			all_tg_pt = (int)arg;
			break;
		case Opt_mapped_lun:
1731
			ret = match_u64(args, &tmp_ll);
1732 1733
			if (ret)
				goto out;
1734
			mapped_lun = (u64)tmp_ll;
1735 1736 1737 1738 1739
			break;
		/*
		 * PR APTPL Metadata for Target Port
		 */
		case Opt_target_fabric:
1740
			t_fabric = match_strdup(args);
1741 1742 1743 1744
			if (!t_fabric) {
				ret = -ENOMEM;
				goto out;
			}
1745 1746
			break;
		case Opt_target_node:
1747
			t_port = match_strdup(args);
1748 1749 1750 1751
			if (!t_port) {
				ret = -ENOMEM;
				goto out;
			}
1752
			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1753
				pr_err("APTPL metadata target_node="
1754 1755 1756 1757 1758 1759 1760
					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
					PR_APTPL_MAX_TPORT_LEN);
				ret = -EINVAL;
				break;
			}
			break;
		case Opt_tpgt:
1761 1762 1763
			ret = match_int(args, &arg);
			if (ret)
				goto out;
1764 1765 1766
			tpgt = (u16)arg;
			break;
		case Opt_port_rtpi:
1767 1768 1769
			ret = match_int(args, &arg);
			if (ret)
				goto out;
1770 1771
			break;
		case Opt_target_lun:
1772
			ret = match_u64(args, &tmp_ll);
1773 1774
			if (ret)
				goto out;
1775
			target_lun = (u64)tmp_ll;
1776 1777 1778 1779 1780 1781
			break;
		default:
			break;
		}
	}

1782 1783
	if (!i_port || !t_port || !sa_res_key) {
		pr_err("Illegal parameters for APTPL registration\n");
1784 1785 1786 1787 1788
		ret = -EINVAL;
		goto out;
	}

	if (res_holder && !(type)) {
1789
		pr_err("Illegal PR type: 0x%02x for reservation"
1790 1791 1792 1793 1794
				" holder\n", type);
		ret = -EINVAL;
		goto out;
	}

1795
	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1796 1797 1798
			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
			res_holder, all_tg_pt, type);
out:
1799 1800 1801 1802 1803
	kfree(i_fabric);
	kfree(i_port);
	kfree(isid);
	kfree(t_fabric);
	kfree(t_port);
1804 1805 1806 1807 1808
	kfree(orig);
	return (ret == 0) ? count : ret;
}


1809 1810 1811 1812 1813 1814 1815 1816 1817
CONFIGFS_ATTR_RO(target_pr_, res_holder);
CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
CONFIGFS_ATTR_RO(target_pr_, res_type);
CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
1818 1819

static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1820 1821 1822 1823 1824 1825 1826 1827 1828
	&target_pr_attr_res_holder,
	&target_pr_attr_res_pr_all_tgt_pts,
	&target_pr_attr_res_pr_generation,
	&target_pr_attr_res_pr_holder_tg_port,
	&target_pr_attr_res_pr_registered_i_pts,
	&target_pr_attr_res_pr_type,
	&target_pr_attr_res_type,
	&target_pr_attr_res_aptpl_active,
	&target_pr_attr_res_aptpl_metadata,
1829 1830 1831
	NULL,
};

1832
TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
1833

1834
/*  End functions for struct config_item_type tb_dev_pr_cit */
1835

1836
/*  Start functions for struct config_item_type tb_dev_cit */
1837

1838 1839 1840 1841 1842 1843
static inline struct se_device *to_device(struct config_item *item)
{
	return container_of(to_config_group(item), struct se_device, dev_group);
}

static ssize_t target_dev_info_show(struct config_item *item, char *page)
1844
{
1845
	struct se_device *dev = to_device(item);
1846 1847 1848
	int bl = 0;
	ssize_t read_bytes = 0;

1849
	transport_dump_dev_state(dev, page, &bl);
1850
	read_bytes += bl;
1851 1852
	read_bytes += dev->transport->show_configfs_dev_params(dev,
			page+read_bytes);
1853 1854 1855
	return read_bytes;
}

1856 1857
static ssize_t target_dev_control_store(struct config_item *item,
		const char *page, size_t count)
1858
{
1859
	struct se_device *dev = to_device(item);
1860

1861
	return dev->transport->set_configfs_dev_params(dev, page, count);
1862 1863
}

1864
static ssize_t target_dev_alias_show(struct config_item *item, char *page)
1865
{
1866
	struct se_device *dev = to_device(item);
1867

1868
	if (!(dev->dev_flags & DF_USING_ALIAS))
1869 1870
		return 0;

1871
	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1872 1873
}

1874 1875
static ssize_t target_dev_alias_store(struct config_item *item,
		const char *page, size_t count)
1876
{
1877
	struct se_device *dev = to_device(item);
1878
	struct se_hba *hba = dev->se_hba;
1879 1880 1881
	ssize_t read_bytes;

	if (count > (SE_DEV_ALIAS_LEN-1)) {
1882
		pr_err("alias count: %d exceeds"
1883 1884 1885 1886 1887
			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
			SE_DEV_ALIAS_LEN-1);
		return -EINVAL;
	}

1888
	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
1889 1890
	if (!read_bytes)
		return -EINVAL;
1891 1892
	if (dev->dev_alias[read_bytes - 1] == '\n')
		dev->dev_alias[read_bytes - 1] = '\0';
1893

1894
	dev->dev_flags |= DF_USING_ALIAS;
1895

1896
	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1897
		config_item_name(&hba->hba_group.cg_item),
1898 1899
		config_item_name(&dev->dev_group.cg_item),
		dev->dev_alias);
1900 1901 1902 1903

	return read_bytes;
}

1904
static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
1905
{
1906
	struct se_device *dev = to_device(item);
1907

1908
	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1909 1910
		return 0;

1911
	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1912 1913
}

1914 1915
static ssize_t target_dev_udev_path_store(struct config_item *item,
		const char *page, size_t count)
1916
{
1917
	struct se_device *dev = to_device(item);
1918
	struct se_hba *hba = dev->se_hba;
1919 1920 1921
	ssize_t read_bytes;

	if (count > (SE_UDEV_PATH_LEN-1)) {
1922
		pr_err("udev_path count: %d exceeds"
1923 1924 1925 1926 1927
			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
			SE_UDEV_PATH_LEN-1);
		return -EINVAL;
	}

1928
	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1929
			"%s", page);
1930 1931
	if (!read_bytes)
		return -EINVAL;
1932 1933
	if (dev->udev_path[read_bytes - 1] == '\n')
		dev->udev_path[read_bytes - 1] = '\0';
1934

1935
	dev->dev_flags |= DF_USING_UDEV_PATH;
1936

1937
	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1938
		config_item_name(&hba->hba_group.cg_item),
1939 1940
		config_item_name(&dev->dev_group.cg_item),
		dev->udev_path);
1941 1942 1943 1944

	return read_bytes;
}

1945
static ssize_t target_dev_enable_show(struct config_item *item, char *page)
1946
{
1947
	struct se_device *dev = to_device(item);
1948 1949 1950 1951

	return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
}

1952 1953
static ssize_t target_dev_enable_store(struct config_item *item,
		const char *page, size_t count)
1954
{
1955
	struct se_device *dev = to_device(item);
1956
	char *ptr;
1957
	int ret;
1958 1959

	ptr = strstr(page, "1");
1960 1961
	if (!ptr) {
		pr_err("For dev_enable ops, only valid value"
1962 1963 1964 1965
				" is \"1\"\n");
		return -EINVAL;
	}

1966 1967 1968
	ret = target_configure_device(dev);
	if (ret)
		return ret;
1969 1970 1971
	return count;
}

1972
static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
1973
{
1974
	struct se_device *dev = to_device(item);
1975 1976 1977 1978 1979 1980
	struct config_item *lu_ci;
	struct t10_alua_lu_gp *lu_gp;
	struct t10_alua_lu_gp_member *lu_gp_mem;
	ssize_t len = 0;

	lu_gp_mem = dev->dev_alua_lu_gp_mem;
C
Christoph Hellwig 已提交
1981 1982
	if (!lu_gp_mem)
		return 0;
1983 1984 1985

	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
	lu_gp = lu_gp_mem->lu_gp;
1986
	if (lu_gp) {
1987 1988 1989 1990 1991 1992 1993 1994 1995
		lu_ci = &lu_gp->lu_gp_group.cg_item;
		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
			config_item_name(lu_ci), lu_gp->lu_gp_id);
	}
	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);

	return len;
}

1996 1997
static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
		const char *page, size_t count)
1998
{
1999
	struct se_device *dev = to_device(item);
2000
	struct se_hba *hba = dev->se_hba;
2001 2002 2003 2004 2005
	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
	struct t10_alua_lu_gp_member *lu_gp_mem;
	unsigned char buf[LU_GROUP_NAME_BUF];
	int move = 0;

C
Christoph Hellwig 已提交
2006 2007
	lu_gp_mem = dev->dev_alua_lu_gp_mem;
	if (!lu_gp_mem)
2008
		return count;
C
Christoph Hellwig 已提交
2009

2010
	if (count > LU_GROUP_NAME_BUF) {
2011
		pr_err("ALUA LU Group Alias too large!\n");
2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
		return -EINVAL;
	}
	memset(buf, 0, LU_GROUP_NAME_BUF);
	memcpy(buf, page, count);
	/*
	 * Any ALUA logical unit alias besides "NULL" means we will be
	 * making a new group association.
	 */
	if (strcmp(strstrip(buf), "NULL")) {
		/*
		 * core_alua_get_lu_gp_by_name() will increment reference to
		 * struct t10_alua_lu_gp.  This reference is released with
		 * core_alua_get_lu_gp_by_name below().
		 */
		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
2027
		if (!lu_gp_new)
2028 2029 2030 2031 2032
			return -ENODEV;
	}

	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
	lu_gp = lu_gp_mem->lu_gp;
2033
	if (lu_gp) {
2034 2035 2036 2037
		/*
		 * Clearing an existing lu_gp association, and replacing
		 * with NULL
		 */
2038 2039
		if (!lu_gp_new) {
			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2040 2041 2042
				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
				" %hu\n",
				config_item_name(&hba->hba_group.cg_item),
2043
				config_item_name(&dev->dev_group.cg_item),
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
				config_item_name(&lu_gp->lu_gp_group.cg_item),
				lu_gp->lu_gp_id);

			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);

			return count;
		}
		/*
		 * Removing existing association of lu_gp_mem with lu_gp
		 */
		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
		move = 1;
	}
	/*
	 * Associate lu_gp_mem with lu_gp_new.
	 */
	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);

2064
	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2065 2066 2067
		" core/alua/lu_gps/%s, ID: %hu\n",
		(move) ? "Moving" : "Adding",
		config_item_name(&hba->hba_group.cg_item),
2068
		config_item_name(&dev->dev_group.cg_item),
2069 2070 2071 2072 2073 2074 2075
		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
		lu_gp_new->lu_gp_id);

	core_alua_put_lu_gp_from_name(lu_gp_new);
	return count;
}

2076
static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2077
{
2078
	struct se_device *dev = to_device(item);
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
	struct t10_alua_lba_map *map;
	struct t10_alua_lba_map_member *mem;
	char *b = page;
	int bl = 0;
	char state;

	spin_lock(&dev->t10_alua.lba_map_lock);
	if (!list_empty(&dev->t10_alua.lba_map_list))
	    bl += sprintf(b + bl, "%u %u\n",
			  dev->t10_alua.lba_map_segment_size,
			  dev->t10_alua.lba_map_segment_multiplier);
	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
		bl += sprintf(b + bl, "%llu %llu",
			      map->lba_map_first_lba, map->lba_map_last_lba);
		list_for_each_entry(mem, &map->lba_map_mem_list,
				    lba_map_mem_list) {
			switch (mem->lba_map_mem_alua_state) {
			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
				state = 'O';
				break;
			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
				state = 'A';
				break;
			case ALUA_ACCESS_STATE_STANDBY:
				state = 'S';
				break;
			case ALUA_ACCESS_STATE_UNAVAILABLE:
				state = 'U';
				break;
			default:
				state = '.';
				break;
			}
			bl += sprintf(b + bl, " %d:%c",
				      mem->lba_map_mem_alua_pg_id, state);
		}
		bl += sprintf(b + bl, "\n");
	}
	spin_unlock(&dev->t10_alua.lba_map_lock);
	return bl;
}

2121 2122
static ssize_t target_dev_lba_map_store(struct config_item *item,
		const char *page, size_t count)
2123
{
2124
	struct se_device *dev = to_device(item);
2125 2126
	struct t10_alua_lba_map *lba_map = NULL;
	struct list_head lba_list;
2127
	char *map_entries, *orig, *ptr;
2128 2129 2130 2131 2132 2133
	char state;
	int pg_num = -1, pg;
	int ret = 0, num = 0, pg_id, alua_state;
	unsigned long start_lba = -1, end_lba = -1;
	unsigned long segment_size = -1, segment_mult = -1;

2134
	orig = map_entries = kstrdup(page, GFP_KERNEL);
2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
	if (!map_entries)
		return -ENOMEM;

	INIT_LIST_HEAD(&lba_list);
	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
		if (!*ptr)
			continue;

		if (num == 0) {
			if (sscanf(ptr, "%lu %lu\n",
				   &segment_size, &segment_mult) != 2) {
				pr_err("Invalid line %d\n", num);
				ret = -EINVAL;
				break;
			}
			num++;
			continue;
		}
		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
			pr_err("Invalid line %d\n", num);
			ret = -EINVAL;
			break;
		}
		ptr = strchr(ptr, ' ');
		if (!ptr) {
			pr_err("Invalid line %d, missing end lba\n", num);
			ret = -EINVAL;
			break;
		}
		ptr++;
		ptr = strchr(ptr, ' ');
		if (!ptr) {
			pr_err("Invalid line %d, missing state definitions\n",
			       num);
			ret = -EINVAL;
			break;
		}
		ptr++;
		lba_map = core_alua_allocate_lba_map(&lba_list,
						     start_lba, end_lba);
		if (IS_ERR(lba_map)) {
			ret = PTR_ERR(lba_map);
			break;
		}
		pg = 0;
		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
			switch (state) {
			case 'O':
				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
				break;
			case 'A':
				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
				break;
			case 'S':
				alua_state = ALUA_ACCESS_STATE_STANDBY;
				break;
			case 'U':
				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
				break;
			default:
				pr_err("Invalid ALUA state '%c'\n", state);
				ret = -EINVAL;
				goto out;
			}

			ret = core_alua_allocate_lba_map_mem(lba_map,
							     pg_id, alua_state);
			if (ret) {
				pr_err("Invalid target descriptor %d:%c "
				       "at line %d\n",
				       pg_id, state, num);
				break;
			}
			pg++;
			ptr = strchr(ptr, ' ');
			if (ptr)
				ptr++;
			else
				break;
		}
		if (pg_num == -1)
		    pg_num = pg;
		else if (pg != pg_num) {
			pr_err("Only %d from %d port groups definitions "
			       "at line %d\n", pg, pg_num, num);
			ret = -EINVAL;
			break;
		}
		num++;
	}
out:
	if (ret) {
		core_alua_free_lba_map(&lba_list);
		count = ret;
	} else
		core_alua_set_lba_map(dev, &lba_list,
				      segment_size, segment_mult);
2232
	kfree(orig);
2233 2234 2235
	return count;
}

2236 2237 2238 2239 2240 2241 2242
CONFIGFS_ATTR_RO(target_dev_, info);
CONFIGFS_ATTR_WO(target_dev_, control);
CONFIGFS_ATTR(target_dev_, alias);
CONFIGFS_ATTR(target_dev_, udev_path);
CONFIGFS_ATTR(target_dev_, enable);
CONFIGFS_ATTR(target_dev_, alua_lu_gp);
CONFIGFS_ATTR(target_dev_, lba_map);
2243

2244
static struct configfs_attribute *target_core_dev_attrs[] = {
2245 2246 2247 2248 2249 2250 2251
	&target_dev_attr_info,
	&target_dev_attr_control,
	&target_dev_attr_alias,
	&target_dev_attr_udev_path,
	&target_dev_attr_enable,
	&target_dev_attr_alua_lu_gp,
	&target_dev_attr_lba_map,
2252 2253 2254 2255 2256
	NULL,
};

static void target_core_dev_release(struct config_item *item)
{
2257 2258 2259
	struct config_group *dev_cg = to_config_group(item);
	struct se_device *dev =
		container_of(dev_cg, struct se_device, dev_group);
2260

2261
	target_free_device(dev);
2262 2263
}

2264 2265 2266 2267 2268
/*
 * Used in target_core_fabric_configfs.c to verify valid se_device symlink
 * within target_fabric_port_link()
 */
struct configfs_item_operations target_core_dev_item_ops = {
2269 2270 2271
	.release		= target_core_dev_release,
};

2272
TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2273

2274
/* End functions for struct config_item_type tb_dev_cit */
2275 2276 2277

/* Start functions for struct config_item_type target_core_alua_lu_gp_cit */

2278 2279 2280 2281 2282
static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
{
	return container_of(to_config_group(item), struct t10_alua_lu_gp,
			lu_gp_group);
}
2283

2284
static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2285
{
2286 2287
	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);

2288
	if (!lu_gp->lu_gp_valid_id)
2289 2290 2291 2292
		return 0;
	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
}

2293 2294
static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
		const char *page, size_t count)
2295
{
2296
	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2297 2298 2299 2300
	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
	unsigned long lu_gp_id;
	int ret;

2301
	ret = kstrtoul(page, 0, &lu_gp_id);
2302
	if (ret < 0) {
2303
		pr_err("kstrtoul() returned %d for"
2304
			" lu_gp_id\n", ret);
2305
		return ret;
2306 2307
	}
	if (lu_gp_id > 0x0000ffff) {
2308
		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2309 2310 2311 2312 2313 2314 2315 2316
			" 0x0000ffff\n", lu_gp_id);
		return -EINVAL;
	}

	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
	if (ret < 0)
		return -EINVAL;

2317
	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2318 2319 2320 2321 2322 2323 2324
		" Group: core/alua/lu_gps/%s to ID: %hu\n",
		config_item_name(&alua_lu_gp_cg->cg_item),
		lu_gp->lu_gp_id);

	return count;
}

2325
static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2326
{
2327
	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
	struct se_device *dev;
	struct se_hba *hba;
	struct t10_alua_lu_gp_member *lu_gp_mem;
	ssize_t len = 0, cur_len;
	unsigned char buf[LU_GROUP_NAME_BUF];

	memset(buf, 0, LU_GROUP_NAME_BUF);

	spin_lock(&lu_gp->lu_gp_lock);
	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
		dev = lu_gp_mem->lu_gp_mem_dev;
2339
		hba = dev->se_hba;
2340 2341 2342

		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
			config_item_name(&hba->hba_group.cg_item),
2343
			config_item_name(&dev->dev_group.cg_item));
2344 2345 2346
		cur_len++; /* Extra byte for NULL terminator */

		if ((cur_len + len) > PAGE_SIZE) {
2347
			pr_warn("Ran out of lu_gp_show_attr"
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
				"_members buffer\n");
			break;
		}
		memcpy(page+len, buf, cur_len);
		len += cur_len;
	}
	spin_unlock(&lu_gp->lu_gp_lock);

	return len;
}

2359 2360
CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
CONFIGFS_ATTR_RO(target_lu_gp_, members);
2361 2362

static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2363 2364
	&target_lu_gp_attr_lu_gp_id,
	&target_lu_gp_attr_members,
2365 2366 2367
	NULL,
};

2368 2369 2370 2371 2372 2373 2374 2375
static void target_core_alua_lu_gp_release(struct config_item *item)
{
	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
			struct t10_alua_lu_gp, lu_gp_group);

	core_alua_free_lu_gp(lu_gp);
}

2376
static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2377
	.release		= target_core_alua_lu_gp_release,
2378 2379
};

2380
static const struct config_item_type target_core_alua_lu_gp_cit = {
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
	.ct_item_ops		= &target_core_alua_lu_gp_ops,
	.ct_attrs		= target_core_alua_lu_gp_attrs,
	.ct_owner		= THIS_MODULE,
};

/* End functions for struct config_item_type target_core_alua_lu_gp_cit */

/* Start functions for struct config_item_type target_core_alua_lu_gps_cit */

static struct config_group *target_core_alua_create_lu_gp(
	struct config_group *group,
	const char *name)
{
	struct t10_alua_lu_gp *lu_gp;
	struct config_group *alua_lu_gp_cg = NULL;
	struct config_item *alua_lu_gp_ci = NULL;

	lu_gp = core_alua_allocate_lu_gp(name, 0);
	if (IS_ERR(lu_gp))
		return NULL;

	alua_lu_gp_cg = &lu_gp->lu_gp_group;
	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;

	config_group_init_type_name(alua_lu_gp_cg, name,
			&target_core_alua_lu_gp_cit);

2408
	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422
		" Group: core/alua/lu_gps/%s\n",
		config_item_name(alua_lu_gp_ci));

	return alua_lu_gp_cg;

}

static void target_core_alua_drop_lu_gp(
	struct config_group *group,
	struct config_item *item)
{
	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
			struct t10_alua_lu_gp, lu_gp_group);

2423
	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2424 2425
		" Group: core/alua/lu_gps/%s, ID: %hu\n",
		config_item_name(item), lu_gp->lu_gp_id);
2426 2427 2428 2429
	/*
	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
	 * -> target_core_alua_lu_gp_release()
	 */
2430 2431 2432 2433 2434 2435 2436 2437
	config_item_put(item);
}

static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
	.make_group		= &target_core_alua_create_lu_gp,
	.drop_item		= &target_core_alua_drop_lu_gp,
};

2438
static const struct config_item_type target_core_alua_lu_gps_cit = {
2439 2440 2441 2442 2443 2444 2445 2446 2447
	.ct_item_ops		= NULL,
	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
	.ct_owner		= THIS_MODULE,
};

/* End functions for struct config_item_type target_core_alua_lu_gps_cit */

/* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */

2448 2449 2450 2451 2452
static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
{
	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
			tg_pt_gp_group);
}
2453

2454 2455
static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
		char *page)
2456 2457
{
	return sprintf(page, "%d\n",
2458
		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2459 2460
}

2461 2462
static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
		const char *page, size_t count)
2463
{
2464
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2465
	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2466 2467 2468
	unsigned long tmp;
	int new_state, ret;

2469
	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2470
		pr_err("Unable to do implicit ALUA on non valid"
2471 2472 2473
			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
		return -EINVAL;
	}
2474 2475 2476 2477 2478
	if (!(dev->dev_flags & DF_CONFIGURED)) {
		pr_err("Unable to set alua_access_state while device is"
		       " not configured\n");
		return -ENODEV;
	}
2479

2480
	ret = kstrtoul(page, 0, &tmp);
2481
	if (ret < 0) {
2482
		pr_err("Unable to extract new ALUA access state from"
2483
				" %s\n", page);
2484
		return ret;
2485 2486 2487
	}
	new_state = (int)tmp;

2488 2489 2490
	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
		pr_err("Unable to process implicit configfs ALUA"
			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2491 2492
		return -EINVAL;
	}
2493 2494 2495 2496 2497 2498 2499
	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
		/* LBA DEPENDENT is only allowed with implicit ALUA */
		pr_err("Unable to process implicit configfs ALUA transition"
		       " while explicit ALUA management is enabled\n");
		return -EINVAL;
	}
2500

2501
	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2502 2503 2504 2505
					NULL, NULL, new_state, 0);
	return (!ret) ? count : -EINVAL;
}

2506 2507
static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
		char *page)
2508
{
2509
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2510 2511 2512 2513
	return sprintf(page, "%s\n",
		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
}

2514 2515
static ssize_t target_tg_pt_gp_alua_access_status_store(
		struct config_item *item, const char *page, size_t count)
2516
{
2517
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2518 2519 2520
	unsigned long tmp;
	int new_status, ret;

2521 2522
	if (!tg_pt_gp->tg_pt_gp_valid_id) {
		pr_err("Unable to do set ALUA access status on non"
2523 2524 2525 2526 2527
			" valid tg_pt_gp ID: %hu\n",
			tg_pt_gp->tg_pt_gp_valid_id);
		return -EINVAL;
	}

2528
	ret = kstrtoul(page, 0, &tmp);
2529
	if (ret < 0) {
2530
		pr_err("Unable to extract new ALUA access status"
2531
				" from %s\n", page);
2532
		return ret;
2533 2534 2535 2536
	}
	new_status = (int)tmp;

	if ((new_status != ALUA_STATUS_NONE) &&
2537 2538
	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2539
		pr_err("Illegal ALUA access status: 0x%02x\n",
2540 2541 2542 2543 2544 2545 2546 2547
				new_status);
		return -EINVAL;
	}

	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
	return count;
}

2548 2549
static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
		char *page)
2550
{
2551
	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2552 2553
}

2554 2555
static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
		const char *page, size_t count)
2556
{
2557
	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2558 2559
}

2560 2561 2562
#define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
		struct config_item *item, char *p)			\
2563
{									\
2564 2565 2566 2567 2568 2569 2570
	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
	return sprintf(p, "%d\n",					\
		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
}									\
									\
static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
		struct config_item *item, const char *p, size_t c)	\
2571
{									\
2572
	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2573 2574 2575 2576
	unsigned long tmp;						\
	int ret;							\
									\
	if (!t->tg_pt_gp_valid_id) {					\
2577
		pr_err("Unable to do set " #_name " ALUA state on non"	\
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
		       " valid tg_pt_gp ID: %hu\n",			\
		       t->tg_pt_gp_valid_id);				\
		return -EINVAL;						\
	}								\
									\
	ret = kstrtoul(p, 0, &tmp);					\
	if (ret < 0) {							\
		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
		return -EINVAL;						\
	}								\
	if (tmp > 1) {							\
		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
		return -EINVAL;						\
	}								\
2592
	if (tmp)							\
2593
		t->tg_pt_gp_alua_supported_states |= _bit;		\
2594
	else								\
2595
		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2596 2597 2598 2599
									\
	return c;							\
}

2600 2601 2602 2603 2604 2605 2606
ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
2607

2608 2609
static ssize_t target_tg_pt_gp_alua_write_metadata_show(
		struct config_item *item, char *page)
2610
{
2611 2612
	return sprintf(page, "%d\n",
		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2613 2614
}

2615 2616
static ssize_t target_tg_pt_gp_alua_write_metadata_store(
		struct config_item *item, const char *page, size_t count)
2617
{
2618
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2619 2620 2621
	unsigned long tmp;
	int ret;

2622
	ret = kstrtoul(page, 0, &tmp);
2623
	if (ret < 0) {
2624
		pr_err("Unable to extract alua_write_metadata\n");
2625
		return ret;
2626 2627 2628
	}

	if ((tmp != 0) && (tmp != 1)) {
2629
		pr_err("Illegal value for alua_write_metadata:"
2630 2631 2632 2633 2634 2635 2636 2637
			" %lu\n", tmp);
		return -EINVAL;
	}
	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;

	return count;
}

2638 2639
static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
		char *page)
2640
{
2641
	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2642 2643
}

2644 2645
static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
		const char *page, size_t count)
2646
{
2647 2648
	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
			count);
2649 2650
}

2651 2652
static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
		char *page)
2653
{
2654
	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2655 2656
}

2657 2658
static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
		const char *page, size_t count)
2659
{
2660 2661
	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
			count);
2662 2663
}

2664 2665
static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
		struct config_item *item, char *page)
2666
{
2667
	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
2668 2669
}

2670 2671
static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
		struct config_item *item, const char *page, size_t count)
2672
{
2673 2674
	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
			count);
2675 2676
}

2677 2678
static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
		char *page)
2679
{
2680
	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
2681 2682
}

2683 2684
static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
		const char *page, size_t count)
2685
{
2686
	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
2687 2688
}

2689 2690
static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
		char *page)
2691
{
2692 2693
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);

2694
	if (!tg_pt_gp->tg_pt_gp_valid_id)
2695 2696 2697 2698
		return 0;
	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
}

2699 2700
static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
		const char *page, size_t count)
2701
{
2702
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2703 2704 2705 2706
	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
	unsigned long tg_pt_gp_id;
	int ret;

2707
	ret = kstrtoul(page, 0, &tg_pt_gp_id);
2708
	if (ret < 0) {
2709 2710
		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
		       page);
2711
		return ret;
2712 2713
	}
	if (tg_pt_gp_id > 0x0000ffff) {
2714 2715
		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
		       tg_pt_gp_id);
2716 2717 2718 2719 2720 2721 2722
		return -EINVAL;
	}

	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
	if (ret < 0)
		return -EINVAL;

2723
	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2724 2725 2726 2727 2728 2729 2730
		"core/alua/tg_pt_gps/%s to ID: %hu\n",
		config_item_name(&alua_tg_pt_gp_cg->cg_item),
		tg_pt_gp->tg_pt_gp_id);

	return count;
}

2731 2732
static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
		char *page)
2733
{
2734
	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2735 2736 2737 2738 2739 2740 2741
	struct se_lun *lun;
	ssize_t len = 0, cur_len;
	unsigned char buf[TG_PT_GROUP_NAME_BUF];

	memset(buf, 0, TG_PT_GROUP_NAME_BUF);

	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2742 2743 2744
	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
			lun_tg_pt_gp_link) {
		struct se_portal_group *tpg = lun->lun_tpg;
2745 2746

		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2747 2748 2749
			"/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
			tpg->se_tpg_tfo->tpg_get_tag(tpg),
2750 2751 2752 2753
			config_item_name(&lun->lun_group.cg_item));
		cur_len++; /* Extra byte for NULL terminator */

		if ((cur_len + len) > PAGE_SIZE) {
2754
			pr_warn("Ran out of lu_gp_show_attr"
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
				"_members buffer\n");
			break;
		}
		memcpy(page+len, buf, cur_len);
		len += cur_len;
	}
	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);

	return len;
}

2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
2783 2784

static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
	&target_tg_pt_gp_attr_alua_access_state,
	&target_tg_pt_gp_attr_alua_access_status,
	&target_tg_pt_gp_attr_alua_access_type,
	&target_tg_pt_gp_attr_alua_support_transitioning,
	&target_tg_pt_gp_attr_alua_support_offline,
	&target_tg_pt_gp_attr_alua_support_lba_dependent,
	&target_tg_pt_gp_attr_alua_support_unavailable,
	&target_tg_pt_gp_attr_alua_support_standby,
	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
	&target_tg_pt_gp_attr_alua_support_active_optimized,
	&target_tg_pt_gp_attr_alua_write_metadata,
	&target_tg_pt_gp_attr_nonop_delay_msecs,
	&target_tg_pt_gp_attr_trans_delay_msecs,
	&target_tg_pt_gp_attr_implicit_trans_secs,
	&target_tg_pt_gp_attr_preferred,
	&target_tg_pt_gp_attr_tg_pt_gp_id,
	&target_tg_pt_gp_attr_members,
2802 2803 2804
	NULL,
};

2805 2806 2807 2808 2809 2810 2811 2812
static void target_core_alua_tg_pt_gp_release(struct config_item *item)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
			struct t10_alua_tg_pt_gp, tg_pt_gp_group);

	core_alua_free_tg_pt_gp(tg_pt_gp);
}

2813
static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2814
	.release		= target_core_alua_tg_pt_gp_release,
2815 2816
};

2817
static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
2818 2819 2820 2821 2822 2823 2824
	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
	.ct_owner		= THIS_MODULE,
};

/* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */

2825
/* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836

static struct config_group *target_core_alua_create_tg_pt_gp(
	struct config_group *group,
	const char *name)
{
	struct t10_alua *alua = container_of(group, struct t10_alua,
					alua_tg_pt_gps_group);
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct config_group *alua_tg_pt_gp_cg = NULL;
	struct config_item *alua_tg_pt_gp_ci = NULL;

2837
	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
2838
	if (!tg_pt_gp)
2839 2840 2841 2842 2843 2844 2845 2846
		return NULL;

	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;

	config_group_init_type_name(alua_tg_pt_gp_cg, name,
			&target_core_alua_tg_pt_gp_cit);

2847
	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
		" Group: alua/tg_pt_gps/%s\n",
		config_item_name(alua_tg_pt_gp_ci));

	return alua_tg_pt_gp_cg;
}

static void target_core_alua_drop_tg_pt_gp(
	struct config_group *group,
	struct config_item *item)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
			struct t10_alua_tg_pt_gp, tg_pt_gp_group);

2861
	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2862 2863
		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2864 2865 2866 2867
	/*
	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
	 * -> target_core_alua_tg_pt_gp_release().
	 */
2868 2869 2870 2871 2872 2873 2874 2875
	config_item_put(item);
}

static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
	.make_group		= &target_core_alua_create_tg_pt_gp,
	.drop_item		= &target_core_alua_drop_tg_pt_gp,
};

2876
TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
2877

2878
/* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
2879 2880 2881 2882 2883 2884 2885 2886 2887

/* Start functions for struct config_item_type target_core_alua_cit */

/*
 * target_core_alua_cit is a ConfigFS group that lives under
 * /sys/kernel/config/target/core/alua.  There are default groups
 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
 * target_core_alua_cit in target_core_init_configfs() below.
 */
2888
static const struct config_item_type target_core_alua_cit = {
2889 2890 2891 2892 2893 2894 2895
	.ct_item_ops		= NULL,
	.ct_attrs		= NULL,
	.ct_owner		= THIS_MODULE,
};

/* End functions for struct config_item_type target_core_alua_cit */

2896
/* Start functions for struct config_item_type tb_dev_stat_cit */
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916

static struct config_group *target_core_stat_mkdir(
	struct config_group *group,
	const char *name)
{
	return ERR_PTR(-ENOSYS);
}

static void target_core_stat_rmdir(
	struct config_group *group,
	struct config_item *item)
{
	return;
}

static struct configfs_group_operations target_core_stat_group_ops = {
	.make_group		= &target_core_stat_mkdir,
	.drop_item		= &target_core_stat_rmdir,
};

2917
TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
2918

2919
/* End functions for struct config_item_type tb_dev_stat_cit */
2920

2921 2922 2923 2924 2925 2926 2927 2928 2929
/* Start functions for struct config_item_type target_core_hba_cit */

static struct config_group *target_core_make_subdev(
	struct config_group *group,
	const char *name)
{
	struct t10_alua_tg_pt_gp *tg_pt_gp;
	struct config_item *hba_ci = &group->cg_item;
	struct se_hba *hba = item_to_hba(hba_ci);
2930
	struct target_backend *tb = hba->backend;
2931
	struct se_device *dev;
2932
	int errno = -ENOMEM, ret;
2933

2934 2935 2936
	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
	if (ret)
		return ERR_PTR(ret);
2937

2938 2939 2940 2941
	dev = target_alloc_device(hba, name);
	if (!dev)
		goto out_unlock;

2942
	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
2943

2944 2945 2946 2947
	config_group_init_type_name(&dev->dev_action_group, "action",
			&tb->tb_dev_action_cit);
	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);

2948
	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2949
			&tb->tb_dev_attrib_cit);
2950 2951
	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);

2952
	config_group_init_type_name(&dev->dev_pr_group, "pr",
2953
			&tb->tb_dev_pr_cit);
2954 2955
	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);

2956
	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2957
			&tb->tb_dev_wwn_cit);
2958 2959 2960
	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
			&dev->dev_group);

2961
	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
2962
			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
2963 2964 2965
	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
			&dev->dev_group);

2966
	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2967
			"statistics", &tb->tb_dev_stat_cit);
2968 2969
	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
			&dev->dev_group);
2970

2971
	/*
2972
	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2973
	 */
2974
	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
2975
	if (!tg_pt_gp)
2976
		goto out_free_device;
2977
	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2978 2979 2980

	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2981 2982 2983
	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
			&dev->t10_alua.alua_tg_pt_gps_group);

2984 2985 2986
	/*
	 * Add core/$HBA/$DEV/statistics/ default groups
	 */
2987
	target_stat_setup_dev_default_groups(dev);
2988 2989

	mutex_unlock(&hba->hba_access_mutex);
2990
	return &dev->dev_group;
2991 2992 2993 2994

out_free_device:
	target_free_device(dev);
out_unlock:
2995
	mutex_unlock(&hba->hba_access_mutex);
2996
	return ERR_PTR(errno);
2997 2998 2999 3000 3001 3002
}

static void target_core_drop_subdev(
	struct config_group *group,
	struct config_item *item)
{
3003 3004 3005
	struct config_group *dev_cg = to_config_group(item);
	struct se_device *dev =
		container_of(dev_cg, struct se_device, dev_group);
3006 3007
	struct se_hba *hba;

3008
	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3009

3010
	mutex_lock(&hba->hba_access_mutex);
3011

3012 3013
	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
3014

3015 3016 3017 3018
	/*
	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
	 * directly from target_core_alua_tg_pt_gp_release().
	 */
3019
	dev->t10_alua.default_tg_pt_gp = NULL;
3020

3021 3022
	configfs_remove_default_groups(dev_cg);

3023
	/*
3024
	 * se_dev is released from target_core_dev_item_ops->release()
3025
	 */
3026
	config_item_put(item);
3027 3028 3029 3030 3031 3032 3033 3034 3035
	mutex_unlock(&hba->hba_access_mutex);
}

static struct configfs_group_operations target_core_hba_group_ops = {
	.make_group		= target_core_make_subdev,
	.drop_item		= target_core_drop_subdev,
};


3036 3037 3038 3039
static inline struct se_hba *to_hba(struct config_item *item)
{
	return container_of(to_config_group(item), struct se_hba, hba_group);
}
3040

3041
static ssize_t target_hba_info_show(struct config_item *item, char *page)
3042
{
3043 3044
	struct se_hba *hba = to_hba(item);

3045
	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
3046
			hba->hba_id, hba->backend->ops->name,
3047
			TARGET_CORE_VERSION);
3048 3049
}

3050
static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3051
{
3052
	struct se_hba *hba = to_hba(item);
3053 3054 3055 3056 3057 3058 3059 3060
	int hba_mode = 0;

	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
		hba_mode = 1;

	return sprintf(page, "%d\n", hba_mode);
}

3061 3062
static ssize_t target_hba_mode_store(struct config_item *item,
		const char *page, size_t count)
3063
{
3064
	struct se_hba *hba = to_hba(item);
3065 3066 3067
	unsigned long mode_flag;
	int ret;

3068
	if (hba->backend->ops->pmode_enable_hba == NULL)
3069 3070
		return -EINVAL;

3071
	ret = kstrtoul(page, 0, &mode_flag);
3072
	if (ret < 0) {
3073
		pr_err("Unable to extract hba mode flag: %d\n", ret);
3074
		return ret;
3075 3076
	}

3077
	if (hba->dev_count) {
3078
		pr_err("Unable to set hba_mode with active devices\n");
3079 3080 3081
		return -EINVAL;
	}

3082
	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
	if (ret < 0)
		return -EINVAL;
	if (ret > 0)
		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
	else if (ret == 0)
		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;

	return count;
}

3093 3094
CONFIGFS_ATTR_RO(target_, hba_info);
CONFIGFS_ATTR(target_, hba_mode);
3095

3096 3097 3098 3099 3100 3101 3102
static void target_core_hba_release(struct config_item *item)
{
	struct se_hba *hba = container_of(to_config_group(item),
				struct se_hba, hba_group);
	core_delete_hba(hba);
}

3103
static struct configfs_attribute *target_core_hba_attrs[] = {
3104 3105
	&target_attr_hba_info,
	&target_attr_hba_mode,
3106 3107 3108 3109
	NULL,
};

static struct configfs_item_operations target_core_hba_item_ops = {
3110
	.release		= target_core_hba_release,
3111 3112
};

3113
static const struct config_item_type target_core_hba_cit = {
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130
	.ct_item_ops		= &target_core_hba_item_ops,
	.ct_group_ops		= &target_core_hba_group_ops,
	.ct_attrs		= target_core_hba_attrs,
	.ct_owner		= THIS_MODULE,
};

static struct config_group *target_core_call_addhbatotarget(
	struct config_group *group,
	const char *name)
{
	char *se_plugin_str, *str, *str2;
	struct se_hba *hba;
	char buf[TARGET_CORE_NAME_MAX_LEN];
	unsigned long plugin_dep_id = 0;
	int ret;

	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3131
	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3132
		pr_err("Passed *name strlen(): %d exceeds"
3133 3134 3135 3136 3137 3138 3139
			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
			TARGET_CORE_NAME_MAX_LEN);
		return ERR_PTR(-ENAMETOOLONG);
	}
	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);

	str = strstr(buf, "_");
3140 3141
	if (!str) {
		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3142 3143 3144 3145 3146 3147 3148 3149
		return ERR_PTR(-EINVAL);
	}
	se_plugin_str = buf;
	/*
	 * Special case for subsystem plugins that have "_" in their names.
	 * Namely rd_direct and rd_mcp..
	 */
	str2 = strstr(str+1, "_");
3150
	if (str2) {
3151 3152 3153 3154 3155 3156 3157 3158
		*str2 = '\0'; /* Terminate for *se_plugin_str */
		str2++; /* Skip to start of plugin dependent ID */
		str = str2;
	} else {
		*str = '\0'; /* Terminate for *se_plugin_str */
		str++; /* Skip to start of plugin dependent ID */
	}

3159
	ret = kstrtoul(str, 0, &plugin_dep_id);
3160
	if (ret < 0) {
3161
		pr_err("kstrtoul() returned %d for"
3162
				" plugin_dep_id\n", ret);
3163
		return ERR_PTR(ret);
3164 3165 3166 3167
	}
	/*
	 * Load up TCM subsystem plugins if they have not already been loaded.
	 */
3168
	transport_subsystem_check_init();
3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183

	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
	if (IS_ERR(hba))
		return ERR_CAST(hba);

	config_group_init_type_name(&hba->hba_group, name,
			&target_core_hba_cit);

	return &hba->hba_group;
}

static void target_core_call_delhbafromtarget(
	struct config_group *group,
	struct config_item *item)
{
3184 3185 3186 3187
	/*
	 * core_delete_hba() is called from target_core_hba_item_ops->release()
	 * -> target_core_hba_release()
	 */
3188 3189 3190 3191 3192 3193 3194 3195
	config_item_put(item);
}

static struct configfs_group_operations target_core_group_ops = {
	.make_group	= target_core_call_addhbatotarget,
	.drop_item	= target_core_call_delhbafromtarget,
};

3196
static const struct config_item_type target_core_cit = {
3197 3198 3199 3200 3201 3202 3203 3204
	.ct_item_ops	= NULL,
	.ct_group_ops	= &target_core_group_ops,
	.ct_attrs	= NULL,
	.ct_owner	= THIS_MODULE,
};

/* Stop functions for struct config_item_type target_core_hba_cit */

3205
void target_setup_backend_cits(struct target_backend *tb)
3206
{
3207
	target_core_setup_dev_cit(tb);
3208
	target_core_setup_dev_action_cit(tb);
3209 3210 3211 3212 3213
	target_core_setup_dev_attrib_cit(tb);
	target_core_setup_dev_pr_cit(tb);
	target_core_setup_dev_wwn_cit(tb);
	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
	target_core_setup_dev_stat_cit(tb);
3214 3215
}

3216
static int __init target_core_init_configfs(void)
3217
{
3218
	struct configfs_subsystem *subsys = &target_core_fabrics;
3219 3220 3221
	struct t10_alua_lu_gp *lu_gp;
	int ret;

3222
	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3223 3224 3225 3226 3227 3228
		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);

	config_group_init(&subsys->su_group);
	mutex_init(&subsys->su_mutex);

3229
	ret = init_se_kmem_caches();
3230
	if (ret < 0)
3231
		return ret;
3232 3233 3234 3235
	/*
	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
	 */
3236 3237 3238
	config_group_init_type_name(&target_core_hbagroup, "core",
			&target_core_cit);
	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3239 3240 3241 3242

	/*
	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
	 */
3243 3244 3245
	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
	configfs_add_default_group(&alua_group, &target_core_hbagroup);

3246 3247 3248 3249
	/*
	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
	 * groups under /sys/kernel/config/target/core/alua/
	 */
3250 3251 3252
	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
			&target_core_alua_lu_gps_cit);
	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3253 3254 3255 3256 3257

	/*
	 * Add core/alua/lu_gps/default_lu_gp
	 */
	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3258 3259
	if (IS_ERR(lu_gp)) {
		ret = -ENOMEM;
3260
		goto out_global;
3261
	}
3262 3263 3264

	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
				&target_core_alua_lu_gp_cit);
3265 3266
	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);

3267
	default_lu_gp = lu_gp;
3268

3269 3270 3271 3272 3273
	/*
	 * Register the target_core_mod subsystem with configfs.
	 */
	ret = configfs_register_subsystem(subsys);
	if (ret < 0) {
3274
		pr_err("Error %d while registering subsystem %s\n",
3275 3276 3277
			ret, subsys->su_group.cg_item.ci_namebuf);
		goto out_global;
	}
3278
	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3279
		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3280 3281 3282 3283 3284 3285 3286 3287
		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
	/*
	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
	 */
	ret = rd_module_init();
	if (ret < 0)
		goto out;

3288 3289
	ret = core_dev_setup_virtual_lun0();
	if (ret < 0)
3290 3291
		goto out;

3292 3293 3294 3295
	ret = target_xcopy_setup_pt();
	if (ret < 0)
		goto out;

3296 3297 3298 3299 3300 3301 3302
	return 0;

out:
	configfs_unregister_subsystem(subsys);
	core_dev_release_virtual_lun0();
	rd_module_exit();
out_global:
3303 3304 3305
	if (default_lu_gp) {
		core_alua_free_lu_gp(default_lu_gp);
		default_lu_gp = NULL;
3306
	}
3307 3308
	release_se_kmem_caches();
	return ret;
3309 3310
}

3311
static void __exit target_core_exit_configfs(void)
3312
{
3313 3314 3315
	configfs_remove_default_groups(&alua_lu_gps_group);
	configfs_remove_default_groups(&alua_group);
	configfs_remove_default_groups(&target_core_hbagroup);
3316

3317 3318 3319 3320
	/*
	 * We expect subsys->su_group.default_groups to be released
	 * by configfs subsystem provider logic..
	 */
3321
	configfs_unregister_subsystem(&target_core_fabrics);
3322

3323 3324
	core_alua_free_lu_gp(default_lu_gp);
	default_lu_gp = NULL;
3325

3326
	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3327 3328 3329 3330
			" Infrastructure\n");

	core_dev_release_virtual_lun0();
	rd_module_exit();
3331
	target_xcopy_release_pt();
3332
	release_se_kmem_caches();
3333 3334 3335 3336 3337 3338 3339 3340
}

MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
MODULE_AUTHOR("nab@Linux-iSCSI.org");
MODULE_LICENSE("GPL");

module_init(target_core_init_configfs);
module_exit(target_core_exit_configfs);