{
  "slug": "scsi-compare-and-write",
  "term": "SCSI COMPARE AND WRITE",
  "aliases": [
    "SCSI atomic compare and write"
  ],
  "definition": "SCSI COMPARE AND WRITE conditionally replaces a logical-block range when its contents match supplied comparison data, performing comparison and conditional replacement as one uninterrupted operation.",
  "note": "The host supplies expected and replacement buffers for the same LBA range; a mismatch reports MISCOMPARE without performing the replacement. The device executes comparison and conditional writing without interruption, unlike separate host read and write commands. [sg-compare-write]\n\nThe Block Limits VPD page reports MAXIMUM COMPARE AND WRITE LENGTH in logical blocks. A zero value in this field means the command is unsupported; a nonzero value is the largest accepted block count, not a byte count. [scsi-compare-limits]\n\nThe documented sg_compare_and_write utility calculates its default data-out length assuming 512 bytes per logical block, without querying READ CAPACITY. Other block sizes and protection-information settings require appropriate transfer sizing. [sg-compare-write]\n\nIn the pinned sg3_utils implementation, MISCOMPARE with valid sense Information reports the first differing byte offset; without valid Information it reports a mismatch without an offset. This byte offset is not a failing LBA. [sg-compare-diagnostic]\n\nHesela's interpretation: this conditional block update does not by itself make a multi-command workflow transactional or establish its power-loss durability; those require separate persistence and recovery contracts.",
  "status": "shipping",
  "advantages": [],
  "limitations": [],
  "related": [
    "compare-and-swap",
    "nvme-compare-and-write",
    "conditional-write",
    "force-unit-access",
    "logical-block-address",
    "scsi-information-field"
  ],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "citations": [
    {
      "id": "sg-compare-write",
      "title": "sg3_utils: sg_compare_and_write manual, DESCRIPTION and OPTIONS; source commit 8799caa3741b2b1640970747aeb84f17dbbaad86.",
      "url": "https://raw.githubusercontent.com/doug-gilbert/sg3_utils/8799caa3741b2b1640970747aeb84f17dbbaad86/doc/sg_compare_and_write.8",
      "accessed": "2026-11-08"
    },
    {
      "id": "scsi-compare-limits",
      "title": "Seagate SCSI Commands Reference Manual, 100293068 Rev. L, November 2020, section 5.4.5, Table 520 and page 534.",
      "url": "https://www.seagate.com/files/staticfiles/support/docs/manual/Interface%20manuals/100293068L.pdf",
      "accessed": "2026-11-08"
    },
    {
      "id": "sg-compare-diagnostic",
      "title": "sg3_utils: sg_compare_and_write.c, scsi_compare_and_write MISCOMPARE branch; commit 8799caa3741b2b1640970747aeb84f17dbbaad86.",
      "url": "https://raw.githubusercontent.com/doug-gilbert/sg3_utils/8799caa3741b2b1640970747aeb84f17dbbaad86/src/sg_compare_and_write.c",
      "accessed": "2026-11-08"
    }
  ],
  "provenance": [
    "Source-based command explanation, not a device conformance or power-failure test.",
    "The sg3_utils source is commit-pinned; its manpage header is not treated as the current package version.",
    "Seagate Rev. L is used for the Block Limits VPD field, not as evidence that every SCSI device implements the optional command."
  ],
  "updated": "2026-11-08"
}
