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