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