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