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