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