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