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 /* if server hostname isn't populated, there's nothing to do here */
101 if (server->hostname[0] == '\0')
104 len = strlen(server->hostname) + 3;
106 unc = kmalloc(len, GFP_KERNEL);
108 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
111 scnprintf(unc, len, "\\\\%s", server->hostname);
113 rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry);
117 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
118 __func__, server->hostname, rc);
119 goto requeue_resolve;
122 spin_lock(&server->srv_lock);
123 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
125 spin_unlock(&server->srv_lock);
128 /* rc == 1 means success here */
130 now = ktime_get_real_seconds();
131 if (expiry && expiry > now)
133 * To make sure we don't use the cached entry, retry 1s
136 ttl = max_t(unsigned long, expiry - now, SMB_DNS_RESOLVE_INTERVAL_MIN) + 1;
141 cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n",
143 mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ));
148 static void smb2_query_server_interfaces(struct work_struct *work)
151 struct cifs_tcon *tcon = container_of(work,
153 query_interfaces.work);
156 * query server network interfaces, in case they change
158 rc = SMB3_request_interfaces(0, tcon, false);
160 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
164 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
165 (SMB_INTERFACE_POLL_INTERVAL * HZ));
168 static void cifs_resolve_server(struct work_struct *work)
171 struct TCP_Server_Info *server = container_of(work,
172 struct TCP_Server_Info, resolve.work);
174 cifs_server_lock(server);
177 * Resolve the hostname again to make sure that IP address is up-to-date.
179 rc = reconn_set_ipaddr_from_hostname(server);
181 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
185 cifs_server_unlock(server);
189 * Update the tcpStatus for the server.
190 * This is used to signal the cifsd thread to call cifs_reconnect
191 * ONLY cifsd thread should call cifs_reconnect. For any other
192 * thread, use this function
194 * @server: the tcp ses for which reconnect is needed
195 * @all_channels: if this needs to be done for all channels
198 cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
201 struct TCP_Server_Info *pserver;
202 struct cifs_ses *ses;
205 /* If server is a channel, select the primary channel */
206 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
208 spin_lock(&pserver->srv_lock);
210 pserver->tcpStatus = CifsNeedReconnect;
211 spin_unlock(&pserver->srv_lock);
214 spin_unlock(&pserver->srv_lock);
216 spin_lock(&cifs_tcp_ses_lock);
217 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
218 spin_lock(&ses->chan_lock);
219 for (i = 0; i < ses->chan_count; i++) {
220 spin_lock(&ses->chans[i].server->srv_lock);
221 ses->chans[i].server->tcpStatus = CifsNeedReconnect;
222 spin_unlock(&ses->chans[i].server->srv_lock);
224 spin_unlock(&ses->chan_lock);
226 spin_unlock(&cifs_tcp_ses_lock);
230 * Mark all sessions and tcons for reconnect.
231 * IMPORTANT: make sure that this gets called only from
232 * cifsd thread. For any other thread, use
233 * cifs_signal_cifsd_for_reconnect
235 * @server: the tcp ses for which reconnect is needed
236 * @server needs to be previously set to CifsNeedReconnect.
237 * @mark_smb_session: whether even sessions need to be marked
240 cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
241 bool mark_smb_session)
243 struct TCP_Server_Info *pserver;
244 struct cifs_ses *ses, *nses;
245 struct cifs_tcon *tcon;
248 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
249 * are not used until reconnected.
251 cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
253 /* If server is a channel, select the primary channel */
254 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
257 spin_lock(&cifs_tcp_ses_lock);
258 list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
259 /* check if iface is still active */
260 if (!cifs_chan_is_iface_active(ses, server))
261 cifs_chan_update_iface(ses, server);
263 spin_lock(&ses->chan_lock);
264 if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server))
267 if (mark_smb_session)
268 CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
270 cifs_chan_set_need_reconnect(ses, server);
272 /* If all channels need reconnect, then tcon needs reconnect */
273 if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses))
276 ses->ses_status = SES_NEED_RECON;
278 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
279 tcon->need_reconnect = true;
280 tcon->status = TID_NEED_RECON;
283 ses->tcon_ipc->need_reconnect = true;
286 spin_unlock(&ses->chan_lock);
288 spin_unlock(&cifs_tcp_ses_lock);
292 cifs_abort_connection(struct TCP_Server_Info *server)
294 struct mid_q_entry *mid, *nmid;
295 struct list_head retry_list;
298 server->max_read = 0;
300 /* do not want to be sending data on a socket we are freeing */
301 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
302 cifs_server_lock(server);
303 if (server->ssocket) {
304 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
305 server->ssocket->flags);
306 kernel_sock_shutdown(server->ssocket, SHUT_WR);
307 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
308 server->ssocket->flags);
309 sock_release(server->ssocket);
310 server->ssocket = NULL;
312 server->sequence_number = 0;
313 server->session_estab = false;
314 kfree_sensitive(server->session_key.response);
315 server->session_key.response = NULL;
316 server->session_key.len = 0;
317 server->lstrp = jiffies;
319 /* mark submitted MIDs for retry and issue callback */
320 INIT_LIST_HEAD(&retry_list);
321 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
322 spin_lock(&server->mid_lock);
323 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
324 kref_get(&mid->refcount);
325 if (mid->mid_state == MID_REQUEST_SUBMITTED)
326 mid->mid_state = MID_RETRY_NEEDED;
327 list_move(&mid->qhead, &retry_list);
328 mid->mid_flags |= MID_DELETED;
330 spin_unlock(&server->mid_lock);
331 cifs_server_unlock(server);
333 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
334 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
335 list_del_init(&mid->qhead);
340 if (cifs_rdma_enabled(server)) {
341 cifs_server_lock(server);
342 smbd_destroy(server);
343 cifs_server_unlock(server);
347 static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
349 spin_lock(&server->srv_lock);
350 server->nr_targets = num_targets;
351 if (server->tcpStatus == CifsExiting) {
352 /* the demux thread will exit normally next time through the loop */
353 spin_unlock(&server->srv_lock);
354 wake_up(&server->response_q);
358 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
359 trace_smb3_reconnect(server->CurrentMid, server->conn_id,
361 server->tcpStatus = CifsNeedReconnect;
363 spin_unlock(&server->srv_lock);
368 * cifs tcp session reconnection
370 * mark tcp session as reconnecting so temporarily locked
371 * mark all smb sessions as reconnecting for tcp session
372 * reconnect tcp session
373 * wake up waiters on reconnection? - (not needed currently)
375 * if mark_smb_session is passed as true, unconditionally mark
376 * the smb session (and tcon) for reconnect as well. This value
377 * doesn't really matter for non-multichannel scenario.
380 static int __cifs_reconnect(struct TCP_Server_Info *server,
381 bool mark_smb_session)
385 if (!cifs_tcp_ses_needs_reconnect(server, 1))
388 cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
390 cifs_abort_connection(server);
394 cifs_server_lock(server);
396 if (!cifs_swn_set_server_dstaddr(server)) {
397 /* resolve the hostname again to make sure that IP address is up-to-date */
398 rc = reconn_set_ipaddr_from_hostname(server);
399 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
402 if (cifs_rdma_enabled(server))
403 rc = smbd_reconnect(server);
405 rc = generic_ip_connect(server);
407 cifs_server_unlock(server);
408 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
411 atomic_inc(&tcpSesReconnectCount);
412 set_credits(server, 1);
413 spin_lock(&server->srv_lock);
414 if (server->tcpStatus != CifsExiting)
415 server->tcpStatus = CifsNeedNegotiate;
416 spin_unlock(&server->srv_lock);
417 cifs_swn_reset_server_dstaddr(server);
418 cifs_server_unlock(server);
419 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
421 } while (server->tcpStatus == CifsNeedReconnect);
423 spin_lock(&server->srv_lock);
424 if (server->tcpStatus == CifsNeedNegotiate)
425 mod_delayed_work(cifsiod_wq, &server->echo, 0);
426 spin_unlock(&server->srv_lock);
428 wake_up(&server->response_q);
432 #ifdef CONFIG_CIFS_DFS_UPCALL
433 static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
438 if (!cifs_swn_set_server_dstaddr(server)) {
439 if (server->hostname != target) {
440 hostname = extract_hostname(target);
441 if (!IS_ERR(hostname)) {
442 kfree(server->hostname);
443 server->hostname = hostname;
445 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
446 __func__, PTR_ERR(hostname));
447 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
451 /* resolve the hostname again to make sure that IP address is up-to-date. */
452 rc = reconn_set_ipaddr_from_hostname(server);
453 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
455 /* Reconnect the socket */
456 if (cifs_rdma_enabled(server))
457 rc = smbd_reconnect(server);
459 rc = generic_ip_connect(server);
464 static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
465 struct dfs_cache_tgt_iterator **target_hint)
468 struct dfs_cache_tgt_iterator *tit;
472 /* If dfs target list is empty, then reconnect to last server */
473 tit = dfs_cache_get_tgt_iterator(tl);
475 return __reconnect_target_unlocked(server, server->hostname);
477 /* Otherwise, try every dfs target in @tl */
478 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
479 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
488 static int reconnect_dfs_server(struct TCP_Server_Info *server)
491 const char *refpath = server->current_fullpath + 1;
492 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
493 struct dfs_cache_tgt_iterator *target_hint = NULL;
497 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
499 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
500 * targets (server->nr_targets). It's also possible that the cached referral was cleared
501 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
502 * refreshing the referral, so, in this case, default it to 1.
504 if (!dfs_cache_noreq_find(refpath, NULL, &tl))
505 num_targets = dfs_cache_get_nr_tgts(&tl);
509 if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
513 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
514 * different server or share during failover. It could be improved by adding some logic to
515 * only do that in case it connects to a different server or share, though.
517 cifs_mark_tcp_ses_conns_for_reconnect(server, true);
519 cifs_abort_connection(server);
523 cifs_server_lock(server);
525 rc = reconnect_target_unlocked(server, &tl, &target_hint);
527 /* Failed to reconnect socket */
528 cifs_server_unlock(server);
529 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
534 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
535 * process waiting for reconnect will know it needs to re-establish session and tcon
536 * through the reconnected target server.
538 atomic_inc(&tcpSesReconnectCount);
539 set_credits(server, 1);
540 spin_lock(&server->srv_lock);
541 if (server->tcpStatus != CifsExiting)
542 server->tcpStatus = CifsNeedNegotiate;
543 spin_unlock(&server->srv_lock);
544 cifs_swn_reset_server_dstaddr(server);
545 cifs_server_unlock(server);
546 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
547 } while (server->tcpStatus == CifsNeedReconnect);
550 dfs_cache_noreq_update_tgthint(refpath, target_hint);
552 dfs_cache_free_tgts(&tl);
554 /* Need to set up echo worker again once connection has been established */
555 spin_lock(&server->srv_lock);
556 if (server->tcpStatus == CifsNeedNegotiate)
557 mod_delayed_work(cifsiod_wq, &server->echo, 0);
558 spin_unlock(&server->srv_lock);
560 wake_up(&server->response_q);
564 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
566 /* If tcp session is not an dfs connection, then reconnect to last target server */
567 spin_lock(&server->srv_lock);
568 if (!server->is_dfs_conn) {
569 spin_unlock(&server->srv_lock);
570 return __cifs_reconnect(server, mark_smb_session);
572 spin_unlock(&server->srv_lock);
574 mutex_lock(&server->refpath_lock);
575 if (!server->origin_fullpath || !server->leaf_fullpath) {
576 mutex_unlock(&server->refpath_lock);
577 return __cifs_reconnect(server, mark_smb_session);
579 mutex_unlock(&server->refpath_lock);
581 return reconnect_dfs_server(server);
584 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
586 return __cifs_reconnect(server, mark_smb_session);
591 cifs_echo_request(struct work_struct *work)
594 struct TCP_Server_Info *server = container_of(work,
595 struct TCP_Server_Info, echo.work);
598 * We cannot send an echo if it is disabled.
599 * Also, no need to ping if we got a response recently.
602 if (server->tcpStatus == CifsNeedReconnect ||
603 server->tcpStatus == CifsExiting ||
604 server->tcpStatus == CifsNew ||
605 (server->ops->can_echo && !server->ops->can_echo(server)) ||
606 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
609 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
611 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
614 /* Check witness registrations */
618 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
622 allocate_buffers(struct TCP_Server_Info *server)
624 if (!server->bigbuf) {
625 server->bigbuf = (char *)cifs_buf_get();
626 if (!server->bigbuf) {
627 cifs_server_dbg(VFS, "No memory for large SMB response\n");
629 /* retry will check if exiting */
632 } else if (server->large_buf) {
633 /* we are reusing a dirty large buf, clear its start */
634 memset(server->bigbuf, 0, HEADER_SIZE(server));
637 if (!server->smallbuf) {
638 server->smallbuf = (char *)cifs_small_buf_get();
639 if (!server->smallbuf) {
640 cifs_server_dbg(VFS, "No memory for SMB response\n");
642 /* retry will check if exiting */
645 /* beginning of smb buffer is cleared in our buf_get */
647 /* if existing small buf clear beginning */
648 memset(server->smallbuf, 0, HEADER_SIZE(server));
655 server_unresponsive(struct TCP_Server_Info *server)
658 * We need to wait 3 echo intervals to make sure we handle such
660 * 1s client sends a normal SMB request
661 * 2s client gets a response
662 * 30s echo workqueue job pops, and decides we got a response recently
663 * and don't need to send another
665 * 65s kernel_recvmsg times out, and we see that we haven't gotten
666 * a response in >60s.
668 spin_lock(&server->srv_lock);
669 if ((server->tcpStatus == CifsGood ||
670 server->tcpStatus == CifsNeedNegotiate) &&
671 (!server->ops->can_echo || server->ops->can_echo(server)) &&
672 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
673 spin_unlock(&server->srv_lock);
674 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
675 (3 * server->echo_interval) / HZ);
676 cifs_reconnect(server, false);
679 spin_unlock(&server->srv_lock);
685 zero_credits(struct TCP_Server_Info *server)
689 spin_lock(&server->req_lock);
690 val = server->credits + server->echo_credits + server->oplock_credits;
691 if (server->in_flight == 0 && val == 0) {
692 spin_unlock(&server->req_lock);
695 spin_unlock(&server->req_lock);
700 cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
705 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
708 /* reconnect if no credits and no requests in flight */
709 if (zero_credits(server)) {
710 cifs_reconnect(server, false);
711 return -ECONNABORTED;
714 if (server_unresponsive(server))
715 return -ECONNABORTED;
716 if (cifs_rdma_enabled(server) && server->smbd_conn)
717 length = smbd_recv(server->smbd_conn, smb_msg);
719 length = sock_recvmsg(server->ssocket, smb_msg, 0);
721 spin_lock(&server->srv_lock);
722 if (server->tcpStatus == CifsExiting) {
723 spin_unlock(&server->srv_lock);
727 if (server->tcpStatus == CifsNeedReconnect) {
728 spin_unlock(&server->srv_lock);
729 cifs_reconnect(server, false);
730 return -ECONNABORTED;
732 spin_unlock(&server->srv_lock);
734 if (length == -ERESTARTSYS ||
738 * Minimum sleep to prevent looping, allowing socket
739 * to clear and app threads to set tcpStatus
740 * CifsNeedReconnect if server hung.
742 usleep_range(1000, 2000);
748 cifs_dbg(FYI, "Received no data or error: %d\n", length);
749 cifs_reconnect(server, false);
750 return -ECONNABORTED;
757 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
758 unsigned int to_read)
760 struct msghdr smb_msg = {};
761 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
762 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
764 return cifs_readv_from_socket(server, &smb_msg);
768 cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
770 struct msghdr smb_msg = {};
773 * iov_iter_discard already sets smb_msg.type and count and iov_offset
774 * and cifs_readv_from_socket sets msg_control and msg_controllen
775 * so little to initialize in struct msghdr
777 iov_iter_discard(&smb_msg.msg_iter, READ, to_read);
779 return cifs_readv_from_socket(server, &smb_msg);
783 cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
784 unsigned int page_offset, unsigned int to_read)
786 struct msghdr smb_msg = {};
787 struct bio_vec bv = {
788 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
789 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
790 return cifs_readv_from_socket(server, &smb_msg);
794 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
797 * The first byte big endian of the length field,
798 * is actually not part of the length but the type
799 * with the most common, zero, as regular data.
802 case RFC1002_SESSION_MESSAGE:
803 /* Regular SMB response */
805 case RFC1002_SESSION_KEEP_ALIVE:
806 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
808 case RFC1002_POSITIVE_SESSION_RESPONSE:
809 cifs_dbg(FYI, "RFC 1002 positive session response\n");
811 case RFC1002_NEGATIVE_SESSION_RESPONSE:
813 * We get this from Windows 98 instead of an error on
814 * SMB negprot response.
816 cifs_dbg(FYI, "RFC 1002 negative session response\n");
817 /* give server a second to clean up */
820 * Always try 445 first on reconnect since we get NACK
821 * on some if we ever connected to port 139 (the NACK
822 * is since we do not begin with RFC1001 session
825 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
826 cifs_reconnect(server, true);
829 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
830 cifs_reconnect(server, true);
837 dequeue_mid(struct mid_q_entry *mid, bool malformed)
839 #ifdef CONFIG_CIFS_STATS2
840 mid->when_received = jiffies;
842 spin_lock(&mid->server->mid_lock);
844 mid->mid_state = MID_RESPONSE_RECEIVED;
846 mid->mid_state = MID_RESPONSE_MALFORMED;
848 * Trying to handle/dequeue a mid after the send_recv()
849 * function has finished processing it is a bug.
851 if (mid->mid_flags & MID_DELETED) {
852 spin_unlock(&mid->server->mid_lock);
853 pr_warn_once("trying to dequeue a deleted mid\n");
855 list_del_init(&mid->qhead);
856 mid->mid_flags |= MID_DELETED;
857 spin_unlock(&mid->server->mid_lock);
862 smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
864 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
867 * SMB1 does not use credits.
872 return le16_to_cpu(shdr->CreditRequest);
876 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
877 char *buf, int malformed)
879 if (server->ops->check_trans2 &&
880 server->ops->check_trans2(mid, server, buf, malformed))
882 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
884 mid->large_buf = server->large_buf;
885 /* Was previous buf put in mpx struct for multi-rsp? */
886 if (!mid->multiRsp) {
887 /* smb buffer will be freed by user thread */
888 if (server->large_buf)
889 server->bigbuf = NULL;
891 server->smallbuf = NULL;
893 dequeue_mid(mid, malformed);
897 cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
899 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
900 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
901 bool mnt_sign_enabled;
904 * Is signing required by mnt options? If not then check
905 * global_secflags to see if it is there.
907 if (!mnt_sign_required)
908 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
912 * If signing is required then it's automatically enabled too,
913 * otherwise, check to see if the secflags allow it.
915 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
916 (global_secflags & CIFSSEC_MAY_SIGN);
918 /* If server requires signing, does client allow it? */
919 if (srv_sign_required) {
920 if (!mnt_sign_enabled) {
921 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
927 /* If client requires signing, does server allow it? */
928 if (mnt_sign_required) {
929 if (!srv_sign_enabled) {
930 cifs_dbg(VFS, "Server does not support signing!\n");
936 if (cifs_rdma_enabled(server) && server->sign)
937 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
943 static void clean_demultiplex_info(struct TCP_Server_Info *server)
947 /* take it off the list, if it's not already */
948 spin_lock(&server->srv_lock);
949 list_del_init(&server->tcp_ses_list);
950 spin_unlock(&server->srv_lock);
952 cancel_delayed_work_sync(&server->echo);
953 cancel_delayed_work_sync(&server->resolve);
955 spin_lock(&server->srv_lock);
956 server->tcpStatus = CifsExiting;
957 spin_unlock(&server->srv_lock);
958 wake_up_all(&server->response_q);
960 /* check if we have blocked requests that need to free */
961 spin_lock(&server->req_lock);
962 if (server->credits <= 0)
964 spin_unlock(&server->req_lock);
966 * Although there should not be any requests blocked on this queue it
967 * can not hurt to be paranoid and try to wake up requests that may
968 * haven been blocked when more than 50 at time were on the wire to the
969 * same server - they now will see the session is in exit state and get
970 * out of SendReceive.
972 wake_up_all(&server->request_q);
973 /* give those requests time to exit */
975 if (cifs_rdma_enabled(server))
976 smbd_destroy(server);
977 if (server->ssocket) {
978 sock_release(server->ssocket);
979 server->ssocket = NULL;
982 if (!list_empty(&server->pending_mid_q)) {
983 struct list_head dispose_list;
984 struct mid_q_entry *mid_entry;
985 struct list_head *tmp, *tmp2;
987 INIT_LIST_HEAD(&dispose_list);
988 spin_lock(&server->mid_lock);
989 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
990 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
991 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
992 kref_get(&mid_entry->refcount);
993 mid_entry->mid_state = MID_SHUTDOWN;
994 list_move(&mid_entry->qhead, &dispose_list);
995 mid_entry->mid_flags |= MID_DELETED;
997 spin_unlock(&server->mid_lock);
999 /* now walk dispose list and issue callbacks */
1000 list_for_each_safe(tmp, tmp2, &dispose_list) {
1001 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
1002 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
1003 list_del_init(&mid_entry->qhead);
1004 mid_entry->callback(mid_entry);
1005 release_mid(mid_entry);
1007 /* 1/8th of sec is more than enough time for them to exit */
1011 if (!list_empty(&server->pending_mid_q)) {
1013 * mpx threads have not exited yet give them at least the smb
1014 * send timeout time for long ops.
1016 * Due to delays on oplock break requests, we need to wait at
1017 * least 45 seconds before giving up on a request getting a
1018 * response and going ahead and killing cifsd.
1020 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
1023 * If threads still have not exited they are probably never
1024 * coming home not much else we can do but free the memory.
1028 #ifdef CONFIG_CIFS_DFS_UPCALL
1029 kfree(server->origin_fullpath);
1030 kfree(server->leaf_fullpath);
1034 length = atomic_dec_return(&tcpSesAllocCount);
1036 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1040 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1043 char *buf = server->smallbuf;
1044 unsigned int pdu_length = server->pdu_size;
1046 /* make sure this will fit in a large buffer */
1047 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
1048 HEADER_PREAMBLE_SIZE(server)) {
1049 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
1050 cifs_reconnect(server, true);
1051 return -ECONNABORTED;
1054 /* switch to large buffer if too big for a small one */
1055 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
1056 server->large_buf = true;
1057 memcpy(server->bigbuf, buf, server->total_read);
1058 buf = server->bigbuf;
1061 /* now read the rest */
1062 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
1063 pdu_length - MID_HEADER_SIZE(server));
1067 server->total_read += length;
1069 dump_smb(buf, server->total_read);
1071 return cifs_handle_standard(server, mid);
1075 cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1077 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1081 * We know that we received enough to get to the MID as we
1082 * checked the pdu_length earlier. Now check to see
1083 * if the rest of the header is OK.
1085 * 48 bytes is enough to display the header and a little bit
1086 * into the payload for debugging purposes.
1088 rc = server->ops->check_message(buf, server->total_read, server);
1090 cifs_dump_mem("Bad SMB: ", buf,
1091 min_t(unsigned int, server->total_read, 48));
1093 if (server->ops->is_session_expired &&
1094 server->ops->is_session_expired(buf)) {
1095 cifs_reconnect(server, true);
1099 if (server->ops->is_status_pending &&
1100 server->ops->is_status_pending(buf, server))
1106 handle_mid(mid, server, buf, rc);
1111 smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1113 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
1114 int scredits, in_flight;
1117 * SMB1 does not use credits.
1119 if (is_smb1(server))
1122 if (shdr->CreditRequest) {
1123 spin_lock(&server->req_lock);
1124 server->credits += le16_to_cpu(shdr->CreditRequest);
1125 scredits = server->credits;
1126 in_flight = server->in_flight;
1127 spin_unlock(&server->req_lock);
1128 wake_up(&server->request_q);
1130 trace_smb3_hdr_credits(server->CurrentMid,
1131 server->conn_id, server->hostname, scredits,
1132 le16_to_cpu(shdr->CreditRequest), in_flight);
1133 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
1134 __func__, le16_to_cpu(shdr->CreditRequest),
1141 cifs_demultiplex_thread(void *p)
1143 int i, num_mids, length;
1144 struct TCP_Server_Info *server = p;
1145 unsigned int pdu_length;
1146 unsigned int next_offset;
1148 struct task_struct *task_to_wake = NULL;
1149 struct mid_q_entry *mids[MAX_COMPOUND];
1150 char *bufs[MAX_COMPOUND];
1151 unsigned int noreclaim_flag, num_io_timeout = 0;
1153 noreclaim_flag = memalloc_noreclaim_save();
1154 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
1156 length = atomic_inc_return(&tcpSesAllocCount);
1158 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1161 allow_kernel_signal(SIGKILL);
1162 while (server->tcpStatus != CifsExiting) {
1163 if (try_to_freeze())
1166 if (!allocate_buffers(server))
1169 server->large_buf = false;
1170 buf = server->smallbuf;
1171 pdu_length = 4; /* enough to get RFC1001 header */
1173 length = cifs_read_from_socket(server, buf, pdu_length);
1177 if (is_smb1(server))
1178 server->total_read = length;
1180 server->total_read = 0;
1183 * The right amount was read from socket - 4 bytes,
1184 * so we can now interpret the length field.
1186 pdu_length = get_rfc1002_length(buf);
1188 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
1189 if (!is_smb_response(server, buf[0]))
1192 server->pdu_size = pdu_length;
1194 /* make sure we have enough to get to the MID */
1195 if (server->pdu_size < MID_HEADER_SIZE(server)) {
1196 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
1198 cifs_reconnect(server, true);
1202 /* read down to the MID */
1203 length = cifs_read_from_socket(server,
1204 buf + HEADER_PREAMBLE_SIZE(server),
1205 MID_HEADER_SIZE(server));
1208 server->total_read += length;
1210 if (server->ops->next_header) {
1211 next_offset = server->ops->next_header(buf);
1213 server->pdu_size = next_offset;
1216 memset(mids, 0, sizeof(mids));
1217 memset(bufs, 0, sizeof(bufs));
1220 if (server->ops->is_transform_hdr &&
1221 server->ops->receive_transform &&
1222 server->ops->is_transform_hdr(buf)) {
1223 length = server->ops->receive_transform(server,
1228 mids[0] = server->ops->find_mid(server, buf);
1232 if (!mids[0] || !mids[0]->receive)
1233 length = standard_receive3(server, mids[0]);
1235 length = mids[0]->receive(server, mids[0]);
1239 for (i = 0; i < num_mids; i++)
1241 release_mid(mids[i]);
1245 if (server->ops->is_status_io_timeout &&
1246 server->ops->is_status_io_timeout(buf)) {
1248 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1249 cifs_reconnect(server, false);
1255 server->lstrp = jiffies;
1257 for (i = 0; i < num_mids; i++) {
1258 if (mids[i] != NULL) {
1259 mids[i]->resp_buf_size = server->pdu_size;
1261 if (bufs[i] && server->ops->is_network_name_deleted)
1262 server->ops->is_network_name_deleted(bufs[i],
1265 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1266 mids[i]->callback(mids[i]);
1268 release_mid(mids[i]);
1269 } else if (server->ops->is_oplock_break &&
1270 server->ops->is_oplock_break(bufs[i],
1272 smb2_add_credits_from_hdr(bufs[i], server);
1273 cifs_dbg(FYI, "Received oplock break\n");
1275 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1276 atomic_read(&mid_count));
1277 cifs_dump_mem("Received Data is: ", bufs[i],
1278 HEADER_SIZE(server));
1279 smb2_add_credits_from_hdr(bufs[i], server);
1280 #ifdef CONFIG_CIFS_DEBUG2
1281 if (server->ops->dump_detail)
1282 server->ops->dump_detail(bufs[i],
1284 cifs_dump_mids(server);
1285 #endif /* CIFS_DEBUG2 */
1289 if (pdu_length > server->pdu_size) {
1290 if (!allocate_buffers(server))
1292 pdu_length -= server->pdu_size;
1293 server->total_read = 0;
1294 server->large_buf = false;
1295 buf = server->smallbuf;
1298 } /* end while !EXITING */
1300 /* buffer usually freed in free_mid - need to free it here on exit */
1301 cifs_buf_release(server->bigbuf);
1302 if (server->smallbuf) /* no sense logging a debug message if NULL */
1303 cifs_small_buf_release(server->smallbuf);
1305 task_to_wake = xchg(&server->tsk, NULL);
1306 clean_demultiplex_info(server);
1308 /* if server->tsk was NULL then wait for a signal before exiting */
1309 if (!task_to_wake) {
1310 set_current_state(TASK_INTERRUPTIBLE);
1311 while (!signal_pending(current)) {
1313 set_current_state(TASK_INTERRUPTIBLE);
1315 set_current_state(TASK_RUNNING);
1318 memalloc_noreclaim_restore(noreclaim_flag);
1319 module_put_and_kthread_exit(0);
1323 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1324 * if srcaddr is specified and matches the IP address of the rhs argument
1327 cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
1329 switch (srcaddr->sa_family) {
1331 return (rhs->sa_family == AF_UNSPEC);
1333 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1334 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1335 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1338 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1339 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1340 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1344 return false; /* don't expect to be here */
1349 * If no port is specified in addr structure, we try to match with 445 port
1350 * and if it fails - with 139 ports. It should be called only if address
1351 * families of server and addr are equal.
1354 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1356 __be16 port, *sport;
1358 /* SMBDirect manages its own ports, don't match it here */
1362 switch (addr->sa_family) {
1364 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1365 port = ((struct sockaddr_in *) addr)->sin_port;
1368 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1369 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1377 port = htons(CIFS_PORT);
1381 port = htons(RFC1001_PORT);
1384 return port == *sport;
1388 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1389 struct sockaddr *srcaddr)
1391 switch (addr->sa_family) {
1393 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1394 struct sockaddr_in *srv_addr4 =
1395 (struct sockaddr_in *)&server->dstaddr;
1397 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
1402 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1403 struct sockaddr_in6 *srv_addr6 =
1404 (struct sockaddr_in6 *)&server->dstaddr;
1406 if (!ipv6_addr_equal(&addr6->sin6_addr,
1407 &srv_addr6->sin6_addr))
1409 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
1415 return false; /* don't expect to be here */
1418 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
1425 match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1428 * The select_sectype function should either return the ctx->sectype
1429 * that was specified, or "Unspecified" if that sectype was not
1430 * compatible with the given NEGOTIATE request.
1432 if (server->ops->select_sectype(server, ctx->sectype)
1437 * Now check if signing mode is acceptable. No need to check
1438 * global_secflags at this point since if MUST_SIGN is set then
1439 * the server->sign had better be too.
1441 if (ctx->sign && !server->sign)
1447 /* this function must be called with srv_lock held */
1448 static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1450 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
1452 if (ctx->nosharesock)
1455 /* this server does not share socket */
1456 if (server->nosharesock)
1459 /* If multidialect negotiation see if existing sessions match one */
1460 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
1461 if (server->vals->protocol_id < SMB30_PROT_ID)
1463 } else if (strcmp(ctx->vals->version_string,
1464 SMBDEFAULT_VERSION_STRING) == 0) {
1465 if (server->vals->protocol_id < SMB21_PROT_ID)
1467 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
1470 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1473 if (strcasecmp(server->hostname, ctx->server_hostname))
1476 if (!match_address(server, addr,
1477 (struct sockaddr *)&ctx->srcaddr))
1480 if (!match_port(server, addr))
1483 if (!match_security(server, ctx))
1486 if (server->echo_interval != ctx->echo_interval * HZ)
1489 if (server->rdma != ctx->rdma)
1492 if (server->ignore_signature != ctx->ignore_signature)
1495 if (server->min_offload != ctx->min_offload)
1501 struct TCP_Server_Info *
1502 cifs_find_tcp_session(struct smb3_fs_context *ctx)
1504 struct TCP_Server_Info *server;
1506 spin_lock(&cifs_tcp_ses_lock);
1507 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1508 spin_lock(&server->srv_lock);
1509 #ifdef CONFIG_CIFS_DFS_UPCALL
1511 * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for
1512 * DFS connections to do failover properly, so avoid sharing them with regular
1513 * shares or even links that may connect to same server but having completely
1514 * different failover targets.
1516 if (server->is_dfs_conn) {
1517 spin_unlock(&server->srv_lock);
1522 * Skip ses channels since they're only handled in lower layers
1523 * (e.g. cifs_send_recv).
1525 if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx)) {
1526 spin_unlock(&server->srv_lock);
1529 spin_unlock(&server->srv_lock);
1531 ++server->srv_count;
1532 spin_unlock(&cifs_tcp_ses_lock);
1533 cifs_dbg(FYI, "Existing tcp session with server found\n");
1536 spin_unlock(&cifs_tcp_ses_lock);
1541 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1543 struct task_struct *task;
1545 spin_lock(&cifs_tcp_ses_lock);
1546 if (--server->srv_count > 0) {
1547 spin_unlock(&cifs_tcp_ses_lock);
1551 /* srv_count can never go negative */
1552 WARN_ON(server->srv_count < 0);
1554 put_net(cifs_net_ns(server));
1556 list_del_init(&server->tcp_ses_list);
1557 spin_unlock(&cifs_tcp_ses_lock);
1559 /* For secondary channels, we pick up ref-count on the primary server */
1560 if (CIFS_SERVER_IS_CHAN(server))
1561 cifs_put_tcp_session(server->primary_server, from_reconnect);
1563 cancel_delayed_work_sync(&server->echo);
1564 cancel_delayed_work_sync(&server->resolve);
1568 * Avoid deadlock here: reconnect work calls
1569 * cifs_put_tcp_session() at its end. Need to be sure
1570 * that reconnect work does nothing with server pointer after
1573 cancel_delayed_work(&server->reconnect);
1575 cancel_delayed_work_sync(&server->reconnect);
1577 spin_lock(&server->srv_lock);
1578 server->tcpStatus = CifsExiting;
1579 spin_unlock(&server->srv_lock);
1581 cifs_crypto_secmech_release(server);
1583 kfree_sensitive(server->session_key.response);
1584 server->session_key.response = NULL;
1585 server->session_key.len = 0;
1586 kfree(server->hostname);
1588 task = xchg(&server->tsk, NULL);
1590 send_sig(SIGKILL, task, 1);
1593 struct TCP_Server_Info *
1594 cifs_get_tcp_session(struct smb3_fs_context *ctx,
1595 struct TCP_Server_Info *primary_server)
1597 struct TCP_Server_Info *tcp_ses = NULL;
1600 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
1602 /* see if we already have a matching tcp_ses */
1603 tcp_ses = cifs_find_tcp_session(ctx);
1607 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1613 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1614 if (!tcp_ses->hostname) {
1619 if (ctx->nosharesock)
1620 tcp_ses->nosharesock = true;
1622 tcp_ses->ops = ctx->ops;
1623 tcp_ses->vals = ctx->vals;
1624 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1626 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
1627 tcp_ses->noblockcnt = ctx->rootfs;
1628 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1629 tcp_ses->noautotune = ctx->noautotune;
1630 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1631 tcp_ses->rdma = ctx->rdma;
1632 tcp_ses->in_flight = 0;
1633 tcp_ses->max_in_flight = 0;
1634 tcp_ses->credits = 1;
1635 if (primary_server) {
1636 spin_lock(&cifs_tcp_ses_lock);
1637 ++primary_server->srv_count;
1638 spin_unlock(&cifs_tcp_ses_lock);
1639 tcp_ses->primary_server = primary_server;
1641 init_waitqueue_head(&tcp_ses->response_q);
1642 init_waitqueue_head(&tcp_ses->request_q);
1643 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1644 mutex_init(&tcp_ses->_srv_mutex);
1645 memcpy(tcp_ses->workstation_RFC1001_name,
1646 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1647 memcpy(tcp_ses->server_RFC1001_name,
1648 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1649 tcp_ses->session_estab = false;
1650 tcp_ses->sequence_number = 0;
1651 tcp_ses->reconnect_instance = 1;
1652 tcp_ses->lstrp = jiffies;
1653 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
1654 spin_lock_init(&tcp_ses->req_lock);
1655 spin_lock_init(&tcp_ses->srv_lock);
1656 spin_lock_init(&tcp_ses->mid_lock);
1657 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1658 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
1659 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
1660 INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server);
1661 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1662 mutex_init(&tcp_ses->reconnect_mutex);
1663 #ifdef CONFIG_CIFS_DFS_UPCALL
1664 mutex_init(&tcp_ses->refpath_lock);
1666 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
1667 sizeof(tcp_ses->srcaddr));
1668 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
1669 sizeof(tcp_ses->dstaddr));
1670 if (ctx->use_client_guid)
1671 memcpy(tcp_ses->client_guid, ctx->client_guid,
1672 SMB2_CLIENT_GUID_SIZE);
1674 generate_random_uuid(tcp_ses->client_guid);
1676 * at this point we are the only ones with the pointer
1677 * to the struct since the kernel thread not created yet
1678 * no need to spinlock this init of tcpStatus or srv_count
1680 tcp_ses->tcpStatus = CifsNew;
1681 ++tcp_ses->srv_count;
1683 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1684 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1685 tcp_ses->echo_interval = ctx->echo_interval * HZ;
1687 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
1688 if (tcp_ses->rdma) {
1689 #ifndef CONFIG_CIFS_SMB_DIRECT
1690 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1692 goto out_err_crypto_release;
1694 tcp_ses->smbd_conn = smbd_get_connection(
1695 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
1696 if (tcp_ses->smbd_conn) {
1697 cifs_dbg(VFS, "RDMA transport established\n");
1699 goto smbd_connected;
1702 goto out_err_crypto_release;
1705 rc = ip_connect(tcp_ses);
1707 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
1708 goto out_err_crypto_release;
1712 * since we're in a cifs function already, we know that
1713 * this will succeed. No need for try_module_get().
1715 __module_get(THIS_MODULE);
1716 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
1718 if (IS_ERR(tcp_ses->tsk)) {
1719 rc = PTR_ERR(tcp_ses->tsk);
1720 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
1721 module_put(THIS_MODULE);
1722 goto out_err_crypto_release;
1724 tcp_ses->min_offload = ctx->min_offload;
1726 * at this point we are the only ones with the pointer
1727 * to the struct since the kernel thread not created yet
1728 * no need to spinlock this update of tcpStatus
1730 spin_lock(&tcp_ses->srv_lock);
1731 tcp_ses->tcpStatus = CifsNeedNegotiate;
1732 spin_unlock(&tcp_ses->srv_lock);
1734 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1735 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1737 tcp_ses->max_credits = ctx->max_credits;
1739 tcp_ses->nr_targets = 1;
1740 tcp_ses->ignore_signature = ctx->ignore_signature;
1741 /* thread spawned, put it on the list */
1742 spin_lock(&cifs_tcp_ses_lock);
1743 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
1744 spin_unlock(&cifs_tcp_ses_lock);
1746 /* queue echo request delayed work */
1747 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
1749 /* queue dns resolution delayed work */
1750 cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n",
1751 __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT);
1753 queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ));
1757 out_err_crypto_release:
1758 cifs_crypto_secmech_release(tcp_ses);
1760 put_net(cifs_net_ns(tcp_ses));
1764 if (CIFS_SERVER_IS_CHAN(tcp_ses))
1765 cifs_put_tcp_session(tcp_ses->primary_server, false);
1766 kfree(tcp_ses->hostname);
1767 if (tcp_ses->ssocket)
1768 sock_release(tcp_ses->ssocket);
1774 /* this function must be called with ses_lock held */
1775 static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1777 if (ctx->sectype != Unspecified &&
1778 ctx->sectype != ses->sectype)
1782 * If an existing session is limited to less channels than
1783 * requested, it should not be reused
1785 spin_lock(&ses->chan_lock);
1786 if (ses->chan_max < ctx->max_channels) {
1787 spin_unlock(&ses->chan_lock);
1790 spin_unlock(&ses->chan_lock);
1792 switch (ses->sectype) {
1794 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
1798 /* NULL username means anonymous session */
1799 if (ses->user_name == NULL) {
1805 /* anything else takes username/password */
1806 if (strncmp(ses->user_name,
1807 ctx->username ? ctx->username : "",
1808 CIFS_MAX_USERNAME_LEN))
1810 if ((ctx->username && strlen(ctx->username) != 0) &&
1811 ses->password != NULL &&
1812 strncmp(ses->password,
1813 ctx->password ? ctx->password : "",
1814 CIFS_MAX_PASSWORD_LEN))
1821 * cifs_setup_ipc - helper to setup the IPC tcon for the session
1822 * @ses: smb session to issue the request on
1823 * @ctx: the superblock configuration context to use for building the
1824 * new tree connection for the IPC (interprocess communication RPC)
1826 * A new IPC connection is made and stored in the session
1827 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1830 cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1833 struct cifs_tcon *tcon;
1834 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1836 struct TCP_Server_Info *server = ses->server;
1839 * If the mount request that resulted in the creation of the
1840 * session requires encryption, force IPC to be encrypted too.
1843 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
1846 cifs_server_dbg(VFS,
1847 "IPC: server doesn't support encryption\n");
1852 tcon = tconInfoAlloc();
1856 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
1862 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
1866 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
1871 cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
1873 ses->tcon_ipc = tcon;
1879 * cifs_free_ipc - helper to release the session IPC tcon
1880 * @ses: smb session to unmount the IPC from
1882 * Needs to be called everytime a session is destroyed.
1884 * On session close, the IPC is closed and the server must release all tcons of the session.
1885 * No need to send a tree disconnect here.
1887 * Besides, it will make the server to not close durable and resilient files on session close, as
1888 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
1891 cifs_free_ipc(struct cifs_ses *ses)
1893 struct cifs_tcon *tcon = ses->tcon_ipc;
1899 ses->tcon_ipc = NULL;
1903 static struct cifs_ses *
1904 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1906 struct cifs_ses *ses;
1908 spin_lock(&cifs_tcp_ses_lock);
1909 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1910 spin_lock(&ses->ses_lock);
1911 if (ses->ses_status == SES_EXITING) {
1912 spin_unlock(&ses->ses_lock);
1915 if (!match_session(ses, ctx)) {
1916 spin_unlock(&ses->ses_lock);
1919 spin_unlock(&ses->ses_lock);
1922 spin_unlock(&cifs_tcp_ses_lock);
1925 spin_unlock(&cifs_tcp_ses_lock);
1929 void cifs_put_smb_ses(struct cifs_ses *ses)
1931 unsigned int rc, xid;
1932 unsigned int chan_count;
1933 struct TCP_Server_Info *server = ses->server;
1935 spin_lock(&ses->ses_lock);
1936 if (ses->ses_status == SES_EXITING) {
1937 spin_unlock(&ses->ses_lock);
1940 spin_unlock(&ses->ses_lock);
1942 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1944 "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->tree_name : "NONE");
1946 spin_lock(&cifs_tcp_ses_lock);
1947 if (--ses->ses_count > 0) {
1948 spin_unlock(&cifs_tcp_ses_lock);
1951 spin_unlock(&cifs_tcp_ses_lock);
1953 /* ses_count can never go negative */
1954 WARN_ON(ses->ses_count < 0);
1956 if (ses->ses_status == SES_GOOD)
1957 ses->ses_status = SES_EXITING;
1961 if (ses->ses_status == SES_EXITING && server->ops->logoff) {
1963 rc = server->ops->logoff(xid, ses);
1965 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
1970 spin_lock(&cifs_tcp_ses_lock);
1971 list_del_init(&ses->smb_ses_list);
1972 spin_unlock(&cifs_tcp_ses_lock);
1974 chan_count = ses->chan_count;
1976 /* close any extra channels */
1977 if (chan_count > 1) {
1980 for (i = 1; i < chan_count; i++) {
1981 if (ses->chans[i].iface) {
1982 kref_put(&ses->chans[i].iface->refcount, release_iface);
1983 ses->chans[i].iface = NULL;
1985 cifs_put_tcp_session(ses->chans[i].server, 0);
1986 ses->chans[i].server = NULL;
1991 cifs_put_tcp_session(server, 0);
1996 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1997 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
1999 /* Populate username and pw fields from keyring if possible */
2001 cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
2005 const char *delim, *payload;
2009 struct TCP_Server_Info *server = ses->server;
2010 struct sockaddr_in *sa;
2011 struct sockaddr_in6 *sa6;
2012 const struct user_key_payload *upayload;
2014 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2018 /* try to find an address key first */
2019 switch (server->dstaddr.ss_family) {
2021 sa = (struct sockaddr_in *)&server->dstaddr;
2022 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2025 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2026 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2029 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2030 server->dstaddr.ss_family);
2035 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2036 key = request_key(&key_type_logon, desc, "");
2038 if (!ses->domainName) {
2039 cifs_dbg(FYI, "domainName is NULL\n");
2044 /* didn't work, try to find a domain key */
2045 sprintf(desc, "cifs:d:%s", ses->domainName);
2046 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2047 key = request_key(&key_type_logon, desc, "");
2055 down_read(&key->sem);
2056 upayload = user_key_payload_locked(key);
2057 if (IS_ERR_OR_NULL(upayload)) {
2058 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2062 /* find first : in payload */
2063 payload = upayload->data;
2064 delim = strnchr(payload, upayload->datalen, ':');
2065 cifs_dbg(FYI, "payload=%s\n", payload);
2067 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2073 len = delim - payload;
2074 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2075 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2081 ctx->username = kstrndup(payload, len, GFP_KERNEL);
2082 if (!ctx->username) {
2083 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2088 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
2090 len = key->datalen - (len + 1);
2091 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2092 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2094 kfree(ctx->username);
2095 ctx->username = NULL;
2100 ctx->password = kstrndup(delim, len, GFP_KERNEL);
2101 if (!ctx->password) {
2102 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2105 kfree(ctx->username);
2106 ctx->username = NULL;
2111 * If we have a domain key then we must set the domainName in the
2114 if (is_domain && ses->domainName) {
2115 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
2116 if (!ctx->domainname) {
2117 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
2120 kfree(ctx->username);
2121 ctx->username = NULL;
2122 kfree_sensitive(ctx->password);
2123 ctx->password = NULL;
2128 strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
2135 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2138 #else /* ! CONFIG_KEYS */
2140 cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
2141 struct cifs_ses *ses __attribute__((unused)))
2145 #endif /* CONFIG_KEYS */
2148 * cifs_get_smb_ses - get a session matching @ctx data from @server
2149 * @server: server to setup the session to
2150 * @ctx: superblock configuration context to use to setup the session
2152 * This function assumes it is being called from cifs_mount() where we
2153 * already got a server reference (server refcount +1). See
2154 * cifs_get_tcon() for refcount explanations.
2157 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
2161 struct cifs_ses *ses;
2162 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2163 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2167 ses = cifs_find_smb_ses(server, ctx);
2169 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2172 spin_lock(&ses->chan_lock);
2173 if (cifs_chan_needs_reconnect(ses, server)) {
2174 spin_unlock(&ses->chan_lock);
2175 cifs_dbg(FYI, "Session needs reconnect\n");
2177 mutex_lock(&ses->session_mutex);
2178 rc = cifs_negotiate_protocol(xid, ses, server);
2180 mutex_unlock(&ses->session_mutex);
2181 /* problem -- put our ses reference */
2182 cifs_put_smb_ses(ses);
2187 rc = cifs_setup_session(xid, ses, server,
2190 mutex_unlock(&ses->session_mutex);
2191 /* problem -- put our reference */
2192 cifs_put_smb_ses(ses);
2196 mutex_unlock(&ses->session_mutex);
2198 spin_lock(&ses->chan_lock);
2200 spin_unlock(&ses->chan_lock);
2202 /* existing SMB ses has a server reference already */
2203 cifs_put_tcp_session(server, 0);
2208 cifs_dbg(FYI, "Existing smb sess not found\n");
2209 ses = sesInfoAlloc();
2213 /* new SMB session uses our server ref */
2214 ses->server = server;
2215 if (server->dstaddr.ss_family == AF_INET6)
2216 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
2218 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
2220 if (ctx->username) {
2221 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
2222 if (!ses->user_name)
2226 /* ctx->password freed at unmount */
2227 if (ctx->password) {
2228 ses->password = kstrdup(ctx->password, GFP_KERNEL);
2232 if (ctx->domainname) {
2233 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
2234 if (!ses->domainName)
2238 strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
2240 if (ctx->domainauto)
2241 ses->domainAuto = ctx->domainauto;
2242 ses->cred_uid = ctx->cred_uid;
2243 ses->linux_uid = ctx->linux_uid;
2245 ses->sectype = ctx->sectype;
2246 ses->sign = ctx->sign;
2248 /* add server as first channel */
2249 spin_lock(&ses->chan_lock);
2250 ses->chans[0].server = server;
2251 ses->chan_count = 1;
2252 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
2253 ses->chans_need_reconnect = 1;
2254 spin_unlock(&ses->chan_lock);
2256 mutex_lock(&ses->session_mutex);
2257 rc = cifs_negotiate_protocol(xid, ses, server);
2259 rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
2260 mutex_unlock(&ses->session_mutex);
2262 /* each channel uses a different signing key */
2263 spin_lock(&ses->chan_lock);
2264 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2265 sizeof(ses->smb3signingkey));
2266 spin_unlock(&ses->chan_lock);
2272 * success, put it on the list and add it as first channel
2273 * note: the session becomes active soon after this. So you'll
2274 * need to lock before changing something in the session.
2276 spin_lock(&cifs_tcp_ses_lock);
2277 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2278 spin_unlock(&cifs_tcp_ses_lock);
2282 cifs_setup_ipc(ses, ctx);
2292 /* this function must be called with tc_lock held */
2293 static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
2295 if (tcon->status == TID_EXITING)
2297 if (strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE))
2299 if (tcon->seal != ctx->seal)
2301 if (tcon->snapshot_time != ctx->snapshot_time)
2303 if (tcon->handle_timeout != ctx->handle_timeout)
2305 if (tcon->no_lease != ctx->no_lease)
2307 if (tcon->nodelete != ctx->nodelete)
2312 static struct cifs_tcon *
2313 cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2315 struct cifs_tcon *tcon;
2317 spin_lock(&cifs_tcp_ses_lock);
2318 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2319 spin_lock(&tcon->tc_lock);
2320 if (!match_tcon(tcon, ctx)) {
2321 spin_unlock(&tcon->tc_lock);
2325 spin_unlock(&tcon->tc_lock);
2326 spin_unlock(&cifs_tcp_ses_lock);
2329 spin_unlock(&cifs_tcp_ses_lock);
2334 cifs_put_tcon(struct cifs_tcon *tcon)
2337 struct cifs_ses *ses;
2340 * IPC tcon share the lifetime of their session and are
2341 * destroyed in the session put function
2343 if (tcon == NULL || tcon->ipc)
2347 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2348 spin_lock(&cifs_tcp_ses_lock);
2349 spin_lock(&tcon->tc_lock);
2350 if (--tcon->tc_count > 0) {
2351 spin_unlock(&tcon->tc_lock);
2352 spin_unlock(&cifs_tcp_ses_lock);
2356 /* tc_count can never go negative */
2357 WARN_ON(tcon->tc_count < 0);
2359 list_del_init(&tcon->tcon_list);
2360 spin_unlock(&tcon->tc_lock);
2361 spin_unlock(&cifs_tcp_ses_lock);
2363 /* cancel polling of interfaces */
2364 cancel_delayed_work_sync(&tcon->query_interfaces);
2366 if (tcon->use_witness) {
2369 rc = cifs_swn_unregister(tcon);
2371 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2377 if (ses->server->ops->tree_disconnect)
2378 ses->server->ops->tree_disconnect(xid, tcon);
2381 cifs_fscache_release_super_cookie(tcon);
2383 cifs_put_smb_ses(ses);
2387 * cifs_get_tcon - get a tcon matching @ctx data from @ses
2388 * @ses: smb session to issue the request on
2389 * @ctx: the superblock configuration context to use for building the
2391 * - tcon refcount is the number of mount points using the tcon.
2392 * - ses refcount is the number of tcon using the session.
2394 * 1. This function assumes it is being called from cifs_mount() where
2395 * we already got a session reference (ses refcount +1).
2397 * 2. Since we're in the context of adding a mount point, the end
2398 * result should be either:
2400 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2401 * its session refcount incremented (1 new tcon). This +1 was
2402 * already done in (1).
2404 * b) an existing tcon with refcount+1 (add a mount point to it) and
2405 * identical ses refcount (no new tcon). Because of (1) we need to
2406 * decrement the ses refcount.
2408 static struct cifs_tcon *
2409 cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2412 struct cifs_tcon *tcon;
2414 tcon = cifs_find_tcon(ses, ctx);
2417 * tcon has refcount already incremented but we need to
2418 * decrement extra ses reference gotten by caller (case b)
2420 cifs_dbg(FYI, "Found match on UNC path\n");
2421 cifs_put_smb_ses(ses);
2425 if (!ses->server->ops->tree_connect) {
2430 tcon = tconInfoAlloc();
2436 if (ctx->snapshot_time) {
2437 if (ses->server->vals->protocol_id == 0) {
2439 "Use SMB2 or later for snapshot mount option\n");
2443 tcon->snapshot_time = ctx->snapshot_time;
2446 if (ctx->handle_timeout) {
2447 if (ses->server->vals->protocol_id == 0) {
2449 "Use SMB2.1 or later for handle timeout option\n");
2453 tcon->handle_timeout = ctx->handle_timeout;
2457 if (ctx->password) {
2458 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
2459 if (!tcon->password) {
2466 if (ses->server->vals->protocol_id == 0) {
2468 "SMB3 or later required for encryption\n");
2471 } else if (tcon->ses->server->capabilities &
2472 SMB2_GLOBAL_CAP_ENCRYPTION)
2475 cifs_dbg(VFS, "Encryption is not supported on share\n");
2481 if (ctx->linux_ext) {
2482 if (ses->server->posix_ext_supported) {
2483 tcon->posix_extensions = true;
2484 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
2485 } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
2486 (strcmp(ses->server->vals->version_string,
2487 SMB3ANY_VERSION_STRING) == 0) ||
2488 (strcmp(ses->server->vals->version_string,
2489 SMBDEFAULT_VERSION_STRING) == 0)) {
2490 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
2494 cifs_dbg(VFS, "Check vers= mount option. SMB3.11 "
2495 "disabled but required for POSIX extensions\n");
2502 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2505 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2509 tcon->use_persistent = false;
2510 /* check if SMB2 or later, CIFS does not support persistent handles */
2511 if (ctx->persistent) {
2512 if (ses->server->vals->protocol_id == 0) {
2514 "SMB3 or later required for persistent handles\n");
2517 } else if (ses->server->capabilities &
2518 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2519 tcon->use_persistent = true;
2520 else /* persistent handles requested but not supported */ {
2522 "Persistent handles not supported on share\n");
2526 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2527 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2528 && (ctx->nopersistent == false)) {
2529 cifs_dbg(FYI, "enabling persistent handles\n");
2530 tcon->use_persistent = true;
2531 } else if (ctx->resilient) {
2532 if (ses->server->vals->protocol_id == 0) {
2534 "SMB2.1 or later required for resilient handles\n");
2538 tcon->use_resilient = true;
2541 tcon->use_witness = false;
2542 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
2543 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2544 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
2546 * Set witness in use flag in first place
2547 * to retry registration in the echo task
2549 tcon->use_witness = true;
2550 /* And try to register immediately */
2551 rc = cifs_swn_register(tcon);
2553 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2557 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2558 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2563 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2569 /* If the user really knows what they are doing they can override */
2570 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
2572 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
2573 else if (ctx->cache_rw)
2574 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2577 if (ctx->no_lease) {
2578 if (ses->server->vals->protocol_id == 0) {
2580 "SMB2 or later required for nolease option\n");
2584 tcon->no_lease = ctx->no_lease;
2588 * We can have only one retry value for a connection to a share so for
2589 * resources mounted more than once to the same server share the last
2590 * value passed in for the retry flag is used.
2592 tcon->retry = ctx->retry;
2593 tcon->nocase = ctx->nocase;
2594 tcon->broken_sparse_sup = ctx->no_sparse;
2595 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
2596 tcon->nohandlecache = ctx->nohandlecache;
2598 tcon->nohandlecache = true;
2599 tcon->nodelete = ctx->nodelete;
2600 tcon->local_lease = ctx->local_lease;
2601 INIT_LIST_HEAD(&tcon->pending_opens);
2603 /* schedule query interfaces poll */
2604 INIT_DELAYED_WORK(&tcon->query_interfaces,
2605 smb2_query_server_interfaces);
2606 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
2607 (SMB_INTERFACE_POLL_INTERVAL * HZ));
2609 spin_lock(&cifs_tcp_ses_lock);
2610 list_add(&tcon->tcon_list, &ses->tcon_list);
2611 spin_unlock(&cifs_tcp_ses_lock);
2621 cifs_put_tlink(struct tcon_link *tlink)
2623 if (!tlink || IS_ERR(tlink))
2626 if (!atomic_dec_and_test(&tlink->tl_count) ||
2627 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2628 tlink->tl_time = jiffies;
2632 if (!IS_ERR(tlink_tcon(tlink)))
2633 cifs_put_tcon(tlink_tcon(tlink));
2639 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2641 struct cifs_sb_info *old = CIFS_SB(sb);
2642 struct cifs_sb_info *new = mnt_data->cifs_sb;
2643 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2644 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
2646 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2649 if (old->mnt_cifs_serverino_autodisabled)
2650 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2652 if (oldflags != newflags)
2656 * We want to share sb only if we don't specify an r/wsize or
2657 * specified r/wsize is greater than or equal to existing one.
2659 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
2662 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
2665 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2666 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
2669 if (old->ctx->file_mode != new->ctx->file_mode ||
2670 old->ctx->dir_mode != new->ctx->dir_mode)
2673 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2676 if (old->ctx->acregmax != new->ctx->acregmax)
2678 if (old->ctx->acdirmax != new->ctx->acdirmax)
2680 if (old->ctx->closetimeo != new->ctx->closetimeo)
2687 match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2689 struct cifs_sb_info *old = CIFS_SB(sb);
2690 struct cifs_sb_info *new = mnt_data->cifs_sb;
2691 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2693 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2696 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
2698 else if (!old_set && !new_set)
2705 cifs_match_super(struct super_block *sb, void *data)
2707 struct cifs_mnt_data *mnt_data = data;
2708 struct smb3_fs_context *ctx;
2709 struct cifs_sb_info *cifs_sb;
2710 struct TCP_Server_Info *tcp_srv;
2711 struct cifs_ses *ses;
2712 struct cifs_tcon *tcon;
2713 struct tcon_link *tlink;
2716 spin_lock(&cifs_tcp_ses_lock);
2717 cifs_sb = CIFS_SB(sb);
2718 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2719 if (tlink == NULL) {
2720 /* can not match superblock if tlink were ever null */
2721 spin_unlock(&cifs_tcp_ses_lock);
2724 tcon = tlink_tcon(tlink);
2726 tcp_srv = ses->server;
2728 ctx = mnt_data->ctx;
2730 spin_lock(&tcp_srv->srv_lock);
2731 spin_lock(&ses->ses_lock);
2732 spin_lock(&tcon->tc_lock);
2733 if (!match_server(tcp_srv, ctx) ||
2734 !match_session(ses, ctx) ||
2735 !match_tcon(tcon, ctx) ||
2736 !match_prepath(sb, mnt_data)) {
2741 rc = compare_mount_options(sb, mnt_data);
2743 spin_unlock(&tcon->tc_lock);
2744 spin_unlock(&ses->ses_lock);
2745 spin_unlock(&tcp_srv->srv_lock);
2747 spin_unlock(&cifs_tcp_ses_lock);
2748 cifs_put_tlink(tlink);
2752 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2753 static struct lock_class_key cifs_key[2];
2754 static struct lock_class_key cifs_slock_key[2];
2757 cifs_reclassify_socket4(struct socket *sock)
2759 struct sock *sk = sock->sk;
2760 BUG_ON(!sock_allow_reclassification(sk));
2761 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2762 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2766 cifs_reclassify_socket6(struct socket *sock)
2768 struct sock *sk = sock->sk;
2769 BUG_ON(!sock_allow_reclassification(sk));
2770 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2771 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2775 cifs_reclassify_socket4(struct socket *sock)
2780 cifs_reclassify_socket6(struct socket *sock)
2785 /* See RFC1001 section 14 on representation of Netbios names */
2786 static void rfc1002mangle(char *target, char *source, unsigned int length)
2790 for (i = 0, j = 0; i < (length); i++) {
2791 /* mask a nibble at a time and encode */
2792 target[j] = 'A' + (0x0F & (source[i] >> 4));
2793 target[j+1] = 'A' + (0x0F & source[i]);
2800 bind_socket(struct TCP_Server_Info *server)
2803 if (server->srcaddr.ss_family != AF_UNSPEC) {
2804 /* Bind to the specified local IP address */
2805 struct socket *socket = server->ssocket;
2806 rc = socket->ops->bind(socket,
2807 (struct sockaddr *) &server->srcaddr,
2808 sizeof(server->srcaddr));
2810 struct sockaddr_in *saddr4;
2811 struct sockaddr_in6 *saddr6;
2812 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2813 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2814 if (saddr6->sin6_family == AF_INET6)
2815 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
2816 &saddr6->sin6_addr, rc);
2818 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
2819 &saddr4->sin_addr.s_addr, rc);
2826 ip_rfc1001_connect(struct TCP_Server_Info *server)
2830 * some servers require RFC1001 sessinit before sending
2831 * negprot - BB check reconnection in case where second
2832 * sessinit is sent but no second negprot
2834 struct rfc1002_session_packet *ses_init_buf;
2835 unsigned int req_noscope_len;
2836 struct smb_hdr *smb_buf;
2838 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2842 ses_init_buf->trailer.session_req.called_len = 32;
2844 if (server->server_RFC1001_name[0] != 0)
2845 rfc1002mangle(ses_init_buf->trailer.
2846 session_req.called_name,
2847 server->server_RFC1001_name,
2848 RFC1001_NAME_LEN_WITH_NULL);
2850 rfc1002mangle(ses_init_buf->trailer.
2851 session_req.called_name,
2852 DEFAULT_CIFS_CALLED_NAME,
2853 RFC1001_NAME_LEN_WITH_NULL);
2855 ses_init_buf->trailer.session_req.calling_len = 32;
2858 * calling name ends in null (byte 16) from old smb
2861 if (server->workstation_RFC1001_name[0] != 0)
2862 rfc1002mangle(ses_init_buf->trailer.
2863 session_req.calling_name,
2864 server->workstation_RFC1001_name,
2865 RFC1001_NAME_LEN_WITH_NULL);
2867 rfc1002mangle(ses_init_buf->trailer.
2868 session_req.calling_name,
2870 RFC1001_NAME_LEN_WITH_NULL);
2872 ses_init_buf->trailer.session_req.scope1 = 0;
2873 ses_init_buf->trailer.session_req.scope2 = 0;
2874 smb_buf = (struct smb_hdr *)ses_init_buf;
2876 /* sizeof RFC1002_SESSION_REQUEST with no scopes */
2877 req_noscope_len = sizeof(struct rfc1002_session_packet) - 2;
2879 /* == cpu_to_be32(0x81000044) */
2880 smb_buf->smb_buf_length =
2881 cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | req_noscope_len);
2882 rc = smb_send(server, smb_buf, 0x44);
2883 kfree(ses_init_buf);
2885 * RFC1001 layer in at least one server
2886 * requires very short break before negprot
2887 * presumably because not expecting negprot
2888 * to follow so fast. This is a simple
2889 * solution that works without
2890 * complicating the code and causes no
2891 * significant slowing down on mount
2894 usleep_range(1000, 2000);
2897 * else the negprot may still work without this
2898 * even though malloc failed
2905 generic_ip_connect(struct TCP_Server_Info *server)
2910 struct socket *socket = server->ssocket;
2911 struct sockaddr *saddr;
2913 saddr = (struct sockaddr *) &server->dstaddr;
2915 if (server->dstaddr.ss_family == AF_INET6) {
2916 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2918 sport = ipv6->sin6_port;
2919 slen = sizeof(struct sockaddr_in6);
2921 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2924 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2926 sport = ipv4->sin_port;
2927 slen = sizeof(struct sockaddr_in);
2929 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2933 if (socket == NULL) {
2934 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2935 IPPROTO_TCP, &socket, 1);
2937 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
2938 server->ssocket = NULL;
2942 /* BB other socket options to set KEEPALIVE, NODELAY? */
2943 cifs_dbg(FYI, "Socket created\n");
2944 server->ssocket = socket;
2945 socket->sk->sk_allocation = GFP_NOFS;
2946 if (sfamily == AF_INET6)
2947 cifs_reclassify_socket6(socket);
2949 cifs_reclassify_socket4(socket);
2952 rc = bind_socket(server);
2957 * Eventually check for other socket options to change from
2958 * the default. sock_setsockopt not used because it expects
2961 socket->sk->sk_rcvtimeo = 7 * HZ;
2962 socket->sk->sk_sndtimeo = 5 * HZ;
2964 /* make the bufsizes depend on wsize/rsize and max requests */
2965 if (server->noautotune) {
2966 if (socket->sk->sk_sndbuf < (200 * 1024))
2967 socket->sk->sk_sndbuf = 200 * 1024;
2968 if (socket->sk->sk_rcvbuf < (140 * 1024))
2969 socket->sk->sk_rcvbuf = 140 * 1024;
2972 if (server->tcp_nodelay)
2973 tcp_sock_set_nodelay(socket->sk);
2975 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
2976 socket->sk->sk_sndbuf,
2977 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2979 rc = socket->ops->connect(socket, saddr, slen,
2980 server->noblockcnt ? O_NONBLOCK : 0);
2982 * When mounting SMB root file systems, we do not want to block in
2983 * connect. Otherwise bail out and then let cifs_reconnect() perform
2984 * reconnect failover - if possible.
2986 if (server->noblockcnt && rc == -EINPROGRESS)
2989 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
2990 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
2991 sock_release(socket);
2992 server->ssocket = NULL;
2995 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
2996 if (sport == htons(RFC1001_PORT))
2997 rc = ip_rfc1001_connect(server);
3003 ip_connect(struct TCP_Server_Info *server)
3006 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3007 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3009 if (server->dstaddr.ss_family == AF_INET6)
3010 sport = &addr6->sin6_port;
3012 sport = &addr->sin_port;
3017 /* try with 445 port at first */
3018 *sport = htons(CIFS_PORT);
3020 rc = generic_ip_connect(server);
3024 /* if it failed, try with 139 port */
3025 *sport = htons(RFC1001_PORT);
3028 return generic_ip_connect(server);
3031 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3032 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3033 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3036 * If we are reconnecting then should we check to see if
3037 * any requested capabilities changed locally e.g. via
3038 * remount but we can not do much about it here
3039 * if they have (even if we could detect it by the following)
3040 * Perhaps we could add a backpointer to array of sb from tcon
3041 * or if we change to make all sb to same share the same
3042 * sb as NFS - then we only have one backpointer to sb.
3043 * What if we wanted to mount the server share twice once with
3044 * and once without posixacls or posix paths?
3046 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3048 if (ctx && ctx->no_linux_ext) {
3049 tcon->fsUnixInfo.Capability = 0;
3050 tcon->unix_ext = 0; /* Unix Extensions disabled */
3051 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3054 tcon->unix_ext = 1; /* Unix Extensions supported */
3056 if (!tcon->unix_ext) {
3057 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3061 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3062 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3063 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3065 * check for reconnect case in which we do not
3066 * want to change the mount behavior if we can avoid it
3070 * turn off POSIX ACL and PATHNAMES if not set
3071 * originally at mount time
3073 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3074 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3075 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3076 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3077 cifs_dbg(VFS, "POSIXPATH support change\n");
3078 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3079 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3080 cifs_dbg(VFS, "possible reconnect error\n");
3081 cifs_dbg(VFS, "server disabled POSIX path support\n");
3085 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3086 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3088 cap &= CIFS_UNIX_CAP_MASK;
3089 if (ctx && ctx->no_psx_acl)
3090 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3091 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3092 cifs_dbg(FYI, "negotiated posix acl support\n");
3094 cifs_sb->mnt_cifs_flags |=
3095 CIFS_MOUNT_POSIXACL;
3098 if (ctx && ctx->posix_paths == 0)
3099 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3100 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3101 cifs_dbg(FYI, "negotiate posix pathnames\n");
3103 cifs_sb->mnt_cifs_flags |=
3104 CIFS_MOUNT_POSIX_PATHS;
3107 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3108 #ifdef CONFIG_CIFS_DEBUG2
3109 if (cap & CIFS_UNIX_FCNTL_CAP)
3110 cifs_dbg(FYI, "FCNTL cap\n");
3111 if (cap & CIFS_UNIX_EXTATTR_CAP)
3112 cifs_dbg(FYI, "EXTATTR cap\n");
3113 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3114 cifs_dbg(FYI, "POSIX path cap\n");
3115 if (cap & CIFS_UNIX_XATTR_CAP)
3116 cifs_dbg(FYI, "XATTR cap\n");
3117 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3118 cifs_dbg(FYI, "POSIX ACL cap\n");
3119 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3120 cifs_dbg(FYI, "very large read cap\n");
3121 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3122 cifs_dbg(FYI, "very large write cap\n");
3123 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3124 cifs_dbg(FYI, "transport encryption cap\n");
3125 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3126 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3127 #endif /* CIFS_DEBUG2 */
3128 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3130 cifs_dbg(FYI, "resetting capabilities failed\n");
3132 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");
3137 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3139 int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
3141 struct smb3_fs_context *ctx = cifs_sb->ctx;
3143 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3145 spin_lock_init(&cifs_sb->tlink_tree_lock);
3146 cifs_sb->tlink_tree = RB_ROOT;
3148 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
3149 ctx->file_mode, ctx->dir_mode);
3151 /* this is needed for ASCII cp to Unicode converts */
3152 if (ctx->iocharset == NULL) {
3153 /* load_nls_default cannot return null */
3154 cifs_sb->local_nls = load_nls_default();
3156 cifs_sb->local_nls = load_nls(ctx->iocharset);
3157 if (cifs_sb->local_nls == NULL) {
3158 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3163 ctx->local_nls = cifs_sb->local_nls;
3165 smb3_update_mnt_flags(cifs_sb);
3168 cifs_dbg(FYI, "mounting share using direct i/o\n");
3169 if (ctx->cache_ro) {
3170 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
3171 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
3172 } else if (ctx->cache_rw) {
3173 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
3174 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
3175 CIFS_MOUNT_RW_CACHE);
3178 if ((ctx->cifs_acl) && (ctx->dynperm))
3179 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3182 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
3183 if (cifs_sb->prepath == NULL)
3185 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3191 /* Release all succeed connections */
3192 static inline void mount_put_conns(struct mount_ctx *mnt_ctx)
3197 cifs_put_tcon(mnt_ctx->tcon);
3198 else if (mnt_ctx->ses)
3199 cifs_put_smb_ses(mnt_ctx->ses);
3200 else if (mnt_ctx->server)
3201 cifs_put_tcp_session(mnt_ctx->server, 0);
3202 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3203 free_xid(mnt_ctx->xid);
3206 /* Get connections for tcp, ses and tcon */
3207 static int mount_get_conns(struct mount_ctx *mnt_ctx)
3210 struct TCP_Server_Info *server = NULL;
3211 struct cifs_ses *ses = NULL;
3212 struct cifs_tcon *tcon = NULL;
3213 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3214 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3219 /* get a reference to a tcp session */
3220 server = cifs_get_tcp_session(ctx, NULL);
3221 if (IS_ERR(server)) {
3222 rc = PTR_ERR(server);
3227 /* get a reference to a SMB session */
3228 ses = cifs_get_smb_ses(server, ctx);
3235 if ((ctx->persistent == true) && (!(ses->server->capabilities &
3236 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3237 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
3242 /* search for existing tcon to this server share */
3243 tcon = cifs_get_tcon(ses, ctx);
3250 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3251 if (tcon->posix_extensions)
3252 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3254 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3255 /* tell server which Unix caps we support */
3256 if (cap_unix(tcon->ses)) {
3258 * reset of caps checks mount to see if unix extensions disabled
3259 * for just this mount.
3261 reset_cifs_unix_caps(xid, tcon, cifs_sb, ctx);
3262 spin_lock(&tcon->ses->server->srv_lock);
3263 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3264 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3265 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3266 spin_unlock(&tcon->ses->server->srv_lock);
3270 spin_unlock(&tcon->ses->server->srv_lock);
3272 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3273 tcon->unix_ext = 0; /* server does not support them */
3275 /* do not care if a following call succeed - informational */
3276 if (!tcon->pipe && server->ops->qfs_tcon) {
3277 server->ops->qfs_tcon(xid, tcon, cifs_sb);
3278 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3279 if (tcon->fsDevInfo.DeviceCharacteristics &
3280 cpu_to_le32(FILE_READ_ONLY_DEVICE))
3281 cifs_dbg(VFS, "mounted to read only share\n");
3282 else if ((cifs_sb->mnt_cifs_flags &
3283 CIFS_MOUNT_RW_CACHE) == 0)
3284 cifs_dbg(VFS, "read only mount of RW share\n");
3285 /* no need to log a RW mount of a typical RW share */
3290 * Clamp the rsize/wsize mount arguments if they are too big for the server
3291 * and set the rsize/wsize to the negotiated values if not passed in by
3294 if ((cifs_sb->ctx->wsize == 0) ||
3295 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
3296 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
3297 if ((cifs_sb->ctx->rsize == 0) ||
3298 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
3299 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
3302 * The cookie is initialized from volume info returned above.
3303 * Inside cifs_fscache_get_super_cookie it checks
3304 * that we do not get super cookie twice.
3306 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
3307 cifs_fscache_get_super_cookie(tcon);
3310 mnt_ctx->server = server;
3312 mnt_ctx->tcon = tcon;
3318 static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3319 struct cifs_tcon *tcon)
3321 struct tcon_link *tlink;
3323 /* hang the tcon off of the superblock */
3324 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3328 tlink->tl_uid = ses->linux_uid;
3329 tlink->tl_tcon = tcon;
3330 tlink->tl_time = jiffies;
3331 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3332 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3334 cifs_sb->master_tlink = tlink;
3335 spin_lock(&cifs_sb->tlink_tree_lock);
3336 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3337 spin_unlock(&cifs_sb->tlink_tree_lock);
3339 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3344 #ifdef CONFIG_CIFS_DFS_UPCALL
3345 /* Get unique dfs connections */
3346 static int mount_get_dfs_conns(struct mount_ctx *mnt_ctx)
3350 mnt_ctx->fs_ctx->nosharesock = true;
3351 rc = mount_get_conns(mnt_ctx);
3352 if (mnt_ctx->server) {
3353 cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__);
3354 spin_lock(&mnt_ctx->server->srv_lock);
3355 mnt_ctx->server->is_dfs_conn = true;
3356 spin_unlock(&mnt_ctx->server->srv_lock);
3362 * cifs_build_path_to_root returns full path to root when we do not have an
3363 * existing connection (tcon)
3366 build_unc_path_to_root(const struct smb3_fs_context *ctx,
3367 const struct cifs_sb_info *cifs_sb, bool useppath)
3369 char *full_path, *pos;
3370 unsigned int pplen = useppath && ctx->prepath ?
3371 strlen(ctx->prepath) + 1 : 0;
3372 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
3374 if (unc_len > MAX_TREE_SIZE)
3375 return ERR_PTR(-EINVAL);
3377 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3378 if (full_path == NULL)
3379 return ERR_PTR(-ENOMEM);
3381 memcpy(full_path, ctx->UNC, unc_len);
3382 pos = full_path + unc_len;
3385 *pos = CIFS_DIR_SEP(cifs_sb);
3386 memcpy(pos + 1, ctx->prepath, pplen);
3390 *pos = '\0'; /* add trailing null */
3391 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3392 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3397 * expand_dfs_referral - Update cifs_sb from dfs referral path
3399 * cifs_sb->ctx->mount_options will be (re-)allocated to a string containing updated options for the
3400 * submount. Otherwise it will be left untouched.
3402 static int expand_dfs_referral(struct mount_ctx *mnt_ctx, const char *full_path,
3403 struct dfs_info3_param *referral)
3406 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3407 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3408 char *fake_devname = NULL, *mdata = NULL;
3410 mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, referral,
3412 if (IS_ERR(mdata)) {
3413 rc = PTR_ERR(mdata);
3417 * We can not clear out the whole structure since we no longer have an explicit
3418 * function to parse a mount-string. Instead we need to clear out the individual
3419 * fields that are no longer valid.
3421 kfree(ctx->prepath);
3422 ctx->prepath = NULL;
3423 rc = cifs_setup_volume_info(ctx, mdata, fake_devname);
3425 kfree(fake_devname);
3426 kfree(cifs_sb->ctx->mount_options);
3427 cifs_sb->ctx->mount_options = mdata;
3433 /* TODO: all callers to this are broken. We are not parsing mount_options here
3434 * we should pass a clone of the original context?
3437 cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname)
3442 cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname);
3443 rc = smb3_parse_devname(devname, ctx);
3445 cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc);
3453 rc = smb3_parse_opt(mntopts, "ip", &ip);
3455 cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc);
3459 rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip));
3462 cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__);
3467 if (ctx->nullauth) {
3468 cifs_dbg(FYI, "Anonymous login\n");
3469 kfree(ctx->username);
3470 ctx->username = NULL;
3471 } else if (ctx->username) {
3472 /* BB fixme parse for domain name here */
3473 cifs_dbg(FYI, "Username: %s\n", ctx->username);
3475 cifs_dbg(VFS, "No username specified\n");
3476 /* In userspace mount helper we can get user name from alternate
3477 locations such as env variables and files on disk */
3485 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3487 struct cifs_tcon *tcon,
3488 struct cifs_sb_info *cifs_sb,
3495 int skip = added_treename ? 1 : 0;
3497 sep = CIFS_DIR_SEP(cifs_sb);
3500 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3502 /* skip separators */
3507 /* next separator */
3508 while (*s && *s != sep)
3511 * if the treename is added, we then have to skip the first
3512 * part within the separators
3519 * temporarily null-terminate the path at the end of
3520 * the current component
3524 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3532 * Check if path is remote (i.e. a DFS share).
3534 * Return -EREMOTE if it is, otherwise 0 or -errno.
3536 static int is_path_remote(struct mount_ctx *mnt_ctx)
3539 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3540 struct TCP_Server_Info *server = mnt_ctx->server;
3541 unsigned int xid = mnt_ctx->xid;
3542 struct cifs_tcon *tcon = mnt_ctx->tcon;
3543 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3545 #ifdef CONFIG_CIFS_DFS_UPCALL
3546 bool nodfs = cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS;
3549 if (!server->ops->is_path_accessible)
3553 * cifs_build_path_to_root works only when we have a valid tcon
3555 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
3556 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3557 if (full_path == NULL)
3560 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3562 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3564 #ifdef CONFIG_CIFS_DFS_UPCALL
3571 /* path *might* exist with non-ASCII characters in DFS root
3572 * try again with full path (only if nodfs is not set) */
3573 if (rc == -ENOENT && is_tcon_dfs(tcon))
3574 rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon, cifs_sb,
3577 if (rc != 0 && rc != -EREMOTE)
3580 if (rc != -EREMOTE) {
3581 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
3582 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
3584 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3585 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3595 #ifdef CONFIG_CIFS_DFS_UPCALL
3596 static void set_root_ses(struct mount_ctx *mnt_ctx)
3599 spin_lock(&cifs_tcp_ses_lock);
3600 mnt_ctx->ses->ses_count++;
3601 spin_unlock(&cifs_tcp_ses_lock);
3602 dfs_cache_add_refsrv_session(&mnt_ctx->mount_id, mnt_ctx->ses);
3604 mnt_ctx->root_ses = mnt_ctx->ses;
3607 static int is_dfs_mount(struct mount_ctx *mnt_ctx, bool *isdfs, struct dfs_cache_tgt_list *root_tl)
3610 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3611 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3615 rc = mount_get_conns(mnt_ctx);
3617 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
3618 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
3620 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
3621 * to respond with PATH_NOT_COVERED to requests that include the prefix.
3623 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
3624 dfs_cache_find(mnt_ctx->xid, mnt_ctx->ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3625 ctx->UNC + 1, NULL, root_tl)) {
3628 /* Check if it is fully accessible and then mount it */
3629 rc = is_path_remote(mnt_ctx);
3632 else if (rc != -EREMOTE)
3638 static int connect_dfs_target(struct mount_ctx *mnt_ctx, const char *full_path,
3639 const char *ref_path, struct dfs_cache_tgt_iterator *tit)
3642 struct dfs_info3_param ref = {};
3643 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3644 char *oldmnt = cifs_sb->ctx->mount_options;
3646 cifs_dbg(FYI, "%s: full_path=%s ref_path=%s target=%s\n", __func__, full_path, ref_path,
3647 dfs_cache_get_tgt_name(tit));
3649 rc = dfs_cache_get_tgt_referral(ref_path, tit, &ref);
3653 rc = expand_dfs_referral(mnt_ctx, full_path, &ref);
3657 /* Connect to new target only if we were redirected (e.g. mount options changed) */
3658 if (oldmnt != cifs_sb->ctx->mount_options) {
3659 mount_put_conns(mnt_ctx);
3660 rc = mount_get_dfs_conns(mnt_ctx);
3663 if (cifs_is_referral_server(mnt_ctx->tcon, &ref))
3664 set_root_ses(mnt_ctx);
3665 rc = dfs_cache_update_tgthint(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3666 cifs_remap(cifs_sb), ref_path, tit);
3670 free_dfs_info_param(&ref);
3674 static int connect_dfs_root(struct mount_ctx *mnt_ctx, struct dfs_cache_tgt_list *root_tl)
3678 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3679 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3680 struct dfs_cache_tgt_iterator *tit;
3682 /* Put initial connections as they might be shared with other mounts. We need unique dfs
3683 * connections per mount to properly failover, so mount_get_dfs_conns() must be used from
3686 mount_put_conns(mnt_ctx);
3687 mount_get_dfs_conns(mnt_ctx);
3688 set_root_ses(mnt_ctx);
3690 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3691 if (IS_ERR(full_path))
3692 return PTR_ERR(full_path);
3694 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(ctx->UNC, cifs_sb->local_nls,
3695 cifs_remap(cifs_sb));
3696 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3697 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3698 mnt_ctx->origin_fullpath = NULL;
3702 /* Try all dfs root targets */
3703 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(root_tl);
3704 tit; tit = dfs_cache_get_next_tgt(root_tl, tit)) {
3705 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->origin_fullpath + 1, tit);
3707 mnt_ctx->leaf_fullpath = kstrdup(mnt_ctx->origin_fullpath, GFP_KERNEL);
3708 if (!mnt_ctx->leaf_fullpath)
3719 static int __follow_dfs_link(struct mount_ctx *mnt_ctx)
3722 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3723 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3725 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3726 struct dfs_cache_tgt_iterator *tit;
3728 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3729 if (IS_ERR(full_path))
3730 return PTR_ERR(full_path);
3732 kfree(mnt_ctx->leaf_fullpath);
3733 mnt_ctx->leaf_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3734 cifs_remap(cifs_sb));
3735 if (IS_ERR(mnt_ctx->leaf_fullpath)) {
3736 rc = PTR_ERR(mnt_ctx->leaf_fullpath);
3737 mnt_ctx->leaf_fullpath = NULL;
3741 /* Get referral from dfs link */
3742 rc = dfs_cache_find(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls,
3743 cifs_remap(cifs_sb), mnt_ctx->leaf_fullpath + 1, NULL, &tl);
3747 /* Try all dfs link targets. If an I/O fails from currently connected DFS target with an
3748 * error other than STATUS_PATH_NOT_COVERED (-EREMOTE), then retry it from other targets as
3749 * specified in MS-DFSC "3.1.5.2 I/O Operation to Target Fails with an Error Other Than
3750 * STATUS_PATH_NOT_COVERED."
3752 for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(&tl);
3753 tit; tit = dfs_cache_get_next_tgt(&tl, tit)) {
3754 rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->leaf_fullpath + 1, tit);
3756 rc = is_path_remote(mnt_ctx);
3757 if (!rc || rc == -EREMOTE)
3764 dfs_cache_free_tgts(&tl);
3768 static int follow_dfs_link(struct mount_ctx *mnt_ctx)
3771 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3772 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3776 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
3777 if (IS_ERR(full_path))
3778 return PTR_ERR(full_path);
3780 kfree(mnt_ctx->origin_fullpath);
3781 mnt_ctx->origin_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls,
3782 cifs_remap(cifs_sb));
3785 if (IS_ERR(mnt_ctx->origin_fullpath)) {
3786 rc = PTR_ERR(mnt_ctx->origin_fullpath);
3787 mnt_ctx->origin_fullpath = NULL;
3792 rc = __follow_dfs_link(mnt_ctx);
3793 if (!rc || rc != -EREMOTE)
3795 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
3800 /* Set up DFS referral paths for failover */
3801 static void setup_server_referral_paths(struct mount_ctx *mnt_ctx)
3803 struct TCP_Server_Info *server = mnt_ctx->server;
3805 mutex_lock(&server->refpath_lock);
3806 server->origin_fullpath = mnt_ctx->origin_fullpath;
3807 server->leaf_fullpath = mnt_ctx->leaf_fullpath;
3808 server->current_fullpath = mnt_ctx->leaf_fullpath;
3809 mutex_unlock(&server->refpath_lock);
3810 mnt_ctx->origin_fullpath = mnt_ctx->leaf_fullpath = NULL;
3813 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3816 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3817 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
3820 rc = is_dfs_mount(&mnt_ctx, &isdfs, &tl);
3826 /* proceed as DFS mount */
3827 uuid_gen(&mnt_ctx.mount_id);
3828 rc = connect_dfs_root(&mnt_ctx, &tl);
3829 dfs_cache_free_tgts(&tl);
3834 rc = is_path_remote(&mnt_ctx);
3836 rc = follow_dfs_link(&mnt_ctx);
3840 setup_server_referral_paths(&mnt_ctx);
3842 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3843 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
3845 cifs_autodisable_serverino(cifs_sb);
3847 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3848 * that have different prefix paths.
3850 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3851 kfree(cifs_sb->prepath);
3852 cifs_sb->prepath = ctx->prepath;
3853 ctx->prepath = NULL;
3854 uuid_copy(&cifs_sb->dfs_mount_id, &mnt_ctx.mount_id);
3857 free_xid(mnt_ctx.xid);
3858 cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3859 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3862 dfs_cache_put_refsrv_sessions(&mnt_ctx.mount_id);
3863 kfree(mnt_ctx.origin_fullpath);
3864 kfree(mnt_ctx.leaf_fullpath);
3865 mount_put_conns(&mnt_ctx);
3869 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3872 struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3874 rc = mount_get_conns(&mnt_ctx);
3879 rc = is_path_remote(&mnt_ctx);
3886 free_xid(mnt_ctx.xid);
3887 return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3890 mount_put_conns(&mnt_ctx);
3896 * Issue a TREE_CONNECT request.
3899 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3900 const char *tree, struct cifs_tcon *tcon,
3901 const struct nls_table *nls_codepage)
3903 struct smb_hdr *smb_buffer;
3904 struct smb_hdr *smb_buffer_response;
3907 unsigned char *bcc_ptr;
3910 __u16 bytes_left, count;
3915 smb_buffer = cifs_buf_get();
3916 if (smb_buffer == NULL)
3919 smb_buffer_response = smb_buffer;
3921 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3922 NULL /*no tid */ , 4 /*wct */ );
3924 smb_buffer->Mid = get_next_mid(ses->server);
3925 smb_buffer->Uid = ses->Suid;
3926 pSMB = (TCONX_REQ *) smb_buffer;
3927 pSMBr = (TCONX_RSP *) smb_buffer_response;
3929 pSMB->AndXCommand = 0xFF;
3930 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3931 bcc_ptr = &pSMB->Password[0];
3933 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
3934 *bcc_ptr = 0; /* password is null byte */
3935 bcc_ptr++; /* skip password */
3936 /* already aligned so no need to do it below */
3938 if (ses->server->sign)
3939 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3941 if (ses->capabilities & CAP_STATUS32) {
3942 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3944 if (ses->capabilities & CAP_DFS) {
3945 smb_buffer->Flags2 |= SMBFLG2_DFS;
3947 if (ses->capabilities & CAP_UNICODE) {
3948 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3950 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3951 6 /* max utf8 char length in bytes */ *
3952 (/* server len*/ + 256 /* share len */), nls_codepage);
3953 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
3954 bcc_ptr += 2; /* skip trailing null */
3955 } else { /* ASCII */
3956 strcpy(bcc_ptr, tree);
3957 bcc_ptr += strlen(tree) + 1;
3959 strcpy(bcc_ptr, "?????");
3960 bcc_ptr += strlen("?????");
3962 count = bcc_ptr - &pSMB->Password[0];
3963 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
3964 pSMB->ByteCount = cpu_to_le16(count);
3966 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3969 /* above now done in SendReceive */
3973 tcon->tid = smb_buffer_response->Tid;
3974 bcc_ptr = pByteArea(smb_buffer_response);
3975 bytes_left = get_bcc(smb_buffer_response);
3976 length = strnlen(bcc_ptr, bytes_left - 2);
3977 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3983 /* skip service field (NB: this field is always ASCII) */
3985 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3986 (bcc_ptr[2] == 'C')) {
3987 cifs_dbg(FYI, "IPC connection\n");
3991 } else if (length == 2) {
3992 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3993 /* the most common case */
3994 cifs_dbg(FYI, "disk share connection\n");
3997 bcc_ptr += length + 1;
3998 bytes_left -= (length + 1);
3999 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
4001 /* mostly informational -- no need to fail on error here */
4002 kfree(tcon->nativeFileSystem);
4003 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
4004 bytes_left, is_unicode,
4007 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
4009 if ((smb_buffer_response->WordCount == 3) ||
4010 (smb_buffer_response->WordCount == 7))
4011 /* field is in same location */
4012 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4015 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
4018 cifs_buf_release(smb_buffer);
4022 static void delayed_free(struct rcu_head *p)
4024 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
4026 unload_nls(cifs_sb->local_nls);
4027 smb3_cleanup_fs_context(cifs_sb->ctx);
4032 cifs_umount(struct cifs_sb_info *cifs_sb)
4034 struct rb_root *root = &cifs_sb->tlink_tree;
4035 struct rb_node *node;
4036 struct tcon_link *tlink;
4038 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4040 spin_lock(&cifs_sb->tlink_tree_lock);
4041 while ((node = rb_first(root))) {
4042 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4043 cifs_get_tlink(tlink);
4044 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4045 rb_erase(node, root);
4047 spin_unlock(&cifs_sb->tlink_tree_lock);
4048 cifs_put_tlink(tlink);
4049 spin_lock(&cifs_sb->tlink_tree_lock);
4051 spin_unlock(&cifs_sb->tlink_tree_lock);
4053 kfree(cifs_sb->prepath);
4054 #ifdef CONFIG_CIFS_DFS_UPCALL
4055 dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id);
4057 call_rcu(&cifs_sb->rcu, delayed_free);
4061 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
4062 struct TCP_Server_Info *server)
4066 if (!server->ops->need_neg || !server->ops->negotiate)
4069 /* only send once per connect */
4070 spin_lock(&server->srv_lock);
4071 if (!server->ops->need_neg(server) ||
4072 server->tcpStatus != CifsNeedNegotiate) {
4073 spin_unlock(&server->srv_lock);
4076 server->tcpStatus = CifsInNegotiate;
4077 spin_unlock(&server->srv_lock);
4079 rc = server->ops->negotiate(xid, ses, server);
4081 spin_lock(&server->srv_lock);
4082 if (server->tcpStatus == CifsInNegotiate)
4083 server->tcpStatus = CifsGood;
4086 spin_unlock(&server->srv_lock);
4088 spin_lock(&server->srv_lock);
4089 if (server->tcpStatus == CifsInNegotiate)
4090 server->tcpStatus = CifsNeedNegotiate;
4091 spin_unlock(&server->srv_lock);
4098 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4099 struct TCP_Server_Info *server,
4100 struct nls_table *nls_info)
4103 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
4104 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
4105 bool is_binding = false;
4107 spin_lock(&ses->ses_lock);
4108 if (server->dstaddr.ss_family == AF_INET6)
4109 scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
4111 scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
4113 if (ses->ses_status != SES_GOOD &&
4114 ses->ses_status != SES_NEW &&
4115 ses->ses_status != SES_NEED_RECON) {
4116 spin_unlock(&ses->ses_lock);
4120 /* only send once per connect */
4121 spin_lock(&ses->chan_lock);
4122 if (CIFS_ALL_CHANS_GOOD(ses) ||
4123 cifs_chan_in_reconnect(ses, server)) {
4124 spin_unlock(&ses->chan_lock);
4125 spin_unlock(&ses->ses_lock);
4128 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
4129 cifs_chan_set_in_reconnect(ses, server);
4130 spin_unlock(&ses->chan_lock);
4133 ses->ses_status = SES_IN_SETUP;
4134 spin_unlock(&ses->ses_lock);
4137 ses->capabilities = server->capabilities;
4138 if (!linuxExtEnabled)
4139 ses->capabilities &= (~server->vals->cap_unix);
4141 if (ses->auth_key.response) {
4142 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
4143 ses->auth_key.response);
4144 kfree_sensitive(ses->auth_key.response);
4145 ses->auth_key.response = NULL;
4146 ses->auth_key.len = 0;
4150 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
4151 server->sec_mode, server->capabilities, server->timeAdj);
4153 if (server->ops->sess_setup)
4154 rc = server->ops->sess_setup(xid, ses, server, nls_info);
4157 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
4158 spin_lock(&ses->ses_lock);
4159 if (ses->ses_status == SES_IN_SETUP)
4160 ses->ses_status = SES_NEED_RECON;
4161 spin_lock(&ses->chan_lock);
4162 cifs_chan_clear_in_reconnect(ses, server);
4163 spin_unlock(&ses->chan_lock);
4164 spin_unlock(&ses->ses_lock);
4166 spin_lock(&ses->ses_lock);
4167 if (ses->ses_status == SES_IN_SETUP)
4168 ses->ses_status = SES_GOOD;
4169 spin_lock(&ses->chan_lock);
4170 cifs_chan_clear_in_reconnect(ses, server);
4171 cifs_chan_clear_need_reconnect(ses, server);
4172 spin_unlock(&ses->chan_lock);
4173 spin_unlock(&ses->ses_lock);
4180 cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
4182 ctx->sectype = ses->sectype;
4184 /* krb5 is special, since we don't need username or pw */
4185 if (ctx->sectype == Kerberos)
4188 return cifs_set_cifscreds(ctx, ses);
4191 static struct cifs_tcon *
4192 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
4195 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4196 struct cifs_ses *ses;
4197 struct cifs_tcon *tcon = NULL;
4198 struct smb3_fs_context *ctx;
4200 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
4202 return ERR_PTR(-ENOMEM);
4204 ctx->local_nls = cifs_sb->local_nls;
4205 ctx->linux_uid = fsuid;
4206 ctx->cred_uid = fsuid;
4207 ctx->UNC = master_tcon->tree_name;
4208 ctx->retry = master_tcon->retry;
4209 ctx->nocase = master_tcon->nocase;
4210 ctx->nohandlecache = master_tcon->nohandlecache;
4211 ctx->local_lease = master_tcon->local_lease;
4212 ctx->no_lease = master_tcon->no_lease;
4213 ctx->resilient = master_tcon->use_resilient;
4214 ctx->persistent = master_tcon->use_persistent;
4215 ctx->handle_timeout = master_tcon->handle_timeout;
4216 ctx->no_linux_ext = !master_tcon->unix_ext;
4217 ctx->linux_ext = master_tcon->posix_extensions;
4218 ctx->sectype = master_tcon->ses->sectype;
4219 ctx->sign = master_tcon->ses->sign;
4220 ctx->seal = master_tcon->seal;
4221 ctx->witness = master_tcon->use_witness;
4223 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
4229 /* get a reference for the same TCP session */
4230 spin_lock(&cifs_tcp_ses_lock);
4231 ++master_tcon->ses->server->srv_count;
4232 spin_unlock(&cifs_tcp_ses_lock);
4234 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
4236 tcon = (struct cifs_tcon *)ses;
4237 cifs_put_tcp_session(master_tcon->ses->server, 0);
4241 tcon = cifs_get_tcon(ses, ctx);
4243 cifs_put_smb_ses(ses);
4247 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4249 reset_cifs_unix_caps(0, tcon, NULL, ctx);
4250 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
4253 kfree(ctx->username);
4254 kfree_sensitive(ctx->password);
4261 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4263 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4266 /* find and return a tlink with given uid */
4267 static struct tcon_link *
4268 tlink_rb_search(struct rb_root *root, kuid_t uid)
4270 struct rb_node *node = root->rb_node;
4271 struct tcon_link *tlink;
4274 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4276 if (uid_gt(tlink->tl_uid, uid))
4277 node = node->rb_left;
4278 else if (uid_lt(tlink->tl_uid, uid))
4279 node = node->rb_right;
4286 /* insert a tcon_link into the tree */
4288 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4290 struct rb_node **new = &(root->rb_node), *parent = NULL;
4291 struct tcon_link *tlink;
4294 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4297 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4298 new = &((*new)->rb_left);
4300 new = &((*new)->rb_right);
4303 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4304 rb_insert_color(&new_tlink->tl_rbnode, root);
4308 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4311 * If the superblock doesn't refer to a multiuser mount, then just return
4312 * the master tcon for the mount.
4314 * First, search the rbtree for an existing tcon for this fsuid. If one
4315 * exists, then check to see if it's pending construction. If it is then wait
4316 * for construction to complete. Once it's no longer pending, check to see if
4317 * it failed and either return an error or retry construction, depending on
4320 * If one doesn't exist then insert a new tcon_link struct into the tree and
4321 * try to construct a new one.
4324 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4327 kuid_t fsuid = current_fsuid();
4328 struct tcon_link *tlink, *newtlink;
4330 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4331 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4333 spin_lock(&cifs_sb->tlink_tree_lock);
4334 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4336 cifs_get_tlink(tlink);
4337 spin_unlock(&cifs_sb->tlink_tree_lock);
4339 if (tlink == NULL) {
4340 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4341 if (newtlink == NULL)
4342 return ERR_PTR(-ENOMEM);
4343 newtlink->tl_uid = fsuid;
4344 newtlink->tl_tcon = ERR_PTR(-EACCES);
4345 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4346 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4347 cifs_get_tlink(newtlink);
4349 spin_lock(&cifs_sb->tlink_tree_lock);
4350 /* was one inserted after previous search? */
4351 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4353 cifs_get_tlink(tlink);
4354 spin_unlock(&cifs_sb->tlink_tree_lock);
4356 goto wait_for_construction;
4359 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4360 spin_unlock(&cifs_sb->tlink_tree_lock);
4362 wait_for_construction:
4363 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4364 TASK_INTERRUPTIBLE);
4366 cifs_put_tlink(tlink);
4367 return ERR_PTR(-ERESTARTSYS);
4370 /* if it's good, return it */
4371 if (!IS_ERR(tlink->tl_tcon))
4374 /* return error if we tried this already recently */
4375 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4376 cifs_put_tlink(tlink);
4377 return ERR_PTR(-EACCES);
4380 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4381 goto wait_for_construction;
4384 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4385 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4386 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4388 if (IS_ERR(tlink->tl_tcon)) {
4389 cifs_put_tlink(tlink);
4390 return ERR_PTR(-EACCES);
4397 * periodic workqueue job that scans tcon_tree for a superblock and closes
4401 cifs_prune_tlinks(struct work_struct *work)
4403 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4405 struct rb_root *root = &cifs_sb->tlink_tree;
4406 struct rb_node *node;
4407 struct rb_node *tmp;
4408 struct tcon_link *tlink;
4411 * Because we drop the spinlock in the loop in order to put the tlink
4412 * it's not guarded against removal of links from the tree. The only
4413 * places that remove entries from the tree are this function and
4414 * umounts. Because this function is non-reentrant and is canceled
4415 * before umount can proceed, this is safe.
4417 spin_lock(&cifs_sb->tlink_tree_lock);
4418 node = rb_first(root);
4419 while (node != NULL) {
4421 node = rb_next(tmp);
4422 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4424 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4425 atomic_read(&tlink->tl_count) != 0 ||
4426 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4429 cifs_get_tlink(tlink);
4430 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4431 rb_erase(tmp, root);
4433 spin_unlock(&cifs_sb->tlink_tree_lock);
4434 cifs_put_tlink(tlink);
4435 spin_lock(&cifs_sb->tlink_tree_lock);
4437 spin_unlock(&cifs_sb->tlink_tree_lock);
4439 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4443 #ifdef CONFIG_CIFS_DFS_UPCALL
4444 /* Update dfs referral path of superblock */
4445 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
4449 size_t len = strlen(target);
4450 char *refpath, *npath;
4452 if (unlikely(len < 2 || *target != '\\'))
4455 if (target[1] == '\\') {
4457 refpath = kmalloc(len, GFP_KERNEL);
4461 scnprintf(refpath, len, "%s", target);
4463 len += sizeof("\\");
4464 refpath = kmalloc(len, GFP_KERNEL);
4468 scnprintf(refpath, len, "\\%s", target);
4471 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
4474 if (IS_ERR(npath)) {
4475 rc = PTR_ERR(npath);
4477 mutex_lock(&server->refpath_lock);
4478 kfree(server->leaf_fullpath);
4479 server->leaf_fullpath = npath;
4480 mutex_unlock(&server->refpath_lock);
4481 server->current_fullpath = server->leaf_fullpath;
4486 static int target_share_matches_server(struct TCP_Server_Info *server, const char *tcp_host,
4487 size_t tcp_host_len, char *share, bool *target_match)
4490 const char *dfs_host;
4491 size_t dfs_host_len;
4493 *target_match = true;
4494 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
4496 /* Check if hostnames or addresses match */
4497 if (dfs_host_len != tcp_host_len || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
4498 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
4499 dfs_host, (int)tcp_host_len, tcp_host);
4500 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
4502 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
4507 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
4508 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4509 struct dfs_cache_tgt_list *tl)
4512 struct TCP_Server_Info *server = tcon->ses->server;
4513 const struct smb_version_operations *ops = server->ops;
4514 struct cifs_tcon *ipc = tcon->ses->tcon_ipc;
4515 char *share = NULL, *prefix = NULL;
4516 const char *tcp_host;
4517 size_t tcp_host_len;
4518 struct dfs_cache_tgt_iterator *tit;
4521 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
4523 tit = dfs_cache_get_tgt_iterator(tl);
4529 /* Try to tree connect to all dfs targets */
4530 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
4531 const char *target = dfs_cache_get_tgt_name(tit);
4532 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
4536 share = prefix = NULL;
4538 /* Check if share matches with tcp ses */
4539 rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix);
4541 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
4545 rc = target_share_matches_server(server, tcp_host, tcp_host_len, share,
4549 if (!target_match) {
4554 if (ipc->need_reconnect) {
4555 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4556 rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls);
4561 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
4563 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4567 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
4568 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
4569 * reconnect so either the demultiplex thread or the echo worker will reconnect to
4570 * newly resolved target.
4572 if (dfs_cache_find(xid, tcon->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
4574 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
4577 rc = dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit);
4579 rc = cifs_update_super_prepath(cifs_sb, prefix);
4581 /* Target is another dfs share */
4582 rc = update_server_fullpath(server, cifs_sb, target);
4583 dfs_cache_free_tgts(tl);
4587 list_replace_init(&ntl.tl_list, &tl->tl_list);
4589 dfs_cache_free_tgts(&ntl);
4601 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
4602 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
4603 struct dfs_cache_tgt_list *tl)
4607 struct TCP_Server_Info *server = tcon->ses->server;
4610 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
4611 if (!rc || rc != -EREMOTE)
4613 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
4615 * If we couldn't tree connect to any targets from last referral path, then retry from
4616 * original referral path.
4618 if (rc && server->current_fullpath != server->origin_fullpath) {
4619 server->current_fullpath = server->origin_fullpath;
4620 cifs_signal_cifsd_for_reconnect(server, true);
4623 dfs_cache_free_tgts(tl);
4627 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4630 struct TCP_Server_Info *server = tcon->ses->server;
4631 const struct smb_version_operations *ops = server->ops;
4632 struct super_block *sb = NULL;
4633 struct cifs_sb_info *cifs_sb;
4634 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
4636 struct dfs_info3_param ref = {0};
4638 /* only send once per connect */
4639 spin_lock(&tcon->tc_lock);
4640 if (tcon->ses->ses_status != SES_GOOD ||
4641 (tcon->status != TID_NEW &&
4642 tcon->status != TID_NEED_TCON)) {
4643 spin_unlock(&tcon->tc_lock);
4646 tcon->status = TID_IN_TCON;
4647 spin_unlock(&tcon->tc_lock);
4649 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
4656 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
4657 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
4661 sb = cifs_get_tcp_super(server);
4664 cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
4668 cifs_sb = CIFS_SB(sb);
4670 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
4671 if (!server->current_fullpath ||
4672 dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) {
4673 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, cifs_sb->local_nls);
4677 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
4679 free_dfs_info_param(&ref);
4683 cifs_put_tcp_super(sb);
4686 spin_lock(&tcon->tc_lock);
4687 if (tcon->status == TID_IN_TCON)
4688 tcon->status = TID_NEED_TCON;
4689 spin_unlock(&tcon->tc_lock);
4691 spin_lock(&tcon->tc_lock);
4692 if (tcon->status == TID_IN_TCON)
4693 tcon->status = TID_GOOD;
4694 spin_unlock(&tcon->tc_lock);
4695 tcon->need_reconnect = false;
4701 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4704 const struct smb_version_operations *ops = tcon->ses->server->ops;
4706 /* only send once per connect */
4707 spin_lock(&tcon->tc_lock);
4708 if (tcon->ses->ses_status != SES_GOOD ||
4709 (tcon->status != TID_NEW &&
4710 tcon->status != TID_NEED_TCON)) {
4711 spin_unlock(&tcon->tc_lock);
4714 tcon->status = TID_IN_TCON;
4715 spin_unlock(&tcon->tc_lock);
4717 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
4719 spin_lock(&tcon->tc_lock);
4720 if (tcon->status == TID_IN_TCON)
4721 tcon->status = TID_NEED_TCON;
4722 spin_unlock(&tcon->tc_lock);
4724 spin_lock(&tcon->tc_lock);
4725 if (tcon->status == TID_IN_TCON)
4726 tcon->status = TID_GOOD;
4727 tcon->need_reconnect = false;
4728 spin_unlock(&tcon->tc_lock);