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