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