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