1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_configfs.c
4  *
5  * This file contains ConfigFS logic for the Generic Target Engine project.
6  *
7  * (c) Copyright 2008-2013 Datera, Inc.
8  *
9  * Nicholas A. Bellinger <nab@kernel.org>
10  *
11  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
12  *
13  ****************************************************************************/
14 
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <generated/utsrelease.h>
18 #include <linux/utsname.h>
19 #include <linux/init.h>
20 #include <linux/fs.h>
21 #include <linux/namei.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/delay.h>
25 #include <linux/unistd.h>
26 #include <linux/string.h>
27 #include <linux/parser.h>
28 #include <linux/syscalls.h>
29 #include <linux/configfs.h>
30 #include <linux/spinlock.h>
31 
32 #include <target/target_core_base.h>
33 #include <target/target_core_backend.h>
34 #include <target/target_core_fabric.h>
35 
36 #include "target_core_internal.h"
37 #include "target_core_alua.h"
38 #include "target_core_pr.h"
39 #include "target_core_rd.h"
40 #include "target_core_xcopy.h"
41 
42 #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
43 static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
44 {									\
45 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
46 									\
47 	cit->ct_item_ops = _item_ops;					\
48 	cit->ct_group_ops = _group_ops;					\
49 	cit->ct_attrs = _attrs;						\
50 	cit->ct_owner = tb->ops->owner;					\
51 	pr_debug("Setup generic %s\n", __stringify(_name));		\
52 }
53 
54 #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops)			\
55 static void target_core_setup_##_name##_cit(struct target_backend *tb)	\
56 {									\
57 	struct config_item_type *cit = &tb->tb_##_name##_cit;		\
58 									\
59 	cit->ct_item_ops = _item_ops;					\
60 	cit->ct_group_ops = _group_ops;					\
61 	cit->ct_attrs = tb->ops->tb_##_name##_attrs;			\
62 	cit->ct_owner = tb->ops->owner;					\
63 	pr_debug("Setup generic %s\n", __stringify(_name));		\
64 }
65 
66 extern struct t10_alua_lu_gp *default_lu_gp;
67 
68 static LIST_HEAD(g_tf_list);
69 static DEFINE_MUTEX(g_tf_lock);
70 
71 static struct config_group target_core_hbagroup;
72 static struct config_group alua_group;
73 static struct config_group alua_lu_gps_group;
74 
75 static unsigned int target_devices;
76 static DEFINE_MUTEX(target_devices_lock);
77 
78 static inline struct se_hba *
79 item_to_hba(struct config_item *item)
80 {
81 	return container_of(to_config_group(item), struct se_hba, hba_group);
82 }
83 
84 /*
85  * Attributes for /sys/kernel/config/target/
86  */
87 static ssize_t target_core_item_version_show(struct config_item *item,
88 		char *page)
89 {
90 	return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
91 		" on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
92 		utsname()->sysname, utsname()->machine);
93 }
94 
95 CONFIGFS_ATTR_RO(target_core_item_, version);
96 
97 char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
98 static char db_root_stage[DB_ROOT_LEN];
99 
100 static ssize_t target_core_item_dbroot_show(struct config_item *item,
101 					    char *page)
102 {
103 	return sprintf(page, "%s\n", db_root);
104 }
105 
106 static ssize_t target_core_item_dbroot_store(struct config_item *item,
107 					const char *page, size_t count)
108 {
109 	ssize_t read_bytes;
110 	struct file *fp;
111 	ssize_t r = -EINVAL;
112 
113 	mutex_lock(&target_devices_lock);
114 	if (target_devices) {
115 		pr_err("db_root: cannot be changed because it's in use\n");
116 		goto unlock;
117 	}
118 
119 	if (count > (DB_ROOT_LEN - 1)) {
120 		pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
121 		       (int)count, DB_ROOT_LEN - 1);
122 		goto unlock;
123 	}
124 
125 	read_bytes = snprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
126 	if (!read_bytes)
127 		goto unlock;
128 
129 	if (db_root_stage[read_bytes - 1] == '\n')
130 		db_root_stage[read_bytes - 1] = '\0';
131 
132 	/* validate new db root before accepting it */
133 	fp = filp_open(db_root_stage, O_RDONLY, 0);
134 	if (IS_ERR(fp)) {
135 		pr_err("db_root: cannot open: %s\n", db_root_stage);
136 		goto unlock;
137 	}
138 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
139 		filp_close(fp, NULL);
140 		pr_err("db_root: not a directory: %s\n", db_root_stage);
141 		goto unlock;
142 	}
143 	filp_close(fp, NULL);
144 
145 	strncpy(db_root, db_root_stage, read_bytes);
146 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
147 
148 	r = read_bytes;
149 
150 unlock:
151 	mutex_unlock(&target_devices_lock);
152 	return r;
153 }
154 
155 CONFIGFS_ATTR(target_core_item_, dbroot);
156 
157 static struct target_fabric_configfs *target_core_get_fabric(
158 	const char *name)
159 {
160 	struct target_fabric_configfs *tf;
161 
162 	if (!name)
163 		return NULL;
164 
165 	mutex_lock(&g_tf_lock);
166 	list_for_each_entry(tf, &g_tf_list, tf_list) {
167 		const char *cmp_name = tf->tf_ops->fabric_alias;
168 		if (!cmp_name)
169 			cmp_name = tf->tf_ops->fabric_name;
170 		if (!strcmp(cmp_name, name)) {
171 			atomic_inc(&tf->tf_access_cnt);
172 			mutex_unlock(&g_tf_lock);
173 			return tf;
174 		}
175 	}
176 	mutex_unlock(&g_tf_lock);
177 
178 	return NULL;
179 }
180 
181 /*
182  * Called from struct target_core_group_ops->make_group()
183  */
184 static struct config_group *target_core_register_fabric(
185 	struct config_group *group,
186 	const char *name)
187 {
188 	struct target_fabric_configfs *tf;
189 	int ret;
190 
191 	pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
192 			" %s\n", group, name);
193 
194 	tf = target_core_get_fabric(name);
195 	if (!tf) {
196 		pr_debug("target_core_register_fabric() trying autoload for %s\n",
197 			 name);
198 
199 		/*
200 		 * Below are some hardcoded request_module() calls to automatically
201 		 * local fabric modules when the following is called:
202 		 *
203 		 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
204 		 *
205 		 * Note that this does not limit which TCM fabric module can be
206 		 * registered, but simply provids auto loading logic for modules with
207 		 * mkdir(2) system calls with known TCM fabric modules.
208 		 */
209 
210 		if (!strncmp(name, "iscsi", 5)) {
211 			/*
212 			 * Automatically load the LIO Target fabric module when the
213 			 * following is called:
214 			 *
215 			 * mkdir -p $CONFIGFS/target/iscsi
216 			 */
217 			ret = request_module("iscsi_target_mod");
218 			if (ret < 0) {
219 				pr_debug("request_module() failed for"
220 				         " iscsi_target_mod.ko: %d\n", ret);
221 				return ERR_PTR(-EINVAL);
222 			}
223 		} else if (!strncmp(name, "loopback", 8)) {
224 			/*
225 			 * Automatically load the tcm_loop fabric module when the
226 			 * following is called:
227 			 *
228 			 * mkdir -p $CONFIGFS/target/loopback
229 			 */
230 			ret = request_module("tcm_loop");
231 			if (ret < 0) {
232 				pr_debug("request_module() failed for"
233 				         " tcm_loop.ko: %d\n", ret);
234 				return ERR_PTR(-EINVAL);
235 			}
236 		}
237 
238 		tf = target_core_get_fabric(name);
239 	}
240 
241 	if (!tf) {
242 		pr_debug("target_core_get_fabric() failed for %s\n",
243 		         name);
244 		return ERR_PTR(-EINVAL);
245 	}
246 	pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
247 			" %s\n", tf->tf_ops->fabric_name);
248 	/*
249 	 * On a successful target_core_get_fabric() look, the returned
250 	 * struct target_fabric_configfs *tf will contain a usage reference.
251 	 */
252 	pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
253 			&tf->tf_wwn_cit);
254 
255 	config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
256 
257 	config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
258 			&tf->tf_discovery_cit);
259 	configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
260 
261 	pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
262 		 config_item_name(&tf->tf_group.cg_item));
263 	return &tf->tf_group;
264 }
265 
266 /*
267  * Called from struct target_core_group_ops->drop_item()
268  */
269 static void target_core_deregister_fabric(
270 	struct config_group *group,
271 	struct config_item *item)
272 {
273 	struct target_fabric_configfs *tf = container_of(
274 		to_config_group(item), struct target_fabric_configfs, tf_group);
275 
276 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
277 		" tf list\n", config_item_name(item));
278 
279 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
280 			" %s\n", tf->tf_ops->fabric_name);
281 	atomic_dec(&tf->tf_access_cnt);
282 
283 	pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
284 			" %s\n", config_item_name(item));
285 
286 	configfs_remove_default_groups(&tf->tf_group);
287 	config_item_put(item);
288 }
289 
290 static struct configfs_group_operations target_core_fabric_group_ops = {
291 	.make_group	= &target_core_register_fabric,
292 	.drop_item	= &target_core_deregister_fabric,
293 };
294 
295 /*
296  * All item attributes appearing in /sys/kernel/target/ appear here.
297  */
298 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
299 	&target_core_item_attr_version,
300 	&target_core_item_attr_dbroot,
301 	NULL,
302 };
303 
304 /*
305  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
306  */
307 static const struct config_item_type target_core_fabrics_item = {
308 	.ct_group_ops	= &target_core_fabric_group_ops,
309 	.ct_attrs	= target_core_fabric_item_attrs,
310 	.ct_owner	= THIS_MODULE,
311 };
312 
313 static struct configfs_subsystem target_core_fabrics = {
314 	.su_group = {
315 		.cg_item = {
316 			.ci_namebuf = "target",
317 			.ci_type = &target_core_fabrics_item,
318 		},
319 	},
320 };
321 
322 int target_depend_item(struct config_item *item)
323 {
324 	return configfs_depend_item(&target_core_fabrics, item);
325 }
326 EXPORT_SYMBOL(target_depend_item);
327 
328 void target_undepend_item(struct config_item *item)
329 {
330 	return configfs_undepend_item(item);
331 }
332 EXPORT_SYMBOL(target_undepend_item);
333 
334 /*##############################################################################
335 // Start functions called by external Target Fabrics Modules
336 //############################################################################*/
337 
338 static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
339 {
340 	if (tfo->fabric_alias) {
341 		if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
342 			pr_err("Passed alias: %s exceeds "
343 				"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
344 			return -EINVAL;
345 		}
346 	}
347 	if (!tfo->fabric_name) {
348 		pr_err("Missing tfo->fabric_name\n");
349 		return -EINVAL;
350 	}
351 	if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
352 		pr_err("Passed name: %s exceeds "
353 			"TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
354 		return -EINVAL;
355 	}
356 	if (!tfo->tpg_get_wwn) {
357 		pr_err("Missing tfo->tpg_get_wwn()\n");
358 		return -EINVAL;
359 	}
360 	if (!tfo->tpg_get_tag) {
361 		pr_err("Missing tfo->tpg_get_tag()\n");
362 		return -EINVAL;
363 	}
364 	if (!tfo->tpg_check_demo_mode) {
365 		pr_err("Missing tfo->tpg_check_demo_mode()\n");
366 		return -EINVAL;
367 	}
368 	if (!tfo->tpg_check_demo_mode_cache) {
369 		pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
370 		return -EINVAL;
371 	}
372 	if (!tfo->tpg_check_demo_mode_write_protect) {
373 		pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
374 		return -EINVAL;
375 	}
376 	if (!tfo->tpg_check_prod_mode_write_protect) {
377 		pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
378 		return -EINVAL;
379 	}
380 	if (!tfo->tpg_get_inst_index) {
381 		pr_err("Missing tfo->tpg_get_inst_index()\n");
382 		return -EINVAL;
383 	}
384 	if (!tfo->release_cmd) {
385 		pr_err("Missing tfo->release_cmd()\n");
386 		return -EINVAL;
387 	}
388 	if (!tfo->sess_get_index) {
389 		pr_err("Missing tfo->sess_get_index()\n");
390 		return -EINVAL;
391 	}
392 	if (!tfo->write_pending) {
393 		pr_err("Missing tfo->write_pending()\n");
394 		return -EINVAL;
395 	}
396 	if (!tfo->set_default_node_attributes) {
397 		pr_err("Missing tfo->set_default_node_attributes()\n");
398 		return -EINVAL;
399 	}
400 	if (!tfo->get_cmd_state) {
401 		pr_err("Missing tfo->get_cmd_state()\n");
402 		return -EINVAL;
403 	}
404 	if (!tfo->queue_data_in) {
405 		pr_err("Missing tfo->queue_data_in()\n");
406 		return -EINVAL;
407 	}
408 	if (!tfo->queue_status) {
409 		pr_err("Missing tfo->queue_status()\n");
410 		return -EINVAL;
411 	}
412 	if (!tfo->queue_tm_rsp) {
413 		pr_err("Missing tfo->queue_tm_rsp()\n");
414 		return -EINVAL;
415 	}
416 	if (!tfo->aborted_task) {
417 		pr_err("Missing tfo->aborted_task()\n");
418 		return -EINVAL;
419 	}
420 	if (!tfo->check_stop_free) {
421 		pr_err("Missing tfo->check_stop_free()\n");
422 		return -EINVAL;
423 	}
424 	/*
425 	 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
426 	 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
427 	 * target_core_fabric_configfs.c WWN+TPG group context code.
428 	 */
429 	if (!tfo->fabric_make_wwn) {
430 		pr_err("Missing tfo->fabric_make_wwn()\n");
431 		return -EINVAL;
432 	}
433 	if (!tfo->fabric_drop_wwn) {
434 		pr_err("Missing tfo->fabric_drop_wwn()\n");
435 		return -EINVAL;
436 	}
437 	if (!tfo->fabric_make_tpg) {
438 		pr_err("Missing tfo->fabric_make_tpg()\n");
439 		return -EINVAL;
440 	}
441 	if (!tfo->fabric_drop_tpg) {
442 		pr_err("Missing tfo->fabric_drop_tpg()\n");
443 		return -EINVAL;
444 	}
445 
446 	return 0;
447 }
448 
449 int target_register_template(const struct target_core_fabric_ops *fo)
450 {
451 	struct target_fabric_configfs *tf;
452 	int ret;
453 
454 	ret = target_fabric_tf_ops_check(fo);
455 	if (ret)
456 		return ret;
457 
458 	tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
459 	if (!tf) {
460 		pr_err("%s: could not allocate memory!\n", __func__);
461 		return -ENOMEM;
462 	}
463 
464 	INIT_LIST_HEAD(&tf->tf_list);
465 	atomic_set(&tf->tf_access_cnt, 0);
466 	tf->tf_ops = fo;
467 	target_fabric_setup_cits(tf);
468 
469 	mutex_lock(&g_tf_lock);
470 	list_add_tail(&tf->tf_list, &g_tf_list);
471 	mutex_unlock(&g_tf_lock);
472 
473 	return 0;
474 }
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->fabric_name, fo->fabric_name)) {
484 			BUG_ON(atomic_read(&t->tf_access_cnt));
485 			list_del(&t->tf_list);
486 			mutex_unlock(&g_tf_lock);
487 			/*
488 			 * Wait for any outstanding fabric se_deve_entry->rcu_head
489 			 * callbacks to complete post kfree_rcu(), before allowing
490 			 * fabric driver unload of TFO->module to proceed.
491 			 */
492 			rcu_barrier();
493 			kfree(t->tf_tpg_base_cit.ct_attrs);
494 			kfree(t);
495 			return;
496 		}
497 	}
498 	mutex_unlock(&g_tf_lock);
499 }
500 EXPORT_SYMBOL(target_unregister_template);
501 
502 /*##############################################################################
503 // Stop functions called by external Target Fabrics Modules
504 //############################################################################*/
505 
506 static inline struct se_dev_attrib *to_attrib(struct config_item *item)
507 {
508 	return container_of(to_config_group(item), struct se_dev_attrib,
509 			da_group);
510 }
511 
512 /* Start functions for struct config_item_type tb_dev_attrib_cit */
513 #define DEF_CONFIGFS_ATTRIB_SHOW(_name)					\
514 static ssize_t _name##_show(struct config_item *item, char *page)	\
515 {									\
516 	return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
517 }
518 
519 DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
520 DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
521 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
522 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
523 DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
524 DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
525 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
526 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
527 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
528 DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
529 DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
530 DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
531 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
532 DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
533 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
534 DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
535 DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
536 DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
537 DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
538 DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
539 DEF_CONFIGFS_ATTRIB_SHOW(block_size);
540 DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
541 DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
542 DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
543 DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
544 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
545 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
546 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
547 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
548 DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
549 DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
550 
551 #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name)				\
552 static ssize_t _name##_store(struct config_item *item, const char *page,\
553 		size_t count)						\
554 {									\
555 	struct se_dev_attrib *da = to_attrib(item);			\
556 	u32 val;							\
557 	int ret;							\
558 									\
559 	ret = kstrtou32(page, 0, &val);					\
560 	if (ret < 0)							\
561 		return ret;						\
562 	da->_name = val;						\
563 	return count;							\
564 }
565 
566 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
567 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
568 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
569 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
570 DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
571 
572 #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name)				\
573 static ssize_t _name##_store(struct config_item *item, const char *page,	\
574 		size_t count)						\
575 {									\
576 	struct se_dev_attrib *da = to_attrib(item);			\
577 	bool flag;							\
578 	int ret;							\
579 									\
580 	ret = strtobool(page, &flag);					\
581 	if (ret < 0)							\
582 		return ret;						\
583 	da->_name = flag;						\
584 	return count;							\
585 }
586 
587 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
588 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
589 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
590 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
591 DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
592 DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
593 
594 #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name)				\
595 static ssize_t _name##_store(struct config_item *item, const char *page,\
596 		size_t count)						\
597 {									\
598 	printk_once(KERN_WARNING					\
599 		"ignoring deprecated %s attribute\n",			\
600 		__stringify(_name));					\
601 	return count;							\
602 }
603 
604 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
605 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
606 
607 static void dev_set_t10_wwn_model_alias(struct se_device *dev)
608 {
609 	const char *configname;
610 
611 	configname = config_item_name(&dev->dev_group.cg_item);
612 	if (strlen(configname) >= INQUIRY_MODEL_LEN) {
613 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
614 			"INQUIRY_MODEL, truncating to 15 characters\n", dev,
615 			configname);
616 	}
617 	/*
618 	 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
619 	 * here without potentially breaking existing setups, so continue to
620 	 * truncate one byte shorter than what can be carried in INQUIRY.
621 	 */
622 	strlcpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
623 }
624 
625 static ssize_t emulate_model_alias_store(struct config_item *item,
626 		const char *page, size_t count)
627 {
628 	struct se_dev_attrib *da = to_attrib(item);
629 	struct se_device *dev = da->da_dev;
630 	bool flag;
631 	int ret;
632 
633 	if (dev->export_count) {
634 		pr_err("dev[%p]: Unable to change model alias"
635 			" while export_count is %d\n",
636 			dev, dev->export_count);
637 		return -EINVAL;
638 	}
639 
640 	ret = strtobool(page, &flag);
641 	if (ret < 0)
642 		return ret;
643 
644 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
645 	if (flag) {
646 		dev_set_t10_wwn_model_alias(dev);
647 	} else {
648 		strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
649 			sizeof(dev->t10_wwn.model));
650 	}
651 	da->emulate_model_alias = flag;
652 	return count;
653 }
654 
655 static ssize_t emulate_write_cache_store(struct config_item *item,
656 		const char *page, size_t count)
657 {
658 	struct se_dev_attrib *da = to_attrib(item);
659 	bool flag;
660 	int ret;
661 
662 	ret = strtobool(page, &flag);
663 	if (ret < 0)
664 		return ret;
665 
666 	if (flag && da->da_dev->transport->get_write_cache) {
667 		pr_err("emulate_write_cache not supported for this device\n");
668 		return -EINVAL;
669 	}
670 
671 	da->emulate_write_cache = flag;
672 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
673 			da->da_dev, flag);
674 	return count;
675 }
676 
677 static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
678 		const char *page, size_t count)
679 {
680 	struct se_dev_attrib *da = to_attrib(item);
681 	u32 val;
682 	int ret;
683 
684 	ret = kstrtou32(page, 0, &val);
685 	if (ret < 0)
686 		return ret;
687 
688 	if (val != TARGET_UA_INTLCK_CTRL_CLEAR
689 	 && val != TARGET_UA_INTLCK_CTRL_NO_CLEAR
690 	 && val != TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
691 		pr_err("Illegal value %d\n", val);
692 		return -EINVAL;
693 	}
694 
695 	if (da->da_dev->export_count) {
696 		pr_err("dev[%p]: Unable to change SE Device"
697 			" UA_INTRLCK_CTRL while export_count is %d\n",
698 			da->da_dev, da->da_dev->export_count);
699 		return -EINVAL;
700 	}
701 	da->emulate_ua_intlck_ctrl = val;
702 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
703 		da->da_dev, val);
704 	return count;
705 }
706 
707 static ssize_t emulate_tas_store(struct config_item *item,
708 		const char *page, size_t count)
709 {
710 	struct se_dev_attrib *da = to_attrib(item);
711 	bool flag;
712 	int ret;
713 
714 	ret = strtobool(page, &flag);
715 	if (ret < 0)
716 		return ret;
717 
718 	if (da->da_dev->export_count) {
719 		pr_err("dev[%p]: Unable to change SE Device TAS while"
720 			" export_count is %d\n",
721 			da->da_dev, da->da_dev->export_count);
722 		return -EINVAL;
723 	}
724 	da->emulate_tas = flag;
725 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
726 		da->da_dev, flag ? "Enabled" : "Disabled");
727 
728 	return count;
729 }
730 
731 static ssize_t emulate_tpu_store(struct config_item *item,
732 		const char *page, size_t count)
733 {
734 	struct se_dev_attrib *da = to_attrib(item);
735 	struct se_device *dev = da->da_dev;
736 	bool flag;
737 	int ret;
738 
739 	ret = strtobool(page, &flag);
740 	if (ret < 0)
741 		return ret;
742 
743 	/*
744 	 * We expect this value to be non-zero when generic Block Layer
745 	 * Discard supported is detected iblock_create_virtdevice().
746 	 */
747 	if (flag && !da->max_unmap_block_desc_count) {
748 		if (!dev->transport->configure_unmap ||
749 		    !dev->transport->configure_unmap(dev)) {
750 			pr_err("Generic Block Discard not supported\n");
751 			return -ENOSYS;
752 		}
753 	}
754 
755 	da->emulate_tpu = flag;
756 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
757 		da->da_dev, flag);
758 	return count;
759 }
760 
761 static ssize_t emulate_tpws_store(struct config_item *item,
762 		const char *page, size_t count)
763 {
764 	struct se_dev_attrib *da = to_attrib(item);
765 	struct se_device *dev = da->da_dev;
766 	bool flag;
767 	int ret;
768 
769 	ret = strtobool(page, &flag);
770 	if (ret < 0)
771 		return ret;
772 
773 	/*
774 	 * We expect this value to be non-zero when generic Block Layer
775 	 * Discard supported is detected iblock_create_virtdevice().
776 	 */
777 	if (flag && !da->max_unmap_block_desc_count) {
778 		if (!dev->transport->configure_unmap ||
779 		    !dev->transport->configure_unmap(dev)) {
780 			pr_err("Generic Block Discard not supported\n");
781 			return -ENOSYS;
782 		}
783 	}
784 
785 	da->emulate_tpws = flag;
786 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
787 				da->da_dev, flag);
788 	return count;
789 }
790 
791 static ssize_t pi_prot_type_store(struct config_item *item,
792 		const char *page, size_t count)
793 {
794 	struct se_dev_attrib *da = to_attrib(item);
795 	int old_prot = da->pi_prot_type, ret;
796 	struct se_device *dev = da->da_dev;
797 	u32 flag;
798 
799 	ret = kstrtou32(page, 0, &flag);
800 	if (ret < 0)
801 		return ret;
802 
803 	if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
804 		pr_err("Illegal value %d for pi_prot_type\n", flag);
805 		return -EINVAL;
806 	}
807 	if (flag == 2) {
808 		pr_err("DIF TYPE2 protection currently not supported\n");
809 		return -ENOSYS;
810 	}
811 	if (da->hw_pi_prot_type) {
812 		pr_warn("DIF protection enabled on underlying hardware,"
813 			" ignoring\n");
814 		return count;
815 	}
816 	if (!dev->transport->init_prot || !dev->transport->free_prot) {
817 		/* 0 is only allowed value for non-supporting backends */
818 		if (flag == 0)
819 			return count;
820 
821 		pr_err("DIF protection not supported by backend: %s\n",
822 		       dev->transport->name);
823 		return -ENOSYS;
824 	}
825 	if (!target_dev_configured(dev)) {
826 		pr_err("DIF protection requires device to be configured\n");
827 		return -ENODEV;
828 	}
829 	if (dev->export_count) {
830 		pr_err("dev[%p]: Unable to change SE Device PROT type while"
831 		       " export_count is %d\n", dev, dev->export_count);
832 		return -EINVAL;
833 	}
834 
835 	da->pi_prot_type = flag;
836 
837 	if (flag && !old_prot) {
838 		ret = dev->transport->init_prot(dev);
839 		if (ret) {
840 			da->pi_prot_type = old_prot;
841 			da->pi_prot_verify = (bool) da->pi_prot_type;
842 			return ret;
843 		}
844 
845 	} else if (!flag && old_prot) {
846 		dev->transport->free_prot(dev);
847 	}
848 
849 	da->pi_prot_verify = (bool) da->pi_prot_type;
850 	pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
851 	return count;
852 }
853 
854 /* always zero, but attr needs to remain RW to avoid userspace breakage */
855 static ssize_t pi_prot_format_show(struct config_item *item, char *page)
856 {
857 	return snprintf(page, PAGE_SIZE, "0\n");
858 }
859 
860 static ssize_t pi_prot_format_store(struct config_item *item,
861 		const char *page, size_t count)
862 {
863 	struct se_dev_attrib *da = to_attrib(item);
864 	struct se_device *dev = da->da_dev;
865 	bool flag;
866 	int ret;
867 
868 	ret = strtobool(page, &flag);
869 	if (ret < 0)
870 		return ret;
871 
872 	if (!flag)
873 		return count;
874 
875 	if (!dev->transport->format_prot) {
876 		pr_err("DIF protection format not supported by backend %s\n",
877 		       dev->transport->name);
878 		return -ENOSYS;
879 	}
880 	if (!target_dev_configured(dev)) {
881 		pr_err("DIF protection format requires device to be configured\n");
882 		return -ENODEV;
883 	}
884 	if (dev->export_count) {
885 		pr_err("dev[%p]: Unable to format SE Device PROT type while"
886 		       " export_count is %d\n", dev, dev->export_count);
887 		return -EINVAL;
888 	}
889 
890 	ret = dev->transport->format_prot(dev);
891 	if (ret)
892 		return ret;
893 
894 	pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
895 	return count;
896 }
897 
898 static ssize_t pi_prot_verify_store(struct config_item *item,
899 		const char *page, size_t count)
900 {
901 	struct se_dev_attrib *da = to_attrib(item);
902 	bool flag;
903 	int ret;
904 
905 	ret = strtobool(page, &flag);
906 	if (ret < 0)
907 		return ret;
908 
909 	if (!flag) {
910 		da->pi_prot_verify = flag;
911 		return count;
912 	}
913 	if (da->hw_pi_prot_type) {
914 		pr_warn("DIF protection enabled on underlying hardware,"
915 			" ignoring\n");
916 		return count;
917 	}
918 	if (!da->pi_prot_type) {
919 		pr_warn("DIF protection not supported by backend, ignoring\n");
920 		return count;
921 	}
922 	da->pi_prot_verify = flag;
923 
924 	return count;
925 }
926 
927 static ssize_t force_pr_aptpl_store(struct config_item *item,
928 		const char *page, size_t count)
929 {
930 	struct se_dev_attrib *da = to_attrib(item);
931 	bool flag;
932 	int ret;
933 
934 	ret = strtobool(page, &flag);
935 	if (ret < 0)
936 		return ret;
937 	if (da->da_dev->export_count) {
938 		pr_err("dev[%p]: Unable to set force_pr_aptpl while"
939 		       " export_count is %d\n",
940 		       da->da_dev, da->da_dev->export_count);
941 		return -EINVAL;
942 	}
943 
944 	da->force_pr_aptpl = flag;
945 	pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
946 	return count;
947 }
948 
949 static ssize_t emulate_rest_reord_store(struct config_item *item,
950 		const char *page, size_t count)
951 {
952 	struct se_dev_attrib *da = to_attrib(item);
953 	bool flag;
954 	int ret;
955 
956 	ret = strtobool(page, &flag);
957 	if (ret < 0)
958 		return ret;
959 
960 	if (flag != 0) {
961 		printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
962 			" reordering not implemented\n", da->da_dev);
963 		return -ENOSYS;
964 	}
965 	da->emulate_rest_reord = flag;
966 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
967 		da->da_dev, flag);
968 	return count;
969 }
970 
971 static ssize_t unmap_zeroes_data_store(struct config_item *item,
972 		const char *page, size_t count)
973 {
974 	struct se_dev_attrib *da = to_attrib(item);
975 	struct se_device *dev = da->da_dev;
976 	bool flag;
977 	int ret;
978 
979 	ret = strtobool(page, &flag);
980 	if (ret < 0)
981 		return ret;
982 
983 	if (da->da_dev->export_count) {
984 		pr_err("dev[%p]: Unable to change SE Device"
985 		       " unmap_zeroes_data while export_count is %d\n",
986 		       da->da_dev, da->da_dev->export_count);
987 		return -EINVAL;
988 	}
989 	/*
990 	 * We expect this value to be non-zero when generic Block Layer
991 	 * Discard supported is detected iblock_configure_device().
992 	 */
993 	if (flag && !da->max_unmap_block_desc_count) {
994 		if (!dev->transport->configure_unmap ||
995 		    !dev->transport->configure_unmap(dev)) {
996 			pr_err("dev[%p]: Thin Provisioning LBPRZ will not be set because max_unmap_block_desc_count is zero\n",
997 			       da->da_dev);
998 			return -ENOSYS;
999 		}
1000 	}
1001 	da->unmap_zeroes_data = flag;
1002 	pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
1003 		 da->da_dev, flag);
1004 	return count;
1005 }
1006 
1007 /*
1008  * Note, this can only be called on unexported SE Device Object.
1009  */
1010 static ssize_t queue_depth_store(struct config_item *item,
1011 		const char *page, size_t count)
1012 {
1013 	struct se_dev_attrib *da = to_attrib(item);
1014 	struct se_device *dev = da->da_dev;
1015 	u32 val;
1016 	int ret;
1017 
1018 	ret = kstrtou32(page, 0, &val);
1019 	if (ret < 0)
1020 		return ret;
1021 
1022 	if (dev->export_count) {
1023 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
1024 			" export_count is %d\n",
1025 			dev, dev->export_count);
1026 		return -EINVAL;
1027 	}
1028 	if (!val) {
1029 		pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
1030 		return -EINVAL;
1031 	}
1032 
1033 	if (val > dev->dev_attrib.queue_depth) {
1034 		if (val > dev->dev_attrib.hw_queue_depth) {
1035 			pr_err("dev[%p]: Passed queue_depth:"
1036 				" %u exceeds TCM/SE_Device MAX"
1037 				" TCQ: %u\n", dev, val,
1038 				dev->dev_attrib.hw_queue_depth);
1039 			return -EINVAL;
1040 		}
1041 	}
1042 	da->queue_depth = dev->queue_depth = val;
1043 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
1044 	return count;
1045 }
1046 
1047 static ssize_t optimal_sectors_store(struct config_item *item,
1048 		const char *page, size_t count)
1049 {
1050 	struct se_dev_attrib *da = to_attrib(item);
1051 	u32 val;
1052 	int ret;
1053 
1054 	ret = kstrtou32(page, 0, &val);
1055 	if (ret < 0)
1056 		return ret;
1057 
1058 	if (da->da_dev->export_count) {
1059 		pr_err("dev[%p]: Unable to change SE Device"
1060 			" optimal_sectors while export_count is %d\n",
1061 			da->da_dev, da->da_dev->export_count);
1062 		return -EINVAL;
1063 	}
1064 	if (val > da->hw_max_sectors) {
1065 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1066 			" greater than hw_max_sectors: %u\n",
1067 			da->da_dev, val, da->hw_max_sectors);
1068 		return -EINVAL;
1069 	}
1070 
1071 	da->optimal_sectors = val;
1072 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1073 			da->da_dev, val);
1074 	return count;
1075 }
1076 
1077 static ssize_t block_size_store(struct config_item *item,
1078 		const char *page, size_t count)
1079 {
1080 	struct se_dev_attrib *da = to_attrib(item);
1081 	u32 val;
1082 	int ret;
1083 
1084 	ret = kstrtou32(page, 0, &val);
1085 	if (ret < 0)
1086 		return ret;
1087 
1088 	if (da->da_dev->export_count) {
1089 		pr_err("dev[%p]: Unable to change SE Device block_size"
1090 			" while export_count is %d\n",
1091 			da->da_dev, da->da_dev->export_count);
1092 		return -EINVAL;
1093 	}
1094 
1095 	if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1096 		pr_err("dev[%p]: Illegal value for block_device: %u"
1097 			" for SE device, must be 512, 1024, 2048 or 4096\n",
1098 			da->da_dev, val);
1099 		return -EINVAL;
1100 	}
1101 
1102 	da->block_size = val;
1103 	if (da->max_bytes_per_io)
1104 		da->hw_max_sectors = da->max_bytes_per_io / val;
1105 
1106 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1107 			da->da_dev, val);
1108 	return count;
1109 }
1110 
1111 static ssize_t alua_support_show(struct config_item *item, char *page)
1112 {
1113 	struct se_dev_attrib *da = to_attrib(item);
1114 	u8 flags = da->da_dev->transport_flags;
1115 
1116 	return snprintf(page, PAGE_SIZE, "%d\n",
1117 			flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1118 }
1119 
1120 static ssize_t alua_support_store(struct config_item *item,
1121 		const char *page, size_t count)
1122 {
1123 	struct se_dev_attrib *da = to_attrib(item);
1124 	struct se_device *dev = da->da_dev;
1125 	bool flag, oldflag;
1126 	int ret;
1127 
1128 	ret = strtobool(page, &flag);
1129 	if (ret < 0)
1130 		return ret;
1131 
1132 	oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA);
1133 	if (flag == oldflag)
1134 		return count;
1135 
1136 	if (!(dev->transport->transport_flags_changeable &
1137 	      TRANSPORT_FLAG_PASSTHROUGH_ALUA)) {
1138 		pr_err("dev[%p]: Unable to change SE Device alua_support:"
1139 			" alua_support has fixed value\n", dev);
1140 		return -ENOSYS;
1141 	}
1142 
1143 	if (flag)
1144 		dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1145 	else
1146 		dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1147 	return count;
1148 }
1149 
1150 static ssize_t pgr_support_show(struct config_item *item, char *page)
1151 {
1152 	struct se_dev_attrib *da = to_attrib(item);
1153 	u8 flags = da->da_dev->transport_flags;
1154 
1155 	return snprintf(page, PAGE_SIZE, "%d\n",
1156 			flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1157 }
1158 
1159 static ssize_t pgr_support_store(struct config_item *item,
1160 		const char *page, size_t count)
1161 {
1162 	struct se_dev_attrib *da = to_attrib(item);
1163 	struct se_device *dev = da->da_dev;
1164 	bool flag, oldflag;
1165 	int ret;
1166 
1167 	ret = strtobool(page, &flag);
1168 	if (ret < 0)
1169 		return ret;
1170 
1171 	oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR);
1172 	if (flag == oldflag)
1173 		return count;
1174 
1175 	if (!(dev->transport->transport_flags_changeable &
1176 	      TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1177 		pr_err("dev[%p]: Unable to change SE Device pgr_support:"
1178 			" pgr_support has fixed value\n", dev);
1179 		return -ENOSYS;
1180 	}
1181 
1182 	if (flag)
1183 		dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_PGR;
1184 	else
1185 		dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_PGR;
1186 	return count;
1187 }
1188 
1189 CONFIGFS_ATTR(, emulate_model_alias);
1190 CONFIGFS_ATTR(, emulate_dpo);
1191 CONFIGFS_ATTR(, emulate_fua_write);
1192 CONFIGFS_ATTR(, emulate_fua_read);
1193 CONFIGFS_ATTR(, emulate_write_cache);
1194 CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1195 CONFIGFS_ATTR(, emulate_tas);
1196 CONFIGFS_ATTR(, emulate_tpu);
1197 CONFIGFS_ATTR(, emulate_tpws);
1198 CONFIGFS_ATTR(, emulate_caw);
1199 CONFIGFS_ATTR(, emulate_3pc);
1200 CONFIGFS_ATTR(, emulate_pr);
1201 CONFIGFS_ATTR(, pi_prot_type);
1202 CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1203 CONFIGFS_ATTR(, pi_prot_format);
1204 CONFIGFS_ATTR(, pi_prot_verify);
1205 CONFIGFS_ATTR(, enforce_pr_isids);
1206 CONFIGFS_ATTR(, is_nonrot);
1207 CONFIGFS_ATTR(, emulate_rest_reord);
1208 CONFIGFS_ATTR(, force_pr_aptpl);
1209 CONFIGFS_ATTR_RO(, hw_block_size);
1210 CONFIGFS_ATTR(, block_size);
1211 CONFIGFS_ATTR_RO(, hw_max_sectors);
1212 CONFIGFS_ATTR(, optimal_sectors);
1213 CONFIGFS_ATTR_RO(, hw_queue_depth);
1214 CONFIGFS_ATTR(, queue_depth);
1215 CONFIGFS_ATTR(, max_unmap_lba_count);
1216 CONFIGFS_ATTR(, max_unmap_block_desc_count);
1217 CONFIGFS_ATTR(, unmap_granularity);
1218 CONFIGFS_ATTR(, unmap_granularity_alignment);
1219 CONFIGFS_ATTR(, unmap_zeroes_data);
1220 CONFIGFS_ATTR(, max_write_same_len);
1221 CONFIGFS_ATTR(, alua_support);
1222 CONFIGFS_ATTR(, pgr_support);
1223 
1224 /*
1225  * dev_attrib attributes for devices using the target core SBC/SPC
1226  * interpreter.  Any backend using spc_parse_cdb should be using
1227  * these.
1228  */
1229 struct configfs_attribute *sbc_attrib_attrs[] = {
1230 	&attr_emulate_model_alias,
1231 	&attr_emulate_dpo,
1232 	&attr_emulate_fua_write,
1233 	&attr_emulate_fua_read,
1234 	&attr_emulate_write_cache,
1235 	&attr_emulate_ua_intlck_ctrl,
1236 	&attr_emulate_tas,
1237 	&attr_emulate_tpu,
1238 	&attr_emulate_tpws,
1239 	&attr_emulate_caw,
1240 	&attr_emulate_3pc,
1241 	&attr_emulate_pr,
1242 	&attr_pi_prot_type,
1243 	&attr_hw_pi_prot_type,
1244 	&attr_pi_prot_format,
1245 	&attr_pi_prot_verify,
1246 	&attr_enforce_pr_isids,
1247 	&attr_is_nonrot,
1248 	&attr_emulate_rest_reord,
1249 	&attr_force_pr_aptpl,
1250 	&attr_hw_block_size,
1251 	&attr_block_size,
1252 	&attr_hw_max_sectors,
1253 	&attr_optimal_sectors,
1254 	&attr_hw_queue_depth,
1255 	&attr_queue_depth,
1256 	&attr_max_unmap_lba_count,
1257 	&attr_max_unmap_block_desc_count,
1258 	&attr_unmap_granularity,
1259 	&attr_unmap_granularity_alignment,
1260 	&attr_unmap_zeroes_data,
1261 	&attr_max_write_same_len,
1262 	&attr_alua_support,
1263 	&attr_pgr_support,
1264 	NULL,
1265 };
1266 EXPORT_SYMBOL(sbc_attrib_attrs);
1267 
1268 /*
1269  * Minimal dev_attrib attributes for devices passing through CDBs.
1270  * In this case we only provide a few read-only attributes for
1271  * backwards compatibility.
1272  */
1273 struct configfs_attribute *passthrough_attrib_attrs[] = {
1274 	&attr_hw_pi_prot_type,
1275 	&attr_hw_block_size,
1276 	&attr_hw_max_sectors,
1277 	&attr_hw_queue_depth,
1278 	&attr_emulate_pr,
1279 	&attr_alua_support,
1280 	&attr_pgr_support,
1281 	NULL,
1282 };
1283 EXPORT_SYMBOL(passthrough_attrib_attrs);
1284 
1285 /*
1286  * pr related dev_attrib attributes for devices passing through CDBs,
1287  * but allowing in core pr emulation.
1288  */
1289 struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
1290 	&attr_enforce_pr_isids,
1291 	&attr_force_pr_aptpl,
1292 	NULL,
1293 };
1294 EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
1295 
1296 TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1297 TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1298 
1299 /* End functions for struct config_item_type tb_dev_attrib_cit */
1300 
1301 /*  Start functions for struct config_item_type tb_dev_wwn_cit */
1302 
1303 static struct t10_wwn *to_t10_wwn(struct config_item *item)
1304 {
1305 	return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1306 }
1307 
1308 static ssize_t target_check_inquiry_data(char *buf)
1309 {
1310 	size_t len;
1311 	int i;
1312 
1313 	len = strlen(buf);
1314 
1315 	/*
1316 	 * SPC 4.3.1:
1317 	 * ASCII data fields shall contain only ASCII printable characters
1318 	 * (i.e., code values 20h to 7Eh) and may be terminated with one or
1319 	 * more ASCII null (00h) characters.
1320 	 */
1321 	for (i = 0; i < len; i++) {
1322 		if (buf[i] < 0x20 || buf[i] > 0x7E) {
1323 			pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
1324 			return -EINVAL;
1325 		}
1326 	}
1327 
1328 	return len;
1329 }
1330 
1331 /*
1332  * STANDARD and VPD page 0x83 T10 Vendor Identification
1333  */
1334 static ssize_t target_wwn_vendor_id_show(struct config_item *item,
1335 		char *page)
1336 {
1337 	return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
1338 }
1339 
1340 static ssize_t target_wwn_vendor_id_store(struct config_item *item,
1341 		const char *page, size_t count)
1342 {
1343 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1344 	struct se_device *dev = t10_wwn->t10_dev;
1345 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1346 	unsigned char buf[INQUIRY_VENDOR_LEN + 2];
1347 	char *stripped = NULL;
1348 	size_t len;
1349 	ssize_t ret;
1350 
1351 	len = strlcpy(buf, page, sizeof(buf));
1352 	if (len < sizeof(buf)) {
1353 		/* Strip any newline added from userspace. */
1354 		stripped = strstrip(buf);
1355 		len = strlen(stripped);
1356 	}
1357 	if (len > INQUIRY_VENDOR_LEN) {
1358 		pr_err("Emulated T10 Vendor Identification exceeds"
1359 			" INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
1360 			"\n");
1361 		return -EOVERFLOW;
1362 	}
1363 
1364 	ret = target_check_inquiry_data(stripped);
1365 
1366 	if (ret < 0)
1367 		return ret;
1368 
1369 	/*
1370 	 * Check to see if any active exports exist.  If they do exist, fail
1371 	 * here as changing this information on the fly (underneath the
1372 	 * initiator side OS dependent multipath code) could cause negative
1373 	 * effects.
1374 	 */
1375 	if (dev->export_count) {
1376 		pr_err("Unable to set T10 Vendor Identification while"
1377 			" active %d exports exist\n", dev->export_count);
1378 		return -EINVAL;
1379 	}
1380 
1381 	BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
1382 	strlcpy(dev->t10_wwn.vendor, stripped, sizeof(dev->t10_wwn.vendor));
1383 
1384 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
1385 		 " %s\n", dev->t10_wwn.vendor);
1386 
1387 	return count;
1388 }
1389 
1390 static ssize_t target_wwn_product_id_show(struct config_item *item,
1391 		char *page)
1392 {
1393 	return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
1394 }
1395 
1396 static ssize_t target_wwn_product_id_store(struct config_item *item,
1397 		const char *page, size_t count)
1398 {
1399 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1400 	struct se_device *dev = t10_wwn->t10_dev;
1401 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1402 	unsigned char buf[INQUIRY_MODEL_LEN + 2];
1403 	char *stripped = NULL;
1404 	size_t len;
1405 	ssize_t ret;
1406 
1407 	len = strlcpy(buf, page, sizeof(buf));
1408 	if (len < sizeof(buf)) {
1409 		/* Strip any newline added from userspace. */
1410 		stripped = strstrip(buf);
1411 		len = strlen(stripped);
1412 	}
1413 	if (len > INQUIRY_MODEL_LEN) {
1414 		pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
1415 			 __stringify(INQUIRY_MODEL_LEN)
1416 			"\n");
1417 		return -EOVERFLOW;
1418 	}
1419 
1420 	ret = target_check_inquiry_data(stripped);
1421 
1422 	if (ret < 0)
1423 		return ret;
1424 
1425 	/*
1426 	 * Check to see if any active exports exist.  If they do exist, fail
1427 	 * here as changing this information on the fly (underneath the
1428 	 * initiator side OS dependent multipath code) could cause negative
1429 	 * effects.
1430 	 */
1431 	if (dev->export_count) {
1432 		pr_err("Unable to set T10 Model while active %d exports exist\n",
1433 			dev->export_count);
1434 		return -EINVAL;
1435 	}
1436 
1437 	BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
1438 	strlcpy(dev->t10_wwn.model, stripped, sizeof(dev->t10_wwn.model));
1439 
1440 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
1441 		 dev->t10_wwn.model);
1442 
1443 	return count;
1444 }
1445 
1446 static ssize_t target_wwn_revision_show(struct config_item *item,
1447 		char *page)
1448 {
1449 	return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
1450 }
1451 
1452 static ssize_t target_wwn_revision_store(struct config_item *item,
1453 		const char *page, size_t count)
1454 {
1455 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1456 	struct se_device *dev = t10_wwn->t10_dev;
1457 	/* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1458 	unsigned char buf[INQUIRY_REVISION_LEN + 2];
1459 	char *stripped = NULL;
1460 	size_t len;
1461 	ssize_t ret;
1462 
1463 	len = strlcpy(buf, page, sizeof(buf));
1464 	if (len < sizeof(buf)) {
1465 		/* Strip any newline added from userspace. */
1466 		stripped = strstrip(buf);
1467 		len = strlen(stripped);
1468 	}
1469 	if (len > INQUIRY_REVISION_LEN) {
1470 		pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
1471 			 __stringify(INQUIRY_REVISION_LEN)
1472 			"\n");
1473 		return -EOVERFLOW;
1474 	}
1475 
1476 	ret = target_check_inquiry_data(stripped);
1477 
1478 	if (ret < 0)
1479 		return ret;
1480 
1481 	/*
1482 	 * Check to see if any active exports exist.  If they do exist, fail
1483 	 * here as changing this information on the fly (underneath the
1484 	 * initiator side OS dependent multipath code) could cause negative
1485 	 * effects.
1486 	 */
1487 	if (dev->export_count) {
1488 		pr_err("Unable to set T10 Revision while active %d exports exist\n",
1489 			dev->export_count);
1490 		return -EINVAL;
1491 	}
1492 
1493 	BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
1494 	strlcpy(dev->t10_wwn.revision, stripped, sizeof(dev->t10_wwn.revision));
1495 
1496 	pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
1497 		 dev->t10_wwn.revision);
1498 
1499 	return count;
1500 }
1501 
1502 static ssize_t
1503 target_wwn_company_id_show(struct config_item *item,
1504 				char *page)
1505 {
1506 	return snprintf(page, PAGE_SIZE, "%#08x\n",
1507 			to_t10_wwn(item)->company_id);
1508 }
1509 
1510 static ssize_t
1511 target_wwn_company_id_store(struct config_item *item,
1512 				 const char *page, size_t count)
1513 {
1514 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1515 	struct se_device *dev = t10_wwn->t10_dev;
1516 	u32 val;
1517 	int ret;
1518 
1519 	/*
1520 	 * The IEEE COMPANY_ID field should contain a 24-bit canonical
1521 	 * form OUI assigned by the IEEE.
1522 	 */
1523 	ret = kstrtou32(page, 0, &val);
1524 	if (ret < 0)
1525 		return ret;
1526 
1527 	if (val >= 0x1000000)
1528 		return -EOVERFLOW;
1529 
1530 	/*
1531 	 * Check to see if any active exports exist. If they do exist, fail
1532 	 * here as changing this information on the fly (underneath the
1533 	 * initiator side OS dependent multipath code) could cause negative
1534 	 * effects.
1535 	 */
1536 	if (dev->export_count) {
1537 		pr_err("Unable to set Company ID while %u exports exist\n",
1538 		       dev->export_count);
1539 		return -EINVAL;
1540 	}
1541 
1542 	t10_wwn->company_id = val;
1543 
1544 	pr_debug("Target_Core_ConfigFS: Set IEEE Company ID: %#08x\n",
1545 		 t10_wwn->company_id);
1546 
1547 	return count;
1548 }
1549 
1550 /*
1551  * VPD page 0x80 Unit serial
1552  */
1553 static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1554 		char *page)
1555 {
1556 	return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
1557 		&to_t10_wwn(item)->unit_serial[0]);
1558 }
1559 
1560 static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1561 		const char *page, size_t count)
1562 {
1563 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1564 	struct se_device *dev = t10_wwn->t10_dev;
1565 	unsigned char buf[INQUIRY_VPD_SERIAL_LEN] = { };
1566 
1567 	/*
1568 	 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1569 	 * from the struct scsi_device level firmware, do not allow
1570 	 * VPD Unit Serial to be emulated.
1571 	 *
1572 	 * Note this struct scsi_device could also be emulating VPD
1573 	 * information from its drivers/scsi LLD.  But for now we assume
1574 	 * it is doing 'the right thing' wrt a world wide unique
1575 	 * VPD Unit Serial Number that OS dependent multipath can depend on.
1576 	 */
1577 	if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
1578 		pr_err("Underlying SCSI device firmware provided VPD"
1579 			" Unit Serial, ignoring request\n");
1580 		return -EOPNOTSUPP;
1581 	}
1582 
1583 	if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
1584 		pr_err("Emulated VPD Unit Serial exceeds"
1585 		" INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1586 		return -EOVERFLOW;
1587 	}
1588 	/*
1589 	 * Check to see if any active $FABRIC_MOD exports exist.  If they
1590 	 * do exist, fail here as changing this information on the fly
1591 	 * (underneath the initiator side OS dependent multipath code)
1592 	 * could cause negative effects.
1593 	 */
1594 	if (dev->export_count) {
1595 		pr_err("Unable to set VPD Unit Serial while"
1596 			" active %d $FABRIC_MOD exports exist\n",
1597 			dev->export_count);
1598 		return -EINVAL;
1599 	}
1600 
1601 	/*
1602 	 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1603 	 *
1604 	 * Also, strip any newline added from the userspace
1605 	 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1606 	 */
1607 	snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
1608 	snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1609 			"%s", strstrip(buf));
1610 	dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1611 
1612 	pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
1613 			" %s\n", dev->t10_wwn.unit_serial);
1614 
1615 	return count;
1616 }
1617 
1618 /*
1619  * VPD page 0x83 Protocol Identifier
1620  */
1621 static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1622 		char *page)
1623 {
1624 	struct t10_wwn *t10_wwn = to_t10_wwn(item);
1625 	struct t10_vpd *vpd;
1626 	unsigned char buf[VPD_TMP_BUF_SIZE] = { };
1627 	ssize_t len = 0;
1628 
1629 	spin_lock(&t10_wwn->t10_vpd_lock);
1630 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
1631 		if (!vpd->protocol_identifier_set)
1632 			continue;
1633 
1634 		transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1635 
1636 		if (len + strlen(buf) >= PAGE_SIZE)
1637 			break;
1638 
1639 		len += sprintf(page+len, "%s", buf);
1640 	}
1641 	spin_unlock(&t10_wwn->t10_vpd_lock);
1642 
1643 	return len;
1644 }
1645 
1646 /*
1647  * Generic wrapper for dumping VPD identifiers by association.
1648  */
1649 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)				\
1650 static ssize_t target_wwn_##_name##_show(struct config_item *item,	\
1651 		char *page)						\
1652 {									\
1653 	struct t10_wwn *t10_wwn = to_t10_wwn(item);			\
1654 	struct t10_vpd *vpd;						\
1655 	unsigned char buf[VPD_TMP_BUF_SIZE];				\
1656 	ssize_t len = 0;						\
1657 									\
1658 	spin_lock(&t10_wwn->t10_vpd_lock);				\
1659 	list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {	\
1660 		if (vpd->association != _assoc)				\
1661 			continue;					\
1662 									\
1663 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1664 		transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);	\
1665 		if (len + strlen(buf) >= PAGE_SIZE)			\
1666 			break;						\
1667 		len += sprintf(page+len, "%s", buf);			\
1668 									\
1669 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1670 		transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
1671 		if (len + strlen(buf) >= PAGE_SIZE)			\
1672 			break;						\
1673 		len += sprintf(page+len, "%s", buf);			\
1674 									\
1675 		memset(buf, 0, VPD_TMP_BUF_SIZE);			\
1676 		transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1677 		if (len + strlen(buf) >= PAGE_SIZE)			\
1678 			break;						\
1679 		len += sprintf(page+len, "%s", buf);			\
1680 	}								\
1681 	spin_unlock(&t10_wwn->t10_vpd_lock);				\
1682 									\
1683 	return len;							\
1684 }
1685 
1686 /* VPD page 0x83 Association: Logical Unit */
1687 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1688 /* VPD page 0x83 Association: Target Port */
1689 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1690 /* VPD page 0x83 Association: SCSI Target Device */
1691 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1692 
1693 CONFIGFS_ATTR(target_wwn_, vendor_id);
1694 CONFIGFS_ATTR(target_wwn_, product_id);
1695 CONFIGFS_ATTR(target_wwn_, revision);
1696 CONFIGFS_ATTR(target_wwn_, company_id);
1697 CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1698 CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1699 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1700 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1701 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1702 
1703 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1704 	&target_wwn_attr_vendor_id,
1705 	&target_wwn_attr_product_id,
1706 	&target_wwn_attr_revision,
1707 	&target_wwn_attr_company_id,
1708 	&target_wwn_attr_vpd_unit_serial,
1709 	&target_wwn_attr_vpd_protocol_identifier,
1710 	&target_wwn_attr_vpd_assoc_logical_unit,
1711 	&target_wwn_attr_vpd_assoc_target_port,
1712 	&target_wwn_attr_vpd_assoc_scsi_target_device,
1713 	NULL,
1714 };
1715 
1716 TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1717 
1718 /*  End functions for struct config_item_type tb_dev_wwn_cit */
1719 
1720 /*  Start functions for struct config_item_type tb_dev_pr_cit */
1721 
1722 static struct se_device *pr_to_dev(struct config_item *item)
1723 {
1724 	return container_of(to_config_group(item), struct se_device,
1725 			dev_pr_group);
1726 }
1727 
1728 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1729 		char *page)
1730 {
1731 	struct se_node_acl *se_nacl;
1732 	struct t10_pr_registration *pr_reg;
1733 	char i_buf[PR_REG_ISID_ID_LEN] = { };
1734 
1735 	pr_reg = dev->dev_pr_res_holder;
1736 	if (!pr_reg)
1737 		return sprintf(page, "No SPC-3 Reservation holder\n");
1738 
1739 	se_nacl = pr_reg->pr_reg_nacl;
1740 	core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1741 
1742 	return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1743 		se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1744 		se_nacl->initiatorname, i_buf);
1745 }
1746 
1747 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1748 		char *page)
1749 {
1750 	struct se_session *sess = dev->reservation_holder;
1751 	struct se_node_acl *se_nacl;
1752 	ssize_t len;
1753 
1754 	if (sess) {
1755 		se_nacl = sess->se_node_acl;
1756 		len = sprintf(page,
1757 			      "SPC-2 Reservation: %s Initiator: %s\n",
1758 			      se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1759 			      se_nacl->initiatorname);
1760 	} else {
1761 		len = sprintf(page, "No SPC-2 Reservation holder\n");
1762 	}
1763 	return len;
1764 }
1765 
1766 static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1767 {
1768 	struct se_device *dev = pr_to_dev(item);
1769 	int ret;
1770 
1771 	if (!dev->dev_attrib.emulate_pr)
1772 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1773 
1774 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1775 		return sprintf(page, "Passthrough\n");
1776 
1777 	spin_lock(&dev->dev_reservation_lock);
1778 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1779 		ret = target_core_dev_pr_show_spc2_res(dev, page);
1780 	else
1781 		ret = target_core_dev_pr_show_spc3_res(dev, page);
1782 	spin_unlock(&dev->dev_reservation_lock);
1783 	return ret;
1784 }
1785 
1786 static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1787 		char *page)
1788 {
1789 	struct se_device *dev = pr_to_dev(item);
1790 	ssize_t len = 0;
1791 
1792 	spin_lock(&dev->dev_reservation_lock);
1793 	if (!dev->dev_pr_res_holder) {
1794 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1795 	} else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1796 		len = sprintf(page, "SPC-3 Reservation: All Target"
1797 			" Ports registration\n");
1798 	} else {
1799 		len = sprintf(page, "SPC-3 Reservation: Single"
1800 			" Target Port registration\n");
1801 	}
1802 
1803 	spin_unlock(&dev->dev_reservation_lock);
1804 	return len;
1805 }
1806 
1807 static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1808 		char *page)
1809 {
1810 	return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1811 }
1812 
1813 
1814 static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1815 		char *page)
1816 {
1817 	struct se_device *dev = pr_to_dev(item);
1818 	struct se_node_acl *se_nacl;
1819 	struct se_portal_group *se_tpg;
1820 	struct t10_pr_registration *pr_reg;
1821 	const struct target_core_fabric_ops *tfo;
1822 	ssize_t len = 0;
1823 
1824 	spin_lock(&dev->dev_reservation_lock);
1825 	pr_reg = dev->dev_pr_res_holder;
1826 	if (!pr_reg) {
1827 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1828 		goto out_unlock;
1829 	}
1830 
1831 	se_nacl = pr_reg->pr_reg_nacl;
1832 	se_tpg = se_nacl->se_tpg;
1833 	tfo = se_tpg->se_tpg_tfo;
1834 
1835 	len += sprintf(page+len, "SPC-3 Reservation: %s"
1836 		" Target Node Endpoint: %s\n", tfo->fabric_name,
1837 		tfo->tpg_get_wwn(se_tpg));
1838 	len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1839 		" Identifier Tag: %hu %s Portal Group Tag: %hu"
1840 		" %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1841 		tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
1842 		tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1843 
1844 out_unlock:
1845 	spin_unlock(&dev->dev_reservation_lock);
1846 	return len;
1847 }
1848 
1849 
1850 static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1851 		char *page)
1852 {
1853 	struct se_device *dev = pr_to_dev(item);
1854 	const struct target_core_fabric_ops *tfo;
1855 	struct t10_pr_registration *pr_reg;
1856 	unsigned char buf[384];
1857 	char i_buf[PR_REG_ISID_ID_LEN];
1858 	ssize_t len = 0;
1859 	int reg_count = 0;
1860 
1861 	len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1862 
1863 	spin_lock(&dev->t10_pr.registration_lock);
1864 	list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1865 			pr_reg_list) {
1866 
1867 		memset(buf, 0, 384);
1868 		memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1869 		tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1870 		core_pr_dump_initiator_port(pr_reg, i_buf,
1871 					PR_REG_ISID_ID_LEN);
1872 		sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1873 			tfo->fabric_name,
1874 			pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1875 			pr_reg->pr_res_generation);
1876 
1877 		if (len + strlen(buf) >= PAGE_SIZE)
1878 			break;
1879 
1880 		len += sprintf(page+len, "%s", buf);
1881 		reg_count++;
1882 	}
1883 	spin_unlock(&dev->t10_pr.registration_lock);
1884 
1885 	if (!reg_count)
1886 		len += sprintf(page+len, "None\n");
1887 
1888 	return len;
1889 }
1890 
1891 static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1892 {
1893 	struct se_device *dev = pr_to_dev(item);
1894 	struct t10_pr_registration *pr_reg;
1895 	ssize_t len = 0;
1896 
1897 	spin_lock(&dev->dev_reservation_lock);
1898 	pr_reg = dev->dev_pr_res_holder;
1899 	if (pr_reg) {
1900 		len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1901 			core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1902 	} else {
1903 		len = sprintf(page, "No SPC-3 Reservation holder\n");
1904 	}
1905 
1906 	spin_unlock(&dev->dev_reservation_lock);
1907 	return len;
1908 }
1909 
1910 static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
1911 {
1912 	struct se_device *dev = pr_to_dev(item);
1913 
1914 	if (!dev->dev_attrib.emulate_pr)
1915 		return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1916 	if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1917 		return sprintf(page, "SPC_PASSTHROUGH\n");
1918 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1919 		return sprintf(page, "SPC2_RESERVATIONS\n");
1920 
1921 	return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1922 }
1923 
1924 static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
1925 		char *page)
1926 {
1927 	struct se_device *dev = pr_to_dev(item);
1928 
1929 	if (!dev->dev_attrib.emulate_pr ||
1930 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1931 		return 0;
1932 
1933 	return sprintf(page, "APTPL Bit Status: %s\n",
1934 		(dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1935 }
1936 
1937 static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
1938 		char *page)
1939 {
1940 	struct se_device *dev = pr_to_dev(item);
1941 
1942 	if (!dev->dev_attrib.emulate_pr ||
1943 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1944 		return 0;
1945 
1946 	return sprintf(page, "Ready to process PR APTPL metadata..\n");
1947 }
1948 
1949 enum {
1950 	Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1951 	Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1952 	Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1953 	Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1954 };
1955 
1956 static match_table_t tokens = {
1957 	{Opt_initiator_fabric, "initiator_fabric=%s"},
1958 	{Opt_initiator_node, "initiator_node=%s"},
1959 	{Opt_initiator_sid, "initiator_sid=%s"},
1960 	{Opt_sa_res_key, "sa_res_key=%s"},
1961 	{Opt_res_holder, "res_holder=%d"},
1962 	{Opt_res_type, "res_type=%d"},
1963 	{Opt_res_scope, "res_scope=%d"},
1964 	{Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1965 	{Opt_mapped_lun, "mapped_lun=%u"},
1966 	{Opt_target_fabric, "target_fabric=%s"},
1967 	{Opt_target_node, "target_node=%s"},
1968 	{Opt_tpgt, "tpgt=%d"},
1969 	{Opt_port_rtpi, "port_rtpi=%d"},
1970 	{Opt_target_lun, "target_lun=%u"},
1971 	{Opt_err, NULL}
1972 };
1973 
1974 static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
1975 		const char *page, size_t count)
1976 {
1977 	struct se_device *dev = pr_to_dev(item);
1978 	unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1979 	unsigned char *t_fabric = NULL, *t_port = NULL;
1980 	char *orig, *ptr, *opts;
1981 	substring_t args[MAX_OPT_ARGS];
1982 	unsigned long long tmp_ll;
1983 	u64 sa_res_key = 0;
1984 	u64 mapped_lun = 0, target_lun = 0;
1985 	int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1986 	u16 tpgt = 0;
1987 	u8 type = 0;
1988 
1989 	if (!dev->dev_attrib.emulate_pr ||
1990 	    (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
1991 		return count;
1992 	if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1993 		return count;
1994 
1995 	if (dev->export_count) {
1996 		pr_debug("Unable to process APTPL metadata while"
1997 			" active fabric exports exist\n");
1998 		return -EINVAL;
1999 	}
2000 
2001 	opts = kstrdup(page, GFP_KERNEL);
2002 	if (!opts)
2003 		return -ENOMEM;
2004 
2005 	orig = opts;
2006 	while ((ptr = strsep(&opts, ",\n")) != NULL) {
2007 		if (!*ptr)
2008 			continue;
2009 
2010 		token = match_token(ptr, tokens, args);
2011 		switch (token) {
2012 		case Opt_initiator_fabric:
2013 			i_fabric = match_strdup(args);
2014 			if (!i_fabric) {
2015 				ret = -ENOMEM;
2016 				goto out;
2017 			}
2018 			break;
2019 		case Opt_initiator_node:
2020 			i_port = match_strdup(args);
2021 			if (!i_port) {
2022 				ret = -ENOMEM;
2023 				goto out;
2024 			}
2025 			if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
2026 				pr_err("APTPL metadata initiator_node="
2027 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2028 					PR_APTPL_MAX_IPORT_LEN);
2029 				ret = -EINVAL;
2030 				break;
2031 			}
2032 			break;
2033 		case Opt_initiator_sid:
2034 			isid = match_strdup(args);
2035 			if (!isid) {
2036 				ret = -ENOMEM;
2037 				goto out;
2038 			}
2039 			if (strlen(isid) >= PR_REG_ISID_LEN) {
2040 				pr_err("APTPL metadata initiator_isid"
2041 					"= exceeds PR_REG_ISID_LEN: %d\n",
2042 					PR_REG_ISID_LEN);
2043 				ret = -EINVAL;
2044 				break;
2045 			}
2046 			break;
2047 		case Opt_sa_res_key:
2048 			ret = match_u64(args,  &tmp_ll);
2049 			if (ret < 0) {
2050 				pr_err("kstrtoull() failed for sa_res_key=\n");
2051 				goto out;
2052 			}
2053 			sa_res_key = (u64)tmp_ll;
2054 			break;
2055 		/*
2056 		 * PR APTPL Metadata for Reservation
2057 		 */
2058 		case Opt_res_holder:
2059 			ret = match_int(args, &arg);
2060 			if (ret)
2061 				goto out;
2062 			res_holder = arg;
2063 			break;
2064 		case Opt_res_type:
2065 			ret = match_int(args, &arg);
2066 			if (ret)
2067 				goto out;
2068 			type = (u8)arg;
2069 			break;
2070 		case Opt_res_scope:
2071 			ret = match_int(args, &arg);
2072 			if (ret)
2073 				goto out;
2074 			break;
2075 		case Opt_res_all_tg_pt:
2076 			ret = match_int(args, &arg);
2077 			if (ret)
2078 				goto out;
2079 			all_tg_pt = (int)arg;
2080 			break;
2081 		case Opt_mapped_lun:
2082 			ret = match_u64(args, &tmp_ll);
2083 			if (ret)
2084 				goto out;
2085 			mapped_lun = (u64)tmp_ll;
2086 			break;
2087 		/*
2088 		 * PR APTPL Metadata for Target Port
2089 		 */
2090 		case Opt_target_fabric:
2091 			t_fabric = match_strdup(args);
2092 			if (!t_fabric) {
2093 				ret = -ENOMEM;
2094 				goto out;
2095 			}
2096 			break;
2097 		case Opt_target_node:
2098 			t_port = match_strdup(args);
2099 			if (!t_port) {
2100 				ret = -ENOMEM;
2101 				goto out;
2102 			}
2103 			if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
2104 				pr_err("APTPL metadata target_node="
2105 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2106 					PR_APTPL_MAX_TPORT_LEN);
2107 				ret = -EINVAL;
2108 				break;
2109 			}
2110 			break;
2111 		case Opt_tpgt:
2112 			ret = match_int(args, &arg);
2113 			if (ret)
2114 				goto out;
2115 			tpgt = (u16)arg;
2116 			break;
2117 		case Opt_port_rtpi:
2118 			ret = match_int(args, &arg);
2119 			if (ret)
2120 				goto out;
2121 			break;
2122 		case Opt_target_lun:
2123 			ret = match_u64(args, &tmp_ll);
2124 			if (ret)
2125 				goto out;
2126 			target_lun = (u64)tmp_ll;
2127 			break;
2128 		default:
2129 			break;
2130 		}
2131 	}
2132 
2133 	if (!i_port || !t_port || !sa_res_key) {
2134 		pr_err("Illegal parameters for APTPL registration\n");
2135 		ret = -EINVAL;
2136 		goto out;
2137 	}
2138 
2139 	if (res_holder && !(type)) {
2140 		pr_err("Illegal PR type: 0x%02x for reservation"
2141 				" holder\n", type);
2142 		ret = -EINVAL;
2143 		goto out;
2144 	}
2145 
2146 	ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2147 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2148 			res_holder, all_tg_pt, type);
2149 out:
2150 	kfree(i_fabric);
2151 	kfree(i_port);
2152 	kfree(isid);
2153 	kfree(t_fabric);
2154 	kfree(t_port);
2155 	kfree(orig);
2156 	return (ret == 0) ? count : ret;
2157 }
2158 
2159 
2160 CONFIGFS_ATTR_RO(target_pr_, res_holder);
2161 CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
2162 CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
2163 CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
2164 CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
2165 CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
2166 CONFIGFS_ATTR_RO(target_pr_, res_type);
2167 CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
2168 CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2169 
2170 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
2171 	&target_pr_attr_res_holder,
2172 	&target_pr_attr_res_pr_all_tgt_pts,
2173 	&target_pr_attr_res_pr_generation,
2174 	&target_pr_attr_res_pr_holder_tg_port,
2175 	&target_pr_attr_res_pr_registered_i_pts,
2176 	&target_pr_attr_res_pr_type,
2177 	&target_pr_attr_res_type,
2178 	&target_pr_attr_res_aptpl_active,
2179 	&target_pr_attr_res_aptpl_metadata,
2180 	NULL,
2181 };
2182 
2183 TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2184 
2185 /*  End functions for struct config_item_type tb_dev_pr_cit */
2186 
2187 /*  Start functions for struct config_item_type tb_dev_cit */
2188 
2189 static inline struct se_device *to_device(struct config_item *item)
2190 {
2191 	return container_of(to_config_group(item), struct se_device, dev_group);
2192 }
2193 
2194 static ssize_t target_dev_info_show(struct config_item *item, char *page)
2195 {
2196 	struct se_device *dev = to_device(item);
2197 	int bl = 0;
2198 	ssize_t read_bytes = 0;
2199 
2200 	transport_dump_dev_state(dev, page, &bl);
2201 	read_bytes += bl;
2202 	read_bytes += dev->transport->show_configfs_dev_params(dev,
2203 			page+read_bytes);
2204 	return read_bytes;
2205 }
2206 
2207 static ssize_t target_dev_control_store(struct config_item *item,
2208 		const char *page, size_t count)
2209 {
2210 	struct se_device *dev = to_device(item);
2211 
2212 	return dev->transport->set_configfs_dev_params(dev, page, count);
2213 }
2214 
2215 static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2216 {
2217 	struct se_device *dev = to_device(item);
2218 
2219 	if (!(dev->dev_flags & DF_USING_ALIAS))
2220 		return 0;
2221 
2222 	return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2223 }
2224 
2225 static ssize_t target_dev_alias_store(struct config_item *item,
2226 		const char *page, size_t count)
2227 {
2228 	struct se_device *dev = to_device(item);
2229 	struct se_hba *hba = dev->se_hba;
2230 	ssize_t read_bytes;
2231 
2232 	if (count > (SE_DEV_ALIAS_LEN-1)) {
2233 		pr_err("alias count: %d exceeds"
2234 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2235 			SE_DEV_ALIAS_LEN-1);
2236 		return -EINVAL;
2237 	}
2238 
2239 	read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
2240 	if (!read_bytes)
2241 		return -EINVAL;
2242 	if (dev->dev_alias[read_bytes - 1] == '\n')
2243 		dev->dev_alias[read_bytes - 1] = '\0';
2244 
2245 	dev->dev_flags |= DF_USING_ALIAS;
2246 
2247 	pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2248 		config_item_name(&hba->hba_group.cg_item),
2249 		config_item_name(&dev->dev_group.cg_item),
2250 		dev->dev_alias);
2251 
2252 	return read_bytes;
2253 }
2254 
2255 static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2256 {
2257 	struct se_device *dev = to_device(item);
2258 
2259 	if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2260 		return 0;
2261 
2262 	return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2263 }
2264 
2265 static ssize_t target_dev_udev_path_store(struct config_item *item,
2266 		const char *page, size_t count)
2267 {
2268 	struct se_device *dev = to_device(item);
2269 	struct se_hba *hba = dev->se_hba;
2270 	ssize_t read_bytes;
2271 
2272 	if (count > (SE_UDEV_PATH_LEN-1)) {
2273 		pr_err("udev_path count: %d exceeds"
2274 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2275 			SE_UDEV_PATH_LEN-1);
2276 		return -EINVAL;
2277 	}
2278 
2279 	read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2280 			"%s", page);
2281 	if (!read_bytes)
2282 		return -EINVAL;
2283 	if (dev->udev_path[read_bytes - 1] == '\n')
2284 		dev->udev_path[read_bytes - 1] = '\0';
2285 
2286 	dev->dev_flags |= DF_USING_UDEV_PATH;
2287 
2288 	pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2289 		config_item_name(&hba->hba_group.cg_item),
2290 		config_item_name(&dev->dev_group.cg_item),
2291 		dev->udev_path);
2292 
2293 	return read_bytes;
2294 }
2295 
2296 static ssize_t target_dev_enable_show(struct config_item *item, char *page)
2297 {
2298 	struct se_device *dev = to_device(item);
2299 
2300 	return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
2301 }
2302 
2303 static ssize_t target_dev_enable_store(struct config_item *item,
2304 		const char *page, size_t count)
2305 {
2306 	struct se_device *dev = to_device(item);
2307 	char *ptr;
2308 	int ret;
2309 
2310 	ptr = strstr(page, "1");
2311 	if (!ptr) {
2312 		pr_err("For dev_enable ops, only valid value"
2313 				" is \"1\"\n");
2314 		return -EINVAL;
2315 	}
2316 
2317 	ret = target_configure_device(dev);
2318 	if (ret)
2319 		return ret;
2320 	return count;
2321 }
2322 
2323 static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2324 {
2325 	struct se_device *dev = to_device(item);
2326 	struct config_item *lu_ci;
2327 	struct t10_alua_lu_gp *lu_gp;
2328 	struct t10_alua_lu_gp_member *lu_gp_mem;
2329 	ssize_t len = 0;
2330 
2331 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2332 	if (!lu_gp_mem)
2333 		return 0;
2334 
2335 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2336 	lu_gp = lu_gp_mem->lu_gp;
2337 	if (lu_gp) {
2338 		lu_ci = &lu_gp->lu_gp_group.cg_item;
2339 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2340 			config_item_name(lu_ci), lu_gp->lu_gp_id);
2341 	}
2342 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2343 
2344 	return len;
2345 }
2346 
2347 static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
2348 		const char *page, size_t count)
2349 {
2350 	struct se_device *dev = to_device(item);
2351 	struct se_hba *hba = dev->se_hba;
2352 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2353 	struct t10_alua_lu_gp_member *lu_gp_mem;
2354 	unsigned char buf[LU_GROUP_NAME_BUF] = { };
2355 	int move = 0;
2356 
2357 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
2358 	if (!lu_gp_mem)
2359 		return count;
2360 
2361 	if (count > LU_GROUP_NAME_BUF) {
2362 		pr_err("ALUA LU Group Alias too large!\n");
2363 		return -EINVAL;
2364 	}
2365 	memcpy(buf, page, count);
2366 	/*
2367 	 * Any ALUA logical unit alias besides "NULL" means we will be
2368 	 * making a new group association.
2369 	 */
2370 	if (strcmp(strstrip(buf), "NULL")) {
2371 		/*
2372 		 * core_alua_get_lu_gp_by_name() will increment reference to
2373 		 * struct t10_alua_lu_gp.  This reference is released with
2374 		 * core_alua_get_lu_gp_by_name below().
2375 		 */
2376 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
2377 		if (!lu_gp_new)
2378 			return -ENODEV;
2379 	}
2380 
2381 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2382 	lu_gp = lu_gp_mem->lu_gp;
2383 	if (lu_gp) {
2384 		/*
2385 		 * Clearing an existing lu_gp association, and replacing
2386 		 * with NULL
2387 		 */
2388 		if (!lu_gp_new) {
2389 			pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2390 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2391 				" %hu\n",
2392 				config_item_name(&hba->hba_group.cg_item),
2393 				config_item_name(&dev->dev_group.cg_item),
2394 				config_item_name(&lu_gp->lu_gp_group.cg_item),
2395 				lu_gp->lu_gp_id);
2396 
2397 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2398 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2399 
2400 			return count;
2401 		}
2402 		/*
2403 		 * Removing existing association of lu_gp_mem with lu_gp
2404 		 */
2405 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2406 		move = 1;
2407 	}
2408 	/*
2409 	 * Associate lu_gp_mem with lu_gp_new.
2410 	 */
2411 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2412 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2413 
2414 	pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2415 		" core/alua/lu_gps/%s, ID: %hu\n",
2416 		(move) ? "Moving" : "Adding",
2417 		config_item_name(&hba->hba_group.cg_item),
2418 		config_item_name(&dev->dev_group.cg_item),
2419 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2420 		lu_gp_new->lu_gp_id);
2421 
2422 	core_alua_put_lu_gp_from_name(lu_gp_new);
2423 	return count;
2424 }
2425 
2426 static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2427 {
2428 	struct se_device *dev = to_device(item);
2429 	struct t10_alua_lba_map *map;
2430 	struct t10_alua_lba_map_member *mem;
2431 	char *b = page;
2432 	int bl = 0;
2433 	char state;
2434 
2435 	spin_lock(&dev->t10_alua.lba_map_lock);
2436 	if (!list_empty(&dev->t10_alua.lba_map_list))
2437 	    bl += sprintf(b + bl, "%u %u\n",
2438 			  dev->t10_alua.lba_map_segment_size,
2439 			  dev->t10_alua.lba_map_segment_multiplier);
2440 	list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2441 		bl += sprintf(b + bl, "%llu %llu",
2442 			      map->lba_map_first_lba, map->lba_map_last_lba);
2443 		list_for_each_entry(mem, &map->lba_map_mem_list,
2444 				    lba_map_mem_list) {
2445 			switch (mem->lba_map_mem_alua_state) {
2446 			case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2447 				state = 'O';
2448 				break;
2449 			case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2450 				state = 'A';
2451 				break;
2452 			case ALUA_ACCESS_STATE_STANDBY:
2453 				state = 'S';
2454 				break;
2455 			case ALUA_ACCESS_STATE_UNAVAILABLE:
2456 				state = 'U';
2457 				break;
2458 			default:
2459 				state = '.';
2460 				break;
2461 			}
2462 			bl += sprintf(b + bl, " %d:%c",
2463 				      mem->lba_map_mem_alua_pg_id, state);
2464 		}
2465 		bl += sprintf(b + bl, "\n");
2466 	}
2467 	spin_unlock(&dev->t10_alua.lba_map_lock);
2468 	return bl;
2469 }
2470 
2471 static ssize_t target_dev_lba_map_store(struct config_item *item,
2472 		const char *page, size_t count)
2473 {
2474 	struct se_device *dev = to_device(item);
2475 	struct t10_alua_lba_map *lba_map = NULL;
2476 	struct list_head lba_list;
2477 	char *map_entries, *orig, *ptr;
2478 	char state;
2479 	int pg_num = -1, pg;
2480 	int ret = 0, num = 0, pg_id, alua_state;
2481 	unsigned long start_lba = -1, end_lba = -1;
2482 	unsigned long segment_size = -1, segment_mult = -1;
2483 
2484 	orig = map_entries = kstrdup(page, GFP_KERNEL);
2485 	if (!map_entries)
2486 		return -ENOMEM;
2487 
2488 	INIT_LIST_HEAD(&lba_list);
2489 	while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2490 		if (!*ptr)
2491 			continue;
2492 
2493 		if (num == 0) {
2494 			if (sscanf(ptr, "%lu %lu\n",
2495 				   &segment_size, &segment_mult) != 2) {
2496 				pr_err("Invalid line %d\n", num);
2497 				ret = -EINVAL;
2498 				break;
2499 			}
2500 			num++;
2501 			continue;
2502 		}
2503 		if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2504 			pr_err("Invalid line %d\n", num);
2505 			ret = -EINVAL;
2506 			break;
2507 		}
2508 		ptr = strchr(ptr, ' ');
2509 		if (!ptr) {
2510 			pr_err("Invalid line %d, missing end lba\n", num);
2511 			ret = -EINVAL;
2512 			break;
2513 		}
2514 		ptr++;
2515 		ptr = strchr(ptr, ' ');
2516 		if (!ptr) {
2517 			pr_err("Invalid line %d, missing state definitions\n",
2518 			       num);
2519 			ret = -EINVAL;
2520 			break;
2521 		}
2522 		ptr++;
2523 		lba_map = core_alua_allocate_lba_map(&lba_list,
2524 						     start_lba, end_lba);
2525 		if (IS_ERR(lba_map)) {
2526 			ret = PTR_ERR(lba_map);
2527 			break;
2528 		}
2529 		pg = 0;
2530 		while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2531 			switch (state) {
2532 			case 'O':
2533 				alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2534 				break;
2535 			case 'A':
2536 				alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2537 				break;
2538 			case 'S':
2539 				alua_state = ALUA_ACCESS_STATE_STANDBY;
2540 				break;
2541 			case 'U':
2542 				alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2543 				break;
2544 			default:
2545 				pr_err("Invalid ALUA state '%c'\n", state);
2546 				ret = -EINVAL;
2547 				goto out;
2548 			}
2549 
2550 			ret = core_alua_allocate_lba_map_mem(lba_map,
2551 							     pg_id, alua_state);
2552 			if (ret) {
2553 				pr_err("Invalid target descriptor %d:%c "
2554 				       "at line %d\n",
2555 				       pg_id, state, num);
2556 				break;
2557 			}
2558 			pg++;
2559 			ptr = strchr(ptr, ' ');
2560 			if (ptr)
2561 				ptr++;
2562 			else
2563 				break;
2564 		}
2565 		if (pg_num == -1)
2566 		    pg_num = pg;
2567 		else if (pg != pg_num) {
2568 			pr_err("Only %d from %d port groups definitions "
2569 			       "at line %d\n", pg, pg_num, num);
2570 			ret = -EINVAL;
2571 			break;
2572 		}
2573 		num++;
2574 	}
2575 out:
2576 	if (ret) {
2577 		core_alua_free_lba_map(&lba_list);
2578 		count = ret;
2579 	} else
2580 		core_alua_set_lba_map(dev, &lba_list,
2581 				      segment_size, segment_mult);
2582 	kfree(orig);
2583 	return count;
2584 }
2585 
2586 CONFIGFS_ATTR_RO(target_dev_, info);
2587 CONFIGFS_ATTR_WO(target_dev_, control);
2588 CONFIGFS_ATTR(target_dev_, alias);
2589 CONFIGFS_ATTR(target_dev_, udev_path);
2590 CONFIGFS_ATTR(target_dev_, enable);
2591 CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2592 CONFIGFS_ATTR(target_dev_, lba_map);
2593 
2594 static struct configfs_attribute *target_core_dev_attrs[] = {
2595 	&target_dev_attr_info,
2596 	&target_dev_attr_control,
2597 	&target_dev_attr_alias,
2598 	&target_dev_attr_udev_path,
2599 	&target_dev_attr_enable,
2600 	&target_dev_attr_alua_lu_gp,
2601 	&target_dev_attr_lba_map,
2602 	NULL,
2603 };
2604 
2605 static void target_core_dev_release(struct config_item *item)
2606 {
2607 	struct config_group *dev_cg = to_config_group(item);
2608 	struct se_device *dev =
2609 		container_of(dev_cg, struct se_device, dev_group);
2610 
2611 	target_free_device(dev);
2612 }
2613 
2614 /*
2615  * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2616  * within target_fabric_port_link()
2617  */
2618 struct configfs_item_operations target_core_dev_item_ops = {
2619 	.release		= target_core_dev_release,
2620 };
2621 
2622 TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2623 
2624 /* End functions for struct config_item_type tb_dev_cit */
2625 
2626 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2627 
2628 static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2629 {
2630 	return container_of(to_config_group(item), struct t10_alua_lu_gp,
2631 			lu_gp_group);
2632 }
2633 
2634 static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2635 {
2636 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2637 
2638 	if (!lu_gp->lu_gp_valid_id)
2639 		return 0;
2640 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2641 }
2642 
2643 static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2644 		const char *page, size_t count)
2645 {
2646 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2647 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2648 	unsigned long lu_gp_id;
2649 	int ret;
2650 
2651 	ret = kstrtoul(page, 0, &lu_gp_id);
2652 	if (ret < 0) {
2653 		pr_err("kstrtoul() returned %d for"
2654 			" lu_gp_id\n", ret);
2655 		return ret;
2656 	}
2657 	if (lu_gp_id > 0x0000ffff) {
2658 		pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2659 			" 0x0000ffff\n", lu_gp_id);
2660 		return -EINVAL;
2661 	}
2662 
2663 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2664 	if (ret < 0)
2665 		return -EINVAL;
2666 
2667 	pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2668 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2669 		config_item_name(&alua_lu_gp_cg->cg_item),
2670 		lu_gp->lu_gp_id);
2671 
2672 	return count;
2673 }
2674 
2675 static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2676 {
2677 	struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2678 	struct se_device *dev;
2679 	struct se_hba *hba;
2680 	struct t10_alua_lu_gp_member *lu_gp_mem;
2681 	ssize_t len = 0, cur_len;
2682 	unsigned char buf[LU_GROUP_NAME_BUF] = { };
2683 
2684 	spin_lock(&lu_gp->lu_gp_lock);
2685 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2686 		dev = lu_gp_mem->lu_gp_mem_dev;
2687 		hba = dev->se_hba;
2688 
2689 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2690 			config_item_name(&hba->hba_group.cg_item),
2691 			config_item_name(&dev->dev_group.cg_item));
2692 		cur_len++; /* Extra byte for NULL terminator */
2693 
2694 		if ((cur_len + len) > PAGE_SIZE) {
2695 			pr_warn("Ran out of lu_gp_show_attr"
2696 				"_members buffer\n");
2697 			break;
2698 		}
2699 		memcpy(page+len, buf, cur_len);
2700 		len += cur_len;
2701 	}
2702 	spin_unlock(&lu_gp->lu_gp_lock);
2703 
2704 	return len;
2705 }
2706 
2707 CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2708 CONFIGFS_ATTR_RO(target_lu_gp_, members);
2709 
2710 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2711 	&target_lu_gp_attr_lu_gp_id,
2712 	&target_lu_gp_attr_members,
2713 	NULL,
2714 };
2715 
2716 static void target_core_alua_lu_gp_release(struct config_item *item)
2717 {
2718 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2719 			struct t10_alua_lu_gp, lu_gp_group);
2720 
2721 	core_alua_free_lu_gp(lu_gp);
2722 }
2723 
2724 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2725 	.release		= target_core_alua_lu_gp_release,
2726 };
2727 
2728 static const struct config_item_type target_core_alua_lu_gp_cit = {
2729 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2730 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2731 	.ct_owner		= THIS_MODULE,
2732 };
2733 
2734 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2735 
2736 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2737 
2738 static struct config_group *target_core_alua_create_lu_gp(
2739 	struct config_group *group,
2740 	const char *name)
2741 {
2742 	struct t10_alua_lu_gp *lu_gp;
2743 	struct config_group *alua_lu_gp_cg = NULL;
2744 	struct config_item *alua_lu_gp_ci = NULL;
2745 
2746 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2747 	if (IS_ERR(lu_gp))
2748 		return NULL;
2749 
2750 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2751 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2752 
2753 	config_group_init_type_name(alua_lu_gp_cg, name,
2754 			&target_core_alua_lu_gp_cit);
2755 
2756 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2757 		" Group: core/alua/lu_gps/%s\n",
2758 		config_item_name(alua_lu_gp_ci));
2759 
2760 	return alua_lu_gp_cg;
2761 
2762 }
2763 
2764 static void target_core_alua_drop_lu_gp(
2765 	struct config_group *group,
2766 	struct config_item *item)
2767 {
2768 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2769 			struct t10_alua_lu_gp, lu_gp_group);
2770 
2771 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2772 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2773 		config_item_name(item), lu_gp->lu_gp_id);
2774 	/*
2775 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2776 	 * -> target_core_alua_lu_gp_release()
2777 	 */
2778 	config_item_put(item);
2779 }
2780 
2781 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2782 	.make_group		= &target_core_alua_create_lu_gp,
2783 	.drop_item		= &target_core_alua_drop_lu_gp,
2784 };
2785 
2786 static const struct config_item_type target_core_alua_lu_gps_cit = {
2787 	.ct_item_ops		= NULL,
2788 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2789 	.ct_owner		= THIS_MODULE,
2790 };
2791 
2792 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2793 
2794 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2795 
2796 static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
2797 {
2798 	return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2799 			tg_pt_gp_group);
2800 }
2801 
2802 static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2803 		char *page)
2804 {
2805 	return sprintf(page, "%d\n",
2806 		       to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2807 }
2808 
2809 static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2810 		const char *page, size_t count)
2811 {
2812 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2813 	struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2814 	unsigned long tmp;
2815 	int new_state, ret;
2816 
2817 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2818 		pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
2819 		return -EINVAL;
2820 	}
2821 	if (!target_dev_configured(dev)) {
2822 		pr_err("Unable to set alua_access_state while device is"
2823 		       " not configured\n");
2824 		return -ENODEV;
2825 	}
2826 
2827 	ret = kstrtoul(page, 0, &tmp);
2828 	if (ret < 0) {
2829 		pr_err("Unable to extract new ALUA access state from"
2830 				" %s\n", page);
2831 		return ret;
2832 	}
2833 	new_state = (int)tmp;
2834 
2835 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2836 		pr_err("Unable to process implicit configfs ALUA"
2837 			" transition while TPGS_IMPLICIT_ALUA is disabled\n");
2838 		return -EINVAL;
2839 	}
2840 	if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2841 	    new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2842 		/* LBA DEPENDENT is only allowed with implicit ALUA */
2843 		pr_err("Unable to process implicit configfs ALUA transition"
2844 		       " while explicit ALUA management is enabled\n");
2845 		return -EINVAL;
2846 	}
2847 
2848 	ret = core_alua_do_port_transition(tg_pt_gp, dev,
2849 					NULL, NULL, new_state, 0);
2850 	return (!ret) ? count : -EINVAL;
2851 }
2852 
2853 static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2854 		char *page)
2855 {
2856 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2857 	return sprintf(page, "%s\n",
2858 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2859 }
2860 
2861 static ssize_t target_tg_pt_gp_alua_access_status_store(
2862 		struct config_item *item, const char *page, size_t count)
2863 {
2864 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2865 	unsigned long tmp;
2866 	int new_status, ret;
2867 
2868 	if (!tg_pt_gp->tg_pt_gp_valid_id) {
2869 		pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
2870 		return -EINVAL;
2871 	}
2872 
2873 	ret = kstrtoul(page, 0, &tmp);
2874 	if (ret < 0) {
2875 		pr_err("Unable to extract new ALUA access status"
2876 				" from %s\n", page);
2877 		return ret;
2878 	}
2879 	new_status = (int)tmp;
2880 
2881 	if ((new_status != ALUA_STATUS_NONE) &&
2882 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2883 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2884 		pr_err("Illegal ALUA access status: 0x%02x\n",
2885 				new_status);
2886 		return -EINVAL;
2887 	}
2888 
2889 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2890 	return count;
2891 }
2892 
2893 static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2894 		char *page)
2895 {
2896 	return core_alua_show_access_type(to_tg_pt_gp(item), page);
2897 }
2898 
2899 static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2900 		const char *page, size_t count)
2901 {
2902 	return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2903 }
2904 
2905 #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit)				\
2906 static ssize_t target_tg_pt_gp_alua_support_##_name##_show(		\
2907 		struct config_item *item, char *p)			\
2908 {									\
2909 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2910 	return sprintf(p, "%d\n",					\
2911 		!!(t->tg_pt_gp_alua_supported_states & _bit));		\
2912 }									\
2913 									\
2914 static ssize_t target_tg_pt_gp_alua_support_##_name##_store(		\
2915 		struct config_item *item, const char *p, size_t c)	\
2916 {									\
2917 	struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item);		\
2918 	unsigned long tmp;						\
2919 	int ret;							\
2920 									\
2921 	if (!t->tg_pt_gp_valid_id) {					\
2922 		pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
2923 		return -EINVAL;						\
2924 	}								\
2925 									\
2926 	ret = kstrtoul(p, 0, &tmp);					\
2927 	if (ret < 0) {							\
2928 		pr_err("Invalid value '%s', must be '0' or '1'\n", p);	\
2929 		return -EINVAL;						\
2930 	}								\
2931 	if (tmp > 1) {							\
2932 		pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
2933 		return -EINVAL;						\
2934 	}								\
2935 	if (tmp)							\
2936 		t->tg_pt_gp_alua_supported_states |= _bit;		\
2937 	else								\
2938 		t->tg_pt_gp_alua_supported_states &= ~_bit;		\
2939 									\
2940 	return c;							\
2941 }
2942 
2943 ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
2944 ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
2945 ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
2946 ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
2947 ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
2948 ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
2949 ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
2950 
2951 static ssize_t target_tg_pt_gp_alua_write_metadata_show(
2952 		struct config_item *item, char *page)
2953 {
2954 	return sprintf(page, "%d\n",
2955 		to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
2956 }
2957 
2958 static ssize_t target_tg_pt_gp_alua_write_metadata_store(
2959 		struct config_item *item, const char *page, size_t count)
2960 {
2961 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2962 	unsigned long tmp;
2963 	int ret;
2964 
2965 	ret = kstrtoul(page, 0, &tmp);
2966 	if (ret < 0) {
2967 		pr_err("Unable to extract alua_write_metadata\n");
2968 		return ret;
2969 	}
2970 
2971 	if ((tmp != 0) && (tmp != 1)) {
2972 		pr_err("Illegal value for alua_write_metadata:"
2973 			" %lu\n", tmp);
2974 		return -EINVAL;
2975 	}
2976 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2977 
2978 	return count;
2979 }
2980 
2981 static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
2982 		char *page)
2983 {
2984 	return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
2985 }
2986 
2987 static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
2988 		const char *page, size_t count)
2989 {
2990 	return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
2991 			count);
2992 }
2993 
2994 static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
2995 		char *page)
2996 {
2997 	return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
2998 }
2999 
3000 static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
3001 		const char *page, size_t count)
3002 {
3003 	return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
3004 			count);
3005 }
3006 
3007 static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
3008 		struct config_item *item, char *page)
3009 {
3010 	return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
3011 }
3012 
3013 static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
3014 		struct config_item *item, const char *page, size_t count)
3015 {
3016 	return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
3017 			count);
3018 }
3019 
3020 static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
3021 		char *page)
3022 {
3023 	return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
3024 }
3025 
3026 static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
3027 		const char *page, size_t count)
3028 {
3029 	return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
3030 }
3031 
3032 static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3033 		char *page)
3034 {
3035 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3036 
3037 	if (!tg_pt_gp->tg_pt_gp_valid_id)
3038 		return 0;
3039 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3040 }
3041 
3042 static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
3043 		const char *page, size_t count)
3044 {
3045 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3046 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3047 	unsigned long tg_pt_gp_id;
3048 	int ret;
3049 
3050 	ret = kstrtoul(page, 0, &tg_pt_gp_id);
3051 	if (ret < 0) {
3052 		pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
3053 		       page);
3054 		return ret;
3055 	}
3056 	if (tg_pt_gp_id > 0x0000ffff) {
3057 		pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
3058 		       tg_pt_gp_id);
3059 		return -EINVAL;
3060 	}
3061 
3062 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3063 	if (ret < 0)
3064 		return -EINVAL;
3065 
3066 	pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
3067 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
3068 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
3069 		tg_pt_gp->tg_pt_gp_id);
3070 
3071 	return count;
3072 }
3073 
3074 static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3075 		char *page)
3076 {
3077 	struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3078 	struct se_lun *lun;
3079 	ssize_t len = 0, cur_len;
3080 	unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
3081 
3082 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
3083 	list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3084 			lun_tg_pt_gp_link) {
3085 		struct se_portal_group *tpg = lun->lun_tpg;
3086 
3087 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
3088 			"/%s\n", tpg->se_tpg_tfo->fabric_name,
3089 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3090 			tpg->se_tpg_tfo->tpg_get_tag(tpg),
3091 			config_item_name(&lun->lun_group.cg_item));
3092 		cur_len++; /* Extra byte for NULL terminator */
3093 
3094 		if ((cur_len + len) > PAGE_SIZE) {
3095 			pr_warn("Ran out of lu_gp_show_attr"
3096 				"_members buffer\n");
3097 			break;
3098 		}
3099 		memcpy(page+len, buf, cur_len);
3100 		len += cur_len;
3101 	}
3102 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3103 
3104 	return len;
3105 }
3106 
3107 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
3108 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
3109 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
3110 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
3111 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
3112 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
3113 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
3114 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
3115 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
3116 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
3117 CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
3118 CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
3119 CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
3120 CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
3121 CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
3122 CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
3123 CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3124 
3125 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
3126 	&target_tg_pt_gp_attr_alua_access_state,
3127 	&target_tg_pt_gp_attr_alua_access_status,
3128 	&target_tg_pt_gp_attr_alua_access_type,
3129 	&target_tg_pt_gp_attr_alua_support_transitioning,
3130 	&target_tg_pt_gp_attr_alua_support_offline,
3131 	&target_tg_pt_gp_attr_alua_support_lba_dependent,
3132 	&target_tg_pt_gp_attr_alua_support_unavailable,
3133 	&target_tg_pt_gp_attr_alua_support_standby,
3134 	&target_tg_pt_gp_attr_alua_support_active_nonoptimized,
3135 	&target_tg_pt_gp_attr_alua_support_active_optimized,
3136 	&target_tg_pt_gp_attr_alua_write_metadata,
3137 	&target_tg_pt_gp_attr_nonop_delay_msecs,
3138 	&target_tg_pt_gp_attr_trans_delay_msecs,
3139 	&target_tg_pt_gp_attr_implicit_trans_secs,
3140 	&target_tg_pt_gp_attr_preferred,
3141 	&target_tg_pt_gp_attr_tg_pt_gp_id,
3142 	&target_tg_pt_gp_attr_members,
3143 	NULL,
3144 };
3145 
3146 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
3147 {
3148 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3149 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3150 
3151 	core_alua_free_tg_pt_gp(tg_pt_gp);
3152 }
3153 
3154 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
3155 	.release		= target_core_alua_tg_pt_gp_release,
3156 };
3157 
3158 static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3159 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
3160 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
3161 	.ct_owner		= THIS_MODULE,
3162 };
3163 
3164 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3165 
3166 /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3167 
3168 static struct config_group *target_core_alua_create_tg_pt_gp(
3169 	struct config_group *group,
3170 	const char *name)
3171 {
3172 	struct t10_alua *alua = container_of(group, struct t10_alua,
3173 					alua_tg_pt_gps_group);
3174 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3175 	struct config_group *alua_tg_pt_gp_cg = NULL;
3176 	struct config_item *alua_tg_pt_gp_ci = NULL;
3177 
3178 	tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
3179 	if (!tg_pt_gp)
3180 		return NULL;
3181 
3182 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3183 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3184 
3185 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
3186 			&target_core_alua_tg_pt_gp_cit);
3187 
3188 	pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3189 		" Group: alua/tg_pt_gps/%s\n",
3190 		config_item_name(alua_tg_pt_gp_ci));
3191 
3192 	return alua_tg_pt_gp_cg;
3193 }
3194 
3195 static void target_core_alua_drop_tg_pt_gp(
3196 	struct config_group *group,
3197 	struct config_item *item)
3198 {
3199 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3200 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3201 
3202 	pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3203 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
3204 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
3205 	/*
3206 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
3207 	 * -> target_core_alua_tg_pt_gp_release().
3208 	 */
3209 	config_item_put(item);
3210 }
3211 
3212 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3213 	.make_group		= &target_core_alua_create_tg_pt_gp,
3214 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
3215 };
3216 
3217 TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3218 
3219 /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3220 
3221 /* Start functions for struct config_item_type target_core_alua_cit */
3222 
3223 /*
3224  * target_core_alua_cit is a ConfigFS group that lives under
3225  * /sys/kernel/config/target/core/alua.  There are default groups
3226  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3227  * target_core_alua_cit in target_core_init_configfs() below.
3228  */
3229 static const struct config_item_type target_core_alua_cit = {
3230 	.ct_item_ops		= NULL,
3231 	.ct_attrs		= NULL,
3232 	.ct_owner		= THIS_MODULE,
3233 };
3234 
3235 /* End functions for struct config_item_type target_core_alua_cit */
3236 
3237 /* Start functions for struct config_item_type tb_dev_stat_cit */
3238 
3239 static struct config_group *target_core_stat_mkdir(
3240 	struct config_group *group,
3241 	const char *name)
3242 {
3243 	return ERR_PTR(-ENOSYS);
3244 }
3245 
3246 static void target_core_stat_rmdir(
3247 	struct config_group *group,
3248 	struct config_item *item)
3249 {
3250 	return;
3251 }
3252 
3253 static struct configfs_group_operations target_core_stat_group_ops = {
3254 	.make_group		= &target_core_stat_mkdir,
3255 	.drop_item		= &target_core_stat_rmdir,
3256 };
3257 
3258 TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
3259 
3260 /* End functions for struct config_item_type tb_dev_stat_cit */
3261 
3262 /* Start functions for struct config_item_type target_core_hba_cit */
3263 
3264 static struct config_group *target_core_make_subdev(
3265 	struct config_group *group,
3266 	const char *name)
3267 {
3268 	struct t10_alua_tg_pt_gp *tg_pt_gp;
3269 	struct config_item *hba_ci = &group->cg_item;
3270 	struct se_hba *hba = item_to_hba(hba_ci);
3271 	struct target_backend *tb = hba->backend;
3272 	struct se_device *dev;
3273 	int errno = -ENOMEM, ret;
3274 
3275 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3276 	if (ret)
3277 		return ERR_PTR(ret);
3278 
3279 	dev = target_alloc_device(hba, name);
3280 	if (!dev)
3281 		goto out_unlock;
3282 
3283 	config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3284 
3285 	config_group_init_type_name(&dev->dev_action_group, "action",
3286 			&tb->tb_dev_action_cit);
3287 	configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
3288 
3289 	config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
3290 			&tb->tb_dev_attrib_cit);
3291 	configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
3292 
3293 	config_group_init_type_name(&dev->dev_pr_group, "pr",
3294 			&tb->tb_dev_pr_cit);
3295 	configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
3296 
3297 	config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
3298 			&tb->tb_dev_wwn_cit);
3299 	configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
3300 			&dev->dev_group);
3301 
3302 	config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
3303 			"alua", &tb->tb_dev_alua_tg_pt_gps_cit);
3304 	configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
3305 			&dev->dev_group);
3306 
3307 	config_group_init_type_name(&dev->dev_stat_grps.stat_group,
3308 			"statistics", &tb->tb_dev_stat_cit);
3309 	configfs_add_default_group(&dev->dev_stat_grps.stat_group,
3310 			&dev->dev_group);
3311 
3312 	/*
3313 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3314 	 */
3315 	tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
3316 	if (!tg_pt_gp)
3317 		goto out_free_device;
3318 	dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3319 
3320 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3321 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
3322 	configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
3323 			&dev->t10_alua.alua_tg_pt_gps_group);
3324 
3325 	/*
3326 	 * Add core/$HBA/$DEV/statistics/ default groups
3327 	 */
3328 	target_stat_setup_dev_default_groups(dev);
3329 
3330 	mutex_lock(&target_devices_lock);
3331 	target_devices++;
3332 	mutex_unlock(&target_devices_lock);
3333 
3334 	mutex_unlock(&hba->hba_access_mutex);
3335 	return &dev->dev_group;
3336 
3337 out_free_device:
3338 	target_free_device(dev);
3339 out_unlock:
3340 	mutex_unlock(&hba->hba_access_mutex);
3341 	return ERR_PTR(errno);
3342 }
3343 
3344 static void target_core_drop_subdev(
3345 	struct config_group *group,
3346 	struct config_item *item)
3347 {
3348 	struct config_group *dev_cg = to_config_group(item);
3349 	struct se_device *dev =
3350 		container_of(dev_cg, struct se_device, dev_group);
3351 	struct se_hba *hba;
3352 
3353 	hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3354 
3355 	mutex_lock(&hba->hba_access_mutex);
3356 
3357 	configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
3358 	configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
3359 
3360 	/*
3361 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3362 	 * directly from target_core_alua_tg_pt_gp_release().
3363 	 */
3364 	dev->t10_alua.default_tg_pt_gp = NULL;
3365 
3366 	configfs_remove_default_groups(dev_cg);
3367 
3368 	/*
3369 	 * se_dev is released from target_core_dev_item_ops->release()
3370 	 */
3371 	config_item_put(item);
3372 
3373 	mutex_lock(&target_devices_lock);
3374 	target_devices--;
3375 	mutex_unlock(&target_devices_lock);
3376 
3377 	mutex_unlock(&hba->hba_access_mutex);
3378 }
3379 
3380 static struct configfs_group_operations target_core_hba_group_ops = {
3381 	.make_group		= target_core_make_subdev,
3382 	.drop_item		= target_core_drop_subdev,
3383 };
3384 
3385 
3386 static inline struct se_hba *to_hba(struct config_item *item)
3387 {
3388 	return container_of(to_config_group(item), struct se_hba, hba_group);
3389 }
3390 
3391 static ssize_t target_hba_info_show(struct config_item *item, char *page)
3392 {
3393 	struct se_hba *hba = to_hba(item);
3394 
3395 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
3396 			hba->hba_id, hba->backend->ops->name,
3397 			TARGET_CORE_VERSION);
3398 }
3399 
3400 static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3401 {
3402 	struct se_hba *hba = to_hba(item);
3403 	int hba_mode = 0;
3404 
3405 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3406 		hba_mode = 1;
3407 
3408 	return sprintf(page, "%d\n", hba_mode);
3409 }
3410 
3411 static ssize_t target_hba_mode_store(struct config_item *item,
3412 		const char *page, size_t count)
3413 {
3414 	struct se_hba *hba = to_hba(item);
3415 	unsigned long mode_flag;
3416 	int ret;
3417 
3418 	if (hba->backend->ops->pmode_enable_hba == NULL)
3419 		return -EINVAL;
3420 
3421 	ret = kstrtoul(page, 0, &mode_flag);
3422 	if (ret < 0) {
3423 		pr_err("Unable to extract hba mode flag: %d\n", ret);
3424 		return ret;
3425 	}
3426 
3427 	if (hba->dev_count) {
3428 		pr_err("Unable to set hba_mode with active devices\n");
3429 		return -EINVAL;
3430 	}
3431 
3432 	ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3433 	if (ret < 0)
3434 		return -EINVAL;
3435 	if (ret > 0)
3436 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3437 	else if (ret == 0)
3438 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3439 
3440 	return count;
3441 }
3442 
3443 CONFIGFS_ATTR_RO(target_, hba_info);
3444 CONFIGFS_ATTR(target_, hba_mode);
3445 
3446 static void target_core_hba_release(struct config_item *item)
3447 {
3448 	struct se_hba *hba = container_of(to_config_group(item),
3449 				struct se_hba, hba_group);
3450 	core_delete_hba(hba);
3451 }
3452 
3453 static struct configfs_attribute *target_core_hba_attrs[] = {
3454 	&target_attr_hba_info,
3455 	&target_attr_hba_mode,
3456 	NULL,
3457 };
3458 
3459 static struct configfs_item_operations target_core_hba_item_ops = {
3460 	.release		= target_core_hba_release,
3461 };
3462 
3463 static const struct config_item_type target_core_hba_cit = {
3464 	.ct_item_ops		= &target_core_hba_item_ops,
3465 	.ct_group_ops		= &target_core_hba_group_ops,
3466 	.ct_attrs		= target_core_hba_attrs,
3467 	.ct_owner		= THIS_MODULE,
3468 };
3469 
3470 static struct config_group *target_core_call_addhbatotarget(
3471 	struct config_group *group,
3472 	const char *name)
3473 {
3474 	char *se_plugin_str, *str, *str2;
3475 	struct se_hba *hba;
3476 	char buf[TARGET_CORE_NAME_MAX_LEN] = { };
3477 	unsigned long plugin_dep_id = 0;
3478 	int ret;
3479 
3480 	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3481 		pr_err("Passed *name strlen(): %d exceeds"
3482 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3483 			TARGET_CORE_NAME_MAX_LEN);
3484 		return ERR_PTR(-ENAMETOOLONG);
3485 	}
3486 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3487 
3488 	str = strstr(buf, "_");
3489 	if (!str) {
3490 		pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3491 		return ERR_PTR(-EINVAL);
3492 	}
3493 	se_plugin_str = buf;
3494 	/*
3495 	 * Special case for subsystem plugins that have "_" in their names.
3496 	 * Namely rd_direct and rd_mcp..
3497 	 */
3498 	str2 = strstr(str+1, "_");
3499 	if (str2) {
3500 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3501 		str2++; /* Skip to start of plugin dependent ID */
3502 		str = str2;
3503 	} else {
3504 		*str = '\0'; /* Terminate for *se_plugin_str */
3505 		str++; /* Skip to start of plugin dependent ID */
3506 	}
3507 
3508 	ret = kstrtoul(str, 0, &plugin_dep_id);
3509 	if (ret < 0) {
3510 		pr_err("kstrtoul() returned %d for"
3511 				" plugin_dep_id\n", ret);
3512 		return ERR_PTR(ret);
3513 	}
3514 	/*
3515 	 * Load up TCM subsystem plugins if they have not already been loaded.
3516 	 */
3517 	transport_subsystem_check_init();
3518 
3519 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3520 	if (IS_ERR(hba))
3521 		return ERR_CAST(hba);
3522 
3523 	config_group_init_type_name(&hba->hba_group, name,
3524 			&target_core_hba_cit);
3525 
3526 	return &hba->hba_group;
3527 }
3528 
3529 static void target_core_call_delhbafromtarget(
3530 	struct config_group *group,
3531 	struct config_item *item)
3532 {
3533 	/*
3534 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
3535 	 * -> target_core_hba_release()
3536 	 */
3537 	config_item_put(item);
3538 }
3539 
3540 static struct configfs_group_operations target_core_group_ops = {
3541 	.make_group	= target_core_call_addhbatotarget,
3542 	.drop_item	= target_core_call_delhbafromtarget,
3543 };
3544 
3545 static const struct config_item_type target_core_cit = {
3546 	.ct_item_ops	= NULL,
3547 	.ct_group_ops	= &target_core_group_ops,
3548 	.ct_attrs	= NULL,
3549 	.ct_owner	= THIS_MODULE,
3550 };
3551 
3552 /* Stop functions for struct config_item_type target_core_hba_cit */
3553 
3554 void target_setup_backend_cits(struct target_backend *tb)
3555 {
3556 	target_core_setup_dev_cit(tb);
3557 	target_core_setup_dev_action_cit(tb);
3558 	target_core_setup_dev_attrib_cit(tb);
3559 	target_core_setup_dev_pr_cit(tb);
3560 	target_core_setup_dev_wwn_cit(tb);
3561 	target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3562 	target_core_setup_dev_stat_cit(tb);
3563 }
3564 
3565 static void target_init_dbroot(void)
3566 {
3567 	struct file *fp;
3568 
3569 	snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
3570 	fp = filp_open(db_root_stage, O_RDONLY, 0);
3571 	if (IS_ERR(fp)) {
3572 		pr_err("db_root: cannot open: %s\n", db_root_stage);
3573 		return;
3574 	}
3575 	if (!S_ISDIR(file_inode(fp)->i_mode)) {
3576 		filp_close(fp, NULL);
3577 		pr_err("db_root: not a valid directory: %s\n", db_root_stage);
3578 		return;
3579 	}
3580 	filp_close(fp, NULL);
3581 
3582 	strncpy(db_root, db_root_stage, DB_ROOT_LEN);
3583 	pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
3584 }
3585 
3586 static int __init target_core_init_configfs(void)
3587 {
3588 	struct configfs_subsystem *subsys = &target_core_fabrics;
3589 	struct t10_alua_lu_gp *lu_gp;
3590 	int ret;
3591 
3592 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3593 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3594 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3595 
3596 	config_group_init(&subsys->su_group);
3597 	mutex_init(&subsys->su_mutex);
3598 
3599 	ret = init_se_kmem_caches();
3600 	if (ret < 0)
3601 		return ret;
3602 	/*
3603 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3604 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3605 	 */
3606 	config_group_init_type_name(&target_core_hbagroup, "core",
3607 			&target_core_cit);
3608 	configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3609 
3610 	/*
3611 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3612 	 */
3613 	config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3614 	configfs_add_default_group(&alua_group, &target_core_hbagroup);
3615 
3616 	/*
3617 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3618 	 * groups under /sys/kernel/config/target/core/alua/
3619 	 */
3620 	config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3621 			&target_core_alua_lu_gps_cit);
3622 	configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3623 
3624 	/*
3625 	 * Add core/alua/lu_gps/default_lu_gp
3626 	 */
3627 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3628 	if (IS_ERR(lu_gp)) {
3629 		ret = -ENOMEM;
3630 		goto out_global;
3631 	}
3632 
3633 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3634 				&target_core_alua_lu_gp_cit);
3635 	configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3636 
3637 	default_lu_gp = lu_gp;
3638 
3639 	/*
3640 	 * Register the target_core_mod subsystem with configfs.
3641 	 */
3642 	ret = configfs_register_subsystem(subsys);
3643 	if (ret < 0) {
3644 		pr_err("Error %d while registering subsystem %s\n",
3645 			ret, subsys->su_group.cg_item.ci_namebuf);
3646 		goto out_global;
3647 	}
3648 	pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3649 		" Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3650 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3651 	/*
3652 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3653 	 */
3654 	ret = rd_module_init();
3655 	if (ret < 0)
3656 		goto out;
3657 
3658 	ret = core_dev_setup_virtual_lun0();
3659 	if (ret < 0)
3660 		goto out;
3661 
3662 	ret = target_xcopy_setup_pt();
3663 	if (ret < 0)
3664 		goto out;
3665 
3666 	target_init_dbroot();
3667 
3668 	return 0;
3669 
3670 out:
3671 	configfs_unregister_subsystem(subsys);
3672 	core_dev_release_virtual_lun0();
3673 	rd_module_exit();
3674 out_global:
3675 	if (default_lu_gp) {
3676 		core_alua_free_lu_gp(default_lu_gp);
3677 		default_lu_gp = NULL;
3678 	}
3679 	release_se_kmem_caches();
3680 	return ret;
3681 }
3682 
3683 static void __exit target_core_exit_configfs(void)
3684 {
3685 	configfs_remove_default_groups(&alua_lu_gps_group);
3686 	configfs_remove_default_groups(&alua_group);
3687 	configfs_remove_default_groups(&target_core_hbagroup);
3688 
3689 	/*
3690 	 * We expect subsys->su_group.default_groups to be released
3691 	 * by configfs subsystem provider logic..
3692 	 */
3693 	configfs_unregister_subsystem(&target_core_fabrics);
3694 
3695 	core_alua_free_lu_gp(default_lu_gp);
3696 	default_lu_gp = NULL;
3697 
3698 	pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3699 			" Infrastructure\n");
3700 
3701 	core_dev_release_virtual_lun0();
3702 	rd_module_exit();
3703 	target_xcopy_release_pt();
3704 	release_se_kmem_caches();
3705 }
3706 
3707 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3708 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3709 MODULE_LICENSE("GPL");
3710 
3711 module_init(target_core_init_configfs);
3712 module_exit(target_core_exit_configfs);
3713