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