OSDN Git Service

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