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