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