OSDN Git Service

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