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 const 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_obj(struct target_fabric_configfs);
479 if (!tf) {
480 pr_err("%s: could not allocate memory!\n", __func__);
481 return -ENOMEM;
482 }
483 tfo = kzalloc_obj(struct target_core_fabric_ops);
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
target_wwn_pd_text_id_info_show(struct config_item * item,char * page)1744 static ssize_t target_wwn_pd_text_id_info_show(struct config_item *item,
1745 char *page)
1746 {
1747 return sysfs_emit(page, "%s\n", &to_t10_wwn(item)->pd_text_id_info[0]);
1748 }
1749
target_wwn_pd_text_id_info_store(struct config_item * item,const char * page,size_t count)1750 static ssize_t target_wwn_pd_text_id_info_store(struct config_item *item,
1751 const char *page, size_t count)
1752 {
1753 struct t10_wwn *t10_wwn = to_t10_wwn(item);
1754 struct se_device *dev = t10_wwn->t10_dev;
1755
1756 /* +2 to allow for a trailing (stripped) '\n' and null-terminator */
1757 unsigned char buf[PD_TEXT_ID_INFO_LEN + 2];
1758 char *stripped;
1759
1760 /*
1761 * Check to see if any active exports exist. If they do exist, fail
1762 * here as changing this information on the fly (underneath the
1763 * initiator side OS dependent multipath code) could cause negative
1764 * effects.
1765 */
1766 if (dev->export_count) {
1767 pr_err("Unable to set the peripheral device text id info while active %d exports exist\n",
1768 dev->export_count);
1769 return -EINVAL;
1770 }
1771
1772 if (strscpy(buf, page, sizeof(buf)) < 0)
1773 return -EOVERFLOW;
1774
1775 /* Strip any newline added from userspace. */
1776 stripped = strstrip(buf);
1777 if (strlen(stripped) >= PD_TEXT_ID_INFO_LEN) {
1778 pr_err("Emulated peripheral device text id info exceeds PD_TEXT_ID_INFO_LEN: " __stringify(PD_TEXT_ID_INFO_LEN "\n"));
1779 return -EOVERFLOW;
1780 }
1781
1782 BUILD_BUG_ON(sizeof(dev->t10_wwn.pd_text_id_info) != PD_TEXT_ID_INFO_LEN);
1783 strscpy(dev->t10_wwn.pd_text_id_info, stripped,
1784 sizeof(dev->t10_wwn.pd_text_id_info));
1785
1786 pr_debug("Target_Core_ConfigFS: Set emulated peripheral dev text id info:"
1787 " %s\n", dev->t10_wwn.pd_text_id_info);
1788
1789 return count;
1790 }
1791
1792 /*
1793 * Generic wrapper for dumping VPD identifiers by association.
1794 */
1795 #define DEF_DEV_WWN_ASSOC_SHOW(_name, _assoc) \
1796 static ssize_t target_wwn_##_name##_show(struct config_item *item, \
1797 char *page) \
1798 { \
1799 struct t10_wwn *t10_wwn = to_t10_wwn(item); \
1800 struct t10_vpd *vpd; \
1801 unsigned char buf[VPD_TMP_BUF_SIZE]; \
1802 ssize_t len = 0; \
1803 \
1804 spin_lock(&t10_wwn->t10_vpd_lock); \
1805 list_for_each_entry(vpd, &t10_wwn->t10_vpd_list, vpd_list) { \
1806 if (vpd->association != _assoc) \
1807 continue; \
1808 \
1809 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1810 transport_dump_vpd_assoc(vpd, buf, VPD_TMP_BUF_SIZE); \
1811 if (len + strlen(buf) >= PAGE_SIZE) \
1812 break; \
1813 len += sprintf(page+len, "%s", buf); \
1814 \
1815 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1816 transport_dump_vpd_ident_type(vpd, buf, VPD_TMP_BUF_SIZE); \
1817 if (len + strlen(buf) >= PAGE_SIZE) \
1818 break; \
1819 len += sprintf(page+len, "%s", buf); \
1820 \
1821 memset(buf, 0, VPD_TMP_BUF_SIZE); \
1822 transport_dump_vpd_ident(vpd, buf, VPD_TMP_BUF_SIZE); \
1823 if (len + strlen(buf) >= PAGE_SIZE) \
1824 break; \
1825 len += sprintf(page+len, "%s", buf); \
1826 } \
1827 spin_unlock(&t10_wwn->t10_vpd_lock); \
1828 \
1829 return len; \
1830 }
1831
1832 /* VPD page 0x83 Association: Logical Unit */
1833 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_logical_unit, 0x00);
1834 /* VPD page 0x83 Association: Target Port */
1835 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_target_port, 0x10);
1836 /* VPD page 0x83 Association: SCSI Target Device */
1837 DEF_DEV_WWN_ASSOC_SHOW(vpd_assoc_scsi_target_device, 0x20);
1838
1839 CONFIGFS_ATTR(target_wwn_, vendor_id);
1840 CONFIGFS_ATTR(target_wwn_, product_id);
1841 CONFIGFS_ATTR(target_wwn_, revision);
1842 CONFIGFS_ATTR(target_wwn_, company_id);
1843 CONFIGFS_ATTR(target_wwn_, vpd_unit_serial);
1844 CONFIGFS_ATTR_RO(target_wwn_, vpd_protocol_identifier);
1845 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_logical_unit);
1846 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_target_port);
1847 CONFIGFS_ATTR_RO(target_wwn_, vpd_assoc_scsi_target_device);
1848 CONFIGFS_ATTR(target_wwn_, pd_text_id_info);
1849
1850 static struct configfs_attribute *target_core_dev_wwn_attrs[] = {
1851 &target_wwn_attr_vendor_id,
1852 &target_wwn_attr_product_id,
1853 &target_wwn_attr_revision,
1854 &target_wwn_attr_company_id,
1855 &target_wwn_attr_vpd_unit_serial,
1856 &target_wwn_attr_vpd_protocol_identifier,
1857 &target_wwn_attr_vpd_assoc_logical_unit,
1858 &target_wwn_attr_vpd_assoc_target_port,
1859 &target_wwn_attr_vpd_assoc_scsi_target_device,
1860 &target_wwn_attr_pd_text_id_info,
1861 NULL,
1862 };
1863
1864 TB_CIT_SETUP(dev_wwn, NULL, NULL, target_core_dev_wwn_attrs);
1865
1866 /* End functions for struct config_item_type tb_dev_wwn_cit */
1867
1868 /* Start functions for struct config_item_type tb_dev_pr_cit */
1869
pr_to_dev(struct config_item * item)1870 static struct se_device *pr_to_dev(struct config_item *item)
1871 {
1872 return container_of(to_config_group(item), struct se_device,
1873 dev_pr_group);
1874 }
1875
target_core_dev_pr_show_spc3_res(struct se_device * dev,char * page)1876 static ssize_t target_core_dev_pr_show_spc3_res(struct se_device *dev,
1877 char *page)
1878 {
1879 struct se_node_acl *se_nacl;
1880 struct t10_pr_registration *pr_reg;
1881 char i_buf[PR_REG_ISID_ID_LEN] = { };
1882
1883 pr_reg = dev->dev_pr_res_holder;
1884 if (!pr_reg)
1885 return sprintf(page, "No SPC-3 Reservation holder\n");
1886
1887 se_nacl = pr_reg->pr_reg_nacl;
1888 core_pr_dump_initiator_port(pr_reg, i_buf, PR_REG_ISID_ID_LEN);
1889
1890 return sprintf(page, "SPC-3 Reservation: %s Initiator: %s%s\n",
1891 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1892 se_nacl->initiatorname, i_buf);
1893 }
1894
target_core_dev_pr_show_spc2_res(struct se_device * dev,char * page)1895 static ssize_t target_core_dev_pr_show_spc2_res(struct se_device *dev,
1896 char *page)
1897 {
1898 struct se_session *sess = dev->reservation_holder;
1899 struct se_node_acl *se_nacl;
1900 ssize_t len;
1901
1902 if (sess) {
1903 se_nacl = sess->se_node_acl;
1904 len = sprintf(page,
1905 "SPC-2 Reservation: %s Initiator: %s\n",
1906 se_nacl->se_tpg->se_tpg_tfo->fabric_name,
1907 se_nacl->initiatorname);
1908 } else {
1909 len = sprintf(page, "No SPC-2 Reservation holder\n");
1910 }
1911 return len;
1912 }
1913
target_pr_res_holder_show(struct config_item * item,char * page)1914 static ssize_t target_pr_res_holder_show(struct config_item *item, char *page)
1915 {
1916 struct se_device *dev = pr_to_dev(item);
1917 int ret;
1918
1919 if (!dev->dev_attrib.emulate_pr)
1920 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
1921
1922 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
1923 return sprintf(page, "Passthrough\n");
1924
1925 spin_lock(&dev->dev_reservation_lock);
1926 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
1927 ret = target_core_dev_pr_show_spc2_res(dev, page);
1928 else
1929 ret = target_core_dev_pr_show_spc3_res(dev, page);
1930 spin_unlock(&dev->dev_reservation_lock);
1931 return ret;
1932 }
1933
target_pr_res_pr_all_tgt_pts_show(struct config_item * item,char * page)1934 static ssize_t target_pr_res_pr_all_tgt_pts_show(struct config_item *item,
1935 char *page)
1936 {
1937 struct se_device *dev = pr_to_dev(item);
1938 ssize_t len = 0;
1939
1940 spin_lock(&dev->dev_reservation_lock);
1941 if (!dev->dev_pr_res_holder) {
1942 len = sprintf(page, "No SPC-3 Reservation holder\n");
1943 } else if (dev->dev_pr_res_holder->pr_reg_all_tg_pt) {
1944 len = sprintf(page, "SPC-3 Reservation: All Target"
1945 " Ports registration\n");
1946 } else {
1947 len = sprintf(page, "SPC-3 Reservation: Single"
1948 " Target Port registration\n");
1949 }
1950
1951 spin_unlock(&dev->dev_reservation_lock);
1952 return len;
1953 }
1954
target_pr_res_pr_generation_show(struct config_item * item,char * page)1955 static ssize_t target_pr_res_pr_generation_show(struct config_item *item,
1956 char *page)
1957 {
1958 return sprintf(page, "0x%08x\n", pr_to_dev(item)->t10_pr.pr_generation);
1959 }
1960
1961
target_pr_res_pr_holder_tg_port_show(struct config_item * item,char * page)1962 static ssize_t target_pr_res_pr_holder_tg_port_show(struct config_item *item,
1963 char *page)
1964 {
1965 struct se_device *dev = pr_to_dev(item);
1966 struct se_node_acl *se_nacl;
1967 struct se_portal_group *se_tpg;
1968 struct t10_pr_registration *pr_reg;
1969 const struct target_core_fabric_ops *tfo;
1970 ssize_t len = 0;
1971
1972 spin_lock(&dev->dev_reservation_lock);
1973 pr_reg = dev->dev_pr_res_holder;
1974 if (!pr_reg) {
1975 len = sprintf(page, "No SPC-3 Reservation holder\n");
1976 goto out_unlock;
1977 }
1978
1979 se_nacl = pr_reg->pr_reg_nacl;
1980 se_tpg = se_nacl->se_tpg;
1981 tfo = se_tpg->se_tpg_tfo;
1982
1983 len += sprintf(page+len, "SPC-3 Reservation: %s"
1984 " Target Node Endpoint: %s\n", tfo->fabric_name,
1985 tfo->tpg_get_wwn(se_tpg));
1986 len += sprintf(page+len, "SPC-3 Reservation: Relative Port"
1987 " Identifier Tag: %hu %s Portal Group Tag: %hu"
1988 " %s Logical Unit: %llu\n", pr_reg->tg_pt_sep_rtpi,
1989 tfo->fabric_name, tfo->tpg_get_tag(se_tpg),
1990 tfo->fabric_name, pr_reg->pr_aptpl_target_lun);
1991
1992 out_unlock:
1993 spin_unlock(&dev->dev_reservation_lock);
1994 return len;
1995 }
1996
1997
target_pr_res_pr_registered_i_pts_show(struct config_item * item,char * page)1998 static ssize_t target_pr_res_pr_registered_i_pts_show(struct config_item *item,
1999 char *page)
2000 {
2001 struct se_device *dev = pr_to_dev(item);
2002 const struct target_core_fabric_ops *tfo;
2003 struct t10_pr_registration *pr_reg;
2004 unsigned char buf[384];
2005 char i_buf[PR_REG_ISID_ID_LEN];
2006 ssize_t len = 0;
2007 int reg_count = 0;
2008
2009 len += sprintf(page+len, "SPC-3 PR Registrations:\n");
2010
2011 spin_lock(&dev->t10_pr.registration_lock);
2012 list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
2013 pr_reg_list) {
2014
2015 memset(buf, 0, 384);
2016 memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2017 tfo = pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
2018 core_pr_dump_initiator_port(pr_reg, i_buf,
2019 PR_REG_ISID_ID_LEN);
2020 sprintf(buf, "%s Node: %s%s Key: 0x%016Lx PRgen: 0x%08x\n",
2021 tfo->fabric_name,
2022 pr_reg->pr_reg_nacl->initiatorname, i_buf, pr_reg->pr_res_key,
2023 pr_reg->pr_res_generation);
2024
2025 if (len + strlen(buf) >= PAGE_SIZE)
2026 break;
2027
2028 len += sprintf(page+len, "%s", buf);
2029 reg_count++;
2030 }
2031 spin_unlock(&dev->t10_pr.registration_lock);
2032
2033 if (!reg_count)
2034 len += sprintf(page+len, "None\n");
2035
2036 return len;
2037 }
2038
target_pr_res_pr_type_show(struct config_item * item,char * page)2039 static ssize_t target_pr_res_pr_type_show(struct config_item *item, char *page)
2040 {
2041 struct se_device *dev = pr_to_dev(item);
2042 struct t10_pr_registration *pr_reg;
2043 ssize_t len = 0;
2044
2045 spin_lock(&dev->dev_reservation_lock);
2046 pr_reg = dev->dev_pr_res_holder;
2047 if (pr_reg) {
2048 len = sprintf(page, "SPC-3 Reservation Type: %s\n",
2049 core_scsi3_pr_dump_type(pr_reg->pr_res_type));
2050 } else {
2051 len = sprintf(page, "No SPC-3 Reservation holder\n");
2052 }
2053
2054 spin_unlock(&dev->dev_reservation_lock);
2055 return len;
2056 }
2057
target_pr_res_type_show(struct config_item * item,char * page)2058 static ssize_t target_pr_res_type_show(struct config_item *item, char *page)
2059 {
2060 struct se_device *dev = pr_to_dev(item);
2061
2062 if (!dev->dev_attrib.emulate_pr)
2063 return sprintf(page, "SPC_RESERVATIONS_DISABLED\n");
2064 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR)
2065 return sprintf(page, "SPC_PASSTHROUGH\n");
2066 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
2067 return sprintf(page, "SPC2_RESERVATIONS\n");
2068
2069 return sprintf(page, "SPC3_PERSISTENT_RESERVATIONS\n");
2070 }
2071
target_pr_res_aptpl_active_show(struct config_item * item,char * page)2072 static ssize_t target_pr_res_aptpl_active_show(struct config_item *item,
2073 char *page)
2074 {
2075 struct se_device *dev = pr_to_dev(item);
2076
2077 if (!dev->dev_attrib.emulate_pr ||
2078 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
2079 return 0;
2080
2081 return sprintf(page, "APTPL Bit Status: %s\n",
2082 (dev->t10_pr.pr_aptpl_active) ? "Activated" : "Disabled");
2083 }
2084
target_pr_res_aptpl_metadata_show(struct config_item * item,char * page)2085 static ssize_t target_pr_res_aptpl_metadata_show(struct config_item *item,
2086 char *page)
2087 {
2088 struct se_device *dev = pr_to_dev(item);
2089
2090 if (!dev->dev_attrib.emulate_pr ||
2091 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
2092 return 0;
2093
2094 return sprintf(page, "Ready to process PR APTPL metadata..\n");
2095 }
2096
2097 enum {
2098 Opt_initiator_fabric, Opt_initiator_node, Opt_initiator_sid,
2099 Opt_sa_res_key, Opt_res_holder, Opt_res_type, Opt_res_scope,
2100 Opt_res_all_tg_pt, Opt_mapped_lun, Opt_target_fabric,
2101 Opt_target_node, Opt_tpgt, Opt_port_rtpi, Opt_target_lun, Opt_err
2102 };
2103
2104 static match_table_t tokens = {
2105 {Opt_initiator_fabric, "initiator_fabric=%s"},
2106 {Opt_initiator_node, "initiator_node=%s"},
2107 {Opt_initiator_sid, "initiator_sid=%s"},
2108 {Opt_sa_res_key, "sa_res_key=%s"},
2109 {Opt_res_holder, "res_holder=%d"},
2110 {Opt_res_type, "res_type=%d"},
2111 {Opt_res_scope, "res_scope=%d"},
2112 {Opt_res_all_tg_pt, "res_all_tg_pt=%d"},
2113 {Opt_mapped_lun, "mapped_lun=%u"},
2114 {Opt_target_fabric, "target_fabric=%s"},
2115 {Opt_target_node, "target_node=%s"},
2116 {Opt_tpgt, "tpgt=%d"},
2117 {Opt_port_rtpi, "port_rtpi=%d"},
2118 {Opt_target_lun, "target_lun=%u"},
2119 {Opt_err, NULL}
2120 };
2121
target_pr_res_aptpl_metadata_store(struct config_item * item,const char * page,size_t count)2122 static ssize_t target_pr_res_aptpl_metadata_store(struct config_item *item,
2123 const char *page, size_t count)
2124 {
2125 struct se_device *dev = pr_to_dev(item);
2126 unsigned char *i_fabric = NULL, *i_port = NULL, *isid = NULL;
2127 unsigned char *t_fabric = NULL, *t_port = NULL;
2128 char *orig, *ptr, *opts;
2129 substring_t args[MAX_OPT_ARGS];
2130 unsigned long long tmp_ll;
2131 u64 sa_res_key = 0;
2132 u64 mapped_lun = 0, target_lun = 0;
2133 int ret = -1, res_holder = 0, all_tg_pt = 0, arg, token;
2134 u16 tpgt = 0;
2135 u8 type = 0;
2136
2137 if (!dev->dev_attrib.emulate_pr ||
2138 (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_PGR))
2139 return count;
2140 if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
2141 return count;
2142
2143 if (dev->export_count) {
2144 pr_debug("Unable to process APTPL metadata while"
2145 " active fabric exports exist\n");
2146 return -EINVAL;
2147 }
2148
2149 opts = kstrdup(page, GFP_KERNEL);
2150 if (!opts)
2151 return -ENOMEM;
2152
2153 orig = opts;
2154 while ((ptr = strsep(&opts, ",\n")) != NULL) {
2155 if (!*ptr)
2156 continue;
2157
2158 token = match_token(ptr, tokens, args);
2159 switch (token) {
2160 case Opt_initiator_fabric:
2161 i_fabric = match_strdup(args);
2162 if (!i_fabric) {
2163 ret = -ENOMEM;
2164 goto out;
2165 }
2166 break;
2167 case Opt_initiator_node:
2168 i_port = match_strdup(args);
2169 if (!i_port) {
2170 ret = -ENOMEM;
2171 goto out;
2172 }
2173 if (strlen(i_port) >= PR_APTPL_MAX_IPORT_LEN) {
2174 pr_err("APTPL metadata initiator_node="
2175 " exceeds PR_APTPL_MAX_IPORT_LEN: %d\n",
2176 PR_APTPL_MAX_IPORT_LEN);
2177 ret = -EINVAL;
2178 break;
2179 }
2180 break;
2181 case Opt_initiator_sid:
2182 isid = match_strdup(args);
2183 if (!isid) {
2184 ret = -ENOMEM;
2185 goto out;
2186 }
2187 if (strlen(isid) >= PR_REG_ISID_LEN) {
2188 pr_err("APTPL metadata initiator_isid"
2189 "= exceeds PR_REG_ISID_LEN: %d\n",
2190 PR_REG_ISID_LEN);
2191 ret = -EINVAL;
2192 break;
2193 }
2194 break;
2195 case Opt_sa_res_key:
2196 ret = match_u64(args, &tmp_ll);
2197 if (ret < 0) {
2198 pr_err("kstrtoull() failed for sa_res_key=\n");
2199 goto out;
2200 }
2201 sa_res_key = (u64)tmp_ll;
2202 break;
2203 /*
2204 * PR APTPL Metadata for Reservation
2205 */
2206 case Opt_res_holder:
2207 ret = match_int(args, &arg);
2208 if (ret)
2209 goto out;
2210 res_holder = arg;
2211 break;
2212 case Opt_res_type:
2213 ret = match_int(args, &arg);
2214 if (ret)
2215 goto out;
2216 type = (u8)arg;
2217 break;
2218 case Opt_res_scope:
2219 ret = match_int(args, &arg);
2220 if (ret)
2221 goto out;
2222 break;
2223 case Opt_res_all_tg_pt:
2224 ret = match_int(args, &arg);
2225 if (ret)
2226 goto out;
2227 all_tg_pt = (int)arg;
2228 break;
2229 case Opt_mapped_lun:
2230 ret = match_u64(args, &tmp_ll);
2231 if (ret)
2232 goto out;
2233 mapped_lun = (u64)tmp_ll;
2234 break;
2235 /*
2236 * PR APTPL Metadata for Target Port
2237 */
2238 case Opt_target_fabric:
2239 t_fabric = match_strdup(args);
2240 if (!t_fabric) {
2241 ret = -ENOMEM;
2242 goto out;
2243 }
2244 break;
2245 case Opt_target_node:
2246 t_port = match_strdup(args);
2247 if (!t_port) {
2248 ret = -ENOMEM;
2249 goto out;
2250 }
2251 if (strlen(t_port) >= PR_APTPL_MAX_TPORT_LEN) {
2252 pr_err("APTPL metadata target_node="
2253 " exceeds PR_APTPL_MAX_TPORT_LEN: %d\n",
2254 PR_APTPL_MAX_TPORT_LEN);
2255 ret = -EINVAL;
2256 break;
2257 }
2258 break;
2259 case Opt_tpgt:
2260 ret = match_int(args, &arg);
2261 if (ret)
2262 goto out;
2263 tpgt = (u16)arg;
2264 break;
2265 case Opt_port_rtpi:
2266 ret = match_int(args, &arg);
2267 if (ret)
2268 goto out;
2269 break;
2270 case Opt_target_lun:
2271 ret = match_u64(args, &tmp_ll);
2272 if (ret)
2273 goto out;
2274 target_lun = (u64)tmp_ll;
2275 break;
2276 default:
2277 break;
2278 }
2279 }
2280
2281 if (!i_port || !t_port || !sa_res_key) {
2282 pr_err("Illegal parameters for APTPL registration\n");
2283 ret = -EINVAL;
2284 goto out;
2285 }
2286
2287 if (res_holder && !(type)) {
2288 pr_err("Illegal PR type: 0x%02x for reservation"
2289 " holder\n", type);
2290 ret = -EINVAL;
2291 goto out;
2292 }
2293
2294 ret = core_scsi3_alloc_aptpl_registration(&dev->t10_pr, sa_res_key,
2295 i_port, isid, mapped_lun, t_port, tpgt, target_lun,
2296 res_holder, all_tg_pt, type);
2297 out:
2298 kfree(i_fabric);
2299 kfree(i_port);
2300 kfree(isid);
2301 kfree(t_fabric);
2302 kfree(t_port);
2303 kfree(orig);
2304 return (ret == 0) ? count : ret;
2305 }
2306
2307
2308 CONFIGFS_ATTR_RO(target_pr_, res_holder);
2309 CONFIGFS_ATTR_RO(target_pr_, res_pr_all_tgt_pts);
2310 CONFIGFS_ATTR_RO(target_pr_, res_pr_generation);
2311 CONFIGFS_ATTR_RO(target_pr_, res_pr_holder_tg_port);
2312 CONFIGFS_ATTR_RO(target_pr_, res_pr_registered_i_pts);
2313 CONFIGFS_ATTR_RO(target_pr_, res_pr_type);
2314 CONFIGFS_ATTR_RO(target_pr_, res_type);
2315 CONFIGFS_ATTR_RO(target_pr_, res_aptpl_active);
2316 CONFIGFS_ATTR(target_pr_, res_aptpl_metadata);
2317
2318 static struct configfs_attribute *target_core_dev_pr_attrs[] = {
2319 &target_pr_attr_res_holder,
2320 &target_pr_attr_res_pr_all_tgt_pts,
2321 &target_pr_attr_res_pr_generation,
2322 &target_pr_attr_res_pr_holder_tg_port,
2323 &target_pr_attr_res_pr_registered_i_pts,
2324 &target_pr_attr_res_pr_type,
2325 &target_pr_attr_res_type,
2326 &target_pr_attr_res_aptpl_active,
2327 &target_pr_attr_res_aptpl_metadata,
2328 NULL,
2329 };
2330
2331 TB_CIT_SETUP(dev_pr, NULL, NULL, target_core_dev_pr_attrs);
2332
2333 /* End functions for struct config_item_type tb_dev_pr_cit */
2334
2335 /* Start functions for struct config_item_type tb_dev_cit */
2336
to_device(struct config_item * item)2337 static inline struct se_device *to_device(struct config_item *item)
2338 {
2339 return container_of(to_config_group(item), struct se_device, dev_group);
2340 }
2341
target_dev_info_show(struct config_item * item,char * page)2342 static ssize_t target_dev_info_show(struct config_item *item, char *page)
2343 {
2344 struct se_device *dev = to_device(item);
2345 int bl = 0;
2346 ssize_t read_bytes = 0;
2347
2348 transport_dump_dev_state(dev, page, &bl);
2349 read_bytes += bl;
2350 read_bytes += dev->transport->show_configfs_dev_params(dev,
2351 page+read_bytes);
2352 return read_bytes;
2353 }
2354
target_dev_control_store(struct config_item * item,const char * page,size_t count)2355 static ssize_t target_dev_control_store(struct config_item *item,
2356 const char *page, size_t count)
2357 {
2358 struct se_device *dev = to_device(item);
2359
2360 return dev->transport->set_configfs_dev_params(dev, page, count);
2361 }
2362
target_dev_alias_show(struct config_item * item,char * page)2363 static ssize_t target_dev_alias_show(struct config_item *item, char *page)
2364 {
2365 struct se_device *dev = to_device(item);
2366
2367 if (!(dev->dev_flags & DF_USING_ALIAS))
2368 return 0;
2369
2370 return snprintf(page, PAGE_SIZE, "%s\n", dev->dev_alias);
2371 }
2372
target_dev_alias_store(struct config_item * item,const char * page,size_t count)2373 static ssize_t target_dev_alias_store(struct config_item *item,
2374 const char *page, size_t count)
2375 {
2376 struct se_device *dev = to_device(item);
2377 struct se_hba *hba = dev->se_hba;
2378 ssize_t read_bytes;
2379
2380 if (count > (SE_DEV_ALIAS_LEN-1)) {
2381 pr_err("alias count: %d exceeds"
2382 " SE_DEV_ALIAS_LEN-1: %u\n", (int)count,
2383 SE_DEV_ALIAS_LEN-1);
2384 return -EINVAL;
2385 }
2386
2387 read_bytes = snprintf(&dev->dev_alias[0], SE_DEV_ALIAS_LEN, "%s", page);
2388 if (!read_bytes)
2389 return -EINVAL;
2390 if (dev->dev_alias[read_bytes - 1] == '\n')
2391 dev->dev_alias[read_bytes - 1] = '\0';
2392
2393 dev->dev_flags |= DF_USING_ALIAS;
2394
2395 pr_debug("Target_Core_ConfigFS: %s/%s set alias: %s\n",
2396 config_item_name(&hba->hba_group.cg_item),
2397 config_item_name(&dev->dev_group.cg_item),
2398 dev->dev_alias);
2399
2400 return read_bytes;
2401 }
2402
target_dev_udev_path_show(struct config_item * item,char * page)2403 static ssize_t target_dev_udev_path_show(struct config_item *item, char *page)
2404 {
2405 struct se_device *dev = to_device(item);
2406
2407 if (!(dev->dev_flags & DF_USING_UDEV_PATH))
2408 return 0;
2409
2410 return snprintf(page, PAGE_SIZE, "%s\n", dev->udev_path);
2411 }
2412
target_dev_udev_path_store(struct config_item * item,const char * page,size_t count)2413 static ssize_t target_dev_udev_path_store(struct config_item *item,
2414 const char *page, size_t count)
2415 {
2416 struct se_device *dev = to_device(item);
2417 struct se_hba *hba = dev->se_hba;
2418 ssize_t read_bytes;
2419
2420 if (count > (SE_UDEV_PATH_LEN-1)) {
2421 pr_err("udev_path count: %d exceeds"
2422 " SE_UDEV_PATH_LEN-1: %u\n", (int)count,
2423 SE_UDEV_PATH_LEN-1);
2424 return -EINVAL;
2425 }
2426
2427 read_bytes = snprintf(&dev->udev_path[0], SE_UDEV_PATH_LEN,
2428 "%s", page);
2429 if (!read_bytes)
2430 return -EINVAL;
2431 if (dev->udev_path[read_bytes - 1] == '\n')
2432 dev->udev_path[read_bytes - 1] = '\0';
2433
2434 dev->dev_flags |= DF_USING_UDEV_PATH;
2435
2436 pr_debug("Target_Core_ConfigFS: %s/%s set udev_path: %s\n",
2437 config_item_name(&hba->hba_group.cg_item),
2438 config_item_name(&dev->dev_group.cg_item),
2439 dev->udev_path);
2440
2441 return read_bytes;
2442 }
2443
target_dev_enable_show(struct config_item * item,char * page)2444 static ssize_t target_dev_enable_show(struct config_item *item, char *page)
2445 {
2446 struct se_device *dev = to_device(item);
2447
2448 return snprintf(page, PAGE_SIZE, "%d\n", target_dev_configured(dev));
2449 }
2450
target_dev_enable_store(struct config_item * item,const char * page,size_t count)2451 static ssize_t target_dev_enable_store(struct config_item *item,
2452 const char *page, size_t count)
2453 {
2454 struct se_device *dev = to_device(item);
2455 char *ptr;
2456 int ret;
2457
2458 ptr = strstr(page, "1");
2459 if (!ptr) {
2460 pr_err("For dev_enable ops, only valid value"
2461 " is \"1\"\n");
2462 return -EINVAL;
2463 }
2464
2465 ret = target_configure_device(dev);
2466 if (ret)
2467 return ret;
2468 return count;
2469 }
2470
target_dev_alua_lu_gp_show(struct config_item * item,char * page)2471 static ssize_t target_dev_alua_lu_gp_show(struct config_item *item, char *page)
2472 {
2473 struct se_device *dev = to_device(item);
2474 struct config_item *lu_ci;
2475 struct t10_alua_lu_gp *lu_gp;
2476 struct t10_alua_lu_gp_member *lu_gp_mem;
2477 ssize_t len = 0;
2478
2479 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2480 if (!lu_gp_mem)
2481 return 0;
2482
2483 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2484 lu_gp = lu_gp_mem->lu_gp;
2485 if (lu_gp) {
2486 lu_ci = &lu_gp->lu_gp_group.cg_item;
2487 len += sprintf(page, "LU Group Alias: %s\nLU Group ID: %hu\n",
2488 config_item_name(lu_ci), lu_gp->lu_gp_id);
2489 }
2490 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2491
2492 return len;
2493 }
2494
target_dev_alua_lu_gp_store(struct config_item * item,const char * page,size_t count)2495 static ssize_t target_dev_alua_lu_gp_store(struct config_item *item,
2496 const char *page, size_t count)
2497 {
2498 struct se_device *dev = to_device(item);
2499 struct se_hba *hba = dev->se_hba;
2500 struct t10_alua_lu_gp *lu_gp = NULL, *lu_gp_new = NULL;
2501 struct t10_alua_lu_gp_member *lu_gp_mem;
2502 unsigned char buf[LU_GROUP_NAME_BUF] = { };
2503 int move = 0;
2504
2505 lu_gp_mem = dev->dev_alua_lu_gp_mem;
2506 if (!lu_gp_mem)
2507 return count;
2508
2509 if (count > LU_GROUP_NAME_BUF) {
2510 pr_err("ALUA LU Group Alias too large!\n");
2511 return -EINVAL;
2512 }
2513 memcpy(buf, page, count);
2514 /*
2515 * Any ALUA logical unit alias besides "NULL" means we will be
2516 * making a new group association.
2517 */
2518 if (strcmp(strstrip(buf), "NULL")) {
2519 /*
2520 * core_alua_get_lu_gp_by_name() will increment reference to
2521 * struct t10_alua_lu_gp. This reference is released with
2522 * core_alua_get_lu_gp_by_name below().
2523 */
2524 lu_gp_new = core_alua_get_lu_gp_by_name(strstrip(buf));
2525 if (!lu_gp_new)
2526 return -ENODEV;
2527 }
2528
2529 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2530 lu_gp = lu_gp_mem->lu_gp;
2531 if (lu_gp) {
2532 /*
2533 * Clearing an existing lu_gp association, and replacing
2534 * with NULL
2535 */
2536 if (!lu_gp_new) {
2537 pr_debug("Target_Core_ConfigFS: Releasing %s/%s"
2538 " from ALUA LU Group: core/alua/lu_gps/%s, ID:"
2539 " %hu\n",
2540 config_item_name(&hba->hba_group.cg_item),
2541 config_item_name(&dev->dev_group.cg_item),
2542 config_item_name(&lu_gp->lu_gp_group.cg_item),
2543 lu_gp->lu_gp_id);
2544
2545 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2546 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2547
2548 return count;
2549 }
2550 /*
2551 * Removing existing association of lu_gp_mem with lu_gp
2552 */
2553 __core_alua_drop_lu_gp_mem(lu_gp_mem, lu_gp);
2554 move = 1;
2555 }
2556 /*
2557 * Associate lu_gp_mem with lu_gp_new.
2558 */
2559 __core_alua_attach_lu_gp_mem(lu_gp_mem, lu_gp_new);
2560 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2561
2562 pr_debug("Target_Core_ConfigFS: %s %s/%s to ALUA LU Group:"
2563 " core/alua/lu_gps/%s, ID: %hu\n",
2564 (move) ? "Moving" : "Adding",
2565 config_item_name(&hba->hba_group.cg_item),
2566 config_item_name(&dev->dev_group.cg_item),
2567 config_item_name(&lu_gp_new->lu_gp_group.cg_item),
2568 lu_gp_new->lu_gp_id);
2569
2570 core_alua_put_lu_gp_from_name(lu_gp_new);
2571 return count;
2572 }
2573
target_dev_lba_map_show(struct config_item * item,char * page)2574 static ssize_t target_dev_lba_map_show(struct config_item *item, char *page)
2575 {
2576 struct se_device *dev = to_device(item);
2577 struct t10_alua_lba_map *map;
2578 struct t10_alua_lba_map_member *mem;
2579 char *b = page;
2580 int bl = 0;
2581 char state;
2582
2583 spin_lock(&dev->t10_alua.lba_map_lock);
2584 if (!list_empty(&dev->t10_alua.lba_map_list))
2585 bl += sprintf(b + bl, "%u %u\n",
2586 dev->t10_alua.lba_map_segment_size,
2587 dev->t10_alua.lba_map_segment_multiplier);
2588 list_for_each_entry(map, &dev->t10_alua.lba_map_list, lba_map_list) {
2589 bl += sprintf(b + bl, "%llu %llu",
2590 map->lba_map_first_lba, map->lba_map_last_lba);
2591 list_for_each_entry(mem, &map->lba_map_mem_list,
2592 lba_map_mem_list) {
2593 switch (mem->lba_map_mem_alua_state) {
2594 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
2595 state = 'O';
2596 break;
2597 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
2598 state = 'A';
2599 break;
2600 case ALUA_ACCESS_STATE_STANDBY:
2601 state = 'S';
2602 break;
2603 case ALUA_ACCESS_STATE_UNAVAILABLE:
2604 state = 'U';
2605 break;
2606 default:
2607 state = '.';
2608 break;
2609 }
2610 bl += sprintf(b + bl, " %d:%c",
2611 mem->lba_map_mem_alua_pg_id, state);
2612 }
2613 bl += sprintf(b + bl, "\n");
2614 }
2615 spin_unlock(&dev->t10_alua.lba_map_lock);
2616 return bl;
2617 }
2618
target_dev_lba_map_store(struct config_item * item,const char * page,size_t count)2619 static ssize_t target_dev_lba_map_store(struct config_item *item,
2620 const char *page, size_t count)
2621 {
2622 struct se_device *dev = to_device(item);
2623 struct t10_alua_lba_map *lba_map = NULL;
2624 struct list_head lba_list;
2625 char *map_entries, *orig, *ptr;
2626 char state;
2627 int pg_num = -1, pg;
2628 int ret = 0, num = 0, pg_id, alua_state;
2629 unsigned long start_lba = -1, end_lba = -1;
2630 unsigned long segment_size = -1, segment_mult = -1;
2631
2632 orig = map_entries = kstrdup(page, GFP_KERNEL);
2633 if (!map_entries)
2634 return -ENOMEM;
2635
2636 INIT_LIST_HEAD(&lba_list);
2637 while ((ptr = strsep(&map_entries, "\n")) != NULL) {
2638 if (!*ptr)
2639 continue;
2640
2641 if (num == 0) {
2642 if (sscanf(ptr, "%lu %lu\n",
2643 &segment_size, &segment_mult) != 2) {
2644 pr_err("Invalid line %d\n", num);
2645 ret = -EINVAL;
2646 break;
2647 }
2648 num++;
2649 continue;
2650 }
2651 if (sscanf(ptr, "%lu %lu", &start_lba, &end_lba) != 2) {
2652 pr_err("Invalid line %d\n", num);
2653 ret = -EINVAL;
2654 break;
2655 }
2656 ptr = strchr(ptr, ' ');
2657 if (!ptr) {
2658 pr_err("Invalid line %d, missing end lba\n", num);
2659 ret = -EINVAL;
2660 break;
2661 }
2662 ptr++;
2663 ptr = strchr(ptr, ' ');
2664 if (!ptr) {
2665 pr_err("Invalid line %d, missing state definitions\n",
2666 num);
2667 ret = -EINVAL;
2668 break;
2669 }
2670 ptr++;
2671 lba_map = core_alua_allocate_lba_map(&lba_list,
2672 start_lba, end_lba);
2673 if (IS_ERR(lba_map)) {
2674 ret = PTR_ERR(lba_map);
2675 break;
2676 }
2677 pg = 0;
2678 while (sscanf(ptr, "%d:%c", &pg_id, &state) == 2) {
2679 switch (state) {
2680 case 'O':
2681 alua_state = ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
2682 break;
2683 case 'A':
2684 alua_state = ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED;
2685 break;
2686 case 'S':
2687 alua_state = ALUA_ACCESS_STATE_STANDBY;
2688 break;
2689 case 'U':
2690 alua_state = ALUA_ACCESS_STATE_UNAVAILABLE;
2691 break;
2692 default:
2693 pr_err("Invalid ALUA state '%c'\n", state);
2694 ret = -EINVAL;
2695 goto out;
2696 }
2697
2698 ret = core_alua_allocate_lba_map_mem(lba_map,
2699 pg_id, alua_state);
2700 if (ret) {
2701 pr_err("Invalid target descriptor %d:%c "
2702 "at line %d\n",
2703 pg_id, state, num);
2704 break;
2705 }
2706 pg++;
2707 ptr = strchr(ptr, ' ');
2708 if (ptr)
2709 ptr++;
2710 else
2711 break;
2712 }
2713 if (pg_num == -1)
2714 pg_num = pg;
2715 else if (pg != pg_num) {
2716 pr_err("Only %d from %d port groups definitions "
2717 "at line %d\n", pg, pg_num, num);
2718 ret = -EINVAL;
2719 break;
2720 }
2721 num++;
2722 }
2723 out:
2724 if (ret) {
2725 core_alua_free_lba_map(&lba_list);
2726 count = ret;
2727 } else
2728 core_alua_set_lba_map(dev, &lba_list,
2729 segment_size, segment_mult);
2730 kfree(orig);
2731 return count;
2732 }
2733
2734 CONFIGFS_ATTR_RO(target_dev_, info);
2735 CONFIGFS_ATTR_WO(target_dev_, control);
2736 CONFIGFS_ATTR(target_dev_, alias);
2737 CONFIGFS_ATTR(target_dev_, udev_path);
2738 CONFIGFS_ATTR(target_dev_, enable);
2739 CONFIGFS_ATTR(target_dev_, alua_lu_gp);
2740 CONFIGFS_ATTR(target_dev_, lba_map);
2741
2742 static struct configfs_attribute *target_core_dev_attrs[] = {
2743 &target_dev_attr_info,
2744 &target_dev_attr_control,
2745 &target_dev_attr_alias,
2746 &target_dev_attr_udev_path,
2747 &target_dev_attr_enable,
2748 &target_dev_attr_alua_lu_gp,
2749 &target_dev_attr_lba_map,
2750 NULL,
2751 };
2752
target_core_dev_release(struct config_item * item)2753 static void target_core_dev_release(struct config_item *item)
2754 {
2755 struct config_group *dev_cg = to_config_group(item);
2756 struct se_device *dev =
2757 container_of(dev_cg, struct se_device, dev_group);
2758
2759 target_free_device(dev);
2760 }
2761
2762 /*
2763 * Used in target_core_fabric_configfs.c to verify valid se_device symlink
2764 * within target_fabric_port_link()
2765 */
2766 struct configfs_item_operations target_core_dev_item_ops = {
2767 .release = target_core_dev_release,
2768 };
2769
2770 TB_CIT_SETUP(dev, &target_core_dev_item_ops, NULL, target_core_dev_attrs);
2771
2772 /* End functions for struct config_item_type tb_dev_cit */
2773
2774 /* Start functions for struct config_item_type target_core_alua_lu_gp_cit */
2775
to_lu_gp(struct config_item * item)2776 static inline struct t10_alua_lu_gp *to_lu_gp(struct config_item *item)
2777 {
2778 return container_of(to_config_group(item), struct t10_alua_lu_gp,
2779 lu_gp_group);
2780 }
2781
target_lu_gp_lu_gp_id_show(struct config_item * item,char * page)2782 static ssize_t target_lu_gp_lu_gp_id_show(struct config_item *item, char *page)
2783 {
2784 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2785
2786 if (!lu_gp->lu_gp_valid_id)
2787 return 0;
2788 return sprintf(page, "%hu\n", lu_gp->lu_gp_id);
2789 }
2790
target_lu_gp_lu_gp_id_store(struct config_item * item,const char * page,size_t count)2791 static ssize_t target_lu_gp_lu_gp_id_store(struct config_item *item,
2792 const char *page, size_t count)
2793 {
2794 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2795 struct config_group *alua_lu_gp_cg = &lu_gp->lu_gp_group;
2796 unsigned long lu_gp_id;
2797 int ret;
2798
2799 ret = kstrtoul(page, 0, &lu_gp_id);
2800 if (ret < 0) {
2801 pr_err("kstrtoul() returned %d for"
2802 " lu_gp_id\n", ret);
2803 return ret;
2804 }
2805 if (lu_gp_id > 0x0000ffff) {
2806 pr_err("ALUA lu_gp_id: %lu exceeds maximum:"
2807 " 0x0000ffff\n", lu_gp_id);
2808 return -EINVAL;
2809 }
2810
2811 ret = core_alua_set_lu_gp_id(lu_gp, (u16)lu_gp_id);
2812 if (ret < 0)
2813 return -EINVAL;
2814
2815 pr_debug("Target_Core_ConfigFS: Set ALUA Logical Unit"
2816 " Group: core/alua/lu_gps/%s to ID: %hu\n",
2817 config_item_name(&alua_lu_gp_cg->cg_item),
2818 lu_gp->lu_gp_id);
2819
2820 return count;
2821 }
2822
target_lu_gp_members_show(struct config_item * item,char * page)2823 static ssize_t target_lu_gp_members_show(struct config_item *item, char *page)
2824 {
2825 struct t10_alua_lu_gp *lu_gp = to_lu_gp(item);
2826 struct t10_alua_lu_gp_member *lu_gp_mem;
2827 const char *const end = page + PAGE_SIZE;
2828 char *cur = page;
2829
2830 spin_lock(&lu_gp->lu_gp_lock);
2831 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
2832 struct se_device *dev = lu_gp_mem->lu_gp_mem_dev;
2833 struct se_hba *hba = dev->se_hba;
2834
2835 cur += scnprintf(cur, end - cur, "%s/%s\n",
2836 config_item_name(&hba->hba_group.cg_item),
2837 config_item_name(&dev->dev_group.cg_item));
2838 if (WARN_ON_ONCE(cur >= end))
2839 break;
2840 }
2841 spin_unlock(&lu_gp->lu_gp_lock);
2842
2843 return cur - page;
2844 }
2845
2846 CONFIGFS_ATTR(target_lu_gp_, lu_gp_id);
2847 CONFIGFS_ATTR_RO(target_lu_gp_, members);
2848
2849 static struct configfs_attribute *target_core_alua_lu_gp_attrs[] = {
2850 &target_lu_gp_attr_lu_gp_id,
2851 &target_lu_gp_attr_members,
2852 NULL,
2853 };
2854
target_core_alua_lu_gp_release(struct config_item * item)2855 static void target_core_alua_lu_gp_release(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 core_alua_free_lu_gp(lu_gp);
2861 }
2862
2863 static const struct configfs_item_operations target_core_alua_lu_gp_ops = {
2864 .release = target_core_alua_lu_gp_release,
2865 };
2866
2867 static const struct config_item_type target_core_alua_lu_gp_cit = {
2868 .ct_item_ops = &target_core_alua_lu_gp_ops,
2869 .ct_attrs = target_core_alua_lu_gp_attrs,
2870 .ct_owner = THIS_MODULE,
2871 };
2872
2873 /* End functions for struct config_item_type target_core_alua_lu_gp_cit */
2874
2875 /* Start functions for struct config_item_type target_core_alua_lu_gps_cit */
2876
target_core_alua_create_lu_gp(struct config_group * group,const char * name)2877 static struct config_group *target_core_alua_create_lu_gp(
2878 struct config_group *group,
2879 const char *name)
2880 {
2881 struct t10_alua_lu_gp *lu_gp;
2882 struct config_group *alua_lu_gp_cg = NULL;
2883 struct config_item *alua_lu_gp_ci = NULL;
2884
2885 lu_gp = core_alua_allocate_lu_gp(name, 0);
2886 if (IS_ERR(lu_gp))
2887 return NULL;
2888
2889 alua_lu_gp_cg = &lu_gp->lu_gp_group;
2890 alua_lu_gp_ci = &alua_lu_gp_cg->cg_item;
2891
2892 config_group_init_type_name(alua_lu_gp_cg, name,
2893 &target_core_alua_lu_gp_cit);
2894
2895 pr_debug("Target_Core_ConfigFS: Allocated ALUA Logical Unit"
2896 " Group: core/alua/lu_gps/%s\n",
2897 config_item_name(alua_lu_gp_ci));
2898
2899 return alua_lu_gp_cg;
2900
2901 }
2902
target_core_alua_drop_lu_gp(struct config_group * group,struct config_item * item)2903 static void target_core_alua_drop_lu_gp(
2904 struct config_group *group,
2905 struct config_item *item)
2906 {
2907 struct t10_alua_lu_gp *lu_gp = container_of(to_config_group(item),
2908 struct t10_alua_lu_gp, lu_gp_group);
2909
2910 pr_debug("Target_Core_ConfigFS: Releasing ALUA Logical Unit"
2911 " Group: core/alua/lu_gps/%s, ID: %hu\n",
2912 config_item_name(item), lu_gp->lu_gp_id);
2913 /*
2914 * core_alua_free_lu_gp() is called from target_core_alua_lu_gp_ops->release()
2915 * -> target_core_alua_lu_gp_release()
2916 */
2917 config_item_put(item);
2918 }
2919
2920 static const struct configfs_group_operations target_core_alua_lu_gps_group_ops = {
2921 .make_group = &target_core_alua_create_lu_gp,
2922 .drop_item = &target_core_alua_drop_lu_gp,
2923 };
2924
2925 static const struct config_item_type target_core_alua_lu_gps_cit = {
2926 .ct_item_ops = NULL,
2927 .ct_group_ops = &target_core_alua_lu_gps_group_ops,
2928 .ct_owner = THIS_MODULE,
2929 };
2930
2931 /* End functions for struct config_item_type target_core_alua_lu_gps_cit */
2932
2933 /* Start functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
2934
to_tg_pt_gp(struct config_item * item)2935 static inline struct t10_alua_tg_pt_gp *to_tg_pt_gp(struct config_item *item)
2936 {
2937 return container_of(to_config_group(item), struct t10_alua_tg_pt_gp,
2938 tg_pt_gp_group);
2939 }
2940
target_tg_pt_gp_alua_access_state_show(struct config_item * item,char * page)2941 static ssize_t target_tg_pt_gp_alua_access_state_show(struct config_item *item,
2942 char *page)
2943 {
2944 return sprintf(page, "%d\n",
2945 to_tg_pt_gp(item)->tg_pt_gp_alua_access_state);
2946 }
2947
target_tg_pt_gp_alua_access_state_store(struct config_item * item,const char * page,size_t count)2948 static ssize_t target_tg_pt_gp_alua_access_state_store(struct config_item *item,
2949 const char *page, size_t count)
2950 {
2951 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2952 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
2953 unsigned long tmp;
2954 int new_state, ret;
2955
2956 if (!tg_pt_gp->tg_pt_gp_valid_id) {
2957 pr_err("Unable to do implicit ALUA on invalid tg_pt_gp ID\n");
2958 return -EINVAL;
2959 }
2960 if (!target_dev_configured(dev)) {
2961 pr_err("Unable to set alua_access_state while device is"
2962 " not configured\n");
2963 return -ENODEV;
2964 }
2965
2966 ret = kstrtoul(page, 0, &tmp);
2967 if (ret < 0) {
2968 pr_err("Unable to extract new ALUA access state from"
2969 " %s\n", page);
2970 return ret;
2971 }
2972 new_state = (int)tmp;
2973
2974 if (!(tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)) {
2975 pr_err("Unable to process implicit configfs ALUA"
2976 " transition while TPGS_IMPLICIT_ALUA is disabled\n");
2977 return -EINVAL;
2978 }
2979 if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA &&
2980 new_state == ALUA_ACCESS_STATE_LBA_DEPENDENT) {
2981 /* LBA DEPENDENT is only allowed with implicit ALUA */
2982 pr_err("Unable to process implicit configfs ALUA transition"
2983 " while explicit ALUA management is enabled\n");
2984 return -EINVAL;
2985 }
2986
2987 ret = core_alua_do_port_transition(tg_pt_gp, dev,
2988 NULL, NULL, new_state, 0);
2989 return (!ret) ? count : -EINVAL;
2990 }
2991
target_tg_pt_gp_alua_access_status_show(struct config_item * item,char * page)2992 static ssize_t target_tg_pt_gp_alua_access_status_show(struct config_item *item,
2993 char *page)
2994 {
2995 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
2996 return sprintf(page, "%s\n",
2997 core_alua_dump_status(tg_pt_gp->tg_pt_gp_alua_access_status));
2998 }
2999
target_tg_pt_gp_alua_access_status_store(struct config_item * item,const char * page,size_t count)3000 static ssize_t target_tg_pt_gp_alua_access_status_store(
3001 struct config_item *item, const char *page, size_t count)
3002 {
3003 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3004 unsigned long tmp;
3005 int new_status, ret;
3006
3007 if (!tg_pt_gp->tg_pt_gp_valid_id) {
3008 pr_err("Unable to set ALUA access status on invalid tg_pt_gp ID\n");
3009 return -EINVAL;
3010 }
3011
3012 ret = kstrtoul(page, 0, &tmp);
3013 if (ret < 0) {
3014 pr_err("Unable to extract new ALUA access status"
3015 " from %s\n", page);
3016 return ret;
3017 }
3018 new_status = (int)tmp;
3019
3020 if ((new_status != ALUA_STATUS_NONE) &&
3021 (new_status != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
3022 (new_status != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
3023 pr_err("Illegal ALUA access status: 0x%02x\n",
3024 new_status);
3025 return -EINVAL;
3026 }
3027
3028 tg_pt_gp->tg_pt_gp_alua_access_status = new_status;
3029 return count;
3030 }
3031
target_tg_pt_gp_alua_access_type_show(struct config_item * item,char * page)3032 static ssize_t target_tg_pt_gp_alua_access_type_show(struct config_item *item,
3033 char *page)
3034 {
3035 return core_alua_show_access_type(to_tg_pt_gp(item), page);
3036 }
3037
target_tg_pt_gp_alua_access_type_store(struct config_item * item,const char * page,size_t count)3038 static ssize_t target_tg_pt_gp_alua_access_type_store(struct config_item *item,
3039 const char *page, size_t count)
3040 {
3041 return core_alua_store_access_type(to_tg_pt_gp(item), page, count);
3042 }
3043
3044 #define ALUA_SUPPORTED_STATE_ATTR(_name, _bit) \
3045 static ssize_t target_tg_pt_gp_alua_support_##_name##_show( \
3046 struct config_item *item, char *p) \
3047 { \
3048 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
3049 return sprintf(p, "%d\n", \
3050 !!(t->tg_pt_gp_alua_supported_states & _bit)); \
3051 } \
3052 \
3053 static ssize_t target_tg_pt_gp_alua_support_##_name##_store( \
3054 struct config_item *item, const char *p, size_t c) \
3055 { \
3056 struct t10_alua_tg_pt_gp *t = to_tg_pt_gp(item); \
3057 unsigned long tmp; \
3058 int ret; \
3059 \
3060 if (!t->tg_pt_gp_valid_id) { \
3061 pr_err("Unable to set " #_name " ALUA state on invalid tg_pt_gp ID\n"); \
3062 return -EINVAL; \
3063 } \
3064 \
3065 ret = kstrtoul(p, 0, &tmp); \
3066 if (ret < 0) { \
3067 pr_err("Invalid value '%s', must be '0' or '1'\n", p); \
3068 return -EINVAL; \
3069 } \
3070 if (tmp > 1) { \
3071 pr_err("Invalid value '%ld', must be '0' or '1'\n", tmp); \
3072 return -EINVAL; \
3073 } \
3074 if (tmp) \
3075 t->tg_pt_gp_alua_supported_states |= _bit; \
3076 else \
3077 t->tg_pt_gp_alua_supported_states &= ~_bit; \
3078 \
3079 return c; \
3080 }
3081
3082 ALUA_SUPPORTED_STATE_ATTR(transitioning, ALUA_T_SUP);
3083 ALUA_SUPPORTED_STATE_ATTR(offline, ALUA_O_SUP);
3084 ALUA_SUPPORTED_STATE_ATTR(lba_dependent, ALUA_LBD_SUP);
3085 ALUA_SUPPORTED_STATE_ATTR(unavailable, ALUA_U_SUP);
3086 ALUA_SUPPORTED_STATE_ATTR(standby, ALUA_S_SUP);
3087 ALUA_SUPPORTED_STATE_ATTR(active_optimized, ALUA_AO_SUP);
3088 ALUA_SUPPORTED_STATE_ATTR(active_nonoptimized, ALUA_AN_SUP);
3089
target_tg_pt_gp_alua_write_metadata_show(struct config_item * item,char * page)3090 static ssize_t target_tg_pt_gp_alua_write_metadata_show(
3091 struct config_item *item, char *page)
3092 {
3093 return sprintf(page, "%d\n",
3094 to_tg_pt_gp(item)->tg_pt_gp_write_metadata);
3095 }
3096
target_tg_pt_gp_alua_write_metadata_store(struct config_item * item,const char * page,size_t count)3097 static ssize_t target_tg_pt_gp_alua_write_metadata_store(
3098 struct config_item *item, const char *page, size_t count)
3099 {
3100 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3101 unsigned long tmp;
3102 int ret;
3103
3104 ret = kstrtoul(page, 0, &tmp);
3105 if (ret < 0) {
3106 pr_err("Unable to extract alua_write_metadata\n");
3107 return ret;
3108 }
3109
3110 if ((tmp != 0) && (tmp != 1)) {
3111 pr_err("Illegal value for alua_write_metadata:"
3112 " %lu\n", tmp);
3113 return -EINVAL;
3114 }
3115 tg_pt_gp->tg_pt_gp_write_metadata = (int)tmp;
3116
3117 return count;
3118 }
3119
target_tg_pt_gp_nonop_delay_msecs_show(struct config_item * item,char * page)3120 static ssize_t target_tg_pt_gp_nonop_delay_msecs_show(struct config_item *item,
3121 char *page)
3122 {
3123 return core_alua_show_nonop_delay_msecs(to_tg_pt_gp(item), page);
3124 }
3125
target_tg_pt_gp_nonop_delay_msecs_store(struct config_item * item,const char * page,size_t count)3126 static ssize_t target_tg_pt_gp_nonop_delay_msecs_store(struct config_item *item,
3127 const char *page, size_t count)
3128 {
3129 return core_alua_store_nonop_delay_msecs(to_tg_pt_gp(item), page,
3130 count);
3131 }
3132
target_tg_pt_gp_trans_delay_msecs_show(struct config_item * item,char * page)3133 static ssize_t target_tg_pt_gp_trans_delay_msecs_show(struct config_item *item,
3134 char *page)
3135 {
3136 return core_alua_show_trans_delay_msecs(to_tg_pt_gp(item), page);
3137 }
3138
target_tg_pt_gp_trans_delay_msecs_store(struct config_item * item,const char * page,size_t count)3139 static ssize_t target_tg_pt_gp_trans_delay_msecs_store(struct config_item *item,
3140 const char *page, size_t count)
3141 {
3142 return core_alua_store_trans_delay_msecs(to_tg_pt_gp(item), page,
3143 count);
3144 }
3145
target_tg_pt_gp_implicit_trans_secs_show(struct config_item * item,char * page)3146 static ssize_t target_tg_pt_gp_implicit_trans_secs_show(
3147 struct config_item *item, char *page)
3148 {
3149 return core_alua_show_implicit_trans_secs(to_tg_pt_gp(item), page);
3150 }
3151
target_tg_pt_gp_implicit_trans_secs_store(struct config_item * item,const char * page,size_t count)3152 static ssize_t target_tg_pt_gp_implicit_trans_secs_store(
3153 struct config_item *item, const char *page, size_t count)
3154 {
3155 return core_alua_store_implicit_trans_secs(to_tg_pt_gp(item), page,
3156 count);
3157 }
3158
target_tg_pt_gp_preferred_show(struct config_item * item,char * page)3159 static ssize_t target_tg_pt_gp_preferred_show(struct config_item *item,
3160 char *page)
3161 {
3162 return core_alua_show_preferred_bit(to_tg_pt_gp(item), page);
3163 }
3164
target_tg_pt_gp_preferred_store(struct config_item * item,const char * page,size_t count)3165 static ssize_t target_tg_pt_gp_preferred_store(struct config_item *item,
3166 const char *page, size_t count)
3167 {
3168 return core_alua_store_preferred_bit(to_tg_pt_gp(item), page, count);
3169 }
3170
target_tg_pt_gp_tg_pt_gp_id_show(struct config_item * item,char * page)3171 static ssize_t target_tg_pt_gp_tg_pt_gp_id_show(struct config_item *item,
3172 char *page)
3173 {
3174 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3175
3176 if (!tg_pt_gp->tg_pt_gp_valid_id)
3177 return 0;
3178 return sprintf(page, "%hu\n", tg_pt_gp->tg_pt_gp_id);
3179 }
3180
target_tg_pt_gp_tg_pt_gp_id_store(struct config_item * item,const char * page,size_t count)3181 static ssize_t target_tg_pt_gp_tg_pt_gp_id_store(struct config_item *item,
3182 const char *page, size_t count)
3183 {
3184 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3185 struct config_group *alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3186 unsigned long tg_pt_gp_id;
3187 int ret;
3188
3189 ret = kstrtoul(page, 0, &tg_pt_gp_id);
3190 if (ret < 0) {
3191 pr_err("ALUA tg_pt_gp_id: invalid value '%s' for tg_pt_gp_id\n",
3192 page);
3193 return ret;
3194 }
3195 if (tg_pt_gp_id > 0x0000ffff) {
3196 pr_err("ALUA tg_pt_gp_id: %lu exceeds maximum: 0x0000ffff\n",
3197 tg_pt_gp_id);
3198 return -EINVAL;
3199 }
3200
3201 ret = core_alua_set_tg_pt_gp_id(tg_pt_gp, (u16)tg_pt_gp_id);
3202 if (ret < 0)
3203 return -EINVAL;
3204
3205 pr_debug("Target_Core_ConfigFS: Set ALUA Target Port Group: "
3206 "core/alua/tg_pt_gps/%s to ID: %hu\n",
3207 config_item_name(&alua_tg_pt_gp_cg->cg_item),
3208 tg_pt_gp->tg_pt_gp_id);
3209
3210 return count;
3211 }
3212
target_tg_pt_gp_members_show(struct config_item * item,char * page)3213 static ssize_t target_tg_pt_gp_members_show(struct config_item *item,
3214 char *page)
3215 {
3216 struct t10_alua_tg_pt_gp *tg_pt_gp = to_tg_pt_gp(item);
3217 struct se_lun *lun;
3218 ssize_t len = 0, cur_len;
3219 unsigned char buf[TG_PT_GROUP_NAME_BUF] = { };
3220
3221 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
3222 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
3223 lun_tg_pt_gp_link) {
3224 struct se_portal_group *tpg = lun->lun_tpg;
3225
3226 cur_len = snprintf(buf, TG_PT_GROUP_NAME_BUF, "%s/%s/tpgt_%hu"
3227 "/%s\n", tpg->se_tpg_tfo->fabric_name,
3228 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
3229 tpg->se_tpg_tfo->tpg_get_tag(tpg),
3230 config_item_name(&lun->lun_group.cg_item));
3231 cur_len++; /* Extra byte for NULL terminator */
3232
3233 if ((cur_len + len) > PAGE_SIZE) {
3234 pr_warn("Ran out of lu_gp_show_attr"
3235 "_members buffer\n");
3236 break;
3237 }
3238 memcpy(page+len, buf, cur_len);
3239 len += cur_len;
3240 }
3241 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
3242
3243 return len;
3244 }
3245
3246 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_state);
3247 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_status);
3248 CONFIGFS_ATTR(target_tg_pt_gp_, alua_access_type);
3249 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_transitioning);
3250 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_offline);
3251 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_lba_dependent);
3252 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_unavailable);
3253 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_standby);
3254 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_optimized);
3255 CONFIGFS_ATTR(target_tg_pt_gp_, alua_support_active_nonoptimized);
3256 CONFIGFS_ATTR(target_tg_pt_gp_, alua_write_metadata);
3257 CONFIGFS_ATTR(target_tg_pt_gp_, nonop_delay_msecs);
3258 CONFIGFS_ATTR(target_tg_pt_gp_, trans_delay_msecs);
3259 CONFIGFS_ATTR(target_tg_pt_gp_, implicit_trans_secs);
3260 CONFIGFS_ATTR(target_tg_pt_gp_, preferred);
3261 CONFIGFS_ATTR(target_tg_pt_gp_, tg_pt_gp_id);
3262 CONFIGFS_ATTR_RO(target_tg_pt_gp_, members);
3263
3264 static struct configfs_attribute *target_core_alua_tg_pt_gp_attrs[] = {
3265 &target_tg_pt_gp_attr_alua_access_state,
3266 &target_tg_pt_gp_attr_alua_access_status,
3267 &target_tg_pt_gp_attr_alua_access_type,
3268 &target_tg_pt_gp_attr_alua_support_transitioning,
3269 &target_tg_pt_gp_attr_alua_support_offline,
3270 &target_tg_pt_gp_attr_alua_support_lba_dependent,
3271 &target_tg_pt_gp_attr_alua_support_unavailable,
3272 &target_tg_pt_gp_attr_alua_support_standby,
3273 &target_tg_pt_gp_attr_alua_support_active_nonoptimized,
3274 &target_tg_pt_gp_attr_alua_support_active_optimized,
3275 &target_tg_pt_gp_attr_alua_write_metadata,
3276 &target_tg_pt_gp_attr_nonop_delay_msecs,
3277 &target_tg_pt_gp_attr_trans_delay_msecs,
3278 &target_tg_pt_gp_attr_implicit_trans_secs,
3279 &target_tg_pt_gp_attr_preferred,
3280 &target_tg_pt_gp_attr_tg_pt_gp_id,
3281 &target_tg_pt_gp_attr_members,
3282 NULL,
3283 };
3284
target_core_alua_tg_pt_gp_release(struct config_item * item)3285 static void target_core_alua_tg_pt_gp_release(struct config_item *item)
3286 {
3287 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3288 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3289
3290 core_alua_free_tg_pt_gp(tg_pt_gp);
3291 }
3292
3293 static const struct configfs_item_operations target_core_alua_tg_pt_gp_ops = {
3294 .release = target_core_alua_tg_pt_gp_release,
3295 };
3296
3297 static const struct config_item_type target_core_alua_tg_pt_gp_cit = {
3298 .ct_item_ops = &target_core_alua_tg_pt_gp_ops,
3299 .ct_attrs = target_core_alua_tg_pt_gp_attrs,
3300 .ct_owner = THIS_MODULE,
3301 };
3302
3303 /* End functions for struct config_item_type target_core_alua_tg_pt_gp_cit */
3304
3305 /* Start functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3306
target_core_alua_create_tg_pt_gp(struct config_group * group,const char * name)3307 static struct config_group *target_core_alua_create_tg_pt_gp(
3308 struct config_group *group,
3309 const char *name)
3310 {
3311 struct t10_alua *alua = container_of(group, struct t10_alua,
3312 alua_tg_pt_gps_group);
3313 struct t10_alua_tg_pt_gp *tg_pt_gp;
3314 struct config_group *alua_tg_pt_gp_cg = NULL;
3315 struct config_item *alua_tg_pt_gp_ci = NULL;
3316
3317 tg_pt_gp = core_alua_allocate_tg_pt_gp(alua->t10_dev, name, 0);
3318 if (!tg_pt_gp)
3319 return NULL;
3320
3321 alua_tg_pt_gp_cg = &tg_pt_gp->tg_pt_gp_group;
3322 alua_tg_pt_gp_ci = &alua_tg_pt_gp_cg->cg_item;
3323
3324 config_group_init_type_name(alua_tg_pt_gp_cg, name,
3325 &target_core_alua_tg_pt_gp_cit);
3326
3327 pr_debug("Target_Core_ConfigFS: Allocated ALUA Target Port"
3328 " Group: alua/tg_pt_gps/%s\n",
3329 config_item_name(alua_tg_pt_gp_ci));
3330
3331 return alua_tg_pt_gp_cg;
3332 }
3333
target_core_alua_drop_tg_pt_gp(struct config_group * group,struct config_item * item)3334 static void target_core_alua_drop_tg_pt_gp(
3335 struct config_group *group,
3336 struct config_item *item)
3337 {
3338 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(to_config_group(item),
3339 struct t10_alua_tg_pt_gp, tg_pt_gp_group);
3340
3341 pr_debug("Target_Core_ConfigFS: Releasing ALUA Target Port"
3342 " Group: alua/tg_pt_gps/%s, ID: %hu\n",
3343 config_item_name(item), tg_pt_gp->tg_pt_gp_id);
3344 /*
3345 * core_alua_free_tg_pt_gp() is called from target_core_alua_tg_pt_gp_ops->release()
3346 * -> target_core_alua_tg_pt_gp_release().
3347 */
3348 config_item_put(item);
3349 }
3350
3351 static const struct configfs_group_operations target_core_alua_tg_pt_gps_group_ops = {
3352 .make_group = &target_core_alua_create_tg_pt_gp,
3353 .drop_item = &target_core_alua_drop_tg_pt_gp,
3354 };
3355
3356 TB_CIT_SETUP(dev_alua_tg_pt_gps, NULL, &target_core_alua_tg_pt_gps_group_ops, NULL);
3357
3358 /* End functions for struct config_item_type tb_alua_tg_pt_gps_cit */
3359
3360 /* Start functions for struct config_item_type target_core_alua_cit */
3361
3362 /*
3363 * target_core_alua_cit is a ConfigFS group that lives under
3364 * /sys/kernel/config/target/core/alua. There are default groups
3365 * core/alua/lu_gps and core/alua/tg_pt_gps that are attached to
3366 * target_core_alua_cit in target_core_init_configfs() below.
3367 */
3368 static const struct config_item_type target_core_alua_cit = {
3369 .ct_item_ops = NULL,
3370 .ct_attrs = NULL,
3371 .ct_owner = THIS_MODULE,
3372 };
3373
3374 /* End functions for struct config_item_type target_core_alua_cit */
3375
3376 /* Start functions for struct config_item_type tb_dev_stat_cit */
3377
target_core_stat_mkdir(struct config_group * group,const char * name)3378 static struct config_group *target_core_stat_mkdir(
3379 struct config_group *group,
3380 const char *name)
3381 {
3382 return ERR_PTR(-ENOSYS);
3383 }
3384
target_core_stat_rmdir(struct config_group * group,struct config_item * item)3385 static void target_core_stat_rmdir(
3386 struct config_group *group,
3387 struct config_item *item)
3388 {
3389 return;
3390 }
3391
3392 static const struct configfs_group_operations target_core_stat_group_ops = {
3393 .make_group = &target_core_stat_mkdir,
3394 .drop_item = &target_core_stat_rmdir,
3395 };
3396
3397 TB_CIT_SETUP(dev_stat, NULL, &target_core_stat_group_ops, NULL);
3398
3399 /* End functions for struct config_item_type tb_dev_stat_cit */
3400
3401 /* Start functions for struct config_item_type target_core_hba_cit */
3402
target_core_make_subdev(struct config_group * group,const char * name)3403 static struct config_group *target_core_make_subdev(
3404 struct config_group *group,
3405 const char *name)
3406 {
3407 struct t10_alua_tg_pt_gp *tg_pt_gp;
3408 struct config_item *hba_ci = &group->cg_item;
3409 struct se_hba *hba = item_to_hba(hba_ci);
3410 struct target_backend *tb = hba->backend;
3411 struct se_device *dev;
3412 int errno = -ENOMEM, ret;
3413
3414 ret = mutex_lock_interruptible(&hba->hba_access_mutex);
3415 if (ret)
3416 return ERR_PTR(ret);
3417
3418 dev = target_alloc_device(hba, name);
3419 if (!dev)
3420 goto out_unlock;
3421
3422 config_group_init_type_name(&dev->dev_group, name, &tb->tb_dev_cit);
3423
3424 config_group_init_type_name(&dev->dev_action_group, "action",
3425 &tb->tb_dev_action_cit);
3426 configfs_add_default_group(&dev->dev_action_group, &dev->dev_group);
3427
3428 config_group_init_type_name(&dev->dev_attrib.da_group, "attrib",
3429 &tb->tb_dev_attrib_cit);
3430 configfs_add_default_group(&dev->dev_attrib.da_group, &dev->dev_group);
3431
3432 config_group_init_type_name(&dev->dev_pr_group, "pr",
3433 &tb->tb_dev_pr_cit);
3434 configfs_add_default_group(&dev->dev_pr_group, &dev->dev_group);
3435
3436 config_group_init_type_name(&dev->t10_wwn.t10_wwn_group, "wwn",
3437 &tb->tb_dev_wwn_cit);
3438 configfs_add_default_group(&dev->t10_wwn.t10_wwn_group,
3439 &dev->dev_group);
3440
3441 config_group_init_type_name(&dev->t10_alua.alua_tg_pt_gps_group,
3442 "alua", &tb->tb_dev_alua_tg_pt_gps_cit);
3443 configfs_add_default_group(&dev->t10_alua.alua_tg_pt_gps_group,
3444 &dev->dev_group);
3445
3446 config_group_init_type_name(&dev->dev_stat_grps.stat_group,
3447 "statistics", &tb->tb_dev_stat_cit);
3448 configfs_add_default_group(&dev->dev_stat_grps.stat_group,
3449 &dev->dev_group);
3450
3451 /*
3452 * Add core/$HBA/$DEV/alua/default_tg_pt_gp
3453 */
3454 tg_pt_gp = core_alua_allocate_tg_pt_gp(dev, "default_tg_pt_gp", 1);
3455 if (!tg_pt_gp)
3456 goto out_free_device;
3457 dev->t10_alua.default_tg_pt_gp = tg_pt_gp;
3458
3459 config_group_init_type_name(&tg_pt_gp->tg_pt_gp_group,
3460 "default_tg_pt_gp", &target_core_alua_tg_pt_gp_cit);
3461 configfs_add_default_group(&tg_pt_gp->tg_pt_gp_group,
3462 &dev->t10_alua.alua_tg_pt_gps_group);
3463
3464 /*
3465 * Add core/$HBA/$DEV/statistics/ default groups
3466 */
3467 target_stat_setup_dev_default_groups(dev);
3468
3469 mutex_lock(&target_devices_lock);
3470 target_devices++;
3471 mutex_unlock(&target_devices_lock);
3472
3473 mutex_unlock(&hba->hba_access_mutex);
3474 return &dev->dev_group;
3475
3476 out_free_device:
3477 target_free_device(dev);
3478 out_unlock:
3479 mutex_unlock(&hba->hba_access_mutex);
3480 return ERR_PTR(errno);
3481 }
3482
target_core_drop_subdev(struct config_group * group,struct config_item * item)3483 static void target_core_drop_subdev(
3484 struct config_group *group,
3485 struct config_item *item)
3486 {
3487 struct config_group *dev_cg = to_config_group(item);
3488 struct se_device *dev =
3489 container_of(dev_cg, struct se_device, dev_group);
3490 struct se_hba *hba;
3491
3492 hba = item_to_hba(&dev->se_hba->hba_group.cg_item);
3493
3494 mutex_lock(&hba->hba_access_mutex);
3495
3496 configfs_remove_default_groups(&dev->dev_stat_grps.stat_group);
3497 configfs_remove_default_groups(&dev->t10_alua.alua_tg_pt_gps_group);
3498
3499 /*
3500 * core_alua_free_tg_pt_gp() is called from ->default_tg_pt_gp
3501 * directly from target_core_alua_tg_pt_gp_release().
3502 */
3503 dev->t10_alua.default_tg_pt_gp = NULL;
3504
3505 configfs_remove_default_groups(dev_cg);
3506
3507 /*
3508 * se_dev is released from target_core_dev_item_ops->release()
3509 */
3510 config_item_put(item);
3511
3512 mutex_lock(&target_devices_lock);
3513 target_devices--;
3514 mutex_unlock(&target_devices_lock);
3515
3516 mutex_unlock(&hba->hba_access_mutex);
3517 }
3518
3519 static const struct configfs_group_operations target_core_hba_group_ops = {
3520 .make_group = target_core_make_subdev,
3521 .drop_item = target_core_drop_subdev,
3522 };
3523
3524
to_hba(struct config_item * item)3525 static inline struct se_hba *to_hba(struct config_item *item)
3526 {
3527 return container_of(to_config_group(item), struct se_hba, hba_group);
3528 }
3529
target_hba_info_show(struct config_item * item,char * page)3530 static ssize_t target_hba_info_show(struct config_item *item, char *page)
3531 {
3532 struct se_hba *hba = to_hba(item);
3533
3534 return sprintf(page, "HBA Index: %d plugin: %s version: %s\n",
3535 hba->hba_id, hba->backend->ops->name,
3536 TARGET_CORE_VERSION);
3537 }
3538
target_hba_mode_show(struct config_item * item,char * page)3539 static ssize_t target_hba_mode_show(struct config_item *item, char *page)
3540 {
3541 struct se_hba *hba = to_hba(item);
3542 int hba_mode = 0;
3543
3544 if (hba->hba_flags & HBA_FLAGS_PSCSI_MODE)
3545 hba_mode = 1;
3546
3547 return sprintf(page, "%d\n", hba_mode);
3548 }
3549
target_hba_mode_store(struct config_item * item,const char * page,size_t count)3550 static ssize_t target_hba_mode_store(struct config_item *item,
3551 const char *page, size_t count)
3552 {
3553 struct se_hba *hba = to_hba(item);
3554 unsigned long mode_flag;
3555 int ret;
3556
3557 if (hba->backend->ops->pmode_enable_hba == NULL)
3558 return -EINVAL;
3559
3560 ret = kstrtoul(page, 0, &mode_flag);
3561 if (ret < 0) {
3562 pr_err("Unable to extract hba mode flag: %d\n", ret);
3563 return ret;
3564 }
3565
3566 if (hba->dev_count) {
3567 pr_err("Unable to set hba_mode with active devices\n");
3568 return -EINVAL;
3569 }
3570
3571 ret = hba->backend->ops->pmode_enable_hba(hba, mode_flag);
3572 if (ret < 0)
3573 return -EINVAL;
3574 if (ret > 0)
3575 hba->hba_flags |= HBA_FLAGS_PSCSI_MODE;
3576 else if (ret == 0)
3577 hba->hba_flags &= ~HBA_FLAGS_PSCSI_MODE;
3578
3579 return count;
3580 }
3581
3582 CONFIGFS_ATTR_RO(target_, hba_info);
3583 CONFIGFS_ATTR(target_, hba_mode);
3584
target_core_hba_release(struct config_item * item)3585 static void target_core_hba_release(struct config_item *item)
3586 {
3587 struct se_hba *hba = container_of(to_config_group(item),
3588 struct se_hba, hba_group);
3589 core_delete_hba(hba);
3590 }
3591
3592 static struct configfs_attribute *target_core_hba_attrs[] = {
3593 &target_attr_hba_info,
3594 &target_attr_hba_mode,
3595 NULL,
3596 };
3597
3598 static const struct configfs_item_operations target_core_hba_item_ops = {
3599 .release = target_core_hba_release,
3600 };
3601
3602 static const struct config_item_type target_core_hba_cit = {
3603 .ct_item_ops = &target_core_hba_item_ops,
3604 .ct_group_ops = &target_core_hba_group_ops,
3605 .ct_attrs = target_core_hba_attrs,
3606 .ct_owner = THIS_MODULE,
3607 };
3608
target_core_call_addhbatotarget(struct config_group * group,const char * name)3609 static struct config_group *target_core_call_addhbatotarget(
3610 struct config_group *group,
3611 const char *name)
3612 {
3613 char *se_plugin_str, *str, *str2;
3614 struct se_hba *hba;
3615 char buf[TARGET_CORE_NAME_MAX_LEN] = { };
3616 unsigned long plugin_dep_id = 0;
3617 int ret;
3618
3619 if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
3620 pr_err("Passed *name strlen(): %d exceeds"
3621 " TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
3622 TARGET_CORE_NAME_MAX_LEN);
3623 return ERR_PTR(-ENAMETOOLONG);
3624 }
3625 snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
3626
3627 str = strstr(buf, "_");
3628 if (!str) {
3629 pr_err("Unable to locate \"_\" for $SUBSYSTEM_PLUGIN_$HOST_ID\n");
3630 return ERR_PTR(-EINVAL);
3631 }
3632 se_plugin_str = buf;
3633 /*
3634 * Special case for subsystem plugins that have "_" in their names.
3635 * Namely rd_direct and rd_mcp..
3636 */
3637 str2 = strstr(str+1, "_");
3638 if (str2) {
3639 *str2 = '\0'; /* Terminate for *se_plugin_str */
3640 str2++; /* Skip to start of plugin dependent ID */
3641 str = str2;
3642 } else {
3643 *str = '\0'; /* Terminate for *se_plugin_str */
3644 str++; /* Skip to start of plugin dependent ID */
3645 }
3646
3647 ret = kstrtoul(str, 0, &plugin_dep_id);
3648 if (ret < 0) {
3649 pr_err("kstrtoul() returned %d for"
3650 " plugin_dep_id\n", ret);
3651 return ERR_PTR(ret);
3652 }
3653 /*
3654 * Load up TCM subsystem plugins if they have not already been loaded.
3655 */
3656 transport_subsystem_check_init();
3657
3658 hba = core_alloc_hba(se_plugin_str, plugin_dep_id, 0);
3659 if (IS_ERR(hba))
3660 return ERR_CAST(hba);
3661
3662 config_group_init_type_name(&hba->hba_group, name,
3663 &target_core_hba_cit);
3664
3665 return &hba->hba_group;
3666 }
3667
target_core_call_delhbafromtarget(struct config_group * group,struct config_item * item)3668 static void target_core_call_delhbafromtarget(
3669 struct config_group *group,
3670 struct config_item *item)
3671 {
3672 /*
3673 * core_delete_hba() is called from target_core_hba_item_ops->release()
3674 * -> target_core_hba_release()
3675 */
3676 config_item_put(item);
3677 }
3678
3679 static const struct configfs_group_operations target_core_group_ops = {
3680 .make_group = target_core_call_addhbatotarget,
3681 .drop_item = target_core_call_delhbafromtarget,
3682 };
3683
3684 static const struct config_item_type target_core_cit = {
3685 .ct_item_ops = NULL,
3686 .ct_group_ops = &target_core_group_ops,
3687 .ct_attrs = NULL,
3688 .ct_owner = THIS_MODULE,
3689 };
3690
3691 /* Stop functions for struct config_item_type target_core_hba_cit */
3692
target_setup_backend_cits(struct target_backend * tb)3693 void target_setup_backend_cits(struct target_backend *tb)
3694 {
3695 target_core_setup_dev_cit(tb);
3696 target_core_setup_dev_action_cit(tb);
3697 target_core_setup_dev_attrib_cit(tb);
3698 target_core_setup_dev_pr_cit(tb);
3699 target_core_setup_dev_wwn_cit(tb);
3700 target_core_setup_dev_alua_tg_pt_gps_cit(tb);
3701 target_core_setup_dev_stat_cit(tb);
3702 }
3703
target_init_dbroot(void)3704 static void target_init_dbroot(void)
3705 {
3706 struct file *fp;
3707
3708 snprintf(db_root_stage, DB_ROOT_LEN, DB_ROOT_PREFERRED);
3709 fp = filp_open(db_root_stage, O_RDONLY, 0);
3710 if (IS_ERR(fp)) {
3711 pr_err("db_root: cannot open: %s\n", db_root_stage);
3712 return;
3713 }
3714 if (!S_ISDIR(file_inode(fp)->i_mode)) {
3715 filp_close(fp, NULL);
3716 pr_err("db_root: not a valid directory: %s\n", db_root_stage);
3717 return;
3718 }
3719 filp_close(fp, NULL);
3720
3721 strscpy(db_root, db_root_stage);
3722 pr_debug("Target_Core_ConfigFS: db_root set to %s\n", db_root);
3723 }
3724
target_core_init_configfs(void)3725 static int __init target_core_init_configfs(void)
3726 {
3727 struct configfs_subsystem *subsys = &target_core_fabrics;
3728 struct t10_alua_lu_gp *lu_gp;
3729 int ret;
3730
3731 pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
3732 " Engine: %s on %s/%s on "UTS_RELEASE"\n",
3733 TARGET_CORE_VERSION, utsname()->sysname, utsname()->machine);
3734
3735 config_group_init(&subsys->su_group);
3736 mutex_init(&subsys->su_mutex);
3737
3738 ret = init_se_kmem_caches();
3739 if (ret < 0)
3740 return ret;
3741 /*
3742 * Create $CONFIGFS/target/core default group for HBA <-> Storage Object
3743 * and ALUA Logical Unit Group and Target Port Group infrastructure.
3744 */
3745 config_group_init_type_name(&target_core_hbagroup, "core",
3746 &target_core_cit);
3747 configfs_add_default_group(&target_core_hbagroup, &subsys->su_group);
3748
3749 /*
3750 * Create ALUA infrastructure under /sys/kernel/config/target/core/alua/
3751 */
3752 config_group_init_type_name(&alua_group, "alua", &target_core_alua_cit);
3753 configfs_add_default_group(&alua_group, &target_core_hbagroup);
3754
3755 /*
3756 * Add ALUA Logical Unit Group and Target Port Group ConfigFS
3757 * groups under /sys/kernel/config/target/core/alua/
3758 */
3759 config_group_init_type_name(&alua_lu_gps_group, "lu_gps",
3760 &target_core_alua_lu_gps_cit);
3761 configfs_add_default_group(&alua_lu_gps_group, &alua_group);
3762
3763 /*
3764 * Add core/alua/lu_gps/default_lu_gp
3765 */
3766 lu_gp = core_alua_allocate_lu_gp("default_lu_gp", 1);
3767 if (IS_ERR(lu_gp)) {
3768 ret = -ENOMEM;
3769 goto out_global;
3770 }
3771
3772 config_group_init_type_name(&lu_gp->lu_gp_group, "default_lu_gp",
3773 &target_core_alua_lu_gp_cit);
3774 configfs_add_default_group(&lu_gp->lu_gp_group, &alua_lu_gps_group);
3775
3776 default_lu_gp = lu_gp;
3777
3778 /*
3779 * Register the target_core_mod subsystem with configfs.
3780 */
3781 ret = configfs_register_subsystem(subsys);
3782 if (ret < 0) {
3783 pr_err("Error %d while registering subsystem %s\n",
3784 ret, subsys->su_group.cg_item.ci_namebuf);
3785 goto out_global;
3786 }
3787 pr_debug("TARGET_CORE[0]: Initialized ConfigFS Fabric"
3788 " Infrastructure: "TARGET_CORE_VERSION" on %s/%s"
3789 " on "UTS_RELEASE"\n", utsname()->sysname, utsname()->machine);
3790 /*
3791 * Register built-in RAMDISK subsystem logic for virtual LUN 0
3792 */
3793 ret = rd_module_init();
3794 if (ret < 0)
3795 goto out;
3796
3797 ret = core_dev_setup_virtual_lun0();
3798 if (ret < 0)
3799 goto out;
3800
3801 ret = target_xcopy_setup_pt();
3802 if (ret < 0)
3803 goto out;
3804
3805 scoped_with_kernel_creds()
3806 target_init_dbroot();
3807
3808 return 0;
3809
3810 out:
3811 target_xcopy_release_pt();
3812 configfs_unregister_subsystem(subsys);
3813 core_dev_release_virtual_lun0();
3814 rd_module_exit();
3815 out_global:
3816 if (default_lu_gp) {
3817 core_alua_free_lu_gp(default_lu_gp);
3818 default_lu_gp = NULL;
3819 }
3820 release_se_kmem_caches();
3821 return ret;
3822 }
3823
target_core_exit_configfs(void)3824 static void __exit target_core_exit_configfs(void)
3825 {
3826 configfs_remove_default_groups(&alua_lu_gps_group);
3827 configfs_remove_default_groups(&alua_group);
3828 configfs_remove_default_groups(&target_core_hbagroup);
3829
3830 /*
3831 * We expect subsys->su_group.default_groups to be released
3832 * by configfs subsystem provider logic..
3833 */
3834 configfs_unregister_subsystem(&target_core_fabrics);
3835
3836 core_alua_free_lu_gp(default_lu_gp);
3837 default_lu_gp = NULL;
3838
3839 pr_debug("TARGET_CORE[0]: Released ConfigFS Fabric"
3840 " Infrastructure\n");
3841
3842 core_dev_release_virtual_lun0();
3843 rd_module_exit();
3844 target_xcopy_release_pt();
3845 release_se_kmem_caches();
3846 }
3847
3848 MODULE_DESCRIPTION("Target_Core_Mod/ConfigFS");
3849 MODULE_AUTHOR("nab@Linux-iSCSI.org");
3850 MODULE_LICENSE("GPL");
3851
3852 module_init(target_core_init_configfs);
3853 module_exit(target_core_exit_configfs);
3854