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