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