smb1ops.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * SMB1 (CIFS) version specific operations
  3. *
  4. * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License v2 as published
  8. * by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "cifsglob.h"
  20. #include "cifsproto.h"
  21. #include "cifs_debug.h"
  22. #include "cifspdu.h"
  23. /*
  24. * An NT cancel request header looks just like the original request except:
  25. *
  26. * The Command is SMB_COM_NT_CANCEL
  27. * The WordCount is zeroed out
  28. * The ByteCount is zeroed out
  29. *
  30. * This function mangles an existing request buffer into a
  31. * SMB_COM_NT_CANCEL request and then sends it.
  32. */
  33. static int
  34. send_nt_cancel(struct TCP_Server_Info *server, void *buf,
  35. struct mid_q_entry *mid)
  36. {
  37. int rc = 0;
  38. struct smb_hdr *in_buf = (struct smb_hdr *)buf;
  39. /* -4 for RFC1001 length and +2 for BCC field */
  40. in_buf->smb_buf_length = cpu_to_be32(sizeof(struct smb_hdr) - 4 + 2);
  41. in_buf->Command = SMB_COM_NT_CANCEL;
  42. in_buf->WordCount = 0;
  43. put_bcc(0, in_buf);
  44. mutex_lock(&server->srv_mutex);
  45. rc = cifs_sign_smb(in_buf, server, &mid->sequence_number);
  46. if (rc) {
  47. mutex_unlock(&server->srv_mutex);
  48. return rc;
  49. }
  50. rc = smb_send(server, in_buf, be32_to_cpu(in_buf->smb_buf_length));
  51. mutex_unlock(&server->srv_mutex);
  52. cFYI(1, "issued NT_CANCEL for mid %u, rc = %d",
  53. in_buf->Mid, rc);
  54. return rc;
  55. }
  56. static bool
  57. cifs_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
  58. {
  59. return ob1->netfid == ob2->netfid;
  60. }
  61. static unsigned int
  62. cifs_read_data_offset(char *buf)
  63. {
  64. READ_RSP *rsp = (READ_RSP *)buf;
  65. return le16_to_cpu(rsp->DataOffset);
  66. }
  67. static unsigned int
  68. cifs_read_data_length(char *buf)
  69. {
  70. READ_RSP *rsp = (READ_RSP *)buf;
  71. return (le16_to_cpu(rsp->DataLengthHigh) << 16) +
  72. le16_to_cpu(rsp->DataLength);
  73. }
  74. static struct mid_q_entry *
  75. cifs_find_mid(struct TCP_Server_Info *server, char *buffer)
  76. {
  77. struct smb_hdr *buf = (struct smb_hdr *)buffer;
  78. struct mid_q_entry *mid;
  79. spin_lock(&GlobalMid_Lock);
  80. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  81. if (mid->mid == buf->Mid &&
  82. mid->mid_state == MID_REQUEST_SUBMITTED &&
  83. le16_to_cpu(mid->command) == buf->Command) {
  84. spin_unlock(&GlobalMid_Lock);
  85. return mid;
  86. }
  87. }
  88. spin_unlock(&GlobalMid_Lock);
  89. return NULL;
  90. }
  91. static void
  92. cifs_add_credits(struct TCP_Server_Info *server, const unsigned int add)
  93. {
  94. spin_lock(&server->req_lock);
  95. server->credits += add;
  96. server->in_flight--;
  97. spin_unlock(&server->req_lock);
  98. wake_up(&server->request_q);
  99. }
  100. static void
  101. cifs_set_credits(struct TCP_Server_Info *server, const int val)
  102. {
  103. spin_lock(&server->req_lock);
  104. server->credits = val;
  105. server->oplocks = val > 1 ? enable_oplocks : false;
  106. spin_unlock(&server->req_lock);
  107. }
  108. static int *
  109. cifs_get_credits_field(struct TCP_Server_Info *server)
  110. {
  111. return &server->credits;
  112. }
  113. /*
  114. * Find a free multiplex id (SMB mid). Otherwise there could be
  115. * mid collisions which might cause problems, demultiplexing the
  116. * wrong response to this request. Multiplex ids could collide if
  117. * one of a series requests takes much longer than the others, or
  118. * if a very large number of long lived requests (byte range
  119. * locks or FindNotify requests) are pending. No more than
  120. * 64K-1 requests can be outstanding at one time. If no
  121. * mids are available, return zero. A future optimization
  122. * could make the combination of mids and uid the key we use
  123. * to demultiplex on (rather than mid alone).
  124. * In addition to the above check, the cifs demultiplex
  125. * code already used the command code as a secondary
  126. * check of the frame and if signing is negotiated the
  127. * response would be discarded if the mid were the same
  128. * but the signature was wrong. Since the mid is not put in the
  129. * pending queue until later (when it is about to be dispatched)
  130. * we do have to limit the number of outstanding requests
  131. * to somewhat less than 64K-1 although it is hard to imagine
  132. * so many threads being in the vfs at one time.
  133. */
  134. static __u64
  135. cifs_get_next_mid(struct TCP_Server_Info *server)
  136. {
  137. __u64 mid = 0;
  138. __u16 last_mid, cur_mid;
  139. bool collision;
  140. spin_lock(&GlobalMid_Lock);
  141. /* mid is 16 bit only for CIFS/SMB */
  142. cur_mid = (__u16)((server->CurrentMid) & 0xffff);
  143. /* we do not want to loop forever */
  144. last_mid = cur_mid;
  145. cur_mid++;
  146. /*
  147. * This nested loop looks more expensive than it is.
  148. * In practice the list of pending requests is short,
  149. * fewer than 50, and the mids are likely to be unique
  150. * on the first pass through the loop unless some request
  151. * takes longer than the 64 thousand requests before it
  152. * (and it would also have to have been a request that
  153. * did not time out).
  154. */
  155. while (cur_mid != last_mid) {
  156. struct mid_q_entry *mid_entry;
  157. unsigned int num_mids;
  158. collision = false;
  159. if (cur_mid == 0)
  160. cur_mid++;
  161. num_mids = 0;
  162. list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
  163. ++num_mids;
  164. if (mid_entry->mid == cur_mid &&
  165. mid_entry->mid_state == MID_REQUEST_SUBMITTED) {
  166. /* This mid is in use, try a different one */
  167. collision = true;
  168. break;
  169. }
  170. }
  171. /*
  172. * if we have more than 32k mids in the list, then something
  173. * is very wrong. Possibly a local user is trying to DoS the
  174. * box by issuing long-running calls and SIGKILL'ing them. If
  175. * we get to 2^16 mids then we're in big trouble as this
  176. * function could loop forever.
  177. *
  178. * Go ahead and assign out the mid in this situation, but force
  179. * an eventual reconnect to clean out the pending_mid_q.
  180. */
  181. if (num_mids > 32768)
  182. server->tcpStatus = CifsNeedReconnect;
  183. if (!collision) {
  184. mid = (__u64)cur_mid;
  185. server->CurrentMid = mid;
  186. break;
  187. }
  188. cur_mid++;
  189. }
  190. spin_unlock(&GlobalMid_Lock);
  191. return mid;
  192. }
  193. struct smb_version_operations smb1_operations = {
  194. .send_cancel = send_nt_cancel,
  195. .compare_fids = cifs_compare_fids,
  196. .setup_request = cifs_setup_request,
  197. .check_receive = cifs_check_receive,
  198. .add_credits = cifs_add_credits,
  199. .set_credits = cifs_set_credits,
  200. .get_credits_field = cifs_get_credits_field,
  201. .get_next_mid = cifs_get_next_mid,
  202. .read_data_offset = cifs_read_data_offset,
  203. .read_data_length = cifs_read_data_length,
  204. .map_error = map_smb_to_linux_error,
  205. .find_mid = cifs_find_mid,
  206. .check_message = checkSMB,
  207. .dump_detail = cifs_dump_detail,
  208. .is_oplock_break = is_valid_oplock_break,
  209. };
  210. struct smb_version_values smb1_values = {
  211. .version_string = SMB1_VERSION_STRING,
  212. .large_lock_type = LOCKING_ANDX_LARGE_FILES,
  213. .exclusive_lock_type = 0,
  214. .shared_lock_type = LOCKING_ANDX_SHARED_LOCK,
  215. .unlock_lock_type = 0,
  216. .header_size = sizeof(struct smb_hdr),
  217. .max_header_size = MAX_CIFS_HDR_SIZE,
  218. .read_rsp_size = sizeof(READ_RSP),
  219. };