GNU Linux-libre 4.4.284-gnu1
[releases.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 <linux/falloc.h>
23 #include "cifsglob.h"
24 #include "smb2pdu.h"
25 #include "smb2proto.h"
26 #include "cifsproto.h"
27 #include "cifs_debug.h"
28 #include "cifs_unicode.h"
29 #include "smb2status.h"
30 #include "smb2glob.h"
31
32 static int
33 change_conf(struct TCP_Server_Info *server)
34 {
35         server->credits += server->echo_credits + server->oplock_credits;
36         server->oplock_credits = server->echo_credits = 0;
37         switch (server->credits) {
38         case 0:
39                 return -1;
40         case 1:
41                 server->echoes = false;
42                 server->oplocks = false;
43                 cifs_dbg(VFS, "disabling echoes and oplocks\n");
44                 break;
45         case 2:
46                 server->echoes = true;
47                 server->oplocks = false;
48                 server->echo_credits = 1;
49                 cifs_dbg(FYI, "disabling oplocks\n");
50                 break;
51         default:
52                 server->echoes = true;
53                 if (enable_oplocks) {
54                         server->oplocks = true;
55                         server->oplock_credits = 1;
56                 } else
57                         server->oplocks = false;
58
59                 server->echo_credits = 1;
60         }
61         server->credits -= server->echo_credits + server->oplock_credits;
62         return 0;
63 }
64
65 static void
66 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
67                  const int optype)
68 {
69         int *val, rc = 0;
70         spin_lock(&server->req_lock);
71         val = server->ops->get_credits_field(server, optype);
72         *val += add;
73         server->in_flight--;
74         if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
75                 rc = change_conf(server);
76         /*
77          * Sometimes server returns 0 credits on oplock break ack - we need to
78          * rebalance credits in this case.
79          */
80         else if (server->in_flight > 0 && server->oplock_credits == 0 &&
81                  server->oplocks) {
82                 if (server->credits > 1) {
83                         server->credits--;
84                         server->oplock_credits++;
85                 }
86         }
87         spin_unlock(&server->req_lock);
88         wake_up(&server->request_q);
89         if (rc)
90                 cifs_reconnect(server);
91 }
92
93 static void
94 smb2_set_credits(struct TCP_Server_Info *server, const int val)
95 {
96         spin_lock(&server->req_lock);
97         server->credits = val;
98         spin_unlock(&server->req_lock);
99 }
100
101 static int *
102 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
103 {
104         switch (optype) {
105         case CIFS_ECHO_OP:
106                 return &server->echo_credits;
107         case CIFS_OBREAK_OP:
108                 return &server->oplock_credits;
109         default:
110                 return &server->credits;
111         }
112 }
113
114 static unsigned int
115 smb2_get_credits(struct mid_q_entry *mid)
116 {
117         return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
118 }
119
120 static int
121 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
122                       unsigned int *num, unsigned int *credits)
123 {
124         int rc = 0;
125         unsigned int scredits;
126
127         spin_lock(&server->req_lock);
128         while (1) {
129                 if (server->credits <= 0) {
130                         spin_unlock(&server->req_lock);
131                         cifs_num_waiters_inc(server);
132                         rc = wait_event_killable(server->request_q,
133                                         has_credits(server, &server->credits));
134                         cifs_num_waiters_dec(server);
135                         if (rc)
136                                 return rc;
137                         spin_lock(&server->req_lock);
138                 } else {
139                         if (server->tcpStatus == CifsExiting) {
140                                 spin_unlock(&server->req_lock);
141                                 return -ENOENT;
142                         }
143
144                         scredits = server->credits;
145                         /* can deadlock with reopen */
146                         if (scredits <= 8) {
147                                 *num = SMB2_MAX_BUFFER_SIZE;
148                                 *credits = 0;
149                                 break;
150                         }
151
152                         /* leave some credits for reopen and other ops */
153                         scredits -= 8;
154                         *num = min_t(unsigned int, size,
155                                      scredits * SMB2_MAX_BUFFER_SIZE);
156
157                         *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
158                         server->credits -= *credits;
159                         server->in_flight++;
160                         break;
161                 }
162         }
163         spin_unlock(&server->req_lock);
164         return rc;
165 }
166
167 static __u64
168 smb2_get_next_mid(struct TCP_Server_Info *server)
169 {
170         __u64 mid;
171         /* for SMB2 we need the current value */
172         spin_lock(&GlobalMid_Lock);
173         mid = server->CurrentMid++;
174         spin_unlock(&GlobalMid_Lock);
175         return mid;
176 }
177
178 static struct mid_q_entry *
179 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
180 {
181         struct mid_q_entry *mid;
182         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
183         __u64 wire_mid = le64_to_cpu(hdr->MessageId);
184
185         spin_lock(&GlobalMid_Lock);
186         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
187                 if ((mid->mid == wire_mid) &&
188                     (mid->mid_state == MID_REQUEST_SUBMITTED) &&
189                     (mid->command == hdr->Command)) {
190                         spin_unlock(&GlobalMid_Lock);
191                         return mid;
192                 }
193         }
194         spin_unlock(&GlobalMid_Lock);
195         return NULL;
196 }
197
198 static void
199 smb2_dump_detail(void *buf)
200 {
201 #ifdef CONFIG_CIFS_DEBUG2
202         struct smb2_hdr *smb = (struct smb2_hdr *)buf;
203
204         cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
205                  smb->Command, smb->Status, smb->Flags, smb->MessageId,
206                  smb->ProcessId);
207         cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
208 #endif
209 }
210
211 static bool
212 smb2_need_neg(struct TCP_Server_Info *server)
213 {
214         return server->max_read == 0;
215 }
216
217 static int
218 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
219 {
220         int rc;
221         ses->server->CurrentMid = 0;
222         rc = SMB2_negotiate(xid, ses);
223         /* BB we probably don't need to retry with modern servers */
224         if (rc == -EAGAIN)
225                 rc = -EHOSTDOWN;
226         return rc;
227 }
228
229 static unsigned int
230 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
231 {
232         struct TCP_Server_Info *server = tcon->ses->server;
233         unsigned int wsize;
234
235         /* start with specified wsize, or default */
236         wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
237         wsize = min_t(unsigned int, wsize, server->max_write);
238
239         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
240                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
241
242         return wsize;
243 }
244
245 static unsigned int
246 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
247 {
248         struct TCP_Server_Info *server = tcon->ses->server;
249         unsigned int rsize;
250
251         /* start with specified rsize, or default */
252         rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
253         rsize = min_t(unsigned int, rsize, server->max_read);
254
255         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
256                 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
257
258         return rsize;
259 }
260
261 #ifdef CONFIG_CIFS_STATS2
262 static int
263 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
264 {
265         int rc;
266         unsigned int ret_data_len = 0;
267         struct network_interface_info_ioctl_rsp *out_buf;
268
269         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
270                         FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
271                         NULL /* no data input */, 0 /* no data input */,
272                         (char **)&out_buf, &ret_data_len);
273         if (rc != 0)
274                 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
275         else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
276                 cifs_dbg(VFS, "server returned bad net interface info buf\n");
277                 rc = -EINVAL;
278         } else {
279                 /* Dump info on first interface */
280                 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
281                         le32_to_cpu(out_buf->Capability));
282                 cifs_dbg(FYI, "Link Speed %lld\n",
283                         le64_to_cpu(out_buf->LinkSpeed));
284         }
285         kfree(out_buf);
286         return rc;
287 }
288 #endif /* STATS2 */
289
290 static void
291 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
292 {
293         int rc;
294         __le16 srch_path = 0; /* Null - open root of share */
295         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
296         struct cifs_open_parms oparms;
297         struct cifs_fid fid;
298
299         oparms.tcon = tcon;
300         oparms.desired_access = FILE_READ_ATTRIBUTES;
301         oparms.disposition = FILE_OPEN;
302         oparms.create_options = 0;
303         oparms.fid = &fid;
304         oparms.reconnect = false;
305
306         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
307         if (rc)
308                 return;
309
310 #ifdef CONFIG_CIFS_STATS2
311         SMB3_request_interfaces(xid, tcon);
312 #endif /* STATS2 */
313
314         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
315                         FS_ATTRIBUTE_INFORMATION);
316         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
317                         FS_DEVICE_INFORMATION);
318         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
319                         FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
320         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
321         return;
322 }
323
324 static void
325 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
326 {
327         int rc;
328         __le16 srch_path = 0; /* Null - open root of share */
329         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
330         struct cifs_open_parms oparms;
331         struct cifs_fid fid;
332
333         oparms.tcon = tcon;
334         oparms.desired_access = FILE_READ_ATTRIBUTES;
335         oparms.disposition = FILE_OPEN;
336         oparms.create_options = 0;
337         oparms.fid = &fid;
338         oparms.reconnect = false;
339
340         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
341         if (rc)
342                 return;
343
344         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
345                         FS_ATTRIBUTE_INFORMATION);
346         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
347                         FS_DEVICE_INFORMATION);
348         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
349         return;
350 }
351
352 static int
353 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
354                         struct cifs_sb_info *cifs_sb, const char *full_path)
355 {
356         int rc;
357         __le16 *utf16_path;
358         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
359         struct cifs_open_parms oparms;
360         struct cifs_fid fid;
361
362         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
363         if (!utf16_path)
364                 return -ENOMEM;
365
366         oparms.tcon = tcon;
367         oparms.desired_access = FILE_READ_ATTRIBUTES;
368         oparms.disposition = FILE_OPEN;
369         oparms.create_options = 0;
370         oparms.fid = &fid;
371         oparms.reconnect = false;
372
373         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
374         if (rc) {
375                 kfree(utf16_path);
376                 return rc;
377         }
378
379         rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
380         kfree(utf16_path);
381         return rc;
382 }
383
384 static int
385 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
386                   struct cifs_sb_info *cifs_sb, const char *full_path,
387                   u64 *uniqueid, FILE_ALL_INFO *data)
388 {
389         *uniqueid = le64_to_cpu(data->IndexNumber);
390         return 0;
391 }
392
393 static int
394 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
395                      struct cifs_fid *fid, FILE_ALL_INFO *data)
396 {
397         int rc;
398         struct smb2_file_all_info *smb2_data;
399
400         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
401                             GFP_KERNEL);
402         if (smb2_data == NULL)
403                 return -ENOMEM;
404
405         rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
406                              smb2_data);
407         if (!rc)
408                 move_smb2_info_to_cifs(data, smb2_data);
409         kfree(smb2_data);
410         return rc;
411 }
412
413 static bool
414 smb2_can_echo(struct TCP_Server_Info *server)
415 {
416         return server->echoes;
417 }
418
419 static void
420 smb2_clear_stats(struct cifs_tcon *tcon)
421 {
422 #ifdef CONFIG_CIFS_STATS
423         int i;
424         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
425                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
426                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
427         }
428 #endif
429 }
430
431 static void
432 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
433 {
434         seq_puts(m, "\n\tShare Capabilities:");
435         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
436                 seq_puts(m, " DFS,");
437         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
438                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
439         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
440                 seq_puts(m, " SCALEOUT,");
441         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
442                 seq_puts(m, " CLUSTER,");
443         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
444                 seq_puts(m, " ASYMMETRIC,");
445         if (tcon->capabilities == 0)
446                 seq_puts(m, " None");
447         if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
448                 seq_puts(m, " Aligned,");
449         if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
450                 seq_puts(m, " Partition Aligned,");
451         if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
452                 seq_puts(m, " SSD,");
453         if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
454                 seq_puts(m, " TRIM-support,");
455
456         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
457         if (tcon->perf_sector_size)
458                 seq_printf(m, "\tOptimal sector size: 0x%x",
459                            tcon->perf_sector_size);
460 }
461
462 static void
463 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
464 {
465 #ifdef CONFIG_CIFS_STATS
466         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
467         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
468         seq_printf(m, "\nNegotiates: %d sent %d failed",
469                    atomic_read(&sent[SMB2_NEGOTIATE_HE]),
470                    atomic_read(&failed[SMB2_NEGOTIATE_HE]));
471         seq_printf(m, "\nSessionSetups: %d sent %d failed",
472                    atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
473                    atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
474         seq_printf(m, "\nLogoffs: %d sent %d failed",
475                    atomic_read(&sent[SMB2_LOGOFF_HE]),
476                    atomic_read(&failed[SMB2_LOGOFF_HE]));
477         seq_printf(m, "\nTreeConnects: %d sent %d failed",
478                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
479                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
480         seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
481                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
482                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
483         seq_printf(m, "\nCreates: %d sent %d failed",
484                    atomic_read(&sent[SMB2_CREATE_HE]),
485                    atomic_read(&failed[SMB2_CREATE_HE]));
486         seq_printf(m, "\nCloses: %d sent %d failed",
487                    atomic_read(&sent[SMB2_CLOSE_HE]),
488                    atomic_read(&failed[SMB2_CLOSE_HE]));
489         seq_printf(m, "\nFlushes: %d sent %d failed",
490                    atomic_read(&sent[SMB2_FLUSH_HE]),
491                    atomic_read(&failed[SMB2_FLUSH_HE]));
492         seq_printf(m, "\nReads: %d sent %d failed",
493                    atomic_read(&sent[SMB2_READ_HE]),
494                    atomic_read(&failed[SMB2_READ_HE]));
495         seq_printf(m, "\nWrites: %d sent %d failed",
496                    atomic_read(&sent[SMB2_WRITE_HE]),
497                    atomic_read(&failed[SMB2_WRITE_HE]));
498         seq_printf(m, "\nLocks: %d sent %d failed",
499                    atomic_read(&sent[SMB2_LOCK_HE]),
500                    atomic_read(&failed[SMB2_LOCK_HE]));
501         seq_printf(m, "\nIOCTLs: %d sent %d failed",
502                    atomic_read(&sent[SMB2_IOCTL_HE]),
503                    atomic_read(&failed[SMB2_IOCTL_HE]));
504         seq_printf(m, "\nCancels: %d sent %d failed",
505                    atomic_read(&sent[SMB2_CANCEL_HE]),
506                    atomic_read(&failed[SMB2_CANCEL_HE]));
507         seq_printf(m, "\nEchos: %d sent %d failed",
508                    atomic_read(&sent[SMB2_ECHO_HE]),
509                    atomic_read(&failed[SMB2_ECHO_HE]));
510         seq_printf(m, "\nQueryDirectories: %d sent %d failed",
511                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
512                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
513         seq_printf(m, "\nChangeNotifies: %d sent %d failed",
514                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
515                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
516         seq_printf(m, "\nQueryInfos: %d sent %d failed",
517                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
518                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
519         seq_printf(m, "\nSetInfos: %d sent %d failed",
520                    atomic_read(&sent[SMB2_SET_INFO_HE]),
521                    atomic_read(&failed[SMB2_SET_INFO_HE]));
522         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
523                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
524                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
525 #endif
526 }
527
528 static void
529 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
530 {
531         struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
532         struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
533
534         cfile->fid.persistent_fid = fid->persistent_fid;
535         cfile->fid.volatile_fid = fid->volatile_fid;
536         server->ops->set_oplock_level(cinode, oplock, fid->epoch,
537                                       &fid->purge_cache);
538         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
539         memcpy(cfile->fid.create_guid, fid->create_guid, 16);
540 }
541
542 static void
543 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
544                 struct cifs_fid *fid)
545 {
546         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
547 }
548
549 static int
550 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
551                      u64 persistent_fid, u64 volatile_fid,
552                      struct copychunk_ioctl *pcchunk)
553 {
554         int rc;
555         unsigned int ret_data_len;
556         struct resume_key_req *res_key;
557
558         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
559                         FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
560                         NULL, 0 /* no input */,
561                         (char **)&res_key, &ret_data_len);
562
563         if (rc) {
564                 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
565                 goto req_res_key_exit;
566         }
567         if (ret_data_len < sizeof(struct resume_key_req)) {
568                 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
569                 rc = -EINVAL;
570                 goto req_res_key_exit;
571         }
572         memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
573
574 req_res_key_exit:
575         kfree(res_key);
576         return rc;
577 }
578
579 static int
580 smb2_clone_range(const unsigned int xid,
581                         struct cifsFileInfo *srcfile,
582                         struct cifsFileInfo *trgtfile, u64 src_off,
583                         u64 len, u64 dest_off)
584 {
585         int rc;
586         unsigned int ret_data_len;
587         struct copychunk_ioctl *pcchunk;
588         struct copychunk_ioctl_rsp *retbuf = NULL;
589         struct cifs_tcon *tcon;
590         int chunks_copied = 0;
591         bool chunk_sizes_updated = false;
592
593         pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
594
595         if (pcchunk == NULL)
596                 return -ENOMEM;
597
598         cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
599         /* Request a key from the server to identify the source of the copy */
600         rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
601                                 srcfile->fid.persistent_fid,
602                                 srcfile->fid.volatile_fid, pcchunk);
603
604         /* Note: request_res_key sets res_key null only if rc !=0 */
605         if (rc)
606                 goto cchunk_out;
607
608         /* For now array only one chunk long, will make more flexible later */
609         pcchunk->ChunkCount = cpu_to_le32(1);
610         pcchunk->Reserved = 0;
611         pcchunk->Reserved2 = 0;
612
613         tcon = tlink_tcon(trgtfile->tlink);
614
615         while (len > 0) {
616                 pcchunk->SourceOffset = cpu_to_le64(src_off);
617                 pcchunk->TargetOffset = cpu_to_le64(dest_off);
618                 pcchunk->Length =
619                         cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
620
621                 /* Request server copy to target from src identified by key */
622                 kfree(retbuf);
623                 retbuf = NULL;
624                 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
625                         trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
626                         true /* is_fsctl */, (char *)pcchunk,
627                         sizeof(struct copychunk_ioctl), (char **)&retbuf,
628                         &ret_data_len);
629                 if (rc == 0) {
630                         if (ret_data_len !=
631                                         sizeof(struct copychunk_ioctl_rsp)) {
632                                 cifs_dbg(VFS, "invalid cchunk response size\n");
633                                 rc = -EIO;
634                                 goto cchunk_out;
635                         }
636                         if (retbuf->TotalBytesWritten == 0) {
637                                 cifs_dbg(FYI, "no bytes copied\n");
638                                 rc = -EIO;
639                                 goto cchunk_out;
640                         }
641                         /*
642                          * Check if server claimed to write more than we asked
643                          */
644                         if (le32_to_cpu(retbuf->TotalBytesWritten) >
645                             le32_to_cpu(pcchunk->Length)) {
646                                 cifs_dbg(VFS, "invalid copy chunk response\n");
647                                 rc = -EIO;
648                                 goto cchunk_out;
649                         }
650                         if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
651                                 cifs_dbg(VFS, "invalid num chunks written\n");
652                                 rc = -EIO;
653                                 goto cchunk_out;
654                         }
655                         chunks_copied++;
656
657                         src_off += le32_to_cpu(retbuf->TotalBytesWritten);
658                         dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
659                         len -= le32_to_cpu(retbuf->TotalBytesWritten);
660
661                         cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
662                                 le32_to_cpu(retbuf->ChunksWritten),
663                                 le32_to_cpu(retbuf->ChunkBytesWritten),
664                                 le32_to_cpu(retbuf->TotalBytesWritten));
665                 } else if (rc == -EINVAL) {
666                         if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
667                                 goto cchunk_out;
668
669                         cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
670                                 le32_to_cpu(retbuf->ChunksWritten),
671                                 le32_to_cpu(retbuf->ChunkBytesWritten),
672                                 le32_to_cpu(retbuf->TotalBytesWritten));
673
674                         /*
675                          * Check if this is the first request using these sizes,
676                          * (ie check if copy succeed once with original sizes
677                          * and check if the server gave us different sizes after
678                          * we already updated max sizes on previous request).
679                          * if not then why is the server returning an error now
680                          */
681                         if ((chunks_copied != 0) || chunk_sizes_updated)
682                                 goto cchunk_out;
683
684                         /* Check that server is not asking us to grow size */
685                         if (le32_to_cpu(retbuf->ChunkBytesWritten) <
686                                         tcon->max_bytes_chunk)
687                                 tcon->max_bytes_chunk =
688                                         le32_to_cpu(retbuf->ChunkBytesWritten);
689                         else
690                                 goto cchunk_out; /* server gave us bogus size */
691
692                         /* No need to change MaxChunks since already set to 1 */
693                         chunk_sizes_updated = true;
694                 } else
695                         goto cchunk_out;
696         }
697
698 cchunk_out:
699         kfree(pcchunk);
700         kfree(retbuf);
701         return rc;
702 }
703
704 static int
705 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
706                 struct cifs_fid *fid)
707 {
708         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
709 }
710
711 static unsigned int
712 smb2_read_data_offset(char *buf)
713 {
714         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
715         return rsp->DataOffset;
716 }
717
718 static unsigned int
719 smb2_read_data_length(char *buf)
720 {
721         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
722         return le32_to_cpu(rsp->DataLength);
723 }
724
725
726 static int
727 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
728                struct cifs_io_parms *parms, unsigned int *bytes_read,
729                char **buf, int *buf_type)
730 {
731         parms->persistent_fid = pfid->persistent_fid;
732         parms->volatile_fid = pfid->volatile_fid;
733         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
734 }
735
736 static int
737 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
738                 struct cifs_io_parms *parms, unsigned int *written,
739                 struct kvec *iov, unsigned long nr_segs)
740 {
741
742         parms->persistent_fid = pfid->persistent_fid;
743         parms->volatile_fid = pfid->volatile_fid;
744         return SMB2_write(xid, parms, written, iov, nr_segs);
745 }
746
747 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
748 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
749                 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
750 {
751         struct cifsInodeInfo *cifsi;
752         int rc;
753
754         cifsi = CIFS_I(inode);
755
756         /* if file already sparse don't bother setting sparse again */
757         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
758                 return true; /* already sparse */
759
760         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
761                 return true; /* already not sparse */
762
763         /*
764          * Can't check for sparse support on share the usual way via the
765          * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
766          * since Samba server doesn't set the flag on the share, yet
767          * supports the set sparse FSCTL and returns sparse correctly
768          * in the file attributes. If we fail setting sparse though we
769          * mark that server does not support sparse files for this share
770          * to avoid repeatedly sending the unsupported fsctl to server
771          * if the file is repeatedly extended.
772          */
773         if (tcon->broken_sparse_sup)
774                 return false;
775
776         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
777                         cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
778                         true /* is_fctl */, &setsparse, 1, NULL, NULL);
779         if (rc) {
780                 tcon->broken_sparse_sup = true;
781                 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
782                 return false;
783         }
784
785         if (setsparse)
786                 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
787         else
788                 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
789
790         return true;
791 }
792
793 static int
794 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
795                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
796 {
797         __le64 eof = cpu_to_le64(size);
798         struct inode *inode;
799
800         /*
801          * If extending file more than one page make sparse. Many Linux fs
802          * make files sparse by default when extending via ftruncate
803          */
804         inode = d_inode(cfile->dentry);
805
806         if (!set_alloc && (size > inode->i_size + 8192)) {
807                 __u8 set_sparse = 1;
808
809                 /* whether set sparse succeeds or not, extend the file */
810                 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
811         }
812
813         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
814                             cfile->fid.volatile_fid, cfile->pid, &eof, false);
815 }
816
817 static int
818 smb2_duplicate_extents(const unsigned int xid,
819                         struct cifsFileInfo *srcfile,
820                         struct cifsFileInfo *trgtfile, u64 src_off,
821                         u64 len, u64 dest_off)
822 {
823         int rc;
824         unsigned int ret_data_len;
825         struct duplicate_extents_to_file dup_ext_buf;
826         struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
827
828         /* server fileays advertise duplicate extent support with this flag */
829         if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
830              FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
831                 return -EOPNOTSUPP;
832
833         dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
834         dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
835         dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
836         dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
837         dup_ext_buf.ByteCount = cpu_to_le64(len);
838         cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
839                 src_off, dest_off, len);
840
841         rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
842         if (rc)
843                 goto duplicate_extents_out;
844
845         rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
846                         trgtfile->fid.volatile_fid,
847                         FSCTL_DUPLICATE_EXTENTS_TO_FILE,
848                         true /* is_fsctl */, (char *)&dup_ext_buf,
849                         sizeof(struct duplicate_extents_to_file),
850                         NULL,
851                         &ret_data_len);
852
853         if (ret_data_len > 0)
854                 cifs_dbg(FYI, "non-zero response length in duplicate extents");
855
856 duplicate_extents_out:
857         return rc;
858 }
859
860 static int
861 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
862                    struct cifsFileInfo *cfile)
863 {
864         return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
865                             cfile->fid.volatile_fid);
866 }
867
868 static int
869 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
870                    struct cifsFileInfo *cfile)
871 {
872         struct fsctl_set_integrity_information_req integr_info;
873         unsigned int ret_data_len;
874
875         integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
876         integr_info.Flags = 0;
877         integr_info.Reserved = 0;
878
879         return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
880                         cfile->fid.volatile_fid,
881                         FSCTL_SET_INTEGRITY_INFORMATION,
882                         true /* is_fsctl */, (char *)&integr_info,
883                         sizeof(struct fsctl_set_integrity_information_req),
884                         NULL,
885                         &ret_data_len);
886
887 }
888
889 static int
890 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
891                      const char *path, struct cifs_sb_info *cifs_sb,
892                      struct cifs_fid *fid, __u16 search_flags,
893                      struct cifs_search_info *srch_inf)
894 {
895         __le16 *utf16_path;
896         int rc;
897         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
898         struct cifs_open_parms oparms;
899
900         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
901         if (!utf16_path)
902                 return -ENOMEM;
903
904         oparms.tcon = tcon;
905         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
906         oparms.disposition = FILE_OPEN;
907         oparms.create_options = 0;
908         oparms.fid = fid;
909         oparms.reconnect = false;
910
911         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
912         kfree(utf16_path);
913         if (rc) {
914                 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
915                 return rc;
916         }
917
918         srch_inf->entries_in_buffer = 0;
919         srch_inf->index_of_last_entry = 2;
920
921         rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
922                                   fid->volatile_fid, 0, srch_inf);
923         if (rc) {
924                 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
925                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
926         }
927         return rc;
928 }
929
930 static int
931 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
932                     struct cifs_fid *fid, __u16 search_flags,
933                     struct cifs_search_info *srch_inf)
934 {
935         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
936                                     fid->volatile_fid, 0, srch_inf);
937 }
938
939 static int
940 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
941                struct cifs_fid *fid)
942 {
943         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
944 }
945
946 /*
947 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
948 * the number of credits and return true. Otherwise - return false.
949 */
950 static bool
951 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
952 {
953         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
954
955         if (hdr->Status != STATUS_PENDING)
956                 return false;
957
958         if (!length) {
959                 spin_lock(&server->req_lock);
960                 server->credits += le16_to_cpu(hdr->CreditRequest);
961                 spin_unlock(&server->req_lock);
962                 wake_up(&server->request_q);
963         }
964
965         return true;
966 }
967
968 static bool
969 smb2_is_session_expired(char *buf)
970 {
971         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
972
973         if (hdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
974                 return false;
975
976         cifs_dbg(FYI, "Session expired\n");
977         return true;
978 }
979
980 static int
981 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
982                      struct cifsInodeInfo *cinode)
983 {
984         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
985                 return SMB2_lease_break(0, tcon, cinode->lease_key,
986                                         smb2_get_lease_state(cinode));
987
988         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
989                                  fid->volatile_fid,
990                                  CIFS_CACHE_READ(cinode) ? 1 : 0);
991 }
992
993 static int
994 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
995              struct kstatfs *buf)
996 {
997         int rc;
998         __le16 srch_path = 0; /* Null - open root of share */
999         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1000         struct cifs_open_parms oparms;
1001         struct cifs_fid fid;
1002
1003         oparms.tcon = tcon;
1004         oparms.desired_access = FILE_READ_ATTRIBUTES;
1005         oparms.disposition = FILE_OPEN;
1006         oparms.create_options = 0;
1007         oparms.fid = &fid;
1008         oparms.reconnect = false;
1009
1010         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
1011         if (rc)
1012                 return rc;
1013         buf->f_type = SMB2_MAGIC_NUMBER;
1014         rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1015                            buf);
1016         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1017         return rc;
1018 }
1019
1020 static bool
1021 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1022 {
1023         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1024                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1025 }
1026
1027 static int
1028 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1029                __u64 length, __u32 type, int lock, int unlock, bool wait)
1030 {
1031         if (unlock && !lock)
1032                 type = SMB2_LOCKFLAG_UNLOCK;
1033         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1034                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1035                          current->tgid, length, offset, type, wait);
1036 }
1037
1038 static void
1039 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1040 {
1041         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1042 }
1043
1044 static void
1045 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1046 {
1047         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1048 }
1049
1050 static void
1051 smb2_new_lease_key(struct cifs_fid *fid)
1052 {
1053         generate_random_uuid(fid->lease_key);
1054 }
1055
1056 #define SMB2_SYMLINK_STRUCT_SIZE \
1057         (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1058
1059 static int
1060 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1061                    const char *full_path, char **target_path,
1062                    struct cifs_sb_info *cifs_sb)
1063 {
1064         int rc;
1065         __le16 *utf16_path;
1066         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1067         struct cifs_open_parms oparms;
1068         struct cifs_fid fid;
1069         struct smb2_err_rsp *err_buf = NULL;
1070         struct smb2_symlink_err_rsp *symlink;
1071         unsigned int sub_len;
1072         unsigned int sub_offset;
1073         unsigned int print_len;
1074         unsigned int print_offset;
1075
1076         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1077
1078         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1079         if (!utf16_path)
1080                 return -ENOMEM;
1081
1082         oparms.tcon = tcon;
1083         oparms.desired_access = FILE_READ_ATTRIBUTES;
1084         oparms.disposition = FILE_OPEN;
1085         oparms.create_options = 0;
1086         oparms.fid = &fid;
1087         oparms.reconnect = false;
1088
1089         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1090
1091         if (!rc || !err_buf) {
1092                 kfree(utf16_path);
1093                 return -ENOENT;
1094         }
1095
1096         if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1097             get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1098                 kfree(utf16_path);
1099                 return -ENOENT;
1100         }
1101
1102         /* open must fail on symlink - reset rc */
1103         rc = 0;
1104         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1105         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1106         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1107         print_len = le16_to_cpu(symlink->PrintNameLength);
1108         print_offset = le16_to_cpu(symlink->PrintNameOffset);
1109
1110         if (get_rfc1002_length(err_buf) + 4 <
1111                         SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1112                 kfree(utf16_path);
1113                 return -ENOENT;
1114         }
1115
1116         if (get_rfc1002_length(err_buf) + 4 <
1117                         SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1118                 kfree(utf16_path);
1119                 return -ENOENT;
1120         }
1121
1122         *target_path = cifs_strndup_from_utf16(
1123                                 (char *)symlink->PathBuffer + sub_offset,
1124                                 sub_len, true, cifs_sb->local_nls);
1125         if (!(*target_path)) {
1126                 kfree(utf16_path);
1127                 return -ENOMEM;
1128         }
1129         convert_delimiter(*target_path, '/');
1130         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1131         kfree(utf16_path);
1132         return rc;
1133 }
1134
1135 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1136                             loff_t offset, loff_t len, bool keep_size)
1137 {
1138         struct inode *inode;
1139         struct cifsInodeInfo *cifsi;
1140         struct cifsFileInfo *cfile = file->private_data;
1141         struct file_zero_data_information fsctl_buf;
1142         long rc;
1143         unsigned int xid;
1144
1145         xid = get_xid();
1146
1147         inode = d_inode(cfile->dentry);
1148         cifsi = CIFS_I(inode);
1149
1150         /*
1151          * We zero the range through ioctl, so we need remove the page caches
1152          * first, otherwise the data may be inconsistent with the server.
1153          */
1154         truncate_pagecache_range(inode, offset, offset + len - 1);
1155
1156         /* if file not oplocked can't be sure whether asking to extend size */
1157         if (!CIFS_CACHE_READ(cifsi))
1158                 if (keep_size == false)
1159                         return -EOPNOTSUPP;
1160
1161         /*
1162          * Must check if file sparse since fallocate -z (zero range) assumes
1163          * non-sparse allocation
1164          */
1165         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1166                 return -EOPNOTSUPP;
1167
1168         /*
1169          * need to make sure we are not asked to extend the file since the SMB3
1170          * fsctl does not change the file size. In the future we could change
1171          * this to zero the first part of the range then set the file size
1172          * which for a non sparse file would zero the newly extended range
1173          */
1174         if (keep_size == false)
1175                 if (i_size_read(inode) < offset + len)
1176                         return -EOPNOTSUPP;
1177
1178         cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1179
1180         fsctl_buf.FileOffset = cpu_to_le64(offset);
1181         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1182
1183         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1184                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1185                         true /* is_fctl */, (char *)&fsctl_buf,
1186                         sizeof(struct file_zero_data_information), NULL, NULL);
1187         free_xid(xid);
1188         return rc;
1189 }
1190
1191 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1192                             loff_t offset, loff_t len)
1193 {
1194         struct inode *inode;
1195         struct cifsInodeInfo *cifsi;
1196         struct cifsFileInfo *cfile = file->private_data;
1197         struct file_zero_data_information fsctl_buf;
1198         long rc;
1199         unsigned int xid;
1200         __u8 set_sparse = 1;
1201
1202         xid = get_xid();
1203
1204         inode = d_inode(cfile->dentry);
1205         cifsi = CIFS_I(inode);
1206
1207         /* Need to make file sparse, if not already, before freeing range. */
1208         /* Consider adding equivalent for compressed since it could also work */
1209         if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1210                 return -EOPNOTSUPP;
1211
1212         /*
1213          * We implement the punch hole through ioctl, so we need remove the page
1214          * caches first, otherwise the data may be inconsistent with the server.
1215          */
1216         truncate_pagecache_range(inode, offset, offset + len - 1);
1217
1218         cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1219
1220         fsctl_buf.FileOffset = cpu_to_le64(offset);
1221         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1222
1223         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1224                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1225                         true /* is_fctl */, (char *)&fsctl_buf,
1226                         sizeof(struct file_zero_data_information), NULL, NULL);
1227         free_xid(xid);
1228         return rc;
1229 }
1230
1231 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1232                             loff_t off, loff_t len, bool keep_size)
1233 {
1234         struct inode *inode;
1235         struct cifsInodeInfo *cifsi;
1236         struct cifsFileInfo *cfile = file->private_data;
1237         long rc = -EOPNOTSUPP;
1238         unsigned int xid;
1239
1240         xid = get_xid();
1241
1242         inode = d_inode(cfile->dentry);
1243         cifsi = CIFS_I(inode);
1244
1245         /* if file not oplocked can't be sure whether asking to extend size */
1246         if (!CIFS_CACHE_READ(cifsi))
1247                 if (keep_size == false)
1248                         return -EOPNOTSUPP;
1249
1250         /*
1251          * Files are non-sparse by default so falloc may be a no-op
1252          * Must check if file sparse. If not sparse, and not extending
1253          * then no need to do anything since file already allocated
1254          */
1255         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1256                 if (keep_size == true)
1257                         return 0;
1258                 /* check if extending file */
1259                 else if (i_size_read(inode) >= off + len)
1260                         /* not extending file and already not sparse */
1261                         return 0;
1262                 /* BB: in future add else clause to extend file */
1263                 else
1264                         return -EOPNOTSUPP;
1265         }
1266
1267         if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1268                 /*
1269                  * Check if falloc starts within first few pages of file
1270                  * and ends within a few pages of the end of file to
1271                  * ensure that most of file is being forced to be
1272                  * fallocated now. If so then setting whole file sparse
1273                  * ie potentially making a few extra pages at the beginning
1274                  * or end of the file non-sparse via set_sparse is harmless.
1275                  */
1276                 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1277                         return -EOPNOTSUPP;
1278
1279                 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1280         }
1281         /* BB: else ... in future add code to extend file and set sparse */
1282
1283
1284         free_xid(xid);
1285         return rc;
1286 }
1287
1288
1289 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1290                            loff_t off, loff_t len)
1291 {
1292         /* KEEP_SIZE already checked for by do_fallocate */
1293         if (mode & FALLOC_FL_PUNCH_HOLE)
1294                 return smb3_punch_hole(file, tcon, off, len);
1295         else if (mode & FALLOC_FL_ZERO_RANGE) {
1296                 if (mode & FALLOC_FL_KEEP_SIZE)
1297                         return smb3_zero_range(file, tcon, off, len, true);
1298                 return smb3_zero_range(file, tcon, off, len, false);
1299         } else if (mode == FALLOC_FL_KEEP_SIZE)
1300                 return smb3_simple_falloc(file, tcon, off, len, true);
1301         else if (mode == 0)
1302                 return smb3_simple_falloc(file, tcon, off, len, false);
1303
1304         return -EOPNOTSUPP;
1305 }
1306
1307 static void
1308 smb2_downgrade_oplock(struct TCP_Server_Info *server,
1309                         struct cifsInodeInfo *cinode, bool set_level2)
1310 {
1311         if (set_level2)
1312                 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1313                                                 0, NULL);
1314         else
1315                 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1316 }
1317
1318 static void
1319 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1320                       unsigned int epoch, bool *purge_cache)
1321 {
1322         oplock &= 0xFF;
1323         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1324                 return;
1325         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1326                 cinode->oplock = CIFS_CACHE_RHW_FLG;
1327                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1328                          &cinode->vfs_inode);
1329         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1330                 cinode->oplock = CIFS_CACHE_RW_FLG;
1331                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1332                          &cinode->vfs_inode);
1333         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1334                 cinode->oplock = CIFS_CACHE_READ_FLG;
1335                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1336                          &cinode->vfs_inode);
1337         } else
1338                 cinode->oplock = 0;
1339 }
1340
1341 static void
1342 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1343                        unsigned int epoch, bool *purge_cache)
1344 {
1345         char message[5] = {0};
1346         unsigned int new_oplock = 0;
1347
1348         oplock &= 0xFF;
1349         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1350                 return;
1351
1352         /* Check if the server granted an oplock rather than a lease */
1353         if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1354                 return smb2_set_oplock_level(cinode, oplock, epoch,
1355                                              purge_cache);
1356
1357         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1358                 new_oplock |= CIFS_CACHE_READ_FLG;
1359                 strcat(message, "R");
1360         }
1361         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1362                 new_oplock |= CIFS_CACHE_HANDLE_FLG;
1363                 strcat(message, "H");
1364         }
1365         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1366                 new_oplock |= CIFS_CACHE_WRITE_FLG;
1367                 strcat(message, "W");
1368         }
1369         if (!new_oplock)
1370                 strncpy(message, "None", sizeof(message));
1371
1372         cinode->oplock = new_oplock;
1373         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1374                  &cinode->vfs_inode);
1375 }
1376
1377 static void
1378 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1379                       unsigned int epoch, bool *purge_cache)
1380 {
1381         unsigned int old_oplock = cinode->oplock;
1382
1383         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1384
1385         if (purge_cache) {
1386                 *purge_cache = false;
1387                 if (old_oplock == CIFS_CACHE_READ_FLG) {
1388                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1389                             (epoch - cinode->epoch > 0))
1390                                 *purge_cache = true;
1391                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1392                                  (epoch - cinode->epoch > 1))
1393                                 *purge_cache = true;
1394                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1395                                  (epoch - cinode->epoch > 1))
1396                                 *purge_cache = true;
1397                         else if (cinode->oplock == 0 &&
1398                                  (epoch - cinode->epoch > 0))
1399                                 *purge_cache = true;
1400                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1401                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1402                             (epoch - cinode->epoch > 0))
1403                                 *purge_cache = true;
1404                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1405                                  (epoch - cinode->epoch > 1))
1406                                 *purge_cache = true;
1407                 }
1408                 cinode->epoch = epoch;
1409         }
1410 }
1411
1412 static bool
1413 smb2_is_read_op(__u32 oplock)
1414 {
1415         return oplock == SMB2_OPLOCK_LEVEL_II;
1416 }
1417
1418 static bool
1419 smb21_is_read_op(__u32 oplock)
1420 {
1421         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1422                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1423 }
1424
1425 static __le32
1426 map_oplock_to_lease(u8 oplock)
1427 {
1428         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1429                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1430         else if (oplock == SMB2_OPLOCK_LEVEL_II)
1431                 return SMB2_LEASE_READ_CACHING;
1432         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1433                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1434                        SMB2_LEASE_WRITE_CACHING;
1435         return 0;
1436 }
1437
1438 static char *
1439 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1440 {
1441         struct create_lease *buf;
1442
1443         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1444         if (!buf)
1445                 return NULL;
1446
1447         buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1448         buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1449         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1450
1451         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1452                                         (struct create_lease, lcontext));
1453         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1454         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1455                                 (struct create_lease, Name));
1456         buf->ccontext.NameLength = cpu_to_le16(4);
1457         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1458         buf->Name[0] = 'R';
1459         buf->Name[1] = 'q';
1460         buf->Name[2] = 'L';
1461         buf->Name[3] = 's';
1462         return (char *)buf;
1463 }
1464
1465 static char *
1466 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1467 {
1468         struct create_lease_v2 *buf;
1469
1470         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1471         if (!buf)
1472                 return NULL;
1473
1474         buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1475         buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1476         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1477
1478         buf->ccontext.DataOffset = cpu_to_le16(offsetof
1479                                         (struct create_lease_v2, lcontext));
1480         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1481         buf->ccontext.NameOffset = cpu_to_le16(offsetof
1482                                 (struct create_lease_v2, Name));
1483         buf->ccontext.NameLength = cpu_to_le16(4);
1484         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1485         buf->Name[0] = 'R';
1486         buf->Name[1] = 'q';
1487         buf->Name[2] = 'L';
1488         buf->Name[3] = 's';
1489         return (char *)buf;
1490 }
1491
1492 static __u8
1493 smb2_parse_lease_buf(void *buf, unsigned int *epoch)
1494 {
1495         struct create_lease *lc = (struct create_lease *)buf;
1496
1497         *epoch = 0; /* not used */
1498         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1499                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1500         return le32_to_cpu(lc->lcontext.LeaseState);
1501 }
1502
1503 static __u8
1504 smb3_parse_lease_buf(void *buf, unsigned int *epoch)
1505 {
1506         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1507
1508         *epoch = le16_to_cpu(lc->lcontext.Epoch);
1509         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1510                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1511         return le32_to_cpu(lc->lcontext.LeaseState);
1512 }
1513
1514 static unsigned int
1515 smb2_wp_retry_size(struct inode *inode)
1516 {
1517         return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1518                      SMB2_MAX_BUFFER_SIZE);
1519 }
1520
1521 static bool
1522 smb2_dir_needs_close(struct cifsFileInfo *cfile)
1523 {
1524         return !cfile->invalidHandle;
1525 }
1526
1527 struct smb_version_operations smb20_operations = {
1528         .compare_fids = smb2_compare_fids,
1529         .setup_request = smb2_setup_request,
1530         .setup_async_request = smb2_setup_async_request,
1531         .check_receive = smb2_check_receive,
1532         .add_credits = smb2_add_credits,
1533         .set_credits = smb2_set_credits,
1534         .get_credits_field = smb2_get_credits_field,
1535         .get_credits = smb2_get_credits,
1536         .wait_mtu_credits = cifs_wait_mtu_credits,
1537         .get_next_mid = smb2_get_next_mid,
1538         .read_data_offset = smb2_read_data_offset,
1539         .read_data_length = smb2_read_data_length,
1540         .map_error = map_smb2_to_linux_error,
1541         .find_mid = smb2_find_mid,
1542         .check_message = smb2_check_message,
1543         .dump_detail = smb2_dump_detail,
1544         .clear_stats = smb2_clear_stats,
1545         .print_stats = smb2_print_stats,
1546         .is_oplock_break = smb2_is_valid_oplock_break,
1547         .handle_cancelled_mid = smb2_handle_cancelled_mid,
1548         .downgrade_oplock = smb2_downgrade_oplock,
1549         .need_neg = smb2_need_neg,
1550         .negotiate = smb2_negotiate,
1551         .negotiate_wsize = smb2_negotiate_wsize,
1552         .negotiate_rsize = smb2_negotiate_rsize,
1553         .sess_setup = SMB2_sess_setup,
1554         .logoff = SMB2_logoff,
1555         .tree_connect = SMB2_tcon,
1556         .tree_disconnect = SMB2_tdis,
1557         .qfs_tcon = smb2_qfs_tcon,
1558         .is_path_accessible = smb2_is_path_accessible,
1559         .can_echo = smb2_can_echo,
1560         .echo = SMB2_echo,
1561         .query_path_info = smb2_query_path_info,
1562         .get_srv_inum = smb2_get_srv_inum,
1563         .query_file_info = smb2_query_file_info,
1564         .set_path_size = smb2_set_path_size,
1565         .set_file_size = smb2_set_file_size,
1566         .set_file_info = smb2_set_file_info,
1567         .set_compression = smb2_set_compression,
1568         .mkdir = smb2_mkdir,
1569         .mkdir_setinfo = smb2_mkdir_setinfo,
1570         .rmdir = smb2_rmdir,
1571         .unlink = smb2_unlink,
1572         .rename = smb2_rename_path,
1573         .create_hardlink = smb2_create_hardlink,
1574         .query_symlink = smb2_query_symlink,
1575         .open = smb2_open_file,
1576         .set_fid = smb2_set_fid,
1577         .close = smb2_close_file,
1578         .flush = smb2_flush_file,
1579         .async_readv = smb2_async_readv,
1580         .async_writev = smb2_async_writev,
1581         .sync_read = smb2_sync_read,
1582         .sync_write = smb2_sync_write,
1583         .query_dir_first = smb2_query_dir_first,
1584         .query_dir_next = smb2_query_dir_next,
1585         .close_dir = smb2_close_dir,
1586         .calc_smb_size = smb2_calc_size,
1587         .is_status_pending = smb2_is_status_pending,
1588         .is_session_expired = smb2_is_session_expired,
1589         .oplock_response = smb2_oplock_response,
1590         .queryfs = smb2_queryfs,
1591         .mand_lock = smb2_mand_lock,
1592         .mand_unlock_range = smb2_unlock_range,
1593         .push_mand_locks = smb2_push_mandatory_locks,
1594         .get_lease_key = smb2_get_lease_key,
1595         .set_lease_key = smb2_set_lease_key,
1596         .new_lease_key = smb2_new_lease_key,
1597         .calc_signature = smb2_calc_signature,
1598         .is_read_op = smb2_is_read_op,
1599         .set_oplock_level = smb2_set_oplock_level,
1600         .create_lease_buf = smb2_create_lease_buf,
1601         .parse_lease_buf = smb2_parse_lease_buf,
1602         .clone_range = smb2_clone_range,
1603         .wp_retry_size = smb2_wp_retry_size,
1604         .dir_needs_close = smb2_dir_needs_close,
1605 };
1606
1607 struct smb_version_operations smb21_operations = {
1608         .compare_fids = smb2_compare_fids,
1609         .setup_request = smb2_setup_request,
1610         .setup_async_request = smb2_setup_async_request,
1611         .check_receive = smb2_check_receive,
1612         .add_credits = smb2_add_credits,
1613         .set_credits = smb2_set_credits,
1614         .get_credits_field = smb2_get_credits_field,
1615         .get_credits = smb2_get_credits,
1616         .wait_mtu_credits = smb2_wait_mtu_credits,
1617         .get_next_mid = smb2_get_next_mid,
1618         .read_data_offset = smb2_read_data_offset,
1619         .read_data_length = smb2_read_data_length,
1620         .map_error = map_smb2_to_linux_error,
1621         .find_mid = smb2_find_mid,
1622         .check_message = smb2_check_message,
1623         .dump_detail = smb2_dump_detail,
1624         .clear_stats = smb2_clear_stats,
1625         .print_stats = smb2_print_stats,
1626         .is_oplock_break = smb2_is_valid_oplock_break,
1627         .handle_cancelled_mid = smb2_handle_cancelled_mid,
1628         .downgrade_oplock = smb2_downgrade_oplock,
1629         .need_neg = smb2_need_neg,
1630         .negotiate = smb2_negotiate,
1631         .negotiate_wsize = smb2_negotiate_wsize,
1632         .negotiate_rsize = smb2_negotiate_rsize,
1633         .sess_setup = SMB2_sess_setup,
1634         .logoff = SMB2_logoff,
1635         .tree_connect = SMB2_tcon,
1636         .tree_disconnect = SMB2_tdis,
1637         .qfs_tcon = smb2_qfs_tcon,
1638         .is_path_accessible = smb2_is_path_accessible,
1639         .can_echo = smb2_can_echo,
1640         .echo = SMB2_echo,
1641         .query_path_info = smb2_query_path_info,
1642         .get_srv_inum = smb2_get_srv_inum,
1643         .query_file_info = smb2_query_file_info,
1644         .set_path_size = smb2_set_path_size,
1645         .set_file_size = smb2_set_file_size,
1646         .set_file_info = smb2_set_file_info,
1647         .set_compression = smb2_set_compression,
1648         .mkdir = smb2_mkdir,
1649         .mkdir_setinfo = smb2_mkdir_setinfo,
1650         .rmdir = smb2_rmdir,
1651         .unlink = smb2_unlink,
1652         .rename = smb2_rename_path,
1653         .create_hardlink = smb2_create_hardlink,
1654         .query_symlink = smb2_query_symlink,
1655         .query_mf_symlink = smb3_query_mf_symlink,
1656         .create_mf_symlink = smb3_create_mf_symlink,
1657         .open = smb2_open_file,
1658         .set_fid = smb2_set_fid,
1659         .close = smb2_close_file,
1660         .flush = smb2_flush_file,
1661         .async_readv = smb2_async_readv,
1662         .async_writev = smb2_async_writev,
1663         .sync_read = smb2_sync_read,
1664         .sync_write = smb2_sync_write,
1665         .query_dir_first = smb2_query_dir_first,
1666         .query_dir_next = smb2_query_dir_next,
1667         .close_dir = smb2_close_dir,
1668         .calc_smb_size = smb2_calc_size,
1669         .is_status_pending = smb2_is_status_pending,
1670         .is_session_expired = smb2_is_session_expired,
1671         .oplock_response = smb2_oplock_response,
1672         .queryfs = smb2_queryfs,
1673         .mand_lock = smb2_mand_lock,
1674         .mand_unlock_range = smb2_unlock_range,
1675         .push_mand_locks = smb2_push_mandatory_locks,
1676         .get_lease_key = smb2_get_lease_key,
1677         .set_lease_key = smb2_set_lease_key,
1678         .new_lease_key = smb2_new_lease_key,
1679         .calc_signature = smb2_calc_signature,
1680         .is_read_op = smb21_is_read_op,
1681         .set_oplock_level = smb21_set_oplock_level,
1682         .create_lease_buf = smb2_create_lease_buf,
1683         .parse_lease_buf = smb2_parse_lease_buf,
1684         .clone_range = smb2_clone_range,
1685         .wp_retry_size = smb2_wp_retry_size,
1686         .dir_needs_close = smb2_dir_needs_close,
1687 };
1688
1689 struct smb_version_operations smb30_operations = {
1690         .compare_fids = smb2_compare_fids,
1691         .setup_request = smb2_setup_request,
1692         .setup_async_request = smb2_setup_async_request,
1693         .check_receive = smb2_check_receive,
1694         .add_credits = smb2_add_credits,
1695         .set_credits = smb2_set_credits,
1696         .get_credits_field = smb2_get_credits_field,
1697         .get_credits = smb2_get_credits,
1698         .wait_mtu_credits = smb2_wait_mtu_credits,
1699         .get_next_mid = smb2_get_next_mid,
1700         .read_data_offset = smb2_read_data_offset,
1701         .read_data_length = smb2_read_data_length,
1702         .map_error = map_smb2_to_linux_error,
1703         .find_mid = smb2_find_mid,
1704         .check_message = smb2_check_message,
1705         .dump_detail = smb2_dump_detail,
1706         .clear_stats = smb2_clear_stats,
1707         .print_stats = smb2_print_stats,
1708         .dump_share_caps = smb2_dump_share_caps,
1709         .is_oplock_break = smb2_is_valid_oplock_break,
1710         .handle_cancelled_mid = smb2_handle_cancelled_mid,
1711         .downgrade_oplock = smb2_downgrade_oplock,
1712         .need_neg = smb2_need_neg,
1713         .negotiate = smb2_negotiate,
1714         .negotiate_wsize = smb2_negotiate_wsize,
1715         .negotiate_rsize = smb2_negotiate_rsize,
1716         .sess_setup = SMB2_sess_setup,
1717         .logoff = SMB2_logoff,
1718         .tree_connect = SMB2_tcon,
1719         .tree_disconnect = SMB2_tdis,
1720         .qfs_tcon = smb3_qfs_tcon,
1721         .is_path_accessible = smb2_is_path_accessible,
1722         .can_echo = smb2_can_echo,
1723         .echo = SMB2_echo,
1724         .query_path_info = smb2_query_path_info,
1725         .get_srv_inum = smb2_get_srv_inum,
1726         .query_file_info = smb2_query_file_info,
1727         .set_path_size = smb2_set_path_size,
1728         .set_file_size = smb2_set_file_size,
1729         .set_file_info = smb2_set_file_info,
1730         .set_compression = smb2_set_compression,
1731         .mkdir = smb2_mkdir,
1732         .mkdir_setinfo = smb2_mkdir_setinfo,
1733         .rmdir = smb2_rmdir,
1734         .unlink = smb2_unlink,
1735         .rename = smb2_rename_path,
1736         .create_hardlink = smb2_create_hardlink,
1737         .query_symlink = smb2_query_symlink,
1738         .query_mf_symlink = smb3_query_mf_symlink,
1739         .create_mf_symlink = smb3_create_mf_symlink,
1740         .open = smb2_open_file,
1741         .set_fid = smb2_set_fid,
1742         .close = smb2_close_file,
1743         .flush = smb2_flush_file,
1744         .async_readv = smb2_async_readv,
1745         .async_writev = smb2_async_writev,
1746         .sync_read = smb2_sync_read,
1747         .sync_write = smb2_sync_write,
1748         .query_dir_first = smb2_query_dir_first,
1749         .query_dir_next = smb2_query_dir_next,
1750         .close_dir = smb2_close_dir,
1751         .calc_smb_size = smb2_calc_size,
1752         .is_status_pending = smb2_is_status_pending,
1753         .is_session_expired = smb2_is_session_expired,
1754         .oplock_response = smb2_oplock_response,
1755         .queryfs = smb2_queryfs,
1756         .mand_lock = smb2_mand_lock,
1757         .mand_unlock_range = smb2_unlock_range,
1758         .push_mand_locks = smb2_push_mandatory_locks,
1759         .get_lease_key = smb2_get_lease_key,
1760         .set_lease_key = smb2_set_lease_key,
1761         .new_lease_key = smb2_new_lease_key,
1762         .generate_signingkey = generate_smb3signingkey,
1763         .calc_signature = smb3_calc_signature,
1764         .set_integrity  = smb3_set_integrity,
1765         .is_read_op = smb21_is_read_op,
1766         .set_oplock_level = smb3_set_oplock_level,
1767         .create_lease_buf = smb3_create_lease_buf,
1768         .parse_lease_buf = smb3_parse_lease_buf,
1769         .clone_range = smb2_clone_range,
1770         .duplicate_extents = smb2_duplicate_extents,
1771         .validate_negotiate = smb3_validate_negotiate,
1772         .wp_retry_size = smb2_wp_retry_size,
1773         .dir_needs_close = smb2_dir_needs_close,
1774         .fallocate = smb3_fallocate,
1775 };
1776
1777 #ifdef CONFIG_CIFS_SMB311
1778 struct smb_version_operations smb311_operations = {
1779         .compare_fids = smb2_compare_fids,
1780         .setup_request = smb2_setup_request,
1781         .setup_async_request = smb2_setup_async_request,
1782         .check_receive = smb2_check_receive,
1783         .add_credits = smb2_add_credits,
1784         .set_credits = smb2_set_credits,
1785         .get_credits_field = smb2_get_credits_field,
1786         .get_credits = smb2_get_credits,
1787         .wait_mtu_credits = smb2_wait_mtu_credits,
1788         .get_next_mid = smb2_get_next_mid,
1789         .read_data_offset = smb2_read_data_offset,
1790         .read_data_length = smb2_read_data_length,
1791         .map_error = map_smb2_to_linux_error,
1792         .find_mid = smb2_find_mid,
1793         .check_message = smb2_check_message,
1794         .dump_detail = smb2_dump_detail,
1795         .clear_stats = smb2_clear_stats,
1796         .print_stats = smb2_print_stats,
1797         .dump_share_caps = smb2_dump_share_caps,
1798         .is_oplock_break = smb2_is_valid_oplock_break,
1799         .handle_cancelled_mid = smb2_handle_cancelled_mid,
1800         .downgrade_oplock = smb2_downgrade_oplock,
1801         .need_neg = smb2_need_neg,
1802         .negotiate = smb2_negotiate,
1803         .negotiate_wsize = smb2_negotiate_wsize,
1804         .negotiate_rsize = smb2_negotiate_rsize,
1805         .sess_setup = SMB2_sess_setup,
1806         .logoff = SMB2_logoff,
1807         .tree_connect = SMB2_tcon,
1808         .tree_disconnect = SMB2_tdis,
1809         .qfs_tcon = smb3_qfs_tcon,
1810         .is_path_accessible = smb2_is_path_accessible,
1811         .can_echo = smb2_can_echo,
1812         .echo = SMB2_echo,
1813         .query_path_info = smb2_query_path_info,
1814         .get_srv_inum = smb2_get_srv_inum,
1815         .query_file_info = smb2_query_file_info,
1816         .set_path_size = smb2_set_path_size,
1817         .set_file_size = smb2_set_file_size,
1818         .set_file_info = smb2_set_file_info,
1819         .set_compression = smb2_set_compression,
1820         .mkdir = smb2_mkdir,
1821         .mkdir_setinfo = smb2_mkdir_setinfo,
1822         .rmdir = smb2_rmdir,
1823         .unlink = smb2_unlink,
1824         .rename = smb2_rename_path,
1825         .create_hardlink = smb2_create_hardlink,
1826         .query_symlink = smb2_query_symlink,
1827         .query_mf_symlink = smb3_query_mf_symlink,
1828         .create_mf_symlink = smb3_create_mf_symlink,
1829         .open = smb2_open_file,
1830         .set_fid = smb2_set_fid,
1831         .close = smb2_close_file,
1832         .flush = smb2_flush_file,
1833         .async_readv = smb2_async_readv,
1834         .async_writev = smb2_async_writev,
1835         .sync_read = smb2_sync_read,
1836         .sync_write = smb2_sync_write,
1837         .query_dir_first = smb2_query_dir_first,
1838         .query_dir_next = smb2_query_dir_next,
1839         .close_dir = smb2_close_dir,
1840         .calc_smb_size = smb2_calc_size,
1841         .is_status_pending = smb2_is_status_pending,
1842         .is_session_expired = smb2_is_session_expired,
1843         .oplock_response = smb2_oplock_response,
1844         .queryfs = smb2_queryfs,
1845         .mand_lock = smb2_mand_lock,
1846         .mand_unlock_range = smb2_unlock_range,
1847         .push_mand_locks = smb2_push_mandatory_locks,
1848         .get_lease_key = smb2_get_lease_key,
1849         .set_lease_key = smb2_set_lease_key,
1850         .new_lease_key = smb2_new_lease_key,
1851         .generate_signingkey = generate_smb3signingkey,
1852         .calc_signature = smb3_calc_signature,
1853         .set_integrity  = smb3_set_integrity,
1854         .is_read_op = smb21_is_read_op,
1855         .set_oplock_level = smb3_set_oplock_level,
1856         .create_lease_buf = smb3_create_lease_buf,
1857         .parse_lease_buf = smb3_parse_lease_buf,
1858         .clone_range = smb2_clone_range,
1859         .duplicate_extents = smb2_duplicate_extents,
1860 /*      .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
1861         .wp_retry_size = smb2_wp_retry_size,
1862         .dir_needs_close = smb2_dir_needs_close,
1863         .fallocate = smb3_fallocate,
1864 };
1865 #endif /* CIFS_SMB311 */
1866
1867 struct smb_version_values smb20_values = {
1868         .version_string = SMB20_VERSION_STRING,
1869         .protocol_id = SMB20_PROT_ID,
1870         .req_capabilities = 0, /* MBZ */
1871         .large_lock_type = 0,
1872         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1873         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1874         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1875         .header_size = sizeof(struct smb2_hdr),
1876         .max_header_size = MAX_SMB2_HDR_SIZE,
1877         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1878         .lock_cmd = SMB2_LOCK,
1879         .cap_unix = 0,
1880         .cap_nt_find = SMB2_NT_FIND,
1881         .cap_large_files = SMB2_LARGE_FILES,
1882         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1883         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1884         .create_lease_size = sizeof(struct create_lease),
1885 };
1886
1887 struct smb_version_values smb21_values = {
1888         .version_string = SMB21_VERSION_STRING,
1889         .protocol_id = SMB21_PROT_ID,
1890         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1891         .large_lock_type = 0,
1892         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1893         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1894         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1895         .header_size = sizeof(struct smb2_hdr),
1896         .max_header_size = MAX_SMB2_HDR_SIZE,
1897         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1898         .lock_cmd = SMB2_LOCK,
1899         .cap_unix = 0,
1900         .cap_nt_find = SMB2_NT_FIND,
1901         .cap_large_files = SMB2_LARGE_FILES,
1902         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1903         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1904         .create_lease_size = sizeof(struct create_lease),
1905 };
1906
1907 struct smb_version_values smb30_values = {
1908         .version_string = SMB30_VERSION_STRING,
1909         .protocol_id = SMB30_PROT_ID,
1910         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
1911         .large_lock_type = 0,
1912         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1913         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1914         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1915         .header_size = sizeof(struct smb2_hdr),
1916         .max_header_size = MAX_SMB2_HDR_SIZE,
1917         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1918         .lock_cmd = SMB2_LOCK,
1919         .cap_unix = 0,
1920         .cap_nt_find = SMB2_NT_FIND,
1921         .cap_large_files = SMB2_LARGE_FILES,
1922         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1923         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1924         .create_lease_size = sizeof(struct create_lease_v2),
1925 };
1926
1927 struct smb_version_values smb302_values = {
1928         .version_string = SMB302_VERSION_STRING,
1929         .protocol_id = SMB302_PROT_ID,
1930         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
1931         .large_lock_type = 0,
1932         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1933         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1934         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1935         .header_size = sizeof(struct smb2_hdr),
1936         .max_header_size = MAX_SMB2_HDR_SIZE,
1937         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1938         .lock_cmd = SMB2_LOCK,
1939         .cap_unix = 0,
1940         .cap_nt_find = SMB2_NT_FIND,
1941         .cap_large_files = SMB2_LARGE_FILES,
1942         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1943         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1944         .create_lease_size = sizeof(struct create_lease_v2),
1945 };
1946
1947 #ifdef CONFIG_CIFS_SMB311
1948 struct smb_version_values smb311_values = {
1949         .version_string = SMB311_VERSION_STRING,
1950         .protocol_id = SMB311_PROT_ID,
1951         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
1952         .large_lock_type = 0,
1953         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1954         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1955         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1956         .header_size = sizeof(struct smb2_hdr),
1957         .max_header_size = MAX_SMB2_HDR_SIZE,
1958         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1959         .lock_cmd = SMB2_LOCK,
1960         .cap_unix = 0,
1961         .cap_nt_find = SMB2_NT_FIND,
1962         .cap_large_files = SMB2_LARGE_FILES,
1963         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1964         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1965         .create_lease_size = sizeof(struct create_lease_v2),
1966 };
1967 #endif /* SMB311 */