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