]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - queue-4.9/scsi-zfcp-fix-to-prevent-port_remove-with-pure-auto-scan-luns-only-sdevs.patch
5.1-stable patches
[thirdparty/kernel/stable-queue.git] / queue-4.9 / scsi-zfcp-fix-to-prevent-port_remove-with-pure-auto-scan-luns-only-sdevs.patch
1 From ef4021fe5fd77ced0323cede27979d80a56211ca Mon Sep 17 00:00:00 2001
2 From: Steffen Maier <maier@linux.ibm.com>
3 Date: Thu, 23 May 2019 15:23:46 +0200
4 Subject: scsi: zfcp: fix to prevent port_remove with pure auto scan LUNs (only sdevs)
5
6 From: Steffen Maier <maier@linux.ibm.com>
7
8 commit ef4021fe5fd77ced0323cede27979d80a56211ca upstream.
9
10 When the user tries to remove a zfcp port via sysfs, we only rejected it if
11 there are zfcp unit children under the port. With purely automatically
12 scanned LUNs there are no zfcp units but only SCSI devices. In such cases,
13 the port_remove erroneously continued. We close the port and this
14 implicitly closes all LUNs under the port. The SCSI devices survive with
15 their private zfcp_scsi_dev still holding a reference to the "removed"
16 zfcp_port (still allocated but invisible in sysfs) [zfcp_get_port_by_wwpn
17 in zfcp_scsi_slave_alloc]. This is not a problem as long as the fc_rport
18 stays blocked. Once (auto) port scan brings back the removed port, we
19 unblock its fc_rport again by design. However, there is no mechanism that
20 would recover (open) the LUNs under the port (no "ersfs_3" without
21 zfcp_unit [zfcp_erp_strategy_followup_success]). Any pending or new I/O to
22 such LUN leads to repeated:
23
24 Done: NEEDS_RETRY Result: hostbyte=DID_IMM_RETRY driverbyte=DRIVER_OK
25
26 See also v4.10 commit 6f2ce1c6af37 ("scsi: zfcp: fix rport unblock race
27 with LUN recovery"). Even a manual LUN recovery
28 (echo 0 > /sys/bus/scsi/devices/H:C:T:L/zfcp_failed)
29 does not help, as the LUN links to the old "removed" port which remains
30 to lack ZFCP_STATUS_COMMON_RUNNING [zfcp_erp_required_act].
31 The only workaround is to first ensure that the fc_rport is blocked
32 (e.g. port_remove again in case it was re-discovered by (auto) port scan),
33 then delete the SCSI devices, and finally re-discover by (auto) port scan.
34 The port scan includes an fc_rport unblock, which in turn triggers
35 a new scan on the scsi target to freshly get new pure auto scan LUNs.
36
37 Fix this by rejecting port_remove also if there are SCSI devices
38 (even without any zfcp_unit) under this port. Re-use mechanics from v3.7
39 commit d99b601b6338 ("[SCSI] zfcp: restore refcount check on port_remove").
40 However, we have to give up zfcp_sysfs_port_units_mutex earlier in unit_add
41 to prevent a deadlock with scsi_host scan taking shost->scan_mutex first
42 and then zfcp_sysfs_port_units_mutex now in our zfcp_scsi_slave_alloc().
43
44 Signed-off-by: Steffen Maier <maier@linux.ibm.com>
45 Fixes: b62a8d9b45b9 ("[SCSI] zfcp: Use SCSI device data zfcp scsi dev instead of zfcp unit")
46 Fixes: f8210e34887e ("[SCSI] zfcp: Allow midlayer to scan for LUNs when running in NPIV mode")
47 Cc: <stable@vger.kernel.org> #2.6.37+
48 Reviewed-by: Benjamin Block <bblock@linux.ibm.com>
49 Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
50 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
51
52 ---
53 drivers/s390/scsi/zfcp_ext.h | 1
54 drivers/s390/scsi/zfcp_scsi.c | 9 ++++++
55 drivers/s390/scsi/zfcp_sysfs.c | 54 ++++++++++++++++++++++++++++++++++++-----
56 drivers/s390/scsi/zfcp_unit.c | 8 +++++-
57 4 files changed, 65 insertions(+), 7 deletions(-)
58
59 --- a/drivers/s390/scsi/zfcp_ext.h
60 +++ b/drivers/s390/scsi/zfcp_ext.h
61 @@ -161,6 +161,7 @@ extern const struct attribute_group *zfc
62 extern struct mutex zfcp_sysfs_port_units_mutex;
63 extern struct device_attribute *zfcp_sysfs_sdev_attrs[];
64 extern struct device_attribute *zfcp_sysfs_shost_attrs[];
65 +bool zfcp_sysfs_port_is_removing(const struct zfcp_port *const port);
66
67 /* zfcp_unit.c */
68 extern int zfcp_unit_add(struct zfcp_port *, u64);
69 --- a/drivers/s390/scsi/zfcp_scsi.c
70 +++ b/drivers/s390/scsi/zfcp_scsi.c
71 @@ -124,6 +124,15 @@ static int zfcp_scsi_slave_alloc(struct
72
73 zfcp_sdev->erp_action.port = port;
74
75 + mutex_lock(&zfcp_sysfs_port_units_mutex);
76 + if (zfcp_sysfs_port_is_removing(port)) {
77 + /* port is already gone */
78 + mutex_unlock(&zfcp_sysfs_port_units_mutex);
79 + put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
80 + return -ENXIO;
81 + }
82 + mutex_unlock(&zfcp_sysfs_port_units_mutex);
83 +
84 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev));
85 if (unit)
86 put_device(&unit->dev);
87 --- a/drivers/s390/scsi/zfcp_sysfs.c
88 +++ b/drivers/s390/scsi/zfcp_sysfs.c
89 @@ -237,6 +237,53 @@ static ZFCP_DEV_ATTR(adapter, port_resca
90
91 DEFINE_MUTEX(zfcp_sysfs_port_units_mutex);
92
93 +static void zfcp_sysfs_port_set_removing(struct zfcp_port *const port)
94 +{
95 + lockdep_assert_held(&zfcp_sysfs_port_units_mutex);
96 + atomic_set(&port->units, -1);
97 +}
98 +
99 +bool zfcp_sysfs_port_is_removing(const struct zfcp_port *const port)
100 +{
101 + lockdep_assert_held(&zfcp_sysfs_port_units_mutex);
102 + return atomic_read(&port->units) == -1;
103 +}
104 +
105 +static bool zfcp_sysfs_port_in_use(struct zfcp_port *const port)
106 +{
107 + struct zfcp_adapter *const adapter = port->adapter;
108 + unsigned long flags;
109 + struct scsi_device *sdev;
110 + bool in_use = true;
111 +
112 + mutex_lock(&zfcp_sysfs_port_units_mutex);
113 + if (atomic_read(&port->units) > 0)
114 + goto unlock_port_units_mutex; /* zfcp_unit(s) under port */
115 +
116 + spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
117 + __shost_for_each_device(sdev, adapter->scsi_host) {
118 + const struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
119 +
120 + if (sdev->sdev_state == SDEV_DEL ||
121 + sdev->sdev_state == SDEV_CANCEL)
122 + continue;
123 + if (zsdev->port != port)
124 + continue;
125 + /* alive scsi_device under port of interest */
126 + goto unlock_host_lock;
127 + }
128 +
129 + /* port is about to be removed, so no more unit_add or slave_alloc */
130 + zfcp_sysfs_port_set_removing(port);
131 + in_use = false;
132 +
133 +unlock_host_lock:
134 + spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
135 +unlock_port_units_mutex:
136 + mutex_unlock(&zfcp_sysfs_port_units_mutex);
137 + return in_use;
138 +}
139 +
140 static ssize_t zfcp_sysfs_port_remove_store(struct device *dev,
141 struct device_attribute *attr,
142 const char *buf, size_t count)
143 @@ -259,16 +306,11 @@ static ssize_t zfcp_sysfs_port_remove_st
144 else
145 retval = 0;
146
147 - mutex_lock(&zfcp_sysfs_port_units_mutex);
148 - if (atomic_read(&port->units) > 0) {
149 + if (zfcp_sysfs_port_in_use(port)) {
150 retval = -EBUSY;
151 - mutex_unlock(&zfcp_sysfs_port_units_mutex);
152 put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
153 goto out;
154 }
155 - /* port is about to be removed, so no more unit_add */
156 - atomic_set(&port->units, -1);
157 - mutex_unlock(&zfcp_sysfs_port_units_mutex);
158
159 write_lock_irq(&adapter->port_list_lock);
160 list_del(&port->list);
161 --- a/drivers/s390/scsi/zfcp_unit.c
162 +++ b/drivers/s390/scsi/zfcp_unit.c
163 @@ -123,7 +123,7 @@ int zfcp_unit_add(struct zfcp_port *port
164 int retval = 0;
165
166 mutex_lock(&zfcp_sysfs_port_units_mutex);
167 - if (atomic_read(&port->units) == -1) {
168 + if (zfcp_sysfs_port_is_removing(port)) {
169 /* port is already gone */
170 retval = -ENODEV;
171 goto out;
172 @@ -167,8 +167,14 @@ int zfcp_unit_add(struct zfcp_port *port
173 write_lock_irq(&port->unit_list_lock);
174 list_add_tail(&unit->list, &port->unit_list);
175 write_unlock_irq(&port->unit_list_lock);
176 + /*
177 + * lock order: shost->scan_mutex before zfcp_sysfs_port_units_mutex
178 + * due to zfcp_unit_scsi_scan() => zfcp_scsi_slave_alloc()
179 + */
180 + mutex_unlock(&zfcp_sysfs_port_units_mutex);
181
182 zfcp_unit_scsi_scan(unit);
183 + return retval;
184
185 out:
186 mutex_unlock(&zfcp_sysfs_port_units_mutex);