blob: 6d15cab95b9908baf0509bceef86fbeb79c93ebd [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>
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070021#include <linux/vfs.h>
Steve French1080ef72011-02-24 18:07:19 +000022#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040023#include "smb2pdu.h"
24#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040025#include "cifsproto.h"
26#include "cifs_debug.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070027#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070028#include "smb2glob.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040029
30static int
31change_conf(struct TCP_Server_Info *server)
32{
33 server->credits += server->echo_credits + server->oplock_credits;
34 server->oplock_credits = server->echo_credits = 0;
35 switch (server->credits) {
36 case 0:
37 return -1;
38 case 1:
39 server->echoes = false;
40 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050041 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040042 break;
43 case 2:
44 server->echoes = true;
45 server->oplocks = false;
46 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050047 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040048 break;
49 default:
50 server->echoes = true;
51 server->oplocks = true;
52 server->echo_credits = 1;
53 server->oplock_credits = 1;
54 }
55 server->credits -= server->echo_credits + server->oplock_credits;
56 return 0;
57}
58
59static void
60smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
61 const int optype)
62{
63 int *val, rc = 0;
64 spin_lock(&server->req_lock);
65 val = server->ops->get_credits_field(server, optype);
66 *val += add;
67 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040068 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040069 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070070 /*
71 * Sometimes server returns 0 credits on oplock break ack - we need to
72 * rebalance credits in this case.
73 */
74 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
75 server->oplocks) {
76 if (server->credits > 1) {
77 server->credits--;
78 server->oplock_credits++;
79 }
80 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040081 spin_unlock(&server->req_lock);
82 wake_up(&server->request_q);
83 if (rc)
84 cifs_reconnect(server);
85}
86
87static void
88smb2_set_credits(struct TCP_Server_Info *server, const int val)
89{
90 spin_lock(&server->req_lock);
91 server->credits = val;
92 spin_unlock(&server->req_lock);
93}
94
95static int *
96smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
97{
98 switch (optype) {
99 case CIFS_ECHO_OP:
100 return &server->echo_credits;
101 case CIFS_OBREAK_OP:
102 return &server->oplock_credits;
103 default:
104 return &server->credits;
105 }
106}
107
108static unsigned int
109smb2_get_credits(struct mid_q_entry *mid)
110{
111 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
112}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400113
114static __u64
115smb2_get_next_mid(struct TCP_Server_Info *server)
116{
117 __u64 mid;
118 /* for SMB2 we need the current value */
119 spin_lock(&GlobalMid_Lock);
120 mid = server->CurrentMid++;
121 spin_unlock(&GlobalMid_Lock);
122 return mid;
123}
Steve French1080ef72011-02-24 18:07:19 +0000124
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400125static struct mid_q_entry *
126smb2_find_mid(struct TCP_Server_Info *server, char *buf)
127{
128 struct mid_q_entry *mid;
129 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
130
131 spin_lock(&GlobalMid_Lock);
132 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
133 if ((mid->mid == hdr->MessageId) &&
134 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
135 (mid->command == hdr->Command)) {
136 spin_unlock(&GlobalMid_Lock);
137 return mid;
138 }
139 }
140 spin_unlock(&GlobalMid_Lock);
141 return NULL;
142}
143
144static void
145smb2_dump_detail(void *buf)
146{
147#ifdef CONFIG_CIFS_DEBUG2
148 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
149
Joe Perchesf96637b2013-05-04 22:12:25 -0500150 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
151 smb->Command, smb->Status, smb->Flags, smb->MessageId,
152 smb->ProcessId);
153 cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400154#endif
155}
156
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400157static bool
158smb2_need_neg(struct TCP_Server_Info *server)
159{
160 return server->max_read == 0;
161}
162
163static int
164smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
165{
166 int rc;
167 ses->server->CurrentMid = 0;
168 rc = SMB2_negotiate(xid, ses);
169 /* BB we probably don't need to retry with modern servers */
170 if (rc == -EAGAIN)
171 rc = -EHOSTDOWN;
172 return rc;
173}
174
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700175static unsigned int
176smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
177{
178 struct TCP_Server_Info *server = tcon->ses->server;
179 unsigned int wsize;
180
181 /* start with specified wsize, or default */
182 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
183 wsize = min_t(unsigned int, wsize, server->max_write);
184 /*
185 * limit write size to 2 ** 16, because we don't support multicredit
186 * requests now.
187 */
188 wsize = min_t(unsigned int, wsize, 2 << 15);
189
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700190 return wsize;
191}
192
193static unsigned int
194smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
195{
196 struct TCP_Server_Info *server = tcon->ses->server;
197 unsigned int rsize;
198
199 /* start with specified rsize, or default */
200 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
201 rsize = min_t(unsigned int, rsize, server->max_read);
202 /*
203 * limit write size to 2 ** 16, because we don't support multicredit
204 * requests now.
205 */
206 rsize = min_t(unsigned int, rsize, 2 << 15);
207
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700208 return rsize;
209}
210
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400211static int
212smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
213 struct cifs_sb_info *cifs_sb, const char *full_path)
214{
215 int rc;
216 __u64 persistent_fid, volatile_fid;
217 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700218 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400219
220 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
221 if (!utf16_path)
222 return -ENOMEM;
223
224 rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700225 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, &oplock, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400226 if (rc) {
227 kfree(utf16_path);
228 return rc;
229 }
230
231 rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
232 kfree(utf16_path);
233 return rc;
234}
235
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400236static int
237smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
238 struct cifs_sb_info *cifs_sb, const char *full_path,
239 u64 *uniqueid, FILE_ALL_INFO *data)
240{
241 *uniqueid = le64_to_cpu(data->IndexNumber);
242 return 0;
243}
244
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700245static int
246smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
247 struct cifs_fid *fid, FILE_ALL_INFO *data)
248{
249 int rc;
250 struct smb2_file_all_info *smb2_data;
251
252 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
253 GFP_KERNEL);
254 if (smb2_data == NULL)
255 return -ENOMEM;
256
257 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
258 smb2_data);
259 if (!rc)
260 move_smb2_info_to_cifs(data, smb2_data);
261 kfree(smb2_data);
262 return rc;
263}
264
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400265static bool
266smb2_can_echo(struct TCP_Server_Info *server)
267{
268 return server->echoes;
269}
270
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400271static void
272smb2_clear_stats(struct cifs_tcon *tcon)
273{
274#ifdef CONFIG_CIFS_STATS
275 int i;
276 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
277 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
278 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
279 }
280#endif
281}
282
283static void
Steve French769ee6a2013-06-19 14:15:30 -0500284smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
285{
286 seq_puts(m, "\n\tShare Capabilities:");
287 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
288 seq_puts(m, " DFS,");
289 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
290 seq_puts(m, " CONTINUOUS AVAILABILITY,");
291 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
292 seq_puts(m, " SCALEOUT,");
293 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
294 seq_puts(m, " CLUSTER,");
295 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
296 seq_puts(m, " ASYMMETRIC,");
297 if (tcon->capabilities == 0)
298 seq_puts(m, " None");
299 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
300}
301
302static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400303smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
304{
305#ifdef CONFIG_CIFS_STATS
306 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
307 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
308 seq_printf(m, "\nNegotiates: %d sent %d failed",
309 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
310 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
311 seq_printf(m, "\nSessionSetups: %d sent %d failed",
312 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
313 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400314 seq_printf(m, "\nLogoffs: %d sent %d failed",
315 atomic_read(&sent[SMB2_LOGOFF_HE]),
316 atomic_read(&failed[SMB2_LOGOFF_HE]));
317 seq_printf(m, "\nTreeConnects: %d sent %d failed",
318 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
319 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
320 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
321 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
322 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
323 seq_printf(m, "\nCreates: %d sent %d failed",
324 atomic_read(&sent[SMB2_CREATE_HE]),
325 atomic_read(&failed[SMB2_CREATE_HE]));
326 seq_printf(m, "\nCloses: %d sent %d failed",
327 atomic_read(&sent[SMB2_CLOSE_HE]),
328 atomic_read(&failed[SMB2_CLOSE_HE]));
329 seq_printf(m, "\nFlushes: %d sent %d failed",
330 atomic_read(&sent[SMB2_FLUSH_HE]),
331 atomic_read(&failed[SMB2_FLUSH_HE]));
332 seq_printf(m, "\nReads: %d sent %d failed",
333 atomic_read(&sent[SMB2_READ_HE]),
334 atomic_read(&failed[SMB2_READ_HE]));
335 seq_printf(m, "\nWrites: %d sent %d failed",
336 atomic_read(&sent[SMB2_WRITE_HE]),
337 atomic_read(&failed[SMB2_WRITE_HE]));
338 seq_printf(m, "\nLocks: %d sent %d failed",
339 atomic_read(&sent[SMB2_LOCK_HE]),
340 atomic_read(&failed[SMB2_LOCK_HE]));
341 seq_printf(m, "\nIOCTLs: %d sent %d failed",
342 atomic_read(&sent[SMB2_IOCTL_HE]),
343 atomic_read(&failed[SMB2_IOCTL_HE]));
344 seq_printf(m, "\nCancels: %d sent %d failed",
345 atomic_read(&sent[SMB2_CANCEL_HE]),
346 atomic_read(&failed[SMB2_CANCEL_HE]));
347 seq_printf(m, "\nEchos: %d sent %d failed",
348 atomic_read(&sent[SMB2_ECHO_HE]),
349 atomic_read(&failed[SMB2_ECHO_HE]));
350 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
351 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
352 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
353 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
354 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
355 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
356 seq_printf(m, "\nQueryInfos: %d sent %d failed",
357 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
358 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
359 seq_printf(m, "\nSetInfos: %d sent %d failed",
360 atomic_read(&sent[SMB2_SET_INFO_HE]),
361 atomic_read(&failed[SMB2_SET_INFO_HE]));
362 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
363 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
364 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
365#endif
366}
367
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700368static void
369smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
370{
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700371 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700372 cfile->fid.persistent_fid = fid->persistent_fid;
373 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700374 smb2_set_oplock_level(cinode, oplock);
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700375 cinode->can_cache_brlcks = cinode->clientCanCacheAll;
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700376}
377
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400378static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700379smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
380 struct cifs_fid *fid)
381{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400382 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700383}
384
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700385static int
386smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
387 struct cifs_fid *fid)
388{
389 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
390}
391
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700392static unsigned int
393smb2_read_data_offset(char *buf)
394{
395 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
396 return rsp->DataOffset;
397}
398
399static unsigned int
400smb2_read_data_length(char *buf)
401{
402 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
403 return le32_to_cpu(rsp->DataLength);
404}
405
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700406
407static int
408smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
409 struct cifs_io_parms *parms, unsigned int *bytes_read,
410 char **buf, int *buf_type)
411{
412 parms->persistent_fid = cfile->fid.persistent_fid;
413 parms->volatile_fid = cfile->fid.volatile_fid;
414 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
415}
416
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700417static int
418smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
419 struct cifs_io_parms *parms, unsigned int *written,
420 struct kvec *iov, unsigned long nr_segs)
421{
422
423 parms->persistent_fid = cfile->fid.persistent_fid;
424 parms->volatile_fid = cfile->fid.volatile_fid;
425 return SMB2_write(xid, parms, written, iov, nr_segs);
426}
427
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700428static int
429smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
430 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
431{
432 __le64 eof = cpu_to_le64(size);
433 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
434 cfile->fid.volatile_fid, cfile->pid, &eof);
435}
436
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700437static int
438smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
439 const char *path, struct cifs_sb_info *cifs_sb,
440 struct cifs_fid *fid, __u16 search_flags,
441 struct cifs_search_info *srch_inf)
442{
443 __le16 *utf16_path;
444 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700445 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700446 __u64 persistent_fid, volatile_fid;
447
448 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
449 if (!utf16_path)
450 return -ENOMEM;
451
452 rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
453 FILE_READ_ATTRIBUTES | FILE_READ_DATA, FILE_OPEN, 0, 0,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700454 &oplock, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700455 kfree(utf16_path);
456 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500457 cifs_dbg(VFS, "open dir failed\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700458 return rc;
459 }
460
461 srch_inf->entries_in_buffer = 0;
462 srch_inf->index_of_last_entry = 0;
463 fid->persistent_fid = persistent_fid;
464 fid->volatile_fid = volatile_fid;
465
466 rc = SMB2_query_directory(xid, tcon, persistent_fid, volatile_fid, 0,
467 srch_inf);
468 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500469 cifs_dbg(VFS, "query directory failed\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700470 SMB2_close(xid, tcon, persistent_fid, volatile_fid);
471 }
472 return rc;
473}
474
475static int
476smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
477 struct cifs_fid *fid, __u16 search_flags,
478 struct cifs_search_info *srch_inf)
479{
480 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
481 fid->volatile_fid, 0, srch_inf);
482}
483
484static int
485smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
486 struct cifs_fid *fid)
487{
488 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
489}
490
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700491/*
492* If we negotiate SMB2 protocol and get STATUS_PENDING - update
493* the number of credits and return true. Otherwise - return false.
494*/
495static bool
496smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
497{
498 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
499
Steve French12e8a202012-09-19 09:19:39 -0700500 if (hdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700501 return false;
502
503 if (!length) {
504 spin_lock(&server->req_lock);
505 server->credits += le16_to_cpu(hdr->CreditRequest);
506 spin_unlock(&server->req_lock);
507 wake_up(&server->request_q);
508 }
509
510 return true;
511}
512
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700513static int
514smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
515 struct cifsInodeInfo *cinode)
516{
Pavel Shilovsky0822f512012-09-19 06:22:45 -0700517 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
518 return SMB2_lease_break(0, tcon, cinode->lease_key,
519 smb2_get_lease_state(cinode));
520
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700521 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
522 fid->volatile_fid,
523 cinode->clientCanCacheRead ? 1 : 0);
524}
525
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700526static int
527smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
528 struct kstatfs *buf)
529{
530 int rc;
531 u64 persistent_fid, volatile_fid;
532 __le16 srch_path = 0; /* Null - open root of share */
533 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
534
535 rc = SMB2_open(xid, tcon, &srch_path, &persistent_fid, &volatile_fid,
536 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, &oplock, NULL);
537 if (rc)
538 return rc;
539 buf->f_type = SMB2_MAGIC_NUMBER;
540 rc = SMB2_QFS_info(xid, tcon, persistent_fid, volatile_fid, buf);
541 SMB2_close(xid, tcon, persistent_fid, volatile_fid);
542 return rc;
543}
544
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -0700545static bool
546smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
547{
548 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
549 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
550}
551
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700552static int
553smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
554 __u64 length, __u32 type, int lock, int unlock, bool wait)
555{
556 if (unlock && !lock)
557 type = SMB2_LOCKFLAG_UNLOCK;
558 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
559 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
560 current->tgid, length, offset, type, wait);
561}
562
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700563static void
564smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
565{
566 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
567}
568
569static void
570smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
571{
572 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
573}
574
575static void
576smb2_new_lease_key(struct cifs_fid *fid)
577{
578 get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
579}
580
Steve French1080ef72011-02-24 18:07:19 +0000581struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -0700582 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400583 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +0400584 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400585 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400586 .add_credits = smb2_add_credits,
587 .set_credits = smb2_set_credits,
588 .get_credits_field = smb2_get_credits_field,
589 .get_credits = smb2_get_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400590 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700591 .read_data_offset = smb2_read_data_offset,
592 .read_data_length = smb2_read_data_length,
593 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400594 .find_mid = smb2_find_mid,
595 .check_message = smb2_check_message,
596 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400597 .clear_stats = smb2_clear_stats,
598 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700599 .is_oplock_break = smb2_is_valid_oplock_break,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400600 .need_neg = smb2_need_neg,
601 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700602 .negotiate_wsize = smb2_negotiate_wsize,
603 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +0400604 .sess_setup = SMB2_sess_setup,
605 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +0400606 .tree_connect = SMB2_tcon,
607 .tree_disconnect = SMB2_tdis,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400608 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400609 .can_echo = smb2_can_echo,
610 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400611 .query_path_info = smb2_query_path_info,
612 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700613 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700614 .set_path_size = smb2_set_path_size,
615 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -0700616 .set_file_info = smb2_set_file_info,
Pavel Shilovskya0e73182011-07-19 12:56:37 +0400617 .mkdir = smb2_mkdir,
618 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +0400619 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -0700620 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -0700621 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -0700622 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700623 .open = smb2_open_file,
624 .set_fid = smb2_set_fid,
625 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700626 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700627 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -0700628 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700629 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700630 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700631 .query_dir_first = smb2_query_dir_first,
632 .query_dir_next = smb2_query_dir_next,
633 .close_dir = smb2_close_dir,
634 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700635 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700636 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700637 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700638 .mand_lock = smb2_mand_lock,
639 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -0700640 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700641 .get_lease_key = smb2_get_lease_key,
642 .set_lease_key = smb2_set_lease_key,
643 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -0600644 .calc_signature = smb2_calc_signature,
645};
646
647
648struct smb_version_operations smb30_operations = {
649 .compare_fids = smb2_compare_fids,
650 .setup_request = smb2_setup_request,
651 .setup_async_request = smb2_setup_async_request,
652 .check_receive = smb2_check_receive,
653 .add_credits = smb2_add_credits,
654 .set_credits = smb2_set_credits,
655 .get_credits_field = smb2_get_credits_field,
656 .get_credits = smb2_get_credits,
657 .get_next_mid = smb2_get_next_mid,
658 .read_data_offset = smb2_read_data_offset,
659 .read_data_length = smb2_read_data_length,
660 .map_error = map_smb2_to_linux_error,
661 .find_mid = smb2_find_mid,
662 .check_message = smb2_check_message,
663 .dump_detail = smb2_dump_detail,
664 .clear_stats = smb2_clear_stats,
665 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -0500666 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -0600667 .is_oplock_break = smb2_is_valid_oplock_break,
668 .need_neg = smb2_need_neg,
669 .negotiate = smb2_negotiate,
670 .negotiate_wsize = smb2_negotiate_wsize,
671 .negotiate_rsize = smb2_negotiate_rsize,
672 .sess_setup = SMB2_sess_setup,
673 .logoff = SMB2_logoff,
674 .tree_connect = SMB2_tcon,
675 .tree_disconnect = SMB2_tdis,
676 .is_path_accessible = smb2_is_path_accessible,
677 .can_echo = smb2_can_echo,
678 .echo = SMB2_echo,
679 .query_path_info = smb2_query_path_info,
680 .get_srv_inum = smb2_get_srv_inum,
681 .query_file_info = smb2_query_file_info,
682 .set_path_size = smb2_set_path_size,
683 .set_file_size = smb2_set_file_size,
684 .set_file_info = smb2_set_file_info,
685 .mkdir = smb2_mkdir,
686 .mkdir_setinfo = smb2_mkdir_setinfo,
687 .rmdir = smb2_rmdir,
688 .unlink = smb2_unlink,
689 .rename = smb2_rename_path,
690 .create_hardlink = smb2_create_hardlink,
691 .open = smb2_open_file,
692 .set_fid = smb2_set_fid,
693 .close = smb2_close_file,
694 .flush = smb2_flush_file,
695 .async_readv = smb2_async_readv,
696 .async_writev = smb2_async_writev,
697 .sync_read = smb2_sync_read,
698 .sync_write = smb2_sync_write,
699 .query_dir_first = smb2_query_dir_first,
700 .query_dir_next = smb2_query_dir_next,
701 .close_dir = smb2_close_dir,
702 .calc_smb_size = smb2_calc_size,
703 .is_status_pending = smb2_is_status_pending,
704 .oplock_response = smb2_oplock_response,
705 .queryfs = smb2_queryfs,
706 .mand_lock = smb2_mand_lock,
707 .mand_unlock_range = smb2_unlock_range,
708 .push_mand_locks = smb2_push_mandatory_locks,
709 .get_lease_key = smb2_get_lease_key,
710 .set_lease_key = smb2_set_lease_key,
711 .new_lease_key = smb2_new_lease_key,
Steve Frenche65a5cb2013-06-27 01:06:50 -0500712 .generate_signingkey = generate_smb3signingkey,
Steve French38107d42012-12-08 22:08:06 -0600713 .calc_signature = smb3_calc_signature,
Steve French1080ef72011-02-24 18:07:19 +0000714};
715
Steve Frenchdd446b12012-11-28 23:21:06 -0600716struct smb_version_values smb20_values = {
717 .version_string = SMB20_VERSION_STRING,
718 .protocol_id = SMB20_PROT_ID,
719 .req_capabilities = 0, /* MBZ */
720 .large_lock_type = 0,
721 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
722 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
723 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
724 .header_size = sizeof(struct smb2_hdr),
725 .max_header_size = MAX_SMB2_HDR_SIZE,
726 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
727 .lock_cmd = SMB2_LOCK,
728 .cap_unix = 0,
729 .cap_nt_find = SMB2_NT_FIND,
730 .cap_large_files = SMB2_LARGE_FILES,
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400731 .oplock_read = SMB2_OPLOCK_LEVEL_II,
Jeff Layton50285882013-06-27 12:45:00 -0400732 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
733 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Steve Frenchdd446b12012-11-28 23:21:06 -0600734};
735
Steve French1080ef72011-02-24 18:07:19 +0000736struct smb_version_values smb21_values = {
737 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -0500738 .protocol_id = SMB21_PROT_ID,
739 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
740 .large_lock_type = 0,
741 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
742 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
743 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
744 .header_size = sizeof(struct smb2_hdr),
745 .max_header_size = MAX_SMB2_HDR_SIZE,
746 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
747 .lock_cmd = SMB2_LOCK,
748 .cap_unix = 0,
749 .cap_nt_find = SMB2_NT_FIND,
750 .cap_large_files = SMB2_LARGE_FILES,
Pavel Shilovsky63b7d3a2012-12-24 14:41:19 +0400751 .oplock_read = SMB2_OPLOCK_LEVEL_II,
Jeff Layton50285882013-06-27 12:45:00 -0400752 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
753 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Steve Frenche4aa25e2012-10-01 12:26:22 -0500754};
755
756struct smb_version_values smb30_values = {
757 .version_string = SMB30_VERSION_STRING,
758 .protocol_id = SMB30_PROT_ID,
759 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -0700760 .large_lock_type = 0,
761 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
762 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
763 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400764 .header_size = sizeof(struct smb2_hdr),
765 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700766 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400767 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +0400768 .cap_unix = 0,
769 .cap_nt_find = SMB2_NT_FIND,
770 .cap_large_files = SMB2_LARGE_FILES,
Pavel Shilovsky067785c2013-03-06 19:38:36 +0400771 .oplock_read = SMB2_OPLOCK_LEVEL_II,
Jeff Layton50285882013-06-27 12:45:00 -0400772 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
773 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Steve French1080ef72011-02-24 18:07:19 +0000774};
Steve French20b6d8b2013-06-12 22:48:41 -0500775
776struct smb_version_values smb302_values = {
777 .version_string = SMB302_VERSION_STRING,
778 .protocol_id = SMB302_PROT_ID,
779 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
780 .large_lock_type = 0,
781 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
782 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
783 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
784 .header_size = sizeof(struct smb2_hdr),
785 .max_header_size = MAX_SMB2_HDR_SIZE,
786 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
787 .lock_cmd = SMB2_LOCK,
788 .cap_unix = 0,
789 .cap_nt_find = SMB2_NT_FIND,
790 .cap_large_files = SMB2_LARGE_FILES,
791 .oplock_read = SMB2_OPLOCK_LEVEL_II,
Jeff Layton50285882013-06-27 12:45:00 -0400792 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
793 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Steve French20b6d8b2013-06-12 22:48:41 -0500794};