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