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