1 // SPDX-License-Identifier: GPL-2.0
3 * SMB2 version specific operations
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
8 #include <linux/pagemap.h>
10 #include <linux/falloc.h>
11 #include <linux/scatterlist.h>
12 #include <linux/uuid.h>
13 #include <linux/sort.h>
14 #include <crypto/aead.h>
15 #include <linux/fiemap.h>
16 #include <uapi/linux/magic.h>
20 #include "smb2proto.h"
21 #include "cifsproto.h"
22 #include "cifs_debug.h"
23 #include "cifs_unicode.h"
24 #include "smb2status.h"
26 #include "cifs_ioctl.h"
27 #include "smbdirect.h"
29 #include "fs_context.h"
30 #include "cached_dir.h"
32 /* Change credits for different ops and return the total number of credits */
34 change_conf(struct TCP_Server_Info
*server
)
36 server
->credits
+= server
->echo_credits
+ server
->oplock_credits
;
37 if (server
->credits
> server
->max_credits
)
38 server
->credits
= server
->max_credits
;
39 server
->oplock_credits
= server
->echo_credits
= 0;
40 switch (server
->credits
) {
44 server
->echoes
= false;
45 server
->oplocks
= false;
48 server
->echoes
= true;
49 server
->oplocks
= false;
50 server
->echo_credits
= 1;
53 server
->echoes
= true;
55 server
->oplocks
= true;
56 server
->oplock_credits
= 1;
58 server
->oplocks
= false;
60 server
->echo_credits
= 1;
62 server
->credits
-= server
->echo_credits
+ server
->oplock_credits
;
63 return server
->credits
+ server
->echo_credits
+ server
->oplock_credits
;
67 smb2_add_credits(struct TCP_Server_Info
*server
,
68 const struct cifs_credits
*credits
, const int optype
)
71 int scredits
, in_flight
;
72 unsigned int add
= credits
->value
;
73 unsigned int instance
= credits
->instance
;
74 bool reconnect_detected
= false;
75 bool reconnect_with_invalid_credits
= false;
77 spin_lock(&server
->req_lock
);
78 val
= server
->ops
->get_credits_field(server
, optype
);
80 /* eg found case where write overlapping reconnect messed up credits */
81 if (((optype
& CIFS_OP_MASK
) == CIFS_NEG_OP
) && (*val
!= 0))
82 reconnect_with_invalid_credits
= true;
84 if ((instance
== 0) || (instance
== server
->reconnect_instance
))
87 reconnect_detected
= true;
90 *val
= 65000; /* Don't get near 64K credits, avoid srv bugs */
91 pr_warn_once("server overflowed SMB3 credits\n");
92 trace_smb3_overflow_credits(server
->CurrentMid
,
93 server
->conn_id
, server
->hostname
, *val
,
94 add
, server
->in_flight
);
96 WARN_ON_ONCE(server
->in_flight
== 0);
98 if (server
->in_flight
== 0 &&
99 ((optype
& CIFS_OP_MASK
) != CIFS_NEG_OP
) &&
100 ((optype
& CIFS_OP_MASK
) != CIFS_SESS_OP
))
101 rc
= change_conf(server
);
103 * Sometimes server returns 0 credits on oplock break ack - we need to
104 * rebalance credits in this case.
106 else if (server
->in_flight
> 0 && server
->oplock_credits
== 0 &&
108 if (server
->credits
> 1) {
110 server
->oplock_credits
++;
112 } else if ((server
->in_flight
> 0) && (server
->oplock_credits
> 3) &&
113 ((optype
& CIFS_OP_MASK
) == CIFS_OBREAK_OP
))
114 /* if now have too many oplock credits, rebalance so don't starve normal ops */
118 in_flight
= server
->in_flight
;
119 spin_unlock(&server
->req_lock
);
120 wake_up(&server
->request_q
);
122 if (reconnect_detected
) {
123 trace_smb3_reconnect_detected(server
->CurrentMid
,
124 server
->conn_id
, server
->hostname
, scredits
, add
, in_flight
);
126 cifs_dbg(FYI
, "trying to put %d credits from the old server instance %d\n",
130 if (reconnect_with_invalid_credits
) {
131 trace_smb3_reconnect_with_invalid_credits(server
->CurrentMid
,
132 server
->conn_id
, server
->hostname
, scredits
, add
, in_flight
);
133 cifs_dbg(FYI
, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
134 optype
, scredits
, add
);
137 spin_lock(&server
->srv_lock
);
138 if (server
->tcpStatus
== CifsNeedReconnect
139 || server
->tcpStatus
== CifsExiting
) {
140 spin_unlock(&server
->srv_lock
);
143 spin_unlock(&server
->srv_lock
);
147 /* change_conf hasn't been executed */
150 cifs_server_dbg(VFS
, "Possible client or server bug - zero credits\n");
153 cifs_server_dbg(VFS
, "disabling echoes and oplocks\n");
156 cifs_dbg(FYI
, "disabling oplocks\n");
159 /* change_conf rebalanced credits for different types */
163 trace_smb3_add_credits(server
->CurrentMid
,
164 server
->conn_id
, server
->hostname
, scredits
, add
, in_flight
);
165 cifs_dbg(FYI
, "%s: added %u credits total=%d\n", __func__
, add
, scredits
);
169 smb2_set_credits(struct TCP_Server_Info
*server
, const int val
)
171 int scredits
, in_flight
;
173 spin_lock(&server
->req_lock
);
174 server
->credits
= val
;
176 server
->reconnect_instance
++;
178 * ChannelSequence updated for all channels in primary channel so that consistent
179 * across SMB3 requests sent on any channel. See MS-SMB2 3.2.4.1 and 3.2.7.1
181 if (SERVER_IS_CHAN(server
))
182 server
->primary_server
->channel_sequence_num
++;
184 server
->channel_sequence_num
++;
186 scredits
= server
->credits
;
187 in_flight
= server
->in_flight
;
188 spin_unlock(&server
->req_lock
);
190 trace_smb3_set_credits(server
->CurrentMid
,
191 server
->conn_id
, server
->hostname
, scredits
, val
, in_flight
);
192 cifs_dbg(FYI
, "%s: set %u credits\n", __func__
, val
);
194 /* don't log while holding the lock */
196 cifs_dbg(FYI
, "set credits to 1 due to smb2 reconnect\n");
200 smb2_get_credits_field(struct TCP_Server_Info
*server
, const int optype
)
204 return &server
->echo_credits
;
206 return &server
->oplock_credits
;
208 return &server
->credits
;
213 smb2_get_credits(struct mid_q_entry
*mid
)
215 return mid
->credits_received
;
219 smb2_wait_mtu_credits(struct TCP_Server_Info
*server
, unsigned int size
,
220 unsigned int *num
, struct cifs_credits
*credits
)
223 unsigned int scredits
, in_flight
;
225 spin_lock(&server
->req_lock
);
227 spin_unlock(&server
->req_lock
);
229 spin_lock(&server
->srv_lock
);
230 if (server
->tcpStatus
== CifsExiting
) {
231 spin_unlock(&server
->srv_lock
);
234 spin_unlock(&server
->srv_lock
);
236 spin_lock(&server
->req_lock
);
237 if (server
->credits
<= 0) {
238 spin_unlock(&server
->req_lock
);
239 cifs_num_waiters_inc(server
);
240 rc
= wait_event_killable(server
->request_q
,
241 has_credits(server
, &server
->credits
, 1));
242 cifs_num_waiters_dec(server
);
245 spin_lock(&server
->req_lock
);
247 scredits
= server
->credits
;
248 /* can deadlock with reopen */
250 *num
= SMB2_MAX_BUFFER_SIZE
;
252 credits
->instance
= 0;
256 /* leave some credits for reopen and other ops */
258 *num
= min_t(unsigned int, size
,
259 scredits
* SMB2_MAX_BUFFER_SIZE
);
262 DIV_ROUND_UP(*num
, SMB2_MAX_BUFFER_SIZE
);
263 credits
->instance
= server
->reconnect_instance
;
264 server
->credits
-= credits
->value
;
266 if (server
->in_flight
> server
->max_in_flight
)
267 server
->max_in_flight
= server
->in_flight
;
271 scredits
= server
->credits
;
272 in_flight
= server
->in_flight
;
273 spin_unlock(&server
->req_lock
);
275 trace_smb3_wait_credits(server
->CurrentMid
,
276 server
->conn_id
, server
->hostname
, scredits
, -(credits
->value
), in_flight
);
277 cifs_dbg(FYI
, "%s: removed %u credits total=%d\n",
278 __func__
, credits
->value
, scredits
);
284 smb2_adjust_credits(struct TCP_Server_Info
*server
,
285 struct cifs_credits
*credits
,
286 const unsigned int payload_size
)
288 int new_val
= DIV_ROUND_UP(payload_size
, SMB2_MAX_BUFFER_SIZE
);
289 int scredits
, in_flight
;
291 if (!credits
->value
|| credits
->value
== new_val
)
294 if (credits
->value
< new_val
) {
295 trace_smb3_too_many_credits(server
->CurrentMid
,
296 server
->conn_id
, server
->hostname
, 0, credits
->value
- new_val
, 0);
297 cifs_server_dbg(VFS
, "request has less credits (%d) than required (%d)",
298 credits
->value
, new_val
);
303 spin_lock(&server
->req_lock
);
305 if (server
->reconnect_instance
!= credits
->instance
) {
306 scredits
= server
->credits
;
307 in_flight
= server
->in_flight
;
308 spin_unlock(&server
->req_lock
);
310 trace_smb3_reconnect_detected(server
->CurrentMid
,
311 server
->conn_id
, server
->hostname
, scredits
,
312 credits
->value
- new_val
, in_flight
);
313 cifs_server_dbg(VFS
, "trying to return %d credits to old session\n",
314 credits
->value
- new_val
);
318 server
->credits
+= credits
->value
- new_val
;
319 scredits
= server
->credits
;
320 in_flight
= server
->in_flight
;
321 spin_unlock(&server
->req_lock
);
322 wake_up(&server
->request_q
);
324 trace_smb3_adj_credits(server
->CurrentMid
,
325 server
->conn_id
, server
->hostname
, scredits
,
326 credits
->value
- new_val
, in_flight
);
327 cifs_dbg(FYI
, "%s: adjust added %u credits total=%d\n",
328 __func__
, credits
->value
- new_val
, scredits
);
330 credits
->value
= new_val
;
336 smb2_get_next_mid(struct TCP_Server_Info
*server
)
339 /* for SMB2 we need the current value */
340 spin_lock(&server
->mid_lock
);
341 mid
= server
->CurrentMid
++;
342 spin_unlock(&server
->mid_lock
);
347 smb2_revert_current_mid(struct TCP_Server_Info
*server
, const unsigned int val
)
349 spin_lock(&server
->mid_lock
);
350 if (server
->CurrentMid
>= val
)
351 server
->CurrentMid
-= val
;
352 spin_unlock(&server
->mid_lock
);
355 static struct mid_q_entry
*
356 __smb2_find_mid(struct TCP_Server_Info
*server
, char *buf
, bool dequeue
)
358 struct mid_q_entry
*mid
;
359 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
360 __u64 wire_mid
= le64_to_cpu(shdr
->MessageId
);
362 if (shdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
) {
363 cifs_server_dbg(VFS
, "Encrypted frame parsing not supported yet\n");
367 spin_lock(&server
->mid_lock
);
368 list_for_each_entry(mid
, &server
->pending_mid_q
, qhead
) {
369 if ((mid
->mid
== wire_mid
) &&
370 (mid
->mid_state
== MID_REQUEST_SUBMITTED
) &&
371 (mid
->command
== shdr
->Command
)) {
372 kref_get(&mid
->refcount
);
374 list_del_init(&mid
->qhead
);
375 mid
->mid_flags
|= MID_DELETED
;
377 spin_unlock(&server
->mid_lock
);
381 spin_unlock(&server
->mid_lock
);
385 static struct mid_q_entry
*
386 smb2_find_mid(struct TCP_Server_Info
*server
, char *buf
)
388 return __smb2_find_mid(server
, buf
, false);
391 static struct mid_q_entry
*
392 smb2_find_dequeue_mid(struct TCP_Server_Info
*server
, char *buf
)
394 return __smb2_find_mid(server
, buf
, true);
398 smb2_dump_detail(void *buf
, struct TCP_Server_Info
*server
)
400 #ifdef CONFIG_CIFS_DEBUG2
401 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
403 cifs_server_dbg(VFS
, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
404 shdr
->Command
, shdr
->Status
, shdr
->Flags
, shdr
->MessageId
,
405 shdr
->Id
.SyncId
.ProcessId
);
406 if (!server
->ops
->check_message(buf
, server
->total_read
, server
)) {
407 cifs_server_dbg(VFS
, "smb buf %p len %u\n", buf
,
408 server
->ops
->calc_smb_size(buf
));
414 smb2_need_neg(struct TCP_Server_Info
*server
)
416 return server
->max_read
== 0;
420 smb2_negotiate(const unsigned int xid
,
421 struct cifs_ses
*ses
,
422 struct TCP_Server_Info
*server
)
426 spin_lock(&server
->mid_lock
);
427 server
->CurrentMid
= 0;
428 spin_unlock(&server
->mid_lock
);
429 rc
= SMB2_negotiate(xid
, ses
, server
);
430 /* BB we probably don't need to retry with modern servers */
437 smb2_negotiate_wsize(struct cifs_tcon
*tcon
, struct smb3_fs_context
*ctx
)
439 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
442 /* start with specified wsize, or default */
443 wsize
= ctx
->wsize
? ctx
->wsize
: CIFS_DEFAULT_IOSIZE
;
444 wsize
= min_t(unsigned int, wsize
, server
->max_write
);
445 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
446 wsize
= min_t(unsigned int, wsize
, SMB2_MAX_BUFFER_SIZE
);
452 smb3_negotiate_wsize(struct cifs_tcon
*tcon
, struct smb3_fs_context
*ctx
)
454 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
457 /* start with specified wsize, or default */
458 wsize
= ctx
->wsize
? ctx
->wsize
: SMB3_DEFAULT_IOSIZE
;
459 wsize
= min_t(unsigned int, wsize
, server
->max_write
);
460 #ifdef CONFIG_CIFS_SMB_DIRECT
464 * Account for SMB2 data transfer packet header and
465 * possible encryption header
467 wsize
= min_t(unsigned int,
469 server
->smbd_conn
->max_fragmented_send_size
-
470 SMB2_READWRITE_PDU_HEADER_SIZE
-
471 sizeof(struct smb2_transform_hdr
));
473 wsize
= min_t(unsigned int,
474 wsize
, server
->smbd_conn
->max_readwrite_size
);
477 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
478 wsize
= min_t(unsigned int, wsize
, SMB2_MAX_BUFFER_SIZE
);
484 smb2_negotiate_rsize(struct cifs_tcon
*tcon
, struct smb3_fs_context
*ctx
)
486 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
489 /* start with specified rsize, or default */
490 rsize
= ctx
->rsize
? ctx
->rsize
: CIFS_DEFAULT_IOSIZE
;
491 rsize
= min_t(unsigned int, rsize
, server
->max_read
);
493 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
494 rsize
= min_t(unsigned int, rsize
, SMB2_MAX_BUFFER_SIZE
);
500 smb3_negotiate_rsize(struct cifs_tcon
*tcon
, struct smb3_fs_context
*ctx
)
502 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
505 /* start with specified rsize, or default */
506 rsize
= ctx
->rsize
? ctx
->rsize
: SMB3_DEFAULT_IOSIZE
;
507 rsize
= min_t(unsigned int, rsize
, server
->max_read
);
508 #ifdef CONFIG_CIFS_SMB_DIRECT
512 * Account for SMB2 data transfer packet header and
513 * possible encryption header
515 rsize
= min_t(unsigned int,
517 server
->smbd_conn
->max_fragmented_recv_size
-
518 SMB2_READWRITE_PDU_HEADER_SIZE
-
519 sizeof(struct smb2_transform_hdr
));
521 rsize
= min_t(unsigned int,
522 rsize
, server
->smbd_conn
->max_readwrite_size
);
526 if (!(server
->capabilities
& SMB2_GLOBAL_CAP_LARGE_MTU
))
527 rsize
= min_t(unsigned int, rsize
, SMB2_MAX_BUFFER_SIZE
);
533 * compare two interfaces a and b
534 * return 0 if everything matches.
535 * return 1 if a is rdma capable, or rss capable, or has higher link speed
536 * return -1 otherwise.
539 iface_cmp(struct cifs_server_iface
*a
, struct cifs_server_iface
*b
)
544 if (a
->rdma_capable
== b
->rdma_capable
) {
545 if (a
->rss_capable
== b
->rss_capable
) {
546 if (a
->speed
== b
->speed
) {
547 cmp_ret
= cifs_ipaddr_cmp((struct sockaddr
*) &a
->sockaddr
,
548 (struct sockaddr
*) &b
->sockaddr
);
551 else if (cmp_ret
> 0)
555 } else if (a
->speed
> b
->speed
)
559 } else if (a
->rss_capable
> b
->rss_capable
)
563 } else if (a
->rdma_capable
> b
->rdma_capable
)
570 parse_server_interfaces(struct network_interface_info_ioctl_rsp
*buf
,
571 size_t buf_len
, struct cifs_ses
*ses
, bool in_mount
)
573 struct network_interface_info_ioctl_rsp
*p
;
574 struct sockaddr_in
*addr4
;
575 struct sockaddr_in6
*addr6
;
576 struct iface_info_ipv4
*p4
;
577 struct iface_info_ipv6
*p6
;
578 struct cifs_server_iface
*info
= NULL
, *iface
= NULL
, *niface
= NULL
;
579 struct cifs_server_iface tmp_iface
;
585 bytes_left
= buf_len
;
588 spin_lock(&ses
->iface_lock
);
589 /* do not query too frequently, this time with lock held */
590 if (ses
->iface_last_update
&&
591 time_before(jiffies
, ses
->iface_last_update
+
592 (SMB_INTERFACE_POLL_INTERVAL
* HZ
))) {
593 spin_unlock(&ses
->iface_lock
);
598 * Go through iface_list and mark them as inactive
600 list_for_each_entry_safe(iface
, niface
, &ses
->iface_list
,
602 iface
->is_active
= 0;
604 spin_unlock(&ses
->iface_lock
);
607 * Samba server e.g. can return an empty interface list in some cases,
608 * which would only be a problem if we were requesting multichannel
610 if (bytes_left
== 0) {
611 /* avoid spamming logs every 10 minutes, so log only in mount */
612 if ((ses
->chan_max
> 1) && in_mount
)
614 "multichannel not available\n"
615 "Empty network interface list returned by server %s\n",
616 ses
->server
->hostname
);
621 while (bytes_left
>= sizeof(*p
)) {
622 memset(&tmp_iface
, 0, sizeof(tmp_iface
));
623 tmp_iface
.speed
= le64_to_cpu(p
->LinkSpeed
);
624 tmp_iface
.rdma_capable
= le32_to_cpu(p
->Capability
& RDMA_CAPABLE
) ? 1 : 0;
625 tmp_iface
.rss_capable
= le32_to_cpu(p
->Capability
& RSS_CAPABLE
) ? 1 : 0;
629 * The kernel and wire socket structures have the same
630 * layout and use network byte order but make the
631 * conversion explicit in case either one changes.
634 addr4
= (struct sockaddr_in
*)&tmp_iface
.sockaddr
;
635 p4
= (struct iface_info_ipv4
*)p
->Buffer
;
636 addr4
->sin_family
= AF_INET
;
637 memcpy(&addr4
->sin_addr
, &p4
->IPv4Address
, 4);
639 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
640 addr4
->sin_port
= cpu_to_be16(CIFS_PORT
);
642 cifs_dbg(FYI
, "%s: ipv4 %pI4\n", __func__
,
646 addr6
= (struct sockaddr_in6
*)&tmp_iface
.sockaddr
;
647 p6
= (struct iface_info_ipv6
*)p
->Buffer
;
648 addr6
->sin6_family
= AF_INET6
;
649 memcpy(&addr6
->sin6_addr
, &p6
->IPv6Address
, 16);
651 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
652 addr6
->sin6_flowinfo
= 0;
653 addr6
->sin6_scope_id
= 0;
654 addr6
->sin6_port
= cpu_to_be16(CIFS_PORT
);
656 cifs_dbg(FYI
, "%s: ipv6 %pI6\n", __func__
,
661 "%s: skipping unsupported socket family\n",
667 * The iface_list is assumed to be sorted by speed.
668 * Check if the new interface exists in that list.
669 * NEVER change iface. it could be in use.
670 * Add a new one instead
672 spin_lock(&ses
->iface_lock
);
673 list_for_each_entry_safe(iface
, niface
, &ses
->iface_list
,
675 ret
= iface_cmp(iface
, &tmp_iface
);
677 iface
->is_active
= 1;
678 spin_unlock(&ses
->iface_lock
);
680 } else if (ret
< 0) {
681 /* all remaining ifaces are slower */
682 kref_get(&iface
->refcount
);
686 spin_unlock(&ses
->iface_lock
);
688 /* no match. insert the entry in the list */
689 info
= kmalloc(sizeof(struct cifs_server_iface
),
695 memcpy(info
, &tmp_iface
, sizeof(tmp_iface
));
697 /* add this new entry to the list */
698 kref_init(&info
->refcount
);
701 cifs_dbg(FYI
, "%s: adding iface %zu\n", __func__
, ses
->iface_count
);
702 cifs_dbg(FYI
, "%s: speed %zu bps\n", __func__
, info
->speed
);
703 cifs_dbg(FYI
, "%s: capabilities 0x%08x\n", __func__
,
704 le32_to_cpu(p
->Capability
));
706 spin_lock(&ses
->iface_lock
);
707 if (!list_entry_is_head(iface
, &ses
->iface_list
, iface_head
)) {
708 list_add_tail(&info
->iface_head
, &iface
->iface_head
);
709 kref_put(&iface
->refcount
, release_iface
);
711 list_add_tail(&info
->iface_head
, &ses
->iface_list
);
714 spin_unlock(&ses
->iface_lock
);
715 ses
->iface_last_update
= jiffies
;
718 next
= le32_to_cpu(p
->Next
);
720 bytes_left
-= sizeof(*p
);
723 p
= (struct network_interface_info_ioctl_rsp
*)((u8
*)p
+next
);
728 cifs_dbg(VFS
, "%s: malformed interface info\n", __func__
);
733 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
734 if ((bytes_left
> 8) || p
->Next
)
735 cifs_dbg(VFS
, "%s: incomplete interface info\n", __func__
);
738 if (!ses
->iface_count
) {
745 * Go through the list again and put the inactive entries
747 spin_lock(&ses
->iface_lock
);
748 list_for_each_entry_safe(iface
, niface
, &ses
->iface_list
,
750 if (!iface
->is_active
) {
751 list_del(&iface
->iface_head
);
752 kref_put(&iface
->refcount
, release_iface
);
756 spin_unlock(&ses
->iface_lock
);
762 SMB3_request_interfaces(const unsigned int xid
, struct cifs_tcon
*tcon
, bool in_mount
)
765 unsigned int ret_data_len
= 0;
766 struct network_interface_info_ioctl_rsp
*out_buf
= NULL
;
767 struct cifs_ses
*ses
= tcon
->ses
;
768 struct TCP_Server_Info
*pserver
;
770 /* do not query too frequently */
771 if (ses
->iface_last_update
&&
772 time_before(jiffies
, ses
->iface_last_update
+
773 (SMB_INTERFACE_POLL_INTERVAL
* HZ
)))
776 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
777 FSCTL_QUERY_NETWORK_INTERFACE_INFO
,
778 NULL
/* no data input */, 0 /* no data input */,
779 CIFSMaxBufSize
, (char **)&out_buf
, &ret_data_len
);
780 if (rc
== -EOPNOTSUPP
) {
782 "server does not support query network interfaces\n");
784 } else if (rc
!= 0) {
785 cifs_tcon_dbg(VFS
, "error %d on ioctl to get interface list\n", rc
);
789 rc
= parse_server_interfaces(out_buf
, ret_data_len
, ses
, in_mount
);
793 /* check if iface is still active */
794 spin_lock(&ses
->chan_lock
);
795 pserver
= ses
->chans
[0].server
;
796 if (pserver
&& !cifs_chan_is_iface_active(ses
, pserver
)) {
797 spin_unlock(&ses
->chan_lock
);
798 cifs_chan_update_iface(ses
, pserver
);
799 spin_lock(&ses
->chan_lock
);
801 spin_unlock(&ses
->chan_lock
);
809 smb3_qfs_tcon(const unsigned int xid
, struct cifs_tcon
*tcon
,
810 struct cifs_sb_info
*cifs_sb
)
813 __le16 srch_path
= 0; /* Null - open root of share */
814 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
815 struct cifs_open_parms oparms
;
817 struct cached_fid
*cfid
= NULL
;
819 oparms
= (struct cifs_open_parms
) {
822 .desired_access
= FILE_READ_ATTRIBUTES
,
823 .disposition
= FILE_OPEN
,
824 .create_options
= cifs_create_options(cifs_sb
, 0),
828 rc
= open_cached_dir(xid
, tcon
, "", cifs_sb
, false, &cfid
);
830 memcpy(&fid
, &cfid
->fid
, sizeof(struct cifs_fid
));
832 rc
= SMB2_open(xid
, &oparms
, &srch_path
, &oplock
, NULL
, NULL
,
837 SMB3_request_interfaces(xid
, tcon
, true /* called during mount */);
839 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
840 FS_ATTRIBUTE_INFORMATION
);
841 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
842 FS_DEVICE_INFORMATION
);
843 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
844 FS_VOLUME_INFORMATION
);
845 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
846 FS_SECTOR_SIZE_INFORMATION
); /* SMB3 specific */
848 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
850 close_cached_dir(cfid
);
854 smb2_qfs_tcon(const unsigned int xid
, struct cifs_tcon
*tcon
,
855 struct cifs_sb_info
*cifs_sb
)
858 __le16 srch_path
= 0; /* Null - open root of share */
859 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
860 struct cifs_open_parms oparms
;
863 oparms
= (struct cifs_open_parms
) {
866 .desired_access
= FILE_READ_ATTRIBUTES
,
867 .disposition
= FILE_OPEN
,
868 .create_options
= cifs_create_options(cifs_sb
, 0),
872 rc
= SMB2_open(xid
, &oparms
, &srch_path
, &oplock
, NULL
, NULL
,
877 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
878 FS_ATTRIBUTE_INFORMATION
);
879 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
880 FS_DEVICE_INFORMATION
);
881 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
885 smb2_is_path_accessible(const unsigned int xid
, struct cifs_tcon
*tcon
,
886 struct cifs_sb_info
*cifs_sb
, const char *full_path
)
889 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
890 int err_buftype
= CIFS_NO_BUFFER
;
891 struct cifs_open_parms oparms
;
892 struct kvec err_iov
= {};
894 struct cached_fid
*cfid
;
898 rc
= open_cached_dir(xid
, tcon
, full_path
, cifs_sb
, true, &cfid
);
900 if (cfid
->has_lease
) {
901 close_cached_dir(cfid
);
904 close_cached_dir(cfid
);
907 utf16_path
= cifs_convert_path_to_utf16(full_path
, cifs_sb
);
911 oparms
= (struct cifs_open_parms
) {
914 .desired_access
= FILE_READ_ATTRIBUTES
,
915 .disposition
= FILE_OPEN
,
916 .create_options
= cifs_create_options(cifs_sb
, 0),
920 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
,
921 &err_iov
, &err_buftype
);
923 struct smb2_hdr
*hdr
= err_iov
.iov_base
;
925 if (unlikely(!hdr
|| err_buftype
== CIFS_NO_BUFFER
))
928 if (rc
!= -EREMOTE
&& hdr
->Status
== STATUS_OBJECT_NAME_INVALID
) {
929 rc2
= cifs_inval_name_dfs_link_error(xid
, tcon
, cifs_sb
,
938 if (rc
== -EREMOTE
&& IS_ENABLED(CONFIG_CIFS_DFS_UPCALL
) && cifs_sb
&&
939 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_DFS
))
944 rc
= SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
947 free_rsp_buf(err_buftype
, err_iov
.iov_base
);
952 static int smb2_get_srv_inum(const unsigned int xid
, struct cifs_tcon
*tcon
,
953 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
954 u64
*uniqueid
, struct cifs_open_info_data
*data
)
956 *uniqueid
= le64_to_cpu(data
->fi
.IndexNumber
);
960 static int smb2_query_file_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
961 struct cifsFileInfo
*cfile
, struct cifs_open_info_data
*data
)
963 struct cifs_fid
*fid
= &cfile
->fid
;
965 if (cfile
->symlink_target
) {
966 data
->symlink_target
= kstrdup(cfile
->symlink_target
, GFP_KERNEL
);
967 if (!data
->symlink_target
)
970 return SMB2_query_info(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
, &data
->fi
);
973 #ifdef CONFIG_CIFS_XATTR
975 move_smb2_ea_to_cifs(char *dst
, size_t dst_size
,
976 struct smb2_file_full_ea_info
*src
, size_t src_size
,
977 const unsigned char *ea_name
)
980 unsigned int ea_name_len
= ea_name
? strlen(ea_name
) : 0;
982 size_t buf_size
= dst_size
;
983 size_t name_len
, value_len
, user_name_len
;
985 while (src_size
> 0) {
986 name_len
= (size_t)src
->ea_name_length
;
987 value_len
= (size_t)le16_to_cpu(src
->ea_value_length
);
992 if (src_size
< 8 + name_len
+ 1 + value_len
) {
993 cifs_dbg(FYI
, "EA entry goes beyond length of list\n");
998 name
= &src
->ea_data
[0];
999 value
= &src
->ea_data
[src
->ea_name_length
+ 1];
1002 if (ea_name_len
== name_len
&&
1003 memcmp(ea_name
, name
, name_len
) == 0) {
1007 if (dst_size
< value_len
) {
1011 memcpy(dst
, value
, value_len
);
1015 /* 'user.' plus a terminating null */
1016 user_name_len
= 5 + 1 + name_len
;
1018 if (buf_size
== 0) {
1019 /* skip copy - calc size only */
1020 rc
+= user_name_len
;
1021 } else if (dst_size
>= user_name_len
) {
1022 dst_size
-= user_name_len
;
1023 memcpy(dst
, "user.", 5);
1025 memcpy(dst
, src
->ea_data
, name_len
);
1029 rc
+= user_name_len
;
1031 /* stop before overrun buffer */
1037 if (!src
->next_entry_offset
)
1040 if (src_size
< le32_to_cpu(src
->next_entry_offset
)) {
1041 /* stop before overrun buffer */
1045 src_size
-= le32_to_cpu(src
->next_entry_offset
);
1046 src
= (void *)((char *)src
+
1047 le32_to_cpu(src
->next_entry_offset
));
1050 /* didn't find the named attribute */
1059 smb2_query_eas(const unsigned int xid
, struct cifs_tcon
*tcon
,
1060 const unsigned char *path
, const unsigned char *ea_name
,
1061 char *ea_data
, size_t buf_size
,
1062 struct cifs_sb_info
*cifs_sb
)
1065 struct kvec rsp_iov
= {NULL
, 0};
1066 int buftype
= CIFS_NO_BUFFER
;
1067 struct smb2_query_info_rsp
*rsp
;
1068 struct smb2_file_full_ea_info
*info
= NULL
;
1070 rc
= smb2_query_info_compound(xid
, tcon
, path
,
1072 FILE_FULL_EA_INFORMATION
,
1075 MAX_SMB2_CREATE_RESPONSE_SIZE
-
1076 MAX_SMB2_CLOSE_RESPONSE_SIZE
,
1077 &rsp_iov
, &buftype
, cifs_sb
);
1080 * If ea_name is NULL (listxattr) and there are no EAs,
1081 * return 0 as it's not an error. Otherwise, the specified
1082 * ea_name was not found.
1084 if (!ea_name
&& rc
== -ENODATA
)
1089 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
1090 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
1091 le32_to_cpu(rsp
->OutputBufferLength
),
1093 sizeof(struct smb2_file_full_ea_info
));
1097 info
= (struct smb2_file_full_ea_info
*)(
1098 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)rsp
);
1099 rc
= move_smb2_ea_to_cifs(ea_data
, buf_size
, info
,
1100 le32_to_cpu(rsp
->OutputBufferLength
), ea_name
);
1103 free_rsp_buf(buftype
, rsp_iov
.iov_base
);
1108 smb2_set_ea(const unsigned int xid
, struct cifs_tcon
*tcon
,
1109 const char *path
, const char *ea_name
, const void *ea_value
,
1110 const __u16 ea_value_len
, const struct nls_table
*nls_codepage
,
1111 struct cifs_sb_info
*cifs_sb
)
1113 struct smb2_compound_vars
*vars
;
1114 struct cifs_ses
*ses
= tcon
->ses
;
1115 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
1116 struct smb_rqst
*rqst
;
1117 struct kvec
*rsp_iov
;
1118 __le16
*utf16_path
= NULL
;
1119 int ea_name_len
= strlen(ea_name
);
1120 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
1122 int resp_buftype
[3];
1123 struct cifs_open_parms oparms
;
1124 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1125 struct cifs_fid fid
;
1126 unsigned int size
[1];
1128 struct smb2_file_full_ea_info
*ea
= NULL
;
1129 struct smb2_query_info_rsp
*rsp
;
1130 int rc
, used_len
= 0;
1132 if (smb3_encryption_required(tcon
))
1133 flags
|= CIFS_TRANSFORM_REQ
;
1135 if (ea_name_len
> 255)
1138 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
1142 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
1143 vars
= kzalloc(sizeof(*vars
), GFP_KERNEL
);
1149 rsp_iov
= vars
->rsp_iov
;
1151 if (ses
->server
->ops
->query_all_EAs
) {
1153 rc
= ses
->server
->ops
->query_all_EAs(xid
, tcon
, path
,
1159 /* If we are adding a attribute we should first check
1160 * if there will be enough space available to store
1161 * the new EA. If not we should not add it since we
1162 * would not be able to even read the EAs back.
1164 rc
= smb2_query_info_compound(xid
, tcon
, path
,
1166 FILE_FULL_EA_INFORMATION
,
1169 MAX_SMB2_CREATE_RESPONSE_SIZE
-
1170 MAX_SMB2_CLOSE_RESPONSE_SIZE
,
1171 &rsp_iov
[1], &resp_buftype
[1], cifs_sb
);
1173 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
[1].iov_base
;
1174 used_len
= le32_to_cpu(rsp
->OutputBufferLength
);
1176 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
1177 resp_buftype
[1] = CIFS_NO_BUFFER
;
1178 memset(&rsp_iov
[1], 0, sizeof(rsp_iov
[1]));
1181 /* Use a fudge factor of 256 bytes in case we collide
1182 * with a different set_EAs command.
1184 if (CIFSMaxBufSize
- MAX_SMB2_CREATE_RESPONSE_SIZE
-
1185 MAX_SMB2_CLOSE_RESPONSE_SIZE
- 256 <
1186 used_len
+ ea_name_len
+ ea_value_len
+ 1) {
1194 rqst
[0].rq_iov
= vars
->open_iov
;
1195 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
1197 oparms
= (struct cifs_open_parms
) {
1200 .desired_access
= FILE_WRITE_EA
,
1201 .disposition
= FILE_OPEN
,
1202 .create_options
= cifs_create_options(cifs_sb
, 0),
1206 rc
= SMB2_open_init(tcon
, server
,
1207 &rqst
[0], &oplock
, &oparms
, utf16_path
);
1210 smb2_set_next_command(tcon
, &rqst
[0]);
1214 rqst
[1].rq_iov
= vars
->si_iov
;
1215 rqst
[1].rq_nvec
= 1;
1217 len
= sizeof(*ea
) + ea_name_len
+ ea_value_len
+ 1;
1218 ea
= kzalloc(len
, GFP_KERNEL
);
1224 ea
->ea_name_length
= ea_name_len
;
1225 ea
->ea_value_length
= cpu_to_le16(ea_value_len
);
1226 memcpy(ea
->ea_data
, ea_name
, ea_name_len
+ 1);
1227 memcpy(ea
->ea_data
+ ea_name_len
+ 1, ea_value
, ea_value_len
);
1232 rc
= SMB2_set_info_init(tcon
, server
,
1233 &rqst
[1], COMPOUND_FID
,
1234 COMPOUND_FID
, current
->tgid
,
1235 FILE_FULL_EA_INFORMATION
,
1236 SMB2_O_INFO_FILE
, 0, data
, size
);
1239 smb2_set_next_command(tcon
, &rqst
[1]);
1240 smb2_set_related(&rqst
[1]);
1243 rqst
[2].rq_iov
= &vars
->close_iov
;
1244 rqst
[2].rq_nvec
= 1;
1245 rc
= SMB2_close_init(tcon
, server
,
1246 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
1249 smb2_set_related(&rqst
[2]);
1251 rc
= compound_send_recv(xid
, ses
, server
,
1253 resp_buftype
, rsp_iov
);
1254 /* no need to bump num_remote_opens because handle immediately closed */
1258 SMB2_open_free(&rqst
[0]);
1259 SMB2_set_info_free(&rqst
[1]);
1260 SMB2_close_free(&rqst
[2]);
1261 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
1262 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
1263 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
1272 smb2_can_echo(struct TCP_Server_Info
*server
)
1274 return server
->echoes
;
1278 smb2_clear_stats(struct cifs_tcon
*tcon
)
1282 for (i
= 0; i
< NUMBER_OF_SMB2_COMMANDS
; i
++) {
1283 atomic_set(&tcon
->stats
.smb2_stats
.smb2_com_sent
[i
], 0);
1284 atomic_set(&tcon
->stats
.smb2_stats
.smb2_com_failed
[i
], 0);
1289 smb2_dump_share_caps(struct seq_file
*m
, struct cifs_tcon
*tcon
)
1291 seq_puts(m
, "\n\tShare Capabilities:");
1292 if (tcon
->capabilities
& SMB2_SHARE_CAP_DFS
)
1293 seq_puts(m
, " DFS,");
1294 if (tcon
->capabilities
& SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY
)
1295 seq_puts(m
, " CONTINUOUS AVAILABILITY,");
1296 if (tcon
->capabilities
& SMB2_SHARE_CAP_SCALEOUT
)
1297 seq_puts(m
, " SCALEOUT,");
1298 if (tcon
->capabilities
& SMB2_SHARE_CAP_CLUSTER
)
1299 seq_puts(m
, " CLUSTER,");
1300 if (tcon
->capabilities
& SMB2_SHARE_CAP_ASYMMETRIC
)
1301 seq_puts(m
, " ASYMMETRIC,");
1302 if (tcon
->capabilities
== 0)
1303 seq_puts(m
, " None");
1304 if (tcon
->ss_flags
& SSINFO_FLAGS_ALIGNED_DEVICE
)
1305 seq_puts(m
, " Aligned,");
1306 if (tcon
->ss_flags
& SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE
)
1307 seq_puts(m
, " Partition Aligned,");
1308 if (tcon
->ss_flags
& SSINFO_FLAGS_NO_SEEK_PENALTY
)
1309 seq_puts(m
, " SSD,");
1310 if (tcon
->ss_flags
& SSINFO_FLAGS_TRIM_ENABLED
)
1311 seq_puts(m
, " TRIM-support,");
1313 seq_printf(m
, "\tShare Flags: 0x%x", tcon
->share_flags
);
1314 seq_printf(m
, "\n\ttid: 0x%x", tcon
->tid
);
1315 if (tcon
->perf_sector_size
)
1316 seq_printf(m
, "\tOptimal sector size: 0x%x",
1317 tcon
->perf_sector_size
);
1318 seq_printf(m
, "\tMaximal Access: 0x%x", tcon
->maximal_access
);
1322 smb2_print_stats(struct seq_file
*m
, struct cifs_tcon
*tcon
)
1324 atomic_t
*sent
= tcon
->stats
.smb2_stats
.smb2_com_sent
;
1325 atomic_t
*failed
= tcon
->stats
.smb2_stats
.smb2_com_failed
;
1328 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1329 * totals (requests sent) since those SMBs are per-session not per tcon
1331 seq_printf(m
, "\nBytes read: %llu Bytes written: %llu",
1332 (long long)(tcon
->bytes_read
),
1333 (long long)(tcon
->bytes_written
));
1334 seq_printf(m
, "\nOpen files: %d total (local), %d open on server",
1335 atomic_read(&tcon
->num_local_opens
),
1336 atomic_read(&tcon
->num_remote_opens
));
1337 seq_printf(m
, "\nTreeConnects: %d total %d failed",
1338 atomic_read(&sent
[SMB2_TREE_CONNECT_HE
]),
1339 atomic_read(&failed
[SMB2_TREE_CONNECT_HE
]));
1340 seq_printf(m
, "\nTreeDisconnects: %d total %d failed",
1341 atomic_read(&sent
[SMB2_TREE_DISCONNECT_HE
]),
1342 atomic_read(&failed
[SMB2_TREE_DISCONNECT_HE
]));
1343 seq_printf(m
, "\nCreates: %d total %d failed",
1344 atomic_read(&sent
[SMB2_CREATE_HE
]),
1345 atomic_read(&failed
[SMB2_CREATE_HE
]));
1346 seq_printf(m
, "\nCloses: %d total %d failed",
1347 atomic_read(&sent
[SMB2_CLOSE_HE
]),
1348 atomic_read(&failed
[SMB2_CLOSE_HE
]));
1349 seq_printf(m
, "\nFlushes: %d total %d failed",
1350 atomic_read(&sent
[SMB2_FLUSH_HE
]),
1351 atomic_read(&failed
[SMB2_FLUSH_HE
]));
1352 seq_printf(m
, "\nReads: %d total %d failed",
1353 atomic_read(&sent
[SMB2_READ_HE
]),
1354 atomic_read(&failed
[SMB2_READ_HE
]));
1355 seq_printf(m
, "\nWrites: %d total %d failed",
1356 atomic_read(&sent
[SMB2_WRITE_HE
]),
1357 atomic_read(&failed
[SMB2_WRITE_HE
]));
1358 seq_printf(m
, "\nLocks: %d total %d failed",
1359 atomic_read(&sent
[SMB2_LOCK_HE
]),
1360 atomic_read(&failed
[SMB2_LOCK_HE
]));
1361 seq_printf(m
, "\nIOCTLs: %d total %d failed",
1362 atomic_read(&sent
[SMB2_IOCTL_HE
]),
1363 atomic_read(&failed
[SMB2_IOCTL_HE
]));
1364 seq_printf(m
, "\nQueryDirectories: %d total %d failed",
1365 atomic_read(&sent
[SMB2_QUERY_DIRECTORY_HE
]),
1366 atomic_read(&failed
[SMB2_QUERY_DIRECTORY_HE
]));
1367 seq_printf(m
, "\nChangeNotifies: %d total %d failed",
1368 atomic_read(&sent
[SMB2_CHANGE_NOTIFY_HE
]),
1369 atomic_read(&failed
[SMB2_CHANGE_NOTIFY_HE
]));
1370 seq_printf(m
, "\nQueryInfos: %d total %d failed",
1371 atomic_read(&sent
[SMB2_QUERY_INFO_HE
]),
1372 atomic_read(&failed
[SMB2_QUERY_INFO_HE
]));
1373 seq_printf(m
, "\nSetInfos: %d total %d failed",
1374 atomic_read(&sent
[SMB2_SET_INFO_HE
]),
1375 atomic_read(&failed
[SMB2_SET_INFO_HE
]));
1376 seq_printf(m
, "\nOplockBreaks: %d sent %d failed",
1377 atomic_read(&sent
[SMB2_OPLOCK_BREAK_HE
]),
1378 atomic_read(&failed
[SMB2_OPLOCK_BREAK_HE
]));
1382 smb2_set_fid(struct cifsFileInfo
*cfile
, struct cifs_fid
*fid
, __u32 oplock
)
1384 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1385 struct TCP_Server_Info
*server
= tlink_tcon(cfile
->tlink
)->ses
->server
;
1387 cfile
->fid
.persistent_fid
= fid
->persistent_fid
;
1388 cfile
->fid
.volatile_fid
= fid
->volatile_fid
;
1389 cfile
->fid
.access
= fid
->access
;
1390 #ifdef CONFIG_CIFS_DEBUG2
1391 cfile
->fid
.mid
= fid
->mid
;
1392 #endif /* CIFS_DEBUG2 */
1393 server
->ops
->set_oplock_level(cinode
, oplock
, fid
->epoch
,
1395 cinode
->can_cache_brlcks
= CIFS_CACHE_WRITE(cinode
);
1396 memcpy(cfile
->fid
.create_guid
, fid
->create_guid
, 16);
1400 smb2_close_file(const unsigned int xid
, struct cifs_tcon
*tcon
,
1401 struct cifs_fid
*fid
)
1403 SMB2_close(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
1407 smb2_close_getattr(const unsigned int xid
, struct cifs_tcon
*tcon
,
1408 struct cifsFileInfo
*cfile
)
1410 struct smb2_file_network_open_info file_inf
;
1411 struct inode
*inode
;
1414 rc
= __SMB2_close(xid
, tcon
, cfile
->fid
.persistent_fid
,
1415 cfile
->fid
.volatile_fid
, &file_inf
);
1419 inode
= d_inode(cfile
->dentry
);
1421 spin_lock(&inode
->i_lock
);
1422 CIFS_I(inode
)->time
= jiffies
;
1424 /* Creation time should not need to be updated on close */
1425 if (file_inf
.LastWriteTime
)
1426 inode_set_mtime_to_ts(inode
,
1427 cifs_NTtimeToUnix(file_inf
.LastWriteTime
));
1428 if (file_inf
.ChangeTime
)
1429 inode_set_ctime_to_ts(inode
,
1430 cifs_NTtimeToUnix(file_inf
.ChangeTime
));
1431 if (file_inf
.LastAccessTime
)
1432 inode_set_atime_to_ts(inode
,
1433 cifs_NTtimeToUnix(file_inf
.LastAccessTime
));
1436 * i_blocks is not related to (i_size / i_blksize),
1437 * but instead 512 byte (2**9) size is required for
1438 * calculating num blocks.
1440 if (le64_to_cpu(file_inf
.AllocationSize
) > 4096)
1442 (512 - 1 + le64_to_cpu(file_inf
.AllocationSize
)) >> 9;
1444 /* End of file and Attributes should not have to be updated on close */
1445 spin_unlock(&inode
->i_lock
);
1449 SMB2_request_res_key(const unsigned int xid
, struct cifs_tcon
*tcon
,
1450 u64 persistent_fid
, u64 volatile_fid
,
1451 struct copychunk_ioctl
*pcchunk
)
1454 unsigned int ret_data_len
;
1455 struct resume_key_req
*res_key
;
1457 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
1458 FSCTL_SRV_REQUEST_RESUME_KEY
, NULL
, 0 /* no input */,
1459 CIFSMaxBufSize
, (char **)&res_key
, &ret_data_len
);
1461 if (rc
== -EOPNOTSUPP
) {
1462 pr_warn_once("Server share %s does not support copy range\n", tcon
->tree_name
);
1463 goto req_res_key_exit
;
1465 cifs_tcon_dbg(VFS
, "refcpy ioctl error %d getting resume key\n", rc
);
1466 goto req_res_key_exit
;
1468 if (ret_data_len
< sizeof(struct resume_key_req
)) {
1469 cifs_tcon_dbg(VFS
, "Invalid refcopy resume key length\n");
1471 goto req_res_key_exit
;
1473 memcpy(pcchunk
->SourceKey
, res_key
->ResumeKey
, COPY_CHUNK_RES_KEY_SIZE
);
1481 smb2_ioctl_query_info(const unsigned int xid
,
1482 struct cifs_tcon
*tcon
,
1483 struct cifs_sb_info
*cifs_sb
,
1484 __le16
*path
, int is_dir
,
1487 struct smb2_compound_vars
*vars
;
1488 struct smb_rqst
*rqst
;
1489 struct kvec
*rsp_iov
;
1490 struct cifs_ses
*ses
= tcon
->ses
;
1491 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
1492 char __user
*arg
= (char __user
*)p
;
1493 struct smb_query_info qi
;
1494 struct smb_query_info __user
*pqi
;
1496 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
1497 struct smb2_query_info_rsp
*qi_rsp
= NULL
;
1498 struct smb2_ioctl_rsp
*io_rsp
= NULL
;
1499 void *buffer
= NULL
;
1500 int resp_buftype
[3];
1501 struct cifs_open_parms oparms
;
1502 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1503 struct cifs_fid fid
;
1504 unsigned int size
[2];
1506 int create_options
= is_dir
? CREATE_NOT_FILE
: CREATE_NOT_DIR
;
1507 void (*free_req1_func
)(struct smb_rqst
*r
);
1509 vars
= kzalloc(sizeof(*vars
), GFP_ATOMIC
);
1512 rqst
= &vars
->rqst
[0];
1513 rsp_iov
= &vars
->rsp_iov
[0];
1515 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
1517 if (copy_from_user(&qi
, arg
, sizeof(struct smb_query_info
))) {
1521 if (qi
.output_buffer_length
> 1024) {
1526 if (!ses
|| !server
) {
1531 if (smb3_encryption_required(tcon
))
1532 flags
|= CIFS_TRANSFORM_REQ
;
1534 if (qi
.output_buffer_length
) {
1535 buffer
= memdup_user(arg
+ sizeof(struct smb_query_info
), qi
.output_buffer_length
);
1536 if (IS_ERR(buffer
)) {
1537 rc
= PTR_ERR(buffer
);
1543 rqst
[0].rq_iov
= &vars
->open_iov
[0];
1544 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
1546 oparms
= (struct cifs_open_parms
) {
1548 .disposition
= FILE_OPEN
,
1549 .create_options
= cifs_create_options(cifs_sb
, create_options
),
1553 if (qi
.flags
& PASSTHRU_FSCTL
) {
1554 switch (qi
.info_type
& FSCTL_DEVICE_ACCESS_MASK
) {
1555 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS
:
1556 oparms
.desired_access
= FILE_READ_DATA
| FILE_WRITE_DATA
| FILE_READ_ATTRIBUTES
| SYNCHRONIZE
;
1558 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS
:
1559 oparms
.desired_access
= GENERIC_ALL
;
1561 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS
:
1562 oparms
.desired_access
= GENERIC_READ
;
1564 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS
:
1565 oparms
.desired_access
= GENERIC_WRITE
;
1568 } else if (qi
.flags
& PASSTHRU_SET_INFO
) {
1569 oparms
.desired_access
= GENERIC_WRITE
;
1571 oparms
.desired_access
= FILE_READ_ATTRIBUTES
| READ_CONTROL
;
1574 rc
= SMB2_open_init(tcon
, server
,
1575 &rqst
[0], &oplock
, &oparms
, path
);
1577 goto free_output_buffer
;
1578 smb2_set_next_command(tcon
, &rqst
[0]);
1581 if (qi
.flags
& PASSTHRU_FSCTL
) {
1582 /* Can eventually relax perm check since server enforces too */
1583 if (!capable(CAP_SYS_ADMIN
)) {
1587 rqst
[1].rq_iov
= &vars
->io_iov
[0];
1588 rqst
[1].rq_nvec
= SMB2_IOCTL_IOV_SIZE
;
1590 rc
= SMB2_ioctl_init(tcon
, server
, &rqst
[1], COMPOUND_FID
, COMPOUND_FID
,
1591 qi
.info_type
, buffer
, qi
.output_buffer_length
,
1592 CIFSMaxBufSize
- MAX_SMB2_CREATE_RESPONSE_SIZE
-
1593 MAX_SMB2_CLOSE_RESPONSE_SIZE
);
1594 free_req1_func
= SMB2_ioctl_free
;
1595 } else if (qi
.flags
== PASSTHRU_SET_INFO
) {
1596 /* Can eventually relax perm check since server enforces too */
1597 if (!capable(CAP_SYS_ADMIN
)) {
1601 if (qi
.output_buffer_length
< 8) {
1605 rqst
[1].rq_iov
= vars
->si_iov
;
1606 rqst
[1].rq_nvec
= 1;
1608 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1612 rc
= SMB2_set_info_init(tcon
, server
, &rqst
[1], COMPOUND_FID
, COMPOUND_FID
,
1613 current
->tgid
, FILE_END_OF_FILE_INFORMATION
,
1614 SMB2_O_INFO_FILE
, 0, data
, size
);
1615 free_req1_func
= SMB2_set_info_free
;
1616 } else if (qi
.flags
== PASSTHRU_QUERY_INFO
) {
1617 rqst
[1].rq_iov
= &vars
->qi_iov
;
1618 rqst
[1].rq_nvec
= 1;
1620 rc
= SMB2_query_info_init(tcon
, server
,
1621 &rqst
[1], COMPOUND_FID
,
1622 COMPOUND_FID
, qi
.file_info_class
,
1623 qi
.info_type
, qi
.additional_information
,
1624 qi
.input_buffer_length
,
1625 qi
.output_buffer_length
, buffer
);
1626 free_req1_func
= SMB2_query_info_free
;
1627 } else { /* unknown flags */
1628 cifs_tcon_dbg(VFS
, "Invalid passthru query flags: 0x%x\n",
1635 smb2_set_next_command(tcon
, &rqst
[1]);
1636 smb2_set_related(&rqst
[1]);
1639 rqst
[2].rq_iov
= &vars
->close_iov
;
1640 rqst
[2].rq_nvec
= 1;
1642 rc
= SMB2_close_init(tcon
, server
,
1643 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
1646 smb2_set_related(&rqst
[2]);
1648 rc
= compound_send_recv(xid
, ses
, server
,
1650 resp_buftype
, rsp_iov
);
1654 /* No need to bump num_remote_opens since handle immediately closed */
1655 if (qi
.flags
& PASSTHRU_FSCTL
) {
1656 pqi
= (struct smb_query_info __user
*)arg
;
1657 io_rsp
= (struct smb2_ioctl_rsp
*)rsp_iov
[1].iov_base
;
1658 if (le32_to_cpu(io_rsp
->OutputCount
) < qi
.input_buffer_length
)
1659 qi
.input_buffer_length
= le32_to_cpu(io_rsp
->OutputCount
);
1660 if (qi
.input_buffer_length
> 0 &&
1661 le32_to_cpu(io_rsp
->OutputOffset
) + qi
.input_buffer_length
1662 > rsp_iov
[1].iov_len
) {
1667 if (copy_to_user(&pqi
->input_buffer_length
,
1668 &qi
.input_buffer_length
,
1669 sizeof(qi
.input_buffer_length
))) {
1674 if (copy_to_user((void __user
*)pqi
+ sizeof(struct smb_query_info
),
1675 (const void *)io_rsp
+ le32_to_cpu(io_rsp
->OutputOffset
),
1676 qi
.input_buffer_length
))
1679 pqi
= (struct smb_query_info __user
*)arg
;
1680 qi_rsp
= (struct smb2_query_info_rsp
*)rsp_iov
[1].iov_base
;
1681 if (le32_to_cpu(qi_rsp
->OutputBufferLength
) < qi
.input_buffer_length
)
1682 qi
.input_buffer_length
= le32_to_cpu(qi_rsp
->OutputBufferLength
);
1683 if (copy_to_user(&pqi
->input_buffer_length
,
1684 &qi
.input_buffer_length
,
1685 sizeof(qi
.input_buffer_length
))) {
1690 if (copy_to_user(pqi
+ 1, qi_rsp
->Buffer
,
1691 qi
.input_buffer_length
))
1696 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
1697 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
1698 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
1699 SMB2_close_free(&rqst
[2]);
1701 free_req1_func(&rqst
[1]);
1703 SMB2_open_free(&rqst
[0]);
1712 smb2_copychunk_range(const unsigned int xid
,
1713 struct cifsFileInfo
*srcfile
,
1714 struct cifsFileInfo
*trgtfile
, u64 src_off
,
1715 u64 len
, u64 dest_off
)
1718 unsigned int ret_data_len
;
1719 struct copychunk_ioctl
*pcchunk
;
1720 struct copychunk_ioctl_rsp
*retbuf
= NULL
;
1721 struct cifs_tcon
*tcon
;
1722 int chunks_copied
= 0;
1723 bool chunk_sizes_updated
= false;
1724 ssize_t bytes_written
, total_bytes_written
= 0;
1726 pcchunk
= kmalloc(sizeof(struct copychunk_ioctl
), GFP_KERNEL
);
1727 if (pcchunk
== NULL
)
1730 cifs_dbg(FYI
, "%s: about to call request res key\n", __func__
);
1731 /* Request a key from the server to identify the source of the copy */
1732 rc
= SMB2_request_res_key(xid
, tlink_tcon(srcfile
->tlink
),
1733 srcfile
->fid
.persistent_fid
,
1734 srcfile
->fid
.volatile_fid
, pcchunk
);
1736 /* Note: request_res_key sets res_key null only if rc !=0 */
1740 /* For now array only one chunk long, will make more flexible later */
1741 pcchunk
->ChunkCount
= cpu_to_le32(1);
1742 pcchunk
->Reserved
= 0;
1743 pcchunk
->Reserved2
= 0;
1745 tcon
= tlink_tcon(trgtfile
->tlink
);
1748 pcchunk
->SourceOffset
= cpu_to_le64(src_off
);
1749 pcchunk
->TargetOffset
= cpu_to_le64(dest_off
);
1751 cpu_to_le32(min_t(u64
, len
, tcon
->max_bytes_chunk
));
1753 /* Request server copy to target from src identified by key */
1756 rc
= SMB2_ioctl(xid
, tcon
, trgtfile
->fid
.persistent_fid
,
1757 trgtfile
->fid
.volatile_fid
, FSCTL_SRV_COPYCHUNK_WRITE
,
1758 (char *)pcchunk
, sizeof(struct copychunk_ioctl
),
1759 CIFSMaxBufSize
, (char **)&retbuf
, &ret_data_len
);
1762 sizeof(struct copychunk_ioctl_rsp
)) {
1763 cifs_tcon_dbg(VFS
, "Invalid cchunk response size\n");
1767 if (retbuf
->TotalBytesWritten
== 0) {
1768 cifs_dbg(FYI
, "no bytes copied\n");
1773 * Check if server claimed to write more than we asked
1775 if (le32_to_cpu(retbuf
->TotalBytesWritten
) >
1776 le32_to_cpu(pcchunk
->Length
)) {
1777 cifs_tcon_dbg(VFS
, "Invalid copy chunk response\n");
1781 if (le32_to_cpu(retbuf
->ChunksWritten
) != 1) {
1782 cifs_tcon_dbg(VFS
, "Invalid num chunks written\n");
1788 bytes_written
= le32_to_cpu(retbuf
->TotalBytesWritten
);
1789 src_off
+= bytes_written
;
1790 dest_off
+= bytes_written
;
1791 len
-= bytes_written
;
1792 total_bytes_written
+= bytes_written
;
1794 cifs_dbg(FYI
, "Chunks %d PartialChunk %d Total %zu\n",
1795 le32_to_cpu(retbuf
->ChunksWritten
),
1796 le32_to_cpu(retbuf
->ChunkBytesWritten
),
1798 } else if (rc
== -EINVAL
) {
1799 if (ret_data_len
!= sizeof(struct copychunk_ioctl_rsp
))
1802 cifs_dbg(FYI
, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1803 le32_to_cpu(retbuf
->ChunksWritten
),
1804 le32_to_cpu(retbuf
->ChunkBytesWritten
),
1805 le32_to_cpu(retbuf
->TotalBytesWritten
));
1808 * Check if this is the first request using these sizes,
1809 * (ie check if copy succeed once with original sizes
1810 * and check if the server gave us different sizes after
1811 * we already updated max sizes on previous request).
1812 * if not then why is the server returning an error now
1814 if ((chunks_copied
!= 0) || chunk_sizes_updated
)
1817 /* Check that server is not asking us to grow size */
1818 if (le32_to_cpu(retbuf
->ChunkBytesWritten
) <
1819 tcon
->max_bytes_chunk
)
1820 tcon
->max_bytes_chunk
=
1821 le32_to_cpu(retbuf
->ChunkBytesWritten
);
1823 goto cchunk_out
; /* server gave us bogus size */
1825 /* No need to change MaxChunks since already set to 1 */
1826 chunk_sizes_updated
= true;
1837 return total_bytes_written
;
1841 smb2_flush_file(const unsigned int xid
, struct cifs_tcon
*tcon
,
1842 struct cifs_fid
*fid
)
1844 return SMB2_flush(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
1848 smb2_read_data_offset(char *buf
)
1850 struct smb2_read_rsp
*rsp
= (struct smb2_read_rsp
*)buf
;
1852 return rsp
->DataOffset
;
1856 smb2_read_data_length(char *buf
, bool in_remaining
)
1858 struct smb2_read_rsp
*rsp
= (struct smb2_read_rsp
*)buf
;
1861 return le32_to_cpu(rsp
->DataRemaining
);
1863 return le32_to_cpu(rsp
->DataLength
);
1868 smb2_sync_read(const unsigned int xid
, struct cifs_fid
*pfid
,
1869 struct cifs_io_parms
*parms
, unsigned int *bytes_read
,
1870 char **buf
, int *buf_type
)
1872 parms
->persistent_fid
= pfid
->persistent_fid
;
1873 parms
->volatile_fid
= pfid
->volatile_fid
;
1874 return SMB2_read(xid
, parms
, bytes_read
, buf
, buf_type
);
1878 smb2_sync_write(const unsigned int xid
, struct cifs_fid
*pfid
,
1879 struct cifs_io_parms
*parms
, unsigned int *written
,
1880 struct kvec
*iov
, unsigned long nr_segs
)
1883 parms
->persistent_fid
= pfid
->persistent_fid
;
1884 parms
->volatile_fid
= pfid
->volatile_fid
;
1885 return SMB2_write(xid
, parms
, written
, iov
, nr_segs
);
1888 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1889 static bool smb2_set_sparse(const unsigned int xid
, struct cifs_tcon
*tcon
,
1890 struct cifsFileInfo
*cfile
, struct inode
*inode
, __u8 setsparse
)
1892 struct cifsInodeInfo
*cifsi
;
1895 cifsi
= CIFS_I(inode
);
1897 /* if file already sparse don't bother setting sparse again */
1898 if ((cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
) && setsparse
)
1899 return true; /* already sparse */
1901 if (!(cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
) && !setsparse
)
1902 return true; /* already not sparse */
1905 * Can't check for sparse support on share the usual way via the
1906 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1907 * since Samba server doesn't set the flag on the share, yet
1908 * supports the set sparse FSCTL and returns sparse correctly
1909 * in the file attributes. If we fail setting sparse though we
1910 * mark that server does not support sparse files for this share
1911 * to avoid repeatedly sending the unsupported fsctl to server
1912 * if the file is repeatedly extended.
1914 if (tcon
->broken_sparse_sup
)
1917 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
1918 cfile
->fid
.volatile_fid
, FSCTL_SET_SPARSE
,
1919 &setsparse
, 1, CIFSMaxBufSize
, NULL
, NULL
);
1921 tcon
->broken_sparse_sup
= true;
1922 cifs_dbg(FYI
, "set sparse rc = %d\n", rc
);
1927 cifsi
->cifsAttrs
|= FILE_ATTRIBUTE_SPARSE_FILE
;
1929 cifsi
->cifsAttrs
&= (~FILE_ATTRIBUTE_SPARSE_FILE
);
1935 smb2_set_file_size(const unsigned int xid
, struct cifs_tcon
*tcon
,
1936 struct cifsFileInfo
*cfile
, __u64 size
, bool set_alloc
)
1938 __le64 eof
= cpu_to_le64(size
);
1939 struct inode
*inode
;
1942 * If extending file more than one page make sparse. Many Linux fs
1943 * make files sparse by default when extending via ftruncate
1945 inode
= d_inode(cfile
->dentry
);
1947 if (!set_alloc
&& (size
> inode
->i_size
+ 8192)) {
1948 __u8 set_sparse
= 1;
1950 /* whether set sparse succeeds or not, extend the file */
1951 smb2_set_sparse(xid
, tcon
, cfile
, inode
, set_sparse
);
1954 return SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
1955 cfile
->fid
.volatile_fid
, cfile
->pid
, &eof
);
1959 smb2_duplicate_extents(const unsigned int xid
,
1960 struct cifsFileInfo
*srcfile
,
1961 struct cifsFileInfo
*trgtfile
, u64 src_off
,
1962 u64 len
, u64 dest_off
)
1965 unsigned int ret_data_len
;
1966 struct inode
*inode
;
1967 struct duplicate_extents_to_file dup_ext_buf
;
1968 struct cifs_tcon
*tcon
= tlink_tcon(trgtfile
->tlink
);
1970 /* server fileays advertise duplicate extent support with this flag */
1971 if ((le32_to_cpu(tcon
->fsAttrInfo
.Attributes
) &
1972 FILE_SUPPORTS_BLOCK_REFCOUNTING
) == 0)
1975 dup_ext_buf
.VolatileFileHandle
= srcfile
->fid
.volatile_fid
;
1976 dup_ext_buf
.PersistentFileHandle
= srcfile
->fid
.persistent_fid
;
1977 dup_ext_buf
.SourceFileOffset
= cpu_to_le64(src_off
);
1978 dup_ext_buf
.TargetFileOffset
= cpu_to_le64(dest_off
);
1979 dup_ext_buf
.ByteCount
= cpu_to_le64(len
);
1980 cifs_dbg(FYI
, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1981 src_off
, dest_off
, len
);
1983 inode
= d_inode(trgtfile
->dentry
);
1984 if (inode
->i_size
< dest_off
+ len
) {
1985 rc
= smb2_set_file_size(xid
, tcon
, trgtfile
, dest_off
+ len
, false);
1987 goto duplicate_extents_out
;
1990 * Although also could set plausible allocation size (i_blocks)
1991 * here in addition to setting the file size, in reflink
1992 * it is likely that the target file is sparse. Its allocation
1993 * size will be queried on next revalidate, but it is important
1994 * to make sure that file's cached size is updated immediately
1996 cifs_setsize(inode
, dest_off
+ len
);
1998 rc
= SMB2_ioctl(xid
, tcon
, trgtfile
->fid
.persistent_fid
,
1999 trgtfile
->fid
.volatile_fid
,
2000 FSCTL_DUPLICATE_EXTENTS_TO_FILE
,
2001 (char *)&dup_ext_buf
,
2002 sizeof(struct duplicate_extents_to_file
),
2003 CIFSMaxBufSize
, NULL
,
2006 if (ret_data_len
> 0)
2007 cifs_dbg(FYI
, "Non-zero response length in duplicate extents\n");
2009 duplicate_extents_out
:
2014 smb2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
2015 struct cifsFileInfo
*cfile
)
2017 return SMB2_set_compression(xid
, tcon
, cfile
->fid
.persistent_fid
,
2018 cfile
->fid
.volatile_fid
);
2022 smb3_set_integrity(const unsigned int xid
, struct cifs_tcon
*tcon
,
2023 struct cifsFileInfo
*cfile
)
2025 struct fsctl_set_integrity_information_req integr_info
;
2026 unsigned int ret_data_len
;
2028 integr_info
.ChecksumAlgorithm
= cpu_to_le16(CHECKSUM_TYPE_UNCHANGED
);
2029 integr_info
.Flags
= 0;
2030 integr_info
.Reserved
= 0;
2032 return SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
2033 cfile
->fid
.volatile_fid
,
2034 FSCTL_SET_INTEGRITY_INFORMATION
,
2035 (char *)&integr_info
,
2036 sizeof(struct fsctl_set_integrity_information_req
),
2037 CIFSMaxBufSize
, NULL
,
2042 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2043 #define GMT_TOKEN_SIZE 50
2045 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2048 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2049 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2052 smb3_enum_snapshots(const unsigned int xid
, struct cifs_tcon
*tcon
,
2053 struct cifsFileInfo
*cfile
, void __user
*ioc_buf
)
2055 char *retbuf
= NULL
;
2056 unsigned int ret_data_len
= 0;
2058 u32 max_response_size
;
2059 struct smb_snapshot_array snapshot_in
;
2062 * On the first query to enumerate the list of snapshots available
2063 * for this volume the buffer begins with 0 (number of snapshots
2064 * which can be returned is zero since at that point we do not know
2065 * how big the buffer needs to be). On the second query,
2066 * it (ret_data_len) is set to number of snapshots so we can
2067 * know to set the maximum response size larger (see below).
2069 if (get_user(ret_data_len
, (unsigned int __user
*)ioc_buf
))
2073 * Note that for snapshot queries that servers like Azure expect that
2074 * the first query be minimal size (and just used to get the number/size
2075 * of previous versions) so response size must be specified as EXACTLY
2076 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2079 if (ret_data_len
== 0)
2080 max_response_size
= MIN_SNAPSHOT_ARRAY_SIZE
;
2082 max_response_size
= CIFSMaxBufSize
;
2084 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
2085 cfile
->fid
.volatile_fid
,
2086 FSCTL_SRV_ENUMERATE_SNAPSHOTS
,
2087 NULL
, 0 /* no input data */, max_response_size
,
2090 cifs_dbg(FYI
, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2095 if (ret_data_len
&& (ioc_buf
!= NULL
) && (retbuf
!= NULL
)) {
2097 if (copy_from_user(&snapshot_in
, ioc_buf
,
2098 sizeof(struct smb_snapshot_array
))) {
2105 * Check for min size, ie not large enough to fit even one GMT
2106 * token (snapshot). On the first ioctl some users may pass in
2107 * smaller size (or zero) to simply get the size of the array
2108 * so the user space caller can allocate sufficient memory
2109 * and retry the ioctl again with larger array size sufficient
2110 * to hold all of the snapshot GMT tokens on the second try.
2112 if (snapshot_in
.snapshot_array_size
< GMT_TOKEN_SIZE
)
2113 ret_data_len
= sizeof(struct smb_snapshot_array
);
2116 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2117 * the snapshot array (of 50 byte GMT tokens) each
2118 * representing an available previous version of the data
2120 if (ret_data_len
> (snapshot_in
.snapshot_array_size
+
2121 sizeof(struct smb_snapshot_array
)))
2122 ret_data_len
= snapshot_in
.snapshot_array_size
+
2123 sizeof(struct smb_snapshot_array
);
2125 if (copy_to_user(ioc_buf
, retbuf
, ret_data_len
))
2136 smb3_notify(const unsigned int xid
, struct file
*pfile
,
2137 void __user
*ioc_buf
, bool return_changes
)
2139 struct smb3_notify_info notify
;
2140 struct smb3_notify_info __user
*pnotify_buf
;
2141 struct dentry
*dentry
= pfile
->f_path
.dentry
;
2142 struct inode
*inode
= file_inode(pfile
);
2143 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2144 struct cifs_open_parms oparms
;
2145 struct cifs_fid fid
;
2146 struct cifs_tcon
*tcon
;
2147 const unsigned char *path
;
2148 char *returned_ioctl_info
= NULL
;
2149 void *page
= alloc_dentry_path();
2150 __le16
*utf16_path
= NULL
;
2151 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2155 path
= build_path_from_dentry(dentry
, page
);
2161 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
2162 if (utf16_path
== NULL
) {
2167 if (return_changes
) {
2168 if (copy_from_user(¬ify
, ioc_buf
, sizeof(struct smb3_notify_info
))) {
2173 if (copy_from_user(¬ify
, ioc_buf
, sizeof(struct smb3_notify
))) {
2177 notify
.data_len
= 0;
2180 tcon
= cifs_sb_master_tcon(cifs_sb
);
2181 oparms
= (struct cifs_open_parms
) {
2184 .desired_access
= FILE_READ_ATTRIBUTES
| FILE_READ_DATA
,
2185 .disposition
= FILE_OPEN
,
2186 .create_options
= cifs_create_options(cifs_sb
, 0),
2190 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
, NULL
,
2195 rc
= SMB2_change_notify(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
2196 notify
.watch_tree
, notify
.completion_filter
,
2197 notify
.data_len
, &returned_ioctl_info
, &ret_len
);
2199 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
2201 cifs_dbg(FYI
, "change notify for path %s rc %d\n", path
, rc
);
2202 if (return_changes
&& (ret_len
> 0) && (notify
.data_len
> 0)) {
2203 if (ret_len
> notify
.data_len
)
2204 ret_len
= notify
.data_len
;
2205 pnotify_buf
= (struct smb3_notify_info __user
*)ioc_buf
;
2206 if (copy_to_user(pnotify_buf
->notify_data
, returned_ioctl_info
, ret_len
))
2208 else if (copy_to_user(&pnotify_buf
->data_len
, &ret_len
, sizeof(ret_len
)))
2211 kfree(returned_ioctl_info
);
2213 free_dentry_path(page
);
2219 smb2_query_dir_first(const unsigned int xid
, struct cifs_tcon
*tcon
,
2220 const char *path
, struct cifs_sb_info
*cifs_sb
,
2221 struct cifs_fid
*fid
, __u16 search_flags
,
2222 struct cifs_search_info
*srch_inf
)
2225 struct smb_rqst rqst
[2];
2226 struct kvec rsp_iov
[2];
2227 int resp_buftype
[2];
2228 struct kvec open_iov
[SMB2_CREATE_IOV_SIZE
];
2229 struct kvec qd_iov
[SMB2_QUERY_DIRECTORY_IOV_SIZE
];
2231 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2232 struct cifs_open_parms oparms
;
2233 struct smb2_query_directory_rsp
*qd_rsp
= NULL
;
2234 struct smb2_create_rsp
*op_rsp
= NULL
;
2235 struct TCP_Server_Info
*server
= cifs_pick_channel(tcon
->ses
);
2236 int retry_count
= 0;
2238 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
2242 if (smb3_encryption_required(tcon
))
2243 flags
|= CIFS_TRANSFORM_REQ
;
2245 memset(rqst
, 0, sizeof(rqst
));
2246 resp_buftype
[0] = resp_buftype
[1] = CIFS_NO_BUFFER
;
2247 memset(rsp_iov
, 0, sizeof(rsp_iov
));
2250 memset(&open_iov
, 0, sizeof(open_iov
));
2251 rqst
[0].rq_iov
= open_iov
;
2252 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
2254 oparms
= (struct cifs_open_parms
) {
2257 .desired_access
= FILE_READ_ATTRIBUTES
| FILE_READ_DATA
,
2258 .disposition
= FILE_OPEN
,
2259 .create_options
= cifs_create_options(cifs_sb
, 0),
2263 rc
= SMB2_open_init(tcon
, server
,
2264 &rqst
[0], &oplock
, &oparms
, utf16_path
);
2267 smb2_set_next_command(tcon
, &rqst
[0]);
2269 /* Query directory */
2270 srch_inf
->entries_in_buffer
= 0;
2271 srch_inf
->index_of_last_entry
= 2;
2273 memset(&qd_iov
, 0, sizeof(qd_iov
));
2274 rqst
[1].rq_iov
= qd_iov
;
2275 rqst
[1].rq_nvec
= SMB2_QUERY_DIRECTORY_IOV_SIZE
;
2277 rc
= SMB2_query_directory_init(xid
, tcon
, server
,
2279 COMPOUND_FID
, COMPOUND_FID
,
2280 0, srch_inf
->info_level
);
2284 smb2_set_related(&rqst
[1]);
2287 rc
= compound_send_recv(xid
, tcon
->ses
, server
,
2289 resp_buftype
, rsp_iov
);
2291 if (rc
== -EAGAIN
&& retry_count
++ < 10)
2294 /* If the open failed there is nothing to do */
2295 op_rsp
= (struct smb2_create_rsp
*)rsp_iov
[0].iov_base
;
2296 if (op_rsp
== NULL
|| op_rsp
->hdr
.Status
!= STATUS_SUCCESS
) {
2297 cifs_dbg(FYI
, "query_dir_first: open failed rc=%d\n", rc
);
2300 fid
->persistent_fid
= op_rsp
->PersistentFileId
;
2301 fid
->volatile_fid
= op_rsp
->VolatileFileId
;
2303 /* Anything else than ENODATA means a genuine error */
2304 if (rc
&& rc
!= -ENODATA
) {
2305 SMB2_close(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
2306 cifs_dbg(FYI
, "query_dir_first: query directory failed rc=%d\n", rc
);
2307 trace_smb3_query_dir_err(xid
, fid
->persistent_fid
,
2308 tcon
->tid
, tcon
->ses
->Suid
, 0, 0, rc
);
2312 atomic_inc(&tcon
->num_remote_opens
);
2314 qd_rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
[1].iov_base
;
2315 if (qd_rsp
->hdr
.Status
== STATUS_NO_MORE_FILES
) {
2316 trace_smb3_query_dir_done(xid
, fid
->persistent_fid
,
2317 tcon
->tid
, tcon
->ses
->Suid
, 0, 0);
2318 srch_inf
->endOfSearch
= true;
2323 rc
= smb2_parse_query_directory(tcon
, &rsp_iov
[1], resp_buftype
[1],
2326 trace_smb3_query_dir_err(xid
, fid
->persistent_fid
, tcon
->tid
,
2327 tcon
->ses
->Suid
, 0, 0, rc
);
2330 resp_buftype
[1] = CIFS_NO_BUFFER
;
2332 trace_smb3_query_dir_done(xid
, fid
->persistent_fid
, tcon
->tid
,
2333 tcon
->ses
->Suid
, 0, srch_inf
->entries_in_buffer
);
2337 SMB2_open_free(&rqst
[0]);
2338 SMB2_query_directory_free(&rqst
[1]);
2339 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
2340 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
2345 smb2_query_dir_next(const unsigned int xid
, struct cifs_tcon
*tcon
,
2346 struct cifs_fid
*fid
, __u16 search_flags
,
2347 struct cifs_search_info
*srch_inf
)
2349 return SMB2_query_directory(xid
, tcon
, fid
->persistent_fid
,
2350 fid
->volatile_fid
, 0, srch_inf
);
2354 smb2_close_dir(const unsigned int xid
, struct cifs_tcon
*tcon
,
2355 struct cifs_fid
*fid
)
2357 return SMB2_close(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
2361 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2362 * the number of credits and return true. Otherwise - return false.
2365 smb2_is_status_pending(char *buf
, struct TCP_Server_Info
*server
)
2367 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2368 int scredits
, in_flight
;
2370 if (shdr
->Status
!= STATUS_PENDING
)
2373 if (shdr
->CreditRequest
) {
2374 spin_lock(&server
->req_lock
);
2375 server
->credits
+= le16_to_cpu(shdr
->CreditRequest
);
2376 scredits
= server
->credits
;
2377 in_flight
= server
->in_flight
;
2378 spin_unlock(&server
->req_lock
);
2379 wake_up(&server
->request_q
);
2381 trace_smb3_pend_credits(server
->CurrentMid
,
2382 server
->conn_id
, server
->hostname
, scredits
,
2383 le16_to_cpu(shdr
->CreditRequest
), in_flight
);
2384 cifs_dbg(FYI
, "%s: status pending add %u credits total=%d\n",
2385 __func__
, le16_to_cpu(shdr
->CreditRequest
), scredits
);
2392 smb2_is_session_expired(char *buf
)
2394 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2396 if (shdr
->Status
!= STATUS_NETWORK_SESSION_EXPIRED
&&
2397 shdr
->Status
!= STATUS_USER_SESSION_DELETED
)
2400 trace_smb3_ses_expired(le32_to_cpu(shdr
->Id
.SyncId
.TreeId
),
2401 le64_to_cpu(shdr
->SessionId
),
2402 le16_to_cpu(shdr
->Command
),
2403 le64_to_cpu(shdr
->MessageId
));
2404 cifs_dbg(FYI
, "Session expired or deleted\n");
2410 smb2_is_status_io_timeout(char *buf
)
2412 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2414 if (shdr
->Status
== STATUS_IO_TIMEOUT
)
2421 smb2_is_network_name_deleted(char *buf
, struct TCP_Server_Info
*server
)
2423 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2424 struct TCP_Server_Info
*pserver
;
2425 struct cifs_ses
*ses
;
2426 struct cifs_tcon
*tcon
;
2428 if (shdr
->Status
!= STATUS_NETWORK_NAME_DELETED
)
2431 /* If server is a channel, select the primary channel */
2432 pserver
= SERVER_IS_CHAN(server
) ? server
->primary_server
: server
;
2434 spin_lock(&cifs_tcp_ses_lock
);
2435 list_for_each_entry(ses
, &pserver
->smb_ses_list
, smb_ses_list
) {
2436 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
2437 if (tcon
->tid
== le32_to_cpu(shdr
->Id
.SyncId
.TreeId
)) {
2438 spin_lock(&tcon
->tc_lock
);
2439 tcon
->need_reconnect
= true;
2440 spin_unlock(&tcon
->tc_lock
);
2441 spin_unlock(&cifs_tcp_ses_lock
);
2442 pr_warn_once("Server share %s deleted.\n",
2448 spin_unlock(&cifs_tcp_ses_lock
);
2454 smb2_oplock_response(struct cifs_tcon
*tcon
, __u64 persistent_fid
,
2455 __u64 volatile_fid
, __u16 net_fid
, struct cifsInodeInfo
*cinode
)
2457 if (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
)
2458 return SMB2_lease_break(0, tcon
, cinode
->lease_key
,
2459 smb2_get_lease_state(cinode
));
2461 return SMB2_oplock_break(0, tcon
, persistent_fid
, volatile_fid
,
2462 CIFS_CACHE_READ(cinode
) ? 1 : 0);
2466 smb2_set_related(struct smb_rqst
*rqst
)
2468 struct smb2_hdr
*shdr
;
2470 shdr
= (struct smb2_hdr
*)(rqst
->rq_iov
[0].iov_base
);
2472 cifs_dbg(FYI
, "shdr NULL in smb2_set_related\n");
2475 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
2478 char smb2_padding
[7] = {0, 0, 0, 0, 0, 0, 0};
2481 smb2_set_next_command(struct cifs_tcon
*tcon
, struct smb_rqst
*rqst
)
2483 struct smb2_hdr
*shdr
;
2484 struct cifs_ses
*ses
= tcon
->ses
;
2485 struct TCP_Server_Info
*server
= ses
->server
;
2486 unsigned long len
= smb_rqst_len(server
, rqst
);
2489 shdr
= (struct smb2_hdr
*)(rqst
->rq_iov
[0].iov_base
);
2491 cifs_dbg(FYI
, "shdr NULL in smb2_set_next_command\n");
2495 /* SMB headers in a compound are 8 byte aligned. */
2497 /* No padding needed */
2501 num_padding
= 8 - (len
& 7);
2502 if (!smb3_encryption_required(tcon
)) {
2504 * If we do not have encryption then we can just add an extra
2505 * iov for the padding.
2507 rqst
->rq_iov
[rqst
->rq_nvec
].iov_base
= smb2_padding
;
2508 rqst
->rq_iov
[rqst
->rq_nvec
].iov_len
= num_padding
;
2513 * We can not add a small padding iov for the encryption case
2514 * because the encryption framework can not handle the padding
2516 * We have to flatten this into a single buffer and add
2517 * the padding to it.
2519 for (i
= 1; i
< rqst
->rq_nvec
; i
++) {
2520 memcpy(rqst
->rq_iov
[0].iov_base
+
2521 rqst
->rq_iov
[0].iov_len
,
2522 rqst
->rq_iov
[i
].iov_base
,
2523 rqst
->rq_iov
[i
].iov_len
);
2524 rqst
->rq_iov
[0].iov_len
+= rqst
->rq_iov
[i
].iov_len
;
2526 memset(rqst
->rq_iov
[0].iov_base
+ rqst
->rq_iov
[0].iov_len
,
2528 rqst
->rq_iov
[0].iov_len
+= num_padding
;
2534 shdr
->NextCommand
= cpu_to_le32(len
);
2538 * Passes the query info response back to the caller on success.
2539 * Caller need to free this with free_rsp_buf().
2542 smb2_query_info_compound(const unsigned int xid
, struct cifs_tcon
*tcon
,
2543 const char *path
, u32 desired_access
,
2544 u32
class, u32 type
, u32 output_len
,
2545 struct kvec
*rsp
, int *buftype
,
2546 struct cifs_sb_info
*cifs_sb
)
2548 struct smb2_compound_vars
*vars
;
2549 struct cifs_ses
*ses
= tcon
->ses
;
2550 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
2551 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
2552 struct smb_rqst
*rqst
;
2553 int resp_buftype
[3];
2554 struct kvec
*rsp_iov
;
2555 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2556 struct cifs_open_parms oparms
;
2557 struct cifs_fid fid
;
2560 struct cached_fid
*cfid
= NULL
;
2564 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
2568 if (smb3_encryption_required(tcon
))
2569 flags
|= CIFS_TRANSFORM_REQ
;
2571 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
2572 vars
= kzalloc(sizeof(*vars
), GFP_KERNEL
);
2578 rsp_iov
= vars
->rsp_iov
;
2581 * We can only call this for things we know are directories.
2583 if (!strcmp(path
, ""))
2584 open_cached_dir(xid
, tcon
, path
, cifs_sb
, false,
2585 &cfid
); /* cfid null if open dir failed */
2587 rqst
[0].rq_iov
= vars
->open_iov
;
2588 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
2590 oparms
= (struct cifs_open_parms
) {
2593 .desired_access
= desired_access
,
2594 .disposition
= FILE_OPEN
,
2595 .create_options
= cifs_create_options(cifs_sb
, 0),
2599 rc
= SMB2_open_init(tcon
, server
,
2600 &rqst
[0], &oplock
, &oparms
, utf16_path
);
2603 smb2_set_next_command(tcon
, &rqst
[0]);
2605 rqst
[1].rq_iov
= &vars
->qi_iov
;
2606 rqst
[1].rq_nvec
= 1;
2609 rc
= SMB2_query_info_init(tcon
, server
,
2611 cfid
->fid
.persistent_fid
,
2612 cfid
->fid
.volatile_fid
,
2617 rc
= SMB2_query_info_init(tcon
, server
,
2628 smb2_set_next_command(tcon
, &rqst
[1]);
2629 smb2_set_related(&rqst
[1]);
2632 rqst
[2].rq_iov
= &vars
->close_iov
;
2633 rqst
[2].rq_nvec
= 1;
2635 rc
= SMB2_close_init(tcon
, server
,
2636 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
2639 smb2_set_related(&rqst
[2]);
2642 rc
= compound_send_recv(xid
, ses
, server
,
2644 &resp_buftype
[1], &rsp_iov
[1]);
2646 rc
= compound_send_recv(xid
, ses
, server
,
2648 resp_buftype
, rsp_iov
);
2651 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
2652 if (rc
== -EREMCHG
) {
2653 tcon
->need_reconnect
= true;
2654 pr_warn_once("server share %s deleted\n",
2660 *buftype
= resp_buftype
[1];
2663 SMB2_open_free(&rqst
[0]);
2664 SMB2_query_info_free(&rqst
[1]);
2665 SMB2_close_free(&rqst
[2]);
2666 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
2667 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
2669 close_cached_dir(cfid
);
2677 smb2_queryfs(const unsigned int xid
, struct cifs_tcon
*tcon
,
2678 struct cifs_sb_info
*cifs_sb
, struct kstatfs
*buf
)
2680 struct smb2_query_info_rsp
*rsp
;
2681 struct smb2_fs_full_size_info
*info
= NULL
;
2682 struct kvec rsp_iov
= {NULL
, 0};
2683 int buftype
= CIFS_NO_BUFFER
;
2687 rc
= smb2_query_info_compound(xid
, tcon
, "",
2688 FILE_READ_ATTRIBUTES
,
2689 FS_FULL_SIZE_INFORMATION
,
2690 SMB2_O_INFO_FILESYSTEM
,
2691 sizeof(struct smb2_fs_full_size_info
),
2692 &rsp_iov
, &buftype
, cifs_sb
);
2696 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
2697 buf
->f_type
= SMB2_SUPER_MAGIC
;
2698 info
= (struct smb2_fs_full_size_info
*)(
2699 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)rsp
);
2700 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
2701 le32_to_cpu(rsp
->OutputBufferLength
),
2703 sizeof(struct smb2_fs_full_size_info
));
2705 smb2_copy_fs_info_to_kstatfs(info
, buf
);
2708 trace_smb3_qfs_done(xid
, tcon
->tid
, tcon
->ses
->Suid
, tcon
->tree_name
, rc
);
2709 free_rsp_buf(buftype
, rsp_iov
.iov_base
);
2714 smb311_queryfs(const unsigned int xid
, struct cifs_tcon
*tcon
,
2715 struct cifs_sb_info
*cifs_sb
, struct kstatfs
*buf
)
2718 __le16 srch_path
= 0; /* Null - open root of share */
2719 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2720 struct cifs_open_parms oparms
;
2721 struct cifs_fid fid
;
2723 if (!tcon
->posix_extensions
)
2724 return smb2_queryfs(xid
, tcon
, cifs_sb
, buf
);
2726 oparms
= (struct cifs_open_parms
) {
2729 .desired_access
= FILE_READ_ATTRIBUTES
,
2730 .disposition
= FILE_OPEN
,
2731 .create_options
= cifs_create_options(cifs_sb
, 0),
2735 rc
= SMB2_open(xid
, &oparms
, &srch_path
, &oplock
, NULL
, NULL
,
2740 rc
= SMB311_posix_qfs_info(xid
, tcon
, fid
.persistent_fid
,
2741 fid
.volatile_fid
, buf
);
2742 buf
->f_type
= SMB2_SUPER_MAGIC
;
2743 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
2748 smb2_compare_fids(struct cifsFileInfo
*ob1
, struct cifsFileInfo
*ob2
)
2750 return ob1
->fid
.persistent_fid
== ob2
->fid
.persistent_fid
&&
2751 ob1
->fid
.volatile_fid
== ob2
->fid
.volatile_fid
;
2755 smb2_mand_lock(const unsigned int xid
, struct cifsFileInfo
*cfile
, __u64 offset
,
2756 __u64 length
, __u32 type
, int lock
, int unlock
, bool wait
)
2758 if (unlock
&& !lock
)
2759 type
= SMB2_LOCKFLAG_UNLOCK
;
2760 return SMB2_lock(xid
, tlink_tcon(cfile
->tlink
),
2761 cfile
->fid
.persistent_fid
, cfile
->fid
.volatile_fid
,
2762 current
->tgid
, length
, offset
, type
, wait
);
2766 smb2_get_lease_key(struct inode
*inode
, struct cifs_fid
*fid
)
2768 memcpy(fid
->lease_key
, CIFS_I(inode
)->lease_key
, SMB2_LEASE_KEY_SIZE
);
2772 smb2_set_lease_key(struct inode
*inode
, struct cifs_fid
*fid
)
2774 memcpy(CIFS_I(inode
)->lease_key
, fid
->lease_key
, SMB2_LEASE_KEY_SIZE
);
2778 smb2_new_lease_key(struct cifs_fid
*fid
)
2780 generate_random_uuid(fid
->lease_key
);
2784 smb2_get_dfs_refer(const unsigned int xid
, struct cifs_ses
*ses
,
2785 const char *search_name
,
2786 struct dfs_info3_param
**target_nodes
,
2787 unsigned int *num_of_nodes
,
2788 const struct nls_table
*nls_codepage
, int remap
)
2791 __le16
*utf16_path
= NULL
;
2792 int utf16_path_len
= 0;
2793 struct cifs_tcon
*tcon
;
2794 struct fsctl_get_dfs_referral_req
*dfs_req
= NULL
;
2795 struct get_dfs_referral_rsp
*dfs_rsp
= NULL
;
2796 u32 dfs_req_size
= 0, dfs_rsp_size
= 0;
2797 int retry_count
= 0;
2799 cifs_dbg(FYI
, "%s: path: %s\n", __func__
, search_name
);
2802 * Try to use the IPC tcon, otherwise just use any
2804 tcon
= ses
->tcon_ipc
;
2806 spin_lock(&cifs_tcp_ses_lock
);
2807 tcon
= list_first_entry_or_null(&ses
->tcon_list
,
2812 spin_unlock(&cifs_tcp_ses_lock
);
2816 cifs_dbg(VFS
, "session %p has no tcon available for a dfs referral request\n",
2822 utf16_path
= cifs_strndup_to_utf16(search_name
, PATH_MAX
,
2824 nls_codepage
, remap
);
2830 dfs_req_size
= sizeof(*dfs_req
) + utf16_path_len
;
2831 dfs_req
= kzalloc(dfs_req_size
, GFP_KERNEL
);
2837 /* Highest DFS referral version understood */
2838 dfs_req
->MaxReferralLevel
= DFS_VERSION
;
2840 /* Path to resolve in an UTF-16 null-terminated string */
2841 memcpy(dfs_req
->RequestFileName
, utf16_path
, utf16_path_len
);
2844 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
2845 FSCTL_DFS_GET_REFERRALS
,
2846 (char *)dfs_req
, dfs_req_size
, CIFSMaxBufSize
,
2847 (char **)&dfs_rsp
, &dfs_rsp_size
);
2848 if (!is_retryable_error(rc
))
2850 usleep_range(512, 2048);
2851 } while (++retry_count
< 5);
2853 if (!rc
&& !dfs_rsp
)
2856 if (!is_retryable_error(rc
) && rc
!= -ENOENT
&& rc
!= -EOPNOTSUPP
)
2857 cifs_tcon_dbg(VFS
, "%s: ioctl error: rc=%d\n", __func__
, rc
);
2861 rc
= parse_dfs_referrals(dfs_rsp
, dfs_rsp_size
,
2862 num_of_nodes
, target_nodes
,
2863 nls_codepage
, remap
, search_name
,
2864 true /* is_unicode */);
2866 cifs_tcon_dbg(VFS
, "parse error in %s rc=%d\n", __func__
, rc
);
2871 if (tcon
&& !tcon
->ipc
) {
2872 /* ipc tcons are not refcounted */
2873 spin_lock(&cifs_tcp_ses_lock
);
2875 /* tc_count can never go negative */
2876 WARN_ON(tcon
->tc_count
< 0);
2877 spin_unlock(&cifs_tcp_ses_lock
);
2885 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2886 static int parse_reparse_posix(struct reparse_posix_data
*buf
,
2887 struct cifs_sb_info
*cifs_sb
,
2888 struct cifs_open_info_data
*data
)
2893 switch ((type
= le64_to_cpu(buf
->InodeType
))) {
2894 case NFS_SPECFILE_LNK
:
2895 len
= le16_to_cpu(buf
->ReparseDataLength
);
2896 data
->symlink_target
= cifs_strndup_from_utf16(buf
->DataBuffer
,
2898 cifs_sb
->local_nls
);
2899 if (!data
->symlink_target
)
2901 convert_delimiter(data
->symlink_target
, '/');
2902 cifs_dbg(FYI
, "%s: target path: %s\n",
2903 __func__
, data
->symlink_target
);
2905 case NFS_SPECFILE_CHR
:
2906 case NFS_SPECFILE_BLK
:
2907 case NFS_SPECFILE_FIFO
:
2908 case NFS_SPECFILE_SOCK
:
2911 cifs_dbg(VFS
, "%s: unhandled inode type: 0x%llx\n",
2918 static int parse_reparse_symlink(struct reparse_symlink_data_buffer
*sym
,
2919 u32 plen
, bool unicode
,
2920 struct cifs_sb_info
*cifs_sb
,
2921 struct cifs_open_info_data
*data
)
2926 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2928 offs
= le16_to_cpu(sym
->SubstituteNameOffset
);
2929 len
= le16_to_cpu(sym
->SubstituteNameLength
);
2930 if (offs
+ 20 > plen
|| offs
+ len
+ 20 > plen
) {
2931 cifs_dbg(VFS
, "srv returned malformed symlink buffer\n");
2935 data
->symlink_target
= cifs_strndup_from_utf16(sym
->PathBuffer
+ offs
,
2937 cifs_sb
->local_nls
);
2938 if (!data
->symlink_target
)
2941 convert_delimiter(data
->symlink_target
, '/');
2942 cifs_dbg(FYI
, "%s: target path: %s\n", __func__
, data
->symlink_target
);
2947 int parse_reparse_point(struct reparse_data_buffer
*buf
,
2948 u32 plen
, struct cifs_sb_info
*cifs_sb
,
2949 bool unicode
, struct cifs_open_info_data
*data
)
2951 if (plen
< sizeof(*buf
)) {
2952 cifs_dbg(VFS
, "%s: reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2957 if (plen
< le16_to_cpu(buf
->ReparseDataLength
) + sizeof(*buf
)) {
2958 cifs_dbg(VFS
, "%s: invalid reparse buf length: %d\n",
2963 data
->reparse
.buf
= buf
;
2965 /* See MS-FSCC 2.1.2 */
2966 switch (le32_to_cpu(buf
->ReparseTag
)) {
2967 case IO_REPARSE_TAG_NFS
:
2968 return parse_reparse_posix((struct reparse_posix_data
*)buf
,
2970 case IO_REPARSE_TAG_SYMLINK
:
2971 return parse_reparse_symlink(
2972 (struct reparse_symlink_data_buffer
*)buf
,
2973 plen
, unicode
, cifs_sb
, data
);
2974 case IO_REPARSE_TAG_LX_SYMLINK
:
2975 case IO_REPARSE_TAG_AF_UNIX
:
2976 case IO_REPARSE_TAG_LX_FIFO
:
2977 case IO_REPARSE_TAG_LX_CHR
:
2978 case IO_REPARSE_TAG_LX_BLK
:
2981 cifs_dbg(VFS
, "%s: unhandled reparse tag: 0x%08x\n",
2982 __func__
, le32_to_cpu(buf
->ReparseTag
));
2987 static int smb2_parse_reparse_point(struct cifs_sb_info
*cifs_sb
,
2988 struct kvec
*rsp_iov
,
2989 struct cifs_open_info_data
*data
)
2991 struct reparse_data_buffer
*buf
;
2992 struct smb2_ioctl_rsp
*io
= rsp_iov
->iov_base
;
2993 u32 plen
= le32_to_cpu(io
->OutputCount
);
2995 buf
= (struct reparse_data_buffer
*)((u8
*)io
+
2996 le32_to_cpu(io
->OutputOffset
));
2997 return parse_reparse_point(buf
, plen
, cifs_sb
, true, data
);
3000 static int smb2_query_reparse_point(const unsigned int xid
,
3001 struct cifs_tcon
*tcon
,
3002 struct cifs_sb_info
*cifs_sb
,
3003 const char *full_path
,
3004 u32
*tag
, struct kvec
*rsp
,
3007 struct smb2_compound_vars
*vars
;
3009 __le16
*utf16_path
= NULL
;
3010 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
3011 struct cifs_open_parms oparms
;
3012 struct cifs_fid fid
;
3013 struct TCP_Server_Info
*server
= cifs_pick_channel(tcon
->ses
);
3014 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
3015 struct smb_rqst
*rqst
;
3016 int resp_buftype
[3];
3017 struct kvec
*rsp_iov
;
3018 struct smb2_ioctl_rsp
*ioctl_rsp
;
3019 struct reparse_data_buffer
*reparse_buf
;
3020 u32 off
, count
, len
;
3022 cifs_dbg(FYI
, "%s: path: %s\n", __func__
, full_path
);
3024 if (smb3_encryption_required(tcon
))
3025 flags
|= CIFS_TRANSFORM_REQ
;
3027 utf16_path
= cifs_convert_path_to_utf16(full_path
, cifs_sb
);
3031 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
3032 vars
= kzalloc(sizeof(*vars
), GFP_KERNEL
);
3038 rsp_iov
= vars
->rsp_iov
;
3041 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3042 * to see if there is a handle already open that we can use
3044 rqst
[0].rq_iov
= vars
->open_iov
;
3045 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
3047 oparms
= (struct cifs_open_parms
) {
3050 .desired_access
= FILE_READ_ATTRIBUTES
,
3051 .disposition
= FILE_OPEN
,
3052 .create_options
= cifs_create_options(cifs_sb
, OPEN_REPARSE_POINT
),
3056 rc
= SMB2_open_init(tcon
, server
,
3057 &rqst
[0], &oplock
, &oparms
, utf16_path
);
3060 smb2_set_next_command(tcon
, &rqst
[0]);
3064 rqst
[1].rq_iov
= vars
->io_iov
;
3065 rqst
[1].rq_nvec
= SMB2_IOCTL_IOV_SIZE
;
3067 rc
= SMB2_ioctl_init(tcon
, server
,
3068 &rqst
[1], COMPOUND_FID
,
3069 COMPOUND_FID
, FSCTL_GET_REPARSE_POINT
, NULL
, 0,
3071 MAX_SMB2_CREATE_RESPONSE_SIZE
-
3072 MAX_SMB2_CLOSE_RESPONSE_SIZE
);
3076 smb2_set_next_command(tcon
, &rqst
[1]);
3077 smb2_set_related(&rqst
[1]);
3080 rqst
[2].rq_iov
= &vars
->close_iov
;
3081 rqst
[2].rq_nvec
= 1;
3083 rc
= SMB2_close_init(tcon
, server
,
3084 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
3088 smb2_set_related(&rqst
[2]);
3090 rc
= compound_send_recv(xid
, tcon
->ses
, server
,
3092 resp_buftype
, rsp_iov
);
3094 ioctl_rsp
= rsp_iov
[1].iov_base
;
3097 * Open was successful and we got an ioctl response.
3100 /* See MS-FSCC 2.3.23 */
3101 off
= le32_to_cpu(ioctl_rsp
->OutputOffset
);
3102 count
= le32_to_cpu(ioctl_rsp
->OutputCount
);
3103 if (check_add_overflow(off
, count
, &len
) ||
3104 len
> rsp_iov
[1].iov_len
) {
3105 cifs_tcon_dbg(VFS
, "%s: invalid ioctl: off=%d count=%d\n",
3106 __func__
, off
, count
);
3111 reparse_buf
= (void *)((u8
*)ioctl_rsp
+ off
);
3112 len
= sizeof(*reparse_buf
);
3114 count
< le16_to_cpu(reparse_buf
->ReparseDataLength
) + len
) {
3115 cifs_tcon_dbg(VFS
, "%s: invalid ioctl: off=%d count=%d\n",
3116 __func__
, off
, count
);
3120 *tag
= le32_to_cpu(reparse_buf
->ReparseTag
);
3122 *rsp_buftype
= resp_buftype
[1];
3123 resp_buftype
[1] = CIFS_NO_BUFFER
;
3127 SMB2_open_free(&rqst
[0]);
3128 SMB2_ioctl_free(&rqst
[1]);
3129 SMB2_close_free(&rqst
[2]);
3130 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
3131 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
3132 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
3139 static struct cifs_ntsd
*
3140 get_smb2_acl_by_fid(struct cifs_sb_info
*cifs_sb
,
3141 const struct cifs_fid
*cifsfid
, u32
*pacllen
, u32 info
)
3143 struct cifs_ntsd
*pntsd
= NULL
;
3145 int rc
= -EOPNOTSUPP
;
3146 struct tcon_link
*tlink
= cifs_sb_tlink(cifs_sb
);
3149 return ERR_CAST(tlink
);
3152 cifs_dbg(FYI
, "trying to get acl\n");
3154 rc
= SMB2_query_acl(xid
, tlink_tcon(tlink
), cifsfid
->persistent_fid
,
3155 cifsfid
->volatile_fid
, (void **)&pntsd
, pacllen
,
3159 cifs_put_tlink(tlink
);
3161 cifs_dbg(FYI
, "%s: rc = %d ACL len %d\n", __func__
, rc
, *pacllen
);
3168 static struct cifs_ntsd
*
3169 get_smb2_acl_by_path(struct cifs_sb_info
*cifs_sb
,
3170 const char *path
, u32
*pacllen
, u32 info
)
3172 struct cifs_ntsd
*pntsd
= NULL
;
3173 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
3176 struct cifs_tcon
*tcon
;
3177 struct tcon_link
*tlink
= cifs_sb_tlink(cifs_sb
);
3178 struct cifs_fid fid
;
3179 struct cifs_open_parms oparms
;
3182 cifs_dbg(FYI
, "get smb3 acl for path %s\n", path
);
3184 return ERR_CAST(tlink
);
3186 tcon
= tlink_tcon(tlink
);
3189 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
3196 oparms
= (struct cifs_open_parms
) {
3199 .desired_access
= READ_CONTROL
,
3200 .disposition
= FILE_OPEN
,
3202 * When querying an ACL, even if the file is a symlink
3203 * we want to open the source not the target, and so
3204 * the protocol requires that the client specify this
3205 * flag when opening a reparse point
3207 .create_options
= cifs_create_options(cifs_sb
, 0) |
3212 if (info
& SACL_SECINFO
)
3213 oparms
.desired_access
|= SYSTEM_SECURITY
;
3215 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
, NULL
,
3219 rc
= SMB2_query_acl(xid
, tlink_tcon(tlink
), fid
.persistent_fid
,
3220 fid
.volatile_fid
, (void **)&pntsd
, pacllen
,
3222 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
3225 cifs_put_tlink(tlink
);
3228 cifs_dbg(FYI
, "%s: rc = %d ACL len %d\n", __func__
, rc
, *pacllen
);
3235 set_smb2_acl(struct cifs_ntsd
*pnntsd
, __u32 acllen
,
3236 struct inode
*inode
, const char *path
, int aclflag
)
3238 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
3240 int rc
, access_flags
= 0;
3241 struct cifs_tcon
*tcon
;
3242 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
3243 struct tcon_link
*tlink
= cifs_sb_tlink(cifs_sb
);
3244 struct cifs_fid fid
;
3245 struct cifs_open_parms oparms
;
3248 cifs_dbg(FYI
, "set smb3 acl for path %s\n", path
);
3250 return PTR_ERR(tlink
);
3252 tcon
= tlink_tcon(tlink
);
3255 if (aclflag
& CIFS_ACL_OWNER
|| aclflag
& CIFS_ACL_GROUP
)
3256 access_flags
|= WRITE_OWNER
;
3257 if (aclflag
& CIFS_ACL_SACL
)
3258 access_flags
|= SYSTEM_SECURITY
;
3259 if (aclflag
& CIFS_ACL_DACL
)
3260 access_flags
|= WRITE_DAC
;
3262 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
3269 oparms
= (struct cifs_open_parms
) {
3271 .desired_access
= access_flags
,
3272 .create_options
= cifs_create_options(cifs_sb
, 0),
3273 .disposition
= FILE_OPEN
,
3278 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
,
3282 rc
= SMB2_set_acl(xid
, tlink_tcon(tlink
), fid
.persistent_fid
,
3283 fid
.volatile_fid
, pnntsd
, acllen
, aclflag
);
3284 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
3287 cifs_put_tlink(tlink
);
3292 /* Retrieve an ACL from the server */
3293 static struct cifs_ntsd
*
3294 get_smb2_acl(struct cifs_sb_info
*cifs_sb
,
3295 struct inode
*inode
, const char *path
,
3296 u32
*pacllen
, u32 info
)
3298 struct cifs_ntsd
*pntsd
= NULL
;
3299 struct cifsFileInfo
*open_file
= NULL
;
3301 if (inode
&& !(info
& SACL_SECINFO
))
3302 open_file
= find_readable_file(CIFS_I(inode
), true);
3303 if (!open_file
|| (info
& SACL_SECINFO
))
3304 return get_smb2_acl_by_path(cifs_sb
, path
, pacllen
, info
);
3306 pntsd
= get_smb2_acl_by_fid(cifs_sb
, &open_file
->fid
, pacllen
, info
);
3307 cifsFileInfo_put(open_file
);
3311 static long smb3_zero_data(struct file
*file
, struct cifs_tcon
*tcon
,
3312 loff_t offset
, loff_t len
, unsigned int xid
)
3314 struct cifsFileInfo
*cfile
= file
->private_data
;
3315 struct file_zero_data_information fsctl_buf
;
3317 cifs_dbg(FYI
, "Offset %lld len %lld\n", offset
, len
);
3319 fsctl_buf
.FileOffset
= cpu_to_le64(offset
);
3320 fsctl_buf
.BeyondFinalZero
= cpu_to_le64(offset
+ len
);
3322 return SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3323 cfile
->fid
.volatile_fid
, FSCTL_SET_ZERO_DATA
,
3325 sizeof(struct file_zero_data_information
),
3329 static long smb3_zero_range(struct file
*file
, struct cifs_tcon
*tcon
,
3330 loff_t offset
, loff_t len
, bool keep_size
)
3332 struct cifs_ses
*ses
= tcon
->ses
;
3333 struct inode
*inode
= file_inode(file
);
3334 struct cifsInodeInfo
*cifsi
= CIFS_I(inode
);
3335 struct cifsFileInfo
*cfile
= file
->private_data
;
3336 unsigned long long new_size
;
3343 trace_smb3_zero_enter(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3344 ses
->Suid
, offset
, len
);
3347 filemap_invalidate_lock(inode
->i_mapping
);
3350 * We zero the range through ioctl, so we need remove the page caches
3351 * first, otherwise the data may be inconsistent with the server.
3353 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
3355 /* if file not oplocked can't be sure whether asking to extend size */
3357 if (keep_size
== false && !CIFS_CACHE_READ(cifsi
))
3358 goto zero_range_exit
;
3360 rc
= smb3_zero_data(file
, tcon
, offset
, len
, xid
);
3362 goto zero_range_exit
;
3365 * do we also need to change the size of the file?
3367 new_size
= offset
+ len
;
3368 if (keep_size
== false && (unsigned long long)i_size_read(inode
) < new_size
) {
3369 eof
= cpu_to_le64(new_size
);
3370 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3371 cfile
->fid
.volatile_fid
, cfile
->pid
, &eof
);
3373 truncate_setsize(inode
, new_size
);
3374 fscache_resize_cookie(cifs_inode_cookie(inode
), new_size
);
3379 filemap_invalidate_unlock(inode
->i_mapping
);
3380 inode_unlock(inode
);
3383 trace_smb3_zero_err(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3384 ses
->Suid
, offset
, len
, rc
);
3386 trace_smb3_zero_done(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3387 ses
->Suid
, offset
, len
);
3391 static long smb3_punch_hole(struct file
*file
, struct cifs_tcon
*tcon
,
3392 loff_t offset
, loff_t len
)
3394 struct inode
*inode
= file_inode(file
);
3395 struct cifsFileInfo
*cfile
= file
->private_data
;
3396 struct file_zero_data_information fsctl_buf
;
3399 __u8 set_sparse
= 1;
3404 /* Need to make file sparse, if not already, before freeing range. */
3405 /* Consider adding equivalent for compressed since it could also work */
3406 if (!smb2_set_sparse(xid
, tcon
, cfile
, inode
, set_sparse
)) {
3411 filemap_invalidate_lock(inode
->i_mapping
);
3413 * We implement the punch hole through ioctl, so we need remove the page
3414 * caches first, otherwise the data may be inconsistent with the server.
3416 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
3418 cifs_dbg(FYI
, "Offset %lld len %lld\n", offset
, len
);
3420 fsctl_buf
.FileOffset
= cpu_to_le64(offset
);
3421 fsctl_buf
.BeyondFinalZero
= cpu_to_le64(offset
+ len
);
3423 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3424 cfile
->fid
.volatile_fid
, FSCTL_SET_ZERO_DATA
,
3426 sizeof(struct file_zero_data_information
),
3427 CIFSMaxBufSize
, NULL
, NULL
);
3428 filemap_invalidate_unlock(inode
->i_mapping
);
3430 inode_unlock(inode
);
3435 static int smb3_simple_fallocate_write_range(unsigned int xid
,
3436 struct cifs_tcon
*tcon
,
3437 struct cifsFileInfo
*cfile
,
3438 loff_t off
, loff_t len
,
3441 struct cifs_io_parms io_parms
= {0};
3446 io_parms
.netfid
= cfile
->fid
.netfid
;
3447 io_parms
.pid
= current
->tgid
;
3448 io_parms
.tcon
= tcon
;
3449 io_parms
.persistent_fid
= cfile
->fid
.persistent_fid
;
3450 io_parms
.volatile_fid
= cfile
->fid
.volatile_fid
;
3453 io_parms
.offset
= off
;
3454 io_parms
.length
= len
;
3455 if (io_parms
.length
> SMB2_MAX_BUFFER_SIZE
)
3456 io_parms
.length
= SMB2_MAX_BUFFER_SIZE
;
3457 /* iov[0] is reserved for smb header */
3458 iov
[1].iov_base
= buf
;
3459 iov
[1].iov_len
= io_parms
.length
;
3460 rc
= SMB2_write(xid
, &io_parms
, &nbytes
, iov
, 1);
3472 static int smb3_simple_fallocate_range(unsigned int xid
,
3473 struct cifs_tcon
*tcon
,
3474 struct cifsFileInfo
*cfile
,
3475 loff_t off
, loff_t len
)
3477 struct file_allocated_range_buffer in_data
, *out_data
= NULL
, *tmp_data
;
3483 in_data
.file_offset
= cpu_to_le64(off
);
3484 in_data
.length
= cpu_to_le64(len
);
3485 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3486 cfile
->fid
.volatile_fid
,
3487 FSCTL_QUERY_ALLOCATED_RANGES
,
3488 (char *)&in_data
, sizeof(in_data
),
3489 1024 * sizeof(struct file_allocated_range_buffer
),
3490 (char **)&out_data
, &out_data_len
);
3494 buf
= kzalloc(1024 * 1024, GFP_KERNEL
);
3500 tmp_data
= out_data
;
3503 * The rest of the region is unmapped so write it all.
3505 if (out_data_len
== 0) {
3506 rc
= smb3_simple_fallocate_write_range(xid
, tcon
,
3507 cfile
, off
, len
, buf
);
3511 if (out_data_len
< sizeof(struct file_allocated_range_buffer
)) {
3516 if (off
< le64_to_cpu(tmp_data
->file_offset
)) {
3518 * We are at a hole. Write until the end of the region
3519 * or until the next allocated data,
3520 * whichever comes next.
3522 l
= le64_to_cpu(tmp_data
->file_offset
) - off
;
3525 rc
= smb3_simple_fallocate_write_range(xid
, tcon
,
3526 cfile
, off
, l
, buf
);
3535 * We are at a section of allocated data, just skip forward
3536 * until the end of the data or the end of the region
3537 * we are supposed to fallocate, whichever comes first.
3539 l
= le64_to_cpu(tmp_data
->length
);
3545 tmp_data
= &tmp_data
[1];
3546 out_data_len
-= sizeof(struct file_allocated_range_buffer
);
3556 static long smb3_simple_falloc(struct file
*file
, struct cifs_tcon
*tcon
,
3557 loff_t off
, loff_t len
, bool keep_size
)
3559 struct inode
*inode
;
3560 struct cifsInodeInfo
*cifsi
;
3561 struct cifsFileInfo
*cfile
= file
->private_data
;
3562 long rc
= -EOPNOTSUPP
;
3568 inode
= d_inode(cfile
->dentry
);
3569 cifsi
= CIFS_I(inode
);
3571 trace_smb3_falloc_enter(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3572 tcon
->ses
->Suid
, off
, len
);
3573 /* if file not oplocked can't be sure whether asking to extend size */
3574 if (!CIFS_CACHE_READ(cifsi
))
3575 if (keep_size
== false) {
3576 trace_smb3_falloc_err(xid
, cfile
->fid
.persistent_fid
,
3577 tcon
->tid
, tcon
->ses
->Suid
, off
, len
, rc
);
3583 * Extending the file
3585 if ((keep_size
== false) && i_size_read(inode
) < off
+ len
) {
3586 rc
= inode_newsize_ok(inode
, off
+ len
);
3590 if (cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
)
3591 smb2_set_sparse(xid
, tcon
, cfile
, inode
, false);
3593 eof
= cpu_to_le64(off
+ len
);
3594 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3595 cfile
->fid
.volatile_fid
, cfile
->pid
, &eof
);
3597 cifsi
->server_eof
= off
+ len
;
3598 cifs_setsize(inode
, off
+ len
);
3599 cifs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3600 truncate_setsize(inode
, off
+ len
);
3606 * Files are non-sparse by default so falloc may be a no-op
3607 * Must check if file sparse. If not sparse, and since we are not
3608 * extending then no need to do anything since file already allocated
3610 if ((cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
) == 0) {
3615 if (keep_size
== true) {
3617 * We can not preallocate pages beyond the end of the file
3620 if (off
>= i_size_read(inode
)) {
3625 * For fallocates that are partially beyond the end of file,
3626 * clamp len so we only fallocate up to the end of file.
3628 if (off
+ len
> i_size_read(inode
)) {
3629 len
= i_size_read(inode
) - off
;
3633 if ((keep_size
== true) || (i_size_read(inode
) >= off
+ len
)) {
3635 * At this point, we are trying to fallocate an internal
3636 * regions of a sparse file. Since smb2 does not have a
3637 * fallocate command we have two otions on how to emulate this.
3638 * We can either turn the entire file to become non-sparse
3639 * which we only do if the fallocate is for virtually
3640 * the whole file, or we can overwrite the region with zeroes
3641 * using SMB2_write, which could be prohibitevly expensive
3645 * We are only trying to fallocate a small region so
3646 * just write it with zero.
3648 if (len
<= 1024 * 1024) {
3649 rc
= smb3_simple_fallocate_range(xid
, tcon
, cfile
,
3655 * Check if falloc starts within first few pages of file
3656 * and ends within a few pages of the end of file to
3657 * ensure that most of file is being forced to be
3658 * fallocated now. If so then setting whole file sparse
3659 * ie potentially making a few extra pages at the beginning
3660 * or end of the file non-sparse via set_sparse is harmless.
3662 if ((off
> 8192) || (off
+ len
+ 8192 < i_size_read(inode
))) {
3668 smb2_set_sparse(xid
, tcon
, cfile
, inode
, false);
3673 trace_smb3_falloc_err(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3674 tcon
->ses
->Suid
, off
, len
, rc
);
3676 trace_smb3_falloc_done(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3677 tcon
->ses
->Suid
, off
, len
);
3683 static long smb3_collapse_range(struct file
*file
, struct cifs_tcon
*tcon
,
3684 loff_t off
, loff_t len
)
3688 struct inode
*inode
= file_inode(file
);
3689 struct cifsFileInfo
*cfile
= file
->private_data
;
3690 struct cifsInodeInfo
*cifsi
= CIFS_I(inode
);
3698 old_eof
= i_size_read(inode
);
3699 if ((off
>= old_eof
) ||
3700 off
+ len
>= old_eof
) {
3705 filemap_invalidate_lock(inode
->i_mapping
);
3706 rc
= filemap_write_and_wait_range(inode
->i_mapping
, off
, old_eof
- 1);
3710 truncate_pagecache_range(inode
, off
, old_eof
);
3712 rc
= smb2_copychunk_range(xid
, cfile
, cfile
, off
+ len
,
3713 old_eof
- off
- len
, off
);
3717 eof
= cpu_to_le64(old_eof
- len
);
3718 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3719 cfile
->fid
.volatile_fid
, cfile
->pid
, &eof
);
3725 cifsi
->server_eof
= i_size_read(inode
) - len
;
3726 truncate_setsize(inode
, cifsi
->server_eof
);
3727 fscache_resize_cookie(cifs_inode_cookie(inode
), cifsi
->server_eof
);
3729 filemap_invalidate_unlock(inode
->i_mapping
);
3731 inode_unlock(inode
);
3736 static long smb3_insert_range(struct file
*file
, struct cifs_tcon
*tcon
,
3737 loff_t off
, loff_t len
)
3741 struct cifsFileInfo
*cfile
= file
->private_data
;
3742 struct inode
*inode
= file_inode(file
);
3744 __u64 count
, old_eof
;
3750 old_eof
= i_size_read(inode
);
3751 if (off
>= old_eof
) {
3756 count
= old_eof
- off
;
3757 eof
= cpu_to_le64(old_eof
+ len
);
3759 filemap_invalidate_lock(inode
->i_mapping
);
3760 rc
= filemap_write_and_wait_range(inode
->i_mapping
, off
, old_eof
+ len
- 1);
3763 truncate_pagecache_range(inode
, off
, old_eof
);
3765 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3766 cfile
->fid
.volatile_fid
, cfile
->pid
, &eof
);
3770 truncate_setsize(inode
, old_eof
+ len
);
3771 fscache_resize_cookie(cifs_inode_cookie(inode
), i_size_read(inode
));
3773 rc
= smb2_copychunk_range(xid
, cfile
, cfile
, off
, count
, off
+ len
);
3777 rc
= smb3_zero_data(file
, tcon
, off
, len
, xid
);
3783 filemap_invalidate_unlock(inode
->i_mapping
);
3785 inode_unlock(inode
);
3790 static loff_t
smb3_llseek(struct file
*file
, struct cifs_tcon
*tcon
, loff_t offset
, int whence
)
3792 struct cifsFileInfo
*wrcfile
, *cfile
= file
->private_data
;
3793 struct cifsInodeInfo
*cifsi
;
3794 struct inode
*inode
;
3796 struct file_allocated_range_buffer in_data
, *out_data
= NULL
;
3800 if (whence
!= SEEK_HOLE
&& whence
!= SEEK_DATA
)
3801 return generic_file_llseek(file
, offset
, whence
);
3803 inode
= d_inode(cfile
->dentry
);
3804 cifsi
= CIFS_I(inode
);
3806 if (offset
< 0 || offset
>= i_size_read(inode
))
3811 * We need to be sure that all dirty pages are written as they
3812 * might fill holes on the server.
3813 * Note that we also MUST flush any written pages since at least
3814 * some servers (Windows2016) will not reflect recent writes in
3815 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3817 wrcfile
= find_writable_file(cifsi
, FIND_WR_ANY
);
3819 filemap_write_and_wait(inode
->i_mapping
);
3820 smb2_flush_file(xid
, tcon
, &wrcfile
->fid
);
3821 cifsFileInfo_put(wrcfile
);
3824 if (!(cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
)) {
3825 if (whence
== SEEK_HOLE
)
3826 offset
= i_size_read(inode
);
3830 in_data
.file_offset
= cpu_to_le64(offset
);
3831 in_data
.length
= cpu_to_le64(i_size_read(inode
));
3833 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3834 cfile
->fid
.volatile_fid
,
3835 FSCTL_QUERY_ALLOCATED_RANGES
,
3836 (char *)&in_data
, sizeof(in_data
),
3837 sizeof(struct file_allocated_range_buffer
),
3838 (char **)&out_data
, &out_data_len
);
3844 if (whence
== SEEK_HOLE
&& out_data_len
== 0)
3847 if (whence
== SEEK_DATA
&& out_data_len
== 0) {
3852 if (out_data_len
< sizeof(struct file_allocated_range_buffer
)) {
3856 if (whence
== SEEK_DATA
) {
3857 offset
= le64_to_cpu(out_data
->file_offset
);
3860 if (offset
< le64_to_cpu(out_data
->file_offset
))
3863 offset
= le64_to_cpu(out_data
->file_offset
) + le64_to_cpu(out_data
->length
);
3869 return vfs_setpos(file
, offset
, inode
->i_sb
->s_maxbytes
);
3874 static int smb3_fiemap(struct cifs_tcon
*tcon
,
3875 struct cifsFileInfo
*cfile
,
3876 struct fiemap_extent_info
*fei
, u64 start
, u64 len
)
3879 struct file_allocated_range_buffer in_data
, *out_data
;
3881 int i
, num
, rc
, flags
, last_blob
;
3884 rc
= fiemap_prep(d_inode(cfile
->dentry
), fei
, start
, &len
, 0);
3890 in_data
.file_offset
= cpu_to_le64(start
);
3891 in_data
.length
= cpu_to_le64(len
);
3893 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3894 cfile
->fid
.volatile_fid
,
3895 FSCTL_QUERY_ALLOCATED_RANGES
,
3896 (char *)&in_data
, sizeof(in_data
),
3897 1024 * sizeof(struct file_allocated_range_buffer
),
3898 (char **)&out_data
, &out_data_len
);
3907 if (out_data_len
&& out_data_len
< sizeof(struct file_allocated_range_buffer
)) {
3911 if (out_data_len
% sizeof(struct file_allocated_range_buffer
)) {
3916 num
= out_data_len
/ sizeof(struct file_allocated_range_buffer
);
3917 for (i
= 0; i
< num
; i
++) {
3919 if (i
== num
- 1 && last_blob
)
3920 flags
|= FIEMAP_EXTENT_LAST
;
3922 rc
= fiemap_fill_next_extent(fei
,
3923 le64_to_cpu(out_data
[i
].file_offset
),
3924 le64_to_cpu(out_data
[i
].file_offset
),
3925 le64_to_cpu(out_data
[i
].length
),
3936 next
= le64_to_cpu(out_data
[num
- 1].file_offset
) +
3937 le64_to_cpu(out_data
[num
- 1].length
);
3938 len
= len
- (next
- start
);
3949 static long smb3_fallocate(struct file
*file
, struct cifs_tcon
*tcon
, int mode
,
3950 loff_t off
, loff_t len
)
3952 /* KEEP_SIZE already checked for by do_fallocate */
3953 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3954 return smb3_punch_hole(file
, tcon
, off
, len
);
3955 else if (mode
& FALLOC_FL_ZERO_RANGE
) {
3956 if (mode
& FALLOC_FL_KEEP_SIZE
)
3957 return smb3_zero_range(file
, tcon
, off
, len
, true);
3958 return smb3_zero_range(file
, tcon
, off
, len
, false);
3959 } else if (mode
== FALLOC_FL_KEEP_SIZE
)
3960 return smb3_simple_falloc(file
, tcon
, off
, len
, true);
3961 else if (mode
== FALLOC_FL_COLLAPSE_RANGE
)
3962 return smb3_collapse_range(file
, tcon
, off
, len
);
3963 else if (mode
== FALLOC_FL_INSERT_RANGE
)
3964 return smb3_insert_range(file
, tcon
, off
, len
);
3966 return smb3_simple_falloc(file
, tcon
, off
, len
, false);
3972 smb2_downgrade_oplock(struct TCP_Server_Info
*server
,
3973 struct cifsInodeInfo
*cinode
, __u32 oplock
,
3974 unsigned int epoch
, bool *purge_cache
)
3976 server
->ops
->set_oplock_level(cinode
, oplock
, 0, NULL
);
3980 smb21_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
3981 unsigned int epoch
, bool *purge_cache
);
3984 smb3_downgrade_oplock(struct TCP_Server_Info
*server
,
3985 struct cifsInodeInfo
*cinode
, __u32 oplock
,
3986 unsigned int epoch
, bool *purge_cache
)
3988 unsigned int old_state
= cinode
->oplock
;
3989 unsigned int old_epoch
= cinode
->epoch
;
3990 unsigned int new_state
;
3992 if (epoch
> old_epoch
) {
3993 smb21_set_oplock_level(cinode
, oplock
, 0, NULL
);
3994 cinode
->epoch
= epoch
;
3997 new_state
= cinode
->oplock
;
3998 *purge_cache
= false;
4000 if ((old_state
& CIFS_CACHE_READ_FLG
) != 0 &&
4001 (new_state
& CIFS_CACHE_READ_FLG
) == 0)
4002 *purge_cache
= true;
4003 else if (old_state
== new_state
&& (epoch
- old_epoch
> 1))
4004 *purge_cache
= true;
4008 smb2_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
4009 unsigned int epoch
, bool *purge_cache
)
4012 cinode
->lease_granted
= false;
4013 if (oplock
== SMB2_OPLOCK_LEVEL_NOCHANGE
)
4015 if (oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
4016 cinode
->oplock
= CIFS_CACHE_RHW_FLG
;
4017 cifs_dbg(FYI
, "Batch Oplock granted on inode %p\n",
4018 &cinode
->netfs
.inode
);
4019 } else if (oplock
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
) {
4020 cinode
->oplock
= CIFS_CACHE_RW_FLG
;
4021 cifs_dbg(FYI
, "Exclusive Oplock granted on inode %p\n",
4022 &cinode
->netfs
.inode
);
4023 } else if (oplock
== SMB2_OPLOCK_LEVEL_II
) {
4024 cinode
->oplock
= CIFS_CACHE_READ_FLG
;
4025 cifs_dbg(FYI
, "Level II Oplock granted on inode %p\n",
4026 &cinode
->netfs
.inode
);
4032 smb21_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
4033 unsigned int epoch
, bool *purge_cache
)
4035 char message
[5] = {0};
4036 unsigned int new_oplock
= 0;
4039 cinode
->lease_granted
= true;
4040 if (oplock
== SMB2_OPLOCK_LEVEL_NOCHANGE
)
4043 /* Check if the server granted an oplock rather than a lease */
4044 if (oplock
& SMB2_OPLOCK_LEVEL_EXCLUSIVE
)
4045 return smb2_set_oplock_level(cinode
, oplock
, epoch
,
4048 if (oplock
& SMB2_LEASE_READ_CACHING_HE
) {
4049 new_oplock
|= CIFS_CACHE_READ_FLG
;
4050 strcat(message
, "R");
4052 if (oplock
& SMB2_LEASE_HANDLE_CACHING_HE
) {
4053 new_oplock
|= CIFS_CACHE_HANDLE_FLG
;
4054 strcat(message
, "H");
4056 if (oplock
& SMB2_LEASE_WRITE_CACHING_HE
) {
4057 new_oplock
|= CIFS_CACHE_WRITE_FLG
;
4058 strcat(message
, "W");
4061 strncpy(message
, "None", sizeof(message
));
4063 cinode
->oplock
= new_oplock
;
4064 cifs_dbg(FYI
, "%s Lease granted on inode %p\n", message
,
4065 &cinode
->netfs
.inode
);
4069 smb3_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
4070 unsigned int epoch
, bool *purge_cache
)
4072 unsigned int old_oplock
= cinode
->oplock
;
4074 smb21_set_oplock_level(cinode
, oplock
, epoch
, purge_cache
);
4077 *purge_cache
= false;
4078 if (old_oplock
== CIFS_CACHE_READ_FLG
) {
4079 if (cinode
->oplock
== CIFS_CACHE_READ_FLG
&&
4080 (epoch
- cinode
->epoch
> 0))
4081 *purge_cache
= true;
4082 else if (cinode
->oplock
== CIFS_CACHE_RH_FLG
&&
4083 (epoch
- cinode
->epoch
> 1))
4084 *purge_cache
= true;
4085 else if (cinode
->oplock
== CIFS_CACHE_RHW_FLG
&&
4086 (epoch
- cinode
->epoch
> 1))
4087 *purge_cache
= true;
4088 else if (cinode
->oplock
== 0 &&
4089 (epoch
- cinode
->epoch
> 0))
4090 *purge_cache
= true;
4091 } else if (old_oplock
== CIFS_CACHE_RH_FLG
) {
4092 if (cinode
->oplock
== CIFS_CACHE_RH_FLG
&&
4093 (epoch
- cinode
->epoch
> 0))
4094 *purge_cache
= true;
4095 else if (cinode
->oplock
== CIFS_CACHE_RHW_FLG
&&
4096 (epoch
- cinode
->epoch
> 1))
4097 *purge_cache
= true;
4099 cinode
->epoch
= epoch
;
4103 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4105 smb2_is_read_op(__u32 oplock
)
4107 return oplock
== SMB2_OPLOCK_LEVEL_II
;
4109 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
4112 smb21_is_read_op(__u32 oplock
)
4114 return (oplock
& SMB2_LEASE_READ_CACHING_HE
) &&
4115 !(oplock
& SMB2_LEASE_WRITE_CACHING_HE
);
4119 map_oplock_to_lease(u8 oplock
)
4121 if (oplock
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
)
4122 return SMB2_LEASE_WRITE_CACHING_LE
| SMB2_LEASE_READ_CACHING_LE
;
4123 else if (oplock
== SMB2_OPLOCK_LEVEL_II
)
4124 return SMB2_LEASE_READ_CACHING_LE
;
4125 else if (oplock
== SMB2_OPLOCK_LEVEL_BATCH
)
4126 return SMB2_LEASE_HANDLE_CACHING_LE
| SMB2_LEASE_READ_CACHING_LE
|
4127 SMB2_LEASE_WRITE_CACHING_LE
;
4132 smb2_create_lease_buf(u8
*lease_key
, u8 oplock
)
4134 struct create_lease
*buf
;
4136 buf
= kzalloc(sizeof(struct create_lease
), GFP_KERNEL
);
4140 memcpy(&buf
->lcontext
.LeaseKey
, lease_key
, SMB2_LEASE_KEY_SIZE
);
4141 buf
->lcontext
.LeaseState
= map_oplock_to_lease(oplock
);
4143 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
4144 (struct create_lease
, lcontext
));
4145 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct lease_context
));
4146 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
4147 (struct create_lease
, Name
));
4148 buf
->ccontext
.NameLength
= cpu_to_le16(4);
4149 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4158 smb3_create_lease_buf(u8
*lease_key
, u8 oplock
)
4160 struct create_lease_v2
*buf
;
4162 buf
= kzalloc(sizeof(struct create_lease_v2
), GFP_KERNEL
);
4166 memcpy(&buf
->lcontext
.LeaseKey
, lease_key
, SMB2_LEASE_KEY_SIZE
);
4167 buf
->lcontext
.LeaseState
= map_oplock_to_lease(oplock
);
4169 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
4170 (struct create_lease_v2
, lcontext
));
4171 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct lease_context_v2
));
4172 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
4173 (struct create_lease_v2
, Name
));
4174 buf
->ccontext
.NameLength
= cpu_to_le16(4);
4175 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4184 smb2_parse_lease_buf(void *buf
, unsigned int *epoch
, char *lease_key
)
4186 struct create_lease
*lc
= (struct create_lease
*)buf
;
4188 *epoch
= 0; /* not used */
4189 if (lc
->lcontext
.LeaseFlags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE
)
4190 return SMB2_OPLOCK_LEVEL_NOCHANGE
;
4191 return le32_to_cpu(lc
->lcontext
.LeaseState
);
4195 smb3_parse_lease_buf(void *buf
, unsigned int *epoch
, char *lease_key
)
4197 struct create_lease_v2
*lc
= (struct create_lease_v2
*)buf
;
4199 *epoch
= le16_to_cpu(lc
->lcontext
.Epoch
);
4200 if (lc
->lcontext
.LeaseFlags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE
)
4201 return SMB2_OPLOCK_LEVEL_NOCHANGE
;
4203 memcpy(lease_key
, &lc
->lcontext
.LeaseKey
, SMB2_LEASE_KEY_SIZE
);
4204 return le32_to_cpu(lc
->lcontext
.LeaseState
);
4208 smb2_wp_retry_size(struct inode
*inode
)
4210 return min_t(unsigned int, CIFS_SB(inode
->i_sb
)->ctx
->wsize
,
4211 SMB2_MAX_BUFFER_SIZE
);
4215 smb2_dir_needs_close(struct cifsFileInfo
*cfile
)
4217 return !cfile
->invalidHandle
;
4221 fill_transform_hdr(struct smb2_transform_hdr
*tr_hdr
, unsigned int orig_len
,
4222 struct smb_rqst
*old_rq
, __le16 cipher_type
)
4224 struct smb2_hdr
*shdr
=
4225 (struct smb2_hdr
*)old_rq
->rq_iov
[0].iov_base
;
4227 memset(tr_hdr
, 0, sizeof(struct smb2_transform_hdr
));
4228 tr_hdr
->ProtocolId
= SMB2_TRANSFORM_PROTO_NUM
;
4229 tr_hdr
->OriginalMessageSize
= cpu_to_le32(orig_len
);
4230 tr_hdr
->Flags
= cpu_to_le16(0x01);
4231 if ((cipher_type
== SMB2_ENCRYPTION_AES128_GCM
) ||
4232 (cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
4233 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_GCM_NONCE
);
4235 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_CCM_NONCE
);
4236 memcpy(&tr_hdr
->SessionId
, &shdr
->SessionId
, 8);
4239 static void *smb2_aead_req_alloc(struct crypto_aead
*tfm
, const struct smb_rqst
*rqst
,
4240 int num_rqst
, const u8
*sig
, u8
**iv
,
4241 struct aead_request
**req
, struct sg_table
*sgt
,
4242 unsigned int *num_sgs
, size_t *sensitive_size
)
4244 unsigned int req_size
= sizeof(**req
) + crypto_aead_reqsize(tfm
);
4245 unsigned int iv_size
= crypto_aead_ivsize(tfm
);
4249 *num_sgs
= cifs_get_num_sgs(rqst
, num_rqst
, sig
);
4250 if (IS_ERR_VALUE((long)(int)*num_sgs
))
4251 return ERR_PTR(*num_sgs
);
4254 len
+= crypto_aead_alignmask(tfm
) & ~(crypto_tfm_ctx_alignment() - 1);
4255 len
= ALIGN(len
, crypto_tfm_ctx_alignment());
4257 len
= ALIGN(len
, __alignof__(struct scatterlist
));
4258 len
+= array_size(*num_sgs
, sizeof(struct scatterlist
));
4259 *sensitive_size
= len
;
4261 p
= kvzalloc(len
, GFP_NOFS
);
4263 return ERR_PTR(-ENOMEM
);
4265 *iv
= (u8
*)PTR_ALIGN(p
, crypto_aead_alignmask(tfm
) + 1);
4266 *req
= (struct aead_request
*)PTR_ALIGN(*iv
+ iv_size
,
4267 crypto_tfm_ctx_alignment());
4268 sgt
->sgl
= (struct scatterlist
*)PTR_ALIGN((u8
*)*req
+ req_size
,
4269 __alignof__(struct scatterlist
));
4273 static void *smb2_get_aead_req(struct crypto_aead
*tfm
, struct smb_rqst
*rqst
,
4274 int num_rqst
, const u8
*sig
, u8
**iv
,
4275 struct aead_request
**req
, struct scatterlist
**sgl
,
4276 size_t *sensitive_size
)
4278 struct sg_table sgtable
= {};
4279 unsigned int skip
, num_sgs
, i
, j
;
4283 p
= smb2_aead_req_alloc(tfm
, rqst
, num_rqst
, sig
, iv
, req
, &sgtable
,
4284 &num_sgs
, sensitive_size
);
4288 sg_init_marker(sgtable
.sgl
, num_sgs
);
4291 * The first rqst has a transform header where the
4292 * first 20 bytes are not part of the encrypted blob.
4296 for (i
= 0; i
< num_rqst
; i
++) {
4297 struct iov_iter
*iter
= &rqst
[i
].rq_iter
;
4298 size_t count
= iov_iter_count(iter
);
4300 for (j
= 0; j
< rqst
[i
].rq_nvec
; j
++) {
4301 cifs_sg_set_buf(&sgtable
,
4302 rqst
[i
].rq_iov
[j
].iov_base
+ skip
,
4303 rqst
[i
].rq_iov
[j
].iov_len
- skip
);
4305 /* See the above comment on the 'skip' assignment */
4308 sgtable
.orig_nents
= sgtable
.nents
;
4310 rc
= extract_iter_to_sg(iter
, count
, &sgtable
,
4311 num_sgs
- sgtable
.nents
, 0);
4312 iov_iter_revert(iter
, rc
);
4313 sgtable
.orig_nents
= sgtable
.nents
;
4316 cifs_sg_set_buf(&sgtable
, sig
, SMB2_SIGNATURE_SIZE
);
4317 sg_mark_end(&sgtable
.sgl
[sgtable
.nents
- 1]);
4323 smb2_get_enc_key(struct TCP_Server_Info
*server
, __u64 ses_id
, int enc
, u8
*key
)
4325 struct TCP_Server_Info
*pserver
;
4326 struct cifs_ses
*ses
;
4329 /* If server is a channel, select the primary channel */
4330 pserver
= SERVER_IS_CHAN(server
) ? server
->primary_server
: server
;
4332 spin_lock(&cifs_tcp_ses_lock
);
4333 list_for_each_entry(ses
, &pserver
->smb_ses_list
, smb_ses_list
) {
4334 if (ses
->Suid
== ses_id
) {
4335 spin_lock(&ses
->ses_lock
);
4336 ses_enc_key
= enc
? ses
->smb3encryptionkey
:
4337 ses
->smb3decryptionkey
;
4338 memcpy(key
, ses_enc_key
, SMB3_ENC_DEC_KEY_SIZE
);
4339 spin_unlock(&ses
->ses_lock
);
4340 spin_unlock(&cifs_tcp_ses_lock
);
4344 spin_unlock(&cifs_tcp_ses_lock
);
4346 trace_smb3_ses_not_found(ses_id
);
4351 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4352 * iov[0] - transform header (associate data),
4353 * iov[1-N] - SMB2 header and pages - data to encrypt.
4354 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4358 crypt_message(struct TCP_Server_Info
*server
, int num_rqst
,
4359 struct smb_rqst
*rqst
, int enc
)
4361 struct smb2_transform_hdr
*tr_hdr
=
4362 (struct smb2_transform_hdr
*)rqst
[0].rq_iov
[0].iov_base
;
4363 unsigned int assoc_data_len
= sizeof(struct smb2_transform_hdr
) - 20;
4365 struct scatterlist
*sg
;
4366 u8 sign
[SMB2_SIGNATURE_SIZE
] = {};
4367 u8 key
[SMB3_ENC_DEC_KEY_SIZE
];
4368 struct aead_request
*req
;
4370 DECLARE_CRYPTO_WAIT(wait
);
4371 struct crypto_aead
*tfm
;
4372 unsigned int crypt_len
= le32_to_cpu(tr_hdr
->OriginalMessageSize
);
4374 size_t sensitive_size
;
4376 rc
= smb2_get_enc_key(server
, le64_to_cpu(tr_hdr
->SessionId
), enc
, key
);
4378 cifs_server_dbg(FYI
, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__
,
4379 enc
? "en" : "de", le64_to_cpu(tr_hdr
->SessionId
));
4383 rc
= smb3_crypto_aead_allocate(server
);
4385 cifs_server_dbg(VFS
, "%s: crypto alloc failed\n", __func__
);
4389 tfm
= enc
? server
->secmech
.enc
: server
->secmech
.dec
;
4391 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES256_CCM
) ||
4392 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
4393 rc
= crypto_aead_setkey(tfm
, key
, SMB3_GCM256_CRYPTKEY_SIZE
);
4395 rc
= crypto_aead_setkey(tfm
, key
, SMB3_GCM128_CRYPTKEY_SIZE
);
4398 cifs_server_dbg(VFS
, "%s: Failed to set aead key %d\n", __func__
, rc
);
4402 rc
= crypto_aead_setauthsize(tfm
, SMB2_SIGNATURE_SIZE
);
4404 cifs_server_dbg(VFS
, "%s: Failed to set authsize %d\n", __func__
, rc
);
4408 creq
= smb2_get_aead_req(tfm
, rqst
, num_rqst
, sign
, &iv
, &req
, &sg
,
4411 return PTR_ERR(creq
);
4414 memcpy(sign
, &tr_hdr
->Signature
, SMB2_SIGNATURE_SIZE
);
4415 crypt_len
+= SMB2_SIGNATURE_SIZE
;
4418 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES128_GCM
) ||
4419 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
4420 memcpy(iv
, (char *)tr_hdr
->Nonce
, SMB3_AES_GCM_NONCE
);
4423 memcpy(iv
+ 1, (char *)tr_hdr
->Nonce
, SMB3_AES_CCM_NONCE
);
4426 aead_request_set_tfm(req
, tfm
);
4427 aead_request_set_crypt(req
, sg
, sg
, crypt_len
, iv
);
4428 aead_request_set_ad(req
, assoc_data_len
);
4430 aead_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
4431 crypto_req_done
, &wait
);
4433 rc
= crypto_wait_req(enc
? crypto_aead_encrypt(req
)
4434 : crypto_aead_decrypt(req
), &wait
);
4437 memcpy(&tr_hdr
->Signature
, sign
, SMB2_SIGNATURE_SIZE
);
4439 kvfree_sensitive(creq
, sensitive_size
);
4444 * Clear a read buffer, discarding the folios which have XA_MARK_0 set.
4446 static void cifs_clear_xarray_buffer(struct xarray
*buffer
)
4448 struct folio
*folio
;
4450 XA_STATE(xas
, buffer
, 0);
4453 xas_for_each_marked(&xas
, folio
, ULONG_MAX
, XA_MARK_0
) {
4461 smb3_free_compound_rqst(int num_rqst
, struct smb_rqst
*rqst
)
4465 for (i
= 0; i
< num_rqst
; i
++)
4466 if (!xa_empty(&rqst
[i
].rq_buffer
))
4467 cifs_clear_xarray_buffer(&rqst
[i
].rq_buffer
);
4471 * This function will initialize new_rq and encrypt the content.
4472 * The first entry, new_rq[0], only contains a single iov which contains
4473 * a smb2_transform_hdr and is pre-allocated by the caller.
4474 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4476 * The end result is an array of smb_rqst structures where the first structure
4477 * only contains a single iov for the transform header which we then can pass
4478 * to crypt_message().
4480 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4481 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4484 smb3_init_transform_rq(struct TCP_Server_Info
*server
, int num_rqst
,
4485 struct smb_rqst
*new_rq
, struct smb_rqst
*old_rq
)
4487 struct smb2_transform_hdr
*tr_hdr
= new_rq
[0].rq_iov
[0].iov_base
;
4489 unsigned int orig_len
= 0;
4493 for (i
= 1; i
< num_rqst
; i
++) {
4494 struct smb_rqst
*old
= &old_rq
[i
- 1];
4495 struct smb_rqst
*new = &new_rq
[i
];
4496 struct xarray
*buffer
= &new->rq_buffer
;
4497 size_t size
= iov_iter_count(&old
->rq_iter
), seg
, copied
= 0;
4499 orig_len
+= smb_rqst_len(server
, old
);
4500 new->rq_iov
= old
->rq_iov
;
4501 new->rq_nvec
= old
->rq_nvec
;
4506 unsigned int npages
= DIV_ROUND_UP(size
, PAGE_SIZE
);
4508 for (j
= 0; j
< npages
; j
++) {
4512 page
= alloc_page(GFP_KERNEL
|__GFP_HIGHMEM
);
4516 o
= xa_store(buffer
, j
, page
, GFP_KERNEL
);
4523 xa_set_mark(buffer
, j
, XA_MARK_0
);
4525 seg
= min_t(size_t, size
- copied
, PAGE_SIZE
);
4526 if (copy_page_from_iter(page
, 0, seg
, &old
->rq_iter
) != seg
) {
4532 iov_iter_xarray(&new->rq_iter
, ITER_SOURCE
,
4534 new->rq_iter_size
= size
;
4538 /* fill the 1st iov with a transform header */
4539 fill_transform_hdr(tr_hdr
, orig_len
, old_rq
, server
->cipher_type
);
4541 rc
= crypt_message(server
, num_rqst
, new_rq
, 1);
4542 cifs_dbg(FYI
, "Encrypt message returned %d\n", rc
);
4549 smb3_free_compound_rqst(num_rqst
- 1, &new_rq
[1]);
4554 smb3_is_transform_hdr(void *buf
)
4556 struct smb2_transform_hdr
*trhdr
= buf
;
4558 return trhdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
;
4562 decrypt_raw_data(struct TCP_Server_Info
*server
, char *buf
,
4563 unsigned int buf_data_size
, struct iov_iter
*iter
,
4567 struct smb_rqst rqst
= {NULL
};
4568 size_t iter_size
= 0;
4571 iov
[0].iov_base
= buf
;
4572 iov
[0].iov_len
= sizeof(struct smb2_transform_hdr
);
4573 iov
[1].iov_base
= buf
+ sizeof(struct smb2_transform_hdr
);
4574 iov
[1].iov_len
= buf_data_size
;
4579 rqst
.rq_iter
= *iter
;
4580 rqst
.rq_iter_size
= iov_iter_count(iter
);
4581 iter_size
= iov_iter_count(iter
);
4584 rc
= crypt_message(server
, 1, &rqst
, 0);
4585 cifs_dbg(FYI
, "Decrypt message returned %d\n", rc
);
4590 memmove(buf
, iov
[1].iov_base
, buf_data_size
);
4593 server
->total_read
= buf_data_size
+ iter_size
;
4599 cifs_copy_pages_to_iter(struct xarray
*pages
, unsigned int data_size
,
4600 unsigned int skip
, struct iov_iter
*iter
)
4603 unsigned long index
;
4605 xa_for_each(pages
, index
, page
) {
4606 size_t n
, len
= min_t(unsigned int, PAGE_SIZE
- skip
, data_size
);
4608 n
= copy_page_to_iter(page
, skip
, len
, iter
);
4610 cifs_dbg(VFS
, "%s: something went wrong\n", __func__
);
4621 handle_read_data(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
,
4622 char *buf
, unsigned int buf_len
, struct xarray
*pages
,
4623 unsigned int pages_len
, bool is_offloaded
)
4625 unsigned int data_offset
;
4626 unsigned int data_len
;
4627 unsigned int cur_off
;
4628 unsigned int cur_page_idx
;
4629 unsigned int pad_len
;
4630 struct cifs_readdata
*rdata
= mid
->callback_data
;
4631 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
4633 bool use_rdma_mr
= false;
4635 if (shdr
->Command
!= SMB2_READ
) {
4636 cifs_server_dbg(VFS
, "only big read responses are supported\n");
4640 if (server
->ops
->is_session_expired
&&
4641 server
->ops
->is_session_expired(buf
)) {
4643 cifs_reconnect(server
, true);
4647 if (server
->ops
->is_status_pending
&&
4648 server
->ops
->is_status_pending(buf
, server
))
4651 /* set up first two iov to get credits */
4652 rdata
->iov
[0].iov_base
= buf
;
4653 rdata
->iov
[0].iov_len
= 0;
4654 rdata
->iov
[1].iov_base
= buf
;
4655 rdata
->iov
[1].iov_len
=
4656 min_t(unsigned int, buf_len
, server
->vals
->read_rsp_size
);
4657 cifs_dbg(FYI
, "0: iov_base=%p iov_len=%zu\n",
4658 rdata
->iov
[0].iov_base
, rdata
->iov
[0].iov_len
);
4659 cifs_dbg(FYI
, "1: iov_base=%p iov_len=%zu\n",
4660 rdata
->iov
[1].iov_base
, rdata
->iov
[1].iov_len
);
4662 rdata
->result
= server
->ops
->map_error(buf
, true);
4663 if (rdata
->result
!= 0) {
4664 cifs_dbg(FYI
, "%s: server returned error %d\n",
4665 __func__
, rdata
->result
);
4666 /* normal error on read response */
4668 mid
->mid_state
= MID_RESPONSE_RECEIVED
;
4670 dequeue_mid(mid
, false);
4674 data_offset
= server
->ops
->read_data_offset(buf
);
4675 #ifdef CONFIG_CIFS_SMB_DIRECT
4676 use_rdma_mr
= rdata
->mr
;
4678 data_len
= server
->ops
->read_data_length(buf
, use_rdma_mr
);
4680 if (data_offset
< server
->vals
->read_rsp_size
) {
4682 * win2k8 sometimes sends an offset of 0 when the read
4683 * is beyond the EOF. Treat it as if the data starts just after
4686 cifs_dbg(FYI
, "%s: data offset (%u) inside read response header\n",
4687 __func__
, data_offset
);
4688 data_offset
= server
->vals
->read_rsp_size
;
4689 } else if (data_offset
> MAX_CIFS_SMALL_BUFFER_SIZE
) {
4690 /* data_offset is beyond the end of smallbuf */
4691 cifs_dbg(FYI
, "%s: data offset (%u) beyond end of smallbuf\n",
4692 __func__
, data_offset
);
4693 rdata
->result
= -EIO
;
4695 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4697 dequeue_mid(mid
, rdata
->result
);
4701 pad_len
= data_offset
- server
->vals
->read_rsp_size
;
4703 if (buf_len
<= data_offset
) {
4704 /* read response payload is in pages */
4705 cur_page_idx
= pad_len
/ PAGE_SIZE
;
4706 cur_off
= pad_len
% PAGE_SIZE
;
4708 if (cur_page_idx
!= 0) {
4709 /* data offset is beyond the 1st page of response */
4710 cifs_dbg(FYI
, "%s: data offset (%u) beyond 1st page of response\n",
4711 __func__
, data_offset
);
4712 rdata
->result
= -EIO
;
4714 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4716 dequeue_mid(mid
, rdata
->result
);
4720 if (data_len
> pages_len
- pad_len
) {
4721 /* data_len is corrupt -- discard frame */
4722 rdata
->result
= -EIO
;
4724 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4726 dequeue_mid(mid
, rdata
->result
);
4730 /* Copy the data to the output I/O iterator. */
4731 rdata
->result
= cifs_copy_pages_to_iter(pages
, pages_len
,
4732 cur_off
, &rdata
->iter
);
4733 if (rdata
->result
!= 0) {
4735 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4737 dequeue_mid(mid
, rdata
->result
);
4740 rdata
->got_bytes
= pages_len
;
4742 } else if (buf_len
>= data_offset
+ data_len
) {
4743 /* read response payload is in buf */
4744 WARN_ONCE(pages
&& !xa_empty(pages
),
4745 "read data can be either in buf or in pages");
4746 length
= copy_to_iter(buf
+ data_offset
, data_len
, &rdata
->iter
);
4749 rdata
->got_bytes
= data_len
;
4751 /* read response payload cannot be in both buf and pages */
4752 WARN_ONCE(1, "buf can not contain only a part of read data");
4753 rdata
->result
= -EIO
;
4755 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4757 dequeue_mid(mid
, rdata
->result
);
4762 mid
->mid_state
= MID_RESPONSE_RECEIVED
;
4764 dequeue_mid(mid
, false);
4768 struct smb2_decrypt_work
{
4769 struct work_struct decrypt
;
4770 struct TCP_Server_Info
*server
;
4771 struct xarray buffer
;
4777 static void smb2_decrypt_offload(struct work_struct
*work
)
4779 struct smb2_decrypt_work
*dw
= container_of(work
,
4780 struct smb2_decrypt_work
, decrypt
);
4782 struct mid_q_entry
*mid
;
4783 struct iov_iter iter
;
4785 iov_iter_xarray(&iter
, ITER_DEST
, &dw
->buffer
, 0, dw
->len
);
4786 rc
= decrypt_raw_data(dw
->server
, dw
->buf
, dw
->server
->vals
->read_rsp_size
,
4789 cifs_dbg(VFS
, "error decrypting rc=%d\n", rc
);
4793 dw
->server
->lstrp
= jiffies
;
4794 mid
= smb2_find_dequeue_mid(dw
->server
, dw
->buf
);
4796 cifs_dbg(FYI
, "mid not found\n");
4798 mid
->decrypted
= true;
4799 rc
= handle_read_data(dw
->server
, mid
, dw
->buf
,
4800 dw
->server
->vals
->read_rsp_size
,
4801 &dw
->buffer
, dw
->len
,
4804 #ifdef CONFIG_CIFS_STATS2
4805 mid
->when_received
= jiffies
;
4807 if (dw
->server
->ops
->is_network_name_deleted
)
4808 dw
->server
->ops
->is_network_name_deleted(dw
->buf
,
4813 spin_lock(&dw
->server
->srv_lock
);
4814 if (dw
->server
->tcpStatus
== CifsNeedReconnect
) {
4815 spin_lock(&dw
->server
->mid_lock
);
4816 mid
->mid_state
= MID_RETRY_NEEDED
;
4817 spin_unlock(&dw
->server
->mid_lock
);
4818 spin_unlock(&dw
->server
->srv_lock
);
4821 spin_lock(&dw
->server
->mid_lock
);
4822 mid
->mid_state
= MID_REQUEST_SUBMITTED
;
4823 mid
->mid_flags
&= ~(MID_DELETED
);
4824 list_add_tail(&mid
->qhead
,
4825 &dw
->server
->pending_mid_q
);
4826 spin_unlock(&dw
->server
->mid_lock
);
4827 spin_unlock(&dw
->server
->srv_lock
);
4834 cifs_clear_xarray_buffer(&dw
->buffer
);
4835 cifs_small_buf_release(dw
->buf
);
4841 receive_encrypted_read(struct TCP_Server_Info
*server
, struct mid_q_entry
**mid
,
4845 char *buf
= server
->smallbuf
;
4846 struct smb2_transform_hdr
*tr_hdr
= (struct smb2_transform_hdr
*)buf
;
4847 struct iov_iter iter
;
4848 unsigned int len
, npages
;
4849 unsigned int buflen
= server
->pdu_size
;
4852 struct smb2_decrypt_work
*dw
;
4854 dw
= kzalloc(sizeof(struct smb2_decrypt_work
), GFP_KERNEL
);
4857 xa_init(&dw
->buffer
);
4858 INIT_WORK(&dw
->decrypt
, smb2_decrypt_offload
);
4859 dw
->server
= server
;
4862 len
= min_t(unsigned int, buflen
, server
->vals
->read_rsp_size
+
4863 sizeof(struct smb2_transform_hdr
)) - HEADER_SIZE(server
) + 1;
4865 rc
= cifs_read_from_socket(server
, buf
+ HEADER_SIZE(server
) - 1, len
);
4868 server
->total_read
+= rc
;
4870 len
= le32_to_cpu(tr_hdr
->OriginalMessageSize
) -
4871 server
->vals
->read_rsp_size
;
4873 npages
= DIV_ROUND_UP(len
, PAGE_SIZE
);
4876 for (; i
< npages
; i
++) {
4879 page
= alloc_page(GFP_KERNEL
|__GFP_HIGHMEM
);
4883 old
= xa_store(&dw
->buffer
, i
, page
, GFP_KERNEL
);
4884 if (xa_is_err(old
)) {
4889 xa_set_mark(&dw
->buffer
, i
, XA_MARK_0
);
4892 iov_iter_xarray(&iter
, ITER_DEST
, &dw
->buffer
, 0, npages
* PAGE_SIZE
);
4894 /* Read the data into the buffer and clear excess bufferage. */
4895 rc
= cifs_read_iter_from_socket(server
, &iter
, dw
->len
);
4899 server
->total_read
+= rc
;
4900 if (rc
< npages
* PAGE_SIZE
)
4901 iov_iter_zero(npages
* PAGE_SIZE
- rc
, &iter
);
4902 iov_iter_revert(&iter
, npages
* PAGE_SIZE
);
4903 iov_iter_truncate(&iter
, dw
->len
);
4905 rc
= cifs_discard_remaining_data(server
);
4910 * For large reads, offload to different thread for better performance,
4911 * use more cores decrypting which can be expensive
4914 if ((server
->min_offload
) && (server
->in_flight
> 1) &&
4915 (server
->pdu_size
>= server
->min_offload
)) {
4916 dw
->buf
= server
->smallbuf
;
4917 server
->smallbuf
= (char *)cifs_small_buf_get();
4919 queue_work(decrypt_wq
, &dw
->decrypt
);
4920 *num_mids
= 0; /* worker thread takes care of finding mid */
4924 rc
= decrypt_raw_data(server
, buf
, server
->vals
->read_rsp_size
,
4929 *mid
= smb2_find_mid(server
, buf
);
4931 cifs_dbg(FYI
, "mid not found\n");
4933 cifs_dbg(FYI
, "mid found\n");
4934 (*mid
)->decrypted
= true;
4935 rc
= handle_read_data(server
, *mid
, buf
,
4936 server
->vals
->read_rsp_size
,
4937 &dw
->buffer
, dw
->len
, false);
4939 if (server
->ops
->is_network_name_deleted
) {
4940 server
->ops
->is_network_name_deleted(buf
,
4947 cifs_clear_xarray_buffer(&dw
->buffer
);
4952 cifs_discard_remaining_data(server
);
4957 receive_encrypted_standard(struct TCP_Server_Info
*server
,
4958 struct mid_q_entry
**mids
, char **bufs
,
4962 char *buf
= server
->smallbuf
;
4963 struct smb2_hdr
*shdr
;
4964 unsigned int pdu_length
= server
->pdu_size
;
4965 unsigned int buf_size
;
4966 unsigned int next_cmd
;
4967 struct mid_q_entry
*mid_entry
;
4969 char *next_buffer
= NULL
;
4973 /* switch to large buffer if too big for a small one */
4974 if (pdu_length
> MAX_CIFS_SMALL_BUFFER_SIZE
) {
4975 server
->large_buf
= true;
4976 memcpy(server
->bigbuf
, buf
, server
->total_read
);
4977 buf
= server
->bigbuf
;
4980 /* now read the rest */
4981 length
= cifs_read_from_socket(server
, buf
+ HEADER_SIZE(server
) - 1,
4982 pdu_length
- HEADER_SIZE(server
) + 1);
4985 server
->total_read
+= length
;
4987 buf_size
= pdu_length
- sizeof(struct smb2_transform_hdr
);
4988 length
= decrypt_raw_data(server
, buf
, buf_size
, NULL
, false);
4992 next_is_large
= server
->large_buf
;
4994 shdr
= (struct smb2_hdr
*)buf
;
4995 next_cmd
= le32_to_cpu(shdr
->NextCommand
);
4997 if (WARN_ON_ONCE(next_cmd
> pdu_length
))
5000 next_buffer
= (char *)cifs_buf_get();
5002 next_buffer
= (char *)cifs_small_buf_get();
5003 memcpy(next_buffer
, buf
+ next_cmd
, pdu_length
- next_cmd
);
5006 mid_entry
= smb2_find_mid(server
, buf
);
5007 if (mid_entry
== NULL
)
5008 cifs_dbg(FYI
, "mid not found\n");
5010 cifs_dbg(FYI
, "mid found\n");
5011 mid_entry
->decrypted
= true;
5012 mid_entry
->resp_buf_size
= server
->pdu_size
;
5015 if (*num_mids
>= MAX_COMPOUND
) {
5016 cifs_server_dbg(VFS
, "too many PDUs in compound\n");
5019 bufs
[*num_mids
] = buf
;
5020 mids
[(*num_mids
)++] = mid_entry
;
5022 if (mid_entry
&& mid_entry
->handle
)
5023 ret
= mid_entry
->handle(server
, mid_entry
);
5025 ret
= cifs_handle_standard(server
, mid_entry
);
5027 if (ret
== 0 && next_cmd
) {
5028 pdu_length
-= next_cmd
;
5029 server
->large_buf
= next_is_large
;
5031 server
->bigbuf
= buf
= next_buffer
;
5033 server
->smallbuf
= buf
= next_buffer
;
5035 } else if (ret
!= 0) {
5037 * ret != 0 here means that we didn't get to handle_mid() thus
5038 * server->smallbuf and server->bigbuf are still valid. We need
5039 * to free next_buffer because it is not going to be used
5043 free_rsp_buf(CIFS_LARGE_BUFFER
, next_buffer
);
5045 free_rsp_buf(CIFS_SMALL_BUFFER
, next_buffer
);
5052 smb3_receive_transform(struct TCP_Server_Info
*server
,
5053 struct mid_q_entry
**mids
, char **bufs
, int *num_mids
)
5055 char *buf
= server
->smallbuf
;
5056 unsigned int pdu_length
= server
->pdu_size
;
5057 struct smb2_transform_hdr
*tr_hdr
= (struct smb2_transform_hdr
*)buf
;
5058 unsigned int orig_len
= le32_to_cpu(tr_hdr
->OriginalMessageSize
);
5060 if (pdu_length
< sizeof(struct smb2_transform_hdr
) +
5061 sizeof(struct smb2_hdr
)) {
5062 cifs_server_dbg(VFS
, "Transform message is too small (%u)\n",
5064 cifs_reconnect(server
, true);
5065 return -ECONNABORTED
;
5068 if (pdu_length
< orig_len
+ sizeof(struct smb2_transform_hdr
)) {
5069 cifs_server_dbg(VFS
, "Transform message is broken\n");
5070 cifs_reconnect(server
, true);
5071 return -ECONNABORTED
;
5074 /* TODO: add support for compounds containing READ. */
5075 if (pdu_length
> CIFSMaxBufSize
+ MAX_HEADER_SIZE(server
)) {
5076 return receive_encrypted_read(server
, &mids
[0], num_mids
);
5079 return receive_encrypted_standard(server
, mids
, bufs
, num_mids
);
5083 smb3_handle_read_data(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
)
5085 char *buf
= server
->large_buf
? server
->bigbuf
: server
->smallbuf
;
5087 return handle_read_data(server
, mid
, buf
, server
->pdu_size
,
5091 static int smb2_next_header(struct TCP_Server_Info
*server
, char *buf
,
5094 struct smb2_hdr
*hdr
= (struct smb2_hdr
*)buf
;
5095 struct smb2_transform_hdr
*t_hdr
= (struct smb2_transform_hdr
*)buf
;
5097 if (hdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
) {
5098 *noff
= le32_to_cpu(t_hdr
->OriginalMessageSize
);
5099 if (unlikely(check_add_overflow(*noff
, sizeof(*t_hdr
), noff
)))
5102 *noff
= le32_to_cpu(hdr
->NextCommand
);
5104 if (unlikely(*noff
&& *noff
< MID_HEADER_SIZE(server
)))
5109 int cifs_sfu_make_node(unsigned int xid
, struct inode
*inode
,
5110 struct dentry
*dentry
, struct cifs_tcon
*tcon
,
5111 const char *full_path
, umode_t mode
, dev_t dev
)
5113 struct cifs_open_info_data buf
= {};
5114 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
5115 struct cifs_open_parms oparms
;
5116 struct cifs_io_parms io_parms
= {};
5117 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
5118 struct cifs_fid fid
;
5119 unsigned int bytes_written
;
5120 struct win_dev
*pdev
;
5122 __u32 oplock
= server
->oplocks
? REQ_OPLOCK
: 0;
5125 if (!S_ISCHR(mode
) && !S_ISBLK(mode
) && !S_ISFIFO(mode
))
5128 oparms
= (struct cifs_open_parms
) {
5131 .desired_access
= GENERIC_WRITE
,
5132 .create_options
= cifs_create_options(cifs_sb
, CREATE_NOT_DIR
|
5133 CREATE_OPTION_SPECIAL
),
5134 .disposition
= FILE_CREATE
,
5139 rc
= server
->ops
->open(xid
, &oparms
, &oplock
, &buf
);
5144 * BB Do not bother to decode buf since no local inode yet to put
5145 * timestamps in, but we can reuse it safely.
5147 pdev
= (struct win_dev
*)&buf
.fi
;
5148 io_parms
.pid
= current
->tgid
;
5149 io_parms
.tcon
= tcon
;
5150 io_parms
.length
= sizeof(*pdev
);
5151 iov
[1].iov_base
= pdev
;
5152 iov
[1].iov_len
= sizeof(*pdev
);
5153 if (S_ISCHR(mode
)) {
5154 memcpy(pdev
->type
, "IntxCHR", 8);
5155 pdev
->major
= cpu_to_le64(MAJOR(dev
));
5156 pdev
->minor
= cpu_to_le64(MINOR(dev
));
5157 } else if (S_ISBLK(mode
)) {
5158 memcpy(pdev
->type
, "IntxBLK", 8);
5159 pdev
->major
= cpu_to_le64(MAJOR(dev
));
5160 pdev
->minor
= cpu_to_le64(MINOR(dev
));
5161 } else if (S_ISFIFO(mode
)) {
5162 memcpy(pdev
->type
, "LnxFIFO", 8);
5165 rc
= server
->ops
->sync_write(xid
, &fid
, &io_parms
,
5166 &bytes_written
, iov
, 1);
5167 server
->ops
->close(xid
, tcon
, &fid
);
5169 /* FIXME: add code here to set EAs */
5170 cifs_free_open_info(&buf
);
5174 static int smb2_make_node(unsigned int xid
, struct inode
*inode
,
5175 struct dentry
*dentry
, struct cifs_tcon
*tcon
,
5176 const char *full_path
, umode_t mode
, dev_t dev
)
5178 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
5181 * Check if mounted with mount parm 'sfu' mount parm.
5182 * SFU emulation should work with all servers, but only
5183 * supports block and char device (no socket & fifo),
5184 * and was used by default in earlier versions of Windows
5186 if (!(cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
))
5189 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5190 * their current NFS server) uses this approach to expose special files
5191 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5193 return cifs_sfu_make_node(xid
, inode
, dentry
, tcon
,
5194 full_path
, mode
, dev
);
5197 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5198 struct smb_version_operations smb20_operations
= {
5199 .compare_fids
= smb2_compare_fids
,
5200 .setup_request
= smb2_setup_request
,
5201 .setup_async_request
= smb2_setup_async_request
,
5202 .check_receive
= smb2_check_receive
,
5203 .add_credits
= smb2_add_credits
,
5204 .set_credits
= smb2_set_credits
,
5205 .get_credits_field
= smb2_get_credits_field
,
5206 .get_credits
= smb2_get_credits
,
5207 .wait_mtu_credits
= cifs_wait_mtu_credits
,
5208 .get_next_mid
= smb2_get_next_mid
,
5209 .revert_current_mid
= smb2_revert_current_mid
,
5210 .read_data_offset
= smb2_read_data_offset
,
5211 .read_data_length
= smb2_read_data_length
,
5212 .map_error
= map_smb2_to_linux_error
,
5213 .find_mid
= smb2_find_mid
,
5214 .check_message
= smb2_check_message
,
5215 .dump_detail
= smb2_dump_detail
,
5216 .clear_stats
= smb2_clear_stats
,
5217 .print_stats
= smb2_print_stats
,
5218 .is_oplock_break
= smb2_is_valid_oplock_break
,
5219 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5220 .downgrade_oplock
= smb2_downgrade_oplock
,
5221 .need_neg
= smb2_need_neg
,
5222 .negotiate
= smb2_negotiate
,
5223 .negotiate_wsize
= smb2_negotiate_wsize
,
5224 .negotiate_rsize
= smb2_negotiate_rsize
,
5225 .sess_setup
= SMB2_sess_setup
,
5226 .logoff
= SMB2_logoff
,
5227 .tree_connect
= SMB2_tcon
,
5228 .tree_disconnect
= SMB2_tdis
,
5229 .qfs_tcon
= smb2_qfs_tcon
,
5230 .is_path_accessible
= smb2_is_path_accessible
,
5231 .can_echo
= smb2_can_echo
,
5233 .query_path_info
= smb2_query_path_info
,
5234 .query_reparse_point
= smb2_query_reparse_point
,
5235 .get_srv_inum
= smb2_get_srv_inum
,
5236 .query_file_info
= smb2_query_file_info
,
5237 .set_path_size
= smb2_set_path_size
,
5238 .set_file_size
= smb2_set_file_size
,
5239 .set_file_info
= smb2_set_file_info
,
5240 .set_compression
= smb2_set_compression
,
5241 .mkdir
= smb2_mkdir
,
5242 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5243 .rmdir
= smb2_rmdir
,
5244 .unlink
= smb2_unlink
,
5245 .rename
= smb2_rename_path
,
5246 .create_hardlink
= smb2_create_hardlink
,
5247 .parse_reparse_point
= smb2_parse_reparse_point
,
5248 .query_mf_symlink
= smb3_query_mf_symlink
,
5249 .create_mf_symlink
= smb3_create_mf_symlink
,
5250 .open
= smb2_open_file
,
5251 .set_fid
= smb2_set_fid
,
5252 .close
= smb2_close_file
,
5253 .flush
= smb2_flush_file
,
5254 .async_readv
= smb2_async_readv
,
5255 .async_writev
= smb2_async_writev
,
5256 .sync_read
= smb2_sync_read
,
5257 .sync_write
= smb2_sync_write
,
5258 .query_dir_first
= smb2_query_dir_first
,
5259 .query_dir_next
= smb2_query_dir_next
,
5260 .close_dir
= smb2_close_dir
,
5261 .calc_smb_size
= smb2_calc_size
,
5262 .is_status_pending
= smb2_is_status_pending
,
5263 .is_session_expired
= smb2_is_session_expired
,
5264 .oplock_response
= smb2_oplock_response
,
5265 .queryfs
= smb2_queryfs
,
5266 .mand_lock
= smb2_mand_lock
,
5267 .mand_unlock_range
= smb2_unlock_range
,
5268 .push_mand_locks
= smb2_push_mandatory_locks
,
5269 .get_lease_key
= smb2_get_lease_key
,
5270 .set_lease_key
= smb2_set_lease_key
,
5271 .new_lease_key
= smb2_new_lease_key
,
5272 .calc_signature
= smb2_calc_signature
,
5273 .is_read_op
= smb2_is_read_op
,
5274 .set_oplock_level
= smb2_set_oplock_level
,
5275 .create_lease_buf
= smb2_create_lease_buf
,
5276 .parse_lease_buf
= smb2_parse_lease_buf
,
5277 .copychunk_range
= smb2_copychunk_range
,
5278 .wp_retry_size
= smb2_wp_retry_size
,
5279 .dir_needs_close
= smb2_dir_needs_close
,
5280 .get_dfs_refer
= smb2_get_dfs_refer
,
5281 .select_sectype
= smb2_select_sectype
,
5282 #ifdef CONFIG_CIFS_XATTR
5283 .query_all_EAs
= smb2_query_eas
,
5284 .set_EA
= smb2_set_ea
,
5285 #endif /* CIFS_XATTR */
5286 .get_acl
= get_smb2_acl
,
5287 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5288 .set_acl
= set_smb2_acl
,
5289 .next_header
= smb2_next_header
,
5290 .ioctl_query_info
= smb2_ioctl_query_info
,
5291 .make_node
= smb2_make_node
,
5292 .fiemap
= smb3_fiemap
,
5293 .llseek
= smb3_llseek
,
5294 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5295 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5297 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5299 struct smb_version_operations smb21_operations
= {
5300 .compare_fids
= smb2_compare_fids
,
5301 .setup_request
= smb2_setup_request
,
5302 .setup_async_request
= smb2_setup_async_request
,
5303 .check_receive
= smb2_check_receive
,
5304 .add_credits
= smb2_add_credits
,
5305 .set_credits
= smb2_set_credits
,
5306 .get_credits_field
= smb2_get_credits_field
,
5307 .get_credits
= smb2_get_credits
,
5308 .wait_mtu_credits
= smb2_wait_mtu_credits
,
5309 .adjust_credits
= smb2_adjust_credits
,
5310 .get_next_mid
= smb2_get_next_mid
,
5311 .revert_current_mid
= smb2_revert_current_mid
,
5312 .read_data_offset
= smb2_read_data_offset
,
5313 .read_data_length
= smb2_read_data_length
,
5314 .map_error
= map_smb2_to_linux_error
,
5315 .find_mid
= smb2_find_mid
,
5316 .check_message
= smb2_check_message
,
5317 .dump_detail
= smb2_dump_detail
,
5318 .clear_stats
= smb2_clear_stats
,
5319 .print_stats
= smb2_print_stats
,
5320 .is_oplock_break
= smb2_is_valid_oplock_break
,
5321 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5322 .downgrade_oplock
= smb2_downgrade_oplock
,
5323 .need_neg
= smb2_need_neg
,
5324 .negotiate
= smb2_negotiate
,
5325 .negotiate_wsize
= smb2_negotiate_wsize
,
5326 .negotiate_rsize
= smb2_negotiate_rsize
,
5327 .sess_setup
= SMB2_sess_setup
,
5328 .logoff
= SMB2_logoff
,
5329 .tree_connect
= SMB2_tcon
,
5330 .tree_disconnect
= SMB2_tdis
,
5331 .qfs_tcon
= smb2_qfs_tcon
,
5332 .is_path_accessible
= smb2_is_path_accessible
,
5333 .can_echo
= smb2_can_echo
,
5335 .query_path_info
= smb2_query_path_info
,
5336 .query_reparse_point
= smb2_query_reparse_point
,
5337 .get_srv_inum
= smb2_get_srv_inum
,
5338 .query_file_info
= smb2_query_file_info
,
5339 .set_path_size
= smb2_set_path_size
,
5340 .set_file_size
= smb2_set_file_size
,
5341 .set_file_info
= smb2_set_file_info
,
5342 .set_compression
= smb2_set_compression
,
5343 .mkdir
= smb2_mkdir
,
5344 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5345 .rmdir
= smb2_rmdir
,
5346 .unlink
= smb2_unlink
,
5347 .rename
= smb2_rename_path
,
5348 .create_hardlink
= smb2_create_hardlink
,
5349 .parse_reparse_point
= smb2_parse_reparse_point
,
5350 .query_mf_symlink
= smb3_query_mf_symlink
,
5351 .create_mf_symlink
= smb3_create_mf_symlink
,
5352 .open
= smb2_open_file
,
5353 .set_fid
= smb2_set_fid
,
5354 .close
= smb2_close_file
,
5355 .flush
= smb2_flush_file
,
5356 .async_readv
= smb2_async_readv
,
5357 .async_writev
= smb2_async_writev
,
5358 .sync_read
= smb2_sync_read
,
5359 .sync_write
= smb2_sync_write
,
5360 .query_dir_first
= smb2_query_dir_first
,
5361 .query_dir_next
= smb2_query_dir_next
,
5362 .close_dir
= smb2_close_dir
,
5363 .calc_smb_size
= smb2_calc_size
,
5364 .is_status_pending
= smb2_is_status_pending
,
5365 .is_session_expired
= smb2_is_session_expired
,
5366 .oplock_response
= smb2_oplock_response
,
5367 .queryfs
= smb2_queryfs
,
5368 .mand_lock
= smb2_mand_lock
,
5369 .mand_unlock_range
= smb2_unlock_range
,
5370 .push_mand_locks
= smb2_push_mandatory_locks
,
5371 .get_lease_key
= smb2_get_lease_key
,
5372 .set_lease_key
= smb2_set_lease_key
,
5373 .new_lease_key
= smb2_new_lease_key
,
5374 .calc_signature
= smb2_calc_signature
,
5375 .is_read_op
= smb21_is_read_op
,
5376 .set_oplock_level
= smb21_set_oplock_level
,
5377 .create_lease_buf
= smb2_create_lease_buf
,
5378 .parse_lease_buf
= smb2_parse_lease_buf
,
5379 .copychunk_range
= smb2_copychunk_range
,
5380 .wp_retry_size
= smb2_wp_retry_size
,
5381 .dir_needs_close
= smb2_dir_needs_close
,
5382 .enum_snapshots
= smb3_enum_snapshots
,
5383 .notify
= smb3_notify
,
5384 .get_dfs_refer
= smb2_get_dfs_refer
,
5385 .select_sectype
= smb2_select_sectype
,
5386 #ifdef CONFIG_CIFS_XATTR
5387 .query_all_EAs
= smb2_query_eas
,
5388 .set_EA
= smb2_set_ea
,
5389 #endif /* CIFS_XATTR */
5390 .get_acl
= get_smb2_acl
,
5391 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5392 .set_acl
= set_smb2_acl
,
5393 .next_header
= smb2_next_header
,
5394 .ioctl_query_info
= smb2_ioctl_query_info
,
5395 .make_node
= smb2_make_node
,
5396 .fiemap
= smb3_fiemap
,
5397 .llseek
= smb3_llseek
,
5398 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5399 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5402 struct smb_version_operations smb30_operations
= {
5403 .compare_fids
= smb2_compare_fids
,
5404 .setup_request
= smb2_setup_request
,
5405 .setup_async_request
= smb2_setup_async_request
,
5406 .check_receive
= smb2_check_receive
,
5407 .add_credits
= smb2_add_credits
,
5408 .set_credits
= smb2_set_credits
,
5409 .get_credits_field
= smb2_get_credits_field
,
5410 .get_credits
= smb2_get_credits
,
5411 .wait_mtu_credits
= smb2_wait_mtu_credits
,
5412 .adjust_credits
= smb2_adjust_credits
,
5413 .get_next_mid
= smb2_get_next_mid
,
5414 .revert_current_mid
= smb2_revert_current_mid
,
5415 .read_data_offset
= smb2_read_data_offset
,
5416 .read_data_length
= smb2_read_data_length
,
5417 .map_error
= map_smb2_to_linux_error
,
5418 .find_mid
= smb2_find_mid
,
5419 .check_message
= smb2_check_message
,
5420 .dump_detail
= smb2_dump_detail
,
5421 .clear_stats
= smb2_clear_stats
,
5422 .print_stats
= smb2_print_stats
,
5423 .dump_share_caps
= smb2_dump_share_caps
,
5424 .is_oplock_break
= smb2_is_valid_oplock_break
,
5425 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5426 .downgrade_oplock
= smb3_downgrade_oplock
,
5427 .need_neg
= smb2_need_neg
,
5428 .negotiate
= smb2_negotiate
,
5429 .negotiate_wsize
= smb3_negotiate_wsize
,
5430 .negotiate_rsize
= smb3_negotiate_rsize
,
5431 .sess_setup
= SMB2_sess_setup
,
5432 .logoff
= SMB2_logoff
,
5433 .tree_connect
= SMB2_tcon
,
5434 .tree_disconnect
= SMB2_tdis
,
5435 .qfs_tcon
= smb3_qfs_tcon
,
5436 .is_path_accessible
= smb2_is_path_accessible
,
5437 .can_echo
= smb2_can_echo
,
5439 .query_path_info
= smb2_query_path_info
,
5440 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5441 .query_reparse_point
= smb2_query_reparse_point
,
5442 .get_srv_inum
= smb2_get_srv_inum
,
5443 .query_file_info
= smb2_query_file_info
,
5444 .set_path_size
= smb2_set_path_size
,
5445 .set_file_size
= smb2_set_file_size
,
5446 .set_file_info
= smb2_set_file_info
,
5447 .set_compression
= smb2_set_compression
,
5448 .mkdir
= smb2_mkdir
,
5449 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5450 .rmdir
= smb2_rmdir
,
5451 .unlink
= smb2_unlink
,
5452 .rename
= smb2_rename_path
,
5453 .create_hardlink
= smb2_create_hardlink
,
5454 .parse_reparse_point
= smb2_parse_reparse_point
,
5455 .query_mf_symlink
= smb3_query_mf_symlink
,
5456 .create_mf_symlink
= smb3_create_mf_symlink
,
5457 .open
= smb2_open_file
,
5458 .set_fid
= smb2_set_fid
,
5459 .close
= smb2_close_file
,
5460 .close_getattr
= smb2_close_getattr
,
5461 .flush
= smb2_flush_file
,
5462 .async_readv
= smb2_async_readv
,
5463 .async_writev
= smb2_async_writev
,
5464 .sync_read
= smb2_sync_read
,
5465 .sync_write
= smb2_sync_write
,
5466 .query_dir_first
= smb2_query_dir_first
,
5467 .query_dir_next
= smb2_query_dir_next
,
5468 .close_dir
= smb2_close_dir
,
5469 .calc_smb_size
= smb2_calc_size
,
5470 .is_status_pending
= smb2_is_status_pending
,
5471 .is_session_expired
= smb2_is_session_expired
,
5472 .oplock_response
= smb2_oplock_response
,
5473 .queryfs
= smb2_queryfs
,
5474 .mand_lock
= smb2_mand_lock
,
5475 .mand_unlock_range
= smb2_unlock_range
,
5476 .push_mand_locks
= smb2_push_mandatory_locks
,
5477 .get_lease_key
= smb2_get_lease_key
,
5478 .set_lease_key
= smb2_set_lease_key
,
5479 .new_lease_key
= smb2_new_lease_key
,
5480 .generate_signingkey
= generate_smb30signingkey
,
5481 .calc_signature
= smb3_calc_signature
,
5482 .set_integrity
= smb3_set_integrity
,
5483 .is_read_op
= smb21_is_read_op
,
5484 .set_oplock_level
= smb3_set_oplock_level
,
5485 .create_lease_buf
= smb3_create_lease_buf
,
5486 .parse_lease_buf
= smb3_parse_lease_buf
,
5487 .copychunk_range
= smb2_copychunk_range
,
5488 .duplicate_extents
= smb2_duplicate_extents
,
5489 .validate_negotiate
= smb3_validate_negotiate
,
5490 .wp_retry_size
= smb2_wp_retry_size
,
5491 .dir_needs_close
= smb2_dir_needs_close
,
5492 .fallocate
= smb3_fallocate
,
5493 .enum_snapshots
= smb3_enum_snapshots
,
5494 .notify
= smb3_notify
,
5495 .init_transform_rq
= smb3_init_transform_rq
,
5496 .is_transform_hdr
= smb3_is_transform_hdr
,
5497 .receive_transform
= smb3_receive_transform
,
5498 .get_dfs_refer
= smb2_get_dfs_refer
,
5499 .select_sectype
= smb2_select_sectype
,
5500 #ifdef CONFIG_CIFS_XATTR
5501 .query_all_EAs
= smb2_query_eas
,
5502 .set_EA
= smb2_set_ea
,
5503 #endif /* CIFS_XATTR */
5504 .get_acl
= get_smb2_acl
,
5505 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5506 .set_acl
= set_smb2_acl
,
5507 .next_header
= smb2_next_header
,
5508 .ioctl_query_info
= smb2_ioctl_query_info
,
5509 .make_node
= smb2_make_node
,
5510 .fiemap
= smb3_fiemap
,
5511 .llseek
= smb3_llseek
,
5512 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5513 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5516 struct smb_version_operations smb311_operations
= {
5517 .compare_fids
= smb2_compare_fids
,
5518 .setup_request
= smb2_setup_request
,
5519 .setup_async_request
= smb2_setup_async_request
,
5520 .check_receive
= smb2_check_receive
,
5521 .add_credits
= smb2_add_credits
,
5522 .set_credits
= smb2_set_credits
,
5523 .get_credits_field
= smb2_get_credits_field
,
5524 .get_credits
= smb2_get_credits
,
5525 .wait_mtu_credits
= smb2_wait_mtu_credits
,
5526 .adjust_credits
= smb2_adjust_credits
,
5527 .get_next_mid
= smb2_get_next_mid
,
5528 .revert_current_mid
= smb2_revert_current_mid
,
5529 .read_data_offset
= smb2_read_data_offset
,
5530 .read_data_length
= smb2_read_data_length
,
5531 .map_error
= map_smb2_to_linux_error
,
5532 .find_mid
= smb2_find_mid
,
5533 .check_message
= smb2_check_message
,
5534 .dump_detail
= smb2_dump_detail
,
5535 .clear_stats
= smb2_clear_stats
,
5536 .print_stats
= smb2_print_stats
,
5537 .dump_share_caps
= smb2_dump_share_caps
,
5538 .is_oplock_break
= smb2_is_valid_oplock_break
,
5539 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5540 .downgrade_oplock
= smb3_downgrade_oplock
,
5541 .need_neg
= smb2_need_neg
,
5542 .negotiate
= smb2_negotiate
,
5543 .negotiate_wsize
= smb3_negotiate_wsize
,
5544 .negotiate_rsize
= smb3_negotiate_rsize
,
5545 .sess_setup
= SMB2_sess_setup
,
5546 .logoff
= SMB2_logoff
,
5547 .tree_connect
= SMB2_tcon
,
5548 .tree_disconnect
= SMB2_tdis
,
5549 .qfs_tcon
= smb3_qfs_tcon
,
5550 .is_path_accessible
= smb2_is_path_accessible
,
5551 .can_echo
= smb2_can_echo
,
5553 .query_path_info
= smb2_query_path_info
,
5554 .query_reparse_point
= smb2_query_reparse_point
,
5555 .get_srv_inum
= smb2_get_srv_inum
,
5556 .query_file_info
= smb2_query_file_info
,
5557 .set_path_size
= smb2_set_path_size
,
5558 .set_file_size
= smb2_set_file_size
,
5559 .set_file_info
= smb2_set_file_info
,
5560 .set_compression
= smb2_set_compression
,
5561 .mkdir
= smb2_mkdir
,
5562 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5563 .posix_mkdir
= smb311_posix_mkdir
,
5564 .rmdir
= smb2_rmdir
,
5565 .unlink
= smb2_unlink
,
5566 .rename
= smb2_rename_path
,
5567 .create_hardlink
= smb2_create_hardlink
,
5568 .parse_reparse_point
= smb2_parse_reparse_point
,
5569 .query_mf_symlink
= smb3_query_mf_symlink
,
5570 .create_mf_symlink
= smb3_create_mf_symlink
,
5571 .open
= smb2_open_file
,
5572 .set_fid
= smb2_set_fid
,
5573 .close
= smb2_close_file
,
5574 .close_getattr
= smb2_close_getattr
,
5575 .flush
= smb2_flush_file
,
5576 .async_readv
= smb2_async_readv
,
5577 .async_writev
= smb2_async_writev
,
5578 .sync_read
= smb2_sync_read
,
5579 .sync_write
= smb2_sync_write
,
5580 .query_dir_first
= smb2_query_dir_first
,
5581 .query_dir_next
= smb2_query_dir_next
,
5582 .close_dir
= smb2_close_dir
,
5583 .calc_smb_size
= smb2_calc_size
,
5584 .is_status_pending
= smb2_is_status_pending
,
5585 .is_session_expired
= smb2_is_session_expired
,
5586 .oplock_response
= smb2_oplock_response
,
5587 .queryfs
= smb311_queryfs
,
5588 .mand_lock
= smb2_mand_lock
,
5589 .mand_unlock_range
= smb2_unlock_range
,
5590 .push_mand_locks
= smb2_push_mandatory_locks
,
5591 .get_lease_key
= smb2_get_lease_key
,
5592 .set_lease_key
= smb2_set_lease_key
,
5593 .new_lease_key
= smb2_new_lease_key
,
5594 .generate_signingkey
= generate_smb311signingkey
,
5595 .calc_signature
= smb3_calc_signature
,
5596 .set_integrity
= smb3_set_integrity
,
5597 .is_read_op
= smb21_is_read_op
,
5598 .set_oplock_level
= smb3_set_oplock_level
,
5599 .create_lease_buf
= smb3_create_lease_buf
,
5600 .parse_lease_buf
= smb3_parse_lease_buf
,
5601 .copychunk_range
= smb2_copychunk_range
,
5602 .duplicate_extents
= smb2_duplicate_extents
,
5603 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5604 .wp_retry_size
= smb2_wp_retry_size
,
5605 .dir_needs_close
= smb2_dir_needs_close
,
5606 .fallocate
= smb3_fallocate
,
5607 .enum_snapshots
= smb3_enum_snapshots
,
5608 .notify
= smb3_notify
,
5609 .init_transform_rq
= smb3_init_transform_rq
,
5610 .is_transform_hdr
= smb3_is_transform_hdr
,
5611 .receive_transform
= smb3_receive_transform
,
5612 .get_dfs_refer
= smb2_get_dfs_refer
,
5613 .select_sectype
= smb2_select_sectype
,
5614 #ifdef CONFIG_CIFS_XATTR
5615 .query_all_EAs
= smb2_query_eas
,
5616 .set_EA
= smb2_set_ea
,
5617 #endif /* CIFS_XATTR */
5618 .get_acl
= get_smb2_acl
,
5619 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5620 .set_acl
= set_smb2_acl
,
5621 .next_header
= smb2_next_header
,
5622 .ioctl_query_info
= smb2_ioctl_query_info
,
5623 .make_node
= smb2_make_node
,
5624 .fiemap
= smb3_fiemap
,
5625 .llseek
= smb3_llseek
,
5626 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5627 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5630 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5631 struct smb_version_values smb20_values
= {
5632 .version_string
= SMB20_VERSION_STRING
,
5633 .protocol_id
= SMB20_PROT_ID
,
5634 .req_capabilities
= 0, /* MBZ */
5635 .large_lock_type
= 0,
5636 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5637 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5638 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5639 .header_size
= sizeof(struct smb2_hdr
),
5640 .header_preamble_size
= 0,
5641 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5642 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5643 .lock_cmd
= SMB2_LOCK
,
5645 .cap_nt_find
= SMB2_NT_FIND
,
5646 .cap_large_files
= SMB2_LARGE_FILES
,
5647 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5648 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5649 .create_lease_size
= sizeof(struct create_lease
),
5651 #endif /* ALLOW_INSECURE_LEGACY */
5653 struct smb_version_values smb21_values
= {
5654 .version_string
= SMB21_VERSION_STRING
,
5655 .protocol_id
= SMB21_PROT_ID
,
5656 .req_capabilities
= 0, /* MBZ on negotiate req until SMB3 dialect */
5657 .large_lock_type
= 0,
5658 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5659 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5660 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5661 .header_size
= sizeof(struct smb2_hdr
),
5662 .header_preamble_size
= 0,
5663 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5664 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5665 .lock_cmd
= SMB2_LOCK
,
5667 .cap_nt_find
= SMB2_NT_FIND
,
5668 .cap_large_files
= SMB2_LARGE_FILES
,
5669 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5670 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5671 .create_lease_size
= sizeof(struct create_lease
),
5674 struct smb_version_values smb3any_values
= {
5675 .version_string
= SMB3ANY_VERSION_STRING
,
5676 .protocol_id
= SMB302_PROT_ID
, /* doesn't matter, send protocol array */
5677 .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
,
5678 .large_lock_type
= 0,
5679 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5680 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5681 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5682 .header_size
= sizeof(struct smb2_hdr
),
5683 .header_preamble_size
= 0,
5684 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5685 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5686 .lock_cmd
= SMB2_LOCK
,
5688 .cap_nt_find
= SMB2_NT_FIND
,
5689 .cap_large_files
= SMB2_LARGE_FILES
,
5690 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5691 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5692 .create_lease_size
= sizeof(struct create_lease_v2
),
5695 struct smb_version_values smbdefault_values
= {
5696 .version_string
= SMBDEFAULT_VERSION_STRING
,
5697 .protocol_id
= SMB302_PROT_ID
, /* doesn't matter, send protocol array */
5698 .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
,
5699 .large_lock_type
= 0,
5700 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5701 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5702 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5703 .header_size
= sizeof(struct smb2_hdr
),
5704 .header_preamble_size
= 0,
5705 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5706 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5707 .lock_cmd
= SMB2_LOCK
,
5709 .cap_nt_find
= SMB2_NT_FIND
,
5710 .cap_large_files
= SMB2_LARGE_FILES
,
5711 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5712 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5713 .create_lease_size
= sizeof(struct create_lease_v2
),
5716 struct smb_version_values smb30_values
= {
5717 .version_string
= SMB30_VERSION_STRING
,
5718 .protocol_id
= SMB30_PROT_ID
,
5719 .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
,
5720 .large_lock_type
= 0,
5721 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5722 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5723 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5724 .header_size
= sizeof(struct smb2_hdr
),
5725 .header_preamble_size
= 0,
5726 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5727 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5728 .lock_cmd
= SMB2_LOCK
,
5730 .cap_nt_find
= SMB2_NT_FIND
,
5731 .cap_large_files
= SMB2_LARGE_FILES
,
5732 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5733 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5734 .create_lease_size
= sizeof(struct create_lease_v2
),
5737 struct smb_version_values smb302_values
= {
5738 .version_string
= SMB302_VERSION_STRING
,
5739 .protocol_id
= SMB302_PROT_ID
,
5740 .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
,
5741 .large_lock_type
= 0,
5742 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5743 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5744 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5745 .header_size
= sizeof(struct smb2_hdr
),
5746 .header_preamble_size
= 0,
5747 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5748 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5749 .lock_cmd
= SMB2_LOCK
,
5751 .cap_nt_find
= SMB2_NT_FIND
,
5752 .cap_large_files
= SMB2_LARGE_FILES
,
5753 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5754 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5755 .create_lease_size
= sizeof(struct create_lease_v2
),
5758 struct smb_version_values smb311_values
= {
5759 .version_string
= SMB311_VERSION_STRING
,
5760 .protocol_id
= SMB311_PROT_ID
,
5761 .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
,
5762 .large_lock_type
= 0,
5763 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5764 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5765 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5766 .header_size
= sizeof(struct smb2_hdr
),
5767 .header_preamble_size
= 0,
5768 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5769 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5770 .lock_cmd
= SMB2_LOCK
,
5772 .cap_nt_find
= SMB2_NT_FIND
,
5773 .cap_large_files
= SMB2_LARGE_FILES
,
5774 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5775 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5776 .create_lease_size
= sizeof(struct create_lease_v2
),