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