1 /*******************************************************************************
2 * Filename: target_core_fabric_configfs.c
3  *
4  * This file contains generic fabric module configfs infrastructure for
5  * TCM v4.x code
6  *
7  * (c) Copyright 2010-2013 Datera, Inc.
8  *
9  * Nicholas A. Bellinger <nab@linux-iscsi.org>
10 *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  ****************************************************************************/
21 
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/utsname.h>
25 #include <linux/init.h>
26 #include <linux/fs.h>
27 #include <linux/namei.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
31 #include <linux/unistd.h>
32 #include <linux/string.h>
33 #include <linux/syscalls.h>
34 #include <linux/configfs.h>
35 
36 #include <target/target_core_base.h>
37 #include <target/target_core_fabric.h>
38 
39 #include "target_core_internal.h"
40 #include "target_core_alua.h"
41 #include "target_core_pr.h"
42 
43 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)		\
44 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
45 {									\
46 	struct config_item_type *cit = &tf->tf_##_name##_cit;		\
47 									\
48 	cit->ct_item_ops = _item_ops;					\
49 	cit->ct_group_ops = _group_ops;					\
50 	cit->ct_attrs = _attrs;						\
51 	cit->ct_owner = tf->tf_ops->module;				\
52 	pr_debug("Setup generic %s\n", __stringify(_name));		\
53 }
54 
55 #define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops)		\
56 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
57 {									\
58 	struct config_item_type *cit = &tf->tf_##_name##_cit;		\
59 	struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
60 									\
61 	cit->ct_item_ops = _item_ops;					\
62 	cit->ct_group_ops = _group_ops;					\
63 	cit->ct_attrs = attrs;						\
64 	cit->ct_owner = tf->tf_ops->module;				\
65 	pr_debug("Setup generic %s\n", __stringify(_name));		\
66 }
67 
68 /* Start of tfc_tpg_mappedlun_cit */
69 
70 static int target_fabric_mappedlun_link(
71 	struct config_item *lun_acl_ci,
72 	struct config_item *lun_ci)
73 {
74 	struct se_dev_entry *deve;
75 	struct se_lun *lun = container_of(to_config_group(lun_ci),
76 			struct se_lun, lun_group);
77 	struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
78 			struct se_lun_acl, se_lun_group);
79 	struct se_portal_group *se_tpg;
80 	struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
81 	bool lun_access_ro;
82 
83 	if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
84 		pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
85 			" %p to struct lun: %p\n", lun_ci, lun);
86 		return -EFAULT;
87 	}
88 	/*
89 	 * Ensure that the source port exists
90 	 */
91 	if (!lun->lun_se_dev) {
92 		pr_err("Source se_lun->lun_se_dev does not exist\n");
93 		return -EINVAL;
94 	}
95 	if (lun->lun_shutdown) {
96 		pr_err("Unable to create mappedlun symlink because"
97 			" lun->lun_shutdown=true\n");
98 		return -EINVAL;
99 	}
100 	se_tpg = lun->lun_tpg;
101 
102 	nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
103 	tpg_ci = &nacl_ci->ci_group->cg_item;
104 	wwn_ci = &tpg_ci->ci_group->cg_item;
105 	tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
106 	wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
107 	/*
108 	 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
109 	 */
110 	if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
111 		pr_err("Illegal Initiator ACL SymLink outside of %s\n",
112 			config_item_name(wwn_ci));
113 		return -EINVAL;
114 	}
115 	if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
116 		pr_err("Illegal Initiator ACL Symlink outside of %s"
117 			" TPGT: %s\n", config_item_name(wwn_ci),
118 			config_item_name(tpg_ci));
119 		return -EINVAL;
120 	}
121 	/*
122 	 * If this struct se_node_acl was dynamically generated with
123 	 * tpg_1/attrib/generate_node_acls=1, use the existing
124 	 * deve->lun_access_ro value, which will be true when
125 	 * tpg_1/attrib/demo_mode_write_protect=1
126 	 */
127 	rcu_read_lock();
128 	deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
129 	if (deve)
130 		lun_access_ro = deve->lun_access_ro;
131 	else
132 		lun_access_ro =
133 			(se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
134 				se_tpg)) ? true : false;
135 	rcu_read_unlock();
136 	/*
137 	 * Determine the actual mapped LUN value user wants..
138 	 *
139 	 * This value is what the SCSI Initiator actually sees the
140 	 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
141 	 */
142 	return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
143 }
144 
145 static void target_fabric_mappedlun_unlink(
146 	struct config_item *lun_acl_ci,
147 	struct config_item *lun_ci)
148 {
149 	struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
150 			struct se_lun_acl, se_lun_group);
151 	struct se_lun *lun = container_of(to_config_group(lun_ci),
152 			struct se_lun, lun_group);
153 
154 	core_dev_del_initiator_node_lun_acl(lun, lacl);
155 }
156 
157 static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
158 {
159 	return container_of(to_config_group(item), struct se_lun_acl,
160 			se_lun_group);
161 }
162 
163 static ssize_t target_fabric_mappedlun_write_protect_show(
164 		struct config_item *item, char *page)
165 {
166 	struct se_lun_acl *lacl = item_to_lun_acl(item);
167 	struct se_node_acl *se_nacl = lacl->se_lun_nacl;
168 	struct se_dev_entry *deve;
169 	ssize_t len = 0;
170 
171 	rcu_read_lock();
172 	deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
173 	if (deve) {
174 		len = sprintf(page, "%d\n", deve->lun_access_ro);
175 	}
176 	rcu_read_unlock();
177 
178 	return len;
179 }
180 
181 static ssize_t target_fabric_mappedlun_write_protect_store(
182 		struct config_item *item, const char *page, size_t count)
183 {
184 	struct se_lun_acl *lacl = item_to_lun_acl(item);
185 	struct se_node_acl *se_nacl = lacl->se_lun_nacl;
186 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
187 	unsigned long wp;
188 	int ret;
189 
190 	ret = kstrtoul(page, 0, &wp);
191 	if (ret)
192 		return ret;
193 
194 	if ((wp != 1) && (wp != 0))
195 		return -EINVAL;
196 
197 	/* wp=1 means lun_access_ro=true */
198 	core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
199 
200 	pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
201 		" Mapped LUN: %llu Write Protect bit to %s\n",
202 		se_tpg->se_tpg_tfo->get_fabric_name(),
203 		se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
204 
205 	return count;
206 
207 }
208 
209 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
210 
211 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
212 	&target_fabric_mappedlun_attr_write_protect,
213 	NULL,
214 };
215 
216 static void target_fabric_mappedlun_release(struct config_item *item)
217 {
218 	struct se_lun_acl *lacl = container_of(to_config_group(item),
219 				struct se_lun_acl, se_lun_group);
220 	struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
221 
222 	core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
223 }
224 
225 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
226 	.release		= target_fabric_mappedlun_release,
227 	.allow_link		= target_fabric_mappedlun_link,
228 	.drop_link		= target_fabric_mappedlun_unlink,
229 };
230 
231 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
232 		target_fabric_mappedlun_attrs);
233 
234 /* End of tfc_tpg_mappedlun_cit */
235 
236 /* Start of tfc_tpg_mappedlun_port_cit */
237 
238 static struct config_group *target_core_mappedlun_stat_mkdir(
239 	struct config_group *group,
240 	const char *name)
241 {
242 	return ERR_PTR(-ENOSYS);
243 }
244 
245 static void target_core_mappedlun_stat_rmdir(
246 	struct config_group *group,
247 	struct config_item *item)
248 {
249 	return;
250 }
251 
252 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
253 	.make_group		= target_core_mappedlun_stat_mkdir,
254 	.drop_item		= target_core_mappedlun_stat_rmdir,
255 };
256 
257 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
258 		NULL);
259 
260 /* End of tfc_tpg_mappedlun_port_cit */
261 
262 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
263 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
264 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
265 
266 /* Start of tfc_tpg_nacl_base_cit */
267 
268 static struct config_group *target_fabric_make_mappedlun(
269 	struct config_group *group,
270 	const char *name)
271 {
272 	struct se_node_acl *se_nacl = container_of(group,
273 			struct se_node_acl, acl_group);
274 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
275 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
276 	struct se_lun_acl *lacl = NULL;
277 	char *buf;
278 	unsigned long long mapped_lun;
279 	int ret = 0;
280 
281 	buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
282 	if (!buf) {
283 		pr_err("Unable to allocate memory for name buf\n");
284 		return ERR_PTR(-ENOMEM);
285 	}
286 	snprintf(buf, strlen(name) + 1, "%s", name);
287 	/*
288 	 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
289 	 */
290 	if (strstr(buf, "lun_") != buf) {
291 		pr_err("Unable to locate \"lun_\" from buf: %s"
292 			" name: %s\n", buf, name);
293 		ret = -EINVAL;
294 		goto out;
295 	}
296 	/*
297 	 * Determine the Mapped LUN value.  This is what the SCSI Initiator
298 	 * Port will actually see.
299 	 */
300 	ret = kstrtoull(buf + 4, 0, &mapped_lun);
301 	if (ret)
302 		goto out;
303 
304 	lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
305 			mapped_lun, &ret);
306 	if (!lacl) {
307 		ret = -EINVAL;
308 		goto out;
309 	}
310 
311 	config_group_init_type_name(&lacl->se_lun_group, name,
312 			&tf->tf_tpg_mappedlun_cit);
313 
314 	config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
315 			"statistics", &tf->tf_tpg_mappedlun_stat_cit);
316 	configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
317 			&lacl->se_lun_group);
318 
319 	target_stat_setup_mappedlun_default_groups(lacl);
320 
321 	kfree(buf);
322 	return &lacl->se_lun_group;
323 out:
324 	kfree(lacl);
325 	kfree(buf);
326 	return ERR_PTR(ret);
327 }
328 
329 static void target_fabric_drop_mappedlun(
330 	struct config_group *group,
331 	struct config_item *item)
332 {
333 	struct se_lun_acl *lacl = container_of(to_config_group(item),
334 			struct se_lun_acl, se_lun_group);
335 
336 	configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
337 	configfs_remove_default_groups(&lacl->se_lun_group);
338 
339 	config_item_put(item);
340 }
341 
342 static void target_fabric_nacl_base_release(struct config_item *item)
343 {
344 	struct se_node_acl *se_nacl = container_of(to_config_group(item),
345 			struct se_node_acl, acl_group);
346 
347 	configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
348 	core_tpg_del_initiator_node_acl(se_nacl);
349 }
350 
351 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
352 	.release		= target_fabric_nacl_base_release,
353 };
354 
355 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
356 	.make_group		= target_fabric_make_mappedlun,
357 	.drop_item		= target_fabric_drop_mappedlun,
358 };
359 
360 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
361 		&target_fabric_nacl_base_group_ops);
362 
363 /* End of tfc_tpg_nacl_base_cit */
364 
365 /* Start of tfc_node_fabric_stats_cit */
366 /*
367  * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
368  * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
369  */
370 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
371 
372 /* End of tfc_wwn_fabric_stats_cit */
373 
374 /* Start of tfc_tpg_nacl_cit */
375 
376 static struct config_group *target_fabric_make_nodeacl(
377 	struct config_group *group,
378 	const char *name)
379 {
380 	struct se_portal_group *se_tpg = container_of(group,
381 			struct se_portal_group, tpg_acl_group);
382 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
383 	struct se_node_acl *se_nacl;
384 
385 	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
386 	if (IS_ERR(se_nacl))
387 		return ERR_CAST(se_nacl);
388 
389 	config_group_init_type_name(&se_nacl->acl_group, name,
390 			&tf->tf_tpg_nacl_base_cit);
391 
392 	config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
393 			&tf->tf_tpg_nacl_attrib_cit);
394 	configfs_add_default_group(&se_nacl->acl_attrib_group,
395 			&se_nacl->acl_group);
396 
397 	config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
398 			&tf->tf_tpg_nacl_auth_cit);
399 	configfs_add_default_group(&se_nacl->acl_auth_group,
400 			&se_nacl->acl_group);
401 
402 	config_group_init_type_name(&se_nacl->acl_param_group, "param",
403 			&tf->tf_tpg_nacl_param_cit);
404 	configfs_add_default_group(&se_nacl->acl_param_group,
405 			&se_nacl->acl_group);
406 
407 	config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
408 			"fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
409 	configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
410 			&se_nacl->acl_group);
411 
412 	if (tf->tf_ops->fabric_init_nodeacl) {
413 		int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
414 		if (ret) {
415 			configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
416 			core_tpg_del_initiator_node_acl(se_nacl);
417 			return ERR_PTR(ret);
418 		}
419 	}
420 
421 	return &se_nacl->acl_group;
422 }
423 
424 static void target_fabric_drop_nodeacl(
425 	struct config_group *group,
426 	struct config_item *item)
427 {
428 	struct se_node_acl *se_nacl = container_of(to_config_group(item),
429 			struct se_node_acl, acl_group);
430 
431 	configfs_remove_default_groups(&se_nacl->acl_group);
432 
433 	/*
434 	 * struct se_node_acl free is done in target_fabric_nacl_base_release()
435 	 */
436 	config_item_put(item);
437 }
438 
439 static struct configfs_group_operations target_fabric_nacl_group_ops = {
440 	.make_group	= target_fabric_make_nodeacl,
441 	.drop_item	= target_fabric_drop_nodeacl,
442 };
443 
444 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
445 
446 /* End of tfc_tpg_nacl_cit */
447 
448 /* Start of tfc_tpg_np_base_cit */
449 
450 static void target_fabric_np_base_release(struct config_item *item)
451 {
452 	struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
453 				struct se_tpg_np, tpg_np_group);
454 	struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
455 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
456 
457 	tf->tf_ops->fabric_drop_np(se_tpg_np);
458 }
459 
460 static struct configfs_item_operations target_fabric_np_base_item_ops = {
461 	.release		= target_fabric_np_base_release,
462 };
463 
464 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
465 
466 /* End of tfc_tpg_np_base_cit */
467 
468 /* Start of tfc_tpg_np_cit */
469 
470 static struct config_group *target_fabric_make_np(
471 	struct config_group *group,
472 	const char *name)
473 {
474 	struct se_portal_group *se_tpg = container_of(group,
475 				struct se_portal_group, tpg_np_group);
476 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
477 	struct se_tpg_np *se_tpg_np;
478 
479 	if (!tf->tf_ops->fabric_make_np) {
480 		pr_err("tf->tf_ops.fabric_make_np is NULL\n");
481 		return ERR_PTR(-ENOSYS);
482 	}
483 
484 	se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
485 	if (!se_tpg_np || IS_ERR(se_tpg_np))
486 		return ERR_PTR(-EINVAL);
487 
488 	se_tpg_np->tpg_np_parent = se_tpg;
489 	config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
490 			&tf->tf_tpg_np_base_cit);
491 
492 	return &se_tpg_np->tpg_np_group;
493 }
494 
495 static void target_fabric_drop_np(
496 	struct config_group *group,
497 	struct config_item *item)
498 {
499 	/*
500 	 * struct se_tpg_np is released via target_fabric_np_base_release()
501 	 */
502 	config_item_put(item);
503 }
504 
505 static struct configfs_group_operations target_fabric_np_group_ops = {
506 	.make_group	= &target_fabric_make_np,
507 	.drop_item	= &target_fabric_drop_np,
508 };
509 
510 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
511 
512 /* End of tfc_tpg_np_cit */
513 
514 /* Start of tfc_tpg_port_cit */
515 
516 static struct se_lun *item_to_lun(struct config_item *item)
517 {
518 	return container_of(to_config_group(item), struct se_lun,
519 			lun_group);
520 }
521 
522 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
523 		char *page)
524 {
525 	struct se_lun *lun = item_to_lun(item);
526 
527 	if (!lun || !lun->lun_se_dev)
528 		return -ENODEV;
529 
530 	return core_alua_show_tg_pt_gp_info(lun, page);
531 }
532 
533 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
534 		const char *page, size_t count)
535 {
536 	struct se_lun *lun = item_to_lun(item);
537 
538 	if (!lun || !lun->lun_se_dev)
539 		return -ENODEV;
540 
541 	return core_alua_store_tg_pt_gp_info(lun, page, count);
542 }
543 
544 static ssize_t target_fabric_port_alua_tg_pt_offline_show(
545 		struct config_item *item, char *page)
546 {
547 	struct se_lun *lun = item_to_lun(item);
548 
549 	if (!lun || !lun->lun_se_dev)
550 		return -ENODEV;
551 
552 	return core_alua_show_offline_bit(lun, page);
553 }
554 
555 static ssize_t target_fabric_port_alua_tg_pt_offline_store(
556 		struct config_item *item, const char *page, size_t count)
557 {
558 	struct se_lun *lun = item_to_lun(item);
559 
560 	if (!lun || !lun->lun_se_dev)
561 		return -ENODEV;
562 
563 	return core_alua_store_offline_bit(lun, page, count);
564 }
565 
566 static ssize_t target_fabric_port_alua_tg_pt_status_show(
567 		struct config_item *item, char *page)
568 {
569 	struct se_lun *lun = item_to_lun(item);
570 
571 	if (!lun || !lun->lun_se_dev)
572 		return -ENODEV;
573 
574 	return core_alua_show_secondary_status(lun, page);
575 }
576 
577 static ssize_t target_fabric_port_alua_tg_pt_status_store(
578 		struct config_item *item, const char *page, size_t count)
579 {
580 	struct se_lun *lun = item_to_lun(item);
581 
582 	if (!lun || !lun->lun_se_dev)
583 		return -ENODEV;
584 
585 	return core_alua_store_secondary_status(lun, page, count);
586 }
587 
588 static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
589 		struct config_item *item, char *page)
590 {
591 	struct se_lun *lun = item_to_lun(item);
592 
593 	if (!lun || !lun->lun_se_dev)
594 		return -ENODEV;
595 
596 	return core_alua_show_secondary_write_metadata(lun, page);
597 }
598 
599 static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
600 		struct config_item *item, const char *page, size_t count)
601 {
602 	struct se_lun *lun = item_to_lun(item);
603 
604 	if (!lun || !lun->lun_se_dev)
605 		return -ENODEV;
606 
607 	return core_alua_store_secondary_write_metadata(lun, page, count);
608 }
609 
610 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
611 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
612 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
613 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
614 
615 static struct configfs_attribute *target_fabric_port_attrs[] = {
616 	&target_fabric_port_attr_alua_tg_pt_gp,
617 	&target_fabric_port_attr_alua_tg_pt_offline,
618 	&target_fabric_port_attr_alua_tg_pt_status,
619 	&target_fabric_port_attr_alua_tg_pt_write_md,
620 	NULL,
621 };
622 
623 extern struct configfs_item_operations target_core_dev_item_ops;
624 
625 static int target_fabric_port_link(
626 	struct config_item *lun_ci,
627 	struct config_item *se_dev_ci)
628 {
629 	struct config_item *tpg_ci;
630 	struct se_lun *lun = container_of(to_config_group(lun_ci),
631 				struct se_lun, lun_group);
632 	struct se_portal_group *se_tpg;
633 	struct se_device *dev;
634 	struct target_fabric_configfs *tf;
635 	int ret;
636 
637 	if (!se_dev_ci->ci_type ||
638 	    se_dev_ci->ci_type->ct_item_ops != &target_core_dev_item_ops) {
639 		pr_err("Bad se_dev_ci, not a valid se_dev_ci pointer: %p\n", se_dev_ci);
640 		return -EFAULT;
641 	}
642 	dev = container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
643 
644 	if (!(dev->dev_flags & DF_CONFIGURED)) {
645 		pr_err("se_device not configured yet, cannot port link\n");
646 		return -ENODEV;
647 	}
648 
649 	tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
650 	se_tpg = container_of(to_config_group(tpg_ci),
651 				struct se_portal_group, tpg_group);
652 	tf = se_tpg->se_tpg_wwn->wwn_tf;
653 
654 	if (lun->lun_se_dev !=  NULL) {
655 		pr_err("Port Symlink already exists\n");
656 		return -EEXIST;
657 	}
658 
659 	ret = core_dev_add_lun(se_tpg, dev, lun);
660 	if (ret) {
661 		pr_err("core_dev_add_lun() failed: %d\n", ret);
662 		goto out;
663 	}
664 
665 	if (tf->tf_ops->fabric_post_link) {
666 		/*
667 		 * Call the optional fabric_post_link() to allow a
668 		 * fabric module to setup any additional state once
669 		 * core_dev_add_lun() has been called..
670 		 */
671 		tf->tf_ops->fabric_post_link(se_tpg, lun);
672 	}
673 
674 	return 0;
675 out:
676 	return ret;
677 }
678 
679 static void target_fabric_port_unlink(
680 	struct config_item *lun_ci,
681 	struct config_item *se_dev_ci)
682 {
683 	struct se_lun *lun = container_of(to_config_group(lun_ci),
684 				struct se_lun, lun_group);
685 	struct se_portal_group *se_tpg = lun->lun_tpg;
686 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
687 
688 	if (tf->tf_ops->fabric_pre_unlink) {
689 		/*
690 		 * Call the optional fabric_pre_unlink() to allow a
691 		 * fabric module to release any additional stat before
692 		 * core_dev_del_lun() is called.
693 		*/
694 		tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
695 	}
696 
697 	core_dev_del_lun(se_tpg, lun);
698 }
699 
700 static void target_fabric_port_release(struct config_item *item)
701 {
702 	struct se_lun *lun = container_of(to_config_group(item),
703 					  struct se_lun, lun_group);
704 
705 	kfree_rcu(lun, rcu_head);
706 }
707 
708 static struct configfs_item_operations target_fabric_port_item_ops = {
709 	.release		= target_fabric_port_release,
710 	.allow_link		= target_fabric_port_link,
711 	.drop_link		= target_fabric_port_unlink,
712 };
713 
714 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
715 
716 /* End of tfc_tpg_port_cit */
717 
718 /* Start of tfc_tpg_port_stat_cit */
719 
720 static struct config_group *target_core_port_stat_mkdir(
721 	struct config_group *group,
722 	const char *name)
723 {
724 	return ERR_PTR(-ENOSYS);
725 }
726 
727 static void target_core_port_stat_rmdir(
728 	struct config_group *group,
729 	struct config_item *item)
730 {
731 	return;
732 }
733 
734 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
735 	.make_group		= target_core_port_stat_mkdir,
736 	.drop_item		= target_core_port_stat_rmdir,
737 };
738 
739 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
740 
741 /* End of tfc_tpg_port_stat_cit */
742 
743 /* Start of tfc_tpg_lun_cit */
744 
745 static struct config_group *target_fabric_make_lun(
746 	struct config_group *group,
747 	const char *name)
748 {
749 	struct se_lun *lun;
750 	struct se_portal_group *se_tpg = container_of(group,
751 			struct se_portal_group, tpg_lun_group);
752 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
753 	unsigned long long unpacked_lun;
754 	int errno;
755 
756 	if (strstr(name, "lun_") != name) {
757 		pr_err("Unable to locate \'_\" in"
758 				" \"lun_$LUN_NUMBER\"\n");
759 		return ERR_PTR(-EINVAL);
760 	}
761 	errno = kstrtoull(name + 4, 0, &unpacked_lun);
762 	if (errno)
763 		return ERR_PTR(errno);
764 
765 	lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
766 	if (IS_ERR(lun))
767 		return ERR_CAST(lun);
768 
769 	config_group_init_type_name(&lun->lun_group, name,
770 			&tf->tf_tpg_port_cit);
771 
772 	config_group_init_type_name(&lun->port_stat_grps.stat_group,
773 			"statistics", &tf->tf_tpg_port_stat_cit);
774 	configfs_add_default_group(&lun->port_stat_grps.stat_group,
775 			&lun->lun_group);
776 
777 	target_stat_setup_port_default_groups(lun);
778 
779 	return &lun->lun_group;
780 }
781 
782 static void target_fabric_drop_lun(
783 	struct config_group *group,
784 	struct config_item *item)
785 {
786 	struct se_lun *lun = container_of(to_config_group(item),
787 				struct se_lun, lun_group);
788 
789 	configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
790 	configfs_remove_default_groups(&lun->lun_group);
791 
792 	config_item_put(item);
793 }
794 
795 static struct configfs_group_operations target_fabric_lun_group_ops = {
796 	.make_group	= &target_fabric_make_lun,
797 	.drop_item	= &target_fabric_drop_lun,
798 };
799 
800 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
801 
802 /* End of tfc_tpg_lun_cit */
803 
804 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
805 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
806 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
807 
808 /* Start of tfc_tpg_base_cit */
809 
810 static void target_fabric_tpg_release(struct config_item *item)
811 {
812 	struct se_portal_group *se_tpg = container_of(to_config_group(item),
813 			struct se_portal_group, tpg_group);
814 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
815 	struct target_fabric_configfs *tf = wwn->wwn_tf;
816 
817 	tf->tf_ops->fabric_drop_tpg(se_tpg);
818 }
819 
820 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
821 	.release		= target_fabric_tpg_release,
822 };
823 
824 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
825 
826 /* End of tfc_tpg_base_cit */
827 
828 /* Start of tfc_tpg_cit */
829 
830 static struct config_group *target_fabric_make_tpg(
831 	struct config_group *group,
832 	const char *name)
833 {
834 	struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
835 	struct target_fabric_configfs *tf = wwn->wwn_tf;
836 	struct se_portal_group *se_tpg;
837 
838 	if (!tf->tf_ops->fabric_make_tpg) {
839 		pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
840 		return ERR_PTR(-ENOSYS);
841 	}
842 
843 	se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
844 	if (!se_tpg || IS_ERR(se_tpg))
845 		return ERR_PTR(-EINVAL);
846 
847 	config_group_init_type_name(&se_tpg->tpg_group, name,
848 			&tf->tf_tpg_base_cit);
849 
850 	config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
851 			&tf->tf_tpg_lun_cit);
852 	configfs_add_default_group(&se_tpg->tpg_lun_group,
853 			&se_tpg->tpg_group);
854 
855 	config_group_init_type_name(&se_tpg->tpg_np_group, "np",
856 			&tf->tf_tpg_np_cit);
857 	configfs_add_default_group(&se_tpg->tpg_np_group,
858 			&se_tpg->tpg_group);
859 
860 	config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
861 			&tf->tf_tpg_nacl_cit);
862 	configfs_add_default_group(&se_tpg->tpg_acl_group,
863 			&se_tpg->tpg_group);
864 
865 	config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
866 			&tf->tf_tpg_attrib_cit);
867 	configfs_add_default_group(&se_tpg->tpg_attrib_group,
868 			&se_tpg->tpg_group);
869 
870 	config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
871 			&tf->tf_tpg_auth_cit);
872 	configfs_add_default_group(&se_tpg->tpg_auth_group,
873 			&se_tpg->tpg_group);
874 
875 	config_group_init_type_name(&se_tpg->tpg_param_group, "param",
876 			&tf->tf_tpg_param_cit);
877 	configfs_add_default_group(&se_tpg->tpg_param_group,
878 			&se_tpg->tpg_group);
879 
880 	return &se_tpg->tpg_group;
881 }
882 
883 static void target_fabric_drop_tpg(
884 	struct config_group *group,
885 	struct config_item *item)
886 {
887 	struct se_portal_group *se_tpg = container_of(to_config_group(item),
888 				struct se_portal_group, tpg_group);
889 
890 	configfs_remove_default_groups(&se_tpg->tpg_group);
891 	config_item_put(item);
892 }
893 
894 static void target_fabric_release_wwn(struct config_item *item)
895 {
896 	struct se_wwn *wwn = container_of(to_config_group(item),
897 				struct se_wwn, wwn_group);
898 	struct target_fabric_configfs *tf = wwn->wwn_tf;
899 
900 	configfs_remove_default_groups(&wwn->fabric_stat_group);
901 	tf->tf_ops->fabric_drop_wwn(wwn);
902 }
903 
904 static struct configfs_item_operations target_fabric_tpg_item_ops = {
905 	.release	= target_fabric_release_wwn,
906 };
907 
908 static struct configfs_group_operations target_fabric_tpg_group_ops = {
909 	.make_group	= target_fabric_make_tpg,
910 	.drop_item	= target_fabric_drop_tpg,
911 };
912 
913 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
914 		NULL);
915 
916 /* End of tfc_tpg_cit */
917 
918 /* Start of tfc_wwn_fabric_stats_cit */
919 /*
920  * This is used as a placeholder for struct se_wwn->fabric_stat_group
921  * to allow fabrics access to ->fabric_stat_group->default_groups[]
922  */
923 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
924 
925 /* End of tfc_wwn_fabric_stats_cit */
926 
927 /* Start of tfc_wwn_cit */
928 
929 static struct config_group *target_fabric_make_wwn(
930 	struct config_group *group,
931 	const char *name)
932 {
933 	struct target_fabric_configfs *tf = container_of(group,
934 				struct target_fabric_configfs, tf_group);
935 	struct se_wwn *wwn;
936 
937 	if (!tf->tf_ops->fabric_make_wwn) {
938 		pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
939 		return ERR_PTR(-ENOSYS);
940 	}
941 
942 	wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
943 	if (!wwn || IS_ERR(wwn))
944 		return ERR_PTR(-EINVAL);
945 
946 	wwn->wwn_tf = tf;
947 
948 	config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
949 
950 	config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
951 			&tf->tf_wwn_fabric_stats_cit);
952 	configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
953 
954 	if (tf->tf_ops->add_wwn_groups)
955 		tf->tf_ops->add_wwn_groups(wwn);
956 	return &wwn->wwn_group;
957 }
958 
959 static void target_fabric_drop_wwn(
960 	struct config_group *group,
961 	struct config_item *item)
962 {
963 	struct se_wwn *wwn = container_of(to_config_group(item),
964 				struct se_wwn, wwn_group);
965 
966 	configfs_remove_default_groups(&wwn->wwn_group);
967 	config_item_put(item);
968 }
969 
970 static struct configfs_group_operations target_fabric_wwn_group_ops = {
971 	.make_group	= target_fabric_make_wwn,
972 	.drop_item	= target_fabric_drop_wwn,
973 };
974 
975 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
976 TF_CIT_SETUP_DRV(discovery, NULL, NULL);
977 
978 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
979 {
980 	target_fabric_setup_discovery_cit(tf);
981 	target_fabric_setup_wwn_cit(tf);
982 	target_fabric_setup_wwn_fabric_stats_cit(tf);
983 	target_fabric_setup_tpg_cit(tf);
984 	target_fabric_setup_tpg_base_cit(tf);
985 	target_fabric_setup_tpg_port_cit(tf);
986 	target_fabric_setup_tpg_port_stat_cit(tf);
987 	target_fabric_setup_tpg_lun_cit(tf);
988 	target_fabric_setup_tpg_np_cit(tf);
989 	target_fabric_setup_tpg_np_base_cit(tf);
990 	target_fabric_setup_tpg_attrib_cit(tf);
991 	target_fabric_setup_tpg_auth_cit(tf);
992 	target_fabric_setup_tpg_param_cit(tf);
993 	target_fabric_setup_tpg_nacl_cit(tf);
994 	target_fabric_setup_tpg_nacl_base_cit(tf);
995 	target_fabric_setup_tpg_nacl_attrib_cit(tf);
996 	target_fabric_setup_tpg_nacl_auth_cit(tf);
997 	target_fabric_setup_tpg_nacl_param_cit(tf);
998 	target_fabric_setup_tpg_nacl_stat_cit(tf);
999 	target_fabric_setup_tpg_mappedlun_cit(tf);
1000 	target_fabric_setup_tpg_mappedlun_stat_cit(tf);
1001 
1002 	return 0;
1003 }
1004