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