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