GNU Linux-libre 4.14.328-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 <linux/scatterlist.h>
24 #include <linux/uuid.h>
25 #include <crypto/aead.h>
26 #include "cifsglob.h"
27 #include "smb2pdu.h"
28 #include "smb2proto.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_unicode.h"
32 #include "smb2status.h"
33 #include "smb2glob.h"
34 #include "cifs_ioctl.h"
35
36 /* Change credits for different ops and return the total number of credits */
37 static int
38 change_conf(struct TCP_Server_Info *server)
39 {
40         server->credits += server->echo_credits + server->oplock_credits;
41         server->oplock_credits = server->echo_credits = 0;
42         switch (server->credits) {
43         case 0:
44                 return 0;
45         case 1:
46                 server->echoes = false;
47                 server->oplocks = false;
48                 break;
49         case 2:
50                 server->echoes = true;
51                 server->oplocks = false;
52                 server->echo_credits = 1;
53                 break;
54         default:
55                 server->echoes = true;
56                 if (enable_oplocks) {
57                         server->oplocks = true;
58                         server->oplock_credits = 1;
59                 } else
60                         server->oplocks = false;
61
62                 server->echo_credits = 1;
63         }
64         server->credits -= server->echo_credits + server->oplock_credits;
65         return server->credits + server->echo_credits + server->oplock_credits;
66 }
67
68 static void
69 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
70                  const int optype)
71 {
72         int *val, rc = -1;
73
74         spin_lock(&server->req_lock);
75         val = server->ops->get_credits_field(server, optype);
76         *val += add;
77         if (*val > 65000) {
78                 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
79                 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
80         }
81         WARN_ON_ONCE(server->in_flight == 0);
82         server->in_flight--;
83         if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
84                 rc = change_conf(server);
85         /*
86          * Sometimes server returns 0 credits on oplock break ack - we need to
87          * rebalance credits in this case.
88          */
89         else if (server->in_flight > 0 && server->oplock_credits == 0 &&
90                  server->oplocks) {
91                 if (server->credits > 1) {
92                         server->credits--;
93                         server->oplock_credits++;
94                 }
95         }
96         spin_unlock(&server->req_lock);
97         wake_up(&server->request_q);
98
99         if (server->tcpStatus == CifsNeedReconnect)
100                 return;
101
102         switch (rc) {
103         case -1:
104                 /* change_conf hasn't been executed */
105                 break;
106         case 0:
107                 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
108                 break;
109         case 1:
110                 cifs_dbg(VFS, "disabling echoes and oplocks\n");
111                 break;
112         case 2:
113                 cifs_dbg(FYI, "disabling oplocks\n");
114                 break;
115         default:
116                 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
117         }
118 }
119
120 static void
121 smb2_set_credits(struct TCP_Server_Info *server, const int val)
122 {
123         spin_lock(&server->req_lock);
124         server->credits = val;
125         spin_unlock(&server->req_lock);
126 }
127
128 static int *
129 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
130 {
131         switch (optype) {
132         case CIFS_ECHO_OP:
133                 return &server->echo_credits;
134         case CIFS_OBREAK_OP:
135                 return &server->oplock_credits;
136         default:
137                 return &server->credits;
138         }
139 }
140
141 static unsigned int
142 smb2_get_credits(struct mid_q_entry *mid)
143 {
144         struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
145
146         return le16_to_cpu(shdr->CreditRequest);
147 }
148
149 static int
150 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
151                       unsigned int *num, unsigned int *credits)
152 {
153         int rc = 0;
154         unsigned int scredits;
155
156         spin_lock(&server->req_lock);
157         while (1) {
158                 if (server->credits <= 0) {
159                         spin_unlock(&server->req_lock);
160                         cifs_num_waiters_inc(server);
161                         rc = wait_event_killable(server->request_q,
162                                         has_credits(server, &server->credits));
163                         cifs_num_waiters_dec(server);
164                         if (rc)
165                                 return rc;
166                         spin_lock(&server->req_lock);
167                 } else {
168                         if (server->tcpStatus == CifsExiting) {
169                                 spin_unlock(&server->req_lock);
170                                 return -ENOENT;
171                         }
172
173                         scredits = server->credits;
174                         /* can deadlock with reopen */
175                         if (scredits <= 8) {
176                                 *num = SMB2_MAX_BUFFER_SIZE;
177                                 *credits = 0;
178                                 break;
179                         }
180
181                         /* leave some credits for reopen and other ops */
182                         scredits -= 8;
183                         *num = min_t(unsigned int, size,
184                                      scredits * SMB2_MAX_BUFFER_SIZE);
185
186                         *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
187                         server->credits -= *credits;
188                         server->in_flight++;
189                         break;
190                 }
191         }
192         spin_unlock(&server->req_lock);
193         return rc;
194 }
195
196 static __u64
197 smb2_get_next_mid(struct TCP_Server_Info *server)
198 {
199         __u64 mid;
200         /* for SMB2 we need the current value */
201         spin_lock(&GlobalMid_Lock);
202         mid = server->CurrentMid++;
203         spin_unlock(&GlobalMid_Lock);
204         return mid;
205 }
206
207 static struct mid_q_entry *
208 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
209 {
210         struct mid_q_entry *mid;
211         struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
212         __u64 wire_mid = le64_to_cpu(shdr->MessageId);
213
214         if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
215                 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
216                 return NULL;
217         }
218
219         spin_lock(&GlobalMid_Lock);
220         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
221                 if ((mid->mid == wire_mid) &&
222                     (mid->mid_state == MID_REQUEST_SUBMITTED) &&
223                     (mid->command == shdr->Command)) {
224                         kref_get(&mid->refcount);
225                         spin_unlock(&GlobalMid_Lock);
226                         return mid;
227                 }
228         }
229         spin_unlock(&GlobalMid_Lock);
230         return NULL;
231 }
232
233 static void
234 smb2_dump_detail(void *buf)
235 {
236 #ifdef CONFIG_CIFS_DEBUG2
237         struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
238
239         cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
240                  shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
241                  shdr->ProcessId);
242         cifs_dbg(VFS, "smb buf %p len %u\n", buf, smb2_calc_size(buf));
243 #endif
244 }
245
246 static bool
247 smb2_need_neg(struct TCP_Server_Info *server)
248 {
249         return server->max_read == 0;
250 }
251
252 static int
253 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
254 {
255         int rc;
256         ses->server->CurrentMid = 0;
257         rc = SMB2_negotiate(xid, ses);
258         /* BB we probably don't need to retry with modern servers */
259         if (rc == -EAGAIN)
260                 rc = -EHOSTDOWN;
261         return rc;
262 }
263
264 static unsigned int
265 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
266 {
267         struct TCP_Server_Info *server = tcon->ses->server;
268         unsigned int wsize;
269
270         /* start with specified wsize, or default */
271         wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
272         wsize = min_t(unsigned int, wsize, server->max_write);
273
274         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
275                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
276
277         return wsize;
278 }
279
280 static unsigned int
281 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
282 {
283         struct TCP_Server_Info *server = tcon->ses->server;
284         unsigned int rsize;
285
286         /* start with specified rsize, or default */
287         rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
288         rsize = min_t(unsigned int, rsize, server->max_read);
289
290         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
291                 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
292
293         return rsize;
294 }
295
296 #ifdef CONFIG_CIFS_STATS2
297 static int
298 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
299 {
300         int rc;
301         unsigned int ret_data_len = 0;
302         struct network_interface_info_ioctl_rsp *out_buf;
303
304         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
305                         FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
306                         false /* use_ipc */,
307                         NULL /* no data input */, 0 /* no data input */,
308                         (char **)&out_buf, &ret_data_len);
309         if (rc != 0)
310                 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
311         else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
312                 cifs_dbg(VFS, "server returned bad net interface info buf\n");
313                 rc = -EINVAL;
314         } else {
315                 /* Dump info on first interface */
316                 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
317                         le32_to_cpu(out_buf->Capability));
318                 cifs_dbg(FYI, "Link Speed %lld\n",
319                         le64_to_cpu(out_buf->LinkSpeed));
320         }
321         kfree(out_buf);
322         return rc;
323 }
324 #endif /* STATS2 */
325
326 static void
327 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
328 {
329         int rc;
330         __le16 srch_path = 0; /* Null - open root of share */
331         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
332         struct cifs_open_parms oparms;
333         struct cifs_fid fid;
334
335         oparms.tcon = tcon;
336         oparms.desired_access = FILE_READ_ATTRIBUTES;
337         oparms.disposition = FILE_OPEN;
338         oparms.create_options = 0;
339         oparms.fid = &fid;
340         oparms.reconnect = false;
341
342         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
343         if (rc)
344                 return;
345
346 #ifdef CONFIG_CIFS_STATS2
347         SMB3_request_interfaces(xid, tcon);
348 #endif /* STATS2 */
349
350         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
351                         FS_ATTRIBUTE_INFORMATION);
352         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
353                         FS_DEVICE_INFORMATION);
354         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
355                         FS_VOLUME_INFORMATION);
356         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
357                         FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
358         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
359         return;
360 }
361
362 static void
363 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
364 {
365         int rc;
366         __le16 srch_path = 0; /* Null - open root of share */
367         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
368         struct cifs_open_parms oparms;
369         struct cifs_fid fid;
370
371         oparms.tcon = tcon;
372         oparms.desired_access = FILE_READ_ATTRIBUTES;
373         oparms.disposition = FILE_OPEN;
374         oparms.create_options = 0;
375         oparms.fid = &fid;
376         oparms.reconnect = false;
377
378         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
379         if (rc)
380                 return;
381
382         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
383                         FS_ATTRIBUTE_INFORMATION);
384         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
385                         FS_DEVICE_INFORMATION);
386         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
387         return;
388 }
389
390 static int
391 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
392                         struct cifs_sb_info *cifs_sb, const char *full_path)
393 {
394         int rc;
395         __le16 *utf16_path;
396         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
397         struct cifs_open_parms oparms;
398         struct cifs_fid fid;
399
400         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
401         if (!utf16_path)
402                 return -ENOMEM;
403
404         oparms.tcon = tcon;
405         oparms.desired_access = FILE_READ_ATTRIBUTES;
406         oparms.disposition = FILE_OPEN;
407         if (backup_cred(cifs_sb))
408                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
409         else
410                 oparms.create_options = 0;
411         oparms.fid = &fid;
412         oparms.reconnect = false;
413
414         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
415         if (rc) {
416                 kfree(utf16_path);
417                 return rc;
418         }
419
420         rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
421         kfree(utf16_path);
422         return rc;
423 }
424
425 static int
426 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
427                   struct cifs_sb_info *cifs_sb, const char *full_path,
428                   u64 *uniqueid, FILE_ALL_INFO *data)
429 {
430         *uniqueid = le64_to_cpu(data->IndexNumber);
431         return 0;
432 }
433
434 static int
435 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
436                      struct cifs_fid *fid, FILE_ALL_INFO *data)
437 {
438         int rc;
439         struct smb2_file_all_info *smb2_data;
440
441         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
442                             GFP_KERNEL);
443         if (smb2_data == NULL)
444                 return -ENOMEM;
445
446         rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
447                              smb2_data);
448         if (!rc)
449                 move_smb2_info_to_cifs(data, smb2_data);
450         kfree(smb2_data);
451         return rc;
452 }
453
454 #ifdef CONFIG_CIFS_XATTR
455 static ssize_t
456 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
457                      struct smb2_file_full_ea_info *src, size_t src_size,
458                      const unsigned char *ea_name)
459 {
460         int rc = 0;
461         unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
462         char *name, *value;
463         size_t buf_size = dst_size;
464         size_t name_len, value_len, user_name_len;
465
466         while (src_size > 0) {
467                 name_len = (size_t)src->ea_name_length;
468                 value_len = (size_t)le16_to_cpu(src->ea_value_length);
469
470                 if (name_len == 0) {
471                         break;
472                 }
473
474                 if (src_size < 8 + name_len + 1 + value_len) {
475                         cifs_dbg(FYI, "EA entry goes beyond length of list\n");
476                         rc = -EIO;
477                         goto out;
478                 }
479
480                 name = &src->ea_data[0];
481                 value = &src->ea_data[src->ea_name_length + 1];
482
483                 if (ea_name) {
484                         if (ea_name_len == name_len &&
485                             memcmp(ea_name, name, name_len) == 0) {
486                                 rc = value_len;
487                                 if (dst_size == 0)
488                                         goto out;
489                                 if (dst_size < value_len) {
490                                         rc = -ERANGE;
491                                         goto out;
492                                 }
493                                 memcpy(dst, value, value_len);
494                                 goto out;
495                         }
496                 } else {
497                         /* 'user.' plus a terminating null */
498                         user_name_len = 5 + 1 + name_len;
499
500                         if (buf_size == 0) {
501                                 /* skip copy - calc size only */
502                                 rc += user_name_len;
503                         } else if (dst_size >= user_name_len) {
504                                 dst_size -= user_name_len;
505                                 memcpy(dst, "user.", 5);
506                                 dst += 5;
507                                 memcpy(dst, src->ea_data, name_len);
508                                 dst += name_len;
509                                 *dst = 0;
510                                 ++dst;
511                                 rc += user_name_len;
512                         } else {
513                                 /* stop before overrun buffer */
514                                 rc = -ERANGE;
515                                 break;
516                         }
517                 }
518
519                 if (!src->next_entry_offset)
520                         break;
521
522                 if (src_size < le32_to_cpu(src->next_entry_offset)) {
523                         /* stop before overrun buffer */
524                         rc = -ERANGE;
525                         break;
526                 }
527                 src_size -= le32_to_cpu(src->next_entry_offset);
528                 src = (void *)((char *)src +
529                                le32_to_cpu(src->next_entry_offset));
530         }
531
532         /* didn't find the named attribute */
533         if (ea_name)
534                 rc = -ENODATA;
535
536 out:
537         return (ssize_t)rc;
538 }
539
540 static ssize_t
541 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
542                const unsigned char *path, const unsigned char *ea_name,
543                char *ea_data, size_t buf_size,
544                struct cifs_sb_info *cifs_sb)
545 {
546         int rc;
547         __le16 *utf16_path;
548         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
549         struct cifs_open_parms oparms;
550         struct cifs_fid fid;
551         struct smb2_file_full_ea_info *smb2_data;
552         int ea_buf_size = SMB2_MIN_EA_BUF;
553
554         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
555         if (!utf16_path)
556                 return -ENOMEM;
557
558         oparms.tcon = tcon;
559         oparms.desired_access = FILE_READ_EA;
560         oparms.disposition = FILE_OPEN;
561         if (backup_cred(cifs_sb))
562                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
563         else
564                 oparms.create_options = 0;
565         oparms.fid = &fid;
566         oparms.reconnect = false;
567
568         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
569         kfree(utf16_path);
570         if (rc) {
571                 cifs_dbg(FYI, "open failed rc=%d\n", rc);
572                 return rc;
573         }
574
575         while (1) {
576                 smb2_data = kzalloc(ea_buf_size, GFP_KERNEL);
577                 if (smb2_data == NULL) {
578                         SMB2_close(xid, tcon, fid.persistent_fid,
579                                    fid.volatile_fid);
580                         return -ENOMEM;
581                 }
582
583                 rc = SMB2_query_eas(xid, tcon, fid.persistent_fid,
584                                     fid.volatile_fid,
585                                     ea_buf_size, smb2_data);
586
587                 if (rc != -E2BIG)
588                         break;
589
590                 kfree(smb2_data);
591                 ea_buf_size <<= 1;
592
593                 if (ea_buf_size > SMB2_MAX_EA_BUF) {
594                         cifs_dbg(VFS, "EA size is too large\n");
595                         SMB2_close(xid, tcon, fid.persistent_fid,
596                                    fid.volatile_fid);
597                         return -ENOMEM;
598                 }
599         }
600
601         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
602
603         /*
604          * If ea_name is NULL (listxattr) and there are no EAs, return 0 as it's
605          * not an error. Otherwise, the specified ea_name was not found.
606          */
607         if (!rc)
608                 rc = move_smb2_ea_to_cifs(ea_data, buf_size, smb2_data,
609                                           SMB2_MAX_EA_BUF, ea_name);
610         else if (!ea_name && rc == -ENODATA)
611                 rc = 0;
612
613         kfree(smb2_data);
614         return rc;
615 }
616
617
618 static int
619 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
620             const char *path, const char *ea_name, const void *ea_value,
621             const __u16 ea_value_len, const struct nls_table *nls_codepage,
622             struct cifs_sb_info *cifs_sb)
623 {
624         int rc;
625         __le16 *utf16_path;
626         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
627         struct cifs_open_parms oparms;
628         struct cifs_fid fid;
629         struct smb2_file_full_ea_info *ea;
630         int ea_name_len = strlen(ea_name);
631         int len;
632
633         if (ea_name_len > 255)
634                 return -EINVAL;
635
636         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
637         if (!utf16_path)
638                 return -ENOMEM;
639
640         oparms.tcon = tcon;
641         oparms.desired_access = FILE_WRITE_EA;
642         oparms.disposition = FILE_OPEN;
643         if (backup_cred(cifs_sb))
644                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
645         else
646                 oparms.create_options = 0;
647         oparms.fid = &fid;
648         oparms.reconnect = false;
649
650         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
651         kfree(utf16_path);
652         if (rc) {
653                 cifs_dbg(FYI, "open failed rc=%d\n", rc);
654                 return rc;
655         }
656
657         len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
658         ea = kzalloc(len, GFP_KERNEL);
659         if (ea == NULL) {
660                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
661                 return -ENOMEM;
662         }
663
664         ea->ea_name_length = ea_name_len;
665         ea->ea_value_length = cpu_to_le16(ea_value_len);
666         memcpy(ea->ea_data, ea_name, ea_name_len + 1);
667         memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
668
669         rc = SMB2_set_ea(xid, tcon, fid.persistent_fid, fid.volatile_fid, ea,
670                          len);
671         kfree(ea);
672
673         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
674
675         return rc;
676 }
677 #endif
678
679 static bool
680 smb2_can_echo(struct TCP_Server_Info *server)
681 {
682         return server->echoes;
683 }
684
685 static void
686 smb2_clear_stats(struct cifs_tcon *tcon)
687 {
688 #ifdef CONFIG_CIFS_STATS
689         int i;
690         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
691                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
692                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
693         }
694 #endif
695 }
696
697 static void
698 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
699 {
700         seq_puts(m, "\n\tShare Capabilities:");
701         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
702                 seq_puts(m, " DFS,");
703         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
704                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
705         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
706                 seq_puts(m, " SCALEOUT,");
707         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
708                 seq_puts(m, " CLUSTER,");
709         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
710                 seq_puts(m, " ASYMMETRIC,");
711         if (tcon->capabilities == 0)
712                 seq_puts(m, " None");
713         if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
714                 seq_puts(m, " Aligned,");
715         if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
716                 seq_puts(m, " Partition Aligned,");
717         if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
718                 seq_puts(m, " SSD,");
719         if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
720                 seq_puts(m, " TRIM-support,");
721
722         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
723         if (tcon->perf_sector_size)
724                 seq_printf(m, "\tOptimal sector size: 0x%x",
725                            tcon->perf_sector_size);
726 }
727
728 static void
729 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
730 {
731 #ifdef CONFIG_CIFS_STATS
732         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
733         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
734         seq_printf(m, "\nNegotiates: %d sent %d failed",
735                    atomic_read(&sent[SMB2_NEGOTIATE_HE]),
736                    atomic_read(&failed[SMB2_NEGOTIATE_HE]));
737         seq_printf(m, "\nSessionSetups: %d sent %d failed",
738                    atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
739                    atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
740         seq_printf(m, "\nLogoffs: %d sent %d failed",
741                    atomic_read(&sent[SMB2_LOGOFF_HE]),
742                    atomic_read(&failed[SMB2_LOGOFF_HE]));
743         seq_printf(m, "\nTreeConnects: %d sent %d failed",
744                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
745                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
746         seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
747                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
748                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
749         seq_printf(m, "\nCreates: %d sent %d failed",
750                    atomic_read(&sent[SMB2_CREATE_HE]),
751                    atomic_read(&failed[SMB2_CREATE_HE]));
752         seq_printf(m, "\nCloses: %d sent %d failed",
753                    atomic_read(&sent[SMB2_CLOSE_HE]),
754                    atomic_read(&failed[SMB2_CLOSE_HE]));
755         seq_printf(m, "\nFlushes: %d sent %d failed",
756                    atomic_read(&sent[SMB2_FLUSH_HE]),
757                    atomic_read(&failed[SMB2_FLUSH_HE]));
758         seq_printf(m, "\nReads: %d sent %d failed",
759                    atomic_read(&sent[SMB2_READ_HE]),
760                    atomic_read(&failed[SMB2_READ_HE]));
761         seq_printf(m, "\nWrites: %d sent %d failed",
762                    atomic_read(&sent[SMB2_WRITE_HE]),
763                    atomic_read(&failed[SMB2_WRITE_HE]));
764         seq_printf(m, "\nLocks: %d sent %d failed",
765                    atomic_read(&sent[SMB2_LOCK_HE]),
766                    atomic_read(&failed[SMB2_LOCK_HE]));
767         seq_printf(m, "\nIOCTLs: %d sent %d failed",
768                    atomic_read(&sent[SMB2_IOCTL_HE]),
769                    atomic_read(&failed[SMB2_IOCTL_HE]));
770         seq_printf(m, "\nCancels: %d sent %d failed",
771                    atomic_read(&sent[SMB2_CANCEL_HE]),
772                    atomic_read(&failed[SMB2_CANCEL_HE]));
773         seq_printf(m, "\nEchos: %d sent %d failed",
774                    atomic_read(&sent[SMB2_ECHO_HE]),
775                    atomic_read(&failed[SMB2_ECHO_HE]));
776         seq_printf(m, "\nQueryDirectories: %d sent %d failed",
777                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
778                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
779         seq_printf(m, "\nChangeNotifies: %d sent %d failed",
780                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
781                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
782         seq_printf(m, "\nQueryInfos: %d sent %d failed",
783                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
784                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
785         seq_printf(m, "\nSetInfos: %d sent %d failed",
786                    atomic_read(&sent[SMB2_SET_INFO_HE]),
787                    atomic_read(&failed[SMB2_SET_INFO_HE]));
788         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
789                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
790                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
791 #endif
792 }
793
794 static void
795 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
796 {
797         struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
798         struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
799
800         cfile->fid.persistent_fid = fid->persistent_fid;
801         cfile->fid.volatile_fid = fid->volatile_fid;
802         server->ops->set_oplock_level(cinode, oplock, fid->epoch,
803                                       &fid->purge_cache);
804         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
805         memcpy(cfile->fid.create_guid, fid->create_guid, 16);
806 }
807
808 static void
809 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
810                 struct cifs_fid *fid)
811 {
812         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
813 }
814
815 static int
816 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
817                      u64 persistent_fid, u64 volatile_fid,
818                      struct copychunk_ioctl *pcchunk)
819 {
820         int rc;
821         unsigned int ret_data_len;
822         struct resume_key_req *res_key;
823
824         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
825                         FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
826                         false /* use_ipc */,
827                         NULL, 0 /* no input */,
828                         (char **)&res_key, &ret_data_len);
829
830         if (rc) {
831                 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
832                 goto req_res_key_exit;
833         }
834         if (ret_data_len < sizeof(struct resume_key_req)) {
835                 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
836                 rc = -EINVAL;
837                 goto req_res_key_exit;
838         }
839         memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
840
841 req_res_key_exit:
842         kfree(res_key);
843         return rc;
844 }
845
846 static ssize_t
847 smb2_copychunk_range(const unsigned int xid,
848                         struct cifsFileInfo *srcfile,
849                         struct cifsFileInfo *trgtfile, u64 src_off,
850                         u64 len, u64 dest_off)
851 {
852         int rc;
853         unsigned int ret_data_len;
854         struct copychunk_ioctl *pcchunk;
855         struct copychunk_ioctl_rsp *retbuf = NULL;
856         struct cifs_tcon *tcon;
857         int chunks_copied = 0;
858         bool chunk_sizes_updated = false;
859         ssize_t bytes_written, total_bytes_written = 0;
860         struct inode *inode;
861
862         pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
863
864         /*
865          * We need to flush all unwritten data before we can send the
866          * copychunk ioctl to the server.
867          */
868         inode = d_inode(trgtfile->dentry);
869         filemap_write_and_wait(inode->i_mapping);
870
871         if (pcchunk == NULL)
872                 return -ENOMEM;
873
874         cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
875         /* Request a key from the server to identify the source of the copy */
876         rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
877                                 srcfile->fid.persistent_fid,
878                                 srcfile->fid.volatile_fid, pcchunk);
879
880         /* Note: request_res_key sets res_key null only if rc !=0 */
881         if (rc)
882                 goto cchunk_out;
883
884         /* For now array only one chunk long, will make more flexible later */
885         pcchunk->ChunkCount = cpu_to_le32(1);
886         pcchunk->Reserved = 0;
887         pcchunk->Reserved2 = 0;
888
889         tcon = tlink_tcon(trgtfile->tlink);
890
891         while (len > 0) {
892                 pcchunk->SourceOffset = cpu_to_le64(src_off);
893                 pcchunk->TargetOffset = cpu_to_le64(dest_off);
894                 pcchunk->Length =
895                         cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
896
897                 /* Request server copy to target from src identified by key */
898                 kfree(retbuf);
899                 retbuf = NULL;
900                 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
901                         trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
902                         true /* is_fsctl */, false /* use_ipc */,
903                         (char *)pcchunk,
904                         sizeof(struct copychunk_ioctl), (char **)&retbuf,
905                         &ret_data_len);
906                 if (rc == 0) {
907                         if (ret_data_len !=
908                                         sizeof(struct copychunk_ioctl_rsp)) {
909                                 cifs_dbg(VFS, "invalid cchunk response size\n");
910                                 rc = -EIO;
911                                 goto cchunk_out;
912                         }
913                         if (retbuf->TotalBytesWritten == 0) {
914                                 cifs_dbg(FYI, "no bytes copied\n");
915                                 rc = -EIO;
916                                 goto cchunk_out;
917                         }
918                         /*
919                          * Check if server claimed to write more than we asked
920                          */
921                         if (le32_to_cpu(retbuf->TotalBytesWritten) >
922                             le32_to_cpu(pcchunk->Length)) {
923                                 cifs_dbg(VFS, "invalid copy chunk response\n");
924                                 rc = -EIO;
925                                 goto cchunk_out;
926                         }
927                         if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
928                                 cifs_dbg(VFS, "invalid num chunks written\n");
929                                 rc = -EIO;
930                                 goto cchunk_out;
931                         }
932                         chunks_copied++;
933
934                         bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
935                         src_off += bytes_written;
936                         dest_off += bytes_written;
937                         len -= bytes_written;
938                         total_bytes_written += bytes_written;
939
940                         cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
941                                 le32_to_cpu(retbuf->ChunksWritten),
942                                 le32_to_cpu(retbuf->ChunkBytesWritten),
943                                 bytes_written);
944                 } else if (rc == -EINVAL) {
945                         if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
946                                 goto cchunk_out;
947
948                         cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
949                                 le32_to_cpu(retbuf->ChunksWritten),
950                                 le32_to_cpu(retbuf->ChunkBytesWritten),
951                                 le32_to_cpu(retbuf->TotalBytesWritten));
952
953                         /*
954                          * Check if this is the first request using these sizes,
955                          * (ie check if copy succeed once with original sizes
956                          * and check if the server gave us different sizes after
957                          * we already updated max sizes on previous request).
958                          * if not then why is the server returning an error now
959                          */
960                         if ((chunks_copied != 0) || chunk_sizes_updated)
961                                 goto cchunk_out;
962
963                         /* Check that server is not asking us to grow size */
964                         if (le32_to_cpu(retbuf->ChunkBytesWritten) <
965                                         tcon->max_bytes_chunk)
966                                 tcon->max_bytes_chunk =
967                                         le32_to_cpu(retbuf->ChunkBytesWritten);
968                         else
969                                 goto cchunk_out; /* server gave us bogus size */
970
971                         /* No need to change MaxChunks since already set to 1 */
972                         chunk_sizes_updated = true;
973                 } else
974                         goto cchunk_out;
975         }
976
977 cchunk_out:
978         kfree(pcchunk);
979         kfree(retbuf);
980         if (rc)
981                 return rc;
982         else
983                 return total_bytes_written;
984 }
985
986 static int
987 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
988                 struct cifs_fid *fid)
989 {
990         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
991 }
992
993 static unsigned int
994 smb2_read_data_offset(char *buf)
995 {
996         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
997         return rsp->DataOffset;
998 }
999
1000 static unsigned int
1001 smb2_read_data_length(char *buf)
1002 {
1003         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1004         return le32_to_cpu(rsp->DataLength);
1005 }
1006
1007
1008 static int
1009 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1010                struct cifs_io_parms *parms, unsigned int *bytes_read,
1011                char **buf, int *buf_type)
1012 {
1013         parms->persistent_fid = pfid->persistent_fid;
1014         parms->volatile_fid = pfid->volatile_fid;
1015         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1016 }
1017
1018 static int
1019 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1020                 struct cifs_io_parms *parms, unsigned int *written,
1021                 struct kvec *iov, unsigned long nr_segs)
1022 {
1023
1024         parms->persistent_fid = pfid->persistent_fid;
1025         parms->volatile_fid = pfid->volatile_fid;
1026         return SMB2_write(xid, parms, written, iov, nr_segs);
1027 }
1028
1029 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1030 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1031                 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1032 {
1033         struct cifsInodeInfo *cifsi;
1034         int rc;
1035
1036         cifsi = CIFS_I(inode);
1037
1038         /* if file already sparse don't bother setting sparse again */
1039         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1040                 return true; /* already sparse */
1041
1042         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1043                 return true; /* already not sparse */
1044
1045         /*
1046          * Can't check for sparse support on share the usual way via the
1047          * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1048          * since Samba server doesn't set the flag on the share, yet
1049          * supports the set sparse FSCTL and returns sparse correctly
1050          * in the file attributes. If we fail setting sparse though we
1051          * mark that server does not support sparse files for this share
1052          * to avoid repeatedly sending the unsupported fsctl to server
1053          * if the file is repeatedly extended.
1054          */
1055         if (tcon->broken_sparse_sup)
1056                 return false;
1057
1058         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1059                         cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1060                         true /* is_fctl */, false /* use_ipc */,
1061                         &setsparse, 1, NULL, NULL);
1062         if (rc) {
1063                 tcon->broken_sparse_sup = true;
1064                 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1065                 return false;
1066         }
1067
1068         if (setsparse)
1069                 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1070         else
1071                 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1072
1073         return true;
1074 }
1075
1076 static int
1077 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1078                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1079 {
1080         __le64 eof = cpu_to_le64(size);
1081         struct inode *inode;
1082
1083         /*
1084          * If extending file more than one page make sparse. Many Linux fs
1085          * make files sparse by default when extending via ftruncate
1086          */
1087         inode = d_inode(cfile->dentry);
1088
1089         if (!set_alloc && (size > inode->i_size + 8192)) {
1090                 __u8 set_sparse = 1;
1091
1092                 /* whether set sparse succeeds or not, extend the file */
1093                 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1094         }
1095
1096         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1097                             cfile->fid.volatile_fid, cfile->pid, &eof, false);
1098 }
1099
1100 static int
1101 smb2_duplicate_extents(const unsigned int xid,
1102                         struct cifsFileInfo *srcfile,
1103                         struct cifsFileInfo *trgtfile, u64 src_off,
1104                         u64 len, u64 dest_off)
1105 {
1106         int rc;
1107         unsigned int ret_data_len;
1108         struct duplicate_extents_to_file dup_ext_buf;
1109         struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1110
1111         /* server fileays advertise duplicate extent support with this flag */
1112         if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1113              FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1114                 return -EOPNOTSUPP;
1115
1116         dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1117         dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1118         dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1119         dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1120         dup_ext_buf.ByteCount = cpu_to_le64(len);
1121         cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1122                 src_off, dest_off, len);
1123
1124         rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1125         if (rc)
1126                 goto duplicate_extents_out;
1127
1128         rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1129                         trgtfile->fid.volatile_fid,
1130                         FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1131                         true /* is_fsctl */, false /* use_ipc */,
1132                         (char *)&dup_ext_buf,
1133                         sizeof(struct duplicate_extents_to_file),
1134                         NULL,
1135                         &ret_data_len);
1136
1137         if (ret_data_len > 0)
1138                 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1139
1140 duplicate_extents_out:
1141         return rc;
1142 }
1143
1144 static int
1145 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1146                    struct cifsFileInfo *cfile)
1147 {
1148         return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1149                             cfile->fid.volatile_fid);
1150 }
1151
1152 static int
1153 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1154                    struct cifsFileInfo *cfile)
1155 {
1156         struct fsctl_set_integrity_information_req integr_info;
1157         unsigned int ret_data_len;
1158
1159         integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1160         integr_info.Flags = 0;
1161         integr_info.Reserved = 0;
1162
1163         return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1164                         cfile->fid.volatile_fid,
1165                         FSCTL_SET_INTEGRITY_INFORMATION,
1166                         true /* is_fsctl */, false /* use_ipc */,
1167                         (char *)&integr_info,
1168                         sizeof(struct fsctl_set_integrity_information_req),
1169                         NULL,
1170                         &ret_data_len);
1171
1172 }
1173
1174 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1175 #define GMT_TOKEN_SIZE 50
1176
1177 /*
1178  * Input buffer contains (empty) struct smb_snapshot array with size filled in
1179  * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1180  */
1181 static int
1182 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1183                    struct cifsFileInfo *cfile, void __user *ioc_buf)
1184 {
1185         char *retbuf = NULL;
1186         unsigned int ret_data_len = 0;
1187         int rc;
1188         struct smb_snapshot_array snapshot_in;
1189
1190         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1191                         cfile->fid.volatile_fid,
1192                         FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1193                         true /* is_fsctl */, false /* use_ipc */,
1194                         NULL, 0 /* no input data */,
1195                         (char **)&retbuf,
1196                         &ret_data_len);
1197         cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1198                         rc, ret_data_len);
1199         if (rc)
1200                 return rc;
1201
1202         if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1203                 /* Fixup buffer */
1204                 if (copy_from_user(&snapshot_in, ioc_buf,
1205                     sizeof(struct smb_snapshot_array))) {
1206                         rc = -EFAULT;
1207                         kfree(retbuf);
1208                         return rc;
1209                 }
1210
1211                 /*
1212                  * Check for min size, ie not large enough to fit even one GMT
1213                  * token (snapshot).  On the first ioctl some users may pass in
1214                  * smaller size (or zero) to simply get the size of the array
1215                  * so the user space caller can allocate sufficient memory
1216                  * and retry the ioctl again with larger array size sufficient
1217                  * to hold all of the snapshot GMT tokens on the second try.
1218                  */
1219                 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1220                         ret_data_len = sizeof(struct smb_snapshot_array);
1221
1222                 /*
1223                  * We return struct SRV_SNAPSHOT_ARRAY, followed by
1224                  * the snapshot array (of 50 byte GMT tokens) each
1225                  * representing an available previous version of the data
1226                  */
1227                 if (ret_data_len > (snapshot_in.snapshot_array_size +
1228                                         sizeof(struct smb_snapshot_array)))
1229                         ret_data_len = snapshot_in.snapshot_array_size +
1230                                         sizeof(struct smb_snapshot_array);
1231
1232                 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1233                         rc = -EFAULT;
1234         }
1235
1236         kfree(retbuf);
1237         return rc;
1238 }
1239
1240 static int
1241 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1242                      const char *path, struct cifs_sb_info *cifs_sb,
1243                      struct cifs_fid *fid, __u16 search_flags,
1244                      struct cifs_search_info *srch_inf)
1245 {
1246         __le16 *utf16_path;
1247         int rc;
1248         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1249         struct cifs_open_parms oparms;
1250
1251         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1252         if (!utf16_path)
1253                 return -ENOMEM;
1254
1255         oparms.tcon = tcon;
1256         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1257         oparms.disposition = FILE_OPEN;
1258         if (backup_cred(cifs_sb))
1259                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1260         else
1261                 oparms.create_options = 0;
1262         oparms.fid = fid;
1263         oparms.reconnect = false;
1264
1265         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1266         kfree(utf16_path);
1267         if (rc) {
1268                 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1269                 return rc;
1270         }
1271
1272         srch_inf->entries_in_buffer = 0;
1273         srch_inf->index_of_last_entry = 2;
1274
1275         rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1276                                   fid->volatile_fid, 0, srch_inf);
1277         if (rc) {
1278                 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1279                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1280         }
1281         return rc;
1282 }
1283
1284 static int
1285 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1286                     struct cifs_fid *fid, __u16 search_flags,
1287                     struct cifs_search_info *srch_inf)
1288 {
1289         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1290                                     fid->volatile_fid, 0, srch_inf);
1291 }
1292
1293 static int
1294 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1295                struct cifs_fid *fid)
1296 {
1297         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1298 }
1299
1300 /*
1301 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1302 * the number of credits and return true. Otherwise - return false.
1303 */
1304 static bool
1305 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1306 {
1307         struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1308
1309         if (shdr->Status != STATUS_PENDING)
1310                 return false;
1311
1312         if (!length) {
1313                 spin_lock(&server->req_lock);
1314                 server->credits += le16_to_cpu(shdr->CreditRequest);
1315                 spin_unlock(&server->req_lock);
1316                 wake_up(&server->request_q);
1317         }
1318
1319         return true;
1320 }
1321
1322 static bool
1323 smb2_is_session_expired(char *buf)
1324 {
1325         struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1326
1327         if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
1328             shdr->Status != STATUS_USER_SESSION_DELETED)
1329                 return false;
1330
1331         cifs_dbg(FYI, "Session expired or deleted\n");
1332         return true;
1333 }
1334
1335 static int
1336 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1337                      struct cifsInodeInfo *cinode)
1338 {
1339         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1340                 return SMB2_lease_break(0, tcon, cinode->lease_key,
1341                                         smb2_get_lease_state(cinode));
1342
1343         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1344                                  fid->volatile_fid,
1345                                  CIFS_CACHE_READ(cinode) ? 1 : 0);
1346 }
1347
1348 static int
1349 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1350              struct kstatfs *buf)
1351 {
1352         int rc;
1353         __le16 srch_path = 0; /* Null - open root of share */
1354         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1355         struct cifs_open_parms oparms;
1356         struct cifs_fid fid;
1357
1358         oparms.tcon = tcon;
1359         oparms.desired_access = FILE_READ_ATTRIBUTES;
1360         oparms.disposition = FILE_OPEN;
1361         oparms.create_options = 0;
1362         oparms.fid = &fid;
1363         oparms.reconnect = false;
1364
1365         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
1366         if (rc)
1367                 return rc;
1368         buf->f_type = SMB2_MAGIC_NUMBER;
1369         rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1370                            buf);
1371         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1372         return rc;
1373 }
1374
1375 static bool
1376 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1377 {
1378         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1379                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1380 }
1381
1382 static int
1383 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1384                __u64 length, __u32 type, int lock, int unlock, bool wait)
1385 {
1386         if (unlock && !lock)
1387                 type = SMB2_LOCKFLAG_UNLOCK;
1388         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1389                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1390                          current->tgid, length, offset, type, wait);
1391 }
1392
1393 static void
1394 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1395 {
1396         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1397 }
1398
1399 static void
1400 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1401 {
1402         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1403 }
1404
1405 static void
1406 smb2_new_lease_key(struct cifs_fid *fid)
1407 {
1408         generate_random_uuid(fid->lease_key);
1409 }
1410
1411 static int
1412 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
1413                    const char *search_name,
1414                    struct dfs_info3_param **target_nodes,
1415                    unsigned int *num_of_nodes,
1416                    const struct nls_table *nls_codepage, int remap)
1417 {
1418         int rc;
1419         __le16 *utf16_path = NULL;
1420         int utf16_path_len = 0;
1421         struct cifs_tcon *tcon;
1422         struct fsctl_get_dfs_referral_req *dfs_req = NULL;
1423         struct get_dfs_referral_rsp *dfs_rsp = NULL;
1424         u32 dfs_req_size = 0, dfs_rsp_size = 0;
1425
1426         cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
1427
1428         /*
1429          * Use any tcon from the current session. Here, the first one.
1430          */
1431         spin_lock(&cifs_tcp_ses_lock);
1432         tcon = list_first_entry_or_null(&ses->tcon_list, struct cifs_tcon,
1433                                         tcon_list);
1434         if (tcon)
1435                 tcon->tc_count++;
1436         spin_unlock(&cifs_tcp_ses_lock);
1437
1438         if (!tcon) {
1439                 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
1440                          ses);
1441                 rc = -ENOTCONN;
1442                 goto out;
1443         }
1444
1445         utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
1446                                            &utf16_path_len,
1447                                            nls_codepage, remap);
1448         if (!utf16_path) {
1449                 rc = -ENOMEM;
1450                 goto out;
1451         }
1452
1453         dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
1454         dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
1455         if (!dfs_req) {
1456                 rc = -ENOMEM;
1457                 goto out;
1458         }
1459
1460         /* Highest DFS referral version understood */
1461         dfs_req->MaxReferralLevel = DFS_VERSION;
1462
1463         /* Path to resolve in an UTF-16 null-terminated string */
1464         memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
1465
1466         do {
1467                 /* try first with IPC */
1468                 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1469                                 FSCTL_DFS_GET_REFERRALS,
1470                                 true /* is_fsctl */, true /* use_ipc */,
1471                                 (char *)dfs_req, dfs_req_size,
1472                                 (char **)&dfs_rsp, &dfs_rsp_size);
1473                 if (rc == -ENOTCONN) {
1474                         /* try with normal tcon */
1475                         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
1476                                         FSCTL_DFS_GET_REFERRALS,
1477                                         true /* is_fsctl */, false /*use_ipc*/,
1478                                         (char *)dfs_req, dfs_req_size,
1479                                         (char **)&dfs_rsp, &dfs_rsp_size);
1480                 }
1481         } while (rc == -EAGAIN);
1482
1483         if (rc) {
1484                 cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
1485                 goto out;
1486         }
1487
1488         rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
1489                                  num_of_nodes, target_nodes,
1490                                  nls_codepage, remap, search_name,
1491                                  true /* is_unicode */);
1492         if (rc) {
1493                 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
1494                 goto out;
1495         }
1496
1497  out:
1498         if (tcon) {
1499                 spin_lock(&cifs_tcp_ses_lock);
1500                 tcon->tc_count--;
1501                 spin_unlock(&cifs_tcp_ses_lock);
1502         }
1503         kfree(utf16_path);
1504         kfree(dfs_req);
1505         kfree(dfs_rsp);
1506         return rc;
1507 }
1508 #define SMB2_SYMLINK_STRUCT_SIZE \
1509         (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1510
1511 static int
1512 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1513                    const char *full_path, char **target_path,
1514                    struct cifs_sb_info *cifs_sb)
1515 {
1516         int rc;
1517         __le16 *utf16_path;
1518         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1519         struct cifs_open_parms oparms;
1520         struct cifs_fid fid;
1521         struct smb2_err_rsp *err_buf = NULL;
1522         struct smb2_symlink_err_rsp *symlink;
1523         unsigned int sub_len;
1524         unsigned int sub_offset;
1525         unsigned int print_len;
1526         unsigned int print_offset;
1527
1528         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1529
1530         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1531         if (!utf16_path)
1532                 return -ENOMEM;
1533
1534         oparms.tcon = tcon;
1535         oparms.desired_access = FILE_READ_ATTRIBUTES;
1536         oparms.disposition = FILE_OPEN;
1537         if (backup_cred(cifs_sb))
1538                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1539         else
1540                 oparms.create_options = 0;
1541         oparms.fid = &fid;
1542         oparms.reconnect = false;
1543
1544         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1545
1546         if (!rc || !err_buf) {
1547                 kfree(utf16_path);
1548                 return -ENOENT;
1549         }
1550
1551         if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1552             get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1553                 kfree(utf16_path);
1554                 return -ENOENT;
1555         }
1556
1557         /* open must fail on symlink - reset rc */
1558         rc = 0;
1559         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1560         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1561         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1562         print_len = le16_to_cpu(symlink->PrintNameLength);
1563         print_offset = le16_to_cpu(symlink->PrintNameOffset);
1564
1565         if (get_rfc1002_length(err_buf) + 4 <
1566                         SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1567                 kfree(utf16_path);
1568                 return -ENOENT;
1569         }
1570
1571         if (get_rfc1002_length(err_buf) + 4 <
1572                         SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1573                 kfree(utf16_path);
1574                 return -ENOENT;
1575         }
1576
1577         *target_path = cifs_strndup_from_utf16(
1578                                 (char *)symlink->PathBuffer + sub_offset,
1579                                 sub_len, true, cifs_sb->local_nls);
1580         if (!(*target_path)) {
1581                 kfree(utf16_path);
1582                 return -ENOMEM;
1583         }
1584         convert_delimiter(*target_path, '/');
1585         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1586         kfree(utf16_path);
1587         return rc;
1588 }
1589
1590 #ifdef CONFIG_CIFS_ACL
1591 static struct cifs_ntsd *
1592 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
1593                 const struct cifs_fid *cifsfid, u32 *pacllen)
1594 {
1595         struct cifs_ntsd *pntsd = NULL;
1596         unsigned int xid;
1597         int rc = -EOPNOTSUPP;
1598         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1599
1600         if (IS_ERR(tlink))
1601                 return ERR_CAST(tlink);
1602
1603         xid = get_xid();
1604         cifs_dbg(FYI, "trying to get acl\n");
1605
1606         rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
1607                             cifsfid->volatile_fid, (void **)&pntsd, pacllen);
1608         free_xid(xid);
1609
1610         cifs_put_tlink(tlink);
1611
1612         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1613         if (rc)
1614                 return ERR_PTR(rc);
1615         return pntsd;
1616
1617 }
1618
1619 static struct cifs_ntsd *
1620 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
1621                 const char *path, u32 *pacllen)
1622 {
1623         struct cifs_ntsd *pntsd = NULL;
1624         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1625         unsigned int xid;
1626         int rc;
1627         struct cifs_tcon *tcon;
1628         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1629         struct cifs_fid fid;
1630         struct cifs_open_parms oparms;
1631         __le16 *utf16_path;
1632
1633         cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
1634         if (IS_ERR(tlink))
1635                 return ERR_CAST(tlink);
1636
1637         tcon = tlink_tcon(tlink);
1638         xid = get_xid();
1639
1640         if (backup_cred(cifs_sb))
1641                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1642         else
1643                 oparms.create_options = 0;
1644
1645         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1646         if (!utf16_path) {
1647                 rc = -ENOMEM;
1648                 free_xid(xid);
1649                 return ERR_PTR(rc);
1650         }
1651
1652         oparms.tcon = tcon;
1653         oparms.desired_access = READ_CONTROL;
1654         oparms.disposition = FILE_OPEN;
1655         oparms.fid = &fid;
1656         oparms.reconnect = false;
1657
1658         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1659         kfree(utf16_path);
1660         if (!rc) {
1661                 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1662                             fid.volatile_fid, (void **)&pntsd, pacllen);
1663                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1664         }
1665
1666         cifs_put_tlink(tlink);
1667         free_xid(xid);
1668
1669         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
1670         if (rc)
1671                 return ERR_PTR(rc);
1672         return pntsd;
1673 }
1674
1675 #ifdef CONFIG_CIFS_ACL
1676 static int
1677 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
1678                 struct inode *inode, const char *path, int aclflag)
1679 {
1680         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1681         unsigned int xid;
1682         int rc, access_flags = 0;
1683         struct cifs_tcon *tcon;
1684         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1685         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
1686         struct cifs_fid fid;
1687         struct cifs_open_parms oparms;
1688         __le16 *utf16_path;
1689
1690         cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
1691         if (IS_ERR(tlink))
1692                 return PTR_ERR(tlink);
1693
1694         tcon = tlink_tcon(tlink);
1695         xid = get_xid();
1696
1697         if (backup_cred(cifs_sb))
1698                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1699         else
1700                 oparms.create_options = 0;
1701
1702         if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
1703                 access_flags = WRITE_OWNER;
1704         else
1705                 access_flags = WRITE_DAC;
1706
1707         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1708         if (!utf16_path) {
1709                 rc = -ENOMEM;
1710                 free_xid(xid);
1711                 return rc;
1712         }
1713
1714         oparms.tcon = tcon;
1715         oparms.desired_access = access_flags;
1716         oparms.disposition = FILE_OPEN;
1717         oparms.path = path;
1718         oparms.fid = &fid;
1719         oparms.reconnect = false;
1720
1721         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
1722         kfree(utf16_path);
1723         if (!rc) {
1724                 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
1725                             fid.volatile_fid, pnntsd, acllen, aclflag);
1726                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1727         }
1728
1729         cifs_put_tlink(tlink);
1730         free_xid(xid);
1731         return rc;
1732 }
1733 #endif /* CIFS_ACL */
1734
1735 /* Retrieve an ACL from the server */
1736 static struct cifs_ntsd *
1737 get_smb2_acl(struct cifs_sb_info *cifs_sb,
1738                                       struct inode *inode, const char *path,
1739                                       u32 *pacllen)
1740 {
1741         struct cifs_ntsd *pntsd = NULL;
1742         struct cifsFileInfo *open_file = NULL;
1743
1744         if (inode)
1745                 open_file = find_readable_file(CIFS_I(inode), true);
1746         if (!open_file)
1747                 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
1748
1749         pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
1750         cifsFileInfo_put(open_file);
1751         return pntsd;
1752 }
1753 #endif
1754
1755 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1756                             loff_t offset, loff_t len, bool keep_size)
1757 {
1758         struct inode *inode;
1759         struct cifsInodeInfo *cifsi;
1760         struct cifsFileInfo *cfile = file->private_data;
1761         struct file_zero_data_information fsctl_buf;
1762         long rc;
1763         unsigned int xid;
1764
1765         xid = get_xid();
1766
1767         inode = d_inode(cfile->dentry);
1768         cifsi = CIFS_I(inode);
1769
1770         /*
1771          * We zero the range through ioctl, so we need remove the page caches
1772          * first, otherwise the data may be inconsistent with the server.
1773          */
1774         truncate_pagecache_range(inode, offset, offset + len - 1);
1775
1776         /* if file not oplocked can't be sure whether asking to extend size */
1777         if (!CIFS_CACHE_READ(cifsi))
1778                 if (keep_size == false) {
1779                         rc = -EOPNOTSUPP;
1780                         free_xid(xid);
1781                         return rc;
1782                 }
1783
1784         /*
1785          * Must check if file sparse since fallocate -z (zero range) assumes
1786          * non-sparse allocation
1787          */
1788         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
1789                 rc = -EOPNOTSUPP;
1790                 free_xid(xid);
1791                 return rc;
1792         }
1793
1794         /*
1795          * need to make sure we are not asked to extend the file since the SMB3
1796          * fsctl does not change the file size. In the future we could change
1797          * this to zero the first part of the range then set the file size
1798          * which for a non sparse file would zero the newly extended range
1799          */
1800         if (keep_size == false)
1801                 if (i_size_read(inode) < offset + len) {
1802                         rc = -EOPNOTSUPP;
1803                         free_xid(xid);
1804                         return rc;
1805                 }
1806
1807         cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1808
1809         fsctl_buf.FileOffset = cpu_to_le64(offset);
1810         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1811
1812         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1813                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1814                         true /* is_fctl */, false /* use_ipc */,
1815                         (char *)&fsctl_buf,
1816                         sizeof(struct file_zero_data_information), NULL, NULL);
1817         free_xid(xid);
1818         return rc;
1819 }
1820
1821 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1822                             loff_t offset, loff_t len)
1823 {
1824         struct inode *inode;
1825         struct cifsInodeInfo *cifsi;
1826         struct cifsFileInfo *cfile = file->private_data;
1827         struct file_zero_data_information fsctl_buf;
1828         long rc;
1829         unsigned int xid;
1830         __u8 set_sparse = 1;
1831
1832         xid = get_xid();
1833
1834         inode = d_inode(cfile->dentry);
1835         cifsi = CIFS_I(inode);
1836
1837         /* Need to make file sparse, if not already, before freeing range. */
1838         /* Consider adding equivalent for compressed since it could also work */
1839         if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
1840                 rc = -EOPNOTSUPP;
1841                 free_xid(xid);
1842                 return rc;
1843         }
1844
1845         /*
1846          * We implement the punch hole through ioctl, so we need remove the page
1847          * caches first, otherwise the data may be inconsistent with the server.
1848          */
1849         truncate_pagecache_range(inode, offset, offset + len - 1);
1850
1851         cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1852
1853         fsctl_buf.FileOffset = cpu_to_le64(offset);
1854         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1855
1856         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1857                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1858                         true /* is_fctl */, false /* use_ipc */,
1859                         (char *)&fsctl_buf,
1860                         sizeof(struct file_zero_data_information), NULL, NULL);
1861         free_xid(xid);
1862         return rc;
1863 }
1864
1865 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1866                             loff_t off, loff_t len, bool keep_size)
1867 {
1868         struct inode *inode;
1869         struct cifsInodeInfo *cifsi;
1870         struct cifsFileInfo *cfile = file->private_data;
1871         long rc = -EOPNOTSUPP;
1872         unsigned int xid;
1873
1874         xid = get_xid();
1875
1876         inode = d_inode(cfile->dentry);
1877         cifsi = CIFS_I(inode);
1878
1879         /* if file not oplocked can't be sure whether asking to extend size */
1880         if (!CIFS_CACHE_READ(cifsi))
1881                 if (keep_size == false) {
1882                         free_xid(xid);
1883                         return rc;
1884                 }
1885
1886         /*
1887          * Files are non-sparse by default so falloc may be a no-op
1888          * Must check if file sparse. If not sparse, and not extending
1889          * then no need to do anything since file already allocated
1890          */
1891         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1892                 if (keep_size == true)
1893                         rc = 0;
1894                 /* check if extending file */
1895                 else if (i_size_read(inode) >= off + len)
1896                         /* not extending file and already not sparse */
1897                         rc = 0;
1898                 /* BB: in future add else clause to extend file */
1899                 else
1900                         rc = -EOPNOTSUPP;
1901                 free_xid(xid);
1902                 return rc;
1903         }
1904
1905         if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1906                 /*
1907                  * Check if falloc starts within first few pages of file
1908                  * and ends within a few pages of the end of file to
1909                  * ensure that most of file is being forced to be
1910                  * fallocated now. If so then setting whole file sparse
1911                  * ie potentially making a few extra pages at the beginning
1912                  * or end of the file non-sparse via set_sparse is harmless.
1913                  */
1914                 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
1915                         rc = -EOPNOTSUPP;
1916                         free_xid(xid);
1917                         return rc;
1918                 }
1919
1920                 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1921         }
1922         /* BB: else ... in future add code to extend file and set sparse */
1923
1924
1925         free_xid(xid);
1926         return rc;
1927 }
1928
1929
1930 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1931                            loff_t off, loff_t len)
1932 {
1933         /* KEEP_SIZE already checked for by do_fallocate */
1934         if (mode & FALLOC_FL_PUNCH_HOLE)
1935                 return smb3_punch_hole(file, tcon, off, len);
1936         else if (mode & FALLOC_FL_ZERO_RANGE) {
1937                 if (mode & FALLOC_FL_KEEP_SIZE)
1938                         return smb3_zero_range(file, tcon, off, len, true);
1939                 return smb3_zero_range(file, tcon, off, len, false);
1940         } else if (mode == FALLOC_FL_KEEP_SIZE)
1941                 return smb3_simple_falloc(file, tcon, off, len, true);
1942         else if (mode == 0)
1943                 return smb3_simple_falloc(file, tcon, off, len, false);
1944
1945         return -EOPNOTSUPP;
1946 }
1947
1948 static void
1949 smb2_downgrade_oplock(struct TCP_Server_Info *server,
1950                       struct cifsInodeInfo *cinode, __u32 oplock,
1951                       unsigned int epoch, bool *purge_cache)
1952 {
1953         server->ops->set_oplock_level(cinode, oplock, 0, NULL);
1954 }
1955
1956 static void
1957 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1958                        unsigned int epoch, bool *purge_cache);
1959
1960 static void
1961 smb3_downgrade_oplock(struct TCP_Server_Info *server,
1962                        struct cifsInodeInfo *cinode, __u32 oplock,
1963                        unsigned int epoch, bool *purge_cache)
1964 {
1965         unsigned int old_state = cinode->oplock;
1966         unsigned int old_epoch = cinode->epoch;
1967         unsigned int new_state;
1968
1969         if (epoch > old_epoch) {
1970                 smb21_set_oplock_level(cinode, oplock, 0, NULL);
1971                 cinode->epoch = epoch;
1972         }
1973
1974         new_state = cinode->oplock;
1975         *purge_cache = false;
1976
1977         if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
1978             (new_state & CIFS_CACHE_READ_FLG) == 0)
1979                 *purge_cache = true;
1980         else if (old_state == new_state && (epoch - old_epoch > 1))
1981                 *purge_cache = true;
1982 }
1983
1984 static void
1985 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1986                       unsigned int epoch, bool *purge_cache)
1987 {
1988         oplock &= 0xFF;
1989         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1990                 return;
1991         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1992                 cinode->oplock = CIFS_CACHE_RHW_FLG;
1993                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1994                          &cinode->vfs_inode);
1995         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1996                 cinode->oplock = CIFS_CACHE_RW_FLG;
1997                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1998                          &cinode->vfs_inode);
1999         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
2000                 cinode->oplock = CIFS_CACHE_READ_FLG;
2001                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
2002                          &cinode->vfs_inode);
2003         } else
2004                 cinode->oplock = 0;
2005 }
2006
2007 static void
2008 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2009                        unsigned int epoch, bool *purge_cache)
2010 {
2011         char message[5] = {0};
2012         unsigned int new_oplock = 0;
2013
2014         oplock &= 0xFF;
2015         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
2016                 return;
2017
2018         /* Check if the server granted an oplock rather than a lease */
2019         if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2020                 return smb2_set_oplock_level(cinode, oplock, epoch,
2021                                              purge_cache);
2022
2023         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
2024                 new_oplock |= CIFS_CACHE_READ_FLG;
2025                 strcat(message, "R");
2026         }
2027         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
2028                 new_oplock |= CIFS_CACHE_HANDLE_FLG;
2029                 strcat(message, "H");
2030         }
2031         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
2032                 new_oplock |= CIFS_CACHE_WRITE_FLG;
2033                 strcat(message, "W");
2034         }
2035         if (!new_oplock)
2036                 strncpy(message, "None", sizeof(message));
2037
2038         cinode->oplock = new_oplock;
2039         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
2040                  &cinode->vfs_inode);
2041 }
2042
2043 static void
2044 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
2045                       unsigned int epoch, bool *purge_cache)
2046 {
2047         unsigned int old_oplock = cinode->oplock;
2048
2049         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
2050
2051         if (purge_cache) {
2052                 *purge_cache = false;
2053                 if (old_oplock == CIFS_CACHE_READ_FLG) {
2054                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
2055                             (epoch - cinode->epoch > 0))
2056                                 *purge_cache = true;
2057                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2058                                  (epoch - cinode->epoch > 1))
2059                                 *purge_cache = true;
2060                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2061                                  (epoch - cinode->epoch > 1))
2062                                 *purge_cache = true;
2063                         else if (cinode->oplock == 0 &&
2064                                  (epoch - cinode->epoch > 0))
2065                                 *purge_cache = true;
2066                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
2067                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
2068                             (epoch - cinode->epoch > 0))
2069                                 *purge_cache = true;
2070                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
2071                                  (epoch - cinode->epoch > 1))
2072                                 *purge_cache = true;
2073                 }
2074                 cinode->epoch = epoch;
2075         }
2076 }
2077
2078 static bool
2079 smb2_is_read_op(__u32 oplock)
2080 {
2081         return oplock == SMB2_OPLOCK_LEVEL_II;
2082 }
2083
2084 static bool
2085 smb21_is_read_op(__u32 oplock)
2086 {
2087         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
2088                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
2089 }
2090
2091 static __le32
2092 map_oplock_to_lease(u8 oplock)
2093 {
2094         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
2095                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
2096         else if (oplock == SMB2_OPLOCK_LEVEL_II)
2097                 return SMB2_LEASE_READ_CACHING;
2098         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
2099                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
2100                        SMB2_LEASE_WRITE_CACHING;
2101         return 0;
2102 }
2103
2104 static char *
2105 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
2106 {
2107         struct create_lease *buf;
2108
2109         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
2110         if (!buf)
2111                 return NULL;
2112
2113         buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2114         buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2115         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2116
2117         buf->ccontext.DataOffset = cpu_to_le16(offsetof
2118                                         (struct create_lease, lcontext));
2119         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
2120         buf->ccontext.NameOffset = cpu_to_le16(offsetof
2121                                 (struct create_lease, Name));
2122         buf->ccontext.NameLength = cpu_to_le16(4);
2123         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2124         buf->Name[0] = 'R';
2125         buf->Name[1] = 'q';
2126         buf->Name[2] = 'L';
2127         buf->Name[3] = 's';
2128         return (char *)buf;
2129 }
2130
2131 static char *
2132 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
2133 {
2134         struct create_lease_v2 *buf;
2135
2136         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
2137         if (!buf)
2138                 return NULL;
2139
2140         buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
2141         buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
2142         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
2143
2144         buf->ccontext.DataOffset = cpu_to_le16(offsetof
2145                                         (struct create_lease_v2, lcontext));
2146         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
2147         buf->ccontext.NameOffset = cpu_to_le16(offsetof
2148                                 (struct create_lease_v2, Name));
2149         buf->ccontext.NameLength = cpu_to_le16(4);
2150         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
2151         buf->Name[0] = 'R';
2152         buf->Name[1] = 'q';
2153         buf->Name[2] = 'L';
2154         buf->Name[3] = 's';
2155         return (char *)buf;
2156 }
2157
2158 static __u8
2159 smb2_parse_lease_buf(void *buf, unsigned int *epoch)
2160 {
2161         struct create_lease *lc = (struct create_lease *)buf;
2162
2163         *epoch = 0; /* not used */
2164         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2165                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2166         return le32_to_cpu(lc->lcontext.LeaseState);
2167 }
2168
2169 static __u8
2170 smb3_parse_lease_buf(void *buf, unsigned int *epoch)
2171 {
2172         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
2173
2174         *epoch = le16_to_cpu(lc->lcontext.Epoch);
2175         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
2176                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
2177         return le32_to_cpu(lc->lcontext.LeaseState);
2178 }
2179
2180 static unsigned int
2181 smb2_wp_retry_size(struct inode *inode)
2182 {
2183         return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
2184                      SMB2_MAX_BUFFER_SIZE);
2185 }
2186
2187 static bool
2188 smb2_dir_needs_close(struct cifsFileInfo *cfile)
2189 {
2190         return !cfile->invalidHandle;
2191 }
2192
2193 static void
2194 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
2195 {
2196         struct smb2_sync_hdr *shdr =
2197                         (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
2198         unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
2199
2200         memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
2201         tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
2202         tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
2203         tr_hdr->Flags = cpu_to_le16(0x01);
2204         get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2205         memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
2206         inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
2207         inc_rfc1001_len(tr_hdr, orig_len);
2208 }
2209
2210 /* We can not use the normal sg_set_buf() as we will sometimes pass a
2211  * stack object as buf.
2212  */
2213 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
2214                                    unsigned int buflen)
2215 {
2216         void *addr;
2217         /*
2218          * VMAP_STACK (at least) puts stack into the vmalloc address space
2219          */
2220         if (is_vmalloc_addr(buf))
2221                 addr = vmalloc_to_page(buf);
2222         else
2223                 addr = virt_to_page(buf);
2224         sg_set_page(sg, addr, buflen, offset_in_page(buf));
2225 }
2226
2227 static struct scatterlist *
2228 init_sg(struct smb_rqst *rqst, u8 *sign)
2229 {
2230         unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
2231         unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2232         struct scatterlist *sg;
2233         unsigned int i;
2234         unsigned int j;
2235
2236         sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
2237         if (!sg)
2238                 return NULL;
2239
2240         sg_init_table(sg, sg_len);
2241         smb2_sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
2242         for (i = 1; i < rqst->rq_nvec; i++)
2243                 smb2_sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
2244                                                 rqst->rq_iov[i].iov_len);
2245         for (j = 0; i < sg_len - 1; i++, j++) {
2246                 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
2247                                                         : rqst->rq_tailsz;
2248                 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
2249         }
2250         smb2_sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
2251         return sg;
2252 }
2253
2254 struct cifs_crypt_result {
2255         int err;
2256         struct completion completion;
2257 };
2258
2259 static void cifs_crypt_complete(struct crypto_async_request *req, int err)
2260 {
2261         struct cifs_crypt_result *res = req->data;
2262
2263         if (err == -EINPROGRESS)
2264                 return;
2265
2266         res->err = err;
2267         complete(&res->completion);
2268 }
2269
2270 static int
2271 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
2272 {
2273         struct cifs_ses *ses;
2274         u8 *ses_enc_key;
2275
2276         spin_lock(&cifs_tcp_ses_lock);
2277         list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2278                 if (ses->Suid != ses_id)
2279                         continue;
2280                 ses_enc_key = enc ? ses->smb3encryptionkey :
2281                                                         ses->smb3decryptionkey;
2282                 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
2283                 spin_unlock(&cifs_tcp_ses_lock);
2284                 return 0;
2285         }
2286         spin_unlock(&cifs_tcp_ses_lock);
2287
2288         return -EAGAIN;
2289 }
2290 /*
2291  * Encrypt or decrypt @rqst message. @rqst has the following format:
2292  * iov[0] - transform header (associate data),
2293  * iov[1-N] and pages - data to encrypt.
2294  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
2295  * untouched.
2296  */
2297 static int
2298 crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
2299 {
2300         struct smb2_transform_hdr *tr_hdr =
2301                         (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
2302         unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
2303         int rc = 0;
2304         struct scatterlist *sg;
2305         u8 sign[SMB2_SIGNATURE_SIZE] = {};
2306         u8 key[SMB3_SIGN_KEY_SIZE];
2307         struct aead_request *req;
2308         char *iv;
2309         unsigned int iv_len;
2310         struct cifs_crypt_result result = {0, };
2311         struct crypto_aead *tfm;
2312         unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2313
2314         init_completion(&result.completion);
2315
2316         rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
2317         if (rc) {
2318                 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
2319                          enc ? "en" : "de");
2320                 return rc;
2321         }
2322
2323         rc = smb3_crypto_aead_allocate(server);
2324         if (rc) {
2325                 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
2326                 return rc;
2327         }
2328
2329         tfm = enc ? server->secmech.ccmaesencrypt :
2330                                                 server->secmech.ccmaesdecrypt;
2331         rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
2332         if (rc) {
2333                 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
2334                 return rc;
2335         }
2336
2337         rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
2338         if (rc) {
2339                 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
2340                 return rc;
2341         }
2342
2343         req = aead_request_alloc(tfm, GFP_KERNEL);
2344         if (!req) {
2345                 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
2346                 return -ENOMEM;
2347         }
2348
2349         if (!enc) {
2350                 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
2351                 crypt_len += SMB2_SIGNATURE_SIZE;
2352         }
2353
2354         sg = init_sg(rqst, sign);
2355         if (!sg) {
2356                 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
2357                 rc = -ENOMEM;
2358                 goto free_req;
2359         }
2360
2361         iv_len = crypto_aead_ivsize(tfm);
2362         iv = kzalloc(iv_len, GFP_KERNEL);
2363         if (!iv) {
2364                 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
2365                 rc = -ENOMEM;
2366                 goto free_sg;
2367         }
2368         iv[0] = 3;
2369         memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
2370
2371         aead_request_set_crypt(req, sg, sg, crypt_len, iv);
2372         aead_request_set_ad(req, assoc_data_len);
2373
2374         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2375                                   cifs_crypt_complete, &result);
2376
2377         rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
2378
2379         if (rc == -EINPROGRESS || rc == -EBUSY) {
2380                 wait_for_completion(&result.completion);
2381                 rc = result.err;
2382         }
2383
2384         if (!rc && enc)
2385                 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
2386
2387         kfree(iv);
2388 free_sg:
2389         kfree(sg);
2390 free_req:
2391         kfree(req);
2392         return rc;
2393 }
2394
2395 static int
2396 smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
2397                        struct smb_rqst *old_rq)
2398 {
2399         struct kvec *iov;
2400         struct page **pages;
2401         struct smb2_transform_hdr *tr_hdr;
2402         unsigned int npages = old_rq->rq_npages;
2403         int i;
2404         int rc = -ENOMEM;
2405
2406         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2407         if (!pages)
2408                 return rc;
2409
2410         new_rq->rq_pages = pages;
2411         new_rq->rq_npages = old_rq->rq_npages;
2412         new_rq->rq_pagesz = old_rq->rq_pagesz;
2413         new_rq->rq_tailsz = old_rq->rq_tailsz;
2414
2415         for (i = 0; i < npages; i++) {
2416                 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2417                 if (!pages[i])
2418                         goto err_free_pages;
2419         }
2420
2421         iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
2422         if (!iov)
2423                 goto err_free_pages;
2424
2425         /* copy all iovs from the old except the 1st one (rfc1002 length) */
2426         memcpy(&iov[1], &old_rq->rq_iov[1],
2427                                 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
2428         new_rq->rq_iov = iov;
2429         new_rq->rq_nvec = old_rq->rq_nvec;
2430
2431         tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
2432         if (!tr_hdr)
2433                 goto err_free_iov;
2434
2435         /* fill the 1st iov with a transform header */
2436         fill_transform_hdr(tr_hdr, old_rq);
2437         new_rq->rq_iov[0].iov_base = tr_hdr;
2438         new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2439
2440         /* copy pages form the old */
2441         for (i = 0; i < npages; i++) {
2442                 char *dst = kmap(new_rq->rq_pages[i]);
2443                 char *src = kmap(old_rq->rq_pages[i]);
2444                 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
2445                                                         new_rq->rq_tailsz;
2446                 memcpy(dst, src, len);
2447                 kunmap(new_rq->rq_pages[i]);
2448                 kunmap(old_rq->rq_pages[i]);
2449         }
2450
2451         rc = crypt_message(server, new_rq, 1);
2452         cifs_dbg(FYI, "encrypt message returned %d", rc);
2453         if (rc)
2454                 goto err_free_tr_hdr;
2455
2456         return rc;
2457
2458 err_free_tr_hdr:
2459         kfree(tr_hdr);
2460 err_free_iov:
2461         kfree(iov);
2462 err_free_pages:
2463         for (i = i - 1; i >= 0; i--)
2464                 put_page(pages[i]);
2465         kfree(pages);
2466         return rc;
2467 }
2468
2469 static void
2470 smb3_free_transform_rq(struct smb_rqst *rqst)
2471 {
2472         int i = rqst->rq_npages - 1;
2473
2474         for (; i >= 0; i--)
2475                 put_page(rqst->rq_pages[i]);
2476         kfree(rqst->rq_pages);
2477         /* free transform header */
2478         kfree(rqst->rq_iov[0].iov_base);
2479         kfree(rqst->rq_iov);
2480 }
2481
2482 static int
2483 smb3_is_transform_hdr(void *buf)
2484 {
2485         struct smb2_transform_hdr *trhdr = buf;
2486
2487         return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
2488 }
2489
2490 static int
2491 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
2492                  unsigned int buf_data_size, struct page **pages,
2493                  unsigned int npages, unsigned int page_data_size)
2494 {
2495         struct kvec iov[2];
2496         struct smb_rqst rqst = {NULL};
2497         struct smb2_hdr *hdr;
2498         int rc;
2499
2500         iov[0].iov_base = buf;
2501         iov[0].iov_len = sizeof(struct smb2_transform_hdr);
2502         iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
2503         iov[1].iov_len = buf_data_size;
2504
2505         rqst.rq_iov = iov;
2506         rqst.rq_nvec = 2;
2507         rqst.rq_pages = pages;
2508         rqst.rq_npages = npages;
2509         rqst.rq_pagesz = PAGE_SIZE;
2510         rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
2511
2512         rc = crypt_message(server, &rqst, 0);
2513         cifs_dbg(FYI, "decrypt message returned %d\n", rc);
2514
2515         if (rc)
2516                 return rc;
2517
2518         memmove(buf + 4, iov[1].iov_base, buf_data_size);
2519         hdr = (struct smb2_hdr *)buf;
2520         hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
2521         server->total_read = buf_data_size + page_data_size + 4;
2522
2523         return rc;
2524 }
2525
2526 static int
2527 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
2528                      unsigned int npages, unsigned int len)
2529 {
2530         int i;
2531         int length;
2532
2533         for (i = 0; i < npages; i++) {
2534                 struct page *page = pages[i];
2535                 size_t n;
2536
2537                 n = len;
2538                 if (len >= PAGE_SIZE) {
2539                         /* enough data to fill the page */
2540                         n = PAGE_SIZE;
2541                         len -= n;
2542                 } else {
2543                         zero_user(page, len, PAGE_SIZE - len);
2544                         len = 0;
2545                 }
2546                 length = cifs_read_page_from_socket(server, page, n);
2547                 if (length < 0)
2548                         return length;
2549                 server->total_read += length;
2550         }
2551
2552         return 0;
2553 }
2554
2555 static int
2556 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
2557                unsigned int cur_off, struct bio_vec **page_vec)
2558 {
2559         struct bio_vec *bvec;
2560         int i;
2561
2562         bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
2563         if (!bvec)
2564                 return -ENOMEM;
2565
2566         for (i = 0; i < npages; i++) {
2567                 bvec[i].bv_page = pages[i];
2568                 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
2569                 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
2570                 data_size -= bvec[i].bv_len;
2571         }
2572
2573         if (data_size != 0) {
2574                 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
2575                 kfree(bvec);
2576                 return -EIO;
2577         }
2578
2579         *page_vec = bvec;
2580         return 0;
2581 }
2582
2583 static int
2584 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
2585                  char *buf, unsigned int buf_len, struct page **pages,
2586                  unsigned int npages, unsigned int page_data_size)
2587 {
2588         unsigned int data_offset;
2589         unsigned int data_len;
2590         unsigned int cur_off;
2591         unsigned int cur_page_idx;
2592         unsigned int pad_len;
2593         struct cifs_readdata *rdata = mid->callback_data;
2594         struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2595         struct bio_vec *bvec = NULL;
2596         struct iov_iter iter;
2597         struct kvec iov;
2598         int length;
2599
2600         if (shdr->Command != SMB2_READ) {
2601                 cifs_dbg(VFS, "only big read responses are supported\n");
2602                 return -ENOTSUPP;
2603         }
2604
2605         if (server->ops->is_session_expired &&
2606             server->ops->is_session_expired(buf)) {
2607                 cifs_reconnect(server);
2608                 wake_up(&server->response_q);
2609                 return -1;
2610         }
2611
2612         if (server->ops->is_status_pending &&
2613                         server->ops->is_status_pending(buf, server, 0))
2614                 return -1;
2615
2616         /* set up first two iov to get credits */
2617         rdata->iov[0].iov_base = buf;
2618         rdata->iov[0].iov_len = 4;
2619         rdata->iov[1].iov_base = buf + 4;
2620         rdata->iov[1].iov_len =
2621                 min_t(unsigned int, buf_len, server->vals->read_rsp_size) - 4;
2622         cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2623                  rdata->iov[0].iov_base, rdata->iov[0].iov_len);
2624         cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
2625                  rdata->iov[1].iov_base, rdata->iov[1].iov_len);
2626
2627         rdata->result = server->ops->map_error(buf, true);
2628         if (rdata->result != 0) {
2629                 cifs_dbg(FYI, "%s: server returned error %d\n",
2630                          __func__, rdata->result);
2631                 /* normal error on read response */
2632                 dequeue_mid(mid, false);
2633                 return 0;
2634         }
2635
2636         data_offset = server->ops->read_data_offset(buf) + 4;
2637         data_len = server->ops->read_data_length(buf);
2638
2639         if (data_offset < server->vals->read_rsp_size) {
2640                 /*
2641                  * win2k8 sometimes sends an offset of 0 when the read
2642                  * is beyond the EOF. Treat it as if the data starts just after
2643                  * the header.
2644                  */
2645                 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
2646                          __func__, data_offset);
2647                 data_offset = server->vals->read_rsp_size;
2648         } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
2649                 /* data_offset is beyond the end of smallbuf */
2650                 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
2651                          __func__, data_offset);
2652                 rdata->result = -EIO;
2653                 dequeue_mid(mid, rdata->result);
2654                 return 0;
2655         }
2656
2657         pad_len = data_offset - server->vals->read_rsp_size;
2658
2659         if (buf_len <= data_offset) {
2660                 /* read response payload is in pages */
2661                 cur_page_idx = pad_len / PAGE_SIZE;
2662                 cur_off = pad_len % PAGE_SIZE;
2663
2664                 if (cur_page_idx != 0) {
2665                         /* data offset is beyond the 1st page of response */
2666                         cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
2667                                  __func__, data_offset);
2668                         rdata->result = -EIO;
2669                         dequeue_mid(mid, rdata->result);
2670                         return 0;
2671                 }
2672
2673                 if (data_len > page_data_size - pad_len) {
2674                         /* data_len is corrupt -- discard frame */
2675                         rdata->result = -EIO;
2676                         dequeue_mid(mid, rdata->result);
2677                         return 0;
2678                 }
2679
2680                 rdata->result = init_read_bvec(pages, npages, page_data_size,
2681                                                cur_off, &bvec);
2682                 if (rdata->result != 0) {
2683                         dequeue_mid(mid, rdata->result);
2684                         return 0;
2685                 }
2686
2687                 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
2688         } else if (buf_len >= data_offset + data_len) {
2689                 /* read response payload is in buf */
2690                 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2691                 iov.iov_base = buf + data_offset;
2692                 iov.iov_len = data_len;
2693                 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2694         } else {
2695                 /* read response payload cannot be in both buf and pages */
2696                 WARN_ONCE(1, "buf can not contain only a part of read data");
2697                 rdata->result = -EIO;
2698                 dequeue_mid(mid, rdata->result);
2699                 return 0;
2700         }
2701
2702         length = rdata->copy_into_pages(server, rdata, &iter);
2703
2704         kfree(bvec);
2705
2706         if (length < 0)
2707                 return length;
2708
2709         dequeue_mid(mid, false);
2710         return length;
2711 }
2712
2713 static int
2714 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2715 {
2716         char *buf = server->smallbuf;
2717         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2718         unsigned int npages;
2719         struct page **pages;
2720         unsigned int len;
2721         unsigned int buflen = get_rfc1002_length(buf) + 4;
2722         int rc;
2723         int i = 0;
2724
2725         len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2726                 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2727
2728         rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2729         if (rc < 0)
2730                 return rc;
2731         server->total_read += rc;
2732
2733         len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2734                                                 server->vals->read_rsp_size;
2735         npages = DIV_ROUND_UP(len, PAGE_SIZE);
2736
2737         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2738         if (!pages) {
2739                 rc = -ENOMEM;
2740                 goto discard_data;
2741         }
2742
2743         for (; i < npages; i++) {
2744                 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2745                 if (!pages[i]) {
2746                         rc = -ENOMEM;
2747                         goto discard_data;
2748                 }
2749         }
2750
2751         /* read read data into pages */
2752         rc = read_data_into_pages(server, pages, npages, len);
2753         if (rc)
2754                 goto free_pages;
2755
2756         rc = cifs_discard_remaining_data(server);
2757         if (rc)
2758                 goto free_pages;
2759
2760         rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2761                               pages, npages, len);
2762         if (rc)
2763                 goto free_pages;
2764
2765         *mid = smb2_find_mid(server, buf);
2766         if (*mid == NULL)
2767                 cifs_dbg(FYI, "mid not found\n");
2768         else {
2769                 cifs_dbg(FYI, "mid found\n");
2770                 (*mid)->decrypted = true;
2771                 rc = handle_read_data(server, *mid, buf,
2772                                       server->vals->read_rsp_size,
2773                                       pages, npages, len);
2774         }
2775
2776 free_pages:
2777         for (i = i - 1; i >= 0; i--)
2778                 put_page(pages[i]);
2779         kfree(pages);
2780         return rc;
2781 discard_data:
2782         cifs_discard_remaining_data(server);
2783         goto free_pages;
2784 }
2785
2786 static int
2787 receive_encrypted_standard(struct TCP_Server_Info *server,
2788                            struct mid_q_entry **mid)
2789 {
2790         int length;
2791         char *buf = server->smallbuf;
2792         unsigned int pdu_length = get_rfc1002_length(buf);
2793         unsigned int buf_size;
2794         struct mid_q_entry *mid_entry;
2795
2796         /* switch to large buffer if too big for a small one */
2797         if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2798                 server->large_buf = true;
2799                 memcpy(server->bigbuf, buf, server->total_read);
2800                 buf = server->bigbuf;
2801         }
2802
2803         /* now read the rest */
2804         length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2805                                 pdu_length - HEADER_SIZE(server) + 1 + 4);
2806         if (length < 0)
2807                 return length;
2808         server->total_read += length;
2809
2810         buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2811         length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2812         if (length)
2813                 return length;
2814
2815         mid_entry = smb2_find_mid(server, buf);
2816         if (mid_entry == NULL)
2817                 cifs_dbg(FYI, "mid not found\n");
2818         else {
2819                 cifs_dbg(FYI, "mid found\n");
2820                 mid_entry->decrypted = true;
2821         }
2822
2823         *mid = mid_entry;
2824
2825         if (mid_entry && mid_entry->handle)
2826                 return mid_entry->handle(server, mid_entry);
2827
2828         return cifs_handle_standard(server, mid_entry);
2829 }
2830
2831 static int
2832 smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2833 {
2834         char *buf = server->smallbuf;
2835         unsigned int pdu_length = get_rfc1002_length(buf);
2836         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2837         unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2838
2839         if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2840                                                 sizeof(struct smb2_sync_hdr)) {
2841                 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2842                          pdu_length);
2843                 cifs_reconnect(server);
2844                 wake_up(&server->response_q);
2845                 return -ECONNABORTED;
2846         }
2847
2848         if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2849                 cifs_dbg(VFS, "Transform message is broken\n");
2850                 cifs_reconnect(server);
2851                 wake_up(&server->response_q);
2852                 return -ECONNABORTED;
2853         }
2854
2855         if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2856                 return receive_encrypted_read(server, mid);
2857
2858         return receive_encrypted_standard(server, mid);
2859 }
2860
2861 int
2862 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2863 {
2864         char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2865
2866         return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2867                                 NULL, 0, 0);
2868 }
2869
2870 struct smb_version_operations smb20_operations = {
2871         .compare_fids = smb2_compare_fids,
2872         .setup_request = smb2_setup_request,
2873         .setup_async_request = smb2_setup_async_request,
2874         .check_receive = smb2_check_receive,
2875         .add_credits = smb2_add_credits,
2876         .set_credits = smb2_set_credits,
2877         .get_credits_field = smb2_get_credits_field,
2878         .get_credits = smb2_get_credits,
2879         .wait_mtu_credits = cifs_wait_mtu_credits,
2880         .get_next_mid = smb2_get_next_mid,
2881         .read_data_offset = smb2_read_data_offset,
2882         .read_data_length = smb2_read_data_length,
2883         .map_error = map_smb2_to_linux_error,
2884         .find_mid = smb2_find_mid,
2885         .check_message = smb2_check_message,
2886         .dump_detail = smb2_dump_detail,
2887         .clear_stats = smb2_clear_stats,
2888         .print_stats = smb2_print_stats,
2889         .is_oplock_break = smb2_is_valid_oplock_break,
2890         .handle_cancelled_mid = smb2_handle_cancelled_mid,
2891         .downgrade_oplock = smb2_downgrade_oplock,
2892         .need_neg = smb2_need_neg,
2893         .negotiate = smb2_negotiate,
2894         .negotiate_wsize = smb2_negotiate_wsize,
2895         .negotiate_rsize = smb2_negotiate_rsize,
2896         .sess_setup = SMB2_sess_setup,
2897         .logoff = SMB2_logoff,
2898         .tree_connect = SMB2_tcon,
2899         .tree_disconnect = SMB2_tdis,
2900         .qfs_tcon = smb2_qfs_tcon,
2901         .is_path_accessible = smb2_is_path_accessible,
2902         .can_echo = smb2_can_echo,
2903         .echo = SMB2_echo,
2904         .query_path_info = smb2_query_path_info,
2905         .get_srv_inum = smb2_get_srv_inum,
2906         .query_file_info = smb2_query_file_info,
2907         .set_path_size = smb2_set_path_size,
2908         .set_file_size = smb2_set_file_size,
2909         .set_file_info = smb2_set_file_info,
2910         .set_compression = smb2_set_compression,
2911         .mkdir = smb2_mkdir,
2912         .mkdir_setinfo = smb2_mkdir_setinfo,
2913         .rmdir = smb2_rmdir,
2914         .unlink = smb2_unlink,
2915         .rename = smb2_rename_path,
2916         .create_hardlink = smb2_create_hardlink,
2917         .query_symlink = smb2_query_symlink,
2918         .query_mf_symlink = smb3_query_mf_symlink,
2919         .create_mf_symlink = smb3_create_mf_symlink,
2920         .open = smb2_open_file,
2921         .set_fid = smb2_set_fid,
2922         .close = smb2_close_file,
2923         .flush = smb2_flush_file,
2924         .async_readv = smb2_async_readv,
2925         .async_writev = smb2_async_writev,
2926         .sync_read = smb2_sync_read,
2927         .sync_write = smb2_sync_write,
2928         .query_dir_first = smb2_query_dir_first,
2929         .query_dir_next = smb2_query_dir_next,
2930         .close_dir = smb2_close_dir,
2931         .calc_smb_size = smb2_calc_size,
2932         .is_status_pending = smb2_is_status_pending,
2933         .is_session_expired = smb2_is_session_expired,
2934         .oplock_response = smb2_oplock_response,
2935         .queryfs = smb2_queryfs,
2936         .mand_lock = smb2_mand_lock,
2937         .mand_unlock_range = smb2_unlock_range,
2938         .push_mand_locks = smb2_push_mandatory_locks,
2939         .get_lease_key = smb2_get_lease_key,
2940         .set_lease_key = smb2_set_lease_key,
2941         .new_lease_key = smb2_new_lease_key,
2942         .calc_signature = smb2_calc_signature,
2943         .is_read_op = smb2_is_read_op,
2944         .set_oplock_level = smb2_set_oplock_level,
2945         .create_lease_buf = smb2_create_lease_buf,
2946         .parse_lease_buf = smb2_parse_lease_buf,
2947         .copychunk_range = smb2_copychunk_range,
2948         .wp_retry_size = smb2_wp_retry_size,
2949         .dir_needs_close = smb2_dir_needs_close,
2950         .get_dfs_refer = smb2_get_dfs_refer,
2951         .select_sectype = smb2_select_sectype,
2952 #ifdef CONFIG_CIFS_XATTR
2953         .query_all_EAs = smb2_query_eas,
2954         .set_EA = smb2_set_ea,
2955 #endif /* CIFS_XATTR */
2956 #ifdef CONFIG_CIFS_ACL
2957         .get_acl = get_smb2_acl,
2958         .get_acl_by_fid = get_smb2_acl_by_fid,
2959         .set_acl = set_smb2_acl,
2960 #endif /* CIFS_ACL */
2961 };
2962
2963 struct smb_version_operations smb21_operations = {
2964         .compare_fids = smb2_compare_fids,
2965         .setup_request = smb2_setup_request,
2966         .setup_async_request = smb2_setup_async_request,
2967         .check_receive = smb2_check_receive,
2968         .add_credits = smb2_add_credits,
2969         .set_credits = smb2_set_credits,
2970         .get_credits_field = smb2_get_credits_field,
2971         .get_credits = smb2_get_credits,
2972         .wait_mtu_credits = smb2_wait_mtu_credits,
2973         .get_next_mid = smb2_get_next_mid,
2974         .read_data_offset = smb2_read_data_offset,
2975         .read_data_length = smb2_read_data_length,
2976         .map_error = map_smb2_to_linux_error,
2977         .find_mid = smb2_find_mid,
2978         .check_message = smb2_check_message,
2979         .dump_detail = smb2_dump_detail,
2980         .clear_stats = smb2_clear_stats,
2981         .print_stats = smb2_print_stats,
2982         .is_oplock_break = smb2_is_valid_oplock_break,
2983         .handle_cancelled_mid = smb2_handle_cancelled_mid,
2984         .downgrade_oplock = smb2_downgrade_oplock,
2985         .need_neg = smb2_need_neg,
2986         .negotiate = smb2_negotiate,
2987         .negotiate_wsize = smb2_negotiate_wsize,
2988         .negotiate_rsize = smb2_negotiate_rsize,
2989         .sess_setup = SMB2_sess_setup,
2990         .logoff = SMB2_logoff,
2991         .tree_connect = SMB2_tcon,
2992         .tree_disconnect = SMB2_tdis,
2993         .qfs_tcon = smb2_qfs_tcon,
2994         .is_path_accessible = smb2_is_path_accessible,
2995         .can_echo = smb2_can_echo,
2996         .echo = SMB2_echo,
2997         .query_path_info = smb2_query_path_info,
2998         .get_srv_inum = smb2_get_srv_inum,
2999         .query_file_info = smb2_query_file_info,
3000         .set_path_size = smb2_set_path_size,
3001         .set_file_size = smb2_set_file_size,
3002         .set_file_info = smb2_set_file_info,
3003         .set_compression = smb2_set_compression,
3004         .mkdir = smb2_mkdir,
3005         .mkdir_setinfo = smb2_mkdir_setinfo,
3006         .rmdir = smb2_rmdir,
3007         .unlink = smb2_unlink,
3008         .rename = smb2_rename_path,
3009         .create_hardlink = smb2_create_hardlink,
3010         .query_symlink = smb2_query_symlink,
3011         .query_mf_symlink = smb3_query_mf_symlink,
3012         .create_mf_symlink = smb3_create_mf_symlink,
3013         .open = smb2_open_file,
3014         .set_fid = smb2_set_fid,
3015         .close = smb2_close_file,
3016         .flush = smb2_flush_file,
3017         .async_readv = smb2_async_readv,
3018         .async_writev = smb2_async_writev,
3019         .sync_read = smb2_sync_read,
3020         .sync_write = smb2_sync_write,
3021         .query_dir_first = smb2_query_dir_first,
3022         .query_dir_next = smb2_query_dir_next,
3023         .close_dir = smb2_close_dir,
3024         .calc_smb_size = smb2_calc_size,
3025         .is_status_pending = smb2_is_status_pending,
3026         .is_session_expired = smb2_is_session_expired,
3027         .oplock_response = smb2_oplock_response,
3028         .queryfs = smb2_queryfs,
3029         .mand_lock = smb2_mand_lock,
3030         .mand_unlock_range = smb2_unlock_range,
3031         .push_mand_locks = smb2_push_mandatory_locks,
3032         .get_lease_key = smb2_get_lease_key,
3033         .set_lease_key = smb2_set_lease_key,
3034         .new_lease_key = smb2_new_lease_key,
3035         .calc_signature = smb2_calc_signature,
3036         .is_read_op = smb21_is_read_op,
3037         .set_oplock_level = smb21_set_oplock_level,
3038         .create_lease_buf = smb2_create_lease_buf,
3039         .parse_lease_buf = smb2_parse_lease_buf,
3040         .copychunk_range = smb2_copychunk_range,
3041         .wp_retry_size = smb2_wp_retry_size,
3042         .dir_needs_close = smb2_dir_needs_close,
3043         .enum_snapshots = smb3_enum_snapshots,
3044         .get_dfs_refer = smb2_get_dfs_refer,
3045         .select_sectype = smb2_select_sectype,
3046 #ifdef CONFIG_CIFS_XATTR
3047         .query_all_EAs = smb2_query_eas,
3048         .set_EA = smb2_set_ea,
3049 #endif /* CIFS_XATTR */
3050 #ifdef CONFIG_CIFS_ACL
3051         .get_acl = get_smb2_acl,
3052         .get_acl_by_fid = get_smb2_acl_by_fid,
3053         .set_acl = set_smb2_acl,
3054 #endif /* CIFS_ACL */
3055 };
3056
3057 struct smb_version_operations smb30_operations = {
3058         .compare_fids = smb2_compare_fids,
3059         .setup_request = smb2_setup_request,
3060         .setup_async_request = smb2_setup_async_request,
3061         .check_receive = smb2_check_receive,
3062         .add_credits = smb2_add_credits,
3063         .set_credits = smb2_set_credits,
3064         .get_credits_field = smb2_get_credits_field,
3065         .get_credits = smb2_get_credits,
3066         .wait_mtu_credits = smb2_wait_mtu_credits,
3067         .get_next_mid = smb2_get_next_mid,
3068         .read_data_offset = smb2_read_data_offset,
3069         .read_data_length = smb2_read_data_length,
3070         .map_error = map_smb2_to_linux_error,
3071         .find_mid = smb2_find_mid,
3072         .check_message = smb2_check_message,
3073         .dump_detail = smb2_dump_detail,
3074         .clear_stats = smb2_clear_stats,
3075         .print_stats = smb2_print_stats,
3076         .dump_share_caps = smb2_dump_share_caps,
3077         .is_oplock_break = smb2_is_valid_oplock_break,
3078         .handle_cancelled_mid = smb2_handle_cancelled_mid,
3079         .downgrade_oplock = smb3_downgrade_oplock,
3080         .need_neg = smb2_need_neg,
3081         .negotiate = smb2_negotiate,
3082         .negotiate_wsize = smb2_negotiate_wsize,
3083         .negotiate_rsize = smb2_negotiate_rsize,
3084         .sess_setup = SMB2_sess_setup,
3085         .logoff = SMB2_logoff,
3086         .tree_connect = SMB2_tcon,
3087         .tree_disconnect = SMB2_tdis,
3088         .qfs_tcon = smb3_qfs_tcon,
3089         .is_path_accessible = smb2_is_path_accessible,
3090         .can_echo = smb2_can_echo,
3091         .echo = SMB2_echo,
3092         .query_path_info = smb2_query_path_info,
3093         .get_srv_inum = smb2_get_srv_inum,
3094         .query_file_info = smb2_query_file_info,
3095         .set_path_size = smb2_set_path_size,
3096         .set_file_size = smb2_set_file_size,
3097         .set_file_info = smb2_set_file_info,
3098         .set_compression = smb2_set_compression,
3099         .mkdir = smb2_mkdir,
3100         .mkdir_setinfo = smb2_mkdir_setinfo,
3101         .rmdir = smb2_rmdir,
3102         .unlink = smb2_unlink,
3103         .rename = smb2_rename_path,
3104         .create_hardlink = smb2_create_hardlink,
3105         .query_symlink = smb2_query_symlink,
3106         .query_mf_symlink = smb3_query_mf_symlink,
3107         .create_mf_symlink = smb3_create_mf_symlink,
3108         .open = smb2_open_file,
3109         .set_fid = smb2_set_fid,
3110         .close = smb2_close_file,
3111         .flush = smb2_flush_file,
3112         .async_readv = smb2_async_readv,
3113         .async_writev = smb2_async_writev,
3114         .sync_read = smb2_sync_read,
3115         .sync_write = smb2_sync_write,
3116         .query_dir_first = smb2_query_dir_first,
3117         .query_dir_next = smb2_query_dir_next,
3118         .close_dir = smb2_close_dir,
3119         .calc_smb_size = smb2_calc_size,
3120         .is_status_pending = smb2_is_status_pending,
3121         .is_session_expired = smb2_is_session_expired,
3122         .oplock_response = smb2_oplock_response,
3123         .queryfs = smb2_queryfs,
3124         .mand_lock = smb2_mand_lock,
3125         .mand_unlock_range = smb2_unlock_range,
3126         .push_mand_locks = smb2_push_mandatory_locks,
3127         .get_lease_key = smb2_get_lease_key,
3128         .set_lease_key = smb2_set_lease_key,
3129         .new_lease_key = smb2_new_lease_key,
3130         .generate_signingkey = generate_smb30signingkey,
3131         .calc_signature = smb3_calc_signature,
3132         .set_integrity  = smb3_set_integrity,
3133         .is_read_op = smb21_is_read_op,
3134         .set_oplock_level = smb3_set_oplock_level,
3135         .create_lease_buf = smb3_create_lease_buf,
3136         .parse_lease_buf = smb3_parse_lease_buf,
3137         .copychunk_range = smb2_copychunk_range,
3138         .duplicate_extents = smb2_duplicate_extents,
3139         .validate_negotiate = smb3_validate_negotiate,
3140         .wp_retry_size = smb2_wp_retry_size,
3141         .dir_needs_close = smb2_dir_needs_close,
3142         .fallocate = smb3_fallocate,
3143         .enum_snapshots = smb3_enum_snapshots,
3144         .init_transform_rq = smb3_init_transform_rq,
3145         .free_transform_rq = smb3_free_transform_rq,
3146         .is_transform_hdr = smb3_is_transform_hdr,
3147         .receive_transform = smb3_receive_transform,
3148         .get_dfs_refer = smb2_get_dfs_refer,
3149         .select_sectype = smb2_select_sectype,
3150 #ifdef CONFIG_CIFS_XATTR
3151         .query_all_EAs = smb2_query_eas,
3152         .set_EA = smb2_set_ea,
3153 #endif /* CIFS_XATTR */
3154 #ifdef CONFIG_CIFS_ACL
3155         .get_acl = get_smb2_acl,
3156         .get_acl_by_fid = get_smb2_acl_by_fid,
3157         .set_acl = set_smb2_acl,
3158 #endif /* CIFS_ACL */
3159 };
3160
3161 #ifdef CONFIG_CIFS_SMB311
3162 struct smb_version_operations smb311_operations = {
3163         .compare_fids = smb2_compare_fids,
3164         .setup_request = smb2_setup_request,
3165         .setup_async_request = smb2_setup_async_request,
3166         .check_receive = smb2_check_receive,
3167         .add_credits = smb2_add_credits,
3168         .set_credits = smb2_set_credits,
3169         .get_credits_field = smb2_get_credits_field,
3170         .get_credits = smb2_get_credits,
3171         .wait_mtu_credits = smb2_wait_mtu_credits,
3172         .get_next_mid = smb2_get_next_mid,
3173         .read_data_offset = smb2_read_data_offset,
3174         .read_data_length = smb2_read_data_length,
3175         .map_error = map_smb2_to_linux_error,
3176         .find_mid = smb2_find_mid,
3177         .check_message = smb2_check_message,
3178         .dump_detail = smb2_dump_detail,
3179         .clear_stats = smb2_clear_stats,
3180         .print_stats = smb2_print_stats,
3181         .dump_share_caps = smb2_dump_share_caps,
3182         .is_oplock_break = smb2_is_valid_oplock_break,
3183         .handle_cancelled_mid = smb2_handle_cancelled_mid,
3184         .downgrade_oplock = smb3_downgrade_oplock,
3185         .need_neg = smb2_need_neg,
3186         .negotiate = smb2_negotiate,
3187         .negotiate_wsize = smb2_negotiate_wsize,
3188         .negotiate_rsize = smb2_negotiate_rsize,
3189         .sess_setup = SMB2_sess_setup,
3190         .logoff = SMB2_logoff,
3191         .tree_connect = SMB2_tcon,
3192         .tree_disconnect = SMB2_tdis,
3193         .qfs_tcon = smb3_qfs_tcon,
3194         .is_path_accessible = smb2_is_path_accessible,
3195         .can_echo = smb2_can_echo,
3196         .echo = SMB2_echo,
3197         .query_path_info = smb2_query_path_info,
3198         .get_srv_inum = smb2_get_srv_inum,
3199         .query_file_info = smb2_query_file_info,
3200         .set_path_size = smb2_set_path_size,
3201         .set_file_size = smb2_set_file_size,
3202         .set_file_info = smb2_set_file_info,
3203         .set_compression = smb2_set_compression,
3204         .mkdir = smb2_mkdir,
3205         .mkdir_setinfo = smb2_mkdir_setinfo,
3206         .rmdir = smb2_rmdir,
3207         .unlink = smb2_unlink,
3208         .rename = smb2_rename_path,
3209         .create_hardlink = smb2_create_hardlink,
3210         .query_symlink = smb2_query_symlink,
3211         .query_mf_symlink = smb3_query_mf_symlink,
3212         .create_mf_symlink = smb3_create_mf_symlink,
3213         .open = smb2_open_file,
3214         .set_fid = smb2_set_fid,
3215         .close = smb2_close_file,
3216         .flush = smb2_flush_file,
3217         .async_readv = smb2_async_readv,
3218         .async_writev = smb2_async_writev,
3219         .sync_read = smb2_sync_read,
3220         .sync_write = smb2_sync_write,
3221         .query_dir_first = smb2_query_dir_first,
3222         .query_dir_next = smb2_query_dir_next,
3223         .close_dir = smb2_close_dir,
3224         .calc_smb_size = smb2_calc_size,
3225         .is_status_pending = smb2_is_status_pending,
3226         .is_session_expired = smb2_is_session_expired,
3227         .oplock_response = smb2_oplock_response,
3228         .queryfs = smb2_queryfs,
3229         .mand_lock = smb2_mand_lock,
3230         .mand_unlock_range = smb2_unlock_range,
3231         .push_mand_locks = smb2_push_mandatory_locks,
3232         .get_lease_key = smb2_get_lease_key,
3233         .set_lease_key = smb2_set_lease_key,
3234         .new_lease_key = smb2_new_lease_key,
3235         .generate_signingkey = generate_smb311signingkey,
3236         .calc_signature = smb3_calc_signature,
3237         .set_integrity  = smb3_set_integrity,
3238         .is_read_op = smb21_is_read_op,
3239         .set_oplock_level = smb3_set_oplock_level,
3240         .create_lease_buf = smb3_create_lease_buf,
3241         .parse_lease_buf = smb3_parse_lease_buf,
3242         .copychunk_range = smb2_copychunk_range,
3243         .duplicate_extents = smb2_duplicate_extents,
3244 /*      .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
3245         .wp_retry_size = smb2_wp_retry_size,
3246         .dir_needs_close = smb2_dir_needs_close,
3247         .fallocate = smb3_fallocate,
3248         .enum_snapshots = smb3_enum_snapshots,
3249         .init_transform_rq = smb3_init_transform_rq,
3250         .free_transform_rq = smb3_free_transform_rq,
3251         .is_transform_hdr = smb3_is_transform_hdr,
3252         .receive_transform = smb3_receive_transform,
3253         .get_dfs_refer = smb2_get_dfs_refer,
3254         .select_sectype = smb2_select_sectype,
3255 #ifdef CONFIG_CIFS_XATTR
3256         .query_all_EAs = smb2_query_eas,
3257         .set_EA = smb2_set_ea,
3258 #endif /* CIFS_XATTR */
3259 };
3260 #endif /* CIFS_SMB311 */
3261
3262 struct smb_version_values smb20_values = {
3263         .version_string = SMB20_VERSION_STRING,
3264         .protocol_id = SMB20_PROT_ID,
3265         .req_capabilities = 0, /* MBZ */
3266         .large_lock_type = 0,
3267         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3268         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3269         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3270         .header_size = sizeof(struct smb2_hdr),
3271         .max_header_size = MAX_SMB2_HDR_SIZE,
3272         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3273         .lock_cmd = SMB2_LOCK,
3274         .cap_unix = 0,
3275         .cap_nt_find = SMB2_NT_FIND,
3276         .cap_large_files = SMB2_LARGE_FILES,
3277         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3278         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3279         .create_lease_size = sizeof(struct create_lease),
3280 };
3281
3282 struct smb_version_values smb21_values = {
3283         .version_string = SMB21_VERSION_STRING,
3284         .protocol_id = SMB21_PROT_ID,
3285         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
3286         .large_lock_type = 0,
3287         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3288         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3289         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3290         .header_size = sizeof(struct smb2_hdr),
3291         .max_header_size = MAX_SMB2_HDR_SIZE,
3292         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3293         .lock_cmd = SMB2_LOCK,
3294         .cap_unix = 0,
3295         .cap_nt_find = SMB2_NT_FIND,
3296         .cap_large_files = SMB2_LARGE_FILES,
3297         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3298         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3299         .create_lease_size = sizeof(struct create_lease),
3300 };
3301
3302 struct smb_version_values smb3any_values = {
3303         .version_string = SMB3ANY_VERSION_STRING,
3304         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3305         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
3306         .large_lock_type = 0,
3307         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3308         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3309         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3310         .header_size = sizeof(struct smb2_hdr),
3311         .max_header_size = MAX_SMB2_HDR_SIZE,
3312         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3313         .lock_cmd = SMB2_LOCK,
3314         .cap_unix = 0,
3315         .cap_nt_find = SMB2_NT_FIND,
3316         .cap_large_files = SMB2_LARGE_FILES,
3317         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3318         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3319         .create_lease_size = sizeof(struct create_lease_v2),
3320 };
3321
3322 struct smb_version_values smbdefault_values = {
3323         .version_string = SMBDEFAULT_VERSION_STRING,
3324         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
3325         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
3326         .large_lock_type = 0,
3327         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3328         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3329         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3330         .header_size = sizeof(struct smb2_hdr),
3331         .max_header_size = MAX_SMB2_HDR_SIZE,
3332         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3333         .lock_cmd = SMB2_LOCK,
3334         .cap_unix = 0,
3335         .cap_nt_find = SMB2_NT_FIND,
3336         .cap_large_files = SMB2_LARGE_FILES,
3337         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3338         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3339         .create_lease_size = sizeof(struct create_lease_v2),
3340 };
3341
3342 struct smb_version_values smb30_values = {
3343         .version_string = SMB30_VERSION_STRING,
3344         .protocol_id = SMB30_PROT_ID,
3345         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
3346         .large_lock_type = 0,
3347         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3348         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3349         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3350         .header_size = sizeof(struct smb2_hdr),
3351         .max_header_size = MAX_SMB2_HDR_SIZE,
3352         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3353         .lock_cmd = SMB2_LOCK,
3354         .cap_unix = 0,
3355         .cap_nt_find = SMB2_NT_FIND,
3356         .cap_large_files = SMB2_LARGE_FILES,
3357         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3358         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3359         .create_lease_size = sizeof(struct create_lease_v2),
3360 };
3361
3362 struct smb_version_values smb302_values = {
3363         .version_string = SMB302_VERSION_STRING,
3364         .protocol_id = SMB302_PROT_ID,
3365         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
3366         .large_lock_type = 0,
3367         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3368         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3369         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3370         .header_size = sizeof(struct smb2_hdr),
3371         .max_header_size = MAX_SMB2_HDR_SIZE,
3372         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3373         .lock_cmd = SMB2_LOCK,
3374         .cap_unix = 0,
3375         .cap_nt_find = SMB2_NT_FIND,
3376         .cap_large_files = SMB2_LARGE_FILES,
3377         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3378         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3379         .create_lease_size = sizeof(struct create_lease_v2),
3380 };
3381
3382 #ifdef CONFIG_CIFS_SMB311
3383 struct smb_version_values smb311_values = {
3384         .version_string = SMB311_VERSION_STRING,
3385         .protocol_id = SMB311_PROT_ID,
3386         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
3387         .large_lock_type = 0,
3388         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
3389         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
3390         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
3391         .header_size = sizeof(struct smb2_hdr),
3392         .max_header_size = MAX_SMB2_HDR_SIZE,
3393         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
3394         .lock_cmd = SMB2_LOCK,
3395         .cap_unix = 0,
3396         .cap_nt_find = SMB2_NT_FIND,
3397         .cap_large_files = SMB2_LARGE_FILES,
3398         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
3399         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
3400         .create_lease_size = sizeof(struct create_lease_v2),
3401 };
3402 #endif /* SMB311 */