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