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