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