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