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