Merge branch 'devel-stable' of master.kernel.org:/home/rmk/linux-2.6-arm
[pandora-kernel.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  * Copyright (c) 2010 Rising Tide Systems
8  * Copyright (c) 2010 Linux-iSCSI.org
9  *
10  * Copyright (c) 2010 Nicholas A. Bellinger <nab@linux-iscsi.org>
11 *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  ****************************************************************************/
22
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/version.h>
26 #include <generated/utsrelease.h>
27 #include <linux/utsname.h>
28 #include <linux/init.h>
29 #include <linux/fs.h>
30 #include <linux/namei.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
34 #include <linux/unistd.h>
35 #include <linux/string.h>
36 #include <linux/syscalls.h>
37 #include <linux/configfs.h>
38
39 #include <target/target_core_base.h>
40 #include <target/target_core_device.h>
41 #include <target/target_core_tpg.h>
42 #include <target/target_core_transport.h>
43 #include <target/target_core_fabric_ops.h>
44 #include <target/target_core_fabric_configfs.h>
45 #include <target/target_core_configfs.h>
46 #include <target/configfs_macros.h>
47
48 #include "target_core_alua.h"
49 #include "target_core_hba.h"
50 #include "target_core_pr.h"
51
52 #define TF_CIT_SETUP(_name, _item_ops, _group_ops, _attrs)              \
53 static void target_fabric_setup_##_name##_cit(struct target_fabric_configfs *tf) \
54 {                                                                       \
55         struct target_fabric_configfs_template *tfc = &tf->tf_cit_tmpl; \
56         struct config_item_type *cit = &tfc->tfc_##_name##_cit;         \
57                                                                         \
58         cit->ct_item_ops = _item_ops;                                   \
59         cit->ct_group_ops = _group_ops;                                 \
60         cit->ct_attrs = _attrs;                                         \
61         cit->ct_owner = tf->tf_module;                                  \
62         printk("Setup generic %s\n", __stringify(_name));               \
63 }
64
65 /* Start of tfc_tpg_mappedlun_cit */
66
67 static int target_fabric_mappedlun_link(
68         struct config_item *lun_acl_ci,
69         struct config_item *lun_ci)
70 {
71         struct se_dev_entry *deve;
72         struct se_lun *lun = container_of(to_config_group(lun_ci),
73                         struct se_lun, lun_group);
74         struct se_lun_acl *lacl = container_of(to_config_group(lun_acl_ci),
75                         struct se_lun_acl, se_lun_group);
76         struct se_portal_group *se_tpg;
77         struct config_item *nacl_ci, *tpg_ci, *tpg_ci_s, *wwn_ci, *wwn_ci_s;
78         int ret = 0, lun_access;
79         /*
80          * Ensure that the source port exists
81          */
82         if (!(lun->lun_sep) || !(lun->lun_sep->sep_tpg)) {
83                 printk(KERN_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                 printk(KERN_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                 printk(KERN_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                         (TPG_TFO(se_tpg)->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
136 static 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
159 CONFIGFS_EATTR_STRUCT(target_fabric_mappedlun, se_lun_acl);
160 #define TCM_MAPPEDLUN_ATTR(_name, _mode)                                \
161 static 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
166 static 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
184 static 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
193         if (strict_strtoul(page, 0, &op))
194                 return -EINVAL;
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         printk(KERN_INFO "%s_ConfigFS: Changed Initiator ACL: %s"
205                 " Mapped LUN: %u Write Protect bit to %s\n",
206                 TPG_TFO(se_tpg)->get_fabric_name(),
207                 lacl->initiatorname, lacl->mapped_lun, (op) ? "ON" : "OFF");
208
209         return count;
210
211 }
212
213 TCM_MAPPEDLUN_ATTR(write_protect, S_IRUGO | S_IWUSR);
214
215 CONFIGFS_EATTR_OPS(target_fabric_mappedlun, se_lun_acl, se_lun_group);
216
217 static struct configfs_attribute *target_fabric_mappedlun_attrs[] = {
218         &target_fabric_mappedlun_write_protect.attr,
219         NULL,
220 };
221
222 static struct configfs_item_operations target_fabric_mappedlun_item_ops = {
223         .show_attribute         = target_fabric_mappedlun_attr_show,
224         .store_attribute        = target_fabric_mappedlun_attr_store,
225         .allow_link             = target_fabric_mappedlun_link,
226         .drop_link              = target_fabric_mappedlun_unlink,
227 };
228
229 TF_CIT_SETUP(tpg_mappedlun, &target_fabric_mappedlun_item_ops, NULL,
230                 target_fabric_mappedlun_attrs);
231
232 /* End of tfc_tpg_mappedlun_cit */
233
234 /* Start of tfc_tpg_nacl_attrib_cit */
235
236 CONFIGFS_EATTR_OPS(target_fabric_nacl_attrib, se_node_acl, acl_attrib_group);
237
238 static struct configfs_item_operations target_fabric_nacl_attrib_item_ops = {
239         .show_attribute         = target_fabric_nacl_attrib_attr_show,
240         .store_attribute        = target_fabric_nacl_attrib_attr_store,
241 };
242
243 TF_CIT_SETUP(tpg_nacl_attrib, &target_fabric_nacl_attrib_item_ops, NULL, NULL);
244
245 /* End of tfc_tpg_nacl_attrib_cit */
246
247 /* Start of tfc_tpg_nacl_auth_cit */
248
249 CONFIGFS_EATTR_OPS(target_fabric_nacl_auth, se_node_acl, acl_auth_group);
250
251 static struct configfs_item_operations target_fabric_nacl_auth_item_ops = {
252         .show_attribute         = target_fabric_nacl_auth_attr_show,
253         .store_attribute        = target_fabric_nacl_auth_attr_store,
254 };
255
256 TF_CIT_SETUP(tpg_nacl_auth, &target_fabric_nacl_auth_item_ops, NULL, NULL);
257
258 /* End of tfc_tpg_nacl_auth_cit */
259
260 /* Start of tfc_tpg_nacl_param_cit */
261
262 CONFIGFS_EATTR_OPS(target_fabric_nacl_param, se_node_acl, acl_param_group);
263
264 static struct configfs_item_operations target_fabric_nacl_param_item_ops = {
265         .show_attribute         = target_fabric_nacl_param_attr_show,
266         .store_attribute        = target_fabric_nacl_param_attr_store,
267 };
268
269 TF_CIT_SETUP(tpg_nacl_param, &target_fabric_nacl_param_item_ops, NULL, NULL);
270
271 /* End of tfc_tpg_nacl_param_cit */
272
273 /* Start of tfc_tpg_nacl_base_cit */
274
275 CONFIGFS_EATTR_OPS(target_fabric_nacl_base, se_node_acl, acl_group);
276
277 static struct config_group *target_fabric_make_mappedlun(
278         struct config_group *group,
279         const char *name)
280 {
281         struct se_node_acl *se_nacl = container_of(group,
282                         struct se_node_acl, acl_group);
283         struct se_portal_group *se_tpg = se_nacl->se_tpg;
284         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
285         struct se_lun_acl *lacl;
286         struct config_item *acl_ci;
287         char *buf;
288         unsigned long mapped_lun;
289         int ret = 0;
290
291         acl_ci = &group->cg_item;
292         if (!(acl_ci)) {
293                 printk(KERN_ERR "Unable to locatel acl_ci\n");
294                 return NULL;
295         }
296
297         buf = kzalloc(strlen(name) + 1, GFP_KERNEL);
298         if (!(buf)) {
299                 printk(KERN_ERR "Unable to allocate memory for name buf\n");
300                 return ERR_PTR(-ENOMEM);
301         }
302         snprintf(buf, strlen(name) + 1, "%s", name);
303         /*
304          * Make sure user is creating iscsi/$IQN/$TPGT/acls/$INITIATOR/lun_$ID.
305          */
306         if (strstr(buf, "lun_") != buf) {
307                 printk(KERN_ERR "Unable to locate \"lun_\" from buf: %s"
308                         " name: %s\n", buf, name);
309                 ret = -EINVAL;
310                 goto out;
311         }
312         /*
313          * Determine the Mapped LUN value.  This is what the SCSI Initiator
314          * Port will actually see.
315          */
316         if (strict_strtoul(buf + 4, 0, &mapped_lun) || mapped_lun > UINT_MAX) {
317                 ret = -EINVAL;
318                 goto out;
319         }
320
321         lacl = core_dev_init_initiator_node_lun_acl(se_tpg, mapped_lun,
322                         config_item_name(acl_ci), &ret);
323         if (!(lacl))
324                 goto out;
325
326         config_group_init_type_name(&lacl->se_lun_group, name,
327                         &TF_CIT_TMPL(tf)->tfc_tpg_mappedlun_cit);
328
329         kfree(buf);
330         return &lacl->se_lun_group;
331 out:
332         kfree(buf);
333         return ERR_PTR(ret);
334 }
335
336 static void target_fabric_drop_mappedlun(
337         struct config_group *group,
338         struct config_item *item)
339 {
340         struct se_lun_acl *lacl = container_of(to_config_group(item),
341                         struct se_lun_acl, se_lun_group);
342         struct se_portal_group *se_tpg = lacl->se_lun_nacl->se_tpg;
343
344         config_item_put(item);
345         core_dev_free_initiator_node_lun_acl(se_tpg, lacl);
346 }
347
348 static struct configfs_item_operations target_fabric_nacl_base_item_ops = {
349         .show_attribute         = target_fabric_nacl_base_attr_show,
350         .store_attribute        = target_fabric_nacl_base_attr_store,
351 };
352
353 static struct configfs_group_operations target_fabric_nacl_base_group_ops = {
354         .make_group             = target_fabric_make_mappedlun,
355         .drop_item              = target_fabric_drop_mappedlun,
356 };
357
358 TF_CIT_SETUP(tpg_nacl_base, &target_fabric_nacl_base_item_ops,
359                 &target_fabric_nacl_base_group_ops, NULL);
360
361 /* End of tfc_tpg_nacl_base_cit */
362
363 /* Start of tfc_tpg_nacl_cit */
364
365 static struct config_group *target_fabric_make_nodeacl(
366         struct config_group *group,
367         const char *name)
368 {
369         struct se_portal_group *se_tpg = container_of(group,
370                         struct se_portal_group, tpg_acl_group);
371         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
372         struct se_node_acl *se_nacl;
373         struct config_group *nacl_cg;
374
375         if (!(tf->tf_ops.fabric_make_nodeacl)) {
376                 printk(KERN_ERR "tf->tf_ops.fabric_make_nodeacl is NULL\n");
377                 return ERR_PTR(-ENOSYS);
378         }
379
380         se_nacl = tf->tf_ops.fabric_make_nodeacl(se_tpg, group, name);
381         if (IS_ERR(se_nacl))
382                 return ERR_PTR(PTR_ERR(se_nacl));
383
384         nacl_cg = &se_nacl->acl_group;
385         nacl_cg->default_groups = se_nacl->acl_default_groups;
386         nacl_cg->default_groups[0] = &se_nacl->acl_attrib_group;
387         nacl_cg->default_groups[1] = &se_nacl->acl_auth_group;
388         nacl_cg->default_groups[2] = &se_nacl->acl_param_group;
389         nacl_cg->default_groups[3] = NULL;
390
391         config_group_init_type_name(&se_nacl->acl_group, name,
392                         &TF_CIT_TMPL(tf)->tfc_tpg_nacl_base_cit);
393         config_group_init_type_name(&se_nacl->acl_attrib_group, "attrib",
394                         &TF_CIT_TMPL(tf)->tfc_tpg_nacl_attrib_cit);
395         config_group_init_type_name(&se_nacl->acl_auth_group, "auth",
396                         &TF_CIT_TMPL(tf)->tfc_tpg_nacl_auth_cit);
397         config_group_init_type_name(&se_nacl->acl_param_group, "param",
398                         &TF_CIT_TMPL(tf)->tfc_tpg_nacl_param_cit);
399
400         return &se_nacl->acl_group;
401 }
402
403 static void target_fabric_drop_nodeacl(
404         struct config_group *group,
405         struct config_item *item)
406 {
407         struct se_portal_group *se_tpg = container_of(group,
408                         struct se_portal_group, tpg_acl_group);
409         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
410         struct se_node_acl *se_nacl = container_of(to_config_group(item),
411                         struct se_node_acl, acl_group);
412         struct config_item *df_item;
413         struct config_group *nacl_cg;
414         int i;
415
416         nacl_cg = &se_nacl->acl_group;
417         for (i = 0; nacl_cg->default_groups[i]; i++) {
418                 df_item = &nacl_cg->default_groups[i]->cg_item;
419                 nacl_cg->default_groups[i] = NULL;
420                 config_item_put(df_item);
421         }
422
423         config_item_put(item);
424         tf->tf_ops.fabric_drop_nodeacl(se_nacl);
425 }
426
427 static struct configfs_group_operations target_fabric_nacl_group_ops = {
428         .make_group     = target_fabric_make_nodeacl,
429         .drop_item      = target_fabric_drop_nodeacl,
430 };
431
432 TF_CIT_SETUP(tpg_nacl, NULL, &target_fabric_nacl_group_ops, NULL);
433
434 /* End of tfc_tpg_nacl_cit */
435
436 /* Start of tfc_tpg_np_base_cit */
437
438 CONFIGFS_EATTR_OPS(target_fabric_np_base, se_tpg_np, tpg_np_group);
439
440 static struct configfs_item_operations target_fabric_np_base_item_ops = {
441         .show_attribute         = target_fabric_np_base_attr_show,
442         .store_attribute        = target_fabric_np_base_attr_store,
443 };
444
445 TF_CIT_SETUP(tpg_np_base, &target_fabric_np_base_item_ops, NULL, NULL);
446
447 /* End of tfc_tpg_np_base_cit */
448
449 /* Start of tfc_tpg_np_cit */
450
451 static struct config_group *target_fabric_make_np(
452         struct config_group *group,
453         const char *name)
454 {
455         struct se_portal_group *se_tpg = container_of(group,
456                                 struct se_portal_group, tpg_np_group);
457         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
458         struct se_tpg_np *se_tpg_np;
459
460         if (!(tf->tf_ops.fabric_make_np)) {
461                 printk(KERN_ERR "tf->tf_ops.fabric_make_np is NULL\n");
462                 return ERR_PTR(-ENOSYS);
463         }
464
465         se_tpg_np = tf->tf_ops.fabric_make_np(se_tpg, group, name);
466         if (!(se_tpg_np) || IS_ERR(se_tpg_np))
467                 return ERR_PTR(-EINVAL);
468
469         config_group_init_type_name(&se_tpg_np->tpg_np_group, name,
470                         &TF_CIT_TMPL(tf)->tfc_tpg_np_base_cit);
471
472         return &se_tpg_np->tpg_np_group;
473 }
474
475 static void target_fabric_drop_np(
476         struct config_group *group,
477         struct config_item *item)
478 {
479         struct se_portal_group *se_tpg = container_of(group,
480                                 struct se_portal_group, tpg_np_group);
481         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
482         struct se_tpg_np *se_tpg_np = container_of(to_config_group(item),
483                                 struct se_tpg_np, tpg_np_group);
484
485         config_item_put(item);
486         tf->tf_ops.fabric_drop_np(se_tpg_np);
487 }
488
489 static struct configfs_group_operations target_fabric_np_group_ops = {
490         .make_group     = &target_fabric_make_np,
491         .drop_item      = &target_fabric_drop_np,
492 };
493
494 TF_CIT_SETUP(tpg_np, NULL, &target_fabric_np_group_ops, NULL);
495
496 /* End of tfc_tpg_np_cit */
497
498 /* Start of tfc_tpg_port_cit */
499
500 CONFIGFS_EATTR_STRUCT(target_fabric_port, se_lun);
501 #define TCM_PORT_ATTR(_name, _mode)                                     \
502 static struct target_fabric_port_attribute target_fabric_port_##_name = \
503         __CONFIGFS_EATTR(_name, _mode,                                  \
504         target_fabric_port_show_attr_##_name,                           \
505         target_fabric_port_store_attr_##_name);
506
507 #define TCM_PORT_ATTOR_RO(_name)                                        \
508         __CONFIGFS_EATTR_RO(_name,                                      \
509         target_fabric_port_show_attr_##_name);
510
511 /*
512  * alua_tg_pt_gp
513  */
514 static ssize_t target_fabric_port_show_attr_alua_tg_pt_gp(
515         struct se_lun *lun,
516         char *page)
517 {
518         if (!(lun))
519                 return -ENODEV;
520
521         if (!(lun->lun_sep))
522                 return -ENODEV;
523
524         return core_alua_show_tg_pt_gp_info(lun->lun_sep, page);
525 }
526
527 static ssize_t target_fabric_port_store_attr_alua_tg_pt_gp(
528         struct se_lun *lun,
529         const char *page,
530         size_t count)
531 {
532         if (!(lun))
533                 return -ENODEV;
534
535         if (!(lun->lun_sep))
536                 return -ENODEV;
537
538         return core_alua_store_tg_pt_gp_info(lun->lun_sep, page, count);
539 }
540
541 TCM_PORT_ATTR(alua_tg_pt_gp, S_IRUGO | S_IWUSR);
542
543 /*
544  * alua_tg_pt_offline
545  */
546 static ssize_t target_fabric_port_show_attr_alua_tg_pt_offline(
547         struct se_lun *lun,
548         char *page)
549 {
550         if (!(lun))
551                 return -ENODEV;
552
553         if (!(lun->lun_sep))
554                 return -ENODEV;
555
556         return core_alua_show_offline_bit(lun, page);
557 }
558
559 static ssize_t target_fabric_port_store_attr_alua_tg_pt_offline(
560         struct se_lun *lun,
561         const char *page,
562         size_t count)
563 {
564         if (!(lun))
565                 return -ENODEV;
566
567         if (!(lun->lun_sep))
568                 return -ENODEV;
569
570         return core_alua_store_offline_bit(lun, page, count);
571 }
572
573 TCM_PORT_ATTR(alua_tg_pt_offline, S_IRUGO | S_IWUSR);
574
575 /*
576  * alua_tg_pt_status
577  */
578 static ssize_t target_fabric_port_show_attr_alua_tg_pt_status(
579         struct se_lun *lun,
580         char *page)
581 {
582         if (!(lun))
583                 return -ENODEV;
584
585         if (!(lun->lun_sep))
586                 return -ENODEV;
587
588         return core_alua_show_secondary_status(lun, page);
589 }
590
591 static ssize_t target_fabric_port_store_attr_alua_tg_pt_status(
592         struct se_lun *lun,
593         const char *page,
594         size_t count)
595 {
596         if (!(lun))
597                 return -ENODEV;
598
599         if (!(lun->lun_sep))
600                 return -ENODEV;
601
602         return core_alua_store_secondary_status(lun, page, count);
603 }
604
605 TCM_PORT_ATTR(alua_tg_pt_status, S_IRUGO | S_IWUSR);
606
607 /*
608  * alua_tg_pt_write_md
609  */
610 static ssize_t target_fabric_port_show_attr_alua_tg_pt_write_md(
611         struct se_lun *lun,
612         char *page)
613 {
614         if (!(lun))
615                 return -ENODEV;
616
617         if (!(lun->lun_sep))
618                 return -ENODEV;
619
620         return core_alua_show_secondary_write_metadata(lun, page);
621 }
622
623 static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
624         struct se_lun *lun,
625         const char *page,
626         size_t count)
627 {
628         if (!(lun))
629                 return -ENODEV;
630
631         if (!(lun->lun_sep))
632                 return -ENODEV;
633
634         return core_alua_store_secondary_write_metadata(lun, page, count);
635 }
636
637 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
638
639
640 static struct configfs_attribute *target_fabric_port_attrs[] = {
641         &target_fabric_port_alua_tg_pt_gp.attr,
642         &target_fabric_port_alua_tg_pt_offline.attr,
643         &target_fabric_port_alua_tg_pt_status.attr,
644         &target_fabric_port_alua_tg_pt_write_md.attr,
645         NULL,
646 };
647
648 CONFIGFS_EATTR_OPS(target_fabric_port, se_lun, lun_group);
649
650 static int target_fabric_port_link(
651         struct config_item *lun_ci,
652         struct config_item *se_dev_ci)
653 {
654         struct config_item *tpg_ci;
655         struct se_device *dev;
656         struct se_lun *lun = container_of(to_config_group(lun_ci),
657                                 struct se_lun, lun_group);
658         struct se_lun *lun_p;
659         struct se_portal_group *se_tpg;
660         struct se_subsystem_dev *se_dev = container_of(
661                                 to_config_group(se_dev_ci), struct se_subsystem_dev,
662                                 se_dev_group);
663         struct target_fabric_configfs *tf;
664         int ret;
665
666         tpg_ci = &lun_ci->ci_parent->ci_group->cg_item;
667         se_tpg = container_of(to_config_group(tpg_ci),
668                                 struct se_portal_group, tpg_group);
669         tf = se_tpg->se_tpg_wwn->wwn_tf;
670
671         if (lun->lun_se_dev !=  NULL) {
672                 printk(KERN_ERR "Port Symlink already exists\n");
673                 return -EEXIST;
674         }
675
676         dev = se_dev->se_dev_ptr;
677         if (!(dev)) {
678                 printk(KERN_ERR "Unable to locate struct se_device pointer from"
679                         " %s\n", config_item_name(se_dev_ci));
680                 ret = -ENODEV;
681                 goto out;
682         }
683
684         lun_p = core_dev_add_lun(se_tpg, dev->se_hba, dev,
685                                 lun->unpacked_lun);
686         if ((IS_ERR(lun_p)) || !(lun_p)) {
687                 printk(KERN_ERR "core_dev_add_lun() failed\n");
688                 ret = -EINVAL;
689                 goto out;
690         }
691
692         if (tf->tf_ops.fabric_post_link) {
693                 /*
694                  * Call the optional fabric_post_link() to allow a
695                  * fabric module to setup any additional state once
696                  * core_dev_add_lun() has been called..
697                  */
698                 tf->tf_ops.fabric_post_link(se_tpg, lun);
699         }
700
701         return 0;
702 out:
703         return ret;
704 }
705
706 static int target_fabric_port_unlink(
707         struct config_item *lun_ci,
708         struct config_item *se_dev_ci)
709 {
710         struct se_lun *lun = container_of(to_config_group(lun_ci),
711                                 struct se_lun, lun_group);
712         struct se_portal_group *se_tpg = lun->lun_sep->sep_tpg;
713         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
714
715         if (tf->tf_ops.fabric_pre_unlink) {
716                 /*
717                  * Call the optional fabric_pre_unlink() to allow a
718                  * fabric module to release any additional stat before
719                  * core_dev_del_lun() is called.
720                 */
721                 tf->tf_ops.fabric_pre_unlink(se_tpg, lun);
722         }
723
724         core_dev_del_lun(se_tpg, lun->unpacked_lun);
725         return 0;
726 }
727
728 static struct configfs_item_operations target_fabric_port_item_ops = {
729         .show_attribute         = target_fabric_port_attr_show,
730         .store_attribute        = target_fabric_port_attr_store,
731         .allow_link             = target_fabric_port_link,
732         .drop_link              = target_fabric_port_unlink,
733 };
734
735 TF_CIT_SETUP(tpg_port, &target_fabric_port_item_ops, NULL, target_fabric_port_attrs);
736
737 /* End of tfc_tpg_port_cit */
738
739 /* Start of tfc_tpg_lun_cit */
740
741 static struct config_group *target_fabric_make_lun(
742         struct config_group *group,
743         const char *name)
744 {
745         struct se_lun *lun;
746         struct se_portal_group *se_tpg = container_of(group,
747                         struct se_portal_group, tpg_lun_group);
748         struct target_fabric_configfs *tf = se_tpg->se_tpg_wwn->wwn_tf;
749         unsigned long unpacked_lun;
750
751         if (strstr(name, "lun_") != name) {
752                 printk(KERN_ERR "Unable to locate \'_\" in"
753                                 " \"lun_$LUN_NUMBER\"\n");
754                 return ERR_PTR(-EINVAL);
755         }
756         if (strict_strtoul(name + 4, 0, &unpacked_lun) || unpacked_lun > UINT_MAX)
757                 return ERR_PTR(-EINVAL);
758
759         lun = core_get_lun_from_tpg(se_tpg, unpacked_lun);
760         if (!(lun))
761                 return ERR_PTR(-EINVAL);
762
763         config_group_init_type_name(&lun->lun_group, name,
764                         &TF_CIT_TMPL(tf)->tfc_tpg_port_cit);
765
766         return &lun->lun_group;
767 }
768
769 static void target_fabric_drop_lun(
770         struct config_group *group,
771         struct config_item *item)
772 {
773         config_item_put(item);
774 }
775
776 static struct configfs_group_operations target_fabric_lun_group_ops = {
777         .make_group     = &target_fabric_make_lun,
778         .drop_item      = &target_fabric_drop_lun,
779 };
780
781 TF_CIT_SETUP(tpg_lun, NULL, &target_fabric_lun_group_ops, NULL);
782
783 /* End of tfc_tpg_lun_cit */
784
785 /* Start of tfc_tpg_attrib_cit */
786
787 CONFIGFS_EATTR_OPS(target_fabric_tpg_attrib, se_portal_group, tpg_attrib_group);
788
789 static struct configfs_item_operations target_fabric_tpg_attrib_item_ops = {
790         .show_attribute         = target_fabric_tpg_attrib_attr_show,
791         .store_attribute        = target_fabric_tpg_attrib_attr_store,
792 };
793
794 TF_CIT_SETUP(tpg_attrib, &target_fabric_tpg_attrib_item_ops, NULL, NULL);
795
796 /* End of tfc_tpg_attrib_cit */
797
798 /* Start of tfc_tpg_param_cit */
799
800 CONFIGFS_EATTR_OPS(target_fabric_tpg_param, se_portal_group, tpg_param_group);
801
802 static struct configfs_item_operations target_fabric_tpg_param_item_ops = {
803         .show_attribute         = target_fabric_tpg_param_attr_show,
804         .store_attribute        = target_fabric_tpg_param_attr_store,
805 };
806
807 TF_CIT_SETUP(tpg_param, &target_fabric_tpg_param_item_ops, NULL, NULL);
808
809 /* End of tfc_tpg_param_cit */
810
811 /* Start of tfc_tpg_base_cit */
812 /*
813  * For use with TF_TPG_ATTR() and TF_TPG_ATTR_RO()
814  */
815 CONFIGFS_EATTR_OPS(target_fabric_tpg, se_portal_group, tpg_group);
816
817 static struct configfs_item_operations target_fabric_tpg_base_item_ops = {
818         .show_attribute         = target_fabric_tpg_attr_show,
819         .store_attribute        = target_fabric_tpg_attr_store,
820 };
821
822 TF_CIT_SETUP(tpg_base, &target_fabric_tpg_base_item_ops, NULL, NULL);
823
824 /* End of tfc_tpg_base_cit */
825
826 /* Start of tfc_tpg_cit */
827
828 static struct config_group *target_fabric_make_tpg(
829         struct config_group *group,
830         const char *name)
831 {
832         struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
833         struct target_fabric_configfs *tf = wwn->wwn_tf;
834         struct se_portal_group *se_tpg;
835
836         if (!(tf->tf_ops.fabric_make_tpg)) {
837                 printk(KERN_ERR "tf->tf_ops.fabric_make_tpg is NULL\n");
838                 return ERR_PTR(-ENOSYS);
839         }
840
841         se_tpg = tf->tf_ops.fabric_make_tpg(wwn, group, name);
842         if (!(se_tpg) || IS_ERR(se_tpg))
843                 return ERR_PTR(-EINVAL);
844         /*
845          * Setup default groups from pre-allocated se_tpg->tpg_default_groups
846          */
847         se_tpg->tpg_group.default_groups = se_tpg->tpg_default_groups;
848         se_tpg->tpg_group.default_groups[0] = &se_tpg->tpg_lun_group;
849         se_tpg->tpg_group.default_groups[1] = &se_tpg->tpg_np_group;
850         se_tpg->tpg_group.default_groups[2] = &se_tpg->tpg_acl_group;
851         se_tpg->tpg_group.default_groups[3] = &se_tpg->tpg_attrib_group;
852         se_tpg->tpg_group.default_groups[4] = &se_tpg->tpg_param_group;
853         se_tpg->tpg_group.default_groups[5] = NULL;
854
855         config_group_init_type_name(&se_tpg->tpg_group, name,
856                         &TF_CIT_TMPL(tf)->tfc_tpg_base_cit);
857         config_group_init_type_name(&se_tpg->tpg_lun_group, "lun",
858                         &TF_CIT_TMPL(tf)->tfc_tpg_lun_cit);
859         config_group_init_type_name(&se_tpg->tpg_np_group, "np",
860                         &TF_CIT_TMPL(tf)->tfc_tpg_np_cit);
861         config_group_init_type_name(&se_tpg->tpg_acl_group, "acls",
862                         &TF_CIT_TMPL(tf)->tfc_tpg_nacl_cit);
863         config_group_init_type_name(&se_tpg->tpg_attrib_group, "attrib",
864                         &TF_CIT_TMPL(tf)->tfc_tpg_attrib_cit);
865         config_group_init_type_name(&se_tpg->tpg_param_group, "param",
866                         &TF_CIT_TMPL(tf)->tfc_tpg_param_cit);
867
868         return &se_tpg->tpg_group;
869 }
870
871 static void target_fabric_drop_tpg(
872         struct config_group *group,
873         struct config_item *item)
874 {
875         struct se_wwn *wwn = container_of(group, struct se_wwn, wwn_group);
876         struct target_fabric_configfs *tf = wwn->wwn_tf;
877         struct se_portal_group *se_tpg = container_of(to_config_group(item),
878                                 struct se_portal_group, tpg_group);
879         struct config_group *tpg_cg = &se_tpg->tpg_group;
880         struct config_item *df_item;
881         int i;
882         /*
883          * Release default groups, but do not release tpg_cg->default_groups
884          * memory as it is statically allocated at se_tpg->tpg_default_groups.
885          */
886         for (i = 0; tpg_cg->default_groups[i]; i++) {
887                 df_item = &tpg_cg->default_groups[i]->cg_item;
888                 tpg_cg->default_groups[i] = NULL;
889                 config_item_put(df_item);
890         }
891
892         config_item_put(item);
893         tf->tf_ops.fabric_drop_tpg(se_tpg);
894 }
895
896 static struct configfs_group_operations target_fabric_tpg_group_ops = {
897         .make_group     = target_fabric_make_tpg,
898         .drop_item      = target_fabric_drop_tpg,
899 };
900
901 TF_CIT_SETUP(tpg, NULL, &target_fabric_tpg_group_ops, NULL);
902
903 /* End of tfc_tpg_cit */
904
905 /* Start of tfc_wwn_cit */
906
907 static struct config_group *target_fabric_make_wwn(
908         struct config_group *group,
909         const char *name)
910 {
911         struct target_fabric_configfs *tf = container_of(group,
912                                 struct target_fabric_configfs, tf_group);
913         struct se_wwn *wwn;
914
915         if (!(tf->tf_ops.fabric_make_wwn)) {
916                 printk(KERN_ERR "tf->tf_ops.fabric_make_wwn is NULL\n");
917                 return ERR_PTR(-ENOSYS);
918         }
919
920         wwn = tf->tf_ops.fabric_make_wwn(tf, group, name);
921         if (!(wwn) || IS_ERR(wwn))
922                 return ERR_PTR(-EINVAL);
923
924         wwn->wwn_tf = tf;
925         config_group_init_type_name(&wwn->wwn_group, name,
926                         &TF_CIT_TMPL(tf)->tfc_tpg_cit);
927
928         return &wwn->wwn_group;
929 }
930
931 static void target_fabric_drop_wwn(
932         struct config_group *group,
933         struct config_item *item)
934 {
935         struct target_fabric_configfs *tf = container_of(group,
936                                 struct target_fabric_configfs, tf_group);
937         struct se_wwn *wwn = container_of(to_config_group(item),
938                                 struct se_wwn, wwn_group);
939
940         config_item_put(item);
941         tf->tf_ops.fabric_drop_wwn(wwn);
942 }
943
944 static struct configfs_group_operations target_fabric_wwn_group_ops = {
945         .make_group     = target_fabric_make_wwn,
946         .drop_item      = target_fabric_drop_wwn,
947 };
948 /*
949  * For use with TF_WWN_ATTR() and TF_WWN_ATTR_RO()
950  */
951 CONFIGFS_EATTR_OPS(target_fabric_wwn, target_fabric_configfs, tf_group);
952
953 static struct configfs_item_operations target_fabric_wwn_item_ops = {
954         .show_attribute         = target_fabric_wwn_attr_show,
955         .store_attribute        = target_fabric_wwn_attr_store,
956 };
957
958 TF_CIT_SETUP(wwn, &target_fabric_wwn_item_ops, &target_fabric_wwn_group_ops, NULL);
959
960 /* End of tfc_wwn_cit */
961
962 /* Start of tfc_discovery_cit */
963
964 CONFIGFS_EATTR_OPS(target_fabric_discovery, target_fabric_configfs,
965                 tf_disc_group);
966
967 static struct configfs_item_operations target_fabric_discovery_item_ops = {
968         .show_attribute         = target_fabric_discovery_attr_show,
969         .store_attribute        = target_fabric_discovery_attr_store,
970 };
971
972 TF_CIT_SETUP(discovery, &target_fabric_discovery_item_ops, NULL, NULL);
973
974 /* End of tfc_discovery_cit */
975
976 int target_fabric_setup_cits(struct target_fabric_configfs *tf)
977 {
978         target_fabric_setup_discovery_cit(tf);
979         target_fabric_setup_wwn_cit(tf);
980         target_fabric_setup_tpg_cit(tf);
981         target_fabric_setup_tpg_base_cit(tf);
982         target_fabric_setup_tpg_port_cit(tf);
983         target_fabric_setup_tpg_lun_cit(tf);
984         target_fabric_setup_tpg_np_cit(tf);
985         target_fabric_setup_tpg_np_base_cit(tf);
986         target_fabric_setup_tpg_attrib_cit(tf);
987         target_fabric_setup_tpg_param_cit(tf);
988         target_fabric_setup_tpg_nacl_cit(tf);
989         target_fabric_setup_tpg_nacl_base_cit(tf);
990         target_fabric_setup_tpg_nacl_attrib_cit(tf);
991         target_fabric_setup_tpg_nacl_auth_cit(tf);
992         target_fabric_setup_tpg_nacl_param_cit(tf);
993         target_fabric_setup_tpg_mappedlun_cit(tf);
994
995         return 0;
996 }