1 /*******************************************************************************
2  * Filename:  target_core_configfs.c
3  *
4  * This file contains ConfigFS logic for the Generic Target Engine project.
5  *
6  * Copyright (c) 2008-2011 Rising Tide Systems
7  * Copyright (c) 2008-2011 Linux-iSCSI.org
8  *
9  * Nicholas A. Bellinger <nab@kernel.org>
10  *
11  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  ****************************************************************************/
23 
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/version.h>
27 #include <generated/utsrelease.h>
28 #include <linux/utsname.h>
29 #include <linux/init.h>
30 #include <linux/fs.h>
31 #include <linux/namei.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/delay.h>
35 #include <linux/unistd.h>
36 #include <linux/string.h>
37 #include <linux/parser.h>
38 #include <linux/syscalls.h>
39 #include <linux/configfs.h>
40 
41 #include <target/target_core_base.h>
42 #include <target/target_core_device.h>
43 #include <target/target_core_transport.h>
44 #include <target/target_core_fabric_ops.h>
45 #include <target/target_core_fabric_configfs.h>
46 #include <target/target_core_configfs.h>
47 #include <target/configfs_macros.h>
48 
49 #include "target_core_alua.h"
50 #include "target_core_hba.h"
51 #include "target_core_pr.h"
52 #include "target_core_rd.h"
53 #include "target_core_stat.h"
54 
55 static struct list_head g_tf_list;
56 static struct mutex g_tf_lock;
57 
58 struct target_core_configfs_attribute {
59 	struct configfs_attribute attr;
60 	ssize_t (*show)(void *, char *);
61 	ssize_t (*store)(void *, const char *, size_t);
62 };
63 
64 static inline struct se_hba *
item_to_hba(struct config_item * item)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  */
target_core_attr_show(struct config_item * item,struct configfs_attribute * attr,char * page)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 
target_core_get_fabric(const char * name)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  */
target_core_register_fabric(struct config_group * group,const char * name)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  */
target_core_deregister_fabric(struct config_group * group,struct config_item * item)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  */
target_fabric_configfs_init(struct module * fabric_mod,const char * name)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  */
target_fabric_configfs_free(struct target_fabric_configfs * tf)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  */
target_fabric_tf_ops_check(struct target_fabric_configfs * tf)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  */
target_fabric_configfs_register(struct target_fabric_configfs * tf)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 
target_fabric_configfs_deregister(struct target_fabric_configfs * tf)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  */
target_core_dev_wwn_show_attr_vpd_unit_serial(struct t10_wwn * t10_wwn,char * page)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 
target_core_dev_wwn_store_attr_vpd_unit_serial(struct t10_wwn * t10_wwn,const char * page,size_t count)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  */
target_core_dev_wwn_show_attr_vpd_protocol_identifier(struct t10_wwn * t10_wwn,char * page)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 
target_core_dev_wwn_store_attr_vpd_protocol_identifier(struct t10_wwn * t10_wwn,const char * page,size_t count)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 
target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(struct t10_wwn * t10_wwn,const char * page,size_t count)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 
target_core_dev_wwn_store_attr_vpd_assoc_target_port(struct t10_wwn * t10_wwn,const char * page,size_t count)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 
target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(struct t10_wwn * t10_wwn,const char * page,size_t count)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  */
target_core_dev_pr_show_spc3_res(struct se_device * dev,char * page,ssize_t * len)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 
target_core_dev_pr_show_spc2_res(struct se_device * dev,char * page,ssize_t * len)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 
target_core_dev_pr_show_attr_res_holder(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_pr_all_tgt_pts(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_pr_generation(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_pr_holder_tg_port(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_pr_registered_i_pts(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_pr_type(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_type(struct se_subsystem_dev * su_dev,char * page)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 
target_core_dev_pr_show_attr_res_aptpl_active(struct se_subsystem_dev * su_dev,char * page)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  */
target_core_dev_pr_show_attr_res_aptpl_metadata(struct se_subsystem_dev * su_dev,char * page)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 
target_core_dev_pr_store_attr_res_aptpl_metadata(struct se_subsystem_dev * su_dev,const char * page,size_t count)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 = NULL, *i_port = NULL, *isid = NULL;
1456 	unsigned char *t_fabric = NULL, *t_port = 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 			if (!i_fabric) {
1493 				ret = -ENOMEM;
1494 				goto out;
1495 			}
1496 			break;
1497 		case Opt_initiator_node:
1498 			i_port = match_strdup(&args[0]);
1499 			if (!i_port) {
1500 				ret = -ENOMEM;
1501 				goto out;
1502 			}
1503 			if (strlen(i_port) > PR_APTPL_MAX_IPORT_LEN) {
1504 				printk(KERN_ERR "APTPL metadata initiator_node="
1505 					" exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1506 					PR_APTPL_MAX_IPORT_LEN);
1507 				ret = -EINVAL;
1508 				break;
1509 			}
1510 			break;
1511 		case Opt_initiator_sid:
1512 			isid = match_strdup(&args[0]);
1513 			if (!isid) {
1514 				ret = -ENOMEM;
1515 				goto out;
1516 			}
1517 			if (strlen(isid) > PR_REG_ISID_LEN) {
1518 				printk(KERN_ERR "APTPL metadata initiator_isid"
1519 					"= exceeds PR_REG_ISID_LEN: %d\n",
1520 					PR_REG_ISID_LEN);
1521 				ret = -EINVAL;
1522 				break;
1523 			}
1524 			break;
1525 		case Opt_sa_res_key:
1526 			arg_p = match_strdup(&args[0]);
1527 			if (!arg_p) {
1528 				ret = -ENOMEM;
1529 				goto out;
1530 			}
1531 			ret = strict_strtoull(arg_p, 0, &tmp_ll);
1532 			if (ret < 0) {
1533 				printk(KERN_ERR "strict_strtoull() failed for"
1534 					" sa_res_key=\n");
1535 				goto out;
1536 			}
1537 			sa_res_key = (u64)tmp_ll;
1538 			break;
1539 		/*
1540 		 * PR APTPL Metadata for Reservation
1541 		 */
1542 		case Opt_res_holder:
1543 			match_int(args, &arg);
1544 			res_holder = arg;
1545 			break;
1546 		case Opt_res_type:
1547 			match_int(args, &arg);
1548 			type = (u8)arg;
1549 			break;
1550 		case Opt_res_scope:
1551 			match_int(args, &arg);
1552 			scope = (u8)arg;
1553 			break;
1554 		case Opt_res_all_tg_pt:
1555 			match_int(args, &arg);
1556 			all_tg_pt = (int)arg;
1557 			break;
1558 		case Opt_mapped_lun:
1559 			match_int(args, &arg);
1560 			mapped_lun = (u32)arg;
1561 			break;
1562 		/*
1563 		 * PR APTPL Metadata for Target Port
1564 		 */
1565 		case Opt_target_fabric:
1566 			t_fabric = match_strdup(&args[0]);
1567 			if (!t_fabric) {
1568 				ret = -ENOMEM;
1569 				goto out;
1570 			}
1571 			break;
1572 		case Opt_target_node:
1573 			t_port = match_strdup(&args[0]);
1574 			if (!t_port) {
1575 				ret = -ENOMEM;
1576 				goto out;
1577 			}
1578 			if (strlen(t_port) > PR_APTPL_MAX_TPORT_LEN) {
1579 				printk(KERN_ERR "APTPL metadata target_node="
1580 					" exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1581 					PR_APTPL_MAX_TPORT_LEN);
1582 				ret = -EINVAL;
1583 				break;
1584 			}
1585 			break;
1586 		case Opt_tpgt:
1587 			match_int(args, &arg);
1588 			tpgt = (u16)arg;
1589 			break;
1590 		case Opt_port_rtpi:
1591 			match_int(args, &arg);
1592 			port_rpti = (u16)arg;
1593 			break;
1594 		case Opt_target_lun:
1595 			match_int(args, &arg);
1596 			target_lun = (u32)arg;
1597 			break;
1598 		default:
1599 			break;
1600 		}
1601 	}
1602 
1603 	if (!(i_port) || !(t_port) || !(sa_res_key)) {
1604 		printk(KERN_ERR "Illegal parameters for APTPL registration\n");
1605 		ret = -EINVAL;
1606 		goto out;
1607 	}
1608 
1609 	if (res_holder && !(type)) {
1610 		printk(KERN_ERR "Illegal PR type: 0x%02x for reservation"
1611 				" holder\n", type);
1612 		ret = -EINVAL;
1613 		goto out;
1614 	}
1615 
1616 	ret = core_scsi3_alloc_aptpl_registration(T10_RES(su_dev), sa_res_key,
1617 			i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1618 			res_holder, all_tg_pt, type);
1619 out:
1620 	kfree(i_fabric);
1621 	kfree(i_port);
1622 	kfree(isid);
1623 	kfree(t_fabric);
1624 	kfree(t_port);
1625 	kfree(orig);
1626 	return (ret == 0) ? count : ret;
1627 }
1628 
1629 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1630 
1631 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_subsystem_dev, se_dev_pr_group);
1632 
1633 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1634 	&target_core_dev_pr_res_holder.attr,
1635 	&target_core_dev_pr_res_pr_all_tgt_pts.attr,
1636 	&target_core_dev_pr_res_pr_generation.attr,
1637 	&target_core_dev_pr_res_pr_holder_tg_port.attr,
1638 	&target_core_dev_pr_res_pr_registered_i_pts.attr,
1639 	&target_core_dev_pr_res_pr_type.attr,
1640 	&target_core_dev_pr_res_type.attr,
1641 	&target_core_dev_pr_res_aptpl_active.attr,
1642 	&target_core_dev_pr_res_aptpl_metadata.attr,
1643 	NULL,
1644 };
1645 
1646 static struct configfs_item_operations target_core_dev_pr_ops = {
1647 	.show_attribute		= target_core_dev_pr_attr_show,
1648 	.store_attribute	= target_core_dev_pr_attr_store,
1649 };
1650 
1651 static struct config_item_type target_core_dev_pr_cit = {
1652 	.ct_item_ops		= &target_core_dev_pr_ops,
1653 	.ct_attrs		= target_core_dev_pr_attrs,
1654 	.ct_owner		= THIS_MODULE,
1655 };
1656 
1657 /*  End functions for struct config_item_type target_core_dev_pr_cit */
1658 
1659 /*  Start functions for struct config_item_type target_core_dev_cit */
1660 
target_core_show_dev_info(void * p,char * page)1661 static ssize_t target_core_show_dev_info(void *p, char *page)
1662 {
1663 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1664 	struct se_hba *hba = se_dev->se_dev_hba;
1665 	struct se_subsystem_api *t = hba->transport;
1666 	int bl = 0;
1667 	ssize_t read_bytes = 0;
1668 
1669 	if (!(se_dev->se_dev_ptr))
1670 		return -ENODEV;
1671 
1672 	transport_dump_dev_state(se_dev->se_dev_ptr, page, &bl);
1673 	read_bytes += bl;
1674 	read_bytes += t->show_configfs_dev_params(hba, se_dev, page+read_bytes);
1675 	return read_bytes;
1676 }
1677 
1678 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1679 	.attr	= { .ca_owner = THIS_MODULE,
1680 		    .ca_name = "info",
1681 		    .ca_mode = S_IRUGO },
1682 	.show	= target_core_show_dev_info,
1683 	.store	= NULL,
1684 };
1685 
target_core_store_dev_control(void * p,const char * page,size_t count)1686 static ssize_t target_core_store_dev_control(
1687 	void *p,
1688 	const char *page,
1689 	size_t count)
1690 {
1691 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1692 	struct se_hba *hba = se_dev->se_dev_hba;
1693 	struct se_subsystem_api *t = hba->transport;
1694 
1695 	if (!(se_dev->se_dev_su_ptr)) {
1696 		printk(KERN_ERR "Unable to locate struct se_subsystem_dev>se"
1697 				"_dev_su_ptr\n");
1698 		return -EINVAL;
1699 	}
1700 
1701 	return t->set_configfs_dev_params(hba, se_dev, page, count);
1702 }
1703 
1704 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1705 	.attr	= { .ca_owner = THIS_MODULE,
1706 		    .ca_name = "control",
1707 		    .ca_mode = S_IWUSR },
1708 	.show	= NULL,
1709 	.store	= target_core_store_dev_control,
1710 };
1711 
target_core_show_dev_alias(void * p,char * page)1712 static ssize_t target_core_show_dev_alias(void *p, char *page)
1713 {
1714 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1715 
1716 	if (!(se_dev->su_dev_flags & SDF_USING_ALIAS))
1717 		return 0;
1718 
1719 	return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_alias);
1720 }
1721 
target_core_store_dev_alias(void * p,const char * page,size_t count)1722 static ssize_t target_core_store_dev_alias(
1723 	void *p,
1724 	const char *page,
1725 	size_t count)
1726 {
1727 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1728 	struct se_hba *hba = se_dev->se_dev_hba;
1729 	ssize_t read_bytes;
1730 
1731 	if (count > (SE_DEV_ALIAS_LEN-1)) {
1732 		printk(KERN_ERR "alias count: %d exceeds"
1733 			" SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1734 			SE_DEV_ALIAS_LEN-1);
1735 		return -EINVAL;
1736 	}
1737 
1738 	se_dev->su_dev_flags |= SDF_USING_ALIAS;
1739 	read_bytes = snprintf(&se_dev->se_dev_alias[0], SE_DEV_ALIAS_LEN,
1740 			"%s", page);
1741 
1742 	printk(KERN_INFO "Target_Core_ConfigFS: %s/%s set alias: %s\n",
1743 		config_item_name(&hba->hba_group.cg_item),
1744 		config_item_name(&se_dev->se_dev_group.cg_item),
1745 		se_dev->se_dev_alias);
1746 
1747 	return read_bytes;
1748 }
1749 
1750 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1751 	.attr	= { .ca_owner = THIS_MODULE,
1752 		    .ca_name = "alias",
1753 		    .ca_mode =  S_IRUGO | S_IWUSR },
1754 	.show	= target_core_show_dev_alias,
1755 	.store	= target_core_store_dev_alias,
1756 };
1757 
target_core_show_dev_udev_path(void * p,char * page)1758 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1759 {
1760 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1761 
1762 	if (!(se_dev->su_dev_flags & SDF_USING_UDEV_PATH))
1763 		return 0;
1764 
1765 	return snprintf(page, PAGE_SIZE, "%s\n", se_dev->se_dev_udev_path);
1766 }
1767 
target_core_store_dev_udev_path(void * p,const char * page,size_t count)1768 static ssize_t target_core_store_dev_udev_path(
1769 	void *p,
1770 	const char *page,
1771 	size_t count)
1772 {
1773 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1774 	struct se_hba *hba = se_dev->se_dev_hba;
1775 	ssize_t read_bytes;
1776 
1777 	if (count > (SE_UDEV_PATH_LEN-1)) {
1778 		printk(KERN_ERR "udev_path count: %d exceeds"
1779 			" SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1780 			SE_UDEV_PATH_LEN-1);
1781 		return -EINVAL;
1782 	}
1783 
1784 	se_dev->su_dev_flags |= SDF_USING_UDEV_PATH;
1785 	read_bytes = snprintf(&se_dev->se_dev_udev_path[0], SE_UDEV_PATH_LEN,
1786 			"%s", page);
1787 
1788 	printk(KERN_INFO "Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1789 		config_item_name(&hba->hba_group.cg_item),
1790 		config_item_name(&se_dev->se_dev_group.cg_item),
1791 		se_dev->se_dev_udev_path);
1792 
1793 	return read_bytes;
1794 }
1795 
1796 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1797 	.attr	= { .ca_owner = THIS_MODULE,
1798 		    .ca_name = "udev_path",
1799 		    .ca_mode =  S_IRUGO | S_IWUSR },
1800 	.show	= target_core_show_dev_udev_path,
1801 	.store	= target_core_store_dev_udev_path,
1802 };
1803 
target_core_store_dev_enable(void * p,const char * page,size_t count)1804 static ssize_t target_core_store_dev_enable(
1805 	void *p,
1806 	const char *page,
1807 	size_t count)
1808 {
1809 	struct se_subsystem_dev *se_dev = (struct se_subsystem_dev *)p;
1810 	struct se_device *dev;
1811 	struct se_hba *hba = se_dev->se_dev_hba;
1812 	struct se_subsystem_api *t = hba->transport;
1813 	char *ptr;
1814 
1815 	ptr = strstr(page, "1");
1816 	if (!(ptr)) {
1817 		printk(KERN_ERR "For dev_enable ops, only valid value"
1818 				" is \"1\"\n");
1819 		return -EINVAL;
1820 	}
1821 	if ((se_dev->se_dev_ptr)) {
1822 		printk(KERN_ERR "se_dev->se_dev_ptr already set for storage"
1823 				" object\n");
1824 		return -EEXIST;
1825 	}
1826 
1827 	if (t->check_configfs_dev_params(hba, se_dev) < 0)
1828 		return -EINVAL;
1829 
1830 	dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1831 	if (IS_ERR(dev))
1832 		return PTR_ERR(dev);
1833 	else if (!dev)
1834 		return -EINVAL;
1835 
1836 	se_dev->se_dev_ptr = dev;
1837 	printk(KERN_INFO "Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1838 		" %p\n", se_dev->se_dev_ptr);
1839 
1840 	return count;
1841 }
1842 
1843 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1844 	.attr	= { .ca_owner = THIS_MODULE,
1845 		    .ca_name = "enable",
1846 		    .ca_mode = S_IWUSR },
1847 	.show	= NULL,
1848 	.store	= target_core_store_dev_enable,
1849 };
1850 
target_core_show_alua_lu_gp(void * p,char * page)1851 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1852 {
1853 	struct se_device *dev;
1854 	struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1855 	struct config_item *lu_ci;
1856 	struct t10_alua_lu_gp *lu_gp;
1857 	struct t10_alua_lu_gp_member *lu_gp_mem;
1858 	ssize_t len = 0;
1859 
1860 	dev = su_dev->se_dev_ptr;
1861 	if (!(dev))
1862 		return -ENODEV;
1863 
1864 	if (T10_ALUA(su_dev)->alua_type != SPC3_ALUA_EMULATED)
1865 		return len;
1866 
1867 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1868 	if (!(lu_gp_mem)) {
1869 		printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1870 				" pointer\n");
1871 		return -EINVAL;
1872 	}
1873 
1874 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1875 	lu_gp = lu_gp_mem->lu_gp;
1876 	if ((lu_gp)) {
1877 		lu_ci = &lu_gp->lu_gp_group.cg_item;
1878 		len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1879 			config_item_name(lu_ci), lu_gp->lu_gp_id);
1880 	}
1881 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1882 
1883 	return len;
1884 }
1885 
target_core_store_alua_lu_gp(void * p,const char * page,size_t count)1886 static ssize_t target_core_store_alua_lu_gp(
1887 	void *p,
1888 	const char *page,
1889 	size_t count)
1890 {
1891 	struct se_device *dev;
1892 	struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1893 	struct se_hba *hba = su_dev->se_dev_hba;
1894 	struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1895 	struct t10_alua_lu_gp_member *lu_gp_mem;
1896 	unsigned char buf[LU_GROUP_NAME_BUF];
1897 	int move = 0;
1898 
1899 	dev = su_dev->se_dev_ptr;
1900 	if (!(dev))
1901 		return -ENODEV;
1902 
1903 	if (T10_ALUA(su_dev)->alua_type != SPC3_ALUA_EMULATED) {
1904 		printk(KERN_WARNING "SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1905 			config_item_name(&hba->hba_group.cg_item),
1906 			config_item_name(&su_dev->se_dev_group.cg_item));
1907 		return -EINVAL;
1908 	}
1909 	if (count > LU_GROUP_NAME_BUF) {
1910 		printk(KERN_ERR "ALUA LU Group Alias too large!\n");
1911 		return -EINVAL;
1912 	}
1913 	memset(buf, 0, LU_GROUP_NAME_BUF);
1914 	memcpy(buf, page, count);
1915 	/*
1916 	 * Any ALUA logical unit alias besides "NULL" means we will be
1917 	 * making a new group association.
1918 	 */
1919 	if (strcmp(strstrip(buf), "NULL")) {
1920 		/*
1921 		 * core_alua_get_lu_gp_by_name() will increment reference to
1922 		 * struct t10_alua_lu_gp.  This reference is released with
1923 		 * core_alua_get_lu_gp_by_name below().
1924 		 */
1925 		lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1926 		if (!(lu_gp_new))
1927 			return -ENODEV;
1928 	}
1929 	lu_gp_mem = dev->dev_alua_lu_gp_mem;
1930 	if (!(lu_gp_mem)) {
1931 		if (lu_gp_new)
1932 			core_alua_put_lu_gp_from_name(lu_gp_new);
1933 		printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1934 				" pointer\n");
1935 		return -EINVAL;
1936 	}
1937 
1938 	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1939 	lu_gp = lu_gp_mem->lu_gp;
1940 	if ((lu_gp)) {
1941 		/*
1942 		 * Clearing an existing lu_gp association, and replacing
1943 		 * with NULL
1944 		 */
1945 		if (!(lu_gp_new)) {
1946 			printk(KERN_INFO "Target_Core_ConfigFS: Releasing %s/%s"
1947 				" from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1948 				" %hu\n",
1949 				config_item_name(&hba->hba_group.cg_item),
1950 				config_item_name(&su_dev->se_dev_group.cg_item),
1951 				config_item_name(&lu_gp->lu_gp_group.cg_item),
1952 				lu_gp->lu_gp_id);
1953 
1954 			__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1955 			spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1956 
1957 			return count;
1958 		}
1959 		/*
1960 		 * Removing existing association of lu_gp_mem with lu_gp
1961 		 */
1962 		__core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1963 		move = 1;
1964 	}
1965 	/*
1966 	 * Associate lu_gp_mem with lu_gp_new.
1967 	 */
1968 	__core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1969 	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1970 
1971 	printk(KERN_INFO "Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1972 		" core/alua/lu_gps/%s, ID: %hu\n",
1973 		(move) ? "Moving" : "Adding",
1974 		config_item_name(&hba->hba_group.cg_item),
1975 		config_item_name(&su_dev->se_dev_group.cg_item),
1976 		config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1977 		lu_gp_new->lu_gp_id);
1978 
1979 	core_alua_put_lu_gp_from_name(lu_gp_new);
1980 	return count;
1981 }
1982 
1983 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1984 	.attr	= { .ca_owner = THIS_MODULE,
1985 		    .ca_name = "alua_lu_gp",
1986 		    .ca_mode = S_IRUGO | S_IWUSR },
1987 	.show	= target_core_show_alua_lu_gp,
1988 	.store	= target_core_store_alua_lu_gp,
1989 };
1990 
1991 static struct configfs_attribute *lio_core_dev_attrs[] = {
1992 	&target_core_attr_dev_info.attr,
1993 	&target_core_attr_dev_control.attr,
1994 	&target_core_attr_dev_alias.attr,
1995 	&target_core_attr_dev_udev_path.attr,
1996 	&target_core_attr_dev_enable.attr,
1997 	&target_core_attr_dev_alua_lu_gp.attr,
1998 	NULL,
1999 };
2000 
target_core_dev_release(struct config_item * item)2001 static void target_core_dev_release(struct config_item *item)
2002 {
2003 	struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2004 				struct se_subsystem_dev, se_dev_group);
2005 	struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2006 	struct se_subsystem_api *t = hba->transport;
2007 	struct config_group *dev_cg = &se_dev->se_dev_group;
2008 
2009 	kfree(dev_cg->default_groups);
2010 	/*
2011 	 * This pointer will set when the storage is enabled with:
2012 	 *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
2013 	 */
2014 	if (se_dev->se_dev_ptr) {
2015 		printk(KERN_INFO "Target_Core_ConfigFS: Calling se_free_"
2016 			"virtual_device() for se_dev_ptr: %p\n",
2017 			se_dev->se_dev_ptr);
2018 
2019 		se_free_virtual_device(se_dev->se_dev_ptr, hba);
2020 	} else {
2021 		/*
2022 		 * Release struct se_subsystem_dev->se_dev_su_ptr..
2023 		 */
2024 		printk(KERN_INFO "Target_Core_ConfigFS: Calling t->free_"
2025 			"device() for se_dev_su_ptr: %p\n",
2026 			se_dev->se_dev_su_ptr);
2027 
2028 		t->free_device(se_dev->se_dev_su_ptr);
2029 	}
2030 
2031 	printk(KERN_INFO "Target_Core_ConfigFS: Deallocating se_subsystem"
2032 			"_dev_t: %p\n", se_dev);
2033 	kfree(se_dev);
2034 }
2035 
target_core_dev_show(struct config_item * item,struct configfs_attribute * attr,char * page)2036 static ssize_t target_core_dev_show(struct config_item *item,
2037 				     struct configfs_attribute *attr,
2038 				     char *page)
2039 {
2040 	struct se_subsystem_dev *se_dev = container_of(
2041 			to_config_group(item), struct se_subsystem_dev,
2042 			se_dev_group);
2043 	struct target_core_configfs_attribute *tc_attr = container_of(
2044 			attr, struct target_core_configfs_attribute, attr);
2045 
2046 	if (!(tc_attr->show))
2047 		return -EINVAL;
2048 
2049 	return tc_attr->show((void *)se_dev, page);
2050 }
2051 
target_core_dev_store(struct config_item * item,struct configfs_attribute * attr,const char * page,size_t count)2052 static ssize_t target_core_dev_store(struct config_item *item,
2053 				      struct configfs_attribute *attr,
2054 				      const char *page, size_t count)
2055 {
2056 	struct se_subsystem_dev *se_dev = container_of(
2057 			to_config_group(item), struct se_subsystem_dev,
2058 			se_dev_group);
2059 	struct target_core_configfs_attribute *tc_attr = container_of(
2060 			attr, struct target_core_configfs_attribute, attr);
2061 
2062 	if (!(tc_attr->store))
2063 		return -EINVAL;
2064 
2065 	return tc_attr->store((void *)se_dev, page, count);
2066 }
2067 
2068 static struct configfs_item_operations target_core_dev_item_ops = {
2069 	.release		= target_core_dev_release,
2070 	.show_attribute		= target_core_dev_show,
2071 	.store_attribute	= target_core_dev_store,
2072 };
2073 
2074 static struct config_item_type target_core_dev_cit = {
2075 	.ct_item_ops		= &target_core_dev_item_ops,
2076 	.ct_attrs		= lio_core_dev_attrs,
2077 	.ct_owner		= THIS_MODULE,
2078 };
2079 
2080 /* End functions for struct config_item_type target_core_dev_cit */
2081 
2082 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2083 
2084 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2085 #define SE_DEV_ALUA_LU_ATTR(_name, _mode)				\
2086 static struct target_core_alua_lu_gp_attribute				\
2087 			target_core_alua_lu_gp_##_name =		\
2088 	__CONFIGFS_EATTR(_name, _mode,					\
2089 	target_core_alua_lu_gp_show_attr_##_name,			\
2090 	target_core_alua_lu_gp_store_attr_##_name);
2091 
2092 #define SE_DEV_ALUA_LU_ATTR_RO(_name)					\
2093 static struct target_core_alua_lu_gp_attribute				\
2094 			target_core_alua_lu_gp_##_name =		\
2095 	__CONFIGFS_EATTR_RO(_name,					\
2096 	target_core_alua_lu_gp_show_attr_##_name);
2097 
2098 /*
2099  * lu_gp_id
2100  */
target_core_alua_lu_gp_show_attr_lu_gp_id(struct t10_alua_lu_gp * lu_gp,char * page)2101 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2102 	struct t10_alua_lu_gp *lu_gp,
2103 	char *page)
2104 {
2105 	if (!(lu_gp->lu_gp_valid_id))
2106 		return 0;
2107 
2108 	return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2109 }
2110 
target_core_alua_lu_gp_store_attr_lu_gp_id(struct t10_alua_lu_gp * lu_gp,const char * page,size_t count)2111 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2112 	struct t10_alua_lu_gp *lu_gp,
2113 	const char *page,
2114 	size_t count)
2115 {
2116 	struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2117 	unsigned long lu_gp_id;
2118 	int ret;
2119 
2120 	ret = strict_strtoul(page, 0, &lu_gp_id);
2121 	if (ret < 0) {
2122 		printk(KERN_ERR "strict_strtoul() returned %d for"
2123 			" lu_gp_id\n", ret);
2124 		return -EINVAL;
2125 	}
2126 	if (lu_gp_id > 0x0000ffff) {
2127 		printk(KERN_ERR "ALUA lu_gp_id: %lu exceeds maximum:"
2128 			" 0x0000ffff\n", lu_gp_id);
2129 		return -EINVAL;
2130 	}
2131 
2132 	ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2133 	if (ret < 0)
2134 		return -EINVAL;
2135 
2136 	printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Logical Unit"
2137 		" Group: core/alua/lu_gps/%s to ID: %hu\n",
2138 		config_item_name(&alua_lu_gp_cg->cg_item),
2139 		lu_gp->lu_gp_id);
2140 
2141 	return count;
2142 }
2143 
2144 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2145 
2146 /*
2147  * members
2148  */
target_core_alua_lu_gp_show_attr_members(struct t10_alua_lu_gp * lu_gp,char * page)2149 static ssize_t target_core_alua_lu_gp_show_attr_members(
2150 	struct t10_alua_lu_gp *lu_gp,
2151 	char *page)
2152 {
2153 	struct se_device *dev;
2154 	struct se_hba *hba;
2155 	struct se_subsystem_dev *su_dev;
2156 	struct t10_alua_lu_gp_member *lu_gp_mem;
2157 	ssize_t len = 0, cur_len;
2158 	unsigned char buf[LU_GROUP_NAME_BUF];
2159 
2160 	memset(buf, 0, LU_GROUP_NAME_BUF);
2161 
2162 	spin_lock(&lu_gp->lu_gp_lock);
2163 	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2164 		dev = lu_gp_mem->lu_gp_mem_dev;
2165 		su_dev = dev->se_sub_dev;
2166 		hba = su_dev->se_dev_hba;
2167 
2168 		cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2169 			config_item_name(&hba->hba_group.cg_item),
2170 			config_item_name(&su_dev->se_dev_group.cg_item));
2171 		cur_len++; /* Extra byte for NULL terminator */
2172 
2173 		if ((cur_len + len) > PAGE_SIZE) {
2174 			printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2175 				"_members buffer\n");
2176 			break;
2177 		}
2178 		memcpy(page+len, buf, cur_len);
2179 		len += cur_len;
2180 	}
2181 	spin_unlock(&lu_gp->lu_gp_lock);
2182 
2183 	return len;
2184 }
2185 
2186 SE_DEV_ALUA_LU_ATTR_RO(members);
2187 
2188 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2189 
2190 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2191 	&target_core_alua_lu_gp_lu_gp_id.attr,
2192 	&target_core_alua_lu_gp_members.attr,
2193 	NULL,
2194 };
2195 
target_core_alua_lu_gp_release(struct config_item * item)2196 static void target_core_alua_lu_gp_release(struct config_item *item)
2197 {
2198 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2199 			struct t10_alua_lu_gp, lu_gp_group);
2200 
2201 	core_alua_free_lu_gp(lu_gp);
2202 }
2203 
2204 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2205 	.release		= target_core_alua_lu_gp_release,
2206 	.show_attribute		= target_core_alua_lu_gp_attr_show,
2207 	.store_attribute	= target_core_alua_lu_gp_attr_store,
2208 };
2209 
2210 static struct config_item_type target_core_alua_lu_gp_cit = {
2211 	.ct_item_ops		= &target_core_alua_lu_gp_ops,
2212 	.ct_attrs		= target_core_alua_lu_gp_attrs,
2213 	.ct_owner		= THIS_MODULE,
2214 };
2215 
2216 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2217 
2218 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2219 
target_core_alua_create_lu_gp(struct config_group * group,const char * name)2220 static struct config_group *target_core_alua_create_lu_gp(
2221 	struct config_group *group,
2222 	const char *name)
2223 {
2224 	struct t10_alua_lu_gp *lu_gp;
2225 	struct config_group *alua_lu_gp_cg = NULL;
2226 	struct config_item *alua_lu_gp_ci = NULL;
2227 
2228 	lu_gp = core_alua_allocate_lu_gp(name, 0);
2229 	if (IS_ERR(lu_gp))
2230 		return NULL;
2231 
2232 	alua_lu_gp_cg = &lu_gp->lu_gp_group;
2233 	alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2234 
2235 	config_group_init_type_name(alua_lu_gp_cg, name,
2236 			&target_core_alua_lu_gp_cit);
2237 
2238 	printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2239 		" Group: core/alua/lu_gps/%s\n",
2240 		config_item_name(alua_lu_gp_ci));
2241 
2242 	return alua_lu_gp_cg;
2243 
2244 }
2245 
target_core_alua_drop_lu_gp(struct config_group * group,struct config_item * item)2246 static void target_core_alua_drop_lu_gp(
2247 	struct config_group *group,
2248 	struct config_item *item)
2249 {
2250 	struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2251 			struct t10_alua_lu_gp, lu_gp_group);
2252 
2253 	printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2254 		" Group: core/alua/lu_gps/%s, ID: %hu\n",
2255 		config_item_name(item), lu_gp->lu_gp_id);
2256 	/*
2257 	 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2258 	 * -> target_core_alua_lu_gp_release()
2259 	 */
2260 	config_item_put(item);
2261 }
2262 
2263 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2264 	.make_group		= &target_core_alua_create_lu_gp,
2265 	.drop_item		= &target_core_alua_drop_lu_gp,
2266 };
2267 
2268 static struct config_item_type target_core_alua_lu_gps_cit = {
2269 	.ct_item_ops		= NULL,
2270 	.ct_group_ops		= &target_core_alua_lu_gps_group_ops,
2271 	.ct_owner		= THIS_MODULE,
2272 };
2273 
2274 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2275 
2276 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2277 
2278 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2279 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)				\
2280 static struct target_core_alua_tg_pt_gp_attribute			\
2281 			target_core_alua_tg_pt_gp_##_name =		\
2282 	__CONFIGFS_EATTR(_name, _mode,					\
2283 	target_core_alua_tg_pt_gp_show_attr_##_name,			\
2284 	target_core_alua_tg_pt_gp_store_attr_##_name);
2285 
2286 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)				\
2287 static struct target_core_alua_tg_pt_gp_attribute			\
2288 			target_core_alua_tg_pt_gp_##_name =		\
2289 	__CONFIGFS_EATTR_RO(_name,					\
2290 	target_core_alua_tg_pt_gp_show_attr_##_name);
2291 
2292 /*
2293  * alua_access_state
2294  */
target_core_alua_tg_pt_gp_show_attr_alua_access_state(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2295 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2296 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2297 	char *page)
2298 {
2299 	return sprintf(page, "%d\n",
2300 		atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2301 }
2302 
target_core_alua_tg_pt_gp_store_attr_alua_access_state(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2303 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2304 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2305 	const char *page,
2306 	size_t count)
2307 {
2308 	struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2309 	unsigned long tmp;
2310 	int new_state, ret;
2311 
2312 	if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2313 		printk(KERN_ERR "Unable to do implict ALUA on non valid"
2314 			" tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2315 		return -EINVAL;
2316 	}
2317 
2318 	ret = strict_strtoul(page, 0, &tmp);
2319 	if (ret < 0) {
2320 		printk("Unable to extract new ALUA access state from"
2321 				" %s\n", page);
2322 		return -EINVAL;
2323 	}
2324 	new_state = (int)tmp;
2325 
2326 	if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2327 		printk(KERN_ERR "Unable to process implict configfs ALUA"
2328 			" transition while TPGS_IMPLICT_ALUA is diabled\n");
2329 		return -EINVAL;
2330 	}
2331 
2332 	ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2333 					NULL, NULL, new_state, 0);
2334 	return (!ret) ? count : -EINVAL;
2335 }
2336 
2337 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2338 
2339 /*
2340  * alua_access_status
2341  */
target_core_alua_tg_pt_gp_show_attr_alua_access_status(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2342 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2343 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2344 	char *page)
2345 {
2346 	return sprintf(page, "%s\n",
2347 		core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2348 }
2349 
target_core_alua_tg_pt_gp_store_attr_alua_access_status(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2350 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2351 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2352 	const char *page,
2353 	size_t count)
2354 {
2355 	unsigned long tmp;
2356 	int new_status, ret;
2357 
2358 	if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2359 		printk(KERN_ERR "Unable to do set ALUA access status on non"
2360 			" valid tg_pt_gp ID: %hu\n",
2361 			tg_pt_gp->tg_pt_gp_valid_id);
2362 		return -EINVAL;
2363 	}
2364 
2365 	ret = strict_strtoul(page, 0, &tmp);
2366 	if (ret < 0) {
2367 		printk(KERN_ERR "Unable to extract new ALUA access status"
2368 				" from %s\n", page);
2369 		return -EINVAL;
2370 	}
2371 	new_status = (int)tmp;
2372 
2373 	if ((new_status != ALUA_STATUS_NONE) &&
2374 	    (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2375 	    (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2376 		printk(KERN_ERR "Illegal ALUA access status: 0x%02x\n",
2377 				new_status);
2378 		return -EINVAL;
2379 	}
2380 
2381 	tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2382 	return count;
2383 }
2384 
2385 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2386 
2387 /*
2388  * alua_access_type
2389  */
target_core_alua_tg_pt_gp_show_attr_alua_access_type(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2390 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2391 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2392 	char *page)
2393 {
2394 	return core_alua_show_access_type(tg_pt_gp, page);
2395 }
2396 
target_core_alua_tg_pt_gp_store_attr_alua_access_type(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2397 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2398 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2399 	const char *page,
2400 	size_t count)
2401 {
2402 	return core_alua_store_access_type(tg_pt_gp, page, count);
2403 }
2404 
2405 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2406 
2407 /*
2408  * alua_write_metadata
2409  */
target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2410 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2411 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2412 	char *page)
2413 {
2414 	return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2415 }
2416 
target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2417 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2418 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2419 	const char *page,
2420 	size_t count)
2421 {
2422 	unsigned long tmp;
2423 	int ret;
2424 
2425 	ret = strict_strtoul(page, 0, &tmp);
2426 	if (ret < 0) {
2427 		printk(KERN_ERR "Unable to extract alua_write_metadata\n");
2428 		return -EINVAL;
2429 	}
2430 
2431 	if ((tmp != 0) && (tmp != 1)) {
2432 		printk(KERN_ERR "Illegal value for alua_write_metadata:"
2433 			" %lu\n", tmp);
2434 		return -EINVAL;
2435 	}
2436 	tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2437 
2438 	return count;
2439 }
2440 
2441 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2442 
2443 
2444 
2445 /*
2446  * nonop_delay_msecs
2447  */
target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2448 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2449 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2450 	char *page)
2451 {
2452 	return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2453 
2454 }
2455 
target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2456 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2457 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2458 	const char *page,
2459 	size_t count)
2460 {
2461 	return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2462 }
2463 
2464 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2465 
2466 /*
2467  * trans_delay_msecs
2468  */
target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2469 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2470 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2471 	char *page)
2472 {
2473 	return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2474 }
2475 
target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2476 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2477 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2478 	const char *page,
2479 	size_t count)
2480 {
2481 	return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2482 }
2483 
2484 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2485 
2486 /*
2487  * preferred
2488  */
2489 
target_core_alua_tg_pt_gp_show_attr_preferred(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2490 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2491 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2492 	char *page)
2493 {
2494 	return core_alua_show_preferred_bit(tg_pt_gp, page);
2495 }
2496 
target_core_alua_tg_pt_gp_store_attr_preferred(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2497 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2498 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2499 	const char *page,
2500 	size_t count)
2501 {
2502 	return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2503 }
2504 
2505 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2506 
2507 /*
2508  * tg_pt_gp_id
2509  */
target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2510 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2511 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2512 	char *page)
2513 {
2514 	if (!(tg_pt_gp->tg_pt_gp_valid_id))
2515 		return 0;
2516 
2517 	return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2518 }
2519 
target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(struct t10_alua_tg_pt_gp * tg_pt_gp,const char * page,size_t count)2520 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2521 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2522 	const char *page,
2523 	size_t count)
2524 {
2525 	struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2526 	unsigned long tg_pt_gp_id;
2527 	int ret;
2528 
2529 	ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2530 	if (ret < 0) {
2531 		printk(KERN_ERR "strict_strtoul() returned %d for"
2532 			" tg_pt_gp_id\n", ret);
2533 		return -EINVAL;
2534 	}
2535 	if (tg_pt_gp_id > 0x0000ffff) {
2536 		printk(KERN_ERR "ALUA tg_pt_gp_id: %lu exceeds maximum:"
2537 			" 0x0000ffff\n", tg_pt_gp_id);
2538 		return -EINVAL;
2539 	}
2540 
2541 	ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2542 	if (ret < 0)
2543 		return -EINVAL;
2544 
2545 	printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Target Port Group: "
2546 		"core/alua/tg_pt_gps/%s to ID: %hu\n",
2547 		config_item_name(&alua_tg_pt_gp_cg->cg_item),
2548 		tg_pt_gp->tg_pt_gp_id);
2549 
2550 	return count;
2551 }
2552 
2553 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2554 
2555 /*
2556  * members
2557  */
target_core_alua_tg_pt_gp_show_attr_members(struct t10_alua_tg_pt_gp * tg_pt_gp,char * page)2558 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2559 	struct t10_alua_tg_pt_gp *tg_pt_gp,
2560 	char *page)
2561 {
2562 	struct se_port *port;
2563 	struct se_portal_group *tpg;
2564 	struct se_lun *lun;
2565 	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2566 	ssize_t len = 0, cur_len;
2567 	unsigned char buf[TG_PT_GROUP_NAME_BUF];
2568 
2569 	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2570 
2571 	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2572 	list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2573 			tg_pt_gp_mem_list) {
2574 		port = tg_pt_gp_mem->tg_pt;
2575 		tpg = port->sep_tpg;
2576 		lun = port->sep_lun;
2577 
2578 		cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2579 			"/%s\n", TPG_TFO(tpg)->get_fabric_name(),
2580 			TPG_TFO(tpg)->tpg_get_wwn(tpg),
2581 			TPG_TFO(tpg)->tpg_get_tag(tpg),
2582 			config_item_name(&lun->lun_group.cg_item));
2583 		cur_len++; /* Extra byte for NULL terminator */
2584 
2585 		if ((cur_len + len) > PAGE_SIZE) {
2586 			printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2587 				"_members buffer\n");
2588 			break;
2589 		}
2590 		memcpy(page+len, buf, cur_len);
2591 		len += cur_len;
2592 	}
2593 	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2594 
2595 	return len;
2596 }
2597 
2598 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2599 
2600 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2601 			tg_pt_gp_group);
2602 
2603 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2604 	&target_core_alua_tg_pt_gp_alua_access_state.attr,
2605 	&target_core_alua_tg_pt_gp_alua_access_status.attr,
2606 	&target_core_alua_tg_pt_gp_alua_access_type.attr,
2607 	&target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2608 	&target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2609 	&target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2610 	&target_core_alua_tg_pt_gp_preferred.attr,
2611 	&target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2612 	&target_core_alua_tg_pt_gp_members.attr,
2613 	NULL,
2614 };
2615 
target_core_alua_tg_pt_gp_release(struct config_item * item)2616 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2617 {
2618 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2619 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2620 
2621 	core_alua_free_tg_pt_gp(tg_pt_gp);
2622 }
2623 
2624 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2625 	.release		= target_core_alua_tg_pt_gp_release,
2626 	.show_attribute		= target_core_alua_tg_pt_gp_attr_show,
2627 	.store_attribute	= target_core_alua_tg_pt_gp_attr_store,
2628 };
2629 
2630 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2631 	.ct_item_ops		= &target_core_alua_tg_pt_gp_ops,
2632 	.ct_attrs		= target_core_alua_tg_pt_gp_attrs,
2633 	.ct_owner		= THIS_MODULE,
2634 };
2635 
2636 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2637 
2638 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2639 
target_core_alua_create_tg_pt_gp(struct config_group * group,const char * name)2640 static struct config_group *target_core_alua_create_tg_pt_gp(
2641 	struct config_group *group,
2642 	const char *name)
2643 {
2644 	struct t10_alua *alua = container_of(group, struct t10_alua,
2645 					alua_tg_pt_gps_group);
2646 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2647 	struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2648 	struct config_group *alua_tg_pt_gp_cg = NULL;
2649 	struct config_item *alua_tg_pt_gp_ci = NULL;
2650 
2651 	tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2652 	if (!(tg_pt_gp))
2653 		return NULL;
2654 
2655 	alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2656 	alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2657 
2658 	config_group_init_type_name(alua_tg_pt_gp_cg, name,
2659 			&target_core_alua_tg_pt_gp_cit);
2660 
2661 	printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Target Port"
2662 		" Group: alua/tg_pt_gps/%s\n",
2663 		config_item_name(alua_tg_pt_gp_ci));
2664 
2665 	return alua_tg_pt_gp_cg;
2666 }
2667 
target_core_alua_drop_tg_pt_gp(struct config_group * group,struct config_item * item)2668 static void target_core_alua_drop_tg_pt_gp(
2669 	struct config_group *group,
2670 	struct config_item *item)
2671 {
2672 	struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2673 			struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2674 
2675 	printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Target Port"
2676 		" Group: alua/tg_pt_gps/%s, ID: %hu\n",
2677 		config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2678 	/*
2679 	 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2680 	 * -> target_core_alua_tg_pt_gp_release().
2681 	 */
2682 	config_item_put(item);
2683 }
2684 
2685 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2686 	.make_group		= &target_core_alua_create_tg_pt_gp,
2687 	.drop_item		= &target_core_alua_drop_tg_pt_gp,
2688 };
2689 
2690 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2691 	.ct_group_ops		= &target_core_alua_tg_pt_gps_group_ops,
2692 	.ct_owner		= THIS_MODULE,
2693 };
2694 
2695 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2696 
2697 /* Start functions for struct config_item_type target_core_alua_cit */
2698 
2699 /*
2700  * target_core_alua_cit is a ConfigFS group that lives under
2701  * /sys/kernel/config/target/core/alua.  There are default groups
2702  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2703  * target_core_alua_cit in target_core_init_configfs() below.
2704  */
2705 static struct config_item_type target_core_alua_cit = {
2706 	.ct_item_ops		= NULL,
2707 	.ct_attrs		= NULL,
2708 	.ct_owner		= THIS_MODULE,
2709 };
2710 
2711 /* End functions for struct config_item_type target_core_alua_cit */
2712 
2713 /* Start functions for struct config_item_type target_core_stat_cit */
2714 
target_core_stat_mkdir(struct config_group * group,const char * name)2715 static struct config_group *target_core_stat_mkdir(
2716 	struct config_group *group,
2717 	const char *name)
2718 {
2719 	return ERR_PTR(-ENOSYS);
2720 }
2721 
target_core_stat_rmdir(struct config_group * group,struct config_item * item)2722 static void target_core_stat_rmdir(
2723 	struct config_group *group,
2724 	struct config_item *item)
2725 {
2726 	return;
2727 }
2728 
2729 static struct configfs_group_operations target_core_stat_group_ops = {
2730 	.make_group		= &target_core_stat_mkdir,
2731 	.drop_item		= &target_core_stat_rmdir,
2732 };
2733 
2734 static struct config_item_type target_core_stat_cit = {
2735 	.ct_group_ops		= &target_core_stat_group_ops,
2736 	.ct_owner		= THIS_MODULE,
2737 };
2738 
2739 /* End functions for struct config_item_type target_core_stat_cit */
2740 
2741 /* Start functions for struct config_item_type target_core_hba_cit */
2742 
target_core_make_subdev(struct config_group * group,const char * name)2743 static struct config_group *target_core_make_subdev(
2744 	struct config_group *group,
2745 	const char *name)
2746 {
2747 	struct t10_alua_tg_pt_gp *tg_pt_gp;
2748 	struct se_subsystem_dev *se_dev;
2749 	struct se_subsystem_api *t;
2750 	struct config_item *hba_ci = &group->cg_item;
2751 	struct se_hba *hba = item_to_hba(hba_ci);
2752 	struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2753 	struct config_group *dev_stat_grp = NULL;
2754 	int errno = -ENOMEM, ret;
2755 
2756 	ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2757 	if (ret)
2758 		return ERR_PTR(ret);
2759 	/*
2760 	 * Locate the struct se_subsystem_api from parent's struct se_hba.
2761 	 */
2762 	t = hba->transport;
2763 
2764 	se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2765 	if (!se_dev) {
2766 		printk(KERN_ERR "Unable to allocate memory for"
2767 				" struct se_subsystem_dev\n");
2768 		goto unlock;
2769 	}
2770 	INIT_LIST_HEAD(&se_dev->g_se_dev_list);
2771 	INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2772 	spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2773 	INIT_LIST_HEAD(&se_dev->t10_reservation.registration_list);
2774 	INIT_LIST_HEAD(&se_dev->t10_reservation.aptpl_reg_list);
2775 	spin_lock_init(&se_dev->t10_reservation.registration_lock);
2776 	spin_lock_init(&se_dev->t10_reservation.aptpl_reg_lock);
2777 	INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2778 	spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2779 	spin_lock_init(&se_dev->se_dev_lock);
2780 	se_dev->t10_reservation.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2781 	se_dev->t10_wwn.t10_sub_dev = se_dev;
2782 	se_dev->t10_alua.t10_sub_dev = se_dev;
2783 	se_dev->se_dev_attrib.da_sub_dev = se_dev;
2784 
2785 	se_dev->se_dev_hba = hba;
2786 	dev_cg = &se_dev->se_dev_group;
2787 
2788 	dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 7,
2789 			GFP_KERNEL);
2790 	if (!(dev_cg->default_groups))
2791 		goto out;
2792 	/*
2793 	 * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2794 	 * for ->allocate_virtdevice()
2795 	 *
2796 	 * se_dev->se_dev_ptr will be set after ->create_virtdev()
2797 	 * has been called successfully in the next level up in the
2798 	 * configfs tree for device object's struct config_group.
2799 	 */
2800 	se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2801 	if (!(se_dev->se_dev_su_ptr)) {
2802 		printk(KERN_ERR "Unable to locate subsystem dependent pointer"
2803 			" from allocate_virtdevice()\n");
2804 		goto out;
2805 	}
2806 	spin_lock(&se_global->g_device_lock);
2807 	list_add_tail(&se_dev->g_se_dev_list, &se_global->g_se_dev_list);
2808 	spin_unlock(&se_global->g_device_lock);
2809 
2810 	config_group_init_type_name(&se_dev->se_dev_group, name,
2811 			&target_core_dev_cit);
2812 	config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2813 			&target_core_dev_attrib_cit);
2814 	config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2815 			&target_core_dev_pr_cit);
2816 	config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2817 			&target_core_dev_wwn_cit);
2818 	config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2819 			"alua", &target_core_alua_tg_pt_gps_cit);
2820 	config_group_init_type_name(&se_dev->dev_stat_grps.stat_group,
2821 			"statistics", &target_core_stat_cit);
2822 
2823 	dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2824 	dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2825 	dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2826 	dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2827 	dev_cg->default_groups[4] = &se_dev->dev_stat_grps.stat_group;
2828 	dev_cg->default_groups[5] = NULL;
2829 	/*
2830 	 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2831 	 */
2832 	tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2833 	if (!(tg_pt_gp))
2834 		goto out;
2835 
2836 	tg_pt_gp_cg = &T10_ALUA(se_dev)->alua_tg_pt_gps_group;
2837 	tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2838 				GFP_KERNEL);
2839 	if (!(tg_pt_gp_cg->default_groups)) {
2840 		printk(KERN_ERR "Unable to allocate tg_pt_gp_cg->"
2841 				"default_groups\n");
2842 		goto out;
2843 	}
2844 
2845 	config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2846 			"default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2847 	tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2848 	tg_pt_gp_cg->default_groups[1] = NULL;
2849 	T10_ALUA(se_dev)->default_tg_pt_gp = tg_pt_gp;
2850 	/*
2851 	 * Add core/$HBA/$DEV/statistics/ default groups
2852 	 */
2853 	dev_stat_grp = &DEV_STAT_GRP(se_dev)->stat_group;
2854 	dev_stat_grp->default_groups = kzalloc(sizeof(struct config_group) * 4,
2855 				GFP_KERNEL);
2856 	if (!dev_stat_grp->default_groups) {
2857 		printk(KERN_ERR "Unable to allocate dev_stat_grp->default_groups\n");
2858 		goto out;
2859 	}
2860 	target_stat_setup_dev_default_groups(se_dev);
2861 
2862 	printk(KERN_INFO "Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2863 		" %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2864 
2865 	mutex_unlock(&hba->hba_access_mutex);
2866 	return &se_dev->se_dev_group;
2867 out:
2868 	if (T10_ALUA(se_dev)->default_tg_pt_gp) {
2869 		core_alua_free_tg_pt_gp(T10_ALUA(se_dev)->default_tg_pt_gp);
2870 		T10_ALUA(se_dev)->default_tg_pt_gp = NULL;
2871 	}
2872 	if (dev_stat_grp)
2873 		kfree(dev_stat_grp->default_groups);
2874 	if (tg_pt_gp_cg)
2875 		kfree(tg_pt_gp_cg->default_groups);
2876 	if (dev_cg)
2877 		kfree(dev_cg->default_groups);
2878 	if (se_dev->se_dev_su_ptr)
2879 		t->free_device(se_dev->se_dev_su_ptr);
2880 	kfree(se_dev);
2881 unlock:
2882 	mutex_unlock(&hba->hba_access_mutex);
2883 	return ERR_PTR(errno);
2884 }
2885 
target_core_drop_subdev(struct config_group * group,struct config_item * item)2886 static void target_core_drop_subdev(
2887 	struct config_group *group,
2888 	struct config_item *item)
2889 {
2890 	struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2891 				struct se_subsystem_dev, se_dev_group);
2892 	struct se_hba *hba;
2893 	struct se_subsystem_api *t;
2894 	struct config_item *df_item;
2895 	struct config_group *dev_cg, *tg_pt_gp_cg, *dev_stat_grp;
2896 	int i;
2897 
2898 	hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2899 
2900 	mutex_lock(&hba->hba_access_mutex);
2901 	t = hba->transport;
2902 
2903 	spin_lock(&se_global->g_device_lock);
2904 	list_del(&se_dev->g_se_dev_list);
2905 	spin_unlock(&se_global->g_device_lock);
2906 
2907 	dev_stat_grp = &DEV_STAT_GRP(se_dev)->stat_group;
2908 	for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2909 		df_item = &dev_stat_grp->default_groups[i]->cg_item;
2910 		dev_stat_grp->default_groups[i] = NULL;
2911 		config_item_put(df_item);
2912 	}
2913 	kfree(dev_stat_grp->default_groups);
2914 
2915 	tg_pt_gp_cg = &T10_ALUA(se_dev)->alua_tg_pt_gps_group;
2916 	for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2917 		df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2918 		tg_pt_gp_cg->default_groups[i] = NULL;
2919 		config_item_put(df_item);
2920 	}
2921 	kfree(tg_pt_gp_cg->default_groups);
2922 	/*
2923 	 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2924 	 * directly from target_core_alua_tg_pt_gp_release().
2925 	 */
2926 	T10_ALUA(se_dev)->default_tg_pt_gp = NULL;
2927 
2928 	dev_cg = &se_dev->se_dev_group;
2929 	for (i = 0; dev_cg->default_groups[i]; i++) {
2930 		df_item = &dev_cg->default_groups[i]->cg_item;
2931 		dev_cg->default_groups[i] = NULL;
2932 		config_item_put(df_item);
2933 	}
2934 	/*
2935 	 * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2936 	 * from target_core_dev_item_ops->release() ->target_core_dev_release().
2937 	 */
2938 	config_item_put(item);
2939 	mutex_unlock(&hba->hba_access_mutex);
2940 }
2941 
2942 static struct configfs_group_operations target_core_hba_group_ops = {
2943 	.make_group		= target_core_make_subdev,
2944 	.drop_item		= target_core_drop_subdev,
2945 };
2946 
2947 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2948 #define SE_HBA_ATTR(_name, _mode)				\
2949 static struct target_core_hba_attribute				\
2950 		target_core_hba_##_name =			\
2951 		__CONFIGFS_EATTR(_name, _mode,			\
2952 		target_core_hba_show_attr_##_name,		\
2953 		target_core_hba_store_attr_##_name);
2954 
2955 #define SE_HBA_ATTR_RO(_name)					\
2956 static struct target_core_hba_attribute				\
2957 		target_core_hba_##_name =			\
2958 		__CONFIGFS_EATTR_RO(_name,			\
2959 		target_core_hba_show_attr_##_name);
2960 
target_core_hba_show_attr_hba_info(struct se_hba * hba,char * page)2961 static ssize_t target_core_hba_show_attr_hba_info(
2962 	struct se_hba *hba,
2963 	char *page)
2964 {
2965 	return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2966 			hba->hba_id, hba->transport->name,
2967 			TARGET_CORE_CONFIGFS_VERSION);
2968 }
2969 
2970 SE_HBA_ATTR_RO(hba_info);
2971 
target_core_hba_show_attr_hba_mode(struct se_hba * hba,char * page)2972 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2973 				char *page)
2974 {
2975 	int hba_mode = 0;
2976 
2977 	if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2978 		hba_mode = 1;
2979 
2980 	return sprintf(page, "%d\n", hba_mode);
2981 }
2982 
target_core_hba_store_attr_hba_mode(struct se_hba * hba,const char * page,size_t count)2983 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2984 				const char *page, size_t count)
2985 {
2986 	struct se_subsystem_api *transport = hba->transport;
2987 	unsigned long mode_flag;
2988 	int ret;
2989 
2990 	if (transport->pmode_enable_hba == NULL)
2991 		return -EINVAL;
2992 
2993 	ret = strict_strtoul(page, 0, &mode_flag);
2994 	if (ret < 0) {
2995 		printk(KERN_ERR "Unable to extract hba mode flag: %d\n", ret);
2996 		return -EINVAL;
2997 	}
2998 
2999 	spin_lock(&hba->device_lock);
3000 	if (!(list_empty(&hba->hba_dev_list))) {
3001 		printk(KERN_ERR "Unable to set hba_mode with active devices\n");
3002 		spin_unlock(&hba->device_lock);
3003 		return -EINVAL;
3004 	}
3005 	spin_unlock(&hba->device_lock);
3006 
3007 	ret = transport->pmode_enable_hba(hba, mode_flag);
3008 	if (ret < 0)
3009 		return -EINVAL;
3010 	if (ret > 0)
3011 		hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3012 	else if (ret == 0)
3013 		hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3014 
3015 	return count;
3016 }
3017 
3018 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
3019 
3020 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
3021 
target_core_hba_release(struct config_item * item)3022 static void target_core_hba_release(struct config_item *item)
3023 {
3024 	struct se_hba *hba = container_of(to_config_group(item),
3025 				struct se_hba, hba_group);
3026 	core_delete_hba(hba);
3027 }
3028 
3029 static struct configfs_attribute *target_core_hba_attrs[] = {
3030 	&target_core_hba_hba_info.attr,
3031 	&target_core_hba_hba_mode.attr,
3032 	NULL,
3033 };
3034 
3035 static struct configfs_item_operations target_core_hba_item_ops = {
3036 	.release		= target_core_hba_release,
3037 	.show_attribute		= target_core_hba_attr_show,
3038 	.store_attribute	= target_core_hba_attr_store,
3039 };
3040 
3041 static struct config_item_type target_core_hba_cit = {
3042 	.ct_item_ops		= &target_core_hba_item_ops,
3043 	.ct_group_ops		= &target_core_hba_group_ops,
3044 	.ct_attrs		= target_core_hba_attrs,
3045 	.ct_owner		= THIS_MODULE,
3046 };
3047 
target_core_call_addhbatotarget(struct config_group * group,const char * name)3048 static struct config_group *target_core_call_addhbatotarget(
3049 	struct config_group *group,
3050 	const char *name)
3051 {
3052 	char *se_plugin_str, *str, *str2;
3053 	struct se_hba *hba;
3054 	char buf[TARGET_CORE_NAME_MAX_LEN];
3055 	unsigned long plugin_dep_id = 0;
3056 	int ret;
3057 
3058 	memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3059 	if (strlen(name) > TARGET_CORE_NAME_MAX_LEN) {
3060 		printk(KERN_ERR "Passed *name strlen(): %d exceeds"
3061 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3062 			TARGET_CORE_NAME_MAX_LEN);
3063 		return ERR_PTR(-ENAMETOOLONG);
3064 	}
3065 	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3066 
3067 	str = strstr(buf, "_");
3068 	if (!(str)) {
3069 		printk(KERN_ERR "Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3070 		return ERR_PTR(-EINVAL);
3071 	}
3072 	se_plugin_str = buf;
3073 	/*
3074 	 * Special case for subsystem plugins that have "_" in their names.
3075 	 * Namely rd_direct and rd_mcp..
3076 	 */
3077 	str2 = strstr(str+1, "_");
3078 	if ((str2)) {
3079 		*str2 = '\0'; /* Terminate for *se_plugin_str */
3080 		str2++; /* Skip to start of plugin dependent ID */
3081 		str = str2;
3082 	} else {
3083 		*str = '\0'; /* Terminate for *se_plugin_str */
3084 		str++; /* Skip to start of plugin dependent ID */
3085 	}
3086 
3087 	ret = strict_strtoul(str, 0, &plugin_dep_id);
3088 	if (ret < 0) {
3089 		printk(KERN_ERR "strict_strtoul() returned %d for"
3090 				" plugin_dep_id\n", ret);
3091 		return ERR_PTR(-EINVAL);
3092 	}
3093 	/*
3094 	 * Load up TCM subsystem plugins if they have not already been loaded.
3095 	 */
3096 	if (transport_subsystem_check_init() < 0)
3097 		return ERR_PTR(-EINVAL);
3098 
3099 	hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3100 	if (IS_ERR(hba))
3101 		return ERR_CAST(hba);
3102 
3103 	config_group_init_type_name(&hba->hba_group, name,
3104 			&target_core_hba_cit);
3105 
3106 	return &hba->hba_group;
3107 }
3108 
target_core_call_delhbafromtarget(struct config_group * group,struct config_item * item)3109 static void target_core_call_delhbafromtarget(
3110 	struct config_group *group,
3111 	struct config_item *item)
3112 {
3113 	/*
3114 	 * core_delete_hba() is called from target_core_hba_item_ops->release()
3115 	 * -> target_core_hba_release()
3116 	 */
3117 	config_item_put(item);
3118 }
3119 
3120 static struct configfs_group_operations target_core_group_ops = {
3121 	.make_group	= target_core_call_addhbatotarget,
3122 	.drop_item	= target_core_call_delhbafromtarget,
3123 };
3124 
3125 static struct config_item_type target_core_cit = {
3126 	.ct_item_ops	= NULL,
3127 	.ct_group_ops	= &target_core_group_ops,
3128 	.ct_attrs	= NULL,
3129 	.ct_owner	= THIS_MODULE,
3130 };
3131 
3132 /* Stop functions for struct config_item_type target_core_hba_cit */
3133 
target_core_init_configfs(void)3134 static int __init target_core_init_configfs(void)
3135 {
3136 	struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3137 	struct config_group *lu_gp_cg = NULL;
3138 	struct configfs_subsystem *subsys;
3139 	struct t10_alua_lu_gp *lu_gp;
3140 	int ret;
3141 
3142 	printk(KERN_INFO "TARGET_CORE[0]: Loading Generic Kernel Storage"
3143 		" Engine: %s on %s/%s on "UTS_RELEASE"\n",
3144 		TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3145 
3146 	subsys = target_core_subsystem[0];
3147 	config_group_init(&subsys->su_group);
3148 	mutex_init(&subsys->su_mutex);
3149 
3150 	INIT_LIST_HEAD(&g_tf_list);
3151 	mutex_init(&g_tf_lock);
3152 	init_scsi_index_table();
3153 	ret = init_se_global();
3154 	if (ret < 0)
3155 		return -1;
3156 	/*
3157 	 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3158 	 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3159 	 */
3160 	target_cg = &subsys->su_group;
3161 	target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3162 				GFP_KERNEL);
3163 	if (!(target_cg->default_groups)) {
3164 		printk(KERN_ERR "Unable to allocate target_cg->default_groups\n");
3165 		goto out_global;
3166 	}
3167 
3168 	config_group_init_type_name(&se_global->target_core_hbagroup,
3169 			"core", &target_core_cit);
3170 	target_cg->default_groups[0] = &se_global->target_core_hbagroup;
3171 	target_cg->default_groups[1] = NULL;
3172 	/*
3173 	 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3174 	 */
3175 	hba_cg = &se_global->target_core_hbagroup;
3176 	hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3177 				GFP_KERNEL);
3178 	if (!(hba_cg->default_groups)) {
3179 		printk(KERN_ERR "Unable to allocate hba_cg->default_groups\n");
3180 		goto out_global;
3181 	}
3182 	config_group_init_type_name(&se_global->alua_group,
3183 			"alua", &target_core_alua_cit);
3184 	hba_cg->default_groups[0] = &se_global->alua_group;
3185 	hba_cg->default_groups[1] = NULL;
3186 	/*
3187 	 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3188 	 * groups under /sys/kernel/config/target/core/alua/
3189 	 */
3190 	alua_cg = &se_global->alua_group;
3191 	alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3192 			GFP_KERNEL);
3193 	if (!(alua_cg->default_groups)) {
3194 		printk(KERN_ERR "Unable to allocate alua_cg->default_groups\n");
3195 		goto out_global;
3196 	}
3197 
3198 	config_group_init_type_name(&se_global->alua_lu_gps_group,
3199 			"lu_gps", &target_core_alua_lu_gps_cit);
3200 	alua_cg->default_groups[0] = &se_global->alua_lu_gps_group;
3201 	alua_cg->default_groups[1] = NULL;
3202 	/*
3203 	 * Add core/alua/lu_gps/default_lu_gp
3204 	 */
3205 	lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3206 	if (IS_ERR(lu_gp))
3207 		goto out_global;
3208 
3209 	lu_gp_cg = &se_global->alua_lu_gps_group;
3210 	lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3211 			GFP_KERNEL);
3212 	if (!(lu_gp_cg->default_groups)) {
3213 		printk(KERN_ERR "Unable to allocate lu_gp_cg->default_groups\n");
3214 		goto out_global;
3215 	}
3216 
3217 	config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3218 				&target_core_alua_lu_gp_cit);
3219 	lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3220 	lu_gp_cg->default_groups[1] = NULL;
3221 	se_global->default_lu_gp = lu_gp;
3222 	/*
3223 	 * Register the target_core_mod subsystem with configfs.
3224 	 */
3225 	ret = configfs_register_subsystem(subsys);
3226 	if (ret < 0) {
3227 		printk(KERN_ERR "Error %d while registering subsystem %s\n",
3228 			ret, subsys->su_group.cg_item.ci_namebuf);
3229 		goto out_global;
3230 	}
3231 	printk(KERN_INFO "TARGET_CORE[0]: Initialized ConfigFS Fabric"
3232 		" Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3233 		" on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3234 	/*
3235 	 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3236 	 */
3237 	ret = rd_module_init();
3238 	if (ret < 0)
3239 		goto out;
3240 
3241 	if (core_dev_setup_virtual_lun0() < 0)
3242 		goto out;
3243 
3244 	return 0;
3245 
3246 out:
3247 	configfs_unregister_subsystem(subsys);
3248 	core_dev_release_virtual_lun0();
3249 	rd_module_exit();
3250 out_global:
3251 	if (se_global->default_lu_gp) {
3252 		core_alua_free_lu_gp(se_global->default_lu_gp);
3253 		se_global->default_lu_gp = NULL;
3254 	}
3255 	if (lu_gp_cg)
3256 		kfree(lu_gp_cg->default_groups);
3257 	if (alua_cg)
3258 		kfree(alua_cg->default_groups);
3259 	if (hba_cg)
3260 		kfree(hba_cg->default_groups);
3261 	kfree(target_cg->default_groups);
3262 	release_se_global();
3263 	return -1;
3264 }
3265 
target_core_exit_configfs(void)3266 static void __exit target_core_exit_configfs(void)
3267 {
3268 	struct configfs_subsystem *subsys;
3269 	struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3270 	struct config_item *item;
3271 	int i;
3272 
3273 	se_global->in_shutdown = 1;
3274 	subsys = target_core_subsystem[0];
3275 
3276 	lu_gp_cg = &se_global->alua_lu_gps_group;
3277 	for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3278 		item = &lu_gp_cg->default_groups[i]->cg_item;
3279 		lu_gp_cg->default_groups[i] = NULL;
3280 		config_item_put(item);
3281 	}
3282 	kfree(lu_gp_cg->default_groups);
3283 	lu_gp_cg->default_groups = NULL;
3284 
3285 	alua_cg = &se_global->alua_group;
3286 	for (i = 0; alua_cg->default_groups[i]; i++) {
3287 		item = &alua_cg->default_groups[i]->cg_item;
3288 		alua_cg->default_groups[i] = NULL;
3289 		config_item_put(item);
3290 	}
3291 	kfree(alua_cg->default_groups);
3292 	alua_cg->default_groups = NULL;
3293 
3294 	hba_cg = &se_global->target_core_hbagroup;
3295 	for (i = 0; hba_cg->default_groups[i]; i++) {
3296 		item = &hba_cg->default_groups[i]->cg_item;
3297 		hba_cg->default_groups[i] = NULL;
3298 		config_item_put(item);
3299 	}
3300 	kfree(hba_cg->default_groups);
3301 	hba_cg->default_groups = NULL;
3302 	/*
3303 	 * We expect subsys->su_group.default_groups to be released
3304 	 * by configfs subsystem provider logic..
3305 	 */
3306 	configfs_unregister_subsystem(subsys);
3307 	kfree(subsys->su_group.default_groups);
3308 
3309 	core_alua_free_lu_gp(se_global->default_lu_gp);
3310 	se_global->default_lu_gp = NULL;
3311 
3312 	printk(KERN_INFO "TARGET_CORE[0]: Released ConfigFS Fabric"
3313 			" Infrastructure\n");
3314 
3315 	core_dev_release_virtual_lun0();
3316 	rd_module_exit();
3317 	release_se_global();
3318 
3319 	return;
3320 }
3321 
3322 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3323 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3324 MODULE_LICENSE("GPL");
3325 
3326 module_init(target_core_init_configfs);
3327 module_exit(target_core_exit_configfs);
3328