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