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