[PATCH 1/1] Readonly disk LUNs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



List, Tomo,

Please find attached a new, reworked patch for making a disk LUN read-only.
Readonly LUNs report this in the modesense reply and also refuse any
write attempts with the proper sense codes.

It expands the tgtadm manpage and adds a "PARAMETERS" section. This
currently only describes the readonly parameter but will be added
later to add all other parameters as well.

regards
ronnie sahlberg

Attachment: 0001-Add-a-new-device-parameter-readonly-0-1-that-whe.patch.gz
Description: GNU Zip compressed data

From 1373f20f2d83c2a207dab8fc8982d948317b56a3 Mon Sep 17 00:00:00 2001
From: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
Date: Tue, 9 Mar 2010 15:27:08 +1100
Subject: [PATCH] Add a new device parameter "readonly={0|1}" that when set to !0 makes the device read-only.

For DISK devices, this is communicated back to the initiator by setting the WP bit in teh device specific byte in the mode sense header.
It will also fail any WRITE6/10/12/16 calls with a DATA_PROTECT/ASC_WRITE_PROTECT

The manpage for tgtadm has been expanded with a PARAMETERS section and the readonly parameter is added as the first parameter for this section.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
---
 doc/htmlpages/tgtadm.8.html |   33 +++++++++++++++++++++++++++------
 doc/manpages/tgtadm.8       |   35 +++++++++++++++++++++++++++++++----
 doc/tgtadm.8.xml            |   43 +++++++++++++++++++++++++++++++++++++++++--
 usr/sbc.c                   |    9 +++++++++
 usr/spc.c                   |   18 +++++++++++++++++-
 usr/tgtd.h                  |    1 +
 6 files changed, 126 insertions(+), 13 deletions(-)

diff --git a/doc/htmlpages/tgtadm.8.html b/doc/htmlpages/tgtadm.8.html
index 9b9c73c..0f266be 100644
--- a/doc/htmlpages/tgtadm.8.html
+++ b/doc/htmlpages/tgtadm.8.html
@@ -1,7 +1,7 @@
-<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>tgtadm</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en"><a name="tgtadm.8"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>tgtadm &#8212; Linux SCSI Target Administration Utility</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">tgtadm [--control-port NNNN] --lld [driver] --op [operation] --mode [mode] [OPTION]...</code> </p></div></div><div class="refsect1" lang="en"><a name="id2475961"></a><h2>DESCRIPTION</h2><p>
+<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>tgtadm</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en"><a name="tgtadm.8"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>tgtadm &#8212; Linux SCSI Target Administration Utility</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">tgtadm [--control-port NNNN] --lld [driver] --op [operation] --mode [mode] [OPTION]...</code> </p></div></div><div class="refsect1" lang="en"><a name="id2476005"></a><h2>DESCRIPTION</h2><p>
       tgtadm is used to monitor and modify everything about Linux SCSI target
       software: targets, volumes, etc.
-    </p></div><div class="refsect1" lang="en"><a name="id2475972"></a><h2>OPTIONS</h2><div class="variablelist"><dl><dt><span class="term"><code class="option">--control-port NNNN</code></span></dt><dd><p>
+    </p></div><div class="refsect1" lang="en"><a name="id2476016"></a><h2>OPTIONS</h2><div class="variablelist"><dl><dt><span class="term"><code class="option">--control-port NNNN</code></span></dt><dd><p>
 	    It is possible to run multiple concurrent instances of tgtd
 	    on a host. This argument is used to control which instance
 	    the tgtadm command will operate on.
@@ -26,15 +26,36 @@
 	    to allow all initiators to access to a target.
           </p></dd><dt><span class="term"><code class="option">--lld [driver] --op unbind --mode=target --tid=[id] --initiator-address=[address]</code></span></dt><dd><p>
 	    delete the address from the access lists of the target with [id].
-          </p></dd><dt><span class="term"><code class="option">--lld [driver] --op update --mode=target --tid=[id] --name=[parameter] --value=[value]</code></span></dt><dd><p>
-	    change the value of [parameter] of the target with [id] to [value].
+          </p></dd><dt><span class="term"><code class="option">--lld [driver] --op update --mode={target|logicalunit} --tid=[id] --lun[lun] --name=[parameter] --value=[value]</code></span></dt><dd><p>
+	    change the value of [parameter] of the target or logical unit with [id] and [lun] to [value].
+          </p><p>
+	    See the PARAMETERS section for a list of available parameters.
+          </p></dd><dt><span class="term"><code class="option">--lld [driver] --op update --mode={target|logicalunit} --tid=[id] --lun[lun] --params=[parameters]</code></span></dt><dd><p>
+	    takes a comma-separated list of parameters and values and is
+	    used to set multiple parameters at the same time.
+          </p><p>
+	    See the PARAMETERS section for a list of available parameters.
           </p></dd><dt><span class="term"><code class="option">--version</code></span></dt><dd><p>
 	    display version and exit.
           </p></dd><dt><span class="term"><code class="option">--help</code></span></dt><dd><p>
 	    display a list of available options and exits.
-          </p></dd></dl></div></div><div class="refsect1" lang="en"><a name="id2476171"></a><h2>SEE ALSO</h2><p>
+          </p></dd></dl></div></div><div class="refsect1" lang="en"><a name="id2476242"></a><h2>PARAMETERS</h2><p>
+      This is a list of the available parameters a LUN can be configured with.
+      Some parameters apply to all types of LUNs while other parameters only
+      apply to specific device types.
+    </p><div class="refsect2" lang="en"><a name="id2476253"></a><h3>readonly={0|1}</h3><p>
+	This sets the read-only flag of a LUN. A read-only LUN will
+	refuse any attempts to write data to it.
+      </p><p>
+	This parameter only applies to DISK devices.
+      </p><p>
+	Example:
+	</p><pre class="screen">
+	tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 1 --params readonly=1
+	</pre><p>
+      </p></div></div><div class="refsect1" lang="en"><a name="id2476280"></a><h2>SEE ALSO</h2><p>
       tgtd(8), tgt-admin(8), tgtimg(8), tgt-setup-lun(8).
       <a class="ulink" href="http://stgt.sourceforge.net/"; target="_top">http://stgt.sourceforge.net/</a>
-    </p></div><div class="refsect1" lang="en"><a name="id2476186"></a><h2>REPORTING BUGS</h2><p>
+    </p></div><div class="refsect1" lang="en"><a name="id2476295"></a><h2>REPORTING BUGS</h2><p>
       Report bugs to &lt;stgt@xxxxxxxxxxxxxxx&gt;
     </p></div></div></body></html>
diff --git a/doc/manpages/tgtadm.8 b/doc/manpages/tgtadm.8
index 92fef89..12d6247 100644
--- a/doc/manpages/tgtadm.8
+++ b/doc/manpages/tgtadm.8
@@ -1,11 +1,11 @@
 .\"     Title: tgtadm
 .\"    Author: 
 .\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
-.\"      Date: 03/04/2010
+.\"      Date: 03/09/2010
 .\"    Manual: 
 .\"    Source: 
 .\"
-.TH "TGTADM" "8" "03/04/2010" "" ""
+.TH "TGTADM" "8" "03/09/2010" "" ""
 .\" disable hyphenation
 .nh
 .\" disable justification (adjust text to left margin only)
@@ -65,9 +65,18 @@ add the address to the access lists of the target with [id]\. Initiators with th
 delete the address from the access lists of the target with [id]\.
 .RE
 .PP
-\fB\-\-lld [driver] \-\-op update \-\-mode=target \-\-tid=[id] \-\-name=[parameter] \-\-value=[value]\fR
+\fB\-\-lld [driver] \-\-op update \-\-mode={target|logicalunit} \-\-tid=[id] \-\-lun[lun] \-\-name=[parameter] \-\-value=[value]\fR
 .RS 4
-change the value of [parameter] of the target with [id] to [value]\.
+change the value of [parameter] of the target or logical unit with [id] and [lun] to [value]\.
+.sp
+See the PARAMETERS section for a list of available parameters\.
+.RE
+.PP
+\fB\-\-lld [driver] \-\-op update \-\-mode={target|logicalunit} \-\-tid=[id] \-\-lun[lun] \-\-params=[parameters]\fR
+.RS 4
+takes a comma\-separated list of parameters and values and is used to set multiple parameters at the same time\.
+.sp
+See the PARAMETERS section for a list of available parameters\.
 .RE
 .PP
 \fB\-\-version\fR
@@ -79,6 +88,24 @@ display version and exit\.
 .RS 4
 display a list of available options and exits\.
 .RE
+.SH "PARAMETERS"
+.PP
+This is a list of the available parameters a LUN can be configured with\. Some parameters apply to all types of LUNs while other parameters only apply to specific device types\.
+.SS "readonly={0|1}"
+.PP
+This sets the read\-only flag of a LUN\. A read\-only LUN will refuse any attempts to write data to it\.
+.PP
+This parameter only applies to DISK devices\.
+.PP
+Example:
+.sp
+.RS 4
+.nf
+	tgtadm \-\-lld iscsi \-\-mode logicalunit \-\-op update \-\-tid 1 \-\-lun 1 \-\-params readonly=1
+	
+.fi
+.RE
+.sp
 .SH "SEE ALSO"
 .PP
 tgtd(8), tgt\-admin(8), tgtimg(8), tgt\-setup\-lun(8)\.
diff --git a/doc/tgtadm.8.xml b/doc/tgtadm.8.xml
index 22d96f1..36f285a 100644
--- a/doc/tgtadm.8.xml
+++ b/doc/tgtadm.8.xml
@@ -110,10 +110,25 @@
         </listitem>
       </varlistentry>
 
-      <varlistentry><term><option>--lld [driver] --op update --mode=target --tid=[id] --name=[parameter] --value=[value]</option></term>
+      <varlistentry><term><option>--lld [driver] --op update --mode={target|logicalunit} --tid=[id] --lun[lun] --name=[parameter] --value=[value]</option></term>
         <listitem>
           <para>
-	    change the value of [parameter] of the target with [id] to [value].
+	    change the value of [parameter] of the target or logical unit with [id] and [lun] to [value].
+          </para>
+          <para>
+	    See the PARAMETERS section for a list of available parameters.
+          </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry><term><option>--lld [driver] --op update --mode={target|logicalunit} --tid=[id] --lun[lun] --params=[parameters]</option></term>
+        <listitem>
+          <para>
+	    takes a comma-separated list of parameters and values and is
+	    used to set multiple parameters at the same time.
+          </para>
+          <para>
+	    See the PARAMETERS section for a list of available parameters.
           </para>
         </listitem>
       </varlistentry>
@@ -137,6 +152,30 @@
     </variablelist>
   </refsect1>
 
+  <refsect1><title>PARAMETERS</title>
+    <para>
+      This is a list of the available parameters a LUN can be configured with.
+      Some parameters apply to all types of LUNs while other parameters only
+      apply to specific device types.
+    </para>
+    <refsect2><title>readonly={0|1}</title>
+      <para>
+	This sets the read-only flag of a LUN. A read-only LUN will
+	refuse any attempts to write data to it.
+      </para>
+      <para>
+	This parameter only applies to DISK devices.
+      </para>
+      <para>
+	Example:
+	<screen format="linespecific">
+	tgtadm --lld iscsi --mode logicalunit --op update --tid 1 --lun 1 --params readonly=1
+	</screen>
+      </para>
+    </refsect2>
+  </refsect1>
+
+
   <refsect1><title>SEE ALSO</title>
     <para>
       tgtd(8), tgt-admin(8), tgtimg(8), tgt-setup-lun(8).
diff --git a/usr/sbc.c b/usr/sbc.c
index a048d53..f7f01df 100644
--- a/usr/sbc.c
+++ b/usr/sbc.c
@@ -77,11 +77,19 @@ static int sbc_rw(int host_no, struct scsi_cmd *cmd)
 	int ret;
 	unsigned char key = ILLEGAL_REQUEST;
 	uint16_t asc = ASC_LUN_NOT_SUPPORTED;
+	struct scsi_lu *lu = cmd->dev;
 
 	ret = device_reserved(cmd);
 	if (ret)
 		return SAM_STAT_RESERVATION_CONFLICT;
 
+	if (lu->attrs.readonly) {
+		if (scsi_data_dir_opcode(cmd->scb[0]) == DATA_WRITE) {
+			key = DATA_PROTECT;
+			asc = ASC_WRITE_PROTECT;
+			goto sense;
+		}
+	}
 	cmd->scsi_cmd_done = target_cmd_io_done;
 
 	cmd->offset = (scsi_rw_offset(cmd->scb) << BLK_SHIFT);
@@ -94,6 +102,7 @@ static int sbc_rw(int host_no, struct scsi_cmd *cmd)
 		return SAM_STAT_GOOD;
 	}
 
+sense:
 	cmd->offset = 0;
 	scsi_set_in_resid_by_actual(cmd, 0);
 	scsi_set_out_resid_by_actual(cmd, 0);
diff --git a/usr/spc.c b/usr/spc.c
index 14a3ee1..25ff849 100644
--- a/usr/spc.c
+++ b/usr/spc.c
@@ -567,8 +567,18 @@ int spc_mode_sense(int host_no, struct scsi_cmd *cmd)
 
 	if (mode6) {
 		data[0] = len - 1;
+
+		/* device specific parameters */
+		if (cmd->dev->attrs.readonly)
+			if (cmd->dev->attrs.device_type == TYPE_DISK)
+				data[2] |= 0x80;
 		data[3] = dbd ? 0 : BLOCK_DESCRIPTOR_LEN;
 	} else {
+		/* device specific parameters */
+		if (cmd->dev->attrs.readonly)
+			if (cmd->dev->attrs.device_type == TYPE_DISK)
+				data[3] |= 0x80;
+
 		*(uint16_t *)(data) = __cpu_to_be16(len - 2);
 		data[7] = dbd ? 0 : BLOCK_DESCRIPTOR_LEN;
 	}
@@ -1578,7 +1588,7 @@ enum {
 	Opt_scsi_id, Opt_scsi_sn,
 	Opt_vendor_id, Opt_product_id,
 	Opt_product_rev, Opt_sense_format,
-	Opt_removable, Opt_online,
+	Opt_removable, Opt_readonly, Opt_online,
 	Opt_mode_page,
 	Opt_path,
 	Opt_err,
@@ -1592,6 +1602,7 @@ static match_table_t tokens = {
 	{Opt_product_rev, "product_rev=%s"},
 	{Opt_sense_format, "sense_format=%s"},
 	{Opt_removable, "removable=%s"},
+	{Opt_readonly, "readonly=%s"},
 	{Opt_online, "online=%s"},
 	{Opt_mode_page, "mode_page=%s"},
 	{Opt_path, "path=%s"},
@@ -1661,6 +1672,10 @@ int lu_config(struct scsi_lu *lu, char *params, match_fn_t *fn)
 			match_strncpy(buf, &args[0], sizeof(buf));
 			attrs->removable = atoi(buf);
 			break;
+		case Opt_readonly:
+			match_strncpy(buf, &args[0], sizeof(buf));
+			attrs->readonly = atoi(buf);
+			break;
 		case Opt_online:
 			match_strncpy(buf, &args[0], sizeof(buf));
 			if (atoi(buf))
@@ -1719,6 +1734,7 @@ int spc_lu_init(struct scsi_lu *lu)
 	lu_vpd[pg]->vpd_update(lu, lu->attrs.scsi_id);
 
 	lu->attrs.removable = 0;
+	lu->attrs.readonly = 0;
 	lu->attrs.sense_format = 0;
 	lu->dev_type_template.lu_offline(lu);
 
diff --git a/usr/tgtd.h b/usr/tgtd.h
index 3323a9b..1e87a64 100644
--- a/usr/tgtd.h
+++ b/usr/tgtd.h
@@ -63,6 +63,7 @@ struct lu_phy_attr {
 	char device_type;	/* Peripheral device type */
 	char qualifier;		/* Peripheral Qualifier */
 	char removable;		/* Removable media */
+	char readonly;		/* Read-Only media */
 	char online;		/* Logical Unit online */
 	char sense_format;	/* Descrptor format sense data supported */
 
-- 
1.5.4.3


[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux