All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi:Prevent deletion of SCSI block device in use
@ 2016-09-19  9:11 Gurunath, Vasundhara (STSD)
  2016-09-19 12:55 ` James Bottomley
  0 siblings, 1 reply; 8+ messages in thread
From: Gurunath, Vasundhara (STSD) @ 2016-09-19  9:11 UTC (permalink / raw)
  To: James Bottomley, martin.petersen
  Cc: linux-scsi, Arackal, Paulose Kuriakose (STSD), Kaur, Jasminder (STSD)


James Bottomley <jejb@linux.vnet.ibm.com> wrote: 

>> From: "Gurunath, Vasundhara" <vasundhara.gurunath@hpe.com>
>>
>> SCSI block device can be removed, using write to sysfs delete file as
>> below:
>> echo 1 > /sys/block/sdX/device/delete If the device is in use by 
>> applications, or part of system configuration such as boot device, 
>> removal can result in application disruptions or system down time.
> >
>> An additional write option ? is added to SCSI sysfs interface as 
>> below, in order to prevent accidental deletion of devices in use.
>> echo ? > /sys/block/sdX/device/delete
>>
>> In the absence of any usage, this option proceeds with device 
>> deletion.  If the device is open, deletion is prevented, and active 
>> Open and IO counts at the time of deletion is logged. Information 
>> logged during latest delete attempt can be obtained by issuing a read 
>> to the delete file as below:
>> cat  /sys/block/sdX/device/delete

>OK, so I'm not too keen on this because our entire system is (finally) designed to be hot plug, so echoing 1 to delete simulates a hotplug event, and they >can come in at any time.

>Can you elaborate on why this is necessary?  Right at the moment, only 
>root is allowed to write to this file and cause a deletion ... plus the file is pretty >hard to find, buried as it is in sysfs;  So I would have thought it was pretty safe from accidental misuse; why does it need additional protection?

Some of the requests we got for such checks were from use cases on large system configurations with several LUNs.
The new changes do not disturb existing interfaces. Writes to the "delete" sysfs file such as a "1", as advertised in some distributions like RedHat today, will continue to delete the LUN. However we thought an option to check usages during delete can complement existing interfaces. 
 
The new changes get activated only when one wants to receive alerts on any lingering usages, and writes a "?" to delete the LUN.
A delete script can write "?" to sysfs delete files in bulk, while most LUNs get removed in the first attempt, the usages can be investigated if any LUNs remain with active usage counts. 

Hopefully overhead of these changes is minimal, it is few additional checks on usage counts and the log.
New changes get active only in delete context and doesn't get into I/O paths.

-Vasundhara



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] scsi:Prevent deletion of SCSI block device in use
  2016-09-19  9:11 [PATCH] scsi:Prevent deletion of SCSI block device in use Gurunath, Vasundhara (STSD)
@ 2016-09-19 12:55 ` James Bottomley
  0 siblings, 0 replies; 8+ messages in thread
From: James Bottomley @ 2016-09-19 12:55 UTC (permalink / raw)
  To: Gurunath, Vasundhara (STSD), martin.petersen
  Cc: linux-scsi, Arackal, Paulose Kuriakose (STSD), Kaur, Jasminder (STSD)

On Mon, 2016-09-19 at 09:11 +0000, Gurunath, Vasundhara (STSD) wrote:
> James Bottomley <jejb@linux.vnet.ibm.com> wrote: 
> 
> > > From: "Gurunath, Vasundhara" <vasundhara.gurunath@hpe.com>
> > > 
> > > SCSI block device can be removed, using write to sysfs delete
> > > file as
> > > below:
> > > echo 1 > /sys/block/sdX/device/delete If the device is in use by 
> > > applications, or part of system configuration such as boot
> > > device, 
> > > removal can result in application disruptions or system down
> > > time.
> > > 
> > > An additional write option ? is added to SCSI sysfs interface as 
> > > below, in order to prevent accidental deletion of devices in use.
> > > echo ? > /sys/block/sdX/device/delete
> > > 
> > > In the absence of any usage, this option proceeds with device 
> > > deletion.  If the device is open, deletion is prevented, and
> > > active 
> > > Open and IO counts at the time of deletion is logged. Information
> > > logged during latest delete attempt can be obtained by issuing a
> > > read 
> > > to the delete file as below:
> > > cat  /sys/block/sdX/device/delete
> 
> > OK, so I'm not too keen on this because our entire system is
> > (finally) designed to be hot plug, so echoing 1 to delete simulates
> > a hotplug event, and they >can come in at any time.
> 
> > Can you elaborate on why this is necessary?  Right at the moment,
> > only 
> > root is allowed to write to this file and cause a deletion ... plus
> > the file is pretty >hard to find, buried as it is in sysfs;  So I
> > would have thought it was pretty safe from accidental misuse; why
> > does it need additional protection?
> 
> Some of the requests we got for such checks were from use cases on
> large system configurations with several LUNs.

People ask for a lot of strange stuff, but unpeel this one further and
tell us why they're asking ... what's the use case they're running into
that makes them need something like this?

> The new changes do not disturb existing interfaces. Writes to the
> "delete" sysfs file such as a "1", as advertised in some
> distributions like RedHat today, will continue to delete the LUN.
> However we thought an option to check usages during delete can
> complement existing interfaces. 
>  
> The new changes get activated only when one wants to receive alerts
> on any lingering usages, and writes a "?" to delete the LUN.
> A delete script can write "?" to sysfs delete files in bulk, while
> most LUNs get removed in the first attempt, the usages can be
> investigated if any LUNs remain with active usage counts. 
> 
> Hopefully overhead of these changes is minimal, it is few additional
> checks on usage counts and the log.
> New changes get active only in delete context and doesn't get into 
> I/O paths.

But that's effectively "because we can".  We can do a lot of stuff that
adds what you call minimal overhead (although the cumulative addition
would be significant), so the rule is we do stuff which is necessary or
useful, which is why the question about use cases.

James



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] scsi:Prevent deletion of SCSI block device in use
  2016-09-13 16:38 Gurunath, Vasundhara
  2016-09-13 17:17 ` James Bottomley
@ 2016-09-13 18:25 ` Ewan D. Milne
  1 sibling, 0 replies; 8+ messages in thread
From: Ewan D. Milne @ 2016-09-13 18:25 UTC (permalink / raw)
  To: Gurunath, Vasundhara
  Cc: jejb, martin.petersen, linux-scsi, paulose.kuriakose.arackal,
	jasminder.kaur

On Tue, 2016-09-13 at 22:08 +0530, Gurunath, Vasundhara wrote:
> From: "Gurunath, Vasundhara" <vasundhara.gurunath@hpe.com>
> 
> SCSI block device can be removed, using write to sysfs
> delete file as below:
> echo 1 > /sys/block/sdX/device/delete
> If the device is in use by applications, or part of
> system configuration such as boot device, removal can
> result in application disruptions or system down time.
> 
> An additional write option ? is added to SCSI sysfs
> interface as below, in order to prevent accidental
> deletion of devices in use.
> echo ? > /sys/block/sdX/device/delete
> 
> In the absence of any usage, this option proceeds with
> device deletion.  If the device is open, deletion is
> prevented, and active Open and IO counts at the time of
> deletion is logged. Information logged during latest
> delete attempt can be obtained by issuing a read to the
> delete file as below:
> cat  /sys/block/sdX/device/delete
> 

This looks like debugging code added to find some culprit
who deleted a device they weren't supposed to, and make it
more difficult for them.

I don't think we'd want this in normal usage.

-Ewan




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] scsi:Prevent deletion of SCSI block device in use
  2016-09-13 16:38 Gurunath, Vasundhara
@ 2016-09-13 17:17 ` James Bottomley
  2016-09-13 18:25 ` Ewan D. Milne
  1 sibling, 0 replies; 8+ messages in thread
From: James Bottomley @ 2016-09-13 17:17 UTC (permalink / raw)
  To: Gurunath, Vasundhara, martin.petersen
  Cc: linux-scsi, paulose.kuriakose.arackal, jasminder.kaur

On Tue, 2016-09-13 at 22:08 +0530, Gurunath, Vasundhara wrote:
> From: "Gurunath, Vasundhara" <vasundhara.gurunath@hpe.com>
> 
> SCSI block device can be removed, using write to sysfs
> delete file as below:
> echo 1 > /sys/block/sdX/device/delete
> If the device is in use by applications, or part of
> system configuration such as boot device, removal can
> result in application disruptions or system down time.
> 
> An additional write option ? is added to SCSI sysfs
> interface as below, in order to prevent accidental
> deletion of devices in use.
> echo ? > /sys/block/sdX/device/delete
> 
> In the absence of any usage, this option proceeds with
> device deletion.  If the device is open, deletion is
> prevented, and active Open and IO counts at the time of
> deletion is logged. Information logged during latest
> delete attempt can be obtained by issuing a read to the
> delete file as below:
> cat  /sys/block/sdX/device/delete

OK, so I'm not too keen on this because our entire system is (finally)
designed to be hot plug, so echoing 1 to delete simulates a hotplug
event, and they can come in at any time.

Can you elaborate on why this is necessary?  Right at the moment, only
root is allowed to write to this file and cause a deletion ... plus the
file is pretty hard to find, buried as it is in sysfs; So I would have
thought it was pretty safe from accidental misuse; why does it need
additional protection?

James



^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] scsi:Prevent deletion of SCSI block device in use
@ 2016-09-13 16:38 Gurunath, Vasundhara
  2016-09-13 17:17 ` James Bottomley
  2016-09-13 18:25 ` Ewan D. Milne
  0 siblings, 2 replies; 8+ messages in thread
From: Gurunath, Vasundhara @ 2016-09-13 16:38 UTC (permalink / raw)
  To: jejb, martin.petersen
  Cc: linux-scsi, paulose.kuriakose.arackal, jasminder.kaur, Gurunath,
	Vasundhara

From: "Gurunath, Vasundhara" <vasundhara.gurunath@hpe.com>

SCSI block device can be removed, using write to sysfs
delete file as below:
echo 1 > /sys/block/sdX/device/delete
If the device is in use by applications, or part of
system configuration such as boot device, removal can
result in application disruptions or system down time.

An additional write option ? is added to SCSI sysfs
interface as below, in order to prevent accidental
deletion of devices in use.
echo ? > /sys/block/sdX/device/delete

In the absence of any usage, this option proceeds with
device deletion.  If the device is open, deletion is
prevented, and active Open and IO counts at the time of
deletion is logged. Information logged during latest
delete attempt can be obtained by issuing a read to the
delete file as below:
cat  /sys/block/sdX/device/delete

Signed-off-by: Vasundhara Gurunath <vasundhara.gurunath@hpe.com>
---
 drivers/scsi/scsi_sysfs.c  | 52 +++++++++++++++++++++++++++++++++++++++++++++-
 drivers/scsi/sd.c          |  4 ++++
 include/scsi/scsi_device.h |  2 ++
 3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index 0734927..b0cbfbb 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -12,6 +12,8 @@
 #include <linux/blkdev.h>
 #include <linux/device.h>
 #include <linux/pm_runtime.h>
+#include <linux/time.h>
+#include <linux/timer.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_device.h>
@@ -457,6 +459,8 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	kfree(sdev->vpd_pg83);
 	kfree(sdev->vpd_pg80);
 	kfree(sdev->inquiry);
+	if (sdev->delete_msg_buf != NULL)
+		kfree(sdev->delete_msg_buf);
 	kfree(sdev);
 
 	if (parent)
@@ -709,11 +713,57 @@ static ssize_t
 sdev_store_delete(struct device *dev, struct device_attribute *attr,
 		  const char *buf, size_t count)
 {
+	struct scsi_device *sdev = to_scsi_device(dev);
+	struct timeval tv;
+	struct tm tms;
+
+	if (buf[0] == '?')  {
+		if (sdev->usage_count) {
+			/*
+			* Buffer to hold I/O statistics on delete attempt.
+			*/
+			if (sdev->delete_msg_buf == NULL) {
+				sdev->delete_msg_buf =
+				kmalloc(128, GFP_KERNEL);
+				memset(sdev->delete_msg_buf, 0, 128);
+			}
+			do_gettimeofday(&tv);
+			time_to_tm(tv.tv_sec, 0, &tms);
+			sprintf(sdev->delete_msg_buf,
+				"Last delete attempt: %d:%d:%ld %02d:%02d\n"
+				"Open Count : %d\n"
+				"IO Active Count : %d\n"
+				"IO Done Count : %d\n",
+				tms.tm_mday, tms.tm_mon + 1,
+				tms.tm_year + 1900,
+				tms.tm_hour, tms.tm_min,
+				sdev->usage_count,
+				sdev->iorequest_cnt.counter,
+				sdev->iodone_cnt.counter);
+
+			return count;
+		}
+	}
+
+
 	if (device_remove_file_self(dev, attr))
 		scsi_remove_device(to_scsi_device(dev));
 	return count;
 };
-static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
+
+static ssize_t sdev_show_delete(struct device *dev,
+	struct device_attribute *attr, char *buf) {
+
+	struct scsi_device *sdev = to_scsi_device(dev);
+
+	if (sdev->delete_msg_buf != NULL)
+		return sprintf(buf, "%s", sdev->delete_msg_buf);
+	else
+		return 0;
+}
+
+static DEVICE_ATTR(delete, S_IRUGO | S_IWUSR,
+	sdev_show_delete, sdev_store_delete);
 
 static ssize_t
 store_state_field(struct device *dev, struct device_attribute *attr,
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index d3e852a..67d3406 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1235,6 +1235,7 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
 		if (scsi_block_when_processing_errors(sdev))
 			scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
 	}
+	sdev->usage_count = sdkp->openers.counter;
 
 	return 0;
 
@@ -1267,6 +1268,7 @@ static void sd_release(struct gendisk *disk, fmode_t mode)
 		if (scsi_block_when_processing_errors(sdev))
 			scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
 	}
+	sdev->usage_count = sdkp->openers.counter;
 
 	/*
 	 * XXX and what if there are packets in flight and this close()
@@ -3082,6 +3084,8 @@ static int sd_probe(struct device *dev)
 	atomic_set(&sdkp->openers, 0);
 	atomic_set(&sdkp->device->ioerr_cnt, 0);
 
+	sdp->usage_count = sdkp->openers.counter;
+
 	if (!sdp->request_queue->rq_timeout) {
 		if (sdp->type != TYPE_MOD)
 			blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 8a95631..ab79675 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -208,6 +208,8 @@ struct scsi_device {
 
 	unsigned char		access_state;
 	enum scsi_device_state sdev_state;
+	char                    *delete_msg_buf;
+	int                     usage_count;
 	unsigned long		sdev_data[0];
 } __attribute__((aligned(sizeof(unsigned long))));
 
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] scsi:Prevent deletion of SCSI block device in use
  2016-08-19 10:36 Vasundhara Gurunath
@ 2016-08-22 15:36   ` kbuild test robot
  0 siblings, 0 replies; 8+ messages in thread
From: kbuild test robot @ 2016-08-22 15:36 UTC (permalink / raw)
  To: Vasundhara Gurunath
  Cc: kbuild-all, jejb, martin.petersen, linux-scsi, linux-kernel,
	jasminder.kaur, paulose.kuriakose.arackal, Vasundhara Gurunath

[-- Attachment #1: Type: text/plain, Size: 2071 bytes --]

Hi Vasundhara,

[auto build test WARNING on scsi/for-next]
[also build test WARNING on v4.8-rc3 next-20160822]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on]
[Check https://git-scm.com/docs/git-format-patch for more information]

url:    https://github.com/0day-ci/linux/commits/Vasundhara-Gurunath/scsi-Prevent-deletion-of-SCSI-block-device-in-use/20160819-184126
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi.git for-next
config: x86_64-randconfig-s2-08191801 (attached as .config)
compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   drivers/scsi/scsi_sysfs.c: In function 'sdev_store_delete':
>> drivers/scsi/scsi_sysfs.c:744: warning: format '%d' expects type 'int', but argument 5 has type 'long int'

vim +744 drivers/scsi/scsi_sysfs.c

   728					sdev->delete_msg_buf =
   729					kmalloc(128, GFP_KERNEL);
   730					memset(sdev->delete_msg_buf, 0, 128);
   731				}
   732				do_gettimeofday(&tv);
   733				time_to_tm(tv.tv_sec, 0, &tms);
   734				sprintf(sdev->delete_msg_buf,
   735					"Last delete attempt: %d:%d:%d %02d:%02d\n"
   736					"Open Count : %d\n"
   737					"IO Active Count : %d\n"
   738					"IO Done Count : %d\n",
   739					tms.tm_mday, tms.tm_mon + 1,
   740					tms.tm_year + 1900,
   741					tms.tm_hour, tms.tm_min,
   742					sdev->usage_count,
   743					sdev->iorequest_cnt.counter,
 > 744					sdev->iodone_cnt.counter);
   745	
   746					return count;
   747				}
   748			}
   749	
   750		if (device_remove_file_self(dev, attr))
   751			scsi_remove_device(to_scsi_device(dev));
   752		return count;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 28829 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] scsi:Prevent deletion of SCSI block device in use
@ 2016-08-22 15:36   ` kbuild test robot
  0 siblings, 0 replies; 8+ messages in thread
From: kbuild test robot @ 2016-08-22 15:36 UTC (permalink / raw)
  Cc: kbuild-all, jejb, martin.petersen, linux-scsi, linux-kernel,
	jasminder.kaur, paulose.kuriakose.arackal, Vasundhara Gurunath

[-- Attachment #1: Type: text/plain, Size: 2071 bytes --]

Hi Vasundhara,

[auto build test WARNING on scsi/for-next]
[also build test WARNING on v4.8-rc3 next-20160822]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on]
[Check https://git-scm.com/docs/git-format-patch for more information]

url:    https://github.com/0day-ci/linux/commits/Vasundhara-Gurunath/scsi-Prevent-deletion-of-SCSI-block-device-in-use/20160819-184126
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi.git for-next
config: x86_64-randconfig-s2-08191801 (attached as .config)
compiler: gcc-4.4 (Debian 4.4.7-8) 4.4.7
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All warnings (new ones prefixed by >>):

   drivers/scsi/scsi_sysfs.c: In function 'sdev_store_delete':
>> drivers/scsi/scsi_sysfs.c:744: warning: format '%d' expects type 'int', but argument 5 has type 'long int'

vim +744 drivers/scsi/scsi_sysfs.c

   728					sdev->delete_msg_buf =
   729					kmalloc(128, GFP_KERNEL);
   730					memset(sdev->delete_msg_buf, 0, 128);
   731				}
   732				do_gettimeofday(&tv);
   733				time_to_tm(tv.tv_sec, 0, &tms);
   734				sprintf(sdev->delete_msg_buf,
   735					"Last delete attempt: %d:%d:%d %02d:%02d\n"
   736					"Open Count : %d\n"
   737					"IO Active Count : %d\n"
   738					"IO Done Count : %d\n",
   739					tms.tm_mday, tms.tm_mon + 1,
   740					tms.tm_year + 1900,
   741					tms.tm_hour, tms.tm_min,
   742					sdev->usage_count,
   743					sdev->iorequest_cnt.counter,
 > 744					sdev->iodone_cnt.counter);
   745	
   746					return count;
   747				}
   748			}
   749	
   750		if (device_remove_file_self(dev, attr))
   751			scsi_remove_device(to_scsi_device(dev));
   752		return count;

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/octet-stream, Size: 28829 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] scsi:Prevent deletion of SCSI block device in use
@ 2016-08-19 10:36 Vasundhara Gurunath
  2016-08-22 15:36   ` kbuild test robot
  0 siblings, 1 reply; 8+ messages in thread
From: Vasundhara Gurunath @ 2016-08-19 10:36 UTC (permalink / raw)
  To: jejb, martin.petersen
  Cc: linux-scsi, linux-kernel, jasminder.kaur,
	paulose.kuriakose.arackal, Vasundhara Gurunath

SCSI block device can be removed, using write to sysfs delete file as below:
echo 1 > /sys/block/sdX/device/delete
If the device is in use by applications, or part of system configuration
such as boot device, removal can result in application disruptions or
system down time.

An additional write option ? is added to SCSI sysfs interface as below,
in order to prevent accidental deletion of devices in use.
echo ? > /sys/block/sdX/device/delete

In the absence of any usage, this option proceeds with device deletion.
If the device is open, deletion is prevented, and active Open and IO
counts at the time of deletion is logged. Information logged during latest
delete attempt can be obtained by issuing a read to the delete file as below:
cat  /sys/block/sdX/device/delete

Signed-off-by: Vasundhara Gurunath <vasundhara.gurunath@hpe.com>
---
 drivers/scsi/scsi_sysfs.c  | 51 +++++++++++++++++++++++++++++++++++++++++++++-
 drivers/scsi/sd.c          |  5 ++++-
 include/scsi/scsi_device.h |  2 ++
 3 files changed, 56 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index 0734927..716b22a 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -12,6 +12,9 @@
 #include <linux/blkdev.h>
 #include <linux/device.h>
 #include <linux/pm_runtime.h>
+#include <linux/time.h>
+#include <linux/timer.h>
+
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_device.h>
@@ -457,6 +460,9 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
 	kfree(sdev->vpd_pg83);
 	kfree(sdev->vpd_pg80);
 	kfree(sdev->inquiry);
+	if (sdev->delete_msg_buf != NULL)
+		kfree(sdev->delete_msg_buf);
+
 	kfree(sdev);
 
 	if (parent)
@@ -709,11 +715,54 @@ static ssize_t
 sdev_store_delete(struct device *dev, struct device_attribute *attr,
 		  const char *buf, size_t count)
 {
+	struct scsi_device *sdev = to_scsi_device(dev);
+	struct timeval tv;
+	struct tm tms;
+
+	if (buf[0] == '?')  {
+		if (sdev->usage_count) {
+			/*
+			 * Buffer to hold I/O statistics on delete attempt.
+			 */
+			if (sdev->delete_msg_buf == NULL) {
+				sdev->delete_msg_buf =
+				kmalloc(128, GFP_KERNEL);
+				memset(sdev->delete_msg_buf, 0, 128);
+			}
+			do_gettimeofday(&tv);
+			time_to_tm(tv.tv_sec, 0, &tms);
+			sprintf(sdev->delete_msg_buf,
+				"Last delete attempt: %d:%d:%d %02d:%02d\n"
+				"Open Count : %d\n"
+				"IO Active Count : %d\n"
+				"IO Done Count : %d\n",
+				tms.tm_mday, tms.tm_mon + 1,
+				tms.tm_year + 1900,
+				tms.tm_hour, tms.tm_min,
+				sdev->usage_count,
+				sdev->iorequest_cnt.counter,
+				sdev->iodone_cnt.counter);
+
+				return count;
+			}
+		}
+
 	if (device_remove_file_self(dev, attr))
 		scsi_remove_device(to_scsi_device(dev));
 	return count;
 };
-static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
+
+static ssize_t sdev_show_delete(struct device *dev,
+		struct device_attribute *attr, char *buf) {
+
+	struct scsi_device *sdev = to_scsi_device(dev);
+	if (sdev->delete_msg_buf != NULL)
+		return sprintf(buf, "%s", sdev->delete_msg_buf);
+	else
+		return 0;
+}
+static DEVICE_ATTR(delete, S_IRUGO | S_IWUSR, sdev_show_delete,
+			 sdev_store_delete);
 
 static ssize_t
 store_state_field(struct device *dev, struct device_attribute *attr,
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index d3e852a..d5cf25c 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1235,6 +1235,7 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
 		if (scsi_block_when_processing_errors(sdev))
 			scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
 	}
+	sdev->usage_count = sdkp->openers.counter;
 
 	return 0;
 
@@ -1267,7 +1268,7 @@ static void sd_release(struct gendisk *disk, fmode_t mode)
 		if (scsi_block_when_processing_errors(sdev))
 			scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
 	}
-
+	sdev->usage_count = sdkp->openers.counter;
 	/*
 	 * XXX and what if there are packets in flight and this close()
 	 * XXX is followed by a "rmmod sd_mod"?
@@ -3082,6 +3083,8 @@ static int sd_probe(struct device *dev)
 	atomic_set(&sdkp->openers, 0);
 	atomic_set(&sdkp->device->ioerr_cnt, 0);
 
+	sdp->usage_count = sdkp->openers.counter;
+
 	if (!sdp->request_queue->rq_timeout) {
 		if (sdp->type != TYPE_MOD)
 			blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 8a95631..ab79675 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -208,6 +208,8 @@ struct scsi_device {
 
 	unsigned char		access_state;
 	enum scsi_device_state sdev_state;
+	char                    *delete_msg_buf;
+	int                     usage_count;
 	unsigned long		sdev_data[0];
 } __attribute__((aligned(sizeof(unsigned long))));
 
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-09-19 12:56 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-19  9:11 [PATCH] scsi:Prevent deletion of SCSI block device in use Gurunath, Vasundhara (STSD)
2016-09-19 12:55 ` James Bottomley
  -- strict thread matches above, loose matches on Subject: below --
2016-09-13 16:38 Gurunath, Vasundhara
2016-09-13 17:17 ` James Bottomley
2016-09-13 18:25 ` Ewan D. Milne
2016-08-19 10:36 Vasundhara Gurunath
2016-08-22 15:36 ` kbuild test robot
2016-08-22 15:36   ` kbuild test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.