Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: zfcp: fix to prevent port_remove with pure auto scan LUNs (only sdevs)

When the user tries to remove a zfcp port via sysfs, we only rejected it if
there are zfcp unit children under the port. With purely automatically
scanned LUNs there are no zfcp units but only SCSI devices. In such cases,
the port_remove erroneously continued. We close the port and this
implicitly closes all LUNs under the port. The SCSI devices survive with
their private zfcp_scsi_dev still holding a reference to the "removed"
zfcp_port (still allocated but invisible in sysfs) [zfcp_get_port_by_wwpn
in zfcp_scsi_slave_alloc]. This is not a problem as long as the fc_rport
stays blocked. Once (auto) port scan brings back the removed port, we
unblock its fc_rport again by design. However, there is no mechanism that
would recover (open) the LUNs under the port (no "ersfs_3" without
zfcp_unit [zfcp_erp_strategy_followup_success]). Any pending or new I/O to
such LUN leads to repeated:

Done: NEEDS_RETRY Result: hostbyte=DID_IMM_RETRY driverbyte=DRIVER_OK

See also v4.10 commit 6f2ce1c6af37 ("scsi: zfcp: fix rport unblock race
with LUN recovery"). Even a manual LUN recovery
(echo 0 > /sys/bus/scsi/devices/H:C:T:L/zfcp_failed)
does not help, as the LUN links to the old "removed" port which remains
to lack ZFCP_STATUS_COMMON_RUNNING [zfcp_erp_required_act].
The only workaround is to first ensure that the fc_rport is blocked
(e.g. port_remove again in case it was re-discovered by (auto) port scan),
then delete the SCSI devices, and finally re-discover by (auto) port scan.
The port scan includes an fc_rport unblock, which in turn triggers
a new scan on the scsi target to freshly get new pure auto scan LUNs.

Fix this by rejecting port_remove also if there are SCSI devices
(even without any zfcp_unit) under this port. Re-use mechanics from v3.7
commit d99b601b6338 ("[SCSI] zfcp: restore refcount check on port_remove").
However, we have to give up zfcp_sysfs_port_units_mutex earlier in unit_add
to prevent a deadlock with scsi_host scan taking shost->scan_mutex first
and then zfcp_sysfs_port_units_mutex now in our zfcp_scsi_slave_alloc().

Signed-off-by: Steffen Maier <maier@linux.ibm.com>
Fixes: b62a8d9b45b9 ("[SCSI] zfcp: Use SCSI device data zfcp scsi dev instead of zfcp unit")
Fixes: f8210e34887e ("[SCSI] zfcp: Allow midlayer to scan for LUNs when running in NPIV mode")
Cc: <stable@vger.kernel.org> #2.6.37+
Reviewed-by: Benjamin Block <bblock@linux.ibm.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Steffen Maier and committed by
Martin K. Petersen
ef4021fe d27e5e07

+65 -7
+1
drivers/s390/scsi/zfcp_ext.h
··· 167 167 extern struct mutex zfcp_sysfs_port_units_mutex; 168 168 extern struct device_attribute *zfcp_sysfs_sdev_attrs[]; 169 169 extern struct device_attribute *zfcp_sysfs_shost_attrs[]; 170 + bool zfcp_sysfs_port_is_removing(const struct zfcp_port *const port); 170 171 171 172 /* zfcp_unit.c */ 172 173 extern int zfcp_unit_add(struct zfcp_port *, u64);
+9
drivers/s390/scsi/zfcp_scsi.c
··· 129 129 130 130 zfcp_sdev->erp_action.port = port; 131 131 132 + mutex_lock(&zfcp_sysfs_port_units_mutex); 133 + if (zfcp_sysfs_port_is_removing(port)) { 134 + /* port is already gone */ 135 + mutex_unlock(&zfcp_sysfs_port_units_mutex); 136 + put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */ 137 + return -ENXIO; 138 + } 139 + mutex_unlock(&zfcp_sysfs_port_units_mutex); 140 + 132 141 unit = zfcp_unit_find(port, zfcp_scsi_dev_lun(sdev)); 133 142 if (unit) 134 143 put_device(&unit->dev);
+48 -6
drivers/s390/scsi/zfcp_sysfs.c
··· 235 235 236 236 DEFINE_MUTEX(zfcp_sysfs_port_units_mutex); 237 237 238 + static void zfcp_sysfs_port_set_removing(struct zfcp_port *const port) 239 + { 240 + lockdep_assert_held(&zfcp_sysfs_port_units_mutex); 241 + atomic_set(&port->units, -1); 242 + } 243 + 244 + bool zfcp_sysfs_port_is_removing(const struct zfcp_port *const port) 245 + { 246 + lockdep_assert_held(&zfcp_sysfs_port_units_mutex); 247 + return atomic_read(&port->units) == -1; 248 + } 249 + 250 + static bool zfcp_sysfs_port_in_use(struct zfcp_port *const port) 251 + { 252 + struct zfcp_adapter *const adapter = port->adapter; 253 + unsigned long flags; 254 + struct scsi_device *sdev; 255 + bool in_use = true; 256 + 257 + mutex_lock(&zfcp_sysfs_port_units_mutex); 258 + if (atomic_read(&port->units) > 0) 259 + goto unlock_port_units_mutex; /* zfcp_unit(s) under port */ 260 + 261 + spin_lock_irqsave(adapter->scsi_host->host_lock, flags); 262 + __shost_for_each_device(sdev, adapter->scsi_host) { 263 + const struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev); 264 + 265 + if (sdev->sdev_state == SDEV_DEL || 266 + sdev->sdev_state == SDEV_CANCEL) 267 + continue; 268 + if (zsdev->port != port) 269 + continue; 270 + /* alive scsi_device under port of interest */ 271 + goto unlock_host_lock; 272 + } 273 + 274 + /* port is about to be removed, so no more unit_add or slave_alloc */ 275 + zfcp_sysfs_port_set_removing(port); 276 + in_use = false; 277 + 278 + unlock_host_lock: 279 + spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags); 280 + unlock_port_units_mutex: 281 + mutex_unlock(&zfcp_sysfs_port_units_mutex); 282 + return in_use; 283 + } 284 + 238 285 static ssize_t zfcp_sysfs_port_remove_store(struct device *dev, 239 286 struct device_attribute *attr, 240 287 const char *buf, size_t count) ··· 304 257 else 305 258 retval = 0; 306 259 307 - mutex_lock(&zfcp_sysfs_port_units_mutex); 308 - if (atomic_read(&port->units) > 0) { 260 + if (zfcp_sysfs_port_in_use(port)) { 309 261 retval = -EBUSY; 310 - mutex_unlock(&zfcp_sysfs_port_units_mutex); 311 262 put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */ 312 263 goto out; 313 264 } 314 - /* port is about to be removed, so no more unit_add */ 315 - atomic_set(&port->units, -1); 316 - mutex_unlock(&zfcp_sysfs_port_units_mutex); 317 265 318 266 write_lock_irq(&adapter->port_list_lock); 319 267 list_del(&port->list);
+7 -1
drivers/s390/scsi/zfcp_unit.c
··· 124 124 int retval = 0; 125 125 126 126 mutex_lock(&zfcp_sysfs_port_units_mutex); 127 - if (atomic_read(&port->units) == -1) { 127 + if (zfcp_sysfs_port_is_removing(port)) { 128 128 /* port is already gone */ 129 129 retval = -ENODEV; 130 130 goto out; ··· 168 168 write_lock_irq(&port->unit_list_lock); 169 169 list_add_tail(&unit->list, &port->unit_list); 170 170 write_unlock_irq(&port->unit_list_lock); 171 + /* 172 + * lock order: shost->scan_mutex before zfcp_sysfs_port_units_mutex 173 + * due to zfcp_unit_scsi_scan() => zfcp_scsi_slave_alloc() 174 + */ 175 + mutex_unlock(&zfcp_sysfs_port_units_mutex); 171 176 172 177 zfcp_unit_scsi_scan(unit); 178 + return retval; 173 179 174 180 out: 175 181 mutex_unlock(&zfcp_sysfs_port_units_mutex);