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