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