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