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