]> Pileus Git - ~andy/linux/blob - drivers/target/target_core_configfs.c
qlcnic: Remove inline keyword
[~andy/linux] / 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
987         memset(i_buf, 0, PR_REG_ISID_ID_LEN);
988
989         pr_reg = dev->dev_pr_res_holder;
990         if (!pr_reg)
991                 return sprintf(page, "No SPC-3 Reservation holder\n");
992
993         se_nacl = pr_reg->pr_reg_nacl;
994         core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
995
996         return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
997                 se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
998                 se_nacl->initiatorname, i_buf);
999 }
1000
1001 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1002                 char *page)
1003 {
1004         struct se_node_acl *se_nacl;
1005         ssize_t len;
1006
1007         se_nacl = dev->dev_reserved_node_acl;
1008         if (se_nacl) {
1009                 len = sprintf(page,
1010                               "SPC-2 Reservation: %s Initiator: %s\n",
1011                               se_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
1012                               se_nacl->initiatorname);
1013         } else {
1014                 len = sprintf(page, "No SPC-2 Reservation holder\n");
1015         }
1016         return len;
1017 }
1018
1019 static ssize_t target_core_dev_pr_show_attr_res_holder(struct se_device *dev,
1020                 char *page)
1021 {
1022         int ret;
1023
1024         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1025                 return sprintf(page, "Passthrough\n");
1026
1027         spin_lock(&dev->dev_reservation_lock);
1028         if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1029                 ret = target_core_dev_pr_show_spc2_res(dev, page);
1030         else
1031                 ret = target_core_dev_pr_show_spc3_res(dev, page);
1032         spin_unlock(&dev->dev_reservation_lock);
1033         return ret;
1034 }
1035
1036 SE_DEV_PR_ATTR_RO(res_holder);
1037
1038 static ssize_t target_core_dev_pr_show_attr_res_pr_all_tgt_pts(
1039                 struct se_device *dev, char *page)
1040 {
1041         ssize_t len = 0;
1042
1043         spin_lock(&dev->dev_reservation_lock);
1044         if (!dev->dev_pr_res_holder) {
1045                 len = sprintf(page, "No SPC-3 Reservation holder\n");
1046         } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1047                 len = sprintf(page, "SPC-3 Reservation: All Target"
1048                         " Ports registration\n");
1049         } else {
1050                 len = sprintf(page, "SPC-3 Reservation: Single"
1051                         " Target Port registration\n");
1052         }
1053
1054         spin_unlock(&dev->dev_reservation_lock);
1055         return len;
1056 }
1057
1058 SE_DEV_PR_ATTR_RO(res_pr_all_tgt_pts);
1059
1060 static ssize_t target_core_dev_pr_show_attr_res_pr_generation(
1061                 struct se_device *dev, char *page)
1062 {
1063         return sprintf(page, "0x%08x\n", dev->t10_pr.pr_generation);
1064 }
1065
1066 SE_DEV_PR_ATTR_RO(res_pr_generation);
1067
1068 /*
1069  * res_pr_holder_tg_port
1070  */
1071 static ssize_t target_core_dev_pr_show_attr_res_pr_holder_tg_port(
1072                 struct se_device *dev, char *page)
1073 {
1074         struct se_node_acl *se_nacl;
1075         struct se_lun *lun;
1076         struct se_portal_group *se_tpg;
1077         struct t10_pr_registration *pr_reg;
1078         struct target_core_fabric_ops *tfo;
1079         ssize_t len = 0;
1080
1081         spin_lock(&dev->dev_reservation_lock);
1082         pr_reg = dev->dev_pr_res_holder;
1083         if (!pr_reg) {
1084                 len = sprintf(page, "No SPC-3 Reservation holder\n");
1085                 goto out_unlock;
1086         }
1087
1088         se_nacl = pr_reg->pr_reg_nacl;
1089         se_tpg = se_nacl->se_tpg;
1090         lun = pr_reg->pr_reg_tg_pt_lun;
1091         tfo = se_tpg->se_tpg_tfo;
1092
1093         len += sprintf(page+len, "SPC-3 Reservation: %s"
1094                 " Target Node Endpoint: %s\n", tfo->get_fabric_name(),
1095                 tfo->tpg_get_wwn(se_tpg));
1096         len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1097                 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1098                 " %s Logical Unit: %u\n", lun->lun_sep->sep_rtpi,
1099                 tfo->get_fabric_name(), tfo->tpg_get_tag(se_tpg),
1100                 tfo->get_fabric_name(), lun->unpacked_lun);
1101
1102 out_unlock:
1103         spin_unlock(&dev->dev_reservation_lock);
1104         return len;
1105 }
1106
1107 SE_DEV_PR_ATTR_RO(res_pr_holder_tg_port);
1108
1109 static ssize_t target_core_dev_pr_show_attr_res_pr_registered_i_pts(
1110                 struct se_device *dev, char *page)
1111 {
1112         struct target_core_fabric_ops *tfo;
1113         struct t10_pr_registration *pr_reg;
1114         unsigned char buf[384];
1115         char i_buf[PR_REG_ISID_ID_LEN];
1116         ssize_t len = 0;
1117         int reg_count = 0;
1118
1119         len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1120
1121         spin_lock(&dev->t10_pr.registration_lock);
1122         list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1123                         pr_reg_list) {
1124
1125                 memset(buf, 0, 384);
1126                 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1127                 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1128                 core_pr_dump_initiator_port(pr_reg, i_buf,
1129                                         PR_REG_ISID_ID_LEN);
1130                 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1131                         tfo->get_fabric_name(),
1132                         pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1133                         pr_reg->pr_res_generation);
1134
1135                 if (len + strlen(buf) >= PAGE_SIZE)
1136                         break;
1137
1138                 len += sprintf(page+len, "%s", buf);
1139                 reg_count++;
1140         }
1141         spin_unlock(&dev->t10_pr.registration_lock);
1142
1143         if (!reg_count)
1144                 len += sprintf(page+len, "None\n");
1145
1146         return len;
1147 }
1148
1149 SE_DEV_PR_ATTR_RO(res_pr_registered_i_pts);
1150
1151 static ssize_t target_core_dev_pr_show_attr_res_pr_type(
1152                 struct se_device *dev, char *page)
1153 {
1154         struct t10_pr_registration *pr_reg;
1155         ssize_t len = 0;
1156
1157         spin_lock(&dev->dev_reservation_lock);
1158         pr_reg = dev->dev_pr_res_holder;
1159         if (pr_reg) {
1160                 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1161                         core_scsi3_pr_dump_type(pr_reg->pr_res_type));
1162         } else {
1163                 len = sprintf(page, "No SPC-3 Reservation holder\n");
1164         }
1165
1166         spin_unlock(&dev->dev_reservation_lock);
1167         return len;
1168 }
1169
1170 SE_DEV_PR_ATTR_RO(res_pr_type);
1171
1172 static ssize_t target_core_dev_pr_show_attr_res_type(
1173                 struct se_device *dev, char *page)
1174 {
1175         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1176                 return sprintf(page, "SPC_PASSTHROUGH\n");
1177         else if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1178                 return sprintf(page, "SPC2_RESERVATIONS\n");
1179         else
1180                 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
1181 }
1182
1183 SE_DEV_PR_ATTR_RO(res_type);
1184
1185 static ssize_t target_core_dev_pr_show_attr_res_aptpl_active(
1186                 struct se_device *dev, char *page)
1187 {
1188         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1189                 return 0;
1190
1191         return sprintf(page, "APTPL Bit Status: %s\n",
1192                 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
1193 }
1194
1195 SE_DEV_PR_ATTR_RO(res_aptpl_active);
1196
1197 /*
1198  * res_aptpl_metadata
1199  */
1200 static ssize_t target_core_dev_pr_show_attr_res_aptpl_metadata(
1201                 struct se_device *dev, char *page)
1202 {
1203         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1204                 return 0;
1205
1206         return sprintf(page, "Ready to process PR APTPL metadata..\n");
1207 }
1208
1209 enum {
1210         Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
1211         Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
1212         Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
1213         Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
1214 };
1215
1216 static match_table_t tokens = {
1217         {Opt_initiator_fabric, "initiator_fabric=%s"},
1218         {Opt_initiator_node, "initiator_node=%s"},
1219         {Opt_initiator_sid, "initiator_sid=%s"},
1220         {Opt_sa_res_key, "sa_res_key=%s"},
1221         {Opt_res_holder, "res_holder=%d"},
1222         {Opt_res_type, "res_type=%d"},
1223         {Opt_res_scope, "res_scope=%d"},
1224         {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
1225         {Opt_mapped_lun, "mapped_lun=%d"},
1226         {Opt_target_fabric, "target_fabric=%s"},
1227         {Opt_target_node, "target_node=%s"},
1228         {Opt_tpgt, "tpgt=%d"},
1229         {Opt_port_rtpi, "port_rtpi=%d"},
1230         {Opt_target_lun, "target_lun=%d"},
1231         {Opt_err, NULL}
1232 };
1233
1234 static ssize_t target_core_dev_pr_store_attr_res_aptpl_metadata(
1235         struct se_device *dev,
1236         const char *page,
1237         size_t count)
1238 {
1239         unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
1240         unsigned char *t_fabric = NULL, *t_port = NULL;
1241         char *orig, *ptr, *arg_p, *opts;
1242         substring_t args[MAX_OPT_ARGS];
1243         unsigned long long tmp_ll;
1244         u64 sa_res_key = 0;
1245         u32 mapped_lun = 0, target_lun = 0;
1246         int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
1247         u16 port_rpti = 0, tpgt = 0;
1248         u8 type = 0, scope;
1249
1250         if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1251                 return 0;
1252         if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1253                 return 0;
1254
1255         if (dev->export_count) {
1256                 pr_debug("Unable to process APTPL metadata while"
1257                         " active fabric exports exist\n");
1258                 return -EINVAL;
1259         }
1260
1261         opts = kstrdup(page, GFP_KERNEL);
1262         if (!opts)
1263                 return -ENOMEM;
1264
1265         orig = opts;
1266         while ((ptr = strsep(&opts, ",\n")) != NULL) {
1267                 if (!*ptr)
1268                         continue;
1269
1270                 token = match_token(ptr, tokens, args);
1271                 switch (token) {
1272                 case Opt_initiator_fabric:
1273                         i_fabric = match_strdup(&args[0]);
1274                         if (!i_fabric) {
1275                                 ret = -ENOMEM;
1276                                 goto out;
1277                         }
1278                         break;
1279                 case Opt_initiator_node:
1280                         i_port = match_strdup(&args[0]);
1281                         if (!i_port) {
1282                                 ret = -ENOMEM;
1283                                 goto out;
1284                         }
1285                         if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
1286                                 pr_err("APTPL metadata initiator_node="
1287                                         " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
1288                                         PR_APTPL_MAX_IPORT_LEN);
1289                                 ret = -EINVAL;
1290                                 break;
1291                         }
1292                         break;
1293                 case Opt_initiator_sid:
1294                         isid = match_strdup(&args[0]);
1295                         if (!isid) {
1296                                 ret = -ENOMEM;
1297                                 goto out;
1298                         }
1299                         if (strlen(isid) >= PR_REG_ISID_LEN) {
1300                                 pr_err("APTPL metadata initiator_isid"
1301                                         "= exceeds PR_REG_ISID_LEN: %d\n",
1302                                         PR_REG_ISID_LEN);
1303                                 ret = -EINVAL;
1304                                 break;
1305                         }
1306                         break;
1307                 case Opt_sa_res_key:
1308                         arg_p = match_strdup(&args[0]);
1309                         if (!arg_p) {
1310                                 ret = -ENOMEM;
1311                                 goto out;
1312                         }
1313                         ret = strict_strtoull(arg_p, 0, &tmp_ll);
1314                         if (ret < 0) {
1315                                 pr_err("strict_strtoull() failed for"
1316                                         " sa_res_key=\n");
1317                                 goto out;
1318                         }
1319                         sa_res_key = (u64)tmp_ll;
1320                         break;
1321                 /*
1322                  * PR APTPL Metadata for Reservation
1323                  */
1324                 case Opt_res_holder:
1325                         match_int(args, &arg);
1326                         res_holder = arg;
1327                         break;
1328                 case Opt_res_type:
1329                         match_int(args, &arg);
1330                         type = (u8)arg;
1331                         break;
1332                 case Opt_res_scope:
1333                         match_int(args, &arg);
1334                         scope = (u8)arg;
1335                         break;
1336                 case Opt_res_all_tg_pt:
1337                         match_int(args, &arg);
1338                         all_tg_pt = (int)arg;
1339                         break;
1340                 case Opt_mapped_lun:
1341                         match_int(args, &arg);
1342                         mapped_lun = (u32)arg;
1343                         break;
1344                 /*
1345                  * PR APTPL Metadata for Target Port
1346                  */
1347                 case Opt_target_fabric:
1348                         t_fabric = match_strdup(&args[0]);
1349                         if (!t_fabric) {
1350                                 ret = -ENOMEM;
1351                                 goto out;
1352                         }
1353                         break;
1354                 case Opt_target_node:
1355                         t_port = match_strdup(&args[0]);
1356                         if (!t_port) {
1357                                 ret = -ENOMEM;
1358                                 goto out;
1359                         }
1360                         if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
1361                                 pr_err("APTPL metadata target_node="
1362                                         " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
1363                                         PR_APTPL_MAX_TPORT_LEN);
1364                                 ret = -EINVAL;
1365                                 break;
1366                         }
1367                         break;
1368                 case Opt_tpgt:
1369                         match_int(args, &arg);
1370                         tpgt = (u16)arg;
1371                         break;
1372                 case Opt_port_rtpi:
1373                         match_int(args, &arg);
1374                         port_rpti = (u16)arg;
1375                         break;
1376                 case Opt_target_lun:
1377                         match_int(args, &arg);
1378                         target_lun = (u32)arg;
1379                         break;
1380                 default:
1381                         break;
1382                 }
1383         }
1384
1385         if (!i_port || !t_port || !sa_res_key) {
1386                 pr_err("Illegal parameters for APTPL registration\n");
1387                 ret = -EINVAL;
1388                 goto out;
1389         }
1390
1391         if (res_holder && !(type)) {
1392                 pr_err("Illegal PR type: 0x%02x for reservation"
1393                                 " holder\n", type);
1394                 ret = -EINVAL;
1395                 goto out;
1396         }
1397
1398         ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
1399                         i_port, isid, mapped_lun, t_port, tpgt, target_lun,
1400                         res_holder, all_tg_pt, type);
1401 out:
1402         kfree(i_fabric);
1403         kfree(i_port);
1404         kfree(isid);
1405         kfree(t_fabric);
1406         kfree(t_port);
1407         kfree(orig);
1408         return (ret == 0) ? count : ret;
1409 }
1410
1411 SE_DEV_PR_ATTR(res_aptpl_metadata, S_IRUGO | S_IWUSR);
1412
1413 CONFIGFS_EATTR_OPS(target_core_dev_pr, se_device, dev_pr_group);
1414
1415 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
1416         &target_core_dev_pr_res_holder.attr,
1417         &target_core_dev_pr_res_pr_all_tgt_pts.attr,
1418         &target_core_dev_pr_res_pr_generation.attr,
1419         &target_core_dev_pr_res_pr_holder_tg_port.attr,
1420         &target_core_dev_pr_res_pr_registered_i_pts.attr,
1421         &target_core_dev_pr_res_pr_type.attr,
1422         &target_core_dev_pr_res_type.attr,
1423         &target_core_dev_pr_res_aptpl_active.attr,
1424         &target_core_dev_pr_res_aptpl_metadata.attr,
1425         NULL,
1426 };
1427
1428 static struct configfs_item_operations target_core_dev_pr_ops = {
1429         .show_attribute         = target_core_dev_pr_attr_show,
1430         .store_attribute        = target_core_dev_pr_attr_store,
1431 };
1432
1433 static struct config_item_type target_core_dev_pr_cit = {
1434         .ct_item_ops            = &target_core_dev_pr_ops,
1435         .ct_attrs               = target_core_dev_pr_attrs,
1436         .ct_owner               = THIS_MODULE,
1437 };
1438
1439 /*  End functions for struct config_item_type target_core_dev_pr_cit */
1440
1441 /*  Start functions for struct config_item_type target_core_dev_cit */
1442
1443 static ssize_t target_core_show_dev_info(void *p, char *page)
1444 {
1445         struct se_device *dev = p;
1446         struct se_subsystem_api *t = dev->transport;
1447         int bl = 0;
1448         ssize_t read_bytes = 0;
1449
1450         transport_dump_dev_state(dev, page, &bl);
1451         read_bytes += bl;
1452         read_bytes += t->show_configfs_dev_params(dev, page+read_bytes);
1453         return read_bytes;
1454 }
1455
1456 static struct target_core_configfs_attribute target_core_attr_dev_info = {
1457         .attr   = { .ca_owner = THIS_MODULE,
1458                     .ca_name = "info",
1459                     .ca_mode = S_IRUGO },
1460         .show   = target_core_show_dev_info,
1461         .store  = NULL,
1462 };
1463
1464 static ssize_t target_core_store_dev_control(
1465         void *p,
1466         const char *page,
1467         size_t count)
1468 {
1469         struct se_device *dev = p;
1470         struct se_subsystem_api *t = dev->transport;
1471
1472         return t->set_configfs_dev_params(dev, page, count);
1473 }
1474
1475 static struct target_core_configfs_attribute target_core_attr_dev_control = {
1476         .attr   = { .ca_owner = THIS_MODULE,
1477                     .ca_name = "control",
1478                     .ca_mode = S_IWUSR },
1479         .show   = NULL,
1480         .store  = target_core_store_dev_control,
1481 };
1482
1483 static ssize_t target_core_show_dev_alias(void *p, char *page)
1484 {
1485         struct se_device *dev = p;
1486
1487         if (!(dev->dev_flags & DF_USING_ALIAS))
1488                 return 0;
1489
1490         return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
1491 }
1492
1493 static ssize_t target_core_store_dev_alias(
1494         void *p,
1495         const char *page,
1496         size_t count)
1497 {
1498         struct se_device *dev = p;
1499         struct se_hba *hba = dev->se_hba;
1500         ssize_t read_bytes;
1501
1502         if (count > (SE_DEV_ALIAS_LEN-1)) {
1503                 pr_err("alias count: %d exceeds"
1504                         " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
1505                         SE_DEV_ALIAS_LEN-1);
1506                 return -EINVAL;
1507         }
1508
1509         read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
1510         if (!read_bytes)
1511                 return -EINVAL;
1512         if (dev->dev_alias[read_bytes - 1] == '\n')
1513                 dev->dev_alias[read_bytes - 1] = '\0';
1514
1515         dev->dev_flags |= DF_USING_ALIAS;
1516
1517         pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
1518                 config_item_name(&hba->hba_group.cg_item),
1519                 config_item_name(&dev->dev_group.cg_item),
1520                 dev->dev_alias);
1521
1522         return read_bytes;
1523 }
1524
1525 static struct target_core_configfs_attribute target_core_attr_dev_alias = {
1526         .attr   = { .ca_owner = THIS_MODULE,
1527                     .ca_name = "alias",
1528                     .ca_mode =  S_IRUGO | S_IWUSR },
1529         .show   = target_core_show_dev_alias,
1530         .store  = target_core_store_dev_alias,
1531 };
1532
1533 static ssize_t target_core_show_dev_udev_path(void *p, char *page)
1534 {
1535         struct se_device *dev = p;
1536
1537         if (!(dev->dev_flags & DF_USING_UDEV_PATH))
1538                 return 0;
1539
1540         return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
1541 }
1542
1543 static ssize_t target_core_store_dev_udev_path(
1544         void *p,
1545         const char *page,
1546         size_t count)
1547 {
1548         struct se_device *dev = p;
1549         struct se_hba *hba = dev->se_hba;
1550         ssize_t read_bytes;
1551
1552         if (count > (SE_UDEV_PATH_LEN-1)) {
1553                 pr_err("udev_path count: %d exceeds"
1554                         " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
1555                         SE_UDEV_PATH_LEN-1);
1556                 return -EINVAL;
1557         }
1558
1559         read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
1560                         "%s", page);
1561         if (!read_bytes)
1562                 return -EINVAL;
1563         if (dev->udev_path[read_bytes - 1] == '\n')
1564                 dev->udev_path[read_bytes - 1] = '\0';
1565
1566         dev->dev_flags |= DF_USING_UDEV_PATH;
1567
1568         pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
1569                 config_item_name(&hba->hba_group.cg_item),
1570                 config_item_name(&dev->dev_group.cg_item),
1571                 dev->udev_path);
1572
1573         return read_bytes;
1574 }
1575
1576 static struct target_core_configfs_attribute target_core_attr_dev_udev_path = {
1577         .attr   = { .ca_owner = THIS_MODULE,
1578                     .ca_name = "udev_path",
1579                     .ca_mode =  S_IRUGO | S_IWUSR },
1580         .show   = target_core_show_dev_udev_path,
1581         .store  = target_core_store_dev_udev_path,
1582 };
1583
1584 static ssize_t target_core_show_dev_enable(void *p, char *page)
1585 {
1586         struct se_device *dev = p;
1587
1588         return snprintf(page, PAGE_SIZE, "%d\n", !!(dev->dev_flags & DF_CONFIGURED));
1589 }
1590
1591 static ssize_t target_core_store_dev_enable(
1592         void *p,
1593         const char *page,
1594         size_t count)
1595 {
1596         struct se_device *dev = p;
1597         char *ptr;
1598         int ret;
1599
1600         ptr = strstr(page, "1");
1601         if (!ptr) {
1602                 pr_err("For dev_enable ops, only valid value"
1603                                 " is \"1\"\n");
1604                 return -EINVAL;
1605         }
1606
1607         ret = target_configure_device(dev);
1608         if (ret)
1609                 return ret;
1610         return count;
1611 }
1612
1613 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1614         .attr   = { .ca_owner = THIS_MODULE,
1615                     .ca_name = "enable",
1616                     .ca_mode =  S_IRUGO | S_IWUSR },
1617         .show   = target_core_show_dev_enable,
1618         .store  = target_core_store_dev_enable,
1619 };
1620
1621 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1622 {
1623         struct se_device *dev = p;
1624         struct config_item *lu_ci;
1625         struct t10_alua_lu_gp *lu_gp;
1626         struct t10_alua_lu_gp_member *lu_gp_mem;
1627         ssize_t len = 0;
1628
1629         lu_gp_mem = dev->dev_alua_lu_gp_mem;
1630         if (!lu_gp_mem)
1631                 return 0;
1632
1633         spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1634         lu_gp = lu_gp_mem->lu_gp;
1635         if (lu_gp) {
1636                 lu_ci = &lu_gp->lu_gp_group.cg_item;
1637                 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1638                         config_item_name(lu_ci), lu_gp->lu_gp_id);
1639         }
1640         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1641
1642         return len;
1643 }
1644
1645 static ssize_t target_core_store_alua_lu_gp(
1646         void *p,
1647         const char *page,
1648         size_t count)
1649 {
1650         struct se_device *dev = p;
1651         struct se_hba *hba = dev->se_hba;
1652         struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1653         struct t10_alua_lu_gp_member *lu_gp_mem;
1654         unsigned char buf[LU_GROUP_NAME_BUF];
1655         int move = 0;
1656
1657         lu_gp_mem = dev->dev_alua_lu_gp_mem;
1658         if (!lu_gp_mem)
1659                 return 0;
1660
1661         if (count > LU_GROUP_NAME_BUF) {
1662                 pr_err("ALUA LU Group Alias too large!\n");
1663                 return -EINVAL;
1664         }
1665         memset(buf, 0, LU_GROUP_NAME_BUF);
1666         memcpy(buf, page, count);
1667         /*
1668          * Any ALUA logical unit alias besides "NULL" means we will be
1669          * making a new group association.
1670          */
1671         if (strcmp(strstrip(buf), "NULL")) {
1672                 /*
1673                  * core_alua_get_lu_gp_by_name() will increment reference to
1674                  * struct t10_alua_lu_gp.  This reference is released with
1675                  * core_alua_get_lu_gp_by_name below().
1676                  */
1677                 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1678                 if (!lu_gp_new)
1679                         return -ENODEV;
1680         }
1681
1682         spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1683         lu_gp = lu_gp_mem->lu_gp;
1684         if (lu_gp) {
1685                 /*
1686                  * Clearing an existing lu_gp association, and replacing
1687                  * with NULL
1688                  */
1689                 if (!lu_gp_new) {
1690                         pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
1691                                 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1692                                 " %hu\n",
1693                                 config_item_name(&hba->hba_group.cg_item),
1694                                 config_item_name(&dev->dev_group.cg_item),
1695                                 config_item_name(&lu_gp->lu_gp_group.cg_item),
1696                                 lu_gp->lu_gp_id);
1697
1698                         __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1699                         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1700
1701                         return count;
1702                 }
1703                 /*
1704                  * Removing existing association of lu_gp_mem with lu_gp
1705                  */
1706                 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1707                 move = 1;
1708         }
1709         /*
1710          * Associate lu_gp_mem with lu_gp_new.
1711          */
1712         __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1713         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1714
1715         pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1716                 " core/alua/lu_gps/%s, ID: %hu\n",
1717                 (move) ? "Moving" : "Adding",
1718                 config_item_name(&hba->hba_group.cg_item),
1719                 config_item_name(&dev->dev_group.cg_item),
1720                 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1721                 lu_gp_new->lu_gp_id);
1722
1723         core_alua_put_lu_gp_from_name(lu_gp_new);
1724         return count;
1725 }
1726
1727 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1728         .attr   = { .ca_owner = THIS_MODULE,
1729                     .ca_name = "alua_lu_gp",
1730                     .ca_mode = S_IRUGO | S_IWUSR },
1731         .show   = target_core_show_alua_lu_gp,
1732         .store  = target_core_store_alua_lu_gp,
1733 };
1734
1735 static struct configfs_attribute *lio_core_dev_attrs[] = {
1736         &target_core_attr_dev_info.attr,
1737         &target_core_attr_dev_control.attr,
1738         &target_core_attr_dev_alias.attr,
1739         &target_core_attr_dev_udev_path.attr,
1740         &target_core_attr_dev_enable.attr,
1741         &target_core_attr_dev_alua_lu_gp.attr,
1742         NULL,
1743 };
1744
1745 static void target_core_dev_release(struct config_item *item)
1746 {
1747         struct config_group *dev_cg = to_config_group(item);
1748         struct se_device *dev =
1749                 container_of(dev_cg, struct se_device, dev_group);
1750
1751         kfree(dev_cg->default_groups);
1752         target_free_device(dev);
1753 }
1754
1755 static ssize_t target_core_dev_show(struct config_item *item,
1756                                      struct configfs_attribute *attr,
1757                                      char *page)
1758 {
1759         struct config_group *dev_cg = to_config_group(item);
1760         struct se_device *dev =
1761                 container_of(dev_cg, struct se_device, dev_group);
1762         struct target_core_configfs_attribute *tc_attr = container_of(
1763                         attr, struct target_core_configfs_attribute, attr);
1764
1765         if (!tc_attr->show)
1766                 return -EINVAL;
1767
1768         return tc_attr->show(dev, page);
1769 }
1770
1771 static ssize_t target_core_dev_store(struct config_item *item,
1772                                       struct configfs_attribute *attr,
1773                                       const char *page, size_t count)
1774 {
1775         struct config_group *dev_cg = to_config_group(item);
1776         struct se_device *dev =
1777                 container_of(dev_cg, struct se_device, dev_group);
1778         struct target_core_configfs_attribute *tc_attr = container_of(
1779                         attr, struct target_core_configfs_attribute, attr);
1780
1781         if (!tc_attr->store)
1782                 return -EINVAL;
1783
1784         return tc_attr->store(dev, page, count);
1785 }
1786
1787 static struct configfs_item_operations target_core_dev_item_ops = {
1788         .release                = target_core_dev_release,
1789         .show_attribute         = target_core_dev_show,
1790         .store_attribute        = target_core_dev_store,
1791 };
1792
1793 static struct config_item_type target_core_dev_cit = {
1794         .ct_item_ops            = &target_core_dev_item_ops,
1795         .ct_attrs               = lio_core_dev_attrs,
1796         .ct_owner               = THIS_MODULE,
1797 };
1798
1799 /* End functions for struct config_item_type target_core_dev_cit */
1800
1801 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
1802
1803 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
1804 #define SE_DEV_ALUA_LU_ATTR(_name, _mode)                               \
1805 static struct target_core_alua_lu_gp_attribute                          \
1806                         target_core_alua_lu_gp_##_name =                \
1807         __CONFIGFS_EATTR(_name, _mode,                                  \
1808         target_core_alua_lu_gp_show_attr_##_name,                       \
1809         target_core_alua_lu_gp_store_attr_##_name);
1810
1811 #define SE_DEV_ALUA_LU_ATTR_RO(_name)                                   \
1812 static struct target_core_alua_lu_gp_attribute                          \
1813                         target_core_alua_lu_gp_##_name =                \
1814         __CONFIGFS_EATTR_RO(_name,                                      \
1815         target_core_alua_lu_gp_show_attr_##_name);
1816
1817 /*
1818  * lu_gp_id
1819  */
1820 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
1821         struct t10_alua_lu_gp *lu_gp,
1822         char *page)
1823 {
1824         if (!lu_gp->lu_gp_valid_id)
1825                 return 0;
1826
1827         return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
1828 }
1829
1830 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
1831         struct t10_alua_lu_gp *lu_gp,
1832         const char *page,
1833         size_t count)
1834 {
1835         struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
1836         unsigned long lu_gp_id;
1837         int ret;
1838
1839         ret = strict_strtoul(page, 0, &lu_gp_id);
1840         if (ret < 0) {
1841                 pr_err("strict_strtoul() returned %d for"
1842                         " lu_gp_id\n", ret);
1843                 return -EINVAL;
1844         }
1845         if (lu_gp_id > 0x0000ffff) {
1846                 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
1847                         " 0x0000ffff\n", lu_gp_id);
1848                 return -EINVAL;
1849         }
1850
1851         ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
1852         if (ret < 0)
1853                 return -EINVAL;
1854
1855         pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
1856                 " Group: core/alua/lu_gps/%s to ID: %hu\n",
1857                 config_item_name(&alua_lu_gp_cg->cg_item),
1858                 lu_gp->lu_gp_id);
1859
1860         return count;
1861 }
1862
1863 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
1864
1865 /*
1866  * members
1867  */
1868 static ssize_t target_core_alua_lu_gp_show_attr_members(
1869         struct t10_alua_lu_gp *lu_gp,
1870         char *page)
1871 {
1872         struct se_device *dev;
1873         struct se_hba *hba;
1874         struct t10_alua_lu_gp_member *lu_gp_mem;
1875         ssize_t len = 0, cur_len;
1876         unsigned char buf[LU_GROUP_NAME_BUF];
1877
1878         memset(buf, 0, LU_GROUP_NAME_BUF);
1879
1880         spin_lock(&lu_gp->lu_gp_lock);
1881         list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1882                 dev = lu_gp_mem->lu_gp_mem_dev;
1883                 hba = dev->se_hba;
1884
1885                 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
1886                         config_item_name(&hba->hba_group.cg_item),
1887                         config_item_name(&dev->dev_group.cg_item));
1888                 cur_len++; /* Extra byte for NULL terminator */
1889
1890                 if ((cur_len + len) > PAGE_SIZE) {
1891                         pr_warn("Ran out of lu_gp_show_attr"
1892                                 "_members buffer\n");
1893                         break;
1894                 }
1895                 memcpy(page+len, buf, cur_len);
1896                 len += cur_len;
1897         }
1898         spin_unlock(&lu_gp->lu_gp_lock);
1899
1900         return len;
1901 }
1902
1903 SE_DEV_ALUA_LU_ATTR_RO(members);
1904
1905 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
1906
1907 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
1908         &target_core_alua_lu_gp_lu_gp_id.attr,
1909         &target_core_alua_lu_gp_members.attr,
1910         NULL,
1911 };
1912
1913 static void target_core_alua_lu_gp_release(struct config_item *item)
1914 {
1915         struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1916                         struct t10_alua_lu_gp, lu_gp_group);
1917
1918         core_alua_free_lu_gp(lu_gp);
1919 }
1920
1921 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
1922         .release                = target_core_alua_lu_gp_release,
1923         .show_attribute         = target_core_alua_lu_gp_attr_show,
1924         .store_attribute        = target_core_alua_lu_gp_attr_store,
1925 };
1926
1927 static struct config_item_type target_core_alua_lu_gp_cit = {
1928         .ct_item_ops            = &target_core_alua_lu_gp_ops,
1929         .ct_attrs               = target_core_alua_lu_gp_attrs,
1930         .ct_owner               = THIS_MODULE,
1931 };
1932
1933 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
1934
1935 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
1936
1937 static struct config_group *target_core_alua_create_lu_gp(
1938         struct config_group *group,
1939         const char *name)
1940 {
1941         struct t10_alua_lu_gp *lu_gp;
1942         struct config_group *alua_lu_gp_cg = NULL;
1943         struct config_item *alua_lu_gp_ci = NULL;
1944
1945         lu_gp = core_alua_allocate_lu_gp(name, 0);
1946         if (IS_ERR(lu_gp))
1947                 return NULL;
1948
1949         alua_lu_gp_cg = &lu_gp->lu_gp_group;
1950         alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
1951
1952         config_group_init_type_name(alua_lu_gp_cg, name,
1953                         &target_core_alua_lu_gp_cit);
1954
1955         pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
1956                 " Group: core/alua/lu_gps/%s\n",
1957                 config_item_name(alua_lu_gp_ci));
1958
1959         return alua_lu_gp_cg;
1960
1961 }
1962
1963 static void target_core_alua_drop_lu_gp(
1964         struct config_group *group,
1965         struct config_item *item)
1966 {
1967         struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
1968                         struct t10_alua_lu_gp, lu_gp_group);
1969
1970         pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
1971                 " Group: core/alua/lu_gps/%s, ID: %hu\n",
1972                 config_item_name(item), lu_gp->lu_gp_id);
1973         /*
1974          * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
1975          * -> target_core_alua_lu_gp_release()
1976          */
1977         config_item_put(item);
1978 }
1979
1980 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
1981         .make_group             = &target_core_alua_create_lu_gp,
1982         .drop_item              = &target_core_alua_drop_lu_gp,
1983 };
1984
1985 static struct config_item_type target_core_alua_lu_gps_cit = {
1986         .ct_item_ops            = NULL,
1987         .ct_group_ops           = &target_core_alua_lu_gps_group_ops,
1988         .ct_owner               = THIS_MODULE,
1989 };
1990
1991 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
1992
1993 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
1994
1995 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
1996 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)                            \
1997 static struct target_core_alua_tg_pt_gp_attribute                       \
1998                         target_core_alua_tg_pt_gp_##_name =             \
1999         __CONFIGFS_EATTR(_name, _mode,                                  \
2000         target_core_alua_tg_pt_gp_show_attr_##_name,                    \
2001         target_core_alua_tg_pt_gp_store_attr_##_name);
2002
2003 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)                                \
2004 static struct target_core_alua_tg_pt_gp_attribute                       \
2005                         target_core_alua_tg_pt_gp_##_name =             \
2006         __CONFIGFS_EATTR_RO(_name,                                      \
2007         target_core_alua_tg_pt_gp_show_attr_##_name);
2008
2009 /*
2010  * alua_access_state
2011  */
2012 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2013         struct t10_alua_tg_pt_gp *tg_pt_gp,
2014         char *page)
2015 {
2016         return sprintf(page, "%d\n",
2017                 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2018 }
2019
2020 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2021         struct t10_alua_tg_pt_gp *tg_pt_gp,
2022         const char *page,
2023         size_t count)
2024 {
2025         struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2026         unsigned long tmp;
2027         int new_state, ret;
2028
2029         if (!tg_pt_gp->tg_pt_gp_valid_id) {
2030                 pr_err("Unable to do implict ALUA on non valid"
2031                         " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2032                 return -EINVAL;
2033         }
2034
2035         ret = strict_strtoul(page, 0, &tmp);
2036         if (ret < 0) {
2037                 pr_err("Unable to extract new ALUA access state from"
2038                                 " %s\n", page);
2039                 return -EINVAL;
2040         }
2041         new_state = (int)tmp;
2042
2043         if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2044                 pr_err("Unable to process implict configfs ALUA"
2045                         " transition while TPGS_IMPLICT_ALUA is disabled\n");
2046                 return -EINVAL;
2047         }
2048
2049         ret = core_alua_do_port_transition(tg_pt_gp, dev,
2050                                         NULL, NULL, new_state, 0);
2051         return (!ret) ? count : -EINVAL;
2052 }
2053
2054 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2055
2056 /*
2057  * alua_access_status
2058  */
2059 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2060         struct t10_alua_tg_pt_gp *tg_pt_gp,
2061         char *page)
2062 {
2063         return sprintf(page, "%s\n",
2064                 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2065 }
2066
2067 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2068         struct t10_alua_tg_pt_gp *tg_pt_gp,
2069         const char *page,
2070         size_t count)
2071 {
2072         unsigned long tmp;
2073         int new_status, ret;
2074
2075         if (!tg_pt_gp->tg_pt_gp_valid_id) {
2076                 pr_err("Unable to do set ALUA access status on non"
2077                         " valid tg_pt_gp ID: %hu\n",
2078                         tg_pt_gp->tg_pt_gp_valid_id);
2079                 return -EINVAL;
2080         }
2081
2082         ret = strict_strtoul(page, 0, &tmp);
2083         if (ret < 0) {
2084                 pr_err("Unable to extract new ALUA access status"
2085                                 " from %s\n", page);
2086                 return -EINVAL;
2087         }
2088         new_status = (int)tmp;
2089
2090         if ((new_status != ALUA_STATUS_NONE) &&
2091             (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2092             (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2093                 pr_err("Illegal ALUA access status: 0x%02x\n",
2094                                 new_status);
2095                 return -EINVAL;
2096         }
2097
2098         tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2099         return count;
2100 }
2101
2102 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2103
2104 /*
2105  * alua_access_type
2106  */
2107 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2108         struct t10_alua_tg_pt_gp *tg_pt_gp,
2109         char *page)
2110 {
2111         return core_alua_show_access_type(tg_pt_gp, page);
2112 }
2113
2114 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2115         struct t10_alua_tg_pt_gp *tg_pt_gp,
2116         const char *page,
2117         size_t count)
2118 {
2119         return core_alua_store_access_type(tg_pt_gp, page, count);
2120 }
2121
2122 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2123
2124 /*
2125  * alua_write_metadata
2126  */
2127 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2128         struct t10_alua_tg_pt_gp *tg_pt_gp,
2129         char *page)
2130 {
2131         return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2132 }
2133
2134 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2135         struct t10_alua_tg_pt_gp *tg_pt_gp,
2136         const char *page,
2137         size_t count)
2138 {
2139         unsigned long tmp;
2140         int ret;
2141
2142         ret = strict_strtoul(page, 0, &tmp);
2143         if (ret < 0) {
2144                 pr_err("Unable to extract alua_write_metadata\n");
2145                 return -EINVAL;
2146         }
2147
2148         if ((tmp != 0) && (tmp != 1)) {
2149                 pr_err("Illegal value for alua_write_metadata:"
2150                         " %lu\n", tmp);
2151                 return -EINVAL;
2152         }
2153         tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2154
2155         return count;
2156 }
2157
2158 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2159
2160
2161
2162 /*
2163  * nonop_delay_msecs
2164  */
2165 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2166         struct t10_alua_tg_pt_gp *tg_pt_gp,
2167         char *page)
2168 {
2169         return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2170
2171 }
2172
2173 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2174         struct t10_alua_tg_pt_gp *tg_pt_gp,
2175         const char *page,
2176         size_t count)
2177 {
2178         return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2179 }
2180
2181 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2182
2183 /*
2184  * trans_delay_msecs
2185  */
2186 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2187         struct t10_alua_tg_pt_gp *tg_pt_gp,
2188         char *page)
2189 {
2190         return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2191 }
2192
2193 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2194         struct t10_alua_tg_pt_gp *tg_pt_gp,
2195         const char *page,
2196         size_t count)
2197 {
2198         return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2199 }
2200
2201 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2202
2203 /*
2204  * implict_trans_secs
2205  */
2206 static ssize_t target_core_alua_tg_pt_gp_show_attr_implict_trans_secs(
2207         struct t10_alua_tg_pt_gp *tg_pt_gp,
2208         char *page)
2209 {
2210         return core_alua_show_implict_trans_secs(tg_pt_gp, page);
2211 }
2212
2213 static ssize_t target_core_alua_tg_pt_gp_store_attr_implict_trans_secs(
2214         struct t10_alua_tg_pt_gp *tg_pt_gp,
2215         const char *page,
2216         size_t count)
2217 {
2218         return core_alua_store_implict_trans_secs(tg_pt_gp, page, count);
2219 }
2220
2221 SE_DEV_ALUA_TG_PT_ATTR(implict_trans_secs, S_IRUGO | S_IWUSR);
2222
2223 /*
2224  * preferred
2225  */
2226
2227 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2228         struct t10_alua_tg_pt_gp *tg_pt_gp,
2229         char *page)
2230 {
2231         return core_alua_show_preferred_bit(tg_pt_gp, page);
2232 }
2233
2234 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2235         struct t10_alua_tg_pt_gp *tg_pt_gp,
2236         const char *page,
2237         size_t count)
2238 {
2239         return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2240 }
2241
2242 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2243
2244 /*
2245  * tg_pt_gp_id
2246  */
2247 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2248         struct t10_alua_tg_pt_gp *tg_pt_gp,
2249         char *page)
2250 {
2251         if (!tg_pt_gp->tg_pt_gp_valid_id)
2252                 return 0;
2253
2254         return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2255 }
2256
2257 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2258         struct t10_alua_tg_pt_gp *tg_pt_gp,
2259         const char *page,
2260         size_t count)
2261 {
2262         struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2263         unsigned long tg_pt_gp_id;
2264         int ret;
2265
2266         ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2267         if (ret < 0) {
2268                 pr_err("strict_strtoul() returned %d for"
2269                         " tg_pt_gp_id\n", ret);
2270                 return -EINVAL;
2271         }
2272         if (tg_pt_gp_id > 0x0000ffff) {
2273                 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum:"
2274                         " 0x0000ffff\n", tg_pt_gp_id);
2275                 return -EINVAL;
2276         }
2277
2278         ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2279         if (ret < 0)
2280                 return -EINVAL;
2281
2282         pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
2283                 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2284                 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2285                 tg_pt_gp->tg_pt_gp_id);
2286
2287         return count;
2288 }
2289
2290 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2291
2292 /*
2293  * members
2294  */
2295 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2296         struct t10_alua_tg_pt_gp *tg_pt_gp,
2297         char *page)
2298 {
2299         struct se_port *port;
2300         struct se_portal_group *tpg;
2301         struct se_lun *lun;
2302         struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2303         ssize_t len = 0, cur_len;
2304         unsigned char buf[TG_PT_GROUP_NAME_BUF];
2305
2306         memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2307
2308         spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2309         list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2310                         tg_pt_gp_mem_list) {
2311                 port = tg_pt_gp_mem->tg_pt;
2312                 tpg = port->sep_tpg;
2313                 lun = port->sep_lun;
2314
2315                 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2316                         "/%s\n", tpg->se_tpg_tfo->get_fabric_name(),
2317                         tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2318                         tpg->se_tpg_tfo->tpg_get_tag(tpg),
2319                         config_item_name(&lun->lun_group.cg_item));
2320                 cur_len++; /* Extra byte for NULL terminator */
2321
2322                 if ((cur_len + len) > PAGE_SIZE) {
2323                         pr_warn("Ran out of lu_gp_show_attr"
2324                                 "_members buffer\n");
2325                         break;
2326                 }
2327                 memcpy(page+len, buf, cur_len);
2328                 len += cur_len;
2329         }
2330         spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2331
2332         return len;
2333 }
2334
2335 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2336
2337 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2338                         tg_pt_gp_group);
2339
2340 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2341         &target_core_alua_tg_pt_gp_alua_access_state.attr,
2342         &target_core_alua_tg_pt_gp_alua_access_status.attr,
2343         &target_core_alua_tg_pt_gp_alua_access_type.attr,
2344         &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2345         &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2346         &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2347         &target_core_alua_tg_pt_gp_implict_trans_secs.attr,
2348         &target_core_alua_tg_pt_gp_preferred.attr,
2349         &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2350         &target_core_alua_tg_pt_gp_members.attr,
2351         NULL,
2352 };
2353
2354 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2355 {
2356         struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2357                         struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2358
2359         core_alua_free_tg_pt_gp(tg_pt_gp);
2360 }
2361
2362 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2363         .release                = target_core_alua_tg_pt_gp_release,
2364         .show_attribute         = target_core_alua_tg_pt_gp_attr_show,
2365         .store_attribute        = target_core_alua_tg_pt_gp_attr_store,
2366 };
2367
2368 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2369         .ct_item_ops            = &target_core_alua_tg_pt_gp_ops,
2370         .ct_attrs               = target_core_alua_tg_pt_gp_attrs,
2371         .ct_owner               = THIS_MODULE,
2372 };
2373
2374 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2375
2376 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2377
2378 static struct config_group *target_core_alua_create_tg_pt_gp(
2379         struct config_group *group,
2380         const char *name)
2381 {
2382         struct t10_alua *alua = container_of(group, struct t10_alua,
2383                                         alua_tg_pt_gps_group);
2384         struct t10_alua_tg_pt_gp *tg_pt_gp;
2385         struct config_group *alua_tg_pt_gp_cg = NULL;
2386         struct config_item *alua_tg_pt_gp_ci = NULL;
2387
2388         tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
2389         if (!tg_pt_gp)
2390                 return NULL;
2391
2392         alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2393         alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2394
2395         config_group_init_type_name(alua_tg_pt_gp_cg, name,
2396                         &target_core_alua_tg_pt_gp_cit);
2397
2398         pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
2399                 " Group: alua/tg_pt_gps/%s\n",
2400                 config_item_name(alua_tg_pt_gp_ci));
2401
2402         return alua_tg_pt_gp_cg;
2403 }
2404
2405 static void target_core_alua_drop_tg_pt_gp(
2406         struct config_group *group,
2407         struct config_item *item)
2408 {
2409         struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2410                         struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2411
2412         pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
2413                 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2414                 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2415         /*
2416          * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2417          * -> target_core_alua_tg_pt_gp_release().
2418          */
2419         config_item_put(item);
2420 }
2421
2422 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2423         .make_group             = &target_core_alua_create_tg_pt_gp,
2424         .drop_item              = &target_core_alua_drop_tg_pt_gp,
2425 };
2426
2427 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2428         .ct_group_ops           = &target_core_alua_tg_pt_gps_group_ops,
2429         .ct_owner               = THIS_MODULE,
2430 };
2431
2432 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2433
2434 /* Start functions for struct config_item_type target_core_alua_cit */
2435
2436 /*
2437  * target_core_alua_cit is a ConfigFS group that lives under
2438  * /sys/kernel/config/target/core/alua.  There are default groups
2439  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2440  * target_core_alua_cit in target_core_init_configfs() below.
2441  */
2442 static struct config_item_type target_core_alua_cit = {
2443         .ct_item_ops            = NULL,
2444         .ct_attrs               = NULL,
2445         .ct_owner               = THIS_MODULE,
2446 };
2447
2448 /* End functions for struct config_item_type target_core_alua_cit */
2449
2450 /* Start functions for struct config_item_type target_core_stat_cit */
2451
2452 static struct config_group *target_core_stat_mkdir(
2453         struct config_group *group,
2454         const char *name)
2455 {
2456         return ERR_PTR(-ENOSYS);
2457 }
2458
2459 static void target_core_stat_rmdir(
2460         struct config_group *group,
2461         struct config_item *item)
2462 {
2463         return;
2464 }
2465
2466 static struct configfs_group_operations target_core_stat_group_ops = {
2467         .make_group             = &target_core_stat_mkdir,
2468         .drop_item              = &target_core_stat_rmdir,
2469 };
2470
2471 static struct config_item_type target_core_stat_cit = {
2472         .ct_group_ops           = &target_core_stat_group_ops,
2473         .ct_owner               = THIS_MODULE,
2474 };
2475
2476 /* End functions for struct config_item_type target_core_stat_cit */
2477
2478 /* Start functions for struct config_item_type target_core_hba_cit */
2479
2480 static struct config_group *target_core_make_subdev(
2481         struct config_group *group,
2482         const char *name)
2483 {
2484         struct t10_alua_tg_pt_gp *tg_pt_gp;
2485         struct se_subsystem_api *t;
2486         struct config_item *hba_ci = &group->cg_item;
2487         struct se_hba *hba = item_to_hba(hba_ci);
2488         struct se_device *dev;
2489         struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2490         struct config_group *dev_stat_grp = NULL;
2491         int errno = -ENOMEM, ret;
2492
2493         ret = mutex_lock_interruptible(&hba->hba_access_mutex);
2494         if (ret)
2495                 return ERR_PTR(ret);
2496         /*
2497          * Locate the struct se_subsystem_api from parent's struct se_hba.
2498          */
2499         t = hba->transport;
2500
2501         dev = target_alloc_device(hba, name);
2502         if (!dev)
2503                 goto out_unlock;
2504
2505         dev_cg = &dev->dev_group;
2506
2507         dev_cg->default_groups = kmalloc(sizeof(struct config_group *) * 6,
2508                         GFP_KERNEL);
2509         if (!dev_cg->default_groups)
2510                 goto out_free_device;
2511
2512         config_group_init_type_name(dev_cg, name, &target_core_dev_cit);
2513         config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
2514                         &target_core_dev_attrib_cit);
2515         config_group_init_type_name(&dev->dev_pr_group, "pr",
2516                         &target_core_dev_pr_cit);
2517         config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
2518                         &target_core_dev_wwn_cit);
2519         config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
2520                         "alua", &target_core_alua_tg_pt_gps_cit);
2521         config_group_init_type_name(&dev->dev_stat_grps.stat_group,
2522                         "statistics", &target_core_stat_cit);
2523
2524         dev_cg->default_groups[0] = &dev->dev_attrib.da_group;
2525         dev_cg->default_groups[1] = &dev->dev_pr_group;
2526         dev_cg->default_groups[2] = &dev->t10_wwn.t10_wwn_group;
2527         dev_cg->default_groups[3] = &dev->t10_alua.alua_tg_pt_gps_group;
2528         dev_cg->default_groups[4] = &dev->dev_stat_grps.stat_group;
2529         dev_cg->default_groups[5] = NULL;
2530         /*
2531          * Add core/$HBA/$DEV/alua/default_tg_pt_gp
2532          */
2533         tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
2534         if (!tg_pt_gp)
2535                 goto out_free_dev_cg_default_groups;
2536         dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
2537
2538         tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2539         tg_pt_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2540                                 GFP_KERNEL);
2541         if (!tg_pt_gp_cg->default_groups) {
2542                 pr_err("Unable to allocate tg_pt_gp_cg->"
2543                                 "default_groups\n");
2544                 goto out_free_tg_pt_gp;
2545         }
2546
2547         config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2548                         "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2549         tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2550         tg_pt_gp_cg->default_groups[1] = NULL;
2551         /*
2552          * Add core/$HBA/$DEV/statistics/ default groups
2553          */
2554         dev_stat_grp = &dev->dev_stat_grps.stat_group;
2555         dev_stat_grp->default_groups = kmalloc(sizeof(struct config_group *) * 4,
2556                                 GFP_KERNEL);
2557         if (!dev_stat_grp->default_groups) {
2558                 pr_err("Unable to allocate dev_stat_grp->default_groups\n");
2559                 goto out_free_tg_pt_gp_cg_default_groups;
2560         }
2561         target_stat_setup_dev_default_groups(dev);
2562
2563         mutex_unlock(&hba->hba_access_mutex);
2564         return dev_cg;
2565
2566 out_free_tg_pt_gp_cg_default_groups:
2567         kfree(tg_pt_gp_cg->default_groups);
2568 out_free_tg_pt_gp:
2569         core_alua_free_tg_pt_gp(tg_pt_gp);
2570 out_free_dev_cg_default_groups:
2571         kfree(dev_cg->default_groups);
2572 out_free_device:
2573         target_free_device(dev);
2574 out_unlock:
2575         mutex_unlock(&hba->hba_access_mutex);
2576         return ERR_PTR(errno);
2577 }
2578
2579 static void target_core_drop_subdev(
2580         struct config_group *group,
2581         struct config_item *item)
2582 {
2583         struct config_group *dev_cg = to_config_group(item);
2584         struct se_device *dev =
2585                 container_of(dev_cg, struct se_device, dev_group);
2586         struct se_hba *hba;
2587         struct config_item *df_item;
2588         struct config_group *tg_pt_gp_cg, *dev_stat_grp;
2589         int i;
2590
2591         hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
2592
2593         mutex_lock(&hba->hba_access_mutex);
2594
2595         dev_stat_grp = &dev->dev_stat_grps.stat_group;
2596         for (i = 0; dev_stat_grp->default_groups[i]; i++) {
2597                 df_item = &dev_stat_grp->default_groups[i]->cg_item;
2598                 dev_stat_grp->default_groups[i] = NULL;
2599                 config_item_put(df_item);
2600         }
2601         kfree(dev_stat_grp->default_groups);
2602
2603         tg_pt_gp_cg = &dev->t10_alua.alua_tg_pt_gps_group;
2604         for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2605                 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2606                 tg_pt_gp_cg->default_groups[i] = NULL;
2607                 config_item_put(df_item);
2608         }
2609         kfree(tg_pt_gp_cg->default_groups);
2610         /*
2611          * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2612          * directly from target_core_alua_tg_pt_gp_release().
2613          */
2614         dev->t10_alua.default_tg_pt_gp = NULL;
2615
2616         for (i = 0; dev_cg->default_groups[i]; i++) {
2617                 df_item = &dev_cg->default_groups[i]->cg_item;
2618                 dev_cg->default_groups[i] = NULL;
2619                 config_item_put(df_item);
2620         }
2621         /*
2622          * se_dev is released from target_core_dev_item_ops->release()
2623          */
2624         config_item_put(item);
2625         mutex_unlock(&hba->hba_access_mutex);
2626 }
2627
2628 static struct configfs_group_operations target_core_hba_group_ops = {
2629         .make_group             = target_core_make_subdev,
2630         .drop_item              = target_core_drop_subdev,
2631 };
2632
2633 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2634 #define SE_HBA_ATTR(_name, _mode)                               \
2635 static struct target_core_hba_attribute                         \
2636                 target_core_hba_##_name =                       \
2637                 __CONFIGFS_EATTR(_name, _mode,                  \
2638                 target_core_hba_show_attr_##_name,              \
2639                 target_core_hba_store_attr_##_name);
2640
2641 #define SE_HBA_ATTR_RO(_name)                                   \
2642 static struct target_core_hba_attribute                         \
2643                 target_core_hba_##_name =                       \
2644                 __CONFIGFS_EATTR_RO(_name,                      \
2645                 target_core_hba_show_attr_##_name);
2646
2647 static ssize_t target_core_hba_show_attr_hba_info(
2648         struct se_hba *hba,
2649         char *page)
2650 {
2651         return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2652                         hba->hba_id, hba->transport->name,
2653                         TARGET_CORE_CONFIGFS_VERSION);
2654 }
2655
2656 SE_HBA_ATTR_RO(hba_info);
2657
2658 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2659                                 char *page)
2660 {
2661         int hba_mode = 0;
2662
2663         if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2664                 hba_mode = 1;
2665
2666         return sprintf(page, "%d\n", hba_mode);
2667 }
2668
2669 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2670                                 const char *page, size_t count)
2671 {
2672         struct se_subsystem_api *transport = hba->transport;
2673         unsigned long mode_flag;
2674         int ret;
2675
2676         if (transport->pmode_enable_hba == NULL)
2677                 return -EINVAL;
2678
2679         ret = strict_strtoul(page, 0, &mode_flag);
2680         if (ret < 0) {
2681                 pr_err("Unable to extract hba mode flag: %d\n", ret);
2682                 return -EINVAL;
2683         }
2684
2685         if (hba->dev_count) {
2686                 pr_err("Unable to set hba_mode with active devices\n");
2687                 return -EINVAL;
2688         }
2689
2690         ret = transport->pmode_enable_hba(hba, mode_flag);
2691         if (ret < 0)
2692                 return -EINVAL;
2693         if (ret > 0)
2694                 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2695         else if (ret == 0)
2696                 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2697
2698         return count;
2699 }
2700
2701 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2702
2703 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2704
2705 static void target_core_hba_release(struct config_item *item)
2706 {
2707         struct se_hba *hba = container_of(to_config_group(item),
2708                                 struct se_hba, hba_group);
2709         core_delete_hba(hba);
2710 }
2711
2712 static struct configfs_attribute *target_core_hba_attrs[] = {
2713         &target_core_hba_hba_info.attr,
2714         &target_core_hba_hba_mode.attr,
2715         NULL,
2716 };
2717
2718 static struct configfs_item_operations target_core_hba_item_ops = {
2719         .release                = target_core_hba_release,
2720         .show_attribute         = target_core_hba_attr_show,
2721         .store_attribute        = target_core_hba_attr_store,
2722 };
2723
2724 static struct config_item_type target_core_hba_cit = {
2725         .ct_item_ops            = &target_core_hba_item_ops,
2726         .ct_group_ops           = &target_core_hba_group_ops,
2727         .ct_attrs               = target_core_hba_attrs,
2728         .ct_owner               = THIS_MODULE,
2729 };
2730
2731 static struct config_group *target_core_call_addhbatotarget(
2732         struct config_group *group,
2733         const char *name)
2734 {
2735         char *se_plugin_str, *str, *str2;
2736         struct se_hba *hba;
2737         char buf[TARGET_CORE_NAME_MAX_LEN];
2738         unsigned long plugin_dep_id = 0;
2739         int ret;
2740
2741         memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
2742         if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
2743                 pr_err("Passed *name strlen(): %d exceeds"
2744                         " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
2745                         TARGET_CORE_NAME_MAX_LEN);
2746                 return ERR_PTR(-ENAMETOOLONG);
2747         }
2748         snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
2749
2750         str = strstr(buf, "_");
2751         if (!str) {
2752                 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
2753                 return ERR_PTR(-EINVAL);
2754         }
2755         se_plugin_str = buf;
2756         /*
2757          * Special case for subsystem plugins that have "_" in their names.
2758          * Namely rd_direct and rd_mcp..
2759          */
2760         str2 = strstr(str+1, "_");
2761         if (str2) {
2762                 *str2 = '\0'; /* Terminate for *se_plugin_str */
2763                 str2++; /* Skip to start of plugin dependent ID */
2764                 str = str2;
2765         } else {
2766                 *str = '\0'; /* Terminate for *se_plugin_str */
2767                 str++; /* Skip to start of plugin dependent ID */
2768         }
2769
2770         ret = strict_strtoul(str, 0, &plugin_dep_id);
2771         if (ret < 0) {
2772                 pr_err("strict_strtoul() returned %d for"
2773                                 " plugin_dep_id\n", ret);
2774                 return ERR_PTR(-EINVAL);
2775         }
2776         /*
2777          * Load up TCM subsystem plugins if they have not already been loaded.
2778          */
2779         transport_subsystem_check_init();
2780
2781         hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
2782         if (IS_ERR(hba))
2783                 return ERR_CAST(hba);
2784
2785         config_group_init_type_name(&hba->hba_group, name,
2786                         &target_core_hba_cit);
2787
2788         return &hba->hba_group;
2789 }
2790
2791 static void target_core_call_delhbafromtarget(
2792         struct config_group *group,
2793         struct config_item *item)
2794 {
2795         /*
2796          * core_delete_hba() is called from target_core_hba_item_ops->release()
2797          * -> target_core_hba_release()
2798          */
2799         config_item_put(item);
2800 }
2801
2802 static struct configfs_group_operations target_core_group_ops = {
2803         .make_group     = target_core_call_addhbatotarget,
2804         .drop_item      = target_core_call_delhbafromtarget,
2805 };
2806
2807 static struct config_item_type target_core_cit = {
2808         .ct_item_ops    = NULL,
2809         .ct_group_ops   = &target_core_group_ops,
2810         .ct_attrs       = NULL,
2811         .ct_owner       = THIS_MODULE,
2812 };
2813
2814 /* Stop functions for struct config_item_type target_core_hba_cit */
2815
2816 static int __init target_core_init_configfs(void)
2817 {
2818         struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
2819         struct config_group *lu_gp_cg = NULL;
2820         struct configfs_subsystem *subsys;
2821         struct t10_alua_lu_gp *lu_gp;
2822         int ret;
2823
2824         pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
2825                 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
2826                 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
2827
2828         subsys = target_core_subsystem[0];
2829         config_group_init(&subsys->su_group);
2830         mutex_init(&subsys->su_mutex);
2831
2832         ret = init_se_kmem_caches();
2833         if (ret < 0)
2834                 return ret;
2835         /*
2836          * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
2837          * and ALUA Logical Unit Group and Target Port Group infrastructure.
2838          */
2839         target_cg = &subsys->su_group;
2840         target_cg->default_groups = kmalloc(sizeof(struct config_group) * 2,
2841                                 GFP_KERNEL);
2842         if (!target_cg->default_groups) {
2843                 pr_err("Unable to allocate target_cg->default_groups\n");
2844                 ret = -ENOMEM;
2845                 goto out_global;
2846         }
2847
2848         config_group_init_type_name(&target_core_hbagroup,
2849                         "core", &target_core_cit);
2850         target_cg->default_groups[0] = &target_core_hbagroup;
2851         target_cg->default_groups[1] = NULL;
2852         /*
2853          * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
2854          */
2855         hba_cg = &target_core_hbagroup;
2856         hba_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2857                                 GFP_KERNEL);
2858         if (!hba_cg->default_groups) {
2859                 pr_err("Unable to allocate hba_cg->default_groups\n");
2860                 ret = -ENOMEM;
2861                 goto out_global;
2862         }
2863         config_group_init_type_name(&alua_group,
2864                         "alua", &target_core_alua_cit);
2865         hba_cg->default_groups[0] = &alua_group;
2866         hba_cg->default_groups[1] = NULL;
2867         /*
2868          * Add ALUA Logical Unit Group and Target Port Group ConfigFS
2869          * groups under /sys/kernel/config/target/core/alua/
2870          */
2871         alua_cg = &alua_group;
2872         alua_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2873                         GFP_KERNEL);
2874         if (!alua_cg->default_groups) {
2875                 pr_err("Unable to allocate alua_cg->default_groups\n");
2876                 ret = -ENOMEM;
2877                 goto out_global;
2878         }
2879
2880         config_group_init_type_name(&alua_lu_gps_group,
2881                         "lu_gps", &target_core_alua_lu_gps_cit);
2882         alua_cg->default_groups[0] = &alua_lu_gps_group;
2883         alua_cg->default_groups[1] = NULL;
2884         /*
2885          * Add core/alua/lu_gps/default_lu_gp
2886          */
2887         lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
2888         if (IS_ERR(lu_gp)) {
2889                 ret = -ENOMEM;
2890                 goto out_global;
2891         }
2892
2893         lu_gp_cg = &alua_lu_gps_group;
2894         lu_gp_cg->default_groups = kmalloc(sizeof(struct config_group *) * 2,
2895                         GFP_KERNEL);
2896         if (!lu_gp_cg->default_groups) {
2897                 pr_err("Unable to allocate lu_gp_cg->default_groups\n");
2898                 ret = -ENOMEM;
2899                 goto out_global;
2900         }
2901
2902         config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
2903                                 &target_core_alua_lu_gp_cit);
2904         lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
2905         lu_gp_cg->default_groups[1] = NULL;
2906         default_lu_gp = lu_gp;
2907         /*
2908          * Register the target_core_mod subsystem with configfs.
2909          */
2910         ret = configfs_register_subsystem(subsys);
2911         if (ret < 0) {
2912                 pr_err("Error %d while registering subsystem %s\n",
2913                         ret, subsys->su_group.cg_item.ci_namebuf);
2914                 goto out_global;
2915         }
2916         pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
2917                 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
2918                 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
2919         /*
2920          * Register built-in RAMDISK subsystem logic for virtual LUN 0
2921          */
2922         ret = rd_module_init();
2923         if (ret < 0)
2924                 goto out;
2925
2926         ret = core_dev_setup_virtual_lun0();
2927         if (ret < 0)
2928                 goto out;
2929
2930         return 0;
2931
2932 out:
2933         configfs_unregister_subsystem(subsys);
2934         core_dev_release_virtual_lun0();
2935         rd_module_exit();
2936 out_global:
2937         if (default_lu_gp) {
2938                 core_alua_free_lu_gp(default_lu_gp);
2939                 default_lu_gp = NULL;
2940         }
2941         if (lu_gp_cg)
2942                 kfree(lu_gp_cg->default_groups);
2943         if (alua_cg)
2944                 kfree(alua_cg->default_groups);
2945         if (hba_cg)
2946                 kfree(hba_cg->default_groups);
2947         kfree(target_cg->default_groups);
2948         release_se_kmem_caches();
2949         return ret;
2950 }
2951
2952 static void __exit target_core_exit_configfs(void)
2953 {
2954         struct configfs_subsystem *subsys;
2955         struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
2956         struct config_item *item;
2957         int i;
2958
2959         subsys = target_core_subsystem[0];
2960
2961         lu_gp_cg = &alua_lu_gps_group;
2962         for (i = 0; lu_gp_cg->default_groups[i]; i++) {
2963                 item = &lu_gp_cg->default_groups[i]->cg_item;
2964                 lu_gp_cg->default_groups[i] = NULL;
2965                 config_item_put(item);
2966         }
2967         kfree(lu_gp_cg->default_groups);
2968         lu_gp_cg->default_groups = NULL;
2969
2970         alua_cg = &alua_group;
2971         for (i = 0; alua_cg->default_groups[i]; i++) {
2972                 item = &alua_cg->default_groups[i]->cg_item;
2973                 alua_cg->default_groups[i] = NULL;
2974                 config_item_put(item);
2975         }
2976         kfree(alua_cg->default_groups);
2977         alua_cg->default_groups = NULL;
2978
2979         hba_cg = &target_core_hbagroup;
2980         for (i = 0; hba_cg->default_groups[i]; i++) {
2981                 item = &hba_cg->default_groups[i]->cg_item;
2982                 hba_cg->default_groups[i] = NULL;
2983                 config_item_put(item);
2984         }
2985         kfree(hba_cg->default_groups);
2986         hba_cg->default_groups = NULL;
2987         /*
2988          * We expect subsys->su_group.default_groups to be released
2989          * by configfs subsystem provider logic..
2990          */
2991         configfs_unregister_subsystem(subsys);
2992         kfree(subsys->su_group.default_groups);
2993
2994         core_alua_free_lu_gp(default_lu_gp);
2995         default_lu_gp = NULL;
2996
2997         pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
2998                         " Infrastructure\n");
2999
3000         core_dev_release_virtual_lun0();
3001         rd_module_exit();
3002         release_se_kmem_caches();
3003 }
3004
3005 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3006 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3007 MODULE_LICENSE("GPL");
3008
3009 module_init(target_core_init_configfs);
3010 module_exit(target_core_exit_configfs);