1 // SPDX-License-Identifier: LGPL-2.1
4 * Copyright (C) International Business Machines Corp., 2002,2011
5 * Author(s): Steve French (sfrench@us.ibm.com)
10 #include <linux/string.h>
11 #include <linux/sched/mm.h>
12 #include <linux/sched/signal.h>
13 #include <linux/list.h>
14 #include <linux/wait.h>
15 #include <linux/slab.h>
16 #include <linux/pagemap.h>
17 #include <linux/ctype.h>
18 #include <linux/utsname.h>
19 #include <linux/mempool.h>
20 #include <linux/delay.h>
21 #include <linux/completion.h>
22 #include <linux/kthread.h>
23 #include <linux/pagevec.h>
24 #include <linux/freezer.h>
25 #include <linux/namei.h>
26 #include <linux/uuid.h>
27 #include <linux/uaccess.h>
28 #include <asm/processor.h>
29 #include <linux/inet.h>
30 #include <linux/module.h>
31 #include <keys/user-type.h>
33 #include <linux/parser.h>
34 #include <linux/bvec.h>
37 #include "cifsproto.h"
38 #include "cifs_unicode.h"
39 #include "cifs_debug.h"
40 #include "cifs_fs_sb.h"
43 #include "rfc1002pdu.h"
45 #include "smb2proto.h"
46 #include "smbdirect.h"
47 #include "dns_resolve.h"
48 #ifdef CONFIG_CIFS_DFS_UPCALL
49 #include "dfs_cache.h"
51 #include "fs_context.h"
54 extern mempool_t *cifs_req_poolp;
55 extern bool disable_legacy_dialects;
57 /* FIXME: should these be tunable? */
58 #define TLINK_ERROR_EXPIRE (1 * HZ)
59 #define TLINK_IDLE_EXPIRE (600 * HZ)
61 /* Drop the connection to not overload the server */
62 #define NUM_STATUS_IO_TIMEOUT 5
65 struct cifs_sb_info *cifs_sb;
66 struct smb3_fs_context *fs_ctx;
68 struct TCP_Server_Info *server;
70 struct cifs_tcon *tcon;
71 #ifdef CONFIG_CIFS_DFS_UPCALL
72 struct cifs_ses *root_ses;
74 char *origin_fullpath, *leaf_fullpath;
78 static int ip_connect(struct TCP_Server_Info *server);
79 static int generic_ip_connect(struct TCP_Server_Info *server);
80 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
81 static void cifs_prune_tlinks(struct work_struct *work);
84 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
85 * get their ip addresses changed at some point.
87 * This should be called with server->srv_mutex held.
89 static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
93 char *unc, *ipaddr = NULL;
95 unsigned long ttl = SMB_DNS_RESOLVE_INTERVAL_DEFAULT;
97 if (!server->hostname)
100 len = strlen(server->hostname) + 3;
102 unc = kmalloc(len, GFP_KERNEL);
104 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
107 scnprintf(unc, len, "\\\\%s", server->hostname);
109 rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry);
113 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
114 __func__, server->hostname, rc);
115 goto requeue_resolve;
118 spin_lock(&cifs_tcp_ses_lock);
119 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
121 spin_unlock(&cifs_tcp_ses_lock);
124 /* rc == 1 means success here */
126 now = ktime_get_real_seconds();
127 if (expiry && expiry > now)
129 * To make sure we don't use the cached entry, retry 1s
132 ttl = max_t(unsigned long, expiry - now, SMB_DNS_RESOLVE_INTERVAL_MIN) + 1;
137 cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n",
139 mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ));
145 static void cifs_resolve_server(struct work_struct *work)
148 struct TCP_Server_Info *server = container_of(work,
149 struct TCP_Server_Info, resolve.work);
151 cifs_server_lock(server);
154 * Resolve the hostname again to make sure that IP address is up-to-date.
156 rc = reconn_set_ipaddr_from_hostname(server);
158 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
162 cifs_server_unlock(server);
166 * Update the tcpStatus for the server.
167 * This is used to signal the cifsd thread to call cifs_reconnect
168 * ONLY cifsd thread should call cifs_reconnect. For any other
169 * thread, use this function
171 * @server: the tcp ses for which reconnect is needed
172 * @all_channels: if this needs to be done for all channels
175 cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
178 struct TCP_Server_Info *pserver;
179 struct cifs_ses *ses;
182 /* If server is a channel, select the primary channel */
183 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
185 spin_lock(&cifs_tcp_ses_lock);
187 pserver->tcpStatus = CifsNeedReconnect;
188 spin_unlock(&cifs_tcp_ses_lock);
192 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
193 spin_lock(&ses->chan_lock);
194 for (i = 0; i < ses->chan_count; i++)
195 ses->chans[i].server->tcpStatus = CifsNeedReconnect;
196 spin_unlock(&ses->chan_lock);
198 spin_unlock(&cifs_tcp_ses_lock);
202 * Mark all sessions and tcons for reconnect.
203 * IMPORTANT: make sure that this gets called only from
204 * cifsd thread. For any other thread, use
205 * cifs_signal_cifsd_for_reconnect
207 * @server: the tcp ses for which reconnect is needed
208 * @server needs to be previously set to CifsNeedReconnect.
209 * @mark_smb_session: whether even sessions need to be marked
212 cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
213 bool mark_smb_session)
215 struct TCP_Server_Info *pserver;
216 struct cifs_ses *ses;
217 struct cifs_tcon *tcon;
220 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
221 * are not used until reconnected.
223 cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
225 /* If server is a channel, select the primary channel */
226 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
229 spin_lock(&cifs_tcp_ses_lock);
230 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
231 spin_lock(&ses->chan_lock);
232 if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server))
235 if (mark_smb_session)
236 CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
238 cifs_chan_set_need_reconnect(ses, server);
240 /* If all channels need reconnect, then tcon needs reconnect */
241 if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses))
244 ses->ses_status = SES_NEED_RECON;
246 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
247 tcon->need_reconnect = true;
248 tcon->status = TID_NEED_RECON;
251 ses->tcon_ipc->need_reconnect = true;
254 spin_unlock(&ses->chan_lock);
256 spin_unlock(&cifs_tcp_ses_lock);
260 cifs_abort_connection(struct TCP_Server_Info *server)
262 struct mid_q_entry *mid, *nmid;
263 struct list_head retry_list;
266 server->max_read = 0;
268 /* do not want to be sending data on a socket we are freeing */
269 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
270 cifs_server_lock(server);
271 if (server->ssocket) {
272 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
273 server->ssocket->flags);
274 kernel_sock_shutdown(server->ssocket, SHUT_WR);
275 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
276 server->ssocket->flags);
277 sock_release(server->ssocket);
278 server->ssocket = NULL;
280 server->sequence_number = 0;
281 server->session_estab = false;
282 kfree(server->session_key.response);
283 server->session_key.response = NULL;
284 server->session_key.len = 0;
285 server->lstrp = jiffies;
287 /* mark submitted MIDs for retry and issue callback */
288 INIT_LIST_HEAD(&retry_list);
289 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
290 spin_lock(&GlobalMid_Lock);
291 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
292 kref_get(&mid->refcount);
293 if (mid->mid_state == MID_REQUEST_SUBMITTED)
294 mid->mid_state = MID_RETRY_NEEDED;
295 list_move(&mid->qhead, &retry_list);
296 mid->mid_flags |= MID_DELETED;
298 spin_unlock(&GlobalMid_Lock);
299 cifs_server_unlock(server);
301 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
302 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
303 list_del_init(&mid->qhead);
305 cifs_mid_q_entry_release(mid);
308 if (cifs_rdma_enabled(server)) {
309 cifs_server_lock(server);
310 smbd_destroy(server);
311 cifs_server_unlock(server);
315 static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
317 spin_lock(&cifs_tcp_ses_lock);
318 server->nr_targets = num_targets;
319 if (server->tcpStatus == CifsExiting) {
320 /* the demux thread will exit normally next time through the loop */
321 spin_unlock(&cifs_tcp_ses_lock);
322 wake_up(&server->response_q);
326 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
327 trace_smb3_reconnect(server->CurrentMid, server->conn_id,
329 server->tcpStatus = CifsNeedReconnect;
331 spin_unlock(&cifs_tcp_ses_lock);
336 * cifs tcp session reconnection
338 * mark tcp session as reconnecting so temporarily locked
339 * mark all smb sessions as reconnecting for tcp session
340 * reconnect tcp session
341 * wake up waiters on reconnection? - (not needed currently)
343 * if mark_smb_session is passed as true, unconditionally mark
344 * the smb session (and tcon) for reconnect as well. This value
345 * doesn't really matter for non-multichannel scenario.
348 static int __cifs_reconnect(struct TCP_Server_Info *server,
349 bool mark_smb_session)
353 if (!cifs_tcp_ses_needs_reconnect(server, 1))
356 cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
358 cifs_abort_connection(server);
362 cifs_server_lock(server);
364 if (!cifs_swn_set_server_dstaddr(server)) {
365 /* resolve the hostname again to make sure that IP address is up-to-date */
366 rc = reconn_set_ipaddr_from_hostname(server);
367 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
370 if (cifs_rdma_enabled(server))
371 rc = smbd_reconnect(server);
373 rc = generic_ip_connect(server);
375 cifs_server_unlock(server);
376 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
379 atomic_inc(&tcpSesReconnectCount);
380 set_credits(server, 1);
381 spin_lock(&cifs_tcp_ses_lock);
382 if (server->tcpStatus != CifsExiting)
383 server->tcpStatus = CifsNeedNegotiate;
384 spin_unlock(&cifs_tcp_ses_lock);
385 cifs_swn_reset_server_dstaddr(server);
386 cifs_server_unlock(server);
387 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
389 } while (server->tcpStatus == CifsNeedReconnect);
391 spin_lock(&cifs_tcp_ses_lock);
392 if (server->tcpStatus == CifsNeedNegotiate)
393 mod_delayed_work(cifsiod_wq, &server->echo, 0);
394 spin_unlock(&cifs_tcp_ses_lock);
396 wake_up(&server->response_q);
400 #ifdef CONFIG_CIFS_DFS_UPCALL
401 static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
406 if (!cifs_swn_set_server_dstaddr(server)) {
407 if (server->hostname != target) {
408 hostname = extract_hostname(target);
409 if (!IS_ERR(hostname)) {
410 kfree(server->hostname);
411 server->hostname = hostname;
413 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
414 __func__, PTR_ERR(hostname));
415 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
419 /* resolve the hostname again to make sure that IP address is up-to-date. */
420 rc = reconn_set_ipaddr_from_hostname(server);
421 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
423 /* Reconnect the socket */
424 if (cifs_rdma_enabled(server))
425 rc = smbd_reconnect(server);
427 rc = generic_ip_connect(server);
432 static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
433 struct dfs_cache_tgt_iterator **target_hint)
436 struct dfs_cache_tgt_iterator *tit;
440 /* If dfs target list is empty, then reconnect to last server */
441 tit = dfs_cache_get_tgt_iterator(tl);
443 return __reconnect_target_unlocked(server, server->hostname);
445 /* Otherwise, try every dfs target in @tl */
446 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
447 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
456 static int reconnect_dfs_server(struct TCP_Server_Info *server)
459 const char *refpath = server->current_fullpath + 1;
460 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
461 struct dfs_cache_tgt_iterator *target_hint = NULL;
465 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
467 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
468 * targets (server->nr_targets). It's also possible that the cached referral was cleared
469 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
470 * refreshing the referral, so, in this case, default it to 1.
472 if (!dfs_cache_noreq_find(refpath, NULL, &tl))
473 num_targets = dfs_cache_get_nr_tgts(&tl);
477 if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
481 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
482 * different server or share during failover. It could be improved by adding some logic to
483 * only do that in case it connects to a different server or share, though.
485 cifs_mark_tcp_ses_conns_for_reconnect(server, true);
487 cifs_abort_connection(server);
491 cifs_server_lock(server);
493 rc = reconnect_target_unlocked(server, &tl, &target_hint);
495 /* Failed to reconnect socket */
496 cifs_server_unlock(server);
497 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
502 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
503 * process waiting for reconnect will know it needs to re-establish session and tcon
504 * through the reconnected target server.
506 atomic_inc(&tcpSesReconnectCount);
507 set_credits(server, 1);
508 spin_lock(&cifs_tcp_ses_lock);
509 if (server->tcpStatus != CifsExiting)
510 server->tcpStatus = CifsNeedNegotiate;
511 spin_unlock(&cifs_tcp_ses_lock);
512 cifs_swn_reset_server_dstaddr(server);
513 cifs_server_unlock(server);
514 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
515 } while (server->tcpStatus == CifsNeedReconnect);
518 dfs_cache_noreq_update_tgthint(refpath, target_hint);
520 dfs_cache_free_tgts(&tl);
522 /* Need to set up echo worker again once connection has been established */
523 spin_lock(&cifs_tcp_ses_lock);
524 if (server->tcpStatus == CifsNeedNegotiate)
525 mod_delayed_work(cifsiod_wq, &server->echo, 0);
527 spin_unlock(&cifs_tcp_ses_lock);
529 wake_up(&server->response_q);
533 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
535 /* If tcp session is not an dfs connection, then reconnect to last target server */
536 spin_lock(&cifs_tcp_ses_lock);
537 if (!server->is_dfs_conn) {
538 spin_unlock(&cifs_tcp_ses_lock);
539 return __cifs_reconnect(server, mark_smb_session);
541 spin_unlock(&cifs_tcp_ses_lock);
543 mutex_lock(&server->refpath_lock);
544 if (!server->origin_fullpath || !server->leaf_fullpath) {
545 mutex_unlock(&server->refpath_lock);
546 return __cifs_reconnect(server, mark_smb_session);
548 mutex_unlock(&server->refpath_lock);
550 return reconnect_dfs_server(server);
553 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
555 return __cifs_reconnect(server, mark_smb_session);
560 cifs_echo_request(struct work_struct *work)
563 struct TCP_Server_Info *server = container_of(work,
564 struct TCP_Server_Info, echo.work);
567 * We cannot send an echo if it is disabled.
568 * Also, no need to ping if we got a response recently.
571 if (server->tcpStatus == CifsNeedReconnect ||
572 server->tcpStatus == CifsExiting ||
573 server->tcpStatus == CifsNew ||
574 (server->ops->can_echo && !server->ops->can_echo(server)) ||
575 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
578 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
580 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
583 /* Check witness registrations */
587 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
591 allocate_buffers(struct TCP_Server_Info *server)
593 if (!server->bigbuf) {
594 server->bigbuf = (char *)cifs_buf_get();
595 if (!server->bigbuf) {
596 cifs_server_dbg(VFS, "No memory for large SMB response\n");
598 /* retry will check if exiting */
601 } else if (server->large_buf) {
602 /* we are reusing a dirty large buf, clear its start */
603 memset(server->bigbuf, 0, HEADER_SIZE(server));
606 if (!server->smallbuf) {
607 server->smallbuf = (char *)cifs_small_buf_get();
608 if (!server->smallbuf) {
609 cifs_server_dbg(VFS, "No memory for SMB response\n");
611 /* retry will check if exiting */
614 /* beginning of smb buffer is cleared in our buf_get */
616 /* if existing small buf clear beginning */
617 memset(server->smallbuf, 0, HEADER_SIZE(server));
624 server_unresponsive(struct TCP_Server_Info *server)
627 * We need to wait 3 echo intervals to make sure we handle such
629 * 1s client sends a normal SMB request
630 * 2s client gets a response
631 * 30s echo workqueue job pops, and decides we got a response recently
632 * and don't need to send another
634 * 65s kernel_recvmsg times out, and we see that we haven't gotten
635 * a response in >60s.
637 spin_lock(&cifs_tcp_ses_lock);
638 if ((server->tcpStatus == CifsGood ||
639 server->tcpStatus == CifsNeedNegotiate) &&
640 (!server->ops->can_echo || server->ops->can_echo(server)) &&
641 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
642 spin_unlock(&cifs_tcp_ses_lock);
643 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
644 (3 * server->echo_interval) / HZ);
645 cifs_reconnect(server, false);
648 spin_unlock(&cifs_tcp_ses_lock);
654 zero_credits(struct TCP_Server_Info *server)
658 spin_lock(&server->req_lock);
659 val = server->credits + server->echo_credits + server->oplock_credits;
660 if (server->in_flight == 0 && val == 0) {
661 spin_unlock(&server->req_lock);
664 spin_unlock(&server->req_lock);
669 cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
674 smb_msg->msg_control = NULL;
675 smb_msg->msg_controllen = 0;
677 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
680 /* reconnect if no credits and no requests in flight */
681 if (zero_credits(server)) {
682 cifs_reconnect(server, false);
683 return -ECONNABORTED;
686 if (server_unresponsive(server))
687 return -ECONNABORTED;
688 if (cifs_rdma_enabled(server) && server->smbd_conn)
689 length = smbd_recv(server->smbd_conn, smb_msg);
691 length = sock_recvmsg(server->ssocket, smb_msg, 0);
693 spin_lock(&cifs_tcp_ses_lock);
694 if (server->tcpStatus == CifsExiting) {
695 spin_unlock(&cifs_tcp_ses_lock);
699 if (server->tcpStatus == CifsNeedReconnect) {
700 spin_unlock(&cifs_tcp_ses_lock);
701 cifs_reconnect(server, false);
702 return -ECONNABORTED;
704 spin_unlock(&cifs_tcp_ses_lock);
706 if (length == -ERESTARTSYS ||
710 * Minimum sleep to prevent looping, allowing socket
711 * to clear and app threads to set tcpStatus
712 * CifsNeedReconnect if server hung.
714 usleep_range(1000, 2000);
720 cifs_dbg(FYI, "Received no data or error: %d\n", length);
721 cifs_reconnect(server, false);
722 return -ECONNABORTED;
729 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
730 unsigned int to_read)
732 struct msghdr smb_msg;
733 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
734 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
736 return cifs_readv_from_socket(server, &smb_msg);
740 cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
742 struct msghdr smb_msg;
745 * iov_iter_discard already sets smb_msg.type and count and iov_offset
746 * and cifs_readv_from_socket sets msg_control and msg_controllen
747 * so little to initialize in struct msghdr
749 smb_msg.msg_name = NULL;
750 smb_msg.msg_namelen = 0;
751 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
753 return cifs_readv_from_socket(server, &smb_msg);
757 cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
758 unsigned int page_offset, unsigned int to_read)
760 struct msghdr smb_msg;
761 struct bio_vec bv = {
762 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
763 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
764 return cifs_readv_from_socket(server, &smb_msg);
768 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
771 * The first byte big endian of the length field,
772 * is actually not part of the length but the type
773 * with the most common, zero, as regular data.
776 case RFC1002_SESSION_MESSAGE:
777 /* Regular SMB response */
779 case RFC1002_SESSION_KEEP_ALIVE:
780 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
782 case RFC1002_POSITIVE_SESSION_RESPONSE:
783 cifs_dbg(FYI, "RFC 1002 positive session response\n");
785 case RFC1002_NEGATIVE_SESSION_RESPONSE:
787 * We get this from Windows 98 instead of an error on
788 * SMB negprot response.
790 cifs_dbg(FYI, "RFC 1002 negative session response\n");
791 /* give server a second to clean up */
794 * Always try 445 first on reconnect since we get NACK
795 * on some if we ever connected to port 139 (the NACK
796 * is since we do not begin with RFC1001 session
799 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
800 cifs_reconnect(server, true);
803 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
804 cifs_reconnect(server, true);
811 dequeue_mid(struct mid_q_entry *mid, bool malformed)
813 #ifdef CONFIG_CIFS_STATS2
814 mid->when_received = jiffies;
816 spin_lock(&GlobalMid_Lock);
818 mid->mid_state = MID_RESPONSE_RECEIVED;
820 mid->mid_state = MID_RESPONSE_MALFORMED;
822 * Trying to handle/dequeue a mid after the send_recv()
823 * function has finished processing it is a bug.
825 if (mid->mid_flags & MID_DELETED) {
826 spin_unlock(&GlobalMid_Lock);
827 pr_warn_once("trying to dequeue a deleted mid\n");
829 list_del_init(&mid->qhead);
830 mid->mid_flags |= MID_DELETED;
831 spin_unlock(&GlobalMid_Lock);
836 smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
838 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
841 * SMB1 does not use credits.
843 if (server->vals->header_preamble_size)
846 return le16_to_cpu(shdr->CreditRequest);
850 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
851 char *buf, int malformed)
853 if (server->ops->check_trans2 &&
854 server->ops->check_trans2(mid, server, buf, malformed))
856 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
858 mid->large_buf = server->large_buf;
859 /* Was previous buf put in mpx struct for multi-rsp? */
860 if (!mid->multiRsp) {
861 /* smb buffer will be freed by user thread */
862 if (server->large_buf)
863 server->bigbuf = NULL;
865 server->smallbuf = NULL;
867 dequeue_mid(mid, malformed);
870 static void clean_demultiplex_info(struct TCP_Server_Info *server)
874 /* take it off the list, if it's not already */
875 spin_lock(&cifs_tcp_ses_lock);
876 list_del_init(&server->tcp_ses_list);
877 spin_unlock(&cifs_tcp_ses_lock);
879 cancel_delayed_work_sync(&server->echo);
880 cancel_delayed_work_sync(&server->resolve);
882 spin_lock(&cifs_tcp_ses_lock);
883 server->tcpStatus = CifsExiting;
884 spin_unlock(&cifs_tcp_ses_lock);
885 wake_up_all(&server->response_q);
887 /* check if we have blocked requests that need to free */
888 spin_lock(&server->req_lock);
889 if (server->credits <= 0)
891 spin_unlock(&server->req_lock);
893 * Although there should not be any requests blocked on this queue it
894 * can not hurt to be paranoid and try to wake up requests that may
895 * haven been blocked when more than 50 at time were on the wire to the
896 * same server - they now will see the session is in exit state and get
897 * out of SendReceive.
899 wake_up_all(&server->request_q);
900 /* give those requests time to exit */
902 if (cifs_rdma_enabled(server))
903 smbd_destroy(server);
904 if (server->ssocket) {
905 sock_release(server->ssocket);
906 server->ssocket = NULL;
909 if (!list_empty(&server->pending_mid_q)) {
910 struct list_head dispose_list;
911 struct mid_q_entry *mid_entry;
912 struct list_head *tmp, *tmp2;
914 INIT_LIST_HEAD(&dispose_list);
915 spin_lock(&GlobalMid_Lock);
916 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
917 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
918 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
919 kref_get(&mid_entry->refcount);
920 mid_entry->mid_state = MID_SHUTDOWN;
921 list_move(&mid_entry->qhead, &dispose_list);
922 mid_entry->mid_flags |= MID_DELETED;
924 spin_unlock(&GlobalMid_Lock);
926 /* now walk dispose list and issue callbacks */
927 list_for_each_safe(tmp, tmp2, &dispose_list) {
928 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
929 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
930 list_del_init(&mid_entry->qhead);
931 mid_entry->callback(mid_entry);
932 cifs_mid_q_entry_release(mid_entry);
934 /* 1/8th of sec is more than enough time for them to exit */
938 if (!list_empty(&server->pending_mid_q)) {
940 * mpx threads have not exited yet give them at least the smb
941 * send timeout time for long ops.
943 * Due to delays on oplock break requests, we need to wait at
944 * least 45 seconds before giving up on a request getting a
945 * response and going ahead and killing cifsd.
947 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
950 * If threads still have not exited they are probably never
951 * coming home not much else we can do but free the memory.
955 #ifdef CONFIG_CIFS_DFS_UPCALL
956 kfree(server->origin_fullpath);
957 kfree(server->leaf_fullpath);
961 length = atomic_dec_return(&tcpSesAllocCount);
963 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
967 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
970 char *buf = server->smallbuf;
971 unsigned int pdu_length = server->pdu_size;
973 /* make sure this will fit in a large buffer */
974 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
975 server->vals->header_preamble_size) {
976 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
977 cifs_reconnect(server, true);
978 return -ECONNABORTED;
981 /* switch to large buffer if too big for a small one */
982 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
983 server->large_buf = true;
984 memcpy(server->bigbuf, buf, server->total_read);
985 buf = server->bigbuf;
988 /* now read the rest */
989 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
990 pdu_length - HEADER_SIZE(server) + 1
991 + server->vals->header_preamble_size);
995 server->total_read += length;
997 dump_smb(buf, server->total_read);
999 return cifs_handle_standard(server, mid);
1003 cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1005 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1009 * We know that we received enough to get to the MID as we
1010 * checked the pdu_length earlier. Now check to see
1011 * if the rest of the header is OK. We borrow the length
1012 * var for the rest of the loop to avoid a new stack var.
1014 * 48 bytes is enough to display the header and a little bit
1015 * into the payload for debugging purposes.
1017 length = server->ops->check_message(buf, server->total_read, server);
1019 cifs_dump_mem("Bad SMB: ", buf,
1020 min_t(unsigned int, server->total_read, 48));
1022 if (server->ops->is_session_expired &&
1023 server->ops->is_session_expired(buf)) {
1024 cifs_reconnect(server, true);
1028 if (server->ops->is_status_pending &&
1029 server->ops->is_status_pending(buf, server))
1035 handle_mid(mid, server, buf, length);
1040 smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1042 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
1043 int scredits, in_flight;
1046 * SMB1 does not use credits.
1048 if (server->vals->header_preamble_size)
1051 if (shdr->CreditRequest) {
1052 spin_lock(&server->req_lock);
1053 server->credits += le16_to_cpu(shdr->CreditRequest);
1054 scredits = server->credits;
1055 in_flight = server->in_flight;
1056 spin_unlock(&server->req_lock);
1057 wake_up(&server->request_q);
1059 trace_smb3_hdr_credits(server->CurrentMid,
1060 server->conn_id, server->hostname, scredits,
1061 le16_to_cpu(shdr->CreditRequest), in_flight);
1062 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
1063 __func__, le16_to_cpu(shdr->CreditRequest),
1070 cifs_demultiplex_thread(void *p)
1072 int i, num_mids, length;
1073 struct TCP_Server_Info *server = p;
1074 unsigned int pdu_length;
1075 unsigned int next_offset;
1077 struct task_struct *task_to_wake = NULL;
1078 struct mid_q_entry *mids[MAX_COMPOUND];
1079 char *bufs[MAX_COMPOUND];
1080 unsigned int noreclaim_flag, num_io_timeout = 0;
1082 noreclaim_flag = memalloc_noreclaim_save();
1083 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
1085 length = atomic_inc_return(&tcpSesAllocCount);
1087 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1090 allow_kernel_signal(SIGKILL);
1091 while (server->tcpStatus != CifsExiting) {
1092 if (try_to_freeze())
1095 if (!allocate_buffers(server))
1098 server->large_buf = false;
1099 buf = server->smallbuf;
1100 pdu_length = 4; /* enough to get RFC1001 header */
1102 length = cifs_read_from_socket(server, buf, pdu_length);
1106 if (server->vals->header_preamble_size == 0)
1107 server->total_read = 0;
1109 server->total_read = length;
1112 * The right amount was read from socket - 4 bytes,
1113 * so we can now interpret the length field.
1115 pdu_length = get_rfc1002_length(buf);
1117 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
1118 if (!is_smb_response(server, buf[0]))
1121 server->pdu_size = pdu_length;
1123 /* make sure we have enough to get to the MID */
1124 if (server->pdu_size < HEADER_SIZE(server) - 1 -
1125 server->vals->header_preamble_size) {
1126 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
1128 cifs_reconnect(server, true);
1132 /* read down to the MID */
1133 length = cifs_read_from_socket(server,
1134 buf + server->vals->header_preamble_size,
1135 HEADER_SIZE(server) - 1
1136 - server->vals->header_preamble_size);
1139 server->total_read += length;
1141 if (server->ops->next_header) {
1142 next_offset = server->ops->next_header(buf);
1144 server->pdu_size = next_offset;
1147 memset(mids, 0, sizeof(mids));
1148 memset(bufs, 0, sizeof(bufs));
1151 if (server->ops->is_transform_hdr &&
1152 server->ops->receive_transform &&
1153 server->ops->is_transform_hdr(buf)) {
1154 length = server->ops->receive_transform(server,
1159 mids[0] = server->ops->find_mid(server, buf);
1163 if (!mids[0] || !mids[0]->receive)
1164 length = standard_receive3(server, mids[0]);
1166 length = mids[0]->receive(server, mids[0]);
1170 for (i = 0; i < num_mids; i++)
1172 cifs_mid_q_entry_release(mids[i]);
1176 if (server->ops->is_status_io_timeout &&
1177 server->ops->is_status_io_timeout(buf)) {
1179 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1180 cifs_reconnect(server, false);
1186 server->lstrp = jiffies;
1188 for (i = 0; i < num_mids; i++) {
1189 if (mids[i] != NULL) {
1190 mids[i]->resp_buf_size = server->pdu_size;
1192 if (bufs[i] && server->ops->is_network_name_deleted)
1193 server->ops->is_network_name_deleted(bufs[i],
1196 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1197 mids[i]->callback(mids[i]);
1199 cifs_mid_q_entry_release(mids[i]);
1200 } else if (server->ops->is_oplock_break &&
1201 server->ops->is_oplock_break(bufs[i],
1203 smb2_add_credits_from_hdr(bufs[i], server);
1204 cifs_dbg(FYI, "Received oplock break\n");
1206 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1207 atomic_read(&midCount));
1208 cifs_dump_mem("Received Data is: ", bufs[i],
1209 HEADER_SIZE(server));
1210 smb2_add_credits_from_hdr(bufs[i], server);
1211 #ifdef CONFIG_CIFS_DEBUG2
1212 if (server->ops->dump_detail)
1213 server->ops->dump_detail(bufs[i],
1215 cifs_dump_mids(server);
1216 #endif /* CIFS_DEBUG2 */
1220 if (pdu_length > server->pdu_size) {
1221 if (!allocate_buffers(server))
1223 pdu_length -= server->pdu_size;
1224 server->total_read = 0;
1225 server->large_buf = false;
1226 buf = server->smallbuf;
1229 } /* end while !EXITING */
1231 /* buffer usually freed in free_mid - need to free it here on exit */
1232 cifs_buf_release(server->bigbuf);
1233 if (server->smallbuf) /* no sense logging a debug message if NULL */
1234 cifs_small_buf_release(server->smallbuf);
1236 task_to_wake = xchg(&server->tsk, NULL);
1237 clean_demultiplex_info(server);
1239 /* if server->tsk was NULL then wait for a signal before exiting */
1240 if (!task_to_wake) {
1241 set_current_state(TASK_INTERRUPTIBLE);
1242 while (!signal_pending(current)) {
1244 set_current_state(TASK_INTERRUPTIBLE);
1246 set_current_state(TASK_RUNNING);
1249 memalloc_noreclaim_restore(noreclaim_flag);
1250 module_put_and_kthread_exit(0);
1254 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1255 * if srcaddr is specified and matches the IP address of the rhs argument
1258 cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
1260 switch (srcaddr->sa_family) {
1262 return (rhs->sa_family == AF_UNSPEC);
1264 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1265 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1266 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1269 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1270 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1271 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1275 return false; /* don't expect to be here */
1280 * If no port is specified in addr structure, we try to match with 445 port
1281 * and if it fails - with 139 ports. It should be called only if address
1282 * families of server and addr are equal.
1285 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1287 __be16 port, *sport;
1289 /* SMBDirect manages its own ports, don't match it here */
1293 switch (addr->sa_family) {
1295 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1296 port = ((struct sockaddr_in *) addr)->sin_port;
1299 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1300 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1308 port = htons(CIFS_PORT);
1312 port = htons(RFC1001_PORT);
1315 return port == *sport;
1319 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1320 struct sockaddr *srcaddr)
1322 switch (addr->sa_family) {
1324 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1325 struct sockaddr_in *srv_addr4 =
1326 (struct sockaddr_in *)&server->dstaddr;
1328 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
1333 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1334 struct sockaddr_in6 *srv_addr6 =
1335 (struct sockaddr_in6 *)&server->dstaddr;
1337 if (!ipv6_addr_equal(&addr6->sin6_addr,
1338 &srv_addr6->sin6_addr))
1340 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
1346 return false; /* don't expect to be here */
1349 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
1356 match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1359 * The select_sectype function should either return the ctx->sectype
1360 * that was specified, or "Unspecified" if that sectype was not
1361 * compatible with the given NEGOTIATE request.
1363 if (server->ops->select_sectype(server, ctx->sectype)
1368 * Now check if signing mode is acceptable. No need to check
1369 * global_secflags at this point since if MUST_SIGN is set then
1370 * the server->sign had better be too.
1372 if (ctx->sign && !server->sign)
1378 static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1380 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
1382 if (ctx->nosharesock)
1385 /* this server does not share socket */
1386 if (server->nosharesock)
1389 /* If multidialect negotiation see if existing sessions match one */
1390 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
1391 if (server->vals->protocol_id < SMB30_PROT_ID)
1393 } else if (strcmp(ctx->vals->version_string,
1394 SMBDEFAULT_VERSION_STRING) == 0) {
1395 if (server->vals->protocol_id < SMB21_PROT_ID)
1397 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
1400 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1403 if (strcasecmp(server->hostname, ctx->server_hostname))
1406 if (!match_address(server, addr,
1407 (struct sockaddr *)&ctx->srcaddr))
1410 if (!match_port(server, addr))
1413 if (!match_security(server, ctx))
1416 if (server->echo_interval != ctx->echo_interval * HZ)
1419 if (server->rdma != ctx->rdma)
1422 if (server->ignore_signature != ctx->ignore_signature)
1425 if (server->min_offload != ctx->min_offload)
1431 struct TCP_Server_Info *
1432 cifs_find_tcp_session(struct smb3_fs_context *ctx)
1434 struct TCP_Server_Info *server;
1436 spin_lock(&cifs_tcp_ses_lock);
1437 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1438 #ifdef CONFIG_CIFS_DFS_UPCALL
1440 * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for
1441 * DFS connections to do failover properly, so avoid sharing them with regular
1442 * shares or even links that may connect to same server but having completely
1443 * different failover targets.
1445 if (server->is_dfs_conn)
1449 * Skip ses channels since they're only handled in lower layers
1450 * (e.g. cifs_send_recv).
1452 if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx))
1455 ++server->srv_count;
1456 spin_unlock(&cifs_tcp_ses_lock);
1457 cifs_dbg(FYI, "Existing tcp session with server found\n");
1460 spin_unlock(&cifs_tcp_ses_lock);
1465 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1467 struct task_struct *task;
1469 spin_lock(&cifs_tcp_ses_lock);
1470 if (--server->srv_count > 0) {
1471 spin_unlock(&cifs_tcp_ses_lock);
1475 /* srv_count can never go negative */
1476 WARN_ON(server->srv_count < 0);
1478 put_net(cifs_net_ns(server));
1480 list_del_init(&server->tcp_ses_list);
1481 spin_unlock(&cifs_tcp_ses_lock);
1483 /* For secondary channels, we pick up ref-count on the primary server */
1484 if (CIFS_SERVER_IS_CHAN(server))
1485 cifs_put_tcp_session(server->primary_server, from_reconnect);
1487 cancel_delayed_work_sync(&server->echo);
1488 cancel_delayed_work_sync(&server->resolve);
1492 * Avoid deadlock here: reconnect work calls
1493 * cifs_put_tcp_session() at its end. Need to be sure
1494 * that reconnect work does nothing with server pointer after
1497 cancel_delayed_work(&server->reconnect);
1499 cancel_delayed_work_sync(&server->reconnect);
1501 spin_lock(&cifs_tcp_ses_lock);
1502 server->tcpStatus = CifsExiting;
1503 spin_unlock(&cifs_tcp_ses_lock);
1505 cifs_crypto_secmech_release(server);
1507 kfree(server->session_key.response);
1508 server->session_key.response = NULL;
1509 server->session_key.len = 0;
1510 kfree(server->hostname);
1512 task = xchg(&server->tsk, NULL);
1514 send_sig(SIGKILL, task, 1);
1517 struct TCP_Server_Info *
1518 cifs_get_tcp_session(struct smb3_fs_context *ctx,
1519 struct TCP_Server_Info *primary_server)
1521 struct TCP_Server_Info *tcp_ses = NULL;
1524 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
1526 /* see if we already have a matching tcp_ses */
1527 tcp_ses = cifs_find_tcp_session(ctx);
1531 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1537 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1538 if (!tcp_ses->hostname) {
1543 if (ctx->nosharesock)
1544 tcp_ses->nosharesock = true;
1546 tcp_ses->ops = ctx->ops;
1547 tcp_ses->vals = ctx->vals;
1548 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1550 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
1551 tcp_ses->noblockcnt = ctx->rootfs;
1552 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1553 tcp_ses->noautotune = ctx->noautotune;
1554 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1555 tcp_ses->rdma = ctx->rdma;
1556 tcp_ses->in_flight = 0;
1557 tcp_ses->max_in_flight = 0;
1558 tcp_ses->credits = 1;
1559 if (primary_server) {
1560 spin_lock(&cifs_tcp_ses_lock);
1561 ++primary_server->srv_count;
1562 tcp_ses->primary_server = primary_server;
1563 spin_unlock(&cifs_tcp_ses_lock);
1565 init_waitqueue_head(&tcp_ses->response_q);
1566 init_waitqueue_head(&tcp_ses->request_q);
1567 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1568 mutex_init(&tcp_ses->_srv_mutex);
1569 memcpy(tcp_ses->workstation_RFC1001_name,
1570 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1571 memcpy(tcp_ses->server_RFC1001_name,
1572 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1573 tcp_ses->session_estab = false;
1574 tcp_ses->sequence_number = 0;
1575 tcp_ses->reconnect_instance = 1;
1576 tcp_ses->lstrp = jiffies;
1577 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
1578 spin_lock_init(&tcp_ses->req_lock);
1579 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1580 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
1581 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
1582 INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server);
1583 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1584 mutex_init(&tcp_ses->reconnect_mutex);
1585 #ifdef CONFIG_CIFS_DFS_UPCALL
1586 mutex_init(&tcp_ses->refpath_lock);
1588 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
1589 sizeof(tcp_ses->srcaddr));
1590 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
1591 sizeof(tcp_ses->dstaddr));
1592 if (ctx->use_client_guid)
1593 memcpy(tcp_ses->client_guid, ctx->client_guid,
1594 SMB2_CLIENT_GUID_SIZE);
1596 generate_random_uuid(tcp_ses->client_guid);
1598 * at this point we are the only ones with the pointer
1599 * to the struct since the kernel thread not created yet
1600 * no need to spinlock this init of tcpStatus or srv_count
1602 tcp_ses->tcpStatus = CifsNew;
1603 ++tcp_ses->srv_count;
1605 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1606 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1607 tcp_ses->echo_interval = ctx->echo_interval * HZ;
1609 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
1610 if (tcp_ses->rdma) {
1611 #ifndef CONFIG_CIFS_SMB_DIRECT
1612 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1614 goto out_err_crypto_release;
1616 tcp_ses->smbd_conn = smbd_get_connection(
1617 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
1618 if (tcp_ses->smbd_conn) {
1619 cifs_dbg(VFS, "RDMA transport established\n");
1621 goto smbd_connected;
1624 goto out_err_crypto_release;
1627 rc = ip_connect(tcp_ses);
1629 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
1630 goto out_err_crypto_release;
1634 * since we're in a cifs function already, we know that
1635 * this will succeed. No need for try_module_get().
1637 __module_get(THIS_MODULE);
1638 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
1640 if (IS_ERR(tcp_ses->tsk)) {
1641 rc = PTR_ERR(tcp_ses->tsk);
1642 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
1643 module_put(THIS_MODULE);
1644 goto out_err_crypto_release;
1646 tcp_ses->min_offload = ctx->min_offload;
1648 * at this point we are the only ones with the pointer
1649 * to the struct since the kernel thread not created yet
1650 * no need to spinlock this update of tcpStatus
1652 spin_lock(&cifs_tcp_ses_lock);
1653 tcp_ses->tcpStatus = CifsNeedNegotiate;
1654 spin_unlock(&cifs_tcp_ses_lock);
1656 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1657 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1659 tcp_ses->max_credits = ctx->max_credits;
1661 tcp_ses->nr_targets = 1;
1662 tcp_ses->ignore_signature = ctx->ignore_signature;
1663 /* thread spawned, put it on the list */
1664 spin_lock(&cifs_tcp_ses_lock);
1665 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
1666 spin_unlock(&cifs_tcp_ses_lock);
1668 /* queue echo request delayed work */
1669 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
1671 /* queue dns resolution delayed work */
1672 cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n",
1673 __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT);
1675 queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ));
1679 out_err_crypto_release:
1680 cifs_crypto_secmech_release(tcp_ses);
1682 put_net(cifs_net_ns(tcp_ses));
1686 if (CIFS_SERVER_IS_CHAN(tcp_ses))
1687 cifs_put_tcp_session(tcp_ses->primary_server, false);
1688 kfree(tcp_ses->hostname);
1689 if (tcp_ses->ssocket)
1690 sock_release(tcp_ses->ssocket);
1696 static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1698 if (ctx->sectype != Unspecified &&
1699 ctx->sectype != ses->sectype)
1703 * If an existing session is limited to less channels than
1704 * requested, it should not be reused
1706 spin_lock(&ses->chan_lock);
1707 if (ses->chan_max < ctx->max_channels) {
1708 spin_unlock(&ses->chan_lock);
1711 spin_unlock(&ses->chan_lock);
1713 switch (ses->sectype) {
1715 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
1719 /* NULL username means anonymous session */
1720 if (ses->user_name == NULL) {
1726 /* anything else takes username/password */
1727 if (strncmp(ses->user_name,
1728 ctx->username ? ctx->username : "",
1729 CIFS_MAX_USERNAME_LEN))
1731 if ((ctx->username && strlen(ctx->username) != 0) &&
1732 ses->password != NULL &&
1733 strncmp(ses->password,
1734 ctx->password ? ctx->password : "",
1735 CIFS_MAX_PASSWORD_LEN))
1742 * cifs_setup_ipc - helper to setup the IPC tcon for the session
1743 * @ses: smb session to issue the request on
1744 * @ctx: the superblock configuration context to use for building the
1745 * new tree connection for the IPC (interprocess communication RPC)
1747 * A new IPC connection is made and stored in the session
1748 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1751 cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1754 struct cifs_tcon *tcon;
1755 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1757 struct TCP_Server_Info *server = ses->server;
1760 * If the mount request that resulted in the creation of the
1761 * session requires encryption, force IPC to be encrypted too.
1764 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
1767 cifs_server_dbg(VFS,
1768 "IPC: server doesn't support encryption\n");
1773 tcon = tconInfoAlloc();
1777 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
1783 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
1787 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
1792 cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
1794 ses->tcon_ipc = tcon;
1800 * cifs_free_ipc - helper to release the session IPC tcon
1801 * @ses: smb session to unmount the IPC from
1803 * Needs to be called everytime a session is destroyed.
1805 * On session close, the IPC is closed and the server must release all tcons of the session.
1806 * No need to send a tree disconnect here.
1808 * Besides, it will make the server to not close durable and resilient files on session close, as
1809 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
1812 cifs_free_ipc(struct cifs_ses *ses)
1814 struct cifs_tcon *tcon = ses->tcon_ipc;
1820 ses->tcon_ipc = NULL;
1824 static struct cifs_ses *
1825 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1827 struct cifs_ses *ses;
1829 spin_lock(&cifs_tcp_ses_lock);
1830 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1831 if (ses->ses_status == SES_EXITING)
1833 if (!match_session(ses, ctx))
1836 spin_unlock(&cifs_tcp_ses_lock);
1839 spin_unlock(&cifs_tcp_ses_lock);
1843 void cifs_put_smb_ses(struct cifs_ses *ses)
1845 unsigned int rc, xid;
1846 unsigned int chan_count;
1847 struct TCP_Server_Info *server = ses->server;
1849 spin_lock(&cifs_tcp_ses_lock);
1850 if (ses->ses_status == SES_EXITING) {
1851 spin_unlock(&cifs_tcp_ses_lock);
1855 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1856 cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE");
1858 if (--ses->ses_count > 0) {
1859 spin_unlock(&cifs_tcp_ses_lock);
1863 /* ses_count can never go negative */
1864 WARN_ON(ses->ses_count < 0);
1866 if (ses->ses_status == SES_GOOD)
1867 ses->ses_status = SES_EXITING;
1868 spin_unlock(&cifs_tcp_ses_lock);
1872 if (ses->ses_status == SES_EXITING && server->ops->logoff) {
1874 rc = server->ops->logoff(xid, ses);
1876 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
1881 spin_lock(&cifs_tcp_ses_lock);
1882 list_del_init(&ses->smb_ses_list);
1883 spin_unlock(&cifs_tcp_ses_lock);
1885 spin_lock(&ses->chan_lock);
1886 chan_count = ses->chan_count;
1888 /* close any extra channels */
1889 if (chan_count > 1) {
1892 for (i = 1; i < chan_count; i++) {
1893 spin_unlock(&ses->chan_lock);
1894 cifs_put_tcp_session(ses->chans[i].server, 0);
1895 spin_lock(&ses->chan_lock);
1896 ses->chans[i].server = NULL;
1899 spin_unlock(&ses->chan_lock);
1902 cifs_put_tcp_session(server, 0);
1907 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1908 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
1910 /* Populate username and pw fields from keyring if possible */
1912 cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
1916 const char *delim, *payload;
1920 struct TCP_Server_Info *server = ses->server;
1921 struct sockaddr_in *sa;
1922 struct sockaddr_in6 *sa6;
1923 const struct user_key_payload *upayload;
1925 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1929 /* try to find an address key first */
1930 switch (server->dstaddr.ss_family) {
1932 sa = (struct sockaddr_in *)&server->dstaddr;
1933 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1936 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1937 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1940 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1941 server->dstaddr.ss_family);
1946 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
1947 key = request_key(&key_type_logon, desc, "");
1949 if (!ses->domainName) {
1950 cifs_dbg(FYI, "domainName is NULL\n");
1955 /* didn't work, try to find a domain key */
1956 sprintf(desc, "cifs:d:%s", ses->domainName);
1957 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
1958 key = request_key(&key_type_logon, desc, "");
1966 down_read(&key->sem);
1967 upayload = user_key_payload_locked(key);
1968 if (IS_ERR_OR_NULL(upayload)) {
1969 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
1973 /* find first : in payload */
1974 payload = upayload->data;
1975 delim = strnchr(payload, upayload->datalen, ':');
1976 cifs_dbg(FYI, "payload=%s\n", payload);
1978 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
1984 len = delim - payload;
1985 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
1986 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
1992 ctx->username = kstrndup(payload, len, GFP_KERNEL);
1993 if (!ctx->username) {
1994 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
1999 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
2001 len = key->datalen - (len + 1);
2002 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2003 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2005 kfree(ctx->username);
2006 ctx->username = NULL;
2011 ctx->password = kstrndup(delim, len, GFP_KERNEL);
2012 if (!ctx->password) {
2013 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2016 kfree(ctx->username);
2017 ctx->username = NULL;
2022 * If we have a domain key then we must set the domainName in the
2025 if (is_domain && ses->domainName) {
2026 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
2027 if (!ctx->domainname) {
2028 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
2031 kfree(ctx->username);
2032 ctx->username = NULL;
2033 kfree_sensitive(ctx->password);
2034 ctx->password = NULL;
2039 strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
2046 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2049 #else /* ! CONFIG_KEYS */
2051 cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
2052 struct cifs_ses *ses __attribute__((unused)))
2056 #endif /* CONFIG_KEYS */
2059 * cifs_get_smb_ses - get a session matching @ctx data from @server
2060 * @server: server to setup the session to
2061 * @ctx: superblock configuration context to use to setup the session
2063 * This function assumes it is being called from cifs_mount() where we
2064 * already got a server reference (server refcount +1). See
2065 * cifs_get_tcon() for refcount explanations.
2068 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
2072 struct cifs_ses *ses;
2073 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2074 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2078 ses = cifs_find_smb_ses(server, ctx);
2080 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2083 spin_lock(&ses->chan_lock);
2084 if (cifs_chan_needs_reconnect(ses, server)) {
2085 spin_unlock(&ses->chan_lock);
2086 cifs_dbg(FYI, "Session needs reconnect\n");
2088 mutex_lock(&ses->session_mutex);
2089 rc = cifs_negotiate_protocol(xid, ses, server);
2091 mutex_unlock(&ses->session_mutex);
2092 /* problem -- put our ses reference */
2093 cifs_put_smb_ses(ses);
2098 rc = cifs_setup_session(xid, ses, server,
2101 mutex_unlock(&ses->session_mutex);
2102 /* problem -- put our reference */
2103 cifs_put_smb_ses(ses);
2107 mutex_unlock(&ses->session_mutex);
2109 spin_lock(&ses->chan_lock);
2111 spin_unlock(&ses->chan_lock);
2113 /* existing SMB ses has a server reference already */
2114 cifs_put_tcp_session(server, 0);
2119 cifs_dbg(FYI, "Existing smb sess not found\n");
2120 ses = sesInfoAlloc();
2124 /* new SMB session uses our server ref */
2125 ses->server = server;
2126 if (server->dstaddr.ss_family == AF_INET6)
2127 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
2129 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
2131 if (ctx->username) {
2132 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
2133 if (!ses->user_name)
2137 /* ctx->password freed at unmount */
2138 if (ctx->password) {
2139 ses->password = kstrdup(ctx->password, GFP_KERNEL);
2143 if (ctx->domainname) {
2144 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
2145 if (!ses->domainName)
2149 strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
2151 if (ctx->domainauto)
2152 ses->domainAuto = ctx->domainauto;
2153 ses->cred_uid = ctx->cred_uid;
2154 ses->linux_uid = ctx->linux_uid;
2156 ses->sectype = ctx->sectype;
2157 ses->sign = ctx->sign;
2159 /* add server as first channel */
2160 spin_lock(&ses->chan_lock);
2161 ses->chans[0].server = server;
2162 ses->chan_count = 1;
2163 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
2164 ses->chans_need_reconnect = 1;
2165 spin_unlock(&ses->chan_lock);
2167 mutex_lock(&ses->session_mutex);
2168 rc = cifs_negotiate_protocol(xid, ses, server);
2170 rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
2171 mutex_unlock(&ses->session_mutex);
2173 /* each channel uses a different signing key */
2174 spin_lock(&ses->chan_lock);
2175 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2176 sizeof(ses->smb3signingkey));
2177 spin_unlock(&ses->chan_lock);
2183 * success, put it on the list and add it as first channel
2184 * note: the session becomes active soon after this. So you'll
2185 * need to lock before changing something in the session.
2187 spin_lock(&cifs_tcp_ses_lock);
2188 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2189 spin_unlock(&cifs_tcp_ses_lock);
2193 cifs_setup_ipc(ses, ctx);
2203 static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
2205 if (tcon->status == TID_EXITING)
2207 if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE))
2209 if (tcon->seal != ctx->seal)
2211 if (tcon->snapshot_time != ctx->snapshot_time)
2213 if (tcon->handle_timeout != ctx->handle_timeout)
2215 if (tcon->no_lease != ctx->no_lease)
2217 if (tcon->nodelete != ctx->nodelete)
2222 static struct cifs_tcon *
2223 cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2225 struct list_head *tmp;
2226 struct cifs_tcon *tcon;
2228 spin_lock(&cifs_tcp_ses_lock);
2229 list_for_each(tmp, &ses->tcon_list) {
2230 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
2232 if (!match_tcon(tcon, ctx))
2235 spin_unlock(&cifs_tcp_ses_lock);
2238 spin_unlock(&cifs_tcp_ses_lock);
2243 cifs_put_tcon(struct cifs_tcon *tcon)
2246 struct cifs_ses *ses;
2249 * IPC tcon share the lifetime of their session and are
2250 * destroyed in the session put function
2252 if (tcon == NULL || tcon->ipc)
2256 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2257 spin_lock(&cifs_tcp_ses_lock);
2258 if (--tcon->tc_count > 0) {
2259 spin_unlock(&cifs_tcp_ses_lock);
2263 /* tc_count can never go negative */
2264 WARN_ON(tcon->tc_count < 0);
2266 list_del_init(&tcon->tcon_list);
2267 spin_unlock(&cifs_tcp_ses_lock);
2269 if (tcon->use_witness) {
2272 rc = cifs_swn_unregister(tcon);
2274 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2280 if (ses->server->ops->tree_disconnect)
2281 ses->server->ops->tree_disconnect(xid, tcon);
2284 cifs_fscache_release_super_cookie(tcon);
2286 cifs_put_smb_ses(ses);
2290 * cifs_get_tcon - get a tcon matching @ctx data from @ses
2291 * @ses: smb session to issue the request on
2292 * @ctx: the superblock configuration context to use for building the
2294 * - tcon refcount is the number of mount points using the tcon.
2295 * - ses refcount is the number of tcon using the session.
2297 * 1. This function assumes it is being called from cifs_mount() where
2298 * we already got a session reference (ses refcount +1).
2300 * 2. Since we're in the context of adding a mount point, the end
2301 * result should be either:
2303 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2304 * its session refcount incremented (1 new tcon). This +1 was
2305 * already done in (1).
2307 * b) an existing tcon with refcount+1 (add a mount point to it) and
2308 * identical ses refcount (no new tcon). Because of (1) we need to
2309 * decrement the ses refcount.
2311 static struct cifs_tcon *
2312 cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2315 struct cifs_tcon *tcon;
2317 tcon = cifs_find_tcon(ses, ctx);
2320 * tcon has refcount already incremented but we need to
2321 * decrement extra ses reference gotten by caller (case b)
2323 cifs_dbg(FYI, "Found match on UNC path\n");
2324 cifs_put_smb_ses(ses);
2328 if (!ses->server->ops->tree_connect) {
2333 tcon = tconInfoAlloc();
2339 if (ctx->snapshot_time) {
2340 if (ses->server->vals->protocol_id == 0) {
2342 "Use SMB2 or later for snapshot mount option\n");
2346 tcon->snapshot_time = ctx->snapshot_time;
2349 if (ctx->handle_timeout) {
2350 if (ses->server->vals->protocol_id == 0) {
2352 "Use SMB2.1 or later for handle timeout option\n");
2356 tcon->handle_timeout = ctx->handle_timeout;
2360 if (ctx->password) {
2361 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
2362 if (!tcon->password) {
2369 if (ses->server->vals->protocol_id == 0) {
2371 "SMB3 or later required for encryption\n");
2374 } else if (tcon->ses->server->capabilities &
2375 SMB2_GLOBAL_CAP_ENCRYPTION)
2378 cifs_dbg(VFS, "Encryption is not supported on share\n");
2384 if (ctx->linux_ext) {
2385 if (ses->server->posix_ext_supported) {
2386 tcon->posix_extensions = true;
2387 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
2388 } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
2389 (strcmp(ses->server->vals->version_string,
2390 SMB3ANY_VERSION_STRING) == 0) ||
2391 (strcmp(ses->server->vals->version_string,
2392 SMBDEFAULT_VERSION_STRING) == 0)) {
2393 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
2397 cifs_dbg(VFS, "Check vers= mount option. SMB3.11 "
2398 "disabled but required for POSIX extensions\n");
2405 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2408 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2412 tcon->use_persistent = false;
2413 /* check if SMB2 or later, CIFS does not support persistent handles */
2414 if (ctx->persistent) {
2415 if (ses->server->vals->protocol_id == 0) {
2417 "SMB3 or later required for persistent handles\n");
2420 } else if (ses->server->capabilities &
2421 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2422 tcon->use_persistent = true;
2423 else /* persistent handles requested but not supported */ {
2425 "Persistent handles not supported on share\n");
2429 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2430 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2431 && (ctx->nopersistent == false)) {
2432 cifs_dbg(FYI, "enabling persistent handles\n");
2433 tcon->use_persistent = true;
2434 } else if (ctx->resilient) {
2435 if (ses->server->vals->protocol_id == 0) {
2437 "SMB2.1 or later required for resilient handles\n");
2441 tcon->use_resilient = true;
2444 tcon->use_witness = false;
2445 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
2446 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2447 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
2449 * Set witness in use flag in first place
2450 * to retry registration in the echo task
2452 tcon->use_witness = true;
2453 /* And try to register immediately */
2454 rc = cifs_swn_register(tcon);
2456 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2460 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2461 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2466 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2472 /* If the user really knows what they are doing they can override */
2473 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
2475 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
2476 else if (ctx->cache_rw)
2477 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2480 if (ctx->no_lease) {
2481 if (ses->server->vals->protocol_id == 0) {
2483 "SMB2 or later required for nolease option\n");
2487 tcon->no_lease = ctx->no_lease;
2491 * We can have only one retry value for a connection to a share so for
2492 * resources mounted more than once to the same server share the last
2493 * value passed in for the retry flag is used.
2495 tcon->retry = ctx->retry;
2496 tcon->nocase = ctx->nocase;
2497 tcon->broken_sparse_sup = ctx->no_sparse;
2498 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
2499 tcon->nohandlecache = ctx->nohandlecache;
2501 tcon->nohandlecache = true;
2502 tcon->nodelete = ctx->nodelete;
2503 tcon->local_lease = ctx->local_lease;
2504 INIT_LIST_HEAD(&tcon->pending_opens);
2506 spin_lock(&cifs_tcp_ses_lock);
2507 list_add(&tcon->tcon_list, &ses->tcon_list);
2508 spin_unlock(&cifs_tcp_ses_lock);
2518 cifs_put_tlink(struct tcon_link *tlink)
2520 if (!tlink || IS_ERR(tlink))
2523 if (!atomic_dec_and_test(&tlink->tl_count) ||
2524 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2525 tlink->tl_time = jiffies;
2529 if (!IS_ERR(tlink_tcon(tlink)))
2530 cifs_put_tcon(tlink_tcon(tlink));
2536 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2538 struct cifs_sb_info *old = CIFS_SB(sb);
2539 struct cifs_sb_info *new = mnt_data->cifs_sb;
2540 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2541 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
2543 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2546 if (old->mnt_cifs_serverino_autodisabled)
2547 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2549 if (oldflags != newflags)
2553 * We want to share sb only if we don't specify an r/wsize or
2554 * specified r/wsize is greater than or equal to existing one.
2556 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
2559 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
2562 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2563 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
2566 if (old->ctx->file_mode != new->ctx->file_mode ||
2567 old->ctx->dir_mode != new->ctx->dir_mode)
2570 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2573 if (old->ctx->acregmax != new->ctx->acregmax)
2575 if (old->ctx->acdirmax != new->ctx->acdirmax)
2582 match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2584 struct cifs_sb_info *old = CIFS_SB(sb);
2585 struct cifs_sb_info *new = mnt_data->cifs_sb;
2586 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2588 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2591 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
2593 else if (!old_set && !new_set)
2600 cifs_match_super(struct super_block *sb, void *data)
2602 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2603 struct smb3_fs_context *ctx;
2604 struct cifs_sb_info *cifs_sb;
2605 struct TCP_Server_Info *tcp_srv;
2606 struct cifs_ses *ses;
2607 struct cifs_tcon *tcon;
2608 struct tcon_link *tlink;
2611 spin_lock(&cifs_tcp_ses_lock);
2612 cifs_sb = CIFS_SB(sb);
2613 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2614 if (tlink == NULL) {
2615 /* can not match superblock if tlink were ever null */
2616 spin_unlock(&cifs_tcp_ses_lock);
2619 tcon = tlink_tcon(tlink);
2621 tcp_srv = ses->server;
2623 ctx = mnt_data->ctx;
2625 if (!match_server(tcp_srv, ctx) ||
2626 !match_session(ses, ctx) ||
2627 !match_tcon(tcon, ctx) ||
2628 !match_prepath(sb, mnt_data)) {
2633 rc = compare_mount_options(sb, mnt_data);
2635 spin_unlock(&cifs_tcp_ses_lock);
2636 cifs_put_tlink(tlink);
2640 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2641 static struct lock_class_key cifs_key[2];
2642 static struct lock_class_key cifs_slock_key[2];
2645 cifs_reclassify_socket4(struct socket *sock)
2647 struct sock *sk = sock->sk;
2648 BUG_ON(!sock_allow_reclassification(sk));
2649 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2650 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2654 cifs_reclassify_socket6(struct socket *sock)
2656 struct sock *sk = sock->sk;
2657 BUG_ON(!sock_allow_reclassification(sk));
2658 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2659 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2663 cifs_reclassify_socket4(struct socket *sock)
2668 cifs_reclassify_socket6(struct socket *sock)
2673 /* See RFC1001 section 14 on representation of Netbios names */
2674 static void rfc1002mangle(char *target, char *source, unsigned int length)
2678 for (i = 0, j = 0; i < (length); i++) {
2679 /* mask a nibble at a time and encode */
2680 target[j] = 'A' + (0x0F & (source[i] >> 4));
2681 target[j+1] = 'A' + (0x0F & source[i]);
2688 bind_socket(struct TCP_Server_Info *server)
2691 if (server->srcaddr.ss_family != AF_UNSPEC) {
2692 /* Bind to the specified local IP address */
2693 struct socket *socket = server->ssocket;
2694 rc = socket->ops->bind(socket,
2695 (struct sockaddr *) &server->srcaddr,
2696 sizeof(server->srcaddr));
2698 struct sockaddr_in *saddr4;
2699 struct sockaddr_in6 *saddr6;
2700 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2701 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2702 if (saddr6->sin6_family == AF_INET6)
2703 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
2704 &saddr6->sin6_addr, rc);
2706 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
2707 &saddr4->sin_addr.s_addr, rc);
2714 ip_rfc1001_connect(struct TCP_Server_Info *server)
2718 * some servers require RFC1001 sessinit before sending
2719 * negprot - BB check reconnection in case where second
2720 * sessinit is sent but no second negprot
2722 struct rfc1002_session_packet *ses_init_buf;
2723 struct smb_hdr *smb_buf;
2724 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2727 ses_init_buf->trailer.session_req.called_len = 32;
2729 if (server->server_RFC1001_name[0] != 0)
2730 rfc1002mangle(ses_init_buf->trailer.
2731 session_req.called_name,
2732 server->server_RFC1001_name,
2733 RFC1001_NAME_LEN_WITH_NULL);
2735 rfc1002mangle(ses_init_buf->trailer.
2736 session_req.called_name,
2737 DEFAULT_CIFS_CALLED_NAME,
2738 RFC1001_NAME_LEN_WITH_NULL);
2740 ses_init_buf->trailer.session_req.calling_len = 32;
2743 * calling name ends in null (byte 16) from old smb
2746 if (server->workstation_RFC1001_name[0] != 0)
2747 rfc1002mangle(ses_init_buf->trailer.
2748 session_req.calling_name,
2749 server->workstation_RFC1001_name,
2750 RFC1001_NAME_LEN_WITH_NULL);
2752 rfc1002mangle(ses_init_buf->trailer.
2753 session_req.calling_name,
2755 RFC1001_NAME_LEN_WITH_NULL);
2757 ses_init_buf->trailer.session_req.scope1 = 0;
2758 ses_init_buf->trailer.session_req.scope2 = 0;
2759 smb_buf = (struct smb_hdr *)ses_init_buf;
2761 /* sizeof RFC1002_SESSION_REQUEST with no scope */
2762 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
2763 rc = smb_send(server, smb_buf, 0x44);
2764 kfree(ses_init_buf);
2766 * RFC1001 layer in at least one server
2767 * requires very short break before negprot
2768 * presumably because not expecting negprot
2769 * to follow so fast. This is a simple
2770 * solution that works without
2771 * complicating the code and causes no
2772 * significant slowing down on mount
2775 usleep_range(1000, 2000);
2778 * else the negprot may still work without this
2779 * even though malloc failed
2786 generic_ip_connect(struct TCP_Server_Info *server)
2791 struct socket *socket = server->ssocket;
2792 struct sockaddr *saddr;
2794 saddr = (struct sockaddr *) &server->dstaddr;
2796 if (server->dstaddr.ss_family == AF_INET6) {
2797 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2799 sport = ipv6->sin6_port;
2800 slen = sizeof(struct sockaddr_in6);
2802 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2805 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2807 sport = ipv4->sin_port;
2808 slen = sizeof(struct sockaddr_in);
2810 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2814 if (socket == NULL) {
2815 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2816 IPPROTO_TCP, &socket, 1);
2818 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
2819 server->ssocket = NULL;
2823 /* BB other socket options to set KEEPALIVE, NODELAY? */
2824 cifs_dbg(FYI, "Socket created\n");
2825 server->ssocket = socket;
2826 socket->sk->sk_allocation = GFP_NOFS;
2827 if (sfamily == AF_INET6)
2828 cifs_reclassify_socket6(socket);
2830 cifs_reclassify_socket4(socket);
2833 rc = bind_socket(server);
2838 * Eventually check for other socket options to change from
2839 * the default. sock_setsockopt not used because it expects
2842 socket->sk->sk_rcvtimeo = 7 * HZ;
2843 socket->sk->sk_sndtimeo = 5 * HZ;
2845 /* make the bufsizes depend on wsize/rsize and max requests */
2846 if (server->noautotune) {
2847 if (socket->sk->sk_sndbuf < (200 * 1024))
2848 socket->sk->sk_sndbuf = 200 * 1024;
2849 if (socket->sk->sk_rcvbuf < (140 * 1024))
2850 socket->sk->sk_rcvbuf = 140 * 1024;
2853 if (server->tcp_nodelay)
2854 tcp_sock_set_nodelay(socket->sk);
2856 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
2857 socket->sk->sk_sndbuf,
2858 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2860 rc = socket->ops->connect(socket, saddr, slen,
2861 server->noblockcnt ? O_NONBLOCK : 0);
2863 * When mounting SMB root file systems, we do not want to block in
2864 * connect. Otherwise bail out and then let cifs_reconnect() perform
2865 * reconnect failover - if possible.
2867 if (server->noblockcnt && rc == -EINPROGRESS)
2870 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
2871 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
2872 sock_release(socket);
2873 server->ssocket = NULL;
2876 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
2877 if (sport == htons(RFC1001_PORT))
2878 rc = ip_rfc1001_connect(server);
2884 ip_connect(struct TCP_Server_Info *server)
2887 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2888 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2890 if (server->dstaddr.ss_family == AF_INET6)
2891 sport = &addr6->sin6_port;
2893 sport = &addr->sin_port;
2898 /* try with 445 port at first */
2899 *sport = htons(CIFS_PORT);
2901 rc = generic_ip_connect(server);
2905 /* if it failed, try with 139 port */
2906 *sport = htons(RFC1001_PORT);
2909 return generic_ip_connect(server);
2912 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
2913 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
2916 * If we are reconnecting then should we check to see if
2917 * any requested capabilities changed locally e.g. via
2918 * remount but we can not do much about it here
2919 * if they have (even if we could detect it by the following)
2920 * Perhaps we could add a backpointer to array of sb from tcon
2921 * or if we change to make all sb to same share the same
2922 * sb as NFS - then we only have one backpointer to sb.
2923 * What if we wanted to mount the server share twice once with
2924 * and once without posixacls or posix paths?
2926 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
2928 if (ctx && ctx->no_linux_ext) {
2929 tcon->fsUnixInfo.Capability = 0;
2930 tcon->unix_ext = 0; /* Unix Extensions disabled */
2931 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
2934 tcon->unix_ext = 1; /* Unix Extensions supported */
2936 if (!tcon->unix_ext) {
2937 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
2941 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
2942 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
2943 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
2945 * check for reconnect case in which we do not
2946 * want to change the mount behavior if we can avoid it
2950 * turn off POSIX ACL and PATHNAMES if not set
2951 * originally at mount time
2953 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
2954 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
2955 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2956 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
2957 cifs_dbg(VFS, "POSIXPATH support change\n");
2958 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
2959 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
2960 cifs_dbg(VFS, "possible reconnect error\n");
2961 cifs_dbg(VFS, "server disabled POSIX path support\n");
2965 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
2966 cifs_dbg(VFS, "per-share encryption not supported yet\n");
2968 cap &= CIFS_UNIX_CAP_MASK;
2969 if (ctx && ctx->no_psx_acl)
2970 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
2971 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
2972 cifs_dbg(FYI, "negotiated posix acl support\n");
2974 cifs_sb->mnt_cifs_flags |=
2975 CIFS_MOUNT_POSIXACL;
2978 if (ctx && ctx->posix_paths == 0)
2979 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
2980 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2981 cifs_dbg(FYI, "negotiate posix pathnames\n");
2983 cifs_sb->mnt_cifs_flags |=
2984 CIFS_MOUNT_POSIX_PATHS;
2987 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
2988 #ifdef CONFIG_CIFS_DEBUG2
2989 if (cap & CIFS_UNIX_FCNTL_CAP)
2990 cifs_dbg(FYI, "FCNTL cap\n");
2991 if (cap & CIFS_UNIX_EXTATTR_CAP)
2992 cifs_dbg(FYI, "EXTATTR cap\n");
2993 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
2994 cifs_dbg(FYI, "POSIX path cap\n");
2995 if (cap & CIFS_UNIX_XATTR_CAP)
2996 cifs_dbg(FYI, "XATTR cap\n");
2997 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
2998 cifs_dbg(FYI, "POSIX ACL cap\n");
2999 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3000 cifs_dbg(FYI, "very large read cap\n");
3001 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3002 cifs_dbg(FYI, "very large write cap\n");
3003 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3004 cifs_dbg(FYI, "transport encryption cap\n");
3005 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3006 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3007 #endif /* CIFS_DEBUG2 */
3008 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3010 cifs_dbg(FYI, "resetting capabilities failed\n");
3012 cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
3018 int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
3020 struct smb3_fs_context *ctx = cifs_sb->ctx;
3022 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3024 spin_lock_init(&cifs_sb->tlink_tree_lock);
3025 cifs_sb->tlink_tree = RB_ROOT;
3027 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
3028 ctx->file_mode, ctx->dir_mode);
3030 /* this is needed for ASCII cp to Unicode converts */
3031 if (ctx->iocharset == NULL) {
3032 /* load_nls_default cannot return null */
3033 cifs_sb->local_nls = load_nls_default();
3035 cifs_sb->local_nls = load_nls(ctx->iocharset);
3036 if (cifs_sb->local_nls == NULL) {
3037 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3042 ctx->local_nls = cifs_sb->local_nls;
3044 smb3_update_mnt_flags(cifs_sb);
3047 cifs_dbg(FYI, "mounting share using direct i/o\n");
3048 if (ctx->cache_ro) {
3049 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
3050 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
3051 } else if (ctx->cache_rw) {
3052 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
3053 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
3054 CIFS_MOUNT_RW_CACHE);
3057 if ((ctx->cifs_acl) && (ctx->dynperm))
3058 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3061 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
3062 if (cifs_sb->prepath == NULL)
3064 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3070 /* Release all succeed connections */
3071 static inline void mount_put_conns(struct mount_ctx *mnt_ctx)
3076 cifs_put_tcon(mnt_ctx->tcon);
3077 else if (mnt_ctx->ses)
3078 cifs_put_smb_ses(mnt_ctx->ses);
3079 else if (mnt_ctx->server)
3080 cifs_put_tcp_session(mnt_ctx->server, 0);
3081 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3082 free_xid(mnt_ctx->xid);
3085 /* Get connections for tcp, ses and tcon */
3086 static int mount_get_conns(struct mount_ctx *mnt_ctx)
3089 struct TCP_Server_Info *server = NULL;
3090 struct cifs_ses *ses = NULL;
3091 struct cifs_tcon *tcon = NULL;
3092 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3093 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3098 /* get a reference to a tcp session */
3099 server = cifs_get_tcp_session(ctx, NULL);
3100 if (IS_ERR(server)) {
3101 rc = PTR_ERR(server);
3106 /* get a reference to a SMB session */
3107 ses = cifs_get_smb_ses(server, ctx);
3114 if ((ctx->persistent == true) && (!(ses->server->capabilities &
3115 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3116 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
3121 /* search for existing tcon to this server share */
3122 tcon = cifs_get_tcon(ses, ctx);
3129 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3130 if (tcon->posix_extensions)
3131 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3133 /* tell server which Unix caps we support */
3134 if (cap_unix(tcon->ses)) {
3136 * reset of caps checks mount to see if unix extensions disabled
3137 * for just this mount.
3139 reset_cifs_unix_caps(xid, tcon, cifs_sb, ctx);
3140 spin_lock(&cifs_tcp_ses_lock);
3141 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3142 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3143 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3144 spin_unlock(&cifs_tcp_ses_lock);
3148 spin_unlock(&cifs_tcp_ses_lock);
3150 tcon->unix_ext = 0; /* server does not support them */
3152 /* do not care if a following call succeed - informational */
3153 if (!tcon->pipe && server->ops->qfs_tcon) {
3154 server->ops->qfs_tcon(xid, tcon, cifs_sb);
3155 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3156 if (tcon->fsDevInfo.DeviceCharacteristics &
3157 cpu_to_le32(FILE_READ_ONLY_DEVICE))
3158 cifs_dbg(VFS, "mounted to read only share\n");
3159 else if ((cifs_sb->mnt_cifs_flags &
3160 CIFS_MOUNT_RW_CACHE) == 0)
3161 cifs_dbg(VFS, "read only mount of RW share\n");
3162 /* no need to log a RW mount of a typical RW share */
3167 * Clamp the rsize/wsize mount arguments if they are too big for the server
3168 * and set the rsize/wsize to the negotiated values if not passed in by
3171 if ((cifs_sb->ctx->wsize == 0) ||
3172 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
3173 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
3174 if ((cifs_sb->ctx->rsize == 0) ||
3175 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
3176 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
3179 * The cookie is initialized from volume info returned above.
3180 * Inside cifs_fscache_get_super_cookie it checks
3181 * that we do not get super cookie twice.
3183 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
3184 cifs_fscache_get_super_cookie(tcon);
3187 mnt_ctx->server = server;
3189 mnt_ctx->tcon = tcon;
3195 static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3196 struct cifs_tcon *tcon)
3198 struct tcon_link *tlink;
3200 /* hang the tcon off of the superblock */
3201 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3205 tlink->tl_uid = ses->linux_uid;
3206 tlink->tl_tcon = tcon;
3207 tlink->tl_time = jiffies;
3208 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3209 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3211 cifs_sb->master_tlink = tlink;
3212 spin_lock(&cifs_sb->tlink_tree_lock);
3213 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3214 spin_unlock(&cifs_sb->tlink_tree_lock);
3216 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3221 #ifdef CONFIG_CIFS_DFS_UPCALL
3222 /* Get unique dfs connections */
3223 static int mount_get_dfs_conns(struct mount_ctx *mnt_ctx)
3227 mnt_ctx->fs_ctx->nosharesock = true;
3228 rc = mount_get_conns(mnt_ctx);
3229 if (mnt_ctx->server) {
3230 cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__);
3231 spin_lock(&cifs_tcp_ses_lock);
3232 mnt_ctx->server->is_dfs_conn = true;
3233 spin_unlock(&cifs_tcp_ses_lock);
3239 * cifs_build_path_to_root returns full path to root when we do not have an
3240 * existing connection (tcon)
3243 build_unc_path_to_root(const struct smb3_fs_context *ctx,
3244 const struct cifs_sb_info *cifs_sb, bool useppath)
3246 char *full_path, *pos;
3247 unsigned int pplen = useppath && ctx->prepath ?
3248 strlen(ctx->prepath) + 1 : 0;
3249 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
3251 if (unc_len > MAX_TREE_SIZE)
3252 return ERR_PTR(-EINVAL);
3254 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3255 if (full_path == NULL)
3256 return ERR_PTR(-ENOMEM);
3258 memcpy(full_path, ctx->UNC, unc_len);
3259 pos = full_path + unc_len;
3262 *pos = CIFS_DIR_SEP(cifs_sb);
3263 memcpy(pos + 1, ctx->prepath, pplen);
3267 *pos = '\0'; /* add trailing null */
3268 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3269 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3274 * expand_dfs_referral - Update cifs_sb from dfs referral path
3276 * cifs_sb->ctx->mount_options will be (re-)allocated to a string containing updated options for the
3277 * submount. Otherwise it will be left untouched.
3279 static int expand_dfs_referral(struct mount_ctx *mnt_ctx, const char *full_path,
3280 struct dfs_info3_param *referral)
3283 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3284 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3285 char *fake_devname = NULL, *mdata = NULL;
3287 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, referral,
3289 if (IS_ERR(mdata)) {
3290 rc = PTR_ERR(mdata);
3294 * We can not clear out the whole structure since we no longer have an explicit
3295 * function to parse a mount-string. Instead we need to clear out the individual
3296 * fields that are no longer valid.
3298 kfree(ctx->prepath);
3299 ctx->prepath = NULL;
3300 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
3302 kfree(fake_devname);
3303 kfree(cifs_sb->ctx->mount_options);
3304 cifs_sb->ctx->mount_options = mdata;
3310 /* TODO: all callers to this are broken. We are not parsing mount_options here
3311 * we should pass a clone of the original context?
3314 cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
3319 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3320 rc = smb3_parse_devname(devname, ctx);
3322 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3330 rc = smb3_parse_opt(mntopts, "ip", &ip);
3332 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3336 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3339 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3344 if (ctx->nullauth) {
3345 cifs_dbg(FYI, "Anonymous login\n");
3346 kfree(ctx->username);
3347 ctx->username = NULL;
3348 } else if (ctx->username) {
3349 /* BB fixme parse for domain name here */
3350 cifs_dbg(FYI, "Username: %s\n", ctx->username);
3352 cifs_dbg(VFS, "No username specified\n");
3353 /* In userspace mount helper we can get user name from alternate
3354 locations such as env variables and files on disk */
3362 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3364 struct cifs_tcon *tcon,
3365 struct cifs_sb_info *cifs_sb,
3372 int skip = added_treename ? 1 : 0;
3374 sep = CIFS_DIR_SEP(cifs_sb);
3377 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3379 /* skip separators */
3384 /* next separator */
3385 while (*s && *s != sep)
3388 * if the treename is added, we then have to skip the first
3389 * part within the separators
3396 * temporarily null-terminate the path at the end of
3397 * the current component
3401 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3409 * Check if path is remote (i.e. a DFS share).
3411 * Return -EREMOTE if it is, otherwise 0 or -errno.
3413 static int is_path_remote(struct mount_ctx *mnt_ctx)
3416 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3417 struct TCP_Server_Info *server = mnt_ctx->server;
3418 unsigned int xid = mnt_ctx->xid;
3419 struct cifs_tcon *tcon = mnt_ctx->tcon;
3420 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3422 #ifdef CONFIG_CIFS_DFS_UPCALL
3423 bool nodfs = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS;
3426 if (!server->ops->is_path_accessible)
3430 * cifs_build_path_to_root works only when we have a valid tcon
3432 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
3433 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3434 if (full_path == NULL)
3437 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3439 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3441 #ifdef CONFIG_CIFS_DFS_UPCALL
3448 /* path *might* exist with non-ASCII characters in DFS root
3449 * try again with full path (only if nodfs is not set) */
3450 if (rc == -ENOENT && is_tcon_dfs(tcon))
3451 rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon, cifs_sb,
3454 if (rc != 0 && rc != -EREMOTE)
3457 if (rc != -EREMOTE) {
3458 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
3459 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
3461 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3462 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3472 #ifdef CONFIG_CIFS_DFS_UPCALL
3473 static void set_root_ses(struct mount_ctx *mnt_ctx)
3476 spin_lock(&cifs_tcp_ses_lock);
3477 mnt_ctx->ses->ses_count++;
3478 spin_unlock(&cifs_tcp_ses_lock);
3479 dfs_cache_add_refsrv_session(&mnt_ctx->mount_id, mnt_ctx->ses);
3481 mnt_ctx->root_ses = mnt_ctx->ses;
3484 static int is_dfs_mount(struct mount_ctx *mnt_ctx, bool *isdfs, struct dfs_cache_tgt_list *root_tl)
3487 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3488 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3492 rc = mount_get_conns(mnt_ctx);
3494 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3495 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
3497 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3498 * to respond with PATH_NOT_COVERED to requests that include the prefix.
3500 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
3501 dfs_cache_find(mnt_ctx->xid, mnt_ctx->ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3502 ctx->UNC + 1, NULL, root_tl)) {
3505 /* Check if it is fully accessible and then mount it */
3506 rc = is_path_remote(mnt_ctx);
3509 else if (rc != -EREMOTE)
3515 static int connect_dfs_target(struct mount_ctx *mnt_ctx, const char *full_path,
3516 const char *ref_path, struct dfs_cache_tgt_iterator *tit)
3519 struct dfs_info3_param ref = {};
3520 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3521 char *oldmnt = cifs_sb->ctx->mount_options;
3523 cifs_dbg(FYI, "%s: full_path=%s ref_path=%s target=%s\n", __func__, full_path, ref_path,
3524 dfs_cache_get_tgt_name(tit));
3526 rc = dfs_cache_get_tgt_referral(ref_path, tit, &ref);
3530 rc = expand_dfs_referral(mnt_ctx, full_path, &ref);
3534 /* Connect to new target only if we were redirected (e.g. mount options changed) */
3535 if (oldmnt != cifs_sb->ctx->mount_options) {
3536 mount_put_conns(mnt_ctx);
3537 rc = mount_get_dfs_conns(mnt_ctx);
3540 if (cifs_is_referral_server(mnt_ctx->tcon, &ref))
3541 set_root_ses(mnt_ctx);
3542 rc = dfs_cache_update_tgthint(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3543 cifs_remap(cifs_sb), ref_path, tit);
3547 free_dfs_info_param(&ref);
3551 static int connect_dfs_root(struct mount_ctx *mnt_ctx, struct dfs_cache_tgt_list *root_tl)
3555 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3556 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3557 struct dfs_cache_tgt_iterator *tit;
3559 /* Put initial connections as they might be shared with other mounts. We need unique dfs
3560 * connections per mount to properly failover, so mount_get_dfs_conns() must be used from
3563 mount_put_conns(mnt_ctx);
3564 mount_get_dfs_conns(mnt_ctx);
3565 set_root_ses(mnt_ctx);
3567 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3568 if (IS_ERR(full_path))
3569 return PTR_ERR(full_path);
3571 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(ctx->UNC, cifs_sb->local_nls,
3572 cifs_remap(cifs_sb));
3573 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3574 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3575 mnt_ctx->origin_fullpath = NULL;
3579 /* Try all dfs root targets */
3580 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(root_tl);
3581 tit; tit = dfs_cache_get_next_tgt(root_tl, tit)) {
3582 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->origin_fullpath + 1, tit);
3584 mnt_ctx->leaf_fullpath = kstrdup(mnt_ctx->origin_fullpath, GFP_KERNEL);
3585 if (!mnt_ctx->leaf_fullpath)
3596 static int __follow_dfs_link(struct mount_ctx *mnt_ctx)
3599 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3600 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3602 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3603 struct dfs_cache_tgt_iterator *tit;
3605 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3606 if (IS_ERR(full_path))
3607 return PTR_ERR(full_path);
3609 kfree(mnt_ctx->leaf_fullpath);
3610 mnt_ctx->leaf_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3611 cifs_remap(cifs_sb));
3612 if (IS_ERR(mnt_ctx->leaf_fullpath)) {
3613 rc = PTR_ERR(mnt_ctx->leaf_fullpath);
3614 mnt_ctx->leaf_fullpath = NULL;
3618 /* Get referral from dfs link */
3619 rc = dfs_cache_find(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3620 cifs_remap(cifs_sb), mnt_ctx->leaf_fullpath + 1, NULL, &tl);
3624 /* Try all dfs link targets. If an I/O fails from currently connected DFS target with an
3625 * error other than STATUS_PATH_NOT_COVERED (-EREMOTE), then retry it from other targets as
3626 * specified in MS-DFSC "3.1.5.2 I/O Operation to Target Fails with an Error Other Than
3627 * STATUS_PATH_NOT_COVERED."
3629 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(&tl);
3630 tit; tit = dfs_cache_get_next_tgt(&tl, tit)) {
3631 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->leaf_fullpath + 1, tit);
3633 rc = is_path_remote(mnt_ctx);
3634 if (!rc || rc == -EREMOTE)
3641 dfs_cache_free_tgts(&tl);
3645 static int follow_dfs_link(struct mount_ctx *mnt_ctx)
3648 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3649 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3653 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3654 if (IS_ERR(full_path))
3655 return PTR_ERR(full_path);
3657 kfree(mnt_ctx->origin_fullpath);
3658 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3659 cifs_remap(cifs_sb));
3662 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3663 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3664 mnt_ctx->origin_fullpath = NULL;
3669 rc = __follow_dfs_link(mnt_ctx);
3670 if (!rc || rc != -EREMOTE)
3672 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
3677 /* Set up DFS referral paths for failover */
3678 static void setup_server_referral_paths(struct mount_ctx *mnt_ctx)
3680 struct TCP_Server_Info *server = mnt_ctx->server;
3682 mutex_lock(&server->refpath_lock);
3683 server->origin_fullpath = mnt_ctx->origin_fullpath;
3684 server->leaf_fullpath = mnt_ctx->leaf_fullpath;
3685 server->current_fullpath = mnt_ctx->leaf_fullpath;
3686 mutex_unlock(&server->refpath_lock);
3687 mnt_ctx->origin_fullpath = mnt_ctx->leaf_fullpath = NULL;
3690 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3693 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3694 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3697 rc = is_dfs_mount(&mnt_ctx, &isdfs, &tl);
3703 /* proceed as DFS mount */
3704 uuid_gen(&mnt_ctx.mount_id);
3705 rc = connect_dfs_root(&mnt_ctx, &tl);
3706 dfs_cache_free_tgts(&tl);
3711 rc = is_path_remote(&mnt_ctx);
3713 rc = follow_dfs_link(&mnt_ctx);
3717 setup_server_referral_paths(&mnt_ctx);
3719 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3720 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
3722 cifs_autodisable_serverino(cifs_sb);
3724 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3725 * that have different prefix paths.
3727 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3728 kfree(cifs_sb->prepath);
3729 cifs_sb->prepath = ctx->prepath;
3730 ctx->prepath = NULL;
3731 uuid_copy(&cifs_sb->dfs_mount_id, &mnt_ctx.mount_id);
3734 free_xid(mnt_ctx.xid);
3735 cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3736 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3739 dfs_cache_put_refsrv_sessions(&mnt_ctx.mount_id);
3740 kfree(mnt_ctx.origin_fullpath);
3741 kfree(mnt_ctx.leaf_fullpath);
3742 mount_put_conns(&mnt_ctx);
3746 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3749 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3751 rc = mount_get_conns(&mnt_ctx);
3756 rc = is_path_remote(&mnt_ctx);
3763 free_xid(mnt_ctx.xid);
3764 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3767 mount_put_conns(&mnt_ctx);
3773 * Issue a TREE_CONNECT request.
3776 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3777 const char *tree, struct cifs_tcon *tcon,
3778 const struct nls_table *nls_codepage)
3780 struct smb_hdr *smb_buffer;
3781 struct smb_hdr *smb_buffer_response;
3784 unsigned char *bcc_ptr;
3787 __u16 bytes_left, count;
3792 smb_buffer = cifs_buf_get();
3793 if (smb_buffer == NULL)
3796 smb_buffer_response = smb_buffer;
3798 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3799 NULL /*no tid */ , 4 /*wct */ );
3801 smb_buffer->Mid = get_next_mid(ses->server);
3802 smb_buffer->Uid = ses->Suid;
3803 pSMB = (TCONX_REQ *) smb_buffer;
3804 pSMBr = (TCONX_RSP *) smb_buffer_response;
3806 pSMB->AndXCommand = 0xFF;
3807 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3808 bcc_ptr = &pSMB->Password[0];
3809 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
3810 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
3811 *bcc_ptr = 0; /* password is null byte */
3812 bcc_ptr++; /* skip password */
3813 /* already aligned so no need to do it below */
3816 if (ses->server->sign)
3817 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3819 if (ses->capabilities & CAP_STATUS32) {
3820 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3822 if (ses->capabilities & CAP_DFS) {
3823 smb_buffer->Flags2 |= SMBFLG2_DFS;
3825 if (ses->capabilities & CAP_UNICODE) {
3826 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3828 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3829 6 /* max utf8 char length in bytes */ *
3830 (/* server len*/ + 256 /* share len */), nls_codepage);
3831 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
3832 bcc_ptr += 2; /* skip trailing null */
3833 } else { /* ASCII */
3834 strcpy(bcc_ptr, tree);
3835 bcc_ptr += strlen(tree) + 1;
3837 strcpy(bcc_ptr, "?????");
3838 bcc_ptr += strlen("?????");
3840 count = bcc_ptr - &pSMB->Password[0];
3841 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
3842 pSMB->ByteCount = cpu_to_le16(count);
3844 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3847 /* above now done in SendReceive */
3851 tcon->tid = smb_buffer_response->Tid;
3852 bcc_ptr = pByteArea(smb_buffer_response);
3853 bytes_left = get_bcc(smb_buffer_response);
3854 length = strnlen(bcc_ptr, bytes_left - 2);
3855 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3861 /* skip service field (NB: this field is always ASCII) */
3863 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3864 (bcc_ptr[2] == 'C')) {
3865 cifs_dbg(FYI, "IPC connection\n");
3869 } else if (length == 2) {
3870 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3871 /* the most common case */
3872 cifs_dbg(FYI, "disk share connection\n");
3875 bcc_ptr += length + 1;
3876 bytes_left -= (length + 1);
3877 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
3879 /* mostly informational -- no need to fail on error here */
3880 kfree(tcon->nativeFileSystem);
3881 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
3882 bytes_left, is_unicode,
3885 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
3887 if ((smb_buffer_response->WordCount == 3) ||
3888 (smb_buffer_response->WordCount == 7))
3889 /* field is in same location */
3890 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3893 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
3896 cifs_buf_release(smb_buffer);
3900 static void delayed_free(struct rcu_head *p)
3902 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3904 unload_nls(cifs_sb->local_nls);
3905 smb3_cleanup_fs_context(cifs_sb->ctx);
3910 cifs_umount(struct cifs_sb_info *cifs_sb)
3912 struct rb_root *root = &cifs_sb->tlink_tree;
3913 struct rb_node *node;
3914 struct tcon_link *tlink;
3916 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3918 spin_lock(&cifs_sb->tlink_tree_lock);
3919 while ((node = rb_first(root))) {
3920 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3921 cifs_get_tlink(tlink);
3922 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3923 rb_erase(node, root);
3925 spin_unlock(&cifs_sb->tlink_tree_lock);
3926 cifs_put_tlink(tlink);
3927 spin_lock(&cifs_sb->tlink_tree_lock);
3929 spin_unlock(&cifs_sb->tlink_tree_lock);
3931 kfree(cifs_sb->prepath);
3932 #ifdef CONFIG_CIFS_DFS_UPCALL
3933 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
3935 call_rcu(&cifs_sb->rcu, delayed_free);
3939 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
3940 struct TCP_Server_Info *server)
3944 if (!server->ops->need_neg || !server->ops->negotiate)
3947 /* only send once per connect */
3948 spin_lock(&cifs_tcp_ses_lock);
3949 if (!server->ops->need_neg(server) ||
3950 server->tcpStatus != CifsNeedNegotiate) {
3951 spin_unlock(&cifs_tcp_ses_lock);
3954 server->tcpStatus = CifsInNegotiate;
3955 spin_unlock(&cifs_tcp_ses_lock);
3957 rc = server->ops->negotiate(xid, ses, server);
3959 spin_lock(&cifs_tcp_ses_lock);
3960 if (server->tcpStatus == CifsInNegotiate)
3961 server->tcpStatus = CifsGood;
3964 spin_unlock(&cifs_tcp_ses_lock);
3966 spin_lock(&cifs_tcp_ses_lock);
3967 if (server->tcpStatus == CifsInNegotiate)
3968 server->tcpStatus = CifsNeedNegotiate;
3969 spin_unlock(&cifs_tcp_ses_lock);
3976 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3977 struct TCP_Server_Info *server,
3978 struct nls_table *nls_info)
3981 bool is_binding = false;
3984 spin_lock(&cifs_tcp_ses_lock);
3985 if (ses->ses_status != SES_GOOD &&
3986 ses->ses_status != SES_NEW &&
3987 ses->ses_status != SES_NEED_RECON) {
3988 spin_unlock(&cifs_tcp_ses_lock);
3992 /* only send once per connect */
3993 spin_lock(&ses->chan_lock);
3994 if (CIFS_ALL_CHANS_GOOD(ses) ||
3995 cifs_chan_in_reconnect(ses, server)) {
3996 spin_unlock(&ses->chan_lock);
3997 spin_unlock(&cifs_tcp_ses_lock);
4000 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
4001 cifs_chan_set_in_reconnect(ses, server);
4002 spin_unlock(&ses->chan_lock);
4005 ses->ses_status = SES_IN_SETUP;
4006 spin_unlock(&cifs_tcp_ses_lock);
4009 ses->capabilities = server->capabilities;
4010 if (!linuxExtEnabled)
4011 ses->capabilities &= (~server->vals->cap_unix);
4013 if (ses->auth_key.response) {
4014 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
4015 ses->auth_key.response);
4016 kfree(ses->auth_key.response);
4017 ses->auth_key.response = NULL;
4018 ses->auth_key.len = 0;
4022 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
4023 server->sec_mode, server->capabilities, server->timeAdj);
4025 if (server->ops->sess_setup)
4026 rc = server->ops->sess_setup(xid, ses, server, nls_info);
4029 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
4030 spin_lock(&cifs_tcp_ses_lock);
4031 if (ses->ses_status == SES_IN_SETUP)
4032 ses->ses_status = SES_NEED_RECON;
4033 spin_lock(&ses->chan_lock);
4034 cifs_chan_clear_in_reconnect(ses, server);
4035 spin_unlock(&ses->chan_lock);
4036 spin_unlock(&cifs_tcp_ses_lock);
4038 spin_lock(&cifs_tcp_ses_lock);
4039 if (ses->ses_status == SES_IN_SETUP)
4040 ses->ses_status = SES_GOOD;
4041 spin_lock(&ses->chan_lock);
4042 cifs_chan_clear_in_reconnect(ses, server);
4043 cifs_chan_clear_need_reconnect(ses, server);
4044 spin_unlock(&ses->chan_lock);
4045 spin_unlock(&cifs_tcp_ses_lock);
4052 cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
4054 ctx->sectype = ses->sectype;
4056 /* krb5 is special, since we don't need username or pw */
4057 if (ctx->sectype == Kerberos)
4060 return cifs_set_cifscreds(ctx, ses);
4063 static struct cifs_tcon *
4064 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
4067 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4068 struct cifs_ses *ses;
4069 struct cifs_tcon *tcon = NULL;
4070 struct smb3_fs_context *ctx;
4072 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
4074 return ERR_PTR(-ENOMEM);
4076 ctx->local_nls = cifs_sb->local_nls;
4077 ctx->linux_uid = fsuid;
4078 ctx->cred_uid = fsuid;
4079 ctx->UNC = master_tcon->treeName;
4080 ctx->retry = master_tcon->retry;
4081 ctx->nocase = master_tcon->nocase;
4082 ctx->nohandlecache = master_tcon->nohandlecache;
4083 ctx->local_lease = master_tcon->local_lease;
4084 ctx->no_lease = master_tcon->no_lease;
4085 ctx->resilient = master_tcon->use_resilient;
4086 ctx->persistent = master_tcon->use_persistent;
4087 ctx->handle_timeout = master_tcon->handle_timeout;
4088 ctx->no_linux_ext = !master_tcon->unix_ext;
4089 ctx->linux_ext = master_tcon->posix_extensions;
4090 ctx->sectype = master_tcon->ses->sectype;
4091 ctx->sign = master_tcon->ses->sign;
4092 ctx->seal = master_tcon->seal;
4093 ctx->witness = master_tcon->use_witness;
4095 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
4101 /* get a reference for the same TCP session */
4102 spin_lock(&cifs_tcp_ses_lock);
4103 ++master_tcon->ses->server->srv_count;
4104 spin_unlock(&cifs_tcp_ses_lock);
4106 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
4108 tcon = (struct cifs_tcon *)ses;
4109 cifs_put_tcp_session(master_tcon->ses->server, 0);
4113 tcon = cifs_get_tcon(ses, ctx);
4115 cifs_put_smb_ses(ses);
4120 reset_cifs_unix_caps(0, tcon, NULL, ctx);
4123 kfree(ctx->username);
4124 kfree_sensitive(ctx->password);
4131 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4133 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4136 /* find and return a tlink with given uid */
4137 static struct tcon_link *
4138 tlink_rb_search(struct rb_root *root, kuid_t uid)
4140 struct rb_node *node = root->rb_node;
4141 struct tcon_link *tlink;
4144 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4146 if (uid_gt(tlink->tl_uid, uid))
4147 node = node->rb_left;
4148 else if (uid_lt(tlink->tl_uid, uid))
4149 node = node->rb_right;
4156 /* insert a tcon_link into the tree */
4158 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4160 struct rb_node **new = &(root->rb_node), *parent = NULL;
4161 struct tcon_link *tlink;
4164 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4167 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4168 new = &((*new)->rb_left);
4170 new = &((*new)->rb_right);
4173 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4174 rb_insert_color(&new_tlink->tl_rbnode, root);
4178 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4181 * If the superblock doesn't refer to a multiuser mount, then just return
4182 * the master tcon for the mount.
4184 * First, search the rbtree for an existing tcon for this fsuid. If one
4185 * exists, then check to see if it's pending construction. If it is then wait
4186 * for construction to complete. Once it's no longer pending, check to see if
4187 * it failed and either return an error or retry construction, depending on
4190 * If one doesn't exist then insert a new tcon_link struct into the tree and
4191 * try to construct a new one.
4194 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4197 kuid_t fsuid = current_fsuid();
4198 struct tcon_link *tlink, *newtlink;
4200 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4201 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4203 spin_lock(&cifs_sb->tlink_tree_lock);
4204 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4206 cifs_get_tlink(tlink);
4207 spin_unlock(&cifs_sb->tlink_tree_lock);
4209 if (tlink == NULL) {
4210 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4211 if (newtlink == NULL)
4212 return ERR_PTR(-ENOMEM);
4213 newtlink->tl_uid = fsuid;
4214 newtlink->tl_tcon = ERR_PTR(-EACCES);
4215 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4216 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4217 cifs_get_tlink(newtlink);
4219 spin_lock(&cifs_sb->tlink_tree_lock);
4220 /* was one inserted after previous search? */
4221 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4223 cifs_get_tlink(tlink);
4224 spin_unlock(&cifs_sb->tlink_tree_lock);
4226 goto wait_for_construction;
4229 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4230 spin_unlock(&cifs_sb->tlink_tree_lock);
4232 wait_for_construction:
4233 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4234 TASK_INTERRUPTIBLE);
4236 cifs_put_tlink(tlink);
4237 return ERR_PTR(-ERESTARTSYS);
4240 /* if it's good, return it */
4241 if (!IS_ERR(tlink->tl_tcon))
4244 /* return error if we tried this already recently */
4245 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4246 cifs_put_tlink(tlink);
4247 return ERR_PTR(-EACCES);
4250 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4251 goto wait_for_construction;
4254 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4255 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4256 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4258 if (IS_ERR(tlink->tl_tcon)) {
4259 cifs_put_tlink(tlink);
4260 return ERR_PTR(-EACCES);
4267 * periodic workqueue job that scans tcon_tree for a superblock and closes
4271 cifs_prune_tlinks(struct work_struct *work)
4273 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4275 struct rb_root *root = &cifs_sb->tlink_tree;
4276 struct rb_node *node;
4277 struct rb_node *tmp;
4278 struct tcon_link *tlink;
4281 * Because we drop the spinlock in the loop in order to put the tlink
4282 * it's not guarded against removal of links from the tree. The only
4283 * places that remove entries from the tree are this function and
4284 * umounts. Because this function is non-reentrant and is canceled
4285 * before umount can proceed, this is safe.
4287 spin_lock(&cifs_sb->tlink_tree_lock);
4288 node = rb_first(root);
4289 while (node != NULL) {
4291 node = rb_next(tmp);
4292 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4294 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4295 atomic_read(&tlink->tl_count) != 0 ||
4296 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4299 cifs_get_tlink(tlink);
4300 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4301 rb_erase(tmp, root);
4303 spin_unlock(&cifs_sb->tlink_tree_lock);
4304 cifs_put_tlink(tlink);
4305 spin_lock(&cifs_sb->tlink_tree_lock);
4307 spin_unlock(&cifs_sb->tlink_tree_lock);
4309 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4313 #ifdef CONFIG_CIFS_DFS_UPCALL
4314 /* Update dfs referral path of superblock */
4315 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
4319 size_t len = strlen(target);
4320 char *refpath, *npath;
4322 if (unlikely(len < 2 || *target != '\\'))
4325 if (target[1] == '\\') {
4327 refpath = kmalloc(len, GFP_KERNEL);
4331 scnprintf(refpath, len, "%s", target);
4333 len += sizeof("\\");
4334 refpath = kmalloc(len, GFP_KERNEL);
4338 scnprintf(refpath, len, "\\%s", target);
4341 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
4344 if (IS_ERR(npath)) {
4345 rc = PTR_ERR(npath);
4347 mutex_lock(&server->refpath_lock);
4348 kfree(server->leaf_fullpath);
4349 server->leaf_fullpath = npath;
4350 mutex_unlock(&server->refpath_lock);
4351 server->current_fullpath = server->leaf_fullpath;
4356 static int target_share_matches_server(struct TCP_Server_Info *server, const char *tcp_host,
4357 size_t tcp_host_len, char *share, bool *target_match)
4360 const char *dfs_host;
4361 size_t dfs_host_len;
4363 *target_match = true;
4364 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4366 /* Check if hostnames or addresses match */
4367 if (dfs_host_len != tcp_host_len || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4368 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4369 dfs_host, (int)tcp_host_len, tcp_host);
4370 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
4372 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4377 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
4378 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4379 struct dfs_cache_tgt_list *tl)
4382 struct TCP_Server_Info *server = tcon->ses->server;
4383 const struct smb_version_operations *ops = server->ops;
4384 struct cifs_tcon *ipc = tcon->ses->tcon_ipc;
4385 char *share = NULL, *prefix = NULL;
4386 const char *tcp_host;
4387 size_t tcp_host_len;
4388 struct dfs_cache_tgt_iterator *tit;
4391 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4393 tit = dfs_cache_get_tgt_iterator(tl);
4399 /* Try to tree connect to all dfs targets */
4400 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
4401 const char *target = dfs_cache_get_tgt_name(tit);
4402 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
4406 share = prefix = NULL;
4408 /* Check if share matches with tcp ses */
4409 rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix);
4411 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
4415 rc = target_share_matches_server(server, tcp_host, tcp_host_len, share,
4419 if (!target_match) {
4424 if (ipc->need_reconnect) {
4425 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4426 rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls);
4431 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
4433 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4437 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
4438 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
4439 * reconnect so either the demultiplex thread or the echo worker will reconnect to
4440 * newly resolved target.
4442 if (dfs_cache_find(xid, tcon->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
4444 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4447 rc = dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit);
4449 rc = cifs_update_super_prepath(cifs_sb, prefix);
4451 /* Target is another dfs share */
4452 rc = update_server_fullpath(server, cifs_sb, target);
4453 dfs_cache_free_tgts(tl);
4457 list_replace_init(&ntl.tl_list, &tl->tl_list);
4459 dfs_cache_free_tgts(&ntl);
4471 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
4472 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4473 struct dfs_cache_tgt_list *tl)
4477 struct TCP_Server_Info *server = tcon->ses->server;
4480 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
4481 if (!rc || rc != -EREMOTE)
4483 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
4485 * If we couldn't tree connect to any targets from last referral path, then retry from
4486 * original referral path.
4488 if (rc && server->current_fullpath != server->origin_fullpath) {
4489 server->current_fullpath = server->origin_fullpath;
4490 cifs_signal_cifsd_for_reconnect(server, true);
4493 dfs_cache_free_tgts(tl);
4497 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4500 struct TCP_Server_Info *server = tcon->ses->server;
4501 const struct smb_version_operations *ops = server->ops;
4502 struct super_block *sb = NULL;
4503 struct cifs_sb_info *cifs_sb;
4504 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
4506 struct dfs_info3_param ref = {0};
4508 /* only send once per connect */
4509 spin_lock(&cifs_tcp_ses_lock);
4510 if (tcon->ses->ses_status != SES_GOOD ||
4511 (tcon->status != TID_NEW &&
4512 tcon->status != TID_NEED_TCON)) {
4513 spin_unlock(&cifs_tcp_ses_lock);
4516 tcon->status = TID_IN_TCON;
4517 spin_unlock(&cifs_tcp_ses_lock);
4519 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4526 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4527 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4531 sb = cifs_get_tcp_super(server);
4534 cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
4538 cifs_sb = CIFS_SB(sb);
4540 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
4541 if (!server->current_fullpath ||
4542 dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) {
4543 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, cifs_sb->local_nls);
4547 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
4549 free_dfs_info_param(&ref);
4553 cifs_put_tcp_super(sb);
4556 spin_lock(&cifs_tcp_ses_lock);
4557 if (tcon->status == TID_IN_TCON)
4558 tcon->status = TID_NEED_TCON;
4559 spin_unlock(&cifs_tcp_ses_lock);
4561 spin_lock(&cifs_tcp_ses_lock);
4562 if (tcon->status == TID_IN_TCON)
4563 tcon->status = TID_GOOD;
4564 spin_unlock(&cifs_tcp_ses_lock);
4565 tcon->need_reconnect = false;
4571 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4574 const struct smb_version_operations *ops = tcon->ses->server->ops;
4576 /* only send once per connect */
4577 spin_lock(&cifs_tcp_ses_lock);
4578 if (tcon->ses->ses_status != SES_GOOD ||
4579 (tcon->status != TID_NEW &&
4580 tcon->status != TID_NEED_TCON)) {
4581 spin_unlock(&cifs_tcp_ses_lock);
4584 tcon->status = TID_IN_TCON;
4585 spin_unlock(&cifs_tcp_ses_lock);
4587 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
4589 spin_lock(&cifs_tcp_ses_lock);
4590 if (tcon->status == TID_IN_TCON)
4591 tcon->status = TID_NEED_TCON;
4592 spin_unlock(&cifs_tcp_ses_lock);
4594 spin_lock(&cifs_tcp_ses_lock);
4595 if (tcon->status == TID_IN_TCON)
4596 tcon->status = TID_GOOD;
4597 spin_unlock(&cifs_tcp_ses_lock);
4598 tcon->need_reconnect = false;