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