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
);
618 ses
->iface_last_update
= jiffies
;
622 while (bytes_left
>= sizeof(*p
)) {
623 memset(&tmp_iface
, 0, sizeof(tmp_iface
));
624 tmp_iface
.speed
= le64_to_cpu(p
->LinkSpeed
);
625 tmp_iface
.rdma_capable
= le32_to_cpu(p
->Capability
& RDMA_CAPABLE
) ? 1 : 0;
626 tmp_iface
.rss_capable
= le32_to_cpu(p
->Capability
& RSS_CAPABLE
) ? 1 : 0;
630 * The kernel and wire socket structures have the same
631 * layout and use network byte order but make the
632 * conversion explicit in case either one changes.
635 addr4
= (struct sockaddr_in
*)&tmp_iface
.sockaddr
;
636 p4
= (struct iface_info_ipv4
*)p
->Buffer
;
637 addr4
->sin_family
= AF_INET
;
638 memcpy(&addr4
->sin_addr
, &p4
->IPv4Address
, 4);
640 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
641 addr4
->sin_port
= cpu_to_be16(CIFS_PORT
);
643 cifs_dbg(FYI
, "%s: ipv4 %pI4\n", __func__
,
647 addr6
= (struct sockaddr_in6
*)&tmp_iface
.sockaddr
;
648 p6
= (struct iface_info_ipv6
*)p
->Buffer
;
649 addr6
->sin6_family
= AF_INET6
;
650 memcpy(&addr6
->sin6_addr
, &p6
->IPv6Address
, 16);
652 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
653 addr6
->sin6_flowinfo
= 0;
654 addr6
->sin6_scope_id
= 0;
655 addr6
->sin6_port
= cpu_to_be16(CIFS_PORT
);
657 cifs_dbg(FYI
, "%s: ipv6 %pI6\n", __func__
,
662 "%s: skipping unsupported socket family\n",
668 * The iface_list is assumed to be sorted by speed.
669 * Check if the new interface exists in that list.
670 * NEVER change iface. it could be in use.
671 * Add a new one instead
673 spin_lock(&ses
->iface_lock
);
674 list_for_each_entry_safe(iface
, niface
, &ses
->iface_list
,
676 ret
= iface_cmp(iface
, &tmp_iface
);
678 iface
->is_active
= 1;
679 spin_unlock(&ses
->iface_lock
);
681 } else if (ret
< 0) {
682 /* all remaining ifaces are slower */
683 kref_get(&iface
->refcount
);
687 spin_unlock(&ses
->iface_lock
);
689 /* no match. insert the entry in the list */
690 info
= kmalloc(sizeof(struct cifs_server_iface
),
696 memcpy(info
, &tmp_iface
, sizeof(tmp_iface
));
698 /* add this new entry to the list */
699 kref_init(&info
->refcount
);
702 cifs_dbg(FYI
, "%s: adding iface %zu\n", __func__
, ses
->iface_count
);
703 cifs_dbg(FYI
, "%s: speed %zu bps\n", __func__
, info
->speed
);
704 cifs_dbg(FYI
, "%s: capabilities 0x%08x\n", __func__
,
705 le32_to_cpu(p
->Capability
));
707 spin_lock(&ses
->iface_lock
);
708 if (!list_entry_is_head(iface
, &ses
->iface_list
, iface_head
)) {
709 list_add_tail(&info
->iface_head
, &iface
->iface_head
);
710 kref_put(&iface
->refcount
, release_iface
);
712 list_add_tail(&info
->iface_head
, &ses
->iface_list
);
715 spin_unlock(&ses
->iface_lock
);
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__
);
737 ses
->iface_last_update
= jiffies
;
741 * Go through the list again and put the inactive entries
743 spin_lock(&ses
->iface_lock
);
744 list_for_each_entry_safe(iface
, niface
, &ses
->iface_list
,
746 if (!iface
->is_active
) {
747 list_del(&iface
->iface_head
);
748 kref_put(&iface
->refcount
, release_iface
);
752 spin_unlock(&ses
->iface_lock
);
758 SMB3_request_interfaces(const unsigned int xid
, struct cifs_tcon
*tcon
, bool in_mount
)
761 unsigned int ret_data_len
= 0;
762 struct network_interface_info_ioctl_rsp
*out_buf
= NULL
;
763 struct cifs_ses
*ses
= tcon
->ses
;
764 struct TCP_Server_Info
*pserver
;
766 /* do not query too frequently */
767 if (ses
->iface_last_update
&&
768 time_before(jiffies
, ses
->iface_last_update
+
769 (SMB_INTERFACE_POLL_INTERVAL
* HZ
)))
772 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
773 FSCTL_QUERY_NETWORK_INTERFACE_INFO
,
774 NULL
/* no data input */, 0 /* no data input */,
775 CIFSMaxBufSize
, (char **)&out_buf
, &ret_data_len
);
776 if (rc
== -EOPNOTSUPP
) {
778 "server does not support query network interfaces\n");
780 } else if (rc
!= 0) {
781 cifs_tcon_dbg(VFS
, "error %d on ioctl to get interface list\n", rc
);
785 rc
= parse_server_interfaces(out_buf
, ret_data_len
, ses
, in_mount
);
789 /* check if iface is still active */
790 spin_lock(&ses
->chan_lock
);
791 pserver
= ses
->chans
[0].server
;
792 if (pserver
&& !cifs_chan_is_iface_active(ses
, pserver
)) {
793 spin_unlock(&ses
->chan_lock
);
794 cifs_chan_update_iface(ses
, pserver
);
795 spin_lock(&ses
->chan_lock
);
797 spin_unlock(&ses
->chan_lock
);
805 smb3_qfs_tcon(const unsigned int xid
, struct cifs_tcon
*tcon
,
806 struct cifs_sb_info
*cifs_sb
)
809 __le16 srch_path
= 0; /* Null - open root of share */
810 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
811 struct cifs_open_parms oparms
;
813 struct cached_fid
*cfid
= NULL
;
815 oparms
= (struct cifs_open_parms
) {
818 .desired_access
= FILE_READ_ATTRIBUTES
,
819 .disposition
= FILE_OPEN
,
820 .create_options
= cifs_create_options(cifs_sb
, 0),
824 rc
= open_cached_dir(xid
, tcon
, "", cifs_sb
, false, &cfid
);
826 memcpy(&fid
, &cfid
->fid
, sizeof(struct cifs_fid
));
828 rc
= SMB2_open(xid
, &oparms
, &srch_path
, &oplock
, NULL
, NULL
,
833 SMB3_request_interfaces(xid
, tcon
, true /* called during mount */);
835 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
836 FS_ATTRIBUTE_INFORMATION
);
837 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
838 FS_DEVICE_INFORMATION
);
839 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
840 FS_VOLUME_INFORMATION
);
841 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
842 FS_SECTOR_SIZE_INFORMATION
); /* SMB3 specific */
844 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
846 close_cached_dir(cfid
);
850 smb2_qfs_tcon(const unsigned int xid
, struct cifs_tcon
*tcon
,
851 struct cifs_sb_info
*cifs_sb
)
854 __le16 srch_path
= 0; /* Null - open root of share */
855 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
856 struct cifs_open_parms oparms
;
859 oparms
= (struct cifs_open_parms
) {
862 .desired_access
= FILE_READ_ATTRIBUTES
,
863 .disposition
= FILE_OPEN
,
864 .create_options
= cifs_create_options(cifs_sb
, 0),
868 rc
= SMB2_open(xid
, &oparms
, &srch_path
, &oplock
, NULL
, NULL
,
873 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
874 FS_ATTRIBUTE_INFORMATION
);
875 SMB2_QFS_attr(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
876 FS_DEVICE_INFORMATION
);
877 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
881 smb2_is_path_accessible(const unsigned int xid
, struct cifs_tcon
*tcon
,
882 struct cifs_sb_info
*cifs_sb
, const char *full_path
)
885 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
886 int err_buftype
= CIFS_NO_BUFFER
;
887 struct cifs_open_parms oparms
;
888 struct kvec err_iov
= {};
890 struct cached_fid
*cfid
;
894 rc
= open_cached_dir(xid
, tcon
, full_path
, cifs_sb
, true, &cfid
);
896 if (cfid
->has_lease
) {
897 close_cached_dir(cfid
);
900 close_cached_dir(cfid
);
903 utf16_path
= cifs_convert_path_to_utf16(full_path
, cifs_sb
);
907 oparms
= (struct cifs_open_parms
) {
910 .desired_access
= FILE_READ_ATTRIBUTES
,
911 .disposition
= FILE_OPEN
,
912 .create_options
= cifs_create_options(cifs_sb
, 0),
916 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
,
917 &err_iov
, &err_buftype
);
919 struct smb2_hdr
*hdr
= err_iov
.iov_base
;
921 if (unlikely(!hdr
|| err_buftype
== CIFS_NO_BUFFER
))
924 if (rc
!= -EREMOTE
&& hdr
->Status
== STATUS_OBJECT_NAME_INVALID
) {
925 rc2
= cifs_inval_name_dfs_link_error(xid
, tcon
, cifs_sb
,
934 if (rc
== -EREMOTE
&& IS_ENABLED(CONFIG_CIFS_DFS_UPCALL
) && cifs_sb
&&
935 (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_NO_DFS
))
940 rc
= SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
943 free_rsp_buf(err_buftype
, err_iov
.iov_base
);
948 static int smb2_get_srv_inum(const unsigned int xid
, struct cifs_tcon
*tcon
,
949 struct cifs_sb_info
*cifs_sb
, const char *full_path
,
950 u64
*uniqueid
, struct cifs_open_info_data
*data
)
952 *uniqueid
= le64_to_cpu(data
->fi
.IndexNumber
);
956 static int smb2_query_file_info(const unsigned int xid
, struct cifs_tcon
*tcon
,
957 struct cifsFileInfo
*cfile
, struct cifs_open_info_data
*data
)
959 struct cifs_fid
*fid
= &cfile
->fid
;
961 if (cfile
->symlink_target
) {
962 data
->symlink_target
= kstrdup(cfile
->symlink_target
, GFP_KERNEL
);
963 if (!data
->symlink_target
)
966 return SMB2_query_info(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
, &data
->fi
);
969 #ifdef CONFIG_CIFS_XATTR
971 move_smb2_ea_to_cifs(char *dst
, size_t dst_size
,
972 struct smb2_file_full_ea_info
*src
, size_t src_size
,
973 const unsigned char *ea_name
)
976 unsigned int ea_name_len
= ea_name
? strlen(ea_name
) : 0;
978 size_t buf_size
= dst_size
;
979 size_t name_len
, value_len
, user_name_len
;
981 while (src_size
> 0) {
982 name_len
= (size_t)src
->ea_name_length
;
983 value_len
= (size_t)le16_to_cpu(src
->ea_value_length
);
988 if (src_size
< 8 + name_len
+ 1 + value_len
) {
989 cifs_dbg(FYI
, "EA entry goes beyond length of list\n");
994 name
= &src
->ea_data
[0];
995 value
= &src
->ea_data
[src
->ea_name_length
+ 1];
998 if (ea_name_len
== name_len
&&
999 memcmp(ea_name
, name
, name_len
) == 0) {
1003 if (dst_size
< value_len
) {
1007 memcpy(dst
, value
, value_len
);
1011 /* 'user.' plus a terminating null */
1012 user_name_len
= 5 + 1 + name_len
;
1014 if (buf_size
== 0) {
1015 /* skip copy - calc size only */
1016 rc
+= user_name_len
;
1017 } else if (dst_size
>= user_name_len
) {
1018 dst_size
-= user_name_len
;
1019 memcpy(dst
, "user.", 5);
1021 memcpy(dst
, src
->ea_data
, name_len
);
1025 rc
+= user_name_len
;
1027 /* stop before overrun buffer */
1033 if (!src
->next_entry_offset
)
1036 if (src_size
< le32_to_cpu(src
->next_entry_offset
)) {
1037 /* stop before overrun buffer */
1041 src_size
-= le32_to_cpu(src
->next_entry_offset
);
1042 src
= (void *)((char *)src
+
1043 le32_to_cpu(src
->next_entry_offset
));
1046 /* didn't find the named attribute */
1055 smb2_query_eas(const unsigned int xid
, struct cifs_tcon
*tcon
,
1056 const unsigned char *path
, const unsigned char *ea_name
,
1057 char *ea_data
, size_t buf_size
,
1058 struct cifs_sb_info
*cifs_sb
)
1061 struct kvec rsp_iov
= {NULL
, 0};
1062 int buftype
= CIFS_NO_BUFFER
;
1063 struct smb2_query_info_rsp
*rsp
;
1064 struct smb2_file_full_ea_info
*info
= NULL
;
1066 rc
= smb2_query_info_compound(xid
, tcon
, path
,
1068 FILE_FULL_EA_INFORMATION
,
1071 MAX_SMB2_CREATE_RESPONSE_SIZE
-
1072 MAX_SMB2_CLOSE_RESPONSE_SIZE
,
1073 &rsp_iov
, &buftype
, cifs_sb
);
1076 * If ea_name is NULL (listxattr) and there are no EAs,
1077 * return 0 as it's not an error. Otherwise, the specified
1078 * ea_name was not found.
1080 if (!ea_name
&& rc
== -ENODATA
)
1085 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
1086 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
1087 le32_to_cpu(rsp
->OutputBufferLength
),
1089 sizeof(struct smb2_file_full_ea_info
));
1093 info
= (struct smb2_file_full_ea_info
*)(
1094 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)rsp
);
1095 rc
= move_smb2_ea_to_cifs(ea_data
, buf_size
, info
,
1096 le32_to_cpu(rsp
->OutputBufferLength
), ea_name
);
1099 free_rsp_buf(buftype
, rsp_iov
.iov_base
);
1104 smb2_set_ea(const unsigned int xid
, struct cifs_tcon
*tcon
,
1105 const char *path
, const char *ea_name
, const void *ea_value
,
1106 const __u16 ea_value_len
, const struct nls_table
*nls_codepage
,
1107 struct cifs_sb_info
*cifs_sb
)
1109 struct smb2_compound_vars
*vars
;
1110 struct cifs_ses
*ses
= tcon
->ses
;
1111 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
1112 struct smb_rqst
*rqst
;
1113 struct kvec
*rsp_iov
;
1114 __le16
*utf16_path
= NULL
;
1115 int ea_name_len
= strlen(ea_name
);
1116 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
1118 int resp_buftype
[3];
1119 struct cifs_open_parms oparms
;
1120 __u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1121 struct cifs_fid fid
;
1122 unsigned int size
[1];
1124 struct smb2_file_full_ea_info
*ea
= NULL
;
1125 struct smb2_query_info_rsp
*rsp
;
1126 int rc
, used_len
= 0;
1128 if (smb3_encryption_required(tcon
))
1129 flags
|= CIFS_TRANSFORM_REQ
;
1131 if (ea_name_len
> 255)
1134 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
1138 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
1139 vars
= kzalloc(sizeof(*vars
), GFP_KERNEL
);
1145 rsp_iov
= vars
->rsp_iov
;
1147 if (ses
->server
->ops
->query_all_EAs
) {
1149 rc
= ses
->server
->ops
->query_all_EAs(xid
, tcon
, path
,
1155 /* If we are adding a attribute we should first check
1156 * if there will be enough space available to store
1157 * the new EA. If not we should not add it since we
1158 * would not be able to even read the EAs back.
1160 rc
= smb2_query_info_compound(xid
, tcon
, path
,
1162 FILE_FULL_EA_INFORMATION
,
1165 MAX_SMB2_CREATE_RESPONSE_SIZE
-
1166 MAX_SMB2_CLOSE_RESPONSE_SIZE
,
1167 &rsp_iov
[1], &resp_buftype
[1], cifs_sb
);
1169 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
[1].iov_base
;
1170 used_len
= le32_to_cpu(rsp
->OutputBufferLength
);
1172 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
1173 resp_buftype
[1] = CIFS_NO_BUFFER
;
1174 memset(&rsp_iov
[1], 0, sizeof(rsp_iov
[1]));
1177 /* Use a fudge factor of 256 bytes in case we collide
1178 * with a different set_EAs command.
1180 if (CIFSMaxBufSize
- MAX_SMB2_CREATE_RESPONSE_SIZE
-
1181 MAX_SMB2_CLOSE_RESPONSE_SIZE
- 256 <
1182 used_len
+ ea_name_len
+ ea_value_len
+ 1) {
1190 rqst
[0].rq_iov
= vars
->open_iov
;
1191 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
1193 oparms
= (struct cifs_open_parms
) {
1196 .desired_access
= FILE_WRITE_EA
,
1197 .disposition
= FILE_OPEN
,
1198 .create_options
= cifs_create_options(cifs_sb
, 0),
1202 rc
= SMB2_open_init(tcon
, server
,
1203 &rqst
[0], &oplock
, &oparms
, utf16_path
);
1206 smb2_set_next_command(tcon
, &rqst
[0]);
1210 rqst
[1].rq_iov
= vars
->si_iov
;
1211 rqst
[1].rq_nvec
= 1;
1213 len
= sizeof(*ea
) + ea_name_len
+ ea_value_len
+ 1;
1214 ea
= kzalloc(len
, GFP_KERNEL
);
1220 ea
->ea_name_length
= ea_name_len
;
1221 ea
->ea_value_length
= cpu_to_le16(ea_value_len
);
1222 memcpy(ea
->ea_data
, ea_name
, ea_name_len
+ 1);
1223 memcpy(ea
->ea_data
+ ea_name_len
+ 1, ea_value
, ea_value_len
);
1228 rc
= SMB2_set_info_init(tcon
, server
,
1229 &rqst
[1], COMPOUND_FID
,
1230 COMPOUND_FID
, current
->tgid
,
1231 FILE_FULL_EA_INFORMATION
,
1232 SMB2_O_INFO_FILE
, 0, data
, size
);
1235 smb2_set_next_command(tcon
, &rqst
[1]);
1236 smb2_set_related(&rqst
[1]);
1239 rqst
[2].rq_iov
= &vars
->close_iov
;
1240 rqst
[2].rq_nvec
= 1;
1241 rc
= SMB2_close_init(tcon
, server
,
1242 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
1245 smb2_set_related(&rqst
[2]);
1247 rc
= compound_send_recv(xid
, ses
, server
,
1249 resp_buftype
, rsp_iov
);
1250 /* no need to bump num_remote_opens because handle immediately closed */
1254 SMB2_open_free(&rqst
[0]);
1255 SMB2_set_info_free(&rqst
[1]);
1256 SMB2_close_free(&rqst
[2]);
1257 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
1258 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
1259 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
1268 smb2_can_echo(struct TCP_Server_Info
*server
)
1270 return server
->echoes
;
1274 smb2_clear_stats(struct cifs_tcon
*tcon
)
1278 for (i
= 0; i
< NUMBER_OF_SMB2_COMMANDS
; i
++) {
1279 atomic_set(&tcon
->stats
.smb2_stats
.smb2_com_sent
[i
], 0);
1280 atomic_set(&tcon
->stats
.smb2_stats
.smb2_com_failed
[i
], 0);
1285 smb2_dump_share_caps(struct seq_file
*m
, struct cifs_tcon
*tcon
)
1287 seq_puts(m
, "\n\tShare Capabilities:");
1288 if (tcon
->capabilities
& SMB2_SHARE_CAP_DFS
)
1289 seq_puts(m
, " DFS,");
1290 if (tcon
->capabilities
& SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY
)
1291 seq_puts(m
, " CONTINUOUS AVAILABILITY,");
1292 if (tcon
->capabilities
& SMB2_SHARE_CAP_SCALEOUT
)
1293 seq_puts(m
, " SCALEOUT,");
1294 if (tcon
->capabilities
& SMB2_SHARE_CAP_CLUSTER
)
1295 seq_puts(m
, " CLUSTER,");
1296 if (tcon
->capabilities
& SMB2_SHARE_CAP_ASYMMETRIC
)
1297 seq_puts(m
, " ASYMMETRIC,");
1298 if (tcon
->capabilities
== 0)
1299 seq_puts(m
, " None");
1300 if (tcon
->ss_flags
& SSINFO_FLAGS_ALIGNED_DEVICE
)
1301 seq_puts(m
, " Aligned,");
1302 if (tcon
->ss_flags
& SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE
)
1303 seq_puts(m
, " Partition Aligned,");
1304 if (tcon
->ss_flags
& SSINFO_FLAGS_NO_SEEK_PENALTY
)
1305 seq_puts(m
, " SSD,");
1306 if (tcon
->ss_flags
& SSINFO_FLAGS_TRIM_ENABLED
)
1307 seq_puts(m
, " TRIM-support,");
1309 seq_printf(m
, "\tShare Flags: 0x%x", tcon
->share_flags
);
1310 seq_printf(m
, "\n\ttid: 0x%x", tcon
->tid
);
1311 if (tcon
->perf_sector_size
)
1312 seq_printf(m
, "\tOptimal sector size: 0x%x",
1313 tcon
->perf_sector_size
);
1314 seq_printf(m
, "\tMaximal Access: 0x%x", tcon
->maximal_access
);
1318 smb2_print_stats(struct seq_file
*m
, struct cifs_tcon
*tcon
)
1320 atomic_t
*sent
= tcon
->stats
.smb2_stats
.smb2_com_sent
;
1321 atomic_t
*failed
= tcon
->stats
.smb2_stats
.smb2_com_failed
;
1324 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1325 * totals (requests sent) since those SMBs are per-session not per tcon
1327 seq_printf(m
, "\nBytes read: %llu Bytes written: %llu",
1328 (long long)(tcon
->bytes_read
),
1329 (long long)(tcon
->bytes_written
));
1330 seq_printf(m
, "\nOpen files: %d total (local), %d open on server",
1331 atomic_read(&tcon
->num_local_opens
),
1332 atomic_read(&tcon
->num_remote_opens
));
1333 seq_printf(m
, "\nTreeConnects: %d total %d failed",
1334 atomic_read(&sent
[SMB2_TREE_CONNECT_HE
]),
1335 atomic_read(&failed
[SMB2_TREE_CONNECT_HE
]));
1336 seq_printf(m
, "\nTreeDisconnects: %d total %d failed",
1337 atomic_read(&sent
[SMB2_TREE_DISCONNECT_HE
]),
1338 atomic_read(&failed
[SMB2_TREE_DISCONNECT_HE
]));
1339 seq_printf(m
, "\nCreates: %d total %d failed",
1340 atomic_read(&sent
[SMB2_CREATE_HE
]),
1341 atomic_read(&failed
[SMB2_CREATE_HE
]));
1342 seq_printf(m
, "\nCloses: %d total %d failed",
1343 atomic_read(&sent
[SMB2_CLOSE_HE
]),
1344 atomic_read(&failed
[SMB2_CLOSE_HE
]));
1345 seq_printf(m
, "\nFlushes: %d total %d failed",
1346 atomic_read(&sent
[SMB2_FLUSH_HE
]),
1347 atomic_read(&failed
[SMB2_FLUSH_HE
]));
1348 seq_printf(m
, "\nReads: %d total %d failed",
1349 atomic_read(&sent
[SMB2_READ_HE
]),
1350 atomic_read(&failed
[SMB2_READ_HE
]));
1351 seq_printf(m
, "\nWrites: %d total %d failed",
1352 atomic_read(&sent
[SMB2_WRITE_HE
]),
1353 atomic_read(&failed
[SMB2_WRITE_HE
]));
1354 seq_printf(m
, "\nLocks: %d total %d failed",
1355 atomic_read(&sent
[SMB2_LOCK_HE
]),
1356 atomic_read(&failed
[SMB2_LOCK_HE
]));
1357 seq_printf(m
, "\nIOCTLs: %d total %d failed",
1358 atomic_read(&sent
[SMB2_IOCTL_HE
]),
1359 atomic_read(&failed
[SMB2_IOCTL_HE
]));
1360 seq_printf(m
, "\nQueryDirectories: %d total %d failed",
1361 atomic_read(&sent
[SMB2_QUERY_DIRECTORY_HE
]),
1362 atomic_read(&failed
[SMB2_QUERY_DIRECTORY_HE
]));
1363 seq_printf(m
, "\nChangeNotifies: %d total %d failed",
1364 atomic_read(&sent
[SMB2_CHANGE_NOTIFY_HE
]),
1365 atomic_read(&failed
[SMB2_CHANGE_NOTIFY_HE
]));
1366 seq_printf(m
, "\nQueryInfos: %d total %d failed",
1367 atomic_read(&sent
[SMB2_QUERY_INFO_HE
]),
1368 atomic_read(&failed
[SMB2_QUERY_INFO_HE
]));
1369 seq_printf(m
, "\nSetInfos: %d total %d failed",
1370 atomic_read(&sent
[SMB2_SET_INFO_HE
]),
1371 atomic_read(&failed
[SMB2_SET_INFO_HE
]));
1372 seq_printf(m
, "\nOplockBreaks: %d sent %d failed",
1373 atomic_read(&sent
[SMB2_OPLOCK_BREAK_HE
]),
1374 atomic_read(&failed
[SMB2_OPLOCK_BREAK_HE
]));
1378 smb2_set_fid(struct cifsFileInfo
*cfile
, struct cifs_fid
*fid
, __u32 oplock
)
1380 struct cifsInodeInfo
*cinode
= CIFS_I(d_inode(cfile
->dentry
));
1381 struct TCP_Server_Info
*server
= tlink_tcon(cfile
->tlink
)->ses
->server
;
1383 cfile
->fid
.persistent_fid
= fid
->persistent_fid
;
1384 cfile
->fid
.volatile_fid
= fid
->volatile_fid
;
1385 cfile
->fid
.access
= fid
->access
;
1386 #ifdef CONFIG_CIFS_DEBUG2
1387 cfile
->fid
.mid
= fid
->mid
;
1388 #endif /* CIFS_DEBUG2 */
1389 server
->ops
->set_oplock_level(cinode
, oplock
, fid
->epoch
,
1391 cinode
->can_cache_brlcks
= CIFS_CACHE_WRITE(cinode
);
1392 memcpy(cfile
->fid
.create_guid
, fid
->create_guid
, 16);
1396 smb2_close_file(const unsigned int xid
, struct cifs_tcon
*tcon
,
1397 struct cifs_fid
*fid
)
1399 SMB2_close(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
1403 smb2_close_getattr(const unsigned int xid
, struct cifs_tcon
*tcon
,
1404 struct cifsFileInfo
*cfile
)
1406 struct smb2_file_network_open_info file_inf
;
1407 struct inode
*inode
;
1410 rc
= __SMB2_close(xid
, tcon
, cfile
->fid
.persistent_fid
,
1411 cfile
->fid
.volatile_fid
, &file_inf
);
1415 inode
= d_inode(cfile
->dentry
);
1417 spin_lock(&inode
->i_lock
);
1418 CIFS_I(inode
)->time
= jiffies
;
1420 /* Creation time should not need to be updated on close */
1421 if (file_inf
.LastWriteTime
)
1422 inode_set_mtime_to_ts(inode
,
1423 cifs_NTtimeToUnix(file_inf
.LastWriteTime
));
1424 if (file_inf
.ChangeTime
)
1425 inode_set_ctime_to_ts(inode
,
1426 cifs_NTtimeToUnix(file_inf
.ChangeTime
));
1427 if (file_inf
.LastAccessTime
)
1428 inode_set_atime_to_ts(inode
,
1429 cifs_NTtimeToUnix(file_inf
.LastAccessTime
));
1432 * i_blocks is not related to (i_size / i_blksize),
1433 * but instead 512 byte (2**9) size is required for
1434 * calculating num blocks.
1436 if (le64_to_cpu(file_inf
.AllocationSize
) > 4096)
1438 (512 - 1 + le64_to_cpu(file_inf
.AllocationSize
)) >> 9;
1440 /* End of file and Attributes should not have to be updated on close */
1441 spin_unlock(&inode
->i_lock
);
1445 SMB2_request_res_key(const unsigned int xid
, struct cifs_tcon
*tcon
,
1446 u64 persistent_fid
, u64 volatile_fid
,
1447 struct copychunk_ioctl
*pcchunk
)
1450 unsigned int ret_data_len
;
1451 struct resume_key_req
*res_key
;
1453 rc
= SMB2_ioctl(xid
, tcon
, persistent_fid
, volatile_fid
,
1454 FSCTL_SRV_REQUEST_RESUME_KEY
, NULL
, 0 /* no input */,
1455 CIFSMaxBufSize
, (char **)&res_key
, &ret_data_len
);
1457 if (rc
== -EOPNOTSUPP
) {
1458 pr_warn_once("Server share %s does not support copy range\n", tcon
->tree_name
);
1459 goto req_res_key_exit
;
1461 cifs_tcon_dbg(VFS
, "refcpy ioctl error %d getting resume key\n", rc
);
1462 goto req_res_key_exit
;
1464 if (ret_data_len
< sizeof(struct resume_key_req
)) {
1465 cifs_tcon_dbg(VFS
, "Invalid refcopy resume key length\n");
1467 goto req_res_key_exit
;
1469 memcpy(pcchunk
->SourceKey
, res_key
->ResumeKey
, COPY_CHUNK_RES_KEY_SIZE
);
1477 smb2_ioctl_query_info(const unsigned int xid
,
1478 struct cifs_tcon
*tcon
,
1479 struct cifs_sb_info
*cifs_sb
,
1480 __le16
*path
, int is_dir
,
1483 struct smb2_compound_vars
*vars
;
1484 struct smb_rqst
*rqst
;
1485 struct kvec
*rsp_iov
;
1486 struct cifs_ses
*ses
= tcon
->ses
;
1487 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
1488 char __user
*arg
= (char __user
*)p
;
1489 struct smb_query_info qi
;
1490 struct smb_query_info __user
*pqi
;
1492 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
1493 struct smb2_query_info_rsp
*qi_rsp
= NULL
;
1494 struct smb2_ioctl_rsp
*io_rsp
= NULL
;
1495 void *buffer
= NULL
;
1496 int resp_buftype
[3];
1497 struct cifs_open_parms oparms
;
1498 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
1499 struct cifs_fid fid
;
1500 unsigned int size
[2];
1502 int create_options
= is_dir
? CREATE_NOT_FILE
: CREATE_NOT_DIR
;
1503 void (*free_req1_func
)(struct smb_rqst
*r
);
1505 vars
= kzalloc(sizeof(*vars
), GFP_ATOMIC
);
1508 rqst
= &vars
->rqst
[0];
1509 rsp_iov
= &vars
->rsp_iov
[0];
1511 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
1513 if (copy_from_user(&qi
, arg
, sizeof(struct smb_query_info
))) {
1517 if (qi
.output_buffer_length
> 1024) {
1522 if (!ses
|| !server
) {
1527 if (smb3_encryption_required(tcon
))
1528 flags
|= CIFS_TRANSFORM_REQ
;
1530 if (qi
.output_buffer_length
) {
1531 buffer
= memdup_user(arg
+ sizeof(struct smb_query_info
), qi
.output_buffer_length
);
1532 if (IS_ERR(buffer
)) {
1533 rc
= PTR_ERR(buffer
);
1539 rqst
[0].rq_iov
= &vars
->open_iov
[0];
1540 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
1542 oparms
= (struct cifs_open_parms
) {
1544 .disposition
= FILE_OPEN
,
1545 .create_options
= cifs_create_options(cifs_sb
, create_options
),
1549 if (qi
.flags
& PASSTHRU_FSCTL
) {
1550 switch (qi
.info_type
& FSCTL_DEVICE_ACCESS_MASK
) {
1551 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS
:
1552 oparms
.desired_access
= FILE_READ_DATA
| FILE_WRITE_DATA
| FILE_READ_ATTRIBUTES
| SYNCHRONIZE
;
1554 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS
:
1555 oparms
.desired_access
= GENERIC_ALL
;
1557 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS
:
1558 oparms
.desired_access
= GENERIC_READ
;
1560 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS
:
1561 oparms
.desired_access
= GENERIC_WRITE
;
1564 } else if (qi
.flags
& PASSTHRU_SET_INFO
) {
1565 oparms
.desired_access
= GENERIC_WRITE
;
1567 oparms
.desired_access
= FILE_READ_ATTRIBUTES
| READ_CONTROL
;
1570 rc
= SMB2_open_init(tcon
, server
,
1571 &rqst
[0], &oplock
, &oparms
, path
);
1573 goto free_output_buffer
;
1574 smb2_set_next_command(tcon
, &rqst
[0]);
1577 if (qi
.flags
& PASSTHRU_FSCTL
) {
1578 /* Can eventually relax perm check since server enforces too */
1579 if (!capable(CAP_SYS_ADMIN
)) {
1583 rqst
[1].rq_iov
= &vars
->io_iov
[0];
1584 rqst
[1].rq_nvec
= SMB2_IOCTL_IOV_SIZE
;
1586 rc
= SMB2_ioctl_init(tcon
, server
, &rqst
[1], COMPOUND_FID
, COMPOUND_FID
,
1587 qi
.info_type
, buffer
, qi
.output_buffer_length
,
1588 CIFSMaxBufSize
- MAX_SMB2_CREATE_RESPONSE_SIZE
-
1589 MAX_SMB2_CLOSE_RESPONSE_SIZE
);
1590 free_req1_func
= SMB2_ioctl_free
;
1591 } else if (qi
.flags
== PASSTHRU_SET_INFO
) {
1592 /* Can eventually relax perm check since server enforces too */
1593 if (!capable(CAP_SYS_ADMIN
)) {
1597 if (qi
.output_buffer_length
< 8) {
1601 rqst
[1].rq_iov
= vars
->si_iov
;
1602 rqst
[1].rq_nvec
= 1;
1604 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1608 rc
= SMB2_set_info_init(tcon
, server
, &rqst
[1], COMPOUND_FID
, COMPOUND_FID
,
1609 current
->tgid
, FILE_END_OF_FILE_INFORMATION
,
1610 SMB2_O_INFO_FILE
, 0, data
, size
);
1611 free_req1_func
= SMB2_set_info_free
;
1612 } else if (qi
.flags
== PASSTHRU_QUERY_INFO
) {
1613 rqst
[1].rq_iov
= &vars
->qi_iov
;
1614 rqst
[1].rq_nvec
= 1;
1616 rc
= SMB2_query_info_init(tcon
, server
,
1617 &rqst
[1], COMPOUND_FID
,
1618 COMPOUND_FID
, qi
.file_info_class
,
1619 qi
.info_type
, qi
.additional_information
,
1620 qi
.input_buffer_length
,
1621 qi
.output_buffer_length
, buffer
);
1622 free_req1_func
= SMB2_query_info_free
;
1623 } else { /* unknown flags */
1624 cifs_tcon_dbg(VFS
, "Invalid passthru query flags: 0x%x\n",
1631 smb2_set_next_command(tcon
, &rqst
[1]);
1632 smb2_set_related(&rqst
[1]);
1635 rqst
[2].rq_iov
= &vars
->close_iov
;
1636 rqst
[2].rq_nvec
= 1;
1638 rc
= SMB2_close_init(tcon
, server
,
1639 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
1642 smb2_set_related(&rqst
[2]);
1644 rc
= compound_send_recv(xid
, ses
, server
,
1646 resp_buftype
, rsp_iov
);
1650 /* No need to bump num_remote_opens since handle immediately closed */
1651 if (qi
.flags
& PASSTHRU_FSCTL
) {
1652 pqi
= (struct smb_query_info __user
*)arg
;
1653 io_rsp
= (struct smb2_ioctl_rsp
*)rsp_iov
[1].iov_base
;
1654 if (le32_to_cpu(io_rsp
->OutputCount
) < qi
.input_buffer_length
)
1655 qi
.input_buffer_length
= le32_to_cpu(io_rsp
->OutputCount
);
1656 if (qi
.input_buffer_length
> 0 &&
1657 le32_to_cpu(io_rsp
->OutputOffset
) + qi
.input_buffer_length
1658 > rsp_iov
[1].iov_len
) {
1663 if (copy_to_user(&pqi
->input_buffer_length
,
1664 &qi
.input_buffer_length
,
1665 sizeof(qi
.input_buffer_length
))) {
1670 if (copy_to_user((void __user
*)pqi
+ sizeof(struct smb_query_info
),
1671 (const void *)io_rsp
+ le32_to_cpu(io_rsp
->OutputOffset
),
1672 qi
.input_buffer_length
))
1675 pqi
= (struct smb_query_info __user
*)arg
;
1676 qi_rsp
= (struct smb2_query_info_rsp
*)rsp_iov
[1].iov_base
;
1677 if (le32_to_cpu(qi_rsp
->OutputBufferLength
) < qi
.input_buffer_length
)
1678 qi
.input_buffer_length
= le32_to_cpu(qi_rsp
->OutputBufferLength
);
1679 if (copy_to_user(&pqi
->input_buffer_length
,
1680 &qi
.input_buffer_length
,
1681 sizeof(qi
.input_buffer_length
))) {
1686 if (copy_to_user(pqi
+ 1, qi_rsp
->Buffer
,
1687 qi
.input_buffer_length
))
1692 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
1693 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
1694 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
1695 SMB2_close_free(&rqst
[2]);
1697 free_req1_func(&rqst
[1]);
1699 SMB2_open_free(&rqst
[0]);
1708 smb2_copychunk_range(const unsigned int xid
,
1709 struct cifsFileInfo
*srcfile
,
1710 struct cifsFileInfo
*trgtfile
, u64 src_off
,
1711 u64 len
, u64 dest_off
)
1714 unsigned int ret_data_len
;
1715 struct copychunk_ioctl
*pcchunk
;
1716 struct copychunk_ioctl_rsp
*retbuf
= NULL
;
1717 struct cifs_tcon
*tcon
;
1718 int chunks_copied
= 0;
1719 bool chunk_sizes_updated
= false;
1720 ssize_t bytes_written
, total_bytes_written
= 0;
1722 pcchunk
= kmalloc(sizeof(struct copychunk_ioctl
), GFP_KERNEL
);
1723 if (pcchunk
== NULL
)
1726 cifs_dbg(FYI
, "%s: about to call request res key\n", __func__
);
1727 /* Request a key from the server to identify the source of the copy */
1728 rc
= SMB2_request_res_key(xid
, tlink_tcon(srcfile
->tlink
),
1729 srcfile
->fid
.persistent_fid
,
1730 srcfile
->fid
.volatile_fid
, pcchunk
);
1732 /* Note: request_res_key sets res_key null only if rc !=0 */
1736 /* For now array only one chunk long, will make more flexible later */
1737 pcchunk
->ChunkCount
= cpu_to_le32(1);
1738 pcchunk
->Reserved
= 0;
1739 pcchunk
->Reserved2
= 0;
1741 tcon
= tlink_tcon(trgtfile
->tlink
);
1744 pcchunk
->SourceOffset
= cpu_to_le64(src_off
);
1745 pcchunk
->TargetOffset
= cpu_to_le64(dest_off
);
1747 cpu_to_le32(min_t(u64
, len
, tcon
->max_bytes_chunk
));
1749 /* Request server copy to target from src identified by key */
1752 rc
= SMB2_ioctl(xid
, tcon
, trgtfile
->fid
.persistent_fid
,
1753 trgtfile
->fid
.volatile_fid
, FSCTL_SRV_COPYCHUNK_WRITE
,
1754 (char *)pcchunk
, sizeof(struct copychunk_ioctl
),
1755 CIFSMaxBufSize
, (char **)&retbuf
, &ret_data_len
);
1758 sizeof(struct copychunk_ioctl_rsp
)) {
1759 cifs_tcon_dbg(VFS
, "Invalid cchunk response size\n");
1763 if (retbuf
->TotalBytesWritten
== 0) {
1764 cifs_dbg(FYI
, "no bytes copied\n");
1769 * Check if server claimed to write more than we asked
1771 if (le32_to_cpu(retbuf
->TotalBytesWritten
) >
1772 le32_to_cpu(pcchunk
->Length
)) {
1773 cifs_tcon_dbg(VFS
, "Invalid copy chunk response\n");
1777 if (le32_to_cpu(retbuf
->ChunksWritten
) != 1) {
1778 cifs_tcon_dbg(VFS
, "Invalid num chunks written\n");
1784 bytes_written
= le32_to_cpu(retbuf
->TotalBytesWritten
);
1785 src_off
+= bytes_written
;
1786 dest_off
+= bytes_written
;
1787 len
-= bytes_written
;
1788 total_bytes_written
+= bytes_written
;
1790 cifs_dbg(FYI
, "Chunks %d PartialChunk %d Total %zu\n",
1791 le32_to_cpu(retbuf
->ChunksWritten
),
1792 le32_to_cpu(retbuf
->ChunkBytesWritten
),
1794 } else if (rc
== -EINVAL
) {
1795 if (ret_data_len
!= sizeof(struct copychunk_ioctl_rsp
))
1798 cifs_dbg(FYI
, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1799 le32_to_cpu(retbuf
->ChunksWritten
),
1800 le32_to_cpu(retbuf
->ChunkBytesWritten
),
1801 le32_to_cpu(retbuf
->TotalBytesWritten
));
1804 * Check if this is the first request using these sizes,
1805 * (ie check if copy succeed once with original sizes
1806 * and check if the server gave us different sizes after
1807 * we already updated max sizes on previous request).
1808 * if not then why is the server returning an error now
1810 if ((chunks_copied
!= 0) || chunk_sizes_updated
)
1813 /* Check that server is not asking us to grow size */
1814 if (le32_to_cpu(retbuf
->ChunkBytesWritten
) <
1815 tcon
->max_bytes_chunk
)
1816 tcon
->max_bytes_chunk
=
1817 le32_to_cpu(retbuf
->ChunkBytesWritten
);
1819 goto cchunk_out
; /* server gave us bogus size */
1821 /* No need to change MaxChunks since already set to 1 */
1822 chunk_sizes_updated
= true;
1833 return total_bytes_written
;
1837 smb2_flush_file(const unsigned int xid
, struct cifs_tcon
*tcon
,
1838 struct cifs_fid
*fid
)
1840 return SMB2_flush(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
1844 smb2_read_data_offset(char *buf
)
1846 struct smb2_read_rsp
*rsp
= (struct smb2_read_rsp
*)buf
;
1848 return rsp
->DataOffset
;
1852 smb2_read_data_length(char *buf
, bool in_remaining
)
1854 struct smb2_read_rsp
*rsp
= (struct smb2_read_rsp
*)buf
;
1857 return le32_to_cpu(rsp
->DataRemaining
);
1859 return le32_to_cpu(rsp
->DataLength
);
1864 smb2_sync_read(const unsigned int xid
, struct cifs_fid
*pfid
,
1865 struct cifs_io_parms
*parms
, unsigned int *bytes_read
,
1866 char **buf
, int *buf_type
)
1868 parms
->persistent_fid
= pfid
->persistent_fid
;
1869 parms
->volatile_fid
= pfid
->volatile_fid
;
1870 return SMB2_read(xid
, parms
, bytes_read
, buf
, buf_type
);
1874 smb2_sync_write(const unsigned int xid
, struct cifs_fid
*pfid
,
1875 struct cifs_io_parms
*parms
, unsigned int *written
,
1876 struct kvec
*iov
, unsigned long nr_segs
)
1879 parms
->persistent_fid
= pfid
->persistent_fid
;
1880 parms
->volatile_fid
= pfid
->volatile_fid
;
1881 return SMB2_write(xid
, parms
, written
, iov
, nr_segs
);
1884 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1885 static bool smb2_set_sparse(const unsigned int xid
, struct cifs_tcon
*tcon
,
1886 struct cifsFileInfo
*cfile
, struct inode
*inode
, __u8 setsparse
)
1888 struct cifsInodeInfo
*cifsi
;
1891 cifsi
= CIFS_I(inode
);
1893 /* if file already sparse don't bother setting sparse again */
1894 if ((cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
) && setsparse
)
1895 return true; /* already sparse */
1897 if (!(cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
) && !setsparse
)
1898 return true; /* already not sparse */
1901 * Can't check for sparse support on share the usual way via the
1902 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1903 * since Samba server doesn't set the flag on the share, yet
1904 * supports the set sparse FSCTL and returns sparse correctly
1905 * in the file attributes. If we fail setting sparse though we
1906 * mark that server does not support sparse files for this share
1907 * to avoid repeatedly sending the unsupported fsctl to server
1908 * if the file is repeatedly extended.
1910 if (tcon
->broken_sparse_sup
)
1913 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
1914 cfile
->fid
.volatile_fid
, FSCTL_SET_SPARSE
,
1915 &setsparse
, 1, CIFSMaxBufSize
, NULL
, NULL
);
1917 tcon
->broken_sparse_sup
= true;
1918 cifs_dbg(FYI
, "set sparse rc = %d\n", rc
);
1923 cifsi
->cifsAttrs
|= FILE_ATTRIBUTE_SPARSE_FILE
;
1925 cifsi
->cifsAttrs
&= (~FILE_ATTRIBUTE_SPARSE_FILE
);
1931 smb2_set_file_size(const unsigned int xid
, struct cifs_tcon
*tcon
,
1932 struct cifsFileInfo
*cfile
, __u64 size
, bool set_alloc
)
1934 struct inode
*inode
;
1937 * If extending file more than one page make sparse. Many Linux fs
1938 * make files sparse by default when extending via ftruncate
1940 inode
= d_inode(cfile
->dentry
);
1942 if (!set_alloc
&& (size
> inode
->i_size
+ 8192)) {
1943 __u8 set_sparse
= 1;
1945 /* whether set sparse succeeds or not, extend the file */
1946 smb2_set_sparse(xid
, tcon
, cfile
, inode
, set_sparse
);
1949 return SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
1950 cfile
->fid
.volatile_fid
, cfile
->pid
, size
);
1954 smb2_duplicate_extents(const unsigned int xid
,
1955 struct cifsFileInfo
*srcfile
,
1956 struct cifsFileInfo
*trgtfile
, u64 src_off
,
1957 u64 len
, u64 dest_off
)
1960 unsigned int ret_data_len
;
1961 struct inode
*inode
;
1962 struct duplicate_extents_to_file dup_ext_buf
;
1963 struct cifs_tcon
*tcon
= tlink_tcon(trgtfile
->tlink
);
1965 /* server fileays advertise duplicate extent support with this flag */
1966 if ((le32_to_cpu(tcon
->fsAttrInfo
.Attributes
) &
1967 FILE_SUPPORTS_BLOCK_REFCOUNTING
) == 0)
1970 dup_ext_buf
.VolatileFileHandle
= srcfile
->fid
.volatile_fid
;
1971 dup_ext_buf
.PersistentFileHandle
= srcfile
->fid
.persistent_fid
;
1972 dup_ext_buf
.SourceFileOffset
= cpu_to_le64(src_off
);
1973 dup_ext_buf
.TargetFileOffset
= cpu_to_le64(dest_off
);
1974 dup_ext_buf
.ByteCount
= cpu_to_le64(len
);
1975 cifs_dbg(FYI
, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1976 src_off
, dest_off
, len
);
1978 inode
= d_inode(trgtfile
->dentry
);
1979 if (inode
->i_size
< dest_off
+ len
) {
1980 rc
= smb2_set_file_size(xid
, tcon
, trgtfile
, dest_off
+ len
, false);
1982 goto duplicate_extents_out
;
1985 * Although also could set plausible allocation size (i_blocks)
1986 * here in addition to setting the file size, in reflink
1987 * it is likely that the target file is sparse. Its allocation
1988 * size will be queried on next revalidate, but it is important
1989 * to make sure that file's cached size is updated immediately
1991 cifs_setsize(inode
, dest_off
+ len
);
1993 rc
= SMB2_ioctl(xid
, tcon
, trgtfile
->fid
.persistent_fid
,
1994 trgtfile
->fid
.volatile_fid
,
1995 FSCTL_DUPLICATE_EXTENTS_TO_FILE
,
1996 (char *)&dup_ext_buf
,
1997 sizeof(struct duplicate_extents_to_file
),
1998 CIFSMaxBufSize
, NULL
,
2001 if (ret_data_len
> 0)
2002 cifs_dbg(FYI
, "Non-zero response length in duplicate extents\n");
2004 duplicate_extents_out
:
2009 smb2_set_compression(const unsigned int xid
, struct cifs_tcon
*tcon
,
2010 struct cifsFileInfo
*cfile
)
2012 return SMB2_set_compression(xid
, tcon
, cfile
->fid
.persistent_fid
,
2013 cfile
->fid
.volatile_fid
);
2017 smb3_set_integrity(const unsigned int xid
, struct cifs_tcon
*tcon
,
2018 struct cifsFileInfo
*cfile
)
2020 struct fsctl_set_integrity_information_req integr_info
;
2021 unsigned int ret_data_len
;
2023 integr_info
.ChecksumAlgorithm
= cpu_to_le16(CHECKSUM_TYPE_UNCHANGED
);
2024 integr_info
.Flags
= 0;
2025 integr_info
.Reserved
= 0;
2027 return SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
2028 cfile
->fid
.volatile_fid
,
2029 FSCTL_SET_INTEGRITY_INFORMATION
,
2030 (char *)&integr_info
,
2031 sizeof(struct fsctl_set_integrity_information_req
),
2032 CIFSMaxBufSize
, NULL
,
2037 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2038 #define GMT_TOKEN_SIZE 50
2040 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2043 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2044 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2047 smb3_enum_snapshots(const unsigned int xid
, struct cifs_tcon
*tcon
,
2048 struct cifsFileInfo
*cfile
, void __user
*ioc_buf
)
2050 char *retbuf
= NULL
;
2051 unsigned int ret_data_len
= 0;
2053 u32 max_response_size
;
2054 struct smb_snapshot_array snapshot_in
;
2057 * On the first query to enumerate the list of snapshots available
2058 * for this volume the buffer begins with 0 (number of snapshots
2059 * which can be returned is zero since at that point we do not know
2060 * how big the buffer needs to be). On the second query,
2061 * it (ret_data_len) is set to number of snapshots so we can
2062 * know to set the maximum response size larger (see below).
2064 if (get_user(ret_data_len
, (unsigned int __user
*)ioc_buf
))
2068 * Note that for snapshot queries that servers like Azure expect that
2069 * the first query be minimal size (and just used to get the number/size
2070 * of previous versions) so response size must be specified as EXACTLY
2071 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2074 if (ret_data_len
== 0)
2075 max_response_size
= MIN_SNAPSHOT_ARRAY_SIZE
;
2077 max_response_size
= CIFSMaxBufSize
;
2079 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
2080 cfile
->fid
.volatile_fid
,
2081 FSCTL_SRV_ENUMERATE_SNAPSHOTS
,
2082 NULL
, 0 /* no input data */, max_response_size
,
2085 cifs_dbg(FYI
, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2090 if (ret_data_len
&& (ioc_buf
!= NULL
) && (retbuf
!= NULL
)) {
2092 if (copy_from_user(&snapshot_in
, ioc_buf
,
2093 sizeof(struct smb_snapshot_array
))) {
2100 * Check for min size, ie not large enough to fit even one GMT
2101 * token (snapshot). On the first ioctl some users may pass in
2102 * smaller size (or zero) to simply get the size of the array
2103 * so the user space caller can allocate sufficient memory
2104 * and retry the ioctl again with larger array size sufficient
2105 * to hold all of the snapshot GMT tokens on the second try.
2107 if (snapshot_in
.snapshot_array_size
< GMT_TOKEN_SIZE
)
2108 ret_data_len
= sizeof(struct smb_snapshot_array
);
2111 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2112 * the snapshot array (of 50 byte GMT tokens) each
2113 * representing an available previous version of the data
2115 if (ret_data_len
> (snapshot_in
.snapshot_array_size
+
2116 sizeof(struct smb_snapshot_array
)))
2117 ret_data_len
= snapshot_in
.snapshot_array_size
+
2118 sizeof(struct smb_snapshot_array
);
2120 if (copy_to_user(ioc_buf
, retbuf
, ret_data_len
))
2131 smb3_notify(const unsigned int xid
, struct file
*pfile
,
2132 void __user
*ioc_buf
, bool return_changes
)
2134 struct smb3_notify_info notify
;
2135 struct smb3_notify_info __user
*pnotify_buf
;
2136 struct dentry
*dentry
= pfile
->f_path
.dentry
;
2137 struct inode
*inode
= file_inode(pfile
);
2138 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
2139 struct cifs_open_parms oparms
;
2140 struct cifs_fid fid
;
2141 struct cifs_tcon
*tcon
;
2142 const unsigned char *path
;
2143 char *returned_ioctl_info
= NULL
;
2144 void *page
= alloc_dentry_path();
2145 __le16
*utf16_path
= NULL
;
2146 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2150 path
= build_path_from_dentry(dentry
, page
);
2156 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
2157 if (utf16_path
== NULL
) {
2162 if (return_changes
) {
2163 if (copy_from_user(¬ify
, ioc_buf
, sizeof(struct smb3_notify_info
))) {
2168 if (copy_from_user(¬ify
, ioc_buf
, sizeof(struct smb3_notify
))) {
2172 notify
.data_len
= 0;
2175 tcon
= cifs_sb_master_tcon(cifs_sb
);
2176 oparms
= (struct cifs_open_parms
) {
2179 .desired_access
= FILE_READ_ATTRIBUTES
| FILE_READ_DATA
,
2180 .disposition
= FILE_OPEN
,
2181 .create_options
= cifs_create_options(cifs_sb
, 0),
2185 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
, NULL
,
2190 rc
= SMB2_change_notify(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
,
2191 notify
.watch_tree
, notify
.completion_filter
,
2192 notify
.data_len
, &returned_ioctl_info
, &ret_len
);
2194 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
2196 cifs_dbg(FYI
, "change notify for path %s rc %d\n", path
, rc
);
2197 if (return_changes
&& (ret_len
> 0) && (notify
.data_len
> 0)) {
2198 if (ret_len
> notify
.data_len
)
2199 ret_len
= notify
.data_len
;
2200 pnotify_buf
= (struct smb3_notify_info __user
*)ioc_buf
;
2201 if (copy_to_user(pnotify_buf
->notify_data
, returned_ioctl_info
, ret_len
))
2203 else if (copy_to_user(&pnotify_buf
->data_len
, &ret_len
, sizeof(ret_len
)))
2206 kfree(returned_ioctl_info
);
2208 free_dentry_path(page
);
2214 smb2_query_dir_first(const unsigned int xid
, struct cifs_tcon
*tcon
,
2215 const char *path
, struct cifs_sb_info
*cifs_sb
,
2216 struct cifs_fid
*fid
, __u16 search_flags
,
2217 struct cifs_search_info
*srch_inf
)
2220 struct smb_rqst rqst
[2];
2221 struct kvec rsp_iov
[2];
2222 int resp_buftype
[2];
2223 struct kvec open_iov
[SMB2_CREATE_IOV_SIZE
];
2224 struct kvec qd_iov
[SMB2_QUERY_DIRECTORY_IOV_SIZE
];
2226 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2227 struct cifs_open_parms oparms
;
2228 struct smb2_query_directory_rsp
*qd_rsp
= NULL
;
2229 struct smb2_create_rsp
*op_rsp
= NULL
;
2230 struct TCP_Server_Info
*server
= cifs_pick_channel(tcon
->ses
);
2231 int retry_count
= 0;
2233 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
2237 if (smb3_encryption_required(tcon
))
2238 flags
|= CIFS_TRANSFORM_REQ
;
2240 memset(rqst
, 0, sizeof(rqst
));
2241 resp_buftype
[0] = resp_buftype
[1] = CIFS_NO_BUFFER
;
2242 memset(rsp_iov
, 0, sizeof(rsp_iov
));
2245 memset(&open_iov
, 0, sizeof(open_iov
));
2246 rqst
[0].rq_iov
= open_iov
;
2247 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
2249 oparms
= (struct cifs_open_parms
) {
2252 .desired_access
= FILE_READ_ATTRIBUTES
| FILE_READ_DATA
,
2253 .disposition
= FILE_OPEN
,
2254 .create_options
= cifs_create_options(cifs_sb
, 0),
2258 rc
= SMB2_open_init(tcon
, server
,
2259 &rqst
[0], &oplock
, &oparms
, utf16_path
);
2262 smb2_set_next_command(tcon
, &rqst
[0]);
2264 /* Query directory */
2265 srch_inf
->entries_in_buffer
= 0;
2266 srch_inf
->index_of_last_entry
= 2;
2268 memset(&qd_iov
, 0, sizeof(qd_iov
));
2269 rqst
[1].rq_iov
= qd_iov
;
2270 rqst
[1].rq_nvec
= SMB2_QUERY_DIRECTORY_IOV_SIZE
;
2272 rc
= SMB2_query_directory_init(xid
, tcon
, server
,
2274 COMPOUND_FID
, COMPOUND_FID
,
2275 0, srch_inf
->info_level
);
2279 smb2_set_related(&rqst
[1]);
2282 rc
= compound_send_recv(xid
, tcon
->ses
, server
,
2284 resp_buftype
, rsp_iov
);
2286 if (rc
== -EAGAIN
&& retry_count
++ < 10)
2289 /* If the open failed there is nothing to do */
2290 op_rsp
= (struct smb2_create_rsp
*)rsp_iov
[0].iov_base
;
2291 if (op_rsp
== NULL
|| op_rsp
->hdr
.Status
!= STATUS_SUCCESS
) {
2292 cifs_dbg(FYI
, "query_dir_first: open failed rc=%d\n", rc
);
2295 fid
->persistent_fid
= op_rsp
->PersistentFileId
;
2296 fid
->volatile_fid
= op_rsp
->VolatileFileId
;
2298 /* Anything else than ENODATA means a genuine error */
2299 if (rc
&& rc
!= -ENODATA
) {
2300 SMB2_close(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
2301 cifs_dbg(FYI
, "query_dir_first: query directory failed rc=%d\n", rc
);
2302 trace_smb3_query_dir_err(xid
, fid
->persistent_fid
,
2303 tcon
->tid
, tcon
->ses
->Suid
, 0, 0, rc
);
2307 atomic_inc(&tcon
->num_remote_opens
);
2309 qd_rsp
= (struct smb2_query_directory_rsp
*)rsp_iov
[1].iov_base
;
2310 if (qd_rsp
->hdr
.Status
== STATUS_NO_MORE_FILES
) {
2311 trace_smb3_query_dir_done(xid
, fid
->persistent_fid
,
2312 tcon
->tid
, tcon
->ses
->Suid
, 0, 0);
2313 srch_inf
->endOfSearch
= true;
2318 rc
= smb2_parse_query_directory(tcon
, &rsp_iov
[1], resp_buftype
[1],
2321 trace_smb3_query_dir_err(xid
, fid
->persistent_fid
, tcon
->tid
,
2322 tcon
->ses
->Suid
, 0, 0, rc
);
2325 resp_buftype
[1] = CIFS_NO_BUFFER
;
2327 trace_smb3_query_dir_done(xid
, fid
->persistent_fid
, tcon
->tid
,
2328 tcon
->ses
->Suid
, 0, srch_inf
->entries_in_buffer
);
2332 SMB2_open_free(&rqst
[0]);
2333 SMB2_query_directory_free(&rqst
[1]);
2334 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
2335 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
2340 smb2_query_dir_next(const unsigned int xid
, struct cifs_tcon
*tcon
,
2341 struct cifs_fid
*fid
, __u16 search_flags
,
2342 struct cifs_search_info
*srch_inf
)
2344 return SMB2_query_directory(xid
, tcon
, fid
->persistent_fid
,
2345 fid
->volatile_fid
, 0, srch_inf
);
2349 smb2_close_dir(const unsigned int xid
, struct cifs_tcon
*tcon
,
2350 struct cifs_fid
*fid
)
2352 return SMB2_close(xid
, tcon
, fid
->persistent_fid
, fid
->volatile_fid
);
2356 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2357 * the number of credits and return true. Otherwise - return false.
2360 smb2_is_status_pending(char *buf
, struct TCP_Server_Info
*server
)
2362 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2363 int scredits
, in_flight
;
2365 if (shdr
->Status
!= STATUS_PENDING
)
2368 if (shdr
->CreditRequest
) {
2369 spin_lock(&server
->req_lock
);
2370 server
->credits
+= le16_to_cpu(shdr
->CreditRequest
);
2371 scredits
= server
->credits
;
2372 in_flight
= server
->in_flight
;
2373 spin_unlock(&server
->req_lock
);
2374 wake_up(&server
->request_q
);
2376 trace_smb3_pend_credits(server
->CurrentMid
,
2377 server
->conn_id
, server
->hostname
, scredits
,
2378 le16_to_cpu(shdr
->CreditRequest
), in_flight
);
2379 cifs_dbg(FYI
, "%s: status pending add %u credits total=%d\n",
2380 __func__
, le16_to_cpu(shdr
->CreditRequest
), scredits
);
2387 smb2_is_session_expired(char *buf
)
2389 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2391 if (shdr
->Status
!= STATUS_NETWORK_SESSION_EXPIRED
&&
2392 shdr
->Status
!= STATUS_USER_SESSION_DELETED
)
2395 trace_smb3_ses_expired(le32_to_cpu(shdr
->Id
.SyncId
.TreeId
),
2396 le64_to_cpu(shdr
->SessionId
),
2397 le16_to_cpu(shdr
->Command
),
2398 le64_to_cpu(shdr
->MessageId
));
2399 cifs_dbg(FYI
, "Session expired or deleted\n");
2405 smb2_is_status_io_timeout(char *buf
)
2407 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2409 if (shdr
->Status
== STATUS_IO_TIMEOUT
)
2416 smb2_is_network_name_deleted(char *buf
, struct TCP_Server_Info
*server
)
2418 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
2419 struct TCP_Server_Info
*pserver
;
2420 struct cifs_ses
*ses
;
2421 struct cifs_tcon
*tcon
;
2423 if (shdr
->Status
!= STATUS_NETWORK_NAME_DELETED
)
2426 /* If server is a channel, select the primary channel */
2427 pserver
= SERVER_IS_CHAN(server
) ? server
->primary_server
: server
;
2429 spin_lock(&cifs_tcp_ses_lock
);
2430 list_for_each_entry(ses
, &pserver
->smb_ses_list
, smb_ses_list
) {
2431 list_for_each_entry(tcon
, &ses
->tcon_list
, tcon_list
) {
2432 if (tcon
->tid
== le32_to_cpu(shdr
->Id
.SyncId
.TreeId
)) {
2433 spin_lock(&tcon
->tc_lock
);
2434 tcon
->need_reconnect
= true;
2435 spin_unlock(&tcon
->tc_lock
);
2436 spin_unlock(&cifs_tcp_ses_lock
);
2437 pr_warn_once("Server share %s deleted.\n",
2443 spin_unlock(&cifs_tcp_ses_lock
);
2449 smb2_oplock_response(struct cifs_tcon
*tcon
, __u64 persistent_fid
,
2450 __u64 volatile_fid
, __u16 net_fid
, struct cifsInodeInfo
*cinode
)
2452 if (tcon
->ses
->server
->capabilities
& SMB2_GLOBAL_CAP_LEASING
)
2453 return SMB2_lease_break(0, tcon
, cinode
->lease_key
,
2454 smb2_get_lease_state(cinode
));
2456 return SMB2_oplock_break(0, tcon
, persistent_fid
, volatile_fid
,
2457 CIFS_CACHE_READ(cinode
) ? 1 : 0);
2461 smb2_set_related(struct smb_rqst
*rqst
)
2463 struct smb2_hdr
*shdr
;
2465 shdr
= (struct smb2_hdr
*)(rqst
->rq_iov
[0].iov_base
);
2467 cifs_dbg(FYI
, "shdr NULL in smb2_set_related\n");
2470 shdr
->Flags
|= SMB2_FLAGS_RELATED_OPERATIONS
;
2473 char smb2_padding
[7] = {0, 0, 0, 0, 0, 0, 0};
2476 smb2_set_next_command(struct cifs_tcon
*tcon
, struct smb_rqst
*rqst
)
2478 struct smb2_hdr
*shdr
;
2479 struct cifs_ses
*ses
= tcon
->ses
;
2480 struct TCP_Server_Info
*server
= ses
->server
;
2481 unsigned long len
= smb_rqst_len(server
, rqst
);
2484 shdr
= (struct smb2_hdr
*)(rqst
->rq_iov
[0].iov_base
);
2486 cifs_dbg(FYI
, "shdr NULL in smb2_set_next_command\n");
2490 /* SMB headers in a compound are 8 byte aligned. */
2492 /* No padding needed */
2496 num_padding
= 8 - (len
& 7);
2497 if (!smb3_encryption_required(tcon
)) {
2499 * If we do not have encryption then we can just add an extra
2500 * iov for the padding.
2502 rqst
->rq_iov
[rqst
->rq_nvec
].iov_base
= smb2_padding
;
2503 rqst
->rq_iov
[rqst
->rq_nvec
].iov_len
= num_padding
;
2508 * We can not add a small padding iov for the encryption case
2509 * because the encryption framework can not handle the padding
2511 * We have to flatten this into a single buffer and add
2512 * the padding to it.
2514 for (i
= 1; i
< rqst
->rq_nvec
; i
++) {
2515 memcpy(rqst
->rq_iov
[0].iov_base
+
2516 rqst
->rq_iov
[0].iov_len
,
2517 rqst
->rq_iov
[i
].iov_base
,
2518 rqst
->rq_iov
[i
].iov_len
);
2519 rqst
->rq_iov
[0].iov_len
+= rqst
->rq_iov
[i
].iov_len
;
2521 memset(rqst
->rq_iov
[0].iov_base
+ rqst
->rq_iov
[0].iov_len
,
2523 rqst
->rq_iov
[0].iov_len
+= num_padding
;
2529 shdr
->NextCommand
= cpu_to_le32(len
);
2533 * Passes the query info response back to the caller on success.
2534 * Caller need to free this with free_rsp_buf().
2537 smb2_query_info_compound(const unsigned int xid
, struct cifs_tcon
*tcon
,
2538 const char *path
, u32 desired_access
,
2539 u32
class, u32 type
, u32 output_len
,
2540 struct kvec
*rsp
, int *buftype
,
2541 struct cifs_sb_info
*cifs_sb
)
2543 struct smb2_compound_vars
*vars
;
2544 struct cifs_ses
*ses
= tcon
->ses
;
2545 struct TCP_Server_Info
*server
= cifs_pick_channel(ses
);
2546 int flags
= CIFS_CP_CREATE_CLOSE_OP
;
2547 struct smb_rqst
*rqst
;
2548 int resp_buftype
[3];
2549 struct kvec
*rsp_iov
;
2550 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2551 struct cifs_open_parms oparms
;
2552 struct cifs_fid fid
;
2555 struct cached_fid
*cfid
= NULL
;
2559 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
2563 if (smb3_encryption_required(tcon
))
2564 flags
|= CIFS_TRANSFORM_REQ
;
2566 resp_buftype
[0] = resp_buftype
[1] = resp_buftype
[2] = CIFS_NO_BUFFER
;
2567 vars
= kzalloc(sizeof(*vars
), GFP_KERNEL
);
2573 rsp_iov
= vars
->rsp_iov
;
2576 * We can only call this for things we know are directories.
2578 if (!strcmp(path
, ""))
2579 open_cached_dir(xid
, tcon
, path
, cifs_sb
, false,
2580 &cfid
); /* cfid null if open dir failed */
2582 rqst
[0].rq_iov
= vars
->open_iov
;
2583 rqst
[0].rq_nvec
= SMB2_CREATE_IOV_SIZE
;
2585 oparms
= (struct cifs_open_parms
) {
2588 .desired_access
= desired_access
,
2589 .disposition
= FILE_OPEN
,
2590 .create_options
= cifs_create_options(cifs_sb
, 0),
2594 rc
= SMB2_open_init(tcon
, server
,
2595 &rqst
[0], &oplock
, &oparms
, utf16_path
);
2598 smb2_set_next_command(tcon
, &rqst
[0]);
2600 rqst
[1].rq_iov
= &vars
->qi_iov
;
2601 rqst
[1].rq_nvec
= 1;
2604 rc
= SMB2_query_info_init(tcon
, server
,
2606 cfid
->fid
.persistent_fid
,
2607 cfid
->fid
.volatile_fid
,
2612 rc
= SMB2_query_info_init(tcon
, server
,
2623 smb2_set_next_command(tcon
, &rqst
[1]);
2624 smb2_set_related(&rqst
[1]);
2627 rqst
[2].rq_iov
= &vars
->close_iov
;
2628 rqst
[2].rq_nvec
= 1;
2630 rc
= SMB2_close_init(tcon
, server
,
2631 &rqst
[2], COMPOUND_FID
, COMPOUND_FID
, false);
2634 smb2_set_related(&rqst
[2]);
2637 rc
= compound_send_recv(xid
, ses
, server
,
2639 &resp_buftype
[1], &rsp_iov
[1]);
2641 rc
= compound_send_recv(xid
, ses
, server
,
2643 resp_buftype
, rsp_iov
);
2646 free_rsp_buf(resp_buftype
[1], rsp_iov
[1].iov_base
);
2647 if (rc
== -EREMCHG
) {
2648 tcon
->need_reconnect
= true;
2649 pr_warn_once("server share %s deleted\n",
2655 *buftype
= resp_buftype
[1];
2658 SMB2_open_free(&rqst
[0]);
2659 SMB2_query_info_free(&rqst
[1]);
2660 SMB2_close_free(&rqst
[2]);
2661 free_rsp_buf(resp_buftype
[0], rsp_iov
[0].iov_base
);
2662 free_rsp_buf(resp_buftype
[2], rsp_iov
[2].iov_base
);
2664 close_cached_dir(cfid
);
2672 smb2_queryfs(const unsigned int xid
, struct cifs_tcon
*tcon
,
2673 struct cifs_sb_info
*cifs_sb
, struct kstatfs
*buf
)
2675 struct smb2_query_info_rsp
*rsp
;
2676 struct smb2_fs_full_size_info
*info
= NULL
;
2677 struct kvec rsp_iov
= {NULL
, 0};
2678 int buftype
= CIFS_NO_BUFFER
;
2682 rc
= smb2_query_info_compound(xid
, tcon
, "",
2683 FILE_READ_ATTRIBUTES
,
2684 FS_FULL_SIZE_INFORMATION
,
2685 SMB2_O_INFO_FILESYSTEM
,
2686 sizeof(struct smb2_fs_full_size_info
),
2687 &rsp_iov
, &buftype
, cifs_sb
);
2691 rsp
= (struct smb2_query_info_rsp
*)rsp_iov
.iov_base
;
2692 buf
->f_type
= SMB2_SUPER_MAGIC
;
2693 info
= (struct smb2_fs_full_size_info
*)(
2694 le16_to_cpu(rsp
->OutputBufferOffset
) + (char *)rsp
);
2695 rc
= smb2_validate_iov(le16_to_cpu(rsp
->OutputBufferOffset
),
2696 le32_to_cpu(rsp
->OutputBufferLength
),
2698 sizeof(struct smb2_fs_full_size_info
));
2700 smb2_copy_fs_info_to_kstatfs(info
, buf
);
2703 trace_smb3_qfs_done(xid
, tcon
->tid
, tcon
->ses
->Suid
, tcon
->tree_name
, rc
);
2704 free_rsp_buf(buftype
, rsp_iov
.iov_base
);
2709 smb311_queryfs(const unsigned int xid
, struct cifs_tcon
*tcon
,
2710 struct cifs_sb_info
*cifs_sb
, struct kstatfs
*buf
)
2713 __le16 srch_path
= 0; /* Null - open root of share */
2714 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
2715 struct cifs_open_parms oparms
;
2716 struct cifs_fid fid
;
2718 if (!tcon
->posix_extensions
)
2719 return smb2_queryfs(xid
, tcon
, cifs_sb
, buf
);
2721 oparms
= (struct cifs_open_parms
) {
2724 .desired_access
= FILE_READ_ATTRIBUTES
,
2725 .disposition
= FILE_OPEN
,
2726 .create_options
= cifs_create_options(cifs_sb
, 0),
2730 rc
= SMB2_open(xid
, &oparms
, &srch_path
, &oplock
, NULL
, NULL
,
2735 rc
= SMB311_posix_qfs_info(xid
, tcon
, fid
.persistent_fid
,
2736 fid
.volatile_fid
, buf
);
2737 buf
->f_type
= SMB2_SUPER_MAGIC
;
2738 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
2743 smb2_compare_fids(struct cifsFileInfo
*ob1
, struct cifsFileInfo
*ob2
)
2745 return ob1
->fid
.persistent_fid
== ob2
->fid
.persistent_fid
&&
2746 ob1
->fid
.volatile_fid
== ob2
->fid
.volatile_fid
;
2750 smb2_mand_lock(const unsigned int xid
, struct cifsFileInfo
*cfile
, __u64 offset
,
2751 __u64 length
, __u32 type
, int lock
, int unlock
, bool wait
)
2753 if (unlock
&& !lock
)
2754 type
= SMB2_LOCKFLAG_UNLOCK
;
2755 return SMB2_lock(xid
, tlink_tcon(cfile
->tlink
),
2756 cfile
->fid
.persistent_fid
, cfile
->fid
.volatile_fid
,
2757 current
->tgid
, length
, offset
, type
, wait
);
2761 smb2_get_lease_key(struct inode
*inode
, struct cifs_fid
*fid
)
2763 memcpy(fid
->lease_key
, CIFS_I(inode
)->lease_key
, SMB2_LEASE_KEY_SIZE
);
2767 smb2_set_lease_key(struct inode
*inode
, struct cifs_fid
*fid
)
2769 memcpy(CIFS_I(inode
)->lease_key
, fid
->lease_key
, SMB2_LEASE_KEY_SIZE
);
2773 smb2_new_lease_key(struct cifs_fid
*fid
)
2775 generate_random_uuid(fid
->lease_key
);
2779 smb2_get_dfs_refer(const unsigned int xid
, struct cifs_ses
*ses
,
2780 const char *search_name
,
2781 struct dfs_info3_param
**target_nodes
,
2782 unsigned int *num_of_nodes
,
2783 const struct nls_table
*nls_codepage
, int remap
)
2786 __le16
*utf16_path
= NULL
;
2787 int utf16_path_len
= 0;
2788 struct cifs_tcon
*tcon
;
2789 struct fsctl_get_dfs_referral_req
*dfs_req
= NULL
;
2790 struct get_dfs_referral_rsp
*dfs_rsp
= NULL
;
2791 u32 dfs_req_size
= 0, dfs_rsp_size
= 0;
2792 int retry_count
= 0;
2794 cifs_dbg(FYI
, "%s: path: %s\n", __func__
, search_name
);
2797 * Try to use the IPC tcon, otherwise just use any
2799 tcon
= ses
->tcon_ipc
;
2801 spin_lock(&cifs_tcp_ses_lock
);
2802 tcon
= list_first_entry_or_null(&ses
->tcon_list
,
2807 spin_unlock(&cifs_tcp_ses_lock
);
2811 cifs_dbg(VFS
, "session %p has no tcon available for a dfs referral request\n",
2817 utf16_path
= cifs_strndup_to_utf16(search_name
, PATH_MAX
,
2819 nls_codepage
, remap
);
2825 dfs_req_size
= sizeof(*dfs_req
) + utf16_path_len
;
2826 dfs_req
= kzalloc(dfs_req_size
, GFP_KERNEL
);
2832 /* Highest DFS referral version understood */
2833 dfs_req
->MaxReferralLevel
= DFS_VERSION
;
2835 /* Path to resolve in an UTF-16 null-terminated string */
2836 memcpy(dfs_req
->RequestFileName
, utf16_path
, utf16_path_len
);
2839 rc
= SMB2_ioctl(xid
, tcon
, NO_FILE_ID
, NO_FILE_ID
,
2840 FSCTL_DFS_GET_REFERRALS
,
2841 (char *)dfs_req
, dfs_req_size
, CIFSMaxBufSize
,
2842 (char **)&dfs_rsp
, &dfs_rsp_size
);
2843 if (!is_retryable_error(rc
))
2845 usleep_range(512, 2048);
2846 } while (++retry_count
< 5);
2848 if (!rc
&& !dfs_rsp
)
2851 if (!is_retryable_error(rc
) && rc
!= -ENOENT
&& rc
!= -EOPNOTSUPP
)
2852 cifs_tcon_dbg(VFS
, "%s: ioctl error: rc=%d\n", __func__
, rc
);
2856 rc
= parse_dfs_referrals(dfs_rsp
, dfs_rsp_size
,
2857 num_of_nodes
, target_nodes
,
2858 nls_codepage
, remap
, search_name
,
2859 true /* is_unicode */);
2861 cifs_tcon_dbg(VFS
, "parse error in %s rc=%d\n", __func__
, rc
);
2866 if (tcon
&& !tcon
->ipc
) {
2867 /* ipc tcons are not refcounted */
2868 spin_lock(&cifs_tcp_ses_lock
);
2870 /* tc_count can never go negative */
2871 WARN_ON(tcon
->tc_count
< 0);
2872 spin_unlock(&cifs_tcp_ses_lock
);
2880 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2881 static int parse_reparse_posix(struct reparse_posix_data
*buf
,
2882 struct cifs_sb_info
*cifs_sb
,
2883 struct cifs_open_info_data
*data
)
2888 switch ((type
= le64_to_cpu(buf
->InodeType
))) {
2889 case NFS_SPECFILE_LNK
:
2890 len
= le16_to_cpu(buf
->ReparseDataLength
);
2891 data
->symlink_target
= cifs_strndup_from_utf16(buf
->DataBuffer
,
2893 cifs_sb
->local_nls
);
2894 if (!data
->symlink_target
)
2896 convert_delimiter(data
->symlink_target
, '/');
2897 cifs_dbg(FYI
, "%s: target path: %s\n",
2898 __func__
, data
->symlink_target
);
2900 case NFS_SPECFILE_CHR
:
2901 case NFS_SPECFILE_BLK
:
2902 case NFS_SPECFILE_FIFO
:
2903 case NFS_SPECFILE_SOCK
:
2906 cifs_dbg(VFS
, "%s: unhandled inode type: 0x%llx\n",
2913 static int parse_reparse_symlink(struct reparse_symlink_data_buffer
*sym
,
2914 u32 plen
, bool unicode
,
2915 struct cifs_sb_info
*cifs_sb
,
2916 struct cifs_open_info_data
*data
)
2921 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2923 offs
= le16_to_cpu(sym
->SubstituteNameOffset
);
2924 len
= le16_to_cpu(sym
->SubstituteNameLength
);
2925 if (offs
+ 20 > plen
|| offs
+ len
+ 20 > plen
) {
2926 cifs_dbg(VFS
, "srv returned malformed symlink buffer\n");
2930 data
->symlink_target
= cifs_strndup_from_utf16(sym
->PathBuffer
+ offs
,
2932 cifs_sb
->local_nls
);
2933 if (!data
->symlink_target
)
2936 convert_delimiter(data
->symlink_target
, '/');
2937 cifs_dbg(FYI
, "%s: target path: %s\n", __func__
, data
->symlink_target
);
2942 int parse_reparse_point(struct reparse_data_buffer
*buf
,
2943 u32 plen
, struct cifs_sb_info
*cifs_sb
,
2944 bool unicode
, struct cifs_open_info_data
*data
)
2946 data
->reparse
.buf
= buf
;
2948 /* See MS-FSCC 2.1.2 */
2949 switch (le32_to_cpu(buf
->ReparseTag
)) {
2950 case IO_REPARSE_TAG_NFS
:
2951 return parse_reparse_posix((struct reparse_posix_data
*)buf
,
2953 case IO_REPARSE_TAG_SYMLINK
:
2954 return parse_reparse_symlink(
2955 (struct reparse_symlink_data_buffer
*)buf
,
2956 plen
, unicode
, cifs_sb
, data
);
2957 case IO_REPARSE_TAG_LX_SYMLINK
:
2958 case IO_REPARSE_TAG_AF_UNIX
:
2959 case IO_REPARSE_TAG_LX_FIFO
:
2960 case IO_REPARSE_TAG_LX_CHR
:
2961 case IO_REPARSE_TAG_LX_BLK
:
2964 cifs_dbg(VFS
, "%s: unhandled reparse tag: 0x%08x\n",
2965 __func__
, le32_to_cpu(buf
->ReparseTag
));
2970 static int smb2_parse_reparse_point(struct cifs_sb_info
*cifs_sb
,
2971 struct kvec
*rsp_iov
,
2972 struct cifs_open_info_data
*data
)
2974 struct reparse_data_buffer
*buf
;
2975 struct smb2_ioctl_rsp
*io
= rsp_iov
->iov_base
;
2976 u32 plen
= le32_to_cpu(io
->OutputCount
);
2978 buf
= (struct reparse_data_buffer
*)((u8
*)io
+
2979 le32_to_cpu(io
->OutputOffset
));
2980 return parse_reparse_point(buf
, plen
, cifs_sb
, true, data
);
2983 static struct cifs_ntsd
*
2984 get_smb2_acl_by_fid(struct cifs_sb_info
*cifs_sb
,
2985 const struct cifs_fid
*cifsfid
, u32
*pacllen
, u32 info
)
2987 struct cifs_ntsd
*pntsd
= NULL
;
2989 int rc
= -EOPNOTSUPP
;
2990 struct tcon_link
*tlink
= cifs_sb_tlink(cifs_sb
);
2993 return ERR_CAST(tlink
);
2996 cifs_dbg(FYI
, "trying to get acl\n");
2998 rc
= SMB2_query_acl(xid
, tlink_tcon(tlink
), cifsfid
->persistent_fid
,
2999 cifsfid
->volatile_fid
, (void **)&pntsd
, pacllen
,
3003 cifs_put_tlink(tlink
);
3005 cifs_dbg(FYI
, "%s: rc = %d ACL len %d\n", __func__
, rc
, *pacllen
);
3012 static struct cifs_ntsd
*
3013 get_smb2_acl_by_path(struct cifs_sb_info
*cifs_sb
,
3014 const char *path
, u32
*pacllen
, u32 info
)
3016 struct cifs_ntsd
*pntsd
= NULL
;
3017 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
3020 struct cifs_tcon
*tcon
;
3021 struct tcon_link
*tlink
= cifs_sb_tlink(cifs_sb
);
3022 struct cifs_fid fid
;
3023 struct cifs_open_parms oparms
;
3026 cifs_dbg(FYI
, "get smb3 acl for path %s\n", path
);
3028 return ERR_CAST(tlink
);
3030 tcon
= tlink_tcon(tlink
);
3033 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
3040 oparms
= (struct cifs_open_parms
) {
3043 .desired_access
= READ_CONTROL
,
3044 .disposition
= FILE_OPEN
,
3046 * When querying an ACL, even if the file is a symlink
3047 * we want to open the source not the target, and so
3048 * the protocol requires that the client specify this
3049 * flag when opening a reparse point
3051 .create_options
= cifs_create_options(cifs_sb
, 0) |
3056 if (info
& SACL_SECINFO
)
3057 oparms
.desired_access
|= SYSTEM_SECURITY
;
3059 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
, NULL
,
3063 rc
= SMB2_query_acl(xid
, tlink_tcon(tlink
), fid
.persistent_fid
,
3064 fid
.volatile_fid
, (void **)&pntsd
, pacllen
,
3066 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
3069 cifs_put_tlink(tlink
);
3072 cifs_dbg(FYI
, "%s: rc = %d ACL len %d\n", __func__
, rc
, *pacllen
);
3079 set_smb2_acl(struct cifs_ntsd
*pnntsd
, __u32 acllen
,
3080 struct inode
*inode
, const char *path
, int aclflag
)
3082 u8 oplock
= SMB2_OPLOCK_LEVEL_NONE
;
3084 int rc
, access_flags
= 0;
3085 struct cifs_tcon
*tcon
;
3086 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
3087 struct tcon_link
*tlink
= cifs_sb_tlink(cifs_sb
);
3088 struct cifs_fid fid
;
3089 struct cifs_open_parms oparms
;
3092 cifs_dbg(FYI
, "set smb3 acl for path %s\n", path
);
3094 return PTR_ERR(tlink
);
3096 tcon
= tlink_tcon(tlink
);
3099 if (aclflag
& CIFS_ACL_OWNER
|| aclflag
& CIFS_ACL_GROUP
)
3100 access_flags
|= WRITE_OWNER
;
3101 if (aclflag
& CIFS_ACL_SACL
)
3102 access_flags
|= SYSTEM_SECURITY
;
3103 if (aclflag
& CIFS_ACL_DACL
)
3104 access_flags
|= WRITE_DAC
;
3106 utf16_path
= cifs_convert_path_to_utf16(path
, cifs_sb
);
3113 oparms
= (struct cifs_open_parms
) {
3115 .desired_access
= access_flags
,
3116 .create_options
= cifs_create_options(cifs_sb
, 0),
3117 .disposition
= FILE_OPEN
,
3122 rc
= SMB2_open(xid
, &oparms
, utf16_path
, &oplock
, NULL
, NULL
,
3126 rc
= SMB2_set_acl(xid
, tlink_tcon(tlink
), fid
.persistent_fid
,
3127 fid
.volatile_fid
, pnntsd
, acllen
, aclflag
);
3128 SMB2_close(xid
, tcon
, fid
.persistent_fid
, fid
.volatile_fid
);
3131 cifs_put_tlink(tlink
);
3136 /* Retrieve an ACL from the server */
3137 static struct cifs_ntsd
*
3138 get_smb2_acl(struct cifs_sb_info
*cifs_sb
,
3139 struct inode
*inode
, const char *path
,
3140 u32
*pacllen
, u32 info
)
3142 struct cifs_ntsd
*pntsd
= NULL
;
3143 struct cifsFileInfo
*open_file
= NULL
;
3145 if (inode
&& !(info
& SACL_SECINFO
))
3146 open_file
= find_readable_file(CIFS_I(inode
), true);
3147 if (!open_file
|| (info
& SACL_SECINFO
))
3148 return get_smb2_acl_by_path(cifs_sb
, path
, pacllen
, info
);
3150 pntsd
= get_smb2_acl_by_fid(cifs_sb
, &open_file
->fid
, pacllen
, info
);
3151 cifsFileInfo_put(open_file
);
3155 static long smb3_zero_data(struct file
*file
, struct cifs_tcon
*tcon
,
3156 loff_t offset
, loff_t len
, unsigned int xid
)
3158 struct cifsFileInfo
*cfile
= file
->private_data
;
3159 struct file_zero_data_information fsctl_buf
;
3161 cifs_dbg(FYI
, "Offset %lld len %lld\n", offset
, len
);
3163 fsctl_buf
.FileOffset
= cpu_to_le64(offset
);
3164 fsctl_buf
.BeyondFinalZero
= cpu_to_le64(offset
+ len
);
3166 return SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3167 cfile
->fid
.volatile_fid
, FSCTL_SET_ZERO_DATA
,
3169 sizeof(struct file_zero_data_information
),
3173 static long smb3_zero_range(struct file
*file
, struct cifs_tcon
*tcon
,
3174 loff_t offset
, loff_t len
, bool keep_size
)
3176 struct cifs_ses
*ses
= tcon
->ses
;
3177 struct inode
*inode
= file_inode(file
);
3178 struct cifsInodeInfo
*cifsi
= CIFS_I(inode
);
3179 struct cifsFileInfo
*cfile
= file
->private_data
;
3180 unsigned long long new_size
;
3186 trace_smb3_zero_enter(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3187 ses
->Suid
, offset
, len
);
3190 filemap_invalidate_lock(inode
->i_mapping
);
3193 * We zero the range through ioctl, so we need remove the page caches
3194 * first, otherwise the data may be inconsistent with the server.
3196 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
3198 /* if file not oplocked can't be sure whether asking to extend size */
3200 if (keep_size
== false && !CIFS_CACHE_READ(cifsi
))
3201 goto zero_range_exit
;
3203 rc
= smb3_zero_data(file
, tcon
, offset
, len
, xid
);
3205 goto zero_range_exit
;
3208 * do we also need to change the size of the file?
3210 new_size
= offset
+ len
;
3211 if (keep_size
== false && (unsigned long long)i_size_read(inode
) < new_size
) {
3212 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3213 cfile
->fid
.volatile_fid
, cfile
->pid
, new_size
);
3215 truncate_setsize(inode
, new_size
);
3216 fscache_resize_cookie(cifs_inode_cookie(inode
), new_size
);
3221 filemap_invalidate_unlock(inode
->i_mapping
);
3222 inode_unlock(inode
);
3225 trace_smb3_zero_err(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3226 ses
->Suid
, offset
, len
, rc
);
3228 trace_smb3_zero_done(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3229 ses
->Suid
, offset
, len
);
3233 static long smb3_punch_hole(struct file
*file
, struct cifs_tcon
*tcon
,
3234 loff_t offset
, loff_t len
)
3236 struct inode
*inode
= file_inode(file
);
3237 struct cifsFileInfo
*cfile
= file
->private_data
;
3238 struct file_zero_data_information fsctl_buf
;
3241 __u8 set_sparse
= 1;
3246 /* Need to make file sparse, if not already, before freeing range. */
3247 /* Consider adding equivalent for compressed since it could also work */
3248 if (!smb2_set_sparse(xid
, tcon
, cfile
, inode
, set_sparse
)) {
3253 filemap_invalidate_lock(inode
->i_mapping
);
3255 * We implement the punch hole through ioctl, so we need remove the page
3256 * caches first, otherwise the data may be inconsistent with the server.
3258 truncate_pagecache_range(inode
, offset
, offset
+ len
- 1);
3260 cifs_dbg(FYI
, "Offset %lld len %lld\n", offset
, len
);
3262 fsctl_buf
.FileOffset
= cpu_to_le64(offset
);
3263 fsctl_buf
.BeyondFinalZero
= cpu_to_le64(offset
+ len
);
3265 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3266 cfile
->fid
.volatile_fid
, FSCTL_SET_ZERO_DATA
,
3268 sizeof(struct file_zero_data_information
),
3269 CIFSMaxBufSize
, NULL
, NULL
);
3270 filemap_invalidate_unlock(inode
->i_mapping
);
3272 inode_unlock(inode
);
3277 static int smb3_simple_fallocate_write_range(unsigned int xid
,
3278 struct cifs_tcon
*tcon
,
3279 struct cifsFileInfo
*cfile
,
3280 loff_t off
, loff_t len
,
3283 struct cifs_io_parms io_parms
= {0};
3288 io_parms
.netfid
= cfile
->fid
.netfid
;
3289 io_parms
.pid
= current
->tgid
;
3290 io_parms
.tcon
= tcon
;
3291 io_parms
.persistent_fid
= cfile
->fid
.persistent_fid
;
3292 io_parms
.volatile_fid
= cfile
->fid
.volatile_fid
;
3295 io_parms
.offset
= off
;
3296 io_parms
.length
= len
;
3297 if (io_parms
.length
> SMB2_MAX_BUFFER_SIZE
)
3298 io_parms
.length
= SMB2_MAX_BUFFER_SIZE
;
3299 /* iov[0] is reserved for smb header */
3300 iov
[1].iov_base
= buf
;
3301 iov
[1].iov_len
= io_parms
.length
;
3302 rc
= SMB2_write(xid
, &io_parms
, &nbytes
, iov
, 1);
3314 static int smb3_simple_fallocate_range(unsigned int xid
,
3315 struct cifs_tcon
*tcon
,
3316 struct cifsFileInfo
*cfile
,
3317 loff_t off
, loff_t len
)
3319 struct file_allocated_range_buffer in_data
, *out_data
= NULL
, *tmp_data
;
3325 in_data
.file_offset
= cpu_to_le64(off
);
3326 in_data
.length
= cpu_to_le64(len
);
3327 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3328 cfile
->fid
.volatile_fid
,
3329 FSCTL_QUERY_ALLOCATED_RANGES
,
3330 (char *)&in_data
, sizeof(in_data
),
3331 1024 * sizeof(struct file_allocated_range_buffer
),
3332 (char **)&out_data
, &out_data_len
);
3336 buf
= kzalloc(1024 * 1024, GFP_KERNEL
);
3342 tmp_data
= out_data
;
3345 * The rest of the region is unmapped so write it all.
3347 if (out_data_len
== 0) {
3348 rc
= smb3_simple_fallocate_write_range(xid
, tcon
,
3349 cfile
, off
, len
, buf
);
3353 if (out_data_len
< sizeof(struct file_allocated_range_buffer
)) {
3358 if (off
< le64_to_cpu(tmp_data
->file_offset
)) {
3360 * We are at a hole. Write until the end of the region
3361 * or until the next allocated data,
3362 * whichever comes next.
3364 l
= le64_to_cpu(tmp_data
->file_offset
) - off
;
3367 rc
= smb3_simple_fallocate_write_range(xid
, tcon
,
3368 cfile
, off
, l
, buf
);
3377 * We are at a section of allocated data, just skip forward
3378 * until the end of the data or the end of the region
3379 * we are supposed to fallocate, whichever comes first.
3381 l
= le64_to_cpu(tmp_data
->length
);
3387 tmp_data
= &tmp_data
[1];
3388 out_data_len
-= sizeof(struct file_allocated_range_buffer
);
3398 static long smb3_simple_falloc(struct file
*file
, struct cifs_tcon
*tcon
,
3399 loff_t off
, loff_t len
, bool keep_size
)
3401 struct inode
*inode
;
3402 struct cifsInodeInfo
*cifsi
;
3403 struct cifsFileInfo
*cfile
= file
->private_data
;
3404 long rc
= -EOPNOTSUPP
;
3410 inode
= d_inode(cfile
->dentry
);
3411 cifsi
= CIFS_I(inode
);
3413 trace_smb3_falloc_enter(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3414 tcon
->ses
->Suid
, off
, len
);
3415 /* if file not oplocked can't be sure whether asking to extend size */
3416 if (!CIFS_CACHE_READ(cifsi
))
3417 if (keep_size
== false) {
3418 trace_smb3_falloc_err(xid
, cfile
->fid
.persistent_fid
,
3419 tcon
->tid
, tcon
->ses
->Suid
, off
, len
, rc
);
3425 * Extending the file
3427 if ((keep_size
== false) && i_size_read(inode
) < off
+ len
) {
3428 rc
= inode_newsize_ok(inode
, off
+ len
);
3432 if (cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
)
3433 smb2_set_sparse(xid
, tcon
, cfile
, inode
, false);
3435 new_eof
= off
+ len
;
3436 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3437 cfile
->fid
.volatile_fid
, cfile
->pid
, new_eof
);
3439 cifsi
->server_eof
= new_eof
;
3440 cifs_setsize(inode
, new_eof
);
3441 cifs_truncate_page(inode
->i_mapping
, inode
->i_size
);
3442 truncate_setsize(inode
, new_eof
);
3448 * Files are non-sparse by default so falloc may be a no-op
3449 * Must check if file sparse. If not sparse, and since we are not
3450 * extending then no need to do anything since file already allocated
3452 if ((cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
) == 0) {
3457 if (keep_size
== true) {
3459 * We can not preallocate pages beyond the end of the file
3462 if (off
>= i_size_read(inode
)) {
3467 * For fallocates that are partially beyond the end of file,
3468 * clamp len so we only fallocate up to the end of file.
3470 if (off
+ len
> i_size_read(inode
)) {
3471 len
= i_size_read(inode
) - off
;
3475 if ((keep_size
== true) || (i_size_read(inode
) >= off
+ len
)) {
3477 * At this point, we are trying to fallocate an internal
3478 * regions of a sparse file. Since smb2 does not have a
3479 * fallocate command we have two otions on how to emulate this.
3480 * We can either turn the entire file to become non-sparse
3481 * which we only do if the fallocate is for virtually
3482 * the whole file, or we can overwrite the region with zeroes
3483 * using SMB2_write, which could be prohibitevly expensive
3487 * We are only trying to fallocate a small region so
3488 * just write it with zero.
3490 if (len
<= 1024 * 1024) {
3491 rc
= smb3_simple_fallocate_range(xid
, tcon
, cfile
,
3497 * Check if falloc starts within first few pages of file
3498 * and ends within a few pages of the end of file to
3499 * ensure that most of file is being forced to be
3500 * fallocated now. If so then setting whole file sparse
3501 * ie potentially making a few extra pages at the beginning
3502 * or end of the file non-sparse via set_sparse is harmless.
3504 if ((off
> 8192) || (off
+ len
+ 8192 < i_size_read(inode
))) {
3510 smb2_set_sparse(xid
, tcon
, cfile
, inode
, false);
3515 trace_smb3_falloc_err(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3516 tcon
->ses
->Suid
, off
, len
, rc
);
3518 trace_smb3_falloc_done(xid
, cfile
->fid
.persistent_fid
, tcon
->tid
,
3519 tcon
->ses
->Suid
, off
, len
);
3525 static long smb3_collapse_range(struct file
*file
, struct cifs_tcon
*tcon
,
3526 loff_t off
, loff_t len
)
3530 struct inode
*inode
= file_inode(file
);
3531 struct cifsFileInfo
*cfile
= file
->private_data
;
3532 struct cifsInodeInfo
*cifsi
= CIFS_I(inode
);
3533 loff_t old_eof
, new_eof
;
3539 old_eof
= i_size_read(inode
);
3540 if ((off
>= old_eof
) ||
3541 off
+ len
>= old_eof
) {
3546 filemap_invalidate_lock(inode
->i_mapping
);
3547 rc
= filemap_write_and_wait_range(inode
->i_mapping
, off
, old_eof
- 1);
3551 truncate_pagecache_range(inode
, off
, old_eof
);
3553 rc
= smb2_copychunk_range(xid
, cfile
, cfile
, off
+ len
,
3554 old_eof
- off
- len
, off
);
3558 new_eof
= old_eof
- len
;
3559 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3560 cfile
->fid
.volatile_fid
, cfile
->pid
, new_eof
);
3566 cifsi
->server_eof
= i_size_read(inode
) - len
;
3567 truncate_setsize(inode
, cifsi
->server_eof
);
3568 fscache_resize_cookie(cifs_inode_cookie(inode
), cifsi
->server_eof
);
3570 filemap_invalidate_unlock(inode
->i_mapping
);
3572 inode_unlock(inode
);
3577 static long smb3_insert_range(struct file
*file
, struct cifs_tcon
*tcon
,
3578 loff_t off
, loff_t len
)
3582 struct cifsFileInfo
*cfile
= file
->private_data
;
3583 struct inode
*inode
= file_inode(file
);
3584 __u64 count
, old_eof
, new_eof
;
3590 old_eof
= i_size_read(inode
);
3591 if (off
>= old_eof
) {
3596 count
= old_eof
- off
;
3597 new_eof
= old_eof
+ len
;
3599 filemap_invalidate_lock(inode
->i_mapping
);
3600 rc
= filemap_write_and_wait_range(inode
->i_mapping
, off
, new_eof
- 1);
3603 truncate_pagecache_range(inode
, off
, old_eof
);
3605 rc
= SMB2_set_eof(xid
, tcon
, cfile
->fid
.persistent_fid
,
3606 cfile
->fid
.volatile_fid
, cfile
->pid
, new_eof
);
3610 truncate_setsize(inode
, new_eof
);
3611 fscache_resize_cookie(cifs_inode_cookie(inode
), i_size_read(inode
));
3613 rc
= smb2_copychunk_range(xid
, cfile
, cfile
, off
, count
, off
+ len
);
3617 rc
= smb3_zero_data(file
, tcon
, off
, len
, xid
);
3623 filemap_invalidate_unlock(inode
->i_mapping
);
3625 inode_unlock(inode
);
3630 static loff_t
smb3_llseek(struct file
*file
, struct cifs_tcon
*tcon
, loff_t offset
, int whence
)
3632 struct cifsFileInfo
*wrcfile
, *cfile
= file
->private_data
;
3633 struct cifsInodeInfo
*cifsi
;
3634 struct inode
*inode
;
3636 struct file_allocated_range_buffer in_data
, *out_data
= NULL
;
3640 if (whence
!= SEEK_HOLE
&& whence
!= SEEK_DATA
)
3641 return generic_file_llseek(file
, offset
, whence
);
3643 inode
= d_inode(cfile
->dentry
);
3644 cifsi
= CIFS_I(inode
);
3646 if (offset
< 0 || offset
>= i_size_read(inode
))
3651 * We need to be sure that all dirty pages are written as they
3652 * might fill holes on the server.
3653 * Note that we also MUST flush any written pages since at least
3654 * some servers (Windows2016) will not reflect recent writes in
3655 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3657 wrcfile
= find_writable_file(cifsi
, FIND_WR_ANY
);
3659 filemap_write_and_wait(inode
->i_mapping
);
3660 smb2_flush_file(xid
, tcon
, &wrcfile
->fid
);
3661 cifsFileInfo_put(wrcfile
);
3664 if (!(cifsi
->cifsAttrs
& FILE_ATTRIBUTE_SPARSE_FILE
)) {
3665 if (whence
== SEEK_HOLE
)
3666 offset
= i_size_read(inode
);
3670 in_data
.file_offset
= cpu_to_le64(offset
);
3671 in_data
.length
= cpu_to_le64(i_size_read(inode
));
3673 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3674 cfile
->fid
.volatile_fid
,
3675 FSCTL_QUERY_ALLOCATED_RANGES
,
3676 (char *)&in_data
, sizeof(in_data
),
3677 sizeof(struct file_allocated_range_buffer
),
3678 (char **)&out_data
, &out_data_len
);
3684 if (whence
== SEEK_HOLE
&& out_data_len
== 0)
3687 if (whence
== SEEK_DATA
&& out_data_len
== 0) {
3692 if (out_data_len
< sizeof(struct file_allocated_range_buffer
)) {
3696 if (whence
== SEEK_DATA
) {
3697 offset
= le64_to_cpu(out_data
->file_offset
);
3700 if (offset
< le64_to_cpu(out_data
->file_offset
))
3703 offset
= le64_to_cpu(out_data
->file_offset
) + le64_to_cpu(out_data
->length
);
3709 return vfs_setpos(file
, offset
, inode
->i_sb
->s_maxbytes
);
3714 static int smb3_fiemap(struct cifs_tcon
*tcon
,
3715 struct cifsFileInfo
*cfile
,
3716 struct fiemap_extent_info
*fei
, u64 start
, u64 len
)
3719 struct file_allocated_range_buffer in_data
, *out_data
;
3721 int i
, num
, rc
, flags
, last_blob
;
3724 rc
= fiemap_prep(d_inode(cfile
->dentry
), fei
, start
, &len
, 0);
3730 in_data
.file_offset
= cpu_to_le64(start
);
3731 in_data
.length
= cpu_to_le64(len
);
3733 rc
= SMB2_ioctl(xid
, tcon
, cfile
->fid
.persistent_fid
,
3734 cfile
->fid
.volatile_fid
,
3735 FSCTL_QUERY_ALLOCATED_RANGES
,
3736 (char *)&in_data
, sizeof(in_data
),
3737 1024 * sizeof(struct file_allocated_range_buffer
),
3738 (char **)&out_data
, &out_data_len
);
3747 if (out_data_len
&& out_data_len
< sizeof(struct file_allocated_range_buffer
)) {
3751 if (out_data_len
% sizeof(struct file_allocated_range_buffer
)) {
3756 num
= out_data_len
/ sizeof(struct file_allocated_range_buffer
);
3757 for (i
= 0; i
< num
; i
++) {
3759 if (i
== num
- 1 && last_blob
)
3760 flags
|= FIEMAP_EXTENT_LAST
;
3762 rc
= fiemap_fill_next_extent(fei
,
3763 le64_to_cpu(out_data
[i
].file_offset
),
3764 le64_to_cpu(out_data
[i
].file_offset
),
3765 le64_to_cpu(out_data
[i
].length
),
3776 next
= le64_to_cpu(out_data
[num
- 1].file_offset
) +
3777 le64_to_cpu(out_data
[num
- 1].length
);
3778 len
= len
- (next
- start
);
3789 static long smb3_fallocate(struct file
*file
, struct cifs_tcon
*tcon
, int mode
,
3790 loff_t off
, loff_t len
)
3792 /* KEEP_SIZE already checked for by do_fallocate */
3793 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3794 return smb3_punch_hole(file
, tcon
, off
, len
);
3795 else if (mode
& FALLOC_FL_ZERO_RANGE
) {
3796 if (mode
& FALLOC_FL_KEEP_SIZE
)
3797 return smb3_zero_range(file
, tcon
, off
, len
, true);
3798 return smb3_zero_range(file
, tcon
, off
, len
, false);
3799 } else if (mode
== FALLOC_FL_KEEP_SIZE
)
3800 return smb3_simple_falloc(file
, tcon
, off
, len
, true);
3801 else if (mode
== FALLOC_FL_COLLAPSE_RANGE
)
3802 return smb3_collapse_range(file
, tcon
, off
, len
);
3803 else if (mode
== FALLOC_FL_INSERT_RANGE
)
3804 return smb3_insert_range(file
, tcon
, off
, len
);
3806 return smb3_simple_falloc(file
, tcon
, off
, len
, false);
3812 smb2_downgrade_oplock(struct TCP_Server_Info
*server
,
3813 struct cifsInodeInfo
*cinode
, __u32 oplock
,
3814 unsigned int epoch
, bool *purge_cache
)
3816 server
->ops
->set_oplock_level(cinode
, oplock
, 0, NULL
);
3820 smb21_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
3821 unsigned int epoch
, bool *purge_cache
);
3824 smb3_downgrade_oplock(struct TCP_Server_Info
*server
,
3825 struct cifsInodeInfo
*cinode
, __u32 oplock
,
3826 unsigned int epoch
, bool *purge_cache
)
3828 unsigned int old_state
= cinode
->oplock
;
3829 unsigned int old_epoch
= cinode
->epoch
;
3830 unsigned int new_state
;
3832 if (epoch
> old_epoch
) {
3833 smb21_set_oplock_level(cinode
, oplock
, 0, NULL
);
3834 cinode
->epoch
= epoch
;
3837 new_state
= cinode
->oplock
;
3838 *purge_cache
= false;
3840 if ((old_state
& CIFS_CACHE_READ_FLG
) != 0 &&
3841 (new_state
& CIFS_CACHE_READ_FLG
) == 0)
3842 *purge_cache
= true;
3843 else if (old_state
== new_state
&& (epoch
- old_epoch
> 1))
3844 *purge_cache
= true;
3848 smb2_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
3849 unsigned int epoch
, bool *purge_cache
)
3852 cinode
->lease_granted
= false;
3853 if (oplock
== SMB2_OPLOCK_LEVEL_NOCHANGE
)
3855 if (oplock
== SMB2_OPLOCK_LEVEL_BATCH
) {
3856 cinode
->oplock
= CIFS_CACHE_RHW_FLG
;
3857 cifs_dbg(FYI
, "Batch Oplock granted on inode %p\n",
3858 &cinode
->netfs
.inode
);
3859 } else if (oplock
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
) {
3860 cinode
->oplock
= CIFS_CACHE_RW_FLG
;
3861 cifs_dbg(FYI
, "Exclusive Oplock granted on inode %p\n",
3862 &cinode
->netfs
.inode
);
3863 } else if (oplock
== SMB2_OPLOCK_LEVEL_II
) {
3864 cinode
->oplock
= CIFS_CACHE_READ_FLG
;
3865 cifs_dbg(FYI
, "Level II Oplock granted on inode %p\n",
3866 &cinode
->netfs
.inode
);
3872 smb21_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
3873 unsigned int epoch
, bool *purge_cache
)
3875 char message
[5] = {0};
3876 unsigned int new_oplock
= 0;
3879 cinode
->lease_granted
= true;
3880 if (oplock
== SMB2_OPLOCK_LEVEL_NOCHANGE
)
3883 /* Check if the server granted an oplock rather than a lease */
3884 if (oplock
& SMB2_OPLOCK_LEVEL_EXCLUSIVE
)
3885 return smb2_set_oplock_level(cinode
, oplock
, epoch
,
3888 if (oplock
& SMB2_LEASE_READ_CACHING_HE
) {
3889 new_oplock
|= CIFS_CACHE_READ_FLG
;
3890 strcat(message
, "R");
3892 if (oplock
& SMB2_LEASE_HANDLE_CACHING_HE
) {
3893 new_oplock
|= CIFS_CACHE_HANDLE_FLG
;
3894 strcat(message
, "H");
3896 if (oplock
& SMB2_LEASE_WRITE_CACHING_HE
) {
3897 new_oplock
|= CIFS_CACHE_WRITE_FLG
;
3898 strcat(message
, "W");
3901 strncpy(message
, "None", sizeof(message
));
3903 cinode
->oplock
= new_oplock
;
3904 cifs_dbg(FYI
, "%s Lease granted on inode %p\n", message
,
3905 &cinode
->netfs
.inode
);
3909 smb3_set_oplock_level(struct cifsInodeInfo
*cinode
, __u32 oplock
,
3910 unsigned int epoch
, bool *purge_cache
)
3912 unsigned int old_oplock
= cinode
->oplock
;
3914 smb21_set_oplock_level(cinode
, oplock
, epoch
, purge_cache
);
3917 *purge_cache
= false;
3918 if (old_oplock
== CIFS_CACHE_READ_FLG
) {
3919 if (cinode
->oplock
== CIFS_CACHE_READ_FLG
&&
3920 (epoch
- cinode
->epoch
> 0))
3921 *purge_cache
= true;
3922 else if (cinode
->oplock
== CIFS_CACHE_RH_FLG
&&
3923 (epoch
- cinode
->epoch
> 1))
3924 *purge_cache
= true;
3925 else if (cinode
->oplock
== CIFS_CACHE_RHW_FLG
&&
3926 (epoch
- cinode
->epoch
> 1))
3927 *purge_cache
= true;
3928 else if (cinode
->oplock
== 0 &&
3929 (epoch
- cinode
->epoch
> 0))
3930 *purge_cache
= true;
3931 } else if (old_oplock
== CIFS_CACHE_RH_FLG
) {
3932 if (cinode
->oplock
== CIFS_CACHE_RH_FLG
&&
3933 (epoch
- cinode
->epoch
> 0))
3934 *purge_cache
= true;
3935 else if (cinode
->oplock
== CIFS_CACHE_RHW_FLG
&&
3936 (epoch
- cinode
->epoch
> 1))
3937 *purge_cache
= true;
3939 cinode
->epoch
= epoch
;
3943 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3945 smb2_is_read_op(__u32 oplock
)
3947 return oplock
== SMB2_OPLOCK_LEVEL_II
;
3949 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
3952 smb21_is_read_op(__u32 oplock
)
3954 return (oplock
& SMB2_LEASE_READ_CACHING_HE
) &&
3955 !(oplock
& SMB2_LEASE_WRITE_CACHING_HE
);
3959 map_oplock_to_lease(u8 oplock
)
3961 if (oplock
== SMB2_OPLOCK_LEVEL_EXCLUSIVE
)
3962 return SMB2_LEASE_WRITE_CACHING_LE
| SMB2_LEASE_READ_CACHING_LE
;
3963 else if (oplock
== SMB2_OPLOCK_LEVEL_II
)
3964 return SMB2_LEASE_READ_CACHING_LE
;
3965 else if (oplock
== SMB2_OPLOCK_LEVEL_BATCH
)
3966 return SMB2_LEASE_HANDLE_CACHING_LE
| SMB2_LEASE_READ_CACHING_LE
|
3967 SMB2_LEASE_WRITE_CACHING_LE
;
3972 smb2_create_lease_buf(u8
*lease_key
, u8 oplock
)
3974 struct create_lease
*buf
;
3976 buf
= kzalloc(sizeof(struct create_lease
), GFP_KERNEL
);
3980 memcpy(&buf
->lcontext
.LeaseKey
, lease_key
, SMB2_LEASE_KEY_SIZE
);
3981 buf
->lcontext
.LeaseState
= map_oplock_to_lease(oplock
);
3983 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
3984 (struct create_lease
, lcontext
));
3985 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct lease_context
));
3986 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
3987 (struct create_lease
, Name
));
3988 buf
->ccontext
.NameLength
= cpu_to_le16(4);
3989 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3998 smb3_create_lease_buf(u8
*lease_key
, u8 oplock
)
4000 struct create_lease_v2
*buf
;
4002 buf
= kzalloc(sizeof(struct create_lease_v2
), GFP_KERNEL
);
4006 memcpy(&buf
->lcontext
.LeaseKey
, lease_key
, SMB2_LEASE_KEY_SIZE
);
4007 buf
->lcontext
.LeaseState
= map_oplock_to_lease(oplock
);
4009 buf
->ccontext
.DataOffset
= cpu_to_le16(offsetof
4010 (struct create_lease_v2
, lcontext
));
4011 buf
->ccontext
.DataLength
= cpu_to_le32(sizeof(struct lease_context_v2
));
4012 buf
->ccontext
.NameOffset
= cpu_to_le16(offsetof
4013 (struct create_lease_v2
, Name
));
4014 buf
->ccontext
.NameLength
= cpu_to_le16(4);
4015 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4024 smb2_parse_lease_buf(void *buf
, unsigned int *epoch
, char *lease_key
)
4026 struct create_lease
*lc
= (struct create_lease
*)buf
;
4028 *epoch
= 0; /* not used */
4029 if (lc
->lcontext
.LeaseFlags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE
)
4030 return SMB2_OPLOCK_LEVEL_NOCHANGE
;
4031 return le32_to_cpu(lc
->lcontext
.LeaseState
);
4035 smb3_parse_lease_buf(void *buf
, unsigned int *epoch
, char *lease_key
)
4037 struct create_lease_v2
*lc
= (struct create_lease_v2
*)buf
;
4039 *epoch
= le16_to_cpu(lc
->lcontext
.Epoch
);
4040 if (lc
->lcontext
.LeaseFlags
& SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE
)
4041 return SMB2_OPLOCK_LEVEL_NOCHANGE
;
4043 memcpy(lease_key
, &lc
->lcontext
.LeaseKey
, SMB2_LEASE_KEY_SIZE
);
4044 return le32_to_cpu(lc
->lcontext
.LeaseState
);
4048 smb2_wp_retry_size(struct inode
*inode
)
4050 return min_t(unsigned int, CIFS_SB(inode
->i_sb
)->ctx
->wsize
,
4051 SMB2_MAX_BUFFER_SIZE
);
4055 smb2_dir_needs_close(struct cifsFileInfo
*cfile
)
4057 return !cfile
->invalidHandle
;
4061 fill_transform_hdr(struct smb2_transform_hdr
*tr_hdr
, unsigned int orig_len
,
4062 struct smb_rqst
*old_rq
, __le16 cipher_type
)
4064 struct smb2_hdr
*shdr
=
4065 (struct smb2_hdr
*)old_rq
->rq_iov
[0].iov_base
;
4067 memset(tr_hdr
, 0, sizeof(struct smb2_transform_hdr
));
4068 tr_hdr
->ProtocolId
= SMB2_TRANSFORM_PROTO_NUM
;
4069 tr_hdr
->OriginalMessageSize
= cpu_to_le32(orig_len
);
4070 tr_hdr
->Flags
= cpu_to_le16(0x01);
4071 if ((cipher_type
== SMB2_ENCRYPTION_AES128_GCM
) ||
4072 (cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
4073 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_GCM_NONCE
);
4075 get_random_bytes(&tr_hdr
->Nonce
, SMB3_AES_CCM_NONCE
);
4076 memcpy(&tr_hdr
->SessionId
, &shdr
->SessionId
, 8);
4079 static void *smb2_aead_req_alloc(struct crypto_aead
*tfm
, const struct smb_rqst
*rqst
,
4080 int num_rqst
, const u8
*sig
, u8
**iv
,
4081 struct aead_request
**req
, struct sg_table
*sgt
,
4082 unsigned int *num_sgs
, size_t *sensitive_size
)
4084 unsigned int req_size
= sizeof(**req
) + crypto_aead_reqsize(tfm
);
4085 unsigned int iv_size
= crypto_aead_ivsize(tfm
);
4089 *num_sgs
= cifs_get_num_sgs(rqst
, num_rqst
, sig
);
4090 if (IS_ERR_VALUE((long)(int)*num_sgs
))
4091 return ERR_PTR(*num_sgs
);
4094 len
+= crypto_aead_alignmask(tfm
) & ~(crypto_tfm_ctx_alignment() - 1);
4095 len
= ALIGN(len
, crypto_tfm_ctx_alignment());
4097 len
= ALIGN(len
, __alignof__(struct scatterlist
));
4098 len
+= array_size(*num_sgs
, sizeof(struct scatterlist
));
4099 *sensitive_size
= len
;
4101 p
= kvzalloc(len
, GFP_NOFS
);
4103 return ERR_PTR(-ENOMEM
);
4105 *iv
= (u8
*)PTR_ALIGN(p
, crypto_aead_alignmask(tfm
) + 1);
4106 *req
= (struct aead_request
*)PTR_ALIGN(*iv
+ iv_size
,
4107 crypto_tfm_ctx_alignment());
4108 sgt
->sgl
= (struct scatterlist
*)PTR_ALIGN((u8
*)*req
+ req_size
,
4109 __alignof__(struct scatterlist
));
4113 static void *smb2_get_aead_req(struct crypto_aead
*tfm
, struct smb_rqst
*rqst
,
4114 int num_rqst
, const u8
*sig
, u8
**iv
,
4115 struct aead_request
**req
, struct scatterlist
**sgl
,
4116 size_t *sensitive_size
)
4118 struct sg_table sgtable
= {};
4119 unsigned int skip
, num_sgs
, i
, j
;
4123 p
= smb2_aead_req_alloc(tfm
, rqst
, num_rqst
, sig
, iv
, req
, &sgtable
,
4124 &num_sgs
, sensitive_size
);
4128 sg_init_marker(sgtable
.sgl
, num_sgs
);
4131 * The first rqst has a transform header where the
4132 * first 20 bytes are not part of the encrypted blob.
4136 for (i
= 0; i
< num_rqst
; i
++) {
4137 struct iov_iter
*iter
= &rqst
[i
].rq_iter
;
4138 size_t count
= iov_iter_count(iter
);
4140 for (j
= 0; j
< rqst
[i
].rq_nvec
; j
++) {
4141 cifs_sg_set_buf(&sgtable
,
4142 rqst
[i
].rq_iov
[j
].iov_base
+ skip
,
4143 rqst
[i
].rq_iov
[j
].iov_len
- skip
);
4145 /* See the above comment on the 'skip' assignment */
4148 sgtable
.orig_nents
= sgtable
.nents
;
4150 rc
= extract_iter_to_sg(iter
, count
, &sgtable
,
4151 num_sgs
- sgtable
.nents
, 0);
4152 iov_iter_revert(iter
, rc
);
4153 sgtable
.orig_nents
= sgtable
.nents
;
4156 cifs_sg_set_buf(&sgtable
, sig
, SMB2_SIGNATURE_SIZE
);
4157 sg_mark_end(&sgtable
.sgl
[sgtable
.nents
- 1]);
4163 smb2_get_enc_key(struct TCP_Server_Info
*server
, __u64 ses_id
, int enc
, u8
*key
)
4165 struct TCP_Server_Info
*pserver
;
4166 struct cifs_ses
*ses
;
4169 /* If server is a channel, select the primary channel */
4170 pserver
= SERVER_IS_CHAN(server
) ? server
->primary_server
: server
;
4172 spin_lock(&cifs_tcp_ses_lock
);
4173 list_for_each_entry(ses
, &pserver
->smb_ses_list
, smb_ses_list
) {
4174 if (ses
->Suid
== ses_id
) {
4175 spin_lock(&ses
->ses_lock
);
4176 ses_enc_key
= enc
? ses
->smb3encryptionkey
:
4177 ses
->smb3decryptionkey
;
4178 memcpy(key
, ses_enc_key
, SMB3_ENC_DEC_KEY_SIZE
);
4179 spin_unlock(&ses
->ses_lock
);
4180 spin_unlock(&cifs_tcp_ses_lock
);
4184 spin_unlock(&cifs_tcp_ses_lock
);
4186 trace_smb3_ses_not_found(ses_id
);
4191 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4192 * iov[0] - transform header (associate data),
4193 * iov[1-N] - SMB2 header and pages - data to encrypt.
4194 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4198 crypt_message(struct TCP_Server_Info
*server
, int num_rqst
,
4199 struct smb_rqst
*rqst
, int enc
)
4201 struct smb2_transform_hdr
*tr_hdr
=
4202 (struct smb2_transform_hdr
*)rqst
[0].rq_iov
[0].iov_base
;
4203 unsigned int assoc_data_len
= sizeof(struct smb2_transform_hdr
) - 20;
4205 struct scatterlist
*sg
;
4206 u8 sign
[SMB2_SIGNATURE_SIZE
] = {};
4207 u8 key
[SMB3_ENC_DEC_KEY_SIZE
];
4208 struct aead_request
*req
;
4210 DECLARE_CRYPTO_WAIT(wait
);
4211 struct crypto_aead
*tfm
;
4212 unsigned int crypt_len
= le32_to_cpu(tr_hdr
->OriginalMessageSize
);
4214 size_t sensitive_size
;
4216 rc
= smb2_get_enc_key(server
, le64_to_cpu(tr_hdr
->SessionId
), enc
, key
);
4218 cifs_server_dbg(FYI
, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__
,
4219 enc
? "en" : "de", le64_to_cpu(tr_hdr
->SessionId
));
4223 rc
= smb3_crypto_aead_allocate(server
);
4225 cifs_server_dbg(VFS
, "%s: crypto alloc failed\n", __func__
);
4229 tfm
= enc
? server
->secmech
.enc
: server
->secmech
.dec
;
4231 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES256_CCM
) ||
4232 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
4233 rc
= crypto_aead_setkey(tfm
, key
, SMB3_GCM256_CRYPTKEY_SIZE
);
4235 rc
= crypto_aead_setkey(tfm
, key
, SMB3_GCM128_CRYPTKEY_SIZE
);
4238 cifs_server_dbg(VFS
, "%s: Failed to set aead key %d\n", __func__
, rc
);
4242 rc
= crypto_aead_setauthsize(tfm
, SMB2_SIGNATURE_SIZE
);
4244 cifs_server_dbg(VFS
, "%s: Failed to set authsize %d\n", __func__
, rc
);
4248 creq
= smb2_get_aead_req(tfm
, rqst
, num_rqst
, sign
, &iv
, &req
, &sg
,
4251 return PTR_ERR(creq
);
4254 memcpy(sign
, &tr_hdr
->Signature
, SMB2_SIGNATURE_SIZE
);
4255 crypt_len
+= SMB2_SIGNATURE_SIZE
;
4258 if ((server
->cipher_type
== SMB2_ENCRYPTION_AES128_GCM
) ||
4259 (server
->cipher_type
== SMB2_ENCRYPTION_AES256_GCM
))
4260 memcpy(iv
, (char *)tr_hdr
->Nonce
, SMB3_AES_GCM_NONCE
);
4263 memcpy(iv
+ 1, (char *)tr_hdr
->Nonce
, SMB3_AES_CCM_NONCE
);
4266 aead_request_set_tfm(req
, tfm
);
4267 aead_request_set_crypt(req
, sg
, sg
, crypt_len
, iv
);
4268 aead_request_set_ad(req
, assoc_data_len
);
4270 aead_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
4271 crypto_req_done
, &wait
);
4273 rc
= crypto_wait_req(enc
? crypto_aead_encrypt(req
)
4274 : crypto_aead_decrypt(req
), &wait
);
4277 memcpy(&tr_hdr
->Signature
, sign
, SMB2_SIGNATURE_SIZE
);
4279 kvfree_sensitive(creq
, sensitive_size
);
4284 * Clear a read buffer, discarding the folios which have XA_MARK_0 set.
4286 static void cifs_clear_xarray_buffer(struct xarray
*buffer
)
4288 struct folio
*folio
;
4290 XA_STATE(xas
, buffer
, 0);
4293 xas_for_each_marked(&xas
, folio
, ULONG_MAX
, XA_MARK_0
) {
4301 smb3_free_compound_rqst(int num_rqst
, struct smb_rqst
*rqst
)
4305 for (i
= 0; i
< num_rqst
; i
++)
4306 if (!xa_empty(&rqst
[i
].rq_buffer
))
4307 cifs_clear_xarray_buffer(&rqst
[i
].rq_buffer
);
4311 * This function will initialize new_rq and encrypt the content.
4312 * The first entry, new_rq[0], only contains a single iov which contains
4313 * a smb2_transform_hdr and is pre-allocated by the caller.
4314 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4316 * The end result is an array of smb_rqst structures where the first structure
4317 * only contains a single iov for the transform header which we then can pass
4318 * to crypt_message().
4320 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4321 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4324 smb3_init_transform_rq(struct TCP_Server_Info
*server
, int num_rqst
,
4325 struct smb_rqst
*new_rq
, struct smb_rqst
*old_rq
)
4327 struct smb2_transform_hdr
*tr_hdr
= new_rq
[0].rq_iov
[0].iov_base
;
4329 unsigned int orig_len
= 0;
4333 for (i
= 1; i
< num_rqst
; i
++) {
4334 struct smb_rqst
*old
= &old_rq
[i
- 1];
4335 struct smb_rqst
*new = &new_rq
[i
];
4336 struct xarray
*buffer
= &new->rq_buffer
;
4337 size_t size
= iov_iter_count(&old
->rq_iter
), seg
, copied
= 0;
4339 orig_len
+= smb_rqst_len(server
, old
);
4340 new->rq_iov
= old
->rq_iov
;
4341 new->rq_nvec
= old
->rq_nvec
;
4346 unsigned int npages
= DIV_ROUND_UP(size
, PAGE_SIZE
);
4348 for (j
= 0; j
< npages
; j
++) {
4352 page
= alloc_page(GFP_KERNEL
|__GFP_HIGHMEM
);
4356 o
= xa_store(buffer
, j
, page
, GFP_KERNEL
);
4363 xa_set_mark(buffer
, j
, XA_MARK_0
);
4365 seg
= min_t(size_t, size
- copied
, PAGE_SIZE
);
4366 if (copy_page_from_iter(page
, 0, seg
, &old
->rq_iter
) != seg
) {
4372 iov_iter_xarray(&new->rq_iter
, ITER_SOURCE
,
4374 new->rq_iter_size
= size
;
4378 /* fill the 1st iov with a transform header */
4379 fill_transform_hdr(tr_hdr
, orig_len
, old_rq
, server
->cipher_type
);
4381 rc
= crypt_message(server
, num_rqst
, new_rq
, 1);
4382 cifs_dbg(FYI
, "Encrypt message returned %d\n", rc
);
4389 smb3_free_compound_rqst(num_rqst
- 1, &new_rq
[1]);
4394 smb3_is_transform_hdr(void *buf
)
4396 struct smb2_transform_hdr
*trhdr
= buf
;
4398 return trhdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
;
4402 decrypt_raw_data(struct TCP_Server_Info
*server
, char *buf
,
4403 unsigned int buf_data_size
, struct iov_iter
*iter
,
4407 struct smb_rqst rqst
= {NULL
};
4408 size_t iter_size
= 0;
4411 iov
[0].iov_base
= buf
;
4412 iov
[0].iov_len
= sizeof(struct smb2_transform_hdr
);
4413 iov
[1].iov_base
= buf
+ sizeof(struct smb2_transform_hdr
);
4414 iov
[1].iov_len
= buf_data_size
;
4419 rqst
.rq_iter
= *iter
;
4420 rqst
.rq_iter_size
= iov_iter_count(iter
);
4421 iter_size
= iov_iter_count(iter
);
4424 rc
= crypt_message(server
, 1, &rqst
, 0);
4425 cifs_dbg(FYI
, "Decrypt message returned %d\n", rc
);
4430 memmove(buf
, iov
[1].iov_base
, buf_data_size
);
4433 server
->total_read
= buf_data_size
+ iter_size
;
4439 cifs_copy_pages_to_iter(struct xarray
*pages
, unsigned int data_size
,
4440 unsigned int skip
, struct iov_iter
*iter
)
4443 unsigned long index
;
4445 xa_for_each(pages
, index
, page
) {
4446 size_t n
, len
= min_t(unsigned int, PAGE_SIZE
- skip
, data_size
);
4448 n
= copy_page_to_iter(page
, skip
, len
, iter
);
4450 cifs_dbg(VFS
, "%s: something went wrong\n", __func__
);
4461 handle_read_data(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
,
4462 char *buf
, unsigned int buf_len
, struct xarray
*pages
,
4463 unsigned int pages_len
, bool is_offloaded
)
4465 unsigned int data_offset
;
4466 unsigned int data_len
;
4467 unsigned int cur_off
;
4468 unsigned int cur_page_idx
;
4469 unsigned int pad_len
;
4470 struct cifs_readdata
*rdata
= mid
->callback_data
;
4471 struct smb2_hdr
*shdr
= (struct smb2_hdr
*)buf
;
4473 bool use_rdma_mr
= false;
4475 if (shdr
->Command
!= SMB2_READ
) {
4476 cifs_server_dbg(VFS
, "only big read responses are supported\n");
4480 if (server
->ops
->is_session_expired
&&
4481 server
->ops
->is_session_expired(buf
)) {
4483 cifs_reconnect(server
, true);
4487 if (server
->ops
->is_status_pending
&&
4488 server
->ops
->is_status_pending(buf
, server
))
4491 /* set up first two iov to get credits */
4492 rdata
->iov
[0].iov_base
= buf
;
4493 rdata
->iov
[0].iov_len
= 0;
4494 rdata
->iov
[1].iov_base
= buf
;
4495 rdata
->iov
[1].iov_len
=
4496 min_t(unsigned int, buf_len
, server
->vals
->read_rsp_size
);
4497 cifs_dbg(FYI
, "0: iov_base=%p iov_len=%zu\n",
4498 rdata
->iov
[0].iov_base
, rdata
->iov
[0].iov_len
);
4499 cifs_dbg(FYI
, "1: iov_base=%p iov_len=%zu\n",
4500 rdata
->iov
[1].iov_base
, rdata
->iov
[1].iov_len
);
4502 rdata
->result
= server
->ops
->map_error(buf
, true);
4503 if (rdata
->result
!= 0) {
4504 cifs_dbg(FYI
, "%s: server returned error %d\n",
4505 __func__
, rdata
->result
);
4506 /* normal error on read response */
4508 mid
->mid_state
= MID_RESPONSE_RECEIVED
;
4510 dequeue_mid(mid
, false);
4514 data_offset
= server
->ops
->read_data_offset(buf
);
4515 #ifdef CONFIG_CIFS_SMB_DIRECT
4516 use_rdma_mr
= rdata
->mr
;
4518 data_len
= server
->ops
->read_data_length(buf
, use_rdma_mr
);
4520 if (data_offset
< server
->vals
->read_rsp_size
) {
4522 * win2k8 sometimes sends an offset of 0 when the read
4523 * is beyond the EOF. Treat it as if the data starts just after
4526 cifs_dbg(FYI
, "%s: data offset (%u) inside read response header\n",
4527 __func__
, data_offset
);
4528 data_offset
= server
->vals
->read_rsp_size
;
4529 } else if (data_offset
> MAX_CIFS_SMALL_BUFFER_SIZE
) {
4530 /* data_offset is beyond the end of smallbuf */
4531 cifs_dbg(FYI
, "%s: data offset (%u) beyond end of smallbuf\n",
4532 __func__
, data_offset
);
4533 rdata
->result
= -EIO
;
4535 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4537 dequeue_mid(mid
, rdata
->result
);
4541 pad_len
= data_offset
- server
->vals
->read_rsp_size
;
4543 if (buf_len
<= data_offset
) {
4544 /* read response payload is in pages */
4545 cur_page_idx
= pad_len
/ PAGE_SIZE
;
4546 cur_off
= pad_len
% PAGE_SIZE
;
4548 if (cur_page_idx
!= 0) {
4549 /* data offset is beyond the 1st page of response */
4550 cifs_dbg(FYI
, "%s: data offset (%u) beyond 1st page of response\n",
4551 __func__
, data_offset
);
4552 rdata
->result
= -EIO
;
4554 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4556 dequeue_mid(mid
, rdata
->result
);
4560 if (data_len
> pages_len
- pad_len
) {
4561 /* data_len is corrupt -- discard frame */
4562 rdata
->result
= -EIO
;
4564 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4566 dequeue_mid(mid
, rdata
->result
);
4570 /* Copy the data to the output I/O iterator. */
4571 rdata
->result
= cifs_copy_pages_to_iter(pages
, pages_len
,
4572 cur_off
, &rdata
->iter
);
4573 if (rdata
->result
!= 0) {
4575 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4577 dequeue_mid(mid
, rdata
->result
);
4580 rdata
->got_bytes
= pages_len
;
4582 } else if (buf_len
>= data_offset
+ data_len
) {
4583 /* read response payload is in buf */
4584 WARN_ONCE(pages
&& !xa_empty(pages
),
4585 "read data can be either in buf or in pages");
4586 length
= copy_to_iter(buf
+ data_offset
, data_len
, &rdata
->iter
);
4589 rdata
->got_bytes
= data_len
;
4591 /* read response payload cannot be in both buf and pages */
4592 WARN_ONCE(1, "buf can not contain only a part of read data");
4593 rdata
->result
= -EIO
;
4595 mid
->mid_state
= MID_RESPONSE_MALFORMED
;
4597 dequeue_mid(mid
, rdata
->result
);
4602 mid
->mid_state
= MID_RESPONSE_RECEIVED
;
4604 dequeue_mid(mid
, false);
4608 struct smb2_decrypt_work
{
4609 struct work_struct decrypt
;
4610 struct TCP_Server_Info
*server
;
4611 struct xarray buffer
;
4617 static void smb2_decrypt_offload(struct work_struct
*work
)
4619 struct smb2_decrypt_work
*dw
= container_of(work
,
4620 struct smb2_decrypt_work
, decrypt
);
4622 struct mid_q_entry
*mid
;
4623 struct iov_iter iter
;
4625 iov_iter_xarray(&iter
, ITER_DEST
, &dw
->buffer
, 0, dw
->len
);
4626 rc
= decrypt_raw_data(dw
->server
, dw
->buf
, dw
->server
->vals
->read_rsp_size
,
4629 cifs_dbg(VFS
, "error decrypting rc=%d\n", rc
);
4633 dw
->server
->lstrp
= jiffies
;
4634 mid
= smb2_find_dequeue_mid(dw
->server
, dw
->buf
);
4636 cifs_dbg(FYI
, "mid not found\n");
4638 mid
->decrypted
= true;
4639 rc
= handle_read_data(dw
->server
, mid
, dw
->buf
,
4640 dw
->server
->vals
->read_rsp_size
,
4641 &dw
->buffer
, dw
->len
,
4644 #ifdef CONFIG_CIFS_STATS2
4645 mid
->when_received
= jiffies
;
4647 if (dw
->server
->ops
->is_network_name_deleted
)
4648 dw
->server
->ops
->is_network_name_deleted(dw
->buf
,
4653 spin_lock(&dw
->server
->srv_lock
);
4654 if (dw
->server
->tcpStatus
== CifsNeedReconnect
) {
4655 spin_lock(&dw
->server
->mid_lock
);
4656 mid
->mid_state
= MID_RETRY_NEEDED
;
4657 spin_unlock(&dw
->server
->mid_lock
);
4658 spin_unlock(&dw
->server
->srv_lock
);
4661 spin_lock(&dw
->server
->mid_lock
);
4662 mid
->mid_state
= MID_REQUEST_SUBMITTED
;
4663 mid
->mid_flags
&= ~(MID_DELETED
);
4664 list_add_tail(&mid
->qhead
,
4665 &dw
->server
->pending_mid_q
);
4666 spin_unlock(&dw
->server
->mid_lock
);
4667 spin_unlock(&dw
->server
->srv_lock
);
4674 cifs_clear_xarray_buffer(&dw
->buffer
);
4675 cifs_small_buf_release(dw
->buf
);
4681 receive_encrypted_read(struct TCP_Server_Info
*server
, struct mid_q_entry
**mid
,
4685 char *buf
= server
->smallbuf
;
4686 struct smb2_transform_hdr
*tr_hdr
= (struct smb2_transform_hdr
*)buf
;
4687 struct iov_iter iter
;
4688 unsigned int len
, npages
;
4689 unsigned int buflen
= server
->pdu_size
;
4692 struct smb2_decrypt_work
*dw
;
4694 dw
= kzalloc(sizeof(struct smb2_decrypt_work
), GFP_KERNEL
);
4697 xa_init(&dw
->buffer
);
4698 INIT_WORK(&dw
->decrypt
, smb2_decrypt_offload
);
4699 dw
->server
= server
;
4702 len
= min_t(unsigned int, buflen
, server
->vals
->read_rsp_size
+
4703 sizeof(struct smb2_transform_hdr
)) - HEADER_SIZE(server
) + 1;
4705 rc
= cifs_read_from_socket(server
, buf
+ HEADER_SIZE(server
) - 1, len
);
4708 server
->total_read
+= rc
;
4710 len
= le32_to_cpu(tr_hdr
->OriginalMessageSize
) -
4711 server
->vals
->read_rsp_size
;
4713 npages
= DIV_ROUND_UP(len
, PAGE_SIZE
);
4716 for (; i
< npages
; i
++) {
4719 page
= alloc_page(GFP_KERNEL
|__GFP_HIGHMEM
);
4723 old
= xa_store(&dw
->buffer
, i
, page
, GFP_KERNEL
);
4724 if (xa_is_err(old
)) {
4729 xa_set_mark(&dw
->buffer
, i
, XA_MARK_0
);
4732 iov_iter_xarray(&iter
, ITER_DEST
, &dw
->buffer
, 0, npages
* PAGE_SIZE
);
4734 /* Read the data into the buffer and clear excess bufferage. */
4735 rc
= cifs_read_iter_from_socket(server
, &iter
, dw
->len
);
4739 server
->total_read
+= rc
;
4740 if (rc
< npages
* PAGE_SIZE
)
4741 iov_iter_zero(npages
* PAGE_SIZE
- rc
, &iter
);
4742 iov_iter_revert(&iter
, npages
* PAGE_SIZE
);
4743 iov_iter_truncate(&iter
, dw
->len
);
4745 rc
= cifs_discard_remaining_data(server
);
4750 * For large reads, offload to different thread for better performance,
4751 * use more cores decrypting which can be expensive
4754 if ((server
->min_offload
) && (server
->in_flight
> 1) &&
4755 (server
->pdu_size
>= server
->min_offload
)) {
4756 dw
->buf
= server
->smallbuf
;
4757 server
->smallbuf
= (char *)cifs_small_buf_get();
4759 queue_work(decrypt_wq
, &dw
->decrypt
);
4760 *num_mids
= 0; /* worker thread takes care of finding mid */
4764 rc
= decrypt_raw_data(server
, buf
, server
->vals
->read_rsp_size
,
4769 *mid
= smb2_find_mid(server
, buf
);
4771 cifs_dbg(FYI
, "mid not found\n");
4773 cifs_dbg(FYI
, "mid found\n");
4774 (*mid
)->decrypted
= true;
4775 rc
= handle_read_data(server
, *mid
, buf
,
4776 server
->vals
->read_rsp_size
,
4777 &dw
->buffer
, dw
->len
, false);
4779 if (server
->ops
->is_network_name_deleted
) {
4780 server
->ops
->is_network_name_deleted(buf
,
4787 cifs_clear_xarray_buffer(&dw
->buffer
);
4792 cifs_discard_remaining_data(server
);
4797 receive_encrypted_standard(struct TCP_Server_Info
*server
,
4798 struct mid_q_entry
**mids
, char **bufs
,
4802 char *buf
= server
->smallbuf
;
4803 struct smb2_hdr
*shdr
;
4804 unsigned int pdu_length
= server
->pdu_size
;
4805 unsigned int buf_size
;
4806 unsigned int next_cmd
;
4807 struct mid_q_entry
*mid_entry
;
4809 char *next_buffer
= NULL
;
4813 /* switch to large buffer if too big for a small one */
4814 if (pdu_length
> MAX_CIFS_SMALL_BUFFER_SIZE
) {
4815 server
->large_buf
= true;
4816 memcpy(server
->bigbuf
, buf
, server
->total_read
);
4817 buf
= server
->bigbuf
;
4820 /* now read the rest */
4821 length
= cifs_read_from_socket(server
, buf
+ HEADER_SIZE(server
) - 1,
4822 pdu_length
- HEADER_SIZE(server
) + 1);
4825 server
->total_read
+= length
;
4827 buf_size
= pdu_length
- sizeof(struct smb2_transform_hdr
);
4828 length
= decrypt_raw_data(server
, buf
, buf_size
, NULL
, false);
4832 next_is_large
= server
->large_buf
;
4834 shdr
= (struct smb2_hdr
*)buf
;
4835 next_cmd
= le32_to_cpu(shdr
->NextCommand
);
4837 if (WARN_ON_ONCE(next_cmd
> pdu_length
))
4840 next_buffer
= (char *)cifs_buf_get();
4842 next_buffer
= (char *)cifs_small_buf_get();
4843 memcpy(next_buffer
, buf
+ next_cmd
, pdu_length
- next_cmd
);
4846 mid_entry
= smb2_find_mid(server
, buf
);
4847 if (mid_entry
== NULL
)
4848 cifs_dbg(FYI
, "mid not found\n");
4850 cifs_dbg(FYI
, "mid found\n");
4851 mid_entry
->decrypted
= true;
4852 mid_entry
->resp_buf_size
= server
->pdu_size
;
4855 if (*num_mids
>= MAX_COMPOUND
) {
4856 cifs_server_dbg(VFS
, "too many PDUs in compound\n");
4859 bufs
[*num_mids
] = buf
;
4860 mids
[(*num_mids
)++] = mid_entry
;
4862 if (mid_entry
&& mid_entry
->handle
)
4863 ret
= mid_entry
->handle(server
, mid_entry
);
4865 ret
= cifs_handle_standard(server
, mid_entry
);
4867 if (ret
== 0 && next_cmd
) {
4868 pdu_length
-= next_cmd
;
4869 server
->large_buf
= next_is_large
;
4871 server
->bigbuf
= buf
= next_buffer
;
4873 server
->smallbuf
= buf
= next_buffer
;
4875 } else if (ret
!= 0) {
4877 * ret != 0 here means that we didn't get to handle_mid() thus
4878 * server->smallbuf and server->bigbuf are still valid. We need
4879 * to free next_buffer because it is not going to be used
4883 free_rsp_buf(CIFS_LARGE_BUFFER
, next_buffer
);
4885 free_rsp_buf(CIFS_SMALL_BUFFER
, next_buffer
);
4892 smb3_receive_transform(struct TCP_Server_Info
*server
,
4893 struct mid_q_entry
**mids
, char **bufs
, int *num_mids
)
4895 char *buf
= server
->smallbuf
;
4896 unsigned int pdu_length
= server
->pdu_size
;
4897 struct smb2_transform_hdr
*tr_hdr
= (struct smb2_transform_hdr
*)buf
;
4898 unsigned int orig_len
= le32_to_cpu(tr_hdr
->OriginalMessageSize
);
4900 if (pdu_length
< sizeof(struct smb2_transform_hdr
) +
4901 sizeof(struct smb2_hdr
)) {
4902 cifs_server_dbg(VFS
, "Transform message is too small (%u)\n",
4904 cifs_reconnect(server
, true);
4905 return -ECONNABORTED
;
4908 if (pdu_length
< orig_len
+ sizeof(struct smb2_transform_hdr
)) {
4909 cifs_server_dbg(VFS
, "Transform message is broken\n");
4910 cifs_reconnect(server
, true);
4911 return -ECONNABORTED
;
4914 /* TODO: add support for compounds containing READ. */
4915 if (pdu_length
> CIFSMaxBufSize
+ MAX_HEADER_SIZE(server
)) {
4916 return receive_encrypted_read(server
, &mids
[0], num_mids
);
4919 return receive_encrypted_standard(server
, mids
, bufs
, num_mids
);
4923 smb3_handle_read_data(struct TCP_Server_Info
*server
, struct mid_q_entry
*mid
)
4925 char *buf
= server
->large_buf
? server
->bigbuf
: server
->smallbuf
;
4927 return handle_read_data(server
, mid
, buf
, server
->pdu_size
,
4931 static int smb2_next_header(struct TCP_Server_Info
*server
, char *buf
,
4934 struct smb2_hdr
*hdr
= (struct smb2_hdr
*)buf
;
4935 struct smb2_transform_hdr
*t_hdr
= (struct smb2_transform_hdr
*)buf
;
4937 if (hdr
->ProtocolId
== SMB2_TRANSFORM_PROTO_NUM
) {
4938 *noff
= le32_to_cpu(t_hdr
->OriginalMessageSize
);
4939 if (unlikely(check_add_overflow(*noff
, sizeof(*t_hdr
), noff
)))
4942 *noff
= le32_to_cpu(hdr
->NextCommand
);
4944 if (unlikely(*noff
&& *noff
< MID_HEADER_SIZE(server
)))
4949 int cifs_sfu_make_node(unsigned int xid
, struct inode
*inode
,
4950 struct dentry
*dentry
, struct cifs_tcon
*tcon
,
4951 const char *full_path
, umode_t mode
, dev_t dev
)
4953 struct cifs_open_info_data buf
= {};
4954 struct TCP_Server_Info
*server
= tcon
->ses
->server
;
4955 struct cifs_open_parms oparms
;
4956 struct cifs_io_parms io_parms
= {};
4957 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
4958 struct cifs_fid fid
;
4959 unsigned int bytes_written
;
4960 struct win_dev
*pdev
;
4962 __u32 oplock
= server
->oplocks
? REQ_OPLOCK
: 0;
4965 if (!S_ISCHR(mode
) && !S_ISBLK(mode
) && !S_ISFIFO(mode
))
4968 oparms
= (struct cifs_open_parms
) {
4971 .desired_access
= GENERIC_WRITE
,
4972 .create_options
= cifs_create_options(cifs_sb
, CREATE_NOT_DIR
|
4973 CREATE_OPTION_SPECIAL
),
4974 .disposition
= FILE_CREATE
,
4979 rc
= server
->ops
->open(xid
, &oparms
, &oplock
, &buf
);
4984 * BB Do not bother to decode buf since no local inode yet to put
4985 * timestamps in, but we can reuse it safely.
4987 pdev
= (struct win_dev
*)&buf
.fi
;
4988 io_parms
.pid
= current
->tgid
;
4989 io_parms
.tcon
= tcon
;
4990 io_parms
.length
= sizeof(*pdev
);
4991 iov
[1].iov_base
= pdev
;
4992 iov
[1].iov_len
= sizeof(*pdev
);
4993 if (S_ISCHR(mode
)) {
4994 memcpy(pdev
->type
, "IntxCHR", 8);
4995 pdev
->major
= cpu_to_le64(MAJOR(dev
));
4996 pdev
->minor
= cpu_to_le64(MINOR(dev
));
4997 } else if (S_ISBLK(mode
)) {
4998 memcpy(pdev
->type
, "IntxBLK", 8);
4999 pdev
->major
= cpu_to_le64(MAJOR(dev
));
5000 pdev
->minor
= cpu_to_le64(MINOR(dev
));
5001 } else if (S_ISFIFO(mode
)) {
5002 memcpy(pdev
->type
, "LnxFIFO", 8);
5005 rc
= server
->ops
->sync_write(xid
, &fid
, &io_parms
,
5006 &bytes_written
, iov
, 1);
5007 server
->ops
->close(xid
, tcon
, &fid
);
5009 /* FIXME: add code here to set EAs */
5010 cifs_free_open_info(&buf
);
5014 static inline u64
mode_nfs_type(mode_t mode
)
5016 switch (mode
& S_IFMT
) {
5017 case S_IFBLK
: return NFS_SPECFILE_BLK
;
5018 case S_IFCHR
: return NFS_SPECFILE_CHR
;
5019 case S_IFIFO
: return NFS_SPECFILE_FIFO
;
5020 case S_IFSOCK
: return NFS_SPECFILE_SOCK
;
5025 static int nfs_set_reparse_buf(struct reparse_posix_data
*buf
,
5026 mode_t mode
, dev_t dev
,
5034 switch ((type
= mode_nfs_type(mode
))) {
5035 case NFS_SPECFILE_BLK
:
5036 case NFS_SPECFILE_CHR
:
5037 dlen
= sizeof(__le64
);
5039 case NFS_SPECFILE_FIFO
:
5040 case NFS_SPECFILE_SOCK
:
5047 buf
->ReparseTag
= cpu_to_le32(IO_REPARSE_TAG_NFS
);
5049 buf
->InodeType
= cpu_to_le64(type
);
5050 buf
->ReparseDataLength
= cpu_to_le16(len
+ dlen
-
5051 sizeof(struct reparse_data_buffer
));
5052 *(__le64
*)buf
->DataBuffer
= cpu_to_le64(((u64
)MAJOR(dev
) << 32) |
5054 iov
->iov_base
= buf
;
5055 iov
->iov_len
= len
+ dlen
;
5059 static int nfs_make_node(unsigned int xid
, struct inode
*inode
,
5060 struct dentry
*dentry
, struct cifs_tcon
*tcon
,
5061 const char *full_path
, umode_t mode
, dev_t dev
)
5063 struct cifs_open_info_data data
;
5064 struct reparse_posix_data
*p
;
5067 __u8 buf
[sizeof(*p
) + sizeof(__le64
)];
5070 p
= (struct reparse_posix_data
*)buf
;
5071 rc
= nfs_set_reparse_buf(p
, mode
, dev
, &iov
);
5075 data
= (struct cifs_open_info_data
) {
5076 .reparse_point
= true,
5077 .reparse
= { .tag
= IO_REPARSE_TAG_NFS
, .posix
= p
, },
5080 new = smb2_get_reparse_inode(&data
, inode
->i_sb
, xid
,
5081 tcon
, full_path
, &iov
);
5083 d_instantiate(dentry
, new);
5086 cifs_free_open_info(&data
);
5090 static int smb2_create_reparse_symlink(const unsigned int xid
,
5091 struct inode
*inode
,
5092 struct dentry
*dentry
,
5093 struct cifs_tcon
*tcon
,
5094 const char *full_path
,
5095 const char *symname
)
5097 struct reparse_symlink_data_buffer
*buf
= NULL
;
5098 struct cifs_open_info_data data
;
5099 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
5107 sym
= kstrdup(symname
, GFP_KERNEL
);
5111 data
= (struct cifs_open_info_data
) {
5112 .reparse_point
= true,
5113 .reparse
= { .tag
= IO_REPARSE_TAG_SYMLINK
, },
5114 .symlink_target
= sym
,
5117 path
= cifs_convert_path_to_utf16(symname
, cifs_sb
);
5123 plen
= 2 * UniStrnlen((wchar_t *)path
, PATH_MAX
);
5124 len
= sizeof(*buf
) + plen
* 2;
5125 buf
= kzalloc(len
, GFP_KERNEL
);
5131 buf
->ReparseTag
= cpu_to_le32(IO_REPARSE_TAG_SYMLINK
);
5132 buf
->ReparseDataLength
= cpu_to_le16(len
- sizeof(struct reparse_data_buffer
));
5133 buf
->SubstituteNameOffset
= cpu_to_le16(plen
);
5134 buf
->SubstituteNameLength
= cpu_to_le16(plen
);
5135 memcpy(&buf
->PathBuffer
[plen
], path
, plen
);
5136 buf
->PrintNameOffset
= 0;
5137 buf
->PrintNameLength
= cpu_to_le16(plen
);
5138 memcpy(buf
->PathBuffer
, path
, plen
);
5139 buf
->Flags
= cpu_to_le32(*symname
!= '/' ? SYMLINK_FLAG_RELATIVE
: 0);
5143 new = smb2_get_reparse_inode(&data
, inode
->i_sb
, xid
,
5144 tcon
, full_path
, &iov
);
5146 d_instantiate(dentry
, new);
5151 cifs_free_open_info(&data
);
5156 static int smb2_make_node(unsigned int xid
, struct inode
*inode
,
5157 struct dentry
*dentry
, struct cifs_tcon
*tcon
,
5158 const char *full_path
, umode_t mode
, dev_t dev
)
5160 struct cifs_sb_info
*cifs_sb
= CIFS_SB(inode
->i_sb
);
5164 * Check if mounted with mount parm 'sfu' mount parm.
5165 * SFU emulation should work with all servers, but only
5166 * supports block and char device (no socket & fifo),
5167 * and was used by default in earlier versions of Windows
5169 if (cifs_sb
->mnt_cifs_flags
& CIFS_MOUNT_UNX_EMUL
) {
5170 rc
= cifs_sfu_make_node(xid
, inode
, dentry
, tcon
,
5171 full_path
, mode
, dev
);
5173 rc
= nfs_make_node(xid
, inode
, dentry
, tcon
,
5174 full_path
, mode
, dev
);
5179 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5180 struct smb_version_operations smb20_operations
= {
5181 .compare_fids
= smb2_compare_fids
,
5182 .setup_request
= smb2_setup_request
,
5183 .setup_async_request
= smb2_setup_async_request
,
5184 .check_receive
= smb2_check_receive
,
5185 .add_credits
= smb2_add_credits
,
5186 .set_credits
= smb2_set_credits
,
5187 .get_credits_field
= smb2_get_credits_field
,
5188 .get_credits
= smb2_get_credits
,
5189 .wait_mtu_credits
= cifs_wait_mtu_credits
,
5190 .get_next_mid
= smb2_get_next_mid
,
5191 .revert_current_mid
= smb2_revert_current_mid
,
5192 .read_data_offset
= smb2_read_data_offset
,
5193 .read_data_length
= smb2_read_data_length
,
5194 .map_error
= map_smb2_to_linux_error
,
5195 .find_mid
= smb2_find_mid
,
5196 .check_message
= smb2_check_message
,
5197 .dump_detail
= smb2_dump_detail
,
5198 .clear_stats
= smb2_clear_stats
,
5199 .print_stats
= smb2_print_stats
,
5200 .is_oplock_break
= smb2_is_valid_oplock_break
,
5201 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5202 .downgrade_oplock
= smb2_downgrade_oplock
,
5203 .need_neg
= smb2_need_neg
,
5204 .negotiate
= smb2_negotiate
,
5205 .negotiate_wsize
= smb2_negotiate_wsize
,
5206 .negotiate_rsize
= smb2_negotiate_rsize
,
5207 .sess_setup
= SMB2_sess_setup
,
5208 .logoff
= SMB2_logoff
,
5209 .tree_connect
= SMB2_tcon
,
5210 .tree_disconnect
= SMB2_tdis
,
5211 .qfs_tcon
= smb2_qfs_tcon
,
5212 .is_path_accessible
= smb2_is_path_accessible
,
5213 .can_echo
= smb2_can_echo
,
5215 .query_path_info
= smb2_query_path_info
,
5216 .query_reparse_point
= smb2_query_reparse_point
,
5217 .get_srv_inum
= smb2_get_srv_inum
,
5218 .query_file_info
= smb2_query_file_info
,
5219 .set_path_size
= smb2_set_path_size
,
5220 .set_file_size
= smb2_set_file_size
,
5221 .set_file_info
= smb2_set_file_info
,
5222 .set_compression
= smb2_set_compression
,
5223 .mkdir
= smb2_mkdir
,
5224 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5225 .rmdir
= smb2_rmdir
,
5226 .unlink
= smb2_unlink
,
5227 .rename
= smb2_rename_path
,
5228 .create_hardlink
= smb2_create_hardlink
,
5229 .parse_reparse_point
= smb2_parse_reparse_point
,
5230 .query_mf_symlink
= smb3_query_mf_symlink
,
5231 .create_mf_symlink
= smb3_create_mf_symlink
,
5232 .create_reparse_symlink
= smb2_create_reparse_symlink
,
5233 .open
= smb2_open_file
,
5234 .set_fid
= smb2_set_fid
,
5235 .close
= smb2_close_file
,
5236 .flush
= smb2_flush_file
,
5237 .async_readv
= smb2_async_readv
,
5238 .async_writev
= smb2_async_writev
,
5239 .sync_read
= smb2_sync_read
,
5240 .sync_write
= smb2_sync_write
,
5241 .query_dir_first
= smb2_query_dir_first
,
5242 .query_dir_next
= smb2_query_dir_next
,
5243 .close_dir
= smb2_close_dir
,
5244 .calc_smb_size
= smb2_calc_size
,
5245 .is_status_pending
= smb2_is_status_pending
,
5246 .is_session_expired
= smb2_is_session_expired
,
5247 .oplock_response
= smb2_oplock_response
,
5248 .queryfs
= smb2_queryfs
,
5249 .mand_lock
= smb2_mand_lock
,
5250 .mand_unlock_range
= smb2_unlock_range
,
5251 .push_mand_locks
= smb2_push_mandatory_locks
,
5252 .get_lease_key
= smb2_get_lease_key
,
5253 .set_lease_key
= smb2_set_lease_key
,
5254 .new_lease_key
= smb2_new_lease_key
,
5255 .calc_signature
= smb2_calc_signature
,
5256 .is_read_op
= smb2_is_read_op
,
5257 .set_oplock_level
= smb2_set_oplock_level
,
5258 .create_lease_buf
= smb2_create_lease_buf
,
5259 .parse_lease_buf
= smb2_parse_lease_buf
,
5260 .copychunk_range
= smb2_copychunk_range
,
5261 .wp_retry_size
= smb2_wp_retry_size
,
5262 .dir_needs_close
= smb2_dir_needs_close
,
5263 .get_dfs_refer
= smb2_get_dfs_refer
,
5264 .select_sectype
= smb2_select_sectype
,
5265 #ifdef CONFIG_CIFS_XATTR
5266 .query_all_EAs
= smb2_query_eas
,
5267 .set_EA
= smb2_set_ea
,
5268 #endif /* CIFS_XATTR */
5269 .get_acl
= get_smb2_acl
,
5270 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5271 .set_acl
= set_smb2_acl
,
5272 .next_header
= smb2_next_header
,
5273 .ioctl_query_info
= smb2_ioctl_query_info
,
5274 .make_node
= smb2_make_node
,
5275 .fiemap
= smb3_fiemap
,
5276 .llseek
= smb3_llseek
,
5277 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5278 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5280 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5282 struct smb_version_operations smb21_operations
= {
5283 .compare_fids
= smb2_compare_fids
,
5284 .setup_request
= smb2_setup_request
,
5285 .setup_async_request
= smb2_setup_async_request
,
5286 .check_receive
= smb2_check_receive
,
5287 .add_credits
= smb2_add_credits
,
5288 .set_credits
= smb2_set_credits
,
5289 .get_credits_field
= smb2_get_credits_field
,
5290 .get_credits
= smb2_get_credits
,
5291 .wait_mtu_credits
= smb2_wait_mtu_credits
,
5292 .adjust_credits
= smb2_adjust_credits
,
5293 .get_next_mid
= smb2_get_next_mid
,
5294 .revert_current_mid
= smb2_revert_current_mid
,
5295 .read_data_offset
= smb2_read_data_offset
,
5296 .read_data_length
= smb2_read_data_length
,
5297 .map_error
= map_smb2_to_linux_error
,
5298 .find_mid
= smb2_find_mid
,
5299 .check_message
= smb2_check_message
,
5300 .dump_detail
= smb2_dump_detail
,
5301 .clear_stats
= smb2_clear_stats
,
5302 .print_stats
= smb2_print_stats
,
5303 .is_oplock_break
= smb2_is_valid_oplock_break
,
5304 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5305 .downgrade_oplock
= smb2_downgrade_oplock
,
5306 .need_neg
= smb2_need_neg
,
5307 .negotiate
= smb2_negotiate
,
5308 .negotiate_wsize
= smb2_negotiate_wsize
,
5309 .negotiate_rsize
= smb2_negotiate_rsize
,
5310 .sess_setup
= SMB2_sess_setup
,
5311 .logoff
= SMB2_logoff
,
5312 .tree_connect
= SMB2_tcon
,
5313 .tree_disconnect
= SMB2_tdis
,
5314 .qfs_tcon
= smb2_qfs_tcon
,
5315 .is_path_accessible
= smb2_is_path_accessible
,
5316 .can_echo
= smb2_can_echo
,
5318 .query_path_info
= smb2_query_path_info
,
5319 .query_reparse_point
= smb2_query_reparse_point
,
5320 .get_srv_inum
= smb2_get_srv_inum
,
5321 .query_file_info
= smb2_query_file_info
,
5322 .set_path_size
= smb2_set_path_size
,
5323 .set_file_size
= smb2_set_file_size
,
5324 .set_file_info
= smb2_set_file_info
,
5325 .set_compression
= smb2_set_compression
,
5326 .mkdir
= smb2_mkdir
,
5327 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5328 .rmdir
= smb2_rmdir
,
5329 .unlink
= smb2_unlink
,
5330 .rename
= smb2_rename_path
,
5331 .create_hardlink
= smb2_create_hardlink
,
5332 .parse_reparse_point
= smb2_parse_reparse_point
,
5333 .query_mf_symlink
= smb3_query_mf_symlink
,
5334 .create_mf_symlink
= smb3_create_mf_symlink
,
5335 .create_reparse_symlink
= smb2_create_reparse_symlink
,
5336 .open
= smb2_open_file
,
5337 .set_fid
= smb2_set_fid
,
5338 .close
= smb2_close_file
,
5339 .flush
= smb2_flush_file
,
5340 .async_readv
= smb2_async_readv
,
5341 .async_writev
= smb2_async_writev
,
5342 .sync_read
= smb2_sync_read
,
5343 .sync_write
= smb2_sync_write
,
5344 .query_dir_first
= smb2_query_dir_first
,
5345 .query_dir_next
= smb2_query_dir_next
,
5346 .close_dir
= smb2_close_dir
,
5347 .calc_smb_size
= smb2_calc_size
,
5348 .is_status_pending
= smb2_is_status_pending
,
5349 .is_session_expired
= smb2_is_session_expired
,
5350 .oplock_response
= smb2_oplock_response
,
5351 .queryfs
= smb2_queryfs
,
5352 .mand_lock
= smb2_mand_lock
,
5353 .mand_unlock_range
= smb2_unlock_range
,
5354 .push_mand_locks
= smb2_push_mandatory_locks
,
5355 .get_lease_key
= smb2_get_lease_key
,
5356 .set_lease_key
= smb2_set_lease_key
,
5357 .new_lease_key
= smb2_new_lease_key
,
5358 .calc_signature
= smb2_calc_signature
,
5359 .is_read_op
= smb21_is_read_op
,
5360 .set_oplock_level
= smb21_set_oplock_level
,
5361 .create_lease_buf
= smb2_create_lease_buf
,
5362 .parse_lease_buf
= smb2_parse_lease_buf
,
5363 .copychunk_range
= smb2_copychunk_range
,
5364 .wp_retry_size
= smb2_wp_retry_size
,
5365 .dir_needs_close
= smb2_dir_needs_close
,
5366 .enum_snapshots
= smb3_enum_snapshots
,
5367 .notify
= smb3_notify
,
5368 .get_dfs_refer
= smb2_get_dfs_refer
,
5369 .select_sectype
= smb2_select_sectype
,
5370 #ifdef CONFIG_CIFS_XATTR
5371 .query_all_EAs
= smb2_query_eas
,
5372 .set_EA
= smb2_set_ea
,
5373 #endif /* CIFS_XATTR */
5374 .get_acl
= get_smb2_acl
,
5375 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5376 .set_acl
= set_smb2_acl
,
5377 .next_header
= smb2_next_header
,
5378 .ioctl_query_info
= smb2_ioctl_query_info
,
5379 .make_node
= smb2_make_node
,
5380 .fiemap
= smb3_fiemap
,
5381 .llseek
= smb3_llseek
,
5382 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5383 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5386 struct smb_version_operations smb30_operations
= {
5387 .compare_fids
= smb2_compare_fids
,
5388 .setup_request
= smb2_setup_request
,
5389 .setup_async_request
= smb2_setup_async_request
,
5390 .check_receive
= smb2_check_receive
,
5391 .add_credits
= smb2_add_credits
,
5392 .set_credits
= smb2_set_credits
,
5393 .get_credits_field
= smb2_get_credits_field
,
5394 .get_credits
= smb2_get_credits
,
5395 .wait_mtu_credits
= smb2_wait_mtu_credits
,
5396 .adjust_credits
= smb2_adjust_credits
,
5397 .get_next_mid
= smb2_get_next_mid
,
5398 .revert_current_mid
= smb2_revert_current_mid
,
5399 .read_data_offset
= smb2_read_data_offset
,
5400 .read_data_length
= smb2_read_data_length
,
5401 .map_error
= map_smb2_to_linux_error
,
5402 .find_mid
= smb2_find_mid
,
5403 .check_message
= smb2_check_message
,
5404 .dump_detail
= smb2_dump_detail
,
5405 .clear_stats
= smb2_clear_stats
,
5406 .print_stats
= smb2_print_stats
,
5407 .dump_share_caps
= smb2_dump_share_caps
,
5408 .is_oplock_break
= smb2_is_valid_oplock_break
,
5409 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5410 .downgrade_oplock
= smb3_downgrade_oplock
,
5411 .need_neg
= smb2_need_neg
,
5412 .negotiate
= smb2_negotiate
,
5413 .negotiate_wsize
= smb3_negotiate_wsize
,
5414 .negotiate_rsize
= smb3_negotiate_rsize
,
5415 .sess_setup
= SMB2_sess_setup
,
5416 .logoff
= SMB2_logoff
,
5417 .tree_connect
= SMB2_tcon
,
5418 .tree_disconnect
= SMB2_tdis
,
5419 .qfs_tcon
= smb3_qfs_tcon
,
5420 .is_path_accessible
= smb2_is_path_accessible
,
5421 .can_echo
= smb2_can_echo
,
5423 .query_path_info
= smb2_query_path_info
,
5424 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5425 .query_reparse_point
= smb2_query_reparse_point
,
5426 .get_srv_inum
= smb2_get_srv_inum
,
5427 .query_file_info
= smb2_query_file_info
,
5428 .set_path_size
= smb2_set_path_size
,
5429 .set_file_size
= smb2_set_file_size
,
5430 .set_file_info
= smb2_set_file_info
,
5431 .set_compression
= smb2_set_compression
,
5432 .mkdir
= smb2_mkdir
,
5433 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5434 .rmdir
= smb2_rmdir
,
5435 .unlink
= smb2_unlink
,
5436 .rename
= smb2_rename_path
,
5437 .create_hardlink
= smb2_create_hardlink
,
5438 .parse_reparse_point
= smb2_parse_reparse_point
,
5439 .query_mf_symlink
= smb3_query_mf_symlink
,
5440 .create_mf_symlink
= smb3_create_mf_symlink
,
5441 .create_reparse_symlink
= smb2_create_reparse_symlink
,
5442 .open
= smb2_open_file
,
5443 .set_fid
= smb2_set_fid
,
5444 .close
= smb2_close_file
,
5445 .close_getattr
= smb2_close_getattr
,
5446 .flush
= smb2_flush_file
,
5447 .async_readv
= smb2_async_readv
,
5448 .async_writev
= smb2_async_writev
,
5449 .sync_read
= smb2_sync_read
,
5450 .sync_write
= smb2_sync_write
,
5451 .query_dir_first
= smb2_query_dir_first
,
5452 .query_dir_next
= smb2_query_dir_next
,
5453 .close_dir
= smb2_close_dir
,
5454 .calc_smb_size
= smb2_calc_size
,
5455 .is_status_pending
= smb2_is_status_pending
,
5456 .is_session_expired
= smb2_is_session_expired
,
5457 .oplock_response
= smb2_oplock_response
,
5458 .queryfs
= smb2_queryfs
,
5459 .mand_lock
= smb2_mand_lock
,
5460 .mand_unlock_range
= smb2_unlock_range
,
5461 .push_mand_locks
= smb2_push_mandatory_locks
,
5462 .get_lease_key
= smb2_get_lease_key
,
5463 .set_lease_key
= smb2_set_lease_key
,
5464 .new_lease_key
= smb2_new_lease_key
,
5465 .generate_signingkey
= generate_smb30signingkey
,
5466 .calc_signature
= smb3_calc_signature
,
5467 .set_integrity
= smb3_set_integrity
,
5468 .is_read_op
= smb21_is_read_op
,
5469 .set_oplock_level
= smb3_set_oplock_level
,
5470 .create_lease_buf
= smb3_create_lease_buf
,
5471 .parse_lease_buf
= smb3_parse_lease_buf
,
5472 .copychunk_range
= smb2_copychunk_range
,
5473 .duplicate_extents
= smb2_duplicate_extents
,
5474 .validate_negotiate
= smb3_validate_negotiate
,
5475 .wp_retry_size
= smb2_wp_retry_size
,
5476 .dir_needs_close
= smb2_dir_needs_close
,
5477 .fallocate
= smb3_fallocate
,
5478 .enum_snapshots
= smb3_enum_snapshots
,
5479 .notify
= smb3_notify
,
5480 .init_transform_rq
= smb3_init_transform_rq
,
5481 .is_transform_hdr
= smb3_is_transform_hdr
,
5482 .receive_transform
= smb3_receive_transform
,
5483 .get_dfs_refer
= smb2_get_dfs_refer
,
5484 .select_sectype
= smb2_select_sectype
,
5485 #ifdef CONFIG_CIFS_XATTR
5486 .query_all_EAs
= smb2_query_eas
,
5487 .set_EA
= smb2_set_ea
,
5488 #endif /* CIFS_XATTR */
5489 .get_acl
= get_smb2_acl
,
5490 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5491 .set_acl
= set_smb2_acl
,
5492 .next_header
= smb2_next_header
,
5493 .ioctl_query_info
= smb2_ioctl_query_info
,
5494 .make_node
= smb2_make_node
,
5495 .fiemap
= smb3_fiemap
,
5496 .llseek
= smb3_llseek
,
5497 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5498 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5501 struct smb_version_operations smb311_operations
= {
5502 .compare_fids
= smb2_compare_fids
,
5503 .setup_request
= smb2_setup_request
,
5504 .setup_async_request
= smb2_setup_async_request
,
5505 .check_receive
= smb2_check_receive
,
5506 .add_credits
= smb2_add_credits
,
5507 .set_credits
= smb2_set_credits
,
5508 .get_credits_field
= smb2_get_credits_field
,
5509 .get_credits
= smb2_get_credits
,
5510 .wait_mtu_credits
= smb2_wait_mtu_credits
,
5511 .adjust_credits
= smb2_adjust_credits
,
5512 .get_next_mid
= smb2_get_next_mid
,
5513 .revert_current_mid
= smb2_revert_current_mid
,
5514 .read_data_offset
= smb2_read_data_offset
,
5515 .read_data_length
= smb2_read_data_length
,
5516 .map_error
= map_smb2_to_linux_error
,
5517 .find_mid
= smb2_find_mid
,
5518 .check_message
= smb2_check_message
,
5519 .dump_detail
= smb2_dump_detail
,
5520 .clear_stats
= smb2_clear_stats
,
5521 .print_stats
= smb2_print_stats
,
5522 .dump_share_caps
= smb2_dump_share_caps
,
5523 .is_oplock_break
= smb2_is_valid_oplock_break
,
5524 .handle_cancelled_mid
= smb2_handle_cancelled_mid
,
5525 .downgrade_oplock
= smb3_downgrade_oplock
,
5526 .need_neg
= smb2_need_neg
,
5527 .negotiate
= smb2_negotiate
,
5528 .negotiate_wsize
= smb3_negotiate_wsize
,
5529 .negotiate_rsize
= smb3_negotiate_rsize
,
5530 .sess_setup
= SMB2_sess_setup
,
5531 .logoff
= SMB2_logoff
,
5532 .tree_connect
= SMB2_tcon
,
5533 .tree_disconnect
= SMB2_tdis
,
5534 .qfs_tcon
= smb3_qfs_tcon
,
5535 .is_path_accessible
= smb2_is_path_accessible
,
5536 .can_echo
= smb2_can_echo
,
5538 .query_path_info
= smb2_query_path_info
,
5539 .query_reparse_point
= smb2_query_reparse_point
,
5540 .get_srv_inum
= smb2_get_srv_inum
,
5541 .query_file_info
= smb2_query_file_info
,
5542 .set_path_size
= smb2_set_path_size
,
5543 .set_file_size
= smb2_set_file_size
,
5544 .set_file_info
= smb2_set_file_info
,
5545 .set_compression
= smb2_set_compression
,
5546 .mkdir
= smb2_mkdir
,
5547 .mkdir_setinfo
= smb2_mkdir_setinfo
,
5548 .posix_mkdir
= smb311_posix_mkdir
,
5549 .rmdir
= smb2_rmdir
,
5550 .unlink
= smb2_unlink
,
5551 .rename
= smb2_rename_path
,
5552 .create_hardlink
= smb2_create_hardlink
,
5553 .parse_reparse_point
= smb2_parse_reparse_point
,
5554 .query_mf_symlink
= smb3_query_mf_symlink
,
5555 .create_mf_symlink
= smb3_create_mf_symlink
,
5556 .create_reparse_symlink
= smb2_create_reparse_symlink
,
5557 .open
= smb2_open_file
,
5558 .set_fid
= smb2_set_fid
,
5559 .close
= smb2_close_file
,
5560 .close_getattr
= smb2_close_getattr
,
5561 .flush
= smb2_flush_file
,
5562 .async_readv
= smb2_async_readv
,
5563 .async_writev
= smb2_async_writev
,
5564 .sync_read
= smb2_sync_read
,
5565 .sync_write
= smb2_sync_write
,
5566 .query_dir_first
= smb2_query_dir_first
,
5567 .query_dir_next
= smb2_query_dir_next
,
5568 .close_dir
= smb2_close_dir
,
5569 .calc_smb_size
= smb2_calc_size
,
5570 .is_status_pending
= smb2_is_status_pending
,
5571 .is_session_expired
= smb2_is_session_expired
,
5572 .oplock_response
= smb2_oplock_response
,
5573 .queryfs
= smb311_queryfs
,
5574 .mand_lock
= smb2_mand_lock
,
5575 .mand_unlock_range
= smb2_unlock_range
,
5576 .push_mand_locks
= smb2_push_mandatory_locks
,
5577 .get_lease_key
= smb2_get_lease_key
,
5578 .set_lease_key
= smb2_set_lease_key
,
5579 .new_lease_key
= smb2_new_lease_key
,
5580 .generate_signingkey
= generate_smb311signingkey
,
5581 .calc_signature
= smb3_calc_signature
,
5582 .set_integrity
= smb3_set_integrity
,
5583 .is_read_op
= smb21_is_read_op
,
5584 .set_oplock_level
= smb3_set_oplock_level
,
5585 .create_lease_buf
= smb3_create_lease_buf
,
5586 .parse_lease_buf
= smb3_parse_lease_buf
,
5587 .copychunk_range
= smb2_copychunk_range
,
5588 .duplicate_extents
= smb2_duplicate_extents
,
5589 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5590 .wp_retry_size
= smb2_wp_retry_size
,
5591 .dir_needs_close
= smb2_dir_needs_close
,
5592 .fallocate
= smb3_fallocate
,
5593 .enum_snapshots
= smb3_enum_snapshots
,
5594 .notify
= smb3_notify
,
5595 .init_transform_rq
= smb3_init_transform_rq
,
5596 .is_transform_hdr
= smb3_is_transform_hdr
,
5597 .receive_transform
= smb3_receive_transform
,
5598 .get_dfs_refer
= smb2_get_dfs_refer
,
5599 .select_sectype
= smb2_select_sectype
,
5600 #ifdef CONFIG_CIFS_XATTR
5601 .query_all_EAs
= smb2_query_eas
,
5602 .set_EA
= smb2_set_ea
,
5603 #endif /* CIFS_XATTR */
5604 .get_acl
= get_smb2_acl
,
5605 .get_acl_by_fid
= get_smb2_acl_by_fid
,
5606 .set_acl
= set_smb2_acl
,
5607 .next_header
= smb2_next_header
,
5608 .ioctl_query_info
= smb2_ioctl_query_info
,
5609 .make_node
= smb2_make_node
,
5610 .fiemap
= smb3_fiemap
,
5611 .llseek
= smb3_llseek
,
5612 .is_status_io_timeout
= smb2_is_status_io_timeout
,
5613 .is_network_name_deleted
= smb2_is_network_name_deleted
,
5616 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5617 struct smb_version_values smb20_values
= {
5618 .version_string
= SMB20_VERSION_STRING
,
5619 .protocol_id
= SMB20_PROT_ID
,
5620 .req_capabilities
= 0, /* MBZ */
5621 .large_lock_type
= 0,
5622 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5623 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5624 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5625 .header_size
= sizeof(struct smb2_hdr
),
5626 .header_preamble_size
= 0,
5627 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5628 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5629 .lock_cmd
= SMB2_LOCK
,
5631 .cap_nt_find
= SMB2_NT_FIND
,
5632 .cap_large_files
= SMB2_LARGE_FILES
,
5633 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5634 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5635 .create_lease_size
= sizeof(struct create_lease
),
5637 #endif /* ALLOW_INSECURE_LEGACY */
5639 struct smb_version_values smb21_values
= {
5640 .version_string
= SMB21_VERSION_STRING
,
5641 .protocol_id
= SMB21_PROT_ID
,
5642 .req_capabilities
= 0, /* MBZ on negotiate req until SMB3 dialect */
5643 .large_lock_type
= 0,
5644 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5645 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5646 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5647 .header_size
= sizeof(struct smb2_hdr
),
5648 .header_preamble_size
= 0,
5649 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5650 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5651 .lock_cmd
= SMB2_LOCK
,
5653 .cap_nt_find
= SMB2_NT_FIND
,
5654 .cap_large_files
= SMB2_LARGE_FILES
,
5655 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5656 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5657 .create_lease_size
= sizeof(struct create_lease
),
5660 struct smb_version_values smb3any_values
= {
5661 .version_string
= SMB3ANY_VERSION_STRING
,
5662 .protocol_id
= SMB302_PROT_ID
, /* doesn't matter, send protocol array */
5663 .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
,
5664 .large_lock_type
= 0,
5665 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5666 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5667 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5668 .header_size
= sizeof(struct smb2_hdr
),
5669 .header_preamble_size
= 0,
5670 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5671 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5672 .lock_cmd
= SMB2_LOCK
,
5674 .cap_nt_find
= SMB2_NT_FIND
,
5675 .cap_large_files
= SMB2_LARGE_FILES
,
5676 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5677 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5678 .create_lease_size
= sizeof(struct create_lease_v2
),
5681 struct smb_version_values smbdefault_values
= {
5682 .version_string
= SMBDEFAULT_VERSION_STRING
,
5683 .protocol_id
= SMB302_PROT_ID
, /* doesn't matter, send protocol array */
5684 .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
,
5685 .large_lock_type
= 0,
5686 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5687 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5688 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5689 .header_size
= sizeof(struct smb2_hdr
),
5690 .header_preamble_size
= 0,
5691 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5692 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5693 .lock_cmd
= SMB2_LOCK
,
5695 .cap_nt_find
= SMB2_NT_FIND
,
5696 .cap_large_files
= SMB2_LARGE_FILES
,
5697 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5698 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5699 .create_lease_size
= sizeof(struct create_lease_v2
),
5702 struct smb_version_values smb30_values
= {
5703 .version_string
= SMB30_VERSION_STRING
,
5704 .protocol_id
= SMB30_PROT_ID
,
5705 .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
,
5706 .large_lock_type
= 0,
5707 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5708 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5709 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5710 .header_size
= sizeof(struct smb2_hdr
),
5711 .header_preamble_size
= 0,
5712 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5713 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5714 .lock_cmd
= SMB2_LOCK
,
5716 .cap_nt_find
= SMB2_NT_FIND
,
5717 .cap_large_files
= SMB2_LARGE_FILES
,
5718 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5719 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5720 .create_lease_size
= sizeof(struct create_lease_v2
),
5723 struct smb_version_values smb302_values
= {
5724 .version_string
= SMB302_VERSION_STRING
,
5725 .protocol_id
= SMB302_PROT_ID
,
5726 .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
,
5727 .large_lock_type
= 0,
5728 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5729 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5730 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5731 .header_size
= sizeof(struct smb2_hdr
),
5732 .header_preamble_size
= 0,
5733 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5734 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5735 .lock_cmd
= SMB2_LOCK
,
5737 .cap_nt_find
= SMB2_NT_FIND
,
5738 .cap_large_files
= SMB2_LARGE_FILES
,
5739 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5740 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5741 .create_lease_size
= sizeof(struct create_lease_v2
),
5744 struct smb_version_values smb311_values
= {
5745 .version_string
= SMB311_VERSION_STRING
,
5746 .protocol_id
= SMB311_PROT_ID
,
5747 .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
,
5748 .large_lock_type
= 0,
5749 .exclusive_lock_type
= SMB2_LOCKFLAG_EXCLUSIVE
,
5750 .shared_lock_type
= SMB2_LOCKFLAG_SHARED
,
5751 .unlock_lock_type
= SMB2_LOCKFLAG_UNLOCK
,
5752 .header_size
= sizeof(struct smb2_hdr
),
5753 .header_preamble_size
= 0,
5754 .max_header_size
= MAX_SMB2_HDR_SIZE
,
5755 .read_rsp_size
= sizeof(struct smb2_read_rsp
),
5756 .lock_cmd
= SMB2_LOCK
,
5758 .cap_nt_find
= SMB2_NT_FIND
,
5759 .cap_large_files
= SMB2_LARGE_FILES
,
5760 .signing_enabled
= SMB2_NEGOTIATE_SIGNING_ENABLED
| SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5761 .signing_required
= SMB2_NEGOTIATE_SIGNING_REQUIRED
,
5762 .create_lease_size
= sizeof(struct create_lease_v2
),