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