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