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