linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hannes Reinecke <hare@suse.de>
To: Nic Bellinger <nab@daterainc.com>
Cc: target-devel@vger.kernel.org, linux-scsi@vger.kernel.org,
	Christoph Hellwig <hch@lst.de>, Ewan Milne <emilne@redhat.com>,
	Hannes Reinecke <hare@suse.de>
Subject: [PATCH 8/8] target: display 'write_protect' attribute for demo-mode LUNs
Date: Thu, 18 Jun 2015 11:43:42 +0200	[thread overview]
Message-ID: <1434620622-65391-9-git-send-email-hare@suse.de> (raw)
In-Reply-To: <1434620622-65391-1-git-send-email-hare@suse.de>

When LUNs are mapped with a demo-mode initiator we're missing
the 'write_protect' attribute to set a LUN read-only.

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 drivers/target/target_core_device.c          | 27 ++++++++++++++++++
 drivers/target/target_core_fabric_configfs.c | 42 ++++++++++++++++++++++++++++
 drivers/target/target_core_internal.h        |  1 +
 3 files changed, 70 insertions(+)

diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c
index 88d6070..3e17070 100644
--- a/drivers/target/target_core_device.c
+++ b/drivers/target/target_core_device.c
@@ -271,6 +271,33 @@ void core_update_device_list_access(
 	mutex_unlock(&nacl->lun_entry_mutex);
 }
 
+void core_update_lun_access(
+	struct se_lun *lun,
+	u32 lun_access)
+{
+	struct se_dev_entry *deve;
+
+	if (lun_access == TRANSPORT_LUNFLAGS_READ_ONLY) {
+		lun->lun_access &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
+		lun->lun_access |= TRANSPORT_LUNFLAGS_READ_ONLY;
+	} else {
+		lun->lun_access &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
+		lun->lun_access |= TRANSPORT_LUNFLAGS_READ_WRITE;
+	}
+
+	spin_lock_bh(&lun->lun_deve_lock);
+	list_for_each_entry(deve, &lun->lun_deve_list, lun_link) {
+		if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
+			deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
+			deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
+		} else {
+			deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
+			deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
+		}
+	}
+	spin_unlock_bh(&lun->lun_deve_lock);
+}
+
 /*
  * Called with rcu_read_lock or nacl->device_list_lock held.
  */
diff --git a/drivers/target/target_core_fabric_configfs.c b/drivers/target/target_core_fabric_configfs.c
index 6cfee59..299b4b5 100644
--- a/drivers/target/target_core_fabric_configfs.c
+++ b/drivers/target/target_core_fabric_configfs.c
@@ -726,12 +726,54 @@ static ssize_t target_fabric_port_store_attr_alua_tg_pt_write_md(
 
 TCM_PORT_ATTR(alua_tg_pt_write_md, S_IRUGO | S_IWUSR);
 
+static ssize_t target_fabric_port_show_attr_write_protect(
+	struct se_lun *lun,
+	char *page)
+{
+	ssize_t len = 0;
+
+	if (!lun || !lun->lun_se_dev)
+		return -ENODEV;
+
+	len = sprintf(page, "%d\n",
+		      (lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) ? 1 : 0);
+
+	return len;
+}
+
+static ssize_t target_fabric_port_store_attr_write_protect(
+	struct se_lun *lun,
+	const char *page,
+	size_t count)
+{
+	unsigned long op;
+	int ret;
+
+	if (!lun || !lun->lun_se_dev)
+		return -ENODEV;
+
+	ret = kstrtoul(page, 0, &op);
+	if (ret)
+		return ret;
+
+	if ((op != 1) && (op != 0))
+		return -EINVAL;
+
+	core_update_lun_access(lun, (op) ?
+			       TRANSPORT_LUNFLAGS_READ_ONLY :
+			       TRANSPORT_LUNFLAGS_READ_WRITE);
+
+	return count;
+
+}
+TCM_PORT_ATTR(write_protect, S_IRUGO | S_IWUSR);
 
 static struct configfs_attribute *target_fabric_port_attrs[] = {
 	&target_fabric_port_alua_tg_pt_gp.attr,
 	&target_fabric_port_alua_tg_pt_offline.attr,
 	&target_fabric_port_alua_tg_pt_status.attr,
 	&target_fabric_port_alua_tg_pt_write_md.attr,
+	&target_fabric_port_write_protect.attr,
 	NULL,
 };
 
diff --git a/drivers/target/target_core_internal.h b/drivers/target/target_core_internal.h
index 5111789..69a57a4 100644
--- a/drivers/target/target_core_internal.h
+++ b/drivers/target/target_core_internal.h
@@ -36,6 +36,7 @@ void	core_clear_lun_from_tpg(struct se_lun *, struct se_portal_group *);
 int	core_dev_add_lun(struct se_portal_group *, struct se_device *,
 		struct se_lun *lun);
 void	core_dev_del_lun(struct se_portal_group *, struct se_lun *);
+void	core_update_lun_access(struct se_lun *, u32);
 struct se_lun_acl *core_dev_init_initiator_node_lun_acl(struct se_portal_group *,
 		struct se_node_acl *, u64, int *);
 int	core_dev_add_initiator_node_lun_acl(struct se_portal_group *,
-- 
1.8.5.2


  parent reply	other threads:[~2015-06-18  9:43 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-18  9:43 [PATCH 0/8] tcm_loop updates Hannes Reinecke
2015-06-18  9:43 ` [PATCH 1/8] tcm_loop: Hook into SAS transport class Hannes Reinecke
2015-06-18  9:43 ` [PATCH 2/8] tcm_loop: Add SAS transport topology Hannes Reinecke
2015-06-18  9:43 ` [PATCH 3/8] tcm_loop: Remove SAS vestigies Hannes Reinecke
2015-06-18  9:43 ` [PATCH 4/8] tcm_loop: Send I_T_NEXUS_LOSS_OCCURRED UA Hannes Reinecke
2015-06-18  9:43 ` [PATCH 5/8] tcm_loop: Rescan SCSI target on transport online Hannes Reinecke
2015-06-18  9:43 ` [PATCH 6/8] target: Issue Power-On/Reset UA upon LUN instantiation Hannes Reinecke
2015-06-18  9:43 ` [PATCH 7/8] target_core_alua: disallow READ_CAPACITY when in standby Hannes Reinecke
2015-06-18 11:40   ` Chris Boot
2015-06-18 14:32     ` Hannes Reinecke
2015-06-19  6:49   ` Christoph Hellwig
2015-06-19  7:07     ` Hannes Reinecke
2015-06-18  9:43 ` Hannes Reinecke [this message]
2015-06-19  6:51   ` [PATCH 8/8] target: display 'write_protect' attribute for demo-mode LUNs Christoph Hellwig
2015-06-19  7:05     ` Hannes Reinecke
2015-06-19  6:48 ` [PATCH 0/8] tcm_loop updates Christoph Hellwig
2015-06-19  7:13   ` Hannes Reinecke
2015-06-23  8:29     ` Nicholas A. Bellinger
2015-06-23  9:05       ` Hannes Reinecke
2015-07-07  0:25         ` Nicholas A. Bellinger
2015-07-07  5:50           ` Hannes Reinecke
2015-07-07  6:26             ` Nicholas A. Bellinger
2015-07-07  6:29               ` Hannes Reinecke
2015-07-07  6:42                 ` Nicholas A. Bellinger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1434620622-65391-9-git-send-email-hare@suse.de \
    --to=hare@suse.de \
    --cc=emilne@redhat.com \
    --cc=hch@lst.de \
    --cc=linux-scsi@vger.kernel.org \
    --cc=nab@daterainc.com \
    --cc=target-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).