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