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