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