Skip to content

Commit 109bade

Browse files
hreineckemartinkpetersen
authored andcommitted
scsi: sg: use standard lists for sg_requests
'Sg_request' is using a private list implementation; convert it to standard lists. Signed-off-by: Hannes Reinecke <hare@suse.com> Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de> Tested-by: Johannes Thumshirn <jthumshirn@suse.de> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 28676d8 commit 109bade

File tree

1 file changed

+61
-86
lines changed

1 file changed

+61
-86
lines changed

drivers/scsi/sg.c

Lines changed: 61 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ struct sg_device; /* forward declarations */
122122
struct sg_fd;
123123

124124
typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
125-
struct sg_request *nextrp; /* NULL -> tail request (slist) */
125+
struct list_head entry; /* list entry */
126126
struct sg_fd *parentfp; /* NULL -> not in use */
127127
Sg_scatter_hold data; /* hold buffer, perhaps scatter list */
128128
sg_io_hdr_t header; /* scsi command+info, see <scsi/sg.h> */
@@ -146,7 +146,7 @@ typedef struct sg_fd { /* holds the state of a file descriptor */
146146
int timeout; /* defaults to SG_DEFAULT_TIMEOUT */
147147
int timeout_user; /* defaults to SG_DEFAULT_TIMEOUT_USER */
148148
Sg_scatter_hold reserve; /* buffer held for this file descriptor */
149-
Sg_request *headrp; /* head of request slist, NULL->empty */
149+
struct list_head rq_list; /* head of request list */
150150
struct fasync_struct *async_qp; /* used by asynchronous notification */
151151
Sg_request req_arr[SG_MAX_QUEUE]; /* used as singly-linked list */
152152
char force_packid; /* 1 -> pack_id input to read(), 0 -> ignored */
@@ -949,7 +949,7 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
949949
if (!access_ok(VERIFY_WRITE, ip, sizeof (int)))
950950
return -EFAULT;
951951
read_lock_irqsave(&sfp->rq_list_lock, iflags);
952-
for (srp = sfp->headrp; srp; srp = srp->nextrp) {
952+
list_for_each_entry(srp, &sfp->rq_list, entry) {
953953
if ((1 == srp->done) && (!srp->sg_io_owned)) {
954954
read_unlock_irqrestore(&sfp->rq_list_lock,
955955
iflags);
@@ -962,7 +962,8 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
962962
return 0;
963963
case SG_GET_NUM_WAITING:
964964
read_lock_irqsave(&sfp->rq_list_lock, iflags);
965-
for (val = 0, srp = sfp->headrp; srp; srp = srp->nextrp) {
965+
val = 0;
966+
list_for_each_entry(srp, &sfp->rq_list, entry) {
966967
if ((1 == srp->done) && (!srp->sg_io_owned))
967968
++val;
968969
}
@@ -1035,35 +1036,33 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
10351036
if (!rinfo)
10361037
return -ENOMEM;
10371038
read_lock_irqsave(&sfp->rq_list_lock, iflags);
1038-
for (srp = sfp->headrp, val = 0; val < SG_MAX_QUEUE;
1039-
++val, srp = srp ? srp->nextrp : srp) {
1039+
val = 0;
1040+
list_for_each_entry(srp, &sfp->rq_list, entry) {
1041+
if (val > SG_MAX_QUEUE)
1042+
break;
10401043
memset(&rinfo[val], 0, SZ_SG_REQ_INFO);
1041-
if (srp) {
1042-
rinfo[val].req_state = srp->done + 1;
1043-
rinfo[val].problem =
1044-
srp->header.masked_status &
1045-
srp->header.host_status &
1046-
srp->header.driver_status;
1047-
if (srp->done)
1048-
rinfo[val].duration =
1049-
srp->header.duration;
1050-
else {
1051-
ms = jiffies_to_msecs(jiffies);
1052-
rinfo[val].duration =
1053-
(ms > srp->header.duration) ?
1054-
(ms - srp->header.duration) : 0;
1055-
}
1056-
rinfo[val].orphan = srp->orphan;
1057-
rinfo[val].sg_io_owned =
1058-
srp->sg_io_owned;
1059-
rinfo[val].pack_id =
1060-
srp->header.pack_id;
1061-
rinfo[val].usr_ptr =
1062-
srp->header.usr_ptr;
1044+
rinfo[val].req_state = srp->done + 1;
1045+
rinfo[val].problem =
1046+
srp->header.masked_status &
1047+
srp->header.host_status &
1048+
srp->header.driver_status;
1049+
if (srp->done)
1050+
rinfo[val].duration =
1051+
srp->header.duration;
1052+
else {
1053+
ms = jiffies_to_msecs(jiffies);
1054+
rinfo[val].duration =
1055+
(ms > srp->header.duration) ?
1056+
(ms - srp->header.duration) : 0;
10631057
}
1058+
rinfo[val].orphan = srp->orphan;
1059+
rinfo[val].sg_io_owned = srp->sg_io_owned;
1060+
rinfo[val].pack_id = srp->header.pack_id;
1061+
rinfo[val].usr_ptr = srp->header.usr_ptr;
1062+
val++;
10641063
}
10651064
read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1066-
result = __copy_to_user(p, rinfo,
1065+
result = __copy_to_user(p, rinfo,
10671066
SZ_SG_REQ_INFO * SG_MAX_QUEUE);
10681067
result = result ? -EFAULT : 0;
10691068
kfree(rinfo);
@@ -1169,7 +1168,7 @@ sg_poll(struct file *filp, poll_table * wait)
11691168
return POLLERR;
11701169
poll_wait(filp, &sfp->read_wait, wait);
11711170
read_lock_irqsave(&sfp->rq_list_lock, iflags);
1172-
for (srp = sfp->headrp; srp; srp = srp->nextrp) {
1171+
list_for_each_entry(srp, &sfp->rq_list, entry) {
11731172
/* if any read waiting, flag it */
11741173
if ((0 == res) && (1 == srp->done) && (!srp->sg_io_owned))
11751174
res = POLLIN | POLLRDNORM;
@@ -2063,7 +2062,7 @@ sg_get_rq_mark(Sg_fd * sfp, int pack_id)
20632062
unsigned long iflags;
20642063

20652064
write_lock_irqsave(&sfp->rq_list_lock, iflags);
2066-
for (resp = sfp->headrp; resp; resp = resp->nextrp) {
2065+
list_for_each_entry(resp, &sfp->rq_list, entry) {
20672066
/* look for requests that are ready + not SG_IO owned */
20682067
if ((1 == resp->done) && (!resp->sg_io_owned) &&
20692068
((-1 == pack_id) || (resp->header.pack_id == pack_id))) {
@@ -2081,70 +2080,45 @@ sg_add_request(Sg_fd * sfp)
20812080
{
20822081
int k;
20832082
unsigned long iflags;
2084-
Sg_request *resp;
20852083
Sg_request *rp = sfp->req_arr;
20862084

20872085
write_lock_irqsave(&sfp->rq_list_lock, iflags);
2088-
resp = sfp->headrp;
2089-
if (!resp) {
2090-
memset(rp, 0, sizeof (Sg_request));
2091-
rp->parentfp = sfp;
2092-
resp = rp;
2093-
sfp->headrp = resp;
2094-
} else {
2095-
if (0 == sfp->cmd_q)
2096-
resp = NULL; /* command queuing disallowed */
2097-
else {
2098-
for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2099-
if (!rp->parentfp)
2100-
break;
2101-
}
2102-
if (k < SG_MAX_QUEUE) {
2103-
memset(rp, 0, sizeof (Sg_request));
2104-
rp->parentfp = sfp;
2105-
while (resp->nextrp)
2106-
resp = resp->nextrp;
2107-
resp->nextrp = rp;
2108-
resp = rp;
2109-
} else
2110-
resp = NULL;
2086+
if (!list_empty(&sfp->rq_list)) {
2087+
if (!sfp->cmd_q)
2088+
goto out_unlock;
2089+
2090+
for (k = 0; k < SG_MAX_QUEUE; ++k, ++rp) {
2091+
if (!rp->parentfp)
2092+
break;
21112093
}
2094+
if (k >= SG_MAX_QUEUE)
2095+
goto out_unlock;
21122096
}
2113-
if (resp) {
2114-
resp->nextrp = NULL;
2115-
resp->header.duration = jiffies_to_msecs(jiffies);
2116-
}
2097+
memset(rp, 0, sizeof (Sg_request));
2098+
rp->parentfp = sfp;
2099+
rp->header.duration = jiffies_to_msecs(jiffies);
2100+
list_add_tail(&rp->entry, &sfp->rq_list);
21172101
write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2118-
return resp;
2102+
return rp;
2103+
out_unlock:
2104+
write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
2105+
return NULL;
21192106
}
21202107

21212108
/* Return of 1 for found; 0 for not found */
21222109
static int
21232110
sg_remove_request(Sg_fd * sfp, Sg_request * srp)
21242111
{
2125-
Sg_request *prev_rp;
2126-
Sg_request *rp;
21272112
unsigned long iflags;
21282113
int res = 0;
21292114

2130-
if ((!sfp) || (!srp) || (!sfp->headrp))
2115+
if (!sfp || !srp || list_empty(&sfp->rq_list))
21312116
return res;
21322117
write_lock_irqsave(&sfp->rq_list_lock, iflags);
2133-
prev_rp = sfp->headrp;
2134-
if (srp == prev_rp) {
2135-
sfp->headrp = prev_rp->nextrp;
2136-
prev_rp->parentfp = NULL;
2118+
if (!list_empty(&srp->entry)) {
2119+
list_del(&srp->entry);
2120+
srp->parentfp = NULL;
21372121
res = 1;
2138-
} else {
2139-
while ((rp = prev_rp->nextrp)) {
2140-
if (srp == rp) {
2141-
prev_rp->nextrp = rp->nextrp;
2142-
rp->parentfp = NULL;
2143-
res = 1;
2144-
break;
2145-
}
2146-
prev_rp = rp;
2147-
}
21482122
}
21492123
write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
21502124
return res;
@@ -2163,7 +2137,7 @@ sg_add_sfp(Sg_device * sdp)
21632137

21642138
init_waitqueue_head(&sfp->read_wait);
21652139
rwlock_init(&sfp->rq_list_lock);
2166-
2140+
INIT_LIST_HEAD(&sfp->rq_list);
21672141
kref_init(&sfp->f_ref);
21682142
mutex_init(&sfp->f_mutex);
21692143
sfp->timeout = SG_DEFAULT_TIMEOUT;
@@ -2202,10 +2176,13 @@ sg_remove_sfp_usercontext(struct work_struct *work)
22022176
{
22032177
struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
22042178
struct sg_device *sdp = sfp->parentdp;
2179+
Sg_request *srp;
22052180

22062181
/* Cleanup any responses which were never read(). */
2207-
while (sfp->headrp)
2208-
sg_finish_rem_req(sfp->headrp);
2182+
while (!list_empty(&sfp->rq_list)) {
2183+
srp = list_first_entry(&sfp->rq_list, Sg_request, entry);
2184+
sg_finish_rem_req(srp);
2185+
}
22092186

22102187
if (sfp->reserve.bufflen > 0) {
22112188
SCSI_LOG_TIMEOUT(6, sg_printk(KERN_INFO, sdp,
@@ -2608,7 +2585,7 @@ static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
26082585
/* must be called while holding sg_index_lock */
26092586
static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
26102587
{
2611-
int k, m, new_interface, blen, usg;
2588+
int k, new_interface, blen, usg;
26122589
Sg_request *srp;
26132590
Sg_fd *fp;
26142591
const sg_io_hdr_t *hp;
@@ -2628,13 +2605,11 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
26282605
seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n",
26292606
(int) fp->cmd_q, (int) fp->force_packid,
26302607
(int) fp->keep_orphan);
2631-
for (m = 0, srp = fp->headrp;
2632-
srp != NULL;
2633-
++m, srp = srp->nextrp) {
2608+
list_for_each_entry(srp, &fp->rq_list, entry) {
26342609
hp = &srp->header;
26352610
new_interface = (hp->interface_id == '\0') ? 0 : 1;
26362611
if (srp->res_used) {
2637-
if (new_interface &&
2612+
if (new_interface &&
26382613
(SG_FLAG_MMAP_IO & hp->flags))
26392614
cp = " mmap>> ";
26402615
else
@@ -2665,7 +2640,7 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
26652640
seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
26662641
(int) srp->data.cmd_opcode);
26672642
}
2668-
if (0 == m)
2643+
if (list_empty(&fp->rq_list))
26692644
seq_puts(s, " No requests active\n");
26702645
read_unlock(&fp->rq_list_lock);
26712646
}

0 commit comments

Comments
 (0)