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