@@ -122,7 +122,7 @@ struct sg_device; /* forward declarations */
122
122
struct sg_fd ;
123
123
124
124
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 */
126
126
struct sg_fd * parentfp ; /* NULL -> not in use */
127
127
Sg_scatter_hold data ; /* hold buffer, perhaps scatter list */
128
128
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 */
146
146
int timeout ; /* defaults to SG_DEFAULT_TIMEOUT */
147
147
int timeout_user ; /* defaults to SG_DEFAULT_TIMEOUT_USER */
148
148
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 */
150
150
struct fasync_struct * async_qp ; /* used by asynchronous notification */
151
151
Sg_request req_arr [SG_MAX_QUEUE ]; /* used as singly-linked list */
152
152
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)
949
949
if (!access_ok (VERIFY_WRITE , ip , sizeof (int )))
950
950
return - EFAULT ;
951
951
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 ) {
953
953
if ((1 == srp -> done ) && (!srp -> sg_io_owned )) {
954
954
read_unlock_irqrestore (& sfp -> rq_list_lock ,
955
955
iflags );
@@ -962,7 +962,8 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
962
962
return 0 ;
963
963
case SG_GET_NUM_WAITING :
964
964
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 ) {
966
967
if ((1 == srp -> done ) && (!srp -> sg_io_owned ))
967
968
++ val ;
968
969
}
@@ -1035,35 +1036,33 @@ sg_ioctl(struct file *filp, unsigned int cmd_in, unsigned long arg)
1035
1036
if (!rinfo )
1036
1037
return - ENOMEM ;
1037
1038
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 ;
1040
1043
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 ;
1063
1057
}
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 ++ ;
1064
1063
}
1065
1064
read_unlock_irqrestore (& sfp -> rq_list_lock , iflags );
1066
- result = __copy_to_user (p , rinfo ,
1065
+ result = __copy_to_user (p , rinfo ,
1067
1066
SZ_SG_REQ_INFO * SG_MAX_QUEUE );
1068
1067
result = result ? - EFAULT : 0 ;
1069
1068
kfree (rinfo );
@@ -1169,7 +1168,7 @@ sg_poll(struct file *filp, poll_table * wait)
1169
1168
return POLLERR ;
1170
1169
poll_wait (filp , & sfp -> read_wait , wait );
1171
1170
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 ) {
1173
1172
/* if any read waiting, flag it */
1174
1173
if ((0 == res ) && (1 == srp -> done ) && (!srp -> sg_io_owned ))
1175
1174
res = POLLIN | POLLRDNORM ;
@@ -2063,7 +2062,7 @@ sg_get_rq_mark(Sg_fd * sfp, int pack_id)
2063
2062
unsigned long iflags ;
2064
2063
2065
2064
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 ) {
2067
2066
/* look for requests that are ready + not SG_IO owned */
2068
2067
if ((1 == resp -> done ) && (!resp -> sg_io_owned ) &&
2069
2068
((-1 == pack_id ) || (resp -> header .pack_id == pack_id ))) {
@@ -2081,70 +2080,45 @@ sg_add_request(Sg_fd * sfp)
2081
2080
{
2082
2081
int k ;
2083
2082
unsigned long iflags ;
2084
- Sg_request * resp ;
2085
2083
Sg_request * rp = sfp -> req_arr ;
2086
2084
2087
2085
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 ;
2111
2093
}
2094
+ if (k >= SG_MAX_QUEUE )
2095
+ goto out_unlock ;
2112
2096
}
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 );
2117
2101
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 ;
2119
2106
}
2120
2107
2121
2108
/* Return of 1 for found; 0 for not found */
2122
2109
static int
2123
2110
sg_remove_request (Sg_fd * sfp , Sg_request * srp )
2124
2111
{
2125
- Sg_request * prev_rp ;
2126
- Sg_request * rp ;
2127
2112
unsigned long iflags ;
2128
2113
int res = 0 ;
2129
2114
2130
- if (( !sfp ) || ( !srp ) || (! sfp -> headrp ))
2115
+ if (!sfp || !srp || list_empty ( & sfp -> rq_list ))
2131
2116
return res ;
2132
2117
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 ;
2137
2121
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
- }
2148
2122
}
2149
2123
write_unlock_irqrestore (& sfp -> rq_list_lock , iflags );
2150
2124
return res ;
@@ -2163,7 +2137,7 @@ sg_add_sfp(Sg_device * sdp)
2163
2137
2164
2138
init_waitqueue_head (& sfp -> read_wait );
2165
2139
rwlock_init (& sfp -> rq_list_lock );
2166
-
2140
+ INIT_LIST_HEAD ( & sfp -> rq_list );
2167
2141
kref_init (& sfp -> f_ref );
2168
2142
mutex_init (& sfp -> f_mutex );
2169
2143
sfp -> timeout = SG_DEFAULT_TIMEOUT ;
@@ -2202,10 +2176,13 @@ sg_remove_sfp_usercontext(struct work_struct *work)
2202
2176
{
2203
2177
struct sg_fd * sfp = container_of (work , struct sg_fd , ew .work );
2204
2178
struct sg_device * sdp = sfp -> parentdp ;
2179
+ Sg_request * srp ;
2205
2180
2206
2181
/* 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
+ }
2209
2186
2210
2187
if (sfp -> reserve .bufflen > 0 ) {
2211
2188
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)
2608
2585
/* must be called while holding sg_index_lock */
2609
2586
static void sg_proc_debug_helper (struct seq_file * s , Sg_device * sdp )
2610
2587
{
2611
- int k , m , new_interface , blen , usg ;
2588
+ int k , new_interface , blen , usg ;
2612
2589
Sg_request * srp ;
2613
2590
Sg_fd * fp ;
2614
2591
const sg_io_hdr_t * hp ;
@@ -2628,13 +2605,11 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2628
2605
seq_printf (s , " cmd_q=%d f_packid=%d k_orphan=%d closed=0\n" ,
2629
2606
(int ) fp -> cmd_q , (int ) fp -> force_packid ,
2630
2607
(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 ) {
2634
2609
hp = & srp -> header ;
2635
2610
new_interface = (hp -> interface_id == '\0' ) ? 0 : 1 ;
2636
2611
if (srp -> res_used ) {
2637
- if (new_interface &&
2612
+ if (new_interface &&
2638
2613
(SG_FLAG_MMAP_IO & hp -> flags ))
2639
2614
cp = " mmap>> " ;
2640
2615
else
@@ -2665,7 +2640,7 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2665
2640
seq_printf (s , "ms sgat=%d op=0x%02x\n" , usg ,
2666
2641
(int ) srp -> data .cmd_opcode );
2667
2642
}
2668
- if (0 == m )
2643
+ if (list_empty ( & fp -> rq_list ) )
2669
2644
seq_puts (s , " No requests active\n" );
2670
2645
read_unlock (& fp -> rq_list_lock );
2671
2646
}
0 commit comments