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