Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SMB2 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 | |
| 20 | #include "cifsglob.h" |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 21 | #include "smb2pdu.h" |
| 22 | #include "smb2proto.h" |
Pavel Shilovsky | 28ea529 | 2012-05-23 16:18:00 +0400 | [diff] [blame] | 23 | #include "cifsproto.h" |
| 24 | #include "cifs_debug.h" |
| 25 | |
| 26 | static int |
| 27 | change_conf(struct TCP_Server_Info *server) |
| 28 | { |
| 29 | server->credits += server->echo_credits + server->oplock_credits; |
| 30 | server->oplock_credits = server->echo_credits = 0; |
| 31 | switch (server->credits) { |
| 32 | case 0: |
| 33 | return -1; |
| 34 | case 1: |
| 35 | server->echoes = false; |
| 36 | server->oplocks = false; |
| 37 | cERROR(1, "disabling echoes and oplocks"); |
| 38 | break; |
| 39 | case 2: |
| 40 | server->echoes = true; |
| 41 | server->oplocks = false; |
| 42 | server->echo_credits = 1; |
| 43 | cFYI(1, "disabling oplocks"); |
| 44 | break; |
| 45 | default: |
| 46 | server->echoes = true; |
| 47 | server->oplocks = true; |
| 48 | server->echo_credits = 1; |
| 49 | server->oplock_credits = 1; |
| 50 | } |
| 51 | server->credits -= server->echo_credits + server->oplock_credits; |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add, |
| 57 | const int optype) |
| 58 | { |
| 59 | int *val, rc = 0; |
| 60 | spin_lock(&server->req_lock); |
| 61 | val = server->ops->get_credits_field(server, optype); |
| 62 | *val += add; |
| 63 | server->in_flight--; |
| 64 | if (server->in_flight == 0) |
| 65 | rc = change_conf(server); |
| 66 | spin_unlock(&server->req_lock); |
| 67 | wake_up(&server->request_q); |
| 68 | if (rc) |
| 69 | cifs_reconnect(server); |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | smb2_set_credits(struct TCP_Server_Info *server, const int val) |
| 74 | { |
| 75 | spin_lock(&server->req_lock); |
| 76 | server->credits = val; |
| 77 | spin_unlock(&server->req_lock); |
| 78 | } |
| 79 | |
| 80 | static int * |
| 81 | smb2_get_credits_field(struct TCP_Server_Info *server, const int optype) |
| 82 | { |
| 83 | switch (optype) { |
| 84 | case CIFS_ECHO_OP: |
| 85 | return &server->echo_credits; |
| 86 | case CIFS_OBREAK_OP: |
| 87 | return &server->oplock_credits; |
| 88 | default: |
| 89 | return &server->credits; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static unsigned int |
| 94 | smb2_get_credits(struct mid_q_entry *mid) |
| 95 | { |
| 96 | return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest); |
| 97 | } |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 98 | |
| 99 | static __u64 |
| 100 | smb2_get_next_mid(struct TCP_Server_Info *server) |
| 101 | { |
| 102 | __u64 mid; |
| 103 | /* for SMB2 we need the current value */ |
| 104 | spin_lock(&GlobalMid_Lock); |
| 105 | mid = server->CurrentMid++; |
| 106 | spin_unlock(&GlobalMid_Lock); |
| 107 | return mid; |
| 108 | } |
Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 109 | |
Pavel Shilovsky | 093b2bd | 2011-06-08 15:51:07 +0400 | [diff] [blame^] | 110 | static struct mid_q_entry * |
| 111 | smb2_find_mid(struct TCP_Server_Info *server, char *buf) |
| 112 | { |
| 113 | struct mid_q_entry *mid; |
| 114 | struct smb2_hdr *hdr = (struct smb2_hdr *)buf; |
| 115 | |
| 116 | spin_lock(&GlobalMid_Lock); |
| 117 | list_for_each_entry(mid, &server->pending_mid_q, qhead) { |
| 118 | if ((mid->mid == hdr->MessageId) && |
| 119 | (mid->mid_state == MID_REQUEST_SUBMITTED) && |
| 120 | (mid->command == hdr->Command)) { |
| 121 | spin_unlock(&GlobalMid_Lock); |
| 122 | return mid; |
| 123 | } |
| 124 | } |
| 125 | spin_unlock(&GlobalMid_Lock); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | smb2_dump_detail(void *buf) |
| 131 | { |
| 132 | #ifdef CONFIG_CIFS_DEBUG2 |
| 133 | struct smb2_hdr *smb = (struct smb2_hdr *)buf; |
| 134 | |
| 135 | cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d", |
| 136 | smb->Command, smb->Status, smb->Flags, smb->MessageId, |
| 137 | smb->ProcessId); |
| 138 | cERROR(1, "smb buf %p len %u", smb, smb2_calc_size(smb)); |
| 139 | #endif |
| 140 | } |
| 141 | |
Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 142 | struct smb_version_operations smb21_operations = { |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 143 | .setup_request = smb2_setup_request, |
| 144 | .check_receive = smb2_check_receive, |
Pavel Shilovsky | 28ea529 | 2012-05-23 16:18:00 +0400 | [diff] [blame] | 145 | .add_credits = smb2_add_credits, |
| 146 | .set_credits = smb2_set_credits, |
| 147 | .get_credits_field = smb2_get_credits_field, |
| 148 | .get_credits = smb2_get_credits, |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 149 | .get_next_mid = smb2_get_next_mid, |
Pavel Shilovsky | 093b2bd | 2011-06-08 15:51:07 +0400 | [diff] [blame^] | 150 | .find_mid = smb2_find_mid, |
| 151 | .check_message = smb2_check_message, |
| 152 | .dump_detail = smb2_dump_detail, |
Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | struct smb_version_values smb21_values = { |
| 156 | .version_string = SMB21_VERSION_STRING, |
Pavel Shilovsky | 093b2bd | 2011-06-08 15:51:07 +0400 | [diff] [blame^] | 157 | .header_size = sizeof(struct smb2_hdr), |
| 158 | .max_header_size = MAX_SMB2_HDR_SIZE, |
Pavel Shilovsky | 2dc7e1c | 2011-12-26 22:53:34 +0400 | [diff] [blame] | 159 | .lock_cmd = SMB2_LOCK, |
Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 160 | }; |