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