linux/drivers/target/target_core_pr.c
<<
>>
Prefs
   1/*******************************************************************************
   2 * Filename:  target_core_pr.c
   3 *
   4 * This file contains SPC-3 compliant persistent reservations and
   5 * legacy SPC-2 reservations with compatible reservation handling (CRH=1)
   6 *
   7 * (c) Copyright 2009-2012 RisingTide Systems LLC.
   8 *
   9 * Nicholas A. Bellinger <nab@kernel.org>
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or
  14 * (at your option) any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19 * GNU General Public License for more details.
  20 *
  21 * You should have received a copy of the GNU General Public License
  22 * along with this program; if not, write to the Free Software
  23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24 *
  25 ******************************************************************************/
  26
  27#include <linux/slab.h>
  28#include <linux/spinlock.h>
  29#include <linux/list.h>
  30#include <linux/file.h>
  31#include <scsi/scsi.h>
  32#include <scsi/scsi_cmnd.h>
  33#include <asm/unaligned.h>
  34
  35#include <target/target_core_base.h>
  36#include <target/target_core_backend.h>
  37#include <target/target_core_fabric.h>
  38#include <target/target_core_configfs.h>
  39
  40#include "target_core_internal.h"
  41#include "target_core_pr.h"
  42#include "target_core_ua.h"
  43
  44/*
  45 * Used for Specify Initiator Ports Capable Bit (SPEC_I_PT)
  46 */
  47struct pr_transport_id_holder {
  48        int dest_local_nexus;
  49        struct t10_pr_registration *dest_pr_reg;
  50        struct se_portal_group *dest_tpg;
  51        struct se_node_acl *dest_node_acl;
  52        struct se_dev_entry *dest_se_deve;
  53        struct list_head dest_list;
  54};
  55
  56int core_pr_dump_initiator_port(
  57        struct t10_pr_registration *pr_reg,
  58        char *buf,
  59        u32 size)
  60{
  61        if (!pr_reg->isid_present_at_reg)
  62                return 0;
  63
  64        snprintf(buf, size, ",i,0x%s", &pr_reg->pr_reg_isid[0]);
  65        return 1;
  66}
  67
  68static void __core_scsi3_complete_pro_release(struct se_device *, struct se_node_acl *,
  69                        struct t10_pr_registration *, int);
  70
  71static sense_reason_t
  72target_scsi2_reservation_check(struct se_cmd *cmd)
  73{
  74        struct se_device *dev = cmd->se_dev;
  75        struct se_session *sess = cmd->se_sess;
  76
  77        switch (cmd->t_task_cdb[0]) {
  78        case INQUIRY:
  79        case RELEASE:
  80        case RELEASE_10:
  81                return 0;
  82        default:
  83                break;
  84        }
  85
  86        if (!dev->dev_reserved_node_acl || !sess)
  87                return 0;
  88
  89        if (dev->dev_reserved_node_acl != sess->se_node_acl)
  90                return TCM_RESERVATION_CONFLICT;
  91
  92        if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
  93                if (dev->dev_res_bin_isid != sess->sess_bin_isid)
  94                        return TCM_RESERVATION_CONFLICT;
  95        }
  96
  97        return 0;
  98}
  99
 100static struct t10_pr_registration *core_scsi3_locate_pr_reg(struct se_device *,
 101                                        struct se_node_acl *, struct se_session *);
 102static void core_scsi3_put_pr_reg(struct t10_pr_registration *);
 103
 104static int target_check_scsi2_reservation_conflict(struct se_cmd *cmd)
 105{
 106        struct se_session *se_sess = cmd->se_sess;
 107        struct se_device *dev = cmd->se_dev;
 108        struct t10_pr_registration *pr_reg;
 109        struct t10_reservation *pr_tmpl = &dev->t10_pr;
 110        int conflict = 0;
 111
 112        pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
 113                        se_sess);
 114        if (pr_reg) {
 115                /*
 116                 * From spc4r17 5.7.3 Exceptions to SPC-2 RESERVE and RELEASE
 117                 * behavior
 118                 *
 119                 * A RESERVE(6) or RESERVE(10) command shall complete with GOOD
 120                 * status, but no reservation shall be established and the
 121                 * persistent reservation shall not be changed, if the command
 122                 * is received from a) and b) below.
 123                 *
 124                 * A RELEASE(6) or RELEASE(10) command shall complete with GOOD
 125                 * status, but the persistent reservation shall not be released,
 126                 * if the command is received from a) and b)
 127                 *
 128                 * a) An I_T nexus that is a persistent reservation holder; or
 129                 * b) An I_T nexus that is registered if a registrants only or
 130                 *    all registrants type persistent reservation is present.
 131                 *
 132                 * In all other cases, a RESERVE(6) command, RESERVE(10) command,
 133                 * RELEASE(6) command, or RELEASE(10) command shall be processed
 134                 * as defined in SPC-2.
 135                 */
 136                if (pr_reg->pr_res_holder) {
 137                        core_scsi3_put_pr_reg(pr_reg);
 138                        return 1;
 139                }
 140                if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY) ||
 141                    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) ||
 142                    (pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
 143                    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
 144                        core_scsi3_put_pr_reg(pr_reg);
 145                        return 1;
 146                }
 147                core_scsi3_put_pr_reg(pr_reg);
 148                conflict = 1;
 149        } else {
 150                /*
 151                 * Following spc2r20 5.5.1 Reservations overview:
 152                 *
 153                 * If a logical unit has executed a PERSISTENT RESERVE OUT
 154                 * command with the REGISTER or the REGISTER AND IGNORE
 155                 * EXISTING KEY service action and is still registered by any
 156                 * initiator, all RESERVE commands and all RELEASE commands
 157                 * regardless of initiator shall conflict and shall terminate
 158                 * with a RESERVATION CONFLICT status.
 159                 */
 160                spin_lock(&pr_tmpl->registration_lock);
 161                conflict = (list_empty(&pr_tmpl->registration_list)) ? 0 : 1;
 162                spin_unlock(&pr_tmpl->registration_lock);
 163        }
 164
 165        if (conflict) {
 166                pr_err("Received legacy SPC-2 RESERVE/RELEASE"
 167                        " while active SPC-3 registrations exist,"
 168                        " returning RESERVATION_CONFLICT\n");
 169                return -EBUSY;
 170        }
 171
 172        return 0;
 173}
 174
 175sense_reason_t
 176target_scsi2_reservation_release(struct se_cmd *cmd)
 177{
 178        struct se_device *dev = cmd->se_dev;
 179        struct se_session *sess = cmd->se_sess;
 180        struct se_portal_group *tpg;
 181        int rc;
 182
 183        if (!sess || !sess->se_tpg)
 184                goto out;
 185        rc = target_check_scsi2_reservation_conflict(cmd);
 186        if (rc == 1)
 187                goto out;
 188        if (rc < 0)
 189                return TCM_RESERVATION_CONFLICT;
 190
 191        spin_lock(&dev->dev_reservation_lock);
 192        if (!dev->dev_reserved_node_acl || !sess)
 193                goto out_unlock;
 194
 195        if (dev->dev_reserved_node_acl != sess->se_node_acl)
 196                goto out_unlock;
 197
 198        if (dev->dev_res_bin_isid != sess->sess_bin_isid)
 199                goto out_unlock;
 200
 201        dev->dev_reserved_node_acl = NULL;
 202        dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
 203        if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS_WITH_ISID) {
 204                dev->dev_res_bin_isid = 0;
 205                dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS_WITH_ISID;
 206        }
 207        tpg = sess->se_tpg;
 208        pr_debug("SCSI-2 Released reservation for %s LUN: %u ->"
 209                " MAPPED LUN: %u for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
 210                cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
 211                sess->se_node_acl->initiatorname);
 212
 213out_unlock:
 214        spin_unlock(&dev->dev_reservation_lock);
 215out:
 216        target_complete_cmd(cmd, GOOD);
 217        return 0;
 218}
 219
 220sense_reason_t
 221target_scsi2_reservation_reserve(struct se_cmd *cmd)
 222{
 223        struct se_device *dev = cmd->se_dev;
 224        struct se_session *sess = cmd->se_sess;
 225        struct se_portal_group *tpg;
 226        sense_reason_t ret = 0;
 227        int rc;
 228
 229        if ((cmd->t_task_cdb[1] & 0x01) &&
 230            (cmd->t_task_cdb[1] & 0x02)) {
 231                pr_err("LongIO and Obselete Bits set, returning"
 232                                " ILLEGAL_REQUEST\n");
 233                return TCM_UNSUPPORTED_SCSI_OPCODE;
 234        }
 235        /*
 236         * This is currently the case for target_core_mod passthrough struct se_cmd
 237         * ops
 238         */
 239        if (!sess || !sess->se_tpg)
 240                goto out;
 241        rc = target_check_scsi2_reservation_conflict(cmd);
 242        if (rc == 1)
 243                goto out;
 244
 245        if (rc < 0)
 246                return TCM_RESERVATION_CONFLICT;
 247
 248        tpg = sess->se_tpg;
 249        spin_lock(&dev->dev_reservation_lock);
 250        if (dev->dev_reserved_node_acl &&
 251           (dev->dev_reserved_node_acl != sess->se_node_acl)) {
 252                pr_err("SCSI-2 RESERVATION CONFLIFT for %s fabric\n",
 253                        tpg->se_tpg_tfo->get_fabric_name());
 254                pr_err("Original reserver LUN: %u %s\n",
 255                        cmd->se_lun->unpacked_lun,
 256                        dev->dev_reserved_node_acl->initiatorname);
 257                pr_err("Current attempt - LUN: %u -> MAPPED LUN: %u"
 258                        " from %s \n", cmd->se_lun->unpacked_lun,
 259                        cmd->se_deve->mapped_lun,
 260                        sess->se_node_acl->initiatorname);
 261                ret = TCM_RESERVATION_CONFLICT;
 262                goto out_unlock;
 263        }
 264
 265        dev->dev_reserved_node_acl = sess->se_node_acl;
 266        dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS;
 267        if (sess->sess_bin_isid != 0) {
 268                dev->dev_res_bin_isid = sess->sess_bin_isid;
 269                dev->dev_reservation_flags |= DRF_SPC2_RESERVATIONS_WITH_ISID;
 270        }
 271        pr_debug("SCSI-2 Reserved %s LUN: %u -> MAPPED LUN: %u"
 272                " for %s\n", tpg->se_tpg_tfo->get_fabric_name(),
 273                cmd->se_lun->unpacked_lun, cmd->se_deve->mapped_lun,
 274                sess->se_node_acl->initiatorname);
 275
 276out_unlock:
 277        spin_unlock(&dev->dev_reservation_lock);
 278out:
 279        if (!ret)
 280                target_complete_cmd(cmd, GOOD);
 281        return ret;
 282}
 283
 284
 285/*
 286 * Begin SPC-3/SPC-4 Persistent Reservations emulation support
 287 *
 288 * This function is called by those initiator ports who are *NOT*
 289 * the active PR reservation holder when a reservation is present.
 290 */
 291static int core_scsi3_pr_seq_non_holder(
 292        struct se_cmd *cmd,
 293        u32 pr_reg_type)
 294{
 295        unsigned char *cdb = cmd->t_task_cdb;
 296        struct se_dev_entry *se_deve;
 297        struct se_session *se_sess = cmd->se_sess;
 298        int other_cdb = 0, ignore_reg;
 299        int registered_nexus = 0, ret = 1; /* Conflict by default */
 300        int all_reg = 0, reg_only = 0; /* ALL_REG, REG_ONLY */
 301        int we = 0; /* Write Exclusive */
 302        int legacy = 0; /* Act like a legacy device and return
 303                         * RESERVATION CONFLICT on some CDBs */
 304
 305        se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
 306        /*
 307         * Determine if the registration should be ignored due to
 308         * non-matching ISIDs in target_scsi3_pr_reservation_check().
 309         */
 310        ignore_reg = (pr_reg_type & 0x80000000);
 311        if (ignore_reg)
 312                pr_reg_type &= ~0x80000000;
 313
 314        switch (pr_reg_type) {
 315        case PR_TYPE_WRITE_EXCLUSIVE:
 316                we = 1;
 317        case PR_TYPE_EXCLUSIVE_ACCESS:
 318                /*
 319                 * Some commands are only allowed for the persistent reservation
 320                 * holder.
 321                 */
 322                if ((se_deve->def_pr_registered) && !(ignore_reg))
 323                        registered_nexus = 1;
 324                break;
 325        case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
 326                we = 1;
 327        case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
 328                /*
 329                 * Some commands are only allowed for registered I_T Nexuses.
 330                 */
 331                reg_only = 1;
 332                if ((se_deve->def_pr_registered) && !(ignore_reg))
 333                        registered_nexus = 1;
 334                break;
 335        case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
 336                we = 1;
 337        case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
 338                /*
 339                 * Each registered I_T Nexus is a reservation holder.
 340                 */
 341                all_reg = 1;
 342                if ((se_deve->def_pr_registered) && !(ignore_reg))
 343                        registered_nexus = 1;
 344                break;
 345        default:
 346                return -EINVAL;
 347        }
 348        /*
 349         * Referenced from spc4r17 table 45 for *NON* PR holder access
 350         */
 351        switch (cdb[0]) {
 352        case SECURITY_PROTOCOL_IN:
 353                if (registered_nexus)
 354                        return 0;
 355                ret = (we) ? 0 : 1;
 356                break;
 357        case MODE_SENSE:
 358        case MODE_SENSE_10:
 359        case READ_ATTRIBUTE:
 360        case READ_BUFFER:
 361        case RECEIVE_DIAGNOSTIC:
 362                if (legacy) {
 363                        ret = 1;
 364                        break;
 365                }
 366                if (registered_nexus) {
 367                        ret = 0;
 368                        break;
 369                }
 370                ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
 371                break;
 372        case PERSISTENT_RESERVE_OUT:
 373                /*
 374                 * This follows PERSISTENT_RESERVE_OUT service actions that
 375                 * are allowed in the presence of various reservations.
 376                 * See spc4r17, table 46
 377                 */
 378                switch (cdb[1] & 0x1f) {
 379                case PRO_CLEAR:
 380                case PRO_PREEMPT:
 381                case PRO_PREEMPT_AND_ABORT:
 382                        ret = (registered_nexus) ? 0 : 1;
 383                        break;
 384                case PRO_REGISTER:
 385                case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
 386                        ret = 0;
 387                        break;
 388                case PRO_REGISTER_AND_MOVE:
 389                case PRO_RESERVE:
 390                        ret = 1;
 391                        break;
 392                case PRO_RELEASE:
 393                        ret = (registered_nexus) ? 0 : 1;
 394                        break;
 395                default:
 396                        pr_err("Unknown PERSISTENT_RESERVE_OUT service"
 397                                " action: 0x%02x\n", cdb[1] & 0x1f);
 398                        return -EINVAL;
 399                }
 400                break;
 401        case RELEASE:
 402        case RELEASE_10:
 403                /* Handled by CRH=1 in target_scsi2_reservation_release() */
 404                ret = 0;
 405                break;
 406        case RESERVE:
 407        case RESERVE_10:
 408                /* Handled by CRH=1 in target_scsi2_reservation_reserve() */
 409                ret = 0;
 410                break;
 411        case TEST_UNIT_READY:
 412                ret = (legacy) ? 1 : 0; /* Conflict for legacy */
 413                break;
 414        case MAINTENANCE_IN:
 415                switch (cdb[1] & 0x1f) {
 416                case MI_MANAGEMENT_PROTOCOL_IN:
 417                        if (registered_nexus) {
 418                                ret = 0;
 419                                break;
 420                        }
 421                        ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
 422                        break;
 423                case MI_REPORT_SUPPORTED_OPERATION_CODES:
 424                case MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS:
 425                        if (legacy) {
 426                                ret = 1;
 427                                break;
 428                        }
 429                        if (registered_nexus) {
 430                                ret = 0;
 431                                break;
 432                        }
 433                        ret = (we) ? 0 : 1; /* Allowed Write Exclusive */
 434                        break;
 435                case MI_REPORT_ALIASES:
 436                case MI_REPORT_IDENTIFYING_INFORMATION:
 437                case MI_REPORT_PRIORITY:
 438                case MI_REPORT_TARGET_PGS:
 439                case MI_REPORT_TIMESTAMP:
 440                        ret = 0; /* Allowed */
 441                        break;
 442                default:
 443                        pr_err("Unknown MI Service Action: 0x%02x\n",
 444                                (cdb[1] & 0x1f));
 445                        return -EINVAL;
 446                }
 447                break;
 448        case ACCESS_CONTROL_IN:
 449        case ACCESS_CONTROL_OUT:
 450        case INQUIRY:
 451        case LOG_SENSE:
 452        case READ_MEDIA_SERIAL_NUMBER:
 453        case REPORT_LUNS:
 454        case REQUEST_SENSE:
 455        case PERSISTENT_RESERVE_IN:
 456                ret = 0; /*/ Allowed CDBs */
 457                break;
 458        default:
 459                other_cdb = 1;
 460                break;
 461        }
 462        /*
 463         * Case where the CDB is explicitly allowed in the above switch
 464         * statement.
 465         */
 466        if (!ret && !other_cdb) {
 467                pr_debug("Allowing explict CDB: 0x%02x for %s"
 468                        " reservation holder\n", cdb[0],
 469                        core_scsi3_pr_dump_type(pr_reg_type));
 470
 471                return ret;
 472        }
 473        /*
 474         * Check if write exclusive initiator ports *NOT* holding the
 475         * WRITE_EXCLUSIVE_* reservation.
 476         */
 477        if (we && !registered_nexus) {
 478                if (cmd->data_direction == DMA_TO_DEVICE) {
 479                        /*
 480                         * Conflict for write exclusive
 481                         */
 482                        pr_debug("%s Conflict for unregistered nexus"
 483                                " %s CDB: 0x%02x to %s reservation\n",
 484                                transport_dump_cmd_direction(cmd),
 485                                se_sess->se_node_acl->initiatorname, cdb[0],
 486                                core_scsi3_pr_dump_type(pr_reg_type));
 487                        return 1;
 488                } else {
 489                        /*
 490                         * Allow non WRITE CDBs for all Write Exclusive
 491                         * PR TYPEs to pass for registered and
 492                         * non-registered_nexuxes NOT holding the reservation.
 493                         *
 494                         * We only make noise for the unregisterd nexuses,
 495                         * as we expect registered non-reservation holding
 496                         * nexuses to issue CDBs.
 497                         */
 498
 499                        if (!registered_nexus) {
 500                                pr_debug("Allowing implict CDB: 0x%02x"
 501                                        " for %s reservation on unregistered"
 502                                        " nexus\n", cdb[0],
 503                                        core_scsi3_pr_dump_type(pr_reg_type));
 504                        }
 505
 506                        return 0;
 507                }
 508        } else if ((reg_only) || (all_reg)) {
 509                if (registered_nexus) {
 510                        /*
 511                         * For PR_*_REG_ONLY and PR_*_ALL_REG reservations,
 512                         * allow commands from registered nexuses.
 513                         */
 514
 515                        pr_debug("Allowing implict CDB: 0x%02x for %s"
 516                                " reservation\n", cdb[0],
 517                                core_scsi3_pr_dump_type(pr_reg_type));
 518
 519                        return 0;
 520                }
 521        }
 522        pr_debug("%s Conflict for %sregistered nexus %s CDB: 0x%2x"
 523                " for %s reservation\n", transport_dump_cmd_direction(cmd),
 524                (registered_nexus) ? "" : "un",
 525                se_sess->se_node_acl->initiatorname, cdb[0],
 526                core_scsi3_pr_dump_type(pr_reg_type));
 527
 528        return 1; /* Conflict by default */
 529}
 530
 531static sense_reason_t
 532target_scsi3_pr_reservation_check(struct se_cmd *cmd)
 533{
 534        struct se_device *dev = cmd->se_dev;
 535        struct se_session *sess = cmd->se_sess;
 536        u32 pr_reg_type;
 537
 538        if (!dev->dev_pr_res_holder)
 539                return 0;
 540
 541        pr_reg_type = dev->dev_pr_res_holder->pr_res_type;
 542        cmd->pr_res_key = dev->dev_pr_res_holder->pr_res_key;
 543        if (dev->dev_pr_res_holder->pr_reg_nacl != sess->se_node_acl)
 544                goto check_nonholder;
 545
 546        if (dev->dev_pr_res_holder->isid_present_at_reg) {
 547                if (dev->dev_pr_res_holder->pr_reg_bin_isid !=
 548                    sess->sess_bin_isid) {
 549                        pr_reg_type |= 0x80000000;
 550                        goto check_nonholder;
 551                }
 552        }
 553
 554        return 0;
 555
 556check_nonholder:
 557        if (core_scsi3_pr_seq_non_holder(cmd, pr_reg_type))
 558                return TCM_RESERVATION_CONFLICT;
 559        return 0;
 560}
 561
 562static u32 core_scsi3_pr_generation(struct se_device *dev)
 563{
 564        u32 prg;
 565
 566        /*
 567         * PRGeneration field shall contain the value of a 32-bit wrapping
 568         * counter mainted by the device server.
 569         *
 570         * Note that this is done regardless of Active Persist across
 571         * Target PowerLoss (APTPL)
 572         *
 573         * See spc4r17 section 6.3.12 READ_KEYS service action
 574         */
 575        spin_lock(&dev->dev_reservation_lock);
 576        prg = dev->t10_pr.pr_generation++;
 577        spin_unlock(&dev->dev_reservation_lock);
 578
 579        return prg;
 580}
 581
 582static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
 583        struct se_device *dev,
 584        struct se_node_acl *nacl,
 585        struct se_dev_entry *deve,
 586        unsigned char *isid,
 587        u64 sa_res_key,
 588        int all_tg_pt,
 589        int aptpl)
 590{
 591        struct t10_pr_registration *pr_reg;
 592
 593        pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
 594        if (!pr_reg) {
 595                pr_err("Unable to allocate struct t10_pr_registration\n");
 596                return NULL;
 597        }
 598
 599        pr_reg->pr_aptpl_buf = kzalloc(dev->t10_pr.pr_aptpl_buf_len,
 600                                        GFP_ATOMIC);
 601        if (!pr_reg->pr_aptpl_buf) {
 602                pr_err("Unable to allocate pr_reg->pr_aptpl_buf\n");
 603                kmem_cache_free(t10_pr_reg_cache, pr_reg);
 604                return NULL;
 605        }
 606
 607        INIT_LIST_HEAD(&pr_reg->pr_reg_list);
 608        INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
 609        INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
 610        INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
 611        INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
 612        atomic_set(&pr_reg->pr_res_holders, 0);
 613        pr_reg->pr_reg_nacl = nacl;
 614        pr_reg->pr_reg_deve = deve;
 615        pr_reg->pr_res_mapped_lun = deve->mapped_lun;
 616        pr_reg->pr_aptpl_target_lun = deve->se_lun->unpacked_lun;
 617        pr_reg->pr_res_key = sa_res_key;
 618        pr_reg->pr_reg_all_tg_pt = all_tg_pt;
 619        pr_reg->pr_reg_aptpl = aptpl;
 620        pr_reg->pr_reg_tg_pt_lun = deve->se_lun;
 621        /*
 622         * If an ISID value for this SCSI Initiator Port exists,
 623         * save it to the registration now.
 624         */
 625        if (isid != NULL) {
 626                pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
 627                snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
 628                pr_reg->isid_present_at_reg = 1;
 629        }
 630
 631        return pr_reg;
 632}
 633
 634static int core_scsi3_lunacl_depend_item(struct se_dev_entry *);
 635static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *);
 636
 637/*
 638 * Function used for handling PR registrations for ALL_TG_PT=1 and ALL_TG_PT=0
 639 * modes.
 640 */
 641static struct t10_pr_registration *__core_scsi3_alloc_registration(
 642        struct se_device *dev,
 643        struct se_node_acl *nacl,
 644        struct se_dev_entry *deve,
 645        unsigned char *isid,
 646        u64 sa_res_key,
 647        int all_tg_pt,
 648        int aptpl)
 649{
 650        struct se_dev_entry *deve_tmp;
 651        struct se_node_acl *nacl_tmp;
 652        struct se_port *port, *port_tmp;
 653        struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
 654        struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
 655        int ret;
 656        /*
 657         * Create a registration for the I_T Nexus upon which the
 658         * PROUT REGISTER was received.
 659         */
 660        pr_reg = __core_scsi3_do_alloc_registration(dev, nacl, deve, isid,
 661                        sa_res_key, all_tg_pt, aptpl);
 662        if (!pr_reg)
 663                return NULL;
 664        /*
 665         * Return pointer to pr_reg for ALL_TG_PT=0
 666         */
 667        if (!all_tg_pt)
 668                return pr_reg;
 669        /*
 670         * Create list of matching SCSI Initiator Port registrations
 671         * for ALL_TG_PT=1
 672         */
 673        spin_lock(&dev->se_port_lock);
 674        list_for_each_entry_safe(port, port_tmp, &dev->dev_sep_list, sep_list) {
 675                atomic_inc(&port->sep_tg_pt_ref_cnt);
 676                smp_mb__after_atomic_inc();
 677                spin_unlock(&dev->se_port_lock);
 678
 679                spin_lock_bh(&port->sep_alua_lock);
 680                list_for_each_entry(deve_tmp, &port->sep_alua_list,
 681                                        alua_port_list) {
 682                        /*
 683                         * This pointer will be NULL for demo mode MappedLUNs
 684                         * that have not been make explict via a ConfigFS
 685                         * MappedLUN group for the SCSI Initiator Node ACL.
 686                         */
 687                        if (!deve_tmp->se_lun_acl)
 688                                continue;
 689
 690                        nacl_tmp = deve_tmp->se_lun_acl->se_lun_nacl;
 691                        /*
 692                         * Skip the matching struct se_node_acl that is allocated
 693                         * above..
 694                         */
 695                        if (nacl == nacl_tmp)
 696                                continue;
 697                        /*
 698                         * Only perform PR registrations for target ports on
 699                         * the same fabric module as the REGISTER w/ ALL_TG_PT=1
 700                         * arrived.
 701                         */
 702                        if (tfo != nacl_tmp->se_tpg->se_tpg_tfo)
 703                                continue;
 704                        /*
 705                         * Look for a matching Initiator Node ACL in ASCII format
 706                         */
 707                        if (strcmp(nacl->initiatorname, nacl_tmp->initiatorname))
 708                                continue;
 709
 710                        atomic_inc(&deve_tmp->pr_ref_count);
 711                        smp_mb__after_atomic_inc();
 712                        spin_unlock_bh(&port->sep_alua_lock);
 713                        /*
 714                         * Grab a configfs group dependency that is released
 715                         * for the exception path at label out: below, or upon
 716                         * completion of adding ALL_TG_PT=1 registrations in
 717                         * __core_scsi3_add_registration()
 718                         */
 719                        ret = core_scsi3_lunacl_depend_item(deve_tmp);
 720                        if (ret < 0) {
 721                                pr_err("core_scsi3_lunacl_depend"
 722                                                "_item() failed\n");
 723                                atomic_dec(&port->sep_tg_pt_ref_cnt);
 724                                smp_mb__after_atomic_dec();
 725                                atomic_dec(&deve_tmp->pr_ref_count);
 726                                smp_mb__after_atomic_dec();
 727                                goto out;
 728                        }
 729                        /*
 730                         * Located a matching SCSI Initiator Port on a different
 731                         * port, allocate the pr_reg_atp and attach it to the
 732                         * pr_reg->pr_reg_atp_list that will be processed once
 733                         * the original *pr_reg is processed in
 734                         * __core_scsi3_add_registration()
 735                         */
 736                        pr_reg_atp = __core_scsi3_do_alloc_registration(dev,
 737                                                nacl_tmp, deve_tmp, NULL,
 738                                                sa_res_key, all_tg_pt, aptpl);
 739                        if (!pr_reg_atp) {
 740                                atomic_dec(&port->sep_tg_pt_ref_cnt);
 741                                smp_mb__after_atomic_dec();
 742                                atomic_dec(&deve_tmp->pr_ref_count);
 743                                smp_mb__after_atomic_dec();
 744                                core_scsi3_lunacl_undepend_item(deve_tmp);
 745                                goto out;
 746                        }
 747
 748                        list_add_tail(&pr_reg_atp->pr_reg_atp_mem_list,
 749                                      &pr_reg->pr_reg_atp_list);
 750                        spin_lock_bh(&port->sep_alua_lock);
 751                }
 752                spin_unlock_bh(&port->sep_alua_lock);
 753
 754                spin_lock(&dev->se_port_lock);
 755                atomic_dec(&port->sep_tg_pt_ref_cnt);
 756                smp_mb__after_atomic_dec();
 757        }
 758        spin_unlock(&dev->se_port_lock);
 759
 760        return pr_reg;
 761out:
 762        list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
 763                        &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
 764                list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
 765                core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
 766                kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
 767        }
 768        kmem_cache_free(t10_pr_reg_cache, pr_reg);
 769        return NULL;
 770}
 771
 772int core_scsi3_alloc_aptpl_registration(
 773        struct t10_reservation *pr_tmpl,
 774        u64 sa_res_key,
 775        unsigned char *i_port,
 776        unsigned char *isid,
 777        u32 mapped_lun,
 778        unsigned char *t_port,
 779        u16 tpgt,
 780        u32 target_lun,
 781        int res_holder,
 782        int all_tg_pt,
 783        u8 type)
 784{
 785        struct t10_pr_registration *pr_reg;
 786
 787        if (!i_port || !t_port || !sa_res_key) {
 788                pr_err("Illegal parameters for APTPL registration\n");
 789                return -EINVAL;
 790        }
 791
 792        pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_KERNEL);
 793        if (!pr_reg) {
 794                pr_err("Unable to allocate struct t10_pr_registration\n");
 795                return -ENOMEM;
 796        }
 797        pr_reg->pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len, GFP_KERNEL);
 798
 799        INIT_LIST_HEAD(&pr_reg->pr_reg_list);
 800        INIT_LIST_HEAD(&pr_reg->pr_reg_abort_list);
 801        INIT_LIST_HEAD(&pr_reg->pr_reg_aptpl_list);
 802        INIT_LIST_HEAD(&pr_reg->pr_reg_atp_list);
 803        INIT_LIST_HEAD(&pr_reg->pr_reg_atp_mem_list);
 804        atomic_set(&pr_reg->pr_res_holders, 0);
 805        pr_reg->pr_reg_nacl = NULL;
 806        pr_reg->pr_reg_deve = NULL;
 807        pr_reg->pr_res_mapped_lun = mapped_lun;
 808        pr_reg->pr_aptpl_target_lun = target_lun;
 809        pr_reg->pr_res_key = sa_res_key;
 810        pr_reg->pr_reg_all_tg_pt = all_tg_pt;
 811        pr_reg->pr_reg_aptpl = 1;
 812        pr_reg->pr_reg_tg_pt_lun = NULL;
 813        pr_reg->pr_res_scope = 0; /* Always LUN_SCOPE */
 814        pr_reg->pr_res_type = type;
 815        /*
 816         * If an ISID value had been saved in APTPL metadata for this
 817         * SCSI Initiator Port, restore it now.
 818         */
 819        if (isid != NULL) {
 820                pr_reg->pr_reg_bin_isid = get_unaligned_be64(isid);
 821                snprintf(pr_reg->pr_reg_isid, PR_REG_ISID_LEN, "%s", isid);
 822                pr_reg->isid_present_at_reg = 1;
 823        }
 824        /*
 825         * Copy the i_port and t_port information from caller.
 826         */
 827        snprintf(pr_reg->pr_iport, PR_APTPL_MAX_IPORT_LEN, "%s", i_port);
 828        snprintf(pr_reg->pr_tport, PR_APTPL_MAX_TPORT_LEN, "%s", t_port);
 829        pr_reg->pr_reg_tpgt = tpgt;
 830        /*
 831         * Set pr_res_holder from caller, the pr_reg who is the reservation
 832         * holder will get it's pointer set in core_scsi3_aptpl_reserve() once
 833         * the Initiator Node LUN ACL from the fabric module is created for
 834         * this registration.
 835         */
 836        pr_reg->pr_res_holder = res_holder;
 837
 838        list_add_tail(&pr_reg->pr_reg_aptpl_list, &pr_tmpl->aptpl_reg_list);
 839        pr_debug("SPC-3 PR APTPL Successfully added registration%s from"
 840                        " metadata\n", (res_holder) ? "+reservation" : "");
 841        return 0;
 842}
 843
 844static void core_scsi3_aptpl_reserve(
 845        struct se_device *dev,
 846        struct se_portal_group *tpg,
 847        struct se_node_acl *node_acl,
 848        struct t10_pr_registration *pr_reg)
 849{
 850        char i_buf[PR_REG_ISID_ID_LEN];
 851        int prf_isid;
 852
 853        memset(i_buf, 0, PR_REG_ISID_ID_LEN);
 854        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
 855                                PR_REG_ISID_ID_LEN);
 856
 857        spin_lock(&dev->dev_reservation_lock);
 858        dev->dev_pr_res_holder = pr_reg;
 859        spin_unlock(&dev->dev_reservation_lock);
 860
 861        pr_debug("SPC-3 PR [%s] Service Action: APTPL RESERVE created"
 862                " new reservation holder TYPE: %s ALL_TG_PT: %d\n",
 863                tpg->se_tpg_tfo->get_fabric_name(),
 864                core_scsi3_pr_dump_type(pr_reg->pr_res_type),
 865                (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
 866        pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
 867                tpg->se_tpg_tfo->get_fabric_name(), node_acl->initiatorname,
 868                (prf_isid) ? &i_buf[0] : "");
 869}
 870
 871static void __core_scsi3_add_registration(struct se_device *, struct se_node_acl *,
 872                                struct t10_pr_registration *, int, int);
 873
 874static int __core_scsi3_check_aptpl_registration(
 875        struct se_device *dev,
 876        struct se_portal_group *tpg,
 877        struct se_lun *lun,
 878        u32 target_lun,
 879        struct se_node_acl *nacl,
 880        struct se_dev_entry *deve)
 881{
 882        struct t10_pr_registration *pr_reg, *pr_reg_tmp;
 883        struct t10_reservation *pr_tmpl = &dev->t10_pr;
 884        unsigned char i_port[PR_APTPL_MAX_IPORT_LEN];
 885        unsigned char t_port[PR_APTPL_MAX_TPORT_LEN];
 886        u16 tpgt;
 887
 888        memset(i_port, 0, PR_APTPL_MAX_IPORT_LEN);
 889        memset(t_port, 0, PR_APTPL_MAX_TPORT_LEN);
 890        /*
 891         * Copy Initiator Port information from struct se_node_acl
 892         */
 893        snprintf(i_port, PR_APTPL_MAX_IPORT_LEN, "%s", nacl->initiatorname);
 894        snprintf(t_port, PR_APTPL_MAX_TPORT_LEN, "%s",
 895                        tpg->se_tpg_tfo->tpg_get_wwn(tpg));
 896        tpgt = tpg->se_tpg_tfo->tpg_get_tag(tpg);
 897        /*
 898         * Look for the matching registrations+reservation from those
 899         * created from APTPL metadata.  Note that multiple registrations
 900         * may exist for fabrics that use ISIDs in their SCSI Initiator Port
 901         * TransportIDs.
 902         */
 903        spin_lock(&pr_tmpl->aptpl_reg_lock);
 904        list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
 905                                pr_reg_aptpl_list) {
 906                if (!strcmp(pr_reg->pr_iport, i_port) &&
 907                     (pr_reg->pr_res_mapped_lun == deve->mapped_lun) &&
 908                    !(strcmp(pr_reg->pr_tport, t_port)) &&
 909                     (pr_reg->pr_reg_tpgt == tpgt) &&
 910                     (pr_reg->pr_aptpl_target_lun == target_lun)) {
 911
 912                        pr_reg->pr_reg_nacl = nacl;
 913                        pr_reg->pr_reg_deve = deve;
 914                        pr_reg->pr_reg_tg_pt_lun = lun;
 915
 916                        list_del(&pr_reg->pr_reg_aptpl_list);
 917                        spin_unlock(&pr_tmpl->aptpl_reg_lock);
 918                        /*
 919                         * At this point all of the pointers in *pr_reg will
 920                         * be setup, so go ahead and add the registration.
 921                         */
 922
 923                        __core_scsi3_add_registration(dev, nacl, pr_reg, 0, 0);
 924                        /*
 925                         * If this registration is the reservation holder,
 926                         * make that happen now..
 927                         */
 928                        if (pr_reg->pr_res_holder)
 929                                core_scsi3_aptpl_reserve(dev, tpg,
 930                                                nacl, pr_reg);
 931                        /*
 932                         * Reenable pr_aptpl_active to accept new metadata
 933                         * updates once the SCSI device is active again..
 934                         */
 935                        spin_lock(&pr_tmpl->aptpl_reg_lock);
 936                        pr_tmpl->pr_aptpl_active = 1;
 937                }
 938        }
 939        spin_unlock(&pr_tmpl->aptpl_reg_lock);
 940
 941        return 0;
 942}
 943
 944int core_scsi3_check_aptpl_registration(
 945        struct se_device *dev,
 946        struct se_portal_group *tpg,
 947        struct se_lun *lun,
 948        struct se_lun_acl *lun_acl)
 949{
 950        struct se_node_acl *nacl = lun_acl->se_lun_nacl;
 951        struct se_dev_entry *deve = nacl->device_list[lun_acl->mapped_lun];
 952
 953        if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
 954                return 0;
 955
 956        return __core_scsi3_check_aptpl_registration(dev, tpg, lun,
 957                                lun->unpacked_lun, nacl, deve);
 958}
 959
 960static void __core_scsi3_dump_registration(
 961        struct target_core_fabric_ops *tfo,
 962        struct se_device *dev,
 963        struct se_node_acl *nacl,
 964        struct t10_pr_registration *pr_reg,
 965        int register_type)
 966{
 967        struct se_portal_group *se_tpg = nacl->se_tpg;
 968        char i_buf[PR_REG_ISID_ID_LEN];
 969        int prf_isid;
 970
 971        memset(&i_buf[0], 0, PR_REG_ISID_ID_LEN);
 972        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
 973                                PR_REG_ISID_ID_LEN);
 974
 975        pr_debug("SPC-3 PR [%s] Service Action: REGISTER%s Initiator"
 976                " Node: %s%s\n", tfo->get_fabric_name(), (register_type == 2) ?
 977                "_AND_MOVE" : (register_type == 1) ?
 978                "_AND_IGNORE_EXISTING_KEY" : "", nacl->initiatorname,
 979                (prf_isid) ? i_buf : "");
 980        pr_debug("SPC-3 PR [%s] registration on Target Port: %s,0x%04x\n",
 981                 tfo->get_fabric_name(), tfo->tpg_get_wwn(se_tpg),
 982                tfo->tpg_get_tag(se_tpg));
 983        pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
 984                " Port(s)\n",  tfo->get_fabric_name(),
 985                (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
 986                dev->transport->name);
 987        pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
 988                " 0x%08x  APTPL: %d\n", tfo->get_fabric_name(),
 989                pr_reg->pr_res_key, pr_reg->pr_res_generation,
 990                pr_reg->pr_reg_aptpl);
 991}
 992
 993/*
 994 * this function can be called with struct se_device->dev_reservation_lock
 995 * when register_move = 1
 996 */
 997static void __core_scsi3_add_registration(
 998        struct se_device *dev,
 999        struct se_node_acl *nacl,
1000        struct t10_pr_registration *pr_reg,
1001        int register_type,
1002        int register_move)
1003{
1004        struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
1005        struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1006        struct t10_reservation *pr_tmpl = &dev->t10_pr;
1007
1008        /*
1009         * Increment PRgeneration counter for struct se_device upon a successful
1010         * REGISTER, see spc4r17 section 6.3.2 READ_KEYS service action
1011         *
1012         * Also, when register_move = 1 for PROUT REGISTER_AND_MOVE service
1013         * action, the struct se_device->dev_reservation_lock will already be held,
1014         * so we do not call core_scsi3_pr_generation() which grabs the lock
1015         * for the REGISTER.
1016         */
1017        pr_reg->pr_res_generation = (register_move) ?
1018                        dev->t10_pr.pr_generation++ :
1019                        core_scsi3_pr_generation(dev);
1020
1021        spin_lock(&pr_tmpl->registration_lock);
1022        list_add_tail(&pr_reg->pr_reg_list, &pr_tmpl->registration_list);
1023        pr_reg->pr_reg_deve->def_pr_registered = 1;
1024
1025        __core_scsi3_dump_registration(tfo, dev, nacl, pr_reg, register_type);
1026        spin_unlock(&pr_tmpl->registration_lock);
1027        /*
1028         * Skip extra processing for ALL_TG_PT=0 or REGISTER_AND_MOVE.
1029         */
1030        if (!pr_reg->pr_reg_all_tg_pt || register_move)
1031                return;
1032        /*
1033         * Walk pr_reg->pr_reg_atp_list and add registrations for ALL_TG_PT=1
1034         * allocated in __core_scsi3_alloc_registration()
1035         */
1036        list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1037                        &pr_reg->pr_reg_atp_list, pr_reg_atp_mem_list) {
1038                list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1039
1040                pr_reg_tmp->pr_res_generation = core_scsi3_pr_generation(dev);
1041
1042                spin_lock(&pr_tmpl->registration_lock);
1043                list_add_tail(&pr_reg_tmp->pr_reg_list,
1044                              &pr_tmpl->registration_list);
1045                pr_reg_tmp->pr_reg_deve->def_pr_registered = 1;
1046
1047                __core_scsi3_dump_registration(tfo, dev,
1048                                pr_reg_tmp->pr_reg_nacl, pr_reg_tmp,
1049                                register_type);
1050                spin_unlock(&pr_tmpl->registration_lock);
1051                /*
1052                 * Drop configfs group dependency reference from
1053                 * __core_scsi3_alloc_registration()
1054                 */
1055                core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1056        }
1057}
1058
1059static int core_scsi3_alloc_registration(
1060        struct se_device *dev,
1061        struct se_node_acl *nacl,
1062        struct se_dev_entry *deve,
1063        unsigned char *isid,
1064        u64 sa_res_key,
1065        int all_tg_pt,
1066        int aptpl,
1067        int register_type,
1068        int register_move)
1069{
1070        struct t10_pr_registration *pr_reg;
1071
1072        pr_reg = __core_scsi3_alloc_registration(dev, nacl, deve, isid,
1073                        sa_res_key, all_tg_pt, aptpl);
1074        if (!pr_reg)
1075                return -EPERM;
1076
1077        __core_scsi3_add_registration(dev, nacl, pr_reg,
1078                        register_type, register_move);
1079        return 0;
1080}
1081
1082static struct t10_pr_registration *__core_scsi3_locate_pr_reg(
1083        struct se_device *dev,
1084        struct se_node_acl *nacl,
1085        unsigned char *isid)
1086{
1087        struct t10_reservation *pr_tmpl = &dev->t10_pr;
1088        struct t10_pr_registration *pr_reg, *pr_reg_tmp;
1089        struct se_portal_group *tpg;
1090
1091        spin_lock(&pr_tmpl->registration_lock);
1092        list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1093                        &pr_tmpl->registration_list, pr_reg_list) {
1094                /*
1095                 * First look for a matching struct se_node_acl
1096                 */
1097                if (pr_reg->pr_reg_nacl != nacl)
1098                        continue;
1099
1100                tpg = pr_reg->pr_reg_nacl->se_tpg;
1101                /*
1102                 * If this registration does NOT contain a fabric provided
1103                 * ISID, then we have found a match.
1104                 */
1105                if (!pr_reg->isid_present_at_reg) {
1106                        /*
1107                         * Determine if this SCSI device server requires that
1108                         * SCSI Intiatior TransportID w/ ISIDs is enforced
1109                         * for fabric modules (iSCSI) requiring them.
1110                         */
1111                        if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1112                                if (dev->dev_attrib.enforce_pr_isids)
1113                                        continue;
1114                        }
1115                        atomic_inc(&pr_reg->pr_res_holders);
1116                        smp_mb__after_atomic_inc();
1117                        spin_unlock(&pr_tmpl->registration_lock);
1118                        return pr_reg;
1119                }
1120                /*
1121                 * If the *pr_reg contains a fabric defined ISID for multi-value
1122                 * SCSI Initiator Port TransportIDs, then we expect a valid
1123                 * matching ISID to be provided by the local SCSI Initiator Port.
1124                 */
1125                if (!isid)
1126                        continue;
1127                if (strcmp(isid, pr_reg->pr_reg_isid))
1128                        continue;
1129
1130                atomic_inc(&pr_reg->pr_res_holders);
1131                smp_mb__after_atomic_inc();
1132                spin_unlock(&pr_tmpl->registration_lock);
1133                return pr_reg;
1134        }
1135        spin_unlock(&pr_tmpl->registration_lock);
1136
1137        return NULL;
1138}
1139
1140static struct t10_pr_registration *core_scsi3_locate_pr_reg(
1141        struct se_device *dev,
1142        struct se_node_acl *nacl,
1143        struct se_session *sess)
1144{
1145        struct se_portal_group *tpg = nacl->se_tpg;
1146        unsigned char buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
1147
1148        if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
1149                memset(&buf[0], 0, PR_REG_ISID_LEN);
1150                tpg->se_tpg_tfo->sess_get_initiator_sid(sess, &buf[0],
1151                                        PR_REG_ISID_LEN);
1152                isid_ptr = &buf[0];
1153        }
1154
1155        return __core_scsi3_locate_pr_reg(dev, nacl, isid_ptr);
1156}
1157
1158static void core_scsi3_put_pr_reg(struct t10_pr_registration *pr_reg)
1159{
1160        atomic_dec(&pr_reg->pr_res_holders);
1161        smp_mb__after_atomic_dec();
1162}
1163
1164static int core_scsi3_check_implict_release(
1165        struct se_device *dev,
1166        struct t10_pr_registration *pr_reg)
1167{
1168        struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
1169        struct t10_pr_registration *pr_res_holder;
1170        int ret = 0;
1171
1172        spin_lock(&dev->dev_reservation_lock);
1173        pr_res_holder = dev->dev_pr_res_holder;
1174        if (!pr_res_holder) {
1175                spin_unlock(&dev->dev_reservation_lock);
1176                return ret;
1177        }
1178        if (pr_res_holder == pr_reg) {
1179                /*
1180                 * Perform an implict RELEASE if the registration that
1181                 * is being released is holding the reservation.
1182                 *
1183                 * From spc4r17, section 5.7.11.1:
1184                 *
1185                 * e) If the I_T nexus is the persistent reservation holder
1186                 *    and the persistent reservation is not an all registrants
1187                 *    type, then a PERSISTENT RESERVE OUT command with REGISTER
1188                 *    service action or REGISTER AND  IGNORE EXISTING KEY
1189                 *    service action with the SERVICE ACTION RESERVATION KEY
1190                 *    field set to zero (see 5.7.11.3).
1191                 */
1192                __core_scsi3_complete_pro_release(dev, nacl, pr_reg, 0);
1193                ret = 1;
1194                /*
1195                 * For 'All Registrants' reservation types, all existing
1196                 * registrations are still processed as reservation holders
1197                 * in core_scsi3_pr_seq_non_holder() after the initial
1198                 * reservation holder is implictly released here.
1199                 */
1200        } else if (pr_reg->pr_reg_all_tg_pt &&
1201                  (!strcmp(pr_res_holder->pr_reg_nacl->initiatorname,
1202                          pr_reg->pr_reg_nacl->initiatorname)) &&
1203                  (pr_res_holder->pr_res_key == pr_reg->pr_res_key)) {
1204                pr_err("SPC-3 PR: Unable to perform ALL_TG_PT=1"
1205                        " UNREGISTER while existing reservation with matching"
1206                        " key 0x%016Lx is present from another SCSI Initiator"
1207                        " Port\n", pr_reg->pr_res_key);
1208                ret = -EPERM;
1209        }
1210        spin_unlock(&dev->dev_reservation_lock);
1211
1212        return ret;
1213}
1214
1215/*
1216 * Called with struct t10_reservation->registration_lock held.
1217 */
1218static void __core_scsi3_free_registration(
1219        struct se_device *dev,
1220        struct t10_pr_registration *pr_reg,
1221        struct list_head *preempt_and_abort_list,
1222        int dec_holders)
1223{
1224        struct target_core_fabric_ops *tfo =
1225                        pr_reg->pr_reg_nacl->se_tpg->se_tpg_tfo;
1226        struct t10_reservation *pr_tmpl = &dev->t10_pr;
1227        char i_buf[PR_REG_ISID_ID_LEN];
1228        int prf_isid;
1229
1230        memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1231        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
1232                                PR_REG_ISID_ID_LEN);
1233
1234        pr_reg->pr_reg_deve->def_pr_registered = 0;
1235        pr_reg->pr_reg_deve->pr_res_key = 0;
1236        list_del(&pr_reg->pr_reg_list);
1237        /*
1238         * Caller accessing *pr_reg using core_scsi3_locate_pr_reg(),
1239         * so call core_scsi3_put_pr_reg() to decrement our reference.
1240         */
1241        if (dec_holders)
1242                core_scsi3_put_pr_reg(pr_reg);
1243        /*
1244         * Wait until all reference from any other I_T nexuses for this
1245         * *pr_reg have been released.  Because list_del() is called above,
1246         * the last core_scsi3_put_pr_reg(pr_reg) will release this reference
1247         * count back to zero, and we release *pr_reg.
1248         */
1249        while (atomic_read(&pr_reg->pr_res_holders) != 0) {
1250                spin_unlock(&pr_tmpl->registration_lock);
1251                pr_debug("SPC-3 PR [%s] waiting for pr_res_holders\n",
1252                                tfo->get_fabric_name());
1253                cpu_relax();
1254                spin_lock(&pr_tmpl->registration_lock);
1255        }
1256
1257        pr_debug("SPC-3 PR [%s] Service Action: UNREGISTER Initiator"
1258                " Node: %s%s\n", tfo->get_fabric_name(),
1259                pr_reg->pr_reg_nacl->initiatorname,
1260                (prf_isid) ? &i_buf[0] : "");
1261        pr_debug("SPC-3 PR [%s] for %s TCM Subsystem %s Object Target"
1262                " Port(s)\n", tfo->get_fabric_name(),
1263                (pr_reg->pr_reg_all_tg_pt) ? "ALL" : "SINGLE",
1264                dev->transport->name);
1265        pr_debug("SPC-3 PR [%s] SA Res Key: 0x%016Lx PRgeneration:"
1266                " 0x%08x\n", tfo->get_fabric_name(), pr_reg->pr_res_key,
1267                pr_reg->pr_res_generation);
1268
1269        if (!preempt_and_abort_list) {
1270                pr_reg->pr_reg_deve = NULL;
1271                pr_reg->pr_reg_nacl = NULL;
1272                kfree(pr_reg->pr_aptpl_buf);
1273                kmem_cache_free(t10_pr_reg_cache, pr_reg);
1274                return;
1275        }
1276        /*
1277         * For PREEMPT_AND_ABORT, the list of *pr_reg in preempt_and_abort_list
1278         * are released once the ABORT_TASK_SET has completed..
1279         */
1280        list_add_tail(&pr_reg->pr_reg_abort_list, preempt_and_abort_list);
1281}
1282
1283void core_scsi3_free_pr_reg_from_nacl(
1284        struct se_device *dev,
1285        struct se_node_acl *nacl)
1286{
1287        struct t10_reservation *pr_tmpl = &dev->t10_pr;
1288        struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1289        /*
1290         * If the passed se_node_acl matches the reservation holder,
1291         * release the reservation.
1292         */
1293        spin_lock(&dev->dev_reservation_lock);
1294        pr_res_holder = dev->dev_pr_res_holder;
1295        if ((pr_res_holder != NULL) &&
1296            (pr_res_holder->pr_reg_nacl == nacl))
1297                __core_scsi3_complete_pro_release(dev, nacl, pr_res_holder, 0);
1298        spin_unlock(&dev->dev_reservation_lock);
1299        /*
1300         * Release any registration associated with the struct se_node_acl.
1301         */
1302        spin_lock(&pr_tmpl->registration_lock);
1303        list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1304                        &pr_tmpl->registration_list, pr_reg_list) {
1305
1306                if (pr_reg->pr_reg_nacl != nacl)
1307                        continue;
1308
1309                __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1310        }
1311        spin_unlock(&pr_tmpl->registration_lock);
1312}
1313
1314void core_scsi3_free_all_registrations(
1315        struct se_device *dev)
1316{
1317        struct t10_reservation *pr_tmpl = &dev->t10_pr;
1318        struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_res_holder;
1319
1320        spin_lock(&dev->dev_reservation_lock);
1321        pr_res_holder = dev->dev_pr_res_holder;
1322        if (pr_res_holder != NULL) {
1323                struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
1324                __core_scsi3_complete_pro_release(dev, pr_res_nacl,
1325                                pr_res_holder, 0);
1326        }
1327        spin_unlock(&dev->dev_reservation_lock);
1328
1329        spin_lock(&pr_tmpl->registration_lock);
1330        list_for_each_entry_safe(pr_reg, pr_reg_tmp,
1331                        &pr_tmpl->registration_list, pr_reg_list) {
1332
1333                __core_scsi3_free_registration(dev, pr_reg, NULL, 0);
1334        }
1335        spin_unlock(&pr_tmpl->registration_lock);
1336
1337        spin_lock(&pr_tmpl->aptpl_reg_lock);
1338        list_for_each_entry_safe(pr_reg, pr_reg_tmp, &pr_tmpl->aptpl_reg_list,
1339                                pr_reg_aptpl_list) {
1340                list_del(&pr_reg->pr_reg_aptpl_list);
1341                kfree(pr_reg->pr_aptpl_buf);
1342                kmem_cache_free(t10_pr_reg_cache, pr_reg);
1343        }
1344        spin_unlock(&pr_tmpl->aptpl_reg_lock);
1345}
1346
1347static int core_scsi3_tpg_depend_item(struct se_portal_group *tpg)
1348{
1349        return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1350                        &tpg->tpg_group.cg_item);
1351}
1352
1353static void core_scsi3_tpg_undepend_item(struct se_portal_group *tpg)
1354{
1355        configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1356                        &tpg->tpg_group.cg_item);
1357
1358        atomic_dec(&tpg->tpg_pr_ref_count);
1359        smp_mb__after_atomic_dec();
1360}
1361
1362static int core_scsi3_nodeacl_depend_item(struct se_node_acl *nacl)
1363{
1364        struct se_portal_group *tpg = nacl->se_tpg;
1365
1366        if (nacl->dynamic_node_acl)
1367                return 0;
1368
1369        return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1370                        &nacl->acl_group.cg_item);
1371}
1372
1373static void core_scsi3_nodeacl_undepend_item(struct se_node_acl *nacl)
1374{
1375        struct se_portal_group *tpg = nacl->se_tpg;
1376
1377        if (nacl->dynamic_node_acl) {
1378                atomic_dec(&nacl->acl_pr_ref_count);
1379                smp_mb__after_atomic_dec();
1380                return;
1381        }
1382
1383        configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1384                        &nacl->acl_group.cg_item);
1385
1386        atomic_dec(&nacl->acl_pr_ref_count);
1387        smp_mb__after_atomic_dec();
1388}
1389
1390static int core_scsi3_lunacl_depend_item(struct se_dev_entry *se_deve)
1391{
1392        struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1393        struct se_node_acl *nacl;
1394        struct se_portal_group *tpg;
1395        /*
1396         * For nacl->dynamic_node_acl=1
1397         */
1398        if (!lun_acl)
1399                return 0;
1400
1401        nacl = lun_acl->se_lun_nacl;
1402        tpg = nacl->se_tpg;
1403
1404        return configfs_depend_item(tpg->se_tpg_tfo->tf_subsys,
1405                        &lun_acl->se_lun_group.cg_item);
1406}
1407
1408static void core_scsi3_lunacl_undepend_item(struct se_dev_entry *se_deve)
1409{
1410        struct se_lun_acl *lun_acl = se_deve->se_lun_acl;
1411        struct se_node_acl *nacl;
1412        struct se_portal_group *tpg;
1413        /*
1414         * For nacl->dynamic_node_acl=1
1415         */
1416        if (!lun_acl) {
1417                atomic_dec(&se_deve->pr_ref_count);
1418                smp_mb__after_atomic_dec();
1419                return;
1420        }
1421        nacl = lun_acl->se_lun_nacl;
1422        tpg = nacl->se_tpg;
1423
1424        configfs_undepend_item(tpg->se_tpg_tfo->tf_subsys,
1425                        &lun_acl->se_lun_group.cg_item);
1426
1427        atomic_dec(&se_deve->pr_ref_count);
1428        smp_mb__after_atomic_dec();
1429}
1430
1431static sense_reason_t
1432core_scsi3_decode_spec_i_port(
1433        struct se_cmd *cmd,
1434        struct se_portal_group *tpg,
1435        unsigned char *l_isid,
1436        u64 sa_res_key,
1437        int all_tg_pt,
1438        int aptpl)
1439{
1440        struct se_device *dev = cmd->se_dev;
1441        struct se_port *tmp_port;
1442        struct se_portal_group *dest_tpg = NULL, *tmp_tpg;
1443        struct se_session *se_sess = cmd->se_sess;
1444        struct se_node_acl *dest_node_acl = NULL;
1445        struct se_dev_entry *dest_se_deve = NULL, *local_se_deve;
1446        struct t10_pr_registration *dest_pr_reg, *local_pr_reg, *pr_reg_e;
1447        struct t10_pr_registration *pr_reg_tmp, *pr_reg_tmp_safe;
1448        LIST_HEAD(tid_dest_list);
1449        struct pr_transport_id_holder *tidh_new, *tidh, *tidh_tmp;
1450        struct target_core_fabric_ops *tmp_tf_ops;
1451        unsigned char *buf;
1452        unsigned char *ptr, *i_str = NULL, proto_ident, tmp_proto_ident;
1453        char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
1454        sense_reason_t ret;
1455        u32 tpdl, tid_len = 0;
1456        int dest_local_nexus, prf_isid;
1457        u32 dest_rtpi = 0;
1458
1459        memset(dest_iport, 0, 64);
1460
1461        local_se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
1462        /*
1463         * Allocate a struct pr_transport_id_holder and setup the
1464         * local_node_acl and local_se_deve pointers and add to
1465         * struct list_head tid_dest_list for add registration
1466         * processing in the loop of tid_dest_list below.
1467         */
1468        tidh_new = kzalloc(sizeof(struct pr_transport_id_holder), GFP_KERNEL);
1469        if (!tidh_new) {
1470                pr_err("Unable to allocate tidh_new\n");
1471                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1472        }
1473        INIT_LIST_HEAD(&tidh_new->dest_list);
1474        tidh_new->dest_tpg = tpg;
1475        tidh_new->dest_node_acl = se_sess->se_node_acl;
1476        tidh_new->dest_se_deve = local_se_deve;
1477
1478        local_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1479                                se_sess->se_node_acl, local_se_deve, l_isid,
1480                                sa_res_key, all_tg_pt, aptpl);
1481        if (!local_pr_reg) {
1482                kfree(tidh_new);
1483                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1484        }
1485        tidh_new->dest_pr_reg = local_pr_reg;
1486        /*
1487         * The local I_T nexus does not hold any configfs dependances,
1488         * so we set tid_h->dest_local_nexus=1 to prevent the
1489         * configfs_undepend_item() calls in the tid_dest_list loops below.
1490         */
1491        tidh_new->dest_local_nexus = 1;
1492        list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1493
1494        if (cmd->data_length < 28) {
1495                pr_warn("SPC-PR: Received PR OUT parameter list"
1496                        " length too small: %u\n", cmd->data_length);
1497                ret = TCM_INVALID_PARAMETER_LIST;
1498                goto out;
1499        }
1500
1501        buf = transport_kmap_data_sg(cmd);
1502        if (!buf) {
1503                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1504                goto out;
1505        }
1506
1507        /*
1508         * For a PERSISTENT RESERVE OUT specify initiator ports payload,
1509         * first extract TransportID Parameter Data Length, and make sure
1510         * the value matches up to the SCSI expected data transfer length.
1511         */
1512        tpdl = (buf[24] & 0xff) << 24;
1513        tpdl |= (buf[25] & 0xff) << 16;
1514        tpdl |= (buf[26] & 0xff) << 8;
1515        tpdl |= buf[27] & 0xff;
1516
1517        if ((tpdl + 28) != cmd->data_length) {
1518                pr_err("SPC-3 PR: Illegal tpdl: %u + 28 byte header"
1519                        " does not equal CDB data_length: %u\n", tpdl,
1520                        cmd->data_length);
1521                ret = TCM_INVALID_PARAMETER_LIST;
1522                goto out_unmap;
1523        }
1524        /*
1525         * Start processing the received transport IDs using the
1526         * receiving I_T Nexus portal's fabric dependent methods to
1527         * obtain the SCSI Initiator Port/Device Identifiers.
1528         */
1529        ptr = &buf[28];
1530
1531        while (tpdl > 0) {
1532                proto_ident = (ptr[0] & 0x0f);
1533                dest_tpg = NULL;
1534
1535                spin_lock(&dev->se_port_lock);
1536                list_for_each_entry(tmp_port, &dev->dev_sep_list, sep_list) {
1537                        tmp_tpg = tmp_port->sep_tpg;
1538                        if (!tmp_tpg)
1539                                continue;
1540                        tmp_tf_ops = tmp_tpg->se_tpg_tfo;
1541                        if (!tmp_tf_ops)
1542                                continue;
1543                        if (!tmp_tf_ops->get_fabric_proto_ident ||
1544                            !tmp_tf_ops->tpg_parse_pr_out_transport_id)
1545                                continue;
1546                        /*
1547                         * Look for the matching proto_ident provided by
1548                         * the received TransportID
1549                         */
1550                        tmp_proto_ident = tmp_tf_ops->get_fabric_proto_ident(tmp_tpg);
1551                        if (tmp_proto_ident != proto_ident)
1552                                continue;
1553                        dest_rtpi = tmp_port->sep_rtpi;
1554
1555                        i_str = tmp_tf_ops->tpg_parse_pr_out_transport_id(
1556                                        tmp_tpg, (const char *)ptr, &tid_len,
1557                                        &iport_ptr);
1558                        if (!i_str)
1559                                continue;
1560
1561                        atomic_inc(&tmp_tpg->tpg_pr_ref_count);
1562                        smp_mb__after_atomic_inc();
1563                        spin_unlock(&dev->se_port_lock);
1564
1565                        if (core_scsi3_tpg_depend_item(tmp_tpg)) {
1566                                pr_err(" core_scsi3_tpg_depend_item()"
1567                                        " for tmp_tpg\n");
1568                                atomic_dec(&tmp_tpg->tpg_pr_ref_count);
1569                                smp_mb__after_atomic_dec();
1570                                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1571                                goto out_unmap;
1572                        }
1573                        /*
1574                         * Locate the destination initiator ACL to be registered
1575                         * from the decoded fabric module specific TransportID
1576                         * at *i_str.
1577                         */
1578                        spin_lock_irq(&tmp_tpg->acl_node_lock);
1579                        dest_node_acl = __core_tpg_get_initiator_node_acl(
1580                                                tmp_tpg, i_str);
1581                        if (dest_node_acl) {
1582                                atomic_inc(&dest_node_acl->acl_pr_ref_count);
1583                                smp_mb__after_atomic_inc();
1584                        }
1585                        spin_unlock_irq(&tmp_tpg->acl_node_lock);
1586
1587                        if (!dest_node_acl) {
1588                                core_scsi3_tpg_undepend_item(tmp_tpg);
1589                                spin_lock(&dev->se_port_lock);
1590                                continue;
1591                        }
1592
1593                        if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
1594                                pr_err("configfs_depend_item() failed"
1595                                        " for dest_node_acl->acl_group\n");
1596                                atomic_dec(&dest_node_acl->acl_pr_ref_count);
1597                                smp_mb__after_atomic_dec();
1598                                core_scsi3_tpg_undepend_item(tmp_tpg);
1599                                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1600                                goto out_unmap;
1601                        }
1602
1603                        dest_tpg = tmp_tpg;
1604                        pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node:"
1605                                " %s Port RTPI: %hu\n",
1606                                dest_tpg->se_tpg_tfo->get_fabric_name(),
1607                                dest_node_acl->initiatorname, dest_rtpi);
1608
1609                        spin_lock(&dev->se_port_lock);
1610                        break;
1611                }
1612                spin_unlock(&dev->se_port_lock);
1613
1614                if (!dest_tpg) {
1615                        pr_err("SPC-3 PR SPEC_I_PT: Unable to locate"
1616                                        " dest_tpg\n");
1617                        ret = TCM_INVALID_PARAMETER_LIST;
1618                        goto out_unmap;
1619                }
1620
1621                pr_debug("SPC-3 PR SPEC_I_PT: Got %s data_length: %u tpdl: %u"
1622                        " tid_len: %d for %s + %s\n",
1623                        dest_tpg->se_tpg_tfo->get_fabric_name(), cmd->data_length,
1624                        tpdl, tid_len, i_str, iport_ptr);
1625
1626                if (tid_len > tpdl) {
1627                        pr_err("SPC-3 PR SPEC_I_PT: Illegal tid_len:"
1628                                " %u for Transport ID: %s\n", tid_len, ptr);
1629                        core_scsi3_nodeacl_undepend_item(dest_node_acl);
1630                        core_scsi3_tpg_undepend_item(dest_tpg);
1631                        ret = TCM_INVALID_PARAMETER_LIST;
1632                        goto out_unmap;
1633                }
1634                /*
1635                 * Locate the desintation struct se_dev_entry pointer for matching
1636                 * RELATIVE TARGET PORT IDENTIFIER on the receiving I_T Nexus
1637                 * Target Port.
1638                 */
1639                dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl,
1640                                        dest_rtpi);
1641                if (!dest_se_deve) {
1642                        pr_err("Unable to locate %s dest_se_deve"
1643                                " from destination RTPI: %hu\n",
1644                                dest_tpg->se_tpg_tfo->get_fabric_name(),
1645                                dest_rtpi);
1646
1647                        core_scsi3_nodeacl_undepend_item(dest_node_acl);
1648                        core_scsi3_tpg_undepend_item(dest_tpg);
1649                        ret = TCM_INVALID_PARAMETER_LIST;
1650                        goto out_unmap;
1651                }
1652
1653                if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
1654                        pr_err("core_scsi3_lunacl_depend_item()"
1655                                        " failed\n");
1656                        atomic_dec(&dest_se_deve->pr_ref_count);
1657                        smp_mb__after_atomic_dec();
1658                        core_scsi3_nodeacl_undepend_item(dest_node_acl);
1659                        core_scsi3_tpg_undepend_item(dest_tpg);
1660                        ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1661                        goto out_unmap;
1662                }
1663
1664                pr_debug("SPC-3 PR SPEC_I_PT: Located %s Node: %s"
1665                        " dest_se_deve mapped_lun: %u\n",
1666                        dest_tpg->se_tpg_tfo->get_fabric_name(),
1667                        dest_node_acl->initiatorname, dest_se_deve->mapped_lun);
1668
1669                /*
1670                 * Skip any TransportIDs that already have a registration for
1671                 * this target port.
1672                 */
1673                pr_reg_e = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
1674                                        iport_ptr);
1675                if (pr_reg_e) {
1676                        core_scsi3_put_pr_reg(pr_reg_e);
1677                        core_scsi3_lunacl_undepend_item(dest_se_deve);
1678                        core_scsi3_nodeacl_undepend_item(dest_node_acl);
1679                        core_scsi3_tpg_undepend_item(dest_tpg);
1680                        ptr += tid_len;
1681                        tpdl -= tid_len;
1682                        tid_len = 0;
1683                        continue;
1684                }
1685                /*
1686                 * Allocate a struct pr_transport_id_holder and setup
1687                 * the dest_node_acl and dest_se_deve pointers for the
1688                 * loop below.
1689                 */
1690                tidh_new = kzalloc(sizeof(struct pr_transport_id_holder),
1691                                GFP_KERNEL);
1692                if (!tidh_new) {
1693                        pr_err("Unable to allocate tidh_new\n");
1694                        core_scsi3_lunacl_undepend_item(dest_se_deve);
1695                        core_scsi3_nodeacl_undepend_item(dest_node_acl);
1696                        core_scsi3_tpg_undepend_item(dest_tpg);
1697                        ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1698                        goto out_unmap;
1699                }
1700                INIT_LIST_HEAD(&tidh_new->dest_list);
1701                tidh_new->dest_tpg = dest_tpg;
1702                tidh_new->dest_node_acl = dest_node_acl;
1703                tidh_new->dest_se_deve = dest_se_deve;
1704
1705                /*
1706                 * Allocate, but do NOT add the registration for the
1707                 * TransportID referenced SCSI Initiator port.  This
1708                 * done because of the following from spc4r17 in section
1709                 * 6.14.3 wrt SPEC_I_PT:
1710                 *
1711                 * "If a registration fails for any initiator port (e.g., if th
1712                 * logical unit does not have enough resources available to
1713                 * hold the registration information), no registrations shall be
1714                 * made, and the command shall be terminated with
1715                 * CHECK CONDITION status."
1716                 *
1717                 * That means we call __core_scsi3_alloc_registration() here,
1718                 * and then call __core_scsi3_add_registration() in the
1719                 * 2nd loop which will never fail.
1720                 */
1721                dest_pr_reg = __core_scsi3_alloc_registration(cmd->se_dev,
1722                                dest_node_acl, dest_se_deve, iport_ptr,
1723                                sa_res_key, all_tg_pt, aptpl);
1724                if (!dest_pr_reg) {
1725                        core_scsi3_lunacl_undepend_item(dest_se_deve);
1726                        core_scsi3_nodeacl_undepend_item(dest_node_acl);
1727                        core_scsi3_tpg_undepend_item(dest_tpg);
1728                        kfree(tidh_new);
1729                        ret = TCM_INVALID_PARAMETER_LIST;
1730                        goto out_unmap;
1731                }
1732                tidh_new->dest_pr_reg = dest_pr_reg;
1733                list_add_tail(&tidh_new->dest_list, &tid_dest_list);
1734
1735                ptr += tid_len;
1736                tpdl -= tid_len;
1737                tid_len = 0;
1738
1739        }
1740
1741        transport_kunmap_data_sg(cmd);
1742
1743        /*
1744         * Go ahead and create a registrations from tid_dest_list for the
1745         * SPEC_I_PT provided TransportID for the *tidh referenced dest_node_acl
1746         * and dest_se_deve.
1747         *
1748         * The SA Reservation Key from the PROUT is set for the
1749         * registration, and ALL_TG_PT is also passed.  ALL_TG_PT=1
1750         * means that the TransportID Initiator port will be
1751         * registered on all of the target ports in the SCSI target device
1752         * ALL_TG_PT=0 means the registration will only be for the
1753         * SCSI target port the PROUT REGISTER with SPEC_I_PT=1
1754         * was received.
1755         */
1756        list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1757                dest_tpg = tidh->dest_tpg;
1758                dest_node_acl = tidh->dest_node_acl;
1759                dest_se_deve = tidh->dest_se_deve;
1760                dest_pr_reg = tidh->dest_pr_reg;
1761                dest_local_nexus = tidh->dest_local_nexus;
1762
1763                list_del(&tidh->dest_list);
1764                kfree(tidh);
1765
1766                memset(i_buf, 0, PR_REG_ISID_ID_LEN);
1767                prf_isid = core_pr_dump_initiator_port(dest_pr_reg, &i_buf[0],
1768                                                PR_REG_ISID_ID_LEN);
1769
1770                __core_scsi3_add_registration(cmd->se_dev, dest_node_acl,
1771                                        dest_pr_reg, 0, 0);
1772
1773                pr_debug("SPC-3 PR [%s] SPEC_I_PT: Successfully"
1774                        " registered Transport ID for Node: %s%s Mapped LUN:"
1775                        " %u\n", dest_tpg->se_tpg_tfo->get_fabric_name(),
1776                        dest_node_acl->initiatorname, (prf_isid) ?
1777                        &i_buf[0] : "", dest_se_deve->mapped_lun);
1778
1779                if (dest_local_nexus)
1780                        continue;
1781
1782                core_scsi3_lunacl_undepend_item(dest_se_deve);
1783                core_scsi3_nodeacl_undepend_item(dest_node_acl);
1784                core_scsi3_tpg_undepend_item(dest_tpg);
1785        }
1786
1787        return 0;
1788out_unmap:
1789        transport_kunmap_data_sg(cmd);
1790out:
1791        /*
1792         * For the failure case, release everything from tid_dest_list
1793         * including *dest_pr_reg and the configfs dependances..
1794         */
1795        list_for_each_entry_safe(tidh, tidh_tmp, &tid_dest_list, dest_list) {
1796                dest_tpg = tidh->dest_tpg;
1797                dest_node_acl = tidh->dest_node_acl;
1798                dest_se_deve = tidh->dest_se_deve;
1799                dest_pr_reg = tidh->dest_pr_reg;
1800                dest_local_nexus = tidh->dest_local_nexus;
1801
1802                list_del(&tidh->dest_list);
1803                kfree(tidh);
1804                /*
1805                 * Release any extra ALL_TG_PT=1 registrations for
1806                 * the SPEC_I_PT=1 case.
1807                 */
1808                list_for_each_entry_safe(pr_reg_tmp, pr_reg_tmp_safe,
1809                                &dest_pr_reg->pr_reg_atp_list,
1810                                pr_reg_atp_mem_list) {
1811                        list_del(&pr_reg_tmp->pr_reg_atp_mem_list);
1812                        core_scsi3_lunacl_undepend_item(pr_reg_tmp->pr_reg_deve);
1813                        kmem_cache_free(t10_pr_reg_cache, pr_reg_tmp);
1814                }
1815
1816                kfree(dest_pr_reg->pr_aptpl_buf);
1817                kmem_cache_free(t10_pr_reg_cache, dest_pr_reg);
1818
1819                if (dest_local_nexus)
1820                        continue;
1821
1822                core_scsi3_lunacl_undepend_item(dest_se_deve);
1823                core_scsi3_nodeacl_undepend_item(dest_node_acl);
1824                core_scsi3_tpg_undepend_item(dest_tpg);
1825        }
1826        return ret;
1827}
1828
1829/*
1830 * Called with struct se_device->dev_reservation_lock held
1831 */
1832static int __core_scsi3_update_aptpl_buf(
1833        struct se_device *dev,
1834        unsigned char *buf,
1835        u32 pr_aptpl_buf_len,
1836        int clear_aptpl_metadata)
1837{
1838        struct se_lun *lun;
1839        struct se_portal_group *tpg;
1840        struct t10_pr_registration *pr_reg;
1841        unsigned char tmp[512], isid_buf[32];
1842        ssize_t len = 0;
1843        int reg_count = 0;
1844
1845        memset(buf, 0, pr_aptpl_buf_len);
1846        /*
1847         * Called to clear metadata once APTPL has been deactivated.
1848         */
1849        if (clear_aptpl_metadata) {
1850                snprintf(buf, pr_aptpl_buf_len,
1851                                "No Registrations or Reservations\n");
1852                return 0;
1853        }
1854        /*
1855         * Walk the registration list..
1856         */
1857        spin_lock(&dev->t10_pr.registration_lock);
1858        list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
1859                        pr_reg_list) {
1860
1861                tmp[0] = '\0';
1862                isid_buf[0] = '\0';
1863                tpg = pr_reg->pr_reg_nacl->se_tpg;
1864                lun = pr_reg->pr_reg_tg_pt_lun;
1865                /*
1866                 * Write out any ISID value to APTPL metadata that was included
1867                 * in the original registration.
1868                 */
1869                if (pr_reg->isid_present_at_reg)
1870                        snprintf(isid_buf, 32, "initiator_sid=%s\n",
1871                                        pr_reg->pr_reg_isid);
1872                /*
1873                 * Include special metadata if the pr_reg matches the
1874                 * reservation holder.
1875                 */
1876                if (dev->dev_pr_res_holder == pr_reg) {
1877                        snprintf(tmp, 512, "PR_REG_START: %d"
1878                                "\ninitiator_fabric=%s\n"
1879                                "initiator_node=%s\n%s"
1880                                "sa_res_key=%llu\n"
1881                                "res_holder=1\nres_type=%02x\n"
1882                                "res_scope=%02x\nres_all_tg_pt=%d\n"
1883                                "mapped_lun=%u\n", reg_count,
1884                                tpg->se_tpg_tfo->get_fabric_name(),
1885                                pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1886                                pr_reg->pr_res_key, pr_reg->pr_res_type,
1887                                pr_reg->pr_res_scope, pr_reg->pr_reg_all_tg_pt,
1888                                pr_reg->pr_res_mapped_lun);
1889                } else {
1890                        snprintf(tmp, 512, "PR_REG_START: %d\n"
1891                                "initiator_fabric=%s\ninitiator_node=%s\n%s"
1892                                "sa_res_key=%llu\nres_holder=0\n"
1893                                "res_all_tg_pt=%d\nmapped_lun=%u\n",
1894                                reg_count, tpg->se_tpg_tfo->get_fabric_name(),
1895                                pr_reg->pr_reg_nacl->initiatorname, isid_buf,
1896                                pr_reg->pr_res_key, pr_reg->pr_reg_all_tg_pt,
1897                                pr_reg->pr_res_mapped_lun);
1898                }
1899
1900                if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1901                        pr_err("Unable to update renaming"
1902                                " APTPL metadata\n");
1903                        spin_unlock(&dev->t10_pr.registration_lock);
1904                        return -EMSGSIZE;
1905                }
1906                len += sprintf(buf+len, "%s", tmp);
1907
1908                /*
1909                 * Include information about the associated SCSI target port.
1910                 */
1911                snprintf(tmp, 512, "target_fabric=%s\ntarget_node=%s\n"
1912                        "tpgt=%hu\nport_rtpi=%hu\ntarget_lun=%u\nPR_REG_END:"
1913                        " %d\n", tpg->se_tpg_tfo->get_fabric_name(),
1914                        tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1915                        tpg->se_tpg_tfo->tpg_get_tag(tpg),
1916                        lun->lun_sep->sep_rtpi, lun->unpacked_lun, reg_count);
1917
1918                if ((len + strlen(tmp) >= pr_aptpl_buf_len)) {
1919                        pr_err("Unable to update renaming"
1920                                " APTPL metadata\n");
1921                        spin_unlock(&dev->t10_pr.registration_lock);
1922                        return -EMSGSIZE;
1923                }
1924                len += sprintf(buf+len, "%s", tmp);
1925                reg_count++;
1926        }
1927        spin_unlock(&dev->t10_pr.registration_lock);
1928
1929        if (!reg_count)
1930                len += sprintf(buf+len, "No Registrations or Reservations");
1931
1932        return 0;
1933}
1934
1935static int core_scsi3_update_aptpl_buf(
1936        struct se_device *dev,
1937        unsigned char *buf,
1938        u32 pr_aptpl_buf_len,
1939        int clear_aptpl_metadata)
1940{
1941        int ret;
1942
1943        spin_lock(&dev->dev_reservation_lock);
1944        ret = __core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
1945                                clear_aptpl_metadata);
1946        spin_unlock(&dev->dev_reservation_lock);
1947
1948        return ret;
1949}
1950
1951/*
1952 * Called with struct se_device->aptpl_file_mutex held
1953 */
1954static int __core_scsi3_write_aptpl_to_file(
1955        struct se_device *dev,
1956        unsigned char *buf,
1957        u32 pr_aptpl_buf_len)
1958{
1959        struct t10_wwn *wwn = &dev->t10_wwn;
1960        struct file *file;
1961        int flags = O_RDWR | O_CREAT | O_TRUNC;
1962        char path[512];
1963        int ret;
1964
1965        memset(path, 0, 512);
1966
1967        if (strlen(&wwn->unit_serial[0]) >= 512) {
1968                pr_err("WWN value for struct se_device does not fit"
1969                        " into path buffer\n");
1970                return -EMSGSIZE;
1971        }
1972
1973        snprintf(path, 512, "/var/target/pr/aptpl_%s", &wwn->unit_serial[0]);
1974        file = filp_open(path, flags, 0600);
1975        if (IS_ERR(file)) {
1976                pr_err("filp_open(%s) for APTPL metadata"
1977                        " failed\n", path);
1978                return PTR_ERR(file);
1979        }
1980
1981        if (!pr_aptpl_buf_len)
1982                pr_aptpl_buf_len = (strlen(&buf[0]) + 1); /* Add extra for NULL */
1983
1984        ret = kernel_write(file, buf, pr_aptpl_buf_len, 0);
1985
1986        if (ret < 0)
1987                pr_debug("Error writing APTPL metadata file: %s\n", path);
1988        fput(file);
1989
1990        return ret ? -EIO : 0;
1991}
1992
1993static int
1994core_scsi3_update_and_write_aptpl(struct se_device *dev, unsigned char *in_buf,
1995                u32 in_pr_aptpl_buf_len)
1996{
1997        unsigned char null_buf[64], *buf;
1998        u32 pr_aptpl_buf_len;
1999        int clear_aptpl_metadata = 0;
2000        int ret;
2001
2002        /*
2003         * Can be called with a NULL pointer from PROUT service action CLEAR
2004         */
2005        if (!in_buf) {
2006                memset(null_buf, 0, 64);
2007                buf = &null_buf[0];
2008                /*
2009                 * This will clear the APTPL metadata to:
2010                 * "No Registrations or Reservations" status
2011                 */
2012                pr_aptpl_buf_len = 64;
2013                clear_aptpl_metadata = 1;
2014        } else {
2015                buf = in_buf;
2016                pr_aptpl_buf_len = in_pr_aptpl_buf_len;
2017        }
2018
2019        ret = core_scsi3_update_aptpl_buf(dev, buf, pr_aptpl_buf_len,
2020                                clear_aptpl_metadata);
2021        if (ret != 0)
2022                return ret;
2023
2024        /*
2025         * __core_scsi3_write_aptpl_to_file() will call strlen()
2026         * on the passed buf to determine pr_aptpl_buf_len.
2027         */
2028        return __core_scsi3_write_aptpl_to_file(dev, buf, 0);
2029}
2030
2031static sense_reason_t
2032core_scsi3_emulate_pro_register(struct se_cmd *cmd, u64 res_key, u64 sa_res_key,
2033                int aptpl, int all_tg_pt, int spec_i_pt, int ignore_key)
2034{
2035        struct se_session *se_sess = cmd->se_sess;
2036        struct se_device *dev = cmd->se_dev;
2037        struct se_dev_entry *se_deve;
2038        struct se_lun *se_lun = cmd->se_lun;
2039        struct se_portal_group *se_tpg;
2040        struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_reg_tmp, *pr_reg_e;
2041        struct t10_reservation *pr_tmpl = &dev->t10_pr;
2042        /* Used for APTPL metadata w/ UNREGISTER */
2043        unsigned char *pr_aptpl_buf = NULL;
2044        unsigned char isid_buf[PR_REG_ISID_LEN], *isid_ptr = NULL;
2045        sense_reason_t ret = TCM_NO_SENSE;
2046        int pr_holder = 0, type;
2047
2048        if (!se_sess || !se_lun) {
2049                pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2050                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2051        }
2052        se_tpg = se_sess->se_tpg;
2053        se_deve = se_sess->se_node_acl->device_list[cmd->orig_fe_lun];
2054
2055        if (se_tpg->se_tpg_tfo->sess_get_initiator_sid) {
2056                memset(&isid_buf[0], 0, PR_REG_ISID_LEN);
2057                se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, &isid_buf[0],
2058                                PR_REG_ISID_LEN);
2059                isid_ptr = &isid_buf[0];
2060        }
2061        /*
2062         * Follow logic from spc4r17 Section 5.7.7, Register Behaviors Table 47
2063         */
2064        pr_reg_e = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2065        if (!pr_reg_e) {
2066                if (res_key) {
2067                        pr_warn("SPC-3 PR: Reservation Key non-zero"
2068                                " for SA REGISTER, returning CONFLICT\n");
2069                        return TCM_RESERVATION_CONFLICT;
2070                }
2071                /*
2072                 * Do nothing but return GOOD status.
2073                 */
2074                if (!sa_res_key)
2075                        return 0;
2076
2077                if (!spec_i_pt) {
2078                        /*
2079                         * Perform the Service Action REGISTER on the Initiator
2080                         * Port Endpoint that the PRO was received from on the
2081                         * Logical Unit of the SCSI device server.
2082                         */
2083                        if (core_scsi3_alloc_registration(cmd->se_dev,
2084                                        se_sess->se_node_acl, se_deve, isid_ptr,
2085                                        sa_res_key, all_tg_pt, aptpl,
2086                                        ignore_key, 0)) {
2087                                pr_err("Unable to allocate"
2088                                        " struct t10_pr_registration\n");
2089                                return TCM_INVALID_PARAMETER_LIST;
2090                        }
2091                } else {
2092                        /*
2093                         * Register both the Initiator port that received
2094                         * PROUT SA REGISTER + SPEC_I_PT=1 and extract SCSI
2095                         * TransportID from Parameter list and loop through
2096                         * fabric dependent parameter list while calling
2097                         * logic from of core_scsi3_alloc_registration() for
2098                         * each TransportID provided SCSI Initiator Port/Device
2099                         */
2100                        ret = core_scsi3_decode_spec_i_port(cmd, se_tpg,
2101                                        isid_ptr, sa_res_key, all_tg_pt, aptpl);
2102                        if (ret != 0)
2103                                return ret;
2104                }
2105                /*
2106                 * Nothing left to do for the APTPL=0 case.
2107                 */
2108                if (!aptpl) {
2109                        pr_tmpl->pr_aptpl_active = 0;
2110                        core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2111                        pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
2112                                        " REGISTER\n");
2113                        return 0;
2114                }
2115                /*
2116                 * Locate the newly allocated local I_T Nexus *pr_reg, and
2117                 * update the APTPL metadata information using its
2118                 * preallocated *pr_reg->pr_aptpl_buf.
2119                 */
2120                pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev,
2121                                se_sess->se_node_acl, se_sess);
2122
2123                if (core_scsi3_update_and_write_aptpl(cmd->se_dev,
2124                                &pr_reg->pr_aptpl_buf[0],
2125                                pr_tmpl->pr_aptpl_buf_len)) {
2126                        pr_tmpl->pr_aptpl_active = 1;
2127                        pr_debug("SPC-3 PR: Set APTPL Bit Activated for REGISTER\n");
2128                }
2129
2130                goto out_put_pr_reg;
2131        }
2132
2133        /*
2134         * Locate the existing *pr_reg via struct se_node_acl pointers
2135         */
2136        pr_reg = pr_reg_e;
2137        type = pr_reg->pr_res_type;
2138
2139        if (!ignore_key) {
2140                if (res_key != pr_reg->pr_res_key) {
2141                        pr_err("SPC-3 PR REGISTER: Received"
2142                                " res_key: 0x%016Lx does not match"
2143                                " existing SA REGISTER res_key:"
2144                                " 0x%016Lx\n", res_key,
2145                                pr_reg->pr_res_key);
2146                        ret = TCM_RESERVATION_CONFLICT;
2147                        goto out_put_pr_reg;
2148                }
2149        }
2150
2151        if (spec_i_pt) {
2152                pr_err("SPC-3 PR UNREGISTER: SPEC_I_PT"
2153                        " set while sa_res_key=0\n");
2154                ret = TCM_INVALID_PARAMETER_LIST;
2155                goto out_put_pr_reg;
2156        }
2157
2158        /*
2159         * An existing ALL_TG_PT=1 registration being released
2160         * must also set ALL_TG_PT=1 in the incoming PROUT.
2161         */
2162        if (pr_reg->pr_reg_all_tg_pt && !(all_tg_pt)) {
2163                pr_err("SPC-3 PR UNREGISTER: ALL_TG_PT=1"
2164                        " registration exists, but ALL_TG_PT=1 bit not"
2165                        " present in received PROUT\n");
2166                ret = TCM_INVALID_CDB_FIELD;
2167                goto out_put_pr_reg;
2168        }
2169
2170        /*
2171         * Allocate APTPL metadata buffer used for UNREGISTER ops
2172         */
2173        if (aptpl) {
2174                pr_aptpl_buf = kzalloc(pr_tmpl->pr_aptpl_buf_len,
2175                                        GFP_KERNEL);
2176                if (!pr_aptpl_buf) {
2177                        pr_err("Unable to allocate"
2178                                " pr_aptpl_buf\n");
2179                        ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2180                        goto out_put_pr_reg;
2181                }
2182        }
2183
2184        /*
2185         * sa_res_key=0 Unregister Reservation Key for registered I_T
2186         * Nexus sa_res_key=1 Change Reservation Key for registered I_T
2187         * Nexus.
2188         */
2189        if (!sa_res_key) {
2190                pr_holder = core_scsi3_check_implict_release(
2191                                cmd->se_dev, pr_reg);
2192                if (pr_holder < 0) {
2193                        kfree(pr_aptpl_buf);
2194                        ret = TCM_RESERVATION_CONFLICT;
2195                        goto out_put_pr_reg;
2196                }
2197
2198                spin_lock(&pr_tmpl->registration_lock);
2199                /*
2200                 * Release all ALL_TG_PT=1 for the matching SCSI Initiator Port
2201                 * and matching pr_res_key.
2202                 */
2203                if (pr_reg->pr_reg_all_tg_pt) {
2204                        list_for_each_entry_safe(pr_reg_p, pr_reg_tmp,
2205                                        &pr_tmpl->registration_list,
2206                                        pr_reg_list) {
2207
2208                                if (!pr_reg_p->pr_reg_all_tg_pt)
2209                                        continue;
2210                                if (pr_reg_p->pr_res_key != res_key)
2211                                        continue;
2212                                if (pr_reg == pr_reg_p)
2213                                        continue;
2214                                if (strcmp(pr_reg->pr_reg_nacl->initiatorname,
2215                                           pr_reg_p->pr_reg_nacl->initiatorname))
2216                                        continue;
2217
2218                                __core_scsi3_free_registration(dev,
2219                                                pr_reg_p, NULL, 0);
2220                        }
2221                }
2222
2223                /*
2224                 * Release the calling I_T Nexus registration now..
2225                 */
2226                __core_scsi3_free_registration(cmd->se_dev, pr_reg, NULL, 1);
2227
2228                /*
2229                 * From spc4r17, section 5.7.11.3 Unregistering
2230                 *
2231                 * If the persistent reservation is a registrants only
2232                 * type, the device server shall establish a unit
2233                 * attention condition for the initiator port associated
2234                 * with every registered I_T nexus except for the I_T
2235                 * nexus on which the PERSISTENT RESERVE OUT command was
2236                 * received, with the additional sense code set to
2237                 * RESERVATIONS RELEASED.
2238                 */
2239                if (pr_holder &&
2240                   (type == PR_TYPE_WRITE_EXCLUSIVE_REGONLY ||
2241                    type == PR_TYPE_EXCLUSIVE_ACCESS_REGONLY)) {
2242                        list_for_each_entry(pr_reg_p,
2243                                        &pr_tmpl->registration_list,
2244                                        pr_reg_list) {
2245
2246                                core_scsi3_ua_allocate(
2247                                        pr_reg_p->pr_reg_nacl,
2248                                        pr_reg_p->pr_res_mapped_lun,
2249                                        0x2A,
2250                                        ASCQ_2AH_RESERVATIONS_RELEASED);
2251                        }
2252                }
2253                spin_unlock(&pr_tmpl->registration_lock);
2254
2255                if (!aptpl) {
2256                        pr_tmpl->pr_aptpl_active = 0;
2257                        core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2258                        pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2259                                        " for UNREGISTER\n");
2260                        return 0;
2261                }
2262
2263                if (!core_scsi3_update_and_write_aptpl(dev, &pr_aptpl_buf[0],
2264                                pr_tmpl->pr_aptpl_buf_len)) {
2265                        pr_tmpl->pr_aptpl_active = 1;
2266                        pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2267                                        " for UNREGISTER\n");
2268                }
2269
2270                goto out_free_aptpl_buf;
2271        }
2272
2273        /*
2274         * Increment PRgeneration counter for struct se_device"
2275         * upon a successful REGISTER, see spc4r17 section 6.3.2
2276         * READ_KEYS service action.
2277         */
2278        pr_reg->pr_res_generation = core_scsi3_pr_generation(cmd->se_dev);
2279        pr_reg->pr_res_key = sa_res_key;
2280        pr_debug("SPC-3 PR [%s] REGISTER%s: Changed Reservation"
2281                " Key for %s to: 0x%016Lx PRgeneration:"
2282                " 0x%08x\n", cmd->se_tfo->get_fabric_name(),
2283                (ignore_key) ? "_AND_IGNORE_EXISTING_KEY" : "",
2284                pr_reg->pr_reg_nacl->initiatorname,
2285                pr_reg->pr_res_key, pr_reg->pr_res_generation);
2286
2287        if (!aptpl) {
2288                pr_tmpl->pr_aptpl_active = 0;
2289                core_scsi3_update_and_write_aptpl(dev, NULL, 0);
2290                pr_debug("SPC-3 PR: Set APTPL Bit Deactivated"
2291                                " for REGISTER\n");
2292                ret = 0;
2293                goto out_put_pr_reg;
2294        }
2295
2296        if (!core_scsi3_update_and_write_aptpl(dev, &pr_aptpl_buf[0],
2297                                                pr_tmpl->pr_aptpl_buf_len)) {
2298                pr_tmpl->pr_aptpl_active = 1;
2299                pr_debug("SPC-3 PR: Set APTPL Bit Activated"
2300                        " for REGISTER\n");
2301        }
2302
2303out_free_aptpl_buf:
2304        kfree(pr_aptpl_buf);
2305        ret = 0;
2306out_put_pr_reg:
2307        core_scsi3_put_pr_reg(pr_reg);
2308        return ret;
2309}
2310
2311unsigned char *core_scsi3_pr_dump_type(int type)
2312{
2313        switch (type) {
2314        case PR_TYPE_WRITE_EXCLUSIVE:
2315                return "Write Exclusive Access";
2316        case PR_TYPE_EXCLUSIVE_ACCESS:
2317                return "Exclusive Access";
2318        case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2319                return "Write Exclusive Access, Registrants Only";
2320        case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2321                return "Exclusive Access, Registrants Only";
2322        case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2323                return "Write Exclusive Access, All Registrants";
2324        case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2325                return "Exclusive Access, All Registrants";
2326        default:
2327                break;
2328        }
2329
2330        return "Unknown SPC-3 PR Type";
2331}
2332
2333static sense_reason_t
2334core_scsi3_pro_reserve(struct se_cmd *cmd, int type, int scope, u64 res_key)
2335{
2336        struct se_device *dev = cmd->se_dev;
2337        struct se_session *se_sess = cmd->se_sess;
2338        struct se_lun *se_lun = cmd->se_lun;
2339        struct t10_pr_registration *pr_reg, *pr_res_holder;
2340        struct t10_reservation *pr_tmpl = &dev->t10_pr;
2341        char i_buf[PR_REG_ISID_ID_LEN];
2342        sense_reason_t ret;
2343        int prf_isid;
2344
2345        memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2346
2347        if (!se_sess || !se_lun) {
2348                pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2349                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2350        }
2351        /*
2352         * Locate the existing *pr_reg via struct se_node_acl pointers
2353         */
2354        pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2355                                se_sess);
2356        if (!pr_reg) {
2357                pr_err("SPC-3 PR: Unable to locate"
2358                        " PR_REGISTERED *pr_reg for RESERVE\n");
2359                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2360        }
2361        /*
2362         * From spc4r17 Section 5.7.9: Reserving:
2363         *
2364         * An application client creates a persistent reservation by issuing
2365         * a PERSISTENT RESERVE OUT command with RESERVE service action through
2366         * a registered I_T nexus with the following parameters:
2367         *    a) RESERVATION KEY set to the value of the reservation key that is
2368         *       registered with the logical unit for the I_T nexus; and
2369         */
2370        if (res_key != pr_reg->pr_res_key) {
2371                pr_err("SPC-3 PR RESERVE: Received res_key: 0x%016Lx"
2372                        " does not match existing SA REGISTER res_key:"
2373                        " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2374                ret = TCM_RESERVATION_CONFLICT;
2375                goto out_put_pr_reg;
2376        }
2377        /*
2378         * From spc4r17 Section 5.7.9: Reserving:
2379         *
2380         * From above:
2381         *  b) TYPE field and SCOPE field set to the persistent reservation
2382         *     being created.
2383         *
2384         * Only one persistent reservation is allowed at a time per logical unit
2385         * and that persistent reservation has a scope of LU_SCOPE.
2386         */
2387        if (scope != PR_SCOPE_LU_SCOPE) {
2388                pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2389                ret = TCM_INVALID_PARAMETER_LIST;
2390                goto out_put_pr_reg;
2391        }
2392        /*
2393         * See if we have an existing PR reservation holder pointer at
2394         * struct se_device->dev_pr_res_holder in the form struct t10_pr_registration
2395         * *pr_res_holder.
2396         */
2397        spin_lock(&dev->dev_reservation_lock);
2398        pr_res_holder = dev->dev_pr_res_holder;
2399        if (pr_res_holder) {
2400                /*
2401                 * From spc4r17 Section 5.7.9: Reserving:
2402                 *
2403                 * If the device server receives a PERSISTENT RESERVE OUT
2404                 * command from an I_T nexus other than a persistent reservation
2405                 * holder (see 5.7.10) that attempts to create a persistent
2406                 * reservation when a persistent reservation already exists for
2407                 * the logical unit, then the command shall be completed with
2408                 * RESERVATION CONFLICT status.
2409                 */
2410                if (pr_res_holder != pr_reg) {
2411                        struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2412                        pr_err("SPC-3 PR: Attempted RESERVE from"
2413                                " [%s]: %s while reservation already held by"
2414                                " [%s]: %s, returning RESERVATION_CONFLICT\n",
2415                                cmd->se_tfo->get_fabric_name(),
2416                                se_sess->se_node_acl->initiatorname,
2417                                pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2418                                pr_res_holder->pr_reg_nacl->initiatorname);
2419
2420                        spin_unlock(&dev->dev_reservation_lock);
2421                        ret = TCM_RESERVATION_CONFLICT;
2422                        goto out_put_pr_reg;
2423                }
2424                /*
2425                 * From spc4r17 Section 5.7.9: Reserving:
2426                 *
2427                 * If a persistent reservation holder attempts to modify the
2428                 * type or scope of an existing persistent reservation, the
2429                 * command shall be completed with RESERVATION CONFLICT status.
2430                 */
2431                if ((pr_res_holder->pr_res_type != type) ||
2432                    (pr_res_holder->pr_res_scope != scope)) {
2433                        struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2434                        pr_err("SPC-3 PR: Attempted RESERVE from"
2435                                " [%s]: %s trying to change TYPE and/or SCOPE,"
2436                                " while reservation already held by [%s]: %s,"
2437                                " returning RESERVATION_CONFLICT\n",
2438                                cmd->se_tfo->get_fabric_name(),
2439                                se_sess->se_node_acl->initiatorname,
2440                                pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2441                                pr_res_holder->pr_reg_nacl->initiatorname);
2442
2443                        spin_unlock(&dev->dev_reservation_lock);
2444                        ret = TCM_RESERVATION_CONFLICT;
2445                        goto out_put_pr_reg;
2446                }
2447                /*
2448                 * From spc4r17 Section 5.7.9: Reserving:
2449                 *
2450                 * If the device server receives a PERSISTENT RESERVE OUT
2451                 * command with RESERVE service action where the TYPE field and
2452                 * the SCOPE field contain the same values as the existing type
2453                 * and scope from a persistent reservation holder, it shall not
2454                 * make any change to the existing persistent reservation and
2455                 * shall completethe command with GOOD status.
2456                 */
2457                spin_unlock(&dev->dev_reservation_lock);
2458                ret = 0;
2459                goto out_put_pr_reg;
2460        }
2461        /*
2462         * Otherwise, our *pr_reg becomes the PR reservation holder for said
2463         * TYPE/SCOPE.  Also set the received scope and type in *pr_reg.
2464         */
2465        pr_reg->pr_res_scope = scope;
2466        pr_reg->pr_res_type = type;
2467        pr_reg->pr_res_holder = 1;
2468        dev->dev_pr_res_holder = pr_reg;
2469        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2470                                PR_REG_ISID_ID_LEN);
2471
2472        pr_debug("SPC-3 PR [%s] Service Action: RESERVE created new"
2473                " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2474                cmd->se_tfo->get_fabric_name(), core_scsi3_pr_dump_type(type),
2475                (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2476        pr_debug("SPC-3 PR [%s] RESERVE Node: %s%s\n",
2477                        cmd->se_tfo->get_fabric_name(),
2478                        se_sess->se_node_acl->initiatorname,
2479                        (prf_isid) ? &i_buf[0] : "");
2480        spin_unlock(&dev->dev_reservation_lock);
2481
2482        if (pr_tmpl->pr_aptpl_active) {
2483                if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
2484                                &pr_reg->pr_aptpl_buf[0],
2485                                pr_tmpl->pr_aptpl_buf_len)) {
2486                        pr_debug("SPC-3 PR: Updated APTPL metadata"
2487                                        " for RESERVE\n");
2488                }
2489        }
2490
2491        ret = 0;
2492out_put_pr_reg:
2493        core_scsi3_put_pr_reg(pr_reg);
2494        return ret;
2495}
2496
2497static sense_reason_t
2498core_scsi3_emulate_pro_reserve(struct se_cmd *cmd, int type, int scope,
2499                u64 res_key)
2500{
2501        switch (type) {
2502        case PR_TYPE_WRITE_EXCLUSIVE:
2503        case PR_TYPE_EXCLUSIVE_ACCESS:
2504        case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
2505        case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
2506        case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
2507        case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
2508                return core_scsi3_pro_reserve(cmd, type, scope, res_key);
2509        default:
2510                pr_err("SPC-3 PR: Unknown Service Action RESERVE Type:"
2511                        " 0x%02x\n", type);
2512                return TCM_INVALID_CDB_FIELD;
2513        }
2514}
2515
2516/*
2517 * Called with struct se_device->dev_reservation_lock held.
2518 */
2519static void __core_scsi3_complete_pro_release(
2520        struct se_device *dev,
2521        struct se_node_acl *se_nacl,
2522        struct t10_pr_registration *pr_reg,
2523        int explict)
2524{
2525        struct target_core_fabric_ops *tfo = se_nacl->se_tpg->se_tpg_tfo;
2526        char i_buf[PR_REG_ISID_ID_LEN];
2527        int prf_isid;
2528
2529        memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2530        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2531                                PR_REG_ISID_ID_LEN);
2532        /*
2533         * Go ahead and release the current PR reservation holder.
2534         */
2535        dev->dev_pr_res_holder = NULL;
2536
2537        pr_debug("SPC-3 PR [%s] Service Action: %s RELEASE cleared"
2538                " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2539                tfo->get_fabric_name(), (explict) ? "explict" : "implict",
2540                core_scsi3_pr_dump_type(pr_reg->pr_res_type),
2541                (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2542        pr_debug("SPC-3 PR [%s] RELEASE Node: %s%s\n",
2543                tfo->get_fabric_name(), se_nacl->initiatorname,
2544                (prf_isid) ? &i_buf[0] : "");
2545        /*
2546         * Clear TYPE and SCOPE for the next PROUT Service Action: RESERVE
2547         */
2548        pr_reg->pr_res_holder = pr_reg->pr_res_type = pr_reg->pr_res_scope = 0;
2549}
2550
2551static sense_reason_t
2552core_scsi3_emulate_pro_release(struct se_cmd *cmd, int type, int scope,
2553                u64 res_key)
2554{
2555        struct se_device *dev = cmd->se_dev;
2556        struct se_session *se_sess = cmd->se_sess;
2557        struct se_lun *se_lun = cmd->se_lun;
2558        struct t10_pr_registration *pr_reg, *pr_reg_p, *pr_res_holder;
2559        struct t10_reservation *pr_tmpl = &dev->t10_pr;
2560        int all_reg = 0;
2561        sense_reason_t ret = 0;
2562
2563        if (!se_sess || !se_lun) {
2564                pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
2565                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2566        }
2567        /*
2568         * Locate the existing *pr_reg via struct se_node_acl pointers
2569         */
2570        pr_reg = core_scsi3_locate_pr_reg(dev, se_sess->se_node_acl, se_sess);
2571        if (!pr_reg) {
2572                pr_err("SPC-3 PR: Unable to locate"
2573                        " PR_REGISTERED *pr_reg for RELEASE\n");
2574                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2575        }
2576        /*
2577         * From spc4r17 Section 5.7.11.2 Releasing:
2578         *
2579         * If there is no persistent reservation or in response to a persistent
2580         * reservation release request from a registered I_T nexus that is not a
2581         * persistent reservation holder (see 5.7.10), the device server shall
2582         * do the following:
2583         *
2584         *     a) Not release the persistent reservation, if any;
2585         *     b) Not remove any registrations; and
2586         *     c) Complete the command with GOOD status.
2587         */
2588        spin_lock(&dev->dev_reservation_lock);
2589        pr_res_holder = dev->dev_pr_res_holder;
2590        if (!pr_res_holder) {
2591                /*
2592                 * No persistent reservation, return GOOD status.
2593                 */
2594                spin_unlock(&dev->dev_reservation_lock);
2595                goto out_put_pr_reg;
2596        }
2597        if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2598            (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
2599                all_reg = 1;
2600
2601        if ((all_reg == 0) && (pr_res_holder != pr_reg)) {
2602                /*
2603                 * Non 'All Registrants' PR Type cases..
2604                 * Release request from a registered I_T nexus that is not a
2605                 * persistent reservation holder. return GOOD status.
2606                 */
2607                spin_unlock(&dev->dev_reservation_lock);
2608                goto out_put_pr_reg;
2609        }
2610
2611        /*
2612         * From spc4r17 Section 5.7.11.2 Releasing:
2613         *
2614         * Only the persistent reservation holder (see 5.7.10) is allowed to
2615         * release a persistent reservation.
2616         *
2617         * An application client releases the persistent reservation by issuing
2618         * a PERSISTENT RESERVE OUT command with RELEASE service action through
2619         * an I_T nexus that is a persistent reservation holder with the
2620         * following parameters:
2621         *
2622         *     a) RESERVATION KEY field set to the value of the reservation key
2623         *        that is registered with the logical unit for the I_T nexus;
2624         */
2625        if (res_key != pr_reg->pr_res_key) {
2626                pr_err("SPC-3 PR RELEASE: Received res_key: 0x%016Lx"
2627                        " does not match existing SA REGISTER res_key:"
2628                        " 0x%016Lx\n", res_key, pr_reg->pr_res_key);
2629                spin_unlock(&dev->dev_reservation_lock);
2630                ret = TCM_RESERVATION_CONFLICT;
2631                goto out_put_pr_reg;
2632        }
2633        /*
2634         * From spc4r17 Section 5.7.11.2 Releasing and above:
2635         *
2636         * b) TYPE field and SCOPE field set to match the persistent
2637         *    reservation being released.
2638         */
2639        if ((pr_res_holder->pr_res_type != type) ||
2640            (pr_res_holder->pr_res_scope != scope)) {
2641                struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2642                pr_err("SPC-3 PR RELEASE: Attempted to release"
2643                        " reservation from [%s]: %s with different TYPE "
2644                        "and/or SCOPE  while reservation already held by"
2645                        " [%s]: %s, returning RESERVATION_CONFLICT\n",
2646                        cmd->se_tfo->get_fabric_name(),
2647                        se_sess->se_node_acl->initiatorname,
2648                        pr_res_nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
2649                        pr_res_holder->pr_reg_nacl->initiatorname);
2650
2651                spin_unlock(&dev->dev_reservation_lock);
2652                ret = TCM_RESERVATION_CONFLICT;
2653                goto out_put_pr_reg;
2654        }
2655        /*
2656         * In response to a persistent reservation release request from the
2657         * persistent reservation holder the device server shall perform a
2658         * release by doing the following as an uninterrupted series of actions:
2659         * a) Release the persistent reservation;
2660         * b) Not remove any registration(s);
2661         * c) If the released persistent reservation is a registrants only type
2662         * or all registrants type persistent reservation,
2663         *    the device server shall establish a unit attention condition for
2664         *    the initiator port associated with every regis-
2665         *    tered I_T nexus other than I_T nexus on which the PERSISTENT
2666         *    RESERVE OUT command with RELEASE service action was received,
2667         *    with the additional sense code set to RESERVATIONS RELEASED; and
2668         * d) If the persistent reservation is of any other type, the device
2669         *    server shall not establish a unit attention condition.
2670         */
2671        __core_scsi3_complete_pro_release(dev, se_sess->se_node_acl,
2672                        pr_reg, 1);
2673
2674        spin_unlock(&dev->dev_reservation_lock);
2675
2676        if ((type != PR_TYPE_WRITE_EXCLUSIVE_REGONLY) &&
2677            (type != PR_TYPE_EXCLUSIVE_ACCESS_REGONLY) &&
2678            (type != PR_TYPE_WRITE_EXCLUSIVE_ALLREG) &&
2679            (type != PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
2680                /*
2681                 * If no UNIT ATTENTION conditions will be established for
2682                 * PR_TYPE_WRITE_EXCLUSIVE or PR_TYPE_EXCLUSIVE_ACCESS
2683                 * go ahead and check for APTPL=1 update+write below
2684                 */
2685                goto write_aptpl;
2686        }
2687
2688        spin_lock(&pr_tmpl->registration_lock);
2689        list_for_each_entry(pr_reg_p, &pr_tmpl->registration_list,
2690                        pr_reg_list) {
2691                /*
2692                 * Do not establish a UNIT ATTENTION condition
2693                 * for the calling I_T Nexus
2694                 */
2695                if (pr_reg_p == pr_reg)
2696                        continue;
2697
2698                core_scsi3_ua_allocate(pr_reg_p->pr_reg_nacl,
2699                                pr_reg_p->pr_res_mapped_lun,
2700                                0x2A, ASCQ_2AH_RESERVATIONS_RELEASED);
2701        }
2702        spin_unlock(&pr_tmpl->registration_lock);
2703
2704write_aptpl:
2705        if (pr_tmpl->pr_aptpl_active) {
2706                if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
2707                        &pr_reg->pr_aptpl_buf[0], pr_tmpl->pr_aptpl_buf_len)) {
2708                        pr_debug("SPC-3 PR: Updated APTPL metadata for RELEASE\n");
2709                }
2710        }
2711out_put_pr_reg:
2712        core_scsi3_put_pr_reg(pr_reg);
2713        return ret;
2714}
2715
2716static sense_reason_t
2717core_scsi3_emulate_pro_clear(struct se_cmd *cmd, u64 res_key)
2718{
2719        struct se_device *dev = cmd->se_dev;
2720        struct se_node_acl *pr_reg_nacl;
2721        struct se_session *se_sess = cmd->se_sess;
2722        struct t10_reservation *pr_tmpl = &dev->t10_pr;
2723        struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2724        u32 pr_res_mapped_lun = 0;
2725        int calling_it_nexus = 0;
2726        /*
2727         * Locate the existing *pr_reg via struct se_node_acl pointers
2728         */
2729        pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev,
2730                        se_sess->se_node_acl, se_sess);
2731        if (!pr_reg_n) {
2732                pr_err("SPC-3 PR: Unable to locate"
2733                        " PR_REGISTERED *pr_reg for CLEAR\n");
2734                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2735        }
2736        /*
2737         * From spc4r17 section 5.7.11.6, Clearing:
2738         *
2739         * Any application client may release the persistent reservation and
2740         * remove all registrations from a device server by issuing a
2741         * PERSISTENT RESERVE OUT command with CLEAR service action through a
2742         * registered I_T nexus with the following parameter:
2743         *
2744         *      a) RESERVATION KEY field set to the value of the reservation key
2745         *         that is registered with the logical unit for the I_T nexus.
2746         */
2747        if (res_key != pr_reg_n->pr_res_key) {
2748                pr_err("SPC-3 PR REGISTER: Received"
2749                        " res_key: 0x%016Lx does not match"
2750                        " existing SA REGISTER res_key:"
2751                        " 0x%016Lx\n", res_key, pr_reg_n->pr_res_key);
2752                core_scsi3_put_pr_reg(pr_reg_n);
2753                return TCM_RESERVATION_CONFLICT;
2754        }
2755        /*
2756         * a) Release the persistent reservation, if any;
2757         */
2758        spin_lock(&dev->dev_reservation_lock);
2759        pr_res_holder = dev->dev_pr_res_holder;
2760        if (pr_res_holder) {
2761                struct se_node_acl *pr_res_nacl = pr_res_holder->pr_reg_nacl;
2762                __core_scsi3_complete_pro_release(dev, pr_res_nacl,
2763                        pr_res_holder, 0);
2764        }
2765        spin_unlock(&dev->dev_reservation_lock);
2766        /*
2767         * b) Remove all registration(s) (see spc4r17 5.7.7);
2768         */
2769        spin_lock(&pr_tmpl->registration_lock);
2770        list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2771                        &pr_tmpl->registration_list, pr_reg_list) {
2772
2773                calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2774                pr_reg_nacl = pr_reg->pr_reg_nacl;
2775                pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2776                __core_scsi3_free_registration(dev, pr_reg, NULL,
2777                                        calling_it_nexus);
2778                /*
2779                 * e) Establish a unit attention condition for the initiator
2780                 *    port associated with every registered I_T nexus other
2781                 *    than the I_T nexus on which the PERSISTENT RESERVE OUT
2782                 *    command with CLEAR service action was received, with the
2783                 *    additional sense code set to RESERVATIONS PREEMPTED.
2784                 */
2785                if (!calling_it_nexus)
2786                        core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun,
2787                                0x2A, ASCQ_2AH_RESERVATIONS_PREEMPTED);
2788        }
2789        spin_unlock(&pr_tmpl->registration_lock);
2790
2791        pr_debug("SPC-3 PR [%s] Service Action: CLEAR complete\n",
2792                cmd->se_tfo->get_fabric_name());
2793
2794        if (pr_tmpl->pr_aptpl_active) {
2795                core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
2796                pr_debug("SPC-3 PR: Updated APTPL metadata"
2797                                " for CLEAR\n");
2798        }
2799
2800        core_scsi3_pr_generation(dev);
2801        return 0;
2802}
2803
2804/*
2805 * Called with struct se_device->dev_reservation_lock held.
2806 */
2807static void __core_scsi3_complete_pro_preempt(
2808        struct se_device *dev,
2809        struct t10_pr_registration *pr_reg,
2810        struct list_head *preempt_and_abort_list,
2811        int type,
2812        int scope,
2813        int abort)
2814{
2815        struct se_node_acl *nacl = pr_reg->pr_reg_nacl;
2816        struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
2817        char i_buf[PR_REG_ISID_ID_LEN];
2818        int prf_isid;
2819
2820        memset(i_buf, 0, PR_REG_ISID_ID_LEN);
2821        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
2822                                PR_REG_ISID_ID_LEN);
2823        /*
2824         * Do an implict RELEASE of the existing reservation.
2825         */
2826        if (dev->dev_pr_res_holder)
2827                __core_scsi3_complete_pro_release(dev, nacl,
2828                                dev->dev_pr_res_holder, 0);
2829
2830        dev->dev_pr_res_holder = pr_reg;
2831        pr_reg->pr_res_holder = 1;
2832        pr_reg->pr_res_type = type;
2833        pr_reg->pr_res_scope = scope;
2834
2835        pr_debug("SPC-3 PR [%s] Service Action: PREEMPT%s created new"
2836                " reservation holder TYPE: %s ALL_TG_PT: %d\n",
2837                tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2838                core_scsi3_pr_dump_type(type),
2839                (pr_reg->pr_reg_all_tg_pt) ? 1 : 0);
2840        pr_debug("SPC-3 PR [%s] PREEMPT%s from Node: %s%s\n",
2841                tfo->get_fabric_name(), (abort) ? "_AND_ABORT" : "",
2842                nacl->initiatorname, (prf_isid) ? &i_buf[0] : "");
2843        /*
2844         * For PREEMPT_AND_ABORT, add the preempting reservation's
2845         * struct t10_pr_registration to the list that will be compared
2846         * against received CDBs..
2847         */
2848        if (preempt_and_abort_list)
2849                list_add_tail(&pr_reg->pr_reg_abort_list,
2850                                preempt_and_abort_list);
2851}
2852
2853static void core_scsi3_release_preempt_and_abort(
2854        struct list_head *preempt_and_abort_list,
2855        struct t10_pr_registration *pr_reg_holder)
2856{
2857        struct t10_pr_registration *pr_reg, *pr_reg_tmp;
2858
2859        list_for_each_entry_safe(pr_reg, pr_reg_tmp, preempt_and_abort_list,
2860                                pr_reg_abort_list) {
2861
2862                list_del(&pr_reg->pr_reg_abort_list);
2863                if (pr_reg_holder == pr_reg)
2864                        continue;
2865                if (pr_reg->pr_res_holder) {
2866                        pr_warn("pr_reg->pr_res_holder still set\n");
2867                        continue;
2868                }
2869
2870                pr_reg->pr_reg_deve = NULL;
2871                pr_reg->pr_reg_nacl = NULL;
2872                kfree(pr_reg->pr_aptpl_buf);
2873                kmem_cache_free(t10_pr_reg_cache, pr_reg);
2874        }
2875}
2876
2877static sense_reason_t
2878core_scsi3_pro_preempt(struct se_cmd *cmd, int type, int scope, u64 res_key,
2879                u64 sa_res_key, int abort)
2880{
2881        struct se_device *dev = cmd->se_dev;
2882        struct se_node_acl *pr_reg_nacl;
2883        struct se_session *se_sess = cmd->se_sess;
2884        LIST_HEAD(preempt_and_abort_list);
2885        struct t10_pr_registration *pr_reg, *pr_reg_tmp, *pr_reg_n, *pr_res_holder;
2886        struct t10_reservation *pr_tmpl = &dev->t10_pr;
2887        u32 pr_res_mapped_lun = 0;
2888        int all_reg = 0, calling_it_nexus = 0, released_regs = 0;
2889        int prh_type = 0, prh_scope = 0;
2890
2891        if (!se_sess)
2892                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2893
2894        pr_reg_n = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
2895                                se_sess);
2896        if (!pr_reg_n) {
2897                pr_err("SPC-3 PR: Unable to locate"
2898                        " PR_REGISTERED *pr_reg for PREEMPT%s\n",
2899                        (abort) ? "_AND_ABORT" : "");
2900                return TCM_RESERVATION_CONFLICT;
2901        }
2902        if (pr_reg_n->pr_res_key != res_key) {
2903                core_scsi3_put_pr_reg(pr_reg_n);
2904                return TCM_RESERVATION_CONFLICT;
2905        }
2906        if (scope != PR_SCOPE_LU_SCOPE) {
2907                pr_err("SPC-3 PR: Illegal SCOPE: 0x%02x\n", scope);
2908                core_scsi3_put_pr_reg(pr_reg_n);
2909                return TCM_INVALID_PARAMETER_LIST;
2910        }
2911
2912        spin_lock(&dev->dev_reservation_lock);
2913        pr_res_holder = dev->dev_pr_res_holder;
2914        if (pr_res_holder &&
2915           ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
2916            (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)))
2917                all_reg = 1;
2918
2919        if (!all_reg && !sa_res_key) {
2920                spin_unlock(&dev->dev_reservation_lock);
2921                core_scsi3_put_pr_reg(pr_reg_n);
2922                return TCM_INVALID_PARAMETER_LIST;
2923        }
2924        /*
2925         * From spc4r17, section 5.7.11.4.4 Removing Registrations:
2926         *
2927         * If the SERVICE ACTION RESERVATION KEY field does not identify a
2928         * persistent reservation holder or there is no persistent reservation
2929         * holder (i.e., there is no persistent reservation), then the device
2930         * server shall perform a preempt by doing the following in an
2931         * uninterrupted series of actions. (See below..)
2932         */
2933        if (!pr_res_holder || (pr_res_holder->pr_res_key != sa_res_key)) {
2934                /*
2935                 * No existing or SA Reservation Key matching reservations..
2936                 *
2937                 * PROUT SA PREEMPT with All Registrant type reservations are
2938                 * allowed to be processed without a matching SA Reservation Key
2939                 */
2940                spin_lock(&pr_tmpl->registration_lock);
2941                list_for_each_entry_safe(pr_reg, pr_reg_tmp,
2942                                &pr_tmpl->registration_list, pr_reg_list) {
2943                        /*
2944                         * Removing of registrations in non all registrants
2945                         * type reservations without a matching SA reservation
2946                         * key.
2947                         *
2948                         * a) Remove the registrations for all I_T nexuses
2949                         *    specified by the SERVICE ACTION RESERVATION KEY
2950                         *    field;
2951                         * b) Ignore the contents of the SCOPE and TYPE fields;
2952                         * c) Process tasks as defined in 5.7.1; and
2953                         * d) Establish a unit attention condition for the
2954                         *    initiator port associated with every I_T nexus
2955                         *    that lost its registration other than the I_T
2956                         *    nexus on which the PERSISTENT RESERVE OUT command
2957                         *    was received, with the additional sense code set
2958                         *    to REGISTRATIONS PREEMPTED.
2959                         */
2960                        if (!all_reg) {
2961                                if (pr_reg->pr_res_key != sa_res_key)
2962                                        continue;
2963
2964                                calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2965                                pr_reg_nacl = pr_reg->pr_reg_nacl;
2966                                pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2967                                __core_scsi3_free_registration(dev, pr_reg,
2968                                        (abort) ? &preempt_and_abort_list :
2969                                                NULL, calling_it_nexus);
2970                                released_regs++;
2971                        } else {
2972                                /*
2973                                 * Case for any existing all registrants type
2974                                 * reservation, follow logic in spc4r17 section
2975                                 * 5.7.11.4 Preempting, Table 52 and Figure 7.
2976                                 *
2977                                 * For a ZERO SA Reservation key, release
2978                                 * all other registrations and do an implict
2979                                 * release of active persistent reservation.
2980                                 *
2981                                 * For a non-ZERO SA Reservation key, only
2982                                 * release the matching reservation key from
2983                                 * registrations.
2984                                 */
2985                                if ((sa_res_key) &&
2986                                     (pr_reg->pr_res_key != sa_res_key))
2987                                        continue;
2988
2989                                calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
2990                                if (calling_it_nexus)
2991                                        continue;
2992
2993                                pr_reg_nacl = pr_reg->pr_reg_nacl;
2994                                pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
2995                                __core_scsi3_free_registration(dev, pr_reg,
2996                                        (abort) ? &preempt_and_abort_list :
2997                                                NULL, 0);
2998                                released_regs++;
2999                        }
3000                        if (!calling_it_nexus)
3001                                core_scsi3_ua_allocate(pr_reg_nacl,
3002                                        pr_res_mapped_lun, 0x2A,
3003                                        ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3004                }
3005                spin_unlock(&pr_tmpl->registration_lock);
3006                /*
3007                 * If a PERSISTENT RESERVE OUT with a PREEMPT service action or
3008                 * a PREEMPT AND ABORT service action sets the SERVICE ACTION
3009                 * RESERVATION KEY field to a value that does not match any
3010                 * registered reservation key, then the device server shall
3011                 * complete the command with RESERVATION CONFLICT status.
3012                 */
3013                if (!released_regs) {
3014                        spin_unlock(&dev->dev_reservation_lock);
3015                        core_scsi3_put_pr_reg(pr_reg_n);
3016                        return TCM_RESERVATION_CONFLICT;
3017                }
3018                /*
3019                 * For an existing all registrants type reservation
3020                 * with a zero SA rservation key, preempt the existing
3021                 * reservation with the new PR type and scope.
3022                 */
3023                if (pr_res_holder && all_reg && !(sa_res_key)) {
3024                        __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3025                                (abort) ? &preempt_and_abort_list : NULL,
3026                                type, scope, abort);
3027
3028                        if (abort)
3029                                core_scsi3_release_preempt_and_abort(
3030                                        &preempt_and_abort_list, pr_reg_n);
3031                }
3032                spin_unlock(&dev->dev_reservation_lock);
3033
3034                if (pr_tmpl->pr_aptpl_active) {
3035                        if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
3036                                        &pr_reg_n->pr_aptpl_buf[0],
3037                                        pr_tmpl->pr_aptpl_buf_len)) {
3038                                pr_debug("SPC-3 PR: Updated APTPL"
3039                                        " metadata for  PREEMPT%s\n", (abort) ?
3040                                        "_AND_ABORT" : "");
3041                        }
3042                }
3043
3044                core_scsi3_put_pr_reg(pr_reg_n);
3045                core_scsi3_pr_generation(cmd->se_dev);
3046                return 0;
3047        }
3048        /*
3049         * The PREEMPTing SA reservation key matches that of the
3050         * existing persistent reservation, first, we check if
3051         * we are preempting our own reservation.
3052         * From spc4r17, section 5.7.11.4.3 Preempting
3053         * persistent reservations and registration handling
3054         *
3055         * If an all registrants persistent reservation is not
3056         * present, it is not an error for the persistent
3057         * reservation holder to preempt itself (i.e., a
3058         * PERSISTENT RESERVE OUT with a PREEMPT service action
3059         * or a PREEMPT AND ABORT service action with the
3060         * SERVICE ACTION RESERVATION KEY value equal to the
3061         * persistent reservation holder's reservation key that
3062         * is received from the persistent reservation holder).
3063         * In that case, the device server shall establish the
3064         * new persistent reservation and maintain the
3065         * registration.
3066         */
3067        prh_type = pr_res_holder->pr_res_type;
3068        prh_scope = pr_res_holder->pr_res_scope;
3069        /*
3070         * If the SERVICE ACTION RESERVATION KEY field identifies a
3071         * persistent reservation holder (see 5.7.10), the device
3072         * server shall perform a preempt by doing the following as
3073         * an uninterrupted series of actions:
3074         *
3075         * a) Release the persistent reservation for the holder
3076         *    identified by the SERVICE ACTION RESERVATION KEY field;
3077         */
3078        if (pr_reg_n != pr_res_holder)
3079                __core_scsi3_complete_pro_release(dev,
3080                                pr_res_holder->pr_reg_nacl,
3081                                dev->dev_pr_res_holder, 0);
3082        /*
3083         * b) Remove the registrations for all I_T nexuses identified
3084         *    by the SERVICE ACTION RESERVATION KEY field, except the
3085         *    I_T nexus that is being used for the PERSISTENT RESERVE
3086         *    OUT command. If an all registrants persistent reservation
3087         *    is present and the SERVICE ACTION RESERVATION KEY field
3088         *    is set to zero, then all registrations shall be removed
3089         *    except for that of the I_T nexus that is being used for
3090         *    the PERSISTENT RESERVE OUT command;
3091         */
3092        spin_lock(&pr_tmpl->registration_lock);
3093        list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3094                        &pr_tmpl->registration_list, pr_reg_list) {
3095
3096                calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3097                if (calling_it_nexus)
3098                        continue;
3099
3100                if (pr_reg->pr_res_key != sa_res_key)
3101                        continue;
3102
3103                pr_reg_nacl = pr_reg->pr_reg_nacl;
3104                pr_res_mapped_lun = pr_reg->pr_res_mapped_lun;
3105                __core_scsi3_free_registration(dev, pr_reg,
3106                                (abort) ? &preempt_and_abort_list : NULL,
3107                                calling_it_nexus);
3108                /*
3109                 * e) Establish a unit attention condition for the initiator
3110                 *    port associated with every I_T nexus that lost its
3111                 *    persistent reservation and/or registration, with the
3112                 *    additional sense code set to REGISTRATIONS PREEMPTED;
3113                 */
3114                core_scsi3_ua_allocate(pr_reg_nacl, pr_res_mapped_lun, 0x2A,
3115                                ASCQ_2AH_REGISTRATIONS_PREEMPTED);
3116        }
3117        spin_unlock(&pr_tmpl->registration_lock);
3118        /*
3119         * c) Establish a persistent reservation for the preempting
3120         *    I_T nexus using the contents of the SCOPE and TYPE fields;
3121         */
3122        __core_scsi3_complete_pro_preempt(dev, pr_reg_n,
3123                        (abort) ? &preempt_and_abort_list : NULL,
3124                        type, scope, abort);
3125        /*
3126         * d) Process tasks as defined in 5.7.1;
3127         * e) See above..
3128         * f) If the type or scope has changed, then for every I_T nexus
3129         *    whose reservation key was not removed, except for the I_T
3130         *    nexus on which the PERSISTENT RESERVE OUT command was
3131         *    received, the device server shall establish a unit
3132         *    attention condition for the initiator port associated with
3133         *    that I_T nexus, with the additional sense code set to
3134         *    RESERVATIONS RELEASED. If the type or scope have not
3135         *    changed, then no unit attention condition(s) shall be
3136         *    established for this reason.
3137         */
3138        if ((prh_type != type) || (prh_scope != scope)) {
3139                spin_lock(&pr_tmpl->registration_lock);
3140                list_for_each_entry_safe(pr_reg, pr_reg_tmp,
3141                                &pr_tmpl->registration_list, pr_reg_list) {
3142
3143                        calling_it_nexus = (pr_reg_n == pr_reg) ? 1 : 0;
3144                        if (calling_it_nexus)
3145                                continue;
3146
3147                        core_scsi3_ua_allocate(pr_reg->pr_reg_nacl,
3148                                        pr_reg->pr_res_mapped_lun, 0x2A,
3149                                        ASCQ_2AH_RESERVATIONS_RELEASED);
3150                }
3151                spin_unlock(&pr_tmpl->registration_lock);
3152        }
3153        spin_unlock(&dev->dev_reservation_lock);
3154        /*
3155         * Call LUN_RESET logic upon list of struct t10_pr_registration,
3156         * All received CDBs for the matching existing reservation and
3157         * registrations undergo ABORT_TASK logic.
3158         *
3159         * From there, core_scsi3_release_preempt_and_abort() will
3160         * release every registration in the list (which have already
3161         * been removed from the primary pr_reg list), except the
3162         * new persistent reservation holder, the calling Initiator Port.
3163         */
3164        if (abort) {
3165                core_tmr_lun_reset(dev, NULL, &preempt_and_abort_list, cmd);
3166                core_scsi3_release_preempt_and_abort(&preempt_and_abort_list,
3167                                                pr_reg_n);
3168        }
3169
3170        if (pr_tmpl->pr_aptpl_active) {
3171                if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
3172                                &pr_reg_n->pr_aptpl_buf[0],
3173                                pr_tmpl->pr_aptpl_buf_len)) {
3174                        pr_debug("SPC-3 PR: Updated APTPL metadata for PREEMPT"
3175                                "%s\n", abort ? "_AND_ABORT" : "");
3176                }
3177        }
3178
3179        core_scsi3_put_pr_reg(pr_reg_n);
3180        core_scsi3_pr_generation(cmd->se_dev);
3181        return 0;
3182}
3183
3184static sense_reason_t
3185core_scsi3_emulate_pro_preempt(struct se_cmd *cmd, int type, int scope,
3186                u64 res_key, u64 sa_res_key, int abort)
3187{
3188        switch (type) {
3189        case PR_TYPE_WRITE_EXCLUSIVE:
3190        case PR_TYPE_EXCLUSIVE_ACCESS:
3191        case PR_TYPE_WRITE_EXCLUSIVE_REGONLY:
3192        case PR_TYPE_EXCLUSIVE_ACCESS_REGONLY:
3193        case PR_TYPE_WRITE_EXCLUSIVE_ALLREG:
3194        case PR_TYPE_EXCLUSIVE_ACCESS_ALLREG:
3195                return core_scsi3_pro_preempt(cmd, type, scope, res_key,
3196                                              sa_res_key, abort);
3197        default:
3198                pr_err("SPC-3 PR: Unknown Service Action PREEMPT%s"
3199                        " Type: 0x%02x\n", (abort) ? "_AND_ABORT" : "", type);
3200                return TCM_INVALID_CDB_FIELD;
3201        }
3202}
3203
3204
3205static sense_reason_t
3206core_scsi3_emulate_pro_register_and_move(struct se_cmd *cmd, u64 res_key,
3207                u64 sa_res_key, int aptpl, int unreg)
3208{
3209        struct se_session *se_sess = cmd->se_sess;
3210        struct se_device *dev = cmd->se_dev;
3211        struct se_dev_entry *dest_se_deve = NULL;
3212        struct se_lun *se_lun = cmd->se_lun;
3213        struct se_node_acl *pr_res_nacl, *pr_reg_nacl, *dest_node_acl = NULL;
3214        struct se_port *se_port;
3215        struct se_portal_group *se_tpg, *dest_se_tpg = NULL;
3216        struct target_core_fabric_ops *dest_tf_ops = NULL, *tf_ops;
3217        struct t10_pr_registration *pr_reg, *pr_res_holder, *dest_pr_reg;
3218        struct t10_reservation *pr_tmpl = &dev->t10_pr;
3219        unsigned char *buf;
3220        unsigned char *initiator_str;
3221        char *iport_ptr = NULL, dest_iport[64], i_buf[PR_REG_ISID_ID_LEN];
3222        u32 tid_len, tmp_tid_len;
3223        int new_reg = 0, type, scope, matching_iname, prf_isid;
3224        sense_reason_t ret;
3225        unsigned short rtpi;
3226        unsigned char proto_ident;
3227
3228        if (!se_sess || !se_lun) {
3229                pr_err("SPC-3 PR: se_sess || struct se_lun is NULL!\n");
3230                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3231        }
3232
3233        memset(dest_iport, 0, 64);
3234        memset(i_buf, 0, PR_REG_ISID_ID_LEN);
3235        se_tpg = se_sess->se_tpg;
3236        tf_ops = se_tpg->se_tpg_tfo;
3237        /*
3238         * Follow logic from spc4r17 Section 5.7.8, Table 50 --
3239         *      Register behaviors for a REGISTER AND MOVE service action
3240         *
3241         * Locate the existing *pr_reg via struct se_node_acl pointers
3242         */
3243        pr_reg = core_scsi3_locate_pr_reg(cmd->se_dev, se_sess->se_node_acl,
3244                                se_sess);
3245        if (!pr_reg) {
3246                pr_err("SPC-3 PR: Unable to locate PR_REGISTERED"
3247                        " *pr_reg for REGISTER_AND_MOVE\n");
3248                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3249        }
3250        /*
3251         * The provided reservation key much match the existing reservation key
3252         * provided during this initiator's I_T nexus registration.
3253         */
3254        if (res_key != pr_reg->pr_res_key) {
3255                pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received"
3256                        " res_key: 0x%016Lx does not match existing SA REGISTER"
3257                        " res_key: 0x%016Lx\n", res_key, pr_reg->pr_res_key);
3258                ret = TCM_RESERVATION_CONFLICT;
3259                goto out_put_pr_reg;
3260        }
3261        /*
3262         * The service active reservation key needs to be non zero
3263         */
3264        if (!sa_res_key) {
3265                pr_warn("SPC-3 PR REGISTER_AND_MOVE: Received zero"
3266                        " sa_res_key\n");
3267                ret = TCM_INVALID_PARAMETER_LIST;
3268                goto out_put_pr_reg;
3269        }
3270
3271        /*
3272         * Determine the Relative Target Port Identifier where the reservation
3273         * will be moved to for the TransportID containing SCSI initiator WWN
3274         * information.
3275         */
3276        buf = transport_kmap_data_sg(cmd);
3277        if (!buf) {
3278                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3279                goto out_put_pr_reg;
3280        }
3281
3282        rtpi = (buf[18] & 0xff) << 8;
3283        rtpi |= buf[19] & 0xff;
3284        tid_len = (buf[20] & 0xff) << 24;
3285        tid_len |= (buf[21] & 0xff) << 16;
3286        tid_len |= (buf[22] & 0xff) << 8;
3287        tid_len |= buf[23] & 0xff;
3288        transport_kunmap_data_sg(cmd);
3289        buf = NULL;
3290
3291        if ((tid_len + 24) != cmd->data_length) {
3292                pr_err("SPC-3 PR: Illegal tid_len: %u + 24 byte header"
3293                        " does not equal CDB data_length: %u\n", tid_len,
3294                        cmd->data_length);
3295                ret = TCM_INVALID_PARAMETER_LIST;
3296                goto out_put_pr_reg;
3297        }
3298
3299        spin_lock(&dev->se_port_lock);
3300        list_for_each_entry(se_port, &dev->dev_sep_list, sep_list) {
3301                if (se_port->sep_rtpi != rtpi)
3302                        continue;
3303                dest_se_tpg = se_port->sep_tpg;
3304                if (!dest_se_tpg)
3305                        continue;
3306                dest_tf_ops = dest_se_tpg->se_tpg_tfo;
3307                if (!dest_tf_ops)
3308                        continue;
3309
3310                atomic_inc(&dest_se_tpg->tpg_pr_ref_count);
3311                smp_mb__after_atomic_inc();
3312                spin_unlock(&dev->se_port_lock);
3313
3314                if (core_scsi3_tpg_depend_item(dest_se_tpg)) {
3315                        pr_err("core_scsi3_tpg_depend_item() failed"
3316                                " for dest_se_tpg\n");
3317                        atomic_dec(&dest_se_tpg->tpg_pr_ref_count);
3318                        smp_mb__after_atomic_dec();
3319                        ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3320                        goto out_put_pr_reg;
3321                }
3322
3323                spin_lock(&dev->se_port_lock);
3324                break;
3325        }
3326        spin_unlock(&dev->se_port_lock);
3327
3328        if (!dest_se_tpg || !dest_tf_ops) {
3329                pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3330                        " fabric ops from Relative Target Port Identifier:"
3331                        " %hu\n", rtpi);
3332                ret = TCM_INVALID_PARAMETER_LIST;
3333                goto out_put_pr_reg;
3334        }
3335
3336        buf = transport_kmap_data_sg(cmd);
3337        if (!buf) {
3338                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3339                goto out_put_pr_reg;
3340        }
3341        proto_ident = (buf[24] & 0x0f);
3342
3343        pr_debug("SPC-3 PR REGISTER_AND_MOVE: Extracted Protocol Identifier:"
3344                        " 0x%02x\n", proto_ident);
3345
3346        if (proto_ident != dest_tf_ops->get_fabric_proto_ident(dest_se_tpg)) {
3347                pr_err("SPC-3 PR REGISTER_AND_MOVE: Received"
3348                        " proto_ident: 0x%02x does not match ident: 0x%02x"
3349                        " from fabric: %s\n", proto_ident,
3350                        dest_tf_ops->get_fabric_proto_ident(dest_se_tpg),
3351                        dest_tf_ops->get_fabric_name());
3352                ret = TCM_INVALID_PARAMETER_LIST;
3353                goto out;
3354        }
3355        if (dest_tf_ops->tpg_parse_pr_out_transport_id == NULL) {
3356                pr_err("SPC-3 PR REGISTER_AND_MOVE: Fabric does not"
3357                        " containg a valid tpg_parse_pr_out_transport_id"
3358                        " function pointer\n");
3359                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3360                goto out;
3361        }
3362        initiator_str = dest_tf_ops->tpg_parse_pr_out_transport_id(dest_se_tpg,
3363                        (const char *)&buf[24], &tmp_tid_len, &iport_ptr);
3364        if (!initiator_str) {
3365                pr_err("SPC-3 PR REGISTER_AND_MOVE: Unable to locate"
3366                        " initiator_str from Transport ID\n");
3367                ret = TCM_INVALID_PARAMETER_LIST;
3368                goto out;
3369        }
3370
3371        transport_kunmap_data_sg(cmd);
3372        buf = NULL;
3373
3374        pr_debug("SPC-3 PR [%s] Extracted initiator %s identifier: %s"
3375                " %s\n", dest_tf_ops->get_fabric_name(), (iport_ptr != NULL) ?
3376                "port" : "device", initiator_str, (iport_ptr != NULL) ?
3377                iport_ptr : "");
3378        /*
3379         * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3380         * action specifies a TransportID that is the same as the initiator port
3381         * of the I_T nexus for the command received, then the command shall
3382         * be terminated with CHECK CONDITION status, with the sense key set to
3383         * ILLEGAL REQUEST, and the additional sense code set to INVALID FIELD
3384         * IN PARAMETER LIST.
3385         */
3386        pr_reg_nacl = pr_reg->pr_reg_nacl;
3387        matching_iname = (!strcmp(initiator_str,
3388                                  pr_reg_nacl->initiatorname)) ? 1 : 0;
3389        if (!matching_iname)
3390                goto after_iport_check;
3391
3392        if (!iport_ptr || !pr_reg->isid_present_at_reg) {
3393                pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s"
3394                        " matches: %s on received I_T Nexus\n", initiator_str,
3395                        pr_reg_nacl->initiatorname);
3396                ret = TCM_INVALID_PARAMETER_LIST;
3397                goto out;
3398        }
3399        if (!strcmp(iport_ptr, pr_reg->pr_reg_isid)) {
3400                pr_err("SPC-3 PR REGISTER_AND_MOVE: TransportID: %s %s"
3401                        " matches: %s %s on received I_T Nexus\n",
3402                        initiator_str, iport_ptr, pr_reg_nacl->initiatorname,
3403                        pr_reg->pr_reg_isid);
3404                ret = TCM_INVALID_PARAMETER_LIST;
3405                goto out;
3406        }
3407after_iport_check:
3408        /*
3409         * Locate the destination struct se_node_acl from the received Transport ID
3410         */
3411        spin_lock_irq(&dest_se_tpg->acl_node_lock);
3412        dest_node_acl = __core_tpg_get_initiator_node_acl(dest_se_tpg,
3413                                initiator_str);
3414        if (dest_node_acl) {
3415                atomic_inc(&dest_node_acl->acl_pr_ref_count);
3416                smp_mb__after_atomic_inc();
3417        }
3418        spin_unlock_irq(&dest_se_tpg->acl_node_lock);
3419
3420        if (!dest_node_acl) {
3421                pr_err("Unable to locate %s dest_node_acl for"
3422                        " TransportID%s\n", dest_tf_ops->get_fabric_name(),
3423                        initiator_str);
3424                ret = TCM_INVALID_PARAMETER_LIST;
3425                goto out;
3426        }
3427
3428        if (core_scsi3_nodeacl_depend_item(dest_node_acl)) {
3429                pr_err("core_scsi3_nodeacl_depend_item() for"
3430                        " dest_node_acl\n");
3431                atomic_dec(&dest_node_acl->acl_pr_ref_count);
3432                smp_mb__after_atomic_dec();
3433                dest_node_acl = NULL;
3434                ret = TCM_INVALID_PARAMETER_LIST;
3435                goto out;
3436        }
3437
3438        pr_debug("SPC-3 PR REGISTER_AND_MOVE: Found %s dest_node_acl:"
3439                " %s from TransportID\n", dest_tf_ops->get_fabric_name(),
3440                dest_node_acl->initiatorname);
3441
3442        /*
3443         * Locate the struct se_dev_entry pointer for the matching RELATIVE TARGET
3444         * PORT IDENTIFIER.
3445         */
3446        dest_se_deve = core_get_se_deve_from_rtpi(dest_node_acl, rtpi);
3447        if (!dest_se_deve) {
3448                pr_err("Unable to locate %s dest_se_deve from RTPI:"
3449                        " %hu\n",  dest_tf_ops->get_fabric_name(), rtpi);
3450                ret = TCM_INVALID_PARAMETER_LIST;
3451                goto out;
3452        }
3453
3454        if (core_scsi3_lunacl_depend_item(dest_se_deve)) {
3455                pr_err("core_scsi3_lunacl_depend_item() failed\n");
3456                atomic_dec(&dest_se_deve->pr_ref_count);
3457                smp_mb__after_atomic_dec();
3458                dest_se_deve = NULL;
3459                ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3460                goto out;
3461        }
3462
3463        pr_debug("SPC-3 PR REGISTER_AND_MOVE: Located %s node %s LUN"
3464                " ACL for dest_se_deve->mapped_lun: %u\n",
3465                dest_tf_ops->get_fabric_name(), dest_node_acl->initiatorname,
3466                dest_se_deve->mapped_lun);
3467
3468        /*
3469         * A persistent reservation needs to already existing in order to
3470         * successfully complete the REGISTER_AND_MOVE service action..
3471         */
3472        spin_lock(&dev->dev_reservation_lock);
3473        pr_res_holder = dev->dev_pr_res_holder;
3474        if (!pr_res_holder) {
3475                pr_warn("SPC-3 PR REGISTER_AND_MOVE: No reservation"
3476                        " currently held\n");
3477                spin_unlock(&dev->dev_reservation_lock);
3478                ret = TCM_INVALID_CDB_FIELD;
3479                goto out;
3480        }
3481        /*
3482         * The received on I_T Nexus must be the reservation holder.
3483         *
3484         * From spc4r17 section 5.7.8  Table 50 --
3485         *      Register behaviors for a REGISTER AND MOVE service action
3486         */
3487        if (pr_res_holder != pr_reg) {
3488                pr_warn("SPC-3 PR REGISTER_AND_MOVE: Calling I_T"
3489                        " Nexus is not reservation holder\n");
3490                spin_unlock(&dev->dev_reservation_lock);
3491                ret = TCM_RESERVATION_CONFLICT;
3492                goto out;
3493        }
3494        /*
3495         * From spc4r17 section 5.7.8: registering and moving reservation
3496         *
3497         * If a PERSISTENT RESERVE OUT command with a REGISTER AND MOVE service
3498         * action is received and the established persistent reservation is a
3499         * Write Exclusive - All Registrants type or Exclusive Access -
3500         * All Registrants type reservation, then the command shall be completed
3501         * with RESERVATION CONFLICT status.
3502         */
3503        if ((pr_res_holder->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3504            (pr_res_holder->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG)) {
3505                pr_warn("SPC-3 PR REGISTER_AND_MOVE: Unable to move"
3506                        " reservation for type: %s\n",
3507                        core_scsi3_pr_dump_type(pr_res_holder->pr_res_type));
3508                spin_unlock(&dev->dev_reservation_lock);
3509                ret = TCM_RESERVATION_CONFLICT;
3510                goto out;
3511        }
3512        pr_res_nacl = pr_res_holder->pr_reg_nacl;
3513        /*
3514         * b) Ignore the contents of the (received) SCOPE and TYPE fields;
3515         */
3516        type = pr_res_holder->pr_res_type;
3517        scope = pr_res_holder->pr_res_type;
3518        /*
3519         * c) Associate the reservation key specified in the SERVICE ACTION
3520         *    RESERVATION KEY field with the I_T nexus specified as the
3521         *    destination of the register and move, where:
3522         *    A) The I_T nexus is specified by the TransportID and the
3523         *       RELATIVE TARGET PORT IDENTIFIER field (see 6.14.4); and
3524         *    B) Regardless of the TransportID format used, the association for
3525         *       the initiator port is based on either the initiator port name
3526         *       (see 3.1.71) on SCSI transport protocols where port names are
3527         *       required or the initiator port identifier (see 3.1.70) on SCSI
3528         *       transport protocols where port names are not required;
3529         * d) Register the reservation key specified in the SERVICE ACTION
3530         *    RESERVATION KEY field;
3531         * e) Retain the reservation key specified in the SERVICE ACTION
3532         *    RESERVATION KEY field and associated information;
3533         *
3534         * Also, It is not an error for a REGISTER AND MOVE service action to
3535         * register an I_T nexus that is already registered with the same
3536         * reservation key or a different reservation key.
3537         */
3538        dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3539                                        iport_ptr);
3540        if (!dest_pr_reg) {
3541                if (core_scsi3_alloc_registration(cmd->se_dev,
3542                                dest_node_acl, dest_se_deve, iport_ptr,
3543                                sa_res_key, 0, aptpl, 2, 1)) {
3544                        spin_unlock(&dev->dev_reservation_lock);
3545                        ret = TCM_INVALID_PARAMETER_LIST;
3546                        goto out;
3547                }
3548                dest_pr_reg = __core_scsi3_locate_pr_reg(dev, dest_node_acl,
3549                                                iport_ptr);
3550                new_reg = 1;
3551        }
3552        /*
3553         * f) Release the persistent reservation for the persistent reservation
3554         *    holder (i.e., the I_T nexus on which the
3555         */
3556        __core_scsi3_complete_pro_release(dev, pr_res_nacl,
3557                        dev->dev_pr_res_holder, 0);
3558        /*
3559         * g) Move the persistent reservation to the specified I_T nexus using
3560         *    the same scope and type as the persistent reservation released in
3561         *    item f); and
3562         */
3563        dev->dev_pr_res_holder = dest_pr_reg;
3564        dest_pr_reg->pr_res_holder = 1;
3565        dest_pr_reg->pr_res_type = type;
3566        pr_reg->pr_res_scope = scope;
3567        prf_isid = core_pr_dump_initiator_port(pr_reg, &i_buf[0],
3568                                PR_REG_ISID_ID_LEN);
3569        /*
3570         * Increment PRGeneration for existing registrations..
3571         */
3572        if (!new_reg)
3573                dest_pr_reg->pr_res_generation = pr_tmpl->pr_generation++;
3574        spin_unlock(&dev->dev_reservation_lock);
3575
3576        pr_debug("SPC-3 PR [%s] Service Action: REGISTER_AND_MOVE"
3577                " created new reservation holder TYPE: %s on object RTPI:"
3578                " %hu  PRGeneration: 0x%08x\n", dest_tf_ops->get_fabric_name(),
3579                core_scsi3_pr_dump_type(type), rtpi,
3580                dest_pr_reg->pr_res_generation);
3581        pr_debug("SPC-3 PR Successfully moved reservation from"
3582                " %s Fabric Node: %s%s -> %s Fabric Node: %s %s\n",
3583                tf_ops->get_fabric_name(), pr_reg_nacl->initiatorname,
3584                (prf_isid) ? &i_buf[0] : "", dest_tf_ops->get_fabric_name(),
3585                dest_node_acl->initiatorname, (iport_ptr != NULL) ?
3586                iport_ptr : "");
3587        /*
3588         * It is now safe to release configfs group dependencies for destination
3589         * of Transport ID Initiator Device/Port Identifier
3590         */
3591        core_scsi3_lunacl_undepend_item(dest_se_deve);
3592        core_scsi3_nodeacl_undepend_item(dest_node_acl);
3593        core_scsi3_tpg_undepend_item(dest_se_tpg);
3594        /*
3595         * h) If the UNREG bit is set to one, unregister (see 5.7.11.3) the I_T
3596         * nexus on which PERSISTENT RESERVE OUT command was received.
3597         */
3598        if (unreg) {
3599                spin_lock(&pr_tmpl->registration_lock);
3600                __core_scsi3_free_registration(dev, pr_reg, NULL, 1);
3601                spin_unlock(&pr_tmpl->registration_lock);
3602        } else
3603                core_scsi3_put_pr_reg(pr_reg);
3604
3605        /*
3606         * Clear the APTPL metadata if APTPL has been disabled, otherwise
3607         * write out the updated metadata to struct file for this SCSI device.
3608         */
3609        if (!aptpl) {
3610                pr_tmpl->pr_aptpl_active = 0;
3611                core_scsi3_update_and_write_aptpl(cmd->se_dev, NULL, 0);
3612                pr_debug("SPC-3 PR: Set APTPL Bit Deactivated for"
3613                                " REGISTER_AND_MOVE\n");
3614        } else {
3615                pr_tmpl->pr_aptpl_active = 1;
3616                if (!core_scsi3_update_and_write_aptpl(cmd->se_dev,
3617                                &dest_pr_reg->pr_aptpl_buf[0],
3618                                pr_tmpl->pr_aptpl_buf_len)) {
3619                        pr_debug("SPC-3 PR: Set APTPL Bit Activated for"
3620                                        " REGISTER_AND_MOVE\n");
3621                }
3622        }
3623
3624        transport_kunmap_data_sg(cmd);
3625
3626        core_scsi3_put_pr_reg(dest_pr_reg);
3627        return 0;
3628out:
3629        if (buf)
3630                transport_kunmap_data_sg(cmd);
3631        if (dest_se_deve)
3632                core_scsi3_lunacl_undepend_item(dest_se_deve);
3633        if (dest_node_acl)
3634                core_scsi3_nodeacl_undepend_item(dest_node_acl);
3635        core_scsi3_tpg_undepend_item(dest_se_tpg);
3636
3637out_put_pr_reg:
3638        core_scsi3_put_pr_reg(pr_reg);
3639        return ret;
3640}
3641
3642static unsigned long long core_scsi3_extract_reservation_key(unsigned char *cdb)
3643{
3644        unsigned int __v1, __v2;
3645
3646        __v1 = (cdb[0] << 24) | (cdb[1] << 16) | (cdb[2] << 8) | cdb[3];
3647        __v2 = (cdb[4] << 24) | (cdb[5] << 16) | (cdb[6] << 8) | cdb[7];
3648
3649        return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
3650}
3651
3652/*
3653 * See spc4r17 section 6.14 Table 170
3654 */
3655sense_reason_t
3656target_scsi3_emulate_pr_out(struct se_cmd *cmd)
3657{
3658        unsigned char *cdb = &cmd->t_task_cdb[0];
3659        unsigned char *buf;
3660        u64 res_key, sa_res_key;
3661        int sa, scope, type, aptpl;
3662        int spec_i_pt = 0, all_tg_pt = 0, unreg = 0;
3663        sense_reason_t ret;
3664
3665        /*
3666         * Following spc2r20 5.5.1 Reservations overview:
3667         *
3668         * If a logical unit has been reserved by any RESERVE command and is
3669         * still reserved by any initiator, all PERSISTENT RESERVE IN and all
3670         * PERSISTENT RESERVE OUT commands shall conflict regardless of
3671         * initiator or service action and shall terminate with a RESERVATION
3672         * CONFLICT status.
3673         */
3674        if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
3675                pr_err("Received PERSISTENT_RESERVE CDB while legacy"
3676                        " SPC-2 reservation is held, returning"
3677                        " RESERVATION_CONFLICT\n");
3678                return TCM_RESERVATION_CONFLICT;
3679        }
3680
3681        /*
3682         * FIXME: A NULL struct se_session pointer means an this is not coming from
3683         * a $FABRIC_MOD's nexus, but from internal passthrough ops.
3684         */
3685        if (!cmd->se_sess)
3686                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3687
3688        if (cmd->data_length < 24) {
3689                pr_warn("SPC-PR: Received PR OUT parameter list"
3690                        " length too small: %u\n", cmd->data_length);
3691                return TCM_INVALID_PARAMETER_LIST;
3692        }
3693
3694        /*
3695         * From the PERSISTENT_RESERVE_OUT command descriptor block (CDB)
3696         */
3697        sa = (cdb[1] & 0x1f);
3698        scope = (cdb[2] & 0xf0);
3699        type = (cdb[2] & 0x0f);
3700
3701        buf = transport_kmap_data_sg(cmd);
3702        if (!buf)
3703                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3704
3705        /*
3706         * From PERSISTENT_RESERVE_OUT parameter list (payload)
3707         */
3708        res_key = core_scsi3_extract_reservation_key(&buf[0]);
3709        sa_res_key = core_scsi3_extract_reservation_key(&buf[8]);
3710        /*
3711         * REGISTER_AND_MOVE uses a different SA parameter list containing
3712         * SCSI TransportIDs.
3713         */
3714        if (sa != PRO_REGISTER_AND_MOVE) {
3715                spec_i_pt = (buf[20] & 0x08);
3716                all_tg_pt = (buf[20] & 0x04);
3717                aptpl = (buf[20] & 0x01);
3718        } else {
3719                aptpl = (buf[17] & 0x01);
3720                unreg = (buf[17] & 0x02);
3721        }
3722        transport_kunmap_data_sg(cmd);
3723        buf = NULL;
3724
3725        /*
3726         * SPEC_I_PT=1 is only valid for Service action: REGISTER
3727         */
3728        if (spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER))
3729                return TCM_INVALID_PARAMETER_LIST;
3730
3731        /*
3732         * From spc4r17 section 6.14:
3733         *
3734         * If the SPEC_I_PT bit is set to zero, the service action is not
3735         * REGISTER AND MOVE, and the parameter list length is not 24, then
3736         * the command shall be terminated with CHECK CONDITION status, with
3737         * the sense key set to ILLEGAL REQUEST, and the additional sense
3738         * code set to PARAMETER LIST LENGTH ERROR.
3739         */
3740        if (!spec_i_pt && ((cdb[1] & 0x1f) != PRO_REGISTER_AND_MOVE) &&
3741            (cmd->data_length != 24)) {
3742                pr_warn("SPC-PR: Received PR OUT illegal parameter"
3743                        " list length: %u\n", cmd->data_length);
3744                return TCM_INVALID_PARAMETER_LIST;
3745        }
3746
3747        /*
3748         * (core_scsi3_emulate_pro_* function parameters
3749         * are defined by spc4r17 Table 174:
3750         * PERSISTENT_RESERVE_OUT service actions and valid parameters.
3751         */
3752        switch (sa) {
3753        case PRO_REGISTER:
3754                ret = core_scsi3_emulate_pro_register(cmd,
3755                        res_key, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 0);
3756                break;
3757        case PRO_RESERVE:
3758                ret = core_scsi3_emulate_pro_reserve(cmd, type, scope, res_key);
3759                break;
3760        case PRO_RELEASE:
3761                ret = core_scsi3_emulate_pro_release(cmd, type, scope, res_key);
3762                break;
3763        case PRO_CLEAR:
3764                ret = core_scsi3_emulate_pro_clear(cmd, res_key);
3765                break;
3766        case PRO_PREEMPT:
3767                ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3768                                        res_key, sa_res_key, 0);
3769                break;
3770        case PRO_PREEMPT_AND_ABORT:
3771                ret = core_scsi3_emulate_pro_preempt(cmd, type, scope,
3772                                        res_key, sa_res_key, 1);
3773                break;
3774        case PRO_REGISTER_AND_IGNORE_EXISTING_KEY:
3775                ret = core_scsi3_emulate_pro_register(cmd,
3776                        0, sa_res_key, aptpl, all_tg_pt, spec_i_pt, 1);
3777                break;
3778        case PRO_REGISTER_AND_MOVE:
3779                ret = core_scsi3_emulate_pro_register_and_move(cmd, res_key,
3780                                sa_res_key, aptpl, unreg);
3781                break;
3782        default:
3783                pr_err("Unknown PERSISTENT_RESERVE_OUT service"
3784                        " action: 0x%02x\n", cdb[1] & 0x1f);
3785                return TCM_INVALID_CDB_FIELD;
3786        }
3787
3788        if (!ret)
3789                target_complete_cmd(cmd, GOOD);
3790        return ret;
3791}
3792
3793/*
3794 * PERSISTENT_RESERVE_IN Service Action READ_KEYS
3795 *
3796 * See spc4r17 section 5.7.6.2 and section 6.13.2, Table 160
3797 */
3798static sense_reason_t
3799core_scsi3_pri_read_keys(struct se_cmd *cmd)
3800{
3801        struct se_device *dev = cmd->se_dev;
3802        struct t10_pr_registration *pr_reg;
3803        unsigned char *buf;
3804        u32 add_len = 0, off = 8;
3805
3806        if (cmd->data_length < 8) {
3807                pr_err("PRIN SA READ_KEYS SCSI Data Length: %u"
3808                        " too small\n", cmd->data_length);
3809                return TCM_INVALID_CDB_FIELD;
3810        }
3811
3812        buf = transport_kmap_data_sg(cmd);
3813        if (!buf)
3814                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3815
3816        buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3817        buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3818        buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3819        buf[3] = (dev->t10_pr.pr_generation & 0xff);
3820
3821        spin_lock(&dev->t10_pr.registration_lock);
3822        list_for_each_entry(pr_reg, &dev->t10_pr.registration_list,
3823                        pr_reg_list) {
3824                /*
3825                 * Check for overflow of 8byte PRI READ_KEYS payload and
3826                 * next reservation key list descriptor.
3827                 */
3828                if ((add_len + 8) > (cmd->data_length - 8))
3829                        break;
3830
3831                buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
3832                buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
3833                buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
3834                buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
3835                buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
3836                buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
3837                buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
3838                buf[off++] = (pr_reg->pr_res_key & 0xff);
3839
3840                add_len += 8;
3841        }
3842        spin_unlock(&dev->t10_pr.registration_lock);
3843
3844        buf[4] = ((add_len >> 24) & 0xff);
3845        buf[5] = ((add_len >> 16) & 0xff);
3846        buf[6] = ((add_len >> 8) & 0xff);
3847        buf[7] = (add_len & 0xff);
3848
3849        transport_kunmap_data_sg(cmd);
3850
3851        return 0;
3852}
3853
3854/*
3855 * PERSISTENT_RESERVE_IN Service Action READ_RESERVATION
3856 *
3857 * See spc4r17 section 5.7.6.3 and section 6.13.3.2 Table 161 and 162
3858 */
3859static sense_reason_t
3860core_scsi3_pri_read_reservation(struct se_cmd *cmd)
3861{
3862        struct se_device *dev = cmd->se_dev;
3863        struct t10_pr_registration *pr_reg;
3864        unsigned char *buf;
3865        u64 pr_res_key;
3866        u32 add_len = 16; /* Hardcoded to 16 when a reservation is held. */
3867
3868        if (cmd->data_length < 8) {
3869                pr_err("PRIN SA READ_RESERVATIONS SCSI Data Length: %u"
3870                        " too small\n", cmd->data_length);
3871                return TCM_INVALID_CDB_FIELD;
3872        }
3873
3874        buf = transport_kmap_data_sg(cmd);
3875        if (!buf)
3876                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3877
3878        buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
3879        buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
3880        buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
3881        buf[3] = (dev->t10_pr.pr_generation & 0xff);
3882
3883        spin_lock(&dev->dev_reservation_lock);
3884        pr_reg = dev->dev_pr_res_holder;
3885        if (pr_reg) {
3886                /*
3887                 * Set the hardcoded Additional Length
3888                 */
3889                buf[4] = ((add_len >> 24) & 0xff);
3890                buf[5] = ((add_len >> 16) & 0xff);
3891                buf[6] = ((add_len >> 8) & 0xff);
3892                buf[7] = (add_len & 0xff);
3893
3894                if (cmd->data_length < 22)
3895                        goto err;
3896
3897                /*
3898                 * Set the Reservation key.
3899                 *
3900                 * From spc4r17, section 5.7.10:
3901                 * A persistent reservation holder has its reservation key
3902                 * returned in the parameter data from a PERSISTENT
3903                 * RESERVE IN command with READ RESERVATION service action as
3904                 * follows:
3905                 * a) For a persistent reservation of the type Write Exclusive
3906                 *    - All Registrants or Exclusive Access ­ All Regitrants,
3907                 *      the reservation key shall be set to zero; or
3908                 * b) For all other persistent reservation types, the
3909                 *    reservation key shall be set to the registered
3910                 *    reservation key for the I_T nexus that holds the
3911                 *    persistent reservation.
3912                 */
3913                if ((pr_reg->pr_res_type == PR_TYPE_WRITE_EXCLUSIVE_ALLREG) ||
3914                    (pr_reg->pr_res_type == PR_TYPE_EXCLUSIVE_ACCESS_ALLREG))
3915                        pr_res_key = 0;
3916                else
3917                        pr_res_key = pr_reg->pr_res_key;
3918
3919                buf[8] = ((pr_res_key >> 56) & 0xff);
3920                buf[9] = ((pr_res_key >> 48) & 0xff);
3921                buf[10] = ((pr_res_key >> 40) & 0xff);
3922                buf[11] = ((pr_res_key >> 32) & 0xff);
3923                buf[12] = ((pr_res_key >> 24) & 0xff);
3924                buf[13] = ((pr_res_key >> 16) & 0xff);
3925                buf[14] = ((pr_res_key >> 8) & 0xff);
3926                buf[15] = (pr_res_key & 0xff);
3927                /*
3928                 * Set the SCOPE and TYPE
3929                 */
3930                buf[21] = (pr_reg->pr_res_scope & 0xf0) |
3931                          (pr_reg->pr_res_type & 0x0f);
3932        }
3933
3934err:
3935        spin_unlock(&dev->dev_reservation_lock);
3936        transport_kunmap_data_sg(cmd);
3937
3938        return 0;
3939}
3940
3941/*
3942 * PERSISTENT_RESERVE_IN Service Action REPORT_CAPABILITIES
3943 *
3944 * See spc4r17 section 6.13.4 Table 165
3945 */
3946static sense_reason_t
3947core_scsi3_pri_report_capabilities(struct se_cmd *cmd)
3948{
3949        struct se_device *dev = cmd->se_dev;
3950        struct t10_reservation *pr_tmpl = &dev->t10_pr;
3951        unsigned char *buf;
3952        u16 add_len = 8; /* Hardcoded to 8. */
3953
3954        if (cmd->data_length < 6) {
3955                pr_err("PRIN SA REPORT_CAPABILITIES SCSI Data Length:"
3956                        " %u too small\n", cmd->data_length);
3957                return TCM_INVALID_CDB_FIELD;
3958        }
3959
3960        buf = transport_kmap_data_sg(cmd);
3961        if (!buf)
3962                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3963
3964        buf[0] = ((add_len << 8) & 0xff);
3965        buf[1] = (add_len & 0xff);
3966        buf[2] |= 0x10; /* CRH: Compatible Reservation Hanlding bit. */
3967        buf[2] |= 0x08; /* SIP_C: Specify Initiator Ports Capable bit */
3968        buf[2] |= 0x04; /* ATP_C: All Target Ports Capable bit */
3969        buf[2] |= 0x01; /* PTPL_C: Persistence across Target Power Loss bit */
3970        /*
3971         * We are filling in the PERSISTENT RESERVATION TYPE MASK below, so
3972         * set the TMV: Task Mask Valid bit.
3973         */
3974        buf[3] |= 0x80;
3975        /*
3976         * Change ALLOW COMMANDs to 0x20 or 0x40 later from Table 166
3977         */
3978        buf[3] |= 0x10; /* ALLOW COMMANDs field 001b */
3979        /*
3980         * PTPL_A: Persistence across Target Power Loss Active bit
3981         */
3982        if (pr_tmpl->pr_aptpl_active)
3983                buf[3] |= 0x01;
3984        /*
3985         * Setup the PERSISTENT RESERVATION TYPE MASK from Table 167
3986         */
3987        buf[4] |= 0x80; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3988        buf[4] |= 0x40; /* PR_TYPE_EXCLUSIVE_ACCESS_REGONLY */
3989        buf[4] |= 0x20; /* PR_TYPE_WRITE_EXCLUSIVE_REGONLY */
3990        buf[4] |= 0x08; /* PR_TYPE_EXCLUSIVE_ACCESS */
3991        buf[4] |= 0x02; /* PR_TYPE_WRITE_EXCLUSIVE */
3992        buf[5] |= 0x01; /* PR_TYPE_EXCLUSIVE_ACCESS_ALLREG */
3993
3994        transport_kunmap_data_sg(cmd);
3995
3996        return 0;
3997}
3998
3999/*
4000 * PERSISTENT_RESERVE_IN Service Action READ_FULL_STATUS
4001 *
4002 * See spc4r17 section 6.13.5 Table 168 and 169
4003 */
4004static sense_reason_t
4005core_scsi3_pri_read_full_status(struct se_cmd *cmd)
4006{
4007        struct se_device *dev = cmd->se_dev;
4008        struct se_node_acl *se_nacl;
4009        struct se_portal_group *se_tpg;
4010        struct t10_pr_registration *pr_reg, *pr_reg_tmp;
4011        struct t10_reservation *pr_tmpl = &dev->t10_pr;
4012        unsigned char *buf;
4013        u32 add_desc_len = 0, add_len = 0, desc_len, exp_desc_len;
4014        u32 off = 8; /* off into first Full Status descriptor */
4015        int format_code = 0;
4016
4017        if (cmd->data_length < 8) {
4018                pr_err("PRIN SA READ_FULL_STATUS SCSI Data Length: %u"
4019                        " too small\n", cmd->data_length);
4020                return TCM_INVALID_CDB_FIELD;
4021        }
4022
4023        buf = transport_kmap_data_sg(cmd);
4024        if (!buf)
4025                return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4026
4027        buf[0] = ((dev->t10_pr.pr_generation >> 24) & 0xff);
4028        buf[1] = ((dev->t10_pr.pr_generation >> 16) & 0xff);
4029        buf[2] = ((dev->t10_pr.pr_generation >> 8) & 0xff);
4030        buf[3] = (dev->t10_pr.pr_generation & 0xff);
4031
4032        spin_lock(&pr_tmpl->registration_lock);
4033        list_for_each_entry_safe(pr_reg, pr_reg_tmp,
4034                        &pr_tmpl->registration_list, pr_reg_list) {
4035
4036                se_nacl = pr_reg->pr_reg_nacl;
4037                se_tpg = pr_reg->pr_reg_nacl->se_tpg;
4038                add_desc_len = 0;
4039
4040                atomic_inc(&pr_reg->pr_res_holders);
4041                smp_mb__after_atomic_inc();
4042                spin_unlock(&pr_tmpl->registration_lock);
4043                /*
4044                 * Determine expected length of $FABRIC_MOD specific
4045                 * TransportID full status descriptor..
4046                 */
4047                exp_desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id_len(
4048                                se_tpg, se_nacl, pr_reg, &format_code);
4049
4050                if ((exp_desc_len + add_len) > cmd->data_length) {
4051                        pr_warn("SPC-3 PRIN READ_FULL_STATUS ran"
4052                                " out of buffer: %d\n", cmd->data_length);
4053                        spin_lock(&pr_tmpl->registration_lock);
4054                        atomic_dec(&pr_reg->pr_res_holders);
4055                        smp_mb__after_atomic_dec();
4056                        break;
4057                }
4058                /*
4059                 * Set RESERVATION KEY
4060                 */
4061                buf[off++] = ((pr_reg->pr_res_key >> 56) & 0xff);
4062                buf[off++] = ((pr_reg->pr_res_key >> 48) & 0xff);
4063                buf[off++] = ((pr_reg->pr_res_key >> 40) & 0xff);
4064                buf[off++] = ((pr_reg->pr_res_key >> 32) & 0xff);
4065                buf[off++] = ((pr_reg->pr_res_key >> 24) & 0xff);
4066                buf[off++] = ((pr_reg->pr_res_key >> 16) & 0xff);
4067                buf[off++] = ((pr_reg->pr_res_key >> 8) & 0xff);
4068                buf[off++] = (pr_reg->pr_res_key & 0xff);
4069                off += 4; /* Skip Over Reserved area */
4070
4071                /*
4072                 * Set ALL_TG_PT bit if PROUT SA REGISTER had this set.
4073                 */
4074                if (pr_reg->pr_reg_all_tg_pt)
4075                        buf[off] = 0x02;
4076                /*
4077                 * The struct se_lun pointer will be present for the
4078                 * reservation holder for PR_HOLDER bit.
4079                 *
4080                 * Also, if this registration is the reservation
4081                 * holder, fill in SCOPE and TYPE in the next byte.
4082                 */
4083                if (pr_reg->pr_res_holder) {
4084                        buf[off++] |= 0x01;
4085                        buf[off++] = (pr_reg->pr_res_scope & 0xf0) |
4086                                     (pr_reg->pr_res_type & 0x0f);
4087                } else
4088                        off += 2;
4089
4090                off += 4; /* Skip over reserved area */
4091                /*
4092                 * From spc4r17 6.3.15:
4093                 *
4094                 * If the ALL_TG_PT bit set to zero, the RELATIVE TARGET PORT
4095                 * IDENTIFIER field contains the relative port identifier (see
4096                 * 3.1.120) of the target port that is part of the I_T nexus
4097                 * described by this full status descriptor. If the ALL_TG_PT
4098                 * bit is set to one, the contents of the RELATIVE TARGET PORT
4099                 * IDENTIFIER field are not defined by this standard.
4100                 */
4101                if (!pr_reg->pr_reg_all_tg_pt) {
4102                        struct se_port *port = pr_reg->pr_reg_tg_pt_lun->lun_sep;
4103
4104                        buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
4105                        buf[off++] = (port->sep_rtpi & 0xff);
4106                } else
4107                        off += 2; /* Skip over RELATIVE TARGET PORT IDENTIFIER */
4108
4109                /*
4110                 * Now, have the $FABRIC_MOD fill in the protocol identifier
4111                 */
4112                desc_len = se_tpg->se_tpg_tfo->tpg_get_pr_transport_id(se_tpg,
4113                                se_nacl, pr_reg, &format_code, &buf[off+4]);
4114
4115                spin_lock(&pr_tmpl->registration_lock);
4116                atomic_dec(&pr_reg->pr_res_holders);
4117                smp_mb__after_atomic_dec();
4118                /*
4119                 * Set the ADDITIONAL DESCRIPTOR LENGTH
4120                 */
4121                buf[off++] = ((desc_len >> 24) & 0xff);
4122                buf[off++] = ((desc_len >> 16) & 0xff);
4123                buf[off++] = ((desc_len >> 8) & 0xff);
4124                buf[off++] = (desc_len & 0xff);
4125                /*
4126                 * Size of full desctipor header minus TransportID
4127                 * containing $FABRIC_MOD specific) initiator device/port
4128                 * WWN information.
4129                 *
4130                 *  See spc4r17 Section 6.13.5 Table 169
4131                 */
4132                add_desc_len = (24 + desc_len);
4133
4134                off += desc_len;
4135                add_len += add_desc_len;
4136        }
4137        spin_unlock(&pr_tmpl->registration_lock);
4138        /*
4139         * Set ADDITIONAL_LENGTH
4140         */
4141        buf[4] = ((add_len >> 24) & 0xff);
4142        buf[5] = ((add_len >> 16) & 0xff);
4143        buf[6] = ((add_len >> 8) & 0xff);
4144        buf[7] = (add_len & 0xff);
4145
4146        transport_kunmap_data_sg(cmd);
4147
4148        return 0;
4149}
4150
4151sense_reason_t
4152target_scsi3_emulate_pr_in(struct se_cmd *cmd)
4153{
4154        sense_reason_t ret;
4155
4156        /*
4157         * Following spc2r20 5.5.1 Reservations overview:
4158         *
4159         * If a logical unit has been reserved by any RESERVE command and is
4160         * still reserved by any initiator, all PERSISTENT RESERVE IN and all
4161         * PERSISTENT RESERVE OUT commands shall conflict regardless of
4162         * initiator or service action and shall terminate with a RESERVATION
4163         * CONFLICT status.
4164         */
4165        if (cmd->se_dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS) {
4166                pr_err("Received PERSISTENT_RESERVE CDB while legacy"
4167                        " SPC-2 reservation is held, returning"
4168                        " RESERVATION_CONFLICT\n");
4169                return TCM_RESERVATION_CONFLICT;
4170        }
4171
4172        switch (cmd->t_task_cdb[1] & 0x1f) {
4173        case PRI_READ_KEYS:
4174                ret = core_scsi3_pri_read_keys(cmd);
4175                break;
4176        case PRI_READ_RESERVATION:
4177                ret = core_scsi3_pri_read_reservation(cmd);
4178                break;
4179        case PRI_REPORT_CAPABILITIES:
4180                ret = core_scsi3_pri_report_capabilities(cmd);
4181                break;
4182        case PRI_READ_FULL_STATUS:
4183                ret = core_scsi3_pri_read_full_status(cmd);
4184                break;
4185        default:
4186                pr_err("Unknown PERSISTENT_RESERVE_IN service"
4187                        " action: 0x%02x\n", cmd->t_task_cdb[1] & 0x1f);
4188                return TCM_INVALID_CDB_FIELD;
4189        }
4190
4191        if (!ret)
4192                target_complete_cmd(cmd, GOOD);
4193        return ret;
4194}
4195
4196sense_reason_t
4197target_check_reservation(struct se_cmd *cmd)
4198{
4199        struct se_device *dev = cmd->se_dev;
4200        sense_reason_t ret;
4201
4202        if (!cmd->se_sess)
4203                return 0;
4204        if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
4205                return 0;
4206        if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
4207                return 0;
4208
4209        spin_lock(&dev->dev_reservation_lock);
4210        if (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)
4211                ret = target_scsi2_reservation_check(cmd);
4212        else
4213                ret = target_scsi3_pr_reservation_check(cmd);
4214        spin_unlock(&dev->dev_reservation_lock);
4215
4216        return ret;
4217}
4218
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.