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 	int lun_access;
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 	se_tpg = lun->lun_tpg;
96 
97 	nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
98 	tpg_ci = &nacl_ci->ci_group->cg_item;
99 	wwn_ci = &tpg_ci->ci_group->cg_item;
100 	tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
101 	wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
102 	/*
103 	 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
104 	 */
105 	if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
106 		pr_err("Illegal Initiator ACL SymLink outside of %s\n",
107 			config_item_name(wwn_ci));
108 		return -EINVAL;
109 	}
110 	if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
111 		pr_err("Illegal Initiator ACL Symlink outside of %s"
112 			" TPGT: %s\n", config_item_name(wwn_ci),
113 			config_item_name(tpg_ci));
114 		return -EINVAL;
115 	}
116 	/*
117 	 * If this struct se_node_acl was dynamically generated with
118 	 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
119 	 * which be will write protected (READ-ONLY) when
120 	 * tpg_1/attrib/demo_mode_write_protect=1
121 	 */
122 	rcu_read_lock();
123 	deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
124 	if (deve)
125 		lun_access = deve->lun_flags;
126 	else
127 		lun_access =
128 			(se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
129 				se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
130 					   TRANSPORT_LUNFLAGS_READ_WRITE;
131 	rcu_read_unlock();
132 	/*
133 	 * Determine the actual mapped LUN value user wants..
134 	 *
135 	 * This value is what the SCSI Initiator actually sees the
136 	 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
137 	 */
138 	return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access);
139 }
140 
141 static int target_fabric_mappedlun_unlink(
142 	struct config_item *lun_acl_ci,
143 	struct config_item *lun_ci)
144 {
145 	struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
146 			struct se_lun_acl, se_lun_group);
147 	struct se_lun *lun = container_of(to_config_group(lun_ci),
148 			struct se_lun, lun_group);
149 
150 	return core_dev_del_initiator_node_lun_acl(lun, lacl);
151 }
152 
153 static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
154 {
155 	return container_of(to_config_group(item), struct se_lun_acl,
156 			se_lun_group);
157 }
158 
159 static ssize_t target_fabric_mappedlun_write_protect_show(
160 		struct config_item *item, char *page)
161 {
162 	struct se_lun_acl *lacl = item_to_lun_acl(item);
163 	struct se_node_acl *se_nacl = lacl->se_lun_nacl;
164 	struct se_dev_entry *deve;
165 	ssize_t len = 0;
166 
167 	rcu_read_lock();
168 	deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
169 	if (deve) {
170 		len = sprintf(page, "%d\n",
171 			(deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
172 	}
173 	rcu_read_unlock();
174 
175 	return len;
176 }
177 
178 static ssize_t target_fabric_mappedlun_write_protect_store(
179 		struct config_item *item, const char *page, size_t count)
180 {
181 	struct se_lun_acl *lacl = item_to_lun_acl(item);
182 	struct se_node_acl *se_nacl = lacl->se_lun_nacl;
183 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
184 	unsigned long op;
185 	int ret;
186 
187 	ret = kstrtoul(page, 0, &op);
188 	if (ret)
189 		return ret;
190 
191 	if ((op != 1) && (op != 0))
192 		return -EINVAL;
193 
194 	core_update_device_list_access(lacl->mapped_lun, (op) ?
195 			TRANSPORT_LUNFLAGS_READ_ONLY :
196 			TRANSPORT_LUNFLAGS_READ_WRITE,
197 			lacl->se_lun_nacl);
198 
199 	pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
200 		" Mapped LUN: %llu Write Protect bit to %s\n",
201 		se_tpg->se_tpg_tfo->get_fabric_name(),
202 		se_nacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
203 
204 	return count;
205 
206 }
207 
208 CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
209 
210 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
211 	&target_fabric_mappedlun_attr_write_protect,
212 	NULL,
213 };
214 
215 static void target_fabric_mappedlun_release(struct config_item *item)
216 {
217 	struct se_lun_acl *lacl = container_of(to_config_group(item),
218 				struct se_lun_acl, se_lun_group);
219 	struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
220 
221 	core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
222 }
223 
224 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
225 	.release		= target_fabric_mappedlun_release,
226 	.allow_link		= target_fabric_mappedlun_link,
227 	.drop_link		= target_fabric_mappedlun_unlink,
228 };
229 
230 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
231 		target_fabric_mappedlun_attrs);
232 
233 /* End of tfc_tpg_mappedlun_cit */
234 
235 /* Start of tfc_tpg_mappedlun_port_cit */
236 
237 static struct config_group *target_core_mappedlun_stat_mkdir(
238 	struct config_group *group,
239 	const char *name)
240 {
241 	return ERR_PTR(-ENOSYS);
242 }
243 
244 static void target_core_mappedlun_stat_rmdir(
245 	struct config_group *group,
246 	struct config_item *item)
247 {
248 	return;
249 }
250 
251 static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
252 	.make_group		= target_core_mappedlun_stat_mkdir,
253 	.drop_item		= target_core_mappedlun_stat_rmdir,
254 };
255 
256 TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
257 		NULL);
258 
259 /* End of tfc_tpg_mappedlun_port_cit */
260 
261 TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
262 TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
263 TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
264 
265 /* Start of tfc_tpg_nacl_base_cit */
266 
267 static struct config_group *target_fabric_make_mappedlun(
268 	struct config_group *group,
269 	const char *name)
270 {
271 	struct se_node_acl *se_nacl = container_of(group,
272 			struct se_node_acl, acl_group);
273 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
274 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
275 	struct se_lun_acl *lacl = NULL;
276 	struct config_item *acl_ci;
277 	struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
278 	char *buf;
279 	unsigned long long mapped_lun;
280 	int ret = 0;
281 
282 	acl_ci = &group->cg_item;
283 	if (!acl_ci) {
284 		pr_err("Unable to locatel acl_ci\n");
285 		return NULL;
286 	}
287 
288 	buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
289 	if (!buf) {
290 		pr_err("Unable to allocate memory for name buf\n");
291 		return ERR_PTR(-ENOMEM);
292 	}
293 	snprintf(buf, strlen(name) + 1, "%s", name);
294 	/*
295 	 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
296 	 */
297 	if (strstr(buf, "lun_") != buf) {
298 		pr_err("Unable to locate \"lun_\" from buf: %s"
299 			" name: %s\n", buf, name);
300 		ret = -EINVAL;
301 		goto out;
302 	}
303 	/*
304 	 * Determine the Mapped LUN value.  This is what the SCSI Initiator
305 	 * Port will actually see.
306 	 */
307 	ret = kstrtoull(buf + 4, 0, &mapped_lun);
308 	if (ret)
309 		goto out;
310 
311 	lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
312 			mapped_lun, &ret);
313 	if (!lacl) {
314 		ret = -EINVAL;
315 		goto out;
316 	}
317 
318 	lacl_cg = &lacl->se_lun_group;
319 	lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
320 				GFP_KERNEL);
321 	if (!lacl_cg->default_groups) {
322 		pr_err("Unable to allocate lacl_cg->default_groups\n");
323 		ret = -ENOMEM;
324 		goto out;
325 	}
326 
327 	config_group_init_type_name(&lacl->se_lun_group, name,
328 			&tf->tf_tpg_mappedlun_cit);
329 	config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
330 			"statistics", &tf->tf_tpg_mappedlun_stat_cit);
331 	lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
332 	lacl_cg->default_groups[1] = NULL;
333 
334 	ml_stat_grp = &lacl->ml_stat_grps.stat_group;
335 	ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
336 				GFP_KERNEL);
337 	if (!ml_stat_grp->default_groups) {
338 		pr_err("Unable to allocate ml_stat_grp->default_groups\n");
339 		ret = -ENOMEM;
340 		goto out;
341 	}
342 	target_stat_setup_mappedlun_default_groups(lacl);
343 
344 	kfree(buf);
345 	return &lacl->se_lun_group;
346 out:
347 	if (lacl_cg)
348 		kfree(lacl_cg->default_groups);
349 	kfree(lacl);
350 	kfree(buf);
351 	return ERR_PTR(ret);
352 }
353 
354 static void target_fabric_drop_mappedlun(
355 	struct config_group *group,
356 	struct config_item *item)
357 {
358 	struct se_lun_acl *lacl = container_of(to_config_group(item),
359 			struct se_lun_acl, se_lun_group);
360 	struct config_item *df_item;
361 	struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
362 	int i;
363 
364 	ml_stat_grp = &lacl->ml_stat_grps.stat_group;
365 	for (i = 0; ml_stat_grp->default_groups[i]; i++) {
366 		df_item = &ml_stat_grp->default_groups[i]->cg_item;
367 		ml_stat_grp->default_groups[i] = NULL;
368 		config_item_put(df_item);
369 	}
370 	kfree(ml_stat_grp->default_groups);
371 
372 	lacl_cg = &lacl->se_lun_group;
373 	for (i = 0; lacl_cg->default_groups[i]; i++) {
374 		df_item = &lacl_cg->default_groups[i]->cg_item;
375 		lacl_cg->default_groups[i] = NULL;
376 		config_item_put(df_item);
377 	}
378 	kfree(lacl_cg->default_groups);
379 
380 	config_item_put(item);
381 }
382 
383 static void target_fabric_nacl_base_release(struct config_item *item)
384 {
385 	struct se_node_acl *se_nacl = container_of(to_config_group(item),
386 			struct se_node_acl, acl_group);
387 	struct target_fabric_configfs *tf = se_nacl->se_tpg->se_tpg_wwn->wwn_tf;
388 
389 	if (tf->tf_ops->fabric_cleanup_nodeacl)
390 		tf->tf_ops->fabric_cleanup_nodeacl(se_nacl);
391 	core_tpg_del_initiator_node_acl(se_nacl);
392 }
393 
394 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
395 	.release		= target_fabric_nacl_base_release,
396 };
397 
398 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
399 	.make_group		= target_fabric_make_mappedlun,
400 	.drop_item		= target_fabric_drop_mappedlun,
401 };
402 
403 TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
404 		&target_fabric_nacl_base_group_ops);
405 
406 /* End of tfc_tpg_nacl_base_cit */
407 
408 /* Start of tfc_node_fabric_stats_cit */
409 /*
410  * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
411  * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
412  */
413 TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
414 
415 /* End of tfc_wwn_fabric_stats_cit */
416 
417 /* Start of tfc_tpg_nacl_cit */
418 
419 static struct config_group *target_fabric_make_nodeacl(
420 	struct config_group *group,
421 	const char *name)
422 {
423 	struct se_portal_group *se_tpg = container_of(group,
424 			struct se_portal_group, tpg_acl_group);
425 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
426 	struct se_node_acl *se_nacl;
427 	struct config_group *nacl_cg;
428 
429 	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
430 	if (IS_ERR(se_nacl))
431 		return ERR_CAST(se_nacl);
432 
433 	if (tf->tf_ops->fabric_init_nodeacl) {
434 		int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
435 		if (ret) {
436 			core_tpg_del_initiator_node_acl(se_nacl);
437 			return ERR_PTR(ret);
438 		}
439 	}
440 
441 	nacl_cg = &se_nacl->acl_group;
442 	nacl_cg->default_groups = se_nacl->acl_default_groups;
443 	nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
444 	nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
445 	nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
446 	nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
447 	nacl_cg->default_groups[4] = NULL;
448 
449 	config_group_init_type_name(&se_nacl->acl_group, name,
450 			&tf->tf_tpg_nacl_base_cit);
451 	config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
452 			&tf->tf_tpg_nacl_attrib_cit);
453 	config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
454 			&tf->tf_tpg_nacl_auth_cit);
455 	config_group_init_type_name(&se_nacl->acl_param_group, "param",
456 			&tf->tf_tpg_nacl_param_cit);
457 	config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
458 			"fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
459 
460 	return &se_nacl->acl_group;
461 }
462 
463 static void target_fabric_drop_nodeacl(
464 	struct config_group *group,
465 	struct config_item *item)
466 {
467 	struct se_node_acl *se_nacl = container_of(to_config_group(item),
468 			struct se_node_acl, acl_group);
469 	struct config_item *df_item;
470 	struct config_group *nacl_cg;
471 	int i;
472 
473 	nacl_cg = &se_nacl->acl_group;
474 	for (i = 0; nacl_cg->default_groups[i]; i++) {
475 		df_item = &nacl_cg->default_groups[i]->cg_item;
476 		nacl_cg->default_groups[i] = NULL;
477 		config_item_put(df_item);
478 	}
479 	/*
480 	 * struct se_node_acl free is done in target_fabric_nacl_base_release()
481 	 */
482 	config_item_put(item);
483 }
484 
485 static struct configfs_group_operations target_fabric_nacl_group_ops = {
486 	.make_group	= target_fabric_make_nodeacl,
487 	.drop_item	= target_fabric_drop_nodeacl,
488 };
489 
490 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
491 
492 /* End of tfc_tpg_nacl_cit */
493 
494 /* Start of tfc_tpg_np_base_cit */
495 
496 static void target_fabric_np_base_release(struct config_item *item)
497 {
498 	struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
499 				struct se_tpg_np, tpg_np_group);
500 	struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
501 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
502 
503 	tf->tf_ops->fabric_drop_np(se_tpg_np);
504 }
505 
506 static struct configfs_item_operations target_fabric_np_base_item_ops = {
507 	.release		= target_fabric_np_base_release,
508 };
509 
510 TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
511 
512 /* End of tfc_tpg_np_base_cit */
513 
514 /* Start of tfc_tpg_np_cit */
515 
516 static struct config_group *target_fabric_make_np(
517 	struct config_group *group,
518 	const char *name)
519 {
520 	struct se_portal_group *se_tpg = container_of(group,
521 				struct se_portal_group, tpg_np_group);
522 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
523 	struct se_tpg_np *se_tpg_np;
524 
525 	if (!tf->tf_ops->fabric_make_np) {
526 		pr_err("tf->tf_ops.fabric_make_np is NULL\n");
527 		return ERR_PTR(-ENOSYS);
528 	}
529 
530 	se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
531 	if (!se_tpg_np || IS_ERR(se_tpg_np))
532 		return ERR_PTR(-EINVAL);
533 
534 	se_tpg_np->tpg_np_parent = se_tpg;
535 	config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
536 			&tf->tf_tpg_np_base_cit);
537 
538 	return &se_tpg_np->tpg_np_group;
539 }
540 
541 static void target_fabric_drop_np(
542 	struct config_group *group,
543 	struct config_item *item)
544 {
545 	/*
546 	 * struct se_tpg_np is released via target_fabric_np_base_release()
547 	 */
548 	config_item_put(item);
549 }
550 
551 static struct configfs_group_operations target_fabric_np_group_ops = {
552 	.make_group	= &target_fabric_make_np,
553 	.drop_item	= &target_fabric_drop_np,
554 };
555 
556 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
557 
558 /* End of tfc_tpg_np_cit */
559 
560 /* Start of tfc_tpg_port_cit */
561 
562 static struct se_lun *item_to_lun(struct config_item *item)
563 {
564 	return container_of(to_config_group(item), struct se_lun,
565 			lun_group);
566 }
567 
568 static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
569 		char *page)
570 {
571 	struct se_lun *lun = item_to_lun(item);
572 
573 	if (!lun || !lun->lun_se_dev)
574 		return -ENODEV;
575 
576 	return core_alua_show_tg_pt_gp_info(lun, page);
577 }
578 
579 static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
580 		const char *page, size_t count)
581 {
582 	struct se_lun *lun = item_to_lun(item);
583 
584 	if (!lun || !lun->lun_se_dev)
585 		return -ENODEV;
586 
587 	return core_alua_store_tg_pt_gp_info(lun, page, count);
588 }
589 
590 static ssize_t target_fabric_port_alua_tg_pt_offline_show(
591 		struct config_item *item, char *page)
592 {
593 	struct se_lun *lun = item_to_lun(item);
594 
595 	if (!lun || !lun->lun_se_dev)
596 		return -ENODEV;
597 
598 	return core_alua_show_offline_bit(lun, page);
599 }
600 
601 static ssize_t target_fabric_port_alua_tg_pt_offline_store(
602 		struct config_item *item, const char *page, size_t count)
603 {
604 	struct se_lun *lun = item_to_lun(item);
605 
606 	if (!lun || !lun->lun_se_dev)
607 		return -ENODEV;
608 
609 	return core_alua_store_offline_bit(lun, page, count);
610 }
611 
612 static ssize_t target_fabric_port_alua_tg_pt_status_show(
613 		struct config_item *item, char *page)
614 {
615 	struct se_lun *lun = item_to_lun(item);
616 
617 	if (!lun || !lun->lun_se_dev)
618 		return -ENODEV;
619 
620 	return core_alua_show_secondary_status(lun, page);
621 }
622 
623 static ssize_t target_fabric_port_alua_tg_pt_status_store(
624 		struct config_item *item, const char *page, size_t count)
625 {
626 	struct se_lun *lun = item_to_lun(item);
627 
628 	if (!lun || !lun->lun_se_dev)
629 		return -ENODEV;
630 
631 	return core_alua_store_secondary_status(lun, page, count);
632 }
633 
634 static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
635 		struct config_item *item, char *page)
636 {
637 	struct se_lun *lun = item_to_lun(item);
638 
639 	if (!lun || !lun->lun_se_dev)
640 		return -ENODEV;
641 
642 	return core_alua_show_secondary_write_metadata(lun, page);
643 }
644 
645 static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
646 		struct config_item *item, const char *page, size_t count)
647 {
648 	struct se_lun *lun = item_to_lun(item);
649 
650 	if (!lun || !lun->lun_se_dev)
651 		return -ENODEV;
652 
653 	return core_alua_store_secondary_write_metadata(lun, page, count);
654 }
655 
656 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
657 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
658 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
659 CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
660 
661 static struct configfs_attribute *target_fabric_port_attrs[] = {
662 	&target_fabric_port_attr_alua_tg_pt_gp,
663 	&target_fabric_port_attr_alua_tg_pt_offline,
664 	&target_fabric_port_attr_alua_tg_pt_status,
665 	&target_fabric_port_attr_alua_tg_pt_write_md,
666 	NULL,
667 };
668 
669 static int target_fabric_port_link(
670 	struct config_item *lun_ci,
671 	struct config_item *se_dev_ci)
672 {
673 	struct config_item *tpg_ci;
674 	struct se_lun *lun = container_of(to_config_group(lun_ci),
675 				struct se_lun, lun_group);
676 	struct se_portal_group *se_tpg;
677 	struct se_device *dev =
678 		container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
679 	struct target_fabric_configfs *tf;
680 	int ret;
681 
682 	if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
683 		pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
684 			" %p to struct se_device: %p\n", se_dev_ci, dev);
685 		return -EFAULT;
686 	}
687 
688 	if (!(dev->dev_flags & DF_CONFIGURED)) {
689 		pr_err("se_device not configured yet, cannot port link\n");
690 		return -ENODEV;
691 	}
692 
693 	tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
694 	se_tpg = container_of(to_config_group(tpg_ci),
695 				struct se_portal_group, tpg_group);
696 	tf = se_tpg->se_tpg_wwn->wwn_tf;
697 
698 	if (lun->lun_se_dev !=  NULL) {
699 		pr_err("Port Symlink already exists\n");
700 		return -EEXIST;
701 	}
702 
703 	ret = core_dev_add_lun(se_tpg, dev, lun);
704 	if (ret) {
705 		pr_err("core_dev_add_lun() failed: %d\n", ret);
706 		goto out;
707 	}
708 
709 	if (tf->tf_ops->fabric_post_link) {
710 		/*
711 		 * Call the optional fabric_post_link() to allow a
712 		 * fabric module to setup any additional state once
713 		 * core_dev_add_lun() has been called..
714 		 */
715 		tf->tf_ops->fabric_post_link(se_tpg, lun);
716 	}
717 
718 	return 0;
719 out:
720 	return ret;
721 }
722 
723 static int target_fabric_port_unlink(
724 	struct config_item *lun_ci,
725 	struct config_item *se_dev_ci)
726 {
727 	struct se_lun *lun = container_of(to_config_group(lun_ci),
728 				struct se_lun, lun_group);
729 	struct se_portal_group *se_tpg = lun->lun_tpg;
730 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
731 
732 	if (tf->tf_ops->fabric_pre_unlink) {
733 		/*
734 		 * Call the optional fabric_pre_unlink() to allow a
735 		 * fabric module to release any additional stat before
736 		 * core_dev_del_lun() is called.
737 		*/
738 		tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
739 	}
740 
741 	core_dev_del_lun(se_tpg, lun);
742 	return 0;
743 }
744 
745 static void target_fabric_port_release(struct config_item *item)
746 {
747 	struct se_lun *lun = container_of(to_config_group(item),
748 					  struct se_lun, lun_group);
749 
750 	kfree_rcu(lun, rcu_head);
751 }
752 
753 static struct configfs_item_operations target_fabric_port_item_ops = {
754 	.release		= target_fabric_port_release,
755 	.allow_link		= target_fabric_port_link,
756 	.drop_link		= target_fabric_port_unlink,
757 };
758 
759 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
760 
761 /* End of tfc_tpg_port_cit */
762 
763 /* Start of tfc_tpg_port_stat_cit */
764 
765 static struct config_group *target_core_port_stat_mkdir(
766 	struct config_group *group,
767 	const char *name)
768 {
769 	return ERR_PTR(-ENOSYS);
770 }
771 
772 static void target_core_port_stat_rmdir(
773 	struct config_group *group,
774 	struct config_item *item)
775 {
776 	return;
777 }
778 
779 static struct configfs_group_operations target_fabric_port_stat_group_ops = {
780 	.make_group		= target_core_port_stat_mkdir,
781 	.drop_item		= target_core_port_stat_rmdir,
782 };
783 
784 TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
785 
786 /* End of tfc_tpg_port_stat_cit */
787 
788 /* Start of tfc_tpg_lun_cit */
789 
790 static struct config_group *target_fabric_make_lun(
791 	struct config_group *group,
792 	const char *name)
793 {
794 	struct se_lun *lun;
795 	struct se_portal_group *se_tpg = container_of(group,
796 			struct se_portal_group, tpg_lun_group);
797 	struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
798 	struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
799 	unsigned long long unpacked_lun;
800 	int errno;
801 
802 	if (strstr(name, "lun_") != name) {
803 		pr_err("Unable to locate \'_\" in"
804 				" \"lun_$LUN_NUMBER\"\n");
805 		return ERR_PTR(-EINVAL);
806 	}
807 	errno = kstrtoull(name + 4, 0, &unpacked_lun);
808 	if (errno)
809 		return ERR_PTR(errno);
810 
811 	lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
812 	if (IS_ERR(lun))
813 		return ERR_CAST(lun);
814 
815 	lun_cg = &lun->lun_group;
816 	lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
817 				GFP_KERNEL);
818 	if (!lun_cg->default_groups) {
819 		pr_err("Unable to allocate lun_cg->default_groups\n");
820 		kfree(lun);
821 		return ERR_PTR(-ENOMEM);
822 	}
823 
824 	config_group_init_type_name(&lun->lun_group, name,
825 			&tf->tf_tpg_port_cit);
826 	config_group_init_type_name(&lun->port_stat_grps.stat_group,
827 			"statistics", &tf->tf_tpg_port_stat_cit);
828 	lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
829 	lun_cg->default_groups[1] = NULL;
830 
831 	port_stat_grp = &lun->port_stat_grps.stat_group;
832 	port_stat_grp->default_groups =  kzalloc(sizeof(struct config_group *) * 4,
833 				GFP_KERNEL);
834 	if (!port_stat_grp->default_groups) {
835 		pr_err("Unable to allocate port_stat_grp->default_groups\n");
836 		kfree(lun_cg->default_groups);
837 		kfree(lun);
838 		return ERR_PTR(-ENOMEM);
839 	}
840 	target_stat_setup_port_default_groups(lun);
841 
842 	return &lun->lun_group;
843 }
844 
845 static void target_fabric_drop_lun(
846 	struct config_group *group,
847 	struct config_item *item)
848 {
849 	struct se_lun *lun = container_of(to_config_group(item),
850 				struct se_lun, lun_group);
851 	struct config_item *df_item;
852 	struct config_group *lun_cg, *port_stat_grp;
853 	int i;
854 
855 	port_stat_grp = &lun->port_stat_grps.stat_group;
856 	for (i = 0; port_stat_grp->default_groups[i]; i++) {
857 		df_item = &port_stat_grp->default_groups[i]->cg_item;
858 		port_stat_grp->default_groups[i] = NULL;
859 		config_item_put(df_item);
860 	}
861 	kfree(port_stat_grp->default_groups);
862 
863 	lun_cg = &lun->lun_group;
864 	for (i = 0; lun_cg->default_groups[i]; i++) {
865 		df_item = &lun_cg->default_groups[i]->cg_item;
866 		lun_cg->default_groups[i] = NULL;
867 		config_item_put(df_item);
868 	}
869 	kfree(lun_cg->default_groups);
870 
871 	config_item_put(item);
872 }
873 
874 static struct configfs_group_operations target_fabric_lun_group_ops = {
875 	.make_group	= &target_fabric_make_lun,
876 	.drop_item	= &target_fabric_drop_lun,
877 };
878 
879 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
880 
881 /* End of tfc_tpg_lun_cit */
882 
883 TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
884 TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
885 TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
886 
887 /* Start of tfc_tpg_base_cit */
888 
889 static void target_fabric_tpg_release(struct config_item *item)
890 {
891 	struct se_portal_group *se_tpg = container_of(to_config_group(item),
892 			struct se_portal_group, tpg_group);
893 	struct se_wwn *wwn = se_tpg->se_tpg_wwn;
894 	struct target_fabric_configfs *tf = wwn->wwn_tf;
895 
896 	tf->tf_ops->fabric_drop_tpg(se_tpg);
897 }
898 
899 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
900 	.release		= target_fabric_tpg_release,
901 };
902 
903 TF_CIT_SETUP_DRV(tpg_base, &target_fabric_tpg_base_item_ops, NULL);
904 
905 /* End of tfc_tpg_base_cit */
906 
907 /* Start of tfc_tpg_cit */
908 
909 static struct config_group *target_fabric_make_tpg(
910 	struct config_group *group,
911 	const char *name)
912 {
913 	struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
914 	struct target_fabric_configfs *tf = wwn->wwn_tf;
915 	struct se_portal_group *se_tpg;
916 
917 	if (!tf->tf_ops->fabric_make_tpg) {
918 		pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
919 		return ERR_PTR(-ENOSYS);
920 	}
921 
922 	se_tpg = tf->tf_ops->fabric_make_tpg(wwn, group, name);
923 	if (!se_tpg || IS_ERR(se_tpg))
924 		return ERR_PTR(-EINVAL);
925 	/*
926 	 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
927 	 */
928 	se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
929 	se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
930 	se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
931 	se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
932 	se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
933 	se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
934 	se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
935 	se_tpg->tpg_group.default_groups[6] = NULL;
936 
937 	config_group_init_type_name(&se_tpg->tpg_group, name,
938 			&tf->tf_tpg_base_cit);
939 	config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
940 			&tf->tf_tpg_lun_cit);
941 	config_group_init_type_name(&se_tpg->tpg_np_group, "np",
942 			&tf->tf_tpg_np_cit);
943 	config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
944 			&tf->tf_tpg_nacl_cit);
945 	config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
946 			&tf->tf_tpg_attrib_cit);
947 	config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
948 			&tf->tf_tpg_auth_cit);
949 	config_group_init_type_name(&se_tpg->tpg_param_group, "param",
950 			&tf->tf_tpg_param_cit);
951 
952 	return &se_tpg->tpg_group;
953 }
954 
955 static void target_fabric_drop_tpg(
956 	struct config_group *group,
957 	struct config_item *item)
958 {
959 	struct se_portal_group *se_tpg = container_of(to_config_group(item),
960 				struct se_portal_group, tpg_group);
961 	struct config_group *tpg_cg = &se_tpg->tpg_group;
962 	struct config_item *df_item;
963 	int i;
964 	/*
965 	 * Release default groups, but do not release tpg_cg->default_groups
966 	 * memory as it is statically allocated at se_tpg->tpg_default_groups.
967 	 */
968 	for (i = 0; tpg_cg->default_groups[i]; i++) {
969 		df_item = &tpg_cg->default_groups[i]->cg_item;
970 		tpg_cg->default_groups[i] = NULL;
971 		config_item_put(df_item);
972 	}
973 
974 	config_item_put(item);
975 }
976 
977 static void target_fabric_release_wwn(struct config_item *item)
978 {
979 	struct se_wwn *wwn = container_of(to_config_group(item),
980 				struct se_wwn, wwn_group);
981 	struct target_fabric_configfs *tf = wwn->wwn_tf;
982 
983 	tf->tf_ops->fabric_drop_wwn(wwn);
984 }
985 
986 static struct configfs_item_operations target_fabric_tpg_item_ops = {
987 	.release	= target_fabric_release_wwn,
988 };
989 
990 static struct configfs_group_operations target_fabric_tpg_group_ops = {
991 	.make_group	= target_fabric_make_tpg,
992 	.drop_item	= target_fabric_drop_tpg,
993 };
994 
995 TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
996 		NULL);
997 
998 /* End of tfc_tpg_cit */
999 
1000 /* Start of tfc_wwn_fabric_stats_cit */
1001 /*
1002  * This is used as a placeholder for struct se_wwn->fabric_stat_group
1003  * to allow fabrics access to ->fabric_stat_group->default_groups[]
1004  */
1005 TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1006 
1007 /* End of tfc_wwn_fabric_stats_cit */
1008 
1009 /* Start of tfc_wwn_cit */
1010 
1011 static struct config_group *target_fabric_make_wwn(
1012 	struct config_group *group,
1013 	const char *name)
1014 {
1015 	struct target_fabric_configfs *tf = container_of(group,
1016 				struct target_fabric_configfs, tf_group);
1017 	struct se_wwn *wwn;
1018 
1019 	if (!tf->tf_ops->fabric_make_wwn) {
1020 		pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1021 		return ERR_PTR(-ENOSYS);
1022 	}
1023 
1024 	wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
1025 	if (!wwn || IS_ERR(wwn))
1026 		return ERR_PTR(-EINVAL);
1027 
1028 	wwn->wwn_tf = tf;
1029 	/*
1030 	 * Setup default groups from pre-allocated wwn->wwn_default_groups
1031 	 */
1032 	wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1033 	wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1034 	wwn->wwn_group.default_groups[1] = NULL;
1035 
1036 	config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1037 	config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1038 			&tf->tf_wwn_fabric_stats_cit);
1039 
1040 	return &wwn->wwn_group;
1041 }
1042 
1043 static void target_fabric_drop_wwn(
1044 	struct config_group *group,
1045 	struct config_item *item)
1046 {
1047 	struct se_wwn *wwn = container_of(to_config_group(item),
1048 				struct se_wwn, wwn_group);
1049 	struct config_item *df_item;
1050 	struct config_group *cg = &wwn->wwn_group;
1051 	int i;
1052 
1053 	for (i = 0; cg->default_groups[i]; i++) {
1054 		df_item = &cg->default_groups[i]->cg_item;
1055 		cg->default_groups[i] = NULL;
1056 		config_item_put(df_item);
1057 	}
1058 
1059 	config_item_put(item);
1060 }
1061 
1062 static struct configfs_group_operations target_fabric_wwn_group_ops = {
1063 	.make_group	= target_fabric_make_wwn,
1064 	.drop_item	= target_fabric_drop_wwn,
1065 };
1066 
1067 TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
1068 TF_CIT_SETUP_DRV(discovery, NULL, NULL);
1069 
1070 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1071 {
1072 	target_fabric_setup_discovery_cit(tf);
1073 	target_fabric_setup_wwn_cit(tf);
1074 	target_fabric_setup_wwn_fabric_stats_cit(tf);
1075 	target_fabric_setup_tpg_cit(tf);
1076 	target_fabric_setup_tpg_base_cit(tf);
1077 	target_fabric_setup_tpg_port_cit(tf);
1078 	target_fabric_setup_tpg_port_stat_cit(tf);
1079 	target_fabric_setup_tpg_lun_cit(tf);
1080 	target_fabric_setup_tpg_np_cit(tf);
1081 	target_fabric_setup_tpg_np_base_cit(tf);
1082 	target_fabric_setup_tpg_attrib_cit(tf);
1083 	target_fabric_setup_tpg_auth_cit(tf);
1084 	target_fabric_setup_tpg_param_cit(tf);
1085 	target_fabric_setup_tpg_nacl_cit(tf);
1086 	target_fabric_setup_tpg_nacl_base_cit(tf);
1087 	target_fabric_setup_tpg_nacl_attrib_cit(tf);
1088 	target_fabric_setup_tpg_nacl_auth_cit(tf);
1089 	target_fabric_setup_tpg_nacl_param_cit(tf);
1090 	target_fabric_setup_tpg_nacl_stat_cit(tf);
1091 	target_fabric_setup_tpg_mappedlun_cit(tf);
1092 	target_fabric_setup_tpg_mappedlun_stat_cit(tf);
1093 
1094 	return 0;
1095 }
1096