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