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