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