Skip to content

Commit cb0a168

Browse files
hreineckemartinkpetersen
authored andcommitted
scsi_dh_alua: update 'access_state' field
Track attached SCSI devices and update the 'access_state' field whenever an ALUA state change has been detected. Signed-off-by: Hannes Reinecke <[email protected]> Reviewed-by: Bart Van Assche <[email protected]> Reviewed-by: Ewan Milne <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Johannes Thumshirn <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 5115fc7 commit cb0a168

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

drivers/scsi/device_handler/scsi_dh_alua.c

+46-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/module.h>
2525
#include <asm/unaligned.h>
2626
#include <scsi/scsi.h>
27+
#include <scsi/scsi_proto.h>
2728
#include <scsi/scsi_dbg.h>
2829
#include <scsi/scsi_eh.h>
2930
#include <scsi/scsi_dh.h>
@@ -75,6 +76,7 @@ struct alua_port_group {
7576
struct kref kref;
7677
struct rcu_head rcu;
7778
struct list_head node;
79+
struct list_head dh_list;
7880
unsigned char device_id_str[256];
7981
int device_id_len;
8082
int group_id;
@@ -92,6 +94,7 @@ struct alua_port_group {
9294
};
9395

9496
struct alua_dh_data {
97+
struct list_head node;
9598
struct alua_port_group *pg;
9699
int group_id;
97100
spinlock_t pg_lock;
@@ -247,6 +250,7 @@ struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
247250
INIT_DELAYED_WORK(&pg->rtpg_work, alua_rtpg_work);
248251
INIT_LIST_HEAD(&pg->rtpg_list);
249252
INIT_LIST_HEAD(&pg->node);
253+
INIT_LIST_HEAD(&pg->dh_list);
250254
spin_lock_init(&pg->lock);
251255

252256
spin_lock(&port_group_lock);
@@ -328,6 +332,8 @@ static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
328332
{
329333
int rel_port = -1, group_id;
330334
struct alua_port_group *pg, *old_pg = NULL;
335+
bool pg_updated;
336+
unsigned long flags;
331337

332338
group_id = scsi_vpd_tpg_id(sdev, &rel_port);
333339
if (group_id < 0) {
@@ -357,10 +363,22 @@ static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
357363
old_pg = h->pg;
358364
if (old_pg != pg) {
359365
/* port group has changed. Update to new port group */
366+
if (h->pg) {
367+
spin_lock_irqsave(&old_pg->lock, flags);
368+
list_del_rcu(&h->node);
369+
spin_unlock_irqrestore(&old_pg->lock, flags);
370+
}
360371
rcu_assign_pointer(h->pg, pg);
372+
pg_updated = true;
361373
}
374+
375+
spin_lock_irqsave(&pg->lock, flags);
362376
if (sdev->synchronous_alua)
363377
pg->flags |= ALUA_SYNC_STPG;
378+
if (pg_updated)
379+
list_add_rcu(&h->node, &pg->dh_list);
380+
spin_unlock_irqrestore(&pg->lock, flags);
381+
364382
alua_rtpg_queue(h->pg, sdev, NULL, true);
365383
spin_unlock(&h->pg_lock);
366384

@@ -613,8 +631,18 @@ static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
613631
if (spin_trylock_irqsave(&tmp_pg->lock, flags)) {
614632
if ((tmp_pg == pg) ||
615633
!(tmp_pg->flags & ALUA_PG_RUNNING)) {
634+
struct alua_dh_data *h;
635+
616636
tmp_pg->state = desc[0] & 0x0f;
617637
tmp_pg->pref = desc[0] >> 7;
638+
rcu_read_lock();
639+
list_for_each_entry_rcu(h,
640+
&tmp_pg->dh_list, node) {
641+
/* h->sdev should always be valid */
642+
BUG_ON(!h->sdev);
643+
h->sdev->access_state = desc[0];
644+
}
645+
rcu_read_unlock();
618646
}
619647
if (tmp_pg == pg)
620648
valid_states = desc[1];
@@ -645,10 +673,22 @@ static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
645673
pg->interval = 2;
646674
err = SCSI_DH_RETRY;
647675
} else {
676+
struct alua_dh_data *h;
677+
648678
/* Transitioning time exceeded, set port to standby */
649679
err = SCSI_DH_IO;
650680
pg->state = SCSI_ACCESS_STATE_STANDBY;
651681
pg->expiry = 0;
682+
rcu_read_lock();
683+
list_for_each_entry_rcu(h, &pg->dh_list, node) {
684+
BUG_ON(!h->sdev);
685+
h->sdev->access_state =
686+
(pg->state & SCSI_ACCESS_STATE_MASK);
687+
if (pg->pref)
688+
h->sdev->access_state |=
689+
SCSI_ACCESS_STATE_PREFERRED;
690+
}
691+
rcu_read_unlock();
652692
}
653693
break;
654694
case SCSI_ACCESS_STATE_OFFLINE:
@@ -1041,6 +1081,7 @@ static int alua_bus_attach(struct scsi_device *sdev)
10411081
rcu_assign_pointer(h->pg, NULL);
10421082
h->init_error = SCSI_DH_OK;
10431083
h->sdev = sdev;
1084+
INIT_LIST_HEAD(&h->node);
10441085

10451086
mutex_init(&h->init_mutex);
10461087
err = alua_initialize(sdev, h);
@@ -1070,9 +1111,12 @@ static void alua_bus_detach(struct scsi_device *sdev)
10701111
rcu_assign_pointer(h->pg, NULL);
10711112
h->sdev = NULL;
10721113
spin_unlock(&h->pg_lock);
1073-
if (pg)
1114+
if (pg) {
1115+
spin_lock(&pg->lock);
1116+
list_del_rcu(&h->node);
1117+
spin_unlock(&pg->lock);
10741118
kref_put(&pg->kref, release_port_group);
1075-
1119+
}
10761120
sdev->handler_data = NULL;
10771121
kfree(h);
10781122
}

0 commit comments

Comments
 (0)