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