Skip to content

Commit 8825736

Browse files
piastrySteve French
authored andcommitted
CIFS: Move get_next_mid to ops struct
Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Pavel Shilovsky <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 7f0adb5 commit 8825736

File tree

7 files changed

+103
-95
lines changed

7 files changed

+103
-95
lines changed

fs/cifs/cifsglob.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ struct smb_version_operations {
174174
void (*add_credits)(struct TCP_Server_Info *, const unsigned int);
175175
void (*set_credits)(struct TCP_Server_Info *, const int);
176176
int * (*get_credits_field)(struct TCP_Server_Info *);
177+
__u64 (*get_next_mid)(struct TCP_Server_Info *);
177178
/* data offset from read response message */
178179
unsigned int (*read_data_offset)(char *);
179180
/* data length from read response message */
@@ -399,6 +400,12 @@ set_credits(struct TCP_Server_Info *server, const int val)
399400
server->ops->set_credits(server, val);
400401
}
401402

403+
static inline __u64
404+
get_next_mid(struct TCP_Server_Info *server)
405+
{
406+
return server->ops->get_next_mid(server);
407+
}
408+
402409
/*
403410
* Macros to allow the TCP_Server_Info->net field and related code to drop out
404411
* when CONFIG_NET_NS isn't set.

fs/cifs/cifsproto.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ extern int small_smb_init_no_tc(const int smb_cmd, const int wct,
114114
void **request_buf);
115115
extern int CIFS_SessSetup(unsigned int xid, struct cifs_ses *ses,
116116
const struct nls_table *nls_cp);
117-
extern __u64 GetNextMid(struct TCP_Server_Info *server);
118117
extern struct timespec cifs_NTtimeToUnix(__le64 utc_nanoseconds_since_1601);
119118
extern u64 cifs_UnixTimeToNT(struct timespec);
120119
extern struct timespec cnvrtDosUnixTm(__le16 le_date, __le16 le_time,

fs/cifs/cifssmb.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ small_smb_init_no_tc(const int smb_command, const int wct,
268268
return rc;
269269

270270
buffer = (struct smb_hdr *)*request_buf;
271-
buffer->Mid = GetNextMid(ses->server);
271+
buffer->Mid = get_next_mid(ses->server);
272272
if (ses->capabilities & CAP_UNICODE)
273273
buffer->Flags2 |= SMBFLG2_UNICODE;
274274
if (ses->capabilities & CAP_STATUS32)
@@ -402,7 +402,7 @@ CIFSSMBNegotiate(unsigned int xid, struct cifs_ses *ses)
402402

403403
cFYI(1, "secFlags 0x%x", secFlags);
404404

405-
pSMB->hdr.Mid = GetNextMid(server);
405+
pSMB->hdr.Mid = get_next_mid(server);
406406
pSMB->hdr.Flags2 |= (SMBFLG2_UNICODE | SMBFLG2_ERR_STATUS);
407407

408408
if ((secFlags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
@@ -782,7 +782,7 @@ CIFSSMBLogoff(const int xid, struct cifs_ses *ses)
782782
return rc;
783783
}
784784

785-
pSMB->hdr.Mid = GetNextMid(ses->server);
785+
pSMB->hdr.Mid = get_next_mid(ses->server);
786786

787787
if (ses->server->sec_mode &
788788
(SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
@@ -4762,7 +4762,7 @@ CIFSGetDFSRefer(const int xid, struct cifs_ses *ses,
47624762

47634763
/* server pointer checked in called function,
47644764
but should never be null here anyway */
4765-
pSMB->hdr.Mid = GetNextMid(ses->server);
4765+
pSMB->hdr.Mid = get_next_mid(ses->server);
47664766
pSMB->hdr.Tid = ses->ipc_tid;
47674767
pSMB->hdr.Uid = ses->Suid;
47684768
if (ses->capabilities & CAP_STATUS32)

fs/cifs/connect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3940,7 +3940,7 @@ CIFSTCon(unsigned int xid, struct cifs_ses *ses,
39403940
header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
39413941
NULL /*no tid */ , 4 /*wct */ );
39423942

3943-
smb_buffer->Mid = GetNextMid(ses->server);
3943+
smb_buffer->Mid = get_next_mid(ses->server);
39443944
smb_buffer->Uid = ses->Suid;
39453945
pSMB = (TCONX_REQ *) smb_buffer;
39463946
pSMBr = (TCONX_RSP *) smb_buffer_response;

fs/cifs/misc.c

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -212,93 +212,6 @@ cifs_small_buf_release(void *buf_to_free)
212212
return;
213213
}
214214

215-
/*
216-
* Find a free multiplex id (SMB mid). Otherwise there could be
217-
* mid collisions which might cause problems, demultiplexing the
218-
* wrong response to this request. Multiplex ids could collide if
219-
* one of a series requests takes much longer than the others, or
220-
* if a very large number of long lived requests (byte range
221-
* locks or FindNotify requests) are pending. No more than
222-
* 64K-1 requests can be outstanding at one time. If no
223-
* mids are available, return zero. A future optimization
224-
* could make the combination of mids and uid the key we use
225-
* to demultiplex on (rather than mid alone).
226-
* In addition to the above check, the cifs demultiplex
227-
* code already used the command code as a secondary
228-
* check of the frame and if signing is negotiated the
229-
* response would be discarded if the mid were the same
230-
* but the signature was wrong. Since the mid is not put in the
231-
* pending queue until later (when it is about to be dispatched)
232-
* we do have to limit the number of outstanding requests
233-
* to somewhat less than 64K-1 although it is hard to imagine
234-
* so many threads being in the vfs at one time.
235-
*/
236-
__u64 GetNextMid(struct TCP_Server_Info *server)
237-
{
238-
__u64 mid = 0;
239-
__u16 last_mid, cur_mid;
240-
bool collision;
241-
242-
spin_lock(&GlobalMid_Lock);
243-
244-
/* mid is 16 bit only for CIFS/SMB */
245-
cur_mid = (__u16)((server->CurrentMid) & 0xffff);
246-
/* we do not want to loop forever */
247-
last_mid = cur_mid;
248-
cur_mid++;
249-
250-
/*
251-
* This nested loop looks more expensive than it is.
252-
* In practice the list of pending requests is short,
253-
* fewer than 50, and the mids are likely to be unique
254-
* on the first pass through the loop unless some request
255-
* takes longer than the 64 thousand requests before it
256-
* (and it would also have to have been a request that
257-
* did not time out).
258-
*/
259-
while (cur_mid != last_mid) {
260-
struct mid_q_entry *mid_entry;
261-
unsigned int num_mids;
262-
263-
collision = false;
264-
if (cur_mid == 0)
265-
cur_mid++;
266-
267-
num_mids = 0;
268-
list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
269-
++num_mids;
270-
if (mid_entry->mid == cur_mid &&
271-
mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
272-
/* This mid is in use, try a different one */
273-
collision = true;
274-
break;
275-
}
276-
}
277-
278-
/*
279-
* if we have more than 32k mids in the list, then something
280-
* is very wrong. Possibly a local user is trying to DoS the
281-
* box by issuing long-running calls and SIGKILL'ing them. If
282-
* we get to 2^16 mids then we're in big trouble as this
283-
* function could loop forever.
284-
*
285-
* Go ahead and assign out the mid in this situation, but force
286-
* an eventual reconnect to clean out the pending_mid_q.
287-
*/
288-
if (num_mids > 32768)
289-
server->tcpStatus = CifsNeedReconnect;
290-
291-
if (!collision) {
292-
mid = (__u64)cur_mid;
293-
server->CurrentMid = mid;
294-
break;
295-
}
296-
cur_mid++;
297-
}
298-
spin_unlock(&GlobalMid_Lock);
299-
return mid;
300-
}
301-
302215
/* NB: MID can not be set if treeCon not passed in, in that
303216
case it is responsbility of caller to set the mid */
304217
void
@@ -334,7 +247,7 @@ header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
334247

335248
/* Uid is not converted */
336249
buffer->Uid = treeCon->ses->Suid;
337-
buffer->Mid = GetNextMid(treeCon->ses->server);
250+
buffer->Mid = get_next_mid(treeCon->ses->server);
338251
}
339252
if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
340253
buffer->Flags2 |= SMBFLG2_DFS;

fs/cifs/smb1ops.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,94 @@ cifs_get_credits_field(struct TCP_Server_Info *server)
125125
return &server->credits;
126126
}
127127

128+
/*
129+
* Find a free multiplex id (SMB mid). Otherwise there could be
130+
* mid collisions which might cause problems, demultiplexing the
131+
* wrong response to this request. Multiplex ids could collide if
132+
* one of a series requests takes much longer than the others, or
133+
* if a very large number of long lived requests (byte range
134+
* locks or FindNotify requests) are pending. No more than
135+
* 64K-1 requests can be outstanding at one time. If no
136+
* mids are available, return zero. A future optimization
137+
* could make the combination of mids and uid the key we use
138+
* to demultiplex on (rather than mid alone).
139+
* In addition to the above check, the cifs demultiplex
140+
* code already used the command code as a secondary
141+
* check of the frame and if signing is negotiated the
142+
* response would be discarded if the mid were the same
143+
* but the signature was wrong. Since the mid is not put in the
144+
* pending queue until later (when it is about to be dispatched)
145+
* we do have to limit the number of outstanding requests
146+
* to somewhat less than 64K-1 although it is hard to imagine
147+
* so many threads being in the vfs at one time.
148+
*/
149+
static __u64
150+
cifs_get_next_mid(struct TCP_Server_Info *server)
151+
{
152+
__u64 mid = 0;
153+
__u16 last_mid, cur_mid;
154+
bool collision;
155+
156+
spin_lock(&GlobalMid_Lock);
157+
158+
/* mid is 16 bit only for CIFS/SMB */
159+
cur_mid = (__u16)((server->CurrentMid) & 0xffff);
160+
/* we do not want to loop forever */
161+
last_mid = cur_mid;
162+
cur_mid++;
163+
164+
/*
165+
* This nested loop looks more expensive than it is.
166+
* In practice the list of pending requests is short,
167+
* fewer than 50, and the mids are likely to be unique
168+
* on the first pass through the loop unless some request
169+
* takes longer than the 64 thousand requests before it
170+
* (and it would also have to have been a request that
171+
* did not time out).
172+
*/
173+
while (cur_mid != last_mid) {
174+
struct mid_q_entry *mid_entry;
175+
unsigned int num_mids;
176+
177+
collision = false;
178+
if (cur_mid == 0)
179+
cur_mid++;
180+
181+
num_mids = 0;
182+
list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
183+
++num_mids;
184+
if (mid_entry->mid == cur_mid &&
185+
mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
186+
/* This mid is in use, try a different one */
187+
collision = true;
188+
break;
189+
}
190+
}
191+
192+
/*
193+
* if we have more than 32k mids in the list, then something
194+
* is very wrong. Possibly a local user is trying to DoS the
195+
* box by issuing long-running calls and SIGKILL'ing them. If
196+
* we get to 2^16 mids then we're in big trouble as this
197+
* function could loop forever.
198+
*
199+
* Go ahead and assign out the mid in this situation, but force
200+
* an eventual reconnect to clean out the pending_mid_q.
201+
*/
202+
if (num_mids > 32768)
203+
server->tcpStatus = CifsNeedReconnect;
204+
205+
if (!collision) {
206+
mid = (__u64)cur_mid;
207+
server->CurrentMid = mid;
208+
break;
209+
}
210+
cur_mid++;
211+
}
212+
spin_unlock(&GlobalMid_Lock);
213+
return mid;
214+
}
215+
128216
struct smb_version_operations smb1_operations = {
129217
.send_cancel = send_nt_cancel,
130218
.compare_fids = cifs_compare_fids,
@@ -133,6 +221,7 @@ struct smb_version_operations smb1_operations = {
133221
.add_credits = cifs_add_credits,
134222
.set_credits = cifs_set_credits,
135223
.get_credits_field = cifs_get_credits_field,
224+
.get_next_mid = cifs_get_next_mid,
136225
.read_data_offset = cifs_read_data_offset,
137226
.read_data_length = cifs_read_data_length,
138227
.map_error = map_smb_to_linux_error,

fs/cifs/transport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ send_lock_cancel(const unsigned int xid, struct cifs_tcon *tcon,
779779

780780
pSMB->LockType = LOCKING_ANDX_CANCEL_LOCK|LOCKING_ANDX_LARGE_FILES;
781781
pSMB->Timeout = 0;
782-
pSMB->hdr.Mid = GetNextMid(ses->server);
782+
pSMB->hdr.Mid = get_next_mid(ses->server);
783783

784784
return SendReceive(xid, ses, in_buf, out_buf,
785785
&bytes_returned, 0);

0 commit comments

Comments
 (0)