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