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