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