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