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