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