]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/cifs/smb2ops.c
Merge tag 'linux-kselftest-5.6-rc5' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / fs / cifs / smb2ops.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  SMB2 version specific operations
4  *
5  *  Copyright (c) 2012, Jeff Layton <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         struct smb2_query_info_rsp *rsp;
1120         int rc, used_len = 0;
1121
1122         if (smb3_encryption_required(tcon))
1123                 flags |= CIFS_TRANSFORM_REQ;
1124
1125         if (ea_name_len > 255)
1126                 return -EINVAL;
1127
1128         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1129         if (!utf16_path)
1130                 return -ENOMEM;
1131
1132         memset(rqst, 0, sizeof(rqst));
1133         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1134         memset(rsp_iov, 0, sizeof(rsp_iov));
1135
1136         if (ses->server->ops->query_all_EAs) {
1137                 if (!ea_value) {
1138                         rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1139                                                              ea_name, NULL, 0,
1140                                                              cifs_sb);
1141                         if (rc == -ENODATA)
1142                                 goto sea_exit;
1143                 } else {
1144                         /* If we are adding a attribute we should first check
1145                          * if there will be enough space available to store
1146                          * the new EA. If not we should not add it since we
1147                          * would not be able to even read the EAs back.
1148                          */
1149                         rc = smb2_query_info_compound(xid, tcon, utf16_path,
1150                                       FILE_READ_EA,
1151                                       FILE_FULL_EA_INFORMATION,
1152                                       SMB2_O_INFO_FILE,
1153                                       CIFSMaxBufSize -
1154                                       MAX_SMB2_CREATE_RESPONSE_SIZE -
1155                                       MAX_SMB2_CLOSE_RESPONSE_SIZE,
1156                                       &rsp_iov[1], &resp_buftype[1], cifs_sb);
1157                         if (rc == 0) {
1158                                 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1159                                 used_len = le32_to_cpu(rsp->OutputBufferLength);
1160                         }
1161                         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1162                         resp_buftype[1] = CIFS_NO_BUFFER;
1163                         memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1164                         rc = 0;
1165
1166                         /* Use a fudge factor of 256 bytes in case we collide
1167                          * with a different set_EAs command.
1168                          */
1169                         if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1170                            MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1171                            used_len + ea_name_len + ea_value_len + 1) {
1172                                 rc = -ENOSPC;
1173                                 goto sea_exit;
1174                         }
1175                 }
1176         }
1177
1178         /* Open */
1179         memset(&open_iov, 0, sizeof(open_iov));
1180         rqst[0].rq_iov = open_iov;
1181         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1182
1183         memset(&oparms, 0, sizeof(oparms));
1184         oparms.tcon = tcon;
1185         oparms.desired_access = FILE_WRITE_EA;
1186         oparms.disposition = FILE_OPEN;
1187         oparms.create_options = cifs_create_options(cifs_sb, 0);
1188         oparms.fid = &fid;
1189         oparms.reconnect = false;
1190
1191         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1192         if (rc)
1193                 goto sea_exit;
1194         smb2_set_next_command(tcon, &rqst[0]);
1195
1196
1197         /* Set Info */
1198         memset(&si_iov, 0, sizeof(si_iov));
1199         rqst[1].rq_iov = si_iov;
1200         rqst[1].rq_nvec = 1;
1201
1202         len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1203         ea = kzalloc(len, GFP_KERNEL);
1204         if (ea == NULL) {
1205                 rc = -ENOMEM;
1206                 goto sea_exit;
1207         }
1208
1209         ea->ea_name_length = ea_name_len;
1210         ea->ea_value_length = cpu_to_le16(ea_value_len);
1211         memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1212         memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1213
1214         size[0] = len;
1215         data[0] = ea;
1216
1217         rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1218                                 COMPOUND_FID, current->tgid,
1219                                 FILE_FULL_EA_INFORMATION,
1220                                 SMB2_O_INFO_FILE, 0, data, size);
1221         smb2_set_next_command(tcon, &rqst[1]);
1222         smb2_set_related(&rqst[1]);
1223
1224
1225         /* Close */
1226         memset(&close_iov, 0, sizeof(close_iov));
1227         rqst[2].rq_iov = close_iov;
1228         rqst[2].rq_nvec = 1;
1229         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1230         smb2_set_related(&rqst[2]);
1231
1232         rc = compound_send_recv(xid, ses, flags, 3, rqst,
1233                                 resp_buftype, rsp_iov);
1234         /* no need to bump num_remote_opens because handle immediately closed */
1235
1236  sea_exit:
1237         kfree(ea);
1238         kfree(utf16_path);
1239         SMB2_open_free(&rqst[0]);
1240         SMB2_set_info_free(&rqst[1]);
1241         SMB2_close_free(&rqst[2]);
1242         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1243         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1244         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1245         return rc;
1246 }
1247 #endif
1248
1249 static bool
1250 smb2_can_echo(struct TCP_Server_Info *server)
1251 {
1252         return server->echoes;
1253 }
1254
1255 static void
1256 smb2_clear_stats(struct cifs_tcon *tcon)
1257 {
1258         int i;
1259
1260         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1261                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1262                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1263         }
1264 }
1265
1266 static void
1267 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1268 {
1269         seq_puts(m, "\n\tShare Capabilities:");
1270         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1271                 seq_puts(m, " DFS,");
1272         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1273                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1274         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1275                 seq_puts(m, " SCALEOUT,");
1276         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1277                 seq_puts(m, " CLUSTER,");
1278         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1279                 seq_puts(m, " ASYMMETRIC,");
1280         if (tcon->capabilities == 0)
1281                 seq_puts(m, " None");
1282         if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1283                 seq_puts(m, " Aligned,");
1284         if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1285                 seq_puts(m, " Partition Aligned,");
1286         if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1287                 seq_puts(m, " SSD,");
1288         if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1289                 seq_puts(m, " TRIM-support,");
1290
1291         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1292         seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1293         if (tcon->perf_sector_size)
1294                 seq_printf(m, "\tOptimal sector size: 0x%x",
1295                            tcon->perf_sector_size);
1296         seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1297 }
1298
1299 static void
1300 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1301 {
1302         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1303         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1304
1305         /*
1306          *  Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1307          *  totals (requests sent) since those SMBs are per-session not per tcon
1308          */
1309         seq_printf(m, "\nBytes read: %llu  Bytes written: %llu",
1310                    (long long)(tcon->bytes_read),
1311                    (long long)(tcon->bytes_written));
1312         seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1313                    atomic_read(&tcon->num_local_opens),
1314                    atomic_read(&tcon->num_remote_opens));
1315         seq_printf(m, "\nTreeConnects: %d total %d failed",
1316                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1317                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1318         seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1319                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1320                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1321         seq_printf(m, "\nCreates: %d total %d failed",
1322                    atomic_read(&sent[SMB2_CREATE_HE]),
1323                    atomic_read(&failed[SMB2_CREATE_HE]));
1324         seq_printf(m, "\nCloses: %d total %d failed",
1325                    atomic_read(&sent[SMB2_CLOSE_HE]),
1326                    atomic_read(&failed[SMB2_CLOSE_HE]));
1327         seq_printf(m, "\nFlushes: %d total %d failed",
1328                    atomic_read(&sent[SMB2_FLUSH_HE]),
1329                    atomic_read(&failed[SMB2_FLUSH_HE]));
1330         seq_printf(m, "\nReads: %d total %d failed",
1331                    atomic_read(&sent[SMB2_READ_HE]),
1332                    atomic_read(&failed[SMB2_READ_HE]));
1333         seq_printf(m, "\nWrites: %d total %d failed",
1334                    atomic_read(&sent[SMB2_WRITE_HE]),
1335                    atomic_read(&failed[SMB2_WRITE_HE]));
1336         seq_printf(m, "\nLocks: %d total %d failed",
1337                    atomic_read(&sent[SMB2_LOCK_HE]),
1338                    atomic_read(&failed[SMB2_LOCK_HE]));
1339         seq_printf(m, "\nIOCTLs: %d total %d failed",
1340                    atomic_read(&sent[SMB2_IOCTL_HE]),
1341                    atomic_read(&failed[SMB2_IOCTL_HE]));
1342         seq_printf(m, "\nQueryDirectories: %d total %d failed",
1343                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1344                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1345         seq_printf(m, "\nChangeNotifies: %d total %d failed",
1346                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1347                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1348         seq_printf(m, "\nQueryInfos: %d total %d failed",
1349                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1350                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1351         seq_printf(m, "\nSetInfos: %d total %d failed",
1352                    atomic_read(&sent[SMB2_SET_INFO_HE]),
1353                    atomic_read(&failed[SMB2_SET_INFO_HE]));
1354         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1355                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1356                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1357 }
1358
1359 static void
1360 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1361 {
1362         struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1363         struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1364
1365         cfile->fid.persistent_fid = fid->persistent_fid;
1366         cfile->fid.volatile_fid = fid->volatile_fid;
1367         cfile->fid.access = fid->access;
1368 #ifdef CONFIG_CIFS_DEBUG2
1369         cfile->fid.mid = fid->mid;
1370 #endif /* CIFS_DEBUG2 */
1371         server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1372                                       &fid->purge_cache);
1373         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1374         memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1375 }
1376
1377 static void
1378 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1379                 struct cifs_fid *fid)
1380 {
1381         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1382 }
1383
1384 static void
1385 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1386                    struct cifsFileInfo *cfile)
1387 {
1388         struct smb2_file_network_open_info file_inf;
1389         struct inode *inode;
1390         int rc;
1391
1392         rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1393                    cfile->fid.volatile_fid, &file_inf);
1394         if (rc)
1395                 return;
1396
1397         inode = d_inode(cfile->dentry);
1398
1399         spin_lock(&inode->i_lock);
1400         CIFS_I(inode)->time = jiffies;
1401
1402         /* Creation time should not need to be updated on close */
1403         if (file_inf.LastWriteTime)
1404                 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1405         if (file_inf.ChangeTime)
1406                 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1407         if (file_inf.LastAccessTime)
1408                 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1409
1410         /*
1411          * i_blocks is not related to (i_size / i_blksize),
1412          * but instead 512 byte (2**9) size is required for
1413          * calculating num blocks.
1414          */
1415         if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1416                 inode->i_blocks =
1417                         (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1418
1419         /* End of file and Attributes should not have to be updated on close */
1420         spin_unlock(&inode->i_lock);
1421 }
1422
1423 static int
1424 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1425                      u64 persistent_fid, u64 volatile_fid,
1426                      struct copychunk_ioctl *pcchunk)
1427 {
1428         int rc;
1429         unsigned int ret_data_len;
1430         struct resume_key_req *res_key;
1431
1432         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1433                         FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1434                         NULL, 0 /* no input */, CIFSMaxBufSize,
1435                         (char **)&res_key, &ret_data_len);
1436
1437         if (rc) {
1438                 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1439                 goto req_res_key_exit;
1440         }
1441         if (ret_data_len < sizeof(struct resume_key_req)) {
1442                 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1443                 rc = -EINVAL;
1444                 goto req_res_key_exit;
1445         }
1446         memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1447
1448 req_res_key_exit:
1449         kfree(res_key);
1450         return rc;
1451 }
1452
1453 static int
1454 smb2_ioctl_query_info(const unsigned int xid,
1455                       struct cifs_tcon *tcon,
1456                       struct cifs_sb_info *cifs_sb,
1457                       __le16 *path, int is_dir,
1458                       unsigned long p)
1459 {
1460         struct cifs_ses *ses = tcon->ses;
1461         char __user *arg = (char __user *)p;
1462         struct smb_query_info qi;
1463         struct smb_query_info __user *pqi;
1464         int rc = 0;
1465         int flags = 0;
1466         struct smb2_query_info_rsp *qi_rsp = NULL;
1467         struct smb2_ioctl_rsp *io_rsp = NULL;
1468         void *buffer = NULL;
1469         struct smb_rqst rqst[3];
1470         int resp_buftype[3];
1471         struct kvec rsp_iov[3];
1472         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1473         struct cifs_open_parms oparms;
1474         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1475         struct cifs_fid fid;
1476         struct kvec qi_iov[1];
1477         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1478         struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1479         struct kvec close_iov[1];
1480         unsigned int size[2];
1481         void *data[2];
1482         int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1483
1484         memset(rqst, 0, sizeof(rqst));
1485         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1486         memset(rsp_iov, 0, sizeof(rsp_iov));
1487
1488         if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1489                 return -EFAULT;
1490
1491         if (qi.output_buffer_length > 1024)
1492                 return -EINVAL;
1493
1494         if (!ses || !(ses->server))
1495                 return -EIO;
1496
1497         if (smb3_encryption_required(tcon))
1498                 flags |= CIFS_TRANSFORM_REQ;
1499
1500         buffer = memdup_user(arg + sizeof(struct smb_query_info),
1501                              qi.output_buffer_length);
1502         if (IS_ERR(buffer))
1503                 return PTR_ERR(buffer);
1504
1505         /* Open */
1506         memset(&open_iov, 0, sizeof(open_iov));
1507         rqst[0].rq_iov = open_iov;
1508         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1509
1510         memset(&oparms, 0, sizeof(oparms));
1511         oparms.tcon = tcon;
1512         oparms.disposition = FILE_OPEN;
1513         oparms.create_options = cifs_create_options(cifs_sb, create_options);
1514         oparms.fid = &fid;
1515         oparms.reconnect = false;
1516
1517         if (qi.flags & PASSTHRU_FSCTL) {
1518                 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1519                 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1520                         oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1521                         break;
1522                 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1523                         oparms.desired_access = GENERIC_ALL;
1524                         break;
1525                 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1526                         oparms.desired_access = GENERIC_READ;
1527                         break;
1528                 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1529                         oparms.desired_access = GENERIC_WRITE;
1530                         break;
1531                 }
1532         } else if (qi.flags & PASSTHRU_SET_INFO) {
1533                 oparms.desired_access = GENERIC_WRITE;
1534         } else {
1535                 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1536         }
1537
1538         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1539         if (rc)
1540                 goto iqinf_exit;
1541         smb2_set_next_command(tcon, &rqst[0]);
1542
1543         /* Query */
1544         if (qi.flags & PASSTHRU_FSCTL) {
1545                 /* Can eventually relax perm check since server enforces too */
1546                 if (!capable(CAP_SYS_ADMIN))
1547                         rc = -EPERM;
1548                 else  {
1549                         memset(&io_iov, 0, sizeof(io_iov));
1550                         rqst[1].rq_iov = io_iov;
1551                         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1552
1553                         rc = SMB2_ioctl_init(tcon, &rqst[1],
1554                                              COMPOUND_FID, COMPOUND_FID,
1555                                              qi.info_type, true, buffer,
1556                                              qi.output_buffer_length,
1557                                              CIFSMaxBufSize -
1558                                              MAX_SMB2_CREATE_RESPONSE_SIZE -
1559                                              MAX_SMB2_CLOSE_RESPONSE_SIZE);
1560                 }
1561         } else if (qi.flags == PASSTHRU_SET_INFO) {
1562                 /* Can eventually relax perm check since server enforces too */
1563                 if (!capable(CAP_SYS_ADMIN))
1564                         rc = -EPERM;
1565                 else  {
1566                         memset(&si_iov, 0, sizeof(si_iov));
1567                         rqst[1].rq_iov = si_iov;
1568                         rqst[1].rq_nvec = 1;
1569
1570                         size[0] = 8;
1571                         data[0] = buffer;
1572
1573                         rc = SMB2_set_info_init(tcon, &rqst[1],
1574                                         COMPOUND_FID, COMPOUND_FID,
1575                                         current->tgid,
1576                                         FILE_END_OF_FILE_INFORMATION,
1577                                         SMB2_O_INFO_FILE, 0, data, size);
1578                 }
1579         } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1580                 memset(&qi_iov, 0, sizeof(qi_iov));
1581                 rqst[1].rq_iov = qi_iov;
1582                 rqst[1].rq_nvec = 1;
1583
1584                 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1585                                   COMPOUND_FID, qi.file_info_class,
1586                                   qi.info_type, qi.additional_information,
1587                                   qi.input_buffer_length,
1588                                   qi.output_buffer_length, buffer);
1589         } else { /* unknown flags */
1590                 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1591                 rc = -EINVAL;
1592         }
1593
1594         if (rc)
1595                 goto iqinf_exit;
1596         smb2_set_next_command(tcon, &rqst[1]);
1597         smb2_set_related(&rqst[1]);
1598
1599         /* Close */
1600         memset(&close_iov, 0, sizeof(close_iov));
1601         rqst[2].rq_iov = close_iov;
1602         rqst[2].rq_nvec = 1;
1603
1604         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1605         if (rc)
1606                 goto iqinf_exit;
1607         smb2_set_related(&rqst[2]);
1608
1609         rc = compound_send_recv(xid, ses, flags, 3, rqst,
1610                                 resp_buftype, rsp_iov);
1611         if (rc)
1612                 goto iqinf_exit;
1613
1614         /* No need to bump num_remote_opens since handle immediately closed */
1615         if (qi.flags & PASSTHRU_FSCTL) {
1616                 pqi = (struct smb_query_info __user *)arg;
1617                 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1618                 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1619                         qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1620                 if (qi.input_buffer_length > 0 &&
1621                     le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1622                     > rsp_iov[1].iov_len)
1623                         goto e_fault;
1624
1625                 if (copy_to_user(&pqi->input_buffer_length,
1626                                  &qi.input_buffer_length,
1627                                  sizeof(qi.input_buffer_length)))
1628                         goto e_fault;
1629
1630                 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1631                                  (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1632                                  qi.input_buffer_length))
1633                         goto e_fault;
1634         } else {
1635                 pqi = (struct smb_query_info __user *)arg;
1636                 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1637                 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1638                         qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1639                 if (copy_to_user(&pqi->input_buffer_length,
1640                                  &qi.input_buffer_length,
1641                                  sizeof(qi.input_buffer_length)))
1642                         goto e_fault;
1643
1644                 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1645                                  qi.input_buffer_length))
1646                         goto e_fault;
1647         }
1648
1649  iqinf_exit:
1650         kfree(buffer);
1651         SMB2_open_free(&rqst[0]);
1652         if (qi.flags & PASSTHRU_FSCTL)
1653                 SMB2_ioctl_free(&rqst[1]);
1654         else
1655                 SMB2_query_info_free(&rqst[1]);
1656
1657         SMB2_close_free(&rqst[2]);
1658         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1659         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1660         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1661         return rc;
1662
1663 e_fault:
1664         rc = -EFAULT;
1665         goto iqinf_exit;
1666 }
1667
1668 static ssize_t
1669 smb2_copychunk_range(const unsigned int xid,
1670                         struct cifsFileInfo *srcfile,
1671                         struct cifsFileInfo *trgtfile, u64 src_off,
1672                         u64 len, u64 dest_off)
1673 {
1674         int rc;
1675         unsigned int ret_data_len;
1676         struct copychunk_ioctl *pcchunk;
1677         struct copychunk_ioctl_rsp *retbuf = NULL;
1678         struct cifs_tcon *tcon;
1679         int chunks_copied = 0;
1680         bool chunk_sizes_updated = false;
1681         ssize_t bytes_written, total_bytes_written = 0;
1682
1683         pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1684
1685         if (pcchunk == NULL)
1686                 return -ENOMEM;
1687
1688         cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1689         /* Request a key from the server to identify the source of the copy */
1690         rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1691                                 srcfile->fid.persistent_fid,
1692                                 srcfile->fid.volatile_fid, pcchunk);
1693
1694         /* Note: request_res_key sets res_key null only if rc !=0 */
1695         if (rc)
1696                 goto cchunk_out;
1697
1698         /* For now array only one chunk long, will make more flexible later */
1699         pcchunk->ChunkCount = cpu_to_le32(1);
1700         pcchunk->Reserved = 0;
1701         pcchunk->Reserved2 = 0;
1702
1703         tcon = tlink_tcon(trgtfile->tlink);
1704
1705         while (len > 0) {
1706                 pcchunk->SourceOffset = cpu_to_le64(src_off);
1707                 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1708                 pcchunk->Length =
1709                         cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1710
1711                 /* Request server copy to target from src identified by key */
1712                 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1713                         trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1714                         true /* is_fsctl */, (char *)pcchunk,
1715                         sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1716                         (char **)&retbuf, &ret_data_len);
1717                 if (rc == 0) {
1718                         if (ret_data_len !=
1719                                         sizeof(struct copychunk_ioctl_rsp)) {
1720                                 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
1721                                 rc = -EIO;
1722                                 goto cchunk_out;
1723                         }
1724                         if (retbuf->TotalBytesWritten == 0) {
1725                                 cifs_dbg(FYI, "no bytes copied\n");
1726                                 rc = -EIO;
1727                                 goto cchunk_out;
1728                         }
1729                         /*
1730                          * Check if server claimed to write more than we asked
1731                          */
1732                         if (le32_to_cpu(retbuf->TotalBytesWritten) >
1733                             le32_to_cpu(pcchunk->Length)) {
1734                                 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
1735                                 rc = -EIO;
1736                                 goto cchunk_out;
1737                         }
1738                         if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1739                                 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
1740                                 rc = -EIO;
1741                                 goto cchunk_out;
1742                         }
1743                         chunks_copied++;
1744
1745                         bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1746                         src_off += bytes_written;
1747                         dest_off += bytes_written;
1748                         len -= bytes_written;
1749                         total_bytes_written += bytes_written;
1750
1751                         cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1752                                 le32_to_cpu(retbuf->ChunksWritten),
1753                                 le32_to_cpu(retbuf->ChunkBytesWritten),
1754                                 bytes_written);
1755                 } else if (rc == -EINVAL) {
1756                         if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1757                                 goto cchunk_out;
1758
1759                         cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1760                                 le32_to_cpu(retbuf->ChunksWritten),
1761                                 le32_to_cpu(retbuf->ChunkBytesWritten),
1762                                 le32_to_cpu(retbuf->TotalBytesWritten));
1763
1764                         /*
1765                          * Check if this is the first request using these sizes,
1766                          * (ie check if copy succeed once with original sizes
1767                          * and check if the server gave us different sizes after
1768                          * we already updated max sizes on previous request).
1769                          * if not then why is the server returning an error now
1770                          */
1771                         if ((chunks_copied != 0) || chunk_sizes_updated)
1772                                 goto cchunk_out;
1773
1774                         /* Check that server is not asking us to grow size */
1775                         if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1776                                         tcon->max_bytes_chunk)
1777                                 tcon->max_bytes_chunk =
1778                                         le32_to_cpu(retbuf->ChunkBytesWritten);
1779                         else
1780                                 goto cchunk_out; /* server gave us bogus size */
1781
1782                         /* No need to change MaxChunks since already set to 1 */
1783                         chunk_sizes_updated = true;
1784                 } else
1785                         goto cchunk_out;
1786         }
1787
1788 cchunk_out:
1789         kfree(pcchunk);
1790         kfree(retbuf);
1791         if (rc)
1792                 return rc;
1793         else
1794                 return total_bytes_written;
1795 }
1796
1797 static int
1798 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1799                 struct cifs_fid *fid)
1800 {
1801         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1802 }
1803
1804 static unsigned int
1805 smb2_read_data_offset(char *buf)
1806 {
1807         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1808
1809         return rsp->DataOffset;
1810 }
1811
1812 static unsigned int
1813 smb2_read_data_length(char *buf, bool in_remaining)
1814 {
1815         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1816
1817         if (in_remaining)
1818                 return le32_to_cpu(rsp->DataRemaining);
1819
1820         return le32_to_cpu(rsp->DataLength);
1821 }
1822
1823
1824 static int
1825 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1826                struct cifs_io_parms *parms, unsigned int *bytes_read,
1827                char **buf, int *buf_type)
1828 {
1829         parms->persistent_fid = pfid->persistent_fid;
1830         parms->volatile_fid = pfid->volatile_fid;
1831         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1832 }
1833
1834 static int
1835 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1836                 struct cifs_io_parms *parms, unsigned int *written,
1837                 struct kvec *iov, unsigned long nr_segs)
1838 {
1839
1840         parms->persistent_fid = pfid->persistent_fid;
1841         parms->volatile_fid = pfid->volatile_fid;
1842         return SMB2_write(xid, parms, written, iov, nr_segs);
1843 }
1844
1845 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1846 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1847                 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1848 {
1849         struct cifsInodeInfo *cifsi;
1850         int rc;
1851
1852         cifsi = CIFS_I(inode);
1853
1854         /* if file already sparse don't bother setting sparse again */
1855         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1856                 return true; /* already sparse */
1857
1858         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1859                 return true; /* already not sparse */
1860
1861         /*
1862          * Can't check for sparse support on share the usual way via the
1863          * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1864          * since Samba server doesn't set the flag on the share, yet
1865          * supports the set sparse FSCTL and returns sparse correctly
1866          * in the file attributes. If we fail setting sparse though we
1867          * mark that server does not support sparse files for this share
1868          * to avoid repeatedly sending the unsupported fsctl to server
1869          * if the file is repeatedly extended.
1870          */
1871         if (tcon->broken_sparse_sup)
1872                 return false;
1873
1874         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1875                         cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1876                         true /* is_fctl */,
1877                         &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1878         if (rc) {
1879                 tcon->broken_sparse_sup = true;
1880                 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1881                 return false;
1882         }
1883
1884         if (setsparse)
1885                 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1886         else
1887                 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1888
1889         return true;
1890 }
1891
1892 static int
1893 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1894                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1895 {
1896         __le64 eof = cpu_to_le64(size);
1897         struct inode *inode;
1898
1899         /*
1900          * If extending file more than one page make sparse. Many Linux fs
1901          * make files sparse by default when extending via ftruncate
1902          */
1903         inode = d_inode(cfile->dentry);
1904
1905         if (!set_alloc && (size > inode->i_size + 8192)) {
1906                 __u8 set_sparse = 1;
1907
1908                 /* whether set sparse succeeds or not, extend the file */
1909                 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1910         }
1911
1912         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1913                             cfile->fid.volatile_fid, cfile->pid, &eof);
1914 }
1915
1916 static int
1917 smb2_duplicate_extents(const unsigned int xid,
1918                         struct cifsFileInfo *srcfile,
1919                         struct cifsFileInfo *trgtfile, u64 src_off,
1920                         u64 len, u64 dest_off)
1921 {
1922         int rc;
1923         unsigned int ret_data_len;
1924         struct duplicate_extents_to_file dup_ext_buf;
1925         struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1926
1927         /* server fileays advertise duplicate extent support with this flag */
1928         if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1929              FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1930                 return -EOPNOTSUPP;
1931
1932         dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1933         dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1934         dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1935         dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1936         dup_ext_buf.ByteCount = cpu_to_le64(len);
1937         cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1938                 src_off, dest_off, len);
1939
1940         rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1941         if (rc)
1942                 goto duplicate_extents_out;
1943
1944         rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1945                         trgtfile->fid.volatile_fid,
1946                         FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1947                         true /* is_fsctl */,
1948                         (char *)&dup_ext_buf,
1949                         sizeof(struct duplicate_extents_to_file),
1950                         CIFSMaxBufSize, NULL,
1951                         &ret_data_len);
1952
1953         if (ret_data_len > 0)
1954                 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1955
1956 duplicate_extents_out:
1957         return rc;
1958 }
1959
1960 static int
1961 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1962                    struct cifsFileInfo *cfile)
1963 {
1964         return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1965                             cfile->fid.volatile_fid);
1966 }
1967
1968 static int
1969 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1970                    struct cifsFileInfo *cfile)
1971 {
1972         struct fsctl_set_integrity_information_req integr_info;
1973         unsigned int ret_data_len;
1974
1975         integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1976         integr_info.Flags = 0;
1977         integr_info.Reserved = 0;
1978
1979         return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1980                         cfile->fid.volatile_fid,
1981                         FSCTL_SET_INTEGRITY_INFORMATION,
1982                         true /* is_fsctl */,
1983                         (char *)&integr_info,
1984                         sizeof(struct fsctl_set_integrity_information_req),
1985                         CIFSMaxBufSize, NULL,
1986                         &ret_data_len);
1987
1988 }
1989
1990 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1991 #define GMT_TOKEN_SIZE 50
1992
1993 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1994
1995 /*
1996  * Input buffer contains (empty) struct smb_snapshot array with size filled in
1997  * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1998  */
1999 static int
2000 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2001                    struct cifsFileInfo *cfile, void __user *ioc_buf)
2002 {
2003         char *retbuf = NULL;
2004         unsigned int ret_data_len = 0;
2005         int rc;
2006         u32 max_response_size;
2007         struct smb_snapshot_array snapshot_in;
2008
2009         /*
2010          * On the first query to enumerate the list of snapshots available
2011          * for this volume the buffer begins with 0 (number of snapshots
2012          * which can be returned is zero since at that point we do not know
2013          * how big the buffer needs to be). On the second query,
2014          * it (ret_data_len) is set to number of snapshots so we can
2015          * know to set the maximum response size larger (see below).
2016          */
2017         if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2018                 return -EFAULT;
2019
2020         /*
2021          * Note that for snapshot queries that servers like Azure expect that
2022          * the first query be minimal size (and just used to get the number/size
2023          * of previous versions) so response size must be specified as EXACTLY
2024          * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2025          * of eight bytes.
2026          */
2027         if (ret_data_len == 0)
2028                 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2029         else
2030                 max_response_size = CIFSMaxBufSize;
2031
2032         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2033                         cfile->fid.volatile_fid,
2034                         FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2035                         true /* is_fsctl */,
2036                         NULL, 0 /* no input data */, max_response_size,
2037                         (char **)&retbuf,
2038                         &ret_data_len);
2039         cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2040                         rc, ret_data_len);
2041         if (rc)
2042                 return rc;
2043
2044         if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2045                 /* Fixup buffer */
2046                 if (copy_from_user(&snapshot_in, ioc_buf,
2047                     sizeof(struct smb_snapshot_array))) {
2048                         rc = -EFAULT;
2049                         kfree(retbuf);
2050                         return rc;
2051                 }
2052
2053                 /*
2054                  * Check for min size, ie not large enough to fit even one GMT
2055                  * token (snapshot).  On the first ioctl some users may pass in
2056                  * smaller size (or zero) to simply get the size of the array
2057                  * so the user space caller can allocate sufficient memory
2058                  * and retry the ioctl again with larger array size sufficient
2059                  * to hold all of the snapshot GMT tokens on the second try.
2060                  */
2061                 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2062                         ret_data_len = sizeof(struct smb_snapshot_array);
2063
2064                 /*
2065                  * We return struct SRV_SNAPSHOT_ARRAY, followed by
2066                  * the snapshot array (of 50 byte GMT tokens) each
2067                  * representing an available previous version of the data
2068                  */
2069                 if (ret_data_len > (snapshot_in.snapshot_array_size +
2070                                         sizeof(struct smb_snapshot_array)))
2071                         ret_data_len = snapshot_in.snapshot_array_size +
2072                                         sizeof(struct smb_snapshot_array);
2073
2074                 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2075                         rc = -EFAULT;
2076         }
2077
2078         kfree(retbuf);
2079         return rc;
2080 }
2081
2082
2083
2084 static int
2085 smb3_notify(const unsigned int xid, struct file *pfile,
2086             void __user *ioc_buf)
2087 {
2088         struct smb3_notify notify;
2089         struct dentry *dentry = pfile->f_path.dentry;
2090         struct inode *inode = file_inode(pfile);
2091         struct cifs_sb_info *cifs_sb;
2092         struct cifs_open_parms oparms;
2093         struct cifs_fid fid;
2094         struct cifs_tcon *tcon;
2095         unsigned char *path = NULL;
2096         __le16 *utf16_path = NULL;
2097         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2098         int rc = 0;
2099
2100         path = build_path_from_dentry(dentry);
2101         if (path == NULL)
2102                 return -ENOMEM;
2103
2104         cifs_sb = CIFS_SB(inode->i_sb);
2105
2106         utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2107         if (utf16_path == NULL) {
2108                 rc = -ENOMEM;
2109                 goto notify_exit;
2110         }
2111
2112         if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2113                 rc = -EFAULT;
2114                 goto notify_exit;
2115         }
2116
2117         tcon = cifs_sb_master_tcon(cifs_sb);
2118         oparms.tcon = tcon;
2119         oparms.desired_access = FILE_READ_ATTRIBUTES;
2120         oparms.disposition = FILE_OPEN;
2121         oparms.create_options = cifs_create_options(cifs_sb, 0);
2122         oparms.fid = &fid;
2123         oparms.reconnect = false;
2124
2125         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2126         if (rc)
2127                 goto notify_exit;
2128
2129         rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2130                                 notify.watch_tree, notify.completion_filter);
2131
2132         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2133
2134         cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2135
2136 notify_exit:
2137         kfree(path);
2138         kfree(utf16_path);
2139         return rc;
2140 }
2141
2142 static int
2143 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2144                      const char *path, struct cifs_sb_info *cifs_sb,
2145                      struct cifs_fid *fid, __u16 search_flags,
2146                      struct cifs_search_info *srch_inf)
2147 {
2148         __le16 *utf16_path;
2149         struct smb_rqst rqst[2];
2150         struct kvec rsp_iov[2];
2151         int resp_buftype[2];
2152         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2153         struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2154         int rc, flags = 0;
2155         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2156         struct cifs_open_parms oparms;
2157         struct smb2_query_directory_rsp *qd_rsp = NULL;
2158         struct smb2_create_rsp *op_rsp = NULL;
2159
2160         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2161         if (!utf16_path)
2162                 return -ENOMEM;
2163
2164         if (smb3_encryption_required(tcon))
2165                 flags |= CIFS_TRANSFORM_REQ;
2166
2167         memset(rqst, 0, sizeof(rqst));
2168         resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2169         memset(rsp_iov, 0, sizeof(rsp_iov));
2170
2171         /* Open */
2172         memset(&open_iov, 0, sizeof(open_iov));
2173         rqst[0].rq_iov = open_iov;
2174         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2175
2176         oparms.tcon = tcon;
2177         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2178         oparms.disposition = FILE_OPEN;
2179         oparms.create_options = cifs_create_options(cifs_sb, 0);
2180         oparms.fid = fid;
2181         oparms.reconnect = false;
2182
2183         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2184         if (rc)
2185                 goto qdf_free;
2186         smb2_set_next_command(tcon, &rqst[0]);
2187
2188         /* Query directory */
2189         srch_inf->entries_in_buffer = 0;
2190         srch_inf->index_of_last_entry = 2;
2191
2192         memset(&qd_iov, 0, sizeof(qd_iov));
2193         rqst[1].rq_iov = qd_iov;
2194         rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2195
2196         rc = SMB2_query_directory_init(xid, tcon, &rqst[1],
2197                                        COMPOUND_FID, COMPOUND_FID,
2198                                        0, srch_inf->info_level);
2199         if (rc)
2200                 goto qdf_free;
2201
2202         smb2_set_related(&rqst[1]);
2203
2204         rc = compound_send_recv(xid, tcon->ses, flags, 2, rqst,
2205                                 resp_buftype, rsp_iov);
2206
2207         /* If the open failed there is nothing to do */
2208         op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2209         if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2210                 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2211                 goto qdf_free;
2212         }
2213         fid->persistent_fid = op_rsp->PersistentFileId;
2214         fid->volatile_fid = op_rsp->VolatileFileId;
2215
2216         /* Anything else than ENODATA means a genuine error */
2217         if (rc && rc != -ENODATA) {
2218                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2219                 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2220                 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2221                                          tcon->tid, tcon->ses->Suid, 0, 0, rc);
2222                 goto qdf_free;
2223         }
2224
2225         qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2226         if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2227                 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2228                                           tcon->tid, tcon->ses->Suid, 0, 0);
2229                 srch_inf->endOfSearch = true;
2230                 rc = 0;
2231                 goto qdf_free;
2232         }
2233
2234         rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2235                                         srch_inf);
2236         if (rc) {
2237                 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2238                         tcon->ses->Suid, 0, 0, rc);
2239                 goto qdf_free;
2240         }
2241         resp_buftype[1] = CIFS_NO_BUFFER;
2242
2243         trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2244                         tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2245
2246  qdf_free:
2247         kfree(utf16_path);
2248         SMB2_open_free(&rqst[0]);
2249         SMB2_query_directory_free(&rqst[1]);
2250         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2251         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2252         return rc;
2253 }
2254
2255 static int
2256 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2257                     struct cifs_fid *fid, __u16 search_flags,
2258                     struct cifs_search_info *srch_inf)
2259 {
2260         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2261                                     fid->volatile_fid, 0, srch_inf);
2262 }
2263
2264 static int
2265 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2266                struct cifs_fid *fid)
2267 {
2268         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2269 }
2270
2271 /*
2272  * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2273  * the number of credits and return true. Otherwise - return false.
2274  */
2275 static bool
2276 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2277 {
2278         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2279
2280         if (shdr->Status != STATUS_PENDING)
2281                 return false;
2282
2283         if (shdr->CreditRequest) {
2284                 spin_lock(&server->req_lock);
2285                 server->credits += le16_to_cpu(shdr->CreditRequest);
2286                 spin_unlock(&server->req_lock);
2287                 wake_up(&server->request_q);
2288         }
2289
2290         return true;
2291 }
2292
2293 static bool
2294 smb2_is_session_expired(char *buf)
2295 {
2296         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2297
2298         if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2299             shdr->Status != STATUS_USER_SESSION_DELETED)
2300                 return false;
2301
2302         trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2303                                le16_to_cpu(shdr->Command),
2304                                le64_to_cpu(shdr->MessageId));
2305         cifs_dbg(FYI, "Session expired or deleted\n");
2306
2307         return true;
2308 }
2309
2310 static int
2311 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2312                      struct cifsInodeInfo *cinode)
2313 {
2314         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2315                 return SMB2_lease_break(0, tcon, cinode->lease_key,
2316                                         smb2_get_lease_state(cinode));
2317
2318         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2319                                  fid->volatile_fid,
2320                                  CIFS_CACHE_READ(cinode) ? 1 : 0);
2321 }
2322
2323 void
2324 smb2_set_related(struct smb_rqst *rqst)
2325 {
2326         struct smb2_sync_hdr *shdr;
2327
2328         shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2329         if (shdr == NULL) {
2330                 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2331                 return;
2332         }
2333         shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2334 }
2335
2336 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2337
2338 void
2339 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2340 {
2341         struct smb2_sync_hdr *shdr;
2342         struct cifs_ses *ses = tcon->ses;
2343         struct TCP_Server_Info *server = ses->server;
2344         unsigned long len = smb_rqst_len(server, rqst);
2345         int i, num_padding;
2346
2347         shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2348         if (shdr == NULL) {
2349                 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2350                 return;
2351         }
2352
2353         /* SMB headers in a compound are 8 byte aligned. */
2354
2355         /* No padding needed */
2356         if (!(len & 7))
2357                 goto finished;
2358
2359         num_padding = 8 - (len & 7);
2360         if (!smb3_encryption_required(tcon)) {
2361                 /*
2362                  * If we do not have encryption then we can just add an extra
2363                  * iov for the padding.
2364                  */
2365                 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2366                 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2367                 rqst->rq_nvec++;
2368                 len += num_padding;
2369         } else {
2370                 /*
2371                  * We can not add a small padding iov for the encryption case
2372                  * because the encryption framework can not handle the padding
2373                  * iovs.
2374                  * We have to flatten this into a single buffer and add
2375                  * the padding to it.
2376                  */
2377                 for (i = 1; i < rqst->rq_nvec; i++) {
2378                         memcpy(rqst->rq_iov[0].iov_base +
2379                                rqst->rq_iov[0].iov_len,
2380                                rqst->rq_iov[i].iov_base,
2381                                rqst->rq_iov[i].iov_len);
2382                         rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2383                 }
2384                 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2385                        0, num_padding);
2386                 rqst->rq_iov[0].iov_len += num_padding;
2387                 len += num_padding;
2388                 rqst->rq_nvec = 1;
2389         }
2390
2391  finished:
2392         shdr->NextCommand = cpu_to_le32(len);
2393 }
2394
2395 /*
2396  * Passes the query info response back to the caller on success.
2397  * Caller need to free this with free_rsp_buf().
2398  */
2399 int
2400 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2401                          __le16 *utf16_path, u32 desired_access,
2402                          u32 class, u32 type, u32 output_len,
2403                          struct kvec *rsp, int *buftype,
2404                          struct cifs_sb_info *cifs_sb)
2405 {
2406         struct cifs_ses *ses = tcon->ses;
2407         int flags = 0;
2408         struct smb_rqst rqst[3];
2409         int resp_buftype[3];
2410         struct kvec rsp_iov[3];
2411         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2412         struct kvec qi_iov[1];
2413         struct kvec close_iov[1];
2414         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2415         struct cifs_open_parms oparms;
2416         struct cifs_fid fid;
2417         int rc;
2418
2419         if (smb3_encryption_required(tcon))
2420                 flags |= CIFS_TRANSFORM_REQ;
2421
2422         memset(rqst, 0, sizeof(rqst));
2423         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2424         memset(rsp_iov, 0, sizeof(rsp_iov));
2425
2426         memset(&open_iov, 0, sizeof(open_iov));
2427         rqst[0].rq_iov = open_iov;
2428         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2429
2430         oparms.tcon = tcon;
2431         oparms.desired_access = desired_access;
2432         oparms.disposition = FILE_OPEN;
2433         oparms.create_options = cifs_create_options(cifs_sb, 0);
2434         oparms.fid = &fid;
2435         oparms.reconnect = false;
2436
2437         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2438         if (rc)
2439                 goto qic_exit;
2440         smb2_set_next_command(tcon, &rqst[0]);
2441
2442         memset(&qi_iov, 0, sizeof(qi_iov));
2443         rqst[1].rq_iov = qi_iov;
2444         rqst[1].rq_nvec = 1;
2445
2446         rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2447                                   class, type, 0,
2448                                   output_len, 0,
2449                                   NULL);
2450         if (rc)
2451                 goto qic_exit;
2452         smb2_set_next_command(tcon, &rqst[1]);
2453         smb2_set_related(&rqst[1]);
2454
2455         memset(&close_iov, 0, sizeof(close_iov));
2456         rqst[2].rq_iov = close_iov;
2457         rqst[2].rq_nvec = 1;
2458
2459         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2460         if (rc)
2461                 goto qic_exit;
2462         smb2_set_related(&rqst[2]);
2463
2464         rc = compound_send_recv(xid, ses, flags, 3, rqst,
2465                                 resp_buftype, rsp_iov);
2466         if (rc) {
2467                 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2468                 if (rc == -EREMCHG) {
2469                         tcon->need_reconnect = true;
2470                         printk_once(KERN_WARNING "server share %s deleted\n",
2471                                     tcon->treeName);
2472                 }
2473                 goto qic_exit;
2474         }
2475         *rsp = rsp_iov[1];
2476         *buftype = resp_buftype[1];
2477
2478  qic_exit:
2479         SMB2_open_free(&rqst[0]);
2480         SMB2_query_info_free(&rqst[1]);
2481         SMB2_close_free(&rqst[2]);
2482         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2483         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2484         return rc;
2485 }
2486
2487 static int
2488 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2489              struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2490 {
2491         struct smb2_query_info_rsp *rsp;
2492         struct smb2_fs_full_size_info *info = NULL;
2493         __le16 utf16_path = 0; /* Null - open root of share */
2494         struct kvec rsp_iov = {NULL, 0};
2495         int buftype = CIFS_NO_BUFFER;
2496         int rc;
2497
2498
2499         rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2500                                       FILE_READ_ATTRIBUTES,
2501                                       FS_FULL_SIZE_INFORMATION,
2502                                       SMB2_O_INFO_FILESYSTEM,
2503                                       sizeof(struct smb2_fs_full_size_info),
2504                                       &rsp_iov, &buftype, cifs_sb);
2505         if (rc)
2506                 goto qfs_exit;
2507
2508         rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2509         buf->f_type = SMB2_MAGIC_NUMBER;
2510         info = (struct smb2_fs_full_size_info *)(
2511                 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2512         rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2513                                le32_to_cpu(rsp->OutputBufferLength),
2514                                &rsp_iov,
2515                                sizeof(struct smb2_fs_full_size_info));
2516         if (!rc)
2517                 smb2_copy_fs_info_to_kstatfs(info, buf);
2518
2519 qfs_exit:
2520         free_rsp_buf(buftype, rsp_iov.iov_base);
2521         return rc;
2522 }
2523
2524 static int
2525 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2526                struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2527 {
2528         int rc;
2529         __le16 srch_path = 0; /* Null - open root of share */
2530         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2531         struct cifs_open_parms oparms;
2532         struct cifs_fid fid;
2533
2534         if (!tcon->posix_extensions)
2535                 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2536
2537         oparms.tcon = tcon;
2538         oparms.desired_access = FILE_READ_ATTRIBUTES;
2539         oparms.disposition = FILE_OPEN;
2540         oparms.create_options = cifs_create_options(cifs_sb, 0);
2541         oparms.fid = &fid;
2542         oparms.reconnect = false;
2543
2544         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2545         if (rc)
2546                 return rc;
2547
2548         rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2549                                    fid.volatile_fid, buf);
2550         buf->f_type = SMB2_MAGIC_NUMBER;
2551         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2552         return rc;
2553 }
2554
2555 static bool
2556 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2557 {
2558         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2559                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2560 }
2561
2562 static int
2563 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2564                __u64 length, __u32 type, int lock, int unlock, bool wait)
2565 {
2566         if (unlock && !lock)
2567                 type = SMB2_LOCKFLAG_UNLOCK;
2568         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2569                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2570                          current->tgid, length, offset, type, wait);
2571 }
2572
2573 static void
2574 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2575 {
2576         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2577 }
2578
2579 static void
2580 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2581 {
2582         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2583 }
2584
2585 static void
2586 smb2_new_lease_key(struct cifs_fid *fid)
2587 {
2588         generate_random_uuid(fid->lease_key);
2589 }
2590
2591 static int
2592 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2593                    const char *search_name,
2594                    struct dfs_info3_param **target_nodes,
2595                    unsigned int *num_of_nodes,
2596                    const struct nls_table *nls_codepage, int remap)
2597 {
2598         int rc;
2599         __le16 *utf16_path = NULL;
2600         int utf16_path_len = 0;
2601         struct cifs_tcon *tcon;
2602         struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2603         struct get_dfs_referral_rsp *dfs_rsp = NULL;
2604         u32 dfs_req_size = 0, dfs_rsp_size = 0;
2605
2606         cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2607
2608         /*
2609          * Try to use the IPC tcon, otherwise just use any
2610          */
2611         tcon = ses->tcon_ipc;
2612         if (tcon == NULL) {
2613                 spin_lock(&cifs_tcp_ses_lock);
2614                 tcon = list_first_entry_or_null(&ses->tcon_list,
2615                                                 struct cifs_tcon,
2616                                                 tcon_list);
2617                 if (tcon)
2618                         tcon->tc_count++;
2619                 spin_unlock(&cifs_tcp_ses_lock);
2620         }
2621
2622         if (tcon == NULL) {
2623                 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2624                          ses);
2625                 rc = -ENOTCONN;
2626                 goto out;
2627         }
2628
2629         utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2630                                            &utf16_path_len,
2631                                            nls_codepage, remap);
2632         if (!utf16_path) {
2633                 rc = -ENOMEM;
2634                 goto out;
2635         }
2636
2637         dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2638         dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2639         if (!dfs_req) {
2640                 rc = -ENOMEM;
2641                 goto out;
2642         }
2643
2644         /* Highest DFS referral version understood */
2645         dfs_req->MaxReferralLevel = DFS_VERSION;
2646
2647         /* Path to resolve in an UTF-16 null-terminated string */
2648         memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2649
2650         do {
2651                 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2652                                 FSCTL_DFS_GET_REFERRALS,
2653                                 true /* is_fsctl */,
2654                                 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2655                                 (char **)&dfs_rsp, &dfs_rsp_size);
2656         } while (rc == -EAGAIN);
2657
2658         if (rc) {
2659                 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2660                         cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2661                 goto out;
2662         }
2663
2664         rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2665                                  num_of_nodes, target_nodes,
2666                                  nls_codepage, remap, search_name,
2667                                  true /* is_unicode */);
2668         if (rc) {
2669                 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2670                 goto out;
2671         }
2672
2673  out:
2674         if (tcon && !tcon->ipc) {
2675                 /* ipc tcons are not refcounted */
2676                 spin_lock(&cifs_tcp_ses_lock);
2677                 tcon->tc_count--;
2678                 spin_unlock(&cifs_tcp_ses_lock);
2679         }
2680         kfree(utf16_path);
2681         kfree(dfs_req);
2682         kfree(dfs_rsp);
2683         return rc;
2684 }
2685
2686 static int
2687 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2688                       u32 plen, char **target_path,
2689                       struct cifs_sb_info *cifs_sb)
2690 {
2691         unsigned int len;
2692
2693         /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2694         len = le16_to_cpu(symlink_buf->ReparseDataLength);
2695
2696         if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2697                 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2698                         le64_to_cpu(symlink_buf->InodeType));
2699                 return -EOPNOTSUPP;
2700         }
2701
2702         *target_path = cifs_strndup_from_utf16(
2703                                 symlink_buf->PathBuffer,
2704                                 len, true, cifs_sb->local_nls);
2705         if (!(*target_path))
2706                 return -ENOMEM;
2707
2708         convert_delimiter(*target_path, '/');
2709         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2710
2711         return 0;
2712 }
2713
2714 static int
2715 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2716                       u32 plen, char **target_path,
2717                       struct cifs_sb_info *cifs_sb)
2718 {
2719         unsigned int sub_len;
2720         unsigned int sub_offset;
2721
2722         /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2723
2724         sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2725         sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2726         if (sub_offset + 20 > plen ||
2727             sub_offset + sub_len + 20 > plen) {
2728                 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2729                 return -EIO;
2730         }
2731
2732         *target_path = cifs_strndup_from_utf16(
2733                                 symlink_buf->PathBuffer + sub_offset,
2734                                 sub_len, true, cifs_sb->local_nls);
2735         if (!(*target_path))
2736                 return -ENOMEM;
2737
2738         convert_delimiter(*target_path, '/');
2739         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2740
2741         return 0;
2742 }
2743
2744 static int
2745 parse_reparse_point(struct reparse_data_buffer *buf,
2746                     u32 plen, char **target_path,
2747                     struct cifs_sb_info *cifs_sb)
2748 {
2749         if (plen < sizeof(struct reparse_data_buffer)) {
2750                 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2751                          "at least 8 bytes but was %d\n", plen);
2752                 return -EIO;
2753         }
2754
2755         if (plen < le16_to_cpu(buf->ReparseDataLength) +
2756             sizeof(struct reparse_data_buffer)) {
2757                 cifs_dbg(VFS, "srv returned invalid reparse buf "
2758                          "length: %d\n", plen);
2759                 return -EIO;
2760         }
2761
2762         /* See MS-FSCC 2.1.2 */
2763         switch (le32_to_cpu(buf->ReparseTag)) {
2764         case IO_REPARSE_TAG_NFS:
2765                 return parse_reparse_posix(
2766                         (struct reparse_posix_data *)buf,
2767                         plen, target_path, cifs_sb);
2768         case IO_REPARSE_TAG_SYMLINK:
2769                 return parse_reparse_symlink(
2770                         (struct reparse_symlink_data_buffer *)buf,
2771                         plen, target_path, cifs_sb);
2772         default:
2773                 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2774                          "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2775                 return -EOPNOTSUPP;
2776         }
2777 }
2778
2779 #define SMB2_SYMLINK_STRUCT_SIZE \
2780         (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2781
2782 static int
2783 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2784                    struct cifs_sb_info *cifs_sb, const char *full_path,
2785                    char **target_path, bool is_reparse_point)
2786 {
2787         int rc;
2788         __le16 *utf16_path = NULL;
2789         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2790         struct cifs_open_parms oparms;
2791         struct cifs_fid fid;
2792         struct kvec err_iov = {NULL, 0};
2793         struct smb2_err_rsp *err_buf = NULL;
2794         struct smb2_symlink_err_rsp *symlink;
2795         unsigned int sub_len;
2796         unsigned int sub_offset;
2797         unsigned int print_len;
2798         unsigned int print_offset;
2799         int flags = 0;
2800         struct smb_rqst rqst[3];
2801         int resp_buftype[3];
2802         struct kvec rsp_iov[3];
2803         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2804         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2805         struct kvec close_iov[1];
2806         struct smb2_create_rsp *create_rsp;
2807         struct smb2_ioctl_rsp *ioctl_rsp;
2808         struct reparse_data_buffer *reparse_buf;
2809         int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2810         u32 plen;
2811
2812         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2813
2814         *target_path = NULL;
2815
2816         if (smb3_encryption_required(tcon))
2817                 flags |= CIFS_TRANSFORM_REQ;
2818
2819         memset(rqst, 0, sizeof(rqst));
2820         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2821         memset(rsp_iov, 0, sizeof(rsp_iov));
2822
2823         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2824         if (!utf16_path)
2825                 return -ENOMEM;
2826
2827         /* Open */
2828         memset(&open_iov, 0, sizeof(open_iov));
2829         rqst[0].rq_iov = open_iov;
2830         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2831
2832         memset(&oparms, 0, sizeof(oparms));
2833         oparms.tcon = tcon;
2834         oparms.desired_access = FILE_READ_ATTRIBUTES;
2835         oparms.disposition = FILE_OPEN;
2836         oparms.create_options = cifs_create_options(cifs_sb, create_options);
2837         oparms.fid = &fid;
2838         oparms.reconnect = false;
2839
2840         rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2841         if (rc)
2842                 goto querty_exit;
2843         smb2_set_next_command(tcon, &rqst[0]);
2844
2845
2846         /* IOCTL */
2847         memset(&io_iov, 0, sizeof(io_iov));
2848         rqst[1].rq_iov = io_iov;
2849         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2850
2851         rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2852                              fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2853                              true /* is_fctl */, NULL, 0,
2854                              CIFSMaxBufSize -
2855                              MAX_SMB2_CREATE_RESPONSE_SIZE -
2856                              MAX_SMB2_CLOSE_RESPONSE_SIZE);
2857         if (rc)
2858                 goto querty_exit;
2859
2860         smb2_set_next_command(tcon, &rqst[1]);
2861         smb2_set_related(&rqst[1]);
2862
2863
2864         /* Close */
2865         memset(&close_iov, 0, sizeof(close_iov));
2866         rqst[2].rq_iov = close_iov;
2867         rqst[2].rq_nvec = 1;
2868
2869         rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2870         if (rc)
2871                 goto querty_exit;
2872
2873         smb2_set_related(&rqst[2]);
2874
2875         rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2876                                 resp_buftype, rsp_iov);
2877
2878         create_rsp = rsp_iov[0].iov_base;
2879         if (create_rsp && create_rsp->sync_hdr.Status)
2880                 err_iov = rsp_iov[0];
2881         ioctl_rsp = rsp_iov[1].iov_base;
2882
2883         /*
2884          * Open was successful and we got an ioctl response.
2885          */
2886         if ((rc == 0) && (is_reparse_point)) {
2887                 /* See MS-FSCC 2.3.23 */
2888
2889                 reparse_buf = (struct reparse_data_buffer *)
2890                         ((char *)ioctl_rsp +
2891                          le32_to_cpu(ioctl_rsp->OutputOffset));
2892                 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2893
2894                 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2895                     rsp_iov[1].iov_len) {
2896                         cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2897                                  plen);
2898                         rc = -EIO;
2899                         goto querty_exit;
2900                 }
2901
2902                 rc = parse_reparse_point(reparse_buf, plen, target_path,
2903                                          cifs_sb);
2904                 goto querty_exit;
2905         }
2906
2907         if (!rc || !err_iov.iov_base) {
2908                 rc = -ENOENT;
2909                 goto querty_exit;
2910         }
2911
2912         err_buf = err_iov.iov_base;
2913         if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2914             err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2915                 rc = -EINVAL;
2916                 goto querty_exit;
2917         }
2918
2919         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2920         if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2921             le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2922                 rc = -EINVAL;
2923                 goto querty_exit;
2924         }
2925
2926         /* open must fail on symlink - reset rc */
2927         rc = 0;
2928         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2929         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2930         print_len = le16_to_cpu(symlink->PrintNameLength);
2931         print_offset = le16_to_cpu(symlink->PrintNameOffset);
2932
2933         if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2934                 rc = -EINVAL;
2935                 goto querty_exit;
2936         }
2937
2938         if (err_iov.iov_len <
2939             SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2940                 rc = -EINVAL;
2941                 goto querty_exit;
2942         }
2943
2944         *target_path = cifs_strndup_from_utf16(
2945                                 (char *)symlink->PathBuffer + sub_offset,
2946                                 sub_len, true, cifs_sb->local_nls);
2947         if (!(*target_path)) {
2948                 rc = -ENOMEM;
2949                 goto querty_exit;
2950         }
2951         convert_delimiter(*target_path, '/');
2952         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2953
2954  querty_exit:
2955         cifs_dbg(FYI, "query symlink rc %d\n", rc);
2956         kfree(utf16_path);
2957         SMB2_open_free(&rqst[0]);
2958         SMB2_ioctl_free(&rqst[1]);
2959         SMB2_close_free(&rqst[2]);
2960         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2961         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2962         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2963         return rc;
2964 }
2965
2966 static struct cifs_ntsd *
2967 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2968                 const struct cifs_fid *cifsfid, u32 *pacllen)
2969 {
2970         struct cifs_ntsd *pntsd = NULL;
2971         unsigned int xid;
2972         int rc = -EOPNOTSUPP;
2973         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2974
2975         if (IS_ERR(tlink))
2976                 return ERR_CAST(tlink);
2977
2978         xid = get_xid();
2979         cifs_dbg(FYI, "trying to get acl\n");
2980
2981         rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2982                             cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2983         free_xid(xid);
2984
2985         cifs_put_tlink(tlink);
2986
2987         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2988         if (rc)
2989                 return ERR_PTR(rc);
2990         return pntsd;
2991
2992 }
2993
2994 static struct cifs_ntsd *
2995 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2996                 const char *path, u32 *pacllen)
2997 {
2998         struct cifs_ntsd *pntsd = NULL;
2999         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3000         unsigned int xid;
3001         int rc;
3002         struct cifs_tcon *tcon;
3003         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3004         struct cifs_fid fid;
3005         struct cifs_open_parms oparms;
3006         __le16 *utf16_path;
3007
3008         cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3009         if (IS_ERR(tlink))
3010                 return ERR_CAST(tlink);
3011
3012         tcon = tlink_tcon(tlink);
3013         xid = get_xid();
3014
3015         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3016         if (!utf16_path) {
3017                 rc = -ENOMEM;
3018                 free_xid(xid);
3019                 return ERR_PTR(rc);
3020         }
3021
3022         oparms.tcon = tcon;
3023         oparms.desired_access = READ_CONTROL;
3024         oparms.disposition = FILE_OPEN;
3025         oparms.create_options = cifs_create_options(cifs_sb, 0);
3026         oparms.fid = &fid;
3027         oparms.reconnect = false;
3028
3029         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
3030         kfree(utf16_path);
3031         if (!rc) {
3032                 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3033                             fid.volatile_fid, (void **)&pntsd, pacllen);
3034                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3035         }
3036
3037         cifs_put_tlink(tlink);
3038         free_xid(xid);
3039
3040         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3041         if (rc)
3042                 return ERR_PTR(rc);
3043         return pntsd;
3044 }
3045
3046 static int
3047 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3048                 struct inode *inode, const char *path, int aclflag)
3049 {
3050         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3051         unsigned int xid;
3052         int rc, access_flags = 0;
3053         struct cifs_tcon *tcon;
3054         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3055         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3056         struct cifs_fid fid;
3057         struct cifs_open_parms oparms;
3058         __le16 *utf16_path;
3059
3060         cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3061         if (IS_ERR(tlink))
3062                 return PTR_ERR(tlink);
3063
3064         tcon = tlink_tcon(tlink);
3065         xid = get_xid();
3066
3067         if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3068                 access_flags = WRITE_OWNER;
3069         else
3070                 access_flags = WRITE_DAC;
3071
3072         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3073         if (!utf16_path) {
3074                 rc = -ENOMEM;
3075                 free_xid(xid);
3076                 return rc;
3077         }
3078
3079         oparms.tcon = tcon;
3080         oparms.desired_access = access_flags;
3081         oparms.create_options = cifs_create_options(cifs_sb, 0);
3082         oparms.disposition = FILE_OPEN;
3083         oparms.path = path;
3084         oparms.fid = &fid;
3085         oparms.reconnect = false;
3086
3087         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
3088         kfree(utf16_path);
3089         if (!rc) {
3090                 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3091                             fid.volatile_fid, pnntsd, acllen, aclflag);
3092                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3093         }
3094
3095         cifs_put_tlink(tlink);
3096         free_xid(xid);
3097         return rc;
3098 }
3099
3100 /* Retrieve an ACL from the server */
3101 static struct cifs_ntsd *
3102 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3103                                       struct inode *inode, const char *path,
3104                                       u32 *pacllen)
3105 {
3106         struct cifs_ntsd *pntsd = NULL;
3107         struct cifsFileInfo *open_file = NULL;
3108
3109         if (inode)
3110                 open_file = find_readable_file(CIFS_I(inode), true);
3111         if (!open_file)
3112                 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3113
3114         pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3115         cifsFileInfo_put(open_file);
3116         return pntsd;
3117 }
3118
3119 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3120                             loff_t offset, loff_t len, bool keep_size)
3121 {
3122         struct cifs_ses *ses = tcon->ses;
3123         struct inode *inode;
3124         struct cifsInodeInfo *cifsi;
3125         struct cifsFileInfo *cfile = file->private_data;
3126         struct file_zero_data_information fsctl_buf;
3127         long rc;
3128         unsigned int xid;
3129         __le64 eof;
3130
3131         xid = get_xid();
3132
3133         inode = d_inode(cfile->dentry);
3134         cifsi = CIFS_I(inode);
3135
3136         trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3137                               ses->Suid, offset, len);
3138
3139
3140         /* if file not oplocked can't be sure whether asking to extend size */
3141         if (!CIFS_CACHE_READ(cifsi))
3142                 if (keep_size == false) {
3143                         rc = -EOPNOTSUPP;
3144                         trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3145                                 tcon->tid, ses->Suid, offset, len, rc);
3146                         free_xid(xid);
3147                         return rc;
3148                 }
3149
3150         cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3151
3152         fsctl_buf.FileOffset = cpu_to_le64(offset);
3153         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3154
3155         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3156                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3157                         (char *)&fsctl_buf,
3158                         sizeof(struct file_zero_data_information),
3159                         0, NULL, NULL);
3160         if (rc)
3161                 goto zero_range_exit;
3162
3163         /*
3164          * do we also need to change the size of the file?
3165          */
3166         if (keep_size == false && i_size_read(inode) < offset + len) {
3167                 eof = cpu_to_le64(offset + len);
3168                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3169                                   cfile->fid.volatile_fid, cfile->pid, &eof);
3170         }
3171
3172  zero_range_exit:
3173         free_xid(xid);
3174         if (rc)
3175                 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3176                               ses->Suid, offset, len, rc);
3177         else
3178                 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3179                               ses->Suid, offset, len);
3180         return rc;
3181 }
3182
3183 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3184                             loff_t offset, loff_t len)
3185 {
3186         struct inode *inode;
3187         struct cifsFileInfo *cfile = file->private_data;
3188         struct file_zero_data_information fsctl_buf;
3189         long rc;
3190         unsigned int xid;
3191         __u8 set_sparse = 1;
3192
3193         xid = get_xid();
3194
3195         inode = d_inode(cfile->dentry);
3196
3197         /* Need to make file sparse, if not already, before freeing range. */
3198         /* Consider adding equivalent for compressed since it could also work */
3199         if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3200                 rc = -EOPNOTSUPP;
3201                 free_xid(xid);
3202                 return rc;
3203         }
3204
3205         cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3206
3207         fsctl_buf.FileOffset = cpu_to_le64(offset);
3208         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3209
3210         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3211                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3212                         true /* is_fctl */, (char *)&fsctl_buf,
3213                         sizeof(struct file_zero_data_information),
3214                         CIFSMaxBufSize, NULL, NULL);
3215         free_xid(xid);
3216         return rc;
3217 }
3218
3219 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3220                             loff_t off, loff_t len, bool keep_size)
3221 {
3222         struct inode *inode;
3223         struct cifsInodeInfo *cifsi;
3224         struct cifsFileInfo *cfile = file->private_data;
3225         long rc = -EOPNOTSUPP;
3226         unsigned int xid;
3227         __le64 eof;
3228
3229         xid = get_xid();
3230
3231         inode = d_inode(cfile->dentry);
3232         cifsi = CIFS_I(inode);
3233
3234         trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3235                                 tcon->ses->Suid, off, len);
3236         /* if file not oplocked can't be sure whether asking to extend size */
3237         if (!CIFS_CACHE_READ(cifsi))
3238                 if (keep_size == false) {
3239                         trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3240                                 tcon->tid, tcon->ses->Suid, off, len, rc);
3241                         free_xid(xid);
3242                         return rc;
3243                 }
3244
3245         /*
3246          * Extending the file
3247          */
3248         if ((keep_size == false) && i_size_read(inode) < off + len) {
3249                 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3250                         smb2_set_sparse(xid, tcon, cfile, inode, false);
3251
3252                 eof = cpu_to_le64(off + len);
3253                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3254                                   cfile->fid.volatile_fid, cfile->pid, &eof);
3255                 if (rc == 0) {
3256                         cifsi->server_eof = off + len;
3257                         cifs_setsize(inode, off + len);
3258                         cifs_truncate_page(inode->i_mapping, inode->i_size);
3259                         truncate_setsize(inode, off + len);
3260                 }
3261                 goto out;
3262         }
3263
3264         /*
3265          * Files are non-sparse by default so falloc may be a no-op
3266          * Must check if file sparse. If not sparse, and since we are not
3267          * extending then no need to do anything since file already allocated
3268          */
3269         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3270                 rc = 0;
3271                 goto out;
3272         }
3273
3274         if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3275                 /*
3276                  * Check if falloc starts within first few pages of file
3277                  * and ends within a few pages of the end of file to
3278                  * ensure that most of file is being forced to be
3279                  * fallocated now. If so then setting whole file sparse
3280                  * ie potentially making a few extra pages at the beginning
3281                  * or end of the file non-sparse via set_sparse is harmless.
3282                  */
3283                 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3284                         rc = -EOPNOTSUPP;
3285                         goto out;
3286                 }
3287         }
3288
3289         smb2_set_sparse(xid, tcon, cfile, inode, false);
3290         rc = 0;
3291
3292 out:
3293         if (rc)
3294                 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3295                                 tcon->ses->Suid, off, len, rc);
3296         else
3297                 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3298                                 tcon->ses->Suid, off, len);
3299
3300         free_xid(xid);
3301         return rc;
3302 }
3303
3304 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3305 {
3306         struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3307         struct cifsInodeInfo *cifsi;
3308         struct inode *inode;
3309         int rc = 0;
3310         struct file_allocated_range_buffer in_data, *out_data = NULL;
3311         u32 out_data_len;
3312         unsigned int xid;
3313
3314         if (whence != SEEK_HOLE && whence != SEEK_DATA)
3315                 return generic_file_llseek(file, offset, whence);
3316
3317         inode = d_inode(cfile->dentry);
3318         cifsi = CIFS_I(inode);
3319
3320         if (offset < 0 || offset >= i_size_read(inode))
3321                 return -ENXIO;
3322
3323         xid = get_xid();
3324         /*
3325          * We need to be sure that all dirty pages are written as they
3326          * might fill holes on the server.
3327          * Note that we also MUST flush any written pages since at least
3328          * some servers (Windows2016) will not reflect recent writes in
3329          * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3330          */
3331         wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3332         if (wrcfile) {
3333                 filemap_write_and_wait(inode->i_mapping);
3334                 smb2_flush_file(xid, tcon, &wrcfile->fid);
3335                 cifsFileInfo_put(wrcfile);
3336         }
3337
3338         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3339                 if (whence == SEEK_HOLE)
3340                         offset = i_size_read(inode);
3341                 goto lseek_exit;
3342         }
3343
3344         in_data.file_offset = cpu_to_le64(offset);
3345         in_data.length = cpu_to_le64(i_size_read(inode));
3346
3347         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3348                         cfile->fid.volatile_fid,
3349                         FSCTL_QUERY_ALLOCATED_RANGES, true,
3350                         (char *)&in_data, sizeof(in_data),
3351                         sizeof(struct file_allocated_range_buffer),
3352                         (char **)&out_data, &out_data_len);
3353         if (rc == -E2BIG)
3354                 rc = 0;
3355         if (rc)
3356                 goto lseek_exit;
3357
3358         if (whence == SEEK_HOLE && out_data_len == 0)
3359                 goto lseek_exit;
3360
3361         if (whence == SEEK_DATA && out_data_len == 0) {
3362                 rc = -ENXIO;
3363                 goto lseek_exit;
3364         }
3365
3366         if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3367                 rc = -EINVAL;
3368                 goto lseek_exit;
3369         }
3370         if (whence == SEEK_DATA) {
3371                 offset = le64_to_cpu(out_data->file_offset);
3372                 goto lseek_exit;
3373         }
3374         if (offset < le64_to_cpu(out_data->file_offset))
3375                 goto lseek_exit;
3376
3377         offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3378
3379  lseek_exit:
3380         free_xid(xid);
3381         kfree(out_data);
3382         if (!rc)
3383                 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3384         else
3385                 return rc;
3386 }
3387
3388 static int smb3_fiemap(struct cifs_tcon *tcon,
3389                        struct cifsFileInfo *cfile,
3390                        struct fiemap_extent_info *fei, u64 start, u64 len)
3391 {
3392         unsigned int xid;
3393         struct file_allocated_range_buffer in_data, *out_data;
3394         u32 out_data_len;
3395         int i, num, rc, flags, last_blob;
3396         u64 next;
3397
3398         if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3399                 return -EBADR;
3400
3401         xid = get_xid();
3402  again:
3403         in_data.file_offset = cpu_to_le64(start);
3404         in_data.length = cpu_to_le64(len);
3405
3406         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3407                         cfile->fid.volatile_fid,
3408                         FSCTL_QUERY_ALLOCATED_RANGES, true,
3409                         (char *)&in_data, sizeof(in_data),
3410                         1024 * sizeof(struct file_allocated_range_buffer),
3411                         (char **)&out_data, &out_data_len);
3412         if (rc == -E2BIG) {
3413                 last_blob = 0;
3414                 rc = 0;
3415         } else
3416                 last_blob = 1;
3417         if (rc)
3418                 goto out;
3419
3420         if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3421                 rc = -EINVAL;
3422                 goto out;
3423         }
3424         if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3425                 rc = -EINVAL;
3426                 goto out;
3427         }
3428
3429         num = out_data_len / sizeof(struct file_allocated_range_buffer);
3430         for (i = 0; i < num; i++) {
3431                 flags = 0;
3432                 if (i == num - 1 && last_blob)
3433                         flags |= FIEMAP_EXTENT_LAST;
3434
3435                 rc = fiemap_fill_next_extent(fei,
3436                                 le64_to_cpu(out_data[i].file_offset),
3437                                 le64_to_cpu(out_data[i].file_offset),
3438                                 le64_to_cpu(out_data[i].length),
3439                                 flags);
3440                 if (rc < 0)
3441                         goto out;
3442                 if (rc == 1) {
3443                         rc = 0;
3444                         goto out;
3445                 }
3446         }
3447
3448         if (!last_blob) {
3449                 next = le64_to_cpu(out_data[num - 1].file_offset) +
3450                   le64_to_cpu(out_data[num - 1].length);
3451                 len = len - (next - start);
3452                 start = next;
3453                 goto again;
3454         }
3455
3456  out:
3457         free_xid(xid);
3458         kfree(out_data);
3459         return rc;
3460 }
3461
3462 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3463                            loff_t off, loff_t len)
3464 {
3465         /* KEEP_SIZE already checked for by do_fallocate */
3466         if (mode & FALLOC_FL_PUNCH_HOLE)
3467                 return smb3_punch_hole(file, tcon, off, len);
3468         else if (mode & FALLOC_FL_ZERO_RANGE) {
3469                 if (mode & FALLOC_FL_KEEP_SIZE)
3470                         return smb3_zero_range(file, tcon, off, len, true);
3471                 return smb3_zero_range(file, tcon, off, len, false);
3472         } else if (mode == FALLOC_FL_KEEP_SIZE)
3473                 return smb3_simple_falloc(file, tcon, off, len, true);
3474         else if (mode == 0)
3475                 return smb3_simple_falloc(file, tcon, off, len, false);
3476
3477         return -EOPNOTSUPP;
3478 }
3479
3480 static void
3481 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3482                       struct cifsInodeInfo *cinode, __u32 oplock,
3483                       unsigned int epoch, bool *purge_cache)
3484 {
3485         server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3486 }
3487
3488 static void
3489 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3490                        unsigned int epoch, bool *purge_cache);
3491
3492 static void
3493 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3494                        struct cifsInodeInfo *cinode, __u32 oplock,
3495                        unsigned int epoch, bool *purge_cache)
3496 {
3497         unsigned int old_state = cinode->oplock;
3498         unsigned int old_epoch = cinode->epoch;
3499         unsigned int new_state;
3500
3501         if (epoch > old_epoch) {
3502                 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3503                 cinode->epoch = epoch;
3504         }
3505
3506         new_state = cinode->oplock;
3507         *purge_cache = false;
3508
3509         if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3510             (new_state & CIFS_CACHE_READ_FLG) == 0)
3511                 *purge_cache = true;
3512         else if (old_state == new_state && (epoch - old_epoch > 1))
3513                 *purge_cache = true;
3514 }
3515
3516 static void
3517 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3518                       unsigned int epoch, bool *purge_cache)
3519 {
3520         oplock &= 0xFF;
3521         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3522                 return;
3523         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3524                 cinode->oplock = CIFS_CACHE_RHW_FLG;
3525                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3526                          &cinode->vfs_inode);
3527         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3528                 cinode->oplock = CIFS_CACHE_RW_FLG;
3529                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3530                          &cinode->vfs_inode);
3531         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3532                 cinode->oplock = CIFS_CACHE_READ_FLG;
3533                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3534                          &cinode->vfs_inode);
3535         } else
3536                 cinode->oplock = 0;
3537 }
3538
3539 static void
3540 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3541                        unsigned int epoch, bool *purge_cache)
3542 {
3543         char message[5] = {0};
3544         unsigned int new_oplock = 0;
3545
3546         oplock &= 0xFF;
3547         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3548                 return;
3549
3550         /* Check if the server granted an oplock rather than a lease */
3551         if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3552                 return smb2_set_oplock_level(cinode, oplock, epoch,
3553                                              purge_cache);
3554
3555         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3556                 new_oplock |= CIFS_CACHE_READ_FLG;
3557                 strcat(message, "R");
3558         }
3559         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3560                 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3561                 strcat(message, "H");
3562         }
3563         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3564                 new_oplock |= CIFS_CACHE_WRITE_FLG;
3565                 strcat(message, "W");
3566         }
3567         if (!new_oplock)
3568                 strncpy(message, "None", sizeof(message));
3569
3570         cinode->oplock = new_oplock;
3571         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3572                  &cinode->vfs_inode);
3573 }
3574
3575 static void
3576 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3577                       unsigned int epoch, bool *purge_cache)
3578 {
3579         unsigned int old_oplock = cinode->oplock;
3580
3581         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3582
3583         if (purge_cache) {
3584                 *purge_cache = false;
3585                 if (old_oplock == CIFS_CACHE_READ_FLG) {
3586                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3587                             (epoch - cinode->epoch > 0))
3588                                 *purge_cache = true;
3589                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3590                                  (epoch - cinode->epoch > 1))
3591                                 *purge_cache = true;
3592                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3593                                  (epoch - cinode->epoch > 1))
3594                                 *purge_cache = true;
3595                         else if (cinode->oplock == 0 &&
3596                                  (epoch - cinode->epoch > 0))
3597                                 *purge_cache = true;
3598                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3599                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3600                             (epoch - cinode->epoch > 0))
3601                                 *purge_cache = true;
3602                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3603                                  (epoch - cinode->epoch > 1))
3604                                 *purge_cache = true;
3605                 }
3606                 cinode->epoch = epoch;
3607         }
3608 }
3609
3610 static bool
3611 smb2_is_read_op(__u32 oplock)
3612 {
3613         return oplock == SMB2_OPLOCK_LEVEL_II;
3614 }
3615
3616 static bool
3617 smb21_is_read_op(__u32 oplock)
3618 {
3619         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3620                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3621 }
3622
3623 static __le32
3624 map_oplock_to_lease(u8 oplock)
3625 {
3626         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3627                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3628         else if (oplock == SMB2_OPLOCK_LEVEL_II)
3629                 return SMB2_LEASE_READ_CACHING;
3630         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3631                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3632                        SMB2_LEASE_WRITE_CACHING;
3633         return 0;
3634 }
3635
3636 static char *
3637 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3638 {
3639         struct create_lease *buf;
3640
3641         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3642         if (!buf)
3643                 return NULL;
3644
3645         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3646         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3647
3648         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3649                                         (struct create_lease, lcontext));
3650         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3651         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3652                                 (struct create_lease, Name));
3653         buf->ccontext.NameLength = cpu_to_le16(4);
3654         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3655         buf->Name[0] = 'R';
3656         buf->Name[1] = 'q';
3657         buf->Name[2] = 'L';
3658         buf->Name[3] = 's';
3659         return (char *)buf;
3660 }
3661
3662 static char *
3663 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3664 {
3665         struct create_lease_v2 *buf;
3666
3667         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3668         if (!buf)
3669                 return NULL;
3670
3671         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3672         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3673
3674         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3675                                         (struct create_lease_v2, lcontext));
3676         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3677         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3678                                 (struct create_lease_v2, Name));
3679         buf->ccontext.NameLength = cpu_to_le16(4);
3680         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3681         buf->Name[0] = 'R';
3682         buf->Name[1] = 'q';
3683         buf->Name[2] = 'L';
3684         buf->Name[3] = 's';
3685         return (char *)buf;
3686 }
3687
3688 static __u8
3689 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3690 {
3691         struct create_lease *lc = (struct create_lease *)buf;
3692
3693         *epoch = 0; /* not used */
3694         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3695                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3696         return le32_to_cpu(lc->lcontext.LeaseState);
3697 }
3698
3699 static __u8
3700 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3701 {
3702         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3703
3704         *epoch = le16_to_cpu(lc->lcontext.Epoch);
3705         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3706                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3707         if (lease_key)
3708                 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3709         return le32_to_cpu(lc->lcontext.LeaseState);
3710 }
3711
3712 static unsigned int
3713 smb2_wp_retry_size(struct inode *inode)
3714 {
3715         return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3716                      SMB2_MAX_BUFFER_SIZE);
3717 }
3718
3719 static bool
3720 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3721 {
3722         return !cfile->invalidHandle;
3723 }
3724
3725 static void
3726 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3727                    struct smb_rqst *old_rq, __le16 cipher_type)
3728 {
3729         struct smb2_sync_hdr *shdr =
3730                         (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3731
3732         memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3733         tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3734         tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3735         tr_hdr->Flags = cpu_to_le16(0x01);
3736         if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3737                 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3738         else
3739                 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3740         memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3741 }
3742
3743 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3744  * stack object as buf.
3745  */
3746 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3747                                    unsigned int buflen)
3748 {
3749         void *addr;
3750         /*
3751          * VMAP_STACK (at least) puts stack into the vmalloc address space
3752          */
3753         if (is_vmalloc_addr(buf))
3754                 addr = vmalloc_to_page(buf);
3755         else
3756                 addr = virt_to_page(buf);
3757         sg_set_page(sg, addr, buflen, offset_in_page(buf));
3758 }
3759
3760 /* Assumes the first rqst has a transform header as the first iov.
3761  * I.e.
3762  * rqst[0].rq_iov[0]  is transform header
3763  * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3764  * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3765  */
3766 static struct scatterlist *
3767 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3768 {
3769         unsigned int sg_len;
3770         struct scatterlist *sg;
3771         unsigned int i;
3772         unsigned int j;
3773         unsigned int idx = 0;
3774         int skip;
3775
3776         sg_len = 1;
3777         for (i = 0; i < num_rqst; i++)
3778                 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
3779
3780         sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3781         if (!sg)
3782                 return NULL;
3783
3784         sg_init_table(sg, sg_len);
3785         for (i = 0; i < num_rqst; i++) {
3786                 for (j = 0; j < rqst[i].rq_nvec; j++) {
3787                         /*
3788                          * The first rqst has a transform header where the
3789                          * first 20 bytes are not part of the encrypted blob
3790                          */
3791                         skip = (i == 0) && (j == 0) ? 20 : 0;
3792                         smb2_sg_set_buf(&sg[idx++],
3793                                         rqst[i].rq_iov[j].iov_base + skip,
3794                                         rqst[i].rq_iov[j].iov_len - skip);
3795                         }
3796
3797                 for (j = 0; j < rqst[i].rq_npages; j++) {
3798                         unsigned int len, offset;
3799
3800                         rqst_page_get_length(&rqst[i], j, &len, &offset);
3801                         sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3802                 }
3803         }
3804         smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
3805         return sg;
3806 }
3807
3808 static int
3809 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3810 {
3811         struct cifs_ses *ses;
3812         u8 *ses_enc_key;
3813
3814         spin_lock(&cifs_tcp_ses_lock);
3815         list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3816                 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3817                         if (ses->Suid == ses_id) {
3818                                 ses_enc_key = enc ? ses->smb3encryptionkey :
3819                                         ses->smb3decryptionkey;
3820                                 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3821                                 spin_unlock(&cifs_tcp_ses_lock);
3822                                 return 0;
3823                         }
3824                 }
3825         }
3826         spin_unlock(&cifs_tcp_ses_lock);
3827
3828         return 1;
3829 }
3830 /*
3831  * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3832  * iov[0]   - transform header (associate data),
3833  * iov[1-N] - SMB2 header and pages - data to encrypt.
3834  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3835  * untouched.
3836  */
3837 static int
3838 crypt_message(struct TCP_Server_Info *server, int num_rqst,
3839               struct smb_rqst *rqst, int enc)
3840 {
3841         struct smb2_transform_hdr *tr_hdr =
3842                 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3843         unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3844         int rc = 0;
3845         struct scatterlist *sg;
3846         u8 sign[SMB2_SIGNATURE_SIZE] = {};
3847         u8 key[SMB3_SIGN_KEY_SIZE];
3848         struct aead_request *req;
3849         char *iv;
3850         unsigned int iv_len;
3851         DECLARE_CRYPTO_WAIT(wait);
3852         struct crypto_aead *tfm;
3853         unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3854
3855         rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3856         if (rc) {
3857                 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3858                          enc ? "en" : "de");
3859                 return 0;
3860         }
3861
3862         rc = smb3_crypto_aead_allocate(server);
3863         if (rc) {
3864                 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3865                 return rc;
3866         }
3867
3868         tfm = enc ? server->secmech.ccmaesencrypt :
3869                                                 server->secmech.ccmaesdecrypt;
3870         rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3871         if (rc) {
3872                 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3873                 return rc;
3874         }
3875
3876         rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3877         if (rc) {
3878                 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3879                 return rc;
3880         }
3881
3882         req = aead_request_alloc(tfm, GFP_KERNEL);
3883         if (!req) {
3884                 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
3885                 return -ENOMEM;
3886         }
3887
3888         if (!enc) {
3889                 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3890                 crypt_len += SMB2_SIGNATURE_SIZE;
3891         }
3892
3893         sg = init_sg(num_rqst, rqst, sign);
3894         if (!sg) {
3895                 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
3896                 rc = -ENOMEM;
3897                 goto free_req;
3898         }
3899
3900         iv_len = crypto_aead_ivsize(tfm);
3901         iv = kzalloc(iv_len, GFP_KERNEL);
3902         if (!iv) {
3903                 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
3904                 rc = -ENOMEM;
3905                 goto free_sg;
3906         }
3907
3908         if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3909                 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3910         else {
3911                 iv[0] = 3;
3912                 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3913         }
3914
3915         aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3916         aead_request_set_ad(req, assoc_data_len);
3917
3918         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3919                                   crypto_req_done, &wait);
3920
3921         rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3922                                 : crypto_aead_decrypt(req), &wait);
3923
3924         if (!rc && enc)
3925                 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3926
3927         kfree(iv);
3928 free_sg:
3929         kfree(sg);
3930 free_req:
3931         kfree(req);
3932         return rc;
3933 }
3934
3935 void
3936 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3937 {
3938         int i, j;
3939
3940         for (i = 0; i < num_rqst; i++) {
3941                 if (rqst[i].rq_pages) {
3942                         for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3943                                 put_page(rqst[i].rq_pages[j]);
3944                         kfree(rqst[i].rq_pages);
3945                 }
3946         }
3947 }
3948
3949 /*
3950  * This function will initialize new_rq and encrypt the content.
3951  * The first entry, new_rq[0], only contains a single iov which contains
3952  * a smb2_transform_hdr and is pre-allocated by the caller.
3953  * This function then populates new_rq[1+] with the content from olq_rq[0+].
3954  *
3955  * The end result is an array of smb_rqst structures where the first structure
3956  * only contains a single iov for the transform header which we then can pass
3957  * to crypt_message().
3958  *
3959  * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
3960  * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3961  */
3962 static int
3963 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3964                        struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3965 {
3966         struct page **pages;
3967         struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3968         unsigned int npages;
3969         unsigned int orig_len = 0;
3970         int i, j;
3971         int rc = -ENOMEM;
3972
3973         for (i = 1; i < num_rqst; i++) {
3974                 npages = old_rq[i - 1].rq_npages;
3975                 pages = kmalloc_array(npages, sizeof(struct page *),
3976                                       GFP_KERNEL);
3977                 if (!pages)
3978                         goto err_free;
3979
3980                 new_rq[i].rq_pages = pages;
3981                 new_rq[i].rq_npages = npages;
3982                 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3983                 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3984                 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3985                 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3986                 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3987
3988                 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3989
3990                 for (j = 0; j < npages; j++) {
3991                         pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3992                         if (!pages[j])
3993                                 goto err_free;
3994                 }
3995
3996                 /* copy pages form the old */
3997                 for (j = 0; j < npages; j++) {
3998                         char *dst, *src;
3999                         unsigned int offset, len;
4000
4001                         rqst_page_get_length(&new_rq[i], j, &len, &offset);
4002
4003                         dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4004                         src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4005
4006                         memcpy(dst, src, len);
4007                         kunmap(new_rq[i].rq_pages[j]);
4008                         kunmap(old_rq[i - 1].rq_pages[j]);
4009                 }
4010         }
4011
4012         /* fill the 1st iov with a transform header */
4013         fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4014
4015         rc = crypt_message(server, num_rqst, new_rq, 1);
4016         cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4017         if (rc)
4018                 goto err_free;
4019
4020         return rc;
4021
4022 err_free:
4023         smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4024         return rc;
4025 }
4026
4027 static int
4028 smb3_is_transform_hdr(void *buf)
4029 {
4030         struct smb2_transform_hdr *trhdr = buf;
4031
4032         return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4033 }
4034
4035 static int
4036 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4037                  unsigned int buf_data_size, struct page **pages,
4038                  unsigned int npages, unsigned int page_data_size)
4039 {
4040         struct kvec iov[2];
4041         struct smb_rqst rqst = {NULL};
4042         int rc;
4043
4044         iov[0].iov_base = buf;
4045         iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4046         iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4047         iov[1].iov_len = buf_data_size;
4048
4049         rqst.rq_iov = iov;
4050         rqst.rq_nvec = 2;
4051         rqst.rq_pages = pages;
4052         rqst.rq_npages = npages;
4053         rqst.rq_pagesz = PAGE_SIZE;
4054         rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4055
4056         rc = crypt_message(server, 1, &rqst, 0);
4057         cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4058
4059         if (rc)
4060                 return rc;
4061
4062         memmove(buf, iov[1].iov_base, buf_data_size);
4063
4064         server->total_read = buf_data_size + page_data_size;
4065
4066         return rc;
4067 }
4068
4069 static int
4070 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4071                      unsigned int npages, unsigned int len)
4072 {
4073         int i;
4074         int length;
4075
4076         for (i = 0; i < npages; i++) {
4077                 struct page *page = pages[i];
4078                 size_t n;
4079
4080                 n = len;
4081                 if (len >= PAGE_SIZE) {
4082                         /* enough data to fill the page */
4083                         n = PAGE_SIZE;
4084                         len -= n;
4085                 } else {
4086                         zero_user(page, len, PAGE_SIZE - len);
4087                         len = 0;
4088                 }
4089                 length = cifs_read_page_from_socket(server, page, 0, n);
4090                 if (length < 0)
4091                         return length;
4092                 server->total_read += length;
4093         }
4094
4095         return 0;
4096 }
4097
4098 static int
4099 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4100                unsigned int cur_off, struct bio_vec **page_vec)
4101 {
4102         struct bio_vec *bvec;
4103         int i;
4104
4105         bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4106         if (!bvec)
4107                 return -ENOMEM;
4108
4109         for (i = 0; i < npages; i++) {
4110                 bvec[i].bv_page = pages[i];
4111                 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4112                 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4113                 data_size -= bvec[i].bv_len;
4114         }
4115
4116         if (data_size != 0) {
4117                 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4118                 kfree(bvec);
4119                 return -EIO;
4120         }
4121
4122         *page_vec = bvec;
4123         return 0;
4124 }
4125
4126 static int
4127 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4128                  char *buf, unsigned int buf_len, struct page **pages,
4129                  unsigned int npages, unsigned int page_data_size)
4130 {
4131         unsigned int data_offset;
4132         unsigned int data_len;
4133         unsigned int cur_off;
4134         unsigned int cur_page_idx;
4135         unsigned int pad_len;
4136         struct cifs_readdata *rdata = mid->callback_data;
4137         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4138         struct bio_vec *bvec = NULL;
4139         struct iov_iter iter;
4140         struct kvec iov;
4141         int length;
4142         bool use_rdma_mr = false;
4143
4144         if (shdr->Command != SMB2_READ) {
4145                 cifs_server_dbg(VFS, "only big read responses are supported\n");
4146                 return -ENOTSUPP;
4147         }
4148
4149         if (server->ops->is_session_expired &&
4150             server->ops->is_session_expired(buf)) {
4151                 cifs_reconnect(server);
4152                 wake_up(&server->response_q);
4153                 return -1;
4154         }
4155
4156         if (server->ops->is_status_pending &&
4157                         server->ops->is_status_pending(buf, server))
4158                 return -1;
4159
4160         /* set up first two iov to get credits */
4161         rdata->iov[0].iov_base = buf;
4162         rdata->iov[0].iov_len = 0;
4163         rdata->iov[1].iov_base = buf;
4164         rdata->iov[1].iov_len =
4165                 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4166         cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4167                  rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4168         cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4169                  rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4170
4171         rdata->result = server->ops->map_error(buf, true);
4172         if (rdata->result != 0) {
4173                 cifs_dbg(FYI, "%s: server returned error %d\n",
4174                          __func__, rdata->result);
4175                 /* normal error on read response */
4176                 dequeue_mid(mid, false);
4177                 return 0;
4178         }
4179
4180         data_offset = server->ops->read_data_offset(buf);
4181 #ifdef CONFIG_CIFS_SMB_DIRECT
4182         use_rdma_mr = rdata->mr;
4183 #endif
4184         data_len = server->ops->read_data_length(buf, use_rdma_mr);
4185
4186         if (data_offset < server->vals->read_rsp_size) {
4187                 /*
4188                  * win2k8 sometimes sends an offset of 0 when the read
4189                  * is beyond the EOF. Treat it as if the data starts just after
4190                  * the header.
4191                  */
4192                 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4193                          __func__, data_offset);
4194                 data_offset = server->vals->read_rsp_size;
4195         } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4196                 /* data_offset is beyond the end of smallbuf */
4197                 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4198                          __func__, data_offset);
4199                 rdata->result = -EIO;
4200                 dequeue_mid(mid, rdata->result);
4201                 return 0;
4202         }
4203
4204         pad_len = data_offset - server->vals->read_rsp_size;
4205
4206         if (buf_len <= data_offset) {
4207                 /* read response payload is in pages */
4208                 cur_page_idx = pad_len / PAGE_SIZE;
4209                 cur_off = pad_len % PAGE_SIZE;
4210
4211                 if (cur_page_idx != 0) {
4212                         /* data offset is beyond the 1st page of response */
4213                         cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4214                                  __func__, data_offset);
4215                         rdata->result = -EIO;
4216                         dequeue_mid(mid, rdata->result);
4217                         return 0;
4218                 }
4219
4220                 if (data_len > page_data_size - pad_len) {
4221                         /* data_len is corrupt -- discard frame */
4222                         rdata->result = -EIO;
4223                         dequeue_mid(mid, rdata->result);
4224                         return 0;
4225                 }
4226
4227                 rdata->result = init_read_bvec(pages, npages, page_data_size,
4228                                                cur_off, &bvec);
4229                 if (rdata->result != 0) {
4230                         dequeue_mid(mid, rdata->result);
4231                         return 0;
4232                 }
4233
4234                 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4235         } else if (buf_len >= data_offset + data_len) {
4236                 /* read response payload is in buf */
4237                 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4238                 iov.iov_base = buf + data_offset;
4239                 iov.iov_len = data_len;
4240                 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4241         } else {
4242                 /* read response payload cannot be in both buf and pages */
4243                 WARN_ONCE(1, "buf can not contain only a part of read data");
4244                 rdata->result = -EIO;
4245                 dequeue_mid(mid, rdata->result);
4246                 return 0;
4247         }
4248
4249         length = rdata->copy_into_pages(server, rdata, &iter);
4250
4251         kfree(bvec);
4252
4253         if (length < 0)
4254                 return length;
4255
4256         dequeue_mid(mid, false);
4257         return length;
4258 }
4259
4260 struct smb2_decrypt_work {
4261         struct work_struct decrypt;
4262         struct TCP_Server_Info *server;
4263         struct page **ppages;
4264         char *buf;
4265         unsigned int npages;
4266         unsigned int len;
4267 };
4268
4269
4270 static void smb2_decrypt_offload(struct work_struct *work)
4271 {
4272         struct smb2_decrypt_work *dw = container_of(work,
4273                                 struct smb2_decrypt_work, decrypt);
4274         int i, rc;
4275         struct mid_q_entry *mid;
4276
4277         rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4278                               dw->ppages, dw->npages, dw->len);
4279         if (rc) {
4280                 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4281                 goto free_pages;
4282         }
4283
4284         dw->server->lstrp = jiffies;
4285         mid = smb2_find_mid(dw->server, dw->buf);
4286         if (mid == NULL)
4287                 cifs_dbg(FYI, "mid not found\n");
4288         else {
4289                 mid->decrypted = true;
4290                 rc = handle_read_data(dw->server, mid, dw->buf,
4291                                       dw->server->vals->read_rsp_size,
4292                                       dw->ppages, dw->npages, dw->len);
4293                 mid->callback(mid);
4294                 cifs_mid_q_entry_release(mid);
4295         }
4296
4297 free_pages:
4298         for (i = dw->npages-1; i >= 0; i--)
4299                 put_page(dw->ppages[i]);
4300
4301         kfree(dw->ppages);
4302         cifs_small_buf_release(dw->buf);
4303         kfree(dw);
4304 }
4305
4306
4307 static int
4308 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4309                        int *num_mids)
4310 {
4311         char *buf = server->smallbuf;
4312         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4313         unsigned int npages;
4314         struct page **pages;
4315         unsigned int len;
4316         unsigned int buflen = server->pdu_size;
4317         int rc;
4318         int i = 0;
4319         struct smb2_decrypt_work *dw;
4320
4321         *num_mids = 1;
4322         len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4323                 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4324
4325         rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4326         if (rc < 0)
4327                 return rc;
4328         server->total_read += rc;
4329
4330         len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4331                 server->vals->read_rsp_size;
4332         npages = DIV_ROUND_UP(len, PAGE_SIZE);
4333
4334         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4335         if (!pages) {
4336                 rc = -ENOMEM;
4337                 goto discard_data;
4338         }
4339
4340         for (; i < npages; i++) {
4341                 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4342                 if (!pages[i]) {
4343                         rc = -ENOMEM;
4344                         goto discard_data;
4345                 }
4346         }
4347
4348         /* read read data into pages */
4349         rc = read_data_into_pages(server, pages, npages, len);
4350         if (rc)
4351                 goto free_pages;
4352
4353         rc = cifs_discard_remaining_data(server);
4354         if (rc)
4355                 goto free_pages;
4356
4357         /*
4358          * For large reads, offload to different thread for better performance,
4359          * use more cores decrypting which can be expensive
4360          */
4361
4362         if ((server->min_offload) && (server->in_flight > 1) &&
4363             (server->pdu_size >= server->min_offload)) {
4364                 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4365                 if (dw == NULL)
4366                         goto non_offloaded_decrypt;
4367
4368                 dw->buf = server->smallbuf;
4369                 server->smallbuf = (char *)cifs_small_buf_get();
4370
4371                 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4372
4373                 dw->npages = npages;
4374                 dw->server = server;
4375                 dw->ppages = pages;
4376                 dw->len = len;
4377                 queue_work(decrypt_wq, &dw->decrypt);
4378                 *num_mids = 0; /* worker thread takes care of finding mid */
4379                 return -1;
4380         }
4381
4382 non_offloaded_decrypt:
4383         rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4384                               pages, npages, len);
4385         if (rc)
4386                 goto free_pages;
4387
4388         *mid = smb2_find_mid(server, buf);
4389         if (*mid == NULL)
4390                 cifs_dbg(FYI, "mid not found\n");
4391         else {
4392                 cifs_dbg(FYI, "mid found\n");
4393                 (*mid)->decrypted = true;
4394                 rc = handle_read_data(server, *mid, buf,
4395                                       server->vals->read_rsp_size,
4396                                       pages, npages, len);
4397         }
4398
4399 free_pages:
4400         for (i = i - 1; i >= 0; i--)
4401                 put_page(pages[i]);
4402         kfree(pages);
4403         return rc;
4404 discard_data:
4405         cifs_discard_remaining_data(server);
4406         goto free_pages;
4407 }
4408
4409 static int
4410 receive_encrypted_standard(struct TCP_Server_Info *server,
4411                            struct mid_q_entry **mids, char **bufs,
4412                            int *num_mids)
4413 {
4414         int ret, length;
4415         char *buf = server->smallbuf;
4416         struct smb2_sync_hdr *shdr;
4417         unsigned int pdu_length = server->pdu_size;
4418         unsigned int buf_size;
4419         struct mid_q_entry *mid_entry;
4420         int next_is_large;
4421         char *next_buffer = NULL;
4422
4423         *num_mids = 0;
4424
4425         /* switch to large buffer if too big for a small one */
4426         if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4427                 server->large_buf = true;
4428                 memcpy(server->bigbuf, buf, server->total_read);
4429                 buf = server->bigbuf;
4430         }
4431
4432         /* now read the rest */
4433         length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4434                                 pdu_length - HEADER_SIZE(server) + 1);
4435         if (length < 0)
4436                 return length;
4437         server->total_read += length;
4438
4439         buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4440         length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4441         if (length)
4442                 return length;
4443
4444         next_is_large = server->large_buf;
4445 one_more:
4446         shdr = (struct smb2_sync_hdr *)buf;
4447         if (shdr->NextCommand) {
4448                 if (next_is_large)
4449                         next_buffer = (char *)cifs_buf_get();
4450                 else
4451                         next_buffer = (char *)cifs_small_buf_get();
4452                 memcpy(next_buffer,
4453                        buf + le32_to_cpu(shdr->NextCommand),
4454                        pdu_length - le32_to_cpu(shdr->NextCommand));
4455         }
4456
4457         mid_entry = smb2_find_mid(server, buf);
4458         if (mid_entry == NULL)
4459                 cifs_dbg(FYI, "mid not found\n");
4460         else {
4461                 cifs_dbg(FYI, "mid found\n");
4462                 mid_entry->decrypted = true;
4463                 mid_entry->resp_buf_size = server->pdu_size;
4464         }
4465
4466         if (*num_mids >= MAX_COMPOUND) {
4467                 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4468                 return -1;
4469         }
4470         bufs[*num_mids] = buf;
4471         mids[(*num_mids)++] = mid_entry;
4472
4473         if (mid_entry && mid_entry->handle)
4474                 ret = mid_entry->handle(server, mid_entry);
4475         else
4476                 ret = cifs_handle_standard(server, mid_entry);
4477
4478         if (ret == 0 && shdr->NextCommand) {
4479                 pdu_length -= le32_to_cpu(shdr->NextCommand);
4480                 server->large_buf = next_is_large;
4481                 if (next_is_large)
4482                         server->bigbuf = buf = next_buffer;
4483                 else
4484                         server->smallbuf = buf = next_buffer;
4485                 goto one_more;
4486         } else if (ret != 0) {
4487                 /*
4488                  * ret != 0 here means that we didn't get to handle_mid() thus
4489                  * server->smallbuf and server->bigbuf are still valid. We need
4490                  * to free next_buffer because it is not going to be used
4491                  * anywhere.
4492                  */
4493                 if (next_is_large)
4494                         free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4495                 else
4496                         free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4497         }
4498
4499         return ret;
4500 }
4501
4502 static int
4503 smb3_receive_transform(struct TCP_Server_Info *server,
4504                        struct mid_q_entry **mids, char **bufs, int *num_mids)
4505 {
4506         char *buf = server->smallbuf;
4507         unsigned int pdu_length = server->pdu_size;
4508         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4509         unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4510
4511         if (pdu_length < sizeof(struct smb2_transform_hdr) +
4512                                                 sizeof(struct smb2_sync_hdr)) {
4513                 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4514                          pdu_length);
4515                 cifs_reconnect(server);
4516                 wake_up(&server->response_q);
4517                 return -ECONNABORTED;
4518         }
4519
4520         if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4521                 cifs_server_dbg(VFS, "Transform message is broken\n");
4522                 cifs_reconnect(server);
4523                 wake_up(&server->response_q);
4524                 return -ECONNABORTED;
4525         }
4526
4527         /* TODO: add support for compounds containing READ. */
4528         if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4529                 return receive_encrypted_read(server, &mids[0], num_mids);
4530         }
4531
4532         return receive_encrypted_standard(server, mids, bufs, num_mids);
4533 }
4534
4535 int
4536 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4537 {
4538         char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4539
4540         return handle_read_data(server, mid, buf, server->pdu_size,
4541                                 NULL, 0, 0);
4542 }
4543
4544 static int
4545 smb2_next_header(char *buf)
4546 {
4547         struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4548         struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4549
4550         if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4551                 return sizeof(struct smb2_transform_hdr) +
4552                   le32_to_cpu(t_hdr->OriginalMessageSize);
4553
4554         return le32_to_cpu(hdr->NextCommand);
4555 }
4556
4557 static int
4558 smb2_make_node(unsigned int xid, struct inode *inode,
4559                struct dentry *dentry, struct cifs_tcon *tcon,
4560                char *full_path, umode_t mode, dev_t dev)
4561 {
4562         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4563         int rc = -EPERM;
4564         FILE_ALL_INFO *buf = NULL;
4565         struct cifs_io_parms io_parms;
4566         __u32 oplock = 0;
4567         struct cifs_fid fid;
4568         struct cifs_open_parms oparms;
4569         unsigned int bytes_written;
4570         struct win_dev *pdev;
4571         struct kvec iov[2];
4572
4573         /*
4574          * Check if mounted with mount parm 'sfu' mount parm.
4575          * SFU emulation should work with all servers, but only
4576          * supports block and char device (no socket & fifo),
4577          * and was used by default in earlier versions of Windows
4578          */
4579         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4580                 goto out;
4581
4582         /*
4583          * TODO: Add ability to create instead via reparse point. Windows (e.g.
4584          * their current NFS server) uses this approach to expose special files
4585          * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4586          */
4587
4588         if (!S_ISCHR(mode) && !S_ISBLK(mode))
4589                 goto out;
4590
4591         cifs_dbg(FYI, "sfu compat create special file\n");
4592
4593         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4594         if (buf == NULL) {
4595                 rc = -ENOMEM;
4596                 goto out;
4597         }
4598
4599         oparms.tcon = tcon;
4600         oparms.cifs_sb = cifs_sb;
4601         oparms.desired_access = GENERIC_WRITE;
4602         oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4603                                                     CREATE_OPTION_SPECIAL);
4604         oparms.disposition = FILE_CREATE;
4605         oparms.path = full_path;
4606         oparms.fid = &fid;
4607         oparms.reconnect = false;
4608
4609         if (tcon->ses->server->oplocks)
4610                 oplock = REQ_OPLOCK;
4611         else
4612                 oplock = 0;
4613         rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4614         if (rc)
4615                 goto out;
4616
4617         /*
4618          * BB Do not bother to decode buf since no local inode yet to put
4619          * timestamps in, but we can reuse it safely.
4620          */
4621
4622         pdev = (struct win_dev *)buf;
4623         io_parms.pid = current->tgid;
4624         io_parms.tcon = tcon;
4625         io_parms.offset = 0;
4626         io_parms.length = sizeof(struct win_dev);
4627         iov[1].iov_base = buf;
4628         iov[1].iov_len = sizeof(struct win_dev);
4629         if (S_ISCHR(mode)) {
4630                 memcpy(pdev->type, "IntxCHR", 8);
4631                 pdev->major = cpu_to_le64(MAJOR(dev));
4632                 pdev->minor = cpu_to_le64(MINOR(dev));
4633                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4634                                                         &bytes_written, iov, 1);
4635         } else if (S_ISBLK(mode)) {
4636                 memcpy(pdev->type, "IntxBLK", 8);
4637                 pdev->major = cpu_to_le64(MAJOR(dev));
4638                 pdev->minor = cpu_to_le64(MINOR(dev));
4639                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4640                                                         &bytes_written, iov, 1);
4641         }
4642         tcon->ses->server->ops->close(xid, tcon, &fid);
4643         d_drop(dentry);
4644
4645         /* FIXME: add code here to set EAs */
4646 out:
4647         kfree(buf);
4648         return rc;
4649 }
4650
4651
4652 struct smb_version_operations smb20_operations = {
4653         .compare_fids = smb2_compare_fids,
4654         .setup_request = smb2_setup_request,
4655         .setup_async_request = smb2_setup_async_request,
4656         .check_receive = smb2_check_receive,
4657         .add_credits = smb2_add_credits,
4658         .set_credits = smb2_set_credits,
4659         .get_credits_field = smb2_get_credits_field,
4660         .get_credits = smb2_get_credits,
4661         .wait_mtu_credits = cifs_wait_mtu_credits,
4662         .get_next_mid = smb2_get_next_mid,
4663         .revert_current_mid = smb2_revert_current_mid,
4664         .read_data_offset = smb2_read_data_offset,
4665         .read_data_length = smb2_read_data_length,
4666         .map_error = map_smb2_to_linux_error,
4667         .find_mid = smb2_find_mid,
4668         .check_message = smb2_check_message,
4669         .dump_detail = smb2_dump_detail,
4670         .clear_stats = smb2_clear_stats,
4671         .print_stats = smb2_print_stats,
4672         .is_oplock_break = smb2_is_valid_oplock_break,
4673         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4674         .downgrade_oplock = smb2_downgrade_oplock,
4675         .need_neg = smb2_need_neg,
4676         .negotiate = smb2_negotiate,
4677         .negotiate_wsize = smb2_negotiate_wsize,
4678         .negotiate_rsize = smb2_negotiate_rsize,
4679         .sess_setup = SMB2_sess_setup,
4680         .logoff = SMB2_logoff,
4681         .tree_connect = SMB2_tcon,
4682         .tree_disconnect = SMB2_tdis,
4683         .qfs_tcon = smb2_qfs_tcon,
4684         .is_path_accessible = smb2_is_path_accessible,
4685         .can_echo = smb2_can_echo,
4686         .echo = SMB2_echo,
4687         .query_path_info = smb2_query_path_info,
4688         .get_srv_inum = smb2_get_srv_inum,
4689         .query_file_info = smb2_query_file_info,
4690         .set_path_size = smb2_set_path_size,
4691         .set_file_size = smb2_set_file_size,
4692         .set_file_info = smb2_set_file_info,
4693         .set_compression = smb2_set_compression,
4694         .mkdir = smb2_mkdir,
4695         .mkdir_setinfo = smb2_mkdir_setinfo,
4696         .rmdir = smb2_rmdir,
4697         .unlink = smb2_unlink,
4698         .rename = smb2_rename_path,
4699         .create_hardlink = smb2_create_hardlink,
4700         .query_symlink = smb2_query_symlink,
4701         .query_mf_symlink = smb3_query_mf_symlink,
4702         .create_mf_symlink = smb3_create_mf_symlink,
4703         .open = smb2_open_file,
4704         .set_fid = smb2_set_fid,
4705         .close = smb2_close_file,
4706         .flush = smb2_flush_file,
4707         .async_readv = smb2_async_readv,
4708         .async_writev = smb2_async_writev,
4709         .sync_read = smb2_sync_read,
4710         .sync_write = smb2_sync_write,
4711         .query_dir_first = smb2_query_dir_first,
4712         .query_dir_next = smb2_query_dir_next,
4713         .close_dir = smb2_close_dir,
4714         .calc_smb_size = smb2_calc_size,
4715         .is_status_pending = smb2_is_status_pending,
4716         .is_session_expired = smb2_is_session_expired,
4717         .oplock_response = smb2_oplock_response,
4718         .queryfs = smb2_queryfs,
4719         .mand_lock = smb2_mand_lock,
4720         .mand_unlock_range = smb2_unlock_range,
4721         .push_mand_locks = smb2_push_mandatory_locks,
4722         .get_lease_key = smb2_get_lease_key,
4723         .set_lease_key = smb2_set_lease_key,
4724         .new_lease_key = smb2_new_lease_key,
4725         .calc_signature = smb2_calc_signature,
4726         .is_read_op = smb2_is_read_op,
4727         .set_oplock_level = smb2_set_oplock_level,
4728         .create_lease_buf = smb2_create_lease_buf,
4729         .parse_lease_buf = smb2_parse_lease_buf,
4730         .copychunk_range = smb2_copychunk_range,
4731         .wp_retry_size = smb2_wp_retry_size,
4732         .dir_needs_close = smb2_dir_needs_close,
4733         .get_dfs_refer = smb2_get_dfs_refer,
4734         .select_sectype = smb2_select_sectype,
4735 #ifdef CONFIG_CIFS_XATTR
4736         .query_all_EAs = smb2_query_eas,
4737         .set_EA = smb2_set_ea,
4738 #endif /* CIFS_XATTR */
4739         .get_acl = get_smb2_acl,
4740         .get_acl_by_fid = get_smb2_acl_by_fid,
4741         .set_acl = set_smb2_acl,
4742         .next_header = smb2_next_header,
4743         .ioctl_query_info = smb2_ioctl_query_info,
4744         .make_node = smb2_make_node,
4745         .fiemap = smb3_fiemap,
4746         .llseek = smb3_llseek,
4747 };
4748
4749 struct smb_version_operations smb21_operations = {
4750         .compare_fids = smb2_compare_fids,
4751         .setup_request = smb2_setup_request,
4752         .setup_async_request = smb2_setup_async_request,
4753         .check_receive = smb2_check_receive,
4754         .add_credits = smb2_add_credits,
4755         .set_credits = smb2_set_credits,
4756         .get_credits_field = smb2_get_credits_field,
4757         .get_credits = smb2_get_credits,
4758         .wait_mtu_credits = smb2_wait_mtu_credits,
4759         .adjust_credits = smb2_adjust_credits,
4760         .get_next_mid = smb2_get_next_mid,
4761         .revert_current_mid = smb2_revert_current_mid,
4762         .read_data_offset = smb2_read_data_offset,
4763         .read_data_length = smb2_read_data_length,
4764         .map_error = map_smb2_to_linux_error,
4765         .find_mid = smb2_find_mid,
4766         .check_message = smb2_check_message,
4767         .dump_detail = smb2_dump_detail,
4768         .clear_stats = smb2_clear_stats,
4769         .print_stats = smb2_print_stats,
4770         .is_oplock_break = smb2_is_valid_oplock_break,
4771         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4772         .downgrade_oplock = smb2_downgrade_oplock,
4773         .need_neg = smb2_need_neg,
4774         .negotiate = smb2_negotiate,
4775         .negotiate_wsize = smb2_negotiate_wsize,
4776         .negotiate_rsize = smb2_negotiate_rsize,
4777         .sess_setup = SMB2_sess_setup,
4778         .logoff = SMB2_logoff,
4779         .tree_connect = SMB2_tcon,
4780         .tree_disconnect = SMB2_tdis,
4781         .qfs_tcon = smb2_qfs_tcon,
4782         .is_path_accessible = smb2_is_path_accessible,
4783         .can_echo = smb2_can_echo,
4784         .echo = SMB2_echo,
4785         .query_path_info = smb2_query_path_info,
4786         .get_srv_inum = smb2_get_srv_inum,
4787         .query_file_info = smb2_query_file_info,
4788         .set_path_size = smb2_set_path_size,
4789         .set_file_size = smb2_set_file_size,
4790         .set_file_info = smb2_set_file_info,
4791         .set_compression = smb2_set_compression,
4792         .mkdir = smb2_mkdir,
4793         .mkdir_setinfo = smb2_mkdir_setinfo,
4794         .rmdir = smb2_rmdir,
4795         .unlink = smb2_unlink,
4796         .rename = smb2_rename_path,
4797         .create_hardlink = smb2_create_hardlink,
4798         .query_symlink = smb2_query_symlink,
4799         .query_mf_symlink = smb3_query_mf_symlink,
4800         .create_mf_symlink = smb3_create_mf_symlink,
4801         .open = smb2_open_file,
4802         .set_fid = smb2_set_fid,
4803         .close = smb2_close_file,
4804         .flush = smb2_flush_file,
4805         .async_readv = smb2_async_readv,
4806         .async_writev = smb2_async_writev,
4807         .sync_read = smb2_sync_read,
4808         .sync_write = smb2_sync_write,
4809         .query_dir_first = smb2_query_dir_first,
4810         .query_dir_next = smb2_query_dir_next,
4811         .close_dir = smb2_close_dir,
4812         .calc_smb_size = smb2_calc_size,
4813         .is_status_pending = smb2_is_status_pending,
4814         .is_session_expired = smb2_is_session_expired,
4815         .oplock_response = smb2_oplock_response,
4816         .queryfs = smb2_queryfs,
4817         .mand_lock = smb2_mand_lock,
4818         .mand_unlock_range = smb2_unlock_range,
4819         .push_mand_locks = smb2_push_mandatory_locks,
4820         .get_lease_key = smb2_get_lease_key,
4821         .set_lease_key = smb2_set_lease_key,
4822         .new_lease_key = smb2_new_lease_key,
4823         .calc_signature = smb2_calc_signature,
4824         .is_read_op = smb21_is_read_op,
4825         .set_oplock_level = smb21_set_oplock_level,
4826         .create_lease_buf = smb2_create_lease_buf,
4827         .parse_lease_buf = smb2_parse_lease_buf,
4828         .copychunk_range = smb2_copychunk_range,
4829         .wp_retry_size = smb2_wp_retry_size,
4830         .dir_needs_close = smb2_dir_needs_close,
4831         .enum_snapshots = smb3_enum_snapshots,
4832         .notify = smb3_notify,
4833         .get_dfs_refer = smb2_get_dfs_refer,
4834         .select_sectype = smb2_select_sectype,
4835 #ifdef CONFIG_CIFS_XATTR
4836         .query_all_EAs = smb2_query_eas,
4837         .set_EA = smb2_set_ea,
4838 #endif /* CIFS_XATTR */
4839         .get_acl = get_smb2_acl,
4840         .get_acl_by_fid = get_smb2_acl_by_fid,
4841         .set_acl = set_smb2_acl,
4842         .next_header = smb2_next_header,
4843         .ioctl_query_info = smb2_ioctl_query_info,
4844         .make_node = smb2_make_node,
4845         .fiemap = smb3_fiemap,
4846         .llseek = smb3_llseek,
4847 };
4848
4849 struct smb_version_operations smb30_operations = {
4850         .compare_fids = smb2_compare_fids,
4851         .setup_request = smb2_setup_request,
4852         .setup_async_request = smb2_setup_async_request,
4853         .check_receive = smb2_check_receive,
4854         .add_credits = smb2_add_credits,
4855         .set_credits = smb2_set_credits,
4856         .get_credits_field = smb2_get_credits_field,
4857         .get_credits = smb2_get_credits,
4858         .wait_mtu_credits = smb2_wait_mtu_credits,
4859         .adjust_credits = smb2_adjust_credits,
4860         .get_next_mid = smb2_get_next_mid,
4861         .revert_current_mid = smb2_revert_current_mid,
4862         .read_data_offset = smb2_read_data_offset,
4863         .read_data_length = smb2_read_data_length,
4864         .map_error = map_smb2_to_linux_error,
4865         .find_mid = smb2_find_mid,
4866         .check_message = smb2_check_message,
4867         .dump_detail = smb2_dump_detail,
4868         .clear_stats = smb2_clear_stats,
4869         .print_stats = smb2_print_stats,
4870         .dump_share_caps = smb2_dump_share_caps,
4871         .is_oplock_break = smb2_is_valid_oplock_break,
4872         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4873         .downgrade_oplock = smb3_downgrade_oplock,
4874         .need_neg = smb2_need_neg,
4875         .negotiate = smb2_negotiate,
4876         .negotiate_wsize = smb3_negotiate_wsize,
4877         .negotiate_rsize = smb3_negotiate_rsize,
4878         .sess_setup = SMB2_sess_setup,
4879         .logoff = SMB2_logoff,
4880         .tree_connect = SMB2_tcon,
4881         .tree_disconnect = SMB2_tdis,
4882         .qfs_tcon = smb3_qfs_tcon,
4883         .is_path_accessible = smb2_is_path_accessible,
4884         .can_echo = smb2_can_echo,
4885         .echo = SMB2_echo,
4886         .query_path_info = smb2_query_path_info,
4887         .get_srv_inum = smb2_get_srv_inum,
4888         .query_file_info = smb2_query_file_info,
4889         .set_path_size = smb2_set_path_size,
4890         .set_file_size = smb2_set_file_size,
4891         .set_file_info = smb2_set_file_info,
4892         .set_compression = smb2_set_compression,
4893         .mkdir = smb2_mkdir,
4894         .mkdir_setinfo = smb2_mkdir_setinfo,
4895         .rmdir = smb2_rmdir,
4896         .unlink = smb2_unlink,
4897         .rename = smb2_rename_path,
4898         .create_hardlink = smb2_create_hardlink,
4899         .query_symlink = smb2_query_symlink,
4900         .query_mf_symlink = smb3_query_mf_symlink,
4901         .create_mf_symlink = smb3_create_mf_symlink,
4902         .open = smb2_open_file,
4903         .set_fid = smb2_set_fid,
4904         .close = smb2_close_file,
4905         .close_getattr = smb2_close_getattr,
4906         .flush = smb2_flush_file,
4907         .async_readv = smb2_async_readv,
4908         .async_writev = smb2_async_writev,
4909         .sync_read = smb2_sync_read,
4910         .sync_write = smb2_sync_write,
4911         .query_dir_first = smb2_query_dir_first,
4912         .query_dir_next = smb2_query_dir_next,
4913         .close_dir = smb2_close_dir,
4914         .calc_smb_size = smb2_calc_size,
4915         .is_status_pending = smb2_is_status_pending,
4916         .is_session_expired = smb2_is_session_expired,
4917         .oplock_response = smb2_oplock_response,
4918         .queryfs = smb2_queryfs,
4919         .mand_lock = smb2_mand_lock,
4920         .mand_unlock_range = smb2_unlock_range,
4921         .push_mand_locks = smb2_push_mandatory_locks,
4922         .get_lease_key = smb2_get_lease_key,
4923         .set_lease_key = smb2_set_lease_key,
4924         .new_lease_key = smb2_new_lease_key,
4925         .generate_signingkey = generate_smb30signingkey,
4926         .calc_signature = smb3_calc_signature,
4927         .set_integrity  = smb3_set_integrity,
4928         .is_read_op = smb21_is_read_op,
4929         .set_oplock_level = smb3_set_oplock_level,
4930         .create_lease_buf = smb3_create_lease_buf,
4931         .parse_lease_buf = smb3_parse_lease_buf,
4932         .copychunk_range = smb2_copychunk_range,
4933         .duplicate_extents = smb2_duplicate_extents,
4934         .validate_negotiate = smb3_validate_negotiate,
4935         .wp_retry_size = smb2_wp_retry_size,
4936         .dir_needs_close = smb2_dir_needs_close,
4937         .fallocate = smb3_fallocate,
4938         .enum_snapshots = smb3_enum_snapshots,
4939         .notify = smb3_notify,
4940         .init_transform_rq = smb3_init_transform_rq,
4941         .is_transform_hdr = smb3_is_transform_hdr,
4942         .receive_transform = smb3_receive_transform,
4943         .get_dfs_refer = smb2_get_dfs_refer,
4944         .select_sectype = smb2_select_sectype,
4945 #ifdef CONFIG_CIFS_XATTR
4946         .query_all_EAs = smb2_query_eas,
4947         .set_EA = smb2_set_ea,
4948 #endif /* CIFS_XATTR */
4949         .get_acl = get_smb2_acl,
4950         .get_acl_by_fid = get_smb2_acl_by_fid,
4951         .set_acl = set_smb2_acl,
4952         .next_header = smb2_next_header,
4953         .ioctl_query_info = smb2_ioctl_query_info,
4954         .make_node = smb2_make_node,
4955         .fiemap = smb3_fiemap,
4956         .llseek = smb3_llseek,
4957 };
4958
4959 struct smb_version_operations smb311_operations = {
4960         .compare_fids = smb2_compare_fids,
4961         .setup_request = smb2_setup_request,
4962         .setup_async_request = smb2_setup_async_request,
4963         .check_receive = smb2_check_receive,
4964         .add_credits = smb2_add_credits,
4965         .set_credits = smb2_set_credits,
4966         .get_credits_field = smb2_get_credits_field,
4967         .get_credits = smb2_get_credits,
4968         .wait_mtu_credits = smb2_wait_mtu_credits,
4969         .adjust_credits = smb2_adjust_credits,
4970         .get_next_mid = smb2_get_next_mid,
4971         .revert_current_mid = smb2_revert_current_mid,
4972         .read_data_offset = smb2_read_data_offset,
4973         .read_data_length = smb2_read_data_length,
4974         .map_error = map_smb2_to_linux_error,
4975         .find_mid = smb2_find_mid,
4976         .check_message = smb2_check_message,
4977         .dump_detail = smb2_dump_detail,
4978         .clear_stats = smb2_clear_stats,
4979         .print_stats = smb2_print_stats,
4980         .dump_share_caps = smb2_dump_share_caps,
4981         .is_oplock_break = smb2_is_valid_oplock_break,
4982         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4983         .downgrade_oplock = smb3_downgrade_oplock,
4984         .need_neg = smb2_need_neg,
4985         .negotiate = smb2_negotiate,
4986         .negotiate_wsize = smb3_negotiate_wsize,
4987         .negotiate_rsize = smb3_negotiate_rsize,
4988         .sess_setup = SMB2_sess_setup,
4989         .logoff = SMB2_logoff,
4990         .tree_connect = SMB2_tcon,
4991         .tree_disconnect = SMB2_tdis,
4992         .qfs_tcon = smb3_qfs_tcon,
4993         .is_path_accessible = smb2_is_path_accessible,
4994         .can_echo = smb2_can_echo,
4995         .echo = SMB2_echo,
4996         .query_path_info = smb2_query_path_info,
4997         .get_srv_inum = smb2_get_srv_inum,
4998         .query_file_info = smb2_query_file_info,
4999         .set_path_size = smb2_set_path_size,
5000         .set_file_size = smb2_set_file_size,
5001         .set_file_info = smb2_set_file_info,
5002         .set_compression = smb2_set_compression,
5003         .mkdir = smb2_mkdir,
5004         .mkdir_setinfo = smb2_mkdir_setinfo,
5005         .posix_mkdir = smb311_posix_mkdir,
5006         .rmdir = smb2_rmdir,
5007         .unlink = smb2_unlink,
5008         .rename = smb2_rename_path,
5009         .create_hardlink = smb2_create_hardlink,
5010         .query_symlink = smb2_query_symlink,
5011         .query_mf_symlink = smb3_query_mf_symlink,
5012         .create_mf_symlink = smb3_create_mf_symlink,
5013         .open = smb2_open_file,
5014         .set_fid = smb2_set_fid,
5015         .close = smb2_close_file,
5016         .close_getattr = smb2_close_getattr,
5017         .flush = smb2_flush_file,
5018         .async_readv = smb2_async_readv,
5019         .async_writev = smb2_async_writev,
5020         .sync_read = smb2_sync_read,
5021         .sync_write = smb2_sync_write,
5022         .query_dir_first = smb2_query_dir_first,
5023         .query_dir_next = smb2_query_dir_next,
5024         .close_dir = smb2_close_dir,
5025         .calc_smb_size = smb2_calc_size,
5026         .is_status_pending = smb2_is_status_pending,
5027         .is_session_expired = smb2_is_session_expired,
5028         .oplock_response = smb2_oplock_response,
5029         .queryfs = smb311_queryfs,
5030         .mand_lock = smb2_mand_lock,
5031         .mand_unlock_range = smb2_unlock_range,
5032         .push_mand_locks = smb2_push_mandatory_locks,
5033         .get_lease_key = smb2_get_lease_key,
5034         .set_lease_key = smb2_set_lease_key,
5035         .new_lease_key = smb2_new_lease_key,
5036         .generate_signingkey = generate_smb311signingkey,
5037         .calc_signature = smb3_calc_signature,
5038         .set_integrity  = smb3_set_integrity,
5039         .is_read_op = smb21_is_read_op,
5040         .set_oplock_level = smb3_set_oplock_level,
5041         .create_lease_buf = smb3_create_lease_buf,
5042         .parse_lease_buf = smb3_parse_lease_buf,
5043         .copychunk_range = smb2_copychunk_range,
5044         .duplicate_extents = smb2_duplicate_extents,
5045 /*      .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5046         .wp_retry_size = smb2_wp_retry_size,
5047         .dir_needs_close = smb2_dir_needs_close,
5048         .fallocate = smb3_fallocate,
5049         .enum_snapshots = smb3_enum_snapshots,
5050         .notify = smb3_notify,
5051         .init_transform_rq = smb3_init_transform_rq,
5052         .is_transform_hdr = smb3_is_transform_hdr,
5053         .receive_transform = smb3_receive_transform,
5054         .get_dfs_refer = smb2_get_dfs_refer,
5055         .select_sectype = smb2_select_sectype,
5056 #ifdef CONFIG_CIFS_XATTR
5057         .query_all_EAs = smb2_query_eas,
5058         .set_EA = smb2_set_ea,
5059 #endif /* CIFS_XATTR */
5060         .get_acl = get_smb2_acl,
5061         .get_acl_by_fid = get_smb2_acl_by_fid,
5062         .set_acl = set_smb2_acl,
5063         .next_header = smb2_next_header,
5064         .ioctl_query_info = smb2_ioctl_query_info,
5065         .make_node = smb2_make_node,
5066         .fiemap = smb3_fiemap,
5067         .llseek = smb3_llseek,
5068 };
5069
5070 struct smb_version_values smb20_values = {
5071         .version_string = SMB20_VERSION_STRING,
5072         .protocol_id = SMB20_PROT_ID,
5073         .req_capabilities = 0, /* MBZ */
5074         .large_lock_type = 0,
5075         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5076         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5077         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5078         .header_size = sizeof(struct smb2_sync_hdr),
5079         .header_preamble_size = 0,
5080         .max_header_size = MAX_SMB2_HDR_SIZE,
5081         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5082         .lock_cmd = SMB2_LOCK,
5083         .cap_unix = 0,
5084         .cap_nt_find = SMB2_NT_FIND,
5085         .cap_large_files = SMB2_LARGE_FILES,
5086         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5087         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5088         .create_lease_size = sizeof(struct create_lease),
5089 };
5090
5091 struct smb_version_values smb21_values = {
5092         .version_string = SMB21_VERSION_STRING,
5093         .protocol_id = SMB21_PROT_ID,
5094         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5095         .large_lock_type = 0,
5096         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5097         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5098         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5099         .header_size = sizeof(struct smb2_sync_hdr),
5100         .header_preamble_size = 0,
5101         .max_header_size = MAX_SMB2_HDR_SIZE,
5102         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5103         .lock_cmd = SMB2_LOCK,
5104         .cap_unix = 0,
5105         .cap_nt_find = SMB2_NT_FIND,
5106         .cap_large_files = SMB2_LARGE_FILES,
5107         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5108         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5109         .create_lease_size = sizeof(struct create_lease),
5110 };
5111
5112 struct smb_version_values smb3any_values = {
5113         .version_string = SMB3ANY_VERSION_STRING,
5114         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5115         .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,
5116         .large_lock_type = 0,
5117         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5118         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5119         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5120         .header_size = sizeof(struct smb2_sync_hdr),
5121         .header_preamble_size = 0,
5122         .max_header_size = MAX_SMB2_HDR_SIZE,
5123         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5124         .lock_cmd = SMB2_LOCK,
5125         .cap_unix = 0,
5126         .cap_nt_find = SMB2_NT_FIND,
5127         .cap_large_files = SMB2_LARGE_FILES,
5128         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5129         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5130         .create_lease_size = sizeof(struct create_lease_v2),
5131 };
5132
5133 struct smb_version_values smbdefault_values = {
5134         .version_string = SMBDEFAULT_VERSION_STRING,
5135         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5136         .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,
5137         .large_lock_type = 0,
5138         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5139         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5140         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5141         .header_size = sizeof(struct smb2_sync_hdr),
5142         .header_preamble_size = 0,
5143         .max_header_size = MAX_SMB2_HDR_SIZE,
5144         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5145         .lock_cmd = SMB2_LOCK,
5146         .cap_unix = 0,
5147         .cap_nt_find = SMB2_NT_FIND,
5148         .cap_large_files = SMB2_LARGE_FILES,
5149         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5150         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5151         .create_lease_size = sizeof(struct create_lease_v2),
5152 };
5153
5154 struct smb_version_values smb30_values = {
5155         .version_string = SMB30_VERSION_STRING,
5156         .protocol_id = SMB30_PROT_ID,
5157         .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,
5158         .large_lock_type = 0,
5159         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5160         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5161         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5162         .header_size = sizeof(struct smb2_sync_hdr),
5163         .header_preamble_size = 0,
5164         .max_header_size = MAX_SMB2_HDR_SIZE,
5165         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5166         .lock_cmd = SMB2_LOCK,
5167         .cap_unix = 0,
5168         .cap_nt_find = SMB2_NT_FIND,
5169         .cap_large_files = SMB2_LARGE_FILES,
5170         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5171         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5172         .create_lease_size = sizeof(struct create_lease_v2),
5173 };
5174
5175 struct smb_version_values smb302_values = {
5176         .version_string = SMB302_VERSION_STRING,
5177         .protocol_id = SMB302_PROT_ID,
5178         .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,
5179         .large_lock_type = 0,
5180         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5181         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5182         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5183         .header_size = sizeof(struct smb2_sync_hdr),
5184         .header_preamble_size = 0,
5185         .max_header_size = MAX_SMB2_HDR_SIZE,
5186         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5187         .lock_cmd = SMB2_LOCK,
5188         .cap_unix = 0,
5189         .cap_nt_find = SMB2_NT_FIND,
5190         .cap_large_files = SMB2_LARGE_FILES,
5191         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5192         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5193         .create_lease_size = sizeof(struct create_lease_v2),
5194 };
5195
5196 struct smb_version_values smb311_values = {
5197         .version_string = SMB311_VERSION_STRING,
5198         .protocol_id = SMB311_PROT_ID,
5199         .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,
5200         .large_lock_type = 0,
5201         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5202         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5203         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5204         .header_size = sizeof(struct smb2_sync_hdr),
5205         .header_preamble_size = 0,
5206         .max_header_size = MAX_SMB2_HDR_SIZE,
5207         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5208         .lock_cmd = SMB2_LOCK,
5209         .cap_unix = 0,
5210         .cap_nt_find = SMB2_NT_FIND,
5211         .cap_large_files = SMB2_LARGE_FILES,
5212         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5213         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5214         .create_lease_size = sizeof(struct create_lease_v2),
5215 };