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