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