1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * Filename: target_core_configfs.c
4 *
5 * This file contains ConfigFS logic for the Generic Target Engine project.
6 *
7 * (c) Copyright 2008-2013 Datera, Inc.
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * based on configfs Copyright (C) 2005 Oracle. All rights reserved.
12 *
13 ****************************************************************************/
14
15 #include <linux/kstrtox.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <generated/utsrelease.h>
19 #include <linux/utsname.h>
20 #include <linux/init.h>
21 #include <linux/fs.h>
22 #include <linux/namei.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 #include <linux/delay.h>
26 #include <linux/unistd.h>
27 #include <linux/string.h>
28 #include <linux/parser.h>
29 #include <linux/syscalls.h>
30 #include <linux/configfs.h>
31 #include <linux/spinlock.h>
32
33 #include <target/target_core_base.h>
34 #include <target/target_core_backend.h>
35 #include <target/target_core_fabric.h>
36
37 #include "target_core_internal.h"
38 #include "target_core_alua.h"
39 #include "target_core_pr.h"
40 #include "target_core_rd.h"
41 #include "target_core_xcopy.h"
42
43 #define TB_CIT_SETUP(_name, _item_ops, _group_ops, _attrs) \
44 static void target_core_setup_##_name##_cit(struct target_backend *tb) \
45 { \
46 struct config_item_type *cit = &tb->tb_##_name##_cit; \
47 \
48 cit->ct_item_ops = _item_ops; \
49 cit->ct_group_ops = _group_ops; \
50 cit->ct_attrs = _attrs; \
51 cit->ct_owner = tb->ops->owner; \
52 pr_debug("Setup generic %s\n", __stringify(_name)); \
53 }
54
55 #define TB_CIT_SETUP_DRV(_name, _item_ops, _group_ops) \
56 static void target_core_setup_##_name##_cit(struct target_backend *tb) \
57 { \
58 struct config_item_type *cit = &tb->tb_##_name##_cit; \
59 \
60 cit->ct_item_ops = _item_ops; \
61 cit->ct_group_ops = _group_ops; \
62 cit->ct_attrs = tb->ops->tb_##_name##_attrs; \
63 cit->ct_owner = tb->ops->owner; \
64 pr_debug("Setup generic %s\n", __stringify(_name)); \
65 }
66
67 extern struct t10_alua_lu_gp *default_lu_gp;
68
69 static LIST_HEAD(g_tf_list);
70 static DEFINE_MUTEX(g_tf_lock);
71
72 static struct config_group target_core_hbagroup;
73 static struct config_group alua_group;
74 static struct config_group alua_lu_gps_group;
75
76 static unsigned int target_devices;
77 static DEFINE_MUTEX(target_devices_lock);
78
79 static inline struct se_hba *
item_to_hba(struct config_item * item)80 item_to_hba(struct config_item *item)
81 {
82 return container_of(to_config_group(item), struct se_hba, hba_group);
83 }
84
85 /*
86 * Attributes for /sys/kernel/config/target/
87 */
target_core_item_version_show(struct config_item * item,char * page)88 static ssize_t target_core_item_version_show(struct config_item *item,
89 char *page)
90 {
91 return sprintf(page, "Target Engine Core ConfigFS Infrastructure %s"
92 " on %s/%s on "UTS_RELEASE"\n", TARGET_CORE_VERSION,
93 utsname()->sysname, utsname()->machine);
94 }
95
96 CONFIGFS_ATTR_RO(target_core_item_, version);
97
98 char db_root[DB_ROOT_LEN] = DB_ROOT_DEFAULT;
99 static char db_root_stage[DB_ROOT_LEN];
100
target_core_item_dbroot_show(struct config_item * item,char * page)101 static ssize_t target_core_item_dbroot_show(struct config_item *item,
102 char *page)
103 {
104 return sprintf(page, "%s\n", db_root);
105 }
106
target_core_item_dbroot_store(struct config_item * item,const char * page,size_t count)107 static ssize_t target_core_item_dbroot_store(struct config_item *item,
108 const char *page, size_t count)
109 {
110 ssize_t read_bytes;
111 struct file *fp;
112 ssize_t r = -EINVAL;
113
114 mutex_lock(&target_devices_lock);
115 if (target_devices) {
116 pr_err("db_root: cannot be changed because it's in use\n");
117 goto unlock;
118 }
119
120 if (count > (DB_ROOT_LEN - 1)) {
121 pr_err("db_root: count %d exceeds DB_ROOT_LEN-1: %u\n",
122 (int)count, DB_ROOT_LEN - 1);
123 goto unlock;
124 }
125
126 read_bytes = scnprintf(db_root_stage, DB_ROOT_LEN, "%s", page);
127 if (!read_bytes)
128 goto unlock;
129
130 if (db_root_stage[read_bytes - 1] == '\n')
131 db_root_stage[read_bytes - 1] = '\0';
132
133 /* validate new db root before accepting it */
134 fp = filp_open(db_root_stage, O_RDONLY, 0);
135 if (IS_ERR(fp)) {
136 pr_err("db_root: cannot open: %s\n", db_root_stage);
137 goto unlock;
138 }
139 if (!S_ISDIR(file_inode(fp)->i_mode)) {
140 filp_close(fp, NULL);
141 pr_err("db_root: not a directory: %s\n", db_root_stage);
142 goto unlock;
143 }
144 filp_close(fp, NULL);
145
146 strscpy(db_root, db_root_stage);
147 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
148
149 r = read_bytes;
150
151 unlock:
152 mutex_unlock(&target_devices_lock);
153 return r;
154 }
155
156 CONFIGFS_ATTR(target_core_item_, dbroot);
157
target_core_get_fabric(const char * name)158 static struct target_fabric_configfs *target_core_get_fabric(
159 const char *name)
160 {
161 struct target_fabric_configfs *tf;
162
163 if (!name)
164 return NULL;
165
166 mutex_lock(&g_tf_lock);
167 list_for_each_entry(tf, &g_tf_list, tf_list) {
168 const char *cmp_name = tf->tf_ops->fabric_alias;
169 if (!cmp_name)
170 cmp_name = tf->tf_ops->fabric_name;
171 if (!strcmp(cmp_name, name)) {
172 atomic_inc(&tf->tf_access_cnt);
173 mutex_unlock(&g_tf_lock);
174 return tf;
175 }
176 }
177 mutex_unlock(&g_tf_lock);
178
179 return NULL;
180 }
181
182 /*
183 * Called from struct target_core_group_ops->make_group()
184 */
target_core_register_fabric(struct config_group * group,const char * name)185 static struct config_group *target_core_register_fabric(
186 struct config_group *group,
187 const char *name)
188 {
189 struct target_fabric_configfs *tf;
190 int ret;
191
192 pr_debug("Target_Core_ConfigFS: REGISTER -> group: %p name:"
193 " %s\n", group, name);
194
195 tf = target_core_get_fabric(name);
196 if (!tf) {
197 pr_debug("target_core_register_fabric() trying autoload for %s\n",
198 name);
199
200 /*
201 * Below are some hardcoded request_module() calls to automatically
202 * local fabric modules when the following is called:
203 *
204 * mkdir -p /sys/kernel/config/target/$MODULE_NAME
205 *
206 * Note that this does not limit which TCM fabric module can be
207 * registered, but simply provids auto loading logic for modules with
208 * mkdir(2) system calls with known TCM fabric modules.
209 */
210
211 if (!strncmp(name, "iscsi", 5)) {
212 /*
213 * Automatically load the LIO Target fabric module when the
214 * following is called:
215 *
216 * mkdir -p $CONFIGFS/target/iscsi
217 */
218 ret = request_module("iscsi_target_mod");
219 if (ret < 0) {
220 pr_debug("request_module() failed for"
221 " iscsi_target_mod.ko: %d\n", ret);
222 return ERR_PTR(-EINVAL);
223 }
224 } else if (!strncmp(name, "loopback", 8)) {
225 /*
226 * Automatically load the tcm_loop fabric module when the
227 * following is called:
228 *
229 * mkdir -p $CONFIGFS/target/loopback
230 */
231 ret = request_module("tcm_loop");
232 if (ret < 0) {
233 pr_debug("request_module() failed for"
234 " tcm_loop.ko: %d\n", ret);
235 return ERR_PTR(-EINVAL);
236 }
237 }
238
239 tf = target_core_get_fabric(name);
240 }
241
242 if (!tf) {
243 pr_debug("target_core_get_fabric() failed for %s\n",
244 name);
245 return ERR_PTR(-EINVAL);
246 }
247 pr_debug("Target_Core_ConfigFS: REGISTER -> Located fabric:"
248 " %s\n", tf->tf_ops->fabric_name);
249 /*
250 * On a successful target_core_get_fabric() look, the returned
251 * struct target_fabric_configfs *tf will contain a usage reference.
252 */
253 pr_debug("Target_Core_ConfigFS: REGISTER tfc_wwn_cit -> %p\n",
254 &tf->tf_wwn_cit);
255
256 config_group_init_type_name(&tf->tf_group, name, &tf->tf_wwn_cit);
257
258 config_group_init_type_name(&tf->tf_disc_group, "discovery_auth",
259 &tf->tf_discovery_cit);
260 configfs_add_default_group(&tf->tf_disc_group, &tf->tf_group);
261
262 pr_debug("Target_Core_ConfigFS: REGISTER -> Allocated Fabric: %s\n",
263 config_item_name(&tf->tf_group.cg_item));
264 return &tf->tf_group;
265 }
266
267 /*
268 * Called from struct target_core_group_ops->drop_item()
269 */
target_core_deregister_fabric(struct config_group * group,struct config_item * item)270 static void target_core_deregister_fabric(
271 struct config_group *group,
272 struct config_item *item)
273 {
274 struct target_fabric_configfs *tf = container_of(
275 to_config_group(item), struct target_fabric_configfs, tf_group);
276
277 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Looking up %s in"
278 " tf list\n", config_item_name(item));
279
280 pr_debug("Target_Core_ConfigFS: DEREGISTER -> located fabric:"
281 " %s\n", tf->tf_ops->fabric_name);
282 atomic_dec(&tf->tf_access_cnt);
283
284 pr_debug("Target_Core_ConfigFS: DEREGISTER -> Releasing ci"
285 " %s\n", config_item_name(item));
286
287 configfs_remove_default_groups(&tf->tf_group);
288 config_item_put(item);
289 }
290
291 static struct configfs_group_operations target_core_fabric_group_ops = {
292 .make_group = &target_core_register_fabric,
293 .drop_item = &target_core_deregister_fabric,
294 };
295
296 /*
297 * All item attributes appearing in /sys/kernel/target/ appear here.
298 */
299 static struct configfs_attribute *target_core_fabric_item_attrs[] = {
300 &target_core_item_attr_version,
301 &target_core_item_attr_dbroot,
302 NULL,
303 };
304
305 /*
306 * Provides Fabrics Groups and Item Attributes for /sys/kernel/config/target/
307 */
308 static const struct config_item_type target_core_fabrics_item = {
309 .ct_group_ops = &target_core_fabric_group_ops,
310 .ct_attrs = target_core_fabric_item_attrs,
311 .ct_owner = THIS_MODULE,
312 };
313
314 static struct configfs_subsystem target_core_fabrics = {
315 .su_group = {
316 .cg_item = {
317 .ci_namebuf = "target",
318 .ci_type = &target_core_fabrics_item,
319 },
320 },
321 };
322
target_depend_item(struct config_item * item)323 int target_depend_item(struct config_item *item)
324 {
325 return configfs_depend_item(&target_core_fabrics, item);
326 }
327 EXPORT_SYMBOL(target_depend_item);
328
target_undepend_item(struct config_item * item)329 void target_undepend_item(struct config_item *item)
330 {
331 return configfs_undepend_item(item);
332 }
333 EXPORT_SYMBOL(target_undepend_item);
334
335 /*##############################################################################
336 // Start functions called by external Target Fabrics Modules
337 //############################################################################*/
target_disable_feature(struct se_portal_group * se_tpg)338 static int target_disable_feature(struct se_portal_group *se_tpg)
339 {
340 return 0;
341 }
342
target_default_get_inst_index(struct se_portal_group * se_tpg)343 static u32 target_default_get_inst_index(struct se_portal_group *se_tpg)
344 {
345 return 1;
346 }
347
target_default_sess_get_index(struct se_session * se_sess)348 static u32 target_default_sess_get_index(struct se_session *se_sess)
349 {
350 return 0;
351 }
352
target_set_default_node_attributes(struct se_node_acl * se_acl)353 static void target_set_default_node_attributes(struct se_node_acl *se_acl)
354 {
355 }
356
target_default_get_cmd_state(struct se_cmd * se_cmd)357 static int target_default_get_cmd_state(struct se_cmd *se_cmd)
358 {
359 return 0;
360 }
361
target_fabric_tf_ops_check(const struct target_core_fabric_ops * tfo)362 static int target_fabric_tf_ops_check(const struct target_core_fabric_ops *tfo)
363 {
364 if (tfo->fabric_alias) {
365 if (strlen(tfo->fabric_alias) >= TARGET_FABRIC_NAME_SIZE) {
366 pr_err("Passed alias: %s exceeds "
367 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_alias);
368 return -EINVAL;
369 }
370 }
371 if (!tfo->fabric_name) {
372 pr_err("Missing tfo->fabric_name\n");
373 return -EINVAL;
374 }
375 if (strlen(tfo->fabric_name) >= TARGET_FABRIC_NAME_SIZE) {
376 pr_err("Passed name: %s exceeds "
377 "TARGET_FABRIC_NAME_SIZE\n", tfo->fabric_name);
378 return -EINVAL;
379 }
380 if (!tfo->tpg_get_wwn) {
381 pr_err("Missing tfo->tpg_get_wwn()\n");
382 return -EINVAL;
383 }
384 if (!tfo->tpg_get_tag) {
385 pr_err("Missing tfo->tpg_get_tag()\n");
386 return -EINVAL;
387 }
388 if (!tfo->release_cmd) {
389 pr_err("Missing tfo->release_cmd()\n");
390 return -EINVAL;
391 }
392 if (!tfo->write_pending) {
393 pr_err("Missing tfo->write_pending()\n");
394 return -EINVAL;
395 }
396 if (!tfo->queue_data_in) {
397 pr_err("Missing tfo->queue_data_in()\n");
398 return -EINVAL;
399 }
400 if (!tfo->queue_status) {
401 pr_err("Missing tfo->queue_status()\n");
402 return -EINVAL;
403 }
404 if (!tfo->queue_tm_rsp) {
405 pr_err("Missing tfo->queue_tm_rsp()\n");
406 return -EINVAL;
407 }
408 if (!tfo->aborted_task) {
409 pr_err("Missing tfo->aborted_task()\n");
410 return -EINVAL;
411 }
412 if (!tfo->check_stop_free) {
413 pr_err("Missing tfo->check_stop_free()\n");
414 return -EINVAL;
415 }
416 /*
417 * We at least require tfo->fabric_make_wwn(), tfo->fabric_drop_wwn()
418 * tfo->fabric_make_tpg() and tfo->fabric_drop_tpg() in
419 * target_core_fabric_configfs.c WWN+TPG group context code.
420 */
421 if (!tfo->fabric_make_wwn) {
422 pr_err("Missing tfo->fabric_make_wwn()\n");
423 return -EINVAL;
424 }
425 if (!tfo->fabric_drop_wwn) {
426 pr_err("Missing tfo->fabric_drop_wwn()\n");
427 return -EINVAL;
428 }
429 if (!tfo->fabric_make_tpg) {
430 pr_err("Missing tfo->fabric_make_tpg()\n");
431 return -EINVAL;
432 }
433 if (!tfo->fabric_drop_tpg) {
434 pr_err("Missing tfo->fabric_drop_tpg()\n");
435 return -EINVAL;
436 }
437
438 return 0;
439 }
440
target_set_default_ops(struct target_core_fabric_ops * tfo)441 static void target_set_default_ops(struct target_core_fabric_ops *tfo)
442 {
443 if (!tfo->tpg_check_demo_mode)
444 tfo->tpg_check_demo_mode = target_disable_feature;
445
446 if (!tfo->tpg_check_demo_mode_cache)
447 tfo->tpg_check_demo_mode_cache = target_disable_feature;
448
449 if (!tfo->tpg_check_demo_mode_write_protect)
450 tfo->tpg_check_demo_mode_write_protect = target_disable_feature;
451
452 if (!tfo->tpg_check_prod_mode_write_protect)
453 tfo->tpg_check_prod_mode_write_protect = target_disable_feature;
454
455 if (!tfo->tpg_get_inst_index)
456 tfo->tpg_get_inst_index = target_default_get_inst_index;
457
458 if (!tfo->sess_get_index)
459 tfo->sess_get_index = target_default_sess_get_index;
460
461 if (!tfo->set_default_node_attributes)
462 tfo->set_default_node_attributes = target_set_default_node_attributes;
463
464 if (!tfo->get_cmd_state)
465 tfo->get_cmd_state = target_default_get_cmd_state;
466 }
467
target_register_template(const struct target_core_fabric_ops * fo)468 int target_register_template(const struct target_core_fabric_ops *fo)
469 {
470 struct target_core_fabric_ops *tfo;
471 struct target_fabric_configfs *tf;
472 int ret;
473
474 ret = target_fabric_tf_ops_check(fo);
475 if (ret)
476 return ret;
477
478 tf = kzalloc(sizeof(struct target_fabric_configfs), GFP_KERNEL);
479 if (!tf) {
480 pr_err("%s: could not allocate memory!\n", __func__);
481 return -ENOMEM;
482 }
483 tfo = kzalloc(sizeof(struct target_core_fabric_ops), GFP_KERNEL);
484 if (!tfo) {
485 kfree(tf);
486 pr_err("%s: could not allocate memory!\n", __func__);
487 return -ENOMEM;
488 }
489 memcpy(tfo, fo, sizeof(*tfo));
490 target_set_default_ops(tfo);
491
492 INIT_LIST_HEAD(&tf->tf_list);
493 atomic_set(&tf->tf_access_cnt, 0);
494 tf->tf_ops = tfo;
495 target_fabric_setup_cits(tf);
496
497 mutex_lock(&g_tf_lock);
498 list_add_tail(&tf->tf_list, &g_tf_list);
499 mutex_unlock(&g_tf_lock);
500
501 return 0;
502 }
503 EXPORT_SYMBOL(target_register_template);
504
target_unregister_template(const struct target_core_fabric_ops * fo)505 void target_unregister_template(const struct target_core_fabric_ops *fo)
506 {
507 struct target_fabric_configfs *t;
508
509 mutex_lock(&g_tf_lock);
510 list_for_each_entry(t, &g_tf_list, tf_list) {
511 if (!strcmp(t->tf_ops->fabric_name, fo->fabric_name)) {
512 BUG_ON(atomic_read(&t->tf_access_cnt));
513 list_del(&t->tf_list);
514 mutex_unlock(&g_tf_lock);
515 /*
516 * Wait for any outstanding fabric se_deve_entry->rcu_head
517 * callbacks to complete post kfree_rcu(), before allowing
518 * fabric driver unload of TFO->module to proceed.
519 */
520 rcu_barrier();
521 kfree(t->tf_tpg_base_cit.ct_attrs);
522 kfree(t->tf_ops);
523 kfree(t);
524 return;
525 }
526 }
527 mutex_unlock(&g_tf_lock);
528 }
529 EXPORT_SYMBOL(target_unregister_template);
530
531 /*##############################################################################
532 // Stop functions called by external Target Fabrics Modules
533 //############################################################################*/
534
to_attrib(struct config_item * item)535 static inline struct se_dev_attrib *to_attrib(struct config_item *item)
536 {
537 return container_of(to_config_group(item), struct se_dev_attrib,
538 da_group);
539 }
540
541 /* Start functions for struct config_item_type tb_dev_attrib_cit */
542 #define DEF_CONFIGFS_ATTRIB_SHOW(_name) \
543 static ssize_t _name##_show(struct config_item *item, char *page) \
544 { \
545 return snprintf(page, PAGE_SIZE, "%u\n", to_attrib(item)->_name); \
546 }
547
548 DEF_CONFIGFS_ATTRIB_SHOW(emulate_model_alias);
549 DEF_CONFIGFS_ATTRIB_SHOW(emulate_dpo);
550 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_write);
551 DEF_CONFIGFS_ATTRIB_SHOW(emulate_fua_read);
552 DEF_CONFIGFS_ATTRIB_SHOW(emulate_write_cache);
553 DEF_CONFIGFS_ATTRIB_SHOW(emulate_ua_intlck_ctrl);
554 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tas);
555 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpu);
556 DEF_CONFIGFS_ATTRIB_SHOW(emulate_tpws);
557 DEF_CONFIGFS_ATTRIB_SHOW(emulate_caw);
558 DEF_CONFIGFS_ATTRIB_SHOW(emulate_3pc);
559 DEF_CONFIGFS_ATTRIB_SHOW(emulate_pr);
560 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_type);
561 DEF_CONFIGFS_ATTRIB_SHOW(hw_pi_prot_type);
562 DEF_CONFIGFS_ATTRIB_SHOW(pi_prot_verify);
563 DEF_CONFIGFS_ATTRIB_SHOW(enforce_pr_isids);
564 DEF_CONFIGFS_ATTRIB_SHOW(is_nonrot);
565 DEF_CONFIGFS_ATTRIB_SHOW(emulate_rest_reord);
566 DEF_CONFIGFS_ATTRIB_SHOW(force_pr_aptpl);
567 DEF_CONFIGFS_ATTRIB_SHOW(hw_block_size);
568 DEF_CONFIGFS_ATTRIB_SHOW(block_size);
569 DEF_CONFIGFS_ATTRIB_SHOW(hw_max_sectors);
570 DEF_CONFIGFS_ATTRIB_SHOW(optimal_sectors);
571 DEF_CONFIGFS_ATTRIB_SHOW(hw_queue_depth);
572 DEF_CONFIGFS_ATTRIB_SHOW(queue_depth);
573 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_lba_count);
574 DEF_CONFIGFS_ATTRIB_SHOW(max_unmap_block_desc_count);
575 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity);
576 DEF_CONFIGFS_ATTRIB_SHOW(unmap_granularity_alignment);
577 DEF_CONFIGFS_ATTRIB_SHOW(unmap_zeroes_data);
578 DEF_CONFIGFS_ATTRIB_SHOW(max_write_same_len);
579 DEF_CONFIGFS_ATTRIB_SHOW(emulate_rsoc);
580 DEF_CONFIGFS_ATTRIB_SHOW(submit_type);
581 DEF_CONFIGFS_ATTRIB_SHOW(atomic_max_len);
582 DEF_CONFIGFS_ATTRIB_SHOW(atomic_alignment);
583 DEF_CONFIGFS_ATTRIB_SHOW(atomic_granularity);
584 DEF_CONFIGFS_ATTRIB_SHOW(atomic_max_with_boundary);
585 DEF_CONFIGFS_ATTRIB_SHOW(atomic_max_boundary);
586
587 #define DEF_CONFIGFS_ATTRIB_STORE_U32(_name) \
588 static ssize_t _name##_store(struct config_item *item, const char *page,\
589 size_t count) \
590 { \
591 struct se_dev_attrib *da = to_attrib(item); \
592 u32 val; \
593 int ret; \
594 \
595 ret = kstrtou32(page, 0, &val); \
596 if (ret < 0) \
597 return ret; \
598 da->_name = val; \
599 return count; \
600 }
601
602 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_lba_count);
603 DEF_CONFIGFS_ATTRIB_STORE_U32(max_unmap_block_desc_count);
604 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity);
605 DEF_CONFIGFS_ATTRIB_STORE_U32(unmap_granularity_alignment);
606 DEF_CONFIGFS_ATTRIB_STORE_U32(max_write_same_len);
607
608 #define DEF_CONFIGFS_ATTRIB_STORE_BOOL(_name) \
609 static ssize_t _name##_store(struct config_item *item, const char *page, \
610 size_t count) \
611 { \
612 struct se_dev_attrib *da = to_attrib(item); \
613 bool flag; \
614 int ret; \
615 \
616 ret = kstrtobool(page, &flag); \
617 if (ret < 0) \
618 return ret; \
619 da->_name = flag; \
620 return count; \
621 }
622
623 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_fua_write);
624 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_caw);
625 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_3pc);
626 DEF_CONFIGFS_ATTRIB_STORE_BOOL(emulate_pr);
627 DEF_CONFIGFS_ATTRIB_STORE_BOOL(enforce_pr_isids);
628 DEF_CONFIGFS_ATTRIB_STORE_BOOL(is_nonrot);
629
630 #define DEF_CONFIGFS_ATTRIB_STORE_STUB(_name) \
631 static ssize_t _name##_store(struct config_item *item, const char *page,\
632 size_t count) \
633 { \
634 printk_once(KERN_WARNING \
635 "ignoring deprecated %s attribute\n", \
636 __stringify(_name)); \
637 return count; \
638 }
639
640 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_dpo);
641 DEF_CONFIGFS_ATTRIB_STORE_STUB(emulate_fua_read);
642
dev_set_t10_wwn_model_alias(struct se_device * dev)643 static void dev_set_t10_wwn_model_alias(struct se_device *dev)
644 {
645 const char *configname;
646
647 configname = config_item_name(&dev->dev_group.cg_item);
648 if (strlen(configname) >= INQUIRY_MODEL_LEN) {
649 pr_warn("dev[%p]: Backstore name '%s' is too long for "
650 "INQUIRY_MODEL, truncating to 15 characters\n", dev,
651 configname);
652 }
653 /*
654 * XXX We can't use sizeof(dev->t10_wwn.model) (INQUIRY_MODEL_LEN + 1)
655 * here without potentially breaking existing setups, so continue to
656 * truncate one byte shorter than what can be carried in INQUIRY.
657 */
658 strscpy(dev->t10_wwn.model, configname, INQUIRY_MODEL_LEN);
659 }
660
emulate_model_alias_store(struct config_item * item,const char * page,size_t count)661 static ssize_t emulate_model_alias_store(struct config_item *item,
662 const char *page, size_t count)
663 {
664 struct se_dev_attrib *da = to_attrib(item);
665 struct se_device *dev = da->da_dev;
666 bool flag;
667 int ret;
668
669 if (dev->export_count) {
670 pr_err("dev[%p]: Unable to change model alias"
671 " while export_count is %d\n",
672 dev, dev->export_count);
673 return -EINVAL;
674 }
675
676 ret = kstrtobool(page, &flag);
677 if (ret < 0)
678 return ret;
679
680 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
681 if (flag)
682 dev_set_t10_wwn_model_alias(dev);
683 else
684 strscpy(dev->t10_wwn.model, dev->transport->inquiry_prod);
685 da->emulate_model_alias = flag;
686 return count;
687 }
688
emulate_write_cache_store(struct config_item * item,const char * page,size_t count)689 static ssize_t emulate_write_cache_store(struct config_item *item,
690 const char *page, size_t count)
691 {
692 struct se_dev_attrib *da = to_attrib(item);
693 bool flag;
694 int ret;
695
696 ret = kstrtobool(page, &flag);
697 if (ret < 0)
698 return ret;
699
700 if (flag && da->da_dev->transport->get_write_cache) {
701 pr_err("emulate_write_cache not supported for this device\n");
702 return -EINVAL;
703 }
704
705 da->emulate_write_cache = flag;
706 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
707 da->da_dev, flag);
708 return count;
709 }
710
emulate_ua_intlck_ctrl_store(struct config_item * item,const char * page,size_t count)711 static ssize_t emulate_ua_intlck_ctrl_store(struct config_item *item,
712 const char *page, size_t count)
713 {
714 struct se_dev_attrib *da = to_attrib(item);
715 u32 val;
716 int ret;
717
718 ret = kstrtou32(page, 0, &val);
719 if (ret < 0)
720 return ret;
721
722 if (val != TARGET_UA_INTLCK_CTRL_CLEAR
723 && val != TARGET_UA_INTLCK_CTRL_NO_CLEAR
724 && val != TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
725 pr_err("Illegal value %d\n", val);
726 return -EINVAL;
727 }
728
729 if (da->da_dev->export_count) {
730 pr_err("dev[%p]: Unable to change SE Device"
731 " UA_INTRLCK_CTRL while export_count is %d\n",
732 da->da_dev, da->da_dev->export_count);
733 return -EINVAL;
734 }
735 da->emulate_ua_intlck_ctrl = val;
736 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
737 da->da_dev, val);
738 return count;
739 }
740
emulate_tas_store(struct config_item * item,const char * page,size_t count)741 static ssize_t emulate_tas_store(struct config_item *item,
742 const char *page, size_t count)
743 {
744 struct se_dev_attrib *da = to_attrib(item);
745 bool flag;
746 int ret;
747
748 ret = kstrtobool(page, &flag);
749 if (ret < 0)
750 return ret;
751
752 if (da->da_dev->export_count) {
753 pr_err("dev[%p]: Unable to change SE Device TAS while"
754 " export_count is %d\n",
755 da->da_dev, da->da_dev->export_count);
756 return -EINVAL;
757 }
758 da->emulate_tas = flag;
759 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
760 da->da_dev, flag ? "Enabled" : "Disabled");
761
762 return count;
763 }
764
target_try_configure_unmap(struct se_device * dev,const char * config_opt)765 static int target_try_configure_unmap(struct se_device *dev,
766 const char *config_opt)
767 {
768 if (!dev->transport->configure_unmap) {
769 pr_err("Generic Block Discard not supported\n");
770 return -ENOSYS;
771 }
772
773 if (!target_dev_configured(dev)) {
774 pr_err("Generic Block Discard setup for %s requires device to be configured\n",
775 config_opt);
776 return -ENODEV;
777 }
778
779 if (!dev->transport->configure_unmap(dev)) {
780 pr_err("Generic Block Discard setup for %s failed\n",
781 config_opt);
782 return -ENOSYS;
783 }
784
785 return 0;
786 }
787
emulate_tpu_store(struct config_item * item,const char * page,size_t count)788 static ssize_t emulate_tpu_store(struct config_item *item,
789 const char *page, size_t count)
790 {
791 struct se_dev_attrib *da = to_attrib(item);
792 struct se_device *dev = da->da_dev;
793 bool flag;
794 int ret;
795
796 ret = kstrtobool(page, &flag);
797 if (ret < 0)
798 return ret;
799
800 /*
801 * We expect this value to be non-zero when generic Block Layer
802 * Discard supported is detected iblock_create_virtdevice().
803 */
804 if (flag && !da->max_unmap_block_desc_count) {
805 ret = target_try_configure_unmap(dev, "emulate_tpu");
806 if (ret)
807 return ret;
808 }
809
810 da->emulate_tpu = flag;
811 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
812 da->da_dev, flag);
813 return count;
814 }
815
emulate_tpws_store(struct config_item * item,const char * page,size_t count)816 static ssize_t emulate_tpws_store(struct config_item *item,
817 const char *page, size_t count)
818 {
819 struct se_dev_attrib *da = to_attrib(item);
820 struct se_device *dev = da->da_dev;
821 bool flag;
822 int ret;
823
824 ret = kstrtobool(page, &flag);
825 if (ret < 0)
826 return ret;
827
828 /*
829 * We expect this value to be non-zero when generic Block Layer
830 * Discard supported is detected iblock_create_virtdevice().
831 */
832 if (flag && !da->max_unmap_block_desc_count) {
833 ret = target_try_configure_unmap(dev, "emulate_tpws");
834 if (ret)
835 return ret;
836 }
837
838 da->emulate_tpws = flag;
839 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
840 da->da_dev, flag);
841 return count;
842 }
843
pi_prot_type_store(struct config_item * item,const char * page,size_t count)844 static ssize_t pi_prot_type_store(struct config_item *item,
845 const char *page, size_t count)
846 {
847 struct se_dev_attrib *da = to_attrib(item);
848 int old_prot = da->pi_prot_type, ret;
849 struct se_device *dev = da->da_dev;
850 u32 flag;
851
852 ret = kstrtou32(page, 0, &flag);
853 if (ret < 0)
854 return ret;
855
856 if (flag != 0 && flag != 1 && flag != 2 && flag != 3) {
857 pr_err("Illegal value %d for pi_prot_type\n", flag);
858 return -EINVAL;
859 }
860 if (flag == 2) {
861 pr_err("DIF TYPE2 protection currently not supported\n");
862 return -ENOSYS;
863 }
864 if (da->hw_pi_prot_type) {
865 pr_warn("DIF protection enabled on underlying hardware,"
866 " ignoring\n");
867 return count;
868 }
869 if (!dev->transport->init_prot || !dev->transport->free_prot) {
870 /* 0 is only allowed value for non-supporting backends */
871 if (flag == 0)
872 return count;
873
874 pr_err("DIF protection not supported by backend: %s\n",
875 dev->transport->name);
876 return -ENOSYS;
877 }
878 if (!target_dev_configured(dev)) {
879 pr_err("DIF protection requires device to be configured\n");
880 return -ENODEV;
881 }
882 if (dev->export_count) {
883 pr_err("dev[%p]: Unable to change SE Device PROT type while"
884 " export_count is %d\n", dev, dev->export_count);
885 return -EINVAL;
886 }
887
888 da->pi_prot_type = flag;
889
890 if (flag && !old_prot) {
891 ret = dev->transport->init_prot(dev);
892 if (ret) {
893 da->pi_prot_type = old_prot;
894 da->pi_prot_verify = (bool) da->pi_prot_type;
895 return ret;
896 }
897
898 } else if (!flag && old_prot) {
899 dev->transport->free_prot(dev);
900 }
901
902 da->pi_prot_verify = (bool) da->pi_prot_type;
903 pr_debug("dev[%p]: SE Device Protection Type: %d\n", dev, flag);
904 return count;
905 }
906
907 /* always zero, but attr needs to remain RW to avoid userspace breakage */
pi_prot_format_show(struct config_item * item,char * page)908 static ssize_t pi_prot_format_show(struct config_item *item, char *page)
909 {
910 return snprintf(page, PAGE_SIZE, "0\n");
911 }
912
pi_prot_format_store(struct config_item * item,const char * page,size_t count)913 static ssize_t pi_prot_format_store(struct config_item *item,
914 const char *page, size_t count)
915 {
916 struct se_dev_attrib *da = to_attrib(item);
917 struct se_device *dev = da->da_dev;
918 bool flag;
919 int ret;
920
921 ret = kstrtobool(page, &flag);
922 if (ret < 0)
923 return ret;
924
925 if (!flag)
926 return count;
927
928 if (!dev->transport->format_prot) {
929 pr_err("DIF protection format not supported by backend %s\n",
930 dev->transport->name);
931 return -ENOSYS;
932 }
933 if (!target_dev_configured(dev)) {
934 pr_err("DIF protection format requires device to be configured\n");
935 return -ENODEV;
936 }
937 if (dev->export_count) {
938 pr_err("dev[%p]: Unable to format SE Device PROT type while"
939 " export_count is %d\n", dev, dev->export_count);
940 return -EINVAL;
941 }
942
943 ret = dev->transport->format_prot(dev);
944 if (ret)
945 return ret;
946
947 pr_debug("dev[%p]: SE Device Protection Format complete\n", dev);
948 return count;
949 }
950
pi_prot_verify_store(struct config_item * item,const char * page,size_t count)951 static ssize_t pi_prot_verify_store(struct config_item *item,
952 const char *page, size_t count)
953 {
954 struct se_dev_attrib *da = to_attrib(item);
955 bool flag;
956 int ret;
957
958 ret = kstrtobool(page, &flag);
959 if (ret < 0)
960 return ret;
961
962 if (!flag) {
963 da->pi_prot_verify = flag;
964 return count;
965 }
966 if (da->hw_pi_prot_type) {
967 pr_warn("DIF protection enabled on underlying hardware,"
968 " ignoring\n");
969 return count;
970 }
971 if (!da->pi_prot_type) {
972 pr_warn("DIF protection not supported by backend, ignoring\n");
973 return count;
974 }
975 da->pi_prot_verify = flag;
976
977 return count;
978 }
979
force_pr_aptpl_store(struct config_item * item,const char * page,size_t count)980 static ssize_t force_pr_aptpl_store(struct config_item *item,
981 const char *page, size_t count)
982 {
983 struct se_dev_attrib *da = to_attrib(item);
984 bool flag;
985 int ret;
986
987 ret = kstrtobool(page, &flag);
988 if (ret < 0)
989 return ret;
990 if (da->da_dev->export_count) {
991 pr_err("dev[%p]: Unable to set force_pr_aptpl while"
992 " export_count is %d\n",
993 da->da_dev, da->da_dev->export_count);
994 return -EINVAL;
995 }
996
997 da->force_pr_aptpl = flag;
998 pr_debug("dev[%p]: SE Device force_pr_aptpl: %d\n", da->da_dev, flag);
999 return count;
1000 }
1001
emulate_rest_reord_store(struct config_item * item,const char * page,size_t count)1002 static ssize_t emulate_rest_reord_store(struct config_item *item,
1003 const char *page, size_t count)
1004 {
1005 struct se_dev_attrib *da = to_attrib(item);
1006 bool flag;
1007 int ret;
1008
1009 ret = kstrtobool(page, &flag);
1010 if (ret < 0)
1011 return ret;
1012
1013 if (flag != 0) {
1014 printk(KERN_ERR "dev[%p]: SE Device emulation of restricted"
1015 " reordering not implemented\n", da->da_dev);
1016 return -ENOSYS;
1017 }
1018 da->emulate_rest_reord = flag;
1019 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n",
1020 da->da_dev, flag);
1021 return count;
1022 }
1023
unmap_zeroes_data_store(struct config_item * item,const char * page,size_t count)1024 static ssize_t unmap_zeroes_data_store(struct config_item *item,
1025 const char *page, size_t count)
1026 {
1027 struct se_dev_attrib *da = to_attrib(item);
1028 struct se_device *dev = da->da_dev;
1029 bool flag;
1030 int ret;
1031
1032 ret = kstrtobool(page, &flag);
1033 if (ret < 0)
1034 return ret;
1035
1036 if (da->da_dev->export_count) {
1037 pr_err("dev[%p]: Unable to change SE Device"
1038 " unmap_zeroes_data while export_count is %d\n",
1039 da->da_dev, da->da_dev->export_count);
1040 return -EINVAL;
1041 }
1042 /*
1043 * We expect this value to be non-zero when generic Block Layer
1044 * Discard supported is detected iblock_configure_device().
1045 */
1046 if (flag && !da->max_unmap_block_desc_count) {
1047 ret = target_try_configure_unmap(dev, "unmap_zeroes_data");
1048 if (ret)
1049 return ret;
1050 }
1051 da->unmap_zeroes_data = flag;
1052 pr_debug("dev[%p]: SE Device Thin Provisioning LBPRZ bit: %d\n",
1053 da->da_dev, flag);
1054 return count;
1055 }
1056
1057 /*
1058 * Note, this can only be called on unexported SE Device Object.
1059 */
queue_depth_store(struct config_item * item,const char * page,size_t count)1060 static ssize_t queue_depth_store(struct config_item *item,
1061 const char *page, size_t count)
1062 {
1063 struct se_dev_attrib *da = to_attrib(item);
1064 struct se_device *dev = da->da_dev;
1065 u32 val;
1066 int ret;
1067
1068 ret = kstrtou32(page, 0, &val);
1069 if (ret < 0)
1070 return ret;
1071
1072 if (dev->export_count) {
1073 pr_err("dev[%p]: Unable to change SE Device TCQ while"
1074 " export_count is %d\n",
1075 dev, dev->export_count);
1076 return -EINVAL;
1077 }
1078 if (!val) {
1079 pr_err("dev[%p]: Illegal ZERO value for queue_depth\n", dev);
1080 return -EINVAL;
1081 }
1082
1083 if (val > dev->dev_attrib.queue_depth) {
1084 if (val > dev->dev_attrib.hw_queue_depth) {
1085 pr_err("dev[%p]: Passed queue_depth:"
1086 " %u exceeds TCM/SE_Device MAX"
1087 " TCQ: %u\n", dev, val,
1088 dev->dev_attrib.hw_queue_depth);
1089 return -EINVAL;
1090 }
1091 }
1092 da->queue_depth = dev->queue_depth = val;
1093 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n", dev, val);
1094 return count;
1095 }
1096
optimal_sectors_store(struct config_item * item,const char * page,size_t count)1097 static ssize_t optimal_sectors_store(struct config_item *item,
1098 const char *page, size_t count)
1099 {
1100 struct se_dev_attrib *da = to_attrib(item);
1101 u32 val;
1102 int ret;
1103
1104 ret = kstrtou32(page, 0, &val);
1105 if (ret < 0)
1106 return ret;
1107
1108 if (da->da_dev->export_count) {
1109 pr_err("dev[%p]: Unable to change SE Device"
1110 " optimal_sectors while export_count is %d\n",
1111 da->da_dev, da->da_dev->export_count);
1112 return -EINVAL;
1113 }
1114 if (val > da->hw_max_sectors) {
1115 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1116 " greater than hw_max_sectors: %u\n",
1117 da->da_dev, val, da->hw_max_sectors);
1118 return -EINVAL;
1119 }
1120
1121 da->optimal_sectors = val;
1122 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1123 da->da_dev, val);
1124 return count;
1125 }
1126
block_size_store(struct config_item * item,const char * page,size_t count)1127 static ssize_t block_size_store(struct config_item *item,
1128 const char *page, size_t count)
1129 {
1130 struct se_dev_attrib *da = to_attrib(item);
1131 u32 val;
1132 int ret;
1133
1134 ret = kstrtou32(page, 0, &val);
1135 if (ret < 0)
1136 return ret;
1137
1138 if (da->da_dev->export_count) {
1139 pr_err("dev[%p]: Unable to change SE Device block_size"
1140 " while export_count is %d\n",
1141 da->da_dev, da->da_dev->export_count);
1142 return -EINVAL;
1143 }
1144
1145 if (val != 512 && val != 1024 && val != 2048 && val != 4096) {
1146 pr_err("dev[%p]: Illegal value for block_device: %u"
1147 " for SE device, must be 512, 1024, 2048 or 4096\n",
1148 da->da_dev, val);
1149 return -EINVAL;
1150 }
1151
1152 da->block_size = val;
1153
1154 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1155 da->da_dev, val);
1156 return count;
1157 }
1158
alua_support_show(struct config_item * item,char * page)1159 static ssize_t alua_support_show(struct config_item *item, char *page)
1160 {
1161 struct se_dev_attrib *da = to_attrib(item);
1162 u8 flags = da->da_dev->transport_flags;
1163
1164 return snprintf(page, PAGE_SIZE, "%d\n",
1165 flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ? 0 : 1);
1166 }
1167
alua_support_store(struct config_item * item,const char * page,size_t count)1168 static ssize_t alua_support_store(struct config_item *item,
1169 const char *page, size_t count)
1170 {
1171 struct se_dev_attrib *da = to_attrib(item);
1172 struct se_device *dev = da->da_dev;
1173 bool flag, oldflag;
1174 int ret;
1175
1176 ret = kstrtobool(page, &flag);
1177 if (ret < 0)
1178 return ret;
1179
1180 oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA);
1181 if (flag == oldflag)
1182 return count;
1183
1184 if (!(dev->transport->transport_flags_changeable &
1185 TRANSPORT_FLAG_PASSTHROUGH_ALUA)) {
1186 pr_err("dev[%p]: Unable to change SE Device alua_support:"
1187 " alua_support has fixed value\n", dev);
1188 return -ENOSYS;
1189 }
1190
1191 if (flag)
1192 dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1193 else
1194 dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_ALUA;
1195 return count;
1196 }
1197
pgr_support_show(struct config_item * item,char * page)1198 static ssize_t pgr_support_show(struct config_item *item, char *page)
1199 {
1200 struct se_dev_attrib *da = to_attrib(item);
1201 u8 flags = da->da_dev->transport_flags;
1202
1203 return snprintf(page, PAGE_SIZE, "%d\n",
1204 flags & TRANSPORT_FLAG_PASSTHROUGH_PGR ? 0 : 1);
1205 }
1206
pgr_support_store(struct config_item * item,const char * page,size_t count)1207 static ssize_t pgr_support_store(struct config_item *item,
1208 const char *page, size_t count)
1209 {
1210 struct se_dev_attrib *da = to_attrib(item);
1211 struct se_device *dev = da->da_dev;
1212 bool flag, oldflag;
1213 int ret;
1214
1215 ret = kstrtobool(page, &flag);
1216 if (ret < 0)
1217 return ret;
1218
1219 oldflag = !(dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR);
1220 if (flag == oldflag)
1221 return count;
1222
1223 if (!(dev->transport->transport_flags_changeable &
1224 TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
1225 pr_err("dev[%p]: Unable to change SE Device pgr_support:"
1226 " pgr_support has fixed value\n", dev);
1227 return -ENOSYS;
1228 }
1229
1230 if (flag)
1231 dev->transport_flags &= ~TRANSPORT_FLAG_PASSTHROUGH_PGR;
1232 else
1233 dev->transport_flags |= TRANSPORT_FLAG_PASSTHROUGH_PGR;
1234 return count;
1235 }
1236
emulate_rsoc_store(struct config_item * item,const char * page,size_t count)1237 static ssize_t emulate_rsoc_store(struct config_item *item,
1238 const char *page, size_t count)
1239 {
1240 struct se_dev_attrib *da = to_attrib(item);
1241 bool flag;
1242 int ret;
1243
1244 ret = kstrtobool(page, &flag);
1245 if (ret < 0)
1246 return ret;
1247
1248 da->emulate_rsoc = flag;
1249 pr_debug("dev[%p]: SE Device REPORT_SUPPORTED_OPERATION_CODES_EMULATION flag: %d\n",
1250 da->da_dev, flag);
1251 return count;
1252 }
1253
submit_type_store(struct config_item * item,const char * page,size_t count)1254 static ssize_t submit_type_store(struct config_item *item, const char *page,
1255 size_t count)
1256 {
1257 struct se_dev_attrib *da = to_attrib(item);
1258 int ret;
1259 u8 val;
1260
1261 ret = kstrtou8(page, 0, &val);
1262 if (ret < 0)
1263 return ret;
1264
1265 if (val > TARGET_QUEUE_SUBMIT)
1266 return -EINVAL;
1267
1268 da->submit_type = val;
1269 return count;
1270 }
1271
1272 CONFIGFS_ATTR(, emulate_model_alias);
1273 CONFIGFS_ATTR(, emulate_dpo);
1274 CONFIGFS_ATTR(, emulate_fua_write);
1275 CONFIGFS_ATTR(, emulate_fua_read);
1276 CONFIGFS_ATTR(, emulate_write_cache);
1277 CONFIGFS_ATTR(, emulate_ua_intlck_ctrl);
1278 CONFIGFS_ATTR(, emulate_tas);
1279 CONFIGFS_ATTR(, emulate_tpu);
1280 CONFIGFS_ATTR(, emulate_tpws);
1281 CONFIGFS_ATTR(, emulate_caw);
1282 CONFIGFS_ATTR(, emulate_3pc);
1283 CONFIGFS_ATTR(, emulate_pr);
1284 CONFIGFS_ATTR(, emulate_rsoc);
1285 CONFIGFS_ATTR(, pi_prot_type);
1286 CONFIGFS_ATTR_RO(, hw_pi_prot_type);
1287 CONFIGFS_ATTR(, pi_prot_format);
1288 CONFIGFS_ATTR(, pi_prot_verify);
1289 CONFIGFS_ATTR(, enforce_pr_isids);
1290 CONFIGFS_ATTR(, is_nonrot);
1291 CONFIGFS_ATTR(, emulate_rest_reord);
1292 CONFIGFS_ATTR(, force_pr_aptpl);
1293 CONFIGFS_ATTR_RO(, hw_block_size);
1294 CONFIGFS_ATTR(, block_size);
1295 CONFIGFS_ATTR_RO(, hw_max_sectors);
1296 CONFIGFS_ATTR(, optimal_sectors);
1297 CONFIGFS_ATTR_RO(, hw_queue_depth);
1298 CONFIGFS_ATTR(, queue_depth);
1299 CONFIGFS_ATTR(, max_unmap_lba_count);
1300 CONFIGFS_ATTR(, max_unmap_block_desc_count);
1301 CONFIGFS_ATTR(, unmap_granularity);
1302 CONFIGFS_ATTR(, unmap_granularity_alignment);
1303 CONFIGFS_ATTR(, unmap_zeroes_data);
1304 CONFIGFS_ATTR(, max_write_same_len);
1305 CONFIGFS_ATTR(, alua_support);
1306 CONFIGFS_ATTR(, pgr_support);
1307 CONFIGFS_ATTR(, submit_type);
1308 CONFIGFS_ATTR_RO(, atomic_max_len);
1309 CONFIGFS_ATTR_RO(, atomic_alignment);
1310 CONFIGFS_ATTR_RO(, atomic_granularity);
1311 CONFIGFS_ATTR_RO(, atomic_max_with_boundary);
1312 CONFIGFS_ATTR_RO(, atomic_max_boundary);
1313
1314 /*
1315 * dev_attrib attributes for devices using the target core SBC/SPC
1316 * interpreter. Any backend using spc_parse_cdb should be using
1317 * these.
1318 */
1319 struct configfs_attribute *sbc_attrib_attrs[] = {
1320 &attr_emulate_model_alias,
1321 &attr_emulate_dpo,
1322 &attr_emulate_fua_write,
1323 &attr_emulate_fua_read,
1324 &attr_emulate_write_cache,
1325 &attr_emulate_ua_intlck_ctrl,
1326 &attr_emulate_tas,
1327 &attr_emulate_tpu,
1328 &attr_emulate_tpws,
1329 &attr_emulate_caw,
1330 &attr_emulate_3pc,
1331 &attr_emulate_pr,
1332 &attr_pi_prot_type,
1333 &attr_hw_pi_prot_type,
1334 &attr_pi_prot_format,
1335 &attr_pi_prot_verify,
1336 &attr_enforce_pr_isids,
1337 &attr_is_nonrot,
1338 &attr_emulate_rest_reord,
1339 &attr_force_pr_aptpl,
1340 &attr_hw_block_size,
1341 &attr_block_size,
1342 &attr_hw_max_sectors,
1343 &attr_optimal_sectors,
1344 &attr_hw_queue_depth,
1345 &attr_queue_depth,
1346 &attr_max_unmap_lba_count,
1347 &attr_max_unmap_block_desc_count,
1348 &attr_unmap_granularity,
1349 &attr_unmap_granularity_alignment,
1350 &attr_unmap_zeroes_data,
1351 &attr_max_write_same_len,
1352 &attr_alua_support,
1353 &attr_pgr_support,
1354 &attr_emulate_rsoc,
1355 &attr_submit_type,
1356 &attr_atomic_alignment,
1357 &attr_atomic_max_len,
1358 &attr_atomic_granularity,
1359 &attr_atomic_max_with_boundary,
1360 &attr_atomic_max_boundary,
1361 NULL,
1362 };
1363 EXPORT_SYMBOL(sbc_attrib_attrs);
1364
1365 /*
1366 * Minimal dev_attrib attributes for devices passing through CDBs.
1367 * In this case we only provide a few read-only attributes for
1368 * backwards compatibility.
1369 */
1370 struct configfs_attribute *passthrough_attrib_attrs[] = {
1371 &attr_hw_pi_prot_type,
1372 &attr_hw_block_size,
1373 &attr_hw_max_sectors,
1374 &attr_hw_queue_depth,
1375 &attr_emulate_pr,
1376 &attr_alua_support,
1377 &attr_pgr_support,
1378 &attr_submit_type,
1379 NULL,
1380 };
1381 EXPORT_SYMBOL(passthrough_attrib_attrs);
1382
1383 /*
1384 * pr related dev_attrib attributes for devices passing through CDBs,
1385 * but allowing in core pr emulation.
1386 */
1387 struct configfs_attribute *passthrough_pr_attrib_attrs[] = {
1388 &attr_enforce_pr_isids,
1389 &attr_force_pr_aptpl,
1390 NULL,
1391 };
1392 EXPORT_SYMBOL(passthrough_pr_attrib_attrs);
1393
1394 TB_CIT_SETUP_DRV(dev_attrib, NULL, NULL);
1395 TB_CIT_SETUP_DRV(dev_action, NULL, NULL);
1396
1397 /* End functions for struct config_item_type tb_dev_attrib_cit */
1398
1399 /* Start functions for struct config_item_type tb_dev_wwn_cit */
1400
to_t10_wwn(struct config_item * item)1401 static struct t10_wwn *to_t10_wwn(struct config_item *item)
1402 {
1403 return container_of(to_config_group(item), struct t10_wwn, t10_wwn_group);
1404 }
1405
target_check_inquiry_data(char * buf)1406 static ssize_t target_check_inquiry_data(char *buf)
1407 {
1408 size_t len;
1409 int i;
1410
1411 len = strlen(buf);
1412
1413 /*
1414 * SPC 4.3.1:
1415 * ASCII data fields shall contain only ASCII printable characters
1416 * (i.e., code values 20h to 7Eh) and may be terminated with one or
1417 * more ASCII null (00h) characters.
1418 */
1419 for (i = 0; i < len; i++) {
1420 if (buf[i] < 0x20 || buf[i] > 0x7E) {
1421 pr_err("Emulated T10 Inquiry Data contains non-ASCII-printable characters\n");
1422 return -EINVAL;
1423 }
1424 }
1425
1426 return len;
1427 }
1428
1429 /*
1430 * STANDARD and VPD page 0x83 T10 Vendor Identification
1431 */
target_wwn_vendor_id_show(struct config_item * item,char * page)1432 static ssize_t target_wwn_vendor_id_show(struct config_item *item,
1433 char *page)
1434 {
1435 return sprintf(page, "%s\n", &to_t10_wwn(item)->vendor[0]);
1436 }
1437
target_wwn_vendor_id_store(struct config_item * item,const char * page,size_t count)1438 static ssize_t target_wwn_vendor_id_store(struct config_item *item,
1439 const char *page, size_t count)
1440 {
1441 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1442 struct se_device *dev = t10_wwn->t10_dev;
1443 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1444 unsigned char buf[INQUIRY_VENDOR_LEN + 2];
1445 char *stripped = NULL;
1446 ssize_t len;
1447 ssize_t ret;
1448
1449 len = strscpy(buf, page);
1450 if (len > 0) {
1451 /* Strip any newline added from userspace. */
1452 stripped = strstrip(buf);
1453 len = strlen(stripped);
1454 }
1455 if (len < 0 || len > INQUIRY_VENDOR_LEN) {
1456 pr_err("Emulated T10 Vendor Identification exceeds"
1457 " INQUIRY_VENDOR_LEN: " __stringify(INQUIRY_VENDOR_LEN)
1458 "\n");
1459 return -EOVERFLOW;
1460 }
1461
1462 ret = target_check_inquiry_data(stripped);
1463
1464 if (ret < 0)
1465 return ret;
1466
1467 /*
1468 * Check to see if any active exports exist. If they do exist, fail
1469 * here as changing this information on the fly (underneath the
1470 * initiator side OS dependent multipath code) could cause negative
1471 * effects.
1472 */
1473 if (dev->export_count) {
1474 pr_err("Unable to set T10 Vendor Identification while"
1475 " active %d exports exist\n", dev->export_count);
1476 return -EINVAL;
1477 }
1478
1479 BUILD_BUG_ON(sizeof(dev->t10_wwn.vendor) != INQUIRY_VENDOR_LEN + 1);
1480 strscpy(dev->t10_wwn.vendor, stripped);
1481
1482 pr_debug("Target_Core_ConfigFS: Set emulated T10 Vendor Identification:"
1483 " %s\n", dev->t10_wwn.vendor);
1484
1485 return count;
1486 }
1487
target_wwn_product_id_show(struct config_item * item,char * page)1488 static ssize_t target_wwn_product_id_show(struct config_item *item,
1489 char *page)
1490 {
1491 return sprintf(page, "%s\n", &to_t10_wwn(item)->model[0]);
1492 }
1493
target_wwn_product_id_store(struct config_item * item,const char * page,size_t count)1494 static ssize_t target_wwn_product_id_store(struct config_item *item,
1495 const char *page, size_t count)
1496 {
1497 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1498 struct se_device *dev = t10_wwn->t10_dev;
1499 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1500 unsigned char buf[INQUIRY_MODEL_LEN + 2];
1501 char *stripped = NULL;
1502 ssize_t len;
1503 ssize_t ret;
1504
1505 len = strscpy(buf, page);
1506 if (len > 0) {
1507 /* Strip any newline added from userspace. */
1508 stripped = strstrip(buf);
1509 len = strlen(stripped);
1510 }
1511 if (len < 0 || len > INQUIRY_MODEL_LEN) {
1512 pr_err("Emulated T10 Vendor exceeds INQUIRY_MODEL_LEN: "
1513 __stringify(INQUIRY_MODEL_LEN)
1514 "\n");
1515 return -EOVERFLOW;
1516 }
1517
1518 ret = target_check_inquiry_data(stripped);
1519
1520 if (ret < 0)
1521 return ret;
1522
1523 /*
1524 * Check to see if any active exports exist. If they do exist, fail
1525 * here as changing this information on the fly (underneath the
1526 * initiator side OS dependent multipath code) could cause negative
1527 * effects.
1528 */
1529 if (dev->export_count) {
1530 pr_err("Unable to set T10 Model while active %d exports exist\n",
1531 dev->export_count);
1532 return -EINVAL;
1533 }
1534
1535 BUILD_BUG_ON(sizeof(dev->t10_wwn.model) != INQUIRY_MODEL_LEN + 1);
1536 strscpy(dev->t10_wwn.model, stripped);
1537
1538 pr_debug("Target_Core_ConfigFS: Set emulated T10 Model Identification: %s\n",
1539 dev->t10_wwn.model);
1540
1541 return count;
1542 }
1543
target_wwn_revision_show(struct config_item * item,char * page)1544 static ssize_t target_wwn_revision_show(struct config_item *item,
1545 char *page)
1546 {
1547 return sprintf(page, "%s\n", &to_t10_wwn(item)->revision[0]);
1548 }
1549
target_wwn_revision_store(struct config_item * item,const char * page,size_t count)1550 static ssize_t target_wwn_revision_store(struct config_item *item,
1551 const char *page, size_t count)
1552 {
1553 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1554 struct se_device *dev = t10_wwn->t10_dev;
1555 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1556 unsigned char buf[INQUIRY_REVISION_LEN + 2];
1557 char *stripped = NULL;
1558 ssize_t len;
1559 ssize_t ret;
1560
1561 len = strscpy(buf, page);
1562 if (len > 0) {
1563 /* Strip any newline added from userspace. */
1564 stripped = strstrip(buf);
1565 len = strlen(stripped);
1566 }
1567 if (len < 0 || len > INQUIRY_REVISION_LEN) {
1568 pr_err("Emulated T10 Revision exceeds INQUIRY_REVISION_LEN: "
1569 __stringify(INQUIRY_REVISION_LEN)
1570 "\n");
1571 return -EOVERFLOW;
1572 }
1573
1574 ret = target_check_inquiry_data(stripped);
1575
1576 if (ret < 0)
1577 return ret;
1578
1579 /*
1580 * Check to see if any active exports exist. If they do exist, fail
1581 * here as changing this information on the fly (underneath the
1582 * initiator side OS dependent multipath code) could cause negative
1583 * effects.
1584 */
1585 if (dev->export_count) {
1586 pr_err("Unable to set T10 Revision while active %d exports exist\n",
1587 dev->export_count);
1588 return -EINVAL;
1589 }
1590
1591 BUILD_BUG_ON(sizeof(dev->t10_wwn.revision) != INQUIRY_REVISION_LEN + 1);
1592 strscpy(dev->t10_wwn.revision, stripped);
1593
1594 pr_debug("Target_Core_ConfigFS: Set emulated T10 Revision: %s\n",
1595 dev->t10_wwn.revision);
1596
1597 return count;
1598 }
1599
1600 static ssize_t
target_wwn_company_id_show(struct config_item * item,char * page)1601 target_wwn_company_id_show(struct config_item *item,
1602 char *page)
1603 {
1604 return snprintf(page, PAGE_SIZE, "%#08x\n",
1605 to_t10_wwn(item)->company_id);
1606 }
1607
1608 static ssize_t
target_wwn_company_id_store(struct config_item * item,const char * page,size_t count)1609 target_wwn_company_id_store(struct config_item *item,
1610 const char *page, size_t count)
1611 {
1612 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1613 struct se_device *dev = t10_wwn->t10_dev;
1614 u32 val;
1615 int ret;
1616
1617 /*
1618 * The IEEE COMPANY_ID field should contain a 24-bit canonical
1619 * form OUI assigned by the IEEE.
1620 */
1621 ret = kstrtou32(page, 0, &val);
1622 if (ret < 0)
1623 return ret;
1624
1625 if (val >= 0x1000000)
1626 return -EOVERFLOW;
1627
1628 /*
1629 * Check to see if any active exports exist. If they do exist, fail
1630 * here as changing this information on the fly (underneath the
1631 * initiator side OS dependent multipath code) could cause negative
1632 * effects.
1633 */
1634 if (dev->export_count) {
1635 pr_err("Unable to set Company ID while %u exports exist\n",
1636 dev->export_count);
1637 return -EINVAL;
1638 }
1639
1640 t10_wwn->company_id = val;
1641
1642 pr_debug("Target_Core_ConfigFS: Set IEEE Company ID: %#08x\n",
1643 t10_wwn->company_id);
1644
1645 return count;
1646 }
1647
1648 /*
1649 * VPD page 0x80 Unit serial
1650 */
target_wwn_vpd_unit_serial_show(struct config_item * item,char * page)1651 static ssize_t target_wwn_vpd_unit_serial_show(struct config_item *item,
1652 char *page)
1653 {
1654 return sprintf(page, "T10 VPD Unit Serial Number: %s\n",
1655 &to_t10_wwn(item)->unit_serial[0]);
1656 }
1657
target_wwn_vpd_unit_serial_store(struct config_item * item,const char * page,size_t count)1658 static ssize_t target_wwn_vpd_unit_serial_store(struct config_item *item,
1659 const char *page, size_t count)
1660 {
1661 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1662 struct se_device *dev = t10_wwn->t10_dev;
1663 unsigned char buf[INQUIRY_VPD_SERIAL_LEN] = { };
1664
1665 /*
1666 * If Linux/SCSI subsystem_api_t plugin got a VPD Unit Serial
1667 * from the struct scsi_device level firmware, do not allow
1668 * VPD Unit Serial to be emulated.
1669 *
1670 * Note this struct scsi_device could also be emulating VPD
1671 * information from its drivers/scsi LLD. But for now we assume
1672 * it is doing 'the right thing' wrt a world wide unique
1673 * VPD Unit Serial Number that OS dependent multipath can depend on.
1674 */
1675 if (dev->dev_flags & DF_FIRMWARE_VPD_UNIT_SERIAL) {
1676 pr_err("Underlying SCSI device firmware provided VPD"
1677 " Unit Serial, ignoring request\n");
1678 return -EOPNOTSUPP;
1679 }
1680
1681 if (strlen(page) >= INQUIRY_VPD_SERIAL_LEN) {
1682 pr_err("Emulated VPD Unit Serial exceeds"
1683 " INQUIRY_VPD_SERIAL_LEN: %d\n", INQUIRY_VPD_SERIAL_LEN);
1684 return -EOVERFLOW;
1685 }
1686 /*
1687 * Check to see if any active $FABRIC_MOD exports exist. If they
1688 * do exist, fail here as changing this information on the fly
1689 * (underneath the initiator side OS dependent multipath code)
1690 * could cause negative effects.
1691 */
1692 if (dev->export_count) {
1693 pr_err("Unable to set VPD Unit Serial while"
1694 " active %d $FABRIC_MOD exports exist\n",
1695 dev->export_count);
1696 return -EINVAL;
1697 }
1698
1699 /*
1700 * This currently assumes ASCII encoding for emulated VPD Unit Serial.
1701 *
1702 * Also, strip any newline added from the userspace
1703 * echo $UUID > $TARGET/$HBA/$STORAGE_OBJECT/wwn/vpd_unit_serial
1704 */
1705 snprintf(buf, INQUIRY_VPD_SERIAL_LEN, "%s", page);
1706 snprintf(dev->t10_wwn.unit_serial, INQUIRY_VPD_SERIAL_LEN,
1707 "%s", strstrip(buf));
1708 dev->dev_flags |= DF_EMULATED_VPD_UNIT_SERIAL;
1709
1710 pr_debug("Target_Core_ConfigFS: Set emulated VPD Unit Serial:"
1711 " %s\n", dev->t10_wwn.unit_serial);
1712
1713 return count;
1714 }
1715
1716 /*
1717 * VPD page 0x83 Protocol Identifier
1718 */
target_wwn_vpd_protocol_identifier_show(struct config_item * item,char * page)1719 static ssize_t target_wwn_vpd_protocol_identifier_show(struct config_item *item,
1720 char *page)
1721 {
1722 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1723 struct t10_vpd *vpd;
1724 unsigned char buf[VPD_TMP_BUF_SIZE] = { };
1725 ssize_t len = 0;
1726
1727 spin_lock(&t10_wwn->t10_vpd_lock);
1728 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) {
1729 if (!vpd->protocol_identifier_set)
1730 continue;
1731
1732 transport_dump_vpd_proto_id(vpd, buf, VPD_TMP_BUF_SIZE);
1733
1734 if (len + strlen(buf) >= PAGE_SIZE)
1735 break;
1736
1737 len += sprintf(page+len, "%s", buf);
1738 }
1739 spin_unlock(&t10_wwn->t10_vpd_lock);
1740
1741 return len;
1742 }
1743
1744 /*
1745 * Generic wrapper for dumping VPD identifiers by association.
1746 */
1747 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
1748 static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1749 char *page) \
1750 { \
1751 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1752 struct t10_vpd *vpd; \
1753 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1754 ssize_t len = 0; \
1755 \
1756 spin_lock(&t10_wwn->t10_vpd_lock); \
1757 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1758 if (vpd->association != _assoc) \
1759 continue; \
1760 \
1761 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1762 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
1763 if (len + strlen(buf) >= PAGE_SIZE) \
1764 break; \
1765 len += sprintf(page+len, "%s", buf); \
1766 \
1767 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1768 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
1769 if (len + strlen(buf) >= PAGE_SIZE) \
1770 break; \
1771 len += sprintf(page+len, "%s", buf); \
1772 \
1773 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1774 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1775 if (len + strlen(buf) >= PAGE_SIZE) \
1776 break; \
1777 len += sprintf(page+len, "%s", buf); \
1778 } \
1779 spin_unlock(&t10_wwn->t10_vpd_lock); \
1780 \
1781 return len; \
1782 }
1783
1784 /* VPD page 0x83 Association: Logical Unit */
1785 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1786 /* VPD page 0x83 Association: Target Port */
1787 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1788 /* VPD page 0x83 Association: SCSI Target Device */
1789 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1790
1791 CONFIGFS_ATTR(target_wwn_, vendor_id);
1792 CONFIGFS_ATTR(target_wwn_, product_id);
1793 CONFIGFS_ATTR(target_wwn_, revision);
1794 CONFIGFS_ATTR(target_wwn_, company_id);
1795 CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1796 CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1797 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1798 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1799 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1800
1801 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1802 &target_wwn_attr_vendor_id,
1803 &target_wwn_attr_product_id,
1804 &target_wwn_attr_revision,
1805 &target_wwn_attr_company_id,
1806 &target_wwn_attr_vpd_unit_serial,
1807 &target_wwn_attr_vpd_protocol_identifier,
1808 &target_wwn_attr_vpd_assoc_logical_unit,
1809 &target_wwn_attr_vpd_assoc_target_port,
1810 &target_wwn_attr_vpd_assoc_scsi_target_device,
1811 NULL,
1812 };
1813
1814 TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1815
1816 /* End functions for struct config_item_type tb_dev_wwn_cit */
1817
1818 /* Start functions for struct config_item_type tb_dev_pr_cit */
1819
pr_to_dev(struct config_item * item)1820 static struct se_device *pr_to_dev(struct config_item *item)
1821 {
1822 return container_of(to_config_group(item), struct se_device,
1823 dev_pr_group);
1824 }
1825
target_core_dev_pr_show_spc3_res(struct se_device * dev,char * page)1826 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1827 char *page)
1828 {
1829 struct se_node_acl *se_nacl;
1830 struct t10_pr_registration *pr_reg;
1831 char i_buf[PR_REG_ISID_ID_LEN] = { };
1832
1833 pr_reg = dev->dev_pr_res_holder;
1834 if (!pr_reg)
1835 return sprintf(page, "No SPC-3 Reservation holder\n");
1836
1837 se_nacl = pr_reg->pr_reg_nacl;
1838 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1839
1840 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1841 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1842 se_nacl->initiatorname, i_buf);
1843 }
1844
target_core_dev_pr_show_spc2_res(struct se_device * dev,char * page)1845 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1846 char *page)
1847 {
1848 struct se_session *sess = dev->reservation_holder;
1849 struct se_node_acl *se_nacl;
1850 ssize_t len;
1851
1852 if (sess) {
1853 se_nacl = sess->se_node_acl;
1854 len = sprintf(page,
1855 "SPC-2 Reservation: %s Initiator: %s\n",
1856 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1857 se_nacl->initiatorname);
1858 } else {
1859 len = sprintf(page, "No SPC-2 Reservation holder\n");
1860 }
1861 return len;
1862 }
1863
target_pr_res_holder_show(struct config_item * item,char * page)1864 static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1865 {
1866 struct se_device *dev = pr_to_dev(item);
1867 int ret;
1868
1869 if (!dev->dev_attrib.emulate_pr)
1870 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1871
1872 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1873 return sprintf(page, "Passthrough\n");
1874
1875 spin_lock(&dev->dev_reservation_lock);
1876 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1877 ret = target_core_dev_pr_show_spc2_res(dev, page);
1878 else
1879 ret = target_core_dev_pr_show_spc3_res(dev, page);
1880 spin_unlock(&dev->dev_reservation_lock);
1881 return ret;
1882 }
1883
target_pr_res_pr_all_tgt_pts_show(struct config_item * item,char * page)1884 static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1885 char *page)
1886 {
1887 struct se_device *dev = pr_to_dev(item);
1888 ssize_t len = 0;
1889
1890 spin_lock(&dev->dev_reservation_lock);
1891 if (!dev->dev_pr_res_holder) {
1892 len = sprintf(page, "No SPC-3 Reservation holder\n");
1893 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1894 len = sprintf(page, "SPC-3 Reservation: All Target"
1895 " Ports registration\n");
1896 } else {
1897 len = sprintf(page, "SPC-3 Reservation: Single"
1898 " Target Port registration\n");
1899 }
1900
1901 spin_unlock(&dev->dev_reservation_lock);
1902 return len;
1903 }
1904
target_pr_res_pr_generation_show(struct config_item * item,char * page)1905 static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1906 char *page)
1907 {
1908 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1909 }
1910
1911
target_pr_res_pr_holder_tg_port_show(struct config_item * item,char * page)1912 static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1913 char *page)
1914 {
1915 struct se_device *dev = pr_to_dev(item);
1916 struct se_node_acl *se_nacl;
1917 struct se_portal_group *se_tpg;
1918 struct t10_pr_registration *pr_reg;
1919 const struct target_core_fabric_ops *tfo;
1920 ssize_t len = 0;
1921
1922 spin_lock(&dev->dev_reservation_lock);
1923 pr_reg = dev->dev_pr_res_holder;
1924 if (!pr_reg) {
1925 len = sprintf(page, "No SPC-3 Reservation holder\n");
1926 goto out_unlock;
1927 }
1928
1929 se_nacl = pr_reg->pr_reg_nacl;
1930 se_tpg = se_nacl->se_tpg;
1931 tfo = se_tpg->se_tpg_tfo;
1932
1933 len += sprintf(page+len, "SPC-3 Reservation: %s"
1934 " Target Node Endpoint: %s\n", tfo->fabric_name,
1935 tfo->tpg_get_wwn(se_tpg));
1936 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1937 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1938 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1939 tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
1940 tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1941
1942 out_unlock:
1943 spin_unlock(&dev->dev_reservation_lock);
1944 return len;
1945 }
1946
1947
target_pr_res_pr_registered_i_pts_show(struct config_item * item,char * page)1948 static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1949 char *page)
1950 {
1951 struct se_device *dev = pr_to_dev(item);
1952 const struct target_core_fabric_ops *tfo;
1953 struct t10_pr_registration *pr_reg;
1954 unsigned char buf[384];
1955 char i_buf[PR_REG_ISID_ID_LEN];
1956 ssize_t len = 0;
1957 int reg_count = 0;
1958
1959 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
1960
1961 spin_lock(&dev->t10_pr.registration_lock);
1962 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1963 pr_reg_list) {
1964
1965 memset(buf, 0, 384);
1966 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1967 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1968 core_pr_dump_initiator_port(pr_reg, i_buf,
1969 PR_REG_ISID_ID_LEN);
1970 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
1971 tfo->fabric_name,
1972 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
1973 pr_reg->pr_res_generation);
1974
1975 if (len + strlen(buf) >= PAGE_SIZE)
1976 break;
1977
1978 len += sprintf(page+len, "%s", buf);
1979 reg_count++;
1980 }
1981 spin_unlock(&dev->t10_pr.registration_lock);
1982
1983 if (!reg_count)
1984 len += sprintf(page+len, "None\n");
1985
1986 return len;
1987 }
1988
target_pr_res_pr_type_show(struct config_item * item,char * page)1989 static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
1990 {
1991 struct se_device *dev = pr_to_dev(item);
1992 struct t10_pr_registration *pr_reg;
1993 ssize_t len = 0;
1994
1995 spin_lock(&dev->dev_reservation_lock);
1996 pr_reg = dev->dev_pr_res_holder;
1997 if (pr_reg) {
1998 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
1999 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
2000 } else {
2001 len = sprintf(page, "No SPC-3 Reservation holder\n");
2002 }
2003
2004 spin_unlock(&dev->dev_reservation_lock);
2005 return len;
2006 }
2007
target_pr_res_type_show(struct config_item * item,char * page)2008 static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
2009 {
2010 struct se_device *dev = pr_to_dev(item);
2011
2012 if (!dev->dev_attrib.emulate_pr)
2013 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
2014 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
2015 return sprintf(page, "SPC_PASSTHROUGH\n");
2016 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
2017 return sprintf(page, "SPC2_RESERVATIONS\n");
2018
2019 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
2020 }
2021
target_pr_res_aptpl_active_show(struct config_item * item,char * page)2022 static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
2023 char *page)
2024 {
2025 struct se_device *dev = pr_to_dev(item);
2026
2027 if (!dev->dev_attrib.emulate_pr ||
2028 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
2029 return 0;
2030
2031 return sprintf(page, "APTPL Bit Status: %s\n",
2032 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
2033 }
2034
target_pr_res_aptpl_metadata_show(struct config_item * item,char * page)2035 static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
2036 char *page)
2037 {
2038 struct se_device *dev = pr_to_dev(item);
2039
2040 if (!dev->dev_attrib.emulate_pr ||
2041 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
2042 return 0;
2043
2044 return sprintf(page, "Ready to process PR APTPL metadata..\n");
2045 }
2046
2047 enum {
2048 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
2049 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
2050 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
2051 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
2052 };
2053
2054 static match_table_t tokens = {
2055 {Opt_initiator_fabric, "initiator_fabric=%s"},
2056 {Opt_initiator_node, "initiator_node=%s"},
2057 {Opt_initiator_sid, "initiator_sid=%s"},
2058 {Opt_sa_res_key, "sa_res_key=%s"},
2059 {Opt_res_holder, "res_holder=%d"},
2060 {Opt_res_type, "res_type=%d"},
2061 {Opt_res_scope, "res_scope=%d"},
2062 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
2063 {Opt_mapped_lun, "mapped_lun=%u"},
2064 {Opt_target_fabric, "target_fabric=%s"},
2065 {Opt_target_node, "target_node=%s"},
2066 {Opt_tpgt, "tpgt=%d"},
2067 {Opt_port_rtpi, "port_rtpi=%d"},
2068 {Opt_target_lun, "target_lun=%u"},
2069 {Opt_err, NULL}
2070 };
2071
target_pr_res_aptpl_metadata_store(struct config_item * item,const char * page,size_t count)2072 static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
2073 const char *page, size_t count)
2074 {
2075 struct se_device *dev = pr_to_dev(item);
2076 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
2077 unsigned char *t_fabric = NULL, *t_port = NULL;
2078 char *orig, *ptr, *opts;
2079 substring_t args[MAX_OPT_ARGS];
2080 unsigned long long tmp_ll;
2081 u64 sa_res_key = 0;
2082 u64 mapped_lun = 0, target_lun = 0;
2083 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
2084 u16 tpgt = 0;
2085 u8 type = 0;
2086
2087 if (!dev->dev_attrib.emulate_pr ||
2088 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
2089 return count;
2090 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
2091 return count;
2092
2093 if (dev->export_count) {
2094 pr_debug("Unable to process APTPL metadata while"
2095 " active fabric exports exist\n");
2096 return -EINVAL;
2097 }
2098
2099 opts = kstrdup(page, GFP_KERNEL);
2100 if (!opts)
2101 return -ENOMEM;
2102
2103 orig = opts;
2104 while ((ptr = strsep(&opts, ",\n")) != NULL) {
2105 if (!*ptr)
2106 continue;
2107
2108 token = match_token(ptr, tokens, args);
2109 switch (token) {
2110 case Opt_initiator_fabric:
2111 i_fabric = match_strdup(args);
2112 if (!i_fabric) {
2113 ret = -ENOMEM;
2114 goto out;
2115 }
2116 break;
2117 case Opt_initiator_node:
2118 i_port = match_strdup(args);
2119 if (!i_port) {
2120 ret = -ENOMEM;
2121 goto out;
2122 }
2123 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
2124 pr_err("APTPL metadata initiator_node="
2125 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2126 PR_APTPL_MAX_IPORT_LEN);
2127 ret = -EINVAL;
2128 break;
2129 }
2130 break;
2131 case Opt_initiator_sid:
2132 isid = match_strdup(args);
2133 if (!isid) {
2134 ret = -ENOMEM;
2135 goto out;
2136 }
2137 if (strlen(isid) >= PR_REG_ISID_LEN) {
2138 pr_err("APTPL metadata initiator_isid"
2139 "= exceeds PR_REG_ISID_LEN: %d\n",
2140 PR_REG_ISID_LEN);
2141 ret = -EINVAL;
2142 break;
2143 }
2144 break;
2145 case Opt_sa_res_key:
2146 ret = match_u64(args, &tmp_ll);
2147 if (ret < 0) {
2148 pr_err("kstrtoull() failed for sa_res_key=\n");
2149 goto out;
2150 }
2151 sa_res_key = (u64)tmp_ll;
2152 break;
2153 /*
2154 * PR APTPL Metadata for Reservation
2155 */
2156 case Opt_res_holder:
2157 ret = match_int(args, &arg);
2158 if (ret)
2159 goto out;
2160 res_holder = arg;
2161 break;
2162 case Opt_res_type:
2163 ret = match_int(args, &arg);
2164 if (ret)
2165 goto out;
2166 type = (u8)arg;
2167 break;
2168 case Opt_res_scope:
2169 ret = match_int(args, &arg);
2170 if (ret)
2171 goto out;
2172 break;
2173 case Opt_res_all_tg_pt:
2174 ret = match_int(args, &arg);
2175 if (ret)
2176 goto out;
2177 all_tg_pt = (int)arg;
2178 break;
2179 case Opt_mapped_lun:
2180 ret = match_u64(args, &tmp_ll);
2181 if (ret)
2182 goto out;
2183 mapped_lun = (u64)tmp_ll;
2184 break;
2185 /*
2186 * PR APTPL Metadata for Target Port
2187 */
2188 case Opt_target_fabric:
2189 t_fabric = match_strdup(args);
2190 if (!t_fabric) {
2191 ret = -ENOMEM;
2192 goto out;
2193 }
2194 break;
2195 case Opt_target_node:
2196 t_port = match_strdup(args);
2197 if (!t_port) {
2198 ret = -ENOMEM;
2199 goto out;
2200 }
2201 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
2202 pr_err("APTPL metadata target_node="
2203 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2204 PR_APTPL_MAX_TPORT_LEN);
2205 ret = -EINVAL;
2206 break;
2207 }
2208 break;
2209 case Opt_tpgt:
2210 ret = match_int(args, &arg);
2211 if (ret)
2212 goto out;
2213 tpgt = (u16)arg;
2214 break;
2215 case Opt_port_rtpi:
2216 ret = match_int(args, &arg);
2217 if (ret)
2218 goto out;
2219 break;
2220 case Opt_target_lun:
2221 ret = match_u64(args, &tmp_ll);
2222 if (ret)
2223 goto out;
2224 target_lun = (u64)tmp_ll;
2225 break;
2226 default:
2227 break;
2228 }
2229 }
2230
2231 if (!i_port || !t_port || !sa_res_key) {
2232 pr_err("Illegal parameters for APTPL registration\n");
2233 ret = -EINVAL;
2234 goto out;
2235 }
2236
2237 if (res_holder && !(type)) {
2238 pr_err("Illegal PR type: 0x%02x for reservation"
2239 " holder\n", type);
2240 ret = -EINVAL;
2241 goto out;
2242 }
2243
2244 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2245 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2246 res_holder, all_tg_pt, type);
2247 out:
2248 kfree(i_fabric);
2249 kfree(i_port);
2250 kfree(isid);
2251 kfree(t_fabric);
2252 kfree(t_port);
2253 kfree(orig);
2254 return (ret == 0) ? count : ret;
2255 }
2256
2257
2258 CONFIGFS_ATTR_RO(target_pr_, res_holder);
2259 CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
2260 CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
2261 CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
2262 CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
2263 CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
2264 CONFIGFS_ATTR_RO(target_pr_, res_type);
2265 CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
2266 CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2267
2268 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
2269 &target_pr_attr_res_holder,
2270 &target_pr_attr_res_pr_all_tgt_pts,
2271 &target_pr_attr_res_pr_generation,
2272 &target_pr_attr_res_pr_holder_tg_port,
2273 &target_pr_attr_res_pr_registered_i_pts,
2274 &target_pr_attr_res_pr_type,
2275 &target_pr_attr_res_type,
2276 &target_pr_attr_res_aptpl_active,
2277 &target_pr_attr_res_aptpl_metadata,
2278 NULL,
2279 };
2280
2281 TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2282
2283 /* End functions for struct config_item_type tb_dev_pr_cit */
2284
2285 /* Start functions for struct config_item_type tb_dev_cit */
2286
to_device(struct config_item * item)2287 static inline struct se_device *to_device(struct config_item *item)
2288 {
2289 return container_of(to_config_group(item), struct se_device, dev_group);
2290 }
2291
target_dev_info_show(struct config_item * item,char * page)2292 static ssize_t target_dev_info_show(struct config_item *item, char *page)
2293 {
2294 struct se_device *dev = to_device(item);
2295 int bl = 0;
2296 ssize_t read_bytes = 0;
2297
2298 transport_dump_dev_state(dev, page, &bl);
2299 read_bytes += bl;
2300 read_bytes += dev->transport->show_configfs_dev_params(dev,
2301 page+read_bytes);
2302 return read_bytes;
2303 }
2304
target_dev_control_store(struct config_item * item,const char * page,size_t count)2305 static ssize_t target_dev_control_store(struct config_item *item,
2306 const char *page, size_t count)
2307 {
2308 struct se_device *dev = to_device(item);
2309
2310 return dev->transport->set_configfs_dev_params(dev, page, count);
2311 }
2312
target_dev_alias_show(struct config_item * item,char * page)2313 static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2314 {
2315 struct se_device *dev = to_device(item);
2316
2317 if (!(dev->dev_flags & DF_USING_ALIAS))
2318 return 0;
2319
2320 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2321 }
2322
target_dev_alias_store(struct config_item * item,const char * page,size_t count)2323 static ssize_t target_dev_alias_store(struct config_item *item,
2324 const char *page, size_t count)
2325 {
2326 struct se_device *dev = to_device(item);
2327 struct se_hba *hba = dev->se_hba;
2328 ssize_t read_bytes;
2329
2330 if (count > (SE_DEV_ALIAS_LEN-1)) {
2331 pr_err("alias count: %d exceeds"
2332 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2333 SE_DEV_ALIAS_LEN-1);
2334 return -EINVAL;
2335 }
2336
2337 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
2338 if (!read_bytes)
2339 return -EINVAL;
2340 if (dev->dev_alias[read_bytes - 1] == '\n')
2341 dev->dev_alias[read_bytes - 1] = '\0';
2342
2343 dev->dev_flags |= DF_USING_ALIAS;
2344
2345 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2346 config_item_name(&hba->hba_group.cg_item),
2347 config_item_name(&dev->dev_group.cg_item),
2348 dev->dev_alias);
2349
2350 return read_bytes;
2351 }
2352
target_dev_udev_path_show(struct config_item * item,char * page)2353 static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2354 {
2355 struct se_device *dev = to_device(item);
2356
2357 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2358 return 0;
2359
2360 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2361 }
2362
target_dev_udev_path_store(struct config_item * item,const char * page,size_t count)2363 static ssize_t target_dev_udev_path_store(struct config_item *item,
2364 const char *page, size_t count)
2365 {
2366 struct se_device *dev = to_device(item);
2367 struct se_hba *hba = dev->se_hba;
2368 ssize_t read_bytes;
2369
2370 if (count > (SE_UDEV_PATH_LEN-1)) {
2371 pr_err("udev_path count: %d exceeds"
2372 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2373 SE_UDEV_PATH_LEN-1);
2374 return -EINVAL;
2375 }
2376
2377 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2378 "%s", page);
2379 if (!read_bytes)
2380 return -EINVAL;
2381 if (dev->udev_path[read_bytes - 1] == '\n')
2382 dev->udev_path[read_bytes - 1] = '\0';
2383
2384 dev->dev_flags |= DF_USING_UDEV_PATH;
2385
2386 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2387 config_item_name(&hba->hba_group.cg_item),
2388 config_item_name(&dev->dev_group.cg_item),
2389 dev->udev_path);
2390
2391 return read_bytes;
2392 }
2393
target_dev_enable_show(struct config_item * item,char * page)2394 static ssize_t target_dev_enable_show(struct config_item *item, char *page)
2395 {
2396 struct se_device *dev = to_device(item);
2397
2398 return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
2399 }
2400
target_dev_enable_store(struct config_item * item,const char * page,size_t count)2401 static ssize_t target_dev_enable_store(struct config_item *item,
2402 const char *page, size_t count)
2403 {
2404 struct se_device *dev = to_device(item);
2405 char *ptr;
2406 int ret;
2407
2408 ptr = strstr(page, "1");
2409 if (!ptr) {
2410 pr_err("For dev_enable ops, only valid value"
2411 " is \"1\"\n");
2412 return -EINVAL;
2413 }
2414
2415 ret = target_configure_device(dev);
2416 if (ret)
2417 return ret;
2418 return count;
2419 }
2420
target_dev_alua_lu_gp_show(struct config_item * item,char * page)2421 static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2422 {
2423 struct se_device *dev = to_device(item);
2424 struct config_item *lu_ci;
2425 struct t10_alua_lu_gp *lu_gp;
2426 struct t10_alua_lu_gp_member *lu_gp_mem;
2427 ssize_t len = 0;
2428
2429 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2430 if (!lu_gp_mem)
2431 return 0;
2432
2433 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2434 lu_gp = lu_gp_mem->lu_gp;
2435 if (lu_gp) {
2436 lu_ci = &lu_gp->lu_gp_group.cg_item;
2437 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2438 config_item_name(lu_ci), lu_gp->lu_gp_id);
2439 }
2440 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2441
2442 return len;
2443 }
2444
target_dev_alua_lu_gp_store(struct config_item * item,const char * page,size_t count)2445 static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
2446 const char *page, size_t count)
2447 {
2448 struct se_device *dev = to_device(item);
2449 struct se_hba *hba = dev->se_hba;
2450 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2451 struct t10_alua_lu_gp_member *lu_gp_mem;
2452 unsigned char buf[LU_GROUP_NAME_BUF] = { };
2453 int move = 0;
2454
2455 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2456 if (!lu_gp_mem)
2457 return count;
2458
2459 if (count > LU_GROUP_NAME_BUF) {
2460 pr_err("ALUA LU Group Alias too large!\n");
2461 return -EINVAL;
2462 }
2463 memcpy(buf, page, count);
2464 /*
2465 * Any ALUA logical unit alias besides "NULL" means we will be
2466 * making a new group association.
2467 */
2468 if (strcmp(strstrip(buf), "NULL")) {
2469 /*
2470 * core_alua_get_lu_gp_by_name() will increment reference to
2471 * struct t10_alua_lu_gp. This reference is released with
2472 * core_alua_get_lu_gp_by_name below().
2473 */
2474 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
2475 if (!lu_gp_new)
2476 return -ENODEV;
2477 }
2478
2479 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2480 lu_gp = lu_gp_mem->lu_gp;
2481 if (lu_gp) {
2482 /*
2483 * Clearing an existing lu_gp association, and replacing
2484 * with NULL
2485 */
2486 if (!lu_gp_new) {
2487 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2488 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2489 " %hu\n",
2490 config_item_name(&hba->hba_group.cg_item),
2491 config_item_name(&dev->dev_group.cg_item),
2492 config_item_name(&lu_gp->lu_gp_group.cg_item),
2493 lu_gp->lu_gp_id);
2494
2495 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2496 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2497
2498 return count;
2499 }
2500 /*
2501 * Removing existing association of lu_gp_mem with lu_gp
2502 */
2503 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2504 move = 1;
2505 }
2506 /*
2507 * Associate lu_gp_mem with lu_gp_new.
2508 */
2509 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2510 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2511
2512 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2513 " core/alua/lu_gps/%s, ID: %hu\n",
2514 (move) ? "Moving" : "Adding",
2515 config_item_name(&hba->hba_group.cg_item),
2516 config_item_name(&dev->dev_group.cg_item),
2517 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2518 lu_gp_new->lu_gp_id);
2519
2520 core_alua_put_lu_gp_from_name(lu_gp_new);
2521 return count;
2522 }
2523
target_dev_lba_map_show(struct config_item * item,char * page)2524 static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2525 {
2526 struct se_device *dev = to_device(item);
2527 struct t10_alua_lba_map *map;
2528 struct t10_alua_lba_map_member *mem;
2529 char *b = page;
2530 int bl = 0;
2531 char state;
2532
2533 spin_lock(&dev->t10_alua.lba_map_lock);
2534 if (!list_empty(&dev->t10_alua.lba_map_list))
2535 bl += sprintf(b + bl, "%u %u\n",
2536 dev->t10_alua.lba_map_segment_size,
2537 dev->t10_alua.lba_map_segment_multiplier);
2538 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2539 bl += sprintf(b + bl, "%llu %llu",
2540 map->lba_map_first_lba, map->lba_map_last_lba);
2541 list_for_each_entry(mem, &map->lba_map_mem_list,
2542 lba_map_mem_list) {
2543 switch (mem->lba_map_mem_alua_state) {
2544 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2545 state = 'O';
2546 break;
2547 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2548 state = 'A';
2549 break;
2550 case ALUA_ACCESS_STATE_STANDBY:
2551 state = 'S';
2552 break;
2553 case ALUA_ACCESS_STATE_UNAVAILABLE:
2554 state = 'U';
2555 break;
2556 default:
2557 state = '.';
2558 break;
2559 }
2560 bl += sprintf(b + bl, " %d:%c",
2561 mem->lba_map_mem_alua_pg_id, state);
2562 }
2563 bl += sprintf(b + bl, "\n");
2564 }
2565 spin_unlock(&dev->t10_alua.lba_map_lock);
2566 return bl;
2567 }
2568
target_dev_lba_map_store(struct config_item * item,const char * page,size_t count)2569 static ssize_t target_dev_lba_map_store(struct config_item *item,
2570 const char *page, size_t count)
2571 {
2572 struct se_device *dev = to_device(item);
2573 struct t10_alua_lba_map *lba_map = NULL;
2574 struct list_head lba_list;
2575 char *map_entries, *orig, *ptr;
2576 char state;
2577 int pg_num = -1, pg;
2578 int ret = 0, num = 0, pg_id, alua_state;
2579 unsigned long start_lba = -1, end_lba = -1;
2580 unsigned long segment_size = -1, segment_mult = -1;
2581
2582 orig = map_entries = kstrdup(page, GFP_KERNEL);
2583 if (!map_entries)
2584 return -ENOMEM;
2585
2586 INIT_LIST_HEAD(&lba_list);
2587 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2588 if (!*ptr)
2589 continue;
2590
2591 if (num == 0) {
2592 if (sscanf(ptr, "%lu %lu\n",
2593 &segment_size, &segment_mult) != 2) {
2594 pr_err("Invalid line %d\n", num);
2595 ret = -EINVAL;
2596 break;
2597 }
2598 num++;
2599 continue;
2600 }
2601 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2602 pr_err("Invalid line %d\n", num);
2603 ret = -EINVAL;
2604 break;
2605 }
2606 ptr = strchr(ptr, ' ');
2607 if (!ptr) {
2608 pr_err("Invalid line %d, missing end lba\n", num);
2609 ret = -EINVAL;
2610 break;
2611 }
2612 ptr++;
2613 ptr = strchr(ptr, ' ');
2614 if (!ptr) {
2615 pr_err("Invalid line %d, missing state definitions\n",
2616 num);
2617 ret = -EINVAL;
2618 break;
2619 }
2620 ptr++;
2621 lba_map = core_alua_allocate_lba_map(&lba_list,
2622 start_lba, end_lba);
2623 if (IS_ERR(lba_map)) {
2624 ret = PTR_ERR(lba_map);
2625 break;
2626 }
2627 pg = 0;
2628 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2629 switch (state) {
2630 case 'O':
2631 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2632 break;
2633 case 'A':
2634 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2635 break;
2636 case 'S':
2637 alua_state = ALUA_ACCESS_STATE_STANDBY;
2638 break;
2639 case 'U':
2640 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2641 break;
2642 default:
2643 pr_err("Invalid ALUA state '%c'\n", state);
2644 ret = -EINVAL;
2645 goto out;
2646 }
2647
2648 ret = core_alua_allocate_lba_map_mem(lba_map,
2649 pg_id, alua_state);
2650 if (ret) {
2651 pr_err("Invalid target descriptor %d:%c "
2652 "at line %d\n",
2653 pg_id, state, num);
2654 break;
2655 }
2656 pg++;
2657 ptr = strchr(ptr, ' ');
2658 if (ptr)
2659 ptr++;
2660 else
2661 break;
2662 }
2663 if (pg_num == -1)
2664 pg_num = pg;
2665 else if (pg != pg_num) {
2666 pr_err("Only %d from %d port groups definitions "
2667 "at line %d\n", pg, pg_num, num);
2668 ret = -EINVAL;
2669 break;
2670 }
2671 num++;
2672 }
2673 out:
2674 if (ret) {
2675 core_alua_free_lba_map(&lba_list);
2676 count = ret;
2677 } else
2678 core_alua_set_lba_map(dev, &lba_list,
2679 segment_size, segment_mult);
2680 kfree(orig);
2681 return count;
2682 }
2683
2684 CONFIGFS_ATTR_RO(target_dev_, info);
2685 CONFIGFS_ATTR_WO(target_dev_, control);
2686 CONFIGFS_ATTR(target_dev_, alias);
2687 CONFIGFS_ATTR(target_dev_, udev_path);
2688 CONFIGFS_ATTR(target_dev_, enable);
2689 CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2690 CONFIGFS_ATTR(target_dev_, lba_map);
2691
2692 static struct configfs_attribute *target_core_dev_attrs[] = {
2693 &target_dev_attr_info,
2694 &target_dev_attr_control,
2695 &target_dev_attr_alias,
2696 &target_dev_attr_udev_path,
2697 &target_dev_attr_enable,
2698 &target_dev_attr_alua_lu_gp,
2699 &target_dev_attr_lba_map,
2700 NULL,
2701 };
2702
target_core_dev_release(struct config_item * item)2703 static void target_core_dev_release(struct config_item *item)
2704 {
2705 struct config_group *dev_cg = to_config_group(item);
2706 struct se_device *dev =
2707 container_of(dev_cg, struct se_device, dev_group);
2708
2709 target_free_device(dev);
2710 }
2711
2712 /*
2713 * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2714 * within target_fabric_port_link()
2715 */
2716 struct configfs_item_operations target_core_dev_item_ops = {
2717 .release = target_core_dev_release,
2718 };
2719
2720 TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2721
2722 /* End functions for struct config_item_type tb_dev_cit */
2723
2724 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2725
to_lu_gp(struct config_item * item)2726 static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2727 {
2728 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2729 lu_gp_group);
2730 }
2731
target_lu_gp_lu_gp_id_show(struct config_item * item,char * page)2732 static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2733 {
2734 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2735
2736 if (!lu_gp->lu_gp_valid_id)
2737 return 0;
2738 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2739 }
2740
target_lu_gp_lu_gp_id_store(struct config_item * item,const char * page,size_t count)2741 static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2742 const char *page, size_t count)
2743 {
2744 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2745 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2746 unsigned long lu_gp_id;
2747 int ret;
2748
2749 ret = kstrtoul(page, 0, &lu_gp_id);
2750 if (ret < 0) {
2751 pr_err("kstrtoul() returned %d for"
2752 " lu_gp_id\n", ret);
2753 return ret;
2754 }
2755 if (lu_gp_id > 0x0000ffff) {
2756 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2757 " 0x0000ffff\n", lu_gp_id);
2758 return -EINVAL;
2759 }
2760
2761 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2762 if (ret < 0)
2763 return -EINVAL;
2764
2765 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2766 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2767 config_item_name(&alua_lu_gp_cg->cg_item),
2768 lu_gp->lu_gp_id);
2769
2770 return count;
2771 }
2772
target_lu_gp_members_show(struct config_item * item,char * page)2773 static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2774 {
2775 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2776 struct t10_alua_lu_gp_member *lu_gp_mem;
2777 const char *const end = page + PAGE_SIZE;
2778 char *cur = page;
2779
2780 spin_lock(&lu_gp->lu_gp_lock);
2781 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2782 struct se_device *dev = lu_gp_mem->lu_gp_mem_dev;
2783 struct se_hba *hba = dev->se_hba;
2784
2785 cur += scnprintf(cur, end - cur, "%s/%s\n",
2786 config_item_name(&hba->hba_group.cg_item),
2787 config_item_name(&dev->dev_group.cg_item));
2788 if (WARN_ON_ONCE(cur >= end))
2789 break;
2790 }
2791 spin_unlock(&lu_gp->lu_gp_lock);
2792
2793 return cur - page;
2794 }
2795
2796 CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2797 CONFIGFS_ATTR_RO(target_lu_gp_, members);
2798
2799 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2800 &target_lu_gp_attr_lu_gp_id,
2801 &target_lu_gp_attr_members,
2802 NULL,
2803 };
2804
target_core_alua_lu_gp_release(struct config_item * item)2805 static void target_core_alua_lu_gp_release(struct config_item *item)
2806 {
2807 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2808 struct t10_alua_lu_gp, lu_gp_group);
2809
2810 core_alua_free_lu_gp(lu_gp);
2811 }
2812
2813 static struct configfs_item_operations target_core_alua_lu_gp_ops = {
2814 .release = target_core_alua_lu_gp_release,
2815 };
2816
2817 static const struct config_item_type target_core_alua_lu_gp_cit = {
2818 .ct_item_ops = &target_core_alua_lu_gp_ops,
2819 .ct_attrs = target_core_alua_lu_gp_attrs,
2820 .ct_owner = THIS_MODULE,
2821 };
2822
2823 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2824
2825 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2826
target_core_alua_create_lu_gp(struct config_group * group,const char * name)2827 static struct config_group *target_core_alua_create_lu_gp(
2828 struct config_group *group,
2829 const char *name)
2830 {
2831 struct t10_alua_lu_gp *lu_gp;
2832 struct config_group *alua_lu_gp_cg = NULL;
2833 struct config_item *alua_lu_gp_ci = NULL;
2834
2835 lu_gp = core_alua_allocate_lu_gp(name, 0);
2836 if (IS_ERR(lu_gp))
2837 return NULL;
2838
2839 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2840 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2841
2842 config_group_init_type_name(alua_lu_gp_cg, name,
2843 &target_core_alua_lu_gp_cit);
2844
2845 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2846 " Group: core/alua/lu_gps/%s\n",
2847 config_item_name(alua_lu_gp_ci));
2848
2849 return alua_lu_gp_cg;
2850
2851 }
2852
target_core_alua_drop_lu_gp(struct config_group * group,struct config_item * item)2853 static void target_core_alua_drop_lu_gp(
2854 struct config_group *group,
2855 struct config_item *item)
2856 {
2857 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2858 struct t10_alua_lu_gp, lu_gp_group);
2859
2860 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2861 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2862 config_item_name(item), lu_gp->lu_gp_id);
2863 /*
2864 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2865 * -> target_core_alua_lu_gp_release()
2866 */
2867 config_item_put(item);
2868 }
2869
2870 static struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2871 .make_group = &target_core_alua_create_lu_gp,
2872 .drop_item = &target_core_alua_drop_lu_gp,
2873 };
2874
2875 static const struct config_item_type target_core_alua_lu_gps_cit = {
2876 .ct_item_ops = NULL,
2877 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2878 .ct_owner = THIS_MODULE,
2879 };
2880
2881 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2882
2883 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2884
to_tg_pt_gp(struct config_item * item)2885 static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
2886 {
2887 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2888 tg_pt_gp_group);
2889 }
2890
target_tg_pt_gp_alua_access_state_show(struct config_item * item,char * page)2891 static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2892 char *page)
2893 {
2894 return sprintf(page, "%d\n",
2895 to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2896 }
2897
target_tg_pt_gp_alua_access_state_store(struct config_item * item,const char * page,size_t count)2898 static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2899 const char *page, size_t count)
2900 {
2901 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2902 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2903 unsigned long tmp;
2904 int new_state, ret;
2905
2906 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2907 pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
2908 return -EINVAL;
2909 }
2910 if (!target_dev_configured(dev)) {
2911 pr_err("Unable to set alua_access_state while device is"
2912 " not configured\n");
2913 return -ENODEV;
2914 }
2915
2916 ret = kstrtoul(page, 0, &tmp);
2917 if (ret < 0) {
2918 pr_err("Unable to extract new ALUA access state from"
2919 " %s\n", page);
2920 return ret;
2921 }
2922 new_state = (int)tmp;
2923
2924 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2925 pr_err("Unable to process implicit configfs ALUA"
2926 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
2927 return -EINVAL;
2928 }
2929 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2930 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2931 /* LBA DEPENDENT is only allowed with implicit ALUA */
2932 pr_err("Unable to process implicit configfs ALUA transition"
2933 " while explicit ALUA management is enabled\n");
2934 return -EINVAL;
2935 }
2936
2937 ret = core_alua_do_port_transition(tg_pt_gp, dev,
2938 NULL, NULL, new_state, 0);
2939 return (!ret) ? count : -EINVAL;
2940 }
2941
target_tg_pt_gp_alua_access_status_show(struct config_item * item,char * page)2942 static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2943 char *page)
2944 {
2945 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2946 return sprintf(page, "%s\n",
2947 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2948 }
2949
target_tg_pt_gp_alua_access_status_store(struct config_item * item,const char * page,size_t count)2950 static ssize_t target_tg_pt_gp_alua_access_status_store(
2951 struct config_item *item, const char *page, size_t count)
2952 {
2953 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2954 unsigned long tmp;
2955 int new_status, ret;
2956
2957 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2958 pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
2959 return -EINVAL;
2960 }
2961
2962 ret = kstrtoul(page, 0, &tmp);
2963 if (ret < 0) {
2964 pr_err("Unable to extract new ALUA access status"
2965 " from %s\n", page);
2966 return ret;
2967 }
2968 new_status = (int)tmp;
2969
2970 if ((new_status != ALUA_STATUS_NONE) &&
2971 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2972 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2973 pr_err("Illegal ALUA access status: 0x%02x\n",
2974 new_status);
2975 return -EINVAL;
2976 }
2977
2978 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
2979 return count;
2980 }
2981
target_tg_pt_gp_alua_access_type_show(struct config_item * item,char * page)2982 static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
2983 char *page)
2984 {
2985 return core_alua_show_access_type(to_tg_pt_gp(item), page);
2986 }
2987
target_tg_pt_gp_alua_access_type_store(struct config_item * item,const char * page,size_t count)2988 static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
2989 const char *page, size_t count)
2990 {
2991 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
2992 }
2993
2994 #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
2995 static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
2996 struct config_item *item, char *p) \
2997 { \
2998 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
2999 return sprintf(p, "%d\n", \
3000 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
3001 } \
3002 \
3003 static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
3004 struct config_item *item, const char *p, size_t c) \
3005 { \
3006 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
3007 unsigned long tmp; \
3008 int ret; \
3009 \
3010 if (!t->tg_pt_gp_valid_id) { \
3011 pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
3012 return -EINVAL; \
3013 } \
3014 \
3015 ret = kstrtoul(p, 0, &tmp); \
3016 if (ret < 0) { \
3017 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
3018 return -EINVAL; \
3019 } \
3020 if (tmp > 1) { \
3021 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
3022 return -EINVAL; \
3023 } \
3024 if (tmp) \
3025 t->tg_pt_gp_alua_supported_states |= _bit; \
3026 else \
3027 t->tg_pt_gp_alua_supported_states &= ~_bit; \
3028 \
3029 return c; \
3030 }
3031
3032 ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
3033 ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
3034 ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
3035 ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
3036 ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
3037 ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
3038 ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
3039
target_tg_pt_gp_alua_write_metadata_show(struct config_item * item,char * page)3040 static ssize_t target_tg_pt_gp_alua_write_metadata_show(
3041 struct config_item *item, char *page)
3042 {
3043 return sprintf(page, "%d\n",
3044 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
3045 }
3046
target_tg_pt_gp_alua_write_metadata_store(struct config_item * item,const char * page,size_t count)3047 static ssize_t target_tg_pt_gp_alua_write_metadata_store(
3048 struct config_item *item, const char *page, size_t count)
3049 {
3050 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3051 unsigned long tmp;
3052 int ret;
3053
3054 ret = kstrtoul(page, 0, &tmp);
3055 if (ret < 0) {
3056 pr_err("Unable to extract alua_write_metadata\n");
3057 return ret;
3058 }
3059
3060 if ((tmp != 0) && (tmp != 1)) {
3061 pr_err("Illegal value for alua_write_metadata:"
3062 " %lu\n", tmp);
3063 return -EINVAL;
3064 }
3065 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
3066
3067 return count;
3068 }
3069
target_tg_pt_gp_nonop_delay_msecs_show(struct config_item * item,char * page)3070 static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
3071 char *page)
3072 {
3073 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
3074 }
3075
target_tg_pt_gp_nonop_delay_msecs_store(struct config_item * item,const char * page,size_t count)3076 static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
3077 const char *page, size_t count)
3078 {
3079 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
3080 count);
3081 }
3082
target_tg_pt_gp_trans_delay_msecs_show(struct config_item * item,char * page)3083 static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
3084 char *page)
3085 {
3086 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
3087 }
3088
target_tg_pt_gp_trans_delay_msecs_store(struct config_item * item,const char * page,size_t count)3089 static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
3090 const char *page, size_t count)
3091 {
3092 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
3093 count);
3094 }
3095
target_tg_pt_gp_implicit_trans_secs_show(struct config_item * item,char * page)3096 static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
3097 struct config_item *item, char *page)
3098 {
3099 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
3100 }
3101
target_tg_pt_gp_implicit_trans_secs_store(struct config_item * item,const char * page,size_t count)3102 static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
3103 struct config_item *item, const char *page, size_t count)
3104 {
3105 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
3106 count);
3107 }
3108
target_tg_pt_gp_preferred_show(struct config_item * item,char * page)3109 static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
3110 char *page)
3111 {
3112 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
3113 }
3114
target_tg_pt_gp_preferred_store(struct config_item * item,const char * page,size_t count)3115 static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
3116 const char *page, size_t count)
3117 {
3118 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
3119 }
3120
target_tg_pt_gp_tg_pt_gp_id_show(struct config_item * item,char * page)3121 static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3122 char *page)
3123 {
3124 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3125
3126 if (!tg_pt_gp->tg_pt_gp_valid_id)
3127 return 0;
3128 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3129 }
3130
target_tg_pt_gp_tg_pt_gp_id_store(struct config_item * item,const char * page,size_t count)3131 static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
3132 const char *page, size_t count)
3133 {
3134 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3135 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3136 unsigned long tg_pt_gp_id;
3137 int ret;
3138
3139 ret = kstrtoul(page, 0, &tg_pt_gp_id);
3140 if (ret < 0) {
3141 pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
3142 page);
3143 return ret;
3144 }
3145 if (tg_pt_gp_id > 0x0000ffff) {
3146 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
3147 tg_pt_gp_id);
3148 return -EINVAL;
3149 }
3150
3151 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3152 if (ret < 0)
3153 return -EINVAL;
3154
3155 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
3156 "core/alua/tg_pt_gps/%s to ID: %hu\n",
3157 config_item_name(&alua_tg_pt_gp_cg->cg_item),
3158 tg_pt_gp->tg_pt_gp_id);
3159
3160 return count;
3161 }
3162
target_tg_pt_gp_members_show(struct config_item * item,char * page)3163 static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3164 char *page)
3165 {
3166 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3167 struct se_lun *lun;
3168 ssize_t len = 0, cur_len;
3169 unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
3170
3171 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
3172 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3173 lun_tg_pt_gp_link) {
3174 struct se_portal_group *tpg = lun->lun_tpg;
3175
3176 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
3177 "/%s\n", tpg->se_tpg_tfo->fabric_name,
3178 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3179 tpg->se_tpg_tfo->tpg_get_tag(tpg),
3180 config_item_name(&lun->lun_group.cg_item));
3181 cur_len++; /* Extra byte for NULL terminator */
3182
3183 if ((cur_len + len) > PAGE_SIZE) {
3184 pr_warn("Ran out of lu_gp_show_attr"
3185 "_members buffer\n");
3186 break;
3187 }
3188 memcpy(page+len, buf, cur_len);
3189 len += cur_len;
3190 }
3191 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3192
3193 return len;
3194 }
3195
3196 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
3197 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
3198 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
3199 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
3200 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
3201 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
3202 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
3203 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
3204 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
3205 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
3206 CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
3207 CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
3208 CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
3209 CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
3210 CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
3211 CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
3212 CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3213
3214 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
3215 &target_tg_pt_gp_attr_alua_access_state,
3216 &target_tg_pt_gp_attr_alua_access_status,
3217 &target_tg_pt_gp_attr_alua_access_type,
3218 &target_tg_pt_gp_attr_alua_support_transitioning,
3219 &target_tg_pt_gp_attr_alua_support_offline,
3220 &target_tg_pt_gp_attr_alua_support_lba_dependent,
3221 &target_tg_pt_gp_attr_alua_support_unavailable,
3222 &target_tg_pt_gp_attr_alua_support_standby,
3223 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
3224 &target_tg_pt_gp_attr_alua_support_active_optimized,
3225 &target_tg_pt_gp_attr_alua_write_metadata,
3226 &target_tg_pt_gp_attr_nonop_delay_msecs,
3227 &target_tg_pt_gp_attr_trans_delay_msecs,
3228 &target_tg_pt_gp_attr_implicit_trans_secs,
3229 &target_tg_pt_gp_attr_preferred,
3230 &target_tg_pt_gp_attr_tg_pt_gp_id,
3231 &target_tg_pt_gp_attr_members,
3232 NULL,
3233 };
3234
target_core_alua_tg_pt_gp_release(struct config_item * item)3235 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
3236 {
3237 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3238 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3239
3240 core_alua_free_tg_pt_gp(tg_pt_gp);
3241 }
3242
3243 static struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
3244 .release = target_core_alua_tg_pt_gp_release,
3245 };
3246
3247 static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3248 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
3249 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
3250 .ct_owner = THIS_MODULE,
3251 };
3252
3253 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3254
3255 /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3256
target_core_alua_create_tg_pt_gp(struct config_group * group,const char * name)3257 static struct config_group *target_core_alua_create_tg_pt_gp(
3258 struct config_group *group,
3259 const char *name)
3260 {
3261 struct t10_alua *alua = container_of(group, struct t10_alua,
3262 alua_tg_pt_gps_group);
3263 struct t10_alua_tg_pt_gp *tg_pt_gp;
3264 struct config_group *alua_tg_pt_gp_cg = NULL;
3265 struct config_item *alua_tg_pt_gp_ci = NULL;
3266
3267 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
3268 if (!tg_pt_gp)
3269 return NULL;
3270
3271 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3272 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3273
3274 config_group_init_type_name(alua_tg_pt_gp_cg, name,
3275 &target_core_alua_tg_pt_gp_cit);
3276
3277 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3278 " Group: alua/tg_pt_gps/%s\n",
3279 config_item_name(alua_tg_pt_gp_ci));
3280
3281 return alua_tg_pt_gp_cg;
3282 }
3283
target_core_alua_drop_tg_pt_gp(struct config_group * group,struct config_item * item)3284 static void target_core_alua_drop_tg_pt_gp(
3285 struct config_group *group,
3286 struct config_item *item)
3287 {
3288 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3289 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3290
3291 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3292 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
3293 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
3294 /*
3295 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
3296 * -> target_core_alua_tg_pt_gp_release().
3297 */
3298 config_item_put(item);
3299 }
3300
3301 static struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3302 .make_group = &target_core_alua_create_tg_pt_gp,
3303 .drop_item = &target_core_alua_drop_tg_pt_gp,
3304 };
3305
3306 TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3307
3308 /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3309
3310 /* Start functions for struct config_item_type target_core_alua_cit */
3311
3312 /*
3313 * target_core_alua_cit is a ConfigFS group that lives under
3314 * /sys/kernel/config/target/core/alua. There are default groups
3315 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3316 * target_core_alua_cit in target_core_init_configfs() below.
3317 */
3318 static const struct config_item_type target_core_alua_cit = {
3319 .ct_item_ops = NULL,
3320 .ct_attrs = NULL,
3321 .ct_owner = THIS_MODULE,
3322 };
3323
3324 /* End functions for struct config_item_type target_core_alua_cit */
3325
3326 /* Start functions for struct config_item_type tb_dev_stat_cit */
3327
target_core_stat_mkdir(struct config_group * group,const char * name)3328 static struct config_group *target_core_stat_mkdir(
3329 struct config_group *group,
3330 const char *name)
3331 {
3332 return ERR_PTR(-ENOSYS);
3333 }
3334
target_core_stat_rmdir(struct config_group * group,struct config_item * item)3335 static void target_core_stat_rmdir(
3336 struct config_group *group,
3337 struct config_item *item)
3338 {
3339 return;
3340 }
3341
3342 static struct configfs_group_operations target_core_stat_group_ops = {
3343 .make_group = &target_core_stat_mkdir,
3344 .drop_item = &target_core_stat_rmdir,
3345 };
3346
3347 TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
3348
3349 /* End functions for struct config_item_type tb_dev_stat_cit */
3350
3351 /* Start functions for struct config_item_type target_core_hba_cit */
3352
target_core_make_subdev(struct config_group * group,const char * name)3353 static struct config_group *target_core_make_subdev(
3354 struct config_group *group,
3355 const char *name)
3356 {
3357 struct t10_alua_tg_pt_gp *tg_pt_gp;
3358 struct config_item *hba_ci = &group->cg_item;
3359 struct se_hba *hba = item_to_hba(hba_ci);
3360 struct target_backend *tb = hba->backend;
3361 struct se_device *dev;
3362 int errno = -ENOMEM, ret;
3363
3364 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3365 if (ret)
3366 return ERR_PTR(ret);
3367
3368 dev = target_alloc_device(hba, name);
3369 if (!dev)
3370 goto out_unlock;
3371
3372 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3373
3374 config_group_init_type_name(&dev->dev_action_group, "action",
3375 &tb->tb_dev_action_cit);
3376 configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
3377
3378 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
3379 &tb->tb_dev_attrib_cit);
3380 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
3381
3382 config_group_init_type_name(&dev->dev_pr_group, "pr",
3383 &tb->tb_dev_pr_cit);
3384 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
3385
3386 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
3387 &tb->tb_dev_wwn_cit);
3388 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
3389 &dev->dev_group);
3390
3391 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
3392 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
3393 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
3394 &dev->dev_group);
3395
3396 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
3397 "statistics", &tb->tb_dev_stat_cit);
3398 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
3399 &dev->dev_group);
3400
3401 /*
3402 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3403 */
3404 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
3405 if (!tg_pt_gp)
3406 goto out_free_device;
3407 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3408
3409 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3410 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
3411 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
3412 &dev->t10_alua.alua_tg_pt_gps_group);
3413
3414 /*
3415 * Add core/$HBA/$DEV/statistics/ default groups
3416 */
3417 target_stat_setup_dev_default_groups(dev);
3418
3419 mutex_lock(&target_devices_lock);
3420 target_devices++;
3421 mutex_unlock(&target_devices_lock);
3422
3423 mutex_unlock(&hba->hba_access_mutex);
3424 return &dev->dev_group;
3425
3426 out_free_device:
3427 target_free_device(dev);
3428 out_unlock:
3429 mutex_unlock(&hba->hba_access_mutex);
3430 return ERR_PTR(errno);
3431 }
3432
target_core_drop_subdev(struct config_group * group,struct config_item * item)3433 static void target_core_drop_subdev(
3434 struct config_group *group,
3435 struct config_item *item)
3436 {
3437 struct config_group *dev_cg = to_config_group(item);
3438 struct se_device *dev =
3439 container_of(dev_cg, struct se_device, dev_group);
3440 struct se_hba *hba;
3441
3442 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3443
3444 mutex_lock(&hba->hba_access_mutex);
3445
3446 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
3447 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
3448
3449 /*
3450 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3451 * directly from target_core_alua_tg_pt_gp_release().
3452 */
3453 dev->t10_alua.default_tg_pt_gp = NULL;
3454
3455 configfs_remove_default_groups(dev_cg);
3456
3457 /*
3458 * se_dev is released from target_core_dev_item_ops->release()
3459 */
3460 config_item_put(item);
3461
3462 mutex_lock(&target_devices_lock);
3463 target_devices--;
3464 mutex_unlock(&target_devices_lock);
3465
3466 mutex_unlock(&hba->hba_access_mutex);
3467 }
3468
3469 static struct configfs_group_operations target_core_hba_group_ops = {
3470 .make_group = target_core_make_subdev,
3471 .drop_item = target_core_drop_subdev,
3472 };
3473
3474
to_hba(struct config_item * item)3475 static inline struct se_hba *to_hba(struct config_item *item)
3476 {
3477 return container_of(to_config_group(item), struct se_hba, hba_group);
3478 }
3479
target_hba_info_show(struct config_item * item,char * page)3480 static ssize_t target_hba_info_show(struct config_item *item, char *page)
3481 {
3482 struct se_hba *hba = to_hba(item);
3483
3484 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
3485 hba->hba_id, hba->backend->ops->name,
3486 TARGET_CORE_VERSION);
3487 }
3488
target_hba_mode_show(struct config_item * item,char * page)3489 static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3490 {
3491 struct se_hba *hba = to_hba(item);
3492 int hba_mode = 0;
3493
3494 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3495 hba_mode = 1;
3496
3497 return sprintf(page, "%d\n", hba_mode);
3498 }
3499
target_hba_mode_store(struct config_item * item,const char * page,size_t count)3500 static ssize_t target_hba_mode_store(struct config_item *item,
3501 const char *page, size_t count)
3502 {
3503 struct se_hba *hba = to_hba(item);
3504 unsigned long mode_flag;
3505 int ret;
3506
3507 if (hba->backend->ops->pmode_enable_hba == NULL)
3508 return -EINVAL;
3509
3510 ret = kstrtoul(page, 0, &mode_flag);
3511 if (ret < 0) {
3512 pr_err("Unable to extract hba mode flag: %d\n", ret);
3513 return ret;
3514 }
3515
3516 if (hba->dev_count) {
3517 pr_err("Unable to set hba_mode with active devices\n");
3518 return -EINVAL;
3519 }
3520
3521 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3522 if (ret < 0)
3523 return -EINVAL;
3524 if (ret > 0)
3525 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3526 else if (ret == 0)
3527 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3528
3529 return count;
3530 }
3531
3532 CONFIGFS_ATTR_RO(target_, hba_info);
3533 CONFIGFS_ATTR(target_, hba_mode);
3534
target_core_hba_release(struct config_item * item)3535 static void target_core_hba_release(struct config_item *item)
3536 {
3537 struct se_hba *hba = container_of(to_config_group(item),
3538 struct se_hba, hba_group);
3539 core_delete_hba(hba);
3540 }
3541
3542 static struct configfs_attribute *target_core_hba_attrs[] = {
3543 &target_attr_hba_info,
3544 &target_attr_hba_mode,
3545 NULL,
3546 };
3547
3548 static struct configfs_item_operations target_core_hba_item_ops = {
3549 .release = target_core_hba_release,
3550 };
3551
3552 static const struct config_item_type target_core_hba_cit = {
3553 .ct_item_ops = &target_core_hba_item_ops,
3554 .ct_group_ops = &target_core_hba_group_ops,
3555 .ct_attrs = target_core_hba_attrs,
3556 .ct_owner = THIS_MODULE,
3557 };
3558
target_core_call_addhbatotarget(struct config_group * group,const char * name)3559 static struct config_group *target_core_call_addhbatotarget(
3560 struct config_group *group,
3561 const char *name)
3562 {
3563 char *se_plugin_str, *str, *str2;
3564 struct se_hba *hba;
3565 char buf[TARGET_CORE_NAME_MAX_LEN] = { };
3566 unsigned long plugin_dep_id = 0;
3567 int ret;
3568
3569 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3570 pr_err("Passed *name strlen(): %d exceeds"
3571 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3572 TARGET_CORE_NAME_MAX_LEN);
3573 return ERR_PTR(-ENAMETOOLONG);
3574 }
3575 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3576
3577 str = strstr(buf, "_");
3578 if (!str) {
3579 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3580 return ERR_PTR(-EINVAL);
3581 }
3582 se_plugin_str = buf;
3583 /*
3584 * Special case for subsystem plugins that have "_" in their names.
3585 * Namely rd_direct and rd_mcp..
3586 */
3587 str2 = strstr(str+1, "_");
3588 if (str2) {
3589 *str2 = '\0'; /* Terminate for *se_plugin_str */
3590 str2++; /* Skip to start of plugin dependent ID */
3591 str = str2;
3592 } else {
3593 *str = '\0'; /* Terminate for *se_plugin_str */
3594 str++; /* Skip to start of plugin dependent ID */
3595 }
3596
3597 ret = kstrtoul(str, 0, &plugin_dep_id);
3598 if (ret < 0) {
3599 pr_err("kstrtoul() returned %d for"
3600 " plugin_dep_id\n", ret);
3601 return ERR_PTR(ret);
3602 }
3603 /*
3604 * Load up TCM subsystem plugins if they have not already been loaded.
3605 */
3606 transport_subsystem_check_init();
3607
3608 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3609 if (IS_ERR(hba))
3610 return ERR_CAST(hba);
3611
3612 config_group_init_type_name(&hba->hba_group, name,
3613 &target_core_hba_cit);
3614
3615 return &hba->hba_group;
3616 }
3617
target_core_call_delhbafromtarget(struct config_group * group,struct config_item * item)3618 static void target_core_call_delhbafromtarget(
3619 struct config_group *group,
3620 struct config_item *item)
3621 {
3622 /*
3623 * core_delete_hba() is called from target_core_hba_item_ops->release()
3624 * -> target_core_hba_release()
3625 */
3626 config_item_put(item);
3627 }
3628
3629 static struct configfs_group_operations target_core_group_ops = {
3630 .make_group = target_core_call_addhbatotarget,
3631 .drop_item = target_core_call_delhbafromtarget,
3632 };
3633
3634 static const struct config_item_type target_core_cit = {
3635 .ct_item_ops = NULL,
3636 .ct_group_ops = &target_core_group_ops,
3637 .ct_attrs = NULL,
3638 .ct_owner = THIS_MODULE,
3639 };
3640
3641 /* Stop functions for struct config_item_type target_core_hba_cit */
3642
target_setup_backend_cits(struct target_backend * tb)3643 void target_setup_backend_cits(struct target_backend *tb)
3644 {
3645 target_core_setup_dev_cit(tb);
3646 target_core_setup_dev_action_cit(tb);
3647 target_core_setup_dev_attrib_cit(tb);
3648 target_core_setup_dev_pr_cit(tb);
3649 target_core_setup_dev_wwn_cit(tb);
3650 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3651 target_core_setup_dev_stat_cit(tb);
3652 }
3653
target_init_dbroot(void)3654 static void target_init_dbroot(void)
3655 {
3656 struct file *fp;
3657
3658 snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
3659 fp = filp_open(db_root_stage, O_RDONLY, 0);
3660 if (IS_ERR(fp)) {
3661 pr_err("db_root: cannot open: %s\n", db_root_stage);
3662 return;
3663 }
3664 if (!S_ISDIR(file_inode(fp)->i_mode)) {
3665 filp_close(fp, NULL);
3666 pr_err("db_root: not a valid directory: %s\n", db_root_stage);
3667 return;
3668 }
3669 filp_close(fp, NULL);
3670
3671 strscpy(db_root, db_root_stage);
3672 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
3673 }
3674
target_core_init_configfs(void)3675 static int __init target_core_init_configfs(void)
3676 {
3677 struct configfs_subsystem *subsys = &target_core_fabrics;
3678 struct t10_alua_lu_gp *lu_gp;
3679 int ret;
3680
3681 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3682 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3683 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3684
3685 config_group_init(&subsys->su_group);
3686 mutex_init(&subsys->su_mutex);
3687
3688 ret = init_se_kmem_caches();
3689 if (ret < 0)
3690 return ret;
3691 /*
3692 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3693 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3694 */
3695 config_group_init_type_name(&target_core_hbagroup, "core",
3696 &target_core_cit);
3697 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3698
3699 /*
3700 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3701 */
3702 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3703 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3704
3705 /*
3706 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3707 * groups under /sys/kernel/config/target/core/alua/
3708 */
3709 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3710 &target_core_alua_lu_gps_cit);
3711 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3712
3713 /*
3714 * Add core/alua/lu_gps/default_lu_gp
3715 */
3716 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3717 if (IS_ERR(lu_gp)) {
3718 ret = -ENOMEM;
3719 goto out_global;
3720 }
3721
3722 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3723 &target_core_alua_lu_gp_cit);
3724 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3725
3726 default_lu_gp = lu_gp;
3727
3728 /*
3729 * Register the target_core_mod subsystem with configfs.
3730 */
3731 ret = configfs_register_subsystem(subsys);
3732 if (ret < 0) {
3733 pr_err("Error %d while registering subsystem %s\n",
3734 ret, subsys->su_group.cg_item.ci_namebuf);
3735 goto out_global;
3736 }
3737 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3738 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3739 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3740 /*
3741 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3742 */
3743 ret = rd_module_init();
3744 if (ret < 0)
3745 goto out;
3746
3747 ret = core_dev_setup_virtual_lun0();
3748 if (ret < 0)
3749 goto out;
3750
3751 ret = target_xcopy_setup_pt();
3752 if (ret < 0)
3753 goto out;
3754
3755 scoped_with_kernel_creds()
3756 target_init_dbroot();
3757
3758 return 0;
3759
3760 out:
3761 target_xcopy_release_pt();
3762 configfs_unregister_subsystem(subsys);
3763 core_dev_release_virtual_lun0();
3764 rd_module_exit();
3765 out_global:
3766 if (default_lu_gp) {
3767 core_alua_free_lu_gp(default_lu_gp);
3768 default_lu_gp = NULL;
3769 }
3770 release_se_kmem_caches();
3771 return ret;
3772 }
3773
target_core_exit_configfs(void)3774 static void __exit target_core_exit_configfs(void)
3775 {
3776 configfs_remove_default_groups(&alua_lu_gps_group);
3777 configfs_remove_default_groups(&alua_group);
3778 configfs_remove_default_groups(&target_core_hbagroup);
3779
3780 /*
3781 * We expect subsys->su_group.default_groups to be released
3782 * by configfs subsystem provider logic..
3783 */
3784 configfs_unregister_subsystem(&target_core_fabrics);
3785
3786 core_alua_free_lu_gp(default_lu_gp);
3787 default_lu_gp = NULL;
3788
3789 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3790 " Infrastructure\n");
3791
3792 core_dev_release_virtual_lun0();
3793 rd_module_exit();
3794 target_xcopy_release_pt();
3795 release_se_kmem_caches();
3796 }
3797
3798 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3799 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3800 MODULE_LICENSE("GPL");
3801
3802 module_init(target_core_init_configfs);
3803 module_exit(target_core_exit_configfs);
3804