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