Skip to content

Commit

Permalink
libtcmu: add support for write protect
Browse files Browse the repository at this point in the history
Signed-off-by: liulanzheng <lanzheng.liulz@alibaba-inc.com>
  • Loading branch information
liulanzheng authored and lxbsz committed Feb 21, 2022
1 parent 8b03c79 commit 364ae61
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
12 changes: 11 additions & 1 deletion libtcmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,16 @@ bool tcmu_dev_get_unmap_enabled(struct tcmu_device *dev)
return dev->unmap_enabled;
}

void tcmu_dev_set_write_protect_enabled(struct tcmu_device *dev, bool enabled)
{
dev->write_protect_enabled = enabled;
}

bool tcmu_dev_get_write_protect_enabled(struct tcmu_device *dev)
{
return dev->write_protect_enabled;
}

int tcmu_dev_get_fd(struct tcmu_device *dev)
{
return dev->fd;
Expand Down Expand Up @@ -1060,7 +1070,7 @@ tcmu_dev_get_memory_info(struct tcmu_device *dev, void **base,
/* get length of map from file */
ssize_t size;
char *size_name;

if (asprintf(&size_name, sizefmt, dev->dev_name) == -1) {
tcmu_err("cannot construct device map size filename\n");
goto err_free;
Expand Down
2 changes: 2 additions & 0 deletions libtcmu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ void tcmu_dev_set_solid_state_media(struct tcmu_device *dev, bool solid_state);
bool tcmu_dev_get_solid_state_media(struct tcmu_device *dev);
void tcmu_dev_set_unmap_enabled(struct tcmu_device *dev, bool enabled);
bool tcmu_dev_get_unmap_enabled(struct tcmu_device *dev);
void tcmu_dev_set_write_protect_enabled(struct tcmu_device *dev, bool enabled);
bool tcmu_dev_get_write_protect_enabled(struct tcmu_device *dev);
struct tcmulib_handler *tcmu_dev_get_handler(struct tcmu_device *dev);
void tcmu_dev_flush_ring(struct tcmu_device *dev);
bool tcmu_dev_oooc_supported(struct tcmu_device* dev);
Expand Down
1 change: 1 addition & 0 deletions libtcmu_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct tcmu_device {
unsigned int write_cache_enabled:1;
unsigned int solid_state_media:1;
unsigned int unmap_enabled:1;
unsigned int write_protect_enabled:1;

char dev_name[16]; /* e.g. "uio14" */
char tcm_hba_name[16]; /* e.g. "user_8" */
Expand Down
8 changes: 8 additions & 0 deletions scsi.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@ int tcmu_emulate_mode_sense(
orig_buf[0] = used_len - 1;
}

if (tcmu_dev_get_write_protect_enabled(dev)) {
if (sense_ten) {
orig_buf[3] |= 0x80;
} else {
orig_buf[2] |= 0x80;
}
}

tcmu_memcpy_into_iovec(iovec, iov_cnt, orig_buf, alloc_len);
free(orig_buf);
return TCMU_STS_OK;
Expand Down

0 comments on commit 364ae61

Please sign in to comment.