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