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