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