blob: 64ac683498e03b8c059173fe3713c9db8a6d7b69 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * SMB2 version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 */
7
8#include <linux/pagemap.h>
9#include <linux/vfs.h>
10#include <linux/falloc.h>
11#include <linux/scatterlist.h>
12#include <linux/uuid.h>
13#include <crypto/aead.h>
14#include "cifsglob.h"
15#include "smb2pdu.h"
16#include "smb2proto.h"
17#include "cifsproto.h"
18#include "cifs_debug.h"
19#include "cifs_unicode.h"
20#include "smb2status.h"
21#include "smb2glob.h"
22#include "cifs_ioctl.h"
23#include "smbdirect.h"
24
25/* Change credits for different ops and return the total number of credits */
26static int
27change_conf(struct TCP_Server_Info *server)
28{
29 server->credits += server->echo_credits + server->oplock_credits;
30 server->oplock_credits = server->echo_credits = 0;
31 switch (server->credits) {
32 case 0:
33 return 0;
34 case 1:
35 server->echoes = false;
36 server->oplocks = false;
37 break;
38 case 2:
39 server->echoes = true;
40 server->oplocks = false;
41 server->echo_credits = 1;
42 break;
43 default:
44 server->echoes = true;
45 if (enable_oplocks) {
46 server->oplocks = true;
47 server->oplock_credits = 1;
48 } else
49 server->oplocks = false;
50
51 server->echo_credits = 1;
52 }
53 server->credits -= server->echo_credits + server->oplock_credits;
54 return server->credits + server->echo_credits + server->oplock_credits;
55}
56
57static void
58smb2_add_credits(struct TCP_Server_Info *server,
59 const struct cifs_credits *credits, const int optype)
60{
61 int *val, rc = -1;
62 unsigned int add = credits->value;
63 unsigned int instance = credits->instance;
64 bool reconnect_detected = false;
65
66 spin_lock(&server->req_lock);
67 val = server->ops->get_credits_field(server, optype);
68
69 /* eg found case where write overlapping reconnect messed up credits */
70 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
71 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
72 server->hostname, *val);
73 if ((instance == 0) || (instance == server->reconnect_instance))
74 *val += add;
75 else
76 reconnect_detected = true;
77
78 if (*val > 65000) {
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
81 }
82 WARN_ON_ONCE(server->in_flight == 0);
83 server->in_flight--;
84 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
85 rc = change_conf(server);
86 /*
87 * Sometimes server returns 0 credits on oplock break ack - we need to
88 * rebalance credits in this case.
89 */
90 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
91 server->oplocks) {
92 if (server->credits > 1) {
93 server->credits--;
94 server->oplock_credits++;
95 }
96 }
97 spin_unlock(&server->req_lock);
98 wake_up(&server->request_q);
99
100 if (reconnect_detected)
101 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
102 add, instance);
103
104 if (server->tcpStatus == CifsNeedReconnect
105 || server->tcpStatus == CifsExiting)
106 return;
107
108 switch (rc) {
109 case -1:
110 /* change_conf hasn't been executed */
111 break;
112 case 0:
113 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
114 break;
115 case 1:
116 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
117 break;
118 case 2:
119 cifs_dbg(FYI, "disabling oplocks\n");
120 break;
121 default:
122 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
123 }
124}
125
126static void
127smb2_set_credits(struct TCP_Server_Info *server, const int val)
128{
129 spin_lock(&server->req_lock);
130 server->credits = val;
131 if (val == 1)
132 server->reconnect_instance++;
133 spin_unlock(&server->req_lock);
134 /* don't log while holding the lock */
135 if (val == 1)
136 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
137}
138
139static int *
140smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
141{
142 switch (optype) {
143 case CIFS_ECHO_OP:
144 return &server->echo_credits;
145 case CIFS_OBREAK_OP:
146 return &server->oplock_credits;
147 default:
148 return &server->credits;
149 }
150}
151
152static unsigned int
153smb2_get_credits(struct mid_q_entry *mid)
154{
155 return mid->credits_received;
156}
157
158static int
159smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
160 unsigned int *num, struct cifs_credits *credits)
161{
162 int rc = 0;
163 unsigned int scredits;
164
165 spin_lock(&server->req_lock);
166 while (1) {
167 if (server->credits <= 0) {
168 spin_unlock(&server->req_lock);
169 cifs_num_waiters_inc(server);
170 rc = wait_event_killable(server->request_q,
171 has_credits(server, &server->credits, 1));
172 cifs_num_waiters_dec(server);
173 if (rc)
174 return rc;
175 spin_lock(&server->req_lock);
176 } else {
177 if (server->tcpStatus == CifsExiting) {
178 spin_unlock(&server->req_lock);
179 return -ENOENT;
180 }
181
182 scredits = server->credits;
183 /* can deadlock with reopen */
184 if (scredits <= 8) {
185 *num = SMB2_MAX_BUFFER_SIZE;
186 credits->value = 0;
187 credits->instance = 0;
188 break;
189 }
190
191 /* leave some credits for reopen and other ops */
192 scredits -= 8;
193 *num = min_t(unsigned int, size,
194 scredits * SMB2_MAX_BUFFER_SIZE);
195
196 credits->value =
197 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
198 credits->instance = server->reconnect_instance;
199 server->credits -= credits->value;
200 server->in_flight++;
201 if (server->in_flight > server->max_in_flight)
202 server->max_in_flight = server->in_flight;
203 break;
204 }
205 }
206 spin_unlock(&server->req_lock);
207 return rc;
208}
209
210static int
211smb2_adjust_credits(struct TCP_Server_Info *server,
212 struct cifs_credits *credits,
213 const unsigned int payload_size)
214{
215 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
216
217 if (!credits->value || credits->value == new_val)
218 return 0;
219
220 if (credits->value < new_val) {
221 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
222 credits->value, new_val);
223 return -ENOTSUPP;
224 }
225
226 spin_lock(&server->req_lock);
227
228 if (server->reconnect_instance != credits->instance) {
229 spin_unlock(&server->req_lock);
230 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
231 credits->value - new_val);
232 return -EAGAIN;
233 }
234
235 server->credits += credits->value - new_val;
236 spin_unlock(&server->req_lock);
237 wake_up(&server->request_q);
238 credits->value = new_val;
239 return 0;
240}
241
242static __u64
243smb2_get_next_mid(struct TCP_Server_Info *server)
244{
245 __u64 mid;
246 /* for SMB2 we need the current value */
247 spin_lock(&GlobalMid_Lock);
248 mid = server->CurrentMid++;
249 spin_unlock(&GlobalMid_Lock);
250 return mid;
251}
252
253static void
254smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
255{
256 spin_lock(&GlobalMid_Lock);
257 if (server->CurrentMid >= val)
258 server->CurrentMid -= val;
259 spin_unlock(&GlobalMid_Lock);
260}
261
262static struct mid_q_entry *
263__smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
264{
265 struct mid_q_entry *mid;
266 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
267 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
268
269 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
270 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
271 return NULL;
272 }
273
274 spin_lock(&GlobalMid_Lock);
275 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
276 if ((mid->mid == wire_mid) &&
277 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
278 (mid->command == shdr->Command)) {
279 kref_get(&mid->refcount);
280 if (dequeue) {
281 list_del_init(&mid->qhead);
282 mid->mid_flags |= MID_DELETED;
283 }
284 spin_unlock(&GlobalMid_Lock);
285 return mid;
286 }
287 }
288 spin_unlock(&GlobalMid_Lock);
289 return NULL;
290}
291
292static struct mid_q_entry *
293smb2_find_mid(struct TCP_Server_Info *server, char *buf)
294{
295 return __smb2_find_mid(server, buf, false);
296}
297
298static struct mid_q_entry *
299smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
300{
301 return __smb2_find_mid(server, buf, true);
302}
303
304static void
305smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
306{
307#ifdef CONFIG_CIFS_DEBUG2
308 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
309
310 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
311 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
312 shdr->ProcessId);
313 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
314 server->ops->calc_smb_size(buf, server));
315#endif
316}
317
318static bool
319smb2_need_neg(struct TCP_Server_Info *server)
320{
321 return server->max_read == 0;
322}
323
324static int
325smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
326{
327 int rc;
328
329 ses->server->CurrentMid = 0;
330 rc = SMB2_negotiate(xid, ses);
331 /* BB we probably don't need to retry with modern servers */
332 if (rc == -EAGAIN)
333 rc = -EHOSTDOWN;
334 return rc;
335}
336
337static unsigned int
338smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
339{
340 struct TCP_Server_Info *server = tcon->ses->server;
341 unsigned int wsize;
342
343 /* start with specified wsize, or default */
344 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
345 wsize = min_t(unsigned int, wsize, server->max_write);
346#ifdef CONFIG_CIFS_SMB_DIRECT
347 if (server->rdma) {
348 if (server->sign)
349 wsize = min_t(unsigned int,
350 wsize, server->smbd_conn->max_fragmented_send_size);
351 else
352 wsize = min_t(unsigned int,
353 wsize, server->smbd_conn->max_readwrite_size);
354 }
355#endif
356 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
357 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
358
359 return wsize;
360}
361
362static unsigned int
363smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
364{
365 struct TCP_Server_Info *server = tcon->ses->server;
366 unsigned int wsize;
367
368 /* start with specified wsize, or default */
369 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
370 wsize = min_t(unsigned int, wsize, server->max_write);
371#ifdef CONFIG_CIFS_SMB_DIRECT
372 if (server->rdma) {
373 if (server->sign)
374 wsize = min_t(unsigned int,
375 wsize, server->smbd_conn->max_fragmented_send_size);
376 else
377 wsize = min_t(unsigned int,
378 wsize, server->smbd_conn->max_readwrite_size);
379 }
380#endif
381 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
382 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
383
384 return wsize;
385}
386
387static unsigned int
388smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
389{
390 struct TCP_Server_Info *server = tcon->ses->server;
391 unsigned int rsize;
392
393 /* start with specified rsize, or default */
394 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
395 rsize = min_t(unsigned int, rsize, server->max_read);
396#ifdef CONFIG_CIFS_SMB_DIRECT
397 if (server->rdma) {
398 if (server->sign)
399 rsize = min_t(unsigned int,
400 rsize, server->smbd_conn->max_fragmented_recv_size);
401 else
402 rsize = min_t(unsigned int,
403 rsize, server->smbd_conn->max_readwrite_size);
404 }
405#endif
406
407 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
408 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
409
410 return rsize;
411}
412
413static unsigned int
414smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
415{
416 struct TCP_Server_Info *server = tcon->ses->server;
417 unsigned int rsize;
418
419 /* start with specified rsize, or default */
420 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
421 rsize = min_t(unsigned int, rsize, server->max_read);
422#ifdef CONFIG_CIFS_SMB_DIRECT
423 if (server->rdma) {
424 if (server->sign)
425 rsize = min_t(unsigned int,
426 rsize, server->smbd_conn->max_fragmented_recv_size);
427 else
428 rsize = min_t(unsigned int,
429 rsize, server->smbd_conn->max_readwrite_size);
430 }
431#endif
432
433 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
434 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
435
436 return rsize;
437}
438
439static int
440parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
441 size_t buf_len,
442 struct cifs_server_iface **iface_list,
443 size_t *iface_count)
444{
445 struct network_interface_info_ioctl_rsp *p;
446 struct sockaddr_in *addr4;
447 struct sockaddr_in6 *addr6;
448 struct iface_info_ipv4 *p4;
449 struct iface_info_ipv6 *p6;
450 struct cifs_server_iface *info;
451 ssize_t bytes_left;
452 size_t next = 0;
453 int nb_iface = 0;
454 int rc = 0;
455
456 *iface_list = NULL;
457 *iface_count = 0;
458
459 /*
460 * Fist pass: count and sanity check
461 */
462
463 bytes_left = buf_len;
464 p = buf;
465 while (bytes_left >= sizeof(*p)) {
466 nb_iface++;
467 next = le32_to_cpu(p->Next);
468 if (!next) {
469 bytes_left -= sizeof(*p);
470 break;
471 }
472 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
473 bytes_left -= next;
474 }
475
476 if (!nb_iface) {
477 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
478 rc = -EINVAL;
479 goto out;
480 }
481
482 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
483 if ((bytes_left > 8) || p->Next)
484 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
485
486
487 /*
488 * Second pass: extract info to internal structure
489 */
490
491 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
492 if (!*iface_list) {
493 rc = -ENOMEM;
494 goto out;
495 }
496
497 info = *iface_list;
498 bytes_left = buf_len;
499 p = buf;
500 while (bytes_left >= sizeof(*p)) {
501 info->speed = le64_to_cpu(p->LinkSpeed);
502 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
503 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
504
505 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
506 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
507 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
508 le32_to_cpu(p->Capability));
509
510 switch (p->Family) {
511 /*
512 * The kernel and wire socket structures have the same
513 * layout and use network byte order but make the
514 * conversion explicit in case either one changes.
515 */
516 case INTERNETWORK:
517 addr4 = (struct sockaddr_in *)&info->sockaddr;
518 p4 = (struct iface_info_ipv4 *)p->Buffer;
519 addr4->sin_family = AF_INET;
520 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
521
522 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
523 addr4->sin_port = cpu_to_be16(CIFS_PORT);
524
525 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
526 &addr4->sin_addr);
527 break;
528 case INTERNETWORKV6:
529 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
530 p6 = (struct iface_info_ipv6 *)p->Buffer;
531 addr6->sin6_family = AF_INET6;
532 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
533
534 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
535 addr6->sin6_flowinfo = 0;
536 addr6->sin6_scope_id = 0;
537 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
538
539 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
540 &addr6->sin6_addr);
541 break;
542 default:
543 cifs_dbg(VFS,
544 "%s: skipping unsupported socket family\n",
545 __func__);
546 goto next_iface;
547 }
548
549 (*iface_count)++;
550 info++;
551next_iface:
552 next = le32_to_cpu(p->Next);
553 if (!next)
554 break;
555 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
556 bytes_left -= next;
557 }
558
559 if (!*iface_count) {
560 rc = -EINVAL;
561 goto out;
562 }
563
564out:
565 if (rc) {
566 kfree(*iface_list);
567 *iface_count = 0;
568 *iface_list = NULL;
569 }
570 return rc;
571}
572
573
574static int
575SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
576{
577 int rc;
578 unsigned int ret_data_len = 0;
579 struct network_interface_info_ioctl_rsp *out_buf = NULL;
580 struct cifs_server_iface *iface_list;
581 size_t iface_count;
582 struct cifs_ses *ses = tcon->ses;
583
584 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
585 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
586 NULL /* no data input */, 0 /* no data input */,
587 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
588 if (rc == -EOPNOTSUPP) {
589 cifs_dbg(FYI,
590 "server does not support query network interfaces\n");
591 ret_data_len = 0;
592 } else if (rc != 0) {
593 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
594 goto out;
595 }
596
597 rc = parse_server_interfaces(out_buf, ret_data_len,
598 &iface_list, &iface_count);
599 if (rc)
600 goto out;
601
602 spin_lock(&ses->iface_lock);
603 kfree(ses->iface_list);
604 ses->iface_list = iface_list;
605 ses->iface_count = iface_count;
606 ses->iface_last_update = jiffies;
607 spin_unlock(&ses->iface_lock);
608
609out:
610 kfree(out_buf);
611 return rc;
612}
613
614static void
615smb2_close_cached_fid(struct kref *ref)
616{
617 struct cached_fid *cfid = container_of(ref, struct cached_fid,
618 refcount);
619
620 if (cfid->is_valid) {
621 cifs_dbg(FYI, "clear cached root file handle\n");
622 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
623 cfid->fid->volatile_fid);
624 cfid->is_valid = false;
625 cfid->file_all_info_is_valid = false;
626 }
627}
628
629void close_shroot(struct cached_fid *cfid)
630{
631 mutex_lock(&cfid->fid_mutex);
632 kref_put(&cfid->refcount, smb2_close_cached_fid);
633 mutex_unlock(&cfid->fid_mutex);
634}
635
636void
637smb2_cached_lease_break(struct work_struct *work)
638{
639 struct cached_fid *cfid = container_of(work,
640 struct cached_fid, lease_break);
641
642 close_shroot(cfid);
643}
644
645/*
646 * Open the directory at the root of a share
647 */
648int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
649 struct cifs_sb_info *cifs_sb, struct cifs_fid *pfid)
650{
651 struct cifs_ses *ses = tcon->ses;
652 struct TCP_Server_Info *server = ses->server;
653 struct cifs_open_parms oparms;
654 struct smb2_create_rsp *o_rsp = NULL;
655 struct smb2_query_info_rsp *qi_rsp = NULL;
656 int resp_buftype[2];
657 struct smb_rqst rqst[2];
658 struct kvec rsp_iov[2];
659 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
660 struct kvec qi_iov[1];
661 int rc, flags = 0;
662 __le16 utf16_path = 0; /* Null - since an open of top of share */
663 u8 oplock = SMB2_OPLOCK_LEVEL_II;
664
665 mutex_lock(&tcon->crfid.fid_mutex);
666 if (tcon->crfid.is_valid) {
667 cifs_dbg(FYI, "found a cached root file handle\n");
668 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
669 kref_get(&tcon->crfid.refcount);
670 mutex_unlock(&tcon->crfid.fid_mutex);
671 return 0;
672 }
673
674 /*
675 * We do not hold the lock for the open because in case
676 * SMB2_open needs to reconnect, it will end up calling
677 * cifs_mark_open_files_invalid() which takes the lock again
678 * thus causing a deadlock
679 */
680
681 mutex_unlock(&tcon->crfid.fid_mutex);
682
683 if (smb3_encryption_required(tcon))
684 flags |= CIFS_TRANSFORM_REQ;
685
686 if (!server->ops->new_lease_key)
687 return -EIO;
688
689 server->ops->new_lease_key(pfid);
690
691 memset(rqst, 0, sizeof(rqst));
692 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
693 memset(rsp_iov, 0, sizeof(rsp_iov));
694
695 /* Open */
696 memset(&open_iov, 0, sizeof(open_iov));
697 rqst[0].rq_iov = open_iov;
698 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
699
700 oparms.tcon = tcon;
701 oparms.create_options = cifs_create_options(cifs_sb, 0);
702 oparms.desired_access = FILE_READ_ATTRIBUTES;
703 oparms.disposition = FILE_OPEN;
704 oparms.fid = pfid;
705 oparms.reconnect = false;
706
707 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
708 if (rc)
709 goto oshr_free;
710 smb2_set_next_command(tcon, &rqst[0]);
711
712 memset(&qi_iov, 0, sizeof(qi_iov));
713 rqst[1].rq_iov = qi_iov;
714 rqst[1].rq_nvec = 1;
715
716 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
717 COMPOUND_FID, FILE_ALL_INFORMATION,
718 SMB2_O_INFO_FILE, 0,
719 sizeof(struct smb2_file_all_info) +
720 PATH_MAX * 2, 0, NULL);
721 if (rc)
722 goto oshr_free;
723
724 smb2_set_related(&rqst[1]);
725
726 rc = compound_send_recv(xid, ses, flags, 2, rqst,
727 resp_buftype, rsp_iov);
728 mutex_lock(&tcon->crfid.fid_mutex);
729
730 /*
731 * Now we need to check again as the cached root might have
732 * been successfully re-opened from a concurrent process
733 */
734
735 if (tcon->crfid.is_valid) {
736 /* work was already done */
737
738 /* stash fids for close() later */
739 struct cifs_fid fid = {
740 .persistent_fid = pfid->persistent_fid,
741 .volatile_fid = pfid->volatile_fid,
742 };
743
744 /*
745 * caller expects this func to set pfid to a valid
746 * cached root, so we copy the existing one and get a
747 * reference.
748 */
749 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
750 kref_get(&tcon->crfid.refcount);
751
752 mutex_unlock(&tcon->crfid.fid_mutex);
753
754 if (rc == 0) {
755 /* close extra handle outside of crit sec */
756 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
757 }
758 rc = 0;
759 goto oshr_free;
760 }
761
762 /* Cached root is still invalid, continue normaly */
763
764 if (rc) {
765 if (rc == -EREMCHG) {
766 tcon->need_reconnect = true;
767 printk_once(KERN_WARNING "server share %s deleted\n",
768 tcon->treeName);
769 }
770 goto oshr_exit;
771 }
772
773 atomic_inc(&tcon->num_remote_opens);
774
775 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
776 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
777 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
778#ifdef CONFIG_CIFS_DEBUG2
779 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
780#endif /* CIFS_DEBUG2 */
781
782 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
783 tcon->crfid.tcon = tcon;
784 tcon->crfid.is_valid = true;
785 kref_init(&tcon->crfid.refcount);
786
787 /* BB TBD check to see if oplock level check can be removed below */
788 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
789 kref_get(&tcon->crfid.refcount);
790 rc = smb2_parse_contexts(server, rsp_iov,
791 &oparms.fid->epoch,
792 oparms.fid->lease_key, &oplock, NULL);
793 if (rc)
794 goto oshr_exit;
795 } else
796 goto oshr_exit;
797
798 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
799 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
800 goto oshr_exit;
801 if (!smb2_validate_and_copy_iov(
802 le16_to_cpu(qi_rsp->OutputBufferOffset),
803 sizeof(struct smb2_file_all_info),
804 &rsp_iov[1], sizeof(struct smb2_file_all_info),
805 (char *)&tcon->crfid.file_all_info))
806 tcon->crfid.file_all_info_is_valid = 1;
807
808oshr_exit:
809 mutex_unlock(&tcon->crfid.fid_mutex);
810oshr_free:
811 SMB2_open_free(&rqst[0]);
812 SMB2_query_info_free(&rqst[1]);
813 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
814 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
815 return rc;
816}
817
818static void
819smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
820 struct cifs_sb_info *cifs_sb)
821{
822 int rc;
823 __le16 srch_path = 0; /* Null - open root of share */
824 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
825 struct cifs_open_parms oparms;
826 struct cifs_fid fid;
827 bool no_cached_open = tcon->nohandlecache;
828
829 oparms = (struct cifs_open_parms) {
830 .tcon = tcon,
831 .desired_access = FILE_READ_ATTRIBUTES,
832 .disposition = FILE_OPEN,
833 .create_options = cifs_create_options(cifs_sb, 0),
834 .fid = &fid,
835 };
836
837 if (no_cached_open)
838 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
839 NULL);
840 else
841 rc = open_shroot(xid, tcon, cifs_sb, &fid);
842
843 if (rc)
844 return;
845
846 SMB3_request_interfaces(xid, tcon);
847
848 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
849 FS_ATTRIBUTE_INFORMATION);
850 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
851 FS_DEVICE_INFORMATION);
852 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
853 FS_VOLUME_INFORMATION);
854 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
855 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
856 if (no_cached_open)
857 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
858 else
859 close_shroot(&tcon->crfid);
860}
861
862static void
863smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
864 struct cifs_sb_info *cifs_sb)
865{
866 int rc;
867 __le16 srch_path = 0; /* Null - open root of share */
868 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
869 struct cifs_open_parms oparms;
870 struct cifs_fid fid;
871
872 oparms.tcon = tcon;
873 oparms.desired_access = FILE_READ_ATTRIBUTES;
874 oparms.disposition = FILE_OPEN;
875 oparms.create_options = cifs_create_options(cifs_sb, 0);
876 oparms.fid = &fid;
877 oparms.reconnect = false;
878
879 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
880 if (rc)
881 return;
882
883 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
884 FS_ATTRIBUTE_INFORMATION);
885 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
886 FS_DEVICE_INFORMATION);
887 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
888}
889
890static int
891smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
892 struct cifs_sb_info *cifs_sb, const char *full_path)
893{
894 int rc;
895 __le16 *utf16_path;
896 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
897 struct cifs_open_parms oparms;
898 struct cifs_fid fid;
899
900 if ((*full_path == 0) && tcon->crfid.is_valid)
901 return 0;
902
903 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
904 if (!utf16_path)
905 return -ENOMEM;
906
907 oparms.tcon = tcon;
908 oparms.desired_access = FILE_READ_ATTRIBUTES;
909 oparms.disposition = FILE_OPEN;
910 oparms.create_options = cifs_create_options(cifs_sb, 0);
911 oparms.fid = &fid;
912 oparms.reconnect = false;
913
914 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
915 if (rc) {
916 kfree(utf16_path);
917 return rc;
918 }
919
920 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
921 kfree(utf16_path);
922 return rc;
923}
924
925static int
926smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
927 struct cifs_sb_info *cifs_sb, const char *full_path,
928 u64 *uniqueid, FILE_ALL_INFO *data)
929{
930 *uniqueid = le64_to_cpu(data->IndexNumber);
931 return 0;
932}
933
934static int
935smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
936 struct cifs_fid *fid, FILE_ALL_INFO *data)
937{
938 int rc;
939 struct smb2_file_all_info *smb2_data;
940
941 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
942 GFP_KERNEL);
943 if (smb2_data == NULL)
944 return -ENOMEM;
945
946 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
947 smb2_data);
948 if (!rc)
949 move_smb2_info_to_cifs(data, smb2_data);
950 kfree(smb2_data);
951 return rc;
952}
953
954#ifdef CONFIG_CIFS_XATTR
955static ssize_t
956move_smb2_ea_to_cifs(char *dst, size_t dst_size,
957 struct smb2_file_full_ea_info *src, size_t src_size,
958 const unsigned char *ea_name)
959{
960 int rc = 0;
961 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
962 char *name, *value;
963 size_t buf_size = dst_size;
964 size_t name_len, value_len, user_name_len;
965
966 while (src_size > 0) {
967 name_len = (size_t)src->ea_name_length;
968 value_len = (size_t)le16_to_cpu(src->ea_value_length);
969
970 if (name_len == 0)
971 break;
972
973 if (src_size < 8 + name_len + 1 + value_len) {
974 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
975 rc = -EIO;
976 goto out;
977 }
978
979 name = &src->ea_data[0];
980 value = &src->ea_data[src->ea_name_length + 1];
981
982 if (ea_name) {
983 if (ea_name_len == name_len &&
984 memcmp(ea_name, name, name_len) == 0) {
985 rc = value_len;
986 if (dst_size == 0)
987 goto out;
988 if (dst_size < value_len) {
989 rc = -ERANGE;
990 goto out;
991 }
992 memcpy(dst, value, value_len);
993 goto out;
994 }
995 } else {
996 /* 'user.' plus a terminating null */
997 user_name_len = 5 + 1 + name_len;
998
999 if (buf_size == 0) {
1000 /* skip copy - calc size only */
1001 rc += user_name_len;
1002 } else if (dst_size >= user_name_len) {
1003 dst_size -= user_name_len;
1004 memcpy(dst, "user.", 5);
1005 dst += 5;
1006 memcpy(dst, src->ea_data, name_len);
1007 dst += name_len;
1008 *dst = 0;
1009 ++dst;
1010 rc += user_name_len;
1011 } else {
1012 /* stop before overrun buffer */
1013 rc = -ERANGE;
1014 break;
1015 }
1016 }
1017
1018 if (!src->next_entry_offset)
1019 break;
1020
1021 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1022 /* stop before overrun buffer */
1023 rc = -ERANGE;
1024 break;
1025 }
1026 src_size -= le32_to_cpu(src->next_entry_offset);
1027 src = (void *)((char *)src +
1028 le32_to_cpu(src->next_entry_offset));
1029 }
1030
1031 /* didn't find the named attribute */
1032 if (ea_name)
1033 rc = -ENODATA;
1034
1035out:
1036 return (ssize_t)rc;
1037}
1038
1039static ssize_t
1040smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1041 const unsigned char *path, const unsigned char *ea_name,
1042 char *ea_data, size_t buf_size,
1043 struct cifs_sb_info *cifs_sb)
1044{
1045 int rc;
1046 __le16 *utf16_path;
1047 struct kvec rsp_iov = {NULL, 0};
1048 int buftype = CIFS_NO_BUFFER;
1049 struct smb2_query_info_rsp *rsp;
1050 struct smb2_file_full_ea_info *info = NULL;
1051
1052 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1053 if (!utf16_path)
1054 return -ENOMEM;
1055
1056 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1057 FILE_READ_EA,
1058 FILE_FULL_EA_INFORMATION,
1059 SMB2_O_INFO_FILE,
1060 CIFSMaxBufSize -
1061 MAX_SMB2_CREATE_RESPONSE_SIZE -
1062 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1063 &rsp_iov, &buftype, cifs_sb);
1064 if (rc) {
1065 /*
1066 * If ea_name is NULL (listxattr) and there are no EAs,
1067 * return 0 as it's not an error. Otherwise, the specified
1068 * ea_name was not found.
1069 */
1070 if (!ea_name && rc == -ENODATA)
1071 rc = 0;
1072 goto qeas_exit;
1073 }
1074
1075 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1076 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1077 le32_to_cpu(rsp->OutputBufferLength),
1078 &rsp_iov,
1079 sizeof(struct smb2_file_full_ea_info));
1080 if (rc)
1081 goto qeas_exit;
1082
1083 info = (struct smb2_file_full_ea_info *)(
1084 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1085 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1086 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1087
1088 qeas_exit:
1089 kfree(utf16_path);
1090 free_rsp_buf(buftype, rsp_iov.iov_base);
1091 return rc;
1092}
1093
1094
1095static int
1096smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1097 const char *path, const char *ea_name, const void *ea_value,
1098 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1099 struct cifs_sb_info *cifs_sb)
1100{
1101 struct cifs_ses *ses = tcon->ses;
1102 __le16 *utf16_path = NULL;
1103 int ea_name_len = strlen(ea_name);
1104 int flags = 0;
1105 int len;
1106 struct smb_rqst rqst[3];
1107 int resp_buftype[3];
1108 struct kvec rsp_iov[3];
1109 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1110 struct cifs_open_parms oparms;
1111 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1112 struct cifs_fid fid;
1113 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1114 unsigned int size[1];
1115 void *data[1];
1116 struct smb2_file_full_ea_info *ea = NULL;
1117 struct kvec close_iov[1];
1118 struct smb2_query_info_rsp *rsp;
1119 int rc, used_len = 0;
1120
1121 if (smb3_encryption_required(tcon))
1122 flags |= CIFS_TRANSFORM_REQ;
1123
1124 if (ea_name_len > 255)
1125 return -EINVAL;
1126
1127 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1128 if (!utf16_path)
1129 return -ENOMEM;
1130
1131 memset(rqst, 0, sizeof(rqst));
1132 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1133 memset(rsp_iov, 0, sizeof(rsp_iov));
1134
1135 if (ses->server->ops->query_all_EAs) {
1136 if (!ea_value) {
1137 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1138 ea_name, NULL, 0,
1139 cifs_sb);
1140 if (rc == -ENODATA)
1141 goto sea_exit;
1142 } else {
1143 /* If we are adding a attribute we should first check
1144 * if there will be enough space available to store
1145 * the new EA. If not we should not add it since we
1146 * would not be able to even read the EAs back.
1147 */
1148 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1149 FILE_READ_EA,
1150 FILE_FULL_EA_INFORMATION,
1151 SMB2_O_INFO_FILE,
1152 CIFSMaxBufSize -
1153 MAX_SMB2_CREATE_RESPONSE_SIZE -
1154 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1155 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1156 if (rc == 0) {
1157 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1158 used_len = le32_to_cpu(rsp->OutputBufferLength);
1159 }
1160 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1161 resp_buftype[1] = CIFS_NO_BUFFER;
1162 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1163 rc = 0;
1164
1165 /* Use a fudge factor of 256 bytes in case we collide
1166 * with a different set_EAs command.
1167 */
1168 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1169 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1170 used_len + ea_name_len + ea_value_len + 1) {
1171 rc = -ENOSPC;
1172 goto sea_exit;
1173 }
1174 }
1175 }
1176
1177 /* Open */
1178 memset(&open_iov, 0, sizeof(open_iov));
1179 rqst[0].rq_iov = open_iov;
1180 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1181
1182 memset(&oparms, 0, sizeof(oparms));
1183 oparms.tcon = tcon;
1184 oparms.desired_access = FILE_WRITE_EA;
1185 oparms.disposition = FILE_OPEN;
1186 oparms.create_options = cifs_create_options(cifs_sb, 0);
1187 oparms.fid = &fid;
1188 oparms.reconnect = false;
1189
1190 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1191 if (rc)
1192 goto sea_exit;
1193 smb2_set_next_command(tcon, &rqst[0]);
1194
1195
1196 /* Set Info */
1197 memset(&si_iov, 0, sizeof(si_iov));
1198 rqst[1].rq_iov = si_iov;
1199 rqst[1].rq_nvec = 1;
1200
1201 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1202 ea = kzalloc(len, GFP_KERNEL);
1203 if (ea == NULL) {
1204 rc = -ENOMEM;
1205 goto sea_exit;
1206 }
1207
1208 ea->ea_name_length = ea_name_len;
1209 ea->ea_value_length = cpu_to_le16(ea_value_len);
1210 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1211 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1212
1213 size[0] = len;
1214 data[0] = ea;
1215
1216 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1217 COMPOUND_FID, current->tgid,
1218 FILE_FULL_EA_INFORMATION,
1219 SMB2_O_INFO_FILE, 0, data, size);
1220 if (rc)
1221 goto sea_exit;
1222 smb2_set_next_command(tcon, &rqst[1]);
1223 smb2_set_related(&rqst[1]);
1224
1225
1226 /* Close */
1227 memset(&close_iov, 0, sizeof(close_iov));
1228 rqst[2].rq_iov = close_iov;
1229 rqst[2].rq_nvec = 1;
1230 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1231 smb2_set_related(&rqst[2]);
1232
1233 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1234 resp_buftype, rsp_iov);
1235 /* no need to bump num_remote_opens because handle immediately closed */
1236
1237 sea_exit:
1238 kfree(ea);
1239 kfree(utf16_path);
1240 SMB2_open_free(&rqst[0]);
1241 SMB2_set_info_free(&rqst[1]);
1242 SMB2_close_free(&rqst[2]);
1243 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1244 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1245 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1246 return rc;
1247}
1248#endif
1249
1250static bool
1251smb2_can_echo(struct TCP_Server_Info *server)
1252{
1253 return server->echoes;
1254}
1255
1256static void
1257smb2_clear_stats(struct cifs_tcon *tcon)
1258{
1259 int i;
1260
1261 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1262 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1263 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1264 }
1265}
1266
1267static void
1268smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1269{
1270 seq_puts(m, "\n\tShare Capabilities:");
1271 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1272 seq_puts(m, " DFS,");
1273 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1274 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1275 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1276 seq_puts(m, " SCALEOUT,");
1277 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1278 seq_puts(m, " CLUSTER,");
1279 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1280 seq_puts(m, " ASYMMETRIC,");
1281 if (tcon->capabilities == 0)
1282 seq_puts(m, " None");
1283 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1284 seq_puts(m, " Aligned,");
1285 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1286 seq_puts(m, " Partition Aligned,");
1287 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1288 seq_puts(m, " SSD,");
1289 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1290 seq_puts(m, " TRIM-support,");
1291
1292 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1293 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1294 if (tcon->perf_sector_size)
1295 seq_printf(m, "\tOptimal sector size: 0x%x",
1296 tcon->perf_sector_size);
1297 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1298}
1299
1300static void
1301smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1302{
1303 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1304 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1305
1306 /*
1307 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1308 * totals (requests sent) since those SMBs are per-session not per tcon
1309 */
1310 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1311 (long long)(tcon->bytes_read),
1312 (long long)(tcon->bytes_written));
1313 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1314 atomic_read(&tcon->num_local_opens),
1315 atomic_read(&tcon->num_remote_opens));
1316 seq_printf(m, "\nTreeConnects: %d total %d failed",
1317 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1318 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1319 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1320 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1321 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1322 seq_printf(m, "\nCreates: %d total %d failed",
1323 atomic_read(&sent[SMB2_CREATE_HE]),
1324 atomic_read(&failed[SMB2_CREATE_HE]));
1325 seq_printf(m, "\nCloses: %d total %d failed",
1326 atomic_read(&sent[SMB2_CLOSE_HE]),
1327 atomic_read(&failed[SMB2_CLOSE_HE]));
1328 seq_printf(m, "\nFlushes: %d total %d failed",
1329 atomic_read(&sent[SMB2_FLUSH_HE]),
1330 atomic_read(&failed[SMB2_FLUSH_HE]));
1331 seq_printf(m, "\nReads: %d total %d failed",
1332 atomic_read(&sent[SMB2_READ_HE]),
1333 atomic_read(&failed[SMB2_READ_HE]));
1334 seq_printf(m, "\nWrites: %d total %d failed",
1335 atomic_read(&sent[SMB2_WRITE_HE]),
1336 atomic_read(&failed[SMB2_WRITE_HE]));
1337 seq_printf(m, "\nLocks: %d total %d failed",
1338 atomic_read(&sent[SMB2_LOCK_HE]),
1339 atomic_read(&failed[SMB2_LOCK_HE]));
1340 seq_printf(m, "\nIOCTLs: %d total %d failed",
1341 atomic_read(&sent[SMB2_IOCTL_HE]),
1342 atomic_read(&failed[SMB2_IOCTL_HE]));
1343 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1344 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1345 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1346 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1347 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1348 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1349 seq_printf(m, "\nQueryInfos: %d total %d failed",
1350 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1351 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1352 seq_printf(m, "\nSetInfos: %d total %d failed",
1353 atomic_read(&sent[SMB2_SET_INFO_HE]),
1354 atomic_read(&failed[SMB2_SET_INFO_HE]));
1355 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1356 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1357 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1358}
1359
1360static void
1361smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1362{
1363 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1364 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1365
1366 cfile->fid.persistent_fid = fid->persistent_fid;
1367 cfile->fid.volatile_fid = fid->volatile_fid;
1368 cfile->fid.access = fid->access;
1369#ifdef CONFIG_CIFS_DEBUG2
1370 cfile->fid.mid = fid->mid;
1371#endif /* CIFS_DEBUG2 */
1372 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1373 &fid->purge_cache);
1374 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1375 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1376}
1377
1378static void
1379smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1380 struct cifs_fid *fid)
1381{
1382 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1383}
1384
1385static int
1386SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1387 u64 persistent_fid, u64 volatile_fid,
1388 struct copychunk_ioctl *pcchunk)
1389{
1390 int rc;
1391 unsigned int ret_data_len;
1392 struct resume_key_req *res_key;
1393
1394 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1395 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1396 NULL, 0 /* no input */, CIFSMaxBufSize,
1397 (char **)&res_key, &ret_data_len);
1398
1399 if (rc) {
1400 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1401 goto req_res_key_exit;
1402 }
1403 if (ret_data_len < sizeof(struct resume_key_req)) {
1404 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1405 rc = -EINVAL;
1406 goto req_res_key_exit;
1407 }
1408 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1409
1410req_res_key_exit:
1411 kfree(res_key);
1412 return rc;
1413}
1414
1415static int
1416smb2_ioctl_query_info(const unsigned int xid,
1417 struct cifs_tcon *tcon,
1418 struct cifs_sb_info *cifs_sb,
1419 __le16 *path, int is_dir,
1420 unsigned long p)
1421{
1422 struct cifs_ses *ses = tcon->ses;
1423 char __user *arg = (char __user *)p;
1424 struct smb_query_info qi;
1425 struct smb_query_info __user *pqi;
1426 int rc = 0;
1427 int flags = 0;
1428 struct smb2_query_info_rsp *qi_rsp = NULL;
1429 struct smb2_ioctl_rsp *io_rsp = NULL;
1430 void *buffer = NULL;
1431 struct smb_rqst rqst[3];
1432 int resp_buftype[3];
1433 struct kvec rsp_iov[3];
1434 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1435 struct cifs_open_parms oparms;
1436 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1437 struct cifs_fid fid;
1438 struct kvec qi_iov[1];
1439 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1440 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1441 struct kvec close_iov[1];
1442 unsigned int size[2];
1443 void *data[2];
1444 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1445
1446 memset(rqst, 0, sizeof(rqst));
1447 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1448 memset(rsp_iov, 0, sizeof(rsp_iov));
1449
1450 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1451 return -EFAULT;
1452
1453 if (qi.output_buffer_length > 1024)
1454 return -EINVAL;
1455
1456 if (!ses || !(ses->server))
1457 return -EIO;
1458
1459 if (smb3_encryption_required(tcon))
1460 flags |= CIFS_TRANSFORM_REQ;
1461
1462 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1463 if (buffer == NULL)
1464 return -ENOMEM;
1465
1466 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1467 qi.output_buffer_length)) {
1468 rc = -EFAULT;
1469 goto iqinf_exit;
1470 }
1471
1472 /* Open */
1473 memset(&open_iov, 0, sizeof(open_iov));
1474 rqst[0].rq_iov = open_iov;
1475 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1476
1477 memset(&oparms, 0, sizeof(oparms));
1478 oparms.tcon = tcon;
1479 oparms.disposition = FILE_OPEN;
1480 oparms.create_options = cifs_create_options(cifs_sb, create_options);
1481 oparms.fid = &fid;
1482 oparms.reconnect = false;
1483
1484 if (qi.flags & PASSTHRU_FSCTL) {
1485 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1486 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1487 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1488 break;
1489 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1490 oparms.desired_access = GENERIC_ALL;
1491 break;
1492 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1493 oparms.desired_access = GENERIC_READ;
1494 break;
1495 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1496 oparms.desired_access = GENERIC_WRITE;
1497 break;
1498 }
1499 } else if (qi.flags & PASSTHRU_SET_INFO) {
1500 oparms.desired_access = GENERIC_WRITE;
1501 } else {
1502 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1503 }
1504
1505 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1506 if (rc)
1507 goto iqinf_exit;
1508 smb2_set_next_command(tcon, &rqst[0]);
1509
1510 /* Query */
1511 if (qi.flags & PASSTHRU_FSCTL) {
1512 /* Can eventually relax perm check since server enforces too */
1513 if (!capable(CAP_SYS_ADMIN))
1514 rc = -EPERM;
1515 else {
1516 memset(&io_iov, 0, sizeof(io_iov));
1517 rqst[1].rq_iov = io_iov;
1518 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1519
1520 rc = SMB2_ioctl_init(tcon, &rqst[1],
1521 COMPOUND_FID, COMPOUND_FID,
1522 qi.info_type, true, buffer,
1523 qi.output_buffer_length,
1524 CIFSMaxBufSize -
1525 MAX_SMB2_CREATE_RESPONSE_SIZE -
1526 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1527 }
1528 } else if (qi.flags == PASSTHRU_SET_INFO) {
1529 /* Can eventually relax perm check since server enforces too */
1530 if (!capable(CAP_SYS_ADMIN))
1531 rc = -EPERM;
1532 else {
1533 memset(&si_iov, 0, sizeof(si_iov));
1534 rqst[1].rq_iov = si_iov;
1535 rqst[1].rq_nvec = 1;
1536
1537 size[0] = 8;
1538 data[0] = buffer;
1539
1540 rc = SMB2_set_info_init(tcon, &rqst[1],
1541 COMPOUND_FID, COMPOUND_FID,
1542 current->tgid,
1543 FILE_END_OF_FILE_INFORMATION,
1544 SMB2_O_INFO_FILE, 0, data, size);
1545 }
1546 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1547 memset(&qi_iov, 0, sizeof(qi_iov));
1548 rqst[1].rq_iov = qi_iov;
1549 rqst[1].rq_nvec = 1;
1550
1551 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1552 COMPOUND_FID, qi.file_info_class,
1553 qi.info_type, qi.additional_information,
1554 qi.input_buffer_length,
1555 qi.output_buffer_length, buffer);
1556 } else { /* unknown flags */
1557 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1558 rc = -EINVAL;
1559 }
1560
1561 if (rc)
1562 goto iqinf_exit;
1563 smb2_set_next_command(tcon, &rqst[1]);
1564 smb2_set_related(&rqst[1]);
1565
1566 /* Close */
1567 memset(&close_iov, 0, sizeof(close_iov));
1568 rqst[2].rq_iov = close_iov;
1569 rqst[2].rq_nvec = 1;
1570
1571 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1572 if (rc)
1573 goto iqinf_exit;
1574 smb2_set_related(&rqst[2]);
1575
1576 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1577 resp_buftype, rsp_iov);
1578 if (rc)
1579 goto iqinf_exit;
1580
1581 /* No need to bump num_remote_opens since handle immediately closed */
1582 if (qi.flags & PASSTHRU_FSCTL) {
1583 pqi = (struct smb_query_info __user *)arg;
1584 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1585 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1586 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1587 if (qi.input_buffer_length > 0 &&
1588 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1589 > rsp_iov[1].iov_len)
1590 goto e_fault;
1591
1592 if (copy_to_user(&pqi->input_buffer_length,
1593 &qi.input_buffer_length,
1594 sizeof(qi.input_buffer_length)))
1595 goto e_fault;
1596
1597 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1598 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1599 qi.input_buffer_length))
1600 goto e_fault;
1601 } else {
1602 pqi = (struct smb_query_info __user *)arg;
1603 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1604 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1605 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1606 if (copy_to_user(&pqi->input_buffer_length,
1607 &qi.input_buffer_length,
1608 sizeof(qi.input_buffer_length)))
1609 goto e_fault;
1610
1611 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1612 qi.input_buffer_length))
1613 goto e_fault;
1614 }
1615
1616 iqinf_exit:
1617 kfree(buffer);
1618 SMB2_open_free(&rqst[0]);
1619 if (qi.flags & PASSTHRU_FSCTL)
1620 SMB2_ioctl_free(&rqst[1]);
1621 else
1622 SMB2_query_info_free(&rqst[1]);
1623
1624 SMB2_close_free(&rqst[2]);
1625 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1626 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1627 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1628 return rc;
1629
1630e_fault:
1631 rc = -EFAULT;
1632 goto iqinf_exit;
1633}
1634
1635static ssize_t
1636smb2_copychunk_range(const unsigned int xid,
1637 struct cifsFileInfo *srcfile,
1638 struct cifsFileInfo *trgtfile, u64 src_off,
1639 u64 len, u64 dest_off)
1640{
1641 int rc;
1642 unsigned int ret_data_len;
1643 struct copychunk_ioctl *pcchunk;
1644 struct copychunk_ioctl_rsp *retbuf = NULL;
1645 struct cifs_tcon *tcon;
1646 int chunks_copied = 0;
1647 bool chunk_sizes_updated = false;
1648 ssize_t bytes_written, total_bytes_written = 0;
1649 struct inode *inode;
1650
1651 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1652
1653 /*
1654 * We need to flush all unwritten data before we can send the
1655 * copychunk ioctl to the server.
1656 */
1657 inode = d_inode(trgtfile->dentry);
1658 filemap_write_and_wait(inode->i_mapping);
1659
1660 if (pcchunk == NULL)
1661 return -ENOMEM;
1662
1663 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1664 /* Request a key from the server to identify the source of the copy */
1665 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1666 srcfile->fid.persistent_fid,
1667 srcfile->fid.volatile_fid, pcchunk);
1668
1669 /* Note: request_res_key sets res_key null only if rc !=0 */
1670 if (rc)
1671 goto cchunk_out;
1672
1673 /* For now array only one chunk long, will make more flexible later */
1674 pcchunk->ChunkCount = cpu_to_le32(1);
1675 pcchunk->Reserved = 0;
1676 pcchunk->Reserved2 = 0;
1677
1678 tcon = tlink_tcon(trgtfile->tlink);
1679
1680 while (len > 0) {
1681 pcchunk->SourceOffset = cpu_to_le64(src_off);
1682 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1683 pcchunk->Length =
1684 cpu_to_le32(min_t(u64, len, tcon->max_bytes_chunk));
1685
1686 /* Request server copy to target from src identified by key */
1687 kfree(retbuf);
1688 retbuf = NULL;
1689 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1690 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1691 true /* is_fsctl */, (char *)pcchunk,
1692 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1693 (char **)&retbuf, &ret_data_len);
1694 if (rc == 0) {
1695 if (ret_data_len !=
1696 sizeof(struct copychunk_ioctl_rsp)) {
1697 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
1698 rc = -EIO;
1699 goto cchunk_out;
1700 }
1701 if (retbuf->TotalBytesWritten == 0) {
1702 cifs_dbg(FYI, "no bytes copied\n");
1703 rc = -EIO;
1704 goto cchunk_out;
1705 }
1706 /*
1707 * Check if server claimed to write more than we asked
1708 */
1709 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1710 le32_to_cpu(pcchunk->Length)) {
1711 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
1712 rc = -EIO;
1713 goto cchunk_out;
1714 }
1715 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1716 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
1717 rc = -EIO;
1718 goto cchunk_out;
1719 }
1720 chunks_copied++;
1721
1722 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1723 src_off += bytes_written;
1724 dest_off += bytes_written;
1725 len -= bytes_written;
1726 total_bytes_written += bytes_written;
1727
1728 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1729 le32_to_cpu(retbuf->ChunksWritten),
1730 le32_to_cpu(retbuf->ChunkBytesWritten),
1731 bytes_written);
1732 } else if (rc == -EINVAL) {
1733 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1734 goto cchunk_out;
1735
1736 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1737 le32_to_cpu(retbuf->ChunksWritten),
1738 le32_to_cpu(retbuf->ChunkBytesWritten),
1739 le32_to_cpu(retbuf->TotalBytesWritten));
1740
1741 /*
1742 * Check if this is the first request using these sizes,
1743 * (ie check if copy succeed once with original sizes
1744 * and check if the server gave us different sizes after
1745 * we already updated max sizes on previous request).
1746 * if not then why is the server returning an error now
1747 */
1748 if ((chunks_copied != 0) || chunk_sizes_updated)
1749 goto cchunk_out;
1750
1751 /* Check that server is not asking us to grow size */
1752 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1753 tcon->max_bytes_chunk)
1754 tcon->max_bytes_chunk =
1755 le32_to_cpu(retbuf->ChunkBytesWritten);
1756 else
1757 goto cchunk_out; /* server gave us bogus size */
1758
1759 /* No need to change MaxChunks since already set to 1 */
1760 chunk_sizes_updated = true;
1761 } else
1762 goto cchunk_out;
1763 }
1764
1765cchunk_out:
1766 kfree(pcchunk);
1767 kfree(retbuf);
1768 if (rc)
1769 return rc;
1770 else
1771 return total_bytes_written;
1772}
1773
1774static int
1775smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1776 struct cifs_fid *fid)
1777{
1778 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1779}
1780
1781static unsigned int
1782smb2_read_data_offset(char *buf)
1783{
1784 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1785
1786 return rsp->DataOffset;
1787}
1788
1789static unsigned int
1790smb2_read_data_length(char *buf, bool in_remaining)
1791{
1792 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1793
1794 if (in_remaining)
1795 return le32_to_cpu(rsp->DataRemaining);
1796
1797 return le32_to_cpu(rsp->DataLength);
1798}
1799
1800
1801static int
1802smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1803 struct cifs_io_parms *parms, unsigned int *bytes_read,
1804 char **buf, int *buf_type)
1805{
1806 parms->persistent_fid = pfid->persistent_fid;
1807 parms->volatile_fid = pfid->volatile_fid;
1808 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1809}
1810
1811static int
1812smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1813 struct cifs_io_parms *parms, unsigned int *written,
1814 struct kvec *iov, unsigned long nr_segs)
1815{
1816
1817 parms->persistent_fid = pfid->persistent_fid;
1818 parms->volatile_fid = pfid->volatile_fid;
1819 return SMB2_write(xid, parms, written, iov, nr_segs);
1820}
1821
1822/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1823static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1824 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1825{
1826 struct cifsInodeInfo *cifsi;
1827 int rc;
1828
1829 cifsi = CIFS_I(inode);
1830
1831 /* if file already sparse don't bother setting sparse again */
1832 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1833 return true; /* already sparse */
1834
1835 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1836 return true; /* already not sparse */
1837
1838 /*
1839 * Can't check for sparse support on share the usual way via the
1840 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1841 * since Samba server doesn't set the flag on the share, yet
1842 * supports the set sparse FSCTL and returns sparse correctly
1843 * in the file attributes. If we fail setting sparse though we
1844 * mark that server does not support sparse files for this share
1845 * to avoid repeatedly sending the unsupported fsctl to server
1846 * if the file is repeatedly extended.
1847 */
1848 if (tcon->broken_sparse_sup)
1849 return false;
1850
1851 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1852 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1853 true /* is_fctl */,
1854 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1855 if (rc) {
1856 tcon->broken_sparse_sup = true;
1857 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1858 return false;
1859 }
1860
1861 if (setsparse)
1862 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1863 else
1864 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1865
1866 return true;
1867}
1868
1869static int
1870smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1871 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1872{
1873 __le64 eof = cpu_to_le64(size);
1874 struct inode *inode;
1875
1876 /*
1877 * If extending file more than one page make sparse. Many Linux fs
1878 * make files sparse by default when extending via ftruncate
1879 */
1880 inode = d_inode(cfile->dentry);
1881
1882 if (!set_alloc && (size > inode->i_size + 8192)) {
1883 __u8 set_sparse = 1;
1884
1885 /* whether set sparse succeeds or not, extend the file */
1886 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1887 }
1888
1889 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1890 cfile->fid.volatile_fid, cfile->pid, &eof);
1891}
1892
1893static int
1894smb2_duplicate_extents(const unsigned int xid,
1895 struct cifsFileInfo *srcfile,
1896 struct cifsFileInfo *trgtfile, u64 src_off,
1897 u64 len, u64 dest_off)
1898{
1899 int rc;
1900 unsigned int ret_data_len;
1901 struct duplicate_extents_to_file dup_ext_buf;
1902 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1903
1904 /* server fileays advertise duplicate extent support with this flag */
1905 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1906 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1907 return -EOPNOTSUPP;
1908
1909 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1910 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1911 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1912 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1913 dup_ext_buf.ByteCount = cpu_to_le64(len);
1914 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1915 src_off, dest_off, len);
1916
1917 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1918 if (rc)
1919 goto duplicate_extents_out;
1920
1921 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1922 trgtfile->fid.volatile_fid,
1923 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1924 true /* is_fsctl */,
1925 (char *)&dup_ext_buf,
1926 sizeof(struct duplicate_extents_to_file),
1927 CIFSMaxBufSize, NULL,
1928 &ret_data_len);
1929
1930 if (ret_data_len > 0)
1931 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1932
1933duplicate_extents_out:
1934 return rc;
1935}
1936
1937static int
1938smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1939 struct cifsFileInfo *cfile)
1940{
1941 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1942 cfile->fid.volatile_fid);
1943}
1944
1945static int
1946smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1947 struct cifsFileInfo *cfile)
1948{
1949 struct fsctl_set_integrity_information_req integr_info;
1950 unsigned int ret_data_len;
1951
1952 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1953 integr_info.Flags = 0;
1954 integr_info.Reserved = 0;
1955
1956 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1957 cfile->fid.volatile_fid,
1958 FSCTL_SET_INTEGRITY_INFORMATION,
1959 true /* is_fsctl */,
1960 (char *)&integr_info,
1961 sizeof(struct fsctl_set_integrity_information_req),
1962 CIFSMaxBufSize, NULL,
1963 &ret_data_len);
1964
1965}
1966
1967/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1968#define GMT_TOKEN_SIZE 50
1969
1970#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1971
1972/*
1973 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1974 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1975 */
1976static int
1977smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1978 struct cifsFileInfo *cfile, void __user *ioc_buf)
1979{
1980 char *retbuf = NULL;
1981 unsigned int ret_data_len = 0;
1982 int rc;
1983 u32 max_response_size;
1984 struct smb_snapshot_array snapshot_in;
1985
1986 /*
1987 * On the first query to enumerate the list of snapshots available
1988 * for this volume the buffer begins with 0 (number of snapshots
1989 * which can be returned is zero since at that point we do not know
1990 * how big the buffer needs to be). On the second query,
1991 * it (ret_data_len) is set to number of snapshots so we can
1992 * know to set the maximum response size larger (see below).
1993 */
1994 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1995 return -EFAULT;
1996
1997 /*
1998 * Note that for snapshot queries that servers like Azure expect that
1999 * the first query be minimal size (and just used to get the number/size
2000 * of previous versions) so response size must be specified as EXACTLY
2001 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2002 * of eight bytes.
2003 */
2004 if (ret_data_len == 0)
2005 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2006 else
2007 max_response_size = CIFSMaxBufSize;
2008
2009 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2010 cfile->fid.volatile_fid,
2011 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2012 true /* is_fsctl */,
2013 NULL, 0 /* no input data */, max_response_size,
2014 (char **)&retbuf,
2015 &ret_data_len);
2016 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2017 rc, ret_data_len);
2018 if (rc)
2019 return rc;
2020
2021 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2022 /* Fixup buffer */
2023 if (copy_from_user(&snapshot_in, ioc_buf,
2024 sizeof(struct smb_snapshot_array))) {
2025 rc = -EFAULT;
2026 kfree(retbuf);
2027 return rc;
2028 }
2029
2030 /*
2031 * Check for min size, ie not large enough to fit even one GMT
2032 * token (snapshot). On the first ioctl some users may pass in
2033 * smaller size (or zero) to simply get the size of the array
2034 * so the user space caller can allocate sufficient memory
2035 * and retry the ioctl again with larger array size sufficient
2036 * to hold all of the snapshot GMT tokens on the second try.
2037 */
2038 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2039 ret_data_len = sizeof(struct smb_snapshot_array);
2040
2041 /*
2042 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2043 * the snapshot array (of 50 byte GMT tokens) each
2044 * representing an available previous version of the data
2045 */
2046 if (ret_data_len > (snapshot_in.snapshot_array_size +
2047 sizeof(struct smb_snapshot_array)))
2048 ret_data_len = snapshot_in.snapshot_array_size +
2049 sizeof(struct smb_snapshot_array);
2050
2051 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2052 rc = -EFAULT;
2053 }
2054
2055 kfree(retbuf);
2056 return rc;
2057}
2058
2059static int
2060smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2061 const char *path, struct cifs_sb_info *cifs_sb,
2062 struct cifs_fid *fid, __u16 search_flags,
2063 struct cifs_search_info *srch_inf)
2064{
2065 __le16 *utf16_path;
2066 int rc;
2067 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2068 struct cifs_open_parms oparms;
2069
2070 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2071 if (!utf16_path)
2072 return -ENOMEM;
2073
2074 oparms.tcon = tcon;
2075 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2076 oparms.disposition = FILE_OPEN;
2077 oparms.create_options = cifs_create_options(cifs_sb, 0);
2078 oparms.fid = fid;
2079 oparms.reconnect = false;
2080
2081 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2082 kfree(utf16_path);
2083 if (rc) {
2084 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
2085 return rc;
2086 }
2087
2088 srch_inf->entries_in_buffer = 0;
2089 srch_inf->index_of_last_entry = 2;
2090
2091 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
2092 fid->volatile_fid, 0, srch_inf);
2093 if (rc) {
2094 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
2095 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2096 }
2097 return rc;
2098}
2099
2100static int
2101smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2102 struct cifs_fid *fid, __u16 search_flags,
2103 struct cifs_search_info *srch_inf)
2104{
2105 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2106 fid->volatile_fid, 0, srch_inf);
2107}
2108
2109static int
2110smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2111 struct cifs_fid *fid)
2112{
2113 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2114}
2115
2116/*
2117 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2118 * the number of credits and return true. Otherwise - return false.
2119 */
2120static bool
2121smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2122{
2123 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2124
2125 if (shdr->Status != STATUS_PENDING)
2126 return false;
2127
2128 if (shdr->CreditRequest) {
2129 spin_lock(&server->req_lock);
2130 server->credits += le16_to_cpu(shdr->CreditRequest);
2131 spin_unlock(&server->req_lock);
2132 wake_up(&server->request_q);
2133 }
2134
2135 return true;
2136}
2137
2138static bool
2139smb2_is_session_expired(char *buf)
2140{
2141 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2142
2143 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2144 shdr->Status != STATUS_USER_SESSION_DELETED)
2145 return false;
2146
2147 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2148 le16_to_cpu(shdr->Command),
2149 le64_to_cpu(shdr->MessageId));
2150 cifs_dbg(FYI, "Session expired or deleted\n");
2151
2152 return true;
2153}
2154
2155static int
2156smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2157 struct cifsInodeInfo *cinode)
2158{
2159 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2160 return SMB2_lease_break(0, tcon, cinode->lease_key,
2161 smb2_get_lease_state(cinode));
2162
2163 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2164 fid->volatile_fid,
2165 CIFS_CACHE_READ(cinode) ? 1 : 0);
2166}
2167
2168void
2169smb2_set_related(struct smb_rqst *rqst)
2170{
2171 struct smb2_sync_hdr *shdr;
2172
2173 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2174 if (shdr == NULL) {
2175 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2176 return;
2177 }
2178 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2179}
2180
2181char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2182
2183void
2184smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2185{
2186 struct smb2_sync_hdr *shdr;
2187 struct cifs_ses *ses = tcon->ses;
2188 struct TCP_Server_Info *server = ses->server;
2189 unsigned long len = smb_rqst_len(server, rqst);
2190 int i, num_padding;
2191
2192 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2193 if (shdr == NULL) {
2194 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2195 return;
2196 }
2197
2198 /* SMB headers in a compound are 8 byte aligned. */
2199
2200 /* No padding needed */
2201 if (!(len & 7))
2202 goto finished;
2203
2204 num_padding = 8 - (len & 7);
2205 if (!smb3_encryption_required(tcon)) {
2206 /*
2207 * If we do not have encryption then we can just add an extra
2208 * iov for the padding.
2209 */
2210 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2211 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2212 rqst->rq_nvec++;
2213 len += num_padding;
2214 } else {
2215 /*
2216 * We can not add a small padding iov for the encryption case
2217 * because the encryption framework can not handle the padding
2218 * iovs.
2219 * We have to flatten this into a single buffer and add
2220 * the padding to it.
2221 */
2222 for (i = 1; i < rqst->rq_nvec; i++) {
2223 memcpy(rqst->rq_iov[0].iov_base +
2224 rqst->rq_iov[0].iov_len,
2225 rqst->rq_iov[i].iov_base,
2226 rqst->rq_iov[i].iov_len);
2227 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2228 }
2229 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2230 0, num_padding);
2231 rqst->rq_iov[0].iov_len += num_padding;
2232 len += num_padding;
2233 rqst->rq_nvec = 1;
2234 }
2235
2236 finished:
2237 shdr->NextCommand = cpu_to_le32(len);
2238}
2239
2240/*
2241 * Passes the query info response back to the caller on success.
2242 * Caller need to free this with free_rsp_buf().
2243 */
2244int
2245smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2246 __le16 *utf16_path, u32 desired_access,
2247 u32 class, u32 type, u32 output_len,
2248 struct kvec *rsp, int *buftype,
2249 struct cifs_sb_info *cifs_sb)
2250{
2251 struct cifs_ses *ses = tcon->ses;
2252 int flags = 0;
2253 struct smb_rqst rqst[3];
2254 int resp_buftype[3];
2255 struct kvec rsp_iov[3];
2256 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2257 struct kvec qi_iov[1];
2258 struct kvec close_iov[1];
2259 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2260 struct cifs_open_parms oparms;
2261 struct cifs_fid fid;
2262 int rc;
2263
2264 if (smb3_encryption_required(tcon))
2265 flags |= CIFS_TRANSFORM_REQ;
2266
2267 memset(rqst, 0, sizeof(rqst));
2268 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2269 memset(rsp_iov, 0, sizeof(rsp_iov));
2270
2271 memset(&open_iov, 0, sizeof(open_iov));
2272 rqst[0].rq_iov = open_iov;
2273 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2274
2275 oparms.tcon = tcon;
2276 oparms.desired_access = desired_access;
2277 oparms.disposition = FILE_OPEN;
2278 oparms.create_options = cifs_create_options(cifs_sb, 0);
2279 oparms.fid = &fid;
2280 oparms.reconnect = false;
2281
2282 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2283 if (rc)
2284 goto qic_exit;
2285 smb2_set_next_command(tcon, &rqst[0]);
2286
2287 memset(&qi_iov, 0, sizeof(qi_iov));
2288 rqst[1].rq_iov = qi_iov;
2289 rqst[1].rq_nvec = 1;
2290
2291 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
2292 class, type, 0,
2293 output_len, 0,
2294 NULL);
2295 if (rc)
2296 goto qic_exit;
2297 smb2_set_next_command(tcon, &rqst[1]);
2298 smb2_set_related(&rqst[1]);
2299
2300 memset(&close_iov, 0, sizeof(close_iov));
2301 rqst[2].rq_iov = close_iov;
2302 rqst[2].rq_nvec = 1;
2303
2304 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2305 if (rc)
2306 goto qic_exit;
2307 smb2_set_related(&rqst[2]);
2308
2309 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2310 resp_buftype, rsp_iov);
2311 if (rc) {
2312 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2313 if (rc == -EREMCHG) {
2314 tcon->need_reconnect = true;
2315 printk_once(KERN_WARNING "server share %s deleted\n",
2316 tcon->treeName);
2317 }
2318 goto qic_exit;
2319 }
2320 *rsp = rsp_iov[1];
2321 *buftype = resp_buftype[1];
2322
2323 qic_exit:
2324 SMB2_open_free(&rqst[0]);
2325 SMB2_query_info_free(&rqst[1]);
2326 SMB2_close_free(&rqst[2]);
2327 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2328 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2329 return rc;
2330}
2331
2332static int
2333smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2334 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2335{
2336 struct smb2_query_info_rsp *rsp;
2337 struct smb2_fs_full_size_info *info = NULL;
2338 __le16 utf16_path = 0; /* Null - open root of share */
2339 struct kvec rsp_iov = {NULL, 0};
2340 int buftype = CIFS_NO_BUFFER;
2341 int rc;
2342
2343
2344 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2345 FILE_READ_ATTRIBUTES,
2346 FS_FULL_SIZE_INFORMATION,
2347 SMB2_O_INFO_FILESYSTEM,
2348 sizeof(struct smb2_fs_full_size_info),
2349 &rsp_iov, &buftype, cifs_sb);
2350 if (rc)
2351 goto qfs_exit;
2352
2353 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2354 buf->f_type = SMB2_MAGIC_NUMBER;
2355 info = (struct smb2_fs_full_size_info *)(
2356 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2357 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2358 le32_to_cpu(rsp->OutputBufferLength),
2359 &rsp_iov,
2360 sizeof(struct smb2_fs_full_size_info));
2361 if (!rc)
2362 smb2_copy_fs_info_to_kstatfs(info, buf);
2363
2364qfs_exit:
2365 free_rsp_buf(buftype, rsp_iov.iov_base);
2366 return rc;
2367}
2368
2369static int
2370smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2371 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2372{
2373 int rc;
2374 __le16 srch_path = 0; /* Null - open root of share */
2375 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2376 struct cifs_open_parms oparms;
2377 struct cifs_fid fid;
2378
2379 if (!tcon->posix_extensions)
2380 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2381
2382 oparms.tcon = tcon;
2383 oparms.desired_access = FILE_READ_ATTRIBUTES;
2384 oparms.disposition = FILE_OPEN;
2385 oparms.create_options = cifs_create_options(cifs_sb, 0);
2386 oparms.fid = &fid;
2387 oparms.reconnect = false;
2388
2389 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2390 if (rc)
2391 return rc;
2392
2393 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2394 fid.volatile_fid, buf);
2395 buf->f_type = SMB2_MAGIC_NUMBER;
2396 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2397 return rc;
2398}
2399
2400static bool
2401smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2402{
2403 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2404 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2405}
2406
2407static int
2408smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2409 __u64 length, __u32 type, int lock, int unlock, bool wait)
2410{
2411 if (unlock && !lock)
2412 type = SMB2_LOCKFLAG_UNLOCK;
2413 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2414 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2415 current->tgid, length, offset, type, wait);
2416}
2417
2418static void
2419smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2420{
2421 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2422}
2423
2424static void
2425smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2426{
2427 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2428}
2429
2430static void
2431smb2_new_lease_key(struct cifs_fid *fid)
2432{
2433 generate_random_uuid(fid->lease_key);
2434}
2435
2436static int
2437smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2438 const char *search_name,
2439 struct dfs_info3_param **target_nodes,
2440 unsigned int *num_of_nodes,
2441 const struct nls_table *nls_codepage, int remap)
2442{
2443 int rc;
2444 __le16 *utf16_path = NULL;
2445 int utf16_path_len = 0;
2446 struct cifs_tcon *tcon;
2447 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2448 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2449 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2450
2451 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2452
2453 /*
2454 * Try to use the IPC tcon, otherwise just use any
2455 */
2456 tcon = ses->tcon_ipc;
2457 if (tcon == NULL) {
2458 spin_lock(&cifs_tcp_ses_lock);
2459 tcon = list_first_entry_or_null(&ses->tcon_list,
2460 struct cifs_tcon,
2461 tcon_list);
2462 if (tcon)
2463 tcon->tc_count++;
2464 spin_unlock(&cifs_tcp_ses_lock);
2465 }
2466
2467 if (tcon == NULL) {
2468 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2469 ses);
2470 rc = -ENOTCONN;
2471 goto out;
2472 }
2473
2474 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2475 &utf16_path_len,
2476 nls_codepage, remap);
2477 if (!utf16_path) {
2478 rc = -ENOMEM;
2479 goto out;
2480 }
2481
2482 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2483 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2484 if (!dfs_req) {
2485 rc = -ENOMEM;
2486 goto out;
2487 }
2488
2489 /* Highest DFS referral version understood */
2490 dfs_req->MaxReferralLevel = DFS_VERSION;
2491
2492 /* Path to resolve in an UTF-16 null-terminated string */
2493 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2494
2495 do {
2496 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2497 FSCTL_DFS_GET_REFERRALS,
2498 true /* is_fsctl */,
2499 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2500 (char **)&dfs_rsp, &dfs_rsp_size);
2501 } while (rc == -EAGAIN);
2502
2503 if (!rc && !dfs_rsp)
2504 rc = -EIO;
2505 if (rc) {
2506 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2507 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2508 goto out;
2509 }
2510
2511 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2512 num_of_nodes, target_nodes,
2513 nls_codepage, remap, search_name,
2514 true /* is_unicode */);
2515 if (rc) {
2516 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2517 goto out;
2518 }
2519
2520 out:
2521 if (tcon && !tcon->ipc) {
2522 /* ipc tcons are not refcounted */
2523 spin_lock(&cifs_tcp_ses_lock);
2524 tcon->tc_count--;
2525 spin_unlock(&cifs_tcp_ses_lock);
2526 }
2527 kfree(utf16_path);
2528 kfree(dfs_req);
2529 kfree(dfs_rsp);
2530 return rc;
2531}
2532
2533static int
2534parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2535 u32 plen, char **target_path,
2536 struct cifs_sb_info *cifs_sb)
2537{
2538 unsigned int len;
2539
2540 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2541 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2542 if (len < sizeof(symlink_buf->InodeType)) {
2543 cifs_dbg(VFS, "srv returned malformed nfs buffer\n");
2544 return -EIO;
2545 }
2546
2547 len -= sizeof(symlink_buf->InodeType);
2548
2549 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2550 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2551 le64_to_cpu(symlink_buf->InodeType));
2552 return -EOPNOTSUPP;
2553 }
2554
2555 *target_path = cifs_strndup_from_utf16(
2556 symlink_buf->PathBuffer,
2557 len, true, cifs_sb->local_nls);
2558 if (!(*target_path))
2559 return -ENOMEM;
2560
2561 convert_delimiter(*target_path, '/');
2562 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2563
2564 return 0;
2565}
2566
2567static int
2568parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2569 u32 plen, char **target_path,
2570 struct cifs_sb_info *cifs_sb)
2571{
2572 unsigned int sub_len;
2573 unsigned int sub_offset;
2574
2575 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2576
2577 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2578 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2579 if (sub_offset + 20 > plen ||
2580 sub_offset + sub_len + 20 > plen) {
2581 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2582 return -EIO;
2583 }
2584
2585 *target_path = cifs_strndup_from_utf16(
2586 symlink_buf->PathBuffer + sub_offset,
2587 sub_len, true, cifs_sb->local_nls);
2588 if (!(*target_path))
2589 return -ENOMEM;
2590
2591 convert_delimiter(*target_path, '/');
2592 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2593
2594 return 0;
2595}
2596
2597static int
2598parse_reparse_point(struct reparse_data_buffer *buf,
2599 u32 plen, char **target_path,
2600 struct cifs_sb_info *cifs_sb)
2601{
2602 if (plen < sizeof(struct reparse_data_buffer)) {
2603 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2604 "at least 8 bytes but was %d\n", plen);
2605 return -EIO;
2606 }
2607
2608 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2609 sizeof(struct reparse_data_buffer)) {
2610 cifs_dbg(VFS, "srv returned invalid reparse buf "
2611 "length: %d\n", plen);
2612 return -EIO;
2613 }
2614
2615 /* See MS-FSCC 2.1.2 */
2616 switch (le32_to_cpu(buf->ReparseTag)) {
2617 case IO_REPARSE_TAG_NFS:
2618 return parse_reparse_posix(
2619 (struct reparse_posix_data *)buf,
2620 plen, target_path, cifs_sb);
2621 case IO_REPARSE_TAG_SYMLINK:
2622 return parse_reparse_symlink(
2623 (struct reparse_symlink_data_buffer *)buf,
2624 plen, target_path, cifs_sb);
2625 default:
2626 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2627 "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2628 return -EOPNOTSUPP;
2629 }
2630}
2631
2632#define SMB2_SYMLINK_STRUCT_SIZE \
2633 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2634
2635static int
2636smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2637 struct cifs_sb_info *cifs_sb, const char *full_path,
2638 char **target_path, bool is_reparse_point)
2639{
2640 int rc;
2641 __le16 *utf16_path = NULL;
2642 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2643 struct cifs_open_parms oparms;
2644 struct cifs_fid fid;
2645 struct kvec err_iov = {NULL, 0};
2646 struct smb2_err_rsp *err_buf = NULL;
2647 struct smb2_symlink_err_rsp *symlink;
2648 unsigned int sub_len;
2649 unsigned int sub_offset;
2650 unsigned int print_len;
2651 unsigned int print_offset;
2652 int flags = 0;
2653 struct smb_rqst rqst[3];
2654 int resp_buftype[3];
2655 struct kvec rsp_iov[3];
2656 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2657 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2658 struct kvec close_iov[1];
2659 struct smb2_create_rsp *create_rsp;
2660 struct smb2_ioctl_rsp *ioctl_rsp;
2661 struct reparse_data_buffer *reparse_buf;
2662 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2663 u32 plen;
2664
2665 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2666
2667 *target_path = NULL;
2668
2669 if (smb3_encryption_required(tcon))
2670 flags |= CIFS_TRANSFORM_REQ;
2671
2672 memset(rqst, 0, sizeof(rqst));
2673 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2674 memset(rsp_iov, 0, sizeof(rsp_iov));
2675
2676 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2677 if (!utf16_path)
2678 return -ENOMEM;
2679
2680 /* Open */
2681 memset(&open_iov, 0, sizeof(open_iov));
2682 rqst[0].rq_iov = open_iov;
2683 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2684
2685 memset(&oparms, 0, sizeof(oparms));
2686 oparms.tcon = tcon;
2687 oparms.desired_access = FILE_READ_ATTRIBUTES;
2688 oparms.disposition = FILE_OPEN;
2689 oparms.create_options = cifs_create_options(cifs_sb, create_options);
2690 oparms.fid = &fid;
2691 oparms.reconnect = false;
2692
2693 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2694 if (rc)
2695 goto querty_exit;
2696 smb2_set_next_command(tcon, &rqst[0]);
2697
2698
2699 /* IOCTL */
2700 memset(&io_iov, 0, sizeof(io_iov));
2701 rqst[1].rq_iov = io_iov;
2702 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2703
2704 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2705 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2706 true /* is_fctl */, NULL, 0,
2707 CIFSMaxBufSize -
2708 MAX_SMB2_CREATE_RESPONSE_SIZE -
2709 MAX_SMB2_CLOSE_RESPONSE_SIZE);
2710 if (rc)
2711 goto querty_exit;
2712
2713 smb2_set_next_command(tcon, &rqst[1]);
2714 smb2_set_related(&rqst[1]);
2715
2716
2717 /* Close */
2718 memset(&close_iov, 0, sizeof(close_iov));
2719 rqst[2].rq_iov = close_iov;
2720 rqst[2].rq_nvec = 1;
2721
2722 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2723 if (rc)
2724 goto querty_exit;
2725
2726 smb2_set_related(&rqst[2]);
2727
2728 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2729 resp_buftype, rsp_iov);
2730
2731 create_rsp = rsp_iov[0].iov_base;
2732 if (create_rsp && create_rsp->sync_hdr.Status)
2733 err_iov = rsp_iov[0];
2734 ioctl_rsp = rsp_iov[1].iov_base;
2735
2736 /*
2737 * Open was successful and we got an ioctl response.
2738 */
2739 if ((rc == 0) && (is_reparse_point)) {
2740 /* See MS-FSCC 2.3.23 */
2741
2742 reparse_buf = (struct reparse_data_buffer *)
2743 ((char *)ioctl_rsp +
2744 le32_to_cpu(ioctl_rsp->OutputOffset));
2745 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2746
2747 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2748 rsp_iov[1].iov_len) {
2749 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2750 plen);
2751 rc = -EIO;
2752 goto querty_exit;
2753 }
2754
2755 rc = parse_reparse_point(reparse_buf, plen, target_path,
2756 cifs_sb);
2757 goto querty_exit;
2758 }
2759
2760 if (!rc || !err_iov.iov_base) {
2761 rc = -ENOENT;
2762 goto querty_exit;
2763 }
2764
2765 err_buf = err_iov.iov_base;
2766 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2767 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2768 rc = -EINVAL;
2769 goto querty_exit;
2770 }
2771
2772 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2773 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2774 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2775 rc = -EINVAL;
2776 goto querty_exit;
2777 }
2778
2779 /* open must fail on symlink - reset rc */
2780 rc = 0;
2781 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2782 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
2783 print_len = le16_to_cpu(symlink->PrintNameLength);
2784 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2785
2786 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
2787 rc = -EINVAL;
2788 goto querty_exit;
2789 }
2790
2791 if (err_iov.iov_len <
2792 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
2793 rc = -EINVAL;
2794 goto querty_exit;
2795 }
2796
2797 *target_path = cifs_strndup_from_utf16(
2798 (char *)symlink->PathBuffer + sub_offset,
2799 sub_len, true, cifs_sb->local_nls);
2800 if (!(*target_path)) {
2801 rc = -ENOMEM;
2802 goto querty_exit;
2803 }
2804 convert_delimiter(*target_path, '/');
2805 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2806
2807 querty_exit:
2808 cifs_dbg(FYI, "query symlink rc %d\n", rc);
2809 kfree(utf16_path);
2810 SMB2_open_free(&rqst[0]);
2811 SMB2_ioctl_free(&rqst[1]);
2812 SMB2_close_free(&rqst[2]);
2813 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2814 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2815 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2816 return rc;
2817}
2818
2819static struct cifs_ntsd *
2820get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2821 const struct cifs_fid *cifsfid, u32 *pacllen)
2822{
2823 struct cifs_ntsd *pntsd = NULL;
2824 unsigned int xid;
2825 int rc = -EOPNOTSUPP;
2826 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2827
2828 if (IS_ERR(tlink))
2829 return ERR_CAST(tlink);
2830
2831 xid = get_xid();
2832 cifs_dbg(FYI, "trying to get acl\n");
2833
2834 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2835 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2836 free_xid(xid);
2837
2838 cifs_put_tlink(tlink);
2839
2840 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2841 if (rc)
2842 return ERR_PTR(rc);
2843 return pntsd;
2844
2845}
2846
2847static struct cifs_ntsd *
2848get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2849 const char *path, u32 *pacllen)
2850{
2851 struct cifs_ntsd *pntsd = NULL;
2852 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2853 unsigned int xid;
2854 int rc;
2855 struct cifs_tcon *tcon;
2856 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2857 struct cifs_fid fid;
2858 struct cifs_open_parms oparms;
2859 __le16 *utf16_path;
2860
2861 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2862 if (IS_ERR(tlink))
2863 return ERR_CAST(tlink);
2864
2865 tcon = tlink_tcon(tlink);
2866 xid = get_xid();
2867
2868 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2869 if (!utf16_path) {
2870 rc = -ENOMEM;
2871 free_xid(xid);
2872 return ERR_PTR(rc);
2873 }
2874
2875 oparms.tcon = tcon;
2876 oparms.desired_access = READ_CONTROL;
2877 oparms.disposition = FILE_OPEN;
2878 oparms.create_options = cifs_create_options(cifs_sb, 0);
2879 oparms.fid = &fid;
2880 oparms.reconnect = false;
2881
2882 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2883 kfree(utf16_path);
2884 if (!rc) {
2885 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2886 fid.volatile_fid, (void **)&pntsd, pacllen);
2887 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2888 }
2889
2890 cifs_put_tlink(tlink);
2891 free_xid(xid);
2892
2893 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2894 if (rc)
2895 return ERR_PTR(rc);
2896 return pntsd;
2897}
2898
2899static int
2900set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2901 struct inode *inode, const char *path, int aclflag)
2902{
2903 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2904 unsigned int xid;
2905 int rc, access_flags = 0;
2906 struct cifs_tcon *tcon;
2907 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2908 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2909 struct cifs_fid fid;
2910 struct cifs_open_parms oparms;
2911 __le16 *utf16_path;
2912
2913 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2914 if (IS_ERR(tlink))
2915 return PTR_ERR(tlink);
2916
2917 tcon = tlink_tcon(tlink);
2918 xid = get_xid();
2919
2920 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2921 access_flags = WRITE_OWNER;
2922 else
2923 access_flags = WRITE_DAC;
2924
2925 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2926 if (!utf16_path) {
2927 rc = -ENOMEM;
2928 free_xid(xid);
2929 return rc;
2930 }
2931
2932 oparms.tcon = tcon;
2933 oparms.desired_access = access_flags;
2934 oparms.create_options = cifs_create_options(cifs_sb, 0);
2935 oparms.disposition = FILE_OPEN;
2936 oparms.path = path;
2937 oparms.fid = &fid;
2938 oparms.reconnect = false;
2939
2940 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
2941 kfree(utf16_path);
2942 if (!rc) {
2943 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2944 fid.volatile_fid, pnntsd, acllen, aclflag);
2945 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2946 }
2947
2948 cifs_put_tlink(tlink);
2949 free_xid(xid);
2950 return rc;
2951}
2952
2953/* Retrieve an ACL from the server */
2954static struct cifs_ntsd *
2955get_smb2_acl(struct cifs_sb_info *cifs_sb,
2956 struct inode *inode, const char *path,
2957 u32 *pacllen)
2958{
2959 struct cifs_ntsd *pntsd = NULL;
2960 struct cifsFileInfo *open_file = NULL;
2961
2962 if (inode)
2963 open_file = find_readable_file(CIFS_I(inode), true);
2964 if (!open_file)
2965 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2966
2967 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2968 cifsFileInfo_put(open_file);
2969 return pntsd;
2970}
2971
2972static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2973 loff_t offset, loff_t len, bool keep_size)
2974{
2975 struct cifs_ses *ses = tcon->ses;
2976 struct inode *inode;
2977 struct cifsInodeInfo *cifsi;
2978 struct cifsFileInfo *cfile = file->private_data;
2979 struct file_zero_data_information fsctl_buf;
2980 long rc;
2981 unsigned int xid;
2982 __le64 eof;
2983
2984 xid = get_xid();
2985
2986 inode = d_inode(cfile->dentry);
2987 cifsi = CIFS_I(inode);
2988
2989 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2990 ses->Suid, offset, len);
2991
2992 /*
2993 * We zero the range through ioctl, so we need remove the page caches
2994 * first, otherwise the data may be inconsistent with the server.
2995 */
2996 truncate_pagecache_range(inode, offset, offset + len - 1);
2997
2998 /* if file not oplocked can't be sure whether asking to extend size */
2999 if (!CIFS_CACHE_READ(cifsi))
3000 if (keep_size == false) {
3001 rc = -EOPNOTSUPP;
3002 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3003 tcon->tid, ses->Suid, offset, len, rc);
3004 free_xid(xid);
3005 return rc;
3006 }
3007
3008 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3009
3010 fsctl_buf.FileOffset = cpu_to_le64(offset);
3011 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3012
3013 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3014 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3015 (char *)&fsctl_buf,
3016 sizeof(struct file_zero_data_information),
3017 0, NULL, NULL);
3018 if (rc)
3019 goto zero_range_exit;
3020
3021 /*
3022 * do we also need to change the size of the file?
3023 */
3024 if (keep_size == false && i_size_read(inode) < offset + len) {
3025 eof = cpu_to_le64(offset + len);
3026 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3027 cfile->fid.volatile_fid, cfile->pid, &eof);
3028 }
3029
3030 zero_range_exit:
3031 free_xid(xid);
3032 if (rc)
3033 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3034 ses->Suid, offset, len, rc);
3035 else
3036 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3037 ses->Suid, offset, len);
3038 return rc;
3039}
3040
3041static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3042 loff_t offset, loff_t len)
3043{
3044 struct inode *inode = file_inode(file);
3045 struct cifsFileInfo *cfile = file->private_data;
3046 struct file_zero_data_information fsctl_buf;
3047 long rc;
3048 unsigned int xid;
3049 __u8 set_sparse = 1;
3050
3051 xid = get_xid();
3052
3053 inode_lock(inode);
3054 /* Need to make file sparse, if not already, before freeing range. */
3055 /* Consider adding equivalent for compressed since it could also work */
3056 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3057 rc = -EOPNOTSUPP;
3058 goto out;
3059 }
3060
3061 /*
3062 * We implement the punch hole through ioctl, so we need remove the page
3063 * caches first, otherwise the data may be inconsistent with the server.
3064 */
3065 truncate_pagecache_range(inode, offset, offset + len - 1);
3066
3067 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3068
3069 fsctl_buf.FileOffset = cpu_to_le64(offset);
3070 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3071
3072 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3073 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3074 true /* is_fctl */, (char *)&fsctl_buf,
3075 sizeof(struct file_zero_data_information),
3076 CIFSMaxBufSize, NULL, NULL);
3077out:
3078 inode_unlock(inode);
3079 free_xid(xid);
3080 return rc;
3081}
3082
3083static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3084 loff_t off, loff_t len, bool keep_size)
3085{
3086 struct inode *inode;
3087 struct cifsInodeInfo *cifsi;
3088 struct cifsFileInfo *cfile = file->private_data;
3089 long rc = -EOPNOTSUPP;
3090 unsigned int xid;
3091 __le64 eof;
3092
3093 xid = get_xid();
3094
3095 inode = d_inode(cfile->dentry);
3096 cifsi = CIFS_I(inode);
3097
3098 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3099 tcon->ses->Suid, off, len);
3100 /* if file not oplocked can't be sure whether asking to extend size */
3101 if (!CIFS_CACHE_READ(cifsi))
3102 if (keep_size == false) {
3103 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3104 tcon->tid, tcon->ses->Suid, off, len, rc);
3105 free_xid(xid);
3106 return rc;
3107 }
3108
3109 /*
3110 * Files are non-sparse by default so falloc may be a no-op
3111 * Must check if file sparse. If not sparse, and not extending
3112 * then no need to do anything since file already allocated
3113 */
3114 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3115 if (keep_size == true)
3116 rc = 0;
3117 /* check if extending file */
3118 else if (i_size_read(inode) >= off + len)
3119 /* not extending file and already not sparse */
3120 rc = 0;
3121 /* BB: in future add else clause to extend file */
3122 else
3123 rc = -EOPNOTSUPP;
3124 if (rc)
3125 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3126 tcon->tid, tcon->ses->Suid, off, len, rc);
3127 else
3128 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
3129 tcon->tid, tcon->ses->Suid, off, len);
3130 free_xid(xid);
3131 return rc;
3132 }
3133
3134 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3135 /*
3136 * Check if falloc starts within first few pages of file
3137 * and ends within a few pages of the end of file to
3138 * ensure that most of file is being forced to be
3139 * fallocated now. If so then setting whole file sparse
3140 * ie potentially making a few extra pages at the beginning
3141 * or end of the file non-sparse via set_sparse is harmless.
3142 */
3143 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3144 rc = -EOPNOTSUPP;
3145 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3146 tcon->tid, tcon->ses->Suid, off, len, rc);
3147 free_xid(xid);
3148 return rc;
3149 }
3150
3151 smb2_set_sparse(xid, tcon, cfile, inode, false);
3152 rc = 0;
3153 } else {
3154 smb2_set_sparse(xid, tcon, cfile, inode, false);
3155 rc = 0;
3156 if (i_size_read(inode) < off + len) {
3157 eof = cpu_to_le64(off + len);
3158 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3159 cfile->fid.volatile_fid, cfile->pid,
3160 &eof);
3161 }
3162 }
3163
3164 if (rc)
3165 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3166 tcon->ses->Suid, off, len, rc);
3167 else
3168 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3169 tcon->ses->Suid, off, len);
3170
3171 free_xid(xid);
3172 return rc;
3173}
3174
3175static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3176{
3177 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3178 struct cifsInodeInfo *cifsi;
3179 struct inode *inode;
3180 int rc = 0;
3181 struct file_allocated_range_buffer in_data, *out_data = NULL;
3182 u32 out_data_len;
3183 unsigned int xid;
3184
3185 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3186 return generic_file_llseek(file, offset, whence);
3187
3188 inode = d_inode(cfile->dentry);
3189 cifsi = CIFS_I(inode);
3190
3191 if (offset < 0 || offset >= i_size_read(inode))
3192 return -ENXIO;
3193
3194 xid = get_xid();
3195 /*
3196 * We need to be sure that all dirty pages are written as they
3197 * might fill holes on the server.
3198 * Note that we also MUST flush any written pages since at least
3199 * some servers (Windows2016) will not reflect recent writes in
3200 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3201 */
3202 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3203 if (wrcfile) {
3204 filemap_write_and_wait(inode->i_mapping);
3205 smb2_flush_file(xid, tcon, &wrcfile->fid);
3206 cifsFileInfo_put(wrcfile);
3207 }
3208
3209 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3210 if (whence == SEEK_HOLE)
3211 offset = i_size_read(inode);
3212 goto lseek_exit;
3213 }
3214
3215 in_data.file_offset = cpu_to_le64(offset);
3216 in_data.length = cpu_to_le64(i_size_read(inode));
3217
3218 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3219 cfile->fid.volatile_fid,
3220 FSCTL_QUERY_ALLOCATED_RANGES, true,
3221 (char *)&in_data, sizeof(in_data),
3222 sizeof(struct file_allocated_range_buffer),
3223 (char **)&out_data, &out_data_len);
3224 if (rc == -E2BIG)
3225 rc = 0;
3226 if (rc)
3227 goto lseek_exit;
3228
3229 if (whence == SEEK_HOLE && out_data_len == 0)
3230 goto lseek_exit;
3231
3232 if (whence == SEEK_DATA && out_data_len == 0) {
3233 rc = -ENXIO;
3234 goto lseek_exit;
3235 }
3236
3237 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3238 rc = -EINVAL;
3239 goto lseek_exit;
3240 }
3241 if (whence == SEEK_DATA) {
3242 offset = le64_to_cpu(out_data->file_offset);
3243 goto lseek_exit;
3244 }
3245 if (offset < le64_to_cpu(out_data->file_offset))
3246 goto lseek_exit;
3247
3248 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3249
3250 lseek_exit:
3251 free_xid(xid);
3252 kfree(out_data);
3253 if (!rc)
3254 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3255 else
3256 return rc;
3257}
3258
3259static int smb3_fiemap(struct cifs_tcon *tcon,
3260 struct cifsFileInfo *cfile,
3261 struct fiemap_extent_info *fei, u64 start, u64 len)
3262{
3263 unsigned int xid;
3264 struct file_allocated_range_buffer in_data, *out_data;
3265 u32 out_data_len;
3266 int i, num, rc, flags, last_blob;
3267 u64 next;
3268
3269 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3270 return -EBADR;
3271
3272 xid = get_xid();
3273 again:
3274 in_data.file_offset = cpu_to_le64(start);
3275 in_data.length = cpu_to_le64(len);
3276
3277 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3278 cfile->fid.volatile_fid,
3279 FSCTL_QUERY_ALLOCATED_RANGES, true,
3280 (char *)&in_data, sizeof(in_data),
3281 1024 * sizeof(struct file_allocated_range_buffer),
3282 (char **)&out_data, &out_data_len);
3283 if (rc == -E2BIG) {
3284 last_blob = 0;
3285 rc = 0;
3286 } else
3287 last_blob = 1;
3288 if (rc)
3289 goto out;
3290
3291 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3292 rc = -EINVAL;
3293 goto out;
3294 }
3295 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3296 rc = -EINVAL;
3297 goto out;
3298 }
3299
3300 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3301 for (i = 0; i < num; i++) {
3302 flags = 0;
3303 if (i == num - 1 && last_blob)
3304 flags |= FIEMAP_EXTENT_LAST;
3305
3306 rc = fiemap_fill_next_extent(fei,
3307 le64_to_cpu(out_data[i].file_offset),
3308 le64_to_cpu(out_data[i].file_offset),
3309 le64_to_cpu(out_data[i].length),
3310 flags);
3311 if (rc < 0)
3312 goto out;
3313 if (rc == 1) {
3314 rc = 0;
3315 goto out;
3316 }
3317 }
3318
3319 if (!last_blob) {
3320 next = le64_to_cpu(out_data[num - 1].file_offset) +
3321 le64_to_cpu(out_data[num - 1].length);
3322 len = len - (next - start);
3323 start = next;
3324 goto again;
3325 }
3326
3327 out:
3328 free_xid(xid);
3329 kfree(out_data);
3330 return rc;
3331}
3332
3333static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3334 loff_t off, loff_t len)
3335{
3336 /* KEEP_SIZE already checked for by do_fallocate */
3337 if (mode & FALLOC_FL_PUNCH_HOLE)
3338 return smb3_punch_hole(file, tcon, off, len);
3339 else if (mode & FALLOC_FL_ZERO_RANGE) {
3340 if (mode & FALLOC_FL_KEEP_SIZE)
3341 return smb3_zero_range(file, tcon, off, len, true);
3342 return smb3_zero_range(file, tcon, off, len, false);
3343 } else if (mode == FALLOC_FL_KEEP_SIZE)
3344 return smb3_simple_falloc(file, tcon, off, len, true);
3345 else if (mode == 0)
3346 return smb3_simple_falloc(file, tcon, off, len, false);
3347
3348 return -EOPNOTSUPP;
3349}
3350
3351static void
3352smb2_downgrade_oplock(struct TCP_Server_Info *server,
3353 struct cifsInodeInfo *cinode, __u32 oplock,
3354 unsigned int epoch, bool *purge_cache)
3355{
3356 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3357}
3358
3359static void
3360smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3361 unsigned int epoch, bool *purge_cache);
3362
3363static void
3364smb3_downgrade_oplock(struct TCP_Server_Info *server,
3365 struct cifsInodeInfo *cinode, __u32 oplock,
3366 unsigned int epoch, bool *purge_cache)
3367{
3368 unsigned int old_state = cinode->oplock;
3369 unsigned int old_epoch = cinode->epoch;
3370 unsigned int new_state;
3371
3372 if (epoch > old_epoch) {
3373 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3374 cinode->epoch = epoch;
3375 }
3376
3377 new_state = cinode->oplock;
3378 *purge_cache = false;
3379
3380 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3381 (new_state & CIFS_CACHE_READ_FLG) == 0)
3382 *purge_cache = true;
3383 else if (old_state == new_state && (epoch - old_epoch > 1))
3384 *purge_cache = true;
3385}
3386
3387static void
3388smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3389 unsigned int epoch, bool *purge_cache)
3390{
3391 oplock &= 0xFF;
3392 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3393 return;
3394 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3395 cinode->oplock = CIFS_CACHE_RHW_FLG;
3396 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3397 &cinode->vfs_inode);
3398 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3399 cinode->oplock = CIFS_CACHE_RW_FLG;
3400 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3401 &cinode->vfs_inode);
3402 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3403 cinode->oplock = CIFS_CACHE_READ_FLG;
3404 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3405 &cinode->vfs_inode);
3406 } else
3407 cinode->oplock = 0;
3408}
3409
3410static void
3411smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3412 unsigned int epoch, bool *purge_cache)
3413{
3414 char message[5] = {0};
3415 unsigned int new_oplock = 0;
3416
3417 oplock &= 0xFF;
3418 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3419 return;
3420
3421 /* Check if the server granted an oplock rather than a lease */
3422 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3423 return smb2_set_oplock_level(cinode, oplock, epoch,
3424 purge_cache);
3425
3426 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3427 new_oplock |= CIFS_CACHE_READ_FLG;
3428 strcat(message, "R");
3429 }
3430 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3431 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3432 strcat(message, "H");
3433 }
3434 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3435 new_oplock |= CIFS_CACHE_WRITE_FLG;
3436 strcat(message, "W");
3437 }
3438 if (!new_oplock)
3439 strncpy(message, "None", sizeof(message));
3440
3441 cinode->oplock = new_oplock;
3442 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3443 &cinode->vfs_inode);
3444}
3445
3446static void
3447smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3448 unsigned int epoch, bool *purge_cache)
3449{
3450 unsigned int old_oplock = cinode->oplock;
3451
3452 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3453
3454 if (purge_cache) {
3455 *purge_cache = false;
3456 if (old_oplock == CIFS_CACHE_READ_FLG) {
3457 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3458 (epoch - cinode->epoch > 0))
3459 *purge_cache = true;
3460 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3461 (epoch - cinode->epoch > 1))
3462 *purge_cache = true;
3463 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3464 (epoch - cinode->epoch > 1))
3465 *purge_cache = true;
3466 else if (cinode->oplock == 0 &&
3467 (epoch - cinode->epoch > 0))
3468 *purge_cache = true;
3469 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3470 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3471 (epoch - cinode->epoch > 0))
3472 *purge_cache = true;
3473 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3474 (epoch - cinode->epoch > 1))
3475 *purge_cache = true;
3476 }
3477 cinode->epoch = epoch;
3478 }
3479}
3480
3481#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3482static bool
3483smb2_is_read_op(__u32 oplock)
3484{
3485 return oplock == SMB2_OPLOCK_LEVEL_II;
3486}
3487#endif /* CIFS_ALLOW_INSECURE_LEGACY */
3488
3489static bool
3490smb21_is_read_op(__u32 oplock)
3491{
3492 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3493 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3494}
3495
3496static __le32
3497map_oplock_to_lease(u8 oplock)
3498{
3499 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3500 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3501 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3502 return SMB2_LEASE_READ_CACHING;
3503 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3504 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3505 SMB2_LEASE_WRITE_CACHING;
3506 return 0;
3507}
3508
3509static char *
3510smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3511{
3512 struct create_lease *buf;
3513
3514 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3515 if (!buf)
3516 return NULL;
3517
3518 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3519 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3520
3521 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3522 (struct create_lease, lcontext));
3523 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3524 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3525 (struct create_lease, Name));
3526 buf->ccontext.NameLength = cpu_to_le16(4);
3527 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3528 buf->Name[0] = 'R';
3529 buf->Name[1] = 'q';
3530 buf->Name[2] = 'L';
3531 buf->Name[3] = 's';
3532 return (char *)buf;
3533}
3534
3535static char *
3536smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3537{
3538 struct create_lease_v2 *buf;
3539
3540 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3541 if (!buf)
3542 return NULL;
3543
3544 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3545 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3546
3547 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3548 (struct create_lease_v2, lcontext));
3549 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3550 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3551 (struct create_lease_v2, Name));
3552 buf->ccontext.NameLength = cpu_to_le16(4);
3553 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3554 buf->Name[0] = 'R';
3555 buf->Name[1] = 'q';
3556 buf->Name[2] = 'L';
3557 buf->Name[3] = 's';
3558 return (char *)buf;
3559}
3560
3561static __u8
3562smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3563{
3564 struct create_lease *lc = (struct create_lease *)buf;
3565
3566 *epoch = 0; /* not used */
3567 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3568 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3569 return le32_to_cpu(lc->lcontext.LeaseState);
3570}
3571
3572static __u8
3573smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3574{
3575 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3576
3577 *epoch = le16_to_cpu(lc->lcontext.Epoch);
3578 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3579 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3580 if (lease_key)
3581 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3582 return le32_to_cpu(lc->lcontext.LeaseState);
3583}
3584
3585static unsigned int
3586smb2_wp_retry_size(struct inode *inode)
3587{
3588 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3589 SMB2_MAX_BUFFER_SIZE);
3590}
3591
3592static bool
3593smb2_dir_needs_close(struct cifsFileInfo *cfile)
3594{
3595 return !cfile->invalidHandle;
3596}
3597
3598static void
3599fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3600 struct smb_rqst *old_rq, __le16 cipher_type)
3601{
3602 struct smb2_sync_hdr *shdr =
3603 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3604
3605 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3606 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3607 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3608 tr_hdr->Flags = cpu_to_le16(0x01);
3609 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3610 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3611 else
3612 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3613 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3614}
3615
3616static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
3617 int num_rqst, const u8 *sig, u8 **iv,
3618 struct aead_request **req, struct scatterlist **sgl,
3619 unsigned int *num_sgs)
3620{
3621 unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
3622 unsigned int iv_size = crypto_aead_ivsize(tfm);
3623 unsigned int len;
3624 u8 *p;
3625
3626 *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
3627
3628 len = iv_size;
3629 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
3630 len = ALIGN(len, crypto_tfm_ctx_alignment());
3631 len += req_size;
3632 len = ALIGN(len, __alignof__(struct scatterlist));
3633 len += *num_sgs * sizeof(**sgl);
3634
3635 p = kmalloc(len, GFP_ATOMIC);
3636 if (!p)
3637 return NULL;
3638
3639 *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
3640 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
3641 crypto_tfm_ctx_alignment());
3642 *sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
3643 __alignof__(struct scatterlist));
3644 return p;
3645}
3646
3647static void *smb2_get_aead_req(struct crypto_aead *tfm, const struct smb_rqst *rqst,
3648 int num_rqst, const u8 *sig, u8 **iv,
3649 struct aead_request **req, struct scatterlist **sgl)
3650{
3651 unsigned int off, len, skip;
3652 struct scatterlist *sg;
3653 unsigned int num_sgs;
3654 unsigned long addr;
3655 int i, j;
3656 void *p;
3657
3658 p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, sgl, &num_sgs);
3659 if (!p)
3660 return NULL;
3661
3662 sg_init_table(*sgl, num_sgs);
3663 sg = *sgl;
3664
3665 /* Assumes the first rqst has a transform header as the first iov.
3666 * I.e.
3667 * rqst[0].rq_iov[0] is transform header
3668 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3669 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3670 */
3671 for (i = 0; i < num_rqst; i++) {
3672 /*
3673 * The first rqst has a transform header where the
3674 * first 20 bytes are not part of the encrypted blob.
3675 */
3676 for (j = 0; j < rqst[i].rq_nvec; j++) {
3677 struct kvec *iov = &rqst[i].rq_iov[j];
3678
3679 skip = (i == 0) && (j == 0) ? 20 : 0;
3680 addr = (unsigned long)iov->iov_base + skip;
3681 len = iov->iov_len - skip;
3682 sg = cifs_sg_set_buf(sg, (void *)addr, len);
3683 }
3684 for (j = 0; j < rqst[i].rq_npages; j++) {
3685 rqst_page_get_length(&rqst[i], j, &len, &off);
3686 sg_set_page(sg++, rqst[i].rq_pages[j], len, off);
3687 }
3688 }
3689 cifs_sg_set_buf(sg, sig, SMB2_SIGNATURE_SIZE);
3690
3691 return p;
3692}
3693
3694static int
3695smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3696{
3697 struct cifs_ses *ses;
3698 u8 *ses_enc_key;
3699
3700 spin_lock(&cifs_tcp_ses_lock);
3701 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3702 if (ses->Suid != ses_id)
3703 continue;
3704 ses_enc_key = enc ? ses->smb3encryptionkey :
3705 ses->smb3decryptionkey;
3706 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3707 spin_unlock(&cifs_tcp_ses_lock);
3708 return 0;
3709 }
3710 spin_unlock(&cifs_tcp_ses_lock);
3711
3712 return -EAGAIN;
3713}
3714/*
3715 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3716 * iov[0] - transform header (associate data),
3717 * iov[1-N] - SMB2 header and pages - data to encrypt.
3718 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
3719 * untouched.
3720 */
3721static int
3722crypt_message(struct TCP_Server_Info *server, int num_rqst,
3723 struct smb_rqst *rqst, int enc)
3724{
3725 struct smb2_transform_hdr *tr_hdr =
3726 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
3727 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
3728 int rc = 0;
3729 struct scatterlist *sg;
3730 u8 sign[SMB2_SIGNATURE_SIZE] = {};
3731 u8 key[SMB3_SIGN_KEY_SIZE];
3732 struct aead_request *req;
3733 u8 *iv;
3734 DECLARE_CRYPTO_WAIT(wait);
3735 struct crypto_aead *tfm;
3736 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3737 void *creq;
3738
3739 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3740 if (rc) {
3741 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3742 enc ? "en" : "de");
3743 return rc;
3744 }
3745
3746 rc = smb3_crypto_aead_allocate(server);
3747 if (rc) {
3748 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3749 return rc;
3750 }
3751
3752 tfm = enc ? server->secmech.ccmaesencrypt :
3753 server->secmech.ccmaesdecrypt;
3754 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
3755 if (rc) {
3756 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3757 return rc;
3758 }
3759
3760 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3761 if (rc) {
3762 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3763 return rc;
3764 }
3765
3766 creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg);
3767 if (unlikely(!creq))
3768 return -ENOMEM;
3769
3770 if (!enc) {
3771 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3772 crypt_len += SMB2_SIGNATURE_SIZE;
3773 }
3774
3775 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3776 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3777 else {
3778 iv[0] = 3;
3779 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3780 }
3781
3782 aead_request_set_tfm(req, tfm);
3783 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3784 aead_request_set_ad(req, assoc_data_len);
3785
3786 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
3787 crypto_req_done, &wait);
3788
3789 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3790 : crypto_aead_decrypt(req), &wait);
3791
3792 if (!rc && enc)
3793 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3794
3795 kfree(creq);
3796 return rc;
3797}
3798
3799void
3800smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
3801{
3802 int i, j;
3803
3804 for (i = 0; i < num_rqst; i++) {
3805 if (rqst[i].rq_pages) {
3806 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3807 put_page(rqst[i].rq_pages[j]);
3808 kfree(rqst[i].rq_pages);
3809 }
3810 }
3811}
3812
3813/*
3814 * This function will initialize new_rq and encrypt the content.
3815 * The first entry, new_rq[0], only contains a single iov which contains
3816 * a smb2_transform_hdr and is pre-allocated by the caller.
3817 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3818 *
3819 * The end result is an array of smb_rqst structures where the first structure
3820 * only contains a single iov for the transform header which we then can pass
3821 * to crypt_message().
3822 *
3823 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3824 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3825 */
3826static int
3827smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3828 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
3829{
3830 struct page **pages;
3831 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3832 unsigned int npages;
3833 unsigned int orig_len = 0;
3834 int i, j;
3835 int rc = -ENOMEM;
3836
3837 for (i = 1; i < num_rqst; i++) {
3838 npages = old_rq[i - 1].rq_npages;
3839 pages = kmalloc_array(npages, sizeof(struct page *),
3840 GFP_KERNEL);
3841 if (!pages)
3842 goto err_free;
3843
3844 new_rq[i].rq_pages = pages;
3845 new_rq[i].rq_npages = npages;
3846 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3847 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3848 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3849 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3850 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3851
3852 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3853
3854 for (j = 0; j < npages; j++) {
3855 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3856 if (!pages[j])
3857 goto err_free;
3858 }
3859
3860 /* copy pages form the old */
3861 for (j = 0; j < npages; j++) {
3862 char *dst, *src;
3863 unsigned int offset, len;
3864
3865 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3866
3867 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3868 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3869
3870 memcpy(dst, src, len);
3871 kunmap(new_rq[i].rq_pages[j]);
3872 kunmap(old_rq[i - 1].rq_pages[j]);
3873 }
3874 }
3875
3876 /* fill the 1st iov with a transform header */
3877 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
3878
3879 rc = crypt_message(server, num_rqst, new_rq, 1);
3880 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
3881 if (rc)
3882 goto err_free;
3883
3884 return rc;
3885
3886err_free:
3887 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3888 return rc;
3889}
3890
3891static int
3892smb3_is_transform_hdr(void *buf)
3893{
3894 struct smb2_transform_hdr *trhdr = buf;
3895
3896 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3897}
3898
3899static int
3900decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3901 unsigned int buf_data_size, struct page **pages,
3902 unsigned int npages, unsigned int page_data_size,
3903 bool is_offloaded)
3904{
3905 struct kvec iov[2];
3906 struct smb_rqst rqst = {NULL};
3907 int rc;
3908
3909 iov[0].iov_base = buf;
3910 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3911 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3912 iov[1].iov_len = buf_data_size;
3913
3914 rqst.rq_iov = iov;
3915 rqst.rq_nvec = 2;
3916 rqst.rq_pages = pages;
3917 rqst.rq_npages = npages;
3918 rqst.rq_pagesz = PAGE_SIZE;
3919 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3920
3921 rc = crypt_message(server, 1, &rqst, 0);
3922 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
3923
3924 if (rc)
3925 return rc;
3926
3927 memmove(buf, iov[1].iov_base, buf_data_size);
3928
3929 if (!is_offloaded)
3930 server->total_read = buf_data_size + page_data_size;
3931
3932 return rc;
3933}
3934
3935static int
3936read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3937 unsigned int npages, unsigned int len)
3938{
3939 int i;
3940 int length;
3941
3942 for (i = 0; i < npages; i++) {
3943 struct page *page = pages[i];
3944 size_t n;
3945
3946 n = len;
3947 if (len >= PAGE_SIZE) {
3948 /* enough data to fill the page */
3949 n = PAGE_SIZE;
3950 len -= n;
3951 } else {
3952 zero_user(page, len, PAGE_SIZE - len);
3953 len = 0;
3954 }
3955 length = cifs_read_page_from_socket(server, page, 0, n);
3956 if (length < 0)
3957 return length;
3958 server->total_read += length;
3959 }
3960
3961 return 0;
3962}
3963
3964static int
3965init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3966 unsigned int cur_off, struct bio_vec **page_vec)
3967{
3968 struct bio_vec *bvec;
3969 int i;
3970
3971 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3972 if (!bvec)
3973 return -ENOMEM;
3974
3975 for (i = 0; i < npages; i++) {
3976 bvec[i].bv_page = pages[i];
3977 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3978 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3979 data_size -= bvec[i].bv_len;
3980 }
3981
3982 if (data_size != 0) {
3983 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3984 kfree(bvec);
3985 return -EIO;
3986 }
3987
3988 *page_vec = bvec;
3989 return 0;
3990}
3991
3992static int
3993handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3994 char *buf, unsigned int buf_len, struct page **pages,
3995 unsigned int npages, unsigned int page_data_size,
3996 bool is_offloaded)
3997{
3998 unsigned int data_offset;
3999 unsigned int data_len;
4000 unsigned int cur_off;
4001 unsigned int cur_page_idx;
4002 unsigned int pad_len;
4003 struct cifs_readdata *rdata = mid->callback_data;
4004 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4005 struct bio_vec *bvec = NULL;
4006 struct iov_iter iter;
4007 struct kvec iov;
4008 int length;
4009 bool use_rdma_mr = false;
4010
4011 if (shdr->Command != SMB2_READ) {
4012 cifs_server_dbg(VFS, "only big read responses are supported\n");
4013 return -ENOTSUPP;
4014 }
4015
4016 if (server->ops->is_session_expired &&
4017 server->ops->is_session_expired(buf)) {
4018 if (!is_offloaded)
4019 cifs_reconnect(server);
4020 wake_up(&server->response_q);
4021 return -1;
4022 }
4023
4024 if (server->ops->is_status_pending &&
4025 server->ops->is_status_pending(buf, server))
4026 return -1;
4027
4028 /* set up first two iov to get credits */
4029 rdata->iov[0].iov_base = buf;
4030 rdata->iov[0].iov_len = 0;
4031 rdata->iov[1].iov_base = buf;
4032 rdata->iov[1].iov_len =
4033 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4034 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4035 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4036 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4037 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4038
4039 rdata->result = server->ops->map_error(buf, true);
4040 if (rdata->result != 0) {
4041 cifs_dbg(FYI, "%s: server returned error %d\n",
4042 __func__, rdata->result);
4043 /* normal error on read response */
4044 if (is_offloaded)
4045 mid->mid_state = MID_RESPONSE_RECEIVED;
4046 else
4047 dequeue_mid(mid, false);
4048 return 0;
4049 }
4050
4051 data_offset = server->ops->read_data_offset(buf);
4052#ifdef CONFIG_CIFS_SMB_DIRECT
4053 use_rdma_mr = rdata->mr;
4054#endif
4055 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4056
4057 if (data_offset < server->vals->read_rsp_size) {
4058 /*
4059 * win2k8 sometimes sends an offset of 0 when the read
4060 * is beyond the EOF. Treat it as if the data starts just after
4061 * the header.
4062 */
4063 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4064 __func__, data_offset);
4065 data_offset = server->vals->read_rsp_size;
4066 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4067 /* data_offset is beyond the end of smallbuf */
4068 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4069 __func__, data_offset);
4070 rdata->result = -EIO;
4071 if (is_offloaded)
4072 mid->mid_state = MID_RESPONSE_MALFORMED;
4073 else
4074 dequeue_mid(mid, rdata->result);
4075 return 0;
4076 }
4077
4078 pad_len = data_offset - server->vals->read_rsp_size;
4079
4080 if (buf_len <= data_offset) {
4081 /* read response payload is in pages */
4082 cur_page_idx = pad_len / PAGE_SIZE;
4083 cur_off = pad_len % PAGE_SIZE;
4084
4085 if (cur_page_idx != 0) {
4086 /* data offset is beyond the 1st page of response */
4087 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4088 __func__, data_offset);
4089 rdata->result = -EIO;
4090 if (is_offloaded)
4091 mid->mid_state = MID_RESPONSE_MALFORMED;
4092 else
4093 dequeue_mid(mid, rdata->result);
4094 return 0;
4095 }
4096
4097 if (data_len > page_data_size - pad_len) {
4098 /* data_len is corrupt -- discard frame */
4099 rdata->result = -EIO;
4100 if (is_offloaded)
4101 mid->mid_state = MID_RESPONSE_MALFORMED;
4102 else
4103 dequeue_mid(mid, rdata->result);
4104 return 0;
4105 }
4106
4107 rdata->result = init_read_bvec(pages, npages, page_data_size,
4108 cur_off, &bvec);
4109 if (rdata->result != 0) {
4110 if (is_offloaded)
4111 mid->mid_state = MID_RESPONSE_MALFORMED;
4112 else
4113 dequeue_mid(mid, rdata->result);
4114 return 0;
4115 }
4116
4117 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4118 } else if (buf_len >= data_offset + data_len) {
4119 /* read response payload is in buf */
4120 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4121 iov.iov_base = buf + data_offset;
4122 iov.iov_len = data_len;
4123 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4124 } else {
4125 /* read response payload cannot be in both buf and pages */
4126 WARN_ONCE(1, "buf can not contain only a part of read data");
4127 rdata->result = -EIO;
4128 if (is_offloaded)
4129 mid->mid_state = MID_RESPONSE_MALFORMED;
4130 else
4131 dequeue_mid(mid, rdata->result);
4132 return 0;
4133 }
4134
4135 length = rdata->copy_into_pages(server, rdata, &iter);
4136
4137 kfree(bvec);
4138
4139 if (length < 0)
4140 return length;
4141
4142 if (is_offloaded)
4143 mid->mid_state = MID_RESPONSE_RECEIVED;
4144 else
4145 dequeue_mid(mid, false);
4146 return length;
4147}
4148
4149struct smb2_decrypt_work {
4150 struct work_struct decrypt;
4151 struct TCP_Server_Info *server;
4152 struct page **ppages;
4153 char *buf;
4154 unsigned int npages;
4155 unsigned int len;
4156};
4157
4158
4159static void smb2_decrypt_offload(struct work_struct *work)
4160{
4161 struct smb2_decrypt_work *dw = container_of(work,
4162 struct smb2_decrypt_work, decrypt);
4163 int i, rc;
4164 struct mid_q_entry *mid;
4165
4166 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4167 dw->ppages, dw->npages, dw->len, true);
4168 if (rc) {
4169 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4170 goto free_pages;
4171 }
4172
4173 dw->server->lstrp = jiffies;
4174 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4175 if (mid == NULL)
4176 cifs_dbg(FYI, "mid not found\n");
4177 else {
4178 mid->decrypted = true;
4179 rc = handle_read_data(dw->server, mid, dw->buf,
4180 dw->server->vals->read_rsp_size,
4181 dw->ppages, dw->npages, dw->len,
4182 true);
4183 if (rc >= 0) {
4184#ifdef CONFIG_CIFS_STATS2
4185 mid->when_received = jiffies;
4186#endif
4187 mid->callback(mid);
4188 } else {
4189 spin_lock(&GlobalMid_Lock);
4190 if (dw->server->tcpStatus == CifsNeedReconnect) {
4191 mid->mid_state = MID_RETRY_NEEDED;
4192 spin_unlock(&GlobalMid_Lock);
4193 mid->callback(mid);
4194 } else {
4195 mid->mid_state = MID_REQUEST_SUBMITTED;
4196 mid->mid_flags &= ~(MID_DELETED);
4197 list_add_tail(&mid->qhead,
4198 &dw->server->pending_mid_q);
4199 spin_unlock(&GlobalMid_Lock);
4200 }
4201 }
4202 cifs_mid_q_entry_release(mid);
4203 }
4204
4205free_pages:
4206 for (i = dw->npages-1; i >= 0; i--)
4207 put_page(dw->ppages[i]);
4208
4209 kfree(dw->ppages);
4210 cifs_small_buf_release(dw->buf);
4211 kfree(dw);
4212}
4213
4214
4215static int
4216receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4217 int *num_mids)
4218{
4219 char *buf = server->smallbuf;
4220 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4221 unsigned int npages;
4222 struct page **pages;
4223 unsigned int len;
4224 unsigned int buflen = server->pdu_size;
4225 int rc;
4226 int i = 0;
4227 struct smb2_decrypt_work *dw;
4228
4229 *num_mids = 1;
4230 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4231 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4232
4233 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4234 if (rc < 0)
4235 return rc;
4236 server->total_read += rc;
4237
4238 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4239 server->vals->read_rsp_size;
4240 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4241
4242 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4243 if (!pages) {
4244 rc = -ENOMEM;
4245 goto discard_data;
4246 }
4247
4248 for (; i < npages; i++) {
4249 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4250 if (!pages[i]) {
4251 rc = -ENOMEM;
4252 goto discard_data;
4253 }
4254 }
4255
4256 /* read read data into pages */
4257 rc = read_data_into_pages(server, pages, npages, len);
4258 if (rc)
4259 goto free_pages;
4260
4261 rc = cifs_discard_remaining_data(server);
4262 if (rc)
4263 goto free_pages;
4264
4265 /*
4266 * For large reads, offload to different thread for better performance,
4267 * use more cores decrypting which can be expensive
4268 */
4269
4270 if ((server->min_offload) && (server->in_flight > 1) &&
4271 (server->pdu_size >= server->min_offload)) {
4272 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4273 if (dw == NULL)
4274 goto non_offloaded_decrypt;
4275
4276 dw->buf = server->smallbuf;
4277 server->smallbuf = (char *)cifs_small_buf_get();
4278
4279 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4280
4281 dw->npages = npages;
4282 dw->server = server;
4283 dw->ppages = pages;
4284 dw->len = len;
4285 queue_work(decrypt_wq, &dw->decrypt);
4286 *num_mids = 0; /* worker thread takes care of finding mid */
4287 return -1;
4288 }
4289
4290non_offloaded_decrypt:
4291 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4292 pages, npages, len, false);
4293 if (rc)
4294 goto free_pages;
4295
4296 *mid = smb2_find_mid(server, buf);
4297 if (*mid == NULL)
4298 cifs_dbg(FYI, "mid not found\n");
4299 else {
4300 cifs_dbg(FYI, "mid found\n");
4301 (*mid)->decrypted = true;
4302 rc = handle_read_data(server, *mid, buf,
4303 server->vals->read_rsp_size,
4304 pages, npages, len, false);
4305 }
4306
4307free_pages:
4308 for (i = i - 1; i >= 0; i--)
4309 put_page(pages[i]);
4310 kfree(pages);
4311 return rc;
4312discard_data:
4313 cifs_discard_remaining_data(server);
4314 goto free_pages;
4315}
4316
4317static int
4318receive_encrypted_standard(struct TCP_Server_Info *server,
4319 struct mid_q_entry **mids, char **bufs,
4320 int *num_mids)
4321{
4322 int ret, length;
4323 char *buf = server->smallbuf;
4324 struct smb2_sync_hdr *shdr;
4325 unsigned int pdu_length = server->pdu_size;
4326 unsigned int buf_size;
4327 struct mid_q_entry *mid_entry;
4328 int next_is_large;
4329 char *next_buffer = NULL;
4330
4331 *num_mids = 0;
4332
4333 /* switch to large buffer if too big for a small one */
4334 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4335 server->large_buf = true;
4336 memcpy(server->bigbuf, buf, server->total_read);
4337 buf = server->bigbuf;
4338 }
4339
4340 /* now read the rest */
4341 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4342 pdu_length - HEADER_SIZE(server) + 1);
4343 if (length < 0)
4344 return length;
4345 server->total_read += length;
4346
4347 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4348 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
4349 if (length)
4350 return length;
4351
4352 next_is_large = server->large_buf;
4353one_more:
4354 shdr = (struct smb2_sync_hdr *)buf;
4355 if (shdr->NextCommand) {
4356 if (next_is_large)
4357 next_buffer = (char *)cifs_buf_get();
4358 else
4359 next_buffer = (char *)cifs_small_buf_get();
4360 memcpy(next_buffer,
4361 buf + le32_to_cpu(shdr->NextCommand),
4362 pdu_length - le32_to_cpu(shdr->NextCommand));
4363 }
4364
4365 mid_entry = smb2_find_mid(server, buf);
4366 if (mid_entry == NULL)
4367 cifs_dbg(FYI, "mid not found\n");
4368 else {
4369 cifs_dbg(FYI, "mid found\n");
4370 mid_entry->decrypted = true;
4371 mid_entry->resp_buf_size = server->pdu_size;
4372 }
4373
4374 if (*num_mids >= MAX_COMPOUND) {
4375 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4376 return -1;
4377 }
4378 bufs[*num_mids] = buf;
4379 mids[(*num_mids)++] = mid_entry;
4380
4381 if (mid_entry && mid_entry->handle)
4382 ret = mid_entry->handle(server, mid_entry);
4383 else
4384 ret = cifs_handle_standard(server, mid_entry);
4385
4386 if (ret == 0 && shdr->NextCommand) {
4387 pdu_length -= le32_to_cpu(shdr->NextCommand);
4388 server->large_buf = next_is_large;
4389 if (next_is_large)
4390 server->bigbuf = buf = next_buffer;
4391 else
4392 server->smallbuf = buf = next_buffer;
4393 goto one_more;
4394 } else if (ret != 0) {
4395 /*
4396 * ret != 0 here means that we didn't get to handle_mid() thus
4397 * server->smallbuf and server->bigbuf are still valid. We need
4398 * to free next_buffer because it is not going to be used
4399 * anywhere.
4400 */
4401 if (next_is_large)
4402 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4403 else
4404 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4405 }
4406
4407 return ret;
4408}
4409
4410static int
4411smb3_receive_transform(struct TCP_Server_Info *server,
4412 struct mid_q_entry **mids, char **bufs, int *num_mids)
4413{
4414 char *buf = server->smallbuf;
4415 unsigned int pdu_length = server->pdu_size;
4416 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4417 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4418
4419 if (pdu_length < sizeof(struct smb2_transform_hdr) +
4420 sizeof(struct smb2_sync_hdr)) {
4421 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4422 pdu_length);
4423 cifs_reconnect(server);
4424 wake_up(&server->response_q);
4425 return -ECONNABORTED;
4426 }
4427
4428 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4429 cifs_server_dbg(VFS, "Transform message is broken\n");
4430 cifs_reconnect(server);
4431 wake_up(&server->response_q);
4432 return -ECONNABORTED;
4433 }
4434
4435 /* TODO: add support for compounds containing READ. */
4436 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4437 return receive_encrypted_read(server, &mids[0], num_mids);
4438 }
4439
4440 return receive_encrypted_standard(server, mids, bufs, num_mids);
4441}
4442
4443int
4444smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4445{
4446 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4447
4448 return handle_read_data(server, mid, buf, server->pdu_size,
4449 NULL, 0, 0, false);
4450}
4451
4452static int
4453smb2_next_header(char *buf)
4454{
4455 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4456 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4457
4458 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4459 return sizeof(struct smb2_transform_hdr) +
4460 le32_to_cpu(t_hdr->OriginalMessageSize);
4461
4462 return le32_to_cpu(hdr->NextCommand);
4463}
4464
4465static int
4466smb2_make_node(unsigned int xid, struct inode *inode,
4467 struct dentry *dentry, struct cifs_tcon *tcon,
4468 char *full_path, umode_t mode, dev_t dev)
4469{
4470 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4471 int rc = -EPERM;
4472 FILE_ALL_INFO *buf = NULL;
4473 struct cifs_io_parms io_parms;
4474 __u32 oplock = 0;
4475 struct cifs_fid fid;
4476 struct cifs_open_parms oparms;
4477 unsigned int bytes_written;
4478 struct win_dev *pdev;
4479 struct kvec iov[2];
4480
4481 /*
4482 * Check if mounted with mount parm 'sfu' mount parm.
4483 * SFU emulation should work with all servers, but only
4484 * supports block and char device (no socket & fifo),
4485 * and was used by default in earlier versions of Windows
4486 */
4487 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4488 goto out;
4489
4490 /*
4491 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4492 * their current NFS server) uses this approach to expose special files
4493 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4494 */
4495
4496 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4497 goto out;
4498
4499 cifs_dbg(FYI, "sfu compat create special file\n");
4500
4501 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4502 if (buf == NULL) {
4503 rc = -ENOMEM;
4504 goto out;
4505 }
4506
4507 oparms.tcon = tcon;
4508 oparms.cifs_sb = cifs_sb;
4509 oparms.desired_access = GENERIC_WRITE;
4510 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4511 CREATE_OPTION_SPECIAL);
4512 oparms.disposition = FILE_CREATE;
4513 oparms.path = full_path;
4514 oparms.fid = &fid;
4515 oparms.reconnect = false;
4516
4517 if (tcon->ses->server->oplocks)
4518 oplock = REQ_OPLOCK;
4519 else
4520 oplock = 0;
4521 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4522 if (rc)
4523 goto out;
4524
4525 /*
4526 * BB Do not bother to decode buf since no local inode yet to put
4527 * timestamps in, but we can reuse it safely.
4528 */
4529
4530 pdev = (struct win_dev *)buf;
4531 io_parms.pid = current->tgid;
4532 io_parms.tcon = tcon;
4533 io_parms.offset = 0;
4534 io_parms.length = sizeof(struct win_dev);
4535 iov[1].iov_base = buf;
4536 iov[1].iov_len = sizeof(struct win_dev);
4537 if (S_ISCHR(mode)) {
4538 memcpy(pdev->type, "IntxCHR", 8);
4539 pdev->major = cpu_to_le64(MAJOR(dev));
4540 pdev->minor = cpu_to_le64(MINOR(dev));
4541 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4542 &bytes_written, iov, 1);
4543 } else if (S_ISBLK(mode)) {
4544 memcpy(pdev->type, "IntxBLK", 8);
4545 pdev->major = cpu_to_le64(MAJOR(dev));
4546 pdev->minor = cpu_to_le64(MINOR(dev));
4547 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4548 &bytes_written, iov, 1);
4549 }
4550 tcon->ses->server->ops->close(xid, tcon, &fid);
4551 d_drop(dentry);
4552
4553 /* FIXME: add code here to set EAs */
4554out:
4555 kfree(buf);
4556 return rc;
4557}
4558
4559#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4560struct smb_version_operations smb20_operations = {
4561 .compare_fids = smb2_compare_fids,
4562 .setup_request = smb2_setup_request,
4563 .setup_async_request = smb2_setup_async_request,
4564 .check_receive = smb2_check_receive,
4565 .add_credits = smb2_add_credits,
4566 .set_credits = smb2_set_credits,
4567 .get_credits_field = smb2_get_credits_field,
4568 .get_credits = smb2_get_credits,
4569 .wait_mtu_credits = cifs_wait_mtu_credits,
4570 .get_next_mid = smb2_get_next_mid,
4571 .revert_current_mid = smb2_revert_current_mid,
4572 .read_data_offset = smb2_read_data_offset,
4573 .read_data_length = smb2_read_data_length,
4574 .map_error = map_smb2_to_linux_error,
4575 .find_mid = smb2_find_mid,
4576 .check_message = smb2_check_message,
4577 .dump_detail = smb2_dump_detail,
4578 .clear_stats = smb2_clear_stats,
4579 .print_stats = smb2_print_stats,
4580 .is_oplock_break = smb2_is_valid_oplock_break,
4581 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4582 .downgrade_oplock = smb2_downgrade_oplock,
4583 .need_neg = smb2_need_neg,
4584 .negotiate = smb2_negotiate,
4585 .negotiate_wsize = smb2_negotiate_wsize,
4586 .negotiate_rsize = smb2_negotiate_rsize,
4587 .sess_setup = SMB2_sess_setup,
4588 .logoff = SMB2_logoff,
4589 .tree_connect = SMB2_tcon,
4590 .tree_disconnect = SMB2_tdis,
4591 .qfs_tcon = smb2_qfs_tcon,
4592 .is_path_accessible = smb2_is_path_accessible,
4593 .can_echo = smb2_can_echo,
4594 .echo = SMB2_echo,
4595 .query_path_info = smb2_query_path_info,
4596 .get_srv_inum = smb2_get_srv_inum,
4597 .query_file_info = smb2_query_file_info,
4598 .set_path_size = smb2_set_path_size,
4599 .set_file_size = smb2_set_file_size,
4600 .set_file_info = smb2_set_file_info,
4601 .set_compression = smb2_set_compression,
4602 .mkdir = smb2_mkdir,
4603 .mkdir_setinfo = smb2_mkdir_setinfo,
4604 .rmdir = smb2_rmdir,
4605 .unlink = smb2_unlink,
4606 .rename = smb2_rename_path,
4607 .create_hardlink = smb2_create_hardlink,
4608 .query_symlink = smb2_query_symlink,
4609 .query_mf_symlink = smb3_query_mf_symlink,
4610 .create_mf_symlink = smb3_create_mf_symlink,
4611 .open = smb2_open_file,
4612 .set_fid = smb2_set_fid,
4613 .close = smb2_close_file,
4614 .flush = smb2_flush_file,
4615 .async_readv = smb2_async_readv,
4616 .async_writev = smb2_async_writev,
4617 .sync_read = smb2_sync_read,
4618 .sync_write = smb2_sync_write,
4619 .query_dir_first = smb2_query_dir_first,
4620 .query_dir_next = smb2_query_dir_next,
4621 .close_dir = smb2_close_dir,
4622 .calc_smb_size = smb2_calc_size,
4623 .is_status_pending = smb2_is_status_pending,
4624 .is_session_expired = smb2_is_session_expired,
4625 .oplock_response = smb2_oplock_response,
4626 .queryfs = smb2_queryfs,
4627 .mand_lock = smb2_mand_lock,
4628 .mand_unlock_range = smb2_unlock_range,
4629 .push_mand_locks = smb2_push_mandatory_locks,
4630 .get_lease_key = smb2_get_lease_key,
4631 .set_lease_key = smb2_set_lease_key,
4632 .new_lease_key = smb2_new_lease_key,
4633 .calc_signature = smb2_calc_signature,
4634 .is_read_op = smb2_is_read_op,
4635 .set_oplock_level = smb2_set_oplock_level,
4636 .create_lease_buf = smb2_create_lease_buf,
4637 .parse_lease_buf = smb2_parse_lease_buf,
4638 .copychunk_range = smb2_copychunk_range,
4639 .wp_retry_size = smb2_wp_retry_size,
4640 .dir_needs_close = smb2_dir_needs_close,
4641 .get_dfs_refer = smb2_get_dfs_refer,
4642 .select_sectype = smb2_select_sectype,
4643#ifdef CONFIG_CIFS_XATTR
4644 .query_all_EAs = smb2_query_eas,
4645 .set_EA = smb2_set_ea,
4646#endif /* CIFS_XATTR */
4647 .get_acl = get_smb2_acl,
4648 .get_acl_by_fid = get_smb2_acl_by_fid,
4649 .set_acl = set_smb2_acl,
4650 .next_header = smb2_next_header,
4651 .ioctl_query_info = smb2_ioctl_query_info,
4652 .make_node = smb2_make_node,
4653 .fiemap = smb3_fiemap,
4654 .llseek = smb3_llseek,
4655};
4656#endif /* CIFS_ALLOW_INSECURE_LEGACY */
4657
4658struct smb_version_operations smb21_operations = {
4659 .compare_fids = smb2_compare_fids,
4660 .setup_request = smb2_setup_request,
4661 .setup_async_request = smb2_setup_async_request,
4662 .check_receive = smb2_check_receive,
4663 .add_credits = smb2_add_credits,
4664 .set_credits = smb2_set_credits,
4665 .get_credits_field = smb2_get_credits_field,
4666 .get_credits = smb2_get_credits,
4667 .wait_mtu_credits = smb2_wait_mtu_credits,
4668 .adjust_credits = smb2_adjust_credits,
4669 .get_next_mid = smb2_get_next_mid,
4670 .revert_current_mid = smb2_revert_current_mid,
4671 .read_data_offset = smb2_read_data_offset,
4672 .read_data_length = smb2_read_data_length,
4673 .map_error = map_smb2_to_linux_error,
4674 .find_mid = smb2_find_mid,
4675 .check_message = smb2_check_message,
4676 .dump_detail = smb2_dump_detail,
4677 .clear_stats = smb2_clear_stats,
4678 .print_stats = smb2_print_stats,
4679 .is_oplock_break = smb2_is_valid_oplock_break,
4680 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4681 .downgrade_oplock = smb2_downgrade_oplock,
4682 .need_neg = smb2_need_neg,
4683 .negotiate = smb2_negotiate,
4684 .negotiate_wsize = smb2_negotiate_wsize,
4685 .negotiate_rsize = smb2_negotiate_rsize,
4686 .sess_setup = SMB2_sess_setup,
4687 .logoff = SMB2_logoff,
4688 .tree_connect = SMB2_tcon,
4689 .tree_disconnect = SMB2_tdis,
4690 .qfs_tcon = smb2_qfs_tcon,
4691 .is_path_accessible = smb2_is_path_accessible,
4692 .can_echo = smb2_can_echo,
4693 .echo = SMB2_echo,
4694 .query_path_info = smb2_query_path_info,
4695 .get_srv_inum = smb2_get_srv_inum,
4696 .query_file_info = smb2_query_file_info,
4697 .set_path_size = smb2_set_path_size,
4698 .set_file_size = smb2_set_file_size,
4699 .set_file_info = smb2_set_file_info,
4700 .set_compression = smb2_set_compression,
4701 .mkdir = smb2_mkdir,
4702 .mkdir_setinfo = smb2_mkdir_setinfo,
4703 .rmdir = smb2_rmdir,
4704 .unlink = smb2_unlink,
4705 .rename = smb2_rename_path,
4706 .create_hardlink = smb2_create_hardlink,
4707 .query_symlink = smb2_query_symlink,
4708 .query_mf_symlink = smb3_query_mf_symlink,
4709 .create_mf_symlink = smb3_create_mf_symlink,
4710 .open = smb2_open_file,
4711 .set_fid = smb2_set_fid,
4712 .close = smb2_close_file,
4713 .flush = smb2_flush_file,
4714 .async_readv = smb2_async_readv,
4715 .async_writev = smb2_async_writev,
4716 .sync_read = smb2_sync_read,
4717 .sync_write = smb2_sync_write,
4718 .query_dir_first = smb2_query_dir_first,
4719 .query_dir_next = smb2_query_dir_next,
4720 .close_dir = smb2_close_dir,
4721 .calc_smb_size = smb2_calc_size,
4722 .is_status_pending = smb2_is_status_pending,
4723 .is_session_expired = smb2_is_session_expired,
4724 .oplock_response = smb2_oplock_response,
4725 .queryfs = smb2_queryfs,
4726 .mand_lock = smb2_mand_lock,
4727 .mand_unlock_range = smb2_unlock_range,
4728 .push_mand_locks = smb2_push_mandatory_locks,
4729 .get_lease_key = smb2_get_lease_key,
4730 .set_lease_key = smb2_set_lease_key,
4731 .new_lease_key = smb2_new_lease_key,
4732 .calc_signature = smb2_calc_signature,
4733 .is_read_op = smb21_is_read_op,
4734 .set_oplock_level = smb21_set_oplock_level,
4735 .create_lease_buf = smb2_create_lease_buf,
4736 .parse_lease_buf = smb2_parse_lease_buf,
4737 .copychunk_range = smb2_copychunk_range,
4738 .wp_retry_size = smb2_wp_retry_size,
4739 .dir_needs_close = smb2_dir_needs_close,
4740 .enum_snapshots = smb3_enum_snapshots,
4741 .get_dfs_refer = smb2_get_dfs_refer,
4742 .select_sectype = smb2_select_sectype,
4743#ifdef CONFIG_CIFS_XATTR
4744 .query_all_EAs = smb2_query_eas,
4745 .set_EA = smb2_set_ea,
4746#endif /* CIFS_XATTR */
4747 .get_acl = get_smb2_acl,
4748 .get_acl_by_fid = get_smb2_acl_by_fid,
4749 .set_acl = set_smb2_acl,
4750 .next_header = smb2_next_header,
4751 .ioctl_query_info = smb2_ioctl_query_info,
4752 .make_node = smb2_make_node,
4753 .fiemap = smb3_fiemap,
4754 .llseek = smb3_llseek,
4755};
4756
4757struct smb_version_operations smb30_operations = {
4758 .compare_fids = smb2_compare_fids,
4759 .setup_request = smb2_setup_request,
4760 .setup_async_request = smb2_setup_async_request,
4761 .check_receive = smb2_check_receive,
4762 .add_credits = smb2_add_credits,
4763 .set_credits = smb2_set_credits,
4764 .get_credits_field = smb2_get_credits_field,
4765 .get_credits = smb2_get_credits,
4766 .wait_mtu_credits = smb2_wait_mtu_credits,
4767 .adjust_credits = smb2_adjust_credits,
4768 .get_next_mid = smb2_get_next_mid,
4769 .revert_current_mid = smb2_revert_current_mid,
4770 .read_data_offset = smb2_read_data_offset,
4771 .read_data_length = smb2_read_data_length,
4772 .map_error = map_smb2_to_linux_error,
4773 .find_mid = smb2_find_mid,
4774 .check_message = smb2_check_message,
4775 .dump_detail = smb2_dump_detail,
4776 .clear_stats = smb2_clear_stats,
4777 .print_stats = smb2_print_stats,
4778 .dump_share_caps = smb2_dump_share_caps,
4779 .is_oplock_break = smb2_is_valid_oplock_break,
4780 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4781 .downgrade_oplock = smb3_downgrade_oplock,
4782 .need_neg = smb2_need_neg,
4783 .negotiate = smb2_negotiate,
4784 .negotiate_wsize = smb3_negotiate_wsize,
4785 .negotiate_rsize = smb3_negotiate_rsize,
4786 .sess_setup = SMB2_sess_setup,
4787 .logoff = SMB2_logoff,
4788 .tree_connect = SMB2_tcon,
4789 .tree_disconnect = SMB2_tdis,
4790 .qfs_tcon = smb3_qfs_tcon,
4791 .is_path_accessible = smb2_is_path_accessible,
4792 .can_echo = smb2_can_echo,
4793 .echo = SMB2_echo,
4794 .query_path_info = smb2_query_path_info,
4795 .get_srv_inum = smb2_get_srv_inum,
4796 .query_file_info = smb2_query_file_info,
4797 .set_path_size = smb2_set_path_size,
4798 .set_file_size = smb2_set_file_size,
4799 .set_file_info = smb2_set_file_info,
4800 .set_compression = smb2_set_compression,
4801 .mkdir = smb2_mkdir,
4802 .mkdir_setinfo = smb2_mkdir_setinfo,
4803 .rmdir = smb2_rmdir,
4804 .unlink = smb2_unlink,
4805 .rename = smb2_rename_path,
4806 .create_hardlink = smb2_create_hardlink,
4807 .query_symlink = smb2_query_symlink,
4808 .query_mf_symlink = smb3_query_mf_symlink,
4809 .create_mf_symlink = smb3_create_mf_symlink,
4810 .open = smb2_open_file,
4811 .set_fid = smb2_set_fid,
4812 .close = smb2_close_file,
4813 .flush = smb2_flush_file,
4814 .async_readv = smb2_async_readv,
4815 .async_writev = smb2_async_writev,
4816 .sync_read = smb2_sync_read,
4817 .sync_write = smb2_sync_write,
4818 .query_dir_first = smb2_query_dir_first,
4819 .query_dir_next = smb2_query_dir_next,
4820 .close_dir = smb2_close_dir,
4821 .calc_smb_size = smb2_calc_size,
4822 .is_status_pending = smb2_is_status_pending,
4823 .is_session_expired = smb2_is_session_expired,
4824 .oplock_response = smb2_oplock_response,
4825 .queryfs = smb2_queryfs,
4826 .mand_lock = smb2_mand_lock,
4827 .mand_unlock_range = smb2_unlock_range,
4828 .push_mand_locks = smb2_push_mandatory_locks,
4829 .get_lease_key = smb2_get_lease_key,
4830 .set_lease_key = smb2_set_lease_key,
4831 .new_lease_key = smb2_new_lease_key,
4832 .generate_signingkey = generate_smb30signingkey,
4833 .calc_signature = smb3_calc_signature,
4834 .set_integrity = smb3_set_integrity,
4835 .is_read_op = smb21_is_read_op,
4836 .set_oplock_level = smb3_set_oplock_level,
4837 .create_lease_buf = smb3_create_lease_buf,
4838 .parse_lease_buf = smb3_parse_lease_buf,
4839 .copychunk_range = smb2_copychunk_range,
4840 .duplicate_extents = smb2_duplicate_extents,
4841 .validate_negotiate = smb3_validate_negotiate,
4842 .wp_retry_size = smb2_wp_retry_size,
4843 .dir_needs_close = smb2_dir_needs_close,
4844 .fallocate = smb3_fallocate,
4845 .enum_snapshots = smb3_enum_snapshots,
4846 .init_transform_rq = smb3_init_transform_rq,
4847 .is_transform_hdr = smb3_is_transform_hdr,
4848 .receive_transform = smb3_receive_transform,
4849 .get_dfs_refer = smb2_get_dfs_refer,
4850 .select_sectype = smb2_select_sectype,
4851#ifdef CONFIG_CIFS_XATTR
4852 .query_all_EAs = smb2_query_eas,
4853 .set_EA = smb2_set_ea,
4854#endif /* CIFS_XATTR */
4855 .get_acl = get_smb2_acl,
4856 .get_acl_by_fid = get_smb2_acl_by_fid,
4857 .set_acl = set_smb2_acl,
4858 .next_header = smb2_next_header,
4859 .ioctl_query_info = smb2_ioctl_query_info,
4860 .make_node = smb2_make_node,
4861 .fiemap = smb3_fiemap,
4862 .llseek = smb3_llseek,
4863};
4864
4865struct smb_version_operations smb311_operations = {
4866 .compare_fids = smb2_compare_fids,
4867 .setup_request = smb2_setup_request,
4868 .setup_async_request = smb2_setup_async_request,
4869 .check_receive = smb2_check_receive,
4870 .add_credits = smb2_add_credits,
4871 .set_credits = smb2_set_credits,
4872 .get_credits_field = smb2_get_credits_field,
4873 .get_credits = smb2_get_credits,
4874 .wait_mtu_credits = smb2_wait_mtu_credits,
4875 .adjust_credits = smb2_adjust_credits,
4876 .get_next_mid = smb2_get_next_mid,
4877 .revert_current_mid = smb2_revert_current_mid,
4878 .read_data_offset = smb2_read_data_offset,
4879 .read_data_length = smb2_read_data_length,
4880 .map_error = map_smb2_to_linux_error,
4881 .find_mid = smb2_find_mid,
4882 .check_message = smb2_check_message,
4883 .dump_detail = smb2_dump_detail,
4884 .clear_stats = smb2_clear_stats,
4885 .print_stats = smb2_print_stats,
4886 .dump_share_caps = smb2_dump_share_caps,
4887 .is_oplock_break = smb2_is_valid_oplock_break,
4888 .handle_cancelled_mid = smb2_handle_cancelled_mid,
4889 .downgrade_oplock = smb3_downgrade_oplock,
4890 .need_neg = smb2_need_neg,
4891 .negotiate = smb2_negotiate,
4892 .negotiate_wsize = smb3_negotiate_wsize,
4893 .negotiate_rsize = smb3_negotiate_rsize,
4894 .sess_setup = SMB2_sess_setup,
4895 .logoff = SMB2_logoff,
4896 .tree_connect = SMB2_tcon,
4897 .tree_disconnect = SMB2_tdis,
4898 .qfs_tcon = smb3_qfs_tcon,
4899 .is_path_accessible = smb2_is_path_accessible,
4900 .can_echo = smb2_can_echo,
4901 .echo = SMB2_echo,
4902 .query_path_info = smb2_query_path_info,
4903 .get_srv_inum = smb2_get_srv_inum,
4904 .query_file_info = smb2_query_file_info,
4905 .set_path_size = smb2_set_path_size,
4906 .set_file_size = smb2_set_file_size,
4907 .set_file_info = smb2_set_file_info,
4908 .set_compression = smb2_set_compression,
4909 .mkdir = smb2_mkdir,
4910 .mkdir_setinfo = smb2_mkdir_setinfo,
4911 .posix_mkdir = smb311_posix_mkdir,
4912 .rmdir = smb2_rmdir,
4913 .unlink = smb2_unlink,
4914 .rename = smb2_rename_path,
4915 .create_hardlink = smb2_create_hardlink,
4916 .query_symlink = smb2_query_symlink,
4917 .query_mf_symlink = smb3_query_mf_symlink,
4918 .create_mf_symlink = smb3_create_mf_symlink,
4919 .open = smb2_open_file,
4920 .set_fid = smb2_set_fid,
4921 .close = smb2_close_file,
4922 .flush = smb2_flush_file,
4923 .async_readv = smb2_async_readv,
4924 .async_writev = smb2_async_writev,
4925 .sync_read = smb2_sync_read,
4926 .sync_write = smb2_sync_write,
4927 .query_dir_first = smb2_query_dir_first,
4928 .query_dir_next = smb2_query_dir_next,
4929 .close_dir = smb2_close_dir,
4930 .calc_smb_size = smb2_calc_size,
4931 .is_status_pending = smb2_is_status_pending,
4932 .is_session_expired = smb2_is_session_expired,
4933 .oplock_response = smb2_oplock_response,
4934 .queryfs = smb311_queryfs,
4935 .mand_lock = smb2_mand_lock,
4936 .mand_unlock_range = smb2_unlock_range,
4937 .push_mand_locks = smb2_push_mandatory_locks,
4938 .get_lease_key = smb2_get_lease_key,
4939 .set_lease_key = smb2_set_lease_key,
4940 .new_lease_key = smb2_new_lease_key,
4941 .generate_signingkey = generate_smb311signingkey,
4942 .calc_signature = smb3_calc_signature,
4943 .set_integrity = smb3_set_integrity,
4944 .is_read_op = smb21_is_read_op,
4945 .set_oplock_level = smb3_set_oplock_level,
4946 .create_lease_buf = smb3_create_lease_buf,
4947 .parse_lease_buf = smb3_parse_lease_buf,
4948 .copychunk_range = smb2_copychunk_range,
4949 .duplicate_extents = smb2_duplicate_extents,
4950/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4951 .wp_retry_size = smb2_wp_retry_size,
4952 .dir_needs_close = smb2_dir_needs_close,
4953 .fallocate = smb3_fallocate,
4954 .enum_snapshots = smb3_enum_snapshots,
4955 .init_transform_rq = smb3_init_transform_rq,
4956 .is_transform_hdr = smb3_is_transform_hdr,
4957 .receive_transform = smb3_receive_transform,
4958 .get_dfs_refer = smb2_get_dfs_refer,
4959 .select_sectype = smb2_select_sectype,
4960#ifdef CONFIG_CIFS_XATTR
4961 .query_all_EAs = smb2_query_eas,
4962 .set_EA = smb2_set_ea,
4963#endif /* CIFS_XATTR */
4964 .get_acl = get_smb2_acl,
4965 .get_acl_by_fid = get_smb2_acl_by_fid,
4966 .set_acl = set_smb2_acl,
4967 .next_header = smb2_next_header,
4968 .ioctl_query_info = smb2_ioctl_query_info,
4969 .make_node = smb2_make_node,
4970 .fiemap = smb3_fiemap,
4971 .llseek = smb3_llseek,
4972};
4973
4974#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4975struct smb_version_values smb20_values = {
4976 .version_string = SMB20_VERSION_STRING,
4977 .protocol_id = SMB20_PROT_ID,
4978 .req_capabilities = 0, /* MBZ */
4979 .large_lock_type = 0,
4980 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4981 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4982 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
4983 .header_size = sizeof(struct smb2_sync_hdr),
4984 .header_preamble_size = 0,
4985 .max_header_size = MAX_SMB2_HDR_SIZE,
4986 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4987 .lock_cmd = SMB2_LOCK,
4988 .cap_unix = 0,
4989 .cap_nt_find = SMB2_NT_FIND,
4990 .cap_large_files = SMB2_LARGE_FILES,
4991 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4992 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4993 .create_lease_size = sizeof(struct create_lease),
4994};
4995#endif /* ALLOW_INSECURE_LEGACY */
4996
4997struct smb_version_values smb21_values = {
4998 .version_string = SMB21_VERSION_STRING,
4999 .protocol_id = SMB21_PROT_ID,
5000 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5001 .large_lock_type = 0,
5002 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5003 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5004 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5005 .header_size = sizeof(struct smb2_sync_hdr),
5006 .header_preamble_size = 0,
5007 .max_header_size = MAX_SMB2_HDR_SIZE,
5008 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5009 .lock_cmd = SMB2_LOCK,
5010 .cap_unix = 0,
5011 .cap_nt_find = SMB2_NT_FIND,
5012 .cap_large_files = SMB2_LARGE_FILES,
5013 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5014 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5015 .create_lease_size = sizeof(struct create_lease),
5016};
5017
5018struct smb_version_values smb3any_values = {
5019 .version_string = SMB3ANY_VERSION_STRING,
5020 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5021 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5022 .large_lock_type = 0,
5023 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5024 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5025 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5026 .header_size = sizeof(struct smb2_sync_hdr),
5027 .header_preamble_size = 0,
5028 .max_header_size = MAX_SMB2_HDR_SIZE,
5029 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5030 .lock_cmd = SMB2_LOCK,
5031 .cap_unix = 0,
5032 .cap_nt_find = SMB2_NT_FIND,
5033 .cap_large_files = SMB2_LARGE_FILES,
5034 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5035 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5036 .create_lease_size = sizeof(struct create_lease_v2),
5037};
5038
5039struct smb_version_values smbdefault_values = {
5040 .version_string = SMBDEFAULT_VERSION_STRING,
5041 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5042 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5043 .large_lock_type = 0,
5044 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5045 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5046 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5047 .header_size = sizeof(struct smb2_sync_hdr),
5048 .header_preamble_size = 0,
5049 .max_header_size = MAX_SMB2_HDR_SIZE,
5050 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5051 .lock_cmd = SMB2_LOCK,
5052 .cap_unix = 0,
5053 .cap_nt_find = SMB2_NT_FIND,
5054 .cap_large_files = SMB2_LARGE_FILES,
5055 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5056 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5057 .create_lease_size = sizeof(struct create_lease_v2),
5058};
5059
5060struct smb_version_values smb30_values = {
5061 .version_string = SMB30_VERSION_STRING,
5062 .protocol_id = SMB30_PROT_ID,
5063 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5064 .large_lock_type = 0,
5065 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5066 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5067 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5068 .header_size = sizeof(struct smb2_sync_hdr),
5069 .header_preamble_size = 0,
5070 .max_header_size = MAX_SMB2_HDR_SIZE,
5071 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5072 .lock_cmd = SMB2_LOCK,
5073 .cap_unix = 0,
5074 .cap_nt_find = SMB2_NT_FIND,
5075 .cap_large_files = SMB2_LARGE_FILES,
5076 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5077 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5078 .create_lease_size = sizeof(struct create_lease_v2),
5079};
5080
5081struct smb_version_values smb302_values = {
5082 .version_string = SMB302_VERSION_STRING,
5083 .protocol_id = SMB302_PROT_ID,
5084 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5085 .large_lock_type = 0,
5086 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5087 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5088 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5089 .header_size = sizeof(struct smb2_sync_hdr),
5090 .header_preamble_size = 0,
5091 .max_header_size = MAX_SMB2_HDR_SIZE,
5092 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5093 .lock_cmd = SMB2_LOCK,
5094 .cap_unix = 0,
5095 .cap_nt_find = SMB2_NT_FIND,
5096 .cap_large_files = SMB2_LARGE_FILES,
5097 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5098 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5099 .create_lease_size = sizeof(struct create_lease_v2),
5100};
5101
5102struct smb_version_values smb311_values = {
5103 .version_string = SMB311_VERSION_STRING,
5104 .protocol_id = SMB311_PROT_ID,
5105 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION | SMB2_GLOBAL_CAP_DIRECTORY_LEASING,
5106 .large_lock_type = 0,
5107 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5108 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5109 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5110 .header_size = sizeof(struct smb2_sync_hdr),
5111 .header_preamble_size = 0,
5112 .max_header_size = MAX_SMB2_HDR_SIZE,
5113 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5114 .lock_cmd = SMB2_LOCK,
5115 .cap_unix = 0,
5116 .cap_nt_find = SMB2_NT_FIND,
5117 .cap_large_files = SMB2_LARGE_FILES,
5118 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5119 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5120 .create_lease_size = sizeof(struct create_lease_v2),
5121};