Fix COMPARE_AND_WRITE but leave it disabled.

Fix the compare and write opcode but leave it disabled.
It looks like consumers like vmware may be assuming that IF
compare and write is present and if it works, then the target will also
implement other opcodes like XCOPY etc.

So lets fix the command so that it works, but wait with enabling it
until we have everything else that the main consumer of this rare opcode
expects and wants.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
fix alloc len vs buffer len checks and safeguard memcpy in spc_pr_read_reservation()

SPC-3, 6.13.3.2, alloc len must be at least 8 bytes,
when no reservation is held, additional len field should be 0, total data-in len is 8;
when there is reservation, additional len should be 16, total buffer len is 24.

make sure that setting fields in data-in buffer don't pass its boundary.

Signed-off-by: Alexander Nezhinsky <nezhinsky@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
set Type Mask Valid (TMV) in response for PERSISTENT_RESERVE_IN's REPORT_CAPABILITIES service action

Signed-off-by: Alexander Nezhinsky <nezhinsky@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
fix length checks in check_pr_out_basic_parameter()

SPC-3, Table 170, "PERSISTENT RESERVE OUT command" defines PARAMETER LIST LENGTH
as spanning from byte 5 (MSB) to byte 8 (LSB). Need to extract BE 32bit value
starting at 5th byte, and not 16bit value starting at 7 byte.

SPC-3, 6.14.1 states:
"the PARAMETER LIST LENGTH field shall contain 24 (18h), if the
 following conditions are true:
 a) The SPEC_I_PT bit (see 6.14.3) is set to zero; and
 b) The service action is not REGISTER AND MOVE."
As check_pr_out_basic_parameter() covers exactly the above case, we should
stipulate parameter list length to be exactly 24 bytes and data transfer length
to be at least 24 bytes.

Signed-off-by: Alexander Nezhinsky <nezhinsky@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
fix field length checks in spc_pr_register_and_move()

REGISTER_AND_MOVE defines (SPC-3, 6.14.4) an extended (not basic) data-out format.
- It must be at least 24 bytes long, possibly extended by the Transport ID parameter.
- Transport parameter data length should be at least 24, and if greater, a multiple of 4.
- Expected data transfer length must be at least the declared parameter list length
  (this is a WR cmd, so the entire data must be transferred by the initiator).
- Parameter list length must account correctly for the transport parameter
  data length, so that no truncation occurs.

Except correcting the above checks enforcement, transport id comparison is fixed.
It was done using strncmp(), which is incorrect here, as transport id format
starts with a few binary fields potentially containing zeros which would terminate
string comparison prematurely.
memcmp() is used instead of strncmp(), and the reported id length is used instead
of the value returned by strlen().

Signed-off-by: Alexander Nezhinsky <nezhinsky@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
spc: set periph.qualifier to 7Fh (LUN mismatch) before INQUIRY buffer copy to data-in

Signed-off-by: Alexander Nezhinsky <nezhinsky@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
spc: fix bugs in persistent group reservations

I created a small Persistent Group Reservations test suite, available
at git://github.com/gonzoleeman/open-iscsi-pgr-validate. It uncovered
several bugs in the SCSI target's PGR implementation. Here is a list
of the tests that failed or could not run:

Reserve Exclusive Access:
  - Can release reservation:				ERROR
  - Unregister Releases Reservation:			FAIL

Reserve Write Exclusive:
  - Resvn Holder Unregister Releases Resvn:		FAIL
  - Non-Registrant Does Not Have Read Access:		FAIL
  - Non-Resvn-Holder Has Read Access:			FAIL

Reserve Exclusive Access Registrants Only:
  - Unregister Releases Reservation:			FAIL

Reserve Write Exclusive Registrants Only:
  - Resvn Holder Unregister releases resvn:		FAIL

Reserve Exclusive Access All Registrants:
  - Alternative Resvn Holder Can Release Resvn:		FAIL
  - Main Resvn Unregister Does Not Release Resvn:	FAIL

Reserve Write Exclusive All Registrants:
  - Alt Resvn Holder Can Release Resvn:			FAIL
  - Main Resvn Holder Unregister does not
	 release resvn:					FAIL

This patch enables all PGR tests is said suite to pass.

Changes since v1 of this patch:
 - Removed refactoring for pr_type all registrant checks
 - removed previously-added list_is_last() and use thereof

Signed-off-by: Lee Duncan <lduncan@suse.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
ssc: make virtual tapes more closely emulate physical ones

On my physical drive (a Sony AIT-2 drive, if it matters) when
userspace reads a file mark, it gets an EOF, and the tape is advanced
past the file mark so that a subsequent read will return data from the
next file on the tape.  With a virtual drive, the file mark is
sticky--reads continue to return EOF until the tape is manually
advanced with a FSF ioctl.

If the attempt to read the next block fails, return
ASC_MEDIUM_FORMAT_CORRUPT.

Signed-off-by: Jay Fenlason <fenlason@redhat.com>
Acked-by: Mark Harvey <markh794@gmail.com>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>