blob: d9ca357d98093c688ee96eac3834ce8b4be6d26b [file] [log] [blame]
Steve French1080ef72011-02-24 18:07:19 +00001/*
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
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -070020#include <linux/pagemap.h>
Steve French1080ef72011-02-24 18:07:19 +000021#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040022#include "smb2pdu.h"
23#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040024#include "cifsproto.h"
25#include "cifs_debug.h"
26
27static int
28change_conf(struct TCP_Server_Info *server)
29{
30 server->credits += server->echo_credits + server->oplock_credits;
31 server->oplock_credits = server->echo_credits = 0;
32 switch (server->credits) {
33 case 0:
34 return -1;
35 case 1:
36 server->echoes = false;
37 server->oplocks = false;
38 cERROR(1, "disabling echoes and oplocks");
39 break;
40 case 2:
41 server->echoes = true;
42 server->oplocks = false;
43 server->echo_credits = 1;
44 cFYI(1, "disabling oplocks");
45 break;
46 default:
47 server->echoes = true;
48 server->oplocks = true;
49 server->echo_credits = 1;
50 server->oplock_credits = 1;
51 }
52 server->credits -= server->echo_credits + server->oplock_credits;
53 return 0;
54}
55
56static void
57smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
58 const int optype)
59{
60 int *val, rc = 0;
61 spin_lock(&server->req_lock);
62 val = server->ops->get_credits_field(server, optype);
63 *val += add;
64 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040065 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040066 rc = change_conf(server);
67 spin_unlock(&server->req_lock);
68 wake_up(&server->request_q);
69 if (rc)
70 cifs_reconnect(server);
71}
72
73static void
74smb2_set_credits(struct TCP_Server_Info *server, const int val)
75{
76 spin_lock(&server->req_lock);
77 server->credits = val;
78 spin_unlock(&server->req_lock);
79}
80
81static int *
82smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
83{
84 switch (optype) {
85 case CIFS_ECHO_OP:
86 return &server->echo_credits;
87 case CIFS_OBREAK_OP:
88 return &server->oplock_credits;
89 default:
90 return &server->credits;
91 }
92}
93
94static unsigned int
95smb2_get_credits(struct mid_q_entry *mid)
96{
97 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
98}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040099
100static __u64
101smb2_get_next_mid(struct TCP_Server_Info *server)
102{
103 __u64 mid;
104 /* for SMB2 we need the current value */
105 spin_lock(&GlobalMid_Lock);
106 mid = server->CurrentMid++;
107 spin_unlock(&GlobalMid_Lock);
108 return mid;
109}
Steve French1080ef72011-02-24 18:07:19 +0000110
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400111static struct mid_q_entry *
112smb2_find_mid(struct TCP_Server_Info *server, char *buf)
113{
114 struct mid_q_entry *mid;
115 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
116
117 spin_lock(&GlobalMid_Lock);
118 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
119 if ((mid->mid == hdr->MessageId) &&
120 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
121 (mid->command == hdr->Command)) {
122 spin_unlock(&GlobalMid_Lock);
123 return mid;
124 }
125 }
126 spin_unlock(&GlobalMid_Lock);
127 return NULL;
128}
129
130static void
131smb2_dump_detail(void *buf)
132{
133#ifdef CONFIG_CIFS_DEBUG2
134 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
135
136 cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d",
137 smb->Command, smb->Status, smb->Flags, smb->MessageId,
138 smb->ProcessId);
139 cERROR(1, "smb buf %p len %u", smb, smb2_calc_size(smb));
140#endif
141}
142
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400143static bool
144smb2_need_neg(struct TCP_Server_Info *server)
145{
146 return server->max_read == 0;
147}
148
149static int
150smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
151{
152 int rc;
153 ses->server->CurrentMid = 0;
154 rc = SMB2_negotiate(xid, ses);
155 /* BB we probably don't need to retry with modern servers */
156 if (rc == -EAGAIN)
157 rc = -EHOSTDOWN;
158 return rc;
159}
160
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700161static unsigned int
162smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
163{
164 struct TCP_Server_Info *server = tcon->ses->server;
165 unsigned int wsize;
166
167 /* start with specified wsize, or default */
168 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
169 wsize = min_t(unsigned int, wsize, server->max_write);
170 /*
171 * limit write size to 2 ** 16, because we don't support multicredit
172 * requests now.
173 */
174 wsize = min_t(unsigned int, wsize, 2 << 15);
175
176 /* limit to the amount that we can kmap at once */
177 wsize = min_t(unsigned int, wsize, CIFS_KMAP_SIZE_LIMIT);
178
179 return wsize;
180}
181
182static unsigned int
183smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
184{
185 struct TCP_Server_Info *server = tcon->ses->server;
186 unsigned int rsize;
187
188 /* start with specified rsize, or default */
189 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
190 rsize = min_t(unsigned int, rsize, server->max_read);
191 /*
192 * limit write size to 2 ** 16, because we don't support multicredit
193 * requests now.
194 */
195 rsize = min_t(unsigned int, rsize, 2 << 15);
196
197 /* limit to the amount that we can kmap at once */
198 rsize = min_t(unsigned int, rsize, CIFS_KMAP_SIZE_LIMIT);
199
200 return rsize;
201}
202
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400203static int
204smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
205 struct cifs_sb_info *cifs_sb, const char *full_path)
206{
207 int rc;
208 __u64 persistent_fid, volatile_fid;
209 __le16 *utf16_path;
210
211 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
212 if (!utf16_path)
213 return -ENOMEM;
214
215 rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700216 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400217 if (rc) {
218 kfree(utf16_path);
219 return rc;
220 }
221
222 rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
223 kfree(utf16_path);
224 return rc;
225}
226
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400227static int
228smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
229 struct cifs_sb_info *cifs_sb, const char *full_path,
230 u64 *uniqueid, FILE_ALL_INFO *data)
231{
232 *uniqueid = le64_to_cpu(data->IndexNumber);
233 return 0;
234}
235
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700236static int
237smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
238 struct cifs_fid *fid, FILE_ALL_INFO *data)
239{
240 int rc;
241 struct smb2_file_all_info *smb2_data;
242
243 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
244 GFP_KERNEL);
245 if (smb2_data == NULL)
246 return -ENOMEM;
247
248 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
249 smb2_data);
250 if (!rc)
251 move_smb2_info_to_cifs(data, smb2_data);
252 kfree(smb2_data);
253 return rc;
254}
255
Pavel Shilovsky25e26632012-05-27 20:44:23 +0400256static char *
257smb2_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
258 struct cifs_tcon *tcon)
259{
260 int pplen = vol->prepath ? strlen(vol->prepath) : 0;
261 char *full_path = NULL;
262
263 /* if no prefix path, simply set path to the root of share to "" */
264 if (pplen == 0) {
265 full_path = kzalloc(2, GFP_KERNEL);
266 return full_path;
267 }
268
269 cERROR(1, "prefixpath is not supported for SMB2 now");
270 return NULL;
271}
272
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400273static bool
274smb2_can_echo(struct TCP_Server_Info *server)
275{
276 return server->echoes;
277}
278
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400279static void
280smb2_clear_stats(struct cifs_tcon *tcon)
281{
282#ifdef CONFIG_CIFS_STATS
283 int i;
284 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
285 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
286 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
287 }
288#endif
289}
290
291static void
292smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
293{
294#ifdef CONFIG_CIFS_STATS
295 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
296 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
297 seq_printf(m, "\nNegotiates: %d sent %d failed",
298 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
299 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
300 seq_printf(m, "\nSessionSetups: %d sent %d failed",
301 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
302 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
303#define SMB2LOGOFF 0x0002 /* trivial request/resp */
304 seq_printf(m, "\nLogoffs: %d sent %d failed",
305 atomic_read(&sent[SMB2_LOGOFF_HE]),
306 atomic_read(&failed[SMB2_LOGOFF_HE]));
307 seq_printf(m, "\nTreeConnects: %d sent %d failed",
308 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
309 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
310 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
311 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
312 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
313 seq_printf(m, "\nCreates: %d sent %d failed",
314 atomic_read(&sent[SMB2_CREATE_HE]),
315 atomic_read(&failed[SMB2_CREATE_HE]));
316 seq_printf(m, "\nCloses: %d sent %d failed",
317 atomic_read(&sent[SMB2_CLOSE_HE]),
318 atomic_read(&failed[SMB2_CLOSE_HE]));
319 seq_printf(m, "\nFlushes: %d sent %d failed",
320 atomic_read(&sent[SMB2_FLUSH_HE]),
321 atomic_read(&failed[SMB2_FLUSH_HE]));
322 seq_printf(m, "\nReads: %d sent %d failed",
323 atomic_read(&sent[SMB2_READ_HE]),
324 atomic_read(&failed[SMB2_READ_HE]));
325 seq_printf(m, "\nWrites: %d sent %d failed",
326 atomic_read(&sent[SMB2_WRITE_HE]),
327 atomic_read(&failed[SMB2_WRITE_HE]));
328 seq_printf(m, "\nLocks: %d sent %d failed",
329 atomic_read(&sent[SMB2_LOCK_HE]),
330 atomic_read(&failed[SMB2_LOCK_HE]));
331 seq_printf(m, "\nIOCTLs: %d sent %d failed",
332 atomic_read(&sent[SMB2_IOCTL_HE]),
333 atomic_read(&failed[SMB2_IOCTL_HE]));
334 seq_printf(m, "\nCancels: %d sent %d failed",
335 atomic_read(&sent[SMB2_CANCEL_HE]),
336 atomic_read(&failed[SMB2_CANCEL_HE]));
337 seq_printf(m, "\nEchos: %d sent %d failed",
338 atomic_read(&sent[SMB2_ECHO_HE]),
339 atomic_read(&failed[SMB2_ECHO_HE]));
340 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
341 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
342 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
343 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
344 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
345 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
346 seq_printf(m, "\nQueryInfos: %d sent %d failed",
347 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
348 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
349 seq_printf(m, "\nSetInfos: %d sent %d failed",
350 atomic_read(&sent[SMB2_SET_INFO_HE]),
351 atomic_read(&failed[SMB2_SET_INFO_HE]));
352 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
353 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
354 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
355#endif
356}
357
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700358static void
359smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
360{
361 /* struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); */
362 cfile->fid.persistent_fid = fid->persistent_fid;
363 cfile->fid.volatile_fid = fid->volatile_fid;
364 /* cifs_set_oplock_level(cinode, oplock); */
365 /* cinode->can_cache_brlcks = cinode->clientCanCacheAll; */
366}
367
368static int
369smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
370 struct cifs_fid *fid)
371{
372 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
373}
374
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700375static int
376smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
377 struct cifs_fid *fid)
378{
379 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
380}
381
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700382static unsigned int
383smb2_read_data_offset(char *buf)
384{
385 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
386 return rsp->DataOffset;
387}
388
389static unsigned int
390smb2_read_data_length(char *buf)
391{
392 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
393 return le32_to_cpu(rsp->DataLength);
394}
395
Steve French1080ef72011-02-24 18:07:19 +0000396struct smb_version_operations smb21_operations = {
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400397 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400398 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400399 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400400 .add_credits = smb2_add_credits,
401 .set_credits = smb2_set_credits,
402 .get_credits_field = smb2_get_credits_field,
403 .get_credits = smb2_get_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400404 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700405 .read_data_offset = smb2_read_data_offset,
406 .read_data_length = smb2_read_data_length,
407 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400408 .find_mid = smb2_find_mid,
409 .check_message = smb2_check_message,
410 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400411 .clear_stats = smb2_clear_stats,
412 .print_stats = smb2_print_stats,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400413 .need_neg = smb2_need_neg,
414 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700415 .negotiate_wsize = smb2_negotiate_wsize,
416 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400417 .sess_setup = SMB2_sess_setup,
418 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400419 .tree_connect = SMB2_tcon,
420 .tree_disconnect = SMB2_tdis,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400421 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400422 .can_echo = smb2_can_echo,
423 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400424 .query_path_info = smb2_query_path_info,
425 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700426 .query_file_info = smb2_query_file_info,
Pavel Shilovsky25e26632012-05-27 20:44:23 +0400427 .build_path_to_root = smb2_build_path_to_root,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400428 .mkdir = smb2_mkdir,
429 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400430 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700431 .unlink = smb2_unlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700432 .open = smb2_open_file,
433 .set_fid = smb2_set_fid,
434 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700435 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700436 .async_readv = smb2_async_readv,
Steve French1080ef72011-02-24 18:07:19 +0000437};
438
439struct smb_version_values smb21_values = {
440 .version_string = SMB21_VERSION_STRING,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400441 .header_size = sizeof(struct smb2_hdr),
442 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700443 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400444 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400445 .cap_unix = 0,
446 .cap_nt_find = SMB2_NT_FIND,
447 .cap_large_files = SMB2_LARGE_FILES,
Steve French1080ef72011-02-24 18:07:19 +0000448};