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