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