949cd1177147f036d5e84ee09c4f32b384ec9298
[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)
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         free_xid(xid);
3233
3234         cifs_put_tlink(tlink);
3235
3236         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3237         if (rc)
3238                 return ERR_PTR(rc);
3239         return pntsd;
3240
3241 }
3242
3243 static struct cifs_ntsd *
3244 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3245                 const char *path, u32 *pacllen)
3246 {
3247         struct cifs_ntsd *pntsd = NULL;
3248         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3249         unsigned int xid;
3250         int rc;
3251         struct cifs_tcon *tcon;
3252         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3253         struct cifs_fid fid;
3254         struct cifs_open_parms oparms;
3255         __le16 *utf16_path;
3256
3257         cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3258         if (IS_ERR(tlink))
3259                 return ERR_CAST(tlink);
3260
3261         tcon = tlink_tcon(tlink);
3262         xid = get_xid();
3263
3264         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3265         if (!utf16_path) {
3266                 rc = -ENOMEM;
3267                 free_xid(xid);
3268                 return ERR_PTR(rc);
3269         }
3270
3271         oparms.tcon = tcon;
3272         oparms.desired_access = READ_CONTROL;
3273         oparms.disposition = FILE_OPEN;
3274         /*
3275          * When querying an ACL, even if the file is a symlink we want to open
3276          * the source not the target, and so the protocol requires that the
3277          * client specify this flag when opening a reparse point
3278          */
3279         oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3280         oparms.fid = &fid;
3281         oparms.reconnect = false;
3282
3283         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3284                        NULL);
3285         kfree(utf16_path);
3286         if (!rc) {
3287                 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3288                             fid.volatile_fid, (void **)&pntsd, pacllen);
3289                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3290         }
3291
3292         cifs_put_tlink(tlink);
3293         free_xid(xid);
3294
3295         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3296         if (rc)
3297                 return ERR_PTR(rc);
3298         return pntsd;
3299 }
3300
3301 static int
3302 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3303                 struct inode *inode, const char *path, int aclflag)
3304 {
3305         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3306         unsigned int xid;
3307         int rc, access_flags = 0;
3308         struct cifs_tcon *tcon;
3309         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3310         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3311         struct cifs_fid fid;
3312         struct cifs_open_parms oparms;
3313         __le16 *utf16_path;
3314
3315         cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3316         if (IS_ERR(tlink))
3317                 return PTR_ERR(tlink);
3318
3319         tcon = tlink_tcon(tlink);
3320         xid = get_xid();
3321
3322         if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3323                 access_flags = WRITE_OWNER;
3324         else
3325                 access_flags = WRITE_DAC;
3326
3327         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3328         if (!utf16_path) {
3329                 rc = -ENOMEM;
3330                 free_xid(xid);
3331                 return rc;
3332         }
3333
3334         oparms.tcon = tcon;
3335         oparms.desired_access = access_flags;
3336         oparms.create_options = cifs_create_options(cifs_sb, 0);
3337         oparms.disposition = FILE_OPEN;
3338         oparms.path = path;
3339         oparms.fid = &fid;
3340         oparms.reconnect = false;
3341
3342         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3343                        NULL, NULL);
3344         kfree(utf16_path);
3345         if (!rc) {
3346                 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3347                             fid.volatile_fid, pnntsd, acllen, aclflag);
3348                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3349         }
3350
3351         cifs_put_tlink(tlink);
3352         free_xid(xid);
3353         return rc;
3354 }
3355
3356 /* Retrieve an ACL from the server */
3357 static struct cifs_ntsd *
3358 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3359                                       struct inode *inode, const char *path,
3360                                       u32 *pacllen)
3361 {
3362         struct cifs_ntsd *pntsd = NULL;
3363         struct cifsFileInfo *open_file = NULL;
3364
3365         if (inode)
3366                 open_file = find_readable_file(CIFS_I(inode), true);
3367         if (!open_file)
3368                 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3369
3370         pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3371         cifsFileInfo_put(open_file);
3372         return pntsd;
3373 }
3374
3375 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3376                             loff_t offset, loff_t len, bool keep_size)
3377 {
3378         struct cifs_ses *ses = tcon->ses;
3379         struct inode *inode;
3380         struct cifsInodeInfo *cifsi;
3381         struct cifsFileInfo *cfile = file->private_data;
3382         struct file_zero_data_information fsctl_buf;
3383         long rc;
3384         unsigned int xid;
3385         __le64 eof;
3386
3387         xid = get_xid();
3388
3389         inode = d_inode(cfile->dentry);
3390         cifsi = CIFS_I(inode);
3391
3392         trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3393                               ses->Suid, offset, len);
3394
3395         /*
3396          * We zero the range through ioctl, so we need remove the page caches
3397          * first, otherwise the data may be inconsistent with the server.
3398          */
3399         truncate_pagecache_range(inode, offset, offset + len - 1);
3400
3401         /* if file not oplocked can't be sure whether asking to extend size */
3402         if (!CIFS_CACHE_READ(cifsi))
3403                 if (keep_size == false) {
3404                         rc = -EOPNOTSUPP;
3405                         trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3406                                 tcon->tid, ses->Suid, offset, len, rc);
3407                         free_xid(xid);
3408                         return rc;
3409                 }
3410
3411         cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3412
3413         fsctl_buf.FileOffset = cpu_to_le64(offset);
3414         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3415
3416         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3417                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3418                         (char *)&fsctl_buf,
3419                         sizeof(struct file_zero_data_information),
3420                         0, NULL, NULL);
3421         if (rc)
3422                 goto zero_range_exit;
3423
3424         /*
3425          * do we also need to change the size of the file?
3426          */
3427         if (keep_size == false && i_size_read(inode) < offset + len) {
3428                 eof = cpu_to_le64(offset + len);
3429                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3430                                   cfile->fid.volatile_fid, cfile->pid, &eof);
3431         }
3432
3433  zero_range_exit:
3434         free_xid(xid);
3435         if (rc)
3436                 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3437                               ses->Suid, offset, len, rc);
3438         else
3439                 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3440                               ses->Suid, offset, len);
3441         return rc;
3442 }
3443
3444 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3445                             loff_t offset, loff_t len)
3446 {
3447         struct inode *inode;
3448         struct cifsFileInfo *cfile = file->private_data;
3449         struct file_zero_data_information fsctl_buf;
3450         long rc;
3451         unsigned int xid;
3452         __u8 set_sparse = 1;
3453
3454         xid = get_xid();
3455
3456         inode = d_inode(cfile->dentry);
3457
3458         /* Need to make file sparse, if not already, before freeing range. */
3459         /* Consider adding equivalent for compressed since it could also work */
3460         if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3461                 rc = -EOPNOTSUPP;
3462                 free_xid(xid);
3463                 return rc;
3464         }
3465
3466         /*
3467          * We implement the punch hole through ioctl, so we need remove the page
3468          * caches first, otherwise the data may be inconsistent with the server.
3469          */
3470         truncate_pagecache_range(inode, offset, offset + len - 1);
3471
3472         cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3473
3474         fsctl_buf.FileOffset = cpu_to_le64(offset);
3475         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3476
3477         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3478                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3479                         true /* is_fctl */, (char *)&fsctl_buf,
3480                         sizeof(struct file_zero_data_information),
3481                         CIFSMaxBufSize, NULL, NULL);
3482         free_xid(xid);
3483         return rc;
3484 }
3485
3486 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3487                             loff_t off, loff_t len, bool keep_size)
3488 {
3489         struct inode *inode;
3490         struct cifsInodeInfo *cifsi;
3491         struct cifsFileInfo *cfile = file->private_data;
3492         long rc = -EOPNOTSUPP;
3493         unsigned int xid;
3494         __le64 eof;
3495
3496         xid = get_xid();
3497
3498         inode = d_inode(cfile->dentry);
3499         cifsi = CIFS_I(inode);
3500
3501         trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3502                                 tcon->ses->Suid, off, len);
3503         /* if file not oplocked can't be sure whether asking to extend size */
3504         if (!CIFS_CACHE_READ(cifsi))
3505                 if (keep_size == false) {
3506                         trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3507                                 tcon->tid, tcon->ses->Suid, off, len, rc);
3508                         free_xid(xid);
3509                         return rc;
3510                 }
3511
3512         /*
3513          * Extending the file
3514          */
3515         if ((keep_size == false) && i_size_read(inode) < off + len) {
3516                 rc = inode_newsize_ok(inode, off + len);
3517                 if (rc)
3518                         goto out;
3519
3520                 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3521                         smb2_set_sparse(xid, tcon, cfile, inode, false);
3522
3523                 eof = cpu_to_le64(off + len);
3524                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3525                                   cfile->fid.volatile_fid, cfile->pid, &eof);
3526                 if (rc == 0) {
3527                         cifsi->server_eof = off + len;
3528                         cifs_setsize(inode, off + len);
3529                         cifs_truncate_page(inode->i_mapping, inode->i_size);
3530                         truncate_setsize(inode, off + len);
3531                 }
3532                 goto out;
3533         }
3534
3535         /*
3536          * Files are non-sparse by default so falloc may be a no-op
3537          * Must check if file sparse. If not sparse, and since we are not
3538          * extending then no need to do anything since file already allocated
3539          */
3540         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3541                 rc = 0;
3542                 goto out;
3543         }
3544
3545         if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3546                 /*
3547                  * Check if falloc starts within first few pages of file
3548                  * and ends within a few pages of the end of file to
3549                  * ensure that most of file is being forced to be
3550                  * fallocated now. If so then setting whole file sparse
3551                  * ie potentially making a few extra pages at the beginning
3552                  * or end of the file non-sparse via set_sparse is harmless.
3553                  */
3554                 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3555                         rc = -EOPNOTSUPP;
3556                         goto out;
3557                 }
3558         }
3559
3560         smb2_set_sparse(xid, tcon, cfile, inode, false);
3561         rc = 0;
3562
3563 out:
3564         if (rc)
3565                 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3566                                 tcon->ses->Suid, off, len, rc);
3567         else
3568                 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3569                                 tcon->ses->Suid, off, len);
3570
3571         free_xid(xid);
3572         return rc;
3573 }
3574
3575 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3576 {
3577         struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3578         struct cifsInodeInfo *cifsi;
3579         struct inode *inode;
3580         int rc = 0;
3581         struct file_allocated_range_buffer in_data, *out_data = NULL;
3582         u32 out_data_len;
3583         unsigned int xid;
3584
3585         if (whence != SEEK_HOLE && whence != SEEK_DATA)
3586                 return generic_file_llseek(file, offset, whence);
3587
3588         inode = d_inode(cfile->dentry);
3589         cifsi = CIFS_I(inode);
3590
3591         if (offset < 0 || offset >= i_size_read(inode))
3592                 return -ENXIO;
3593
3594         xid = get_xid();
3595         /*
3596          * We need to be sure that all dirty pages are written as they
3597          * might fill holes on the server.
3598          * Note that we also MUST flush any written pages since at least
3599          * some servers (Windows2016) will not reflect recent writes in
3600          * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3601          */
3602         wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3603         if (wrcfile) {
3604                 filemap_write_and_wait(inode->i_mapping);
3605                 smb2_flush_file(xid, tcon, &wrcfile->fid);
3606                 cifsFileInfo_put(wrcfile);
3607         }
3608
3609         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3610                 if (whence == SEEK_HOLE)
3611                         offset = i_size_read(inode);
3612                 goto lseek_exit;
3613         }
3614
3615         in_data.file_offset = cpu_to_le64(offset);
3616         in_data.length = cpu_to_le64(i_size_read(inode));
3617
3618         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3619                         cfile->fid.volatile_fid,
3620                         FSCTL_QUERY_ALLOCATED_RANGES, true,
3621                         (char *)&in_data, sizeof(in_data),
3622                         sizeof(struct file_allocated_range_buffer),
3623                         (char **)&out_data, &out_data_len);
3624         if (rc == -E2BIG)
3625                 rc = 0;
3626         if (rc)
3627                 goto lseek_exit;
3628
3629         if (whence == SEEK_HOLE && out_data_len == 0)
3630                 goto lseek_exit;
3631
3632         if (whence == SEEK_DATA && out_data_len == 0) {
3633                 rc = -ENXIO;
3634                 goto lseek_exit;
3635         }
3636
3637         if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3638                 rc = -EINVAL;
3639                 goto lseek_exit;
3640         }
3641         if (whence == SEEK_DATA) {
3642                 offset = le64_to_cpu(out_data->file_offset);
3643                 goto lseek_exit;
3644         }
3645         if (offset < le64_to_cpu(out_data->file_offset))
3646                 goto lseek_exit;
3647
3648         offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3649
3650  lseek_exit:
3651         free_xid(xid);
3652         kfree(out_data);
3653         if (!rc)
3654                 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3655         else
3656                 return rc;
3657 }
3658
3659 static int smb3_fiemap(struct cifs_tcon *tcon,
3660                        struct cifsFileInfo *cfile,
3661                        struct fiemap_extent_info *fei, u64 start, u64 len)
3662 {
3663         unsigned int xid;
3664         struct file_allocated_range_buffer in_data, *out_data;
3665         u32 out_data_len;
3666         int i, num, rc, flags, last_blob;
3667         u64 next;
3668
3669         rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3670         if (rc)
3671                 return rc;
3672
3673         xid = get_xid();
3674  again:
3675         in_data.file_offset = cpu_to_le64(start);
3676         in_data.length = cpu_to_le64(len);
3677
3678         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3679                         cfile->fid.volatile_fid,
3680                         FSCTL_QUERY_ALLOCATED_RANGES, true,
3681                         (char *)&in_data, sizeof(in_data),
3682                         1024 * sizeof(struct file_allocated_range_buffer),
3683                         (char **)&out_data, &out_data_len);
3684         if (rc == -E2BIG) {
3685                 last_blob = 0;
3686                 rc = 0;
3687         } else
3688                 last_blob = 1;
3689         if (rc)
3690                 goto out;
3691
3692         if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3693                 rc = -EINVAL;
3694                 goto out;
3695         }
3696         if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3697                 rc = -EINVAL;
3698                 goto out;
3699         }
3700
3701         num = out_data_len / sizeof(struct file_allocated_range_buffer);
3702         for (i = 0; i < num; i++) {
3703                 flags = 0;
3704                 if (i == num - 1 && last_blob)
3705                         flags |= FIEMAP_EXTENT_LAST;
3706
3707                 rc = fiemap_fill_next_extent(fei,
3708                                 le64_to_cpu(out_data[i].file_offset),
3709                                 le64_to_cpu(out_data[i].file_offset),
3710                                 le64_to_cpu(out_data[i].length),
3711                                 flags);
3712                 if (rc < 0)
3713                         goto out;
3714                 if (rc == 1) {
3715                         rc = 0;
3716                         goto out;
3717                 }
3718         }
3719
3720         if (!last_blob) {
3721                 next = le64_to_cpu(out_data[num - 1].file_offset) +
3722                   le64_to_cpu(out_data[num - 1].length);
3723                 len = len - (next - start);
3724                 start = next;
3725                 goto again;
3726         }
3727
3728  out:
3729         free_xid(xid);
3730         kfree(out_data);
3731         return rc;
3732 }
3733
3734 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3735                            loff_t off, loff_t len)
3736 {
3737         /* KEEP_SIZE already checked for by do_fallocate */
3738         if (mode & FALLOC_FL_PUNCH_HOLE)
3739                 return smb3_punch_hole(file, tcon, off, len);
3740         else if (mode & FALLOC_FL_ZERO_RANGE) {
3741                 if (mode & FALLOC_FL_KEEP_SIZE)
3742                         return smb3_zero_range(file, tcon, off, len, true);
3743                 return smb3_zero_range(file, tcon, off, len, false);
3744         } else if (mode == FALLOC_FL_KEEP_SIZE)
3745                 return smb3_simple_falloc(file, tcon, off, len, true);
3746         else if (mode == 0)
3747                 return smb3_simple_falloc(file, tcon, off, len, false);
3748
3749         return -EOPNOTSUPP;
3750 }
3751
3752 static void
3753 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3754                       struct cifsInodeInfo *cinode, __u32 oplock,
3755                       unsigned int epoch, bool *purge_cache)
3756 {
3757         server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3758 }
3759
3760 static void
3761 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3762                        unsigned int epoch, bool *purge_cache);
3763
3764 static void
3765 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3766                        struct cifsInodeInfo *cinode, __u32 oplock,
3767                        unsigned int epoch, bool *purge_cache)
3768 {
3769         unsigned int old_state = cinode->oplock;
3770         unsigned int old_epoch = cinode->epoch;
3771         unsigned int new_state;
3772
3773         if (epoch > old_epoch) {
3774                 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3775                 cinode->epoch = epoch;
3776         }
3777
3778         new_state = cinode->oplock;
3779         *purge_cache = false;
3780
3781         if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3782             (new_state & CIFS_CACHE_READ_FLG) == 0)
3783                 *purge_cache = true;
3784         else if (old_state == new_state && (epoch - old_epoch > 1))
3785                 *purge_cache = true;
3786 }
3787
3788 static void
3789 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3790                       unsigned int epoch, bool *purge_cache)
3791 {
3792         oplock &= 0xFF;
3793         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3794                 return;
3795         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3796                 cinode->oplock = CIFS_CACHE_RHW_FLG;
3797                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3798                          &cinode->vfs_inode);
3799         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3800                 cinode->oplock = CIFS_CACHE_RW_FLG;
3801                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3802                          &cinode->vfs_inode);
3803         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3804                 cinode->oplock = CIFS_CACHE_READ_FLG;
3805                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3806                          &cinode->vfs_inode);
3807         } else
3808                 cinode->oplock = 0;
3809 }
3810
3811 static void
3812 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3813                        unsigned int epoch, bool *purge_cache)
3814 {
3815         char message[5] = {0};
3816         unsigned int new_oplock = 0;
3817
3818         oplock &= 0xFF;
3819         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3820                 return;
3821
3822         /* Check if the server granted an oplock rather than a lease */
3823         if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3824                 return smb2_set_oplock_level(cinode, oplock, epoch,
3825                                              purge_cache);
3826
3827         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3828                 new_oplock |= CIFS_CACHE_READ_FLG;
3829                 strcat(message, "R");
3830         }
3831         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3832                 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3833                 strcat(message, "H");
3834         }
3835         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3836                 new_oplock |= CIFS_CACHE_WRITE_FLG;
3837                 strcat(message, "W");
3838         }
3839         if (!new_oplock)
3840                 strncpy(message, "None", sizeof(message));
3841
3842         cinode->oplock = new_oplock;
3843         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3844                  &cinode->vfs_inode);
3845 }
3846
3847 static void
3848 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3849                       unsigned int epoch, bool *purge_cache)
3850 {
3851         unsigned int old_oplock = cinode->oplock;
3852
3853         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3854
3855         if (purge_cache) {
3856                 *purge_cache = false;
3857                 if (old_oplock == CIFS_CACHE_READ_FLG) {
3858                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3859                             (epoch - cinode->epoch > 0))
3860                                 *purge_cache = true;
3861                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3862                                  (epoch - cinode->epoch > 1))
3863                                 *purge_cache = true;
3864                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3865                                  (epoch - cinode->epoch > 1))
3866                                 *purge_cache = true;
3867                         else if (cinode->oplock == 0 &&
3868                                  (epoch - cinode->epoch > 0))
3869                                 *purge_cache = true;
3870                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3871                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3872                             (epoch - cinode->epoch > 0))
3873                                 *purge_cache = true;
3874                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3875                                  (epoch - cinode->epoch > 1))
3876                                 *purge_cache = true;
3877                 }
3878                 cinode->epoch = epoch;
3879         }
3880 }
3881
3882 static bool
3883 smb2_is_read_op(__u32 oplock)
3884 {
3885         return oplock == SMB2_OPLOCK_LEVEL_II;
3886 }
3887
3888 static bool
3889 smb21_is_read_op(__u32 oplock)
3890 {
3891         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3892                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3893 }
3894
3895 static __le32
3896 map_oplock_to_lease(u8 oplock)
3897 {
3898         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3899                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3900         else if (oplock == SMB2_OPLOCK_LEVEL_II)
3901                 return SMB2_LEASE_READ_CACHING;
3902         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3903                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3904                        SMB2_LEASE_WRITE_CACHING;
3905         return 0;
3906 }
3907
3908 static char *
3909 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3910 {
3911         struct create_lease *buf;
3912
3913         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3914         if (!buf)
3915                 return NULL;
3916
3917         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3918         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3919
3920         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3921                                         (struct create_lease, lcontext));
3922         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3923         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3924                                 (struct create_lease, Name));
3925         buf->ccontext.NameLength = cpu_to_le16(4);
3926         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3927         buf->Name[0] = 'R';
3928         buf->Name[1] = 'q';
3929         buf->Name[2] = 'L';
3930         buf->Name[3] = 's';
3931         return (char *)buf;
3932 }
3933
3934 static char *
3935 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3936 {
3937         struct create_lease_v2 *buf;
3938
3939         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3940         if (!buf)
3941                 return NULL;
3942
3943         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3944         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3945
3946         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3947                                         (struct create_lease_v2, lcontext));
3948         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3949         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3950                                 (struct create_lease_v2, Name));
3951         buf->ccontext.NameLength = cpu_to_le16(4);
3952         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3953         buf->Name[0] = 'R';
3954         buf->Name[1] = 'q';
3955         buf->Name[2] = 'L';
3956         buf->Name[3] = 's';
3957         return (char *)buf;
3958 }
3959
3960 static __u8
3961 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3962 {
3963         struct create_lease *lc = (struct create_lease *)buf;
3964
3965         *epoch = 0; /* not used */
3966         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3967                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3968         return le32_to_cpu(lc->lcontext.LeaseState);
3969 }
3970
3971 static __u8
3972 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3973 {
3974         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3975
3976         *epoch = le16_to_cpu(lc->lcontext.Epoch);
3977         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3978                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3979         if (lease_key)
3980                 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3981         return le32_to_cpu(lc->lcontext.LeaseState);
3982 }
3983
3984 static unsigned int
3985 smb2_wp_retry_size(struct inode *inode)
3986 {
3987         return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
3988                      SMB2_MAX_BUFFER_SIZE);
3989 }
3990
3991 static bool
3992 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3993 {
3994         return !cfile->invalidHandle;
3995 }
3996
3997 static void
3998 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3999                    struct smb_rqst *old_rq, __le16 cipher_type)
4000 {
4001         struct smb2_sync_hdr *shdr =
4002                         (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
4003
4004         memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4005         tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4006         tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4007         tr_hdr->Flags = cpu_to_le16(0x01);
4008         if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4009             (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4010                 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4011         else
4012                 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4013         memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4014 }
4015
4016 /* We can not use the normal sg_set_buf() as we will sometimes pass a
4017  * stack object as buf.
4018  */
4019 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4020                                    unsigned int buflen)
4021 {
4022         void *addr;
4023         /*
4024          * VMAP_STACK (at least) puts stack into the vmalloc address space
4025          */
4026         if (is_vmalloc_addr(buf))
4027                 addr = vmalloc_to_page(buf);
4028         else
4029                 addr = virt_to_page(buf);
4030         sg_set_page(sg, addr, buflen, offset_in_page(buf));
4031 }
4032
4033 /* Assumes the first rqst has a transform header as the first iov.
4034  * I.e.
4035  * rqst[0].rq_iov[0]  is transform header
4036  * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4037  * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
4038  */
4039 static struct scatterlist *
4040 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
4041 {
4042         unsigned int sg_len;
4043         struct scatterlist *sg;
4044         unsigned int i;
4045         unsigned int j;
4046         unsigned int idx = 0;
4047         int skip;
4048
4049         sg_len = 1;
4050         for (i = 0; i < num_rqst; i++)
4051                 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4052
4053         sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4054         if (!sg)
4055                 return NULL;
4056
4057         sg_init_table(sg, sg_len);
4058         for (i = 0; i < num_rqst; i++) {
4059                 for (j = 0; j < rqst[i].rq_nvec; j++) {
4060                         /*
4061                          * The first rqst has a transform header where the
4062                          * first 20 bytes are not part of the encrypted blob
4063                          */
4064                         skip = (i == 0) && (j == 0) ? 20 : 0;
4065                         smb2_sg_set_buf(&sg[idx++],
4066                                         rqst[i].rq_iov[j].iov_base + skip,
4067                                         rqst[i].rq_iov[j].iov_len - skip);
4068                         }
4069
4070                 for (j = 0; j < rqst[i].rq_npages; j++) {
4071                         unsigned int len, offset;
4072
4073                         rqst_page_get_length(&rqst[i], j, &len, &offset);
4074                         sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4075                 }
4076         }
4077         smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4078         return sg;
4079 }
4080
4081 static int
4082 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4083 {
4084         struct cifs_ses *ses;
4085         u8 *ses_enc_key;
4086
4087         spin_lock(&cifs_tcp_ses_lock);
4088         list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4089                 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4090                         if (ses->Suid == ses_id) {
4091                                 ses_enc_key = enc ? ses->smb3encryptionkey :
4092                                         ses->smb3decryptionkey;
4093                                 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
4094                                 spin_unlock(&cifs_tcp_ses_lock);
4095                                 return 0;
4096                         }
4097                 }
4098         }
4099         spin_unlock(&cifs_tcp_ses_lock);
4100
4101         return 1;
4102 }
4103 /*
4104  * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4105  * iov[0]   - transform header (associate data),
4106  * iov[1-N] - SMB2 header and pages - data to encrypt.
4107  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4108  * untouched.
4109  */
4110 static int
4111 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4112               struct smb_rqst *rqst, int enc)
4113 {
4114         struct smb2_transform_hdr *tr_hdr =
4115                 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4116         unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4117         int rc = 0;
4118         struct scatterlist *sg;
4119         u8 sign[SMB2_SIGNATURE_SIZE] = {};
4120         u8 key[SMB3_SIGN_KEY_SIZE];
4121         struct aead_request *req;
4122         char *iv;
4123         unsigned int iv_len;
4124         DECLARE_CRYPTO_WAIT(wait);
4125         struct crypto_aead *tfm;
4126         unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4127
4128         rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4129         if (rc) {
4130                 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4131                          enc ? "en" : "de");
4132                 return rc;
4133         }
4134
4135         rc = smb3_crypto_aead_allocate(server);
4136         if (rc) {
4137                 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4138                 return rc;
4139         }
4140
4141         tfm = enc ? server->secmech.ccmaesencrypt :
4142                                                 server->secmech.ccmaesdecrypt;
4143
4144         if (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
4145                 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4146         else
4147                 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
4148
4149         if (rc) {
4150                 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4151                 return rc;
4152         }
4153
4154         rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4155         if (rc) {
4156                 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4157                 return rc;
4158         }
4159
4160         req = aead_request_alloc(tfm, GFP_KERNEL);
4161         if (!req) {
4162                 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4163                 return -ENOMEM;
4164         }
4165
4166         if (!enc) {
4167                 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4168                 crypt_len += SMB2_SIGNATURE_SIZE;
4169         }
4170
4171         sg = init_sg(num_rqst, rqst, sign);
4172         if (!sg) {
4173                 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4174                 rc = -ENOMEM;
4175                 goto free_req;
4176         }
4177
4178         iv_len = crypto_aead_ivsize(tfm);
4179         iv = kzalloc(iv_len, GFP_KERNEL);
4180         if (!iv) {
4181                 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4182                 rc = -ENOMEM;
4183                 goto free_sg;
4184         }
4185
4186         if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4187             (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4188                 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4189         else {
4190                 iv[0] = 3;
4191                 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4192         }
4193
4194         aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4195         aead_request_set_ad(req, assoc_data_len);
4196
4197         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4198                                   crypto_req_done, &wait);
4199
4200         rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4201                                 : crypto_aead_decrypt(req), &wait);
4202
4203         if (!rc && enc)
4204                 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4205
4206         kfree(iv);
4207 free_sg:
4208         kfree(sg);
4209 free_req:
4210         kfree(req);
4211         return rc;
4212 }
4213
4214 void
4215 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4216 {
4217         int i, j;
4218
4219         for (i = 0; i < num_rqst; i++) {
4220                 if (rqst[i].rq_pages) {
4221                         for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4222                                 put_page(rqst[i].rq_pages[j]);
4223                         kfree(rqst[i].rq_pages);
4224                 }
4225         }
4226 }
4227
4228 /*
4229  * This function will initialize new_rq and encrypt the content.
4230  * The first entry, new_rq[0], only contains a single iov which contains
4231  * a smb2_transform_hdr and is pre-allocated by the caller.
4232  * This function then populates new_rq[1+] with the content from olq_rq[0+].
4233  *
4234  * The end result is an array of smb_rqst structures where the first structure
4235  * only contains a single iov for the transform header which we then can pass
4236  * to crypt_message().
4237  *
4238  * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
4239  * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4240  */
4241 static int
4242 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4243                        struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4244 {
4245         struct page **pages;
4246         struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4247         unsigned int npages;
4248         unsigned int orig_len = 0;
4249         int i, j;
4250         int rc = -ENOMEM;
4251
4252         for (i = 1; i < num_rqst; i++) {
4253                 npages = old_rq[i - 1].rq_npages;
4254                 pages = kmalloc_array(npages, sizeof(struct page *),
4255                                       GFP_KERNEL);
4256                 if (!pages)
4257                         goto err_free;
4258
4259                 new_rq[i].rq_pages = pages;
4260                 new_rq[i].rq_npages = npages;
4261                 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4262                 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4263                 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4264                 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4265                 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4266
4267                 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4268
4269                 for (j = 0; j < npages; j++) {
4270                         pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4271                         if (!pages[j])
4272                                 goto err_free;
4273                 }
4274
4275                 /* copy pages form the old */
4276                 for (j = 0; j < npages; j++) {
4277                         char *dst, *src;
4278                         unsigned int offset, len;
4279
4280                         rqst_page_get_length(&new_rq[i], j, &len, &offset);
4281
4282                         dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4283                         src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4284
4285                         memcpy(dst, src, len);
4286                         kunmap(new_rq[i].rq_pages[j]);
4287                         kunmap(old_rq[i - 1].rq_pages[j]);
4288                 }
4289         }
4290
4291         /* fill the 1st iov with a transform header */
4292         fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4293
4294         rc = crypt_message(server, num_rqst, new_rq, 1);
4295         cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4296         if (rc)
4297                 goto err_free;
4298
4299         return rc;
4300
4301 err_free:
4302         smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4303         return rc;
4304 }
4305
4306 static int
4307 smb3_is_transform_hdr(void *buf)
4308 {
4309         struct smb2_transform_hdr *trhdr = buf;
4310
4311         return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4312 }
4313
4314 static int
4315 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4316                  unsigned int buf_data_size, struct page **pages,
4317                  unsigned int npages, unsigned int page_data_size,
4318                  bool is_offloaded)
4319 {
4320         struct kvec iov[2];
4321         struct smb_rqst rqst = {NULL};
4322         int rc;
4323
4324         iov[0].iov_base = buf;
4325         iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4326         iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4327         iov[1].iov_len = buf_data_size;
4328
4329         rqst.rq_iov = iov;
4330         rqst.rq_nvec = 2;
4331         rqst.rq_pages = pages;
4332         rqst.rq_npages = npages;
4333         rqst.rq_pagesz = PAGE_SIZE;
4334         rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4335
4336         rc = crypt_message(server, 1, &rqst, 0);
4337         cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4338
4339         if (rc)
4340                 return rc;
4341
4342         memmove(buf, iov[1].iov_base, buf_data_size);
4343
4344         if (!is_offloaded)
4345                 server->total_read = buf_data_size + page_data_size;
4346
4347         return rc;
4348 }
4349
4350 static int
4351 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4352                      unsigned int npages, unsigned int len)
4353 {
4354         int i;
4355         int length;
4356
4357         for (i = 0; i < npages; i++) {
4358                 struct page *page = pages[i];
4359                 size_t n;
4360
4361                 n = len;
4362                 if (len >= PAGE_SIZE) {
4363                         /* enough data to fill the page */
4364                         n = PAGE_SIZE;
4365                         len -= n;
4366                 } else {
4367                         zero_user(page, len, PAGE_SIZE - len);
4368                         len = 0;
4369                 }
4370                 length = cifs_read_page_from_socket(server, page, 0, n);
4371                 if (length < 0)
4372                         return length;
4373                 server->total_read += length;
4374         }
4375
4376         return 0;
4377 }
4378
4379 static int
4380 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4381                unsigned int cur_off, struct bio_vec **page_vec)
4382 {
4383         struct bio_vec *bvec;
4384         int i;
4385
4386         bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4387         if (!bvec)
4388                 return -ENOMEM;
4389
4390         for (i = 0; i < npages; i++) {
4391                 bvec[i].bv_page = pages[i];
4392                 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4393                 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4394                 data_size -= bvec[i].bv_len;
4395         }
4396
4397         if (data_size != 0) {
4398                 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4399                 kfree(bvec);
4400                 return -EIO;
4401         }
4402
4403         *page_vec = bvec;
4404         return 0;
4405 }
4406
4407 static int
4408 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4409                  char *buf, unsigned int buf_len, struct page **pages,
4410                  unsigned int npages, unsigned int page_data_size,
4411                  bool is_offloaded)
4412 {
4413         unsigned int data_offset;
4414         unsigned int data_len;
4415         unsigned int cur_off;
4416         unsigned int cur_page_idx;
4417         unsigned int pad_len;
4418         struct cifs_readdata *rdata = mid->callback_data;
4419         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4420         struct bio_vec *bvec = NULL;
4421         struct iov_iter iter;
4422         struct kvec iov;
4423         int length;
4424         bool use_rdma_mr = false;
4425
4426         if (shdr->Command != SMB2_READ) {
4427                 cifs_server_dbg(VFS, "only big read responses are supported\n");
4428                 return -ENOTSUPP;
4429         }
4430
4431         if (server->ops->is_session_expired &&
4432             server->ops->is_session_expired(buf)) {
4433                 if (!is_offloaded)
4434                         cifs_reconnect(server);
4435                 return -1;
4436         }
4437
4438         if (server->ops->is_status_pending &&
4439                         server->ops->is_status_pending(buf, server))
4440                 return -1;
4441
4442         /* set up first two iov to get credits */
4443         rdata->iov[0].iov_base = buf;
4444         rdata->iov[0].iov_len = 0;
4445         rdata->iov[1].iov_base = buf;
4446         rdata->iov[1].iov_len =
4447                 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4448         cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4449                  rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4450         cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4451                  rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4452
4453         rdata->result = server->ops->map_error(buf, true);
4454         if (rdata->result != 0) {
4455                 cifs_dbg(FYI, "%s: server returned error %d\n",
4456                          __func__, rdata->result);
4457                 /* normal error on read response */
4458                 if (is_offloaded)
4459                         mid->mid_state = MID_RESPONSE_RECEIVED;
4460                 else
4461                         dequeue_mid(mid, false);
4462                 return 0;
4463         }
4464
4465         data_offset = server->ops->read_data_offset(buf);
4466 #ifdef CONFIG_CIFS_SMB_DIRECT
4467         use_rdma_mr = rdata->mr;
4468 #endif
4469         data_len = server->ops->read_data_length(buf, use_rdma_mr);
4470
4471         if (data_offset < server->vals->read_rsp_size) {
4472                 /*
4473                  * win2k8 sometimes sends an offset of 0 when the read
4474                  * is beyond the EOF. Treat it as if the data starts just after
4475                  * the header.
4476                  */
4477                 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4478                          __func__, data_offset);
4479                 data_offset = server->vals->read_rsp_size;
4480         } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4481                 /* data_offset is beyond the end of smallbuf */
4482                 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4483                          __func__, data_offset);
4484                 rdata->result = -EIO;
4485                 if (is_offloaded)
4486                         mid->mid_state = MID_RESPONSE_MALFORMED;
4487                 else
4488                         dequeue_mid(mid, rdata->result);
4489                 return 0;
4490         }
4491
4492         pad_len = data_offset - server->vals->read_rsp_size;
4493
4494         if (buf_len <= data_offset) {
4495                 /* read response payload is in pages */
4496                 cur_page_idx = pad_len / PAGE_SIZE;
4497                 cur_off = pad_len % PAGE_SIZE;
4498
4499                 if (cur_page_idx != 0) {
4500                         /* data offset is beyond the 1st page of response */
4501                         cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4502                                  __func__, data_offset);
4503                         rdata->result = -EIO;
4504                         if (is_offloaded)
4505                                 mid->mid_state = MID_RESPONSE_MALFORMED;
4506                         else
4507                                 dequeue_mid(mid, rdata->result);
4508                         return 0;
4509                 }
4510
4511                 if (data_len > page_data_size - pad_len) {
4512                         /* data_len is corrupt -- discard frame */
4513                         rdata->result = -EIO;
4514                         if (is_offloaded)
4515                                 mid->mid_state = MID_RESPONSE_MALFORMED;
4516                         else
4517                                 dequeue_mid(mid, rdata->result);
4518                         return 0;
4519                 }
4520
4521                 rdata->result = init_read_bvec(pages, npages, page_data_size,
4522                                                cur_off, &bvec);
4523                 if (rdata->result != 0) {
4524                         if (is_offloaded)
4525                                 mid->mid_state = MID_RESPONSE_MALFORMED;
4526                         else
4527                                 dequeue_mid(mid, rdata->result);
4528                         return 0;
4529                 }
4530
4531                 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4532         } else if (buf_len >= data_offset + data_len) {
4533                 /* read response payload is in buf */
4534                 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4535                 iov.iov_base = buf + data_offset;
4536                 iov.iov_len = data_len;
4537                 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4538         } else {
4539                 /* read response payload cannot be in both buf and pages */
4540                 WARN_ONCE(1, "buf can not contain only a part of read data");
4541                 rdata->result = -EIO;
4542                 if (is_offloaded)
4543                         mid->mid_state = MID_RESPONSE_MALFORMED;
4544                 else
4545                         dequeue_mid(mid, rdata->result);
4546                 return 0;
4547         }
4548
4549         length = rdata->copy_into_pages(server, rdata, &iter);
4550
4551         kfree(bvec);
4552
4553         if (length < 0)
4554                 return length;
4555
4556         if (is_offloaded)
4557                 mid->mid_state = MID_RESPONSE_RECEIVED;
4558         else
4559                 dequeue_mid(mid, false);
4560         return length;
4561 }
4562
4563 struct smb2_decrypt_work {
4564         struct work_struct decrypt;
4565         struct TCP_Server_Info *server;
4566         struct page **ppages;
4567         char *buf;
4568         unsigned int npages;
4569         unsigned int len;
4570 };
4571
4572
4573 static void smb2_decrypt_offload(struct work_struct *work)
4574 {
4575         struct smb2_decrypt_work *dw = container_of(work,
4576                                 struct smb2_decrypt_work, decrypt);
4577         int i, rc;
4578         struct mid_q_entry *mid;
4579
4580         rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4581                               dw->ppages, dw->npages, dw->len, true);
4582         if (rc) {
4583                 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4584                 goto free_pages;
4585         }
4586
4587         dw->server->lstrp = jiffies;
4588         mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4589         if (mid == NULL)
4590                 cifs_dbg(FYI, "mid not found\n");
4591         else {
4592                 mid->decrypted = true;
4593                 rc = handle_read_data(dw->server, mid, dw->buf,
4594                                       dw->server->vals->read_rsp_size,
4595                                       dw->ppages, dw->npages, dw->len,
4596                                       true);
4597                 if (rc >= 0) {
4598 #ifdef CONFIG_CIFS_STATS2
4599                         mid->when_received = jiffies;
4600 #endif
4601                         mid->callback(mid);
4602                 } else {
4603                         spin_lock(&GlobalMid_Lock);
4604                         if (dw->server->tcpStatus == CifsNeedReconnect) {
4605                                 mid->mid_state = MID_RETRY_NEEDED;
4606                                 spin_unlock(&GlobalMid_Lock);
4607                                 mid->callback(mid);
4608                         } else {
4609                                 mid->mid_state = MID_REQUEST_SUBMITTED;
4610                                 mid->mid_flags &= ~(MID_DELETED);
4611                                 list_add_tail(&mid->qhead,
4612                                         &dw->server->pending_mid_q);
4613                                 spin_unlock(&GlobalMid_Lock);
4614                         }
4615                 }
4616                 cifs_mid_q_entry_release(mid);
4617         }
4618
4619 free_pages:
4620         for (i = dw->npages-1; i >= 0; i--)
4621                 put_page(dw->ppages[i]);
4622
4623         kfree(dw->ppages);
4624         cifs_small_buf_release(dw->buf);
4625         kfree(dw);
4626 }
4627
4628
4629 static int
4630 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4631                        int *num_mids)
4632 {
4633         char *buf = server->smallbuf;
4634         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4635         unsigned int npages;
4636         struct page **pages;
4637         unsigned int len;
4638         unsigned int buflen = server->pdu_size;
4639         int rc;
4640         int i = 0;
4641         struct smb2_decrypt_work *dw;
4642
4643         *num_mids = 1;
4644         len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4645                 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4646
4647         rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4648         if (rc < 0)
4649                 return rc;
4650         server->total_read += rc;
4651
4652         len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4653                 server->vals->read_rsp_size;
4654         npages = DIV_ROUND_UP(len, PAGE_SIZE);
4655
4656         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4657         if (!pages) {
4658                 rc = -ENOMEM;
4659                 goto discard_data;
4660         }
4661
4662         for (; i < npages; i++) {
4663                 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4664                 if (!pages[i]) {
4665                         rc = -ENOMEM;
4666                         goto discard_data;
4667                 }
4668         }
4669
4670         /* read read data into pages */
4671         rc = read_data_into_pages(server, pages, npages, len);
4672         if (rc)
4673                 goto free_pages;
4674
4675         rc = cifs_discard_remaining_data(server);
4676         if (rc)
4677                 goto free_pages;
4678
4679         /*
4680          * For large reads, offload to different thread for better performance,
4681          * use more cores decrypting which can be expensive
4682          */
4683
4684         if ((server->min_offload) && (server->in_flight > 1) &&
4685             (server->pdu_size >= server->min_offload)) {
4686                 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4687                 if (dw == NULL)
4688                         goto non_offloaded_decrypt;
4689
4690                 dw->buf = server->smallbuf;
4691                 server->smallbuf = (char *)cifs_small_buf_get();
4692
4693                 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4694
4695                 dw->npages = npages;
4696                 dw->server = server;
4697                 dw->ppages = pages;
4698                 dw->len = len;
4699                 queue_work(decrypt_wq, &dw->decrypt);
4700                 *num_mids = 0; /* worker thread takes care of finding mid */
4701                 return -1;
4702         }
4703
4704 non_offloaded_decrypt:
4705         rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4706                               pages, npages, len, false);
4707         if (rc)
4708                 goto free_pages;
4709
4710         *mid = smb2_find_mid(server, buf);
4711         if (*mid == NULL)
4712                 cifs_dbg(FYI, "mid not found\n");
4713         else {
4714                 cifs_dbg(FYI, "mid found\n");
4715                 (*mid)->decrypted = true;
4716                 rc = handle_read_data(server, *mid, buf,
4717                                       server->vals->read_rsp_size,
4718                                       pages, npages, len, false);
4719         }
4720
4721 free_pages:
4722         for (i = i - 1; i >= 0; i--)
4723                 put_page(pages[i]);
4724         kfree(pages);
4725         return rc;
4726 discard_data:
4727         cifs_discard_remaining_data(server);
4728         goto free_pages;
4729 }
4730
4731 static int
4732 receive_encrypted_standard(struct TCP_Server_Info *server,
4733                            struct mid_q_entry **mids, char **bufs,
4734                            int *num_mids)
4735 {
4736         int ret, length;
4737         char *buf = server->smallbuf;
4738         struct smb2_sync_hdr *shdr;
4739         unsigned int pdu_length = server->pdu_size;
4740         unsigned int buf_size;
4741         struct mid_q_entry *mid_entry;
4742         int next_is_large;
4743         char *next_buffer = NULL;
4744
4745         *num_mids = 0;
4746
4747         /* switch to large buffer if too big for a small one */
4748         if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4749                 server->large_buf = true;
4750                 memcpy(server->bigbuf, buf, server->total_read);
4751                 buf = server->bigbuf;
4752         }
4753
4754         /* now read the rest */
4755         length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4756                                 pdu_length - HEADER_SIZE(server) + 1);
4757         if (length < 0)
4758                 return length;
4759         server->total_read += length;
4760
4761         buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4762         length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
4763         if (length)
4764                 return length;
4765
4766         next_is_large = server->large_buf;
4767 one_more:
4768         shdr = (struct smb2_sync_hdr *)buf;
4769         if (shdr->NextCommand) {
4770                 if (next_is_large)
4771                         next_buffer = (char *)cifs_buf_get();
4772                 else
4773                         next_buffer = (char *)cifs_small_buf_get();
4774                 memcpy(next_buffer,
4775                        buf + le32_to_cpu(shdr->NextCommand),
4776                        pdu_length - le32_to_cpu(shdr->NextCommand));
4777         }
4778
4779         mid_entry = smb2_find_mid(server, buf);
4780         if (mid_entry == NULL)
4781                 cifs_dbg(FYI, "mid not found\n");
4782         else {
4783                 cifs_dbg(FYI, "mid found\n");
4784                 mid_entry->decrypted = true;
4785                 mid_entry->resp_buf_size = server->pdu_size;
4786         }
4787
4788         if (*num_mids >= MAX_COMPOUND) {
4789                 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4790                 return -1;
4791         }
4792         bufs[*num_mids] = buf;
4793         mids[(*num_mids)++] = mid_entry;
4794
4795         if (mid_entry && mid_entry->handle)
4796                 ret = mid_entry->handle(server, mid_entry);
4797         else
4798                 ret = cifs_handle_standard(server, mid_entry);
4799
4800         if (ret == 0 && shdr->NextCommand) {
4801                 pdu_length -= le32_to_cpu(shdr->NextCommand);
4802                 server->large_buf = next_is_large;
4803                 if (next_is_large)
4804                         server->bigbuf = buf = next_buffer;
4805                 else
4806                         server->smallbuf = buf = next_buffer;
4807                 goto one_more;
4808         } else if (ret != 0) {
4809                 /*
4810                  * ret != 0 here means that we didn't get to handle_mid() thus
4811                  * server->smallbuf and server->bigbuf are still valid. We need
4812                  * to free next_buffer because it is not going to be used
4813                  * anywhere.
4814                  */
4815                 if (next_is_large)
4816                         free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4817                 else
4818                         free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4819         }
4820
4821         return ret;
4822 }
4823
4824 static int
4825 smb3_receive_transform(struct TCP_Server_Info *server,
4826                        struct mid_q_entry **mids, char **bufs, int *num_mids)
4827 {
4828         char *buf = server->smallbuf;
4829         unsigned int pdu_length = server->pdu_size;
4830         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4831         unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4832
4833         if (pdu_length < sizeof(struct smb2_transform_hdr) +
4834                                                 sizeof(struct smb2_sync_hdr)) {
4835                 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4836                          pdu_length);
4837                 cifs_reconnect(server);
4838                 return -ECONNABORTED;
4839         }
4840
4841         if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4842                 cifs_server_dbg(VFS, "Transform message is broken\n");
4843                 cifs_reconnect(server);
4844                 return -ECONNABORTED;
4845         }
4846
4847         /* TODO: add support for compounds containing READ. */
4848         if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4849                 return receive_encrypted_read(server, &mids[0], num_mids);
4850         }
4851
4852         return receive_encrypted_standard(server, mids, bufs, num_mids);
4853 }
4854
4855 int
4856 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4857 {
4858         char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4859
4860         return handle_read_data(server, mid, buf, server->pdu_size,
4861                                 NULL, 0, 0, false);
4862 }
4863
4864 static int
4865 smb2_next_header(char *buf)
4866 {
4867         struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4868         struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4869
4870         if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4871                 return sizeof(struct smb2_transform_hdr) +
4872                   le32_to_cpu(t_hdr->OriginalMessageSize);
4873
4874         return le32_to_cpu(hdr->NextCommand);
4875 }
4876
4877 static int
4878 smb2_make_node(unsigned int xid, struct inode *inode,
4879                struct dentry *dentry, struct cifs_tcon *tcon,
4880                char *full_path, umode_t mode, dev_t dev)
4881 {
4882         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4883         int rc = -EPERM;
4884         FILE_ALL_INFO *buf = NULL;
4885         struct cifs_io_parms io_parms = {0};
4886         __u32 oplock = 0;
4887         struct cifs_fid fid;
4888         struct cifs_open_parms oparms;
4889         unsigned int bytes_written;
4890         struct win_dev *pdev;
4891         struct kvec iov[2];
4892
4893         /*
4894          * Check if mounted with mount parm 'sfu' mount parm.
4895          * SFU emulation should work with all servers, but only
4896          * supports block and char device (no socket & fifo),
4897          * and was used by default in earlier versions of Windows
4898          */
4899         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4900                 goto out;
4901
4902         /*
4903          * TODO: Add ability to create instead via reparse point. Windows (e.g.
4904          * their current NFS server) uses this approach to expose special files
4905          * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4906          */
4907
4908         if (!S_ISCHR(mode) && !S_ISBLK(mode))
4909                 goto out;
4910
4911         cifs_dbg(FYI, "sfu compat create special file\n");
4912
4913         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4914         if (buf == NULL) {
4915                 rc = -ENOMEM;
4916                 goto out;
4917         }
4918
4919         oparms.tcon = tcon;
4920         oparms.cifs_sb = cifs_sb;
4921         oparms.desired_access = GENERIC_WRITE;
4922         oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4923                                                     CREATE_OPTION_SPECIAL);
4924         oparms.disposition = FILE_CREATE;
4925         oparms.path = full_path;
4926         oparms.fid = &fid;
4927         oparms.reconnect = false;
4928
4929         if (tcon->ses->server->oplocks)
4930                 oplock = REQ_OPLOCK;
4931         else
4932                 oplock = 0;
4933         rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4934         if (rc)
4935                 goto out;
4936
4937         /*
4938          * BB Do not bother to decode buf since no local inode yet to put
4939          * timestamps in, but we can reuse it safely.
4940          */
4941
4942         pdev = (struct win_dev *)buf;
4943         io_parms.pid = current->tgid;
4944         io_parms.tcon = tcon;
4945         io_parms.offset = 0;
4946         io_parms.length = sizeof(struct win_dev);
4947         iov[1].iov_base = buf;
4948         iov[1].iov_len = sizeof(struct win_dev);
4949         if (S_ISCHR(mode)) {
4950                 memcpy(pdev->type, "IntxCHR", 8);
4951                 pdev->major = cpu_to_le64(MAJOR(dev));
4952                 pdev->minor = cpu_to_le64(MINOR(dev));
4953                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4954                                                         &bytes_written, iov, 1);
4955         } else if (S_ISBLK(mode)) {
4956                 memcpy(pdev->type, "IntxBLK", 8);
4957                 pdev->major = cpu_to_le64(MAJOR(dev));
4958                 pdev->minor = cpu_to_le64(MINOR(dev));
4959                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4960                                                         &bytes_written, iov, 1);
4961         }
4962         tcon->ses->server->ops->close(xid, tcon, &fid);
4963         d_drop(dentry);
4964
4965         /* FIXME: add code here to set EAs */
4966 out:
4967         kfree(buf);
4968         return rc;
4969 }
4970
4971
4972 struct smb_version_operations smb20_operations = {
4973         .compare_fids = smb2_compare_fids,
4974         .setup_request = smb2_setup_request,
4975         .setup_async_request = smb2_setup_async_request,
4976         .check_receive = smb2_check_receive,
4977         .add_credits = smb2_add_credits,
4978         .set_credits = smb2_set_credits,
4979         .get_credits_field = smb2_get_credits_field,
4980         .get_credits = smb2_get_credits,
4981         .wait_mtu_credits = cifs_wait_mtu_credits,
4982         .get_next_mid = smb2_get_next_mid,
4983         .revert_current_mid = smb2_revert_current_mid,
4984         .read_data_offset = smb2_read_data_offset,
4985         .read_data_length = smb2_read_data_length,
4986         .map_error = map_smb2_to_linux_error,
4987         .find_mid = smb2_find_mid,
4988         .check_message = smb2_check_message,
4989         .dump_detail = smb2_dump_detail,
4990         .clear_stats = smb2_clear_stats,
4991         .print_stats = smb2_print_stats,
4992         .is_oplock_break = smb2_is_valid_oplock_break,
4993         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4994         .downgrade_oplock = smb2_downgrade_oplock,
4995         .need_neg = smb2_need_neg,
4996         .negotiate = smb2_negotiate,
4997         .negotiate_wsize = smb2_negotiate_wsize,
4998         .negotiate_rsize = smb2_negotiate_rsize,
4999         .sess_setup = SMB2_sess_setup,
5000         .logoff = SMB2_logoff,
5001         .tree_connect = SMB2_tcon,
5002         .tree_disconnect = SMB2_tdis,
5003         .qfs_tcon = smb2_qfs_tcon,
5004         .is_path_accessible = smb2_is_path_accessible,
5005         .can_echo = smb2_can_echo,
5006         .echo = SMB2_echo,
5007         .query_path_info = smb2_query_path_info,
5008         .get_srv_inum = smb2_get_srv_inum,
5009         .query_file_info = smb2_query_file_info,
5010         .set_path_size = smb2_set_path_size,
5011         .set_file_size = smb2_set_file_size,
5012         .set_file_info = smb2_set_file_info,
5013         .set_compression = smb2_set_compression,
5014         .mkdir = smb2_mkdir,
5015         .mkdir_setinfo = smb2_mkdir_setinfo,
5016         .rmdir = smb2_rmdir,
5017         .unlink = smb2_unlink,
5018         .rename = smb2_rename_path,
5019         .create_hardlink = smb2_create_hardlink,
5020         .query_symlink = smb2_query_symlink,
5021         .query_mf_symlink = smb3_query_mf_symlink,
5022         .create_mf_symlink = smb3_create_mf_symlink,
5023         .open = smb2_open_file,
5024         .set_fid = smb2_set_fid,
5025         .close = smb2_close_file,
5026         .flush = smb2_flush_file,
5027         .async_readv = smb2_async_readv,
5028         .async_writev = smb2_async_writev,
5029         .sync_read = smb2_sync_read,
5030         .sync_write = smb2_sync_write,
5031         .query_dir_first = smb2_query_dir_first,
5032         .query_dir_next = smb2_query_dir_next,
5033         .close_dir = smb2_close_dir,
5034         .calc_smb_size = smb2_calc_size,
5035         .is_status_pending = smb2_is_status_pending,
5036         .is_session_expired = smb2_is_session_expired,
5037         .oplock_response = smb2_oplock_response,
5038         .queryfs = smb2_queryfs,
5039         .mand_lock = smb2_mand_lock,
5040         .mand_unlock_range = smb2_unlock_range,
5041         .push_mand_locks = smb2_push_mandatory_locks,
5042         .get_lease_key = smb2_get_lease_key,
5043         .set_lease_key = smb2_set_lease_key,
5044         .new_lease_key = smb2_new_lease_key,
5045         .calc_signature = smb2_calc_signature,
5046         .is_read_op = smb2_is_read_op,
5047         .set_oplock_level = smb2_set_oplock_level,
5048         .create_lease_buf = smb2_create_lease_buf,
5049         .parse_lease_buf = smb2_parse_lease_buf,
5050         .copychunk_range = smb2_copychunk_range,
5051         .wp_retry_size = smb2_wp_retry_size,
5052         .dir_needs_close = smb2_dir_needs_close,
5053         .get_dfs_refer = smb2_get_dfs_refer,
5054         .select_sectype = smb2_select_sectype,
5055 #ifdef CONFIG_CIFS_XATTR
5056         .query_all_EAs = smb2_query_eas,
5057         .set_EA = smb2_set_ea,
5058 #endif /* CIFS_XATTR */
5059         .get_acl = get_smb2_acl,
5060         .get_acl_by_fid = get_smb2_acl_by_fid,
5061         .set_acl = set_smb2_acl,
5062         .next_header = smb2_next_header,
5063         .ioctl_query_info = smb2_ioctl_query_info,
5064         .make_node = smb2_make_node,
5065         .fiemap = smb3_fiemap,
5066         .llseek = smb3_llseek,
5067         .is_status_io_timeout = smb2_is_status_io_timeout,
5068 };
5069
5070 struct smb_version_operations smb21_operations = {
5071         .compare_fids = smb2_compare_fids,
5072         .setup_request = smb2_setup_request,
5073         .setup_async_request = smb2_setup_async_request,
5074         .check_receive = smb2_check_receive,
5075         .add_credits = smb2_add_credits,
5076         .set_credits = smb2_set_credits,
5077         .get_credits_field = smb2_get_credits_field,
5078         .get_credits = smb2_get_credits,
5079         .wait_mtu_credits = smb2_wait_mtu_credits,
5080         .adjust_credits = smb2_adjust_credits,
5081         .get_next_mid = smb2_get_next_mid,
5082         .revert_current_mid = smb2_revert_current_mid,
5083         .read_data_offset = smb2_read_data_offset,
5084         .read_data_length = smb2_read_data_length,
5085         .map_error = map_smb2_to_linux_error,
5086         .find_mid = smb2_find_mid,
5087         .check_message = smb2_check_message,
5088         .dump_detail = smb2_dump_detail,
5089         .clear_stats = smb2_clear_stats,
5090         .print_stats = smb2_print_stats,
5091         .is_oplock_break = smb2_is_valid_oplock_break,
5092         .handle_cancelled_mid = smb2_handle_cancelled_mid,
5093         .downgrade_oplock = smb2_downgrade_oplock,
5094         .need_neg = smb2_need_neg,
5095         .negotiate = smb2_negotiate,
5096         .negotiate_wsize = smb2_negotiate_wsize,
5097         .negotiate_rsize = smb2_negotiate_rsize,
5098         .sess_setup = SMB2_sess_setup,
5099         .logoff = SMB2_logoff,
5100         .tree_connect = SMB2_tcon,
5101         .tree_disconnect = SMB2_tdis,
5102         .qfs_tcon = smb2_qfs_tcon,
5103         .is_path_accessible = smb2_is_path_accessible,
5104         .can_echo = smb2_can_echo,
5105         .echo = SMB2_echo,
5106         .query_path_info = smb2_query_path_info,
5107         .get_srv_inum = smb2_get_srv_inum,
5108         .query_file_info = smb2_query_file_info,
5109         .set_path_size = smb2_set_path_size,
5110         .set_file_size = smb2_set_file_size,
5111         .set_file_info = smb2_set_file_info,
5112         .set_compression = smb2_set_compression,
5113         .mkdir = smb2_mkdir,
5114         .mkdir_setinfo = smb2_mkdir_setinfo,
5115         .rmdir = smb2_rmdir,
5116         .unlink = smb2_unlink,
5117         .rename = smb2_rename_path,
5118         .create_hardlink = smb2_create_hardlink,
5119         .query_symlink = smb2_query_symlink,
5120         .query_mf_symlink = smb3_query_mf_symlink,
5121         .create_mf_symlink = smb3_create_mf_symlink,
5122         .open = smb2_open_file,
5123         .set_fid = smb2_set_fid,
5124         .close = smb2_close_file,
5125         .flush = smb2_flush_file,
5126         .async_readv = smb2_async_readv,
5127         .async_writev = smb2_async_writev,
5128         .sync_read = smb2_sync_read,
5129         .sync_write = smb2_sync_write,
5130         .query_dir_first = smb2_query_dir_first,
5131         .query_dir_next = smb2_query_dir_next,
5132         .close_dir = smb2_close_dir,
5133         .calc_smb_size = smb2_calc_size,
5134         .is_status_pending = smb2_is_status_pending,
5135         .is_session_expired = smb2_is_session_expired,
5136         .oplock_response = smb2_oplock_response,
5137         .queryfs = smb2_queryfs,
5138         .mand_lock = smb2_mand_lock,
5139         .mand_unlock_range = smb2_unlock_range,
5140         .push_mand_locks = smb2_push_mandatory_locks,
5141         .get_lease_key = smb2_get_lease_key,
5142         .set_lease_key = smb2_set_lease_key,
5143         .new_lease_key = smb2_new_lease_key,
5144         .calc_signature = smb2_calc_signature,
5145         .is_read_op = smb21_is_read_op,
5146         .set_oplock_level = smb21_set_oplock_level,
5147         .create_lease_buf = smb2_create_lease_buf,
5148         .parse_lease_buf = smb2_parse_lease_buf,
5149         .copychunk_range = smb2_copychunk_range,
5150         .wp_retry_size = smb2_wp_retry_size,
5151         .dir_needs_close = smb2_dir_needs_close,
5152         .enum_snapshots = smb3_enum_snapshots,
5153         .notify = smb3_notify,
5154         .get_dfs_refer = smb2_get_dfs_refer,
5155         .select_sectype = smb2_select_sectype,
5156 #ifdef CONFIG_CIFS_XATTR
5157         .query_all_EAs = smb2_query_eas,
5158         .set_EA = smb2_set_ea,
5159 #endif /* CIFS_XATTR */
5160         .get_acl = get_smb2_acl,
5161         .get_acl_by_fid = get_smb2_acl_by_fid,
5162         .set_acl = set_smb2_acl,
5163         .next_header = smb2_next_header,
5164         .ioctl_query_info = smb2_ioctl_query_info,
5165         .make_node = smb2_make_node,
5166         .fiemap = smb3_fiemap,
5167         .llseek = smb3_llseek,
5168         .is_status_io_timeout = smb2_is_status_io_timeout,
5169 };
5170
5171 struct smb_version_operations smb30_operations = {
5172         .compare_fids = smb2_compare_fids,
5173         .setup_request = smb2_setup_request,
5174         .setup_async_request = smb2_setup_async_request,
5175         .check_receive = smb2_check_receive,
5176         .add_credits = smb2_add_credits,
5177         .set_credits = smb2_set_credits,
5178         .get_credits_field = smb2_get_credits_field,
5179         .get_credits = smb2_get_credits,
5180         .wait_mtu_credits = smb2_wait_mtu_credits,
5181         .adjust_credits = smb2_adjust_credits,
5182         .get_next_mid = smb2_get_next_mid,
5183         .revert_current_mid = smb2_revert_current_mid,
5184         .read_data_offset = smb2_read_data_offset,
5185         .read_data_length = smb2_read_data_length,
5186         .map_error = map_smb2_to_linux_error,
5187         .find_mid = smb2_find_mid,
5188         .check_message = smb2_check_message,
5189         .dump_detail = smb2_dump_detail,
5190         .clear_stats = smb2_clear_stats,
5191         .print_stats = smb2_print_stats,
5192         .dump_share_caps = smb2_dump_share_caps,
5193         .is_oplock_break = smb2_is_valid_oplock_break,
5194         .handle_cancelled_mid = smb2_handle_cancelled_mid,
5195         .downgrade_oplock = smb3_downgrade_oplock,
5196         .need_neg = smb2_need_neg,
5197         .negotiate = smb2_negotiate,
5198         .negotiate_wsize = smb3_negotiate_wsize,
5199         .negotiate_rsize = smb3_negotiate_rsize,
5200         .sess_setup = SMB2_sess_setup,
5201         .logoff = SMB2_logoff,
5202         .tree_connect = SMB2_tcon,
5203         .tree_disconnect = SMB2_tdis,
5204         .qfs_tcon = smb3_qfs_tcon,
5205         .is_path_accessible = smb2_is_path_accessible,
5206         .can_echo = smb2_can_echo,
5207         .echo = SMB2_echo,
5208         .query_path_info = smb2_query_path_info,
5209         /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5210         .query_reparse_tag = smb2_query_reparse_tag,
5211         .get_srv_inum = smb2_get_srv_inum,
5212         .query_file_info = smb2_query_file_info,
5213         .set_path_size = smb2_set_path_size,
5214         .set_file_size = smb2_set_file_size,
5215         .set_file_info = smb2_set_file_info,
5216         .set_compression = smb2_set_compression,
5217         .mkdir = smb2_mkdir,
5218         .mkdir_setinfo = smb2_mkdir_setinfo,
5219         .rmdir = smb2_rmdir,
5220         .unlink = smb2_unlink,
5221         .rename = smb2_rename_path,
5222         .create_hardlink = smb2_create_hardlink,
5223         .query_symlink = smb2_query_symlink,
5224         .query_mf_symlink = smb3_query_mf_symlink,
5225         .create_mf_symlink = smb3_create_mf_symlink,
5226         .open = smb2_open_file,
5227         .set_fid = smb2_set_fid,
5228         .close = smb2_close_file,
5229         .close_getattr = smb2_close_getattr,
5230         .flush = smb2_flush_file,
5231         .async_readv = smb2_async_readv,
5232         .async_writev = smb2_async_writev,
5233         .sync_read = smb2_sync_read,
5234         .sync_write = smb2_sync_write,
5235         .query_dir_first = smb2_query_dir_first,
5236         .query_dir_next = smb2_query_dir_next,
5237         .close_dir = smb2_close_dir,
5238         .calc_smb_size = smb2_calc_size,
5239         .is_status_pending = smb2_is_status_pending,
5240         .is_session_expired = smb2_is_session_expired,
5241         .oplock_response = smb2_oplock_response,
5242         .queryfs = smb2_queryfs,
5243         .mand_lock = smb2_mand_lock,
5244         .mand_unlock_range = smb2_unlock_range,
5245         .push_mand_locks = smb2_push_mandatory_locks,
5246         .get_lease_key = smb2_get_lease_key,
5247         .set_lease_key = smb2_set_lease_key,
5248         .new_lease_key = smb2_new_lease_key,
5249         .generate_signingkey = generate_smb30signingkey,
5250         .calc_signature = smb3_calc_signature,
5251         .set_integrity  = smb3_set_integrity,
5252         .is_read_op = smb21_is_read_op,
5253         .set_oplock_level = smb3_set_oplock_level,
5254         .create_lease_buf = smb3_create_lease_buf,
5255         .parse_lease_buf = smb3_parse_lease_buf,
5256         .copychunk_range = smb2_copychunk_range,
5257         .duplicate_extents = smb2_duplicate_extents,
5258         .validate_negotiate = smb3_validate_negotiate,
5259         .wp_retry_size = smb2_wp_retry_size,
5260         .dir_needs_close = smb2_dir_needs_close,
5261         .fallocate = smb3_fallocate,
5262         .enum_snapshots = smb3_enum_snapshots,
5263         .notify = smb3_notify,
5264         .init_transform_rq = smb3_init_transform_rq,
5265         .is_transform_hdr = smb3_is_transform_hdr,
5266         .receive_transform = smb3_receive_transform,
5267         .get_dfs_refer = smb2_get_dfs_refer,
5268         .select_sectype = smb2_select_sectype,
5269 #ifdef CONFIG_CIFS_XATTR
5270         .query_all_EAs = smb2_query_eas,
5271         .set_EA = smb2_set_ea,
5272 #endif /* CIFS_XATTR */
5273         .get_acl = get_smb2_acl,
5274         .get_acl_by_fid = get_smb2_acl_by_fid,
5275         .set_acl = set_smb2_acl,
5276         .next_header = smb2_next_header,
5277         .ioctl_query_info = smb2_ioctl_query_info,
5278         .make_node = smb2_make_node,
5279         .fiemap = smb3_fiemap,
5280         .llseek = smb3_llseek,
5281         .is_status_io_timeout = smb2_is_status_io_timeout,
5282 };
5283
5284 struct smb_version_operations smb311_operations = {
5285         .compare_fids = smb2_compare_fids,
5286         .setup_request = smb2_setup_request,
5287         .setup_async_request = smb2_setup_async_request,
5288         .check_receive = smb2_check_receive,
5289         .add_credits = smb2_add_credits,
5290         .set_credits = smb2_set_credits,
5291         .get_credits_field = smb2_get_credits_field,
5292         .get_credits = smb2_get_credits,
5293         .wait_mtu_credits = smb2_wait_mtu_credits,
5294         .adjust_credits = smb2_adjust_credits,
5295         .get_next_mid = smb2_get_next_mid,
5296         .revert_current_mid = smb2_revert_current_mid,
5297         .read_data_offset = smb2_read_data_offset,
5298         .read_data_length = smb2_read_data_length,
5299         .map_error = map_smb2_to_linux_error,
5300         .find_mid = smb2_find_mid,
5301         .check_message = smb2_check_message,
5302         .dump_detail = smb2_dump_detail,
5303         .clear_stats = smb2_clear_stats,
5304         .print_stats = smb2_print_stats,
5305         .dump_share_caps = smb2_dump_share_caps,
5306         .is_oplock_break = smb2_is_valid_oplock_break,
5307         .handle_cancelled_mid = smb2_handle_cancelled_mid,
5308         .downgrade_oplock = smb3_downgrade_oplock,
5309         .need_neg = smb2_need_neg,
5310         .negotiate = smb2_negotiate,
5311         .negotiate_wsize = smb3_negotiate_wsize,
5312         .negotiate_rsize = smb3_negotiate_rsize,
5313         .sess_setup = SMB2_sess_setup,
5314         .logoff = SMB2_logoff,
5315         .tree_connect = SMB2_tcon,
5316         .tree_disconnect = SMB2_tdis,
5317         .qfs_tcon = smb3_qfs_tcon,
5318         .is_path_accessible = smb2_is_path_accessible,
5319         .can_echo = smb2_can_echo,
5320         .echo = SMB2_echo,
5321         .query_path_info = smb2_query_path_info,
5322         .query_reparse_tag = smb2_query_reparse_tag,
5323         .get_srv_inum = smb2_get_srv_inum,
5324         .query_file_info = smb2_query_file_info,
5325         .set_path_size = smb2_set_path_size,
5326         .set_file_size = smb2_set_file_size,
5327         .set_file_info = smb2_set_file_info,
5328         .set_compression = smb2_set_compression,
5329         .mkdir = smb2_mkdir,
5330         .mkdir_setinfo = smb2_mkdir_setinfo,
5331         .posix_mkdir = smb311_posix_mkdir,
5332         .rmdir = smb2_rmdir,
5333         .unlink = smb2_unlink,
5334         .rename = smb2_rename_path,
5335         .create_hardlink = smb2_create_hardlink,
5336         .query_symlink = smb2_query_symlink,
5337         .query_mf_symlink = smb3_query_mf_symlink,
5338         .create_mf_symlink = smb3_create_mf_symlink,
5339         .open = smb2_open_file,
5340         .set_fid = smb2_set_fid,
5341         .close = smb2_close_file,
5342         .close_getattr = smb2_close_getattr,
5343         .flush = smb2_flush_file,
5344         .async_readv = smb2_async_readv,
5345         .async_writev = smb2_async_writev,
5346         .sync_read = smb2_sync_read,
5347         .sync_write = smb2_sync_write,
5348         .query_dir_first = smb2_query_dir_first,
5349         .query_dir_next = smb2_query_dir_next,
5350         .close_dir = smb2_close_dir,
5351         .calc_smb_size = smb2_calc_size,
5352         .is_status_pending = smb2_is_status_pending,
5353         .is_session_expired = smb2_is_session_expired,
5354         .oplock_response = smb2_oplock_response,
5355         .queryfs = smb311_queryfs,
5356         .mand_lock = smb2_mand_lock,
5357         .mand_unlock_range = smb2_unlock_range,
5358         .push_mand_locks = smb2_push_mandatory_locks,
5359         .get_lease_key = smb2_get_lease_key,
5360         .set_lease_key = smb2_set_lease_key,
5361         .new_lease_key = smb2_new_lease_key,
5362         .generate_signingkey = generate_smb311signingkey,
5363         .calc_signature = smb3_calc_signature,
5364         .set_integrity  = smb3_set_integrity,
5365         .is_read_op = smb21_is_read_op,
5366         .set_oplock_level = smb3_set_oplock_level,
5367         .create_lease_buf = smb3_create_lease_buf,
5368         .parse_lease_buf = smb3_parse_lease_buf,
5369         .copychunk_range = smb2_copychunk_range,
5370         .duplicate_extents = smb2_duplicate_extents,
5371 /*      .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5372         .wp_retry_size = smb2_wp_retry_size,
5373         .dir_needs_close = smb2_dir_needs_close,
5374         .fallocate = smb3_fallocate,
5375         .enum_snapshots = smb3_enum_snapshots,
5376         .notify = smb3_notify,
5377         .init_transform_rq = smb3_init_transform_rq,
5378         .is_transform_hdr = smb3_is_transform_hdr,
5379         .receive_transform = smb3_receive_transform,
5380         .get_dfs_refer = smb2_get_dfs_refer,
5381         .select_sectype = smb2_select_sectype,
5382 #ifdef CONFIG_CIFS_XATTR
5383         .query_all_EAs = smb2_query_eas,
5384         .set_EA = smb2_set_ea,
5385 #endif /* CIFS_XATTR */
5386         .get_acl = get_smb2_acl,
5387         .get_acl_by_fid = get_smb2_acl_by_fid,
5388         .set_acl = set_smb2_acl,
5389         .next_header = smb2_next_header,
5390         .ioctl_query_info = smb2_ioctl_query_info,
5391         .make_node = smb2_make_node,
5392         .fiemap = smb3_fiemap,
5393         .llseek = smb3_llseek,
5394         .is_status_io_timeout = smb2_is_status_io_timeout,
5395 };
5396
5397 struct smb_version_values smb20_values = {
5398         .version_string = SMB20_VERSION_STRING,
5399         .protocol_id = SMB20_PROT_ID,
5400         .req_capabilities = 0, /* MBZ */
5401         .large_lock_type = 0,
5402         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5403         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5404         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5405         .header_size = sizeof(struct smb2_sync_hdr),
5406         .header_preamble_size = 0,
5407         .max_header_size = MAX_SMB2_HDR_SIZE,
5408         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5409         .lock_cmd = SMB2_LOCK,
5410         .cap_unix = 0,
5411         .cap_nt_find = SMB2_NT_FIND,
5412         .cap_large_files = SMB2_LARGE_FILES,
5413         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5414         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5415         .create_lease_size = sizeof(struct create_lease),
5416 };
5417
5418 struct smb_version_values smb21_values = {
5419         .version_string = SMB21_VERSION_STRING,
5420         .protocol_id = SMB21_PROT_ID,
5421         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5422         .large_lock_type = 0,
5423         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5424         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5425         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5426         .header_size = sizeof(struct smb2_sync_hdr),
5427         .header_preamble_size = 0,
5428         .max_header_size = MAX_SMB2_HDR_SIZE,
5429         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5430         .lock_cmd = SMB2_LOCK,
5431         .cap_unix = 0,
5432         .cap_nt_find = SMB2_NT_FIND,
5433         .cap_large_files = SMB2_LARGE_FILES,
5434         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5435         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5436         .create_lease_size = sizeof(struct create_lease),
5437 };
5438
5439 struct smb_version_values smb3any_values = {
5440         .version_string = SMB3ANY_VERSION_STRING,
5441         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5442         .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,
5443         .large_lock_type = 0,
5444         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5445         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5446         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5447         .header_size = sizeof(struct smb2_sync_hdr),
5448         .header_preamble_size = 0,
5449         .max_header_size = MAX_SMB2_HDR_SIZE,
5450         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5451         .lock_cmd = SMB2_LOCK,
5452         .cap_unix = 0,
5453         .cap_nt_find = SMB2_NT_FIND,
5454         .cap_large_files = SMB2_LARGE_FILES,
5455         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5456         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5457         .create_lease_size = sizeof(struct create_lease_v2),
5458 };
5459
5460 struct smb_version_values smbdefault_values = {
5461         .version_string = SMBDEFAULT_VERSION_STRING,
5462         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5463         .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,
5464         .large_lock_type = 0,
5465         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5466         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5467         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5468         .header_size = sizeof(struct smb2_sync_hdr),
5469         .header_preamble_size = 0,
5470         .max_header_size = MAX_SMB2_HDR_SIZE,
5471         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5472         .lock_cmd = SMB2_LOCK,
5473         .cap_unix = 0,
5474         .cap_nt_find = SMB2_NT_FIND,
5475         .cap_large_files = SMB2_LARGE_FILES,
5476         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5477         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5478         .create_lease_size = sizeof(struct create_lease_v2),
5479 };
5480
5481 struct smb_version_values smb30_values = {
5482         .version_string = SMB30_VERSION_STRING,
5483         .protocol_id = SMB30_PROT_ID,
5484         .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,
5485         .large_lock_type = 0,
5486         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5487         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5488         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5489         .header_size = sizeof(struct smb2_sync_hdr),
5490         .header_preamble_size = 0,
5491         .max_header_size = MAX_SMB2_HDR_SIZE,
5492         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5493         .lock_cmd = SMB2_LOCK,
5494         .cap_unix = 0,
5495         .cap_nt_find = SMB2_NT_FIND,
5496         .cap_large_files = SMB2_LARGE_FILES,
5497         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5498         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5499         .create_lease_size = sizeof(struct create_lease_v2),
5500 };
5501
5502 struct smb_version_values smb302_values = {
5503         .version_string = SMB302_VERSION_STRING,
5504         .protocol_id = SMB302_PROT_ID,
5505         .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,
5506         .large_lock_type = 0,
5507         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5508         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5509         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5510         .header_size = sizeof(struct smb2_sync_hdr),
5511         .header_preamble_size = 0,
5512         .max_header_size = MAX_SMB2_HDR_SIZE,
5513         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5514         .lock_cmd = SMB2_LOCK,
5515         .cap_unix = 0,
5516         .cap_nt_find = SMB2_NT_FIND,
5517         .cap_large_files = SMB2_LARGE_FILES,
5518         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5519         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5520         .create_lease_size = sizeof(struct create_lease_v2),
5521 };
5522
5523 struct smb_version_values smb311_values = {
5524         .version_string = SMB311_VERSION_STRING,
5525         .protocol_id = SMB311_PROT_ID,
5526         .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,
5527         .large_lock_type = 0,
5528         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5529         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5530         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5531         .header_size = sizeof(struct smb2_sync_hdr),
5532         .header_preamble_size = 0,
5533         .max_header_size = MAX_SMB2_HDR_SIZE,
5534         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5535         .lock_cmd = SMB2_LOCK,
5536         .cap_unix = 0,
5537         .cap_nt_find = SMB2_NT_FIND,
5538         .cap_large_files = SMB2_LARGE_FILES,
5539         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5540         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5541         .create_lease_size = sizeof(struct create_lease_v2),
5542 };