@@ -47,6 +47,7 @@
#include <linux/blkpg.h>
#include <linux/blk-pm.h>
#include <linux/delay.h>
+#include <linux/list_sort.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/string_helpers.h>
@@ -2058,6 +2059,38 @@ static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp)
return eh_disp;
}
+static int sd_cmp_sector(void *priv, const struct list_head *_a,
+ const struct list_head *_b)
+{
+ struct scsi_cmnd *a = list_entry(_a, typeof(*a), eh_entry);
+ struct scsi_cmnd *b = list_entry(_b, typeof(*b), eh_entry);
+ struct request *rq_a = scsi_cmd_to_rq(a);
+ struct request *rq_b = scsi_cmd_to_rq(b);
+ bool use_zwl_a = rq_a->q->limits.use_zone_write_lock;
+ bool use_zwl_b = rq_b->q->limits.use_zone_write_lock;
+
+ /*
+ * Order the commands that need zone write locking after the commands
+ * that do not need zone write locking. Order the commands that do not
+ * need zone write locking by LBA. Do not reorder the commands that
+ * need zone write locking. See also the comment above the list_sort()
+ * definition.
+ */
+ if (use_zwl_a || use_zwl_b)
+ return use_zwl_a > use_zwl_b;
+ return blk_rq_pos(rq_a) > blk_rq_pos(rq_b);
+}
+
+static void sd_prepare_resubmit(struct list_head *cmd_list)
+{
+ /*
+ * Sort pending SCSI commands in starting sector order. This is
+ * important if one of the SCSI devices associated with @shost is a
+ * zoned block device for which zone write locking is disabled.
+ */
+ list_sort(NULL, cmd_list, sd_cmp_sector);
+}
+
static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
{
struct request *req = scsi_cmd_to_rq(scmd);
@@ -4014,6 +4047,7 @@ static struct scsi_driver sd_template = {
.done = sd_done,
.eh_action = sd_eh_action,
.eh_reset = sd_eh_reset,
+ .eh_prepare_resubmit = sd_prepare_resubmit,
};
/**