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