Merge commit 'ed30f24e8d07d30aa3e69d1f508f4d7bd2e8ea14' of git://git.linaro.org/landi...
[firefly-linux-kernel-4.4.55.git] / drivers / target / target_core_configfs.c
1 /*******************************************************************************
2  * Filename:  target_core_configfs.c
3  *
4  * This file contains ConfigFS logic for the Generic Target Engine project.
5  *
6  * (c) Copyright 2008-2012 RisingTide Systems LLC.
7  *
8  * Nicholas A. Bellinger <nab@kernel.org>
9  *
10  * based on configfs Copyright (C) 2005 Oracle.  All rights reserved.
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 <generated/utsrelease.h>
26 #include <linux/utsname.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/namei.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/delay.h>
33 #include <linux/unistd.h>
34 #include <linux/string.h>
35 #include <linux/parser.h>
36 #include <linux/syscalls.h>
37 #include <linux/configfs.h>
38 #include <linux/spinlock.h>
39
40 #include <target/target_core_base.h>
41 #include <target/target_core_backend.h>
42 #include <target/target_core_fabric.h>
43 #include <target/target_core_fabric_configfs.h>
44 #include <target/target_core_configfs.h>
45 #include <target/configfs_macros.h>
46
47 #include "target_core_internal.h"
48 #include "target_core_alua.h"
49 #include "target_core_pr.h"
50 #include "target_core_rd.h"
51
52 extern struct t10_alua_lu_gp *default_lu_gp;
53
54 static LIST_HEAD(g_tf_list);
55 static DEFINE_MUTEX(g_tf_lock);
56
57 struct target_core_configfs_attribute {
58         struct configfs_attribute attr;
59         ssize_t (*show)(void *, char *);
60         ssize_t (*store)(void *, const char *, size_t);
61 };
62
63 static struct config_group target_core_hbagroup;
64 static struct config_group alua_group;
65 static struct config_group alua_lu_gps_group;
66
67 static inline struct se_hba *
68 item_to_hba(struct config_item *item)
69 {
70         return container_of(to_config_group(item), struct se_hba, hba_group);
71 }
72
73 /*
74  * Attributes for /sys/kernel/config/target/
75  */
76 static ssize_t target_core_attr_show(struct config_item *item,
77                                       struct configfs_attribute *attr,
78                                       char *page)
79 {
80         return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
81                 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_CONFIGFS_VERSION,
82                 utsname()->sysname, utsname()->machine);
83 }
84
85 static struct configfs_item_operations target_core_fabric_item_ops = {
86         .show_attribute = target_core_attr_show,
87 };
88
89 static struct configfs_attribute target_core_item_attr_version = {
90         .ca_owner       = THIS_MODULE,
91         .ca_name        = "version",
92         .ca_mode        = S_IRUGO,
93 };
94
95 static struct target_fabric_configfs *target_core_get_fabric(
96         const char *name)
97 {
98         struct target_fabric_configfs *tf;
99
100         if (!name)
101                 return NULL;
102
103         mutex_lock(&g_tf_lock);
104         list_for_each_entry(tf, &g_tf_list, tf_list) {
105                 if (!strcmp(tf->tf_name, name)) {
106                         atomic_inc(&tf->tf_access_cnt);
107                         mutex_unlock(&g_tf_lock);
108                         return tf;
109                 }
110         }
111         mutex_unlock(&g_tf_lock);
112
113         return NULL;
114 }
115
116 /*
117  * Called from struct target_core_group_ops->make_group()
118  */
119 static struct config_group *target_core_register_fabric(
120         struct config_group *group,
121         const char *name)
122 {
123         struct target_fabric_configfs *tf;
124         int ret;
125
126         pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
127                         " %s\n", group, name);
128         /*
129          * Below are some hardcoded request_module() calls to automatically
130          * local fabric modules when the following is called:
131          *
132          * mkdir -p /sys/kernel/config/target/$MODULE_NAME
133          *
134          * Note that this does not limit which TCM fabric module can be
135          * registered, but simply provids auto loading logic for modules with
136          * mkdir(2) system calls with known TCM fabric modules.
137          */
138         if (!strncmp(name, "iscsi", 5)) {
139                 /*
140                  * Automatically load the LIO Target fabric module when the
141                  * following is called:
142                  *
143                  * mkdir -p $CONFIGFS/target/iscsi
144                  */
145                 ret = request_module("iscsi_target_mod");
146                 if (ret < 0) {
147                         pr_err("request_module() failed for"
148                                 " iscsi_target_mod.ko: %d\n", ret);
149                         return ERR_PTR(-EINVAL);
150                 }
151         } else if (!strncmp(name, "loopback", 8)) {
152                 /*
153                  * Automatically load the tcm_loop fabric module when the
154                  * following is called:
155                  *
156                  * mkdir -p $CONFIGFS/target/loopback
157                  */
158                 ret = request_module("tcm_loop");
159                 if (ret < 0) {
160                         pr_err("request_module() failed for"
161                                 " tcm_loop.ko: %d\n", ret);
162                         return ERR_PTR(-EINVAL);
163                 }
164         }
165
166         tf = target_core_get_fabric(name);
167         if (!tf) {
168                 pr_err("target_core_get_fabric() failed for %s\n",
169                         name);
170                 return ERR_PTR(-EINVAL);
171         }
172         pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
173                         " %s\n", tf->tf_name);
174         /*
175          * On a successful target_core_get_fabric() look, the returned
176          * struct target_fabric_configfs *tf will contain a usage reference.
177          */
178         pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
179                         &TF_CIT_TMPL(tf)->tfc_wwn_cit);
180
181         tf->tf_group.default_groups = tf->tf_default_groups;
182         tf->tf_group.default_groups[0] = &tf->tf_disc_group;
183         tf->tf_group.default_groups[1] = NULL;
184
185         config_group_init_type_name(&tf->tf_group, name,
186                         &TF_CIT_TMPL(tf)->tfc_wwn_cit);
187         config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
188                         &TF_CIT_TMPL(tf)->tfc_discovery_cit);
189
190         pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric:"
191                         " %s\n", tf->tf_group.cg_item.ci_name);
192         /*
193          * Setup tf_ops.tf_subsys pointer for usage with configfs_depend_item()
194          */
195         tf->tf_ops.tf_subsys = tf->tf_subsys;
196         tf->tf_fabric = &tf->tf_group.cg_item;
197         pr_debug("Target_Core_ConfigFS: REGISTER -> Set tf->tf_fabric"
198                         " for %s\n", name);
199
200         return &tf->tf_group;
201 }
202
203 /*
204  * Called from struct target_core_group_ops->drop_item()
205  */
206 static void target_core_deregister_fabric(
207         struct config_group *group,
208         struct config_item *item)
209 {
210         struct target_fabric_configfs *tf = container_of(
211                 to_config_group(item), struct target_fabric_configfs, tf_group);
212         struct config_group *tf_group;
213         struct config_item *df_item;
214         int i;
215
216         pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
217                 " tf list\n", config_item_name(item));
218
219         pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
220                         " %s\n", tf->tf_name);
221         atomic_dec(&tf->tf_access_cnt);
222
223         pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing"
224                         " tf->tf_fabric for %s\n", tf->tf_name);
225         tf->tf_fabric = NULL;
226
227         pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
228                         " %s\n", config_item_name(item));
229
230         tf_group = &tf->tf_group;
231         for (i = 0; tf_group->default_groups[i]; i++) {
232                 df_item = &tf_group->default_groups[i]->cg_item;
233                 tf_group->default_groups[i] = NULL;
234                 config_item_put(df_item);
235         }
236         config_item_put(item);
237 }
238
239 static struct configfs_group_operations target_core_fabric_group_ops = {
240         .make_group     = &target_core_register_fabric,
241         .drop_item      = &target_core_deregister_fabric,
242 };
243
244 /*
245  * All item attributes appearing in /sys/kernel/target/ appear here.
246  */
247 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
248         &target_core_item_attr_version,
249         NULL,
250 };
251
252 /*
253  * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
254  */
255 static struct config_item_type target_core_fabrics_item = {
256         .ct_item_ops    = &target_core_fabric_item_ops,
257         .ct_group_ops   = &target_core_fabric_group_ops,
258         .ct_attrs       = target_core_fabric_item_attrs,
259         .ct_owner       = THIS_MODULE,
260 };
261
262 static struct configfs_subsystem target_core_fabrics = {
263         .su_group = {
264                 .cg_item = {
265                         .ci_namebuf = "target",
266                         .ci_type = &target_core_fabrics_item,
267                 },
268         },
269 };
270
271 static struct configfs_subsystem *target_core_subsystem[] = {
272         &target_core_fabrics,
273         NULL,
274 };
275
276 /*##############################################################################
277 // Start functions called by external Target Fabrics Modules
278 //############################################################################*/
279
280 /*
281  * First function called by fabric modules to:
282  *
283  * 1) Allocate a struct target_fabric_configfs and save the *fabric_cit pointer.
284  * 2) Add struct target_fabric_configfs to g_tf_list
285  * 3) Return struct target_fabric_configfs to fabric module to be passed
286  *    into target_fabric_configfs_register().
287  */
288 struct target_fabric_configfs *target_fabric_configfs_init(
289         struct module *fabric_mod,
290         const char *name)
291 {
292         struct target_fabric_configfs *tf;
293
294         if (!(name)) {
295                 pr_err("Unable to locate passed fabric name\n");
296                 return ERR_PTR(-EINVAL);
297         }
298         if (strlen(name) >= TARGET_FABRIC_NAME_SIZE) {
299                 pr_err("Passed name: %s exceeds TARGET_FABRIC"
300                         "_NAME_SIZE\n", name);
301                 return ERR_PTR(-EINVAL);
302         }
303
304         tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
305         if (!tf)
306                 return ERR_PTR(-ENOMEM);
307
308         INIT_LIST_HEAD(&tf->tf_list);
309         atomic_set(&tf->tf_access_cnt, 0);
310         /*
311          * Setup the default generic struct config_item_type's (cits) in
312          * struct target_fabric_configfs->tf_cit_tmpl
313          */
314         tf->tf_module = fabric_mod;
315         target_fabric_setup_cits(tf);
316
317         tf->tf_subsys = target_core_subsystem[0];
318         snprintf(tf->tf_name, TARGET_FABRIC_NAME_SIZE, "%s", name);
319
320         mutex_lock(&g_tf_lock);
321         list_add_tail(&tf->tf_list, &g_tf_list);
322         mutex_unlock(&g_tf_lock);
323
324         pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>"
325                         ">>>>>>>>>>>>>>\n");
326         pr_debug("Initialized struct target_fabric_configfs: %p for"
327                         " %s\n", tf, tf->tf_name);
328         return tf;
329 }
330 EXPORT_SYMBOL(target_fabric_configfs_init);
331
332 /*
333  * Called by fabric plugins after FAILED target_fabric_configfs_register() call.
334  */
335 void target_fabric_configfs_free(
336         struct target_fabric_configfs *tf)
337 {
338         mutex_lock(&g_tf_lock);
339         list_del(&tf->tf_list);
340         mutex_unlock(&g_tf_lock);
341
342         kfree(tf);
343 }
344 EXPORT_SYMBOL(target_fabric_configfs_free);
345
346 /*
347  * Perform a sanity check of the passed tf->tf_ops before completing
348  * TCM fabric module registration.
349  */
350 static int target_fabric_tf_ops_check(
351         struct target_fabric_configfs *tf)
352 {
353         struct target_core_fabric_ops *tfo = &tf->tf_ops;
354
355         if (!tfo->get_fabric_name) {
356                 pr_err("Missing tfo->get_fabric_name()\n");
357                 return -EINVAL;
358         }
359         if (!tfo->get_fabric_proto_ident) {
360                 pr_err("Missing tfo->get_fabric_proto_ident()\n");
361                 return -EINVAL;
362         }
363         if (!tfo->tpg_get_wwn) {
364                 pr_err("Missing tfo->tpg_get_wwn()\n");
365                 return -EINVAL;
366         }
367         if (!tfo->tpg_get_tag) {
368                 pr_err("Missing tfo->tpg_get_tag()\n");
369                 return -EINVAL;
370         }
371         if (!tfo->tpg_get_default_depth) {
372                 pr_err("Missing tfo->tpg_get_default_depth()\n");
373                 return -EINVAL;
374         }
375         if (!tfo->tpg_get_pr_transport_id) {
376                 pr_err("Missing tfo->tpg_get_pr_transport_id()\n");
377                 return -EINVAL;
378         }
379         if (!tfo->tpg_get_pr_transport_id_len) {
380                 pr_err("Missing tfo->tpg_get_pr_transport_id_len()\n");
381                 return -EINVAL;
382         }
383         if (!tfo->tpg_check_demo_mode) {
384                 pr_err("Missing tfo->tpg_check_demo_mode()\n");
385                 return -EINVAL;
386         }
387         if (!tfo->tpg_check_demo_mode_cache) {
388                 pr_err("Missing tfo->tpg_check_demo_mode_cache()\n");
389                 return -EINVAL;
390         }
391         if (!tfo->tpg_check_demo_mode_write_protect) {
392                 pr_err("Missing tfo->tpg_check_demo_mode_write_protect()\n");
393                 return -EINVAL;
394         }
395         if (!tfo->tpg_check_prod_mode_write_protect) {
396                 pr_err("Missing tfo->tpg_check_prod_mode_write_protect()\n");
397                 return -EINVAL;
398         }
399         if (!tfo->tpg_alloc_fabric_acl) {
400                 pr_err("Missing tfo->tpg_alloc_fabric_acl()\n");
401                 return -EINVAL;
402         }
403         if (!tfo->tpg_release_fabric_acl) {
404                 pr_err("Missing tfo->tpg_release_fabric_acl()\n");
405                 return -EINVAL;
406         }
407         if (!tfo->tpg_get_inst_index) {
408                 pr_err("Missing tfo->tpg_get_inst_index()\n");
409                 return -EINVAL;
410         }
411         if (!tfo->release_cmd) {
412                 pr_err("Missing tfo->release_cmd()\n");
413                 return -EINVAL;
414         }
415         if (!tfo->shutdown_session) {
416                 pr_err("Missing tfo->shutdown_session()\n");
417                 return -EINVAL;
418         }
419         if (!tfo->close_session) {
420                 pr_err("Missing tfo->close_session()\n");
421                 return -EINVAL;
422         }
423         if (!tfo->sess_get_index) {
424                 pr_err("Missing tfo->sess_get_index()\n");
425                 return -EINVAL;
426         }
427         if (!tfo->write_pending) {
428                 pr_err("Missing tfo->write_pending()\n");
429                 return -EINVAL;
430         }
431         if (!tfo->write_pending_status) {
432                 pr_err("Missing tfo->write_pending_status()\n");
433                 return -EINVAL;
434         }
435         if (!tfo->set_default_node_attributes) {
436                 pr_err("Missing tfo->set_default_node_attributes()\n");
437                 return -EINVAL;
438         }
439         if (!tfo->get_task_tag) {
440                 pr_err("Missing tfo->get_task_tag()\n");
441                 return -EINVAL;
442         }
443         if (!tfo->get_cmd_state) {
444                 pr_err("Missing tfo->get_cmd_state()\n");
445                 return -EINVAL;
446         }
447         if (!tfo->queue_data_in) {
448                 pr_err("Missing tfo->queue_data_in()\n");
449                 return -EINVAL;
450         }
451         if (!tfo->queue_status) {
452                 pr_err("Missing tfo->queue_status()\n");
453                 return -EINVAL;
454         }
455         if (!tfo->queue_tm_rsp) {
456                 pr_err("Missing tfo->queue_tm_rsp()\n");
457                 return -EINVAL;
458         }
459         /*
460          * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
461          * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
462          * target_core_fabric_configfs.c WWN+TPG group context code.
463          */
464         if (!tfo->fabric_make_wwn) {
465                 pr_err("Missing tfo->fabric_make_wwn()\n");
466                 return -EINVAL;
467         }
468         if (!tfo->fabric_drop_wwn) {
469                 pr_err("Missing tfo->fabric_drop_wwn()\n");
470                 return -EINVAL;
471         }
472         if (!tfo->fabric_make_tpg) {
473                 pr_err("Missing tfo->fabric_make_tpg()\n");
474                 return -EINVAL;
475         }
476         if (!tfo->fabric_drop_tpg) {
477                 pr_err("Missing tfo->fabric_drop_tpg()\n");
478                 return -EINVAL;
479         }
480
481         return 0;
482 }
483
484 /*
485  * Called 2nd from fabric module with returned parameter of
486  * struct target_fabric_configfs * from target_fabric_configfs_init().
487  *
488  * Upon a successful registration, the new fabric's struct config_item is
489  * return.  Also, a pointer to this struct is set in the passed
490  * struct target_fabric_configfs.
491  */
492 int target_fabric_configfs_register(
493         struct target_fabric_configfs *tf)
494 {
495         int ret;
496
497         if (!tf) {
498                 pr_err("Unable to locate target_fabric_configfs"
499                         " pointer\n");
500                 return -EINVAL;
501         }
502         if (!tf->tf_subsys) {
503                 pr_err("Unable to target struct config_subsystem"
504                         " pointer\n");
505                 return -EINVAL;
506         }
507         ret = target_fabric_tf_ops_check(tf);
508         if (ret < 0)
509                 return ret;
510
511         pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>"
512                 ">>>>>>>>>>\n");
513         return 0;
514 }
515 EXPORT_SYMBOL(target_fabric_configfs_register);
516
517 void target_fabric_configfs_deregister(
518         struct target_fabric_configfs *tf)
519 {
520         struct configfs_subsystem *su;
521
522         if (!tf) {
523                 pr_err("Unable to locate passed target_fabric_"
524                         "configfs\n");
525                 return;
526         }
527         su = tf->tf_subsys;
528         if (!su) {
529                 pr_err("Unable to locate passed tf->tf_subsys"
530                         " pointer\n");
531                 return;
532         }
533         pr_debug("<<<<<<<<<<<<<<<<<<<<<< BEGIN FABRIC API >>>>>>>>>>"
534                         ">>>>>>>>>>>>\n");
535         mutex_lock(&g_tf_lock);
536         if (atomic_read(&tf->tf_access_cnt)) {
537                 mutex_unlock(&g_tf_lock);
538                 pr_err("Non zero tf->tf_access_cnt for fabric %s\n",
539                         tf->tf_name);
540                 BUG();
541         }
542         list_del(&tf->tf_list);
543         mutex_unlock(&g_tf_lock);
544
545         pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing tf:"
546                         " %s\n", tf->tf_name);
547         tf->tf_module = NULL;
548         tf->tf_subsys = NULL;
549         kfree(tf);
550
551         pr_debug("<<<<<<<<<<<<<<<<<<<<<< END FABRIC API >>>>>>>>>>>>>>>>>"
552                         ">>>>>\n");
553 }
554 EXPORT_SYMBOL(target_fabric_configfs_deregister);
555
556 /*##############################################################################
557 // Stop functions called by external Target Fabrics Modules
558 //############################################################################*/
559
560 /* Start functions for struct config_item_type target_core_dev_attrib_cit */
561
562 #define DEF_DEV_ATTRIB_SHOW(_name)                                      \
563 static ssize_t target_core_dev_show_attr_##_name(                       \
564         struct se_dev_attrib *da,                                       \
565         char *page)                                                     \
566 {                                                                       \
567         return snprintf(page, PAGE_SIZE, "%u\n",                        \
568                 (u32)da->da_dev->dev_attrib._name);                     \
569 }
570
571 #define DEF_DEV_ATTRIB_STORE(_name)                                     \
572 static ssize_t target_core_dev_store_attr_##_name(                      \
573         struct se_dev_attrib *da,                                       \
574         const char *page,                                               \
575         size_t count)                                                   \
576 {                                                                       \
577         unsigned long val;                                              \
578         int ret;                                                        \
579                                                                         \
580         ret = strict_strtoul(page, 0, &val);                            \
581         if (ret < 0) {                                                  \
582                 pr_err("strict_strtoul() failed with"           \
583                         " ret: %d\n", ret);                             \
584                 return -EINVAL;                                         \
585         }                                                               \
586         ret = se_dev_set_##_name(da->da_dev, (u32)val);                 \
587                                                                         \
588         return (!ret) ? count : -EINVAL;                                \
589 }
590
591 #define DEF_DEV_ATTRIB(_name)                                           \
592 DEF_DEV_ATTRIB_SHOW(_name);                                             \
593 DEF_DEV_ATTRIB_STORE(_name);
594
595 #define DEF_DEV_ATTRIB_RO(_name)                                        \
596 DEF_DEV_ATTRIB_SHOW(_name);
597
598 CONFIGFS_EATTR_STRUCT(target_core_dev_attrib, se_dev_attrib);
599 #define SE_DEV_ATTR(_name, _mode)                                       \
600 static struct target_core_dev_attrib_attribute                          \
601                         target_core_dev_attrib_##_name =                \
602                 __CONFIGFS_EATTR(_name, _mode,                          \
603                 target_core_dev_show_attr_##_name,                      \
604                 target_core_dev_store_attr_##_name);
605
606 #define SE_DEV_ATTR_RO(_name);                                          \
607 static struct target_core_dev_attrib_attribute                          \
608                         target_core_dev_attrib_##_name =                \
609         __CONFIGFS_EATTR_RO(_name,                                      \
610         target_core_dev_show_attr_##_name);
611
612 DEF_DEV_ATTRIB(emulate_model_alias);
613 SE_DEV_ATTR(emulate_model_alias, S_IRUGO | S_IWUSR);
614
615 DEF_DEV_ATTRIB(emulate_dpo);
616 SE_DEV_ATTR(emulate_dpo, S_IRUGO | S_IWUSR);
617
618 DEF_DEV_ATTRIB(emulate_fua_write);
619 SE_DEV_ATTR(emulate_fua_write, S_IRUGO | S_IWUSR);
620
621 DEF_DEV_ATTRIB(emulate_fua_read);
622 SE_DEV_ATTR(emulate_fua_read, S_IRUGO | S_IWUSR);
623
624 DEF_DEV_ATTRIB(emulate_write_cache);
625 SE_DEV_ATTR(emulate_write_cache, S_IRUGO | S_IWUSR);
626
627 DEF_DEV_ATTRIB(emulate_ua_intlck_ctrl);
628 SE_DEV_ATTR(emulate_ua_intlck_ctrl, S_IRUGO | S_IWUSR);
629
630 DEF_DEV_ATTRIB(emulate_tas);
631 SE_DEV_ATTR(emulate_tas, S_IRUGO | S_IWUSR);
632
633 DEF_DEV_ATTRIB(emulate_tpu);
634 SE_DEV_ATTR(emulate_tpu, S_IRUGO | S_IWUSR);
635
636 DEF_DEV_ATTRIB(emulate_tpws);
637 SE_DEV_ATTR(emulate_tpws, S_IRUGO | S_IWUSR);
638
639 DEF_DEV_ATTRIB(enforce_pr_isids);
640 SE_DEV_ATTR(enforce_pr_isids, S_IRUGO | S_IWUSR);
641
642 DEF_DEV_ATTRIB(is_nonrot);
643 SE_DEV_ATTR(is_nonrot, S_IRUGO | S_IWUSR);
644
645 DEF_DEV_ATTRIB(emulate_rest_reord);
646 SE_DEV_ATTR(emulate_rest_reord, S_IRUGO | S_IWUSR);
647
648 DEF_DEV_ATTRIB_RO(hw_block_size);
649 SE_DEV_ATTR_RO(hw_block_size);
650
651 DEF_DEV_ATTRIB(block_size);
652 SE_DEV_ATTR(block_size, S_IRUGO | S_IWUSR);
653
654 DEF_DEV_ATTRIB_RO(hw_max_sectors);
655 SE_DEV_ATTR_RO(hw_max_sectors);
656
657 DEF_DEV_ATTRIB(fabric_max_sectors);
658 SE_DEV_ATTR(fabric_max_sectors, S_IRUGO | S_IWUSR);
659
660 DEF_DEV_ATTRIB(optimal_sectors);
661 SE_DEV_ATTR(optimal_sectors, S_IRUGO | S_IWUSR);
662
663 DEF_DEV_ATTRIB_RO(hw_queue_depth);
664 SE_DEV_ATTR_RO(hw_queue_depth);
665
666 DEF_DEV_ATTRIB(queue_depth);
667 SE_DEV_ATTR(queue_depth, S_IRUGO | S_IWUSR);
668
669 DEF_DEV_ATTRIB(max_unmap_lba_count);
670 SE_DEV_ATTR(max_unmap_lba_count, S_IRUGO | S_IWUSR);
671
672 DEF_DEV_ATTRIB(max_unmap_block_desc_count);
673 SE_DEV_ATTR(max_unmap_block_desc_count, S_IRUGO | S_IWUSR);
674
675 DEF_DEV_ATTRIB(unmap_granularity);
676 SE_DEV_ATTR(unmap_granularity, S_IRUGO | S_IWUSR);
677
678 DEF_DEV_ATTRIB(unmap_granularity_alignment);
679 SE_DEV_ATTR(unmap_granularity_alignment, S_IRUGO | S_IWUSR);
680
681 DEF_DEV_ATTRIB(max_write_same_len);
682 SE_DEV_ATTR(max_write_same_len, S_IRUGO | S_IWUSR);
683
684 CONFIGFS_EATTR_OPS(target_core_dev_attrib, se_dev_attrib, da_group);
685
686 static struct configfs_attribute *target_core_dev_attrib_attrs[] = {
687         &target_core_dev_attrib_emulate_model_alias.attr,
688         &target_core_dev_attrib_emulate_dpo.attr,
689         &target_core_dev_attrib_emulate_fua_write.attr,
690         &target_core_dev_attrib_emulate_fua_read.attr,
691         &target_core_dev_attrib_emulate_write_cache.attr,
692         &target_core_dev_attrib_emulate_ua_intlck_ctrl.attr,
693         &target_core_dev_attrib_emulate_tas.attr,
694         &target_core_dev_attrib_emulate_tpu.attr,
695         &target_core_dev_attrib_emulate_tpws.attr,
696         &target_core_dev_attrib_enforce_pr_isids.attr,
697         &target_core_dev_attrib_is_nonrot.attr,
698         &target_core_dev_attrib_emulate_rest_reord.attr,
699         &target_core_dev_attrib_hw_block_size.attr,
700         &target_core_dev_attrib_block_size.attr,
701         &target_core_dev_attrib_hw_max_sectors.attr,
702         &target_core_dev_attrib_fabric_max_sectors.attr,
703         &target_core_dev_attrib_optimal_sectors.attr,
704         &target_core_dev_attrib_hw_queue_depth.attr,
705         &target_core_dev_attrib_queue_depth.attr,
706         &target_core_dev_attrib_max_unmap_lba_count.attr,
707         &target_core_dev_attrib_max_unmap_block_desc_count.attr,
708         &target_core_dev_attrib_unmap_granularity.attr,
709         &target_core_dev_attrib_unmap_granularity_alignment.attr,
710         &target_core_dev_attrib_max_write_same_len.attr,
711         NULL,
712 };
713
714 static struct configfs_item_operations target_core_dev_attrib_ops = {
715         .show_attribute         = target_core_dev_attrib_attr_show,
716         .store_attribute        = target_core_dev_attrib_attr_store,
717 };
718
719 static struct config_item_type target_core_dev_attrib_cit = {
720         .ct_item_ops            = &target_core_dev_attrib_ops,
721         .ct_attrs               = target_core_dev_attrib_attrs,
722         .ct_owner               = THIS_MODULE,
723 };
724
725 /* End functions for struct config_item_type target_core_dev_attrib_cit */
726
727 /*  Start functions for struct config_item_type target_core_dev_wwn_cit */
728
729 CONFIGFS_EATTR_STRUCT(target_core_dev_wwn, t10_wwn);
730 #define SE_DEV_WWN_ATTR(_name, _mode)                                   \
731 static struct target_core_dev_wwn_attribute target_core_dev_wwn_##_name = \
732                 __CONFIGFS_EATTR(_name, _mode,                          \
733                 target_core_dev_wwn_show_attr_##_name,                  \
734                 target_core_dev_wwn_store_attr_##_name);
735
736 #define SE_DEV_WWN_ATTR_RO(_name);                                      \
737 do {                                                                    \
738         static struct target_core_dev_wwn_attribute                     \
739                         target_core_dev_wwn_##_name =                   \
740                 __CONFIGFS_EATTR_RO(_name,                              \
741                 target_core_dev_wwn_show_attr_##_name);                 \
742 } while (0);
743
744 /*
745  * VPD page 0x80 Unit serial
746  */
747 static ssize_t target_core_dev_wwn_show_attr_vpd_unit_serial(
748         struct t10_wwn *t10_wwn,
749         char *page)
750 {
751         return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
752                 &t10_wwn->unit_serial[0]);
753 }
754
755 static ssize_t target_core_dev_wwn_store_attr_vpd_unit_serial(
756         struct t10_wwn *t10_wwn,
757         const char *page,
758         size_t count)
759 {
760         struct se_device *dev = t10_wwn->t10_dev;
761         unsigned char buf[INQUIRY_VPD_SERIAL_LEN];
762
763         /*
764          * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
765          * from the struct scsi_device level firmware, do not allow
766          * VPD Unit Serial to be emulated.
767          *
768          * Note this struct scsi_device could also be emulating VPD
769          * information from its drivers/scsi LLD.  But for now we assume
770          * it is doing 'the right thing' wrt a world wide unique
771          * VPD Unit Serial Number that OS dependent multipath can depend on.
772          */
773         if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
774                 pr_err("Underlying SCSI device firmware provided VPD"
775                         " Unit Serial, ignoring request\n");
776                 return -EOPNOTSUPP;
777         }
778
779         if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
780                 pr_err("Emulated VPD Unit Serial exceeds"
781                 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
782                 return -EOVERFLOW;
783         }
784         /*
785          * Check to see if any active $FABRIC_MOD exports exist.  If they
786          * do exist, fail here as changing this information on the fly
787          * (underneath the initiator side OS dependent multipath code)
788          * could cause negative effects.
789          */
790         if (dev->export_count) {
791                 pr_err("Unable to set VPD Unit Serial while"
792                         " active %d $FABRIC_MOD exports exist\n",
793                         dev->export_count);
794                 return -EINVAL;
795         }
796
797         /*
798          * This currently assumes ASCII encoding for emulated VPD Unit Serial.
799          *
800          * Also, strip any newline added from the userspace
801          * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
802          */
803         memset(buf, 0, INQUIRY_VPD_SERIAL_LEN);
804         snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
805         snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
806                         "%s", strstrip(buf));
807         dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
808
809         pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
810                         " %s\n", dev->t10_wwn.unit_serial);
811
812         return count;
813 }
814
815 SE_DEV_WWN_ATTR(vpd_unit_serial, S_IRUGO | S_IWUSR);
816
817 /*
818  * VPD page 0x83 Protocol Identifier
819  */
820 static ssize_t target_core_dev_wwn_show_attr_vpd_protocol_identifier(
821         struct t10_wwn *t10_wwn,
822         char *page)
823 {
824         struct t10_vpd *vpd;
825         unsigned char buf[VPD_TMP_BUF_SIZE];
826         ssize_t len = 0;
827
828         memset(buf, 0, VPD_TMP_BUF_SIZE);
829
830         spin_lock(&t10_wwn->t10_vpd_lock);
831         list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
832                 if (!vpd->protocol_identifier_set)
833                         continue;
834
835                 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
836
837                 if (len + strlen(buf) >= PAGE_SIZE)
838                         break;
839
840                 len += sprintf(page+len, "%s", buf);
841         }
842         spin_unlock(&t10_wwn->t10_vpd_lock);
843
844         return len;
845 }
846
847 static ssize_t target_core_dev_wwn_store_attr_vpd_protocol_identifier(
848         struct t10_wwn *t10_wwn,
849         const char *page,
850         size_t count)
851 {
852         return -ENOSYS;
853 }
854
855 SE_DEV_WWN_ATTR(vpd_protocol_identifier, S_IRUGO | S_IWUSR);
856
857 /*
858  * Generic wrapper for dumping VPD identifiers by association.
859  */
860 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc)                           \
861 static ssize_t target_core_dev_wwn_show_attr_##_name(                   \
862         struct t10_wwn *t10_wwn,                                        \
863         char *page)                                                     \
864 {                                                                       \
865         struct t10_vpd *vpd;                                                    \
866         unsigned char buf[VPD_TMP_BUF_SIZE];                            \
867         ssize_t len = 0;                                                \
868                                                                         \
869         spin_lock(&t10_wwn->t10_vpd_lock);                              \
870         list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {    \
871                 if (vpd->association != _assoc)                         \
872                         continue;                                       \
873                                                                         \
874                 memset(buf, 0, VPD_TMP_BUF_SIZE);                       \
875                 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE);   \
876                 if (len + strlen(buf) >= PAGE_SIZE)                     \
877                         break;                                          \
878                 len += sprintf(page+len, "%s", buf);                    \
879                                                                         \
880                 memset(buf, 0, VPD_TMP_BUF_SIZE);                       \
881                 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
882                 if (len + strlen(buf) >= PAGE_SIZE)                     \
883                         break;                                          \
884                 len += sprintf(page+len, "%s", buf);                    \
885                                                                         \
886                 memset(buf, 0, VPD_TMP_BUF_SIZE);                       \
887                 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
888                 if (len + strlen(buf) >= PAGE_SIZE)                     \
889                         break;                                          \
890                 len += sprintf(page+len, "%s", buf);                    \
891         }                                                               \
892         spin_unlock(&t10_wwn->t10_vpd_lock);                            \
893                                                                         \
894         return len;                                                     \
895 }
896
897 /*
898  * VPD page 0x83 Association: Logical Unit
899  */
900 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
901
902 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_logical_unit(
903         struct t10_wwn *t10_wwn,
904         const char *page,
905         size_t count)
906 {
907         return -ENOSYS;
908 }
909
910 SE_DEV_WWN_ATTR(vpd_assoc_logical_unit, S_IRUGO | S_IWUSR);
911
912 /*
913  * VPD page 0x83 Association: Target Port
914  */
915 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
916
917 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_target_port(
918         struct t10_wwn *t10_wwn,
919         const char *page,
920         size_t count)
921 {
922         return -ENOSYS;
923 }
924
925 SE_DEV_WWN_ATTR(vpd_assoc_target_port, S_IRUGO | S_IWUSR);
926
927 /*
928  * VPD page 0x83 Association: SCSI Target Device
929  */
930 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
931
932 static ssize_t target_core_dev_wwn_store_attr_vpd_assoc_scsi_target_device(
933         struct t10_wwn *t10_wwn,
934         const char *page,
935         size_t count)
936 {
937         return -ENOSYS;
938 }
939
940 SE_DEV_WWN_ATTR(vpd_assoc_scsi_target_device, S_IRUGO | S_IWUSR);
941
942 CONFIGFS_EATTR_OPS(target_core_dev_wwn, t10_wwn, t10_wwn_group);
943
944 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
945         &target_core_dev_wwn_vpd_unit_serial.attr,
946         &target_core_dev_wwn_vpd_protocol_identifier.attr,
947         &target_core_dev_wwn_vpd_assoc_logical_unit.attr,
948         &target_core_dev_wwn_vpd_assoc_target_port.attr,
949         &target_core_dev_wwn_vpd_assoc_scsi_target_device.attr,
950         NULL,
951 };
952
953 static struct configfs_item_operations target_core_dev_wwn_ops = {
954         .show_attribute         = target_core_dev_wwn_attr_show,
955         .store_attribute        = target_core_dev_wwn_attr_store,
956 };
957
958 static struct config_item_type target_core_dev_wwn_cit = {
959         .ct_item_ops            = &target_core_dev_wwn_ops,
960         .ct_attrs               = target_core_dev_wwn_attrs,
961         .ct_owner               = THIS_MODULE,
962 };
963
964 /*  End functions for struct config_item_type target_core_dev_wwn_cit */
965
966 /*  Start functions for struct config_item_type target_core_dev_pr_cit */
967
968 CONFIGFS_EATTR_STRUCT(target_core_dev_pr, se_device);
969 #define SE_DEV_PR_ATTR(_name, _mode)                                    \
970 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
971         __CONFIGFS_EATTR(_name, _mode,                                  \
972         target_core_dev_pr_show_attr_##_name,                           \
973         target_core_dev_pr_store_attr_##_name);
974
975 #define SE_DEV_PR_ATTR_RO(_name);                                       \
976 static struct target_core_dev_pr_attribute target_core_dev_pr_##_name = \
977         __CONFIGFS_EATTR_RO(_name,                                      \
978         target_core_dev_pr_show_attr_##_name);
979
980 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
981                 char *page)
982 {
983         struct se_node_acl *se_nacl;
984         struct t10_pr_registration *pr_reg;
985         char i_buf[PR_REG_ISID_ID_LEN];
986         int prf_isid;
987
988         memset(i_buf, 0, PR_REG_ISID_ID_LEN);
989
990         pr_reg = dev->dev_pr_res_holder;
991         if (!pr_reg)
992                 return sprintf(page, "No SPC-3 Reservation holder\n");
993
994         se_nacl = pr_reg->pr_reg_nacl;
995         prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
996                                 PR_REG_ISID_ID_LEN);
997
998         return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
999                 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1000                 se_nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
1001 }
1002
1003 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1004                 char *page)
1005 {
1006         struct se_node_acl *se_nacl;
1007         ssize_t len;
1008
1009         se_nacl = dev->dev_reserved_node_acl;
1010         if (se_nacl) {
1011                 len = sprintf(page,
1012                               "SPC-2 Reservation: %s Initiator: %s\n",
1013                               se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1014                               se_nacl->initiatorname);
1015         } else {
1016                 len = sprintf(page, "No SPC-2 Reservation holder\n");
1017         }
1018         return len;
1019 }
1020
1021 static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1022                 char *page)
1023 {
1024         int ret;
1025
1026         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1027                 return sprintf(page, "Passthrough\n");
1028
1029         spin_lock(&dev->dev_reservation_lock);
1030         if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1031                 ret = target_core_dev_pr_show_spc2_res(dev, page);
1032         else
1033                 ret = target_core_dev_pr_show_spc3_res(dev, page);
1034         spin_unlock(&dev->dev_reservation_lock);
1035         return ret;
1036 }
1037
1038 SE_DEV_PR_ATTR_RO(res_holder);
1039
1040 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1041                 struct se_device *dev, char *page)
1042 {
1043         ssize_t len = 0;
1044
1045         spin_lock(&dev->dev_reservation_lock);
1046         if (!dev->dev_pr_res_holder) {
1047                 len = sprintf(page, "No SPC-3 Reservation holder\n");
1048         } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1049                 len = sprintf(page, "SPC-3 Reservation: All Target"
1050                         " Ports registration\n");
1051         } else {
1052                 len = sprintf(page, "SPC-3 Reservation: Single"
1053                         " Target Port registration\n");
1054         }
1055
1056         spin_unlock(&dev->dev_reservation_lock);
1057         return len;
1058 }
1059
1060 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1061
1062 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1063                 struct se_device *dev, char *page)
1064 {
1065         return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
1066 }
1067
1068 SE_DEV_PR_ATTR_RO(res_pr_generation);
1069
1070 /*
1071  * res_pr_holder_tg_port
1072  */
1073 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1074                 struct se_device *dev, char *page)
1075 {
1076         struct se_node_acl *se_nacl;
1077         struct se_lun *lun;
1078         struct se_portal_group *se_tpg;
1079         struct t10_pr_registration *pr_reg;
1080         struct target_core_fabric_ops *tfo;
1081         ssize_t len = 0;
1082
1083         spin_lock(&dev->dev_reservation_lock);
1084         pr_reg = dev->dev_pr_res_holder;
1085         if (!pr_reg) {
1086                 len = sprintf(page, "No SPC-3 Reservation holder\n");
1087                 goto out_unlock;
1088         }
1089
1090         se_nacl = pr_reg->pr_reg_nacl;
1091         se_tpg = se_nacl->se_tpg;
1092         lun = pr_reg->pr_reg_tg_pt_lun;
1093         tfo = se_tpg->se_tpg_tfo;
1094
1095         len += sprintf(page+len, "SPC-3 Reservation: %s"
1096                 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1097                 tfo->tpg_get_wwn(se_tpg));
1098         len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1099                 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1100                 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1101                 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1102                 tfo->get_fabric_name(), lun->unpacked_lun);
1103
1104 out_unlock:
1105         spin_unlock(&dev->dev_reservation_lock);
1106         return len;
1107 }
1108
1109 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1110
1111 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1112                 struct se_device *dev, char *page)
1113 {
1114         struct target_core_fabric_ops *tfo;
1115         struct t10_pr_registration *pr_reg;
1116         unsigned char buf[384];
1117         char i_buf[PR_REG_ISID_ID_LEN];
1118         ssize_t len = 0;
1119         int reg_count = 0, prf_isid;
1120
1121         len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1122
1123         spin_lock(&dev->t10_pr.registration_lock);
1124         list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1125                         pr_reg_list) {
1126
1127                 memset(buf, 0, 384);
1128                 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1129                 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1130                 prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1131                                         PR_REG_ISID_ID_LEN);
1132                 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1133                         tfo->get_fabric_name(),
1134                         pr_reg->pr_reg_nacl->initiatorname, (prf_isid) ?
1135                         &i_buf[0] : "", pr_reg->pr_res_key,
1136                         pr_reg->pr_res_generation);
1137
1138                 if (len + strlen(buf) >= PAGE_SIZE)
1139                         break;
1140
1141                 len += sprintf(page+len, "%s", buf);
1142                 reg_count++;
1143         }
1144         spin_unlock(&dev->t10_pr.registration_lock);
1145
1146         if (!reg_count)
1147                 len += sprintf(page+len, "None\n");
1148
1149         return len;
1150 }
1151
1152 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1153
1154 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1155                 struct se_device *dev, char *page)
1156 {
1157         struct t10_pr_registration *pr_reg;
1158         ssize_t len = 0;
1159
1160         spin_lock(&dev->dev_reservation_lock);
1161         pr_reg = dev->dev_pr_res_holder;
1162         if (pr_reg) {
1163                 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1164                         core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1165         } else {
1166                 len = sprintf(page, "No SPC-3 Reservation holder\n");
1167         }
1168
1169         spin_unlock(&dev->dev_reservation_lock);
1170         return len;
1171 }
1172
1173 SE_DEV_PR_ATTR_RO(res_pr_type);
1174
1175 static ssize_t target_core_dev_pr_show_attr_res_type(
1176                 struct se_device *dev, char *page)
1177 {
1178         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1179                 return sprintf(page, "SPC_PASSTHROUGH\n");
1180         else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1181                 return sprintf(page, "SPC2_RESERVATIONS\n");
1182         else
1183                 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1184 }
1185
1186 SE_DEV_PR_ATTR_RO(res_type);
1187
1188 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1189                 struct se_device *dev, char *page)
1190 {
1191         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1192                 return 0;
1193
1194         return sprintf(page, "APTPL Bit Status: %s\n",
1195                 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1196 }
1197
1198 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1199
1200 /*
1201  * res_aptpl_metadata
1202  */
1203 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1204                 struct se_device *dev, char *page)
1205 {
1206         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1207                 return 0;
1208
1209         return sprintf(page, "Ready to process PR APTPL metadata..\n");
1210 }
1211
1212 enum {
1213         Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1214         Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1215         Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1216         Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1217 };
1218
1219 static match_table_t tokens = {
1220         {Opt_initiator_fabric, "initiator_fabric=%s"},
1221         {Opt_initiator_node, "initiator_node=%s"},
1222         {Opt_initiator_sid, "initiator_sid=%s"},
1223         {Opt_sa_res_key, "sa_res_key=%s"},
1224         {Opt_res_holder, "res_holder=%d"},
1225         {Opt_res_type, "res_type=%d"},
1226         {Opt_res_scope, "res_scope=%d"},
1227         {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1228         {Opt_mapped_lun, "mapped_lun=%d"},
1229         {Opt_target_fabric, "target_fabric=%s"},
1230         {Opt_target_node, "target_node=%s"},
1231         {Opt_tpgt, "tpgt=%d"},
1232         {Opt_port_rtpi, "port_rtpi=%d"},
1233         {Opt_target_lun, "target_lun=%d"},
1234         {Opt_err, NULL}
1235 };
1236
1237 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1238         struct se_device *dev,
1239         const char *page,
1240         size_t count)
1241 {
1242         unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1243         unsigned char *t_fabric = NULL, *t_port = NULL;
1244         char *orig, *ptr, *arg_p, *opts;
1245         substring_t args[MAX_OPT_ARGS];
1246         unsigned long long tmp_ll;
1247         u64 sa_res_key = 0;
1248         u32 mapped_lun = 0, target_lun = 0;
1249         int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1250         u16 port_rpti = 0, tpgt = 0;
1251         u8 type = 0, scope;
1252
1253         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1254                 return 0;
1255         if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1256                 return 0;
1257
1258         if (dev->export_count) {
1259                 pr_debug("Unable to process APTPL metadata while"
1260                         " active fabric exports exist\n");
1261                 return -EINVAL;
1262         }
1263
1264         opts = kstrdup(page, GFP_KERNEL);
1265         if (!opts)
1266                 return -ENOMEM;
1267
1268         orig = opts;
1269         while ((ptr = strsep(&opts, ",\n")) != NULL) {
1270                 if (!*ptr)
1271                         continue;
1272
1273                 token = match_token(ptr, tokens, args);
1274                 switch (token) {
1275                 case Opt_initiator_fabric:
1276                         i_fabric = match_strdup(&args[0]);
1277                         if (!i_fabric) {
1278                                 ret = -ENOMEM;
1279                                 goto out;
1280                         }
1281                         break;
1282                 case Opt_initiator_node:
1283                         i_port = match_strdup(&args[0]);
1284                         if (!i_port) {
1285                                 ret = -ENOMEM;
1286                                 goto out;
1287                         }
1288                         if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1289                                 pr_err("APTPL metadata initiator_node="
1290                                         " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1291                                         PR_APTPL_MAX_IPORT_LEN);
1292                                 ret = -EINVAL;
1293                                 break;
1294                         }
1295                         break;
1296                 case Opt_initiator_sid:
1297                         isid = match_strdup(&args[0]);
1298                         if (!isid) {
1299                                 ret = -ENOMEM;
1300                                 goto out;
1301                         }
1302                         if (strlen(isid) >= PR_REG_ISID_LEN) {
1303                                 pr_err("APTPL metadata initiator_isid"
1304                                         "= exceeds PR_REG_ISID_LEN: %d\n",
1305                                         PR_REG_ISID_LEN);
1306                                 ret = -EINVAL;
1307                                 break;
1308                         }
1309                         break;
1310                 case Opt_sa_res_key:
1311                         arg_p = match_strdup(&args[0]);
1312                         if (!arg_p) {
1313                                 ret = -ENOMEM;
1314                                 goto out;
1315                         }
1316                         ret = strict_strtoull(arg_p, 0, &tmp_ll);
1317                         if (ret < 0) {
1318                                 pr_err("strict_strtoull() failed for"
1319                                         " sa_res_key=\n");
1320                                 goto out;
1321                         }
1322                         sa_res_key = (u64)tmp_ll;
1323                         break;
1324                 /*
1325                  * PR APTPL Metadata for Reservation
1326                  */
1327                 case Opt_res_holder:
1328                         match_int(args, &arg);
1329                         res_holder = arg;
1330                         break;
1331                 case Opt_res_type:
1332                         match_int(args, &arg);
1333                         type = (u8)arg;
1334                         break;
1335                 case Opt_res_scope:
1336                         match_int(args, &arg);
1337                         scope = (u8)arg;
1338                         break;
1339                 case Opt_res_all_tg_pt:
1340                         match_int(args, &arg);
1341                         all_tg_pt = (int)arg;
1342                         break;
1343                 case Opt_mapped_lun:
1344                         match_int(args, &arg);
1345                         mapped_lun = (u32)arg;
1346                         break;
1347                 /*
1348                  * PR APTPL Metadata for Target Port
1349                  */
1350                 case Opt_target_fabric:
1351                         t_fabric = match_strdup(&args[0]);
1352                         if (!t_fabric) {
1353                                 ret = -ENOMEM;
1354                                 goto out;
1355                         }
1356                         break;
1357                 case Opt_target_node:
1358                         t_port = match_strdup(&args[0]);
1359                         if (!t_port) {
1360                                 ret = -ENOMEM;
1361                                 goto out;
1362                         }
1363                         if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1364                                 pr_err("APTPL metadata target_node="
1365                                         " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1366                                         PR_APTPL_MAX_TPORT_LEN);
1367                                 ret = -EINVAL;
1368                                 break;
1369                         }
1370                         break;
1371                 case Opt_tpgt:
1372                         match_int(args, &arg);
1373                         tpgt = (u16)arg;
1374                         break;
1375                 case Opt_port_rtpi:
1376                         match_int(args, &arg);
1377                         port_rpti = (u16)arg;
1378                         break;
1379                 case Opt_target_lun:
1380                         match_int(args, &arg);
1381                         target_lun = (u32)arg;
1382                         break;
1383                 default:
1384                         break;
1385                 }
1386         }
1387
1388         if (!i_port || !t_port || !sa_res_key) {
1389                 pr_err("Illegal parameters for APTPL registration\n");
1390                 ret = -EINVAL;
1391                 goto out;
1392         }
1393
1394         if (res_holder && !(type)) {
1395                 pr_err("Illegal PR type: 0x%02x for reservation"
1396                                 " holder\n", type);
1397                 ret = -EINVAL;
1398                 goto out;
1399         }
1400
1401         ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1402                         i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1403                         res_holder, all_tg_pt, type);
1404 out:
1405         kfree(i_fabric);
1406         kfree(i_port);
1407         kfree(isid);
1408         kfree(t_fabric);
1409         kfree(t_port);
1410         kfree(orig);
1411         return (ret == 0) ? count : ret;
1412 }
1413
1414 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1415
1416 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1417
1418 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1419         &target_core_dev_pr_res_holder.attr,
1420         &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1421         &target_core_dev_pr_res_pr_generation.attr,
1422         &target_core_dev_pr_res_pr_holder_tg_port.attr,
1423         &target_core_dev_pr_res_pr_registered_i_pts.attr,
1424         &target_core_dev_pr_res_pr_type.attr,
1425         &target_core_dev_pr_res_type.attr,
1426         &target_core_dev_pr_res_aptpl_active.attr,
1427         &target_core_dev_pr_res_aptpl_metadata.attr,
1428         NULL,
1429 };
1430
1431 static struct configfs_item_operations target_core_dev_pr_ops = {
1432         .show_attribute         = target_core_dev_pr_attr_show,
1433         .store_attribute        = target_core_dev_pr_attr_store,
1434 };
1435
1436 static struct config_item_type target_core_dev_pr_cit = {
1437         .ct_item_ops            = &target_core_dev_pr_ops,
1438         .ct_attrs               = target_core_dev_pr_attrs,
1439         .ct_owner               = THIS_MODULE,
1440 };
1441
1442 /*  End functions for struct config_item_type target_core_dev_pr_cit */
1443
1444 /*  Start functions for struct config_item_type target_core_dev_cit */
1445
1446 static ssize_t target_core_show_dev_info(void *p, char *page)
1447 {
1448         struct se_device *dev = p;
1449         struct se_subsystem_api *t = dev->transport;
1450         int bl = 0;
1451         ssize_t read_bytes = 0;
1452
1453         transport_dump_dev_state(dev, page, &bl);
1454         read_bytes += bl;
1455         read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1456         return read_bytes;
1457 }
1458
1459 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1460         .attr   = { .ca_owner = THIS_MODULE,
1461                     .ca_name = "info",
1462                     .ca_mode = S_IRUGO },
1463         .show   = target_core_show_dev_info,
1464         .store  = NULL,
1465 };
1466
1467 static ssize_t target_core_store_dev_control(
1468         void *p,
1469         const char *page,
1470         size_t count)
1471 {
1472         struct se_device *dev = p;
1473         struct se_subsystem_api *t = dev->transport;
1474
1475         return t->set_configfs_dev_params(dev, page, count);
1476 }
1477
1478 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1479         .attr   = { .ca_owner = THIS_MODULE,
1480                     .ca_name = "control",
1481                     .ca_mode = S_IWUSR },
1482         .show   = NULL,
1483         .store  = target_core_store_dev_control,
1484 };
1485
1486 static ssize_t target_core_show_dev_alias(void *p, char *page)
1487 {
1488         struct se_device *dev = p;
1489
1490         if (!(dev->dev_flags & DF_USING_ALIAS))
1491                 return 0;
1492
1493         return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1494 }
1495
1496 static ssize_t target_core_store_dev_alias(
1497         void *p,
1498         const char *page,
1499         size_t count)
1500 {
1501         struct se_device *dev = p;
1502         struct se_hba *hba = dev->se_hba;
1503         ssize_t read_bytes;
1504
1505         if (count > (SE_DEV_ALIAS_LEN-1)) {
1506                 pr_err("alias count: %d exceeds"
1507                         " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1508                         SE_DEV_ALIAS_LEN-1);
1509                 return -EINVAL;
1510         }
1511
1512         read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
1513         if (!read_bytes)
1514                 return -EINVAL;
1515         if (dev->dev_alias[read_bytes - 1] == '\n')
1516                 dev->dev_alias[read_bytes - 1] = '\0';
1517
1518         dev->dev_flags |= DF_USING_ALIAS;
1519
1520         pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1521                 config_item_name(&hba->hba_group.cg_item),
1522                 config_item_name(&dev->dev_group.cg_item),
1523                 dev->dev_alias);
1524
1525         return read_bytes;
1526 }
1527
1528 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1529         .attr   = { .ca_owner = THIS_MODULE,
1530                     .ca_name = "alias",
1531                     .ca_mode =  S_IRUGO | S_IWUSR },
1532         .show   = target_core_show_dev_alias,
1533         .store  = target_core_store_dev_alias,
1534 };
1535
1536 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1537 {
1538         struct se_device *dev = p;
1539
1540         if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1541                 return 0;
1542
1543         return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1544 }
1545
1546 static ssize_t target_core_store_dev_udev_path(
1547         void *p,
1548         const char *page,
1549         size_t count)
1550 {
1551         struct se_device *dev = p;
1552         struct se_hba *hba = dev->se_hba;
1553         ssize_t read_bytes;
1554
1555         if (count > (SE_UDEV_PATH_LEN-1)) {
1556                 pr_err("udev_path count: %d exceeds"
1557                         " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1558                         SE_UDEV_PATH_LEN-1);
1559                 return -EINVAL;
1560         }
1561
1562         read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1563                         "%s", page);
1564         if (!read_bytes)
1565                 return -EINVAL;
1566         if (dev->udev_path[read_bytes - 1] == '\n')
1567                 dev->udev_path[read_bytes - 1] = '\0';
1568
1569         dev->dev_flags |= DF_USING_UDEV_PATH;
1570
1571         pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1572                 config_item_name(&hba->hba_group.cg_item),
1573                 config_item_name(&dev->dev_group.cg_item),
1574                 dev->udev_path);
1575
1576         return read_bytes;
1577 }
1578
1579 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1580         .attr   = { .ca_owner = THIS_MODULE,
1581                     .ca_name = "udev_path",
1582                     .ca_mode =  S_IRUGO | S_IWUSR },
1583         .show   = target_core_show_dev_udev_path,
1584         .store  = target_core_store_dev_udev_path,
1585 };
1586
1587 static ssize_t target_core_show_dev_enable(void *p, char *page)
1588 {
1589         struct se_device *dev = p;
1590
1591         return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1592 }
1593
1594 static ssize_t target_core_store_dev_enable(
1595         void *p,
1596         const char *page,
1597         size_t count)
1598 {
1599         struct se_device *dev = p;
1600         char *ptr;
1601         int ret;
1602
1603         ptr = strstr(page, "1");
1604         if (!ptr) {
1605                 pr_err("For dev_enable ops, only valid value"
1606                                 " is \"1\"\n");
1607                 return -EINVAL;
1608         }
1609
1610         ret = target_configure_device(dev);
1611         if (ret)
1612                 return ret;
1613         return count;
1614 }
1615
1616 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1617         .attr   = { .ca_owner = THIS_MODULE,
1618                     .ca_name = "enable",
1619                     .ca_mode =  S_IRUGO | S_IWUSR },
1620         .show   = target_core_show_dev_enable,
1621         .store  = target_core_store_dev_enable,
1622 };
1623
1624 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1625 {
1626         struct se_device *dev = p;
1627         struct config_item *lu_ci;
1628         struct t10_alua_lu_gp *lu_gp;
1629         struct t10_alua_lu_gp_member *lu_gp_mem;
1630         ssize_t len = 0;
1631
1632         lu_gp_mem = dev->dev_alua_lu_gp_mem;
1633         if (!lu_gp_mem)
1634                 return 0;
1635
1636         spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1637         lu_gp = lu_gp_mem->lu_gp;
1638         if (lu_gp) {
1639                 lu_ci = &lu_gp->lu_gp_group.cg_item;
1640                 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1641                         config_item_name(lu_ci), lu_gp->lu_gp_id);
1642         }
1643         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1644
1645         return len;
1646 }
1647
1648 static ssize_t target_core_store_alua_lu_gp(
1649         void *p,
1650         const char *page,
1651         size_t count)
1652 {
1653         struct se_device *dev = p;
1654         struct se_hba *hba = dev->se_hba;
1655         struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1656         struct t10_alua_lu_gp_member *lu_gp_mem;
1657         unsigned char buf[LU_GROUP_NAME_BUF];
1658         int move = 0;
1659
1660         lu_gp_mem = dev->dev_alua_lu_gp_mem;
1661         if (!lu_gp_mem)
1662                 return 0;
1663
1664         if (count > LU_GROUP_NAME_BUF) {
1665                 pr_err("ALUA LU Group Alias too large!\n");
1666                 return -EINVAL;
1667         }
1668         memset(buf, 0, LU_GROUP_NAME_BUF);
1669         memcpy(buf, page, count);
1670         /*
1671          * Any ALUA logical unit alias besides "NULL" means we will be
1672          * making a new group association.
1673          */
1674         if (strcmp(strstrip(buf), "NULL")) {
1675                 /*
1676                  * core_alua_get_lu_gp_by_name() will increment reference to
1677                  * struct t10_alua_lu_gp.  This reference is released with
1678                  * core_alua_get_lu_gp_by_name below().
1679                  */
1680                 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1681                 if (!lu_gp_new)
1682                         return -ENODEV;
1683         }
1684
1685         spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1686         lu_gp = lu_gp_mem->lu_gp;
1687         if (lu_gp) {
1688                 /*
1689                  * Clearing an existing lu_gp association, and replacing
1690                  * with NULL
1691                  */
1692                 if (!lu_gp_new) {
1693                         pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1694                                 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1695                                 " %hu\n",
1696                                 config_item_name(&hba->hba_group.cg_item),
1697                                 config_item_name(&dev->dev_group.cg_item),
1698                                 config_item_name(&lu_gp->lu_gp_group.cg_item),
1699                                 lu_gp->lu_gp_id);
1700
1701                         __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1702                         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1703
1704                         return count;
1705                 }
1706                 /*
1707                  * Removing existing association of lu_gp_mem with lu_gp
1708                  */
1709                 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1710                 move = 1;
1711         }
1712         /*
1713          * Associate lu_gp_mem with lu_gp_new.
1714          */
1715         __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1716         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1717
1718         pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1719                 " core/alua/lu_gps/%s, ID: %hu\n",
1720                 (move) ? "Moving" : "Adding",
1721                 config_item_name(&hba->hba_group.cg_item),
1722                 config_item_name(&dev->dev_group.cg_item),
1723                 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1724                 lu_gp_new->lu_gp_id);
1725
1726         core_alua_put_lu_gp_from_name(lu_gp_new);
1727         return count;
1728 }
1729
1730 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1731         .attr   = { .ca_owner = THIS_MODULE,
1732                     .ca_name = "alua_lu_gp",
1733                     .ca_mode = S_IRUGO | S_IWUSR },
1734         .show   = target_core_show_alua_lu_gp,
1735         .store  = target_core_store_alua_lu_gp,
1736 };
1737
1738 static struct configfs_attribute *lio_core_dev_attrs[] = {
1739         &target_core_attr_dev_info.attr,
1740         &target_core_attr_dev_control.attr,
1741         &target_core_attr_dev_alias.attr,
1742         &target_core_attr_dev_udev_path.attr,
1743         &target_core_attr_dev_enable.attr,
1744         &target_core_attr_dev_alua_lu_gp.attr,
1745         NULL,
1746 };
1747
1748 static void target_core_dev_release(struct config_item *item)
1749 {
1750         struct config_group *dev_cg = to_config_group(item);
1751         struct se_device *dev =
1752                 container_of(dev_cg, struct se_device, dev_group);
1753
1754         kfree(dev_cg->default_groups);
1755         target_free_device(dev);
1756 }
1757
1758 static ssize_t target_core_dev_show(struct config_item *item,
1759                                      struct configfs_attribute *attr,
1760                                      char *page)
1761 {
1762         struct config_group *dev_cg = to_config_group(item);
1763         struct se_device *dev =
1764                 container_of(dev_cg, struct se_device, dev_group);
1765         struct target_core_configfs_attribute *tc_attr = container_of(
1766                         attr, struct target_core_configfs_attribute, attr);
1767
1768         if (!tc_attr->show)
1769                 return -EINVAL;
1770
1771         return tc_attr->show(dev, page);
1772 }
1773
1774 static ssize_t target_core_dev_store(struct config_item *item,
1775                                       struct configfs_attribute *attr,
1776                                       const char *page, size_t count)
1777 {
1778         struct config_group *dev_cg = to_config_group(item);
1779         struct se_device *dev =
1780                 container_of(dev_cg, struct se_device, dev_group);
1781         struct target_core_configfs_attribute *tc_attr = container_of(
1782                         attr, struct target_core_configfs_attribute, attr);
1783
1784         if (!tc_attr->store)
1785                 return -EINVAL;
1786
1787         return tc_attr->store(dev, page, count);
1788 }
1789
1790 static struct configfs_item_operations target_core_dev_item_ops = {
1791         .release                = target_core_dev_release,
1792         .show_attribute         = target_core_dev_show,
1793         .store_attribute        = target_core_dev_store,
1794 };
1795
1796 static struct config_item_type target_core_dev_cit = {
1797         .ct_item_ops            = &target_core_dev_item_ops,
1798         .ct_attrs               = lio_core_dev_attrs,
1799         .ct_owner               = THIS_MODULE,
1800 };
1801
1802 /* End functions for struct config_item_type target_core_dev_cit */
1803
1804 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1805
1806 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1807 #define SE_DEV_ALUA_LU_ATTR(_name, _mode)                               \
1808 static struct target_core_alua_lu_gp_attribute                          \
1809                         target_core_alua_lu_gp_##_name =                \
1810         __CONFIGFS_EATTR(_name, _mode,                                  \
1811         target_core_alua_lu_gp_show_attr_##_name,                       \
1812         target_core_alua_lu_gp_store_attr_##_name);
1813
1814 #define SE_DEV_ALUA_LU_ATTR_RO(_name)                                   \
1815 static struct target_core_alua_lu_gp_attribute                          \
1816                         target_core_alua_lu_gp_##_name =                \
1817         __CONFIGFS_EATTR_RO(_name,                                      \
1818         target_core_alua_lu_gp_show_attr_##_name);
1819
1820 /*
1821  * lu_gp_id
1822  */
1823 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1824         struct t10_alua_lu_gp *lu_gp,
1825         char *page)
1826 {
1827         if (!lu_gp->lu_gp_valid_id)
1828                 return 0;
1829
1830         return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1831 }
1832
1833 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1834         struct t10_alua_lu_gp *lu_gp,
1835         const char *page,
1836         size_t count)
1837 {
1838         struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1839         unsigned long lu_gp_id;
1840         int ret;
1841
1842         ret = strict_strtoul(page, 0, &lu_gp_id);
1843         if (ret < 0) {
1844                 pr_err("strict_strtoul() returned %d for"
1845                         " lu_gp_id\n", ret);
1846                 return -EINVAL;
1847         }
1848         if (lu_gp_id > 0x0000ffff) {
1849                 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
1850                         " 0x0000ffff\n", lu_gp_id);
1851                 return -EINVAL;
1852         }
1853
1854         ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1855         if (ret < 0)
1856                 return -EINVAL;
1857
1858         pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
1859                 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1860                 config_item_name(&alua_lu_gp_cg->cg_item),
1861                 lu_gp->lu_gp_id);
1862
1863         return count;
1864 }
1865
1866 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1867
1868 /*
1869  * members
1870  */
1871 static ssize_t target_core_alua_lu_gp_show_attr_members(
1872         struct t10_alua_lu_gp *lu_gp,
1873         char *page)
1874 {
1875         struct se_device *dev;
1876         struct se_hba *hba;
1877         struct t10_alua_lu_gp_member *lu_gp_mem;
1878         ssize_t len = 0, cur_len;
1879         unsigned char buf[LU_GROUP_NAME_BUF];
1880
1881         memset(buf, 0, LU_GROUP_NAME_BUF);
1882
1883         spin_lock(&lu_gp->lu_gp_lock);
1884         list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1885                 dev = lu_gp_mem->lu_gp_mem_dev;
1886                 hba = dev->se_hba;
1887
1888                 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1889                         config_item_name(&hba->hba_group.cg_item),
1890                         config_item_name(&dev->dev_group.cg_item));
1891                 cur_len++; /* Extra byte for NULL terminator */
1892
1893                 if ((cur_len + len) > PAGE_SIZE) {
1894                         pr_warn("Ran out of lu_gp_show_attr"
1895                                 "_members buffer\n");
1896                         break;
1897                 }
1898                 memcpy(page+len, buf, cur_len);
1899                 len += cur_len;
1900         }
1901         spin_unlock(&lu_gp->lu_gp_lock);
1902
1903         return len;
1904 }
1905
1906 SE_DEV_ALUA_LU_ATTR_RO(members);
1907
1908 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1909
1910 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1911         &target_core_alua_lu_gp_lu_gp_id.attr,
1912         &target_core_alua_lu_gp_members.attr,
1913         NULL,
1914 };
1915
1916 static void target_core_alua_lu_gp_release(struct config_item *item)
1917 {
1918         struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1919                         struct t10_alua_lu_gp, lu_gp_group);
1920
1921         core_alua_free_lu_gp(lu_gp);
1922 }
1923
1924 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
1925         .release                = target_core_alua_lu_gp_release,
1926         .show_attribute         = target_core_alua_lu_gp_attr_show,
1927         .store_attribute        = target_core_alua_lu_gp_attr_store,
1928 };
1929
1930 static struct config_item_type target_core_alua_lu_gp_cit = {
1931         .ct_item_ops            = &target_core_alua_lu_gp_ops,
1932         .ct_attrs               = target_core_alua_lu_gp_attrs,
1933         .ct_owner               = THIS_MODULE,
1934 };
1935
1936 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1937
1938 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1939
1940 static struct config_group *target_core_alua_create_lu_gp(
1941         struct config_group *group,
1942         const char *name)
1943 {
1944         struct t10_alua_lu_gp *lu_gp;
1945         struct config_group *alua_lu_gp_cg = NULL;
1946         struct config_item *alua_lu_gp_ci = NULL;
1947
1948         lu_gp = core_alua_allocate_lu_gp(name, 0);
1949         if (IS_ERR(lu_gp))
1950                 return NULL;
1951
1952         alua_lu_gp_cg = &lu_gp->lu_gp_group;
1953         alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1954
1955         config_group_init_type_name(alua_lu_gp_cg, name,
1956                         &target_core_alua_lu_gp_cit);
1957
1958         pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
1959                 " Group: core/alua/lu_gps/%s\n",
1960                 config_item_name(alua_lu_gp_ci));
1961
1962         return alua_lu_gp_cg;
1963
1964 }
1965
1966 static void target_core_alua_drop_lu_gp(
1967         struct config_group *group,
1968         struct config_item *item)
1969 {
1970         struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1971                         struct t10_alua_lu_gp, lu_gp_group);
1972
1973         pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
1974                 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1975                 config_item_name(item), lu_gp->lu_gp_id);
1976         /*
1977          * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1978          * -> target_core_alua_lu_gp_release()
1979          */
1980         config_item_put(item);
1981 }
1982
1983 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1984         .make_group             = &target_core_alua_create_lu_gp,
1985         .drop_item              = &target_core_alua_drop_lu_gp,
1986 };
1987
1988 static struct config_item_type target_core_alua_lu_gps_cit = {
1989         .ct_item_ops            = NULL,
1990         .ct_group_ops           = &target_core_alua_lu_gps_group_ops,
1991         .ct_owner               = THIS_MODULE,
1992 };
1993
1994 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1995
1996 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1997
1998 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1999 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)                            \
2000 static struct target_core_alua_tg_pt_gp_attribute                       \
2001                         target_core_alua_tg_pt_gp_##_name =             \
2002         __CONFIGFS_EATTR(_name, _mode,                                  \
2003         target_core_alua_tg_pt_gp_show_attr_##_name,                    \
2004         target_core_alua_tg_pt_gp_store_attr_##_name);
2005
2006 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)                                \
2007 static struct target_core_alua_tg_pt_gp_attribute                       \
2008                         target_core_alua_tg_pt_gp_##_name =             \
2009         __CONFIGFS_EATTR_RO(_name,                                      \
2010         target_core_alua_tg_pt_gp_show_attr_##_name);
2011
2012 /*
2013  * alua_access_state
2014  */
2015 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2016         struct t10_alua_tg_pt_gp *tg_pt_gp,
2017         char *page)
2018 {
2019         return sprintf(page, "%d\n",
2020                 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2021 }
2022
2023 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2024         struct t10_alua_tg_pt_gp *tg_pt_gp,
2025         const char *page,
2026         size_t count)
2027 {
2028         struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2029         unsigned long tmp;
2030         int new_state, ret;
2031
2032         if (!tg_pt_gp->tg_pt_gp_valid_id) {
2033                 pr_err("Unable to do implict ALUA on non valid"
2034                         " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2035                 return -EINVAL;
2036         }
2037
2038         ret = strict_strtoul(page, 0, &tmp);
2039         if (ret < 0) {
2040                 pr_err("Unable to extract new ALUA access state from"
2041                                 " %s\n", page);
2042                 return -EINVAL;
2043         }
2044         new_state = (int)tmp;
2045
2046         if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2047                 pr_err("Unable to process implict configfs ALUA"
2048                         " transition while TPGS_IMPLICT_ALUA is disabled\n");
2049                 return -EINVAL;
2050         }
2051
2052         ret = core_alua_do_port_transition(tg_pt_gp, dev,
2053                                         NULL, NULL, new_state, 0);
2054         return (!ret) ? count : -EINVAL;
2055 }
2056
2057 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2058
2059 /*
2060  * alua_access_status
2061  */
2062 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2063         struct t10_alua_tg_pt_gp *tg_pt_gp,
2064         char *page)
2065 {
2066         return sprintf(page, "%s\n",
2067                 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2068 }
2069
2070 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2071         struct t10_alua_tg_pt_gp *tg_pt_gp,
2072         const char *page,
2073         size_t count)
2074 {
2075         unsigned long tmp;
2076         int new_status, ret;
2077
2078         if (!tg_pt_gp->tg_pt_gp_valid_id) {
2079                 pr_err("Unable to do set ALUA access status on non"
2080                         " valid tg_pt_gp ID: %hu\n",
2081                         tg_pt_gp->tg_pt_gp_valid_id);
2082                 return -EINVAL;
2083         }
2084
2085         ret = strict_strtoul(page, 0, &tmp);
2086         if (ret < 0) {
2087                 pr_err("Unable to extract new ALUA access status"
2088                                 " from %s\n", page);
2089                 return -EINVAL;
2090         }
2091         new_status = (int)tmp;
2092
2093         if ((new_status != ALUA_STATUS_NONE) &&
2094             (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2095             (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2096                 pr_err("Illegal ALUA access status: 0x%02x\n",
2097                                 new_status);
2098                 return -EINVAL;
2099         }
2100
2101         tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2102         return count;
2103 }
2104
2105 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2106
2107 /*
2108  * alua_access_type
2109  */
2110 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2111         struct t10_alua_tg_pt_gp *tg_pt_gp,
2112         char *page)
2113 {
2114         return core_alua_show_access_type(tg_pt_gp, page);
2115 }
2116
2117 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2118         struct t10_alua_tg_pt_gp *tg_pt_gp,
2119         const char *page,
2120         size_t count)
2121 {
2122         return core_alua_store_access_type(tg_pt_gp, page, count);
2123 }
2124
2125 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2126
2127 /*
2128  * alua_write_metadata
2129  */
2130 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2131         struct t10_alua_tg_pt_gp *tg_pt_gp,
2132         char *page)
2133 {
2134         return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2135 }
2136
2137 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2138         struct t10_alua_tg_pt_gp *tg_pt_gp,
2139         const char *page,
2140         size_t count)
2141 {
2142         unsigned long tmp;
2143         int ret;
2144
2145         ret = strict_strtoul(page, 0, &tmp);
2146         if (ret < 0) {
2147                 pr_err("Unable to extract alua_write_metadata\n");
2148                 return -EINVAL;
2149         }
2150
2151         if ((tmp != 0) && (tmp != 1)) {
2152                 pr_err("Illegal value for alua_write_metadata:"
2153                         " %lu\n", tmp);
2154                 return -EINVAL;
2155         }
2156         tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2157
2158         return count;
2159 }
2160
2161 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2162
2163
2164
2165 /*
2166  * nonop_delay_msecs
2167  */
2168 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2169         struct t10_alua_tg_pt_gp *tg_pt_gp,
2170         char *page)
2171 {
2172         return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2173
2174 }
2175
2176 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2177         struct t10_alua_tg_pt_gp *tg_pt_gp,
2178         const char *page,
2179         size_t count)
2180 {
2181         return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2182 }
2183
2184 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2185
2186 /*
2187  * trans_delay_msecs
2188  */
2189 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2190         struct t10_alua_tg_pt_gp *tg_pt_gp,
2191         char *page)
2192 {
2193         return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2194 }
2195
2196 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2197         struct t10_alua_tg_pt_gp *tg_pt_gp,
2198         const char *page,
2199         size_t count)
2200 {
2201         return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2202 }
2203
2204 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2205
2206 /*
2207  * implict_trans_secs
2208  */
2209 static ssize_t target_core_alua_tg_pt_gp_show_attr_implict_trans_secs(
2210         struct t10_alua_tg_pt_gp *tg_pt_gp,
2211         char *page)
2212 {
2213         return core_alua_show_implict_trans_secs(tg_pt_gp, page);
2214 }
2215
2216 static ssize_t target_core_alua_tg_pt_gp_store_attr_implict_trans_secs(
2217         struct t10_alua_tg_pt_gp *tg_pt_gp,
2218         const char *page,
2219         size_t count)
2220 {
2221         return core_alua_store_implict_trans_secs(tg_pt_gp, page, count);
2222 }
2223
2224 SE_DEV_ALUA_TG_PT_ATTR(implict_trans_secs, S_IRUGO | S_IWUSR);
2225
2226 /*
2227  * preferred
2228  */
2229
2230 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2231         struct t10_alua_tg_pt_gp *tg_pt_gp,
2232         char *page)
2233 {
2234         return core_alua_show_preferred_bit(tg_pt_gp, page);
2235 }
2236
2237 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2238         struct t10_alua_tg_pt_gp *tg_pt_gp,
2239         const char *page,
2240         size_t count)
2241 {
2242         return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2243 }
2244
2245 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2246
2247 /*
2248  * tg_pt_gp_id
2249  */
2250 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2251         struct t10_alua_tg_pt_gp *tg_pt_gp,
2252         char *page)
2253 {
2254         if (!tg_pt_gp->tg_pt_gp_valid_id)
2255                 return 0;
2256
2257         return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2258 }
2259
2260 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2261         struct t10_alua_tg_pt_gp *tg_pt_gp,
2262         const char *page,
2263         size_t count)
2264 {
2265         struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2266         unsigned long tg_pt_gp_id;
2267         int ret;
2268
2269         ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2270         if (ret < 0) {
2271                 pr_err("strict_strtoul() returned %d for"
2272                         " tg_pt_gp_id\n", ret);
2273                 return -EINVAL;
2274         }
2275         if (tg_pt_gp_id > 0x0000ffff) {
2276                 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2277                         " 0x0000ffff\n", tg_pt_gp_id);
2278                 return -EINVAL;
2279         }
2280
2281         ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2282         if (ret < 0)
2283                 return -EINVAL;
2284
2285         pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2286                 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2287                 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2288                 tg_pt_gp->tg_pt_gp_id);
2289
2290         return count;
2291 }
2292
2293 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2294
2295 /*
2296  * members
2297  */
2298 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2299         struct t10_alua_tg_pt_gp *tg_pt_gp,
2300         char *page)
2301 {
2302         struct se_port *port;
2303         struct se_portal_group *tpg;
2304         struct se_lun *lun;
2305         struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2306         ssize_t len = 0, cur_len;
2307         unsigned char buf[TG_PT_GROUP_NAME_BUF];
2308
2309         memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2310
2311         spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2312         list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2313                         tg_pt_gp_mem_list) {
2314                 port = tg_pt_gp_mem->tg_pt;
2315                 tpg = port->sep_tpg;
2316                 lun = port->sep_lun;
2317
2318                 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2319                         "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2320                         tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2321                         tpg->se_tpg_tfo->tpg_get_tag(tpg),
2322                         config_item_name(&lun->lun_group.cg_item));
2323                 cur_len++; /* Extra byte for NULL terminator */
2324
2325                 if ((cur_len + len) > PAGE_SIZE) {
2326                         pr_warn("Ran out of lu_gp_show_attr"
2327                                 "_members buffer\n");
2328                         break;
2329                 }
2330                 memcpy(page+len, buf, cur_len);
2331                 len += cur_len;
2332         }
2333         spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2334
2335         return len;
2336 }
2337
2338 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2339
2340 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2341                         tg_pt_gp_group);
2342
2343 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2344         &target_core_alua_tg_pt_gp_alua_access_state.attr,
2345         &target_core_alua_tg_pt_gp_alua_access_status.attr,
2346         &target_core_alua_tg_pt_gp_alua_access_type.attr,
2347         &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2348         &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2349         &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2350         &target_core_alua_tg_pt_gp_implict_trans_secs.attr,
2351         &target_core_alua_tg_pt_gp_preferred.attr,
2352         &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2353         &target_core_alua_tg_pt_gp_members.attr,
2354         NULL,
2355 };
2356
2357 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2358 {
2359         struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2360                         struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2361
2362         core_alua_free_tg_pt_gp(tg_pt_gp);
2363 }
2364
2365 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2366         .release                = target_core_alua_tg_pt_gp_release,
2367         .show_attribute         = target_core_alua_tg_pt_gp_attr_show,
2368         .store_attribute        = target_core_alua_tg_pt_gp_attr_store,
2369 };
2370
2371 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2372         .ct_item_ops            = &target_core_alua_tg_pt_gp_ops,
2373         .ct_attrs               = target_core_alua_tg_pt_gp_attrs,
2374         .ct_owner               = THIS_MODULE,
2375 };
2376
2377 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2378
2379 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2380
2381 static struct config_group *target_core_alua_create_tg_pt_gp(
2382         struct config_group *group,
2383         const char *name)
2384 {
2385         struct t10_alua *alua = container_of(group, struct t10_alua,
2386                                         alua_tg_pt_gps_group);
2387         struct t10_alua_tg_pt_gp *tg_pt_gp;
2388         struct config_group *alua_tg_pt_gp_cg = NULL;
2389         struct config_item *alua_tg_pt_gp_ci = NULL;
2390
2391         tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
2392         if (!tg_pt_gp)
2393                 return NULL;
2394
2395         alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2396         alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2397
2398         config_group_init_type_name(alua_tg_pt_gp_cg, name,
2399                         &target_core_alua_tg_pt_gp_cit);
2400
2401         pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2402                 " Group: alua/tg_pt_gps/%s\n",
2403                 config_item_name(alua_tg_pt_gp_ci));
2404
2405         return alua_tg_pt_gp_cg;
2406 }
2407
2408 static void target_core_alua_drop_tg_pt_gp(
2409         struct config_group *group,
2410         struct config_item *item)
2411 {
2412         struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2413                         struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2414
2415         pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2416                 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2417                 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2418         /*
2419          * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2420          * -> target_core_alua_tg_pt_gp_release().
2421          */
2422         config_item_put(item);
2423 }
2424
2425 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2426         .make_group             = &target_core_alua_create_tg_pt_gp,
2427         .drop_item              = &target_core_alua_drop_tg_pt_gp,
2428 };
2429
2430 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2431         .ct_group_ops           = &target_core_alua_tg_pt_gps_group_ops,
2432         .ct_owner               = THIS_MODULE,
2433 };
2434
2435 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2436
2437 /* Start functions for struct config_item_type target_core_alua_cit */
2438
2439 /*
2440  * target_core_alua_cit is a ConfigFS group that lives under
2441  * /sys/kernel/config/target/core/alua.  There are default groups
2442  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2443  * target_core_alua_cit in target_core_init_configfs() below.
2444  */
2445 static struct config_item_type target_core_alua_cit = {
2446         .ct_item_ops            = NULL,
2447         .ct_attrs               = NULL,
2448         .ct_owner               = THIS_MODULE,
2449 };
2450
2451 /* End functions for struct config_item_type target_core_alua_cit */
2452
2453 /* Start functions for struct config_item_type target_core_stat_cit */
2454
2455 static struct config_group *target_core_stat_mkdir(
2456         struct config_group *group,
2457         const char *name)
2458 {
2459         return ERR_PTR(-ENOSYS);
2460 }
2461
2462 static void target_core_stat_rmdir(
2463         struct config_group *group,
2464         struct config_item *item)
2465 {
2466         return;
2467 }
2468
2469 static struct configfs_group_operations target_core_stat_group_ops = {
2470         .make_group             = &target_core_stat_mkdir,
2471         .drop_item              = &target_core_stat_rmdir,
2472 };
2473
2474 static struct config_item_type target_core_stat_cit = {
2475         .ct_group_ops           = &target_core_stat_group_ops,
2476         .ct_owner               = THIS_MODULE,
2477 };
2478
2479 /* End functions for struct config_item_type target_core_stat_cit */
2480
2481 /* Start functions for struct config_item_type target_core_hba_cit */
2482
2483 static struct config_group *target_core_make_subdev(
2484         struct config_group *group,
2485         const char *name)
2486 {
2487         struct t10_alua_tg_pt_gp *tg_pt_gp;
2488         struct se_subsystem_api *t;
2489         struct config_item *hba_ci = &group->cg_item;
2490         struct se_hba *hba = item_to_hba(hba_ci);
2491         struct se_device *dev;
2492         struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2493         struct config_group *dev_stat_grp = NULL;
2494         int errno = -ENOMEM, ret;
2495
2496         ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2497         if (ret)
2498                 return ERR_PTR(ret);
2499         /*
2500          * Locate the struct se_subsystem_api from parent's struct se_hba.
2501          */
2502         t = hba->transport;
2503
2504         dev = target_alloc_device(hba, name);
2505         if (!dev)
2506                 goto out_unlock;
2507
2508         dev_cg = &dev->dev_group;
2509
2510         dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2511                         GFP_KERNEL);
2512         if (!dev_cg->default_groups)
2513                 goto out_free_device;
2514
2515         config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2516         config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2517                         &target_core_dev_attrib_cit);
2518         config_group_init_type_name(&dev->dev_pr_group, "pr",
2519                         &target_core_dev_pr_cit);
2520         config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2521                         &target_core_dev_wwn_cit);
2522         config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
2523                         "alua", &target_core_alua_tg_pt_gps_cit);
2524         config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2525                         "statistics", &target_core_stat_cit);
2526
2527         dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2528         dev_cg->default_groups[1] = &dev->dev_pr_group;
2529         dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2530         dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2531         dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
2532         dev_cg->default_groups[5] = NULL;
2533         /*
2534          * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2535          */
2536         tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
2537         if (!tg_pt_gp)
2538                 goto out_free_dev_cg_default_groups;
2539         dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2540
2541         tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2542         tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2543                                 GFP_KERNEL);
2544         if (!tg_pt_gp_cg->default_groups) {
2545                 pr_err("Unable to allocate tg_pt_gp_cg->"
2546                                 "default_groups\n");
2547                 goto out_free_tg_pt_gp;
2548         }
2549
2550         config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2551                         "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2552         tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2553         tg_pt_gp_cg->default_groups[1] = NULL;
2554         /*
2555          * Add core/$HBA/$DEV/statistics/ default groups
2556          */
2557         dev_stat_grp = &dev->dev_stat_grps.stat_group;
2558         dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
2559                                 GFP_KERNEL);
2560         if (!dev_stat_grp->default_groups) {
2561                 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2562                 goto out_free_tg_pt_gp_cg_default_groups;
2563         }
2564         target_stat_setup_dev_default_groups(dev);
2565
2566         mutex_unlock(&hba->hba_access_mutex);
2567         return dev_cg;
2568
2569 out_free_tg_pt_gp_cg_default_groups:
2570         kfree(tg_pt_gp_cg->default_groups);
2571 out_free_tg_pt_gp:
2572         core_alua_free_tg_pt_gp(tg_pt_gp);
2573 out_free_dev_cg_default_groups:
2574         kfree(dev_cg->default_groups);
2575 out_free_device:
2576         target_free_device(dev);
2577 out_unlock:
2578         mutex_unlock(&hba->hba_access_mutex);
2579         return ERR_PTR(errno);
2580 }
2581
2582 static void target_core_drop_subdev(
2583         struct config_group *group,
2584         struct config_item *item)
2585 {
2586         struct config_group *dev_cg = to_config_group(item);
2587         struct se_device *dev =
2588                 container_of(dev_cg, struct se_device, dev_group);
2589         struct se_hba *hba;
2590         struct config_item *df_item;
2591         struct config_group *tg_pt_gp_cg, *dev_stat_grp;
2592         int i;
2593
2594         hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2595
2596         mutex_lock(&hba->hba_access_mutex);
2597
2598         dev_stat_grp = &dev->dev_stat_grps.stat_group;
2599         for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2600                 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2601                 dev_stat_grp->default_groups[i] = NULL;
2602                 config_item_put(df_item);
2603         }
2604         kfree(dev_stat_grp->default_groups);
2605
2606         tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2607         for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2608                 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2609                 tg_pt_gp_cg->default_groups[i] = NULL;
2610                 config_item_put(df_item);
2611         }
2612         kfree(tg_pt_gp_cg->default_groups);
2613         /*
2614          * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2615          * directly from target_core_alua_tg_pt_gp_release().
2616          */
2617         dev->t10_alua.default_tg_pt_gp = NULL;
2618
2619         for (i = 0; dev_cg->default_groups[i]; i++) {
2620                 df_item = &dev_cg->default_groups[i]->cg_item;
2621                 dev_cg->default_groups[i] = NULL;
2622                 config_item_put(df_item);
2623         }
2624         /*
2625          * se_dev is released from target_core_dev_item_ops->release()
2626          */
2627         config_item_put(item);
2628         mutex_unlock(&hba->hba_access_mutex);
2629 }
2630
2631 static struct configfs_group_operations target_core_hba_group_ops = {
2632         .make_group             = target_core_make_subdev,
2633         .drop_item              = target_core_drop_subdev,
2634 };
2635
2636 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2637 #define SE_HBA_ATTR(_name, _mode)                               \
2638 static struct target_core_hba_attribute                         \
2639                 target_core_hba_##_name =                       \
2640                 __CONFIGFS_EATTR(_name, _mode,                  \
2641                 target_core_hba_show_attr_##_name,              \
2642                 target_core_hba_store_attr_##_name);
2643
2644 #define SE_HBA_ATTR_RO(_name)                                   \
2645 static struct target_core_hba_attribute                         \
2646                 target_core_hba_##_name =                       \
2647                 __CONFIGFS_EATTR_RO(_name,                      \
2648                 target_core_hba_show_attr_##_name);
2649
2650 static ssize_t target_core_hba_show_attr_hba_info(
2651         struct se_hba *hba,
2652         char *page)
2653 {
2654         return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2655                         hba->hba_id, hba->transport->name,
2656                         TARGET_CORE_CONFIGFS_VERSION);
2657 }
2658
2659 SE_HBA_ATTR_RO(hba_info);
2660
2661 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2662                                 char *page)
2663 {
2664         int hba_mode = 0;
2665
2666         if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2667                 hba_mode = 1;
2668
2669         return sprintf(page, "%d\n", hba_mode);
2670 }
2671
2672 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2673                                 const char *page, size_t count)
2674 {
2675         struct se_subsystem_api *transport = hba->transport;
2676         unsigned long mode_flag;
2677         int ret;
2678
2679         if (transport->pmode_enable_hba == NULL)
2680                 return -EINVAL;
2681
2682         ret = strict_strtoul(page, 0, &mode_flag);
2683         if (ret < 0) {
2684                 pr_err("Unable to extract hba mode flag: %d\n", ret);
2685                 return -EINVAL;
2686         }
2687
2688         if (hba->dev_count) {
2689                 pr_err("Unable to set hba_mode with active devices\n");
2690                 return -EINVAL;
2691         }
2692
2693         ret = transport->pmode_enable_hba(hba, mode_flag);
2694         if (ret < 0)
2695                 return -EINVAL;
2696         if (ret > 0)
2697                 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2698         else if (ret == 0)
2699                 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2700
2701         return count;
2702 }
2703
2704 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2705
2706 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2707
2708 static void target_core_hba_release(struct config_item *item)
2709 {
2710         struct se_hba *hba = container_of(to_config_group(item),
2711                                 struct se_hba, hba_group);
2712         core_delete_hba(hba);
2713 }
2714
2715 static struct configfs_attribute *target_core_hba_attrs[] = {
2716         &target_core_hba_hba_info.attr,
2717         &target_core_hba_hba_mode.attr,
2718         NULL,
2719 };
2720
2721 static struct configfs_item_operations target_core_hba_item_ops = {
2722         .release                = target_core_hba_release,
2723         .show_attribute         = target_core_hba_attr_show,
2724         .store_attribute        = target_core_hba_attr_store,
2725 };
2726
2727 static struct config_item_type target_core_hba_cit = {
2728         .ct_item_ops            = &target_core_hba_item_ops,
2729         .ct_group_ops           = &target_core_hba_group_ops,
2730         .ct_attrs               = target_core_hba_attrs,
2731         .ct_owner               = THIS_MODULE,
2732 };
2733
2734 static struct config_group *target_core_call_addhbatotarget(
2735         struct config_group *group,
2736         const char *name)
2737 {
2738         char *se_plugin_str, *str, *str2;
2739         struct se_hba *hba;
2740         char buf[TARGET_CORE_NAME_MAX_LEN];
2741         unsigned long plugin_dep_id = 0;
2742         int ret;
2743
2744         memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
2745         if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
2746                 pr_err("Passed *name strlen(): %d exceeds"
2747                         " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2748                         TARGET_CORE_NAME_MAX_LEN);
2749                 return ERR_PTR(-ENAMETOOLONG);
2750         }
2751         snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2752
2753         str = strstr(buf, "_");
2754         if (!str) {
2755                 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
2756                 return ERR_PTR(-EINVAL);
2757         }
2758         se_plugin_str = buf;
2759         /*
2760          * Special case for subsystem plugins that have "_" in their names.
2761          * Namely rd_direct and rd_mcp..
2762          */
2763         str2 = strstr(str+1, "_");
2764         if (str2) {
2765                 *str2 = '\0'; /* Terminate for *se_plugin_str */
2766                 str2++; /* Skip to start of plugin dependent ID */
2767                 str = str2;
2768         } else {
2769                 *str = '\0'; /* Terminate for *se_plugin_str */
2770                 str++; /* Skip to start of plugin dependent ID */
2771         }
2772
2773         ret = strict_strtoul(str, 0, &plugin_dep_id);
2774         if (ret < 0) {
2775                 pr_err("strict_strtoul() returned %d for"
2776                                 " plugin_dep_id\n", ret);
2777                 return ERR_PTR(-EINVAL);
2778         }
2779         /*
2780          * Load up TCM subsystem plugins if they have not already been loaded.
2781          */
2782         transport_subsystem_check_init();
2783
2784         hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2785         if (IS_ERR(hba))
2786                 return ERR_CAST(hba);
2787
2788         config_group_init_type_name(&hba->hba_group, name,
2789                         &target_core_hba_cit);
2790
2791         return &hba->hba_group;
2792 }
2793
2794 static void target_core_call_delhbafromtarget(
2795         struct config_group *group,
2796         struct config_item *item)
2797 {
2798         /*
2799          * core_delete_hba() is called from target_core_hba_item_ops->release()
2800          * -> target_core_hba_release()
2801          */
2802         config_item_put(item);
2803 }
2804
2805 static struct configfs_group_operations target_core_group_ops = {
2806         .make_group     = target_core_call_addhbatotarget,
2807         .drop_item      = target_core_call_delhbafromtarget,
2808 };
2809
2810 static struct config_item_type target_core_cit = {
2811         .ct_item_ops    = NULL,
2812         .ct_group_ops   = &target_core_group_ops,
2813         .ct_attrs       = NULL,
2814         .ct_owner       = THIS_MODULE,
2815 };
2816
2817 /* Stop functions for struct config_item_type target_core_hba_cit */
2818
2819 static int __init target_core_init_configfs(void)
2820 {
2821         struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2822         struct config_group *lu_gp_cg = NULL;
2823         struct configfs_subsystem *subsys;
2824         struct t10_alua_lu_gp *lu_gp;
2825         int ret;
2826
2827         pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
2828                 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2829                 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2830
2831         subsys = target_core_subsystem[0];
2832         config_group_init(&subsys->su_group);
2833         mutex_init(&subsys->su_mutex);
2834
2835         ret = init_se_kmem_caches();
2836         if (ret < 0)
2837                 return ret;
2838         /*
2839          * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2840          * and ALUA Logical Unit Group and Target Port Group infrastructure.
2841          */
2842         target_cg = &subsys->su_group;
2843         target_cg->default_groups = kmalloc(sizeof(struct config_group) * 2,
2844                                 GFP_KERNEL);
2845         if (!target_cg->default_groups) {
2846                 pr_err("Unable to allocate target_cg->default_groups\n");
2847                 ret = -ENOMEM;
2848                 goto out_global;
2849         }
2850
2851         config_group_init_type_name(&target_core_hbagroup,
2852                         "core", &target_core_cit);
2853         target_cg->default_groups[0] = &target_core_hbagroup;
2854         target_cg->default_groups[1] = NULL;
2855         /*
2856          * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2857          */
2858         hba_cg = &target_core_hbagroup;
2859         hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2860                                 GFP_KERNEL);
2861         if (!hba_cg->default_groups) {
2862                 pr_err("Unable to allocate hba_cg->default_groups\n");
2863                 ret = -ENOMEM;
2864                 goto out_global;
2865         }
2866         config_group_init_type_name(&alua_group,
2867                         "alua", &target_core_alua_cit);
2868         hba_cg->default_groups[0] = &alua_group;
2869         hba_cg->default_groups[1] = NULL;
2870         /*
2871          * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2872          * groups under /sys/kernel/config/target/core/alua/
2873          */
2874         alua_cg = &alua_group;
2875         alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2876                         GFP_KERNEL);
2877         if (!alua_cg->default_groups) {
2878                 pr_err("Unable to allocate alua_cg->default_groups\n");
2879                 ret = -ENOMEM;
2880                 goto out_global;
2881         }
2882
2883         config_group_init_type_name(&alua_lu_gps_group,
2884                         "lu_gps", &target_core_alua_lu_gps_cit);
2885         alua_cg->default_groups[0] = &alua_lu_gps_group;
2886         alua_cg->default_groups[1] = NULL;
2887         /*
2888          * Add core/alua/lu_gps/default_lu_gp
2889          */
2890         lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
2891         if (IS_ERR(lu_gp)) {
2892                 ret = -ENOMEM;
2893                 goto out_global;
2894         }
2895
2896         lu_gp_cg = &alua_lu_gps_group;
2897         lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2898                         GFP_KERNEL);
2899         if (!lu_gp_cg->default_groups) {
2900                 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
2901                 ret = -ENOMEM;
2902                 goto out_global;
2903         }
2904
2905         config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2906                                 &target_core_alua_lu_gp_cit);
2907         lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2908         lu_gp_cg->default_groups[1] = NULL;
2909         default_lu_gp = lu_gp;
2910         /*
2911          * Register the target_core_mod subsystem with configfs.
2912          */
2913         ret = configfs_register_subsystem(subsys);
2914         if (ret < 0) {
2915                 pr_err("Error %d while registering subsystem %s\n",
2916                         ret, subsys->su_group.cg_item.ci_namebuf);
2917                 goto out_global;
2918         }
2919         pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
2920                 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2921                 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2922         /*
2923          * Register built-in RAMDISK subsystem logic for virtual LUN 0
2924          */
2925         ret = rd_module_init();
2926         if (ret < 0)
2927                 goto out;
2928
2929         ret = core_dev_setup_virtual_lun0();
2930         if (ret < 0)
2931                 goto out;
2932
2933         return 0;
2934
2935 out:
2936         configfs_unregister_subsystem(subsys);
2937         core_dev_release_virtual_lun0();
2938         rd_module_exit();
2939 out_global:
2940         if (default_lu_gp) {
2941                 core_alua_free_lu_gp(default_lu_gp);
2942                 default_lu_gp = NULL;
2943         }
2944         if (lu_gp_cg)
2945                 kfree(lu_gp_cg->default_groups);
2946         if (alua_cg)
2947                 kfree(alua_cg->default_groups);
2948         if (hba_cg)
2949                 kfree(hba_cg->default_groups);
2950         kfree(target_cg->default_groups);
2951         release_se_kmem_caches();
2952         return ret;
2953 }
2954
2955 static void __exit target_core_exit_configfs(void)
2956 {
2957         struct configfs_subsystem *subsys;
2958         struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2959         struct config_item *item;
2960         int i;
2961
2962         subsys = target_core_subsystem[0];
2963
2964         lu_gp_cg = &alua_lu_gps_group;
2965         for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2966                 item = &lu_gp_cg->default_groups[i]->cg_item;
2967                 lu_gp_cg->default_groups[i] = NULL;
2968                 config_item_put(item);
2969         }
2970         kfree(lu_gp_cg->default_groups);
2971         lu_gp_cg->default_groups = NULL;
2972
2973         alua_cg = &alua_group;
2974         for (i = 0; alua_cg->default_groups[i]; i++) {
2975                 item = &alua_cg->default_groups[i]->cg_item;
2976                 alua_cg->default_groups[i] = NULL;
2977                 config_item_put(item);
2978         }
2979         kfree(alua_cg->default_groups);
2980         alua_cg->default_groups = NULL;
2981
2982         hba_cg = &target_core_hbagroup;
2983         for (i = 0; hba_cg->default_groups[i]; i++) {
2984                 item = &hba_cg->default_groups[i]->cg_item;
2985                 hba_cg->default_groups[i] = NULL;
2986                 config_item_put(item);
2987         }
2988         kfree(hba_cg->default_groups);
2989         hba_cg->default_groups = NULL;
2990         /*
2991          * We expect subsys->su_group.default_groups to be released
2992          * by configfs subsystem provider logic..
2993          */
2994         configfs_unregister_subsystem(subsys);
2995         kfree(subsys->su_group.default_groups);
2996
2997         core_alua_free_lu_gp(default_lu_gp);
2998         default_lu_gp = NULL;
2999
3000         pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3001                         " Infrastructure\n");
3002
3003         core_dev_release_virtual_lun0();
3004         rd_module_exit();
3005         release_se_kmem_caches();
3006 }
3007
3008 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3009 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3010 MODULE_LICENSE("GPL");
3011
3012 module_init(target_core_init_configfs);
3013 module_exit(target_core_exit_configfs);