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