target_core_configfs.c 86.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

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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;
	}
	if (!S_ISDIR(fp->f_inode->i_mode)) {
		filp_close(fp, 0);
		mutex_unlock(&g_tf_lock);
		pr_err("db_root: not a directory: %s\n", db_root_stage);
		return -EINVAL;
	}
	filp_close(fp, 0);

	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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	NULL,
};

/*
 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
 */
static struct config_item_type target_core_fabrics_item = {
	.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 428
	/*
	 * 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.
	 */
429 430
	if (!tfo->fabric_make_wwn) {
		pr_err("Missing tfo->fabric_make_wwn()\n");
431 432
		return -EINVAL;
	}
433 434
	if (!tfo->fabric_drop_wwn) {
		pr_err("Missing tfo->fabric_drop_wwn()\n");
435 436
		return -EINVAL;
	}
437 438
	if (!tfo->fabric_make_tpg) {
		pr_err("Missing tfo->fabric_make_tpg()\n");
439 440
		return -EINVAL;
	}
441 442
	if (!tfo->fabric_drop_tpg) {
		pr_err("Missing tfo->fabric_drop_tpg()\n");
443 444 445 446 447 448
		return -EINVAL;
	}

	return 0;
}

449
int target_register_template(const struct target_core_fabric_ops *fo)
450
{
451
	struct target_fabric_configfs *tf;
452 453
	int ret;

454 455 456 457 458
	ret = target_fabric_tf_ops_check(fo);
	if (ret)
		return ret;

	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
459
	if (!tf) {
460 461
		pr_err("%s: could not allocate memory!\n", __func__);
		return -ENOMEM;
462 463
	}

464 465
	INIT_LIST_HEAD(&tf->tf_list);
	atomic_set(&tf->tf_access_cnt, 0);
466
	tf->tf_ops = fo;
467 468 469 470 471 472
	target_fabric_setup_cits(tf);

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

473 474
	return 0;
}
475
EXPORT_SYMBOL(target_register_template);
476

477
void target_unregister_template(const struct target_core_fabric_ops *fo)
478
{
479
	struct target_fabric_configfs *t;
480 481

	mutex_lock(&g_tf_lock);
482
	list_for_each_entry(t, &g_tf_list, tf_list) {
483
		if (!strcmp(t->tf_ops->name, fo->name)) {
484 485
			BUG_ON(atomic_read(&t->tf_access_cnt));
			list_del(&t->tf_list);
486 487 488 489 490 491 492
			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();
493
			kfree(t);
494
			return;
495
		}
496 497 498
	}
	mutex_unlock(&g_tf_lock);
}
499
EXPORT_SYMBOL(target_unregister_template);
500 501 502 503 504

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

505 506 507 508 509 510
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);
}

511
/* Start functions for struct config_item_type tb_dev_attrib_cit */
512 513
#define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
static ssize_t _name##_show(struct config_item *item, char *page)	\
514
{									\
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	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);
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);
546
DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
547 548 549 550
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,\
551
		size_t count)						\
552
{									\
553
	struct se_dev_attrib *da = to_attrib(item);			\
554
	u32 val;							\
555 556
	int ret;							\
									\
557 558 559 560 561 562 563
	ret = kstrtou32(page, 0, &val);					\
	if (ret < 0)							\
		return ret;						\
	da->_name = val;						\
	return count;							\
}

564 565 566 567 568
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);
569

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

585 586 587 588 589
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);
590

591 592
#define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
static ssize_t _name##_store(struct config_item *item, const char *page,\
593 594 595
		size_t count)						\
{									\
	printk_once(KERN_WARNING					\
596 597
		"ignoring deprecated %s attribute\n",			\
		__stringify(_name));					\
598 599 600
	return count;							\
}

601 602
DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
603 604 605 606 607 608 609 610 611 612 613 614 615 616

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);
}

617
static ssize_t emulate_model_alias_store(struct config_item *item,
618 619
		const char *page, size_t count)
{
620
	struct se_dev_attrib *da = to_attrib(item);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
	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;
}

646
static ssize_t emulate_write_cache_store(struct config_item *item,
647 648
		const char *page, size_t count)
{
649
	struct se_dev_attrib *da = to_attrib(item);
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
	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;
}

668
static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
669 670
		const char *page, size_t count)
{
671
	struct se_dev_attrib *da = to_attrib(item);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
	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;
}

696
static ssize_t emulate_tas_store(struct config_item *item,
697 698
		const char *page, size_t count)
{
699
	struct se_dev_attrib *da = to_attrib(item);
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
	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;
}

720
static ssize_t emulate_tpu_store(struct config_item *item,
721 722
		const char *page, size_t count)
{
723
	struct se_dev_attrib *da = to_attrib(item);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
	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;
}

746
static ssize_t emulate_tpws_store(struct config_item *item,
747 748
		const char *page, size_t count)
{
749
	struct se_dev_attrib *da = to_attrib(item);
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	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;
}

772
static ssize_t pi_prot_type_store(struct config_item *item,
773 774
		const char *page, size_t count)
{
775
	struct se_dev_attrib *da = to_attrib(item);
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
	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)
800
			return count;
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832

		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;
			return ret;
		}

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

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

833
static ssize_t pi_prot_format_store(struct config_item *item,
834 835
		const char *page, size_t count)
{
836
	struct se_dev_attrib *da = to_attrib(item);
837 838 839 840 841 842 843 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
	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;
}

871
static ssize_t force_pr_aptpl_store(struct config_item *item,
872 873
		const char *page, size_t count)
{
874
	struct se_dev_attrib *da = to_attrib(item);
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	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;
}

893
static ssize_t emulate_rest_reord_store(struct config_item *item,
894 895
		const char *page, size_t count)
{
896
	struct se_dev_attrib *da = to_attrib(item);
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
	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;
}

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
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);
940
		return -ENOSYS;
941 942 943 944
	}
	da->unmap_zeroes_data = flag;
	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
		 da->da_dev, flag);
945
	return count;
946 947
}

948 949 950
/*
 * Note, this can only be called on unexported SE Device Object.
 */
951
static ssize_t queue_depth_store(struct config_item *item,
952 953
		const char *page, size_t count)
{
954
	struct se_dev_attrib *da = to_attrib(item);
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	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;
}

988
static ssize_t optimal_sectors_store(struct config_item *item,
989 990
		const char *page, size_t count)
{
991
	struct se_dev_attrib *da = to_attrib(item);
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	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;
1016 1017
}

1018
static ssize_t block_size_store(struct config_item *item,
1019 1020
		const char *page, size_t count)
{
1021
	struct se_dev_attrib *da = to_attrib(item);
1022 1023 1024 1025 1026 1027
	u32 val;
	int ret;

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

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	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;
}
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
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);
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);
1080
CONFIGFS_ATTR(, unmap_zeroes_data);
1081
CONFIGFS_ATTR(, max_write_same_len);
1082

1083 1084 1085 1086 1087 1088
/*
 * 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[] = {
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	&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,
	&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,
1117
	&attr_unmap_zeroes_data,
1118
	&attr_max_write_same_len,
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	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[] = {
1129 1130 1131 1132
	&attr_hw_pi_prot_type,
	&attr_hw_block_size,
	&attr_hw_max_sectors,
	&attr_hw_queue_depth,
1133 1134 1135 1136
	NULL,
};
EXPORT_SYMBOL(passthrough_attrib_attrs);

1137
TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1138

1139
/* End functions for struct config_item_type tb_dev_attrib_cit */
1140

1141
/*  Start functions for struct config_item_type tb_dev_wwn_cit */
1142

1143 1144 1145 1146
static struct t10_wwn *to_t10_wwn(struct config_item *item)
{
	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
}
1147 1148 1149 1150

/*
 * VPD page 0x80 Unit serial
 */
1151 1152
static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
		char *page)
1153 1154
{
	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
1155
		&to_t10_wwn(item)->unit_serial[0]);
1156 1157
}

1158 1159
static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
		const char *page, size_t count)
1160
{
1161
	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1162
	struct se_device *dev = t10_wwn->t10_dev;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	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.
	 */
1175
	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
1176
		pr_err("Underlying SCSI device firmware provided VPD"
1177 1178 1179 1180
			" Unit Serial, ignoring request\n");
		return -EOPNOTSUPP;
	}

1181
	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
1182
		pr_err("Emulated VPD Unit Serial exceeds"
1183 1184 1185 1186 1187 1188 1189 1190 1191
		" 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.
	 */
1192 1193 1194 1195 1196
	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;
1197
	}
1198

1199 1200 1201 1202 1203 1204 1205 1206
	/*
	 * 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);
1207
	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1208
			"%s", strstrip(buf));
1209
	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1210

1211
	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
1212
			" %s\n", dev->t10_wwn.unit_serial);
1213 1214 1215 1216 1217 1218 1219

	return count;
}

/*
 * VPD page 0x83 Protocol Identifier
 */
1220 1221
static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
		char *page)
1222
{
1223
	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1224 1225 1226 1227 1228 1229 1230 1231
	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) {
1232
		if (!vpd->protocol_identifier_set)
1233 1234 1235 1236
			continue;

		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);

1237
		if (len + strlen(buf) >= PAGE_SIZE)
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
			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)				\
1251 1252
static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
		char *page)						\
1253
{									\
1254 1255
	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
	struct t10_vpd *vpd;						\
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	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);	\
1266
		if (len + strlen(buf) >= PAGE_SIZE)			\
1267 1268 1269 1270 1271
			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); \
1272
		if (len + strlen(buf) >= PAGE_SIZE)			\
1273 1274 1275 1276 1277
			break;						\
		len += sprintf(page+len, "%s", buf);			\
									\
		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1278
		if (len + strlen(buf) >= PAGE_SIZE)			\
1279 1280 1281 1282 1283 1284 1285 1286
			break;						\
		len += sprintf(page+len, "%s", buf);			\
	}								\
	spin_unlock(&t10_wwn->t10_vpd_lock);				\
									\
	return len;							\
}

1287
/* VPD page 0x83 Association: Logical Unit */
1288
DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1289
/* VPD page 0x83 Association: Target Port */
1290
DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1291
/* VPD page 0x83 Association: SCSI Target Device */
1292 1293
DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);

1294 1295 1296 1297 1298
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);
1299 1300

static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1301 1302 1303 1304 1305
	&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,
1306 1307 1308
	NULL,
};

1309
TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1310

1311
/*  End functions for struct config_item_type tb_dev_wwn_cit */
1312

1313
/*  Start functions for struct config_item_type tb_dev_pr_cit */
1314

1315 1316 1317 1318 1319
static struct se_device *pr_to_dev(struct config_item *item)
{
	return container_of(to_config_group(item), struct se_device,
			dev_pr_group);
}
1320

1321 1322
static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
		char *page)
1323 1324 1325 1326 1327 1328 1329 1330
{
	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;
1331 1332 1333
	if (!pr_reg)
		return sprintf(page, "No SPC-3 Reservation holder\n");

1334
	se_nacl = pr_reg->pr_reg_nacl;
1335
	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1336

1337
	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1338
		se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1339
		se_nacl->initiatorname, i_buf);
1340 1341
}

1342 1343
static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
		char *page)
1344 1345
{
	struct se_node_acl *se_nacl;
1346
	ssize_t len;
1347 1348

	se_nacl = dev->dev_reserved_node_acl;
1349 1350 1351 1352 1353 1354 1355
	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");
1356
	}
1357
	return len;
1358 1359
}

1360
static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1361
{
1362
	struct se_device *dev = pr_to_dev(item);
1363
	int ret;
1364

1365
	if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1366
		return sprintf(page, "Passthrough\n");
1367

1368 1369 1370 1371 1372 1373 1374
	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;
1375 1376
}

1377 1378
static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
		char *page)
1379
{
1380
	struct se_device *dev = pr_to_dev(item);
1381 1382 1383
	ssize_t len = 0;

	spin_lock(&dev->dev_reservation_lock);
1384
	if (!dev->dev_pr_res_holder) {
1385
		len = sprintf(page, "No SPC-3 Reservation holder\n");
1386
	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1387 1388
		len = sprintf(page, "SPC-3 Reservation: All Target"
			" Ports registration\n");
1389
	} else {
1390 1391
		len = sprintf(page, "SPC-3 Reservation: Single"
			" Target Port registration\n");
1392
	}
1393

1394
	spin_unlock(&dev->dev_reservation_lock);
1395 1396 1397
	return len;
}

1398 1399
static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
		char *page)
1400
{
1401
	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1402 1403 1404
}