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