a38fec4accebc03f9e863246cda494e45c11ae62
[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 (IS_ERR(dev))
1831                 return PTR_ERR(dev);
1832         else if (!dev)
1833                 return -EINVAL;
1834
1835         se_dev->se_dev_ptr = dev;
1836         printk(KERN_INFO "Target_Core_ConfigFS: Registered se_dev->se_dev_ptr:"
1837                 " %p\n", se_dev->se_dev_ptr);
1838
1839         return count;
1840 }
1841
1842 static struct target_core_configfs_attribute target_core_attr_dev_enable = {
1843         .attr   = { .ca_owner = THIS_MODULE,
1844                     .ca_name = "enable",
1845                     .ca_mode = S_IWUSR },
1846         .show   = NULL,
1847         .store  = target_core_store_dev_enable,
1848 };
1849
1850 static ssize_t target_core_show_alua_lu_gp(void *p, char *page)
1851 {
1852         struct se_device *dev;
1853         struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1854         struct config_item *lu_ci;
1855         struct t10_alua_lu_gp *lu_gp;
1856         struct t10_alua_lu_gp_member *lu_gp_mem;
1857         ssize_t len = 0;
1858
1859         dev = su_dev->se_dev_ptr;
1860         if (!(dev))
1861                 return -ENODEV;
1862
1863         if (T10_ALUA(su_dev)->alua_type != SPC3_ALUA_EMULATED)
1864                 return len;
1865
1866         lu_gp_mem = dev->dev_alua_lu_gp_mem;
1867         if (!(lu_gp_mem)) {
1868                 printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1869                                 " pointer\n");
1870                 return -EINVAL;
1871         }
1872
1873         spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1874         lu_gp = lu_gp_mem->lu_gp;
1875         if ((lu_gp)) {
1876                 lu_ci = &lu_gp->lu_gp_group.cg_item;
1877                 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
1878                         config_item_name(lu_ci), lu_gp->lu_gp_id);
1879         }
1880         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1881
1882         return len;
1883 }
1884
1885 static ssize_t target_core_store_alua_lu_gp(
1886         void *p,
1887         const char *page,
1888         size_t count)
1889 {
1890         struct se_device *dev;
1891         struct se_subsystem_dev *su_dev = (struct se_subsystem_dev *)p;
1892         struct se_hba *hba = su_dev->se_dev_hba;
1893         struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
1894         struct t10_alua_lu_gp_member *lu_gp_mem;
1895         unsigned char buf[LU_GROUP_NAME_BUF];
1896         int move = 0;
1897
1898         dev = su_dev->se_dev_ptr;
1899         if (!(dev))
1900                 return -ENODEV;
1901
1902         if (T10_ALUA(su_dev)->alua_type != SPC3_ALUA_EMULATED) {
1903                 printk(KERN_WARNING "SPC3_ALUA_EMULATED not enabled for %s/%s\n",
1904                         config_item_name(&hba->hba_group.cg_item),
1905                         config_item_name(&su_dev->se_dev_group.cg_item));
1906                 return -EINVAL;
1907         }
1908         if (count > LU_GROUP_NAME_BUF) {
1909                 printk(KERN_ERR "ALUA LU Group Alias too large!\n");
1910                 return -EINVAL;
1911         }
1912         memset(buf, 0, LU_GROUP_NAME_BUF);
1913         memcpy(buf, page, count);
1914         /*
1915          * Any ALUA logical unit alias besides "NULL" means we will be
1916          * making a new group association.
1917          */
1918         if (strcmp(strstrip(buf), "NULL")) {
1919                 /*
1920                  * core_alua_get_lu_gp_by_name() will increment reference to
1921                  * struct t10_alua_lu_gp.  This reference is released with
1922                  * core_alua_get_lu_gp_by_name below().
1923                  */
1924                 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
1925                 if (!(lu_gp_new))
1926                         return -ENODEV;
1927         }
1928         lu_gp_mem = dev->dev_alua_lu_gp_mem;
1929         if (!(lu_gp_mem)) {
1930                 if (lu_gp_new)
1931                         core_alua_put_lu_gp_from_name(lu_gp_new);
1932                 printk(KERN_ERR "NULL struct se_device->dev_alua_lu_gp_mem"
1933                                 " pointer\n");
1934                 return -EINVAL;
1935         }
1936
1937         spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1938         lu_gp = lu_gp_mem->lu_gp;
1939         if ((lu_gp)) {
1940                 /*
1941                  * Clearing an existing lu_gp association, and replacing
1942                  * with NULL
1943                  */
1944                 if (!(lu_gp_new)) {
1945                         printk(KERN_INFO "Target_Core_ConfigFS: Releasing %s/%s"
1946                                 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
1947                                 " %hu\n",
1948                                 config_item_name(&hba->hba_group.cg_item),
1949                                 config_item_name(&su_dev->se_dev_group.cg_item),
1950                                 config_item_name(&lu_gp->lu_gp_group.cg_item),
1951                                 lu_gp->lu_gp_id);
1952
1953                         __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1954                         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1955
1956                         return count;
1957                 }
1958                 /*
1959                  * Removing existing association of lu_gp_mem with lu_gp
1960                  */
1961                 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
1962                 move = 1;
1963         }
1964         /*
1965          * Associate lu_gp_mem with lu_gp_new.
1966          */
1967         __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
1968         spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1969
1970         printk(KERN_INFO "Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
1971                 " core/alua/lu_gps/%s, ID: %hu\n",
1972                 (move) ? "Moving" : "Adding",
1973                 config_item_name(&hba->hba_group.cg_item),
1974                 config_item_name(&su_dev->se_dev_group.cg_item),
1975                 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
1976                 lu_gp_new->lu_gp_id);
1977
1978         core_alua_put_lu_gp_from_name(lu_gp_new);
1979         return count;
1980 }
1981
1982 static struct target_core_configfs_attribute target_core_attr_dev_alua_lu_gp = {
1983         .attr   = { .ca_owner = THIS_MODULE,
1984                     .ca_name = "alua_lu_gp",
1985                     .ca_mode = S_IRUGO | S_IWUSR },
1986         .show   = target_core_show_alua_lu_gp,
1987         .store  = target_core_store_alua_lu_gp,
1988 };
1989
1990 static struct configfs_attribute *lio_core_dev_attrs[] = {
1991         &target_core_attr_dev_info.attr,
1992         &target_core_attr_dev_control.attr,
1993         &target_core_attr_dev_alias.attr,
1994         &target_core_attr_dev_udev_path.attr,
1995         &target_core_attr_dev_enable.attr,
1996         &target_core_attr_dev_alua_lu_gp.attr,
1997         NULL,
1998 };
1999
2000 static void target_core_dev_release(struct config_item *item)
2001 {
2002         struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2003                                 struct se_subsystem_dev, se_dev_group);
2004         struct se_hba *hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2005         struct se_subsystem_api *t = hba->transport;
2006         struct config_group *dev_cg = &se_dev->se_dev_group;
2007
2008         kfree(dev_cg->default_groups);
2009         /*
2010          * This pointer will set when the storage is enabled with:
2011          *`echo 1 > $CONFIGFS/core/$HBA/$DEV/dev_enable`
2012          */
2013         if (se_dev->se_dev_ptr) {
2014                 printk(KERN_INFO "Target_Core_ConfigFS: Calling se_free_"
2015                         "virtual_device() for se_dev_ptr: %p\n",
2016                         se_dev->se_dev_ptr);
2017
2018                 se_free_virtual_device(se_dev->se_dev_ptr, hba);
2019         } else {
2020                 /*
2021                  * Release struct se_subsystem_dev->se_dev_su_ptr..
2022                  */
2023                 printk(KERN_INFO "Target_Core_ConfigFS: Calling t->free_"
2024                         "device() for se_dev_su_ptr: %p\n",
2025                         se_dev->se_dev_su_ptr);
2026
2027                 t->free_device(se_dev->se_dev_su_ptr);
2028         }
2029
2030         printk(KERN_INFO "Target_Core_ConfigFS: Deallocating se_subsystem"
2031                         "_dev_t: %p\n", se_dev);
2032         kfree(se_dev);
2033 }
2034
2035 static ssize_t target_core_dev_show(struct config_item *item,
2036                                      struct configfs_attribute *attr,
2037                                      char *page)
2038 {
2039         struct se_subsystem_dev *se_dev = container_of(
2040                         to_config_group(item), struct se_subsystem_dev,
2041                         se_dev_group);
2042         struct target_core_configfs_attribute *tc_attr = container_of(
2043                         attr, struct target_core_configfs_attribute, attr);
2044
2045         if (!(tc_attr->show))
2046                 return -EINVAL;
2047
2048         return tc_attr->show((void *)se_dev, page);
2049 }
2050
2051 static ssize_t target_core_dev_store(struct config_item *item,
2052                                       struct configfs_attribute *attr,
2053                                       const char *page, size_t count)
2054 {
2055         struct se_subsystem_dev *se_dev = container_of(
2056                         to_config_group(item), struct se_subsystem_dev,
2057                         se_dev_group);
2058         struct target_core_configfs_attribute *tc_attr = container_of(
2059                         attr, struct target_core_configfs_attribute, attr);
2060
2061         if (!(tc_attr->store))
2062                 return -EINVAL;
2063
2064         return tc_attr->store((void *)se_dev, page, count);
2065 }
2066
2067 static struct configfs_item_operations target_core_dev_item_ops = {
2068         .release                = target_core_dev_release,
2069         .show_attribute         = target_core_dev_show,
2070         .store_attribute        = target_core_dev_store,
2071 };
2072
2073 static struct config_item_type target_core_dev_cit = {
2074         .ct_item_ops            = &target_core_dev_item_ops,
2075         .ct_attrs               = lio_core_dev_attrs,
2076         .ct_owner               = THIS_MODULE,
2077 };
2078
2079 /* End functions for struct config_item_type target_core_dev_cit */
2080
2081 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2082
2083 CONFIGFS_EATTR_STRUCT(target_core_alua_lu_gp, t10_alua_lu_gp);
2084 #define SE_DEV_ALUA_LU_ATTR(_name, _mode)                               \
2085 static struct target_core_alua_lu_gp_attribute                          \
2086                         target_core_alua_lu_gp_##_name =                \
2087         __CONFIGFS_EATTR(_name, _mode,                                  \
2088         target_core_alua_lu_gp_show_attr_##_name,                       \
2089         target_core_alua_lu_gp_store_attr_##_name);
2090
2091 #define SE_DEV_ALUA_LU_ATTR_RO(_name)                                   \
2092 static struct target_core_alua_lu_gp_attribute                          \
2093                         target_core_alua_lu_gp_##_name =                \
2094         __CONFIGFS_EATTR_RO(_name,                                      \
2095         target_core_alua_lu_gp_show_attr_##_name);
2096
2097 /*
2098  * lu_gp_id
2099  */
2100 static ssize_t target_core_alua_lu_gp_show_attr_lu_gp_id(
2101         struct t10_alua_lu_gp *lu_gp,
2102         char *page)
2103 {
2104         if (!(lu_gp->lu_gp_valid_id))
2105                 return 0;
2106
2107         return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2108 }
2109
2110 static ssize_t target_core_alua_lu_gp_store_attr_lu_gp_id(
2111         struct t10_alua_lu_gp *lu_gp,
2112         const char *page,
2113         size_t count)
2114 {
2115         struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2116         unsigned long lu_gp_id;
2117         int ret;
2118
2119         ret = strict_strtoul(page, 0, &lu_gp_id);
2120         if (ret < 0) {
2121                 printk(KERN_ERR "strict_strtoul() returned %d for"
2122                         " lu_gp_id\n", ret);
2123                 return -EINVAL;
2124         }
2125         if (lu_gp_id > 0x0000ffff) {
2126                 printk(KERN_ERR "ALUA lu_gp_id: %lu exceeds maximum:"
2127                         " 0x0000ffff\n", lu_gp_id);
2128                 return -EINVAL;
2129         }
2130
2131         ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2132         if (ret < 0)
2133                 return -EINVAL;
2134
2135         printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Logical Unit"
2136                 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2137                 config_item_name(&alua_lu_gp_cg->cg_item),
2138                 lu_gp->lu_gp_id);
2139
2140         return count;
2141 }
2142
2143 SE_DEV_ALUA_LU_ATTR(lu_gp_id, S_IRUGO | S_IWUSR);
2144
2145 /*
2146  * members
2147  */
2148 static ssize_t target_core_alua_lu_gp_show_attr_members(
2149         struct t10_alua_lu_gp *lu_gp,
2150         char *page)
2151 {
2152         struct se_device *dev;
2153         struct se_hba *hba;
2154         struct se_subsystem_dev *su_dev;
2155         struct t10_alua_lu_gp_member *lu_gp_mem;
2156         ssize_t len = 0, cur_len;
2157         unsigned char buf[LU_GROUP_NAME_BUF];
2158
2159         memset(buf, 0, LU_GROUP_NAME_BUF);
2160
2161         spin_lock(&lu_gp->lu_gp_lock);
2162         list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2163                 dev = lu_gp_mem->lu_gp_mem_dev;
2164                 su_dev = dev->se_sub_dev;
2165                 hba = su_dev->se_dev_hba;
2166
2167                 cur_len = snprintf(buf, LU_GROUP_NAME_BUF, "%s/%s\n",
2168                         config_item_name(&hba->hba_group.cg_item),
2169                         config_item_name(&su_dev->se_dev_group.cg_item));
2170                 cur_len++; /* Extra byte for NULL terminator */
2171
2172                 if ((cur_len + len) > PAGE_SIZE) {
2173                         printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2174                                 "_members buffer\n");
2175                         break;
2176                 }
2177                 memcpy(page+len, buf, cur_len);
2178                 len += cur_len;
2179         }
2180         spin_unlock(&lu_gp->lu_gp_lock);
2181
2182         return len;
2183 }
2184
2185 SE_DEV_ALUA_LU_ATTR_RO(members);
2186
2187 CONFIGFS_EATTR_OPS(target_core_alua_lu_gp, t10_alua_lu_gp, lu_gp_group);
2188
2189 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2190         &target_core_alua_lu_gp_lu_gp_id.attr,
2191         &target_core_alua_lu_gp_members.attr,
2192         NULL,
2193 };
2194
2195 static void target_core_alua_lu_gp_release(struct config_item *item)
2196 {
2197         struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2198                         struct t10_alua_lu_gp, lu_gp_group);
2199
2200         core_alua_free_lu_gp(lu_gp);
2201 }
2202
2203 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2204         .release                = target_core_alua_lu_gp_release,
2205         .show_attribute         = target_core_alua_lu_gp_attr_show,
2206         .store_attribute        = target_core_alua_lu_gp_attr_store,
2207 };
2208
2209 static struct config_item_type target_core_alua_lu_gp_cit = {
2210         .ct_item_ops            = &target_core_alua_lu_gp_ops,
2211         .ct_attrs               = target_core_alua_lu_gp_attrs,
2212         .ct_owner               = THIS_MODULE,
2213 };
2214
2215 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2216
2217 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2218
2219 static struct config_group *target_core_alua_create_lu_gp(
2220         struct config_group *group,
2221         const char *name)
2222 {
2223         struct t10_alua_lu_gp *lu_gp;
2224         struct config_group *alua_lu_gp_cg = NULL;
2225         struct config_item *alua_lu_gp_ci = NULL;
2226
2227         lu_gp = core_alua_allocate_lu_gp(name, 0);
2228         if (IS_ERR(lu_gp))
2229                 return NULL;
2230
2231         alua_lu_gp_cg = &lu_gp->lu_gp_group;
2232         alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2233
2234         config_group_init_type_name(alua_lu_gp_cg, name,
2235                         &target_core_alua_lu_gp_cit);
2236
2237         printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2238                 " Group: core/alua/lu_gps/%s\n",
2239                 config_item_name(alua_lu_gp_ci));
2240
2241         return alua_lu_gp_cg;
2242
2243 }
2244
2245 static void target_core_alua_drop_lu_gp(
2246         struct config_group *group,
2247         struct config_item *item)
2248 {
2249         struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2250                         struct t10_alua_lu_gp, lu_gp_group);
2251
2252         printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2253                 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2254                 config_item_name(item), lu_gp->lu_gp_id);
2255         /*
2256          * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2257          * -> target_core_alua_lu_gp_release()
2258          */
2259         config_item_put(item);
2260 }
2261
2262 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2263         .make_group             = &target_core_alua_create_lu_gp,
2264         .drop_item              = &target_core_alua_drop_lu_gp,
2265 };
2266
2267 static struct config_item_type target_core_alua_lu_gps_cit = {
2268         .ct_item_ops            = NULL,
2269         .ct_group_ops           = &target_core_alua_lu_gps_group_ops,
2270         .ct_owner               = THIS_MODULE,
2271 };
2272
2273 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2274
2275 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2276
2277 CONFIGFS_EATTR_STRUCT(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp);
2278 #define SE_DEV_ALUA_TG_PT_ATTR(_name, _mode)                            \
2279 static struct target_core_alua_tg_pt_gp_attribute                       \
2280                         target_core_alua_tg_pt_gp_##_name =             \
2281         __CONFIGFS_EATTR(_name, _mode,                                  \
2282         target_core_alua_tg_pt_gp_show_attr_##_name,                    \
2283         target_core_alua_tg_pt_gp_store_attr_##_name);
2284
2285 #define SE_DEV_ALUA_TG_PT_ATTR_RO(_name)                                \
2286 static struct target_core_alua_tg_pt_gp_attribute                       \
2287                         target_core_alua_tg_pt_gp_##_name =             \
2288         __CONFIGFS_EATTR_RO(_name,                                      \
2289         target_core_alua_tg_pt_gp_show_attr_##_name);
2290
2291 /*
2292  * alua_access_state
2293  */
2294 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_state(
2295         struct t10_alua_tg_pt_gp *tg_pt_gp,
2296         char *page)
2297 {
2298         return sprintf(page, "%d\n",
2299                 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state));
2300 }
2301
2302 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_state(
2303         struct t10_alua_tg_pt_gp *tg_pt_gp,
2304         const char *page,
2305         size_t count)
2306 {
2307         struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
2308         unsigned long tmp;
2309         int new_state, ret;
2310
2311         if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2312                 printk(KERN_ERR "Unable to do implict ALUA on non valid"
2313                         " tg_pt_gp ID: %hu\n", tg_pt_gp->tg_pt_gp_valid_id);
2314                 return -EINVAL;
2315         }
2316
2317         ret = strict_strtoul(page, 0, &tmp);
2318         if (ret < 0) {
2319                 printk("Unable to extract new ALUA access state from"
2320                                 " %s\n", page);
2321                 return -EINVAL;
2322         }
2323         new_state = (int)tmp;
2324
2325         if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)) {
2326                 printk(KERN_ERR "Unable to process implict configfs ALUA"
2327                         " transition while TPGS_IMPLICT_ALUA is diabled\n");
2328                 return -EINVAL;
2329         }
2330
2331         ret = core_alua_do_port_transition(tg_pt_gp, su_dev->se_dev_ptr,
2332                                         NULL, NULL, new_state, 0);
2333         return (!ret) ? count : -EINVAL;
2334 }
2335
2336 SE_DEV_ALUA_TG_PT_ATTR(alua_access_state, S_IRUGO | S_IWUSR);
2337
2338 /*
2339  * alua_access_status
2340  */
2341 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_status(
2342         struct t10_alua_tg_pt_gp *tg_pt_gp,
2343         char *page)
2344 {
2345         return sprintf(page, "%s\n",
2346                 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2347 }
2348
2349 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_status(
2350         struct t10_alua_tg_pt_gp *tg_pt_gp,
2351         const char *page,
2352         size_t count)
2353 {
2354         unsigned long tmp;
2355         int new_status, ret;
2356
2357         if (!(tg_pt_gp->tg_pt_gp_valid_id)) {
2358                 printk(KERN_ERR "Unable to do set ALUA access status on non"
2359                         " valid tg_pt_gp ID: %hu\n",
2360                         tg_pt_gp->tg_pt_gp_valid_id);
2361                 return -EINVAL;
2362         }
2363
2364         ret = strict_strtoul(page, 0, &tmp);
2365         if (ret < 0) {
2366                 printk(KERN_ERR "Unable to extract new ALUA access status"
2367                                 " from %s\n", page);
2368                 return -EINVAL;
2369         }
2370         new_status = (int)tmp;
2371
2372         if ((new_status != ALUA_STATUS_NONE) &&
2373             (new_status != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
2374             (new_status != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
2375                 printk(KERN_ERR "Illegal ALUA access status: 0x%02x\n",
2376                                 new_status);
2377                 return -EINVAL;
2378         }
2379
2380         tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2381         return count;
2382 }
2383
2384 SE_DEV_ALUA_TG_PT_ATTR(alua_access_status, S_IRUGO | S_IWUSR);
2385
2386 /*
2387  * alua_access_type
2388  */
2389 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_access_type(
2390         struct t10_alua_tg_pt_gp *tg_pt_gp,
2391         char *page)
2392 {
2393         return core_alua_show_access_type(tg_pt_gp, page);
2394 }
2395
2396 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_access_type(
2397         struct t10_alua_tg_pt_gp *tg_pt_gp,
2398         const char *page,
2399         size_t count)
2400 {
2401         return core_alua_store_access_type(tg_pt_gp, page, count);
2402 }
2403
2404 SE_DEV_ALUA_TG_PT_ATTR(alua_access_type, S_IRUGO | S_IWUSR);
2405
2406 /*
2407  * alua_write_metadata
2408  */
2409 static ssize_t target_core_alua_tg_pt_gp_show_attr_alua_write_metadata(
2410         struct t10_alua_tg_pt_gp *tg_pt_gp,
2411         char *page)
2412 {
2413         return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_write_metadata);
2414 }
2415
2416 static ssize_t target_core_alua_tg_pt_gp_store_attr_alua_write_metadata(
2417         struct t10_alua_tg_pt_gp *tg_pt_gp,
2418         const char *page,
2419         size_t count)
2420 {
2421         unsigned long tmp;
2422         int ret;
2423
2424         ret = strict_strtoul(page, 0, &tmp);
2425         if (ret < 0) {
2426                 printk(KERN_ERR "Unable to extract alua_write_metadata\n");
2427                 return -EINVAL;
2428         }
2429
2430         if ((tmp != 0) && (tmp != 1)) {
2431                 printk(KERN_ERR "Illegal value for alua_write_metadata:"
2432                         " %lu\n", tmp);
2433                 return -EINVAL;
2434         }
2435         tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
2436
2437         return count;
2438 }
2439
2440 SE_DEV_ALUA_TG_PT_ATTR(alua_write_metadata, S_IRUGO | S_IWUSR);
2441
2442
2443
2444 /*
2445  * nonop_delay_msecs
2446  */
2447 static ssize_t target_core_alua_tg_pt_gp_show_attr_nonop_delay_msecs(
2448         struct t10_alua_tg_pt_gp *tg_pt_gp,
2449         char *page)
2450 {
2451         return core_alua_show_nonop_delay_msecs(tg_pt_gp, page);
2452
2453 }
2454
2455 static ssize_t target_core_alua_tg_pt_gp_store_attr_nonop_delay_msecs(
2456         struct t10_alua_tg_pt_gp *tg_pt_gp,
2457         const char *page,
2458         size_t count)
2459 {
2460         return core_alua_store_nonop_delay_msecs(tg_pt_gp, page, count);
2461 }
2462
2463 SE_DEV_ALUA_TG_PT_ATTR(nonop_delay_msecs, S_IRUGO | S_IWUSR);
2464
2465 /*
2466  * trans_delay_msecs
2467  */
2468 static ssize_t target_core_alua_tg_pt_gp_show_attr_trans_delay_msecs(
2469         struct t10_alua_tg_pt_gp *tg_pt_gp,
2470         char *page)
2471 {
2472         return core_alua_show_trans_delay_msecs(tg_pt_gp, page);
2473 }
2474
2475 static ssize_t target_core_alua_tg_pt_gp_store_attr_trans_delay_msecs(
2476         struct t10_alua_tg_pt_gp *tg_pt_gp,
2477         const char *page,
2478         size_t count)
2479 {
2480         return core_alua_store_trans_delay_msecs(tg_pt_gp, page, count);
2481 }
2482
2483 SE_DEV_ALUA_TG_PT_ATTR(trans_delay_msecs, S_IRUGO | S_IWUSR);
2484
2485 /*
2486  * preferred
2487  */
2488
2489 static ssize_t target_core_alua_tg_pt_gp_show_attr_preferred(
2490         struct t10_alua_tg_pt_gp *tg_pt_gp,
2491         char *page)
2492 {
2493         return core_alua_show_preferred_bit(tg_pt_gp, page);
2494 }
2495
2496 static ssize_t target_core_alua_tg_pt_gp_store_attr_preferred(
2497         struct t10_alua_tg_pt_gp *tg_pt_gp,
2498         const char *page,
2499         size_t count)
2500 {
2501         return core_alua_store_preferred_bit(tg_pt_gp, page, count);
2502 }
2503
2504 SE_DEV_ALUA_TG_PT_ATTR(preferred, S_IRUGO | S_IWUSR);
2505
2506 /*
2507  * tg_pt_gp_id
2508  */
2509 static ssize_t target_core_alua_tg_pt_gp_show_attr_tg_pt_gp_id(
2510         struct t10_alua_tg_pt_gp *tg_pt_gp,
2511         char *page)
2512 {
2513         if (!(tg_pt_gp->tg_pt_gp_valid_id))
2514                 return 0;
2515
2516         return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
2517 }
2518
2519 static ssize_t target_core_alua_tg_pt_gp_store_attr_tg_pt_gp_id(
2520         struct t10_alua_tg_pt_gp *tg_pt_gp,
2521         const char *page,
2522         size_t count)
2523 {
2524         struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2525         unsigned long tg_pt_gp_id;
2526         int ret;
2527
2528         ret = strict_strtoul(page, 0, &tg_pt_gp_id);
2529         if (ret < 0) {
2530                 printk(KERN_ERR "strict_strtoul() returned %d for"
2531                         " tg_pt_gp_id\n", ret);
2532                 return -EINVAL;
2533         }
2534         if (tg_pt_gp_id > 0x0000ffff) {
2535                 printk(KERN_ERR "ALUA tg_pt_gp_id: %lu exceeds maximum:"
2536                         " 0x0000ffff\n", tg_pt_gp_id);
2537                 return -EINVAL;
2538         }
2539
2540         ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
2541         if (ret < 0)
2542                 return -EINVAL;
2543
2544         printk(KERN_INFO "Target_Core_ConfigFS: Set ALUA Target Port Group: "
2545                 "core/alua/tg_pt_gps/%s to ID: %hu\n",
2546                 config_item_name(&alua_tg_pt_gp_cg->cg_item),
2547                 tg_pt_gp->tg_pt_gp_id);
2548
2549         return count;
2550 }
2551
2552 SE_DEV_ALUA_TG_PT_ATTR(tg_pt_gp_id, S_IRUGO | S_IWUSR);
2553
2554 /*
2555  * members
2556  */
2557 static ssize_t target_core_alua_tg_pt_gp_show_attr_members(
2558         struct t10_alua_tg_pt_gp *tg_pt_gp,
2559         char *page)
2560 {
2561         struct se_port *port;
2562         struct se_portal_group *tpg;
2563         struct se_lun *lun;
2564         struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
2565         ssize_t len = 0, cur_len;
2566         unsigned char buf[TG_PT_GROUP_NAME_BUF];
2567
2568         memset(buf, 0, TG_PT_GROUP_NAME_BUF);
2569
2570         spin_lock(&tg_pt_gp->tg_pt_gp_lock);
2571         list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
2572                         tg_pt_gp_mem_list) {
2573                 port = tg_pt_gp_mem->tg_pt;
2574                 tpg = port->sep_tpg;
2575                 lun = port->sep_lun;
2576
2577                 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
2578                         "/%s\n", TPG_TFO(tpg)->get_fabric_name(),
2579                         TPG_TFO(tpg)->tpg_get_wwn(tpg),
2580                         TPG_TFO(tpg)->tpg_get_tag(tpg),
2581                         config_item_name(&lun->lun_group.cg_item));
2582                 cur_len++; /* Extra byte for NULL terminator */
2583
2584                 if ((cur_len + len) > PAGE_SIZE) {
2585                         printk(KERN_WARNING "Ran out of lu_gp_show_attr"
2586                                 "_members buffer\n");
2587                         break;
2588                 }
2589                 memcpy(page+len, buf, cur_len);
2590                 len += cur_len;
2591         }
2592         spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
2593
2594         return len;
2595 }
2596
2597 SE_DEV_ALUA_TG_PT_ATTR_RO(members);
2598
2599 CONFIGFS_EATTR_OPS(target_core_alua_tg_pt_gp, t10_alua_tg_pt_gp,
2600                         tg_pt_gp_group);
2601
2602 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
2603         &target_core_alua_tg_pt_gp_alua_access_state.attr,
2604         &target_core_alua_tg_pt_gp_alua_access_status.attr,
2605         &target_core_alua_tg_pt_gp_alua_access_type.attr,
2606         &target_core_alua_tg_pt_gp_alua_write_metadata.attr,
2607         &target_core_alua_tg_pt_gp_nonop_delay_msecs.attr,
2608         &target_core_alua_tg_pt_gp_trans_delay_msecs.attr,
2609         &target_core_alua_tg_pt_gp_preferred.attr,
2610         &target_core_alua_tg_pt_gp_tg_pt_gp_id.attr,
2611         &target_core_alua_tg_pt_gp_members.attr,
2612         NULL,
2613 };
2614
2615 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
2616 {
2617         struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2618                         struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2619
2620         core_alua_free_tg_pt_gp(tg_pt_gp);
2621 }
2622
2623 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
2624         .release                = target_core_alua_tg_pt_gp_release,
2625         .show_attribute         = target_core_alua_tg_pt_gp_attr_show,
2626         .store_attribute        = target_core_alua_tg_pt_gp_attr_store,
2627 };
2628
2629 static struct config_item_type target_core_alua_tg_pt_gp_cit = {
2630         .ct_item_ops            = &target_core_alua_tg_pt_gp_ops,
2631         .ct_attrs               = target_core_alua_tg_pt_gp_attrs,
2632         .ct_owner               = THIS_MODULE,
2633 };
2634
2635 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2636
2637 /* Start functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2638
2639 static struct config_group *target_core_alua_create_tg_pt_gp(
2640         struct config_group *group,
2641         const char *name)
2642 {
2643         struct t10_alua *alua = container_of(group, struct t10_alua,
2644                                         alua_tg_pt_gps_group);
2645         struct t10_alua_tg_pt_gp *tg_pt_gp;
2646         struct se_subsystem_dev *su_dev = alua->t10_sub_dev;
2647         struct config_group *alua_tg_pt_gp_cg = NULL;
2648         struct config_item *alua_tg_pt_gp_ci = NULL;
2649
2650         tg_pt_gp = core_alua_allocate_tg_pt_gp(su_dev, name, 0);
2651         if (!(tg_pt_gp))
2652                 return NULL;
2653
2654         alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
2655         alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
2656
2657         config_group_init_type_name(alua_tg_pt_gp_cg, name,
2658                         &target_core_alua_tg_pt_gp_cit);
2659
2660         printk(KERN_INFO "Target_Core_ConfigFS: Allocated ALUA Target Port"
2661                 " Group: alua/tg_pt_gps/%s\n",
2662                 config_item_name(alua_tg_pt_gp_ci));
2663
2664         return alua_tg_pt_gp_cg;
2665 }
2666
2667 static void target_core_alua_drop_tg_pt_gp(
2668         struct config_group *group,
2669         struct config_item *item)
2670 {
2671         struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
2672                         struct t10_alua_tg_pt_gp, tg_pt_gp_group);
2673
2674         printk(KERN_INFO "Target_Core_ConfigFS: Releasing ALUA Target Port"
2675                 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
2676                 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
2677         /*
2678          * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
2679          * -> target_core_alua_tg_pt_gp_release().
2680          */
2681         config_item_put(item);
2682 }
2683
2684 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
2685         .make_group             = &target_core_alua_create_tg_pt_gp,
2686         .drop_item              = &target_core_alua_drop_tg_pt_gp,
2687 };
2688
2689 static struct config_item_type target_core_alua_tg_pt_gps_cit = {
2690         .ct_group_ops           = &target_core_alua_tg_pt_gps_group_ops,
2691         .ct_owner               = THIS_MODULE,
2692 };
2693
2694 /* End functions for struct config_item_type target_core_alua_tg_pt_gps_cit */
2695
2696 /* Start functions for struct config_item_type target_core_alua_cit */
2697
2698 /*
2699  * target_core_alua_cit is a ConfigFS group that lives under
2700  * /sys/kernel/config/target/core/alua.  There are default groups
2701  * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
2702  * target_core_alua_cit in target_core_init_configfs() below.
2703  */
2704 static struct config_item_type target_core_alua_cit = {
2705         .ct_item_ops            = NULL,
2706         .ct_attrs               = NULL,
2707         .ct_owner               = THIS_MODULE,
2708 };
2709
2710 /* End functions for struct config_item_type target_core_alua_cit */
2711
2712 /* Start functions for struct config_item_type target_core_hba_cit */
2713
2714 static struct config_group *target_core_make_subdev(
2715         struct config_group *group,
2716         const char *name)
2717 {
2718         struct t10_alua_tg_pt_gp *tg_pt_gp;
2719         struct se_subsystem_dev *se_dev;
2720         struct se_subsystem_api *t;
2721         struct config_item *hba_ci = &group->cg_item;
2722         struct se_hba *hba = item_to_hba(hba_ci);
2723         struct config_group *dev_cg = NULL, *tg_pt_gp_cg = NULL;
2724
2725         if (mutex_lock_interruptible(&hba->hba_access_mutex))
2726                 return NULL;
2727
2728         /*
2729          * Locate the struct se_subsystem_api from parent's struct se_hba.
2730          */
2731         t = hba->transport;
2732
2733         se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
2734         if (!se_dev) {
2735                 printk(KERN_ERR "Unable to allocate memory for"
2736                                 " struct se_subsystem_dev\n");
2737                 goto unlock;
2738         }
2739         INIT_LIST_HEAD(&se_dev->g_se_dev_list);
2740         INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
2741         spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
2742         INIT_LIST_HEAD(&se_dev->t10_reservation.registration_list);
2743         INIT_LIST_HEAD(&se_dev->t10_reservation.aptpl_reg_list);
2744         spin_lock_init(&se_dev->t10_reservation.registration_lock);
2745         spin_lock_init(&se_dev->t10_reservation.aptpl_reg_lock);
2746         INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
2747         spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
2748         spin_lock_init(&se_dev->se_dev_lock);
2749         se_dev->t10_reservation.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
2750         se_dev->t10_wwn.t10_sub_dev = se_dev;
2751         se_dev->t10_alua.t10_sub_dev = se_dev;
2752         se_dev->se_dev_attrib.da_sub_dev = se_dev;
2753
2754         se_dev->se_dev_hba = hba;
2755         dev_cg = &se_dev->se_dev_group;
2756
2757         dev_cg->default_groups = kzalloc(sizeof(struct config_group) * 6,
2758                         GFP_KERNEL);
2759         if (!(dev_cg->default_groups))
2760                 goto out;
2761         /*
2762          * Set se_dev_su_ptr from struct se_subsystem_api returned void ptr
2763          * for ->allocate_virtdevice()
2764          *
2765          * se_dev->se_dev_ptr will be set after ->create_virtdev()
2766          * has been called successfully in the next level up in the
2767          * configfs tree for device object's struct config_group.
2768          */
2769         se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, name);
2770         if (!(se_dev->se_dev_su_ptr)) {
2771                 printk(KERN_ERR "Unable to locate subsystem dependent pointer"
2772                         " from allocate_virtdevice()\n");
2773                 goto out;
2774         }
2775         spin_lock(&se_global->g_device_lock);
2776         list_add_tail(&se_dev->g_se_dev_list, &se_global->g_se_dev_list);
2777         spin_unlock(&se_global->g_device_lock);
2778
2779         config_group_init_type_name(&se_dev->se_dev_group, name,
2780                         &target_core_dev_cit);
2781         config_group_init_type_name(&se_dev->se_dev_attrib.da_group, "attrib",
2782                         &target_core_dev_attrib_cit);
2783         config_group_init_type_name(&se_dev->se_dev_pr_group, "pr",
2784                         &target_core_dev_pr_cit);
2785         config_group_init_type_name(&se_dev->t10_wwn.t10_wwn_group, "wwn",
2786                         &target_core_dev_wwn_cit);
2787         config_group_init_type_name(&se_dev->t10_alua.alua_tg_pt_gps_group,
2788                         "alua", &target_core_alua_tg_pt_gps_cit);
2789         dev_cg->default_groups[0] = &se_dev->se_dev_attrib.da_group;
2790         dev_cg->default_groups[1] = &se_dev->se_dev_pr_group;
2791         dev_cg->default_groups[2] = &se_dev->t10_wwn.t10_wwn_group;
2792         dev_cg->default_groups[3] = &se_dev->t10_alua.alua_tg_pt_gps_group;
2793         dev_cg->default_groups[4] = NULL;
2794         /*
2795          * Add core/$HBA/$DEV/alua/tg_pt_gps/default_tg_pt_gp
2796          */
2797         tg_pt_gp = core_alua_allocate_tg_pt_gp(se_dev, "default_tg_pt_gp", 1);
2798         if (!(tg_pt_gp))
2799                 goto out;
2800
2801         tg_pt_gp_cg = &T10_ALUA(se_dev)->alua_tg_pt_gps_group;
2802         tg_pt_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
2803                                 GFP_KERNEL);
2804         if (!(tg_pt_gp_cg->default_groups)) {
2805                 printk(KERN_ERR "Unable to allocate tg_pt_gp_cg->"
2806                                 "default_groups\n");
2807                 goto out;
2808         }
2809
2810         config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
2811                         "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
2812         tg_pt_gp_cg->default_groups[0] = &tg_pt_gp->tg_pt_gp_group;
2813         tg_pt_gp_cg->default_groups[1] = NULL;
2814         T10_ALUA(se_dev)->default_tg_pt_gp = tg_pt_gp;
2815
2816         printk(KERN_INFO "Target_Core_ConfigFS: Allocated struct se_subsystem_dev:"
2817                 " %p se_dev_su_ptr: %p\n", se_dev, se_dev->se_dev_su_ptr);
2818
2819         mutex_unlock(&hba->hba_access_mutex);
2820         return &se_dev->se_dev_group;
2821 out:
2822         if (T10_ALUA(se_dev)->default_tg_pt_gp) {
2823                 core_alua_free_tg_pt_gp(T10_ALUA(se_dev)->default_tg_pt_gp);
2824                 T10_ALUA(se_dev)->default_tg_pt_gp = NULL;
2825         }
2826         if (tg_pt_gp_cg)
2827                 kfree(tg_pt_gp_cg->default_groups);
2828         if (dev_cg)
2829                 kfree(dev_cg->default_groups);
2830         if (se_dev->se_dev_su_ptr)
2831                 t->free_device(se_dev->se_dev_su_ptr);
2832         kfree(se_dev);
2833 unlock:
2834         mutex_unlock(&hba->hba_access_mutex);
2835         return NULL;
2836 }
2837
2838 static void target_core_drop_subdev(
2839         struct config_group *group,
2840         struct config_item *item)
2841 {
2842         struct se_subsystem_dev *se_dev = container_of(to_config_group(item),
2843                                 struct se_subsystem_dev, se_dev_group);
2844         struct se_hba *hba;
2845         struct se_subsystem_api *t;
2846         struct config_item *df_item;
2847         struct config_group *dev_cg, *tg_pt_gp_cg;
2848         int i;
2849
2850         hba = item_to_hba(&se_dev->se_dev_hba->hba_group.cg_item);
2851
2852         mutex_lock(&hba->hba_access_mutex);
2853         t = hba->transport;
2854
2855         spin_lock(&se_global->g_device_lock);
2856         list_del(&se_dev->g_se_dev_list);
2857         spin_unlock(&se_global->g_device_lock);
2858
2859         tg_pt_gp_cg = &T10_ALUA(se_dev)->alua_tg_pt_gps_group;
2860         for (i = 0; tg_pt_gp_cg->default_groups[i]; i++) {
2861                 df_item = &tg_pt_gp_cg->default_groups[i]->cg_item;
2862                 tg_pt_gp_cg->default_groups[i] = NULL;
2863                 config_item_put(df_item);
2864         }
2865         kfree(tg_pt_gp_cg->default_groups);
2866         /*
2867          * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
2868          * directly from target_core_alua_tg_pt_gp_release().
2869          */
2870         T10_ALUA(se_dev)->default_tg_pt_gp = NULL;
2871
2872         dev_cg = &se_dev->se_dev_group;
2873         for (i = 0; dev_cg->default_groups[i]; i++) {
2874                 df_item = &dev_cg->default_groups[i]->cg_item;
2875                 dev_cg->default_groups[i] = NULL;
2876                 config_item_put(df_item);
2877         }
2878         /*
2879          * The releasing of se_dev and associated se_dev->se_dev_ptr is done
2880          * from target_core_dev_item_ops->release() ->target_core_dev_release().
2881          */
2882         config_item_put(item);
2883         mutex_unlock(&hba->hba_access_mutex);
2884 }
2885
2886 static struct configfs_group_operations target_core_hba_group_ops = {
2887         .make_group             = target_core_make_subdev,
2888         .drop_item              = target_core_drop_subdev,
2889 };
2890
2891 CONFIGFS_EATTR_STRUCT(target_core_hba, se_hba);
2892 #define SE_HBA_ATTR(_name, _mode)                               \
2893 static struct target_core_hba_attribute                         \
2894                 target_core_hba_##_name =                       \
2895                 __CONFIGFS_EATTR(_name, _mode,                  \
2896                 target_core_hba_show_attr_##_name,              \
2897                 target_core_hba_store_attr_##_name);
2898
2899 #define SE_HBA_ATTR_RO(_name)                                   \
2900 static struct target_core_hba_attribute                         \
2901                 target_core_hba_##_name =                       \
2902                 __CONFIGFS_EATTR_RO(_name,                      \
2903                 target_core_hba_show_attr_##_name);
2904
2905 static ssize_t target_core_hba_show_attr_hba_info(
2906         struct se_hba *hba,
2907         char *page)
2908 {
2909         return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
2910                         hba->hba_id, hba->transport->name,
2911                         TARGET_CORE_CONFIGFS_VERSION);
2912 }
2913
2914 SE_HBA_ATTR_RO(hba_info);
2915
2916 static ssize_t target_core_hba_show_attr_hba_mode(struct se_hba *hba,
2917                                 char *page)
2918 {
2919         int hba_mode = 0;
2920
2921         if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
2922                 hba_mode = 1;
2923
2924         return sprintf(page, "%d\n", hba_mode);
2925 }
2926
2927 static ssize_t target_core_hba_store_attr_hba_mode(struct se_hba *hba,
2928                                 const char *page, size_t count)
2929 {
2930         struct se_subsystem_api *transport = hba->transport;
2931         unsigned long mode_flag;
2932         int ret;
2933
2934         if (transport->pmode_enable_hba == NULL)
2935                 return -EINVAL;
2936
2937         ret = strict_strtoul(page, 0, &mode_flag);
2938         if (ret < 0) {
2939                 printk(KERN_ERR "Unable to extract hba mode flag: %d\n", ret);
2940                 return -EINVAL;
2941         }
2942
2943         spin_lock(&hba->device_lock);
2944         if (!(list_empty(&hba->hba_dev_list))) {
2945                 printk(KERN_ERR "Unable to set hba_mode with active devices\n");
2946                 spin_unlock(&hba->device_lock);
2947                 return -EINVAL;
2948         }
2949         spin_unlock(&hba->device_lock);
2950
2951         ret = transport->pmode_enable_hba(hba, mode_flag);
2952         if (ret < 0)
2953                 return -EINVAL;
2954         if (ret > 0)
2955                 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
2956         else if (ret == 0)
2957                 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
2958
2959         return count;
2960 }
2961
2962 SE_HBA_ATTR(hba_mode, S_IRUGO | S_IWUSR);
2963
2964 CONFIGFS_EATTR_OPS(target_core_hba, se_hba, hba_group);
2965
2966 static void target_core_hba_release(struct config_item *item)
2967 {
2968         struct se_hba *hba = container_of(to_config_group(item),
2969                                 struct se_hba, hba_group);
2970         core_delete_hba(hba);
2971 }
2972
2973 static struct configfs_attribute *target_core_hba_attrs[] = {
2974         &target_core_hba_hba_info.attr,
2975         &target_core_hba_hba_mode.attr,
2976         NULL,
2977 };
2978
2979 static struct configfs_item_operations target_core_hba_item_ops = {
2980         .release                = target_core_hba_release,
2981         .show_attribute         = target_core_hba_attr_show,
2982         .store_attribute        = target_core_hba_attr_store,
2983 };
2984
2985 static struct config_item_type target_core_hba_cit = {
2986         .ct_item_ops            = &target_core_hba_item_ops,
2987         .ct_group_ops           = &target_core_hba_group_ops,
2988         .ct_attrs               = target_core_hba_attrs,
2989         .ct_owner               = THIS_MODULE,
2990 };
2991
2992 static struct config_group *target_core_call_addhbatotarget(
2993         struct config_group *group,
2994         const char *name)
2995 {
2996         char *se_plugin_str, *str, *str2;
2997         struct se_hba *hba;
2998         char buf[TARGET_CORE_NAME_MAX_LEN];
2999         unsigned long plugin_dep_id = 0;
3000         int ret;
3001
3002         memset(buf, 0, TARGET_CORE_NAME_MAX_LEN);
3003         if (strlen(name) > TARGET_CORE_NAME_MAX_LEN) {
3004                 printk(KERN_ERR "Passed *name strlen(): %d exceeds"
3005                         " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3006                         TARGET_CORE_NAME_MAX_LEN);
3007                 return ERR_PTR(-ENAMETOOLONG);
3008         }
3009         snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3010
3011         str = strstr(buf, "_");
3012         if (!(str)) {
3013                 printk(KERN_ERR "Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3014                 return ERR_PTR(-EINVAL);
3015         }
3016         se_plugin_str = buf;
3017         /*
3018          * Special case for subsystem plugins that have "_" in their names.
3019          * Namely rd_direct and rd_mcp..
3020          */
3021         str2 = strstr(str+1, "_");
3022         if ((str2)) {
3023                 *str2 = '\0'; /* Terminate for *se_plugin_str */
3024                 str2++; /* Skip to start of plugin dependent ID */
3025                 str = str2;
3026         } else {
3027                 *str = '\0'; /* Terminate for *se_plugin_str */
3028                 str++; /* Skip to start of plugin dependent ID */
3029         }
3030
3031         ret = strict_strtoul(str, 0, &plugin_dep_id);
3032         if (ret < 0) {
3033                 printk(KERN_ERR "strict_strtoul() returned %d for"
3034                                 " plugin_dep_id\n", ret);
3035                 return ERR_PTR(-EINVAL);
3036         }
3037         /*
3038          * Load up TCM subsystem plugins if they have not already been loaded.
3039          */
3040         if (transport_subsystem_check_init() < 0)
3041                 return ERR_PTR(-EINVAL);
3042
3043         hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3044         if (IS_ERR(hba))
3045                 return ERR_CAST(hba);
3046
3047         config_group_init_type_name(&hba->hba_group, name,
3048                         &target_core_hba_cit);
3049
3050         return &hba->hba_group;
3051 }
3052
3053 static void target_core_call_delhbafromtarget(
3054         struct config_group *group,
3055         struct config_item *item)
3056 {
3057         /*
3058          * core_delete_hba() is called from target_core_hba_item_ops->release()
3059          * -> target_core_hba_release()
3060          */
3061         config_item_put(item);
3062 }
3063
3064 static struct configfs_group_operations target_core_group_ops = {
3065         .make_group     = target_core_call_addhbatotarget,
3066         .drop_item      = target_core_call_delhbafromtarget,
3067 };
3068
3069 static struct config_item_type target_core_cit = {
3070         .ct_item_ops    = NULL,
3071         .ct_group_ops   = &target_core_group_ops,
3072         .ct_attrs       = NULL,
3073         .ct_owner       = THIS_MODULE,
3074 };
3075
3076 /* Stop functions for struct config_item_type target_core_hba_cit */
3077
3078 static int __init target_core_init_configfs(void)
3079 {
3080         struct config_group *target_cg, *hba_cg = NULL, *alua_cg = NULL;
3081         struct config_group *lu_gp_cg = NULL;
3082         struct configfs_subsystem *subsys;
3083         struct t10_alua_lu_gp *lu_gp;
3084         int ret;
3085
3086         printk(KERN_INFO "TARGET_CORE[0]: Loading Generic Kernel Storage"
3087                 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3088                 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3089
3090         subsys = target_core_subsystem[0];
3091         config_group_init(&subsys->su_group);
3092         mutex_init(&subsys->su_mutex);
3093
3094         INIT_LIST_HEAD(&g_tf_list);
3095         mutex_init(&g_tf_lock);
3096         init_scsi_index_table();
3097         ret = init_se_global();
3098         if (ret < 0)
3099                 return -1;
3100         /*
3101          * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3102          * and ALUA Logical Unit Group and Target Port Group infrastructure.
3103          */
3104         target_cg = &subsys->su_group;
3105         target_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3106                                 GFP_KERNEL);
3107         if (!(target_cg->default_groups)) {
3108                 printk(KERN_ERR "Unable to allocate target_cg->default_groups\n");
3109                 goto out_global;
3110         }
3111
3112         config_group_init_type_name(&se_global->target_core_hbagroup,
3113                         "core", &target_core_cit);
3114         target_cg->default_groups[0] = &se_global->target_core_hbagroup;
3115         target_cg->default_groups[1] = NULL;
3116         /*
3117          * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3118          */
3119         hba_cg = &se_global->target_core_hbagroup;
3120         hba_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3121                                 GFP_KERNEL);
3122         if (!(hba_cg->default_groups)) {
3123                 printk(KERN_ERR "Unable to allocate hba_cg->default_groups\n");
3124                 goto out_global;
3125         }
3126         config_group_init_type_name(&se_global->alua_group,
3127                         "alua", &target_core_alua_cit);
3128         hba_cg->default_groups[0] = &se_global->alua_group;
3129         hba_cg->default_groups[1] = NULL;
3130         /*
3131          * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3132          * groups under /sys/kernel/config/target/core/alua/
3133          */
3134         alua_cg = &se_global->alua_group;
3135         alua_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3136                         GFP_KERNEL);
3137         if (!(alua_cg->default_groups)) {
3138                 printk(KERN_ERR "Unable to allocate alua_cg->default_groups\n");
3139                 goto out_global;
3140         }
3141
3142         config_group_init_type_name(&se_global->alua_lu_gps_group,
3143                         "lu_gps", &target_core_alua_lu_gps_cit);
3144         alua_cg->default_groups[0] = &se_global->alua_lu_gps_group;
3145         alua_cg->default_groups[1] = NULL;
3146         /*
3147          * Add core/alua/lu_gps/default_lu_gp
3148          */
3149         lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3150         if (IS_ERR(lu_gp))
3151                 goto out_global;
3152
3153         lu_gp_cg = &se_global->alua_lu_gps_group;
3154         lu_gp_cg->default_groups = kzalloc(sizeof(struct config_group) * 2,
3155                         GFP_KERNEL);
3156         if (!(lu_gp_cg->default_groups)) {
3157                 printk(KERN_ERR "Unable to allocate lu_gp_cg->default_groups\n");
3158                 goto out_global;
3159         }
3160
3161         config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3162                                 &target_core_alua_lu_gp_cit);
3163         lu_gp_cg->default_groups[0] = &lu_gp->lu_gp_group;
3164         lu_gp_cg->default_groups[1] = NULL;
3165         se_global->default_lu_gp = lu_gp;
3166         /*
3167          * Register the target_core_mod subsystem with configfs.
3168          */
3169         ret = configfs_register_subsystem(subsys);
3170         if (ret < 0) {
3171                 printk(KERN_ERR "Error %d while registering subsystem %s\n",
3172                         ret, subsys->su_group.cg_item.ci_namebuf);
3173                 goto out_global;
3174         }
3175         printk(KERN_INFO "TARGET_CORE[0]: Initialized ConfigFS Fabric"
3176                 " Infrastructure: "TARGET_CORE_CONFIGFS_VERSION" on %s/%s"
3177                 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3178         /*
3179          * Register built-in RAMDISK subsystem logic for virtual LUN 0
3180          */
3181         ret = rd_module_init();
3182         if (ret < 0)
3183                 goto out;
3184
3185         if (core_dev_setup_virtual_lun0() < 0)
3186                 goto out;
3187
3188         return 0;
3189
3190 out:
3191         configfs_unregister_subsystem(subsys);
3192         core_dev_release_virtual_lun0();
3193         rd_module_exit();
3194 out_global:
3195         if (se_global->default_lu_gp) {
3196                 core_alua_free_lu_gp(se_global->default_lu_gp);
3197                 se_global->default_lu_gp = NULL;
3198         }
3199         if (lu_gp_cg)
3200                 kfree(lu_gp_cg->default_groups);
3201         if (alua_cg)
3202                 kfree(alua_cg->default_groups);
3203         if (hba_cg)
3204                 kfree(hba_cg->default_groups);
3205         kfree(target_cg->default_groups);
3206         release_se_global();
3207         return -1;
3208 }
3209
3210 static void __exit target_core_exit_configfs(void)
3211 {
3212         struct configfs_subsystem *subsys;
3213         struct config_group *hba_cg, *alua_cg, *lu_gp_cg;
3214         struct config_item *item;
3215         int i;
3216
3217         se_global->in_shutdown = 1;
3218         subsys = target_core_subsystem[0];
3219
3220         lu_gp_cg = &se_global->alua_lu_gps_group;
3221         for (i = 0; lu_gp_cg->default_groups[i]; i++) {
3222                 item = &lu_gp_cg->default_groups[i]->cg_item;
3223                 lu_gp_cg->default_groups[i] = NULL;
3224                 config_item_put(item);
3225         }
3226         kfree(lu_gp_cg->default_groups);
3227         lu_gp_cg->default_groups = NULL;
3228
3229         alua_cg = &se_global->alua_group;
3230         for (i = 0; alua_cg->default_groups[i]; i++) {
3231                 item = &alua_cg->default_groups[i]->cg_item;
3232                 alua_cg->default_groups[i] = NULL;
3233                 config_item_put(item);
3234         }
3235         kfree(alua_cg->default_groups);
3236         alua_cg->default_groups = NULL;
3237
3238         hba_cg = &se_global->target_core_hbagroup;
3239         for (i = 0; hba_cg->default_groups[i]; i++) {
3240                 item = &hba_cg->default_groups[i]->cg_item;
3241                 hba_cg->default_groups[i] = NULL;
3242                 config_item_put(item);
3243         }
3244         kfree(hba_cg->default_groups);
3245         hba_cg->default_groups = NULL;
3246         /*
3247          * We expect subsys->su_group.default_groups to be released
3248          * by configfs subsystem provider logic..
3249          */
3250         configfs_unregister_subsystem(subsys);
3251         kfree(subsys->su_group.default_groups);
3252
3253         core_alua_free_lu_gp(se_global->default_lu_gp);
3254         se_global->default_lu_gp = NULL;
3255
3256         printk(KERN_INFO "TARGET_CORE[0]: Released ConfigFS Fabric"
3257                         " Infrastructure\n");
3258
3259         core_dev_release_virtual_lun0();
3260         rd_module_exit();
3261         release_se_global();
3262
3263         return;
3264 }
3265
3266 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3267 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3268 MODULE_LICENSE("GPL");
3269
3270 module_init(target_core_init_configfs);
3271 module_exit(target_core_exit_configfs);