CIFS: Use multicredits for SMB 2.1/3 writes
[firefly-linux-kernel-4.4.55.git] / fs / cifs / smb2ops.c
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 <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include "cifsglob.h"
23 #include "smb2pdu.h"
24 #include "smb2proto.h"
25 #include "cifsproto.h"
26 #include "cifs_debug.h"
27 #include "cifs_unicode.h"
28 #include "smb2status.h"
29 #include "smb2glob.h"
30
31 static int
32 change_conf(struct TCP_Server_Info *server)
33 {
34         server->credits += server->echo_credits + server->oplock_credits;
35         server->oplock_credits = server->echo_credits = 0;
36         switch (server->credits) {
37         case 0:
38                 return -1;
39         case 1:
40                 server->echoes = false;
41                 server->oplocks = false;
42                 cifs_dbg(VFS, "disabling echoes and oplocks\n");
43                 break;
44         case 2:
45                 server->echoes = true;
46                 server->oplocks = false;
47                 server->echo_credits = 1;
48                 cifs_dbg(FYI, "disabling oplocks\n");
49                 break;
50         default:
51                 server->echoes = true;
52                 server->oplocks = true;
53                 server->echo_credits = 1;
54                 server->oplock_credits = 1;
55         }
56         server->credits -= server->echo_credits + server->oplock_credits;
57         return 0;
58 }
59
60 static void
61 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
62                  const int optype)
63 {
64         int *val, rc = 0;
65         spin_lock(&server->req_lock);
66         val = server->ops->get_credits_field(server, optype);
67         *val += add;
68         server->in_flight--;
69         if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
70                 rc = change_conf(server);
71         /*
72          * Sometimes server returns 0 credits on oplock break ack - we need to
73          * rebalance credits in this case.
74          */
75         else if (server->in_flight > 0 && server->oplock_credits == 0 &&
76                  server->oplocks) {
77                 if (server->credits > 1) {
78                         server->credits--;
79                         server->oplock_credits++;
80                 }
81         }
82         spin_unlock(&server->req_lock);
83         wake_up(&server->request_q);
84         if (rc)
85                 cifs_reconnect(server);
86 }
87
88 static void
89 smb2_set_credits(struct TCP_Server_Info *server, const int val)
90 {
91         spin_lock(&server->req_lock);
92         server->credits = val;
93         spin_unlock(&server->req_lock);
94 }
95
96 static int *
97 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
98 {
99         switch (optype) {
100         case CIFS_ECHO_OP:
101                 return &server->echo_credits;
102         case CIFS_OBREAK_OP:
103                 return &server->oplock_credits;
104         default:
105                 return &server->credits;
106         }
107 }
108
109 static unsigned int
110 smb2_get_credits(struct mid_q_entry *mid)
111 {
112         return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
113 }
114
115 static int
116 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
117                       unsigned int *num, unsigned int *credits)
118 {
119         int rc = 0;
120         unsigned int scredits;
121
122         spin_lock(&server->req_lock);
123         while (1) {
124                 if (server->credits <= 0) {
125                         spin_unlock(&server->req_lock);
126                         cifs_num_waiters_inc(server);
127                         rc = wait_event_killable(server->request_q,
128                                         has_credits(server, &server->credits));
129                         cifs_num_waiters_dec(server);
130                         if (rc)
131                                 return rc;
132                         spin_lock(&server->req_lock);
133                 } else {
134                         if (server->tcpStatus == CifsExiting) {
135                                 spin_unlock(&server->req_lock);
136                                 return -ENOENT;
137                         }
138
139                         scredits = server->credits;
140                         /* can deadlock with reopen */
141                         if (scredits == 1) {
142                                 *num = SMB2_MAX_BUFFER_SIZE;
143                                 *credits = 0;
144                                 break;
145                         }
146
147                         /* leave one credit for a possible reopen */
148                         scredits--;
149                         *num = min_t(unsigned int, size,
150                                      scredits * SMB2_MAX_BUFFER_SIZE);
151
152                         *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
153                         server->credits -= *credits;
154                         server->in_flight++;
155                         break;
156                 }
157         }
158         spin_unlock(&server->req_lock);
159         return rc;
160 }
161
162 static __u64
163 smb2_get_next_mid(struct TCP_Server_Info *server)
164 {
165         __u64 mid;
166         /* for SMB2 we need the current value */
167         spin_lock(&GlobalMid_Lock);
168         mid = server->CurrentMid++;
169         spin_unlock(&GlobalMid_Lock);
170         return mid;
171 }
172
173 static struct mid_q_entry *
174 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
175 {
176         struct mid_q_entry *mid;
177         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
178
179         spin_lock(&GlobalMid_Lock);
180         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
181                 if ((mid->mid == hdr->MessageId) &&
182                     (mid->mid_state == MID_REQUEST_SUBMITTED) &&
183                     (mid->command == hdr->Command)) {
184                         spin_unlock(&GlobalMid_Lock);
185                         return mid;
186                 }
187         }
188         spin_unlock(&GlobalMid_Lock);
189         return NULL;
190 }
191
192 static void
193 smb2_dump_detail(void *buf)
194 {
195 #ifdef CONFIG_CIFS_DEBUG2
196         struct smb2_hdr *smb = (struct smb2_hdr *)buf;
197
198         cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
199                  smb->Command, smb->Status, smb->Flags, smb->MessageId,
200                  smb->ProcessId);
201         cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
202 #endif
203 }
204
205 static bool
206 smb2_need_neg(struct TCP_Server_Info *server)
207 {
208         return server->max_read == 0;
209 }
210
211 static int
212 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
213 {
214         int rc;
215         ses->server->CurrentMid = 0;
216         rc = SMB2_negotiate(xid, ses);
217         /* BB we probably don't need to retry with modern servers */
218         if (rc == -EAGAIN)
219                 rc = -EHOSTDOWN;
220         return rc;
221 }
222
223 static unsigned int
224 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
225 {
226         struct TCP_Server_Info *server = tcon->ses->server;
227         unsigned int wsize;
228
229         /* start with specified wsize, or default */
230         wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
231         wsize = min_t(unsigned int, wsize, server->max_write);
232
233         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
234                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
235
236         return wsize;
237 }
238
239 static unsigned int
240 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
241 {
242         struct TCP_Server_Info *server = tcon->ses->server;
243         unsigned int rsize;
244
245         /* start with specified rsize, or default */
246         rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
247         rsize = min_t(unsigned int, rsize, server->max_read);
248         /* set it to the maximum buffer size value we can send with 1 credit */
249         rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
250
251         return rsize;
252 }
253
254 #ifdef CONFIG_CIFS_STATS2
255 static int
256 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
257 {
258         int rc;
259         unsigned int ret_data_len = 0;
260         struct network_interface_info_ioctl_rsp *out_buf;
261
262         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
263                         FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
264                         NULL /* no data input */, 0 /* no data input */,
265                         (char **)&out_buf, &ret_data_len);
266
267         if ((rc == 0)  && (ret_data_len > 0)) {
268                 /* Dump info on first interface */
269                 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
270                         le32_to_cpu(out_buf->Capability));
271                 cifs_dbg(FYI, "Link Speed %lld\n",
272                         le64_to_cpu(out_buf->LinkSpeed));
273         } else
274                 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
275
276         return rc;
277 }
278 #endif /* STATS2 */
279
280 static void
281 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
282 {
283         int rc;
284         __le16 srch_path = 0; /* Null - open root of share */
285         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
286         struct cifs_open_parms oparms;
287         struct cifs_fid fid;
288
289         oparms.tcon = tcon;
290         oparms.desired_access = FILE_READ_ATTRIBUTES;
291         oparms.disposition = FILE_OPEN;
292         oparms.create_options = 0;
293         oparms.fid = &fid;
294         oparms.reconnect = false;
295
296         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
297         if (rc)
298                 return;
299
300 #ifdef CONFIG_CIFS_STATS2
301         SMB3_request_interfaces(xid, tcon);
302 #endif /* STATS2 */
303
304         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
305                         FS_ATTRIBUTE_INFORMATION);
306         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
307                         FS_DEVICE_INFORMATION);
308         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
309                         FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
310         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
311         return;
312 }
313
314 static void
315 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
316 {
317         int rc;
318         __le16 srch_path = 0; /* Null - open root of share */
319         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
320         struct cifs_open_parms oparms;
321         struct cifs_fid fid;
322
323         oparms.tcon = tcon;
324         oparms.desired_access = FILE_READ_ATTRIBUTES;
325         oparms.disposition = FILE_OPEN;
326         oparms.create_options = 0;
327         oparms.fid = &fid;
328         oparms.reconnect = false;
329
330         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
331         if (rc)
332                 return;
333
334         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
335                         FS_ATTRIBUTE_INFORMATION);
336         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
337                         FS_DEVICE_INFORMATION);
338         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
339         return;
340 }
341
342 static int
343 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
344                         struct cifs_sb_info *cifs_sb, const char *full_path)
345 {
346         int rc;
347         __le16 *utf16_path;
348         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
349         struct cifs_open_parms oparms;
350         struct cifs_fid fid;
351
352         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
353         if (!utf16_path)
354                 return -ENOMEM;
355
356         oparms.tcon = tcon;
357         oparms.desired_access = FILE_READ_ATTRIBUTES;
358         oparms.disposition = FILE_OPEN;
359         oparms.create_options = 0;
360         oparms.fid = &fid;
361         oparms.reconnect = false;
362
363         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
364         if (rc) {
365                 kfree(utf16_path);
366                 return rc;
367         }
368
369         rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
370         kfree(utf16_path);
371         return rc;
372 }
373
374 static int
375 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
376                   struct cifs_sb_info *cifs_sb, const char *full_path,
377                   u64 *uniqueid, FILE_ALL_INFO *data)
378 {
379         *uniqueid = le64_to_cpu(data->IndexNumber);
380         return 0;
381 }
382
383 static int
384 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
385                      struct cifs_fid *fid, FILE_ALL_INFO *data)
386 {
387         int rc;
388         struct smb2_file_all_info *smb2_data;
389
390         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
391                             GFP_KERNEL);
392         if (smb2_data == NULL)
393                 return -ENOMEM;
394
395         rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
396                              smb2_data);
397         if (!rc)
398                 move_smb2_info_to_cifs(data, smb2_data);
399         kfree(smb2_data);
400         return rc;
401 }
402
403 static bool
404 smb2_can_echo(struct TCP_Server_Info *server)
405 {
406         return server->echoes;
407 }
408
409 static void
410 smb2_clear_stats(struct cifs_tcon *tcon)
411 {
412 #ifdef CONFIG_CIFS_STATS
413         int i;
414         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
415                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
416                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
417         }
418 #endif
419 }
420
421 static void
422 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
423 {
424         seq_puts(m, "\n\tShare Capabilities:");
425         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
426                 seq_puts(m, " DFS,");
427         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
428                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
429         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
430                 seq_puts(m, " SCALEOUT,");
431         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
432                 seq_puts(m, " CLUSTER,");
433         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
434                 seq_puts(m, " ASYMMETRIC,");
435         if (tcon->capabilities == 0)
436                 seq_puts(m, " None");
437         if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
438                 seq_puts(m, " Aligned,");
439         if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
440                 seq_puts(m, " Partition Aligned,");
441         if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
442                 seq_puts(m, " SSD,");
443         if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
444                 seq_puts(m, " TRIM-support,");
445
446         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
447         if (tcon->perf_sector_size)
448                 seq_printf(m, "\tOptimal sector size: 0x%x",
449                            tcon->perf_sector_size);
450 }
451
452 static void
453 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
454 {
455 #ifdef CONFIG_CIFS_STATS
456         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
457         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
458         seq_printf(m, "\nNegotiates: %d sent %d failed",
459                    atomic_read(&sent[SMB2_NEGOTIATE_HE]),
460                    atomic_read(&failed[SMB2_NEGOTIATE_HE]));
461         seq_printf(m, "\nSessionSetups: %d sent %d failed",
462                    atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
463                    atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
464         seq_printf(m, "\nLogoffs: %d sent %d failed",
465                    atomic_read(&sent[SMB2_LOGOFF_HE]),
466                    atomic_read(&failed[SMB2_LOGOFF_HE]));
467         seq_printf(m, "\nTreeConnects: %d sent %d failed",
468                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
469                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
470         seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
471                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
472                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
473         seq_printf(m, "\nCreates: %d sent %d failed",
474                    atomic_read(&sent[SMB2_CREATE_HE]),
475                    atomic_read(&failed[SMB2_CREATE_HE]));
476         seq_printf(m, "\nCloses: %d sent %d failed",
477                    atomic_read(&sent[SMB2_CLOSE_HE]),
478                    atomic_read(&failed[SMB2_CLOSE_HE]));
479         seq_printf(m, "\nFlushes: %d sent %d failed",
480                    atomic_read(&sent[SMB2_FLUSH_HE]),
481                    atomic_read(&failed[SMB2_FLUSH_HE]));
482         seq_printf(m, "\nReads: %d sent %d failed",
483                    atomic_read(&sent[SMB2_READ_HE]),
484                    atomic_read(&failed[SMB2_READ_HE]));
485         seq_printf(m, "\nWrites: %d sent %d failed",
486                    atomic_read(&sent[SMB2_WRITE_HE]),
487                    atomic_read(&failed[SMB2_WRITE_HE]));
488         seq_printf(m, "\nLocks: %d sent %d failed",
489                    atomic_read(&sent[SMB2_LOCK_HE]),
490                    atomic_read(&failed[SMB2_LOCK_HE]));
491         seq_printf(m, "\nIOCTLs: %d sent %d failed",
492                    atomic_read(&sent[SMB2_IOCTL_HE]),
493                    atomic_read(&failed[SMB2_IOCTL_HE]));
494         seq_printf(m, "\nCancels: %d sent %d failed",
495                    atomic_read(&sent[SMB2_CANCEL_HE]),
496                    atomic_read(&failed[SMB2_CANCEL_HE]));
497         seq_printf(m, "\nEchos: %d sent %d failed",
498                    atomic_read(&sent[SMB2_ECHO_HE]),
499                    atomic_read(&failed[SMB2_ECHO_HE]));
500         seq_printf(m, "\nQueryDirectories: %d sent %d failed",
501                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
502                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
503         seq_printf(m, "\nChangeNotifies: %d sent %d failed",
504                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
505                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
506         seq_printf(m, "\nQueryInfos: %d sent %d failed",
507                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
508                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
509         seq_printf(m, "\nSetInfos: %d sent %d failed",
510                    atomic_read(&sent[SMB2_SET_INFO_HE]),
511                    atomic_read(&failed[SMB2_SET_INFO_HE]));
512         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
513                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
514                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
515 #endif
516 }
517
518 static void
519 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
520 {
521         struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
522         struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
523
524         cfile->fid.persistent_fid = fid->persistent_fid;
525         cfile->fid.volatile_fid = fid->volatile_fid;
526         server->ops->set_oplock_level(cinode, oplock, fid->epoch,
527                                       &fid->purge_cache);
528         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
529 }
530
531 static void
532 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
533                 struct cifs_fid *fid)
534 {
535         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
536 }
537
538 static int
539 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
540                      u64 persistent_fid, u64 volatile_fid,
541                      struct copychunk_ioctl *pcchunk)
542 {
543         int rc;
544         unsigned int ret_data_len;
545         struct resume_key_req *res_key;
546
547         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
548                         FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
549                         NULL, 0 /* no input */,
550                         (char **)&res_key, &ret_data_len);
551
552         if (rc) {
553                 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
554                 goto req_res_key_exit;
555         }
556         if (ret_data_len < sizeof(struct resume_key_req)) {
557                 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
558                 rc = -EINVAL;
559                 goto req_res_key_exit;
560         }
561         memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
562
563 req_res_key_exit:
564         kfree(res_key);
565         return rc;
566 }
567
568 static int
569 smb2_clone_range(const unsigned int xid,
570                         struct cifsFileInfo *srcfile,
571                         struct cifsFileInfo *trgtfile, u64 src_off,
572                         u64 len, u64 dest_off)
573 {
574         int rc;
575         unsigned int ret_data_len;
576         struct copychunk_ioctl *pcchunk;
577         struct copychunk_ioctl_rsp *retbuf = NULL;
578         struct cifs_tcon *tcon;
579         int chunks_copied = 0;
580         bool chunk_sizes_updated = false;
581
582         pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
583
584         if (pcchunk == NULL)
585                 return -ENOMEM;
586
587         cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
588         /* Request a key from the server to identify the source of the copy */
589         rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
590                                 srcfile->fid.persistent_fid,
591                                 srcfile->fid.volatile_fid, pcchunk);
592
593         /* Note: request_res_key sets res_key null only if rc !=0 */
594         if (rc)
595                 goto cchunk_out;
596
597         /* For now array only one chunk long, will make more flexible later */
598         pcchunk->ChunkCount = __constant_cpu_to_le32(1);
599         pcchunk->Reserved = 0;
600         pcchunk->Reserved2 = 0;
601
602         tcon = tlink_tcon(trgtfile->tlink);
603
604         while (len > 0) {
605                 pcchunk->SourceOffset = cpu_to_le64(src_off);
606                 pcchunk->TargetOffset = cpu_to_le64(dest_off);
607                 pcchunk->Length =
608                         cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
609
610                 /* Request server copy to target from src identified by key */
611                 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
612                         trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
613                         true /* is_fsctl */, (char *)pcchunk,
614                         sizeof(struct copychunk_ioctl), (char **)&retbuf,
615                         &ret_data_len);
616                 if (rc == 0) {
617                         if (ret_data_len !=
618                                         sizeof(struct copychunk_ioctl_rsp)) {
619                                 cifs_dbg(VFS, "invalid cchunk response size\n");
620                                 rc = -EIO;
621                                 goto cchunk_out;
622                         }
623                         if (retbuf->TotalBytesWritten == 0) {
624                                 cifs_dbg(FYI, "no bytes copied\n");
625                                 rc = -EIO;
626                                 goto cchunk_out;
627                         }
628                         /*
629                          * Check if server claimed to write more than we asked
630                          */
631                         if (le32_to_cpu(retbuf->TotalBytesWritten) >
632                             le32_to_cpu(pcchunk->Length)) {
633                                 cifs_dbg(VFS, "invalid copy chunk response\n");
634                                 rc = -EIO;
635                                 goto cchunk_out;
636                         }
637                         if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
638                                 cifs_dbg(VFS, "invalid num chunks written\n");
639                                 rc = -EIO;
640                                 goto cchunk_out;
641                         }
642                         chunks_copied++;
643
644                         src_off += le32_to_cpu(retbuf->TotalBytesWritten);
645                         dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
646                         len -= le32_to_cpu(retbuf->TotalBytesWritten);
647
648                         cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
649                                 le32_to_cpu(retbuf->ChunksWritten),
650                                 le32_to_cpu(retbuf->ChunkBytesWritten),
651                                 le32_to_cpu(retbuf->TotalBytesWritten));
652                 } else if (rc == -EINVAL) {
653                         if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
654                                 goto cchunk_out;
655
656                         cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
657                                 le32_to_cpu(retbuf->ChunksWritten),
658                                 le32_to_cpu(retbuf->ChunkBytesWritten),
659                                 le32_to_cpu(retbuf->TotalBytesWritten));
660
661                         /*
662                          * Check if this is the first request using these sizes,
663                          * (ie check if copy succeed once with original sizes
664                          * and check if the server gave us different sizes after
665                          * we already updated max sizes on previous request).
666                          * if not then why is the server returning an error now
667                          */
668                         if ((chunks_copied != 0) || chunk_sizes_updated)
669                                 goto cchunk_out;
670
671                         /* Check that server is not asking us to grow size */
672                         if (le32_to_cpu(retbuf->ChunkBytesWritten) <
673                                         tcon->max_bytes_chunk)
674                                 tcon->max_bytes_chunk =
675                                         le32_to_cpu(retbuf->ChunkBytesWritten);
676                         else
677                                 goto cchunk_out; /* server gave us bogus size */
678
679                         /* No need to change MaxChunks since already set to 1 */
680                         chunk_sizes_updated = true;
681                 }
682         }
683
684 cchunk_out:
685         kfree(pcchunk);
686         return rc;
687 }
688
689 static int
690 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
691                 struct cifs_fid *fid)
692 {
693         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
694 }
695
696 static unsigned int
697 smb2_read_data_offset(char *buf)
698 {
699         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
700         return rsp->DataOffset;
701 }
702
703 static unsigned int
704 smb2_read_data_length(char *buf)
705 {
706         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
707         return le32_to_cpu(rsp->DataLength);
708 }
709
710
711 static int
712 smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
713                struct cifs_io_parms *parms, unsigned int *bytes_read,
714                char **buf, int *buf_type)
715 {
716         parms->persistent_fid = cfile->fid.persistent_fid;
717         parms->volatile_fid = cfile->fid.volatile_fid;
718         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
719 }
720
721 static int
722 smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
723                 struct cifs_io_parms *parms, unsigned int *written,
724                 struct kvec *iov, unsigned long nr_segs)
725 {
726
727         parms->persistent_fid = cfile->fid.persistent_fid;
728         parms->volatile_fid = cfile->fid.volatile_fid;
729         return SMB2_write(xid, parms, written, iov, nr_segs);
730 }
731
732 static int
733 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
734                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
735 {
736         __le64 eof = cpu_to_le64(size);
737         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
738                             cfile->fid.volatile_fid, cfile->pid, &eof);
739 }
740
741 static int
742 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
743                    struct cifsFileInfo *cfile)
744 {
745         return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
746                             cfile->fid.volatile_fid);
747 }
748
749 static int
750 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
751                      const char *path, struct cifs_sb_info *cifs_sb,
752                      struct cifs_fid *fid, __u16 search_flags,
753                      struct cifs_search_info *srch_inf)
754 {
755         __le16 *utf16_path;
756         int rc;
757         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
758         struct cifs_open_parms oparms;
759
760         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
761         if (!utf16_path)
762                 return -ENOMEM;
763
764         oparms.tcon = tcon;
765         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
766         oparms.disposition = FILE_OPEN;
767         oparms.create_options = 0;
768         oparms.fid = fid;
769         oparms.reconnect = false;
770
771         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
772         kfree(utf16_path);
773         if (rc) {
774                 cifs_dbg(VFS, "open dir failed\n");
775                 return rc;
776         }
777
778         srch_inf->entries_in_buffer = 0;
779         srch_inf->index_of_last_entry = 0;
780
781         rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
782                                   fid->volatile_fid, 0, srch_inf);
783         if (rc) {
784                 cifs_dbg(VFS, "query directory failed\n");
785                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
786         }
787         return rc;
788 }
789
790 static int
791 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
792                     struct cifs_fid *fid, __u16 search_flags,
793                     struct cifs_search_info *srch_inf)
794 {
795         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
796                                     fid->volatile_fid, 0, srch_inf);
797 }
798
799 static int
800 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
801                struct cifs_fid *fid)
802 {
803         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
804 }
805
806 /*
807 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
808 * the number of credits and return true. Otherwise - return false.
809 */
810 static bool
811 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
812 {
813         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
814
815         if (hdr->Status != STATUS_PENDING)
816                 return false;
817
818         if (!length) {
819                 spin_lock(&server->req_lock);
820                 server->credits += le16_to_cpu(hdr->CreditRequest);
821                 spin_unlock(&server->req_lock);
822                 wake_up(&server->request_q);
823         }
824
825         return true;
826 }
827
828 static int
829 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
830                      struct cifsInodeInfo *cinode)
831 {
832         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
833                 return SMB2_lease_break(0, tcon, cinode->lease_key,
834                                         smb2_get_lease_state(cinode));
835
836         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
837                                  fid->volatile_fid,
838                                  CIFS_CACHE_READ(cinode) ? 1 : 0);
839 }
840
841 static int
842 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
843              struct kstatfs *buf)
844 {
845         int rc;
846         __le16 srch_path = 0; /* Null - open root of share */
847         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
848         struct cifs_open_parms oparms;
849         struct cifs_fid fid;
850
851         oparms.tcon = tcon;
852         oparms.desired_access = FILE_READ_ATTRIBUTES;
853         oparms.disposition = FILE_OPEN;
854         oparms.create_options = 0;
855         oparms.fid = &fid;
856         oparms.reconnect = false;
857
858         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
859         if (rc)
860                 return rc;
861         buf->f_type = SMB2_MAGIC_NUMBER;
862         rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
863                            buf);
864         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
865         return rc;
866 }
867
868 static bool
869 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
870 {
871         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
872                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
873 }
874
875 static int
876 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
877                __u64 length, __u32 type, int lock, int unlock, bool wait)
878 {
879         if (unlock && !lock)
880                 type = SMB2_LOCKFLAG_UNLOCK;
881         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
882                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
883                          current->tgid, length, offset, type, wait);
884 }
885
886 static void
887 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
888 {
889         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
890 }
891
892 static void
893 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
894 {
895         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
896 }
897
898 static void
899 smb2_new_lease_key(struct cifs_fid *fid)
900 {
901         get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
902 }
903
904 static int
905 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
906                    const char *full_path, char **target_path,
907                    struct cifs_sb_info *cifs_sb)
908 {
909         int rc;
910         __le16 *utf16_path;
911         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
912         struct cifs_open_parms oparms;
913         struct cifs_fid fid;
914         struct smb2_err_rsp *err_buf = NULL;
915         struct smb2_symlink_err_rsp *symlink;
916         unsigned int sub_len, sub_offset;
917
918         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
919
920         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
921         if (!utf16_path)
922                 return -ENOMEM;
923
924         oparms.tcon = tcon;
925         oparms.desired_access = FILE_READ_ATTRIBUTES;
926         oparms.disposition = FILE_OPEN;
927         oparms.create_options = 0;
928         oparms.fid = &fid;
929         oparms.reconnect = false;
930
931         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
932
933         if (!rc || !err_buf) {
934                 kfree(utf16_path);
935                 return -ENOENT;
936         }
937         /* open must fail on symlink - reset rc */
938         rc = 0;
939         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
940         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
941         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
942         *target_path = cifs_strndup_from_utf16(
943                                 (char *)symlink->PathBuffer + sub_offset,
944                                 sub_len, true, cifs_sb->local_nls);
945         if (!(*target_path)) {
946                 kfree(utf16_path);
947                 return -ENOMEM;
948         }
949         convert_delimiter(*target_path, '/');
950         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
951         kfree(utf16_path);
952         return rc;
953 }
954
955 static void
956 smb2_downgrade_oplock(struct TCP_Server_Info *server,
957                         struct cifsInodeInfo *cinode, bool set_level2)
958 {
959         if (set_level2)
960                 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
961                                                 0, NULL);
962         else
963                 server->ops->set_oplock_level(cinode, 0, 0, NULL);
964 }
965
966 static void
967 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
968                       unsigned int epoch, bool *purge_cache)
969 {
970         oplock &= 0xFF;
971         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
972                 return;
973         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
974                 cinode->oplock = CIFS_CACHE_RHW_FLG;
975                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
976                          &cinode->vfs_inode);
977         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
978                 cinode->oplock = CIFS_CACHE_RW_FLG;
979                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
980                          &cinode->vfs_inode);
981         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
982                 cinode->oplock = CIFS_CACHE_READ_FLG;
983                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
984                          &cinode->vfs_inode);
985         } else
986                 cinode->oplock = 0;
987 }
988
989 static void
990 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
991                        unsigned int epoch, bool *purge_cache)
992 {
993         char message[5] = {0};
994
995         oplock &= 0xFF;
996         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
997                 return;
998
999         cinode->oplock = 0;
1000         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1001                 cinode->oplock |= CIFS_CACHE_READ_FLG;
1002                 strcat(message, "R");
1003         }
1004         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1005                 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1006                 strcat(message, "H");
1007         }
1008         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1009                 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1010                 strcat(message, "W");
1011         }
1012         if (!cinode->oplock)
1013                 strcat(message, "None");
1014         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1015                  &cinode->vfs_inode);
1016 }
1017
1018 static void
1019 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1020                       unsigned int epoch, bool *purge_cache)
1021 {
1022         unsigned int old_oplock = cinode->oplock;
1023
1024         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1025
1026         if (purge_cache) {
1027                 *purge_cache = false;
1028                 if (old_oplock == CIFS_CACHE_READ_FLG) {
1029                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1030                             (epoch - cinode->epoch > 0))
1031                                 *purge_cache = true;
1032                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1033                                  (epoch - cinode->epoch > 1))
1034                                 *purge_cache = true;
1035                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1036                                  (epoch - cinode->epoch > 1))
1037                                 *purge_cache = true;
1038                         else if (cinode->oplock == 0 &&
1039                                  (epoch - cinode->epoch > 0))
1040                                 *purge_cache = true;
1041                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1042                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1043                             (epoch - cinode->epoch > 0))
1044                                 *purge_cache = true;
1045                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1046                                  (epoch - cinode->epoch > 1))
1047                                 *purge_cache = true;
1048                 }
1049                 cinode->epoch = epoch;
1050         }
1051 }
1052
1053 static bool
1054 smb2_is_read_op(__u32 oplock)
1055 {
1056         return oplock == SMB2_OPLOCK_LEVEL_II;
1057 }
1058
1059 static bool
1060 smb21_is_read_op(__u32 oplock)
1061 {
1062         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1063                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1064 }
1065
1066 static __le32
1067 map_oplock_to_lease(u8 oplock)
1068 {
1069         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1070                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1071         else if (oplock == SMB2_OPLOCK_LEVEL_II)
1072                 return SMB2_LEASE_READ_CACHING;
1073         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1074                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1075                        SMB2_LEASE_WRITE_CACHING;
1076         return 0;
1077 }
1078
1079 static char *
1080 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1081 {
1082         struct create_lease *buf;
1083
1084         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1085         if (!buf)
1086                 return NULL;
1087
1088         buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1089         buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1090         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1091
1092         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1093                                         (struct create_lease, lcontext));
1094         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1095         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1096                                 (struct create_lease, Name));
1097         buf->ccontext.NameLength = cpu_to_le16(4);
1098         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1099         buf->Name[0] = 'R';
1100         buf->Name[1] = 'q';
1101         buf->Name[2] = 'L';
1102         buf->Name[3] = 's';
1103         return (char *)buf;
1104 }
1105
1106 static char *
1107 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1108 {
1109         struct create_lease_v2 *buf;
1110
1111         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1112         if (!buf)
1113                 return NULL;
1114
1115         buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1116         buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1117         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1118
1119         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1120                                         (struct create_lease_v2, lcontext));
1121         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1122         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1123                                 (struct create_lease_v2, Name));
1124         buf->ccontext.NameLength = cpu_to_le16(4);
1125         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1126         buf->Name[0] = 'R';
1127         buf->Name[1] = 'q';
1128         buf->Name[2] = 'L';
1129         buf->Name[3] = 's';
1130         return (char *)buf;
1131 }
1132
1133 static __u8
1134 smb2_parse_lease_buf(void *buf, unsigned int *epoch)
1135 {
1136         struct create_lease *lc = (struct create_lease *)buf;
1137
1138         *epoch = 0; /* not used */
1139         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1140                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1141         return le32_to_cpu(lc->lcontext.LeaseState);
1142 }
1143
1144 static __u8
1145 smb3_parse_lease_buf(void *buf, unsigned int *epoch)
1146 {
1147         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1148
1149         *epoch = le16_to_cpu(lc->lcontext.Epoch);
1150         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1151                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1152         return le32_to_cpu(lc->lcontext.LeaseState);
1153 }
1154
1155 static unsigned int
1156 smb2_wp_retry_size(struct inode *inode)
1157 {
1158         return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1159                      SMB2_MAX_BUFFER_SIZE);
1160 }
1161
1162 struct smb_version_operations smb20_operations = {
1163         .compare_fids = smb2_compare_fids,
1164         .setup_request = smb2_setup_request,
1165         .setup_async_request = smb2_setup_async_request,
1166         .check_receive = smb2_check_receive,
1167         .add_credits = smb2_add_credits,
1168         .set_credits = smb2_set_credits,
1169         .get_credits_field = smb2_get_credits_field,
1170         .get_credits = smb2_get_credits,
1171         .wait_mtu_credits = cifs_wait_mtu_credits,
1172         .get_next_mid = smb2_get_next_mid,
1173         .read_data_offset = smb2_read_data_offset,
1174         .read_data_length = smb2_read_data_length,
1175         .map_error = map_smb2_to_linux_error,
1176         .find_mid = smb2_find_mid,
1177         .check_message = smb2_check_message,
1178         .dump_detail = smb2_dump_detail,
1179         .clear_stats = smb2_clear_stats,
1180         .print_stats = smb2_print_stats,
1181         .is_oplock_break = smb2_is_valid_oplock_break,
1182         .downgrade_oplock = smb2_downgrade_oplock,
1183         .need_neg = smb2_need_neg,
1184         .negotiate = smb2_negotiate,
1185         .negotiate_wsize = smb2_negotiate_wsize,
1186         .negotiate_rsize = smb2_negotiate_rsize,
1187         .sess_setup = SMB2_sess_setup,
1188         .logoff = SMB2_logoff,
1189         .tree_connect = SMB2_tcon,
1190         .tree_disconnect = SMB2_tdis,
1191         .qfs_tcon = smb2_qfs_tcon,
1192         .is_path_accessible = smb2_is_path_accessible,
1193         .can_echo = smb2_can_echo,
1194         .echo = SMB2_echo,
1195         .query_path_info = smb2_query_path_info,
1196         .get_srv_inum = smb2_get_srv_inum,
1197         .query_file_info = smb2_query_file_info,
1198         .set_path_size = smb2_set_path_size,
1199         .set_file_size = smb2_set_file_size,
1200         .set_file_info = smb2_set_file_info,
1201         .set_compression = smb2_set_compression,
1202         .mkdir = smb2_mkdir,
1203         .mkdir_setinfo = smb2_mkdir_setinfo,
1204         .rmdir = smb2_rmdir,
1205         .unlink = smb2_unlink,
1206         .rename = smb2_rename_path,
1207         .create_hardlink = smb2_create_hardlink,
1208         .query_symlink = smb2_query_symlink,
1209         .open = smb2_open_file,
1210         .set_fid = smb2_set_fid,
1211         .close = smb2_close_file,
1212         .flush = smb2_flush_file,
1213         .async_readv = smb2_async_readv,
1214         .async_writev = smb2_async_writev,
1215         .sync_read = smb2_sync_read,
1216         .sync_write = smb2_sync_write,
1217         .query_dir_first = smb2_query_dir_first,
1218         .query_dir_next = smb2_query_dir_next,
1219         .close_dir = smb2_close_dir,
1220         .calc_smb_size = smb2_calc_size,
1221         .is_status_pending = smb2_is_status_pending,
1222         .oplock_response = smb2_oplock_response,
1223         .queryfs = smb2_queryfs,
1224         .mand_lock = smb2_mand_lock,
1225         .mand_unlock_range = smb2_unlock_range,
1226         .push_mand_locks = smb2_push_mandatory_locks,
1227         .get_lease_key = smb2_get_lease_key,
1228         .set_lease_key = smb2_set_lease_key,
1229         .new_lease_key = smb2_new_lease_key,
1230         .calc_signature = smb2_calc_signature,
1231         .is_read_op = smb2_is_read_op,
1232         .set_oplock_level = smb2_set_oplock_level,
1233         .create_lease_buf = smb2_create_lease_buf,
1234         .parse_lease_buf = smb2_parse_lease_buf,
1235         .clone_range = smb2_clone_range,
1236         .wp_retry_size = smb2_wp_retry_size,
1237 };
1238
1239 struct smb_version_operations smb21_operations = {
1240         .compare_fids = smb2_compare_fids,
1241         .setup_request = smb2_setup_request,
1242         .setup_async_request = smb2_setup_async_request,
1243         .check_receive = smb2_check_receive,
1244         .add_credits = smb2_add_credits,
1245         .set_credits = smb2_set_credits,
1246         .get_credits_field = smb2_get_credits_field,
1247         .get_credits = smb2_get_credits,
1248         .wait_mtu_credits = smb2_wait_mtu_credits,
1249         .get_next_mid = smb2_get_next_mid,
1250         .read_data_offset = smb2_read_data_offset,
1251         .read_data_length = smb2_read_data_length,
1252         .map_error = map_smb2_to_linux_error,
1253         .find_mid = smb2_find_mid,
1254         .check_message = smb2_check_message,
1255         .dump_detail = smb2_dump_detail,
1256         .clear_stats = smb2_clear_stats,
1257         .print_stats = smb2_print_stats,
1258         .is_oplock_break = smb2_is_valid_oplock_break,
1259         .downgrade_oplock = smb2_downgrade_oplock,
1260         .need_neg = smb2_need_neg,
1261         .negotiate = smb2_negotiate,
1262         .negotiate_wsize = smb2_negotiate_wsize,
1263         .negotiate_rsize = smb2_negotiate_rsize,
1264         .sess_setup = SMB2_sess_setup,
1265         .logoff = SMB2_logoff,
1266         .tree_connect = SMB2_tcon,
1267         .tree_disconnect = SMB2_tdis,
1268         .qfs_tcon = smb2_qfs_tcon,
1269         .is_path_accessible = smb2_is_path_accessible,
1270         .can_echo = smb2_can_echo,
1271         .echo = SMB2_echo,
1272         .query_path_info = smb2_query_path_info,
1273         .get_srv_inum = smb2_get_srv_inum,
1274         .query_file_info = smb2_query_file_info,
1275         .set_path_size = smb2_set_path_size,
1276         .set_file_size = smb2_set_file_size,
1277         .set_file_info = smb2_set_file_info,
1278         .set_compression = smb2_set_compression,
1279         .mkdir = smb2_mkdir,
1280         .mkdir_setinfo = smb2_mkdir_setinfo,
1281         .rmdir = smb2_rmdir,
1282         .unlink = smb2_unlink,
1283         .rename = smb2_rename_path,
1284         .create_hardlink = smb2_create_hardlink,
1285         .query_symlink = smb2_query_symlink,
1286         .open = smb2_open_file,
1287         .set_fid = smb2_set_fid,
1288         .close = smb2_close_file,
1289         .flush = smb2_flush_file,
1290         .async_readv = smb2_async_readv,
1291         .async_writev = smb2_async_writev,
1292         .sync_read = smb2_sync_read,
1293         .sync_write = smb2_sync_write,
1294         .query_dir_first = smb2_query_dir_first,
1295         .query_dir_next = smb2_query_dir_next,
1296         .close_dir = smb2_close_dir,
1297         .calc_smb_size = smb2_calc_size,
1298         .is_status_pending = smb2_is_status_pending,
1299         .oplock_response = smb2_oplock_response,
1300         .queryfs = smb2_queryfs,
1301         .mand_lock = smb2_mand_lock,
1302         .mand_unlock_range = smb2_unlock_range,
1303         .push_mand_locks = smb2_push_mandatory_locks,
1304         .get_lease_key = smb2_get_lease_key,
1305         .set_lease_key = smb2_set_lease_key,
1306         .new_lease_key = smb2_new_lease_key,
1307         .calc_signature = smb2_calc_signature,
1308         .is_read_op = smb21_is_read_op,
1309         .set_oplock_level = smb21_set_oplock_level,
1310         .create_lease_buf = smb2_create_lease_buf,
1311         .parse_lease_buf = smb2_parse_lease_buf,
1312         .clone_range = smb2_clone_range,
1313         .wp_retry_size = smb2_wp_retry_size,
1314 };
1315
1316 struct smb_version_operations smb30_operations = {
1317         .compare_fids = smb2_compare_fids,
1318         .setup_request = smb2_setup_request,
1319         .setup_async_request = smb2_setup_async_request,
1320         .check_receive = smb2_check_receive,
1321         .add_credits = smb2_add_credits,
1322         .set_credits = smb2_set_credits,
1323         .get_credits_field = smb2_get_credits_field,
1324         .get_credits = smb2_get_credits,
1325         .wait_mtu_credits = smb2_wait_mtu_credits,
1326         .get_next_mid = smb2_get_next_mid,
1327         .read_data_offset = smb2_read_data_offset,
1328         .read_data_length = smb2_read_data_length,
1329         .map_error = map_smb2_to_linux_error,
1330         .find_mid = smb2_find_mid,
1331         .check_message = smb2_check_message,
1332         .dump_detail = smb2_dump_detail,
1333         .clear_stats = smb2_clear_stats,
1334         .print_stats = smb2_print_stats,
1335         .dump_share_caps = smb2_dump_share_caps,
1336         .is_oplock_break = smb2_is_valid_oplock_break,
1337         .downgrade_oplock = smb2_downgrade_oplock,
1338         .need_neg = smb2_need_neg,
1339         .negotiate = smb2_negotiate,
1340         .negotiate_wsize = smb2_negotiate_wsize,
1341         .negotiate_rsize = smb2_negotiate_rsize,
1342         .sess_setup = SMB2_sess_setup,
1343         .logoff = SMB2_logoff,
1344         .tree_connect = SMB2_tcon,
1345         .tree_disconnect = SMB2_tdis,
1346         .qfs_tcon = smb3_qfs_tcon,
1347         .is_path_accessible = smb2_is_path_accessible,
1348         .can_echo = smb2_can_echo,
1349         .echo = SMB2_echo,
1350         .query_path_info = smb2_query_path_info,
1351         .get_srv_inum = smb2_get_srv_inum,
1352         .query_file_info = smb2_query_file_info,
1353         .set_path_size = smb2_set_path_size,
1354         .set_file_size = smb2_set_file_size,
1355         .set_file_info = smb2_set_file_info,
1356         .set_compression = smb2_set_compression,
1357         .mkdir = smb2_mkdir,
1358         .mkdir_setinfo = smb2_mkdir_setinfo,
1359         .rmdir = smb2_rmdir,
1360         .unlink = smb2_unlink,
1361         .rename = smb2_rename_path,
1362         .create_hardlink = smb2_create_hardlink,
1363         .query_symlink = smb2_query_symlink,
1364         .open = smb2_open_file,
1365         .set_fid = smb2_set_fid,
1366         .close = smb2_close_file,
1367         .flush = smb2_flush_file,
1368         .async_readv = smb2_async_readv,
1369         .async_writev = smb2_async_writev,
1370         .sync_read = smb2_sync_read,
1371         .sync_write = smb2_sync_write,
1372         .query_dir_first = smb2_query_dir_first,
1373         .query_dir_next = smb2_query_dir_next,
1374         .close_dir = smb2_close_dir,
1375         .calc_smb_size = smb2_calc_size,
1376         .is_status_pending = smb2_is_status_pending,
1377         .oplock_response = smb2_oplock_response,
1378         .queryfs = smb2_queryfs,
1379         .mand_lock = smb2_mand_lock,
1380         .mand_unlock_range = smb2_unlock_range,
1381         .push_mand_locks = smb2_push_mandatory_locks,
1382         .get_lease_key = smb2_get_lease_key,
1383         .set_lease_key = smb2_set_lease_key,
1384         .new_lease_key = smb2_new_lease_key,
1385         .generate_signingkey = generate_smb3signingkey,
1386         .calc_signature = smb3_calc_signature,
1387         .is_read_op = smb21_is_read_op,
1388         .set_oplock_level = smb3_set_oplock_level,
1389         .create_lease_buf = smb3_create_lease_buf,
1390         .parse_lease_buf = smb3_parse_lease_buf,
1391         .clone_range = smb2_clone_range,
1392         .validate_negotiate = smb3_validate_negotiate,
1393         .wp_retry_size = smb2_wp_retry_size,
1394 };
1395
1396 struct smb_version_values smb20_values = {
1397         .version_string = SMB20_VERSION_STRING,
1398         .protocol_id = SMB20_PROT_ID,
1399         .req_capabilities = 0, /* MBZ */
1400         .large_lock_type = 0,
1401         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1402         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1403         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1404         .header_size = sizeof(struct smb2_hdr),
1405         .max_header_size = MAX_SMB2_HDR_SIZE,
1406         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1407         .lock_cmd = SMB2_LOCK,
1408         .cap_unix = 0,
1409         .cap_nt_find = SMB2_NT_FIND,
1410         .cap_large_files = SMB2_LARGE_FILES,
1411         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1412         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1413         .create_lease_size = sizeof(struct create_lease),
1414 };
1415
1416 struct smb_version_values smb21_values = {
1417         .version_string = SMB21_VERSION_STRING,
1418         .protocol_id = SMB21_PROT_ID,
1419         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1420         .large_lock_type = 0,
1421         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1422         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1423         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1424         .header_size = sizeof(struct smb2_hdr),
1425         .max_header_size = MAX_SMB2_HDR_SIZE,
1426         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1427         .lock_cmd = SMB2_LOCK,
1428         .cap_unix = 0,
1429         .cap_nt_find = SMB2_NT_FIND,
1430         .cap_large_files = SMB2_LARGE_FILES,
1431         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1432         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1433         .create_lease_size = sizeof(struct create_lease),
1434 };
1435
1436 struct smb_version_values smb30_values = {
1437         .version_string = SMB30_VERSION_STRING,
1438         .protocol_id = SMB30_PROT_ID,
1439         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
1440         .large_lock_type = 0,
1441         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1442         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1443         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1444         .header_size = sizeof(struct smb2_hdr),
1445         .max_header_size = MAX_SMB2_HDR_SIZE,
1446         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1447         .lock_cmd = SMB2_LOCK,
1448         .cap_unix = 0,
1449         .cap_nt_find = SMB2_NT_FIND,
1450         .cap_large_files = SMB2_LARGE_FILES,
1451         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1452         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1453         .create_lease_size = sizeof(struct create_lease_v2),
1454 };
1455
1456 struct smb_version_values smb302_values = {
1457         .version_string = SMB302_VERSION_STRING,
1458         .protocol_id = SMB302_PROT_ID,
1459         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
1460         .large_lock_type = 0,
1461         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1462         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1463         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1464         .header_size = sizeof(struct smb2_hdr),
1465         .max_header_size = MAX_SMB2_HDR_SIZE,
1466         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1467         .lock_cmd = SMB2_LOCK,
1468         .cap_unix = 0,
1469         .cap_nt_find = SMB2_NT_FIND,
1470         .cap_large_files = SMB2_LARGE_FILES,
1471         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1472         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1473         .create_lease_size = sizeof(struct create_lease_v2),
1474 };