Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3* Filename: target_core_fabric_configfs.c
4 *
5 * This file contains generic fabric module configfs infrastructure for
6 * TCM v4.x code
7 *
8 * (c) Copyright 2010-2013 Datera, Inc.
9 *
10 * Nicholas A. Bellinger <nab@linux-iscsi.org>
11*
12 ****************************************************************************/
13
14#include <linux/kstrtox.h>
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/utsname.h>
18#include <linux/init.h>
19#include <linux/fs.h>
20#include <linux/namei.h>
21#include <linux/slab.h>
22#include <linux/types.h>
23#include <linux/delay.h>
24#include <linux/unistd.h>
25#include <linux/string.h>
26#include <linux/syscalls.h>
27#include <linux/configfs.h>
28
29#include <target/target_core_base.h>
30#include <target/target_core_backend.h>
31#include <target/target_core_fabric.h>
32
33#include "target_core_internal.h"
34#include "target_core_alua.h"
35#include "target_core_pr.h"
36
37#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
38static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
39{ \
40 struct config_item_type *cit = &tf->tf_##_name##_cit; \
41 \
42 cit->ct_item_ops = _item_ops; \
43 cit->ct_group_ops = _group_ops; \
44 cit->ct_attrs = _attrs; \
45 cit->ct_owner = tf->tf_ops->module; \
46 pr_debug("Setup generic %s\n", __stringify(_name)); \
47}
48
49#define TF_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
50static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
51{ \
52 struct config_item_type *cit = &tf->tf_##_name##_cit; \
53 struct configfs_attribute **attrs = tf->tf_ops->tfc_##_name##_attrs; \
54 \
55 cit->ct_item_ops = _item_ops; \
56 cit->ct_group_ops = _group_ops; \
57 cit->ct_attrs = attrs; \
58 cit->ct_owner = tf->tf_ops->module; \
59 pr_debug("Setup generic %s\n", __stringify(_name)); \
60}
61
62static struct configfs_item_operations target_fabric_port_item_ops;
63
64/* Start of tfc_tpg_mappedlun_cit */
65
66static int target_fabric_mappedlun_link(
67 struct config_item *lun_acl_ci,
68 struct config_item *lun_ci)
69{
70 struct se_dev_entry *deve;
71 struct se_lun *lun;
72 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
73 struct se_lun_acl, se_lun_group);
74 struct se_portal_group *se_tpg;
75 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
76 bool lun_access_ro;
77
78 if (!lun_ci->ci_type ||
79 lun_ci->ci_type->ct_item_ops != &target_fabric_port_item_ops) {
80 pr_err("Bad lun_ci, not a valid lun_ci pointer: %p\n", lun_ci);
81 return -EFAULT;
82 }
83 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
84
85 /*
86 * Ensure that the source port exists
87 */
88 if (!lun->lun_se_dev) {
89 pr_err("Source se_lun->lun_se_dev does not exist\n");
90 return -EINVAL;
91 }
92 if (lun->lun_shutdown) {
93 pr_err("Unable to create mappedlun symlink because"
94 " lun->lun_shutdown=true\n");
95 return -EINVAL;
96 }
97 se_tpg = lun->lun_tpg;
98
99 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
100 tpg_ci = &nacl_ci->ci_group->cg_item;
101 wwn_ci = &tpg_ci->ci_group->cg_item;
102 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
103 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
104 /*
105 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
106 */
107 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
108 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
109 config_item_name(wwn_ci));
110 return -EINVAL;
111 }
112 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
113 pr_err("Illegal Initiator ACL Symlink outside of %s"
114 " TPGT: %s\n", config_item_name(wwn_ci),
115 config_item_name(tpg_ci));
116 return -EINVAL;
117 }
118 /*
119 * If this struct se_node_acl was dynamically generated with
120 * tpg_1/attrib/generate_node_acls=1, use the existing
121 * deve->lun_access_ro value, which will be true when
122 * tpg_1/attrib/demo_mode_write_protect=1
123 */
124 rcu_read_lock();
125 deve = target_nacl_find_deve(lacl->se_lun_nacl, lacl->mapped_lun);
126 if (deve)
127 lun_access_ro = deve->lun_access_ro;
128 else
129 lun_access_ro =
130 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
131 se_tpg)) ? true : false;
132 rcu_read_unlock();
133 /*
134 * Determine the actual mapped LUN value user wants..
135 *
136 * This value is what the SCSI Initiator actually sees the
137 * $FABRIC/$WWPN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
138 */
139 return core_dev_add_initiator_node_lun_acl(se_tpg, lacl, lun, lun_access_ro);
140}
141
142static void target_fabric_mappedlun_unlink(
143 struct config_item *lun_acl_ci,
144 struct config_item *lun_ci)
145{
146 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
147 struct se_lun_acl, se_lun_group);
148 struct se_lun *lun = container_of(to_config_group(lun_ci),
149 struct se_lun, lun_group);
150
151 core_dev_del_initiator_node_lun_acl(lun, lacl);
152}
153
154static struct se_lun_acl *item_to_lun_acl(struct config_item *item)
155{
156 return container_of(to_config_group(item), struct se_lun_acl,
157 se_lun_group);
158}
159
160static ssize_t target_fabric_mappedlun_write_protect_show(
161 struct config_item *item, char *page)
162{
163 struct se_lun_acl *lacl = item_to_lun_acl(item);
164 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
165 struct se_dev_entry *deve;
166 ssize_t len = 0;
167
168 rcu_read_lock();
169 deve = target_nacl_find_deve(se_nacl, lacl->mapped_lun);
170 if (deve) {
171 len = sprintf(page, "%d\n", deve->lun_access_ro);
172 }
173 rcu_read_unlock();
174
175 return len;
176}
177
178static 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 wp;
185 int ret;
186
187 ret = kstrtoul(page, 0, &wp);
188 if (ret)
189 return ret;
190
191 if ((wp != 1) && (wp != 0))
192 return -EINVAL;
193
194 /* wp=1 means lun_access_ro=true */
195 core_update_device_list_access(lacl->mapped_lun, wp, lacl->se_lun_nacl);
196
197 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
198 " Mapped LUN: %llu Write Protect bit to %s\n",
199 se_tpg->se_tpg_tfo->fabric_name,
200 se_nacl->initiatorname, lacl->mapped_lun, (wp) ? "ON" : "OFF");
201
202 return count;
203
204}
205
206CONFIGFS_ATTR(target_fabric_mappedlun_, write_protect);
207
208static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
209 &target_fabric_mappedlun_attr_write_protect,
210 NULL,
211};
212
213static void target_fabric_mappedlun_release(struct config_item *item)
214{
215 struct se_lun_acl *lacl = container_of(to_config_group(item),
216 struct se_lun_acl, se_lun_group);
217 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
218
219 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
220}
221
222static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
223 .release = target_fabric_mappedlun_release,
224 .allow_link = target_fabric_mappedlun_link,
225 .drop_link = target_fabric_mappedlun_unlink,
226};
227
228TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
229 target_fabric_mappedlun_attrs);
230
231/* End of tfc_tpg_mappedlun_cit */
232
233/* Start of tfc_tpg_mappedlun_port_cit */
234
235static struct config_group *target_core_mappedlun_stat_mkdir(
236 struct config_group *group,
237 const char *name)
238{
239 return ERR_PTR(-ENOSYS);
240}
241
242static void target_core_mappedlun_stat_rmdir(
243 struct config_group *group,
244 struct config_item *item)
245{
246 return;
247}
248
249static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
250 .make_group = target_core_mappedlun_stat_mkdir,
251 .drop_item = target_core_mappedlun_stat_rmdir,
252};
253
254TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
255 NULL);
256
257/* End of tfc_tpg_mappedlun_port_cit */
258
259TF_CIT_SETUP_DRV(tpg_nacl_attrib, NULL, NULL);
260TF_CIT_SETUP_DRV(tpg_nacl_auth, NULL, NULL);
261TF_CIT_SETUP_DRV(tpg_nacl_param, NULL, NULL);
262
263/* Start of tfc_tpg_nacl_base_cit */
264
265static struct config_group *target_fabric_make_mappedlun(
266 struct config_group *group,
267 const char *name)
268{
269 struct se_node_acl *se_nacl = container_of(group,
270 struct se_node_acl, acl_group);
271 struct se_portal_group *se_tpg = se_nacl->se_tpg;
272 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
273 struct se_lun_acl *lacl = NULL;
274 char *buf;
275 unsigned long long mapped_lun;
276 int ret = 0;
277
278 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
279 if (!buf) {
280 pr_err("Unable to allocate memory for name buf\n");
281 return ERR_PTR(-ENOMEM);
282 }
283 snprintf(buf, strlen(name) + 1, "%s", name);
284 /*
285 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
286 */
287 if (strstr(buf, "lun_") != buf) {
288 pr_err("Unable to locate \"lun_\" from buf: %s"
289 " name: %s\n", buf, name);
290 ret = -EINVAL;
291 goto out;
292 }
293 /*
294 * Determine the Mapped LUN value. This is what the SCSI Initiator
295 * Port will actually see.
296 */
297 ret = kstrtoull(buf + 4, 0, &mapped_lun);
298 if (ret)
299 goto out;
300
301 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
302 mapped_lun, &ret);
303 if (!lacl) {
304 ret = -EINVAL;
305 goto out;
306 }
307
308 config_group_init_type_name(&lacl->se_lun_group, name,
309 &tf->tf_tpg_mappedlun_cit);
310
311 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
312 "statistics", &tf->tf_tpg_mappedlun_stat_cit);
313 configfs_add_default_group(&lacl->ml_stat_grps.stat_group,
314 &lacl->se_lun_group);
315
316 target_stat_setup_mappedlun_default_groups(lacl);
317
318 kfree(buf);
319 return &lacl->se_lun_group;
320out:
321 kfree(lacl);
322 kfree(buf);
323 return ERR_PTR(ret);
324}
325
326static void target_fabric_drop_mappedlun(
327 struct config_group *group,
328 struct config_item *item)
329{
330 struct se_lun_acl *lacl = container_of(to_config_group(item),
331 struct se_lun_acl, se_lun_group);
332
333 configfs_remove_default_groups(&lacl->ml_stat_grps.stat_group);
334 configfs_remove_default_groups(&lacl->se_lun_group);
335
336 config_item_put(item);
337}
338
339static void target_fabric_nacl_base_release(struct config_item *item)
340{
341 struct se_node_acl *se_nacl = container_of(to_config_group(item),
342 struct se_node_acl, acl_group);
343
344 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
345 core_tpg_del_initiator_node_acl(se_nacl);
346}
347
348static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
349 .release = target_fabric_nacl_base_release,
350};
351
352static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
353 .make_group = target_fabric_make_mappedlun,
354 .drop_item = target_fabric_drop_mappedlun,
355};
356
357TF_CIT_SETUP_DRV(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
358 &target_fabric_nacl_base_group_ops);
359
360/* End of tfc_tpg_nacl_base_cit */
361
362/* Start of tfc_node_fabric_stats_cit */
363/*
364 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
365 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
366 */
367TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
368
369/* End of tfc_wwn_fabric_stats_cit */
370
371/* Start of tfc_tpg_nacl_cit */
372
373static struct config_group *target_fabric_make_nodeacl(
374 struct config_group *group,
375 const char *name)
376{
377 struct se_portal_group *se_tpg = container_of(group,
378 struct se_portal_group, tpg_acl_group);
379 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
380 struct se_node_acl *se_nacl;
381
382 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, name);
383 if (IS_ERR(se_nacl))
384 return ERR_CAST(se_nacl);
385
386 config_group_init_type_name(&se_nacl->acl_group, name,
387 &tf->tf_tpg_nacl_base_cit);
388
389 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
390 &tf->tf_tpg_nacl_attrib_cit);
391 configfs_add_default_group(&se_nacl->acl_attrib_group,
392 &se_nacl->acl_group);
393
394 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
395 &tf->tf_tpg_nacl_auth_cit);
396 configfs_add_default_group(&se_nacl->acl_auth_group,
397 &se_nacl->acl_group);
398
399 config_group_init_type_name(&se_nacl->acl_param_group, "param",
400 &tf->tf_tpg_nacl_param_cit);
401 configfs_add_default_group(&se_nacl->acl_param_group,
402 &se_nacl->acl_group);
403
404 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
405 "fabric_statistics", &tf->tf_tpg_nacl_stat_cit);
406 configfs_add_default_group(&se_nacl->acl_fabric_stat_group,
407 &se_nacl->acl_group);
408
409 if (tf->tf_ops->fabric_init_nodeacl) {
410 int ret = tf->tf_ops->fabric_init_nodeacl(se_nacl, name);
411 if (ret) {
412 configfs_remove_default_groups(&se_nacl->acl_fabric_stat_group);
413 core_tpg_del_initiator_node_acl(se_nacl);
414 return ERR_PTR(ret);
415 }
416 }
417
418 return &se_nacl->acl_group;
419}
420
421static void target_fabric_drop_nodeacl(
422 struct config_group *group,
423 struct config_item *item)
424{
425 struct se_node_acl *se_nacl = container_of(to_config_group(item),
426 struct se_node_acl, acl_group);
427
428 configfs_remove_default_groups(&se_nacl->acl_group);
429
430 /*
431 * struct se_node_acl free is done in target_fabric_nacl_base_release()
432 */
433 config_item_put(item);
434}
435
436static struct configfs_group_operations target_fabric_nacl_group_ops = {
437 .make_group = target_fabric_make_nodeacl,
438 .drop_item = target_fabric_drop_nodeacl,
439};
440
441TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
442
443/* End of tfc_tpg_nacl_cit */
444
445/* Start of tfc_tpg_np_base_cit */
446
447static void target_fabric_np_base_release(struct config_item *item)
448{
449 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
450 struct se_tpg_np, tpg_np_group);
451 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
452 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
453
454 tf->tf_ops->fabric_drop_np(se_tpg_np);
455}
456
457static struct configfs_item_operations target_fabric_np_base_item_ops = {
458 .release = target_fabric_np_base_release,
459};
460
461TF_CIT_SETUP_DRV(tpg_np_base, &target_fabric_np_base_item_ops, NULL);
462
463/* End of tfc_tpg_np_base_cit */
464
465/* Start of tfc_tpg_np_cit */
466
467static struct config_group *target_fabric_make_np(
468 struct config_group *group,
469 const char *name)
470{
471 struct se_portal_group *se_tpg = container_of(group,
472 struct se_portal_group, tpg_np_group);
473 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
474 struct se_tpg_np *se_tpg_np;
475
476 if (!tf->tf_ops->fabric_make_np) {
477 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
478 return ERR_PTR(-ENOSYS);
479 }
480
481 se_tpg_np = tf->tf_ops->fabric_make_np(se_tpg, group, name);
482 if (!se_tpg_np || IS_ERR(se_tpg_np))
483 return ERR_PTR(-EINVAL);
484
485 se_tpg_np->tpg_np_parent = se_tpg;
486 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
487 &tf->tf_tpg_np_base_cit);
488
489 return &se_tpg_np->tpg_np_group;
490}
491
492static void target_fabric_drop_np(
493 struct config_group *group,
494 struct config_item *item)
495{
496 /*
497 * struct se_tpg_np is released via target_fabric_np_base_release()
498 */
499 config_item_put(item);
500}
501
502static struct configfs_group_operations target_fabric_np_group_ops = {
503 .make_group = &target_fabric_make_np,
504 .drop_item = &target_fabric_drop_np,
505};
506
507TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
508
509/* End of tfc_tpg_np_cit */
510
511/* Start of tfc_tpg_port_cit */
512
513static struct se_lun *item_to_lun(struct config_item *item)
514{
515 return container_of(to_config_group(item), struct se_lun,
516 lun_group);
517}
518
519static ssize_t target_fabric_port_alua_tg_pt_gp_show(struct config_item *item,
520 char *page)
521{
522 struct se_lun *lun = item_to_lun(item);
523
524 if (!lun->lun_se_dev)
525 return -ENODEV;
526
527 return core_alua_show_tg_pt_gp_info(lun, page);
528}
529
530static ssize_t target_fabric_port_alua_tg_pt_gp_store(struct config_item *item,
531 const char *page, size_t count)
532{
533 struct se_lun *lun = item_to_lun(item);
534
535 if (!lun->lun_se_dev)
536 return -ENODEV;
537
538 return core_alua_store_tg_pt_gp_info(lun, page, count);
539}
540
541static ssize_t target_fabric_port_alua_tg_pt_offline_show(
542 struct config_item *item, char *page)
543{
544 struct se_lun *lun = item_to_lun(item);
545
546 if (!lun->lun_se_dev)
547 return -ENODEV;
548
549 return core_alua_show_offline_bit(lun, page);
550}
551
552static ssize_t target_fabric_port_alua_tg_pt_offline_store(
553 struct config_item *item, const char *page, size_t count)
554{
555 struct se_lun *lun = item_to_lun(item);
556
557 if (!lun->lun_se_dev)
558 return -ENODEV;
559
560 return core_alua_store_offline_bit(lun, page, count);
561}
562
563static ssize_t target_fabric_port_alua_tg_pt_status_show(
564 struct config_item *item, char *page)
565{
566 struct se_lun *lun = item_to_lun(item);
567
568 if (!lun->lun_se_dev)
569 return -ENODEV;
570
571 return core_alua_show_secondary_status(lun, page);
572}
573
574static ssize_t target_fabric_port_alua_tg_pt_status_store(
575 struct config_item *item, const char *page, size_t count)
576{
577 struct se_lun *lun = item_to_lun(item);
578
579 if (!lun->lun_se_dev)
580 return -ENODEV;
581
582 return core_alua_store_secondary_status(lun, page, count);
583}
584
585static ssize_t target_fabric_port_alua_tg_pt_write_md_show(
586 struct config_item *item, char *page)
587{
588 struct se_lun *lun = item_to_lun(item);
589
590 if (!lun->lun_se_dev)
591 return -ENODEV;
592
593 return core_alua_show_secondary_write_metadata(lun, page);
594}
595
596static ssize_t target_fabric_port_alua_tg_pt_write_md_store(
597 struct config_item *item, const char *page, size_t count)
598{
599 struct se_lun *lun = item_to_lun(item);
600
601 if (!lun->lun_se_dev)
602 return -ENODEV;
603
604 return core_alua_store_secondary_write_metadata(lun, page, count);
605}
606
607CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_gp);
608CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_offline);
609CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_status);
610CONFIGFS_ATTR(target_fabric_port_, alua_tg_pt_write_md);
611
612static struct configfs_attribute *target_fabric_port_attrs[] = {
613 &target_fabric_port_attr_alua_tg_pt_gp,
614 &target_fabric_port_attr_alua_tg_pt_offline,
615 &target_fabric_port_attr_alua_tg_pt_status,
616 &target_fabric_port_attr_alua_tg_pt_write_md,
617 NULL,
618};
619
620static int target_fabric_port_link(
621 struct config_item *lun_ci,
622 struct config_item *se_dev_ci)
623{
624 struct config_item *tpg_ci;
625 struct se_lun *lun = container_of(to_config_group(lun_ci),
626 struct se_lun, lun_group);
627 struct se_portal_group *se_tpg;
628 struct se_device *dev;
629 struct target_fabric_configfs *tf;
630 int ret;
631
632 if (!se_dev_ci->ci_type ||
633 se_dev_ci->ci_type->ct_item_ops != &target_core_dev_item_ops) {
634 pr_err("Bad se_dev_ci, not a valid se_dev_ci pointer: %p\n", se_dev_ci);
635 return -EFAULT;
636 }
637 dev = container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
638
639 if (!target_dev_configured(dev)) {
640 pr_err("se_device not configured yet, cannot port link\n");
641 return -ENODEV;
642 }
643
644 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
645 se_tpg = container_of(to_config_group(tpg_ci),
646 struct se_portal_group, tpg_group);
647 tf = se_tpg->se_tpg_wwn->wwn_tf;
648
649 if (lun->lun_se_dev != NULL) {
650 pr_err("Port Symlink already exists\n");
651 return -EEXIST;
652 }
653
654 ret = core_dev_add_lun(se_tpg, dev, lun);
655 if (ret) {
656 pr_err("core_dev_add_lun() failed: %d\n", ret);
657 goto out;
658 }
659
660 if (tf->tf_ops->fabric_post_link) {
661 /*
662 * Call the optional fabric_post_link() to allow a
663 * fabric module to setup any additional state once
664 * core_dev_add_lun() has been called..
665 */
666 tf->tf_ops->fabric_post_link(se_tpg, lun);
667 }
668
669 return 0;
670out:
671 return ret;
672}
673
674static void target_fabric_port_unlink(
675 struct config_item *lun_ci,
676 struct config_item *se_dev_ci)
677{
678 struct se_lun *lun = container_of(to_config_group(lun_ci),
679 struct se_lun, lun_group);
680 struct se_portal_group *se_tpg = lun->lun_tpg;
681 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
682
683 if (tf->tf_ops->fabric_pre_unlink) {
684 /*
685 * Call the optional fabric_pre_unlink() to allow a
686 * fabric module to release any additional stat before
687 * core_dev_del_lun() is called.
688 */
689 tf->tf_ops->fabric_pre_unlink(se_tpg, lun);
690 }
691
692 core_dev_del_lun(se_tpg, lun);
693}
694
695static void target_fabric_port_release(struct config_item *item)
696{
697 struct se_lun *lun = container_of(to_config_group(item),
698 struct se_lun, lun_group);
699
700 kfree_rcu(lun, rcu_head);
701}
702
703static struct configfs_item_operations target_fabric_port_item_ops = {
704 .release = target_fabric_port_release,
705 .allow_link = target_fabric_port_link,
706 .drop_link = target_fabric_port_unlink,
707};
708
709TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
710
711/* End of tfc_tpg_port_cit */
712
713/* Start of tfc_tpg_port_stat_cit */
714
715static struct config_group *target_core_port_stat_mkdir(
716 struct config_group *group,
717 const char *name)
718{
719 return ERR_PTR(-ENOSYS);
720}
721
722static void target_core_port_stat_rmdir(
723 struct config_group *group,
724 struct config_item *item)
725{
726 return;
727}
728
729static struct configfs_group_operations target_fabric_port_stat_group_ops = {
730 .make_group = target_core_port_stat_mkdir,
731 .drop_item = target_core_port_stat_rmdir,
732};
733
734TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
735
736/* End of tfc_tpg_port_stat_cit */
737
738/* Start of tfc_tpg_lun_cit */
739
740static struct config_group *target_fabric_make_lun(
741 struct config_group *group,
742 const char *name)
743{
744 struct se_lun *lun;
745 struct se_portal_group *se_tpg = container_of(group,
746 struct se_portal_group, tpg_lun_group);
747 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
748 unsigned long long unpacked_lun;
749 int errno;
750
751 if (strstr(name, "lun_") != name) {
752 pr_err("Unable to locate \'_\" in"
753 " \"lun_$LUN_NUMBER\"\n");
754 return ERR_PTR(-EINVAL);
755 }
756 errno = kstrtoull(name + 4, 0, &unpacked_lun);
757 if (errno)
758 return ERR_PTR(errno);
759
760 lun = core_tpg_alloc_lun(se_tpg, unpacked_lun);
761 if (IS_ERR(lun))
762 return ERR_CAST(lun);
763
764 config_group_init_type_name(&lun->lun_group, name,
765 &tf->tf_tpg_port_cit);
766
767 config_group_init_type_name(&lun->port_stat_grps.stat_group,
768 "statistics", &tf->tf_tpg_port_stat_cit);
769 configfs_add_default_group(&lun->port_stat_grps.stat_group,
770 &lun->lun_group);
771
772 target_stat_setup_port_default_groups(lun);
773
774 return &lun->lun_group;
775}
776
777static void target_fabric_drop_lun(
778 struct config_group *group,
779 struct config_item *item)
780{
781 struct se_lun *lun = container_of(to_config_group(item),
782 struct se_lun, lun_group);
783
784 configfs_remove_default_groups(&lun->port_stat_grps.stat_group);
785 configfs_remove_default_groups(&lun->lun_group);
786
787 config_item_put(item);
788}
789
790static struct configfs_group_operations target_fabric_lun_group_ops = {
791 .make_group = &target_fabric_make_lun,
792 .drop_item = &target_fabric_drop_lun,
793};
794
795TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
796
797/* End of tfc_tpg_lun_cit */
798
799TF_CIT_SETUP_DRV(tpg_attrib, NULL, NULL);
800TF_CIT_SETUP_DRV(tpg_auth, NULL, NULL);
801TF_CIT_SETUP_DRV(tpg_param, NULL, NULL);
802
803/* Start of tfc_tpg_base_cit */
804
805static void target_fabric_tpg_release(struct config_item *item)
806{
807 struct se_portal_group *se_tpg = container_of(to_config_group(item),
808 struct se_portal_group, tpg_group);
809 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
810 struct target_fabric_configfs *tf = wwn->wwn_tf;
811
812 tf->tf_ops->fabric_drop_tpg(se_tpg);
813}
814
815static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
816 .release = target_fabric_tpg_release,
817};
818
819static ssize_t target_fabric_tpg_base_enable_show(struct config_item *item,
820 char *page)
821{
822 return sysfs_emit(page, "%d\n", to_tpg(item)->enabled);
823}
824
825static ssize_t target_fabric_tpg_base_enable_store(struct config_item *item,
826 const char *page,
827 size_t count)
828{
829 struct se_portal_group *se_tpg = to_tpg(item);
830 int ret;
831 bool op;
832
833 ret = kstrtobool(page, &op);
834 if (ret)
835 return ret;
836
837 if (se_tpg->enabled == op)
838 return count;
839
840 ret = se_tpg->se_tpg_tfo->fabric_enable_tpg(se_tpg, op);
841 if (ret)
842 return ret;
843
844 se_tpg->enabled = op;
845
846 return count;
847}
848
849CONFIGFS_ATTR(target_fabric_tpg_base_, enable);
850
851static int
852target_fabric_setup_tpg_base_cit(struct target_fabric_configfs *tf)
853{
854 struct config_item_type *cit = &tf->tf_tpg_base_cit;
855 struct configfs_attribute **attrs = NULL;
856 size_t nr_attrs = 0;
857 int i = 0;
858
859 if (tf->tf_ops->tfc_tpg_base_attrs)
860 while (tf->tf_ops->tfc_tpg_base_attrs[nr_attrs] != NULL)
861 nr_attrs++;
862
863 if (tf->tf_ops->fabric_enable_tpg)
864 nr_attrs++;
865
866 if (nr_attrs == 0)
867 goto done;
868
869 /* + 1 for final NULL in the array */
870 attrs = kcalloc(nr_attrs + 1, sizeof(*attrs), GFP_KERNEL);
871 if (!attrs)
872 return -ENOMEM;
873
874 if (tf->tf_ops->tfc_tpg_base_attrs)
875 for (; tf->tf_ops->tfc_tpg_base_attrs[i] != NULL; i++)
876 attrs[i] = tf->tf_ops->tfc_tpg_base_attrs[i];
877
878 if (tf->tf_ops->fabric_enable_tpg)
879 attrs[i] = &target_fabric_tpg_base_attr_enable;
880
881done:
882 cit->ct_item_ops = &target_fabric_tpg_base_item_ops;
883 cit->ct_attrs = attrs;
884 cit->ct_owner = tf->tf_ops->module;
885 pr_debug("Setup generic tpg_base\n");
886
887 return 0;
888}
889/* End of tfc_tpg_base_cit */
890
891/* Start of tfc_tpg_cit */
892
893static struct config_group *target_fabric_make_tpg(
894 struct config_group *group,
895 const char *name)
896{
897 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
898 struct target_fabric_configfs *tf = wwn->wwn_tf;
899 struct se_portal_group *se_tpg;
900
901 if (!tf->tf_ops->fabric_make_tpg) {
902 pr_err("tf->tf_ops->fabric_make_tpg is NULL\n");
903 return ERR_PTR(-ENOSYS);
904 }
905
906 se_tpg = tf->tf_ops->fabric_make_tpg(wwn, name);
907 if (!se_tpg || IS_ERR(se_tpg))
908 return ERR_PTR(-EINVAL);
909
910 config_group_init_type_name(&se_tpg->tpg_group, name,
911 &tf->tf_tpg_base_cit);
912
913 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
914 &tf->tf_tpg_lun_cit);
915 configfs_add_default_group(&se_tpg->tpg_lun_group,
916 &se_tpg->tpg_group);
917
918 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
919 &tf->tf_tpg_np_cit);
920 configfs_add_default_group(&se_tpg->tpg_np_group,
921 &se_tpg->tpg_group);
922
923 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
924 &tf->tf_tpg_nacl_cit);
925 configfs_add_default_group(&se_tpg->tpg_acl_group,
926 &se_tpg->tpg_group);
927
928 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
929 &tf->tf_tpg_attrib_cit);
930 configfs_add_default_group(&se_tpg->tpg_attrib_group,
931 &se_tpg->tpg_group);
932
933 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
934 &tf->tf_tpg_auth_cit);
935 configfs_add_default_group(&se_tpg->tpg_auth_group,
936 &se_tpg->tpg_group);
937
938 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
939 &tf->tf_tpg_param_cit);
940 configfs_add_default_group(&se_tpg->tpg_param_group,
941 &se_tpg->tpg_group);
942
943 return &se_tpg->tpg_group;
944}
945
946static void target_fabric_drop_tpg(
947 struct config_group *group,
948 struct config_item *item)
949{
950 struct se_portal_group *se_tpg = container_of(to_config_group(item),
951 struct se_portal_group, tpg_group);
952
953 configfs_remove_default_groups(&se_tpg->tpg_group);
954 config_item_put(item);
955}
956
957static void target_fabric_release_wwn(struct config_item *item)
958{
959 struct se_wwn *wwn = container_of(to_config_group(item),
960 struct se_wwn, wwn_group);
961 struct target_fabric_configfs *tf = wwn->wwn_tf;
962
963 configfs_remove_default_groups(&wwn->fabric_stat_group);
964 configfs_remove_default_groups(&wwn->param_group);
965 tf->tf_ops->fabric_drop_wwn(wwn);
966}
967
968static struct configfs_item_operations target_fabric_tpg_item_ops = {
969 .release = target_fabric_release_wwn,
970};
971
972static struct configfs_group_operations target_fabric_tpg_group_ops = {
973 .make_group = target_fabric_make_tpg,
974 .drop_item = target_fabric_drop_tpg,
975};
976
977TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
978 NULL);
979
980/* End of tfc_tpg_cit */
981
982/* Start of tfc_wwn_fabric_stats_cit */
983/*
984 * This is used as a placeholder for struct se_wwn->fabric_stat_group
985 * to allow fabrics access to ->fabric_stat_group->default_groups[]
986 */
987TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
988
989/* End of tfc_wwn_fabric_stats_cit */
990
991static ssize_t
992target_fabric_wwn_cmd_completion_affinity_show(struct config_item *item,
993 char *page)
994{
995 struct se_wwn *wwn = container_of(to_config_group(item), struct se_wwn,
996 param_group);
997 return sprintf(page, "%d\n",
998 wwn->cmd_compl_affinity == WORK_CPU_UNBOUND ?
999 SE_COMPL_AFFINITY_CURR_CPU : wwn->cmd_compl_affinity);
1000}
1001
1002static ssize_t
1003target_fabric_wwn_cmd_completion_affinity_store(struct config_item *item,
1004 const char *page, size_t count)
1005{
1006 struct se_wwn *wwn = container_of(to_config_group(item), struct se_wwn,
1007 param_group);
1008 int compl_val;
1009
1010 if (kstrtoint(page, 0, &compl_val))
1011 return -EINVAL;
1012
1013 switch (compl_val) {
1014 case SE_COMPL_AFFINITY_CPUID:
1015 wwn->cmd_compl_affinity = compl_val;
1016 break;
1017 case SE_COMPL_AFFINITY_CURR_CPU:
1018 wwn->cmd_compl_affinity = WORK_CPU_UNBOUND;
1019 break;
1020 default:
1021 if (compl_val < 0 || compl_val >= nr_cpu_ids ||
1022 !cpu_online(compl_val)) {
1023 pr_err("Command completion value must be between %d and %d or an online CPU.\n",
1024 SE_COMPL_AFFINITY_CPUID,
1025 SE_COMPL_AFFINITY_CURR_CPU);
1026 return -EINVAL;
1027 }
1028 wwn->cmd_compl_affinity = compl_val;
1029 }
1030
1031 return count;
1032}
1033CONFIGFS_ATTR(target_fabric_wwn_, cmd_completion_affinity);
1034
1035static struct configfs_attribute *target_fabric_wwn_param_attrs[] = {
1036 &target_fabric_wwn_attr_cmd_completion_affinity,
1037 NULL,
1038};
1039
1040TF_CIT_SETUP(wwn_param, NULL, NULL, target_fabric_wwn_param_attrs);
1041
1042/* Start of tfc_wwn_cit */
1043
1044static struct config_group *target_fabric_make_wwn(
1045 struct config_group *group,
1046 const char *name)
1047{
1048 struct target_fabric_configfs *tf = container_of(group,
1049 struct target_fabric_configfs, tf_group);
1050 struct se_wwn *wwn;
1051
1052 if (!tf->tf_ops->fabric_make_wwn) {
1053 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1054 return ERR_PTR(-ENOSYS);
1055 }
1056
1057 wwn = tf->tf_ops->fabric_make_wwn(tf, group, name);
1058 if (!wwn || IS_ERR(wwn))
1059 return ERR_PTR(-EINVAL);
1060
1061 wwn->cmd_compl_affinity = SE_COMPL_AFFINITY_CPUID;
1062 wwn->wwn_tf = tf;
1063
1064 config_group_init_type_name(&wwn->wwn_group, name, &tf->tf_tpg_cit);
1065
1066 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1067 &tf->tf_wwn_fabric_stats_cit);
1068 configfs_add_default_group(&wwn->fabric_stat_group, &wwn->wwn_group);
1069
1070 config_group_init_type_name(&wwn->param_group, "param",
1071 &tf->tf_wwn_param_cit);
1072 configfs_add_default_group(&wwn->param_group, &wwn->wwn_group);
1073
1074 if (tf->tf_ops->add_wwn_groups)
1075 tf->tf_ops->add_wwn_groups(wwn);
1076 return &wwn->wwn_group;
1077}
1078
1079static void target_fabric_drop_wwn(
1080 struct config_group *group,
1081 struct config_item *item)
1082{
1083 struct se_wwn *wwn = container_of(to_config_group(item),
1084 struct se_wwn, wwn_group);
1085
1086 configfs_remove_default_groups(&wwn->wwn_group);
1087 config_item_put(item);
1088}
1089
1090static struct configfs_group_operations target_fabric_wwn_group_ops = {
1091 .make_group = target_fabric_make_wwn,
1092 .drop_item = target_fabric_drop_wwn,
1093};
1094
1095TF_CIT_SETUP_DRV(wwn, NULL, &target_fabric_wwn_group_ops);
1096TF_CIT_SETUP_DRV(discovery, NULL, NULL);
1097
1098int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1099{
1100 int ret;
1101
1102 target_fabric_setup_discovery_cit(tf);
1103 target_fabric_setup_wwn_cit(tf);
1104 target_fabric_setup_wwn_fabric_stats_cit(tf);
1105 target_fabric_setup_wwn_param_cit(tf);
1106 target_fabric_setup_tpg_cit(tf);
1107
1108 ret = target_fabric_setup_tpg_base_cit(tf);
1109 if (ret)
1110 return ret;
1111
1112 target_fabric_setup_tpg_port_cit(tf);
1113 target_fabric_setup_tpg_port_stat_cit(tf);
1114 target_fabric_setup_tpg_lun_cit(tf);
1115 target_fabric_setup_tpg_np_cit(tf);
1116 target_fabric_setup_tpg_np_base_cit(tf);
1117 target_fabric_setup_tpg_attrib_cit(tf);
1118 target_fabric_setup_tpg_auth_cit(tf);
1119 target_fabric_setup_tpg_param_cit(tf);
1120 target_fabric_setup_tpg_nacl_cit(tf);
1121 target_fabric_setup_tpg_nacl_base_cit(tf);
1122 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1123 target_fabric_setup_tpg_nacl_auth_cit(tf);
1124 target_fabric_setup_tpg_nacl_param_cit(tf);
1125 target_fabric_setup_tpg_nacl_stat_cit(tf);
1126 target_fabric_setup_tpg_mappedlun_cit(tf);
1127 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
1128
1129 return 0;
1130}
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#include <target/target_core_fabric_configfs.h>
39#include <target/target_core_configfs.h>
40#include <target/configfs_macros.h>
41
42#include "target_core_internal.h"
43#include "target_core_alua.h"
44#include "target_core_pr.h"
45
46#define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
47static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
48{ \
49 struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
50 struct config_item_type *cit = &tfc->tfc_##_name##_cit; \
51 \
52 cit->ct_item_ops = _item_ops; \
53 cit->ct_group_ops = _group_ops; \
54 cit->ct_attrs = _attrs; \
55 cit->ct_owner = tf->tf_module; \
56 pr_debug("Setup generic %s\n", __stringify(_name)); \
57}
58
59/* Start of tfc_tpg_mappedlun_cit */
60
61static int target_fabric_mappedlun_link(
62 struct config_item *lun_acl_ci,
63 struct config_item *lun_ci)
64{
65 struct se_dev_entry *deve;
66 struct se_lun *lun = container_of(to_config_group(lun_ci),
67 struct se_lun, lun_group);
68 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
69 struct se_lun_acl, se_lun_group);
70 struct se_portal_group *se_tpg;
71 struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
72 int ret = 0, lun_access;
73
74 if (lun->lun_link_magic != SE_LUN_LINK_MAGIC) {
75 pr_err("Bad lun->lun_link_magic, not a valid lun_ci pointer:"
76 " %p to struct lun: %p\n", lun_ci, lun);
77 return -EFAULT;
78 }
79 /*
80 * Ensure that the source port exists
81 */
82 if (!lun->lun_sep || !lun->lun_sep->sep_tpg) {
83 pr_err("Source se_lun->lun_sep or lun->lun_sep->sep"
84 "_tpg does not exist\n");
85 return -EINVAL;
86 }
87 se_tpg = lun->lun_sep->sep_tpg;
88
89 nacl_ci = &lun_acl_ci->ci_parent->ci_group->cg_item;
90 tpg_ci = &nacl_ci->ci_group->cg_item;
91 wwn_ci = &tpg_ci->ci_group->cg_item;
92 tpg_ci_s = &lun_ci->ci_parent->ci_group->cg_item;
93 wwn_ci_s = &tpg_ci_s->ci_group->cg_item;
94 /*
95 * Make sure the SymLink is going to the same $FABRIC/$WWN/tpgt_$TPGT
96 */
97 if (strcmp(config_item_name(wwn_ci), config_item_name(wwn_ci_s))) {
98 pr_err("Illegal Initiator ACL SymLink outside of %s\n",
99 config_item_name(wwn_ci));
100 return -EINVAL;
101 }
102 if (strcmp(config_item_name(tpg_ci), config_item_name(tpg_ci_s))) {
103 pr_err("Illegal Initiator ACL Symlink outside of %s"
104 " TPGT: %s\n", config_item_name(wwn_ci),
105 config_item_name(tpg_ci));
106 return -EINVAL;
107 }
108 /*
109 * If this struct se_node_acl was dynamically generated with
110 * tpg_1/attrib/generate_node_acls=1, use the existing deve->lun_flags,
111 * which be will write protected (READ-ONLY) when
112 * tpg_1/attrib/demo_mode_write_protect=1
113 */
114 spin_lock_irq(&lacl->se_lun_nacl->device_list_lock);
115 deve = lacl->se_lun_nacl->device_list[lacl->mapped_lun];
116 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)
117 lun_access = deve->lun_flags;
118 else
119 lun_access =
120 (se_tpg->se_tpg_tfo->tpg_check_prod_mode_write_protect(
121 se_tpg)) ? TRANSPORT_LUNFLAGS_READ_ONLY :
122 TRANSPORT_LUNFLAGS_READ_WRITE;
123 spin_unlock_irq(&lacl->se_lun_nacl->device_list_lock);
124 /*
125 * Determine the actual mapped LUN value user wants..
126 *
127 * This value is what the SCSI Initiator actually sees the
128 * iscsi/$IQN/$TPGT/lun/lun_* as on their SCSI Initiator Ports.
129 */
130 ret = core_dev_add_initiator_node_lun_acl(se_tpg, lacl,
131 lun->unpacked_lun, lun_access);
132
133 return (ret < 0) ? -EINVAL : 0;
134}
135
136static int target_fabric_mappedlun_unlink(
137 struct config_item *lun_acl_ci,
138 struct config_item *lun_ci)
139{
140 struct se_lun *lun;
141 struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
142 struct se_lun_acl, se_lun_group);
143 struct se_node_acl *nacl = lacl->se_lun_nacl;
144 struct se_dev_entry *deve = nacl->device_list[lacl->mapped_lun];
145 struct se_portal_group *se_tpg;
146 /*
147 * Determine if the underlying MappedLUN has already been released..
148 */
149 if (!deve->se_lun)
150 return 0;
151
152 lun = container_of(to_config_group(lun_ci), struct se_lun, lun_group);
153 se_tpg = lun->lun_sep->sep_tpg;
154
155 core_dev_del_initiator_node_lun_acl(se_tpg, lun, lacl);
156 return 0;
157}
158
159CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
160#define TCM_MAPPEDLUN_ATTR(_name, _mode) \
161static struct target_fabric_mappedlun_attribute target_fabric_mappedlun_##_name = \
162 __CONFIGFS_EATTR(_name, _mode, \
163 target_fabric_mappedlun_show_##_name, \
164 target_fabric_mappedlun_store_##_name);
165
166static ssize_t target_fabric_mappedlun_show_write_protect(
167 struct se_lun_acl *lacl,
168 char *page)
169{
170 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
171 struct se_dev_entry *deve;
172 ssize_t len;
173
174 spin_lock_irq(&se_nacl->device_list_lock);
175 deve = se_nacl->device_list[lacl->mapped_lun];
176 len = sprintf(page, "%d\n",
177 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY) ?
178 1 : 0);
179 spin_unlock_irq(&se_nacl->device_list_lock);
180
181 return len;
182}
183
184static ssize_t target_fabric_mappedlun_store_write_protect(
185 struct se_lun_acl *lacl,
186 const char *page,
187 size_t count)
188{
189 struct se_node_acl *se_nacl = lacl->se_lun_nacl;
190 struct se_portal_group *se_tpg = se_nacl->se_tpg;
191 unsigned long op;
192 int ret;
193
194 ret = kstrtoul(page, 0, &op);
195 if (ret)
196 return ret;
197
198 if ((op != 1) && (op != 0))
199 return -EINVAL;
200
201 core_update_device_list_access(lacl->mapped_lun, (op) ?
202 TRANSPORT_LUNFLAGS_READ_ONLY :
203 TRANSPORT_LUNFLAGS_READ_WRITE,
204 lacl->se_lun_nacl);
205
206 pr_debug("%s_ConfigFS: Changed Initiator ACL: %s"
207 " Mapped LUN: %u Write Protect bit to %s\n",
208 se_tpg->se_tpg_tfo->get_fabric_name(),
209 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
210
211 return count;
212
213}
214
215TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
216
217CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
218
219static void target_fabric_mappedlun_release(struct config_item *item)
220{
221 struct se_lun_acl *lacl = container_of(to_config_group(item),
222 struct se_lun_acl, se_lun_group);
223 struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
224
225 core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
226}
227
228static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
229 &target_fabric_mappedlun_write_protect.attr,
230 NULL,
231};
232
233static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
234 .release = target_fabric_mappedlun_release,
235 .show_attribute = target_fabric_mappedlun_attr_show,
236 .store_attribute = target_fabric_mappedlun_attr_store,
237 .allow_link = target_fabric_mappedlun_link,
238 .drop_link = target_fabric_mappedlun_unlink,
239};
240
241TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
242 target_fabric_mappedlun_attrs);
243
244/* End of tfc_tpg_mappedlun_cit */
245
246/* Start of tfc_tpg_mappedlun_port_cit */
247
248static struct config_group *target_core_mappedlun_stat_mkdir(
249 struct config_group *group,
250 const char *name)
251{
252 return ERR_PTR(-ENOSYS);
253}
254
255static void target_core_mappedlun_stat_rmdir(
256 struct config_group *group,
257 struct config_item *item)
258{
259 return;
260}
261
262static struct configfs_group_operations target_fabric_mappedlun_stat_group_ops = {
263 .make_group = target_core_mappedlun_stat_mkdir,
264 .drop_item = target_core_mappedlun_stat_rmdir,
265};
266
267TF_CIT_SETUP(tpg_mappedlun_stat, NULL, &target_fabric_mappedlun_stat_group_ops,
268 NULL);
269
270/* End of tfc_tpg_mappedlun_port_cit */
271
272/* Start of tfc_tpg_nacl_attrib_cit */
273
274CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
275
276static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
277 .show_attribute = target_fabric_nacl_attrib_attr_show,
278 .store_attribute = target_fabric_nacl_attrib_attr_store,
279};
280
281TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
282
283/* End of tfc_tpg_nacl_attrib_cit */
284
285/* Start of tfc_tpg_nacl_auth_cit */
286
287CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
288
289static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
290 .show_attribute = target_fabric_nacl_auth_attr_show,
291 .store_attribute = target_fabric_nacl_auth_attr_store,
292};
293
294TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
295
296/* End of tfc_tpg_nacl_auth_cit */
297
298/* Start of tfc_tpg_nacl_param_cit */
299
300CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
301
302static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
303 .show_attribute = target_fabric_nacl_param_attr_show,
304 .store_attribute = target_fabric_nacl_param_attr_store,
305};
306
307TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
308
309/* End of tfc_tpg_nacl_param_cit */
310
311/* Start of tfc_tpg_nacl_base_cit */
312
313CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
314
315static struct config_group *target_fabric_make_mappedlun(
316 struct config_group *group,
317 const char *name)
318{
319 struct se_node_acl *se_nacl = container_of(group,
320 struct se_node_acl, acl_group);
321 struct se_portal_group *se_tpg = se_nacl->se_tpg;
322 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
323 struct se_lun_acl *lacl;
324 struct config_item *acl_ci;
325 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
326 char *buf;
327 unsigned long mapped_lun;
328 int ret = 0;
329
330 acl_ci = &group->cg_item;
331 if (!acl_ci) {
332 pr_err("Unable to locatel acl_ci\n");
333 return NULL;
334 }
335
336 buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
337 if (!buf) {
338 pr_err("Unable to allocate memory for name buf\n");
339 return ERR_PTR(-ENOMEM);
340 }
341 snprintf(buf, strlen(name) + 1, "%s", name);
342 /*
343 * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
344 */
345 if (strstr(buf, "lun_") != buf) {
346 pr_err("Unable to locate \"lun_\" from buf: %s"
347 " name: %s\n", buf, name);
348 ret = -EINVAL;
349 goto out;
350 }
351 /*
352 * Determine the Mapped LUN value. This is what the SCSI Initiator
353 * Port will actually see.
354 */
355 ret = kstrtoul(buf + 4, 0, &mapped_lun);
356 if (ret)
357 goto out;
358 if (mapped_lun > UINT_MAX) {
359 ret = -EINVAL;
360 goto out;
361 }
362 if (mapped_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
363 pr_err("Mapped LUN: %lu exceeds TRANSPORT_MAX_LUNS_PER_TPG"
364 "-1: %u for Target Portal Group: %u\n", mapped_lun,
365 TRANSPORT_MAX_LUNS_PER_TPG-1,
366 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
367 ret = -EINVAL;
368 goto out;
369 }
370
371 lacl = core_dev_init_initiator_node_lun_acl(se_tpg, se_nacl,
372 mapped_lun, &ret);
373 if (!lacl) {
374 ret = -EINVAL;
375 goto out;
376 }
377
378 lacl_cg = &lacl->se_lun_group;
379 lacl_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
380 GFP_KERNEL);
381 if (!lacl_cg->default_groups) {
382 pr_err("Unable to allocate lacl_cg->default_groups\n");
383 ret = -ENOMEM;
384 goto out;
385 }
386
387 config_group_init_type_name(&lacl->se_lun_group, name,
388 &tf->tf_cit_tmpl.tfc_tpg_mappedlun_cit);
389 config_group_init_type_name(&lacl->ml_stat_grps.stat_group,
390 "statistics", &tf->tf_cit_tmpl.tfc_tpg_mappedlun_stat_cit);
391 lacl_cg->default_groups[0] = &lacl->ml_stat_grps.stat_group;
392 lacl_cg->default_groups[1] = NULL;
393
394 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
395 ml_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 3,
396 GFP_KERNEL);
397 if (!ml_stat_grp->default_groups) {
398 pr_err("Unable to allocate ml_stat_grp->default_groups\n");
399 ret = -ENOMEM;
400 goto out;
401 }
402 target_stat_setup_mappedlun_default_groups(lacl);
403
404 kfree(buf);
405 return &lacl->se_lun_group;
406out:
407 if (lacl_cg)
408 kfree(lacl_cg->default_groups);
409 kfree(buf);
410 return ERR_PTR(ret);
411}
412
413static void target_fabric_drop_mappedlun(
414 struct config_group *group,
415 struct config_item *item)
416{
417 struct se_lun_acl *lacl = container_of(to_config_group(item),
418 struct se_lun_acl, se_lun_group);
419 struct config_item *df_item;
420 struct config_group *lacl_cg = NULL, *ml_stat_grp = NULL;
421 int i;
422
423 ml_stat_grp = &lacl->ml_stat_grps.stat_group;
424 for (i = 0; ml_stat_grp->default_groups[i]; i++) {
425 df_item = &ml_stat_grp->default_groups[i]->cg_item;
426 ml_stat_grp->default_groups[i] = NULL;
427 config_item_put(df_item);
428 }
429 kfree(ml_stat_grp->default_groups);
430
431 lacl_cg = &lacl->se_lun_group;
432 for (i = 0; lacl_cg->default_groups[i]; i++) {
433 df_item = &lacl_cg->default_groups[i]->cg_item;
434 lacl_cg->default_groups[i] = NULL;
435 config_item_put(df_item);
436 }
437 kfree(lacl_cg->default_groups);
438
439 config_item_put(item);
440}
441
442static void target_fabric_nacl_base_release(struct config_item *item)
443{
444 struct se_node_acl *se_nacl = container_of(to_config_group(item),
445 struct se_node_acl, acl_group);
446 struct se_portal_group *se_tpg = se_nacl->se_tpg;
447 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
448
449 tf->tf_ops.fabric_drop_nodeacl(se_nacl);
450}
451
452static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
453 .release = target_fabric_nacl_base_release,
454 .show_attribute = target_fabric_nacl_base_attr_show,
455 .store_attribute = target_fabric_nacl_base_attr_store,
456};
457
458static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
459 .make_group = target_fabric_make_mappedlun,
460 .drop_item = target_fabric_drop_mappedlun,
461};
462
463TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
464 &target_fabric_nacl_base_group_ops, NULL);
465
466/* End of tfc_tpg_nacl_base_cit */
467
468/* Start of tfc_node_fabric_stats_cit */
469/*
470 * This is used as a placeholder for struct se_node_acl->acl_fabric_stat_group
471 * to allow fabrics access to ->acl_fabric_stat_group->default_groups[]
472 */
473TF_CIT_SETUP(tpg_nacl_stat, NULL, NULL, NULL);
474
475/* End of tfc_wwn_fabric_stats_cit */
476
477/* Start of tfc_tpg_nacl_cit */
478
479static struct config_group *target_fabric_make_nodeacl(
480 struct config_group *group,
481 const char *name)
482{
483 struct se_portal_group *se_tpg = container_of(group,
484 struct se_portal_group, tpg_acl_group);
485 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
486 struct se_node_acl *se_nacl;
487 struct config_group *nacl_cg;
488
489 if (!tf->tf_ops.fabric_make_nodeacl) {
490 pr_err("tf->tf_ops.fabric_make_nodeacl is NULL\n");
491 return ERR_PTR(-ENOSYS);
492 }
493
494 se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
495 if (IS_ERR(se_nacl))
496 return ERR_CAST(se_nacl);
497
498 nacl_cg = &se_nacl->acl_group;
499 nacl_cg->default_groups = se_nacl->acl_default_groups;
500 nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
501 nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
502 nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
503 nacl_cg->default_groups[3] = &se_nacl->acl_fabric_stat_group;
504 nacl_cg->default_groups[4] = NULL;
505
506 config_group_init_type_name(&se_nacl->acl_group, name,
507 &tf->tf_cit_tmpl.tfc_tpg_nacl_base_cit);
508 config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
509 &tf->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit);
510 config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
511 &tf->tf_cit_tmpl.tfc_tpg_nacl_auth_cit);
512 config_group_init_type_name(&se_nacl->acl_param_group, "param",
513 &tf->tf_cit_tmpl.tfc_tpg_nacl_param_cit);
514 config_group_init_type_name(&se_nacl->acl_fabric_stat_group,
515 "fabric_statistics",
516 &tf->tf_cit_tmpl.tfc_tpg_nacl_stat_cit);
517
518 return &se_nacl->acl_group;
519}
520
521static void target_fabric_drop_nodeacl(
522 struct config_group *group,
523 struct config_item *item)
524{
525 struct se_node_acl *se_nacl = container_of(to_config_group(item),
526 struct se_node_acl, acl_group);
527 struct config_item *df_item;
528 struct config_group *nacl_cg;
529 int i;
530
531 nacl_cg = &se_nacl->acl_group;
532 for (i = 0; nacl_cg->default_groups[i]; i++) {
533 df_item = &nacl_cg->default_groups[i]->cg_item;
534 nacl_cg->default_groups[i] = NULL;
535 config_item_put(df_item);
536 }
537 /*
538 * struct se_node_acl free is done in target_fabric_nacl_base_release()
539 */
540 config_item_put(item);
541}
542
543static struct configfs_group_operations target_fabric_nacl_group_ops = {
544 .make_group = target_fabric_make_nodeacl,
545 .drop_item = target_fabric_drop_nodeacl,
546};
547
548TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
549
550/* End of tfc_tpg_nacl_cit */
551
552/* Start of tfc_tpg_np_base_cit */
553
554CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
555
556static void target_fabric_np_base_release(struct config_item *item)
557{
558 struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
559 struct se_tpg_np, tpg_np_group);
560 struct se_portal_group *se_tpg = se_tpg_np->tpg_np_parent;
561 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
562
563 tf->tf_ops.fabric_drop_np(se_tpg_np);
564}
565
566static struct configfs_item_operations target_fabric_np_base_item_ops = {
567 .release = target_fabric_np_base_release,
568 .show_attribute = target_fabric_np_base_attr_show,
569 .store_attribute = target_fabric_np_base_attr_store,
570};
571
572TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
573
574/* End of tfc_tpg_np_base_cit */
575
576/* Start of tfc_tpg_np_cit */
577
578static struct config_group *target_fabric_make_np(
579 struct config_group *group,
580 const char *name)
581{
582 struct se_portal_group *se_tpg = container_of(group,
583 struct se_portal_group, tpg_np_group);
584 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
585 struct se_tpg_np *se_tpg_np;
586
587 if (!tf->tf_ops.fabric_make_np) {
588 pr_err("tf->tf_ops.fabric_make_np is NULL\n");
589 return ERR_PTR(-ENOSYS);
590 }
591
592 se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
593 if (!se_tpg_np || IS_ERR(se_tpg_np))
594 return ERR_PTR(-EINVAL);
595
596 se_tpg_np->tpg_np_parent = se_tpg;
597 config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
598 &tf->tf_cit_tmpl.tfc_tpg_np_base_cit);
599
600 return &se_tpg_np->tpg_np_group;
601}
602
603static void target_fabric_drop_np(
604 struct config_group *group,
605 struct config_item *item)
606{
607 /*
608 * struct se_tpg_np is released via target_fabric_np_base_release()
609 */
610 config_item_put(item);
611}
612
613static struct configfs_group_operations target_fabric_np_group_ops = {
614 .make_group = &target_fabric_make_np,
615 .drop_item = &target_fabric_drop_np,
616};
617
618TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
619
620/* End of tfc_tpg_np_cit */
621
622/* Start of tfc_tpg_port_cit */
623
624CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
625#define TCM_PORT_ATTR(_name, _mode) \
626static struct target_fabric_port_attribute target_fabric_port_##_name = \
627 __CONFIGFS_EATTR(_name, _mode, \
628 target_fabric_port_show_attr_##_name, \
629 target_fabric_port_store_attr_##_name);
630
631#define TCM_PORT_ATTOR_RO(_name) \
632 __CONFIGFS_EATTR_RO(_name, \
633 target_fabric_port_show_attr_##_name);
634
635/*
636 * alua_tg_pt_gp
637 */
638static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
639 struct se_lun *lun,
640 char *page)
641{
642 if (!lun || !lun->lun_sep)
643 return -ENODEV;
644
645 return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
646}
647
648static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
649 struct se_lun *lun,
650 const char *page,
651 size_t count)
652{
653 if (!lun || !lun->lun_sep)
654 return -ENODEV;
655
656 return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
657}
658
659TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
660
661/*
662 * alua_tg_pt_offline
663 */
664static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
665 struct se_lun *lun,
666 char *page)
667{
668 if (!lun || !lun->lun_sep)
669 return -ENODEV;
670
671 return core_alua_show_offline_bit(lun, page);
672}
673
674static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
675 struct se_lun *lun,
676 const char *page,
677 size_t count)
678{
679 if (!lun || !lun->lun_sep)
680 return -ENODEV;
681
682 return core_alua_store_offline_bit(lun, page, count);
683}
684
685TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
686
687/*
688 * alua_tg_pt_status
689 */
690static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
691 struct se_lun *lun,
692 char *page)
693{
694 if (!lun || !lun->lun_sep)
695 return -ENODEV;
696
697 return core_alua_show_secondary_status(lun, page);
698}
699
700static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
701 struct se_lun *lun,
702 const char *page,
703 size_t count)
704{
705 if (!lun || !lun->lun_sep)
706 return -ENODEV;
707
708 return core_alua_store_secondary_status(lun, page, count);
709}
710
711TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
712
713/*
714 * alua_tg_pt_write_md
715 */
716static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
717 struct se_lun *lun,
718 char *page)
719{
720 if (!lun || !lun->lun_sep)
721 return -ENODEV;
722
723 return core_alua_show_secondary_write_metadata(lun, page);
724}
725
726static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
727 struct se_lun *lun,
728 const char *page,
729 size_t count)
730{
731 if (!lun || !lun->lun_sep)
732 return -ENODEV;
733
734 return core_alua_store_secondary_write_metadata(lun, page, count);
735}
736
737TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
738
739
740static struct configfs_attribute *target_fabric_port_attrs[] = {
741 &target_fabric_port_alua_tg_pt_gp.attr,
742 &target_fabric_port_alua_tg_pt_offline.attr,
743 &target_fabric_port_alua_tg_pt_status.attr,
744 &target_fabric_port_alua_tg_pt_write_md.attr,
745 NULL,
746};
747
748CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
749
750static int target_fabric_port_link(
751 struct config_item *lun_ci,
752 struct config_item *se_dev_ci)
753{
754 struct config_item *tpg_ci;
755 struct se_lun *lun = container_of(to_config_group(lun_ci),
756 struct se_lun, lun_group);
757 struct se_lun *lun_p;
758 struct se_portal_group *se_tpg;
759 struct se_device *dev =
760 container_of(to_config_group(se_dev_ci), struct se_device, dev_group);
761 struct target_fabric_configfs *tf;
762 int ret;
763
764 if (dev->dev_link_magic != SE_DEV_LINK_MAGIC) {
765 pr_err("Bad dev->dev_link_magic, not a valid se_dev_ci pointer:"
766 " %p to struct se_device: %p\n", se_dev_ci, dev);
767 return -EFAULT;
768 }
769
770 if (!(dev->dev_flags & DF_CONFIGURED)) {
771 pr_err("se_device not configured yet, cannot port link\n");
772 return -ENODEV;
773 }
774
775 tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
776 se_tpg = container_of(to_config_group(tpg_ci),
777 struct se_portal_group, tpg_group);
778 tf = se_tpg->se_tpg_wwn->wwn_tf;
779
780 if (lun->lun_se_dev != NULL) {
781 pr_err("Port Symlink already exists\n");
782 return -EEXIST;
783 }
784
785 lun_p = core_dev_add_lun(se_tpg, dev, lun->unpacked_lun);
786 if (IS_ERR(lun_p)) {
787 pr_err("core_dev_add_lun() failed\n");
788 ret = PTR_ERR(lun_p);
789 goto out;
790 }
791
792 if (tf->tf_ops.fabric_post_link) {
793 /*
794 * Call the optional fabric_post_link() to allow a
795 * fabric module to setup any additional state once
796 * core_dev_add_lun() has been called..
797 */
798 tf->tf_ops.fabric_post_link(se_tpg, lun);
799 }
800
801 return 0;
802out:
803 return ret;
804}
805
806static int target_fabric_port_unlink(
807 struct config_item *lun_ci,
808 struct config_item *se_dev_ci)
809{
810 struct se_lun *lun = container_of(to_config_group(lun_ci),
811 struct se_lun, lun_group);
812 struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
813 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
814
815 if (tf->tf_ops.fabric_pre_unlink) {
816 /*
817 * Call the optional fabric_pre_unlink() to allow a
818 * fabric module to release any additional stat before
819 * core_dev_del_lun() is called.
820 */
821 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
822 }
823
824 core_dev_del_lun(se_tpg, lun->unpacked_lun);
825 return 0;
826}
827
828static struct configfs_item_operations target_fabric_port_item_ops = {
829 .show_attribute = target_fabric_port_attr_show,
830 .store_attribute = target_fabric_port_attr_store,
831 .allow_link = target_fabric_port_link,
832 .drop_link = target_fabric_port_unlink,
833};
834
835TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
836
837/* End of tfc_tpg_port_cit */
838
839/* Start of tfc_tpg_port_stat_cit */
840
841static struct config_group *target_core_port_stat_mkdir(
842 struct config_group *group,
843 const char *name)
844{
845 return ERR_PTR(-ENOSYS);
846}
847
848static void target_core_port_stat_rmdir(
849 struct config_group *group,
850 struct config_item *item)
851{
852 return;
853}
854
855static struct configfs_group_operations target_fabric_port_stat_group_ops = {
856 .make_group = target_core_port_stat_mkdir,
857 .drop_item = target_core_port_stat_rmdir,
858};
859
860TF_CIT_SETUP(tpg_port_stat, NULL, &target_fabric_port_stat_group_ops, NULL);
861
862/* End of tfc_tpg_port_stat_cit */
863
864/* Start of tfc_tpg_lun_cit */
865
866static struct config_group *target_fabric_make_lun(
867 struct config_group *group,
868 const char *name)
869{
870 struct se_lun *lun;
871 struct se_portal_group *se_tpg = container_of(group,
872 struct se_portal_group, tpg_lun_group);
873 struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
874 struct config_group *lun_cg = NULL, *port_stat_grp = NULL;
875 unsigned long unpacked_lun;
876 int errno;
877
878 if (strstr(name, "lun_") != name) {
879 pr_err("Unable to locate \'_\" in"
880 " \"lun_$LUN_NUMBER\"\n");
881 return ERR_PTR(-EINVAL);
882 }
883 errno = kstrtoul(name + 4, 0, &unpacked_lun);
884 if (errno)
885 return ERR_PTR(errno);
886 if (unpacked_lun > UINT_MAX)
887 return ERR_PTR(-EINVAL);
888
889 lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
890 if (!lun)
891 return ERR_PTR(-EINVAL);
892
893 lun_cg = &lun->lun_group;
894 lun_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
895 GFP_KERNEL);
896 if (!lun_cg->default_groups) {
897 pr_err("Unable to allocate lun_cg->default_groups\n");
898 return ERR_PTR(-ENOMEM);
899 }
900
901 config_group_init_type_name(&lun->lun_group, name,
902 &tf->tf_cit_tmpl.tfc_tpg_port_cit);
903 config_group_init_type_name(&lun->port_stat_grps.stat_group,
904 "statistics", &tf->tf_cit_tmpl.tfc_tpg_port_stat_cit);
905 lun_cg->default_groups[0] = &lun->port_stat_grps.stat_group;
906 lun_cg->default_groups[1] = NULL;
907
908 port_stat_grp = &lun->port_stat_grps.stat_group;
909 port_stat_grp->default_groups = kzalloc(sizeof(struct config_group *) * 4,
910 GFP_KERNEL);
911 if (!port_stat_grp->default_groups) {
912 pr_err("Unable to allocate port_stat_grp->default_groups\n");
913 errno = -ENOMEM;
914 goto out;
915 }
916 target_stat_setup_port_default_groups(lun);
917
918 return &lun->lun_group;
919out:
920 if (lun_cg)
921 kfree(lun_cg->default_groups);
922 return ERR_PTR(errno);
923}
924
925static void target_fabric_drop_lun(
926 struct config_group *group,
927 struct config_item *item)
928{
929 struct se_lun *lun = container_of(to_config_group(item),
930 struct se_lun, lun_group);
931 struct config_item *df_item;
932 struct config_group *lun_cg, *port_stat_grp;
933 int i;
934
935 port_stat_grp = &lun->port_stat_grps.stat_group;
936 for (i = 0; port_stat_grp->default_groups[i]; i++) {
937 df_item = &port_stat_grp->default_groups[i]->cg_item;
938 port_stat_grp->default_groups[i] = NULL;
939 config_item_put(df_item);
940 }
941 kfree(port_stat_grp->default_groups);
942
943 lun_cg = &lun->lun_group;
944 for (i = 0; lun_cg->default_groups[i]; i++) {
945 df_item = &lun_cg->default_groups[i]->cg_item;
946 lun_cg->default_groups[i] = NULL;
947 config_item_put(df_item);
948 }
949 kfree(lun_cg->default_groups);
950
951 config_item_put(item);
952}
953
954static struct configfs_group_operations target_fabric_lun_group_ops = {
955 .make_group = &target_fabric_make_lun,
956 .drop_item = &target_fabric_drop_lun,
957};
958
959TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
960
961/* End of tfc_tpg_lun_cit */
962
963/* Start of tfc_tpg_attrib_cit */
964
965CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
966
967static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
968 .show_attribute = target_fabric_tpg_attrib_attr_show,
969 .store_attribute = target_fabric_tpg_attrib_attr_store,
970};
971
972TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
973
974/* End of tfc_tpg_attrib_cit */
975
976/* Start of tfc_tpg_auth_cit */
977
978CONFIGFS_EATTR_OPS(target_fabric_tpg_auth, se_portal_group, tpg_auth_group);
979
980static struct configfs_item_operations target_fabric_tpg_auth_item_ops = {
981 .show_attribute = target_fabric_tpg_auth_attr_show,
982 .store_attribute = target_fabric_tpg_auth_attr_store,
983};
984
985TF_CIT_SETUP(tpg_auth, &target_fabric_tpg_auth_item_ops, NULL, NULL);
986
987/* End of tfc_tpg_attrib_cit */
988
989/* Start of tfc_tpg_param_cit */
990
991CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
992
993static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
994 .show_attribute = target_fabric_tpg_param_attr_show,
995 .store_attribute = target_fabric_tpg_param_attr_store,
996};
997
998TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
999
1000/* End of tfc_tpg_param_cit */
1001
1002/* Start of tfc_tpg_base_cit */
1003/*
1004 * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
1005 */
1006CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
1007
1008static void target_fabric_tpg_release(struct config_item *item)
1009{
1010 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1011 struct se_portal_group, tpg_group);
1012 struct se_wwn *wwn = se_tpg->se_tpg_wwn;
1013 struct target_fabric_configfs *tf = wwn->wwn_tf;
1014
1015 tf->tf_ops.fabric_drop_tpg(se_tpg);
1016}
1017
1018static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
1019 .release = target_fabric_tpg_release,
1020 .show_attribute = target_fabric_tpg_attr_show,
1021 .store_attribute = target_fabric_tpg_attr_store,
1022};
1023
1024TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
1025
1026/* End of tfc_tpg_base_cit */
1027
1028/* Start of tfc_tpg_cit */
1029
1030static struct config_group *target_fabric_make_tpg(
1031 struct config_group *group,
1032 const char *name)
1033{
1034 struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
1035 struct target_fabric_configfs *tf = wwn->wwn_tf;
1036 struct se_portal_group *se_tpg;
1037
1038 if (!tf->tf_ops.fabric_make_tpg) {
1039 pr_err("tf->tf_ops.fabric_make_tpg is NULL\n");
1040 return ERR_PTR(-ENOSYS);
1041 }
1042
1043 se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
1044 if (!se_tpg || IS_ERR(se_tpg))
1045 return ERR_PTR(-EINVAL);
1046 /*
1047 * Setup default groups from pre-allocated se_tpg->tpg_default_groups
1048 */
1049 se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
1050 se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
1051 se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
1052 se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
1053 se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
1054 se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_auth_group;
1055 se_tpg->tpg_group.default_groups[5] = &se_tpg->tpg_param_group;
1056 se_tpg->tpg_group.default_groups[6] = NULL;
1057
1058 config_group_init_type_name(&se_tpg->tpg_group, name,
1059 &tf->tf_cit_tmpl.tfc_tpg_base_cit);
1060 config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
1061 &tf->tf_cit_tmpl.tfc_tpg_lun_cit);
1062 config_group_init_type_name(&se_tpg->tpg_np_group, "np",
1063 &tf->tf_cit_tmpl.tfc_tpg_np_cit);
1064 config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
1065 &tf->tf_cit_tmpl.tfc_tpg_nacl_cit);
1066 config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
1067 &tf->tf_cit_tmpl.tfc_tpg_attrib_cit);
1068 config_group_init_type_name(&se_tpg->tpg_auth_group, "auth",
1069 &tf->tf_cit_tmpl.tfc_tpg_auth_cit);
1070 config_group_init_type_name(&se_tpg->tpg_param_group, "param",
1071 &tf->tf_cit_tmpl.tfc_tpg_param_cit);
1072
1073 return &se_tpg->tpg_group;
1074}
1075
1076static void target_fabric_drop_tpg(
1077 struct config_group *group,
1078 struct config_item *item)
1079{
1080 struct se_portal_group *se_tpg = container_of(to_config_group(item),
1081 struct se_portal_group, tpg_group);
1082 struct config_group *tpg_cg = &se_tpg->tpg_group;
1083 struct config_item *df_item;
1084 int i;
1085 /*
1086 * Release default groups, but do not release tpg_cg->default_groups
1087 * memory as it is statically allocated at se_tpg->tpg_default_groups.
1088 */
1089 for (i = 0; tpg_cg->default_groups[i]; i++) {
1090 df_item = &tpg_cg->default_groups[i]->cg_item;
1091 tpg_cg->default_groups[i] = NULL;
1092 config_item_put(df_item);
1093 }
1094
1095 config_item_put(item);
1096}
1097
1098static void target_fabric_release_wwn(struct config_item *item)
1099{
1100 struct se_wwn *wwn = container_of(to_config_group(item),
1101 struct se_wwn, wwn_group);
1102 struct target_fabric_configfs *tf = wwn->wwn_tf;
1103
1104 tf->tf_ops.fabric_drop_wwn(wwn);
1105}
1106
1107static struct configfs_item_operations target_fabric_tpg_item_ops = {
1108 .release = target_fabric_release_wwn,
1109};
1110
1111static struct configfs_group_operations target_fabric_tpg_group_ops = {
1112 .make_group = target_fabric_make_tpg,
1113 .drop_item = target_fabric_drop_tpg,
1114};
1115
1116TF_CIT_SETUP(tpg, &target_fabric_tpg_item_ops, &target_fabric_tpg_group_ops,
1117 NULL);
1118
1119/* End of tfc_tpg_cit */
1120
1121/* Start of tfc_wwn_fabric_stats_cit */
1122/*
1123 * This is used as a placeholder for struct se_wwn->fabric_stat_group
1124 * to allow fabrics access to ->fabric_stat_group->default_groups[]
1125 */
1126TF_CIT_SETUP(wwn_fabric_stats, NULL, NULL, NULL);
1127
1128/* End of tfc_wwn_fabric_stats_cit */
1129
1130/* Start of tfc_wwn_cit */
1131
1132static struct config_group *target_fabric_make_wwn(
1133 struct config_group *group,
1134 const char *name)
1135{
1136 struct target_fabric_configfs *tf = container_of(group,
1137 struct target_fabric_configfs, tf_group);
1138 struct se_wwn *wwn;
1139
1140 if (!tf->tf_ops.fabric_make_wwn) {
1141 pr_err("tf->tf_ops.fabric_make_wwn is NULL\n");
1142 return ERR_PTR(-ENOSYS);
1143 }
1144
1145 wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
1146 if (!wwn || IS_ERR(wwn))
1147 return ERR_PTR(-EINVAL);
1148
1149 wwn->wwn_tf = tf;
1150 /*
1151 * Setup default groups from pre-allocated wwn->wwn_default_groups
1152 */
1153 wwn->wwn_group.default_groups = wwn->wwn_default_groups;
1154 wwn->wwn_group.default_groups[0] = &wwn->fabric_stat_group;
1155 wwn->wwn_group.default_groups[1] = NULL;
1156
1157 config_group_init_type_name(&wwn->wwn_group, name,
1158 &tf->tf_cit_tmpl.tfc_tpg_cit);
1159 config_group_init_type_name(&wwn->fabric_stat_group, "fabric_statistics",
1160 &tf->tf_cit_tmpl.tfc_wwn_fabric_stats_cit);
1161
1162 return &wwn->wwn_group;
1163}
1164
1165static void target_fabric_drop_wwn(
1166 struct config_group *group,
1167 struct config_item *item)
1168{
1169 struct se_wwn *wwn = container_of(to_config_group(item),
1170 struct se_wwn, wwn_group);
1171 struct config_item *df_item;
1172 struct config_group *cg = &wwn->wwn_group;
1173 int i;
1174
1175 for (i = 0; cg->default_groups[i]; i++) {
1176 df_item = &cg->default_groups[i]->cg_item;
1177 cg->default_groups[i] = NULL;
1178 config_item_put(df_item);
1179 }
1180
1181 config_item_put(item);
1182}
1183
1184static struct configfs_group_operations target_fabric_wwn_group_ops = {
1185 .make_group = target_fabric_make_wwn,
1186 .drop_item = target_fabric_drop_wwn,
1187};
1188/*
1189 * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
1190 */
1191CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
1192
1193static struct configfs_item_operations target_fabric_wwn_item_ops = {
1194 .show_attribute = target_fabric_wwn_attr_show,
1195 .store_attribute = target_fabric_wwn_attr_store,
1196};
1197
1198TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
1199
1200/* End of tfc_wwn_cit */
1201
1202/* Start of tfc_discovery_cit */
1203
1204CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
1205 tf_disc_group);
1206
1207static struct configfs_item_operations target_fabric_discovery_item_ops = {
1208 .show_attribute = target_fabric_discovery_attr_show,
1209 .store_attribute = target_fabric_discovery_attr_store,
1210};
1211
1212TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
1213
1214/* End of tfc_discovery_cit */
1215
1216int target_fabric_setup_cits(struct target_fabric_configfs *tf)
1217{
1218 target_fabric_setup_discovery_cit(tf);
1219 target_fabric_setup_wwn_cit(tf);
1220 target_fabric_setup_wwn_fabric_stats_cit(tf);
1221 target_fabric_setup_tpg_cit(tf);
1222 target_fabric_setup_tpg_base_cit(tf);
1223 target_fabric_setup_tpg_port_cit(tf);
1224 target_fabric_setup_tpg_port_stat_cit(tf);
1225 target_fabric_setup_tpg_lun_cit(tf);
1226 target_fabric_setup_tpg_np_cit(tf);
1227 target_fabric_setup_tpg_np_base_cit(tf);
1228 target_fabric_setup_tpg_attrib_cit(tf);
1229 target_fabric_setup_tpg_auth_cit(tf);
1230 target_fabric_setup_tpg_param_cit(tf);
1231 target_fabric_setup_tpg_nacl_cit(tf);
1232 target_fabric_setup_tpg_nacl_base_cit(tf);
1233 target_fabric_setup_tpg_nacl_attrib_cit(tf);
1234 target_fabric_setup_tpg_nacl_auth_cit(tf);
1235 target_fabric_setup_tpg_nacl_param_cit(tf);
1236 target_fabric_setup_tpg_nacl_stat_cit(tf);
1237 target_fabric_setup_tpg_mappedlun_cit(tf);
1238 target_fabric_setup_tpg_mappedlun_stat_cit(tf);
1239
1240 return 0;
1241}