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