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