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