Merge tag 'fuse-update-5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[linux-2.6-microblaze.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 /* Change credits for different ops and return the total number of credits */
38 static int
39 change_conf(struct TCP_Server_Info *server)
40 {
41         server->credits += server->echo_credits + server->oplock_credits;
42         server->oplock_credits = server->echo_credits = 0;
43         switch (server->credits) {
44         case 0:
45                 return 0;
46         case 1:
47                 server->echoes = false;
48                 server->oplocks = false;
49                 break;
50         case 2:
51                 server->echoes = true;
52                 server->oplocks = false;
53                 server->echo_credits = 1;
54                 break;
55         default:
56                 server->echoes = true;
57                 if (enable_oplocks) {
58                         server->oplocks = true;
59                         server->oplock_credits = 1;
60                 } else
61                         server->oplocks = false;
62
63                 server->echo_credits = 1;
64         }
65         server->credits -= server->echo_credits + server->oplock_credits;
66         return server->credits + server->echo_credits + server->oplock_credits;
67 }
68
69 static void
70 smb2_add_credits(struct TCP_Server_Info *server,
71                  const struct cifs_credits *credits, const int optype)
72 {
73         int *val, rc = -1;
74         unsigned int add = credits->value;
75         unsigned int instance = credits->instance;
76         bool reconnect_detected = false;
77
78         spin_lock(&server->req_lock);
79         val = server->ops->get_credits_field(server, optype);
80
81         /* eg found case where write overlapping reconnect messed up credits */
82         if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
83                 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
84                         server->hostname, *val);
85         if ((instance == 0) || (instance == server->reconnect_instance))
86                 *val += add;
87         else
88                 reconnect_detected = true;
89
90         if (*val > 65000) {
91                 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
92                 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
93         }
94         server->in_flight--;
95         if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
96                 rc = change_conf(server);
97         /*
98          * Sometimes server returns 0 credits on oplock break ack - we need to
99          * rebalance credits in this case.
100          */
101         else if (server->in_flight > 0 && server->oplock_credits == 0 &&
102                  server->oplocks) {
103                 if (server->credits > 1) {
104                         server->credits--;
105                         server->oplock_credits++;
106                 }
107         }
108         spin_unlock(&server->req_lock);
109         wake_up(&server->request_q);
110
111         if (reconnect_detected)
112                 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
113                          add, instance);
114
115         if (server->tcpStatus == CifsNeedReconnect
116             || server->tcpStatus == CifsExiting)
117                 return;
118
119         switch (rc) {
120         case -1:
121                 /* change_conf hasn't been executed */
122                 break;
123         case 0:
124                 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
125                 break;
126         case 1:
127                 cifs_dbg(VFS, "disabling echoes and oplocks\n");
128                 break;
129         case 2:
130                 cifs_dbg(FYI, "disabling oplocks\n");
131                 break;
132         default:
133                 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
134         }
135 }
136
137 static void
138 smb2_set_credits(struct TCP_Server_Info *server, const int val)
139 {
140         spin_lock(&server->req_lock);
141         server->credits = val;
142         if (val == 1)
143                 server->reconnect_instance++;
144         spin_unlock(&server->req_lock);
145         /* don't log while holding the lock */
146         if (val == 1)
147                 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
148 }
149
150 static int *
151 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
152 {
153         switch (optype) {
154         case CIFS_ECHO_OP:
155                 return &server->echo_credits;
156         case CIFS_OBREAK_OP:
157                 return &server->oplock_credits;
158         default:
159                 return &server->credits;
160         }
161 }
162
163 static unsigned int
164 smb2_get_credits(struct mid_q_entry *mid)
165 {
166         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
167
168         if (mid->mid_state == MID_RESPONSE_RECEIVED
169             || mid->mid_state == MID_RESPONSE_MALFORMED)
170                 return le16_to_cpu(shdr->CreditRequest);
171
172         return 0;
173 }
174
175 static int
176 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
177                       unsigned int *num, struct cifs_credits *credits)
178 {
179         int rc = 0;
180         unsigned int scredits;
181
182         spin_lock(&server->req_lock);
183         while (1) {
184                 if (server->credits <= 0) {
185                         spin_unlock(&server->req_lock);
186                         cifs_num_waiters_inc(server);
187                         rc = wait_event_killable(server->request_q,
188                                 has_credits(server, &server->credits, 1));
189                         cifs_num_waiters_dec(server);
190                         if (rc)
191                                 return rc;
192                         spin_lock(&server->req_lock);
193                 } else {
194                         if (server->tcpStatus == CifsExiting) {
195                                 spin_unlock(&server->req_lock);
196                                 return -ENOENT;
197                         }
198
199                         scredits = server->credits;
200                         /* can deadlock with reopen */
201                         if (scredits <= 8) {
202                                 *num = SMB2_MAX_BUFFER_SIZE;
203                                 credits->value = 0;
204                                 credits->instance = 0;
205                                 break;
206                         }
207
208                         /* leave some credits for reopen and other ops */
209                         scredits -= 8;
210                         *num = min_t(unsigned int, size,
211                                      scredits * SMB2_MAX_BUFFER_SIZE);
212
213                         credits->value =
214                                 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
215                         credits->instance = server->reconnect_instance;
216                         server->credits -= credits->value;
217                         server->in_flight++;
218                         break;
219                 }
220         }
221         spin_unlock(&server->req_lock);
222         return rc;
223 }
224
225 static int
226 smb2_adjust_credits(struct TCP_Server_Info *server,
227                     struct cifs_credits *credits,
228                     const unsigned int payload_size)
229 {
230         int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
231
232         if (!credits->value || credits->value == new_val)
233                 return 0;
234
235         if (credits->value < new_val) {
236                 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
237                           credits->value, new_val);
238                 return -ENOTSUPP;
239         }
240
241         spin_lock(&server->req_lock);
242
243         if (server->reconnect_instance != credits->instance) {
244                 spin_unlock(&server->req_lock);
245                 cifs_dbg(VFS, "trying to return %d credits to old session\n",
246                          credits->value - new_val);
247                 return -EAGAIN;
248         }
249
250         server->credits += credits->value - new_val;
251         spin_unlock(&server->req_lock);
252         wake_up(&server->request_q);
253         credits->value = new_val;
254         return 0;
255 }
256
257 static __u64
258 smb2_get_next_mid(struct TCP_Server_Info *server)
259 {
260         __u64 mid;
261         /* for SMB2 we need the current value */
262         spin_lock(&GlobalMid_Lock);
263         mid = server->CurrentMid++;
264         spin_unlock(&GlobalMid_Lock);
265         return mid;
266 }
267
268 static void
269 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
270 {
271         spin_lock(&GlobalMid_Lock);
272         if (server->CurrentMid >= val)
273                 server->CurrentMid -= val;
274         spin_unlock(&GlobalMid_Lock);
275 }
276
277 static struct mid_q_entry *
278 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
279 {
280         struct mid_q_entry *mid;
281         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
282         __u64 wire_mid = le64_to_cpu(shdr->MessageId);
283
284         if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
285                 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
286                 return NULL;
287         }
288
289         spin_lock(&GlobalMid_Lock);
290         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
291                 if ((mid->mid == wire_mid) &&
292                     (mid->mid_state == MID_REQUEST_SUBMITTED) &&
293                     (mid->command == shdr->Command)) {
294                         kref_get(&mid->refcount);
295                         spin_unlock(&GlobalMid_Lock);
296                         return mid;
297                 }
298         }
299         spin_unlock(&GlobalMid_Lock);
300         return NULL;
301 }
302
303 static void
304 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
305 {
306 #ifdef CONFIG_CIFS_DEBUG2
307         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
308
309         cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
310                  shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
311                  shdr->ProcessId);
312         cifs_dbg(VFS, "smb buf %p len %u\n", buf,
313                  server->ops->calc_smb_size(buf, server));
314 #endif
315 }
316
317 static bool
318 smb2_need_neg(struct TCP_Server_Info *server)
319 {
320         return server->max_read == 0;
321 }
322
323 static int
324 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
325 {
326         int rc;
327         ses->server->CurrentMid = 0;
328         rc = SMB2_negotiate(xid, ses);
329         /* BB we probably don't need to retry with modern servers */
330         if (rc == -EAGAIN)
331                 rc = -EHOSTDOWN;
332         return rc;
333 }
334
335 static unsigned int
336 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
337 {
338         struct TCP_Server_Info *server = tcon->ses->server;
339         unsigned int wsize;
340
341         /* start with specified wsize, or default */
342         wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
343         wsize = min_t(unsigned int, wsize, server->max_write);
344 #ifdef CONFIG_CIFS_SMB_DIRECT
345         if (server->rdma) {
346                 if (server->sign)
347                         wsize = min_t(unsigned int,
348                                 wsize, server->smbd_conn->max_fragmented_send_size);
349                 else
350                         wsize = min_t(unsigned int,
351                                 wsize, server->smbd_conn->max_readwrite_size);
352         }
353 #endif
354         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
355                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
356
357         return wsize;
358 }
359
360 static unsigned int
361 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
362 {
363         struct TCP_Server_Info *server = tcon->ses->server;
364         unsigned int wsize;
365
366         /* start with specified wsize, or default */
367         wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
368         wsize = min_t(unsigned int, wsize, server->max_write);
369 #ifdef CONFIG_CIFS_SMB_DIRECT
370         if (server->rdma) {
371                 if (server->sign)
372                         wsize = min_t(unsigned int,
373                                 wsize, server->smbd_conn->max_fragmented_send_size);
374                 else
375                         wsize = min_t(unsigned int,
376                                 wsize, server->smbd_conn->max_readwrite_size);
377         }
378 #endif
379         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
380                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
381
382         return wsize;
383 }
384
385 static unsigned int
386 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
387 {
388         struct TCP_Server_Info *server = tcon->ses->server;
389         unsigned int rsize;
390
391         /* start with specified rsize, or default */
392         rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
393         rsize = min_t(unsigned int, rsize, server->max_read);
394 #ifdef CONFIG_CIFS_SMB_DIRECT
395         if (server->rdma) {
396                 if (server->sign)
397                         rsize = min_t(unsigned int,
398                                 rsize, server->smbd_conn->max_fragmented_recv_size);
399                 else
400                         rsize = min_t(unsigned int,
401                                 rsize, server->smbd_conn->max_readwrite_size);
402         }
403 #endif
404
405         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
406                 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
407
408         return rsize;
409 }
410
411 static unsigned int
412 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
413 {
414         struct TCP_Server_Info *server = tcon->ses->server;
415         unsigned int rsize;
416
417         /* start with specified rsize, or default */
418         rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
419         rsize = min_t(unsigned int, rsize, server->max_read);
420 #ifdef CONFIG_CIFS_SMB_DIRECT
421         if (server->rdma) {
422                 if (server->sign)
423                         rsize = min_t(unsigned int,
424                                 rsize, server->smbd_conn->max_fragmented_recv_size);
425                 else
426                         rsize = min_t(unsigned int,
427                                 rsize, server->smbd_conn->max_readwrite_size);
428         }
429 #endif
430
431         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
432                 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
433
434         return rsize;
435 }
436
437 static int
438 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
439                         size_t buf_len,
440                         struct cifs_server_iface **iface_list,
441                         size_t *iface_count)
442 {
443         struct network_interface_info_ioctl_rsp *p;
444         struct sockaddr_in *addr4;
445         struct sockaddr_in6 *addr6;
446         struct iface_info_ipv4 *p4;
447         struct iface_info_ipv6 *p6;
448         struct cifs_server_iface *info;
449         ssize_t bytes_left;
450         size_t next = 0;
451         int nb_iface = 0;
452         int rc = 0;
453
454         *iface_list = NULL;
455         *iface_count = 0;
456
457         /*
458          * Fist pass: count and sanity check
459          */
460
461         bytes_left = buf_len;
462         p = buf;
463         while (bytes_left >= sizeof(*p)) {
464                 nb_iface++;
465                 next = le32_to_cpu(p->Next);
466                 if (!next) {
467                         bytes_left -= sizeof(*p);
468                         break;
469                 }
470                 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
471                 bytes_left -= next;
472         }
473
474         if (!nb_iface) {
475                 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
476                 rc = -EINVAL;
477                 goto out;
478         }
479
480         if (bytes_left || p->Next)
481                 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
482
483
484         /*
485          * Second pass: extract info to internal structure
486          */
487
488         *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
489         if (!*iface_list) {
490                 rc = -ENOMEM;
491                 goto out;
492         }
493
494         info = *iface_list;
495         bytes_left = buf_len;
496         p = buf;
497         while (bytes_left >= sizeof(*p)) {
498                 info->speed = le64_to_cpu(p->LinkSpeed);
499                 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
500                 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
501
502                 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
503                 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
504                 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
505                          le32_to_cpu(p->Capability));
506
507                 switch (p->Family) {
508                 /*
509                  * The kernel and wire socket structures have the same
510                  * layout and use network byte order but make the
511                  * conversion explicit in case either one changes.
512                  */
513                 case INTERNETWORK:
514                         addr4 = (struct sockaddr_in *)&info->sockaddr;
515                         p4 = (struct iface_info_ipv4 *)p->Buffer;
516                         addr4->sin_family = AF_INET;
517                         memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
518
519                         /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
520                         addr4->sin_port = cpu_to_be16(CIFS_PORT);
521
522                         cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
523                                  &addr4->sin_addr);
524                         break;
525                 case INTERNETWORKV6:
526                         addr6 = (struct sockaddr_in6 *)&info->sockaddr;
527                         p6 = (struct iface_info_ipv6 *)p->Buffer;
528                         addr6->sin6_family = AF_INET6;
529                         memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
530
531                         /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
532                         addr6->sin6_flowinfo = 0;
533                         addr6->sin6_scope_id = 0;
534                         addr6->sin6_port = cpu_to_be16(CIFS_PORT);
535
536                         cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
537                                  &addr6->sin6_addr);
538                         break;
539                 default:
540                         cifs_dbg(VFS,
541                                  "%s: skipping unsupported socket family\n",
542                                  __func__);
543                         goto next_iface;
544                 }
545
546                 (*iface_count)++;
547                 info++;
548 next_iface:
549                 next = le32_to_cpu(p->Next);
550                 if (!next)
551                         break;
552                 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
553                 bytes_left -= next;
554         }
555
556         if (!*iface_count) {
557                 rc = -EINVAL;
558                 goto out;
559         }
560
561 out:
562         if (rc) {
563                 kfree(*iface_list);
564                 *iface_count = 0;
565                 *iface_list = NULL;
566         }
567         return rc;
568 }
569
570
571 static int
572 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
573 {
574         int rc;
575         unsigned int ret_data_len = 0;
576         struct network_interface_info_ioctl_rsp *out_buf = NULL;
577         struct cifs_server_iface *iface_list;
578         size_t iface_count;
579         struct cifs_ses *ses = tcon->ses;
580
581         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
582                         FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
583                         NULL /* no data input */, 0 /* no data input */,
584                         CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
585         if (rc == -EOPNOTSUPP) {
586                 cifs_dbg(FYI,
587                          "server does not support query network interfaces\n");
588                 goto out;
589         } else if (rc != 0) {
590                 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
591                 goto out;
592         }
593
594         rc = parse_server_interfaces(out_buf, ret_data_len,
595                                      &iface_list, &iface_count);
596         if (rc)
597                 goto out;
598
599         spin_lock(&ses->iface_lock);
600         kfree(ses->iface_list);
601         ses->iface_list = iface_list;
602         ses->iface_count = iface_count;
603         ses->iface_last_update = jiffies;
604         spin_unlock(&ses->iface_lock);
605
606 out:
607         kfree(out_buf);
608         return rc;
609 }
610
611 static void
612 smb2_close_cached_fid(struct kref *ref)
613 {
614         struct cached_fid *cfid = container_of(ref, struct cached_fid,
615                                                refcount);
616
617         if (cfid->is_valid) {
618                 cifs_dbg(FYI, "clear cached root file handle\n");
619                 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
620                            cfid->fid->volatile_fid);
621                 cfid->is_valid = false;
622                 cfid->file_all_info_is_valid = false;
623         }
624 }
625
626 void close_shroot(struct cached_fid *cfid)
627 {
628         mutex_lock(&cfid->fid_mutex);
629         kref_put(&cfid->refcount, smb2_close_cached_fid);
630         mutex_unlock(&cfid->fid_mutex);
631 }
632
633 void
634 smb2_cached_lease_break(struct work_struct *work)
635 {
636         struct cached_fid *cfid = container_of(work,
637                                 struct cached_fid, lease_break);
638
639         close_shroot(cfid);
640 }
641
642 /*
643  * Open the directory at the root of a share
644  */
645 int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
646 {
647         struct cifs_ses *ses = tcon->ses;
648         struct TCP_Server_Info *server = ses->server;
649         struct cifs_open_parms oparms;
650         struct smb2_create_rsp *o_rsp = NULL;
651         struct smb2_query_info_rsp *qi_rsp = NULL;
652         int resp_buftype[2];
653         struct smb_rqst rqst[2];
654         struct kvec rsp_iov[2];
655         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
656         struct kvec qi_iov[1];
657         int rc, flags = 0;
658         __le16 utf16_path = 0; /* Null - since an open of top of share */
659         u8 oplock = SMB2_OPLOCK_LEVEL_II;
660
661         mutex_lock(&tcon->crfid.fid_mutex);
662         if (tcon->crfid.is_valid) {
663                 cifs_dbg(FYI, "found a cached root file handle\n");
664                 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
665                 kref_get(&tcon->crfid.refcount);
666                 mutex_unlock(&tcon->crfid.fid_mutex);
667                 return 0;
668         }
669
670         if (smb3_encryption_required(tcon))
671                 flags |= CIFS_TRANSFORM_REQ;
672
673         memset(rqst, 0, sizeof(rqst));
674         resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
675         memset(rsp_iov, 0, sizeof(rsp_iov));
676
677         /* Open */
678         memset(&open_iov, 0, sizeof(open_iov));
679         rqst[0].rq_iov = open_iov;
680         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
681
682         oparms.tcon = tcon;
683         oparms.create_options = 0;
684         oparms.desired_access = FILE_READ_ATTRIBUTES;
685         oparms.disposition = FILE_OPEN;
686         oparms.fid = pfid;
687         oparms.reconnect = false;
688
689         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
690         if (rc)
691                 goto oshr_exit;
692         smb2_set_next_command(tcon, &rqst[0]);
693
694         memset(&qi_iov, 0, sizeof(qi_iov));
695         rqst[1].rq_iov = qi_iov;
696         rqst[1].rq_nvec = 1;
697
698         rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
699                                   COMPOUND_FID, FILE_ALL_INFORMATION,
700                                   SMB2_O_INFO_FILE, 0,
701                                   sizeof(struct smb2_file_all_info) +
702                                   PATH_MAX * 2, 0, NULL);
703         if (rc)
704                 goto oshr_exit;
705
706         smb2_set_related(&rqst[1]);
707
708         rc = compound_send_recv(xid, ses, flags, 2, rqst,
709                                 resp_buftype, rsp_iov);
710         if (rc)
711                 goto oshr_exit;
712
713         o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
714         oparms.fid->persistent_fid = o_rsp->PersistentFileId;
715         oparms.fid->volatile_fid = o_rsp->VolatileFileId;
716 #ifdef CONFIG_CIFS_DEBUG2
717         oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
718 #endif /* CIFS_DEBUG2 */
719
720         memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
721         tcon->crfid.tcon = tcon;
722         tcon->crfid.is_valid = true;
723         kref_init(&tcon->crfid.refcount);
724
725         if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
726                 kref_get(&tcon->crfid.refcount);
727                 oplock = smb2_parse_lease_state(server, o_rsp,
728                                                 &oparms.fid->epoch,
729                                                 oparms.fid->lease_key);
730         } else
731                 goto oshr_exit;
732
733         qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
734         if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
735                 goto oshr_exit;
736         if (!smb2_validate_and_copy_iov(
737                                 le16_to_cpu(qi_rsp->OutputBufferOffset),
738                                 sizeof(struct smb2_file_all_info),
739                                 &rsp_iov[1], sizeof(struct smb2_file_all_info),
740                                 (char *)&tcon->crfid.file_all_info))
741                 tcon->crfid.file_all_info_is_valid = 1;
742
743  oshr_exit:
744         mutex_unlock(&tcon->crfid.fid_mutex);
745         SMB2_open_free(&rqst[0]);
746         SMB2_query_info_free(&rqst[1]);
747         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
748         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
749         return rc;
750 }
751
752 static void
753 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
754 {
755         int rc;
756         __le16 srch_path = 0; /* Null - open root of share */
757         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
758         struct cifs_open_parms oparms;
759         struct cifs_fid fid;
760         bool no_cached_open = tcon->nohandlecache;
761
762         oparms.tcon = tcon;
763         oparms.desired_access = FILE_READ_ATTRIBUTES;
764         oparms.disposition = FILE_OPEN;
765         oparms.create_options = 0;
766         oparms.fid = &fid;
767         oparms.reconnect = false;
768
769         if (no_cached_open)
770                 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
771                                NULL);
772         else
773                 rc = open_shroot(xid, tcon, &fid);
774
775         if (rc)
776                 return;
777
778         SMB3_request_interfaces(xid, tcon);
779
780         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
781                         FS_ATTRIBUTE_INFORMATION);
782         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
783                         FS_DEVICE_INFORMATION);
784         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
785                         FS_VOLUME_INFORMATION);
786         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
787                         FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
788         if (no_cached_open)
789                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
790         else
791                 close_shroot(&tcon->crfid);
792
793         return;
794 }
795
796 static void
797 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
798 {
799         int rc;
800         __le16 srch_path = 0; /* Null - open root of share */
801         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
802         struct cifs_open_parms oparms;
803         struct cifs_fid fid;
804
805         oparms.tcon = tcon;
806         oparms.desired_access = FILE_READ_ATTRIBUTES;
807         oparms.disposition = FILE_OPEN;
808         oparms.create_options = 0;
809         oparms.fid = &fid;
810         oparms.reconnect = false;
811
812         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
813         if (rc)
814                 return;
815
816         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
817                         FS_ATTRIBUTE_INFORMATION);
818         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
819                         FS_DEVICE_INFORMATION);
820         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
821         return;
822 }
823
824 static int
825 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
826                         struct cifs_sb_info *cifs_sb, const char *full_path)
827 {
828         int rc;
829         __le16 *utf16_path;
830         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
831         struct cifs_open_parms oparms;
832         struct cifs_fid fid;
833
834         if ((*full_path == 0) && tcon->crfid.is_valid)
835                 return 0;
836
837         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
838         if (!utf16_path)
839                 return -ENOMEM;
840
841         oparms.tcon = tcon;
842         oparms.desired_access = FILE_READ_ATTRIBUTES;
843         oparms.disposition = FILE_OPEN;
844         if (backup_cred(cifs_sb))
845                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
846         else
847                 oparms.create_options = 0;
848         oparms.fid = &fid;
849         oparms.reconnect = false;
850
851         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
852         if (rc) {
853                 kfree(utf16_path);
854                 return rc;
855         }
856
857         rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
858         kfree(utf16_path);
859         return rc;
860 }
861
862 static int
863 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
864                   struct cifs_sb_info *cifs_sb, const char *full_path,
865                   u64 *uniqueid, FILE_ALL_INFO *data)
866 {
867         *uniqueid = le64_to_cpu(data->IndexNumber);
868         return 0;
869 }
870
871 static int
872 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
873                      struct cifs_fid *fid, FILE_ALL_INFO *data)
874 {
875         int rc;
876         struct smb2_file_all_info *smb2_data;
877
878         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
879                             GFP_KERNEL);
880         if (smb2_data == NULL)
881                 return -ENOMEM;
882
883         rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
884                              smb2_data);
885         if (!rc)
886                 move_smb2_info_to_cifs(data, smb2_data);
887         kfree(smb2_data);
888         return rc;
889 }
890
891 #ifdef CONFIG_CIFS_XATTR
892 static ssize_t
893 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
894                      struct smb2_file_full_ea_info *src, size_t src_size,
895                      const unsigned char *ea_name)
896 {
897         int rc = 0;
898         unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
899         char *name, *value;
900         size_t buf_size = dst_size;
901         size_t name_len, value_len, user_name_len;
902
903         while (src_size > 0) {
904                 name = &src->ea_data[0];
905                 name_len = (size_t)src->ea_name_length;
906                 value = &src->ea_data[src->ea_name_length + 1];
907                 value_len = (size_t)le16_to_cpu(src->ea_value_length);
908
909                 if (name_len == 0) {
910                         break;
911                 }
912
913                 if (src_size < 8 + name_len + 1 + value_len) {
914                         cifs_dbg(FYI, "EA entry goes beyond length of list\n");
915                         rc = -EIO;
916                         goto out;
917                 }
918
919                 if (ea_name) {
920                         if (ea_name_len == name_len &&
921                             memcmp(ea_name, name, name_len) == 0) {
922                                 rc = value_len;
923                                 if (dst_size == 0)
924                                         goto out;
925                                 if (dst_size < value_len) {
926                                         rc = -ERANGE;
927                                         goto out;
928                                 }
929                                 memcpy(dst, value, value_len);
930                                 goto out;
931                         }
932                 } else {
933                         /* 'user.' plus a terminating null */
934                         user_name_len = 5 + 1 + name_len;
935
936                         if (buf_size == 0) {
937                                 /* skip copy - calc size only */
938                                 rc += user_name_len;
939                         } else if (dst_size >= user_name_len) {
940                                 dst_size -= user_name_len;
941                                 memcpy(dst, "user.", 5);
942                                 dst += 5;
943                                 memcpy(dst, src->ea_data, name_len);
944                                 dst += name_len;
945                                 *dst = 0;
946                                 ++dst;
947                                 rc += user_name_len;
948                         } else {
949                                 /* stop before overrun buffer */
950                                 rc = -ERANGE;
951                                 break;
952                         }
953                 }
954
955                 if (!src->next_entry_offset)
956                         break;
957
958                 if (src_size < le32_to_cpu(src->next_entry_offset)) {
959                         /* stop before overrun buffer */
960                         rc = -ERANGE;
961                         break;
962                 }
963                 src_size -= le32_to_cpu(src->next_entry_offset);
964                 src = (void *)((char *)src +
965                                le32_to_cpu(src->next_entry_offset));
966         }
967
968         /* didn't find the named attribute */
969         if (ea_name)
970                 rc = -ENODATA;
971
972 out:
973         return (ssize_t)rc;
974 }
975
976 static ssize_t
977 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
978                const unsigned char *path, const unsigned char *ea_name,
979                char *ea_data, size_t buf_size,
980                struct cifs_sb_info *cifs_sb)
981 {
982         int rc;
983         __le16 *utf16_path;
984         struct kvec rsp_iov = {NULL, 0};
985         int buftype = CIFS_NO_BUFFER;
986         struct smb2_query_info_rsp *rsp;
987         struct smb2_file_full_ea_info *info = NULL;
988
989         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
990         if (!utf16_path)
991                 return -ENOMEM;
992
993         rc = smb2_query_info_compound(xid, tcon, utf16_path,
994                                       FILE_READ_EA,
995                                       FILE_FULL_EA_INFORMATION,
996                                       SMB2_O_INFO_FILE,
997                                       CIFSMaxBufSize -
998                                       MAX_SMB2_CREATE_RESPONSE_SIZE -
999                                       MAX_SMB2_CLOSE_RESPONSE_SIZE,
1000                                       &rsp_iov, &buftype, cifs_sb);
1001         if (rc) {
1002                 /*
1003                  * If ea_name is NULL (listxattr) and there are no EAs,
1004                  * return 0 as it's not an error. Otherwise, the specified
1005                  * ea_name was not found.
1006                  */
1007                 if (!ea_name && rc == -ENODATA)
1008                         rc = 0;
1009                 goto qeas_exit;
1010         }
1011
1012         rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1013         rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1014                                le32_to_cpu(rsp->OutputBufferLength),
1015                                &rsp_iov,
1016                                sizeof(struct smb2_file_full_ea_info));
1017         if (rc)
1018                 goto qeas_exit;
1019
1020         info = (struct smb2_file_full_ea_info *)(
1021                         le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1022         rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1023                         le32_to_cpu(rsp->OutputBufferLength), ea_name);
1024
1025  qeas_exit:
1026         kfree(utf16_path);
1027         free_rsp_buf(buftype, rsp_iov.iov_base);
1028         return rc;
1029 }
1030
1031
1032 static int
1033 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1034             const char *path, const char *ea_name, const void *ea_value,
1035             const __u16 ea_value_len, const struct nls_table *nls_codepage,
1036             struct cifs_sb_info *cifs_sb)
1037 {
1038         struct cifs_ses *ses = tcon->ses;
1039         __le16 *utf16_path = NULL;
1040         int ea_name_len = strlen(ea_name);
1041         int flags = 0;
1042         int len;
1043         struct smb_rqst rqst[3];
1044         int resp_buftype[3];
1045         struct kvec rsp_iov[3];
1046         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1047         struct cifs_open_parms oparms;
1048         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1049         struct cifs_fid fid;
1050         struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1051         unsigned int size[1];
1052         void *data[1];
1053         struct smb2_file_full_ea_info *ea = NULL;
1054         struct kvec close_iov[1];
1055         int rc;
1056
1057         if (smb3_encryption_required(tcon))
1058                 flags |= CIFS_TRANSFORM_REQ;
1059
1060         if (ea_name_len > 255)
1061                 return -EINVAL;
1062
1063         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1064         if (!utf16_path)
1065                 return -ENOMEM;
1066
1067         memset(rqst, 0, sizeof(rqst));
1068         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1069         memset(rsp_iov, 0, sizeof(rsp_iov));
1070
1071         if (ses->server->ops->query_all_EAs) {
1072                 if (!ea_value) {
1073                         rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1074                                                              ea_name, NULL, 0,
1075                                                              cifs_sb);
1076                         if (rc == -ENODATA)
1077                                 goto sea_exit;
1078                 }
1079         }
1080
1081         /* Open */
1082         memset(&open_iov, 0, sizeof(open_iov));
1083         rqst[0].rq_iov = open_iov;
1084         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1085
1086         memset(&oparms, 0, sizeof(oparms));
1087         oparms.tcon = tcon;
1088         oparms.desired_access = FILE_WRITE_EA;
1089         oparms.disposition = FILE_OPEN;
1090         if (backup_cred(cifs_sb))
1091                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1092         else
1093                 oparms.create_options = 0;
1094         oparms.fid = &fid;
1095         oparms.reconnect = false;
1096
1097         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1098         if (rc)
1099                 goto sea_exit;
1100         smb2_set_next_command(tcon, &rqst[0]);
1101
1102
1103         /* Set Info */
1104         memset(&si_iov, 0, sizeof(si_iov));
1105         rqst[1].rq_iov = si_iov;
1106         rqst[1].rq_nvec = 1;
1107
1108         len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1109         ea = kzalloc(len, GFP_KERNEL);
1110         if (ea == NULL) {
1111                 rc = -ENOMEM;
1112                 goto sea_exit;
1113         }
1114
1115         ea->ea_name_length = ea_name_len;
1116         ea->ea_value_length = cpu_to_le16(ea_value_len);
1117         memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1118         memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1119
1120         size[0] = len;
1121         data[0] = ea;
1122
1123         rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1124                                 COMPOUND_FID, current->tgid,
1125                                 FILE_FULL_EA_INFORMATION,
1126                                 SMB2_O_INFO_FILE, 0, data, size);
1127         smb2_set_next_command(tcon, &rqst[1]);
1128         smb2_set_related(&rqst[1]);
1129
1130
1131         /* Close */
1132         memset(&close_iov, 0, sizeof(close_iov));
1133         rqst[2].rq_iov = close_iov;
1134         rqst[2].rq_nvec = 1;
1135         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1136         smb2_set_related(&rqst[2]);
1137
1138         rc = compound_send_recv(xid, ses, flags, 3, rqst,
1139                                 resp_buftype, rsp_iov);
1140
1141  sea_exit:
1142         kfree(ea);
1143         kfree(utf16_path);
1144         SMB2_open_free(&rqst[0]);
1145         SMB2_set_info_free(&rqst[1]);
1146         SMB2_close_free(&rqst[2]);
1147         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1148         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1149         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1150         return rc;
1151 }
1152 #endif
1153
1154 static bool
1155 smb2_can_echo(struct TCP_Server_Info *server)
1156 {
1157         return server->echoes;
1158 }
1159
1160 static void
1161 smb2_clear_stats(struct cifs_tcon *tcon)
1162 {
1163         int i;
1164         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1165                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1166                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1167         }
1168 }
1169
1170 static void
1171 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1172 {
1173         seq_puts(m, "\n\tShare Capabilities:");
1174         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1175                 seq_puts(m, " DFS,");
1176         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1177                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1178         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1179                 seq_puts(m, " SCALEOUT,");
1180         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1181                 seq_puts(m, " CLUSTER,");
1182         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1183                 seq_puts(m, " ASYMMETRIC,");
1184         if (tcon->capabilities == 0)
1185                 seq_puts(m, " None");
1186         if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1187                 seq_puts(m, " Aligned,");
1188         if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1189                 seq_puts(m, " Partition Aligned,");
1190         if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1191                 seq_puts(m, " SSD,");
1192         if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1193                 seq_puts(m, " TRIM-support,");
1194
1195         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1196         seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1197         if (tcon->perf_sector_size)
1198                 seq_printf(m, "\tOptimal sector size: 0x%x",
1199                            tcon->perf_sector_size);
1200         seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1201 }
1202
1203 static void
1204 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1205 {
1206         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1207         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1208
1209         /*
1210          *  Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1211          *  totals (requests sent) since those SMBs are per-session not per tcon
1212          */
1213         seq_printf(m, "\nBytes read: %llu  Bytes written: %llu",
1214                    (long long)(tcon->bytes_read),
1215                    (long long)(tcon->bytes_written));
1216         seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1217                    atomic_read(&tcon->num_local_opens),
1218                    atomic_read(&tcon->num_remote_opens));
1219         seq_printf(m, "\nTreeConnects: %d total %d failed",
1220                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1221                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1222         seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1223                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1224                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1225         seq_printf(m, "\nCreates: %d total %d failed",
1226                    atomic_read(&sent[SMB2_CREATE_HE]),
1227                    atomic_read(&failed[SMB2_CREATE_HE]));
1228         seq_printf(m, "\nCloses: %d total %d failed",
1229                    atomic_read(&sent[SMB2_CLOSE_HE]),
1230                    atomic_read(&failed[SMB2_CLOSE_HE]));
1231         seq_printf(m, "\nFlushes: %d total %d failed",
1232                    atomic_read(&sent[SMB2_FLUSH_HE]),
1233                    atomic_read(&failed[SMB2_FLUSH_HE]));
1234         seq_printf(m, "\nReads: %d total %d failed",
1235                    atomic_read(&sent[SMB2_READ_HE]),
1236                    atomic_read(&failed[SMB2_READ_HE]));
1237         seq_printf(m, "\nWrites: %d total %d failed",
1238                    atomic_read(&sent[SMB2_WRITE_HE]),
1239                    atomic_read(&failed[SMB2_WRITE_HE]));
1240         seq_printf(m, "\nLocks: %d total %d failed",
1241                    atomic_read(&sent[SMB2_LOCK_HE]),
1242                    atomic_read(&failed[SMB2_LOCK_HE]));
1243         seq_printf(m, "\nIOCTLs: %d total %d failed",
1244                    atomic_read(&sent[SMB2_IOCTL_HE]),
1245                    atomic_read(&failed[SMB2_IOCTL_HE]));
1246         seq_printf(m, "\nQueryDirectories: %d total %d failed",
1247                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1248                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1249         seq_printf(m, "\nChangeNotifies: %d total %d failed",
1250                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1251                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1252         seq_printf(m, "\nQueryInfos: %d total %d failed",
1253                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1254                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1255         seq_printf(m, "\nSetInfos: %d total %d failed",
1256                    atomic_read(&sent[SMB2_SET_INFO_HE]),
1257                    atomic_read(&failed[SMB2_SET_INFO_HE]));
1258         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1259                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1260                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1261 }
1262
1263 static void
1264 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1265 {
1266         struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1267         struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1268
1269         cfile->fid.persistent_fid = fid->persistent_fid;
1270         cfile->fid.volatile_fid = fid->volatile_fid;
1271 #ifdef CONFIG_CIFS_DEBUG2
1272         cfile->fid.mid = fid->mid;
1273 #endif /* CIFS_DEBUG2 */
1274         server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1275                                       &fid->purge_cache);
1276         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1277         memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1278 }
1279
1280 static void
1281 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1282                 struct cifs_fid *fid)
1283 {
1284         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1285 }
1286
1287 static int
1288 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1289                      u64 persistent_fid, u64 volatile_fid,
1290                      struct copychunk_ioctl *pcchunk)
1291 {
1292         int rc;
1293         unsigned int ret_data_len;
1294         struct resume_key_req *res_key;
1295
1296         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1297                         FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1298                         NULL, 0 /* no input */, CIFSMaxBufSize,
1299                         (char **)&res_key, &ret_data_len);
1300
1301         if (rc) {
1302                 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1303                 goto req_res_key_exit;
1304         }
1305         if (ret_data_len < sizeof(struct resume_key_req)) {
1306                 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1307                 rc = -EINVAL;
1308                 goto req_res_key_exit;
1309         }
1310         memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1311
1312 req_res_key_exit:
1313         kfree(res_key);
1314         return rc;
1315 }
1316
1317 static int
1318 smb2_ioctl_query_info(const unsigned int xid,
1319                       struct cifs_tcon *tcon,
1320                       __le16 *path, int is_dir,
1321                       unsigned long p)
1322 {
1323         struct cifs_ses *ses = tcon->ses;
1324         char __user *arg = (char __user *)p;
1325         struct smb_query_info qi;
1326         struct smb_query_info __user *pqi;
1327         int rc = 0;
1328         int flags = 0;
1329         struct smb2_query_info_rsp *qi_rsp = NULL;
1330         struct smb2_ioctl_rsp *io_rsp = NULL;
1331         void *buffer = NULL;
1332         struct smb_rqst rqst[3];
1333         int resp_buftype[3];
1334         struct kvec rsp_iov[3];
1335         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1336         struct cifs_open_parms oparms;
1337         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1338         struct cifs_fid fid;
1339         struct kvec qi_iov[1];
1340         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1341         struct kvec close_iov[1];
1342
1343         memset(rqst, 0, sizeof(rqst));
1344         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1345         memset(rsp_iov, 0, sizeof(rsp_iov));
1346
1347         if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1348                 return -EFAULT;
1349
1350         if (qi.output_buffer_length > 1024)
1351                 return -EINVAL;
1352
1353         if (!ses || !(ses->server))
1354                 return -EIO;
1355
1356         if (smb3_encryption_required(tcon))
1357                 flags |= CIFS_TRANSFORM_REQ;
1358
1359         buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1360         if (buffer == NULL)
1361                 return -ENOMEM;
1362
1363         if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1364                            qi.output_buffer_length)) {
1365                 rc = -EFAULT;
1366                 goto iqinf_exit;
1367         }
1368
1369         /* Open */
1370         memset(&open_iov, 0, sizeof(open_iov));
1371         rqst[0].rq_iov = open_iov;
1372         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1373
1374         memset(&oparms, 0, sizeof(oparms));
1375         oparms.tcon = tcon;
1376         oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1377         oparms.disposition = FILE_OPEN;
1378         if (is_dir)
1379                 oparms.create_options = CREATE_NOT_FILE;
1380         else
1381                 oparms.create_options = CREATE_NOT_DIR;
1382         oparms.fid = &fid;
1383         oparms.reconnect = false;
1384
1385         /*
1386          * FSCTL codes encode the special access they need in the fsctl code.
1387          */
1388         if (qi.flags & PASSTHRU_FSCTL) {
1389                 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1390                 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1391                         oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1392                         break;
1393                 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1394                         oparms.desired_access = GENERIC_ALL;
1395                         break;
1396                 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1397                         oparms.desired_access = GENERIC_READ;
1398                         break;
1399                 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1400                         oparms.desired_access = GENERIC_WRITE;
1401                         break;
1402                 }
1403         }
1404
1405         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1406         if (rc)
1407                 goto iqinf_exit;
1408         smb2_set_next_command(tcon, &rqst[0]);
1409
1410         /* Query */
1411         if (qi.flags & PASSTHRU_FSCTL) {
1412                 /* Can eventually relax perm check since server enforces too */
1413                 if (!capable(CAP_SYS_ADMIN))
1414                         rc = -EPERM;
1415                 else  {
1416                         memset(&io_iov, 0, sizeof(io_iov));
1417                         rqst[1].rq_iov = io_iov;
1418                         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1419
1420                         rc = SMB2_ioctl_init(tcon, &rqst[1],
1421                                              COMPOUND_FID, COMPOUND_FID,
1422                                              qi.info_type, true, buffer,
1423                                              qi.output_buffer_length,
1424                                              CIFSMaxBufSize);
1425                 }
1426         } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1427                 memset(&qi_iov, 0, sizeof(qi_iov));
1428                 rqst[1].rq_iov = qi_iov;
1429                 rqst[1].rq_nvec = 1;
1430
1431                 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1432                                   COMPOUND_FID, qi.file_info_class,
1433                                   qi.info_type, qi.additional_information,
1434                                   qi.input_buffer_length,
1435                                   qi.output_buffer_length, buffer);
1436         } else { /* unknown flags */
1437                 cifs_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1438                 rc = -EINVAL;
1439         }
1440
1441         if (rc)
1442                 goto iqinf_exit;
1443         smb2_set_next_command(tcon, &rqst[1]);
1444         smb2_set_related(&rqst[1]);
1445
1446         /* Close */
1447         memset(&close_iov, 0, sizeof(close_iov));
1448         rqst[2].rq_iov = close_iov;
1449         rqst[2].rq_nvec = 1;
1450
1451         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1452         if (rc)
1453                 goto iqinf_exit;
1454         smb2_set_related(&rqst[2]);
1455
1456         rc = compound_send_recv(xid, ses, flags, 3, rqst,
1457                                 resp_buftype, rsp_iov);
1458         if (rc)
1459                 goto iqinf_exit;
1460         if (qi.flags & PASSTHRU_FSCTL) {
1461                 pqi = (struct smb_query_info __user *)arg;
1462                 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1463                 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1464                         qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1465                 if (qi.input_buffer_length > 0 &&
1466                     le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length > rsp_iov[1].iov_len) {
1467                         rc = -EFAULT;
1468                         goto iqinf_exit;
1469                 }
1470                 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1471                                  sizeof(qi.input_buffer_length))) {
1472                         rc = -EFAULT;
1473                         goto iqinf_exit;
1474                 }
1475                 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1476                                  (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1477                                  qi.input_buffer_length)) {
1478                         rc = -EFAULT;
1479                         goto iqinf_exit;
1480                 }
1481         } else {
1482                 pqi = (struct smb_query_info __user *)arg;
1483                 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1484                 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1485                         qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1486                 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1487                                  sizeof(qi.input_buffer_length))) {
1488                         rc = -EFAULT;
1489                         goto iqinf_exit;
1490                 }
1491                 if (copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)) {
1492                         rc = -EFAULT;
1493                         goto iqinf_exit;
1494                 }
1495         }
1496
1497  iqinf_exit:
1498         kfree(buffer);
1499         SMB2_open_free(&rqst[0]);
1500         if (qi.flags & PASSTHRU_FSCTL)
1501                 SMB2_ioctl_free(&rqst[1]);
1502         else
1503                 SMB2_query_info_free(&rqst[1]);
1504
1505         SMB2_close_free(&rqst[2]);
1506         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1507         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1508         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1509         return rc;
1510 }
1511
1512 static ssize_t
1513 smb2_copychunk_range(const unsigned int xid,
1514                         struct cifsFileInfo *srcfile,
1515                         struct cifsFileInfo *trgtfile, u64 src_off,
1516                         u64 len, u64 dest_off)
1517 {
1518         int rc;
1519         unsigned int ret_data_len;
1520         struct copychunk_ioctl *pcchunk;
1521         struct copychunk_ioctl_rsp *retbuf = NULL;
1522         struct cifs_tcon *tcon;
1523         int chunks_copied = 0;
1524         bool chunk_sizes_updated = false;
1525         ssize_t bytes_written, total_bytes_written = 0;
1526
1527         pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1528
1529         if (pcchunk == NULL)
1530                 return -ENOMEM;
1531
1532         cifs_dbg(FYI, "in smb2_copychunk_range - about to call request res key\n");
1533         /* Request a key from the server to identify the source of the copy */
1534         rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1535                                 srcfile->fid.persistent_fid,
1536                                 srcfile->fid.volatile_fid, pcchunk);
1537
1538         /* Note: request_res_key sets res_key null only if rc !=0 */
1539         if (rc)
1540                 goto cchunk_out;
1541
1542         /* For now array only one chunk long, will make more flexible later */
1543         pcchunk->ChunkCount = cpu_to_le32(1);
1544         pcchunk->Reserved = 0;
1545         pcchunk->Reserved2 = 0;
1546
1547         tcon = tlink_tcon(trgtfile->tlink);
1548
1549         while (len > 0) {
1550                 pcchunk->SourceOffset = cpu_to_le64(src_off);
1551                 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1552                 pcchunk->Length =
1553                         cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1554
1555                 /* Request server copy to target from src identified by key */
1556                 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1557                         trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1558                         true /* is_fsctl */, (char *)pcchunk,
1559                         sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1560                         (char **)&retbuf, &ret_data_len);
1561                 if (rc == 0) {
1562                         if (ret_data_len !=
1563                                         sizeof(struct copychunk_ioctl_rsp)) {
1564                                 cifs_dbg(VFS, "invalid cchunk response size\n");
1565                                 rc = -EIO;
1566                                 goto cchunk_out;
1567                         }
1568                         if (retbuf->TotalBytesWritten == 0) {
1569                                 cifs_dbg(FYI, "no bytes copied\n");
1570                                 rc = -EIO;
1571                                 goto cchunk_out;
1572                         }
1573                         /*
1574                          * Check if server claimed to write more than we asked
1575                          */
1576                         if (le32_to_cpu(retbuf->TotalBytesWritten) >
1577                             le32_to_cpu(pcchunk->Length)) {
1578                                 cifs_dbg(VFS, "invalid copy chunk response\n");
1579                                 rc = -EIO;
1580                                 goto cchunk_out;
1581                         }
1582                         if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1583                                 cifs_dbg(VFS, "invalid num chunks written\n");
1584                                 rc = -EIO;
1585                                 goto cchunk_out;
1586                         }
1587                         chunks_copied++;
1588
1589                         bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1590                         src_off += bytes_written;
1591                         dest_off += bytes_written;
1592                         len -= bytes_written;
1593                         total_bytes_written += bytes_written;
1594
1595                         cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1596                                 le32_to_cpu(retbuf->ChunksWritten),
1597                                 le32_to_cpu(retbuf->ChunkBytesWritten),
1598                                 bytes_written);
1599                 } else if (rc == -EINVAL) {
1600                         if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1601                                 goto cchunk_out;
1602
1603                         cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1604                                 le32_to_cpu(retbuf->ChunksWritten),
1605                                 le32_to_cpu(retbuf->ChunkBytesWritten),
1606                                 le32_to_cpu(retbuf->TotalBytesWritten));
1607
1608                         /*
1609                          * Check if this is the first request using these sizes,
1610                          * (ie check if copy succeed once with original sizes
1611                          * and check if the server gave us different sizes after
1612                          * we already updated max sizes on previous request).
1613                          * if not then why is the server returning an error now
1614                          */
1615                         if ((chunks_copied != 0) || chunk_sizes_updated)
1616                                 goto cchunk_out;
1617
1618                         /* Check that server is not asking us to grow size */
1619                         if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1620                                         tcon->max_bytes_chunk)
1621                                 tcon->max_bytes_chunk =
1622                                         le32_to_cpu(retbuf->ChunkBytesWritten);
1623                         else
1624                                 goto cchunk_out; /* server gave us bogus size */
1625
1626                         /* No need to change MaxChunks since already set to 1 */
1627                         chunk_sizes_updated = true;
1628                 } else
1629                         goto cchunk_out;
1630         }
1631
1632 cchunk_out:
1633         kfree(pcchunk);
1634         kfree(retbuf);
1635         if (rc)
1636                 return rc;
1637         else
1638                 return total_bytes_written;
1639 }
1640
1641 static int
1642 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1643                 struct cifs_fid *fid)
1644 {
1645         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1646 }
1647
1648 static unsigned int
1649 smb2_read_data_offset(char *buf)
1650 {
1651         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1652         return rsp->DataOffset;
1653 }
1654
1655 static unsigned int
1656 smb2_read_data_length(char *buf, bool in_remaining)
1657 {
1658         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1659
1660         if (in_remaining)
1661                 return le32_to_cpu(rsp->DataRemaining);
1662
1663         return le32_to_cpu(rsp->DataLength);
1664 }
1665
1666
1667 static int
1668 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1669                struct cifs_io_parms *parms, unsigned int *bytes_read,
1670                char **buf, int *buf_type)
1671 {
1672         parms->persistent_fid = pfid->persistent_fid;
1673         parms->volatile_fid = pfid->volatile_fid;
1674         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1675 }
1676
1677 static int
1678 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1679                 struct cifs_io_parms *parms, unsigned int *written,
1680                 struct kvec *iov, unsigned long nr_segs)
1681 {
1682
1683         parms->persistent_fid = pfid->persistent_fid;
1684         parms->volatile_fid = pfid->volatile_fid;
1685         return SMB2_write(xid, parms, written, iov, nr_segs);
1686 }
1687
1688 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1689 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1690                 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1691 {
1692         struct cifsInodeInfo *cifsi;
1693         int rc;
1694
1695         cifsi = CIFS_I(inode);
1696
1697         /* if file already sparse don't bother setting sparse again */
1698         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1699                 return true; /* already sparse */
1700
1701         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1702                 return true; /* already not sparse */
1703
1704         /*
1705          * Can't check for sparse support on share the usual way via the
1706          * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1707          * since Samba server doesn't set the flag on the share, yet
1708          * supports the set sparse FSCTL and returns sparse correctly
1709          * in the file attributes. If we fail setting sparse though we
1710          * mark that server does not support sparse files for this share
1711          * to avoid repeatedly sending the unsupported fsctl to server
1712          * if the file is repeatedly extended.
1713          */
1714         if (tcon->broken_sparse_sup)
1715                 return false;
1716
1717         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1718                         cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1719                         true /* is_fctl */,
1720                         &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1721         if (rc) {
1722                 tcon->broken_sparse_sup = true;
1723                 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1724                 return false;
1725         }
1726
1727         if (setsparse)
1728                 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1729         else
1730                 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1731
1732         return true;
1733 }
1734
1735 static int
1736 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1737                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1738 {
1739         __le64 eof = cpu_to_le64(size);
1740         struct inode *inode;
1741
1742         /*
1743          * If extending file more than one page make sparse. Many Linux fs
1744          * make files sparse by default when extending via ftruncate
1745          */
1746         inode = d_inode(cfile->dentry);
1747
1748         if (!set_alloc && (size > inode->i_size + 8192)) {
1749                 __u8 set_sparse = 1;
1750
1751                 /* whether set sparse succeeds or not, extend the file */
1752                 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1753         }
1754
1755         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1756                             cfile->fid.volatile_fid, cfile->pid, &eof);
1757 }
1758
1759 static int
1760 smb2_duplicate_extents(const unsigned int xid,
1761                         struct cifsFileInfo *srcfile,
1762                         struct cifsFileInfo *trgtfile, u64 src_off,
1763                         u64 len, u64 dest_off)
1764 {
1765         int rc;
1766         unsigned int ret_data_len;
1767         struct duplicate_extents_to_file dup_ext_buf;
1768         struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1769
1770         /* server fileays advertise duplicate extent support with this flag */
1771         if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1772              FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1773                 return -EOPNOTSUPP;
1774
1775         dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1776         dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1777         dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1778         dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1779         dup_ext_buf.ByteCount = cpu_to_le64(len);
1780         cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
1781                 src_off, dest_off, len);
1782
1783         rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1784         if (rc)
1785                 goto duplicate_extents_out;
1786
1787         rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1788                         trgtfile->fid.volatile_fid,
1789                         FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1790                         true /* is_fsctl */,
1791                         (char *)&dup_ext_buf,
1792                         sizeof(struct duplicate_extents_to_file),
1793                         CIFSMaxBufSize, NULL,
1794                         &ret_data_len);
1795
1796         if (ret_data_len > 0)
1797                 cifs_dbg(FYI, "non-zero response length in duplicate extents");
1798
1799 duplicate_extents_out:
1800         return rc;
1801 }
1802
1803 static int
1804 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1805                    struct cifsFileInfo *cfile)
1806 {
1807         return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1808                             cfile->fid.volatile_fid);
1809 }
1810
1811 static int
1812 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1813                    struct cifsFileInfo *cfile)
1814 {
1815         struct fsctl_set_integrity_information_req integr_info;
1816         unsigned int ret_data_len;
1817
1818         integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1819         integr_info.Flags = 0;
1820         integr_info.Reserved = 0;
1821
1822         return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1823                         cfile->fid.volatile_fid,
1824                         FSCTL_SET_INTEGRITY_INFORMATION,
1825                         true /* is_fsctl */,
1826                         (char *)&integr_info,
1827                         sizeof(struct fsctl_set_integrity_information_req),
1828                         CIFSMaxBufSize, NULL,
1829                         &ret_data_len);
1830
1831 }
1832
1833 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1834 #define GMT_TOKEN_SIZE 50
1835
1836 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1837
1838 /*
1839  * Input buffer contains (empty) struct smb_snapshot array with size filled in
1840  * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1841  */
1842 static int
1843 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1844                    struct cifsFileInfo *cfile, void __user *ioc_buf)
1845 {
1846         char *retbuf = NULL;
1847         unsigned int ret_data_len = 0;
1848         int rc;
1849         u32 max_response_size;
1850         struct smb_snapshot_array snapshot_in;
1851
1852         /*
1853          * On the first query to enumerate the list of snapshots available
1854          * for this volume the buffer begins with 0 (number of snapshots
1855          * which can be returned is zero since at that point we do not know
1856          * how big the buffer needs to be). On the second query,
1857          * it (ret_data_len) is set to number of snapshots so we can
1858          * know to set the maximum response size larger (see below).
1859          */
1860         if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1861                 return -EFAULT;
1862
1863         /*
1864          * Note that for snapshot queries that servers like Azure expect that
1865          * the first query be minimal size (and just used to get the number/size
1866          * of previous versions) so response size must be specified as EXACTLY
1867          * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1868          * of eight bytes.
1869          */
1870         if (ret_data_len == 0)
1871                 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1872         else
1873                 max_response_size = CIFSMaxBufSize;
1874
1875         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1876                         cfile->fid.volatile_fid,
1877                         FSCTL_SRV_ENUMERATE_SNAPSHOTS,
1878                         true /* is_fsctl */,
1879                         NULL, 0 /* no input data */, max_response_size,
1880                         (char **)&retbuf,
1881                         &ret_data_len);
1882         cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1883                         rc, ret_data_len);
1884         if (rc)
1885                 return rc;
1886
1887         if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1888                 /* Fixup buffer */
1889                 if (copy_from_user(&snapshot_in, ioc_buf,
1890                     sizeof(struct smb_snapshot_array))) {
1891                         rc = -EFAULT;
1892                         kfree(retbuf);
1893                         return rc;
1894                 }
1895
1896                 /*
1897                  * Check for min size, ie not large enough to fit even one GMT
1898                  * token (snapshot).  On the first ioctl some users may pass in
1899                  * smaller size (or zero) to simply get the size of the array
1900                  * so the user space caller can allocate sufficient memory
1901                  * and retry the ioctl again with larger array size sufficient
1902                  * to hold all of the snapshot GMT tokens on the second try.
1903                  */
1904                 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1905                         ret_data_len = sizeof(struct smb_snapshot_array);
1906
1907                 /*
1908                  * We return struct SRV_SNAPSHOT_ARRAY, followed by
1909                  * the snapshot array (of 50 byte GMT tokens) each
1910                  * representing an available previous version of the data
1911                  */
1912                 if (ret_data_len > (snapshot_in.snapshot_array_size +
1913                                         sizeof(struct smb_snapshot_array)))
1914                         ret_data_len = snapshot_in.snapshot_array_size +
1915                                         sizeof(struct smb_snapshot_array);
1916
1917                 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1918                         rc = -EFAULT;
1919         }
1920
1921         kfree(retbuf);
1922         return rc;
1923 }
1924
1925 static int
1926 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1927                      const char *path, struct cifs_sb_info *cifs_sb,
1928                      struct cifs_fid *fid, __u16 search_flags,
1929                      struct cifs_search_info *srch_inf)
1930 {
1931         __le16 *utf16_path;
1932         int rc;
1933         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1934         struct cifs_open_parms oparms;
1935
1936         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1937         if (!utf16_path)
1938                 return -ENOMEM;
1939
1940         oparms.tcon = tcon;
1941         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1942         oparms.disposition = FILE_OPEN;
1943         if (backup_cred(cifs_sb))
1944                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1945         else
1946                 oparms.create_options = 0;
1947         oparms.fid = fid;
1948         oparms.reconnect = false;
1949
1950         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
1951         kfree(utf16_path);
1952         if (rc) {
1953                 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
1954                 return rc;
1955         }
1956
1957         srch_inf->entries_in_buffer = 0;
1958         srch_inf->index_of_last_entry = 2;
1959
1960         rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1961                                   fid->volatile_fid, 0, srch_inf);
1962         if (rc) {
1963                 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
1964                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1965         }
1966         return rc;
1967 }
1968
1969 static int
1970 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1971                     struct cifs_fid *fid, __u16 search_flags,
1972                     struct cifs_search_info *srch_inf)
1973 {
1974         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1975                                     fid->volatile_fid, 0, srch_inf);
1976 }
1977
1978 static int
1979 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1980                struct cifs_fid *fid)
1981 {
1982         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1983 }
1984
1985 /*
1986 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1987 * the number of credits and return true. Otherwise - return false.
1988 */
1989 static bool
1990 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
1991 {
1992         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
1993
1994         if (shdr->Status != STATUS_PENDING)
1995                 return false;
1996
1997         if (shdr->CreditRequest) {
1998                 spin_lock(&server->req_lock);
1999                 server->credits += le16_to_cpu(shdr->CreditRequest);
2000                 spin_unlock(&server->req_lock);
2001                 wake_up(&server->request_q);
2002         }
2003
2004         return true;
2005 }
2006
2007 static bool
2008 smb2_is_session_expired(char *buf)
2009 {
2010         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2011
2012         if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2013             shdr->Status != STATUS_USER_SESSION_DELETED)
2014                 return false;
2015
2016         trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2017                                le16_to_cpu(shdr->Command),
2018                                le64_to_cpu(shdr->MessageId));
2019         cifs_dbg(FYI, "Session expired or deleted\n");
2020
2021         return true;
2022 }
2023
2024 static int
2025 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2026                      struct cifsInodeInfo *cinode)
2027 {
2028         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2029                 return SMB2_lease_break(0, tcon, cinode->lease_key,
2030                                         smb2_get_lease_state(cinode));
2031
2032         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2033                                  fid->volatile_fid,
2034                                  CIFS_CACHE_READ(cinode) ? 1 : 0);
2035 }
2036
2037 void
2038 smb2_set_related(struct smb_rqst *rqst)
2039 {
2040         struct smb2_sync_hdr *shdr;
2041
2042         shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2043         shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2044 }
2045
2046 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2047
2048 void
2049 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2050 {
2051         struct smb2_sync_hdr *shdr;
2052         struct cifs_ses *ses = tcon->ses;
2053         struct TCP_Server_Info *server = ses->server;
2054         unsigned long len = smb_rqst_len(server, rqst);
2055         int i, num_padding;
2056
2057         /* SMB headers in a compound are 8 byte aligned. */
2058
2059         /* No padding needed */
2060         if (!(len & 7))
2061                 goto finished;
2062
2063         num_padding = 8 - (len & 7);
2064         if (!smb3_encryption_required(tcon)) {
2065                 /*
2066                  * If we do not have encryption then we can just add an extra
2067                  * iov for the padding.
2068                  */
2069                 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2070                 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2071                 rqst->rq_nvec++;
2072                 len += num_padding;
2073         } else {
2074                 /*
2075                  * We can not add a small padding iov for the encryption case
2076                  * because the encryption framework can not handle the padding
2077                  * iovs.
2078                  * We have to flatten this into a single buffer and add
2079                  * the padding to it.
2080                  */
2081                 for (i = 1; i < rqst->rq_nvec; i++) {
2082                         memcpy(rqst->rq_iov[0].iov_base +
2083                                rqst->rq_iov[0].iov_len,
2084                                rqst->rq_iov[i].iov_base,
2085                                rqst->rq_iov[i].iov_len);
2086                         rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2087                 }
2088                 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2089                        0, num_padding);
2090                 rqst->rq_iov[0].iov_len += num_padding;
2091                 len += num_padding;
2092                 rqst->rq_nvec = 1;
2093         }
2094
2095  finished:
2096         shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2097         shdr->NextCommand = cpu_to_le32(len);
2098 }
2099
2100 /*
2101  * Passes the query info response back to the caller on success.
2102  * Caller need to free this with free_rsp_buf().
2103  */
2104 int
2105 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2106                          __le16 *utf16_path, u32 desired_access,
2107                          u32 class, u32 type, u32 output_len,
2108                          struct kvec *rsp, int *buftype,
2109                          struct cifs_sb_info *cifs_sb)
2110 {
2111         struct cifs_ses *ses = tcon->ses;
2112         int flags = 0;
2113         struct smb_rqst rqst[3];
2114         int resp_buftype[3];
2115         struct kvec rsp_iov[3];
2116         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2117         struct kvec qi_iov[1];
2118         struct kvec close_iov[1];
2119         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2120         struct cifs_open_parms oparms;
2121         struct cifs_fid fid;
2122         int rc;
2123
2124         if (smb3_encryption_required(tcon))
2125                 flags |= CIFS_TRANSFORM_REQ;
2126
2127         memset(rqst, 0, sizeof(rqst));
2128         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2129         memset(rsp_iov, 0, sizeof(rsp_iov));
2130
2131         memset(&open_iov, 0, sizeof(open_iov));
2132         rqst[0].rq_iov = open_iov;
2133         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2134
2135         oparms.tcon = tcon;
2136         oparms.desired_access = desired_access;
2137         oparms.disposition = FILE_OPEN;
2138         if (cifs_sb && backup_cred(cifs_sb))
2139                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2140         else
2141                 oparms.create_options = 0;
2142         oparms.fid = &fid;
2143         oparms.reconnect = false;
2144
2145         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2146         if (rc)
2147                 goto qic_exit;
2148         smb2_set_next_command(tcon, &rqst[0]);
2149
2150         memset(&qi_iov, 0, sizeof(qi_iov));
2151         rqst[1].rq_iov = qi_iov;
2152         rqst[1].rq_nvec = 1;
2153
2154         rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2155                                   class, type, 0,
2156                                   output_len, 0,
2157                                   NULL);
2158         if (rc)
2159                 goto qic_exit;
2160         smb2_set_next_command(tcon, &rqst[1]);
2161         smb2_set_related(&rqst[1]);
2162
2163         memset(&close_iov, 0, sizeof(close_iov));
2164         rqst[2].rq_iov = close_iov;
2165         rqst[2].rq_nvec = 1;
2166
2167         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2168         if (rc)
2169                 goto qic_exit;
2170         smb2_set_related(&rqst[2]);
2171
2172         rc = compound_send_recv(xid, ses, flags, 3, rqst,
2173                                 resp_buftype, rsp_iov);
2174         if (rc) {
2175                 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2176                 goto qic_exit;
2177         }
2178         *rsp = rsp_iov[1];
2179         *buftype = resp_buftype[1];
2180
2181  qic_exit:
2182         SMB2_open_free(&rqst[0]);
2183         SMB2_query_info_free(&rqst[1]);
2184         SMB2_close_free(&rqst[2]);
2185         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2186         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2187         return rc;
2188 }
2189
2190 static int
2191 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2192              struct kstatfs *buf)
2193 {
2194         struct smb2_query_info_rsp *rsp;
2195         struct smb2_fs_full_size_info *info = NULL;
2196         __le16 utf16_path = 0; /* Null - open root of share */
2197         struct kvec rsp_iov = {NULL, 0};
2198         int buftype = CIFS_NO_BUFFER;
2199         int rc;
2200
2201
2202         rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2203                                       FILE_READ_ATTRIBUTES,
2204                                       FS_FULL_SIZE_INFORMATION,
2205                                       SMB2_O_INFO_FILESYSTEM,
2206                                       sizeof(struct smb2_fs_full_size_info),
2207                                       &rsp_iov, &buftype, NULL);
2208         if (rc)
2209                 goto qfs_exit;
2210
2211         rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2212         buf->f_type = SMB2_MAGIC_NUMBER;
2213         info = (struct smb2_fs_full_size_info *)(
2214                 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2215         rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2216                                le32_to_cpu(rsp->OutputBufferLength),
2217                                &rsp_iov,
2218                                sizeof(struct smb2_fs_full_size_info));
2219         if (!rc)
2220                 smb2_copy_fs_info_to_kstatfs(info, buf);
2221
2222 qfs_exit:
2223         free_rsp_buf(buftype, rsp_iov.iov_base);
2224         return rc;
2225 }
2226
2227 static int
2228 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2229              struct kstatfs *buf)
2230 {
2231         int rc;
2232         __le16 srch_path = 0; /* Null - open root of share */
2233         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2234         struct cifs_open_parms oparms;
2235         struct cifs_fid fid;
2236
2237         if (!tcon->posix_extensions)
2238                 return smb2_queryfs(xid, tcon, buf);
2239
2240         oparms.tcon = tcon;
2241         oparms.desired_access = FILE_READ_ATTRIBUTES;
2242         oparms.disposition = FILE_OPEN;
2243         oparms.create_options = 0;
2244         oparms.fid = &fid;
2245         oparms.reconnect = false;
2246
2247         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2248         if (rc)
2249                 return rc;
2250
2251         rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2252                                    fid.volatile_fid, buf);
2253         buf->f_type = SMB2_MAGIC_NUMBER;
2254         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2255         return rc;
2256 }
2257
2258 static bool
2259 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2260 {
2261         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2262                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2263 }
2264
2265 static int
2266 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2267                __u64 length, __u32 type, int lock, int unlock, bool wait)
2268 {
2269         if (unlock && !lock)
2270                 type = SMB2_LOCKFLAG_UNLOCK;
2271         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2272                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2273                          current->tgid, length, offset, type, wait);
2274 }
2275
2276 static void
2277 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2278 {
2279         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2280 }
2281
2282 static void
2283 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2284 {
2285         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2286 }
2287
2288 static void
2289 smb2_new_lease_key(struct cifs_fid *fid)
2290 {
2291         generate_random_uuid(fid->lease_key);
2292 }
2293
2294 static int
2295 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2296                    const char *search_name,
2297                    struct dfs_info3_param **target_nodes,
2298                    unsigned int *num_of_nodes,
2299                    const struct nls_table *nls_codepage, int remap)
2300 {
2301         int rc;
2302         __le16 *utf16_path = NULL;
2303         int utf16_path_len = 0;
2304         struct cifs_tcon *tcon;
2305         struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2306         struct get_dfs_referral_rsp *dfs_rsp = NULL;
2307         u32 dfs_req_size = 0, dfs_rsp_size = 0;
2308
2309         cifs_dbg(FYI, "smb2_get_dfs_refer path <%s>\n", search_name);
2310
2311         /*
2312          * Try to use the IPC tcon, otherwise just use any
2313          */
2314         tcon = ses->tcon_ipc;
2315         if (tcon == NULL) {
2316                 spin_lock(&cifs_tcp_ses_lock);
2317                 tcon = list_first_entry_or_null(&ses->tcon_list,
2318                                                 struct cifs_tcon,
2319                                                 tcon_list);
2320                 if (tcon)
2321                         tcon->tc_count++;
2322                 spin_unlock(&cifs_tcp_ses_lock);
2323         }
2324
2325         if (tcon == NULL) {
2326                 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2327                          ses);
2328                 rc = -ENOTCONN;
2329                 goto out;
2330         }
2331
2332         utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2333                                            &utf16_path_len,
2334                                            nls_codepage, remap);
2335         if (!utf16_path) {
2336                 rc = -ENOMEM;
2337                 goto out;
2338         }
2339
2340         dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2341         dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2342         if (!dfs_req) {
2343                 rc = -ENOMEM;
2344                 goto out;
2345         }
2346
2347         /* Highest DFS referral version understood */
2348         dfs_req->MaxReferralLevel = DFS_VERSION;
2349
2350         /* Path to resolve in an UTF-16 null-terminated string */
2351         memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2352
2353         do {
2354                 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2355                                 FSCTL_DFS_GET_REFERRALS,
2356                                 true /* is_fsctl */,
2357                                 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2358                                 (char **)&dfs_rsp, &dfs_rsp_size);
2359         } while (rc == -EAGAIN);
2360
2361         if (rc) {
2362                 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2363                         cifs_dbg(VFS, "ioctl error in smb2_get_dfs_refer rc=%d\n", rc);
2364                 goto out;
2365         }
2366
2367         rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2368                                  num_of_nodes, target_nodes,
2369                                  nls_codepage, remap, search_name,
2370                                  true /* is_unicode */);
2371         if (rc) {
2372                 cifs_dbg(VFS, "parse error in smb2_get_dfs_refer rc=%d\n", rc);
2373                 goto out;
2374         }
2375
2376  out:
2377         if (tcon && !tcon->ipc) {
2378                 /* ipc tcons are not refcounted */
2379                 spin_lock(&cifs_tcp_ses_lock);
2380                 tcon->tc_count--;
2381                 spin_unlock(&cifs_tcp_ses_lock);
2382         }
2383         kfree(utf16_path);
2384         kfree(dfs_req);
2385         kfree(dfs_rsp);
2386         return rc;
2387 }
2388 #define SMB2_SYMLINK_STRUCT_SIZE \
2389         (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2390
2391 static int
2392 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2393                    struct cifs_sb_info *cifs_sb, const char *full_path,
2394                    char **target_path, bool is_reparse_point)
2395 {
2396         int rc;
2397         __le16 *utf16_path = NULL;
2398         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2399         struct cifs_open_parms oparms;
2400         struct cifs_fid fid;
2401         struct kvec err_iov = {NULL, 0};
2402         struct smb2_err_rsp *err_buf = NULL;
2403         struct smb2_symlink_err_rsp *symlink;
2404         unsigned int sub_len;
2405         unsigned int sub_offset;
2406         unsigned int print_len;
2407         unsigned int print_offset;
2408         int flags = 0;
2409         struct smb_rqst rqst[3];
2410         int resp_buftype[3];
2411         struct kvec rsp_iov[3];
2412         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2413         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2414         struct kvec close_iov[1];
2415         struct smb2_create_rsp *create_rsp;
2416         struct smb2_ioctl_rsp *ioctl_rsp;
2417         char *ioctl_buf;
2418         u32 plen;
2419
2420         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2421
2422         if (smb3_encryption_required(tcon))
2423                 flags |= CIFS_TRANSFORM_REQ;
2424
2425         memset(rqst, 0, sizeof(rqst));
2426         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2427         memset(rsp_iov, 0, sizeof(rsp_iov));
2428
2429         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2430         if (!utf16_path)
2431                 return -ENOMEM;
2432
2433         /* Open */
2434         memset(&open_iov, 0, sizeof(open_iov));
2435         rqst[0].rq_iov = open_iov;
2436         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2437
2438         memset(&oparms, 0, sizeof(oparms));
2439         oparms.tcon = tcon;
2440         oparms.desired_access = FILE_READ_ATTRIBUTES;
2441         oparms.disposition = FILE_OPEN;
2442
2443         if (backup_cred(cifs_sb))
2444                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2445         else
2446                 oparms.create_options = 0;
2447         if (is_reparse_point)
2448                 oparms.create_options = OPEN_REPARSE_POINT;
2449
2450         oparms.fid = &fid;
2451         oparms.reconnect = false;
2452
2453         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2454         if (rc)
2455                 goto querty_exit;
2456         smb2_set_next_command(tcon, &rqst[0]);
2457
2458
2459         /* IOCTL */
2460         memset(&io_iov, 0, sizeof(io_iov));
2461         rqst[1].rq_iov = io_iov;
2462         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2463
2464         rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2465                              fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2466                              true /* is_fctl */, NULL, 0, CIFSMaxBufSize);
2467         if (rc)
2468                 goto querty_exit;
2469
2470         smb2_set_next_command(tcon, &rqst[1]);
2471         smb2_set_related(&rqst[1]);
2472
2473
2474         /* Close */
2475         memset(&close_iov, 0, sizeof(close_iov));
2476         rqst[2].rq_iov = close_iov;
2477         rqst[2].rq_nvec = 1;
2478
2479         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2480         if (rc)
2481                 goto querty_exit;
2482
2483         smb2_set_related(&rqst[2]);
2484
2485         rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2486                                 resp_buftype, rsp_iov);
2487
2488         create_rsp = rsp_iov[0].iov_base;
2489         if (create_rsp && create_rsp->sync_hdr.Status)
2490                 err_iov = rsp_iov[0];
2491         ioctl_rsp = rsp_iov[1].iov_base;
2492
2493         /*
2494          * Open was successful and we got an ioctl response.
2495          */
2496         if ((rc == 0) && (is_reparse_point)) {
2497                 /* See MS-FSCC 2.3.23 */
2498
2499                 ioctl_buf = (char *)ioctl_rsp + le32_to_cpu(ioctl_rsp->OutputOffset);
2500                 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2501
2502                 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2503                     rsp_iov[1].iov_len) {
2504                         cifs_dbg(VFS, "srv returned invalid ioctl length: %d\n", plen);
2505                         rc = -EIO;
2506                         goto querty_exit;
2507                 }
2508
2509                 /* Do stuff with ioctl_buf/plen */
2510                 goto querty_exit;
2511         }
2512
2513         if (!rc || !err_iov.iov_base) {
2514                 rc = -ENOENT;
2515                 goto querty_exit;
2516         }
2517
2518         err_buf = err_iov.iov_base;
2519         if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2520             err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2521                 rc = -ENOENT;
2522                 goto querty_exit;
2523         }
2524
2525         /* open must fail on symlink - reset rc */
2526         rc = 0;
2527         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2528         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2529         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2530         print_len = le16_to_cpu(symlink->PrintNameLength);
2531         print_offset = le16_to_cpu(symlink->PrintNameOffset);
2532
2533         if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2534                 rc = -ENOENT;
2535                 goto querty_exit;
2536         }
2537
2538         if (err_iov.iov_len <
2539             SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2540                 rc = -ENOENT;
2541                 goto querty_exit;
2542         }
2543
2544         *target_path = cifs_strndup_from_utf16(
2545                                 (char *)symlink->PathBuffer + sub_offset,
2546                                 sub_len, true, cifs_sb->local_nls);
2547         if (!(*target_path)) {
2548                 rc = -ENOMEM;
2549                 goto querty_exit;
2550         }
2551         convert_delimiter(*target_path, '/');
2552         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2553
2554  querty_exit:
2555         cifs_dbg(FYI, "query symlink rc %d\n", rc);
2556         kfree(utf16_path);
2557         SMB2_open_free(&rqst[0]);
2558         SMB2_ioctl_free(&rqst[1]);
2559         SMB2_close_free(&rqst[2]);
2560         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2561         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2562         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2563         return rc;
2564 }
2565
2566 #ifdef CONFIG_CIFS_ACL
2567 static struct cifs_ntsd *
2568 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2569                 const struct cifs_fid *cifsfid, u32 *pacllen)
2570 {
2571         struct cifs_ntsd *pntsd = NULL;
2572         unsigned int xid;
2573         int rc = -EOPNOTSUPP;
2574         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2575
2576         if (IS_ERR(tlink))
2577                 return ERR_CAST(tlink);
2578
2579         xid = get_xid();
2580         cifs_dbg(FYI, "trying to get acl\n");
2581
2582         rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2583                             cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2584         free_xid(xid);
2585
2586         cifs_put_tlink(tlink);
2587
2588         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2589         if (rc)
2590                 return ERR_PTR(rc);
2591         return pntsd;
2592
2593 }
2594
2595 static struct cifs_ntsd *
2596 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2597                 const char *path, u32 *pacllen)
2598 {
2599         struct cifs_ntsd *pntsd = NULL;
2600         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2601         unsigned int xid;
2602         int rc;
2603         struct cifs_tcon *tcon;
2604         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2605         struct cifs_fid fid;
2606         struct cifs_open_parms oparms;
2607         __le16 *utf16_path;
2608
2609         cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2610         if (IS_ERR(tlink))
2611                 return ERR_CAST(tlink);
2612
2613         tcon = tlink_tcon(tlink);
2614         xid = get_xid();
2615
2616         if (backup_cred(cifs_sb))
2617                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2618         else
2619                 oparms.create_options = 0;
2620
2621         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2622         if (!utf16_path) {
2623                 rc = -ENOMEM;
2624                 free_xid(xid);
2625                 return ERR_PTR(rc);
2626         }
2627
2628         oparms.tcon = tcon;
2629         oparms.desired_access = READ_CONTROL;
2630         oparms.disposition = FILE_OPEN;
2631         oparms.fid = &fid;
2632         oparms.reconnect = false;
2633
2634         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2635         kfree(utf16_path);
2636         if (!rc) {
2637                 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2638                             fid.volatile_fid, (void **)&pntsd, pacllen);
2639                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2640         }
2641
2642         cifs_put_tlink(tlink);
2643         free_xid(xid);
2644
2645         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2646         if (rc)
2647                 return ERR_PTR(rc);
2648         return pntsd;
2649 }
2650
2651 #ifdef CONFIG_CIFS_ACL
2652 static int
2653 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2654                 struct inode *inode, const char *path, int aclflag)
2655 {
2656         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2657         unsigned int xid;
2658         int rc, access_flags = 0;
2659         struct cifs_tcon *tcon;
2660         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2661         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2662         struct cifs_fid fid;
2663         struct cifs_open_parms oparms;
2664         __le16 *utf16_path;
2665
2666         cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2667         if (IS_ERR(tlink))
2668                 return PTR_ERR(tlink);
2669
2670         tcon = tlink_tcon(tlink);
2671         xid = get_xid();
2672
2673         if (backup_cred(cifs_sb))
2674                 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2675         else
2676                 oparms.create_options = 0;
2677
2678         if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2679                 access_flags = WRITE_OWNER;
2680         else
2681                 access_flags = WRITE_DAC;
2682
2683         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2684         if (!utf16_path) {
2685                 rc = -ENOMEM;
2686                 free_xid(xid);
2687                 return rc;
2688         }
2689
2690         oparms.tcon = tcon;
2691         oparms.desired_access = access_flags;
2692         oparms.disposition = FILE_OPEN;
2693         oparms.path = path;
2694         oparms.fid = &fid;
2695         oparms.reconnect = false;
2696
2697         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2698         kfree(utf16_path);
2699         if (!rc) {
2700                 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2701                             fid.volatile_fid, pnntsd, acllen, aclflag);
2702                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2703         }
2704
2705         cifs_put_tlink(tlink);
2706         free_xid(xid);
2707         return rc;
2708 }
2709 #endif /* CIFS_ACL */
2710
2711 /* Retrieve an ACL from the server */
2712 static struct cifs_ntsd *
2713 get_smb2_acl(struct cifs_sb_info *cifs_sb,
2714                                       struct inode *inode, const char *path,
2715                                       u32 *pacllen)
2716 {
2717         struct cifs_ntsd *pntsd = NULL;
2718         struct cifsFileInfo *open_file = NULL;
2719
2720         if (inode)
2721                 open_file = find_readable_file(CIFS_I(inode), true);
2722         if (!open_file)
2723                 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2724
2725         pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2726         cifsFileInfo_put(open_file);
2727         return pntsd;
2728 }
2729 #endif
2730
2731 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2732                             loff_t offset, loff_t len, bool keep_size)
2733 {
2734         struct cifs_ses *ses = tcon->ses;
2735         struct inode *inode;
2736         struct cifsInodeInfo *cifsi;
2737         struct cifsFileInfo *cfile = file->private_data;
2738         struct file_zero_data_information fsctl_buf;
2739         long rc;
2740         unsigned int xid;
2741         __le64 eof;
2742
2743         xid = get_xid();
2744
2745         inode = d_inode(cfile->dentry);
2746         cifsi = CIFS_I(inode);
2747
2748         trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2749                               ses->Suid, offset, len);
2750
2751
2752         /* if file not oplocked can't be sure whether asking to extend size */
2753         if (!CIFS_CACHE_READ(cifsi))
2754                 if (keep_size == false) {
2755                         rc = -EOPNOTSUPP;
2756                         trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2757                                 tcon->tid, ses->Suid, offset, len, rc);
2758                         free_xid(xid);
2759                         return rc;
2760                 }
2761
2762         cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2763
2764         fsctl_buf.FileOffset = cpu_to_le64(offset);
2765         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2766
2767         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2768                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
2769                         (char *)&fsctl_buf,
2770                         sizeof(struct file_zero_data_information),
2771                         0, NULL, NULL);
2772         if (rc)
2773                 goto zero_range_exit;
2774
2775         /*
2776          * do we also need to change the size of the file?
2777          */
2778         if (keep_size == false && i_size_read(inode) < offset + len) {
2779                 eof = cpu_to_le64(offset + len);
2780                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2781                                   cfile->fid.volatile_fid, cfile->pid, &eof);
2782         }
2783
2784  zero_range_exit:
2785         free_xid(xid);
2786         if (rc)
2787                 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2788                               ses->Suid, offset, len, rc);
2789         else
2790                 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
2791                               ses->Suid, offset, len);
2792         return rc;
2793 }
2794
2795 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2796                             loff_t offset, loff_t len)
2797 {
2798         struct inode *inode;
2799         struct cifsInodeInfo *cifsi;
2800         struct cifsFileInfo *cfile = file->private_data;
2801         struct file_zero_data_information fsctl_buf;
2802         long rc;
2803         unsigned int xid;
2804         __u8 set_sparse = 1;
2805
2806         xid = get_xid();
2807
2808         inode = d_inode(cfile->dentry);
2809         cifsi = CIFS_I(inode);
2810
2811         /* Need to make file sparse, if not already, before freeing range. */
2812         /* Consider adding equivalent for compressed since it could also work */
2813         if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2814                 rc = -EOPNOTSUPP;
2815                 free_xid(xid);
2816                 return rc;
2817         }
2818
2819         cifs_dbg(FYI, "offset %lld len %lld", offset, len);
2820
2821         fsctl_buf.FileOffset = cpu_to_le64(offset);
2822         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2823
2824         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2825                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
2826                         true /* is_fctl */, (char *)&fsctl_buf,
2827                         sizeof(struct file_zero_data_information),
2828                         CIFSMaxBufSize, NULL, NULL);
2829         free_xid(xid);
2830         return rc;
2831 }
2832
2833 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2834                             loff_t off, loff_t len, bool keep_size)
2835 {
2836         struct inode *inode;
2837         struct cifsInodeInfo *cifsi;
2838         struct cifsFileInfo *cfile = file->private_data;
2839         long rc = -EOPNOTSUPP;
2840         unsigned int xid;
2841         __le64 eof;
2842
2843         xid = get_xid();
2844
2845         inode = d_inode(cfile->dentry);
2846         cifsi = CIFS_I(inode);
2847
2848         trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2849                                 tcon->ses->Suid, off, len);
2850         /* if file not oplocked can't be sure whether asking to extend size */
2851         if (!CIFS_CACHE_READ(cifsi))
2852                 if (keep_size == false) {
2853                         trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2854                                 tcon->tid, tcon->ses->Suid, off, len, rc);
2855                         free_xid(xid);
2856                         return rc;
2857                 }
2858
2859         /*
2860          * Files are non-sparse by default so falloc may be a no-op
2861          * Must check if file sparse. If not sparse, and not extending
2862          * then no need to do anything since file already allocated
2863          */
2864         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2865                 if (keep_size == true)
2866                         rc = 0;
2867                 /* check if extending file */
2868                 else if (i_size_read(inode) >= off + len)
2869                         /* not extending file and already not sparse */
2870                         rc = 0;
2871                 /* BB: in future add else clause to extend file */
2872                 else
2873                         rc = -EOPNOTSUPP;
2874                 if (rc)
2875                         trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2876                                 tcon->tid, tcon->ses->Suid, off, len, rc);
2877                 else
2878                         trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
2879                                 tcon->tid, tcon->ses->Suid, off, len);
2880                 free_xid(xid);
2881                 return rc;
2882         }
2883
2884         if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2885                 /*
2886                  * Check if falloc starts within first few pages of file
2887                  * and ends within a few pages of the end of file to
2888                  * ensure that most of file is being forced to be
2889                  * fallocated now. If so then setting whole file sparse
2890                  * ie potentially making a few extra pages at the beginning
2891                  * or end of the file non-sparse via set_sparse is harmless.
2892                  */
2893                 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2894                         rc = -EOPNOTSUPP;
2895                         trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2896                                 tcon->tid, tcon->ses->Suid, off, len, rc);
2897                         free_xid(xid);
2898                         return rc;
2899                 }
2900
2901                 smb2_set_sparse(xid, tcon, cfile, inode, false);
2902                 rc = 0;
2903         } else {
2904                 smb2_set_sparse(xid, tcon, cfile, inode, false);
2905                 rc = 0;
2906                 if (i_size_read(inode) < off + len) {
2907                         eof = cpu_to_le64(off + len);
2908                         rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2909                                           cfile->fid.volatile_fid, cfile->pid,
2910                                           &eof);
2911                 }
2912         }
2913
2914         if (rc)
2915                 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
2916                                 tcon->ses->Suid, off, len, rc);
2917         else
2918                 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
2919                                 tcon->ses->Suid, off, len);
2920
2921         free_xid(xid);
2922         return rc;
2923 }
2924
2925 static int smb3_fiemap(struct cifs_tcon *tcon,
2926                        struct cifsFileInfo *cfile,
2927                        struct fiemap_extent_info *fei, u64 start, u64 len)
2928 {
2929         unsigned int xid;
2930         struct file_allocated_range_buffer in_data, *out_data;
2931         u32 out_data_len;
2932         int i, num, rc, flags, last_blob;
2933         u64 next;
2934
2935         if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
2936                 return -EBADR;
2937
2938         xid = get_xid();
2939  again:
2940         in_data.file_offset = cpu_to_le64(start);
2941         in_data.length = cpu_to_le64(len);
2942
2943         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2944                         cfile->fid.volatile_fid,
2945                         FSCTL_QUERY_ALLOCATED_RANGES, true,
2946                         (char *)&in_data, sizeof(in_data),
2947                         1024 * sizeof(struct file_allocated_range_buffer),
2948                         (char **)&out_data, &out_data_len);
2949         if (rc == -E2BIG) {
2950                 last_blob = 0;
2951                 rc = 0;
2952         } else
2953                 last_blob = 1;
2954         if (rc)
2955                 goto out;
2956
2957         if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
2958                 rc = -EINVAL;
2959                 goto out;
2960         }
2961         if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
2962                 rc = -EINVAL;
2963                 goto out;
2964         }
2965
2966         num = out_data_len / sizeof(struct file_allocated_range_buffer);
2967         for (i = 0; i < num; i++) {
2968                 flags = 0;
2969                 if (i == num - 1 && last_blob)
2970                         flags |= FIEMAP_EXTENT_LAST;
2971
2972                 rc = fiemap_fill_next_extent(fei,
2973                                 le64_to_cpu(out_data[i].file_offset),
2974                                 le64_to_cpu(out_data[i].file_offset),
2975                                 le64_to_cpu(out_data[i].length),
2976                                 flags);
2977                 if (rc < 0)
2978                         goto out;
2979                 if (rc == 1) {
2980                         rc = 0;
2981                         goto out;
2982                 }
2983         }
2984
2985         if (!last_blob) {
2986                 next = le64_to_cpu(out_data[num - 1].file_offset) +
2987                   le64_to_cpu(out_data[num - 1].length);
2988                 len = len - (next - start);
2989                 start = next;
2990                 goto again;
2991         }
2992
2993  out:
2994         free_xid(xid);
2995         kfree(out_data);
2996         return rc;
2997 }
2998
2999 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3000                            loff_t off, loff_t len)
3001 {
3002         /* KEEP_SIZE already checked for by do_fallocate */
3003         if (mode & FALLOC_FL_PUNCH_HOLE)
3004                 return smb3_punch_hole(file, tcon, off, len);
3005         else if (mode & FALLOC_FL_ZERO_RANGE) {
3006                 if (mode & FALLOC_FL_KEEP_SIZE)
3007                         return smb3_zero_range(file, tcon, off, len, true);
3008                 return smb3_zero_range(file, tcon, off, len, false);
3009         } else if (mode == FALLOC_FL_KEEP_SIZE)
3010                 return smb3_simple_falloc(file, tcon, off, len, true);
3011         else if (mode == 0)
3012                 return smb3_simple_falloc(file, tcon, off, len, false);
3013
3014         return -EOPNOTSUPP;
3015 }
3016
3017 static void
3018 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3019                         struct cifsInodeInfo *cinode, bool set_level2)
3020 {
3021         if (set_level2)
3022                 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
3023                                                 0, NULL);
3024         else
3025                 server->ops->set_oplock_level(cinode, 0, 0, NULL);
3026 }
3027
3028 static void
3029 smb21_downgrade_oplock(struct TCP_Server_Info *server,
3030                        struct cifsInodeInfo *cinode, bool set_level2)
3031 {
3032         server->ops->set_oplock_level(cinode,
3033                                       set_level2 ? SMB2_LEASE_READ_CACHING_HE :
3034                                       0, 0, NULL);
3035 }
3036
3037 static void
3038 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3039                       unsigned int epoch, bool *purge_cache)
3040 {
3041         oplock &= 0xFF;
3042         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3043                 return;
3044         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3045                 cinode->oplock = CIFS_CACHE_RHW_FLG;
3046                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3047                          &cinode->vfs_inode);
3048         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3049                 cinode->oplock = CIFS_CACHE_RW_FLG;
3050                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3051                          &cinode->vfs_inode);
3052         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3053                 cinode->oplock = CIFS_CACHE_READ_FLG;
3054                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3055                          &cinode->vfs_inode);
3056         } else
3057                 cinode->oplock = 0;
3058 }
3059
3060 static void
3061 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3062                        unsigned int epoch, bool *purge_cache)
3063 {
3064         char message[5] = {0};
3065         unsigned int new_oplock = 0;
3066
3067         oplock &= 0xFF;
3068         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3069                 return;
3070
3071         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3072                 new_oplock |= CIFS_CACHE_READ_FLG;
3073                 strcat(message, "R");
3074         }
3075         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3076                 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3077                 strcat(message, "H");
3078         }
3079         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3080                 new_oplock |= CIFS_CACHE_WRITE_FLG;
3081                 strcat(message, "W");
3082         }
3083         if (!new_oplock)
3084                 strncpy(message, "None", sizeof(message));
3085
3086         cinode->oplock = new_oplock;
3087         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3088                  &cinode->vfs_inode);
3089 }
3090
3091 static void
3092 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3093                       unsigned int epoch, bool *purge_cache)
3094 {
3095         unsigned int old_oplock = cinode->oplock;
3096
3097         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3098
3099         if (purge_cache) {
3100                 *purge_cache = false;
3101                 if (old_oplock == CIFS_CACHE_READ_FLG) {
3102                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3103                             (epoch - cinode->epoch > 0))
3104                                 *purge_cache = true;
3105                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3106                                  (epoch - cinode->epoch > 1))
3107                                 *purge_cache = true;
3108                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3109                                  (epoch - cinode->epoch > 1))
3110                                 *purge_cache = true;
3111                         else if (cinode->oplock == 0 &&
3112                                  (epoch - cinode->epoch > 0))
3113                                 *purge_cache = true;
3114                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3115                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3116                             (epoch - cinode->epoch > 0))
3117                                 *purge_cache = true;
3118                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3119                                  (epoch - cinode->epoch > 1))
3120                                 *purge_cache = true;
3121                 }
3122                 cinode->epoch = epoch;
3123         }
3124 }
3125
3126 static bool
3127 smb2_is_read_op(__u32 oplock)
3128 {
3129         return oplock == SMB2_OPLOCK_LEVEL_II;
3130 }
3131
3132 static bool
3133 smb21_is_read_op(__u32 oplock)
3134 {
3135         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3136                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3137 }
3138
3139 static __le32
3140 map_oplock_to_lease(u8 oplock)
3141 {
3142         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3143                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3144         else if (oplock == SMB2_OPLOCK_LEVEL_II)
3145                 return SMB2_LEASE_READ_CACHING;
3146         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3147                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3148                        SMB2_LEASE_WRITE_CACHING;
3149         return 0;
3150 }
3151
3152 static char *
3153 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3154 {
3155         struct create_lease *buf;
3156
3157         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3158         if (!buf)
3159                 return NULL;
3160
3161         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3162         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3163
3164         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3165                                         (struct create_lease, lcontext));
3166         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3167         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3168                                 (struct create_lease, Name));
3169         buf->ccontext.NameLength = cpu_to_le16(4);
3170         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3171         buf->Name[0] = 'R';
3172         buf->Name[1] = 'q';
3173         buf->Name[2] = 'L';
3174         buf->Name[3] = 's';
3175         return (char *)buf;
3176 }
3177
3178 static char *
3179 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3180 {
3181         struct create_lease_v2 *buf;
3182
3183         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3184         if (!buf)
3185                 return NULL;
3186
3187         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3188         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3189
3190         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3191                                         (struct create_lease_v2, lcontext));
3192         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3193         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3194                                 (struct create_lease_v2, Name));
3195         buf->ccontext.NameLength = cpu_to_le16(4);
3196         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3197         buf->Name[0] = 'R';
3198         buf->Name[1] = 'q';
3199         buf->Name[2] = 'L';
3200         buf->Name[3] = 's';
3201         return (char *)buf;
3202 }
3203
3204 static __u8
3205 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3206 {
3207         struct create_lease *lc = (struct create_lease *)buf;
3208
3209         *epoch = 0; /* not used */
3210         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3211                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3212         return le32_to_cpu(lc->lcontext.LeaseState);
3213 }
3214
3215 static __u8
3216 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3217 {
3218         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3219
3220         *epoch = le16_to_cpu(lc->lcontext.Epoch);
3221         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3222                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3223         if (lease_key)
3224                 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3225         return le32_to_cpu(lc->lcontext.LeaseState);
3226 }
3227
3228 static unsigned int
3229 smb2_wp_retry_size(struct inode *inode)
3230 {
3231         return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3232                      SMB2_MAX_BUFFER_SIZE);
3233 }
3234
3235 static bool
3236 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3237 {
3238         return !cfile->invalidHandle;
3239 }
3240
3241 static void
3242 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3243                    struct smb_rqst *old_rq)
3244 {
3245         struct smb2_sync_hdr *shdr =
3246                         (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3247
3248         memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3249         tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3250         tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3251         tr_hdr->Flags = cpu_to_le16(0x01);
3252         get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3253         memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3254 }
3255
3256 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3257  * stack object as buf.
3258  */
3259 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3260                                    unsigned int buflen)
3261 {
3262         sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
3263 }
3264
3265 /* Assumes the first rqst has a transform header as the first iov.
3266  * I.e.
3267  * rqst[0].rq_iov[0]  is transform header
3268  * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3269  * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3270  */
3271 static struct scatterlist *
3272 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3273 {
3274         unsigned int sg_len;
3275         struct scatterlist *sg;
3276         unsigned int i;
3277         unsigned int j;
3278         unsigned int idx = 0;
3279         int skip;
3280
3281         sg_len = 1;
3282         for (i = 0; i < num_rqst; i++)
3283                 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3284
3285         sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3286         if (!sg)
3287                 return NULL;
3288
3289         sg_init_table(sg, sg_len);
3290         for (i = 0; i < num_rqst; i++) {
3291                 for (j = 0; j < rqst[i].rq_nvec; j++) {
3292                         /*
3293                          * The first rqst has a transform header where the
3294                          * first 20 bytes are not part of the encrypted blob
3295                          */
3296                         skip = (i == 0) && (j == 0) ? 20 : 0;
3297                         smb2_sg_set_buf(&sg[idx++],
3298                                         rqst[i].rq_iov[j].iov_base + skip,
3299                                         rqst[i].rq_iov[j].iov_len - skip);
3300                         }
3301
3302                 for (j = 0; j < rqst[i].rq_npages; j++) {
3303                         unsigned int len, offset;
3304
3305                         rqst_page_get_length(&rqst[i], j, &len, &offset);
3306                         sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3307                 }
3308         }
3309         smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3310         return sg;
3311 }
3312
3313 static int
3314 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3315 {
3316         struct cifs_ses *ses;
3317         u8 *ses_enc_key;
3318
3319         spin_lock(&cifs_tcp_ses_lock);
3320         list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3321                 if (ses->Suid != ses_id)
3322                         continue;
3323                 ses_enc_key = enc ? ses->smb3encryptionkey :
3324                                                         ses->smb3decryptionkey;
3325                 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3326                 spin_unlock(&cifs_tcp_ses_lock);
3327                 return 0;
3328         }
3329         spin_unlock(&cifs_tcp_ses_lock);
3330
3331         return 1;
3332 }
3333 /*
3334  * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3335  * iov[0]   - transform header (associate data),
3336  * iov[1-N] - SMB2 header and pages - data to encrypt.
3337  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3338  * untouched.
3339  */
3340 static int
3341 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3342               struct smb_rqst *rqst, int enc)
3343 {
3344         struct smb2_transform_hdr *tr_hdr =
3345                 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3346         unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3347         int rc = 0;
3348         struct scatterlist *sg;
3349         u8 sign[SMB2_SIGNATURE_SIZE] = {};
3350         u8 key[SMB3_SIGN_KEY_SIZE];
3351         struct aead_request *req;
3352         char *iv;
3353         unsigned int iv_len;
3354         DECLARE_CRYPTO_WAIT(wait);
3355         struct crypto_aead *tfm;
3356         unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3357
3358         rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3359         if (rc) {
3360                 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3361                          enc ? "en" : "de");
3362                 return 0;
3363         }
3364
3365         rc = smb3_crypto_aead_allocate(server);
3366         if (rc) {
3367                 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3368                 return rc;
3369         }
3370
3371         tfm = enc ? server->secmech.ccmaesencrypt :
3372                                                 server->secmech.ccmaesdecrypt;
3373         rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3374         if (rc) {
3375                 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3376                 return rc;
3377         }
3378
3379         rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3380         if (rc) {
3381                 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3382                 return rc;
3383         }
3384
3385         req = aead_request_alloc(tfm, GFP_KERNEL);
3386         if (!req) {
3387                 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
3388                 return -ENOMEM;
3389         }
3390
3391         if (!enc) {
3392                 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3393                 crypt_len += SMB2_SIGNATURE_SIZE;
3394         }
3395
3396         sg = init_sg(num_rqst, rqst, sign);
3397         if (!sg) {
3398                 cifs_dbg(VFS, "%s: Failed to init sg", __func__);
3399                 rc = -ENOMEM;
3400                 goto free_req;
3401         }
3402
3403         iv_len = crypto_aead_ivsize(tfm);
3404         iv = kzalloc(iv_len, GFP_KERNEL);
3405         if (!iv) {
3406                 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
3407                 rc = -ENOMEM;
3408                 goto free_sg;
3409         }
3410         iv[0] = 3;
3411         memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3412
3413         aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3414         aead_request_set_ad(req, assoc_data_len);
3415
3416         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3417                                   crypto_req_done, &wait);
3418
3419         rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3420                                 : crypto_aead_decrypt(req), &wait);
3421
3422         if (!rc && enc)
3423                 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3424
3425         kfree(iv);
3426 free_sg:
3427         kfree(sg);
3428 free_req:
3429         kfree(req);
3430         return rc;
3431 }
3432
3433 void
3434 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3435 {
3436         int i, j;
3437
3438         for (i = 0; i < num_rqst; i++) {
3439                 if (rqst[i].rq_pages) {
3440                         for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3441                                 put_page(rqst[i].rq_pages[j]);
3442                         kfree(rqst[i].rq_pages);
3443                 }
3444         }
3445 }
3446
3447 /*
3448  * This function will initialize new_rq and encrypt the content.
3449  * The first entry, new_rq[0], only contains a single iov which contains
3450  * a smb2_transform_hdr and is pre-allocated by the caller.
3451  * This function then populates new_rq[1+] with the content from olq_rq[0+].
3452  *
3453  * The end result is an array of smb_rqst structures where the first structure
3454  * only contains a single iov for the transform header which we then can pass
3455  * to crypt_message().
3456  *
3457  * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
3458  * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3459  */
3460 static int
3461 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3462                        struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3463 {
3464         struct page **pages;
3465         struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3466         unsigned int npages;
3467         unsigned int orig_len = 0;
3468         int i, j;
3469         int rc = -ENOMEM;
3470
3471         for (i = 1; i < num_rqst; i++) {
3472                 npages = old_rq[i - 1].rq_npages;
3473                 pages = kmalloc_array(npages, sizeof(struct page *),
3474                                       GFP_KERNEL);
3475                 if (!pages)
3476                         goto err_free;
3477
3478                 new_rq[i].rq_pages = pages;
3479                 new_rq[i].rq_npages = npages;
3480                 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3481                 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3482                 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3483                 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3484                 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3485
3486                 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3487
3488                 for (j = 0; j < npages; j++) {
3489                         pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3490                         if (!pages[j])
3491                                 goto err_free;
3492                 }
3493
3494                 /* copy pages form the old */
3495                 for (j = 0; j < npages; j++) {
3496                         char *dst, *src;
3497                         unsigned int offset, len;
3498
3499                         rqst_page_get_length(&new_rq[i], j, &len, &offset);
3500
3501                         dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3502                         src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3503
3504                         memcpy(dst, src, len);
3505                         kunmap(new_rq[i].rq_pages[j]);
3506                         kunmap(old_rq[i - 1].rq_pages[j]);
3507                 }
3508         }
3509
3510         /* fill the 1st iov with a transform header */
3511         fill_transform_hdr(tr_hdr, orig_len, old_rq);
3512
3513         rc = crypt_message(server, num_rqst, new_rq, 1);
3514         cifs_dbg(FYI, "encrypt message returned %d", rc);
3515         if (rc)
3516                 goto err_free;
3517
3518         return rc;
3519
3520 err_free:
3521         smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3522         return rc;
3523 }
3524
3525 static int
3526 smb3_is_transform_hdr(void *buf)
3527 {
3528         struct smb2_transform_hdr *trhdr = buf;
3529
3530         return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3531 }
3532
3533 static int
3534 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3535                  unsigned int buf_data_size, struct page **pages,
3536                  unsigned int npages, unsigned int page_data_size)
3537 {
3538         struct kvec iov[2];
3539         struct smb_rqst rqst = {NULL};
3540         int rc;
3541
3542         iov[0].iov_base = buf;
3543         iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3544         iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3545         iov[1].iov_len = buf_data_size;
3546
3547         rqst.rq_iov = iov;
3548         rqst.rq_nvec = 2;
3549         rqst.rq_pages = pages;
3550         rqst.rq_npages = npages;
3551         rqst.rq_pagesz = PAGE_SIZE;
3552         rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3553
3554         rc = crypt_message(server, 1, &rqst, 0);
3555         cifs_dbg(FYI, "decrypt message returned %d\n", rc);
3556
3557         if (rc)
3558                 return rc;
3559
3560         memmove(buf, iov[1].iov_base, buf_data_size);
3561
3562         server->total_read = buf_data_size + page_data_size;
3563
3564         return rc;
3565 }
3566
3567 static int
3568 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3569                      unsigned int npages, unsigned int len)
3570 {
3571         int i;
3572         int length;
3573
3574         for (i = 0; i < npages; i++) {
3575                 struct page *page = pages[i];
3576                 size_t n;
3577
3578                 n = len;
3579                 if (len >= PAGE_SIZE) {
3580                         /* enough data to fill the page */
3581                         n = PAGE_SIZE;
3582                         len -= n;
3583                 } else {
3584                         zero_user(page, len, PAGE_SIZE - len);
3585                         len = 0;
3586                 }
3587                 length = cifs_read_page_from_socket(server, page, 0, n);
3588                 if (length < 0)
3589                         return length;
3590                 server->total_read += length;
3591         }
3592
3593         return 0;
3594 }
3595
3596 static int
3597 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3598                unsigned int cur_off, struct bio_vec **page_vec)
3599 {
3600         struct bio_vec *bvec;
3601         int i;
3602
3603         bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3604         if (!bvec)
3605                 return -ENOMEM;
3606
3607         for (i = 0; i < npages; i++) {
3608                 bvec[i].bv_page = pages[i];
3609                 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3610                 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3611                 data_size -= bvec[i].bv_len;
3612         }
3613
3614         if (data_size != 0) {
3615                 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3616                 kfree(bvec);
3617                 return -EIO;
3618         }
3619
3620         *page_vec = bvec;
3621         return 0;
3622 }
3623
3624 static int
3625 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3626                  char *buf, unsigned int buf_len, struct page **pages,
3627                  unsigned int npages, unsigned int page_data_size)
3628 {
3629         unsigned int data_offset;
3630         unsigned int data_len;
3631         unsigned int cur_off;
3632         unsigned int cur_page_idx;
3633         unsigned int pad_len;
3634         struct cifs_readdata *rdata = mid->callback_data;
3635         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
3636         struct bio_vec *bvec = NULL;
3637         struct iov_iter iter;
3638         struct kvec iov;
3639         int length;
3640         bool use_rdma_mr = false;
3641
3642         if (shdr->Command != SMB2_READ) {
3643                 cifs_dbg(VFS, "only big read responses are supported\n");
3644                 return -ENOTSUPP;
3645         }
3646
3647         if (server->ops->is_session_expired &&
3648             server->ops->is_session_expired(buf)) {
3649                 cifs_reconnect(server);
3650                 wake_up(&server->response_q);
3651                 return -1;
3652         }
3653
3654         if (server->ops->is_status_pending &&
3655                         server->ops->is_status_pending(buf, server))
3656                 return -1;
3657
3658         /* set up first two iov to get credits */
3659         rdata->iov[0].iov_base = buf;
3660         rdata->iov[0].iov_len = 0;
3661         rdata->iov[1].iov_base = buf;
3662         rdata->iov[1].iov_len =
3663                 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
3664         cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3665                  rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3666         cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3667                  rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3668
3669         rdata->result = server->ops->map_error(buf, true);
3670         if (rdata->result != 0) {
3671                 cifs_dbg(FYI, "%s: server returned error %d\n",
3672                          __func__, rdata->result);
3673                 /* normal error on read response */
3674                 dequeue_mid(mid, false);
3675                 return 0;
3676         }
3677
3678         data_offset = server->ops->read_data_offset(buf);
3679 #ifdef CONFIG_CIFS_SMB_DIRECT
3680         use_rdma_mr = rdata->mr;
3681 #endif
3682         data_len = server->ops->read_data_length(buf, use_rdma_mr);
3683
3684         if (data_offset < server->vals->read_rsp_size) {
3685                 /*
3686                  * win2k8 sometimes sends an offset of 0 when the read
3687                  * is beyond the EOF. Treat it as if the data starts just after
3688                  * the header.
3689                  */
3690                 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3691                          __func__, data_offset);
3692                 data_offset = server->vals->read_rsp_size;
3693         } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3694                 /* data_offset is beyond the end of smallbuf */
3695                 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3696                          __func__, data_offset);
3697                 rdata->result = -EIO;
3698                 dequeue_mid(mid, rdata->result);
3699                 return 0;
3700         }
3701
3702         pad_len = data_offset - server->vals->read_rsp_size;
3703
3704         if (buf_len <= data_offset) {
3705                 /* read response payload is in pages */
3706                 cur_page_idx = pad_len / PAGE_SIZE;
3707                 cur_off = pad_len % PAGE_SIZE;
3708
3709                 if (cur_page_idx != 0) {
3710                         /* data offset is beyond the 1st page of response */
3711                         cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3712                                  __func__, data_offset);
3713                         rdata->result = -EIO;
3714                         dequeue_mid(mid, rdata->result);
3715                         return 0;
3716                 }
3717
3718                 if (data_len > page_data_size - pad_len) {
3719                         /* data_len is corrupt -- discard frame */
3720                         rdata->result = -EIO;
3721                         dequeue_mid(mid, rdata->result);
3722                         return 0;
3723                 }
3724
3725                 rdata->result = init_read_bvec(pages, npages, page_data_size,
3726                                                cur_off, &bvec);
3727                 if (rdata->result != 0) {
3728                         dequeue_mid(mid, rdata->result);
3729                         return 0;
3730                 }
3731
3732                 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
3733         } else if (buf_len >= data_offset + data_len) {
3734                 /* read response payload is in buf */
3735                 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3736                 iov.iov_base = buf + data_offset;
3737                 iov.iov_len = data_len;
3738                 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
3739         } else {
3740                 /* read response payload cannot be in both buf and pages */
3741                 WARN_ONCE(1, "buf can not contain only a part of read data");
3742                 rdata->result = -EIO;
3743                 dequeue_mid(mid, rdata->result);
3744                 return 0;
3745         }
3746
3747         length = rdata->copy_into_pages(server, rdata, &iter);
3748
3749         kfree(bvec);
3750
3751         if (length < 0)
3752                 return length;
3753
3754         dequeue_mid(mid, false);
3755         return length;
3756 }
3757
3758 static int
3759 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3760 {
3761         char *buf = server->smallbuf;
3762         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3763         unsigned int npages;
3764         struct page **pages;
3765         unsigned int len;
3766         unsigned int buflen = server->pdu_size;
3767         int rc;
3768         int i = 0;
3769
3770         len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
3771                 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3772
3773         rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3774         if (rc < 0)
3775                 return rc;
3776         server->total_read += rc;
3777
3778         len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
3779                 server->vals->read_rsp_size;
3780         npages = DIV_ROUND_UP(len, PAGE_SIZE);
3781
3782         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3783         if (!pages) {
3784                 rc = -ENOMEM;
3785                 goto discard_data;
3786         }
3787
3788         for (; i < npages; i++) {
3789                 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3790                 if (!pages[i]) {
3791                         rc = -ENOMEM;
3792                         goto discard_data;
3793                 }
3794         }
3795
3796         /* read read data into pages */
3797         rc = read_data_into_pages(server, pages, npages, len);
3798         if (rc)
3799                 goto free_pages;
3800
3801         rc = cifs_discard_remaining_data(server);
3802         if (rc)
3803                 goto free_pages;
3804
3805         rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
3806                               pages, npages, len);
3807         if (rc)
3808                 goto free_pages;
3809
3810         *mid = smb2_find_mid(server, buf);
3811         if (*mid == NULL)
3812                 cifs_dbg(FYI, "mid not found\n");
3813         else {
3814                 cifs_dbg(FYI, "mid found\n");
3815                 (*mid)->decrypted = true;
3816                 rc = handle_read_data(server, *mid, buf,
3817                                       server->vals->read_rsp_size,
3818                                       pages, npages, len);
3819         }
3820
3821 free_pages:
3822         for (i = i - 1; i >= 0; i--)
3823                 put_page(pages[i]);
3824         kfree(pages);
3825         return rc;
3826 discard_data:
3827         cifs_discard_remaining_data(server);
3828         goto free_pages;
3829 }
3830
3831 static int
3832 receive_encrypted_standard(struct TCP_Server_Info *server,
3833                            struct mid_q_entry **mids, char **bufs,
3834                            int *num_mids)
3835 {
3836         int ret, length;
3837         char *buf = server->smallbuf;
3838         char *tmpbuf;
3839         struct smb2_sync_hdr *shdr;
3840         unsigned int pdu_length = server->pdu_size;
3841         unsigned int buf_size;
3842         struct mid_q_entry *mid_entry;
3843         int next_is_large;
3844         char *next_buffer = NULL;
3845
3846         *num_mids = 0;
3847
3848         /* switch to large buffer if too big for a small one */
3849         if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
3850                 server->large_buf = true;
3851                 memcpy(server->bigbuf, buf, server->total_read);
3852                 buf = server->bigbuf;
3853         }
3854
3855         /* now read the rest */
3856         length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
3857                                 pdu_length - HEADER_SIZE(server) + 1);
3858         if (length < 0)
3859                 return length;
3860         server->total_read += length;
3861
3862         buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
3863         length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3864         if (length)
3865                 return length;
3866
3867         next_is_large = server->large_buf;
3868  one_more:
3869         shdr = (struct smb2_sync_hdr *)buf;
3870         if (shdr->NextCommand) {
3871                 if (next_is_large) {
3872                         tmpbuf = server->bigbuf;
3873                         next_buffer = (char *)cifs_buf_get();
3874                 } else {
3875                         tmpbuf = server->smallbuf;
3876                         next_buffer = (char *)cifs_small_buf_get();
3877                 }
3878                 memcpy(next_buffer,
3879                        tmpbuf + le32_to_cpu(shdr->NextCommand),
3880                        pdu_length - le32_to_cpu(shdr->NextCommand));
3881         }
3882
3883         mid_entry = smb2_find_mid(server, buf);
3884         if (mid_entry == NULL)
3885                 cifs_dbg(FYI, "mid not found\n");
3886         else {
3887                 cifs_dbg(FYI, "mid found\n");
3888                 mid_entry->decrypted = true;
3889                 mid_entry->resp_buf_size = server->pdu_size;
3890         }
3891
3892         if (*num_mids >= MAX_COMPOUND) {
3893                 cifs_dbg(VFS, "too many PDUs in compound\n");
3894                 return -1;
3895         }
3896         bufs[*num_mids] = buf;
3897         mids[(*num_mids)++] = mid_entry;
3898
3899         if (mid_entry && mid_entry->handle)
3900                 ret = mid_entry->handle(server, mid_entry);
3901         else
3902                 ret = cifs_handle_standard(server, mid_entry);
3903
3904         if (ret == 0 && shdr->NextCommand) {
3905                 pdu_length -= le32_to_cpu(shdr->NextCommand);
3906                 server->large_buf = next_is_large;
3907                 if (next_is_large)
3908                         server->bigbuf = next_buffer;
3909                 else
3910                         server->smallbuf = next_buffer;
3911
3912                 buf += le32_to_cpu(shdr->NextCommand);
3913                 goto one_more;
3914         }
3915
3916         return ret;
3917 }
3918
3919 static int
3920 smb3_receive_transform(struct TCP_Server_Info *server,
3921                        struct mid_q_entry **mids, char **bufs, int *num_mids)
3922 {
3923         char *buf = server->smallbuf;
3924         unsigned int pdu_length = server->pdu_size;
3925         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3926         unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3927
3928         if (pdu_length < sizeof(struct smb2_transform_hdr) +
3929                                                 sizeof(struct smb2_sync_hdr)) {
3930                 cifs_dbg(VFS, "Transform message is too small (%u)\n",
3931                          pdu_length);
3932                 cifs_reconnect(server);
3933                 wake_up(&server->response_q);
3934                 return -ECONNABORTED;
3935         }
3936
3937         if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
3938                 cifs_dbg(VFS, "Transform message is broken\n");
3939                 cifs_reconnect(server);
3940                 wake_up(&server->response_q);
3941                 return -ECONNABORTED;
3942         }
3943
3944         /* TODO: add support for compounds containing READ. */
3945         if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
3946                 *num_mids = 1;
3947                 return receive_encrypted_read(server, &mids[0]);
3948         }
3949
3950         return receive_encrypted_standard(server, mids, bufs, num_mids);
3951 }
3952
3953 int
3954 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
3955 {
3956         char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
3957
3958         return handle_read_data(server, mid, buf, server->pdu_size,
3959                                 NULL, 0, 0);
3960 }
3961
3962 static int
3963 smb2_next_header(char *buf)
3964 {
3965         struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
3966         struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
3967
3968         if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
3969                 return sizeof(struct smb2_transform_hdr) +
3970                   le32_to_cpu(t_hdr->OriginalMessageSize);
3971
3972         return le32_to_cpu(hdr->NextCommand);
3973 }
3974
3975 static int
3976 smb2_make_node(unsigned int xid, struct inode *inode,
3977                struct dentry *dentry, struct cifs_tcon *tcon,
3978                char *full_path, umode_t mode, dev_t dev)
3979 {
3980         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3981         int rc = -EPERM;
3982         int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
3983         FILE_ALL_INFO *buf = NULL;
3984         struct cifs_io_parms io_parms;
3985         __u32 oplock = 0;
3986         struct cifs_fid fid;
3987         struct cifs_open_parms oparms;
3988         unsigned int bytes_written;
3989         struct win_dev *pdev;
3990         struct kvec iov[2];
3991
3992         /*
3993          * Check if mounted with mount parm 'sfu' mount parm.
3994          * SFU emulation should work with all servers, but only
3995          * supports block and char device (no socket & fifo),
3996          * and was used by default in earlier versions of Windows
3997          */
3998         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
3999                 goto out;
4000
4001         /*
4002          * TODO: Add ability to create instead via reparse point. Windows (e.g.
4003          * their current NFS server) uses this approach to expose special files
4004          * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4005          */
4006
4007         if (!S_ISCHR(mode) && !S_ISBLK(mode))
4008                 goto out;
4009
4010         cifs_dbg(FYI, "sfu compat create special file\n");
4011
4012         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4013         if (buf == NULL) {
4014                 rc = -ENOMEM;
4015                 goto out;
4016         }
4017
4018         if (backup_cred(cifs_sb))
4019                 create_options |= CREATE_OPEN_BACKUP_INTENT;
4020
4021         oparms.tcon = tcon;
4022         oparms.cifs_sb = cifs_sb;
4023         oparms.desired_access = GENERIC_WRITE;
4024         oparms.create_options = create_options;
4025         oparms.disposition = FILE_CREATE;
4026         oparms.path = full_path;
4027         oparms.fid = &fid;
4028         oparms.reconnect = false;
4029
4030         if (tcon->ses->server->oplocks)
4031                 oplock = REQ_OPLOCK;
4032         else
4033                 oplock = 0;
4034         rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4035         if (rc)
4036                 goto out;
4037
4038         /*
4039          * BB Do not bother to decode buf since no local inode yet to put
4040          * timestamps in, but we can reuse it safely.
4041          */
4042
4043         pdev = (struct win_dev *)buf;
4044         io_parms.pid = current->tgid;
4045         io_parms.tcon = tcon;
4046         io_parms.offset = 0;
4047         io_parms.length = sizeof(struct win_dev);
4048         iov[1].iov_base = buf;
4049         iov[1].iov_len = sizeof(struct win_dev);
4050         if (S_ISCHR(mode)) {
4051                 memcpy(pdev->type, "IntxCHR", 8);
4052                 pdev->major = cpu_to_le64(MAJOR(dev));
4053                 pdev->minor = cpu_to_le64(MINOR(dev));
4054                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4055                                                         &bytes_written, iov, 1);
4056         } else if (S_ISBLK(mode)) {
4057                 memcpy(pdev->type, "IntxBLK", 8);
4058                 pdev->major = cpu_to_le64(MAJOR(dev));
4059                 pdev->minor = cpu_to_le64(MINOR(dev));
4060                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4061                                                         &bytes_written, iov, 1);
4062         }
4063         tcon->ses->server->ops->close(xid, tcon, &fid);
4064         d_drop(dentry);
4065
4066         /* FIXME: add code here to set EAs */
4067 out:
4068         kfree(buf);
4069         return rc;
4070 }
4071
4072
4073 struct smb_version_operations smb20_operations = {
4074         .compare_fids = smb2_compare_fids,
4075         .setup_request = smb2_setup_request,
4076         .setup_async_request = smb2_setup_async_request,
4077         .check_receive = smb2_check_receive,
4078         .add_credits = smb2_add_credits,
4079         .set_credits = smb2_set_credits,
4080         .get_credits_field = smb2_get_credits_field,
4081         .get_credits = smb2_get_credits,
4082         .wait_mtu_credits = cifs_wait_mtu_credits,
4083         .get_next_mid = smb2_get_next_mid,
4084         .revert_current_mid = smb2_revert_current_mid,
4085         .read_data_offset = smb2_read_data_offset,
4086         .read_data_length = smb2_read_data_length,
4087         .map_error = map_smb2_to_linux_error,
4088         .find_mid = smb2_find_mid,
4089         .check_message = smb2_check_message,
4090         .dump_detail = smb2_dump_detail,
4091         .clear_stats = smb2_clear_stats,
4092         .print_stats = smb2_print_stats,
4093         .is_oplock_break = smb2_is_valid_oplock_break,
4094         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4095         .downgrade_oplock = smb2_downgrade_oplock,
4096         .need_neg = smb2_need_neg,
4097         .negotiate = smb2_negotiate,
4098         .negotiate_wsize = smb2_negotiate_wsize,
4099         .negotiate_rsize = smb2_negotiate_rsize,
4100         .sess_setup = SMB2_sess_setup,
4101         .logoff = SMB2_logoff,
4102         .tree_connect = SMB2_tcon,
4103         .tree_disconnect = SMB2_tdis,
4104         .qfs_tcon = smb2_qfs_tcon,
4105         .is_path_accessible = smb2_is_path_accessible,
4106         .can_echo = smb2_can_echo,
4107         .echo = SMB2_echo,
4108         .query_path_info = smb2_query_path_info,
4109         .get_srv_inum = smb2_get_srv_inum,
4110         .query_file_info = smb2_query_file_info,
4111         .set_path_size = smb2_set_path_size,
4112         .set_file_size = smb2_set_file_size,
4113         .set_file_info = smb2_set_file_info,
4114         .set_compression = smb2_set_compression,
4115         .mkdir = smb2_mkdir,
4116         .mkdir_setinfo = smb2_mkdir_setinfo,
4117         .rmdir = smb2_rmdir,
4118         .unlink = smb2_unlink,
4119         .rename = smb2_rename_path,
4120         .create_hardlink = smb2_create_hardlink,
4121         .query_symlink = smb2_query_symlink,
4122         .query_mf_symlink = smb3_query_mf_symlink,
4123         .create_mf_symlink = smb3_create_mf_symlink,
4124         .open = smb2_open_file,
4125         .set_fid = smb2_set_fid,
4126         .close = smb2_close_file,
4127         .flush = smb2_flush_file,
4128         .async_readv = smb2_async_readv,
4129         .async_writev = smb2_async_writev,
4130         .sync_read = smb2_sync_read,
4131         .sync_write = smb2_sync_write,
4132         .query_dir_first = smb2_query_dir_first,
4133         .query_dir_next = smb2_query_dir_next,
4134         .close_dir = smb2_close_dir,
4135         .calc_smb_size = smb2_calc_size,
4136         .is_status_pending = smb2_is_status_pending,
4137         .is_session_expired = smb2_is_session_expired,
4138         .oplock_response = smb2_oplock_response,
4139         .queryfs = smb2_queryfs,
4140         .mand_lock = smb2_mand_lock,
4141         .mand_unlock_range = smb2_unlock_range,
4142         .push_mand_locks = smb2_push_mandatory_locks,
4143         .get_lease_key = smb2_get_lease_key,
4144         .set_lease_key = smb2_set_lease_key,
4145         .new_lease_key = smb2_new_lease_key,
4146         .calc_signature = smb2_calc_signature,
4147         .is_read_op = smb2_is_read_op,
4148         .set_oplock_level = smb2_set_oplock_level,
4149         .create_lease_buf = smb2_create_lease_buf,
4150         .parse_lease_buf = smb2_parse_lease_buf,
4151         .copychunk_range = smb2_copychunk_range,
4152         .wp_retry_size = smb2_wp_retry_size,
4153         .dir_needs_close = smb2_dir_needs_close,
4154         .get_dfs_refer = smb2_get_dfs_refer,
4155         .select_sectype = smb2_select_sectype,
4156 #ifdef CONFIG_CIFS_XATTR
4157         .query_all_EAs = smb2_query_eas,
4158         .set_EA = smb2_set_ea,
4159 #endif /* CIFS_XATTR */
4160 #ifdef CONFIG_CIFS_ACL
4161         .get_acl = get_smb2_acl,
4162         .get_acl_by_fid = get_smb2_acl_by_fid,
4163         .set_acl = set_smb2_acl,
4164 #endif /* CIFS_ACL */
4165         .next_header = smb2_next_header,
4166         .ioctl_query_info = smb2_ioctl_query_info,
4167         .make_node = smb2_make_node,
4168         .fiemap = smb3_fiemap,
4169 };
4170
4171 struct smb_version_operations smb21_operations = {
4172         .compare_fids = smb2_compare_fids,
4173         .setup_request = smb2_setup_request,
4174         .setup_async_request = smb2_setup_async_request,
4175         .check_receive = smb2_check_receive,
4176         .add_credits = smb2_add_credits,
4177         .set_credits = smb2_set_credits,
4178         .get_credits_field = smb2_get_credits_field,
4179         .get_credits = smb2_get_credits,
4180         .wait_mtu_credits = smb2_wait_mtu_credits,
4181         .adjust_credits = smb2_adjust_credits,
4182         .get_next_mid = smb2_get_next_mid,
4183         .revert_current_mid = smb2_revert_current_mid,
4184         .read_data_offset = smb2_read_data_offset,
4185         .read_data_length = smb2_read_data_length,
4186         .map_error = map_smb2_to_linux_error,
4187         .find_mid = smb2_find_mid,
4188         .check_message = smb2_check_message,
4189         .dump_detail = smb2_dump_detail,
4190         .clear_stats = smb2_clear_stats,
4191         .print_stats = smb2_print_stats,
4192         .is_oplock_break = smb2_is_valid_oplock_break,
4193         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4194         .downgrade_oplock = smb21_downgrade_oplock,
4195         .need_neg = smb2_need_neg,
4196         .negotiate = smb2_negotiate,
4197         .negotiate_wsize = smb2_negotiate_wsize,
4198         .negotiate_rsize = smb2_negotiate_rsize,
4199         .sess_setup = SMB2_sess_setup,
4200         .logoff = SMB2_logoff,
4201         .tree_connect = SMB2_tcon,
4202         .tree_disconnect = SMB2_tdis,
4203         .qfs_tcon = smb2_qfs_tcon,
4204         .is_path_accessible = smb2_is_path_accessible,
4205         .can_echo = smb2_can_echo,
4206         .echo = SMB2_echo,
4207         .query_path_info = smb2_query_path_info,
4208         .get_srv_inum = smb2_get_srv_inum,
4209         .query_file_info = smb2_query_file_info,
4210         .set_path_size = smb2_set_path_size,
4211         .set_file_size = smb2_set_file_size,
4212         .set_file_info = smb2_set_file_info,
4213         .set_compression = smb2_set_compression,
4214         .mkdir = smb2_mkdir,
4215         .mkdir_setinfo = smb2_mkdir_setinfo,
4216         .rmdir = smb2_rmdir,
4217         .unlink = smb2_unlink,
4218         .rename = smb2_rename_path,
4219         .create_hardlink = smb2_create_hardlink,
4220         .query_symlink = smb2_query_symlink,
4221         .query_mf_symlink = smb3_query_mf_symlink,
4222         .create_mf_symlink = smb3_create_mf_symlink,
4223         .open = smb2_open_file,
4224         .set_fid = smb2_set_fid,
4225         .close = smb2_close_file,
4226         .flush = smb2_flush_file,
4227         .async_readv = smb2_async_readv,
4228         .async_writev = smb2_async_writev,
4229         .sync_read = smb2_sync_read,
4230         .sync_write = smb2_sync_write,
4231         .query_dir_first = smb2_query_dir_first,
4232         .query_dir_next = smb2_query_dir_next,
4233         .close_dir = smb2_close_dir,
4234         .calc_smb_size = smb2_calc_size,
4235         .is_status_pending = smb2_is_status_pending,
4236         .is_session_expired = smb2_is_session_expired,
4237         .oplock_response = smb2_oplock_response,
4238         .queryfs = smb2_queryfs,
4239         .mand_lock = smb2_mand_lock,
4240         .mand_unlock_range = smb2_unlock_range,
4241         .push_mand_locks = smb2_push_mandatory_locks,
4242         .get_lease_key = smb2_get_lease_key,
4243         .set_lease_key = smb2_set_lease_key,
4244         .new_lease_key = smb2_new_lease_key,
4245         .calc_signature = smb2_calc_signature,
4246         .is_read_op = smb21_is_read_op,
4247         .set_oplock_level = smb21_set_oplock_level,
4248         .create_lease_buf = smb2_create_lease_buf,
4249         .parse_lease_buf = smb2_parse_lease_buf,
4250         .copychunk_range = smb2_copychunk_range,
4251         .wp_retry_size = smb2_wp_retry_size,
4252         .dir_needs_close = smb2_dir_needs_close,
4253         .enum_snapshots = smb3_enum_snapshots,
4254         .get_dfs_refer = smb2_get_dfs_refer,
4255         .select_sectype = smb2_select_sectype,
4256 #ifdef CONFIG_CIFS_XATTR
4257         .query_all_EAs = smb2_query_eas,
4258         .set_EA = smb2_set_ea,
4259 #endif /* CIFS_XATTR */
4260 #ifdef CONFIG_CIFS_ACL
4261         .get_acl = get_smb2_acl,
4262         .get_acl_by_fid = get_smb2_acl_by_fid,
4263         .set_acl = set_smb2_acl,
4264 #endif /* CIFS_ACL */
4265         .next_header = smb2_next_header,
4266         .ioctl_query_info = smb2_ioctl_query_info,
4267         .make_node = smb2_make_node,
4268         .fiemap = smb3_fiemap,
4269 };
4270
4271 struct smb_version_operations smb30_operations = {
4272         .compare_fids = smb2_compare_fids,
4273         .setup_request = smb2_setup_request,
4274         .setup_async_request = smb2_setup_async_request,
4275         .check_receive = smb2_check_receive,
4276         .add_credits = smb2_add_credits,
4277         .set_credits = smb2_set_credits,
4278         .get_credits_field = smb2_get_credits_field,
4279         .get_credits = smb2_get_credits,
4280         .wait_mtu_credits = smb2_wait_mtu_credits,
4281         .adjust_credits = smb2_adjust_credits,
4282         .get_next_mid = smb2_get_next_mid,
4283         .revert_current_mid = smb2_revert_current_mid,
4284         .read_data_offset = smb2_read_data_offset,
4285         .read_data_length = smb2_read_data_length,
4286         .map_error = map_smb2_to_linux_error,
4287         .find_mid = smb2_find_mid,
4288         .check_message = smb2_check_message,
4289         .dump_detail = smb2_dump_detail,
4290         .clear_stats = smb2_clear_stats,
4291         .print_stats = smb2_print_stats,
4292         .dump_share_caps = smb2_dump_share_caps,
4293         .is_oplock_break = smb2_is_valid_oplock_break,
4294         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4295         .downgrade_oplock = smb21_downgrade_oplock,
4296         .need_neg = smb2_need_neg,
4297         .negotiate = smb2_negotiate,
4298         .negotiate_wsize = smb3_negotiate_wsize,
4299         .negotiate_rsize = smb3_negotiate_rsize,
4300         .sess_setup = SMB2_sess_setup,
4301         .logoff = SMB2_logoff,
4302         .tree_connect = SMB2_tcon,
4303         .tree_disconnect = SMB2_tdis,
4304         .qfs_tcon = smb3_qfs_tcon,
4305         .is_path_accessible = smb2_is_path_accessible,
4306         .can_echo = smb2_can_echo,
4307         .echo = SMB2_echo,
4308         .query_path_info = smb2_query_path_info,
4309         .get_srv_inum = smb2_get_srv_inum,
4310         .query_file_info = smb2_query_file_info,
4311         .set_path_size = smb2_set_path_size,
4312         .set_file_size = smb2_set_file_size,
4313         .set_file_info = smb2_set_file_info,
4314         .set_compression = smb2_set_compression,
4315         .mkdir = smb2_mkdir,
4316         .mkdir_setinfo = smb2_mkdir_setinfo,
4317         .rmdir = smb2_rmdir,
4318         .unlink = smb2_unlink,
4319         .rename = smb2_rename_path,
4320         .create_hardlink = smb2_create_hardlink,
4321         .query_symlink = smb2_query_symlink,
4322         .query_mf_symlink = smb3_query_mf_symlink,
4323         .create_mf_symlink = smb3_create_mf_symlink,
4324         .open = smb2_open_file,
4325         .set_fid = smb2_set_fid,
4326         .close = smb2_close_file,
4327         .flush = smb2_flush_file,
4328         .async_readv = smb2_async_readv,
4329         .async_writev = smb2_async_writev,
4330         .sync_read = smb2_sync_read,
4331         .sync_write = smb2_sync_write,
4332         .query_dir_first = smb2_query_dir_first,
4333         .query_dir_next = smb2_query_dir_next,
4334         .close_dir = smb2_close_dir,
4335         .calc_smb_size = smb2_calc_size,
4336         .is_status_pending = smb2_is_status_pending,
4337         .is_session_expired = smb2_is_session_expired,
4338         .oplock_response = smb2_oplock_response,
4339         .queryfs = smb2_queryfs,
4340         .mand_lock = smb2_mand_lock,
4341         .mand_unlock_range = smb2_unlock_range,
4342         .push_mand_locks = smb2_push_mandatory_locks,
4343         .get_lease_key = smb2_get_lease_key,
4344         .set_lease_key = smb2_set_lease_key,
4345         .new_lease_key = smb2_new_lease_key,
4346         .generate_signingkey = generate_smb30signingkey,
4347         .calc_signature = smb3_calc_signature,
4348         .set_integrity  = smb3_set_integrity,
4349         .is_read_op = smb21_is_read_op,
4350         .set_oplock_level = smb3_set_oplock_level,
4351         .create_lease_buf = smb3_create_lease_buf,
4352         .parse_lease_buf = smb3_parse_lease_buf,
4353         .copychunk_range = smb2_copychunk_range,
4354         .duplicate_extents = smb2_duplicate_extents,
4355         .validate_negotiate = smb3_validate_negotiate,
4356         .wp_retry_size = smb2_wp_retry_size,
4357         .dir_needs_close = smb2_dir_needs_close,
4358         .fallocate = smb3_fallocate,
4359         .enum_snapshots = smb3_enum_snapshots,
4360         .init_transform_rq = smb3_init_transform_rq,
4361         .is_transform_hdr = smb3_is_transform_hdr,
4362         .receive_transform = smb3_receive_transform,
4363         .get_dfs_refer = smb2_get_dfs_refer,
4364         .select_sectype = smb2_select_sectype,
4365 #ifdef CONFIG_CIFS_XATTR
4366         .query_all_EAs = smb2_query_eas,
4367         .set_EA = smb2_set_ea,
4368 #endif /* CIFS_XATTR */
4369 #ifdef CONFIG_CIFS_ACL
4370         .get_acl = get_smb2_acl,
4371         .get_acl_by_fid = get_smb2_acl_by_fid,
4372         .set_acl = set_smb2_acl,
4373 #endif /* CIFS_ACL */
4374         .next_header = smb2_next_header,
4375         .ioctl_query_info = smb2_ioctl_query_info,
4376         .make_node = smb2_make_node,
4377         .fiemap = smb3_fiemap,
4378 };
4379
4380 struct smb_version_operations smb311_operations = {
4381         .compare_fids = smb2_compare_fids,
4382         .setup_request = smb2_setup_request,
4383         .setup_async_request = smb2_setup_async_request,
4384         .check_receive = smb2_check_receive,
4385         .add_credits = smb2_add_credits,
4386         .set_credits = smb2_set_credits,
4387         .get_credits_field = smb2_get_credits_field,
4388         .get_credits = smb2_get_credits,
4389         .wait_mtu_credits = smb2_wait_mtu_credits,
4390         .adjust_credits = smb2_adjust_credits,
4391         .get_next_mid = smb2_get_next_mid,
4392         .revert_current_mid = smb2_revert_current_mid,
4393         .read_data_offset = smb2_read_data_offset,
4394         .read_data_length = smb2_read_data_length,
4395         .map_error = map_smb2_to_linux_error,
4396         .find_mid = smb2_find_mid,
4397         .check_message = smb2_check_message,
4398         .dump_detail = smb2_dump_detail,
4399         .clear_stats = smb2_clear_stats,
4400         .print_stats = smb2_print_stats,
4401         .dump_share_caps = smb2_dump_share_caps,
4402         .is_oplock_break = smb2_is_valid_oplock_break,
4403         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4404         .downgrade_oplock = smb21_downgrade_oplock,
4405         .need_neg = smb2_need_neg,
4406         .negotiate = smb2_negotiate,
4407         .negotiate_wsize = smb3_negotiate_wsize,
4408         .negotiate_rsize = smb3_negotiate_rsize,
4409         .sess_setup = SMB2_sess_setup,
4410         .logoff = SMB2_logoff,
4411         .tree_connect = SMB2_tcon,
4412         .tree_disconnect = SMB2_tdis,
4413         .qfs_tcon = smb3_qfs_tcon,
4414         .is_path_accessible = smb2_is_path_accessible,
4415         .can_echo = smb2_can_echo,
4416         .echo = SMB2_echo,
4417         .query_path_info = smb2_query_path_info,
4418         .get_srv_inum = smb2_get_srv_inum,
4419         .query_file_info = smb2_query_file_info,
4420         .set_path_size = smb2_set_path_size,
4421         .set_file_size = smb2_set_file_size,
4422         .set_file_info = smb2_set_file_info,
4423         .set_compression = smb2_set_compression,
4424         .mkdir = smb2_mkdir,
4425         .mkdir_setinfo = smb2_mkdir_setinfo,
4426         .posix_mkdir = smb311_posix_mkdir,
4427         .rmdir = smb2_rmdir,
4428         .unlink = smb2_unlink,
4429         .rename = smb2_rename_path,
4430         .create_hardlink = smb2_create_hardlink,
4431         .query_symlink = smb2_query_symlink,
4432         .query_mf_symlink = smb3_query_mf_symlink,
4433         .create_mf_symlink = smb3_create_mf_symlink,
4434         .open = smb2_open_file,
4435         .set_fid = smb2_set_fid,
4436         .close = smb2_close_file,
4437         .flush = smb2_flush_file,
4438         .async_readv = smb2_async_readv,
4439         .async_writev = smb2_async_writev,
4440         .sync_read = smb2_sync_read,
4441         .sync_write = smb2_sync_write,
4442         .query_dir_first = smb2_query_dir_first,
4443         .query_dir_next = smb2_query_dir_next,
4444         .close_dir = smb2_close_dir,
4445         .calc_smb_size = smb2_calc_size,
4446         .is_status_pending = smb2_is_status_pending,
4447         .is_session_expired = smb2_is_session_expired,
4448         .oplock_response = smb2_oplock_response,
4449         .queryfs = smb311_queryfs,
4450         .mand_lock = smb2_mand_lock,
4451         .mand_unlock_range = smb2_unlock_range,
4452         .push_mand_locks = smb2_push_mandatory_locks,
4453         .get_lease_key = smb2_get_lease_key,
4454         .set_lease_key = smb2_set_lease_key,
4455         .new_lease_key = smb2_new_lease_key,
4456         .generate_signingkey = generate_smb311signingkey,
4457         .calc_signature = smb3_calc_signature,
4458         .set_integrity  = smb3_set_integrity,
4459         .is_read_op = smb21_is_read_op,
4460         .set_oplock_level = smb3_set_oplock_level,
4461         .create_lease_buf = smb3_create_lease_buf,
4462         .parse_lease_buf = smb3_parse_lease_buf,
4463         .copychunk_range = smb2_copychunk_range,
4464         .duplicate_extents = smb2_duplicate_extents,
4465 /*      .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4466         .wp_retry_size = smb2_wp_retry_size,
4467         .dir_needs_close = smb2_dir_needs_close,
4468         .fallocate = smb3_fallocate,
4469         .enum_snapshots = smb3_enum_snapshots,
4470         .init_transform_rq = smb3_init_transform_rq,
4471         .is_transform_hdr = smb3_is_transform_hdr,
4472         .receive_transform = smb3_receive_transform,
4473         .get_dfs_refer = smb2_get_dfs_refer,
4474         .select_sectype = smb2_select_sectype,
4475 #ifdef CONFIG_CIFS_XATTR
4476         .query_all_EAs = smb2_query_eas,
4477         .set_EA = smb2_set_ea,
4478 #endif /* CIFS_XATTR */
4479 #ifdef CONFIG_CIFS_ACL
4480         .get_acl = get_smb2_acl,
4481         .get_acl_by_fid = get_smb2_acl_by_fid,
4482         .set_acl = set_smb2_acl,
4483 #endif /* CIFS_ACL */
4484         .next_header = smb2_next_header,
4485         .ioctl_query_info = smb2_ioctl_query_info,
4486         .make_node = smb2_make_node,
4487         .fiemap = smb3_fiemap,
4488 };
4489
4490 struct smb_version_values smb20_values = {
4491         .version_string = SMB20_VERSION_STRING,
4492         .protocol_id = SMB20_PROT_ID,
4493         .req_capabilities = 0, /* MBZ */
4494         .large_lock_type = 0,
4495         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4496         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4497         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4498         .header_size = sizeof(struct smb2_sync_hdr),
4499         .header_preamble_size = 0,
4500         .max_header_size = MAX_SMB2_HDR_SIZE,
4501         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4502         .lock_cmd = SMB2_LOCK,
4503         .cap_unix = 0,
4504         .cap_nt_find = SMB2_NT_FIND,
4505         .cap_large_files = SMB2_LARGE_FILES,
4506         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4507         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4508         .create_lease_size = sizeof(struct create_lease),
4509 };
4510
4511 struct smb_version_values smb21_values = {
4512         .version_string = SMB21_VERSION_STRING,
4513         .protocol_id = SMB21_PROT_ID,
4514         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4515         .large_lock_type = 0,
4516         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4517         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4518         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4519         .header_size = sizeof(struct smb2_sync_hdr),
4520         .header_preamble_size = 0,
4521         .max_header_size = MAX_SMB2_HDR_SIZE,
4522         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4523         .lock_cmd = SMB2_LOCK,
4524         .cap_unix = 0,
4525         .cap_nt_find = SMB2_NT_FIND,
4526         .cap_large_files = SMB2_LARGE_FILES,
4527         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4528         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4529         .create_lease_size = sizeof(struct create_lease),
4530 };
4531
4532 struct smb_version_values smb3any_values = {
4533         .version_string = SMB3ANY_VERSION_STRING,
4534         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4535         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4536         .large_lock_type = 0,
4537         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4538         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4539         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4540         .header_size = sizeof(struct smb2_sync_hdr),
4541         .header_preamble_size = 0,
4542         .max_header_size = MAX_SMB2_HDR_SIZE,
4543         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4544         .lock_cmd = SMB2_LOCK,
4545         .cap_unix = 0,
4546         .cap_nt_find = SMB2_NT_FIND,
4547         .cap_large_files = SMB2_LARGE_FILES,
4548         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4549         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4550         .create_lease_size = sizeof(struct create_lease_v2),
4551 };
4552
4553 struct smb_version_values smbdefault_values = {
4554         .version_string = SMBDEFAULT_VERSION_STRING,
4555         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
4556         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4557         .large_lock_type = 0,
4558         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4559         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4560         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4561         .header_size = sizeof(struct smb2_sync_hdr),
4562         .header_preamble_size = 0,
4563         .max_header_size = MAX_SMB2_HDR_SIZE,
4564         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4565         .lock_cmd = SMB2_LOCK,
4566         .cap_unix = 0,
4567         .cap_nt_find = SMB2_NT_FIND,
4568         .cap_large_files = SMB2_LARGE_FILES,
4569         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4570         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4571         .create_lease_size = sizeof(struct create_lease_v2),
4572 };
4573
4574 struct smb_version_values smb30_values = {
4575         .version_string = SMB30_VERSION_STRING,
4576         .protocol_id = SMB30_PROT_ID,
4577         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4578         .large_lock_type = 0,
4579         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4580         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4581         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4582         .header_size = sizeof(struct smb2_sync_hdr),
4583         .header_preamble_size = 0,
4584         .max_header_size = MAX_SMB2_HDR_SIZE,
4585         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4586         .lock_cmd = SMB2_LOCK,
4587         .cap_unix = 0,
4588         .cap_nt_find = SMB2_NT_FIND,
4589         .cap_large_files = SMB2_LARGE_FILES,
4590         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4591         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4592         .create_lease_size = sizeof(struct create_lease_v2),
4593 };
4594
4595 struct smb_version_values smb302_values = {
4596         .version_string = SMB302_VERSION_STRING,
4597         .protocol_id = SMB302_PROT_ID,
4598         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4599         .large_lock_type = 0,
4600         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4601         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4602         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4603         .header_size = sizeof(struct smb2_sync_hdr),
4604         .header_preamble_size = 0,
4605         .max_header_size = MAX_SMB2_HDR_SIZE,
4606         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4607         .lock_cmd = SMB2_LOCK,
4608         .cap_unix = 0,
4609         .cap_nt_find = SMB2_NT_FIND,
4610         .cap_large_files = SMB2_LARGE_FILES,
4611         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4612         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4613         .create_lease_size = sizeof(struct create_lease_v2),
4614 };
4615
4616 struct smb_version_values smb311_values = {
4617         .version_string = SMB311_VERSION_STRING,
4618         .protocol_id = SMB311_PROT_ID,
4619         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
4620         .large_lock_type = 0,
4621         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4622         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4623         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4624         .header_size = sizeof(struct smb2_sync_hdr),
4625         .header_preamble_size = 0,
4626         .max_header_size = MAX_SMB2_HDR_SIZE,
4627         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4628         .lock_cmd = SMB2_LOCK,
4629         .cap_unix = 0,
4630         .cap_nt_find = SMB2_NT_FIND,
4631         .cap_large_files = SMB2_LARGE_FILES,
4632         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4633         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4634         .create_lease_size = sizeof(struct create_lease_v2),
4635 };