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