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