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