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