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
50 #include "dfs_cache.h"
52 #include "fs_context.h"
55 extern mempool_t *cifs_req_poolp;
56 extern bool disable_legacy_dialects;
58 /* FIXME: should these be tunable? */
59 #define TLINK_ERROR_EXPIRE (1 * HZ)
60 #define TLINK_IDLE_EXPIRE (600 * HZ)
62 /* Drop the connection to not overload the server */
63 #define NUM_STATUS_IO_TIMEOUT 5
65 static int ip_connect(struct TCP_Server_Info *server);
66 static int generic_ip_connect(struct TCP_Server_Info *server);
67 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
68 static void cifs_prune_tlinks(struct work_struct *work);
71 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
72 * get their ip addresses changed at some point.
74 * This should be called with server->srv_mutex held.
76 static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server)
81 struct sockaddr_storage ss;
83 if (!server->hostname)
86 /* if server hostname isn't populated, there's nothing to do here */
87 if (server->hostname[0] == '\0')
90 len = strlen(server->hostname) + 3;
92 unc = kmalloc(len, GFP_KERNEL);
94 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
97 scnprintf(unc, len, "\\\\%s", server->hostname);
99 spin_lock(&server->srv_lock);
100 ss = server->dstaddr;
101 spin_unlock(&server->srv_lock);
103 rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL);
107 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
108 __func__, server->hostname, rc);
110 spin_lock(&server->srv_lock);
111 memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr));
112 spin_unlock(&server->srv_lock);
119 static void smb2_query_server_interfaces(struct work_struct *work)
122 struct cifs_tcon *tcon = container_of(work,
124 query_interfaces.work);
127 * query server network interfaces, in case they change
129 rc = SMB3_request_interfaces(0, tcon, false);
131 cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n",
135 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
136 (SMB_INTERFACE_POLL_INTERVAL * HZ));
140 * Update the tcpStatus for the server.
141 * This is used to signal the cifsd thread to call cifs_reconnect
142 * ONLY cifsd thread should call cifs_reconnect. For any other
143 * thread, use this function
145 * @server: the tcp ses for which reconnect is needed
146 * @all_channels: if this needs to be done for all channels
149 cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server,
152 struct TCP_Server_Info *pserver;
153 struct cifs_ses *ses;
156 /* If server is a channel, select the primary channel */
157 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
159 spin_lock(&pserver->srv_lock);
161 pserver->tcpStatus = CifsNeedReconnect;
162 spin_unlock(&pserver->srv_lock);
165 spin_unlock(&pserver->srv_lock);
167 spin_lock(&cifs_tcp_ses_lock);
168 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
169 spin_lock(&ses->chan_lock);
170 for (i = 0; i < ses->chan_count; i++) {
171 spin_lock(&ses->chans[i].server->srv_lock);
172 ses->chans[i].server->tcpStatus = CifsNeedReconnect;
173 spin_unlock(&ses->chans[i].server->srv_lock);
175 spin_unlock(&ses->chan_lock);
177 spin_unlock(&cifs_tcp_ses_lock);
181 * Mark all sessions and tcons for reconnect.
182 * IMPORTANT: make sure that this gets called only from
183 * cifsd thread. For any other thread, use
184 * cifs_signal_cifsd_for_reconnect
186 * @server: the tcp ses for which reconnect is needed
187 * @server needs to be previously set to CifsNeedReconnect.
188 * @mark_smb_session: whether even sessions need to be marked
191 cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server,
192 bool mark_smb_session)
194 struct TCP_Server_Info *pserver;
195 struct cifs_ses *ses, *nses;
196 struct cifs_tcon *tcon;
199 * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they
200 * are not used until reconnected.
202 cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__);
204 /* If server is a channel, select the primary channel */
205 pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
208 spin_lock(&cifs_tcp_ses_lock);
209 list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) {
210 /* check if iface is still active */
211 if (!cifs_chan_is_iface_active(ses, server))
212 cifs_chan_update_iface(ses, server);
214 spin_lock(&ses->chan_lock);
215 if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) {
216 spin_unlock(&ses->chan_lock);
220 if (mark_smb_session)
221 CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses);
223 cifs_chan_set_need_reconnect(ses, server);
225 cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
226 __func__, ses->chans_need_reconnect);
228 /* If all channels need reconnect, then tcon needs reconnect */
229 if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) {
230 spin_unlock(&ses->chan_lock);
233 spin_unlock(&ses->chan_lock);
235 spin_lock(&ses->ses_lock);
236 ses->ses_status = SES_NEED_RECON;
237 spin_unlock(&ses->ses_lock);
239 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
240 tcon->need_reconnect = true;
241 spin_lock(&tcon->tc_lock);
242 tcon->status = TID_NEED_RECON;
243 spin_unlock(&tcon->tc_lock);
246 ses->tcon_ipc->need_reconnect = true;
247 spin_lock(&ses->tcon_ipc->tc_lock);
248 ses->tcon_ipc->status = TID_NEED_RECON;
249 spin_unlock(&ses->tcon_ipc->tc_lock);
252 spin_unlock(&cifs_tcp_ses_lock);
256 cifs_abort_connection(struct TCP_Server_Info *server)
258 struct mid_q_entry *mid, *nmid;
259 struct list_head retry_list;
262 server->max_read = 0;
264 /* do not want to be sending data on a socket we are freeing */
265 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
266 cifs_server_lock(server);
267 if (server->ssocket) {
268 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state,
269 server->ssocket->flags);
270 kernel_sock_shutdown(server->ssocket, SHUT_WR);
271 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state,
272 server->ssocket->flags);
273 sock_release(server->ssocket);
274 server->ssocket = NULL;
276 server->sequence_number = 0;
277 server->session_estab = false;
278 kfree_sensitive(server->session_key.response);
279 server->session_key.response = NULL;
280 server->session_key.len = 0;
281 server->lstrp = jiffies;
283 /* mark submitted MIDs for retry and issue callback */
284 INIT_LIST_HEAD(&retry_list);
285 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
286 spin_lock(&server->mid_lock);
287 list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) {
288 kref_get(&mid->refcount);
289 if (mid->mid_state == MID_REQUEST_SUBMITTED)
290 mid->mid_state = MID_RETRY_NEEDED;
291 list_move(&mid->qhead, &retry_list);
292 mid->mid_flags |= MID_DELETED;
294 spin_unlock(&server->mid_lock);
295 cifs_server_unlock(server);
297 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
298 list_for_each_entry_safe(mid, nmid, &retry_list, qhead) {
299 list_del_init(&mid->qhead);
304 if (cifs_rdma_enabled(server)) {
305 cifs_server_lock(server);
306 smbd_destroy(server);
307 cifs_server_unlock(server);
311 static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets)
313 spin_lock(&server->srv_lock);
314 server->nr_targets = num_targets;
315 if (server->tcpStatus == CifsExiting) {
316 /* the demux thread will exit normally next time through the loop */
317 spin_unlock(&server->srv_lock);
318 wake_up(&server->response_q);
322 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
323 trace_smb3_reconnect(server->CurrentMid, server->conn_id,
325 server->tcpStatus = CifsNeedReconnect;
327 spin_unlock(&server->srv_lock);
332 * cifs tcp session reconnection
334 * mark tcp session as reconnecting so temporarily locked
335 * mark all smb sessions as reconnecting for tcp session
336 * reconnect tcp session
337 * wake up waiters on reconnection? - (not needed currently)
339 * if mark_smb_session is passed as true, unconditionally mark
340 * the smb session (and tcon) for reconnect as well. This value
341 * doesn't really matter for non-multichannel scenario.
344 static int __cifs_reconnect(struct TCP_Server_Info *server,
345 bool mark_smb_session)
349 if (!cifs_tcp_ses_needs_reconnect(server, 1))
352 cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session);
354 cifs_abort_connection(server);
358 cifs_server_lock(server);
360 if (!cifs_swn_set_server_dstaddr(server)) {
361 /* resolve the hostname again to make sure that IP address is up-to-date */
362 rc = reconn_set_ipaddr_from_hostname(server);
363 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
366 if (cifs_rdma_enabled(server))
367 rc = smbd_reconnect(server);
369 rc = generic_ip_connect(server);
371 cifs_server_unlock(server);
372 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
375 atomic_inc(&tcpSesReconnectCount);
376 set_credits(server, 1);
377 spin_lock(&server->srv_lock);
378 if (server->tcpStatus != CifsExiting)
379 server->tcpStatus = CifsNeedNegotiate;
380 spin_unlock(&server->srv_lock);
381 cifs_swn_reset_server_dstaddr(server);
382 cifs_server_unlock(server);
383 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
385 } while (server->tcpStatus == CifsNeedReconnect);
387 spin_lock(&server->srv_lock);
388 if (server->tcpStatus == CifsNeedNegotiate)
389 mod_delayed_work(cifsiod_wq, &server->echo, 0);
390 spin_unlock(&server->srv_lock);
392 wake_up(&server->response_q);
396 #ifdef CONFIG_CIFS_DFS_UPCALL
397 static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target)
402 if (!cifs_swn_set_server_dstaddr(server)) {
403 if (server->hostname != target) {
404 hostname = extract_hostname(target);
405 if (!IS_ERR(hostname)) {
406 spin_lock(&server->srv_lock);
407 kfree(server->hostname);
408 server->hostname = hostname;
409 spin_unlock(&server->srv_lock);
411 cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n",
412 __func__, PTR_ERR(hostname));
413 cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__,
417 /* resolve the hostname again to make sure that IP address is up-to-date. */
418 rc = reconn_set_ipaddr_from_hostname(server);
419 cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc);
421 /* Reconnect the socket */
422 if (cifs_rdma_enabled(server))
423 rc = smbd_reconnect(server);
425 rc = generic_ip_connect(server);
430 static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl,
431 struct dfs_cache_tgt_iterator **target_hint)
434 struct dfs_cache_tgt_iterator *tit;
438 /* If dfs target list is empty, then reconnect to last server */
439 tit = dfs_cache_get_tgt_iterator(tl);
441 return __reconnect_target_unlocked(server, server->hostname);
443 /* Otherwise, try every dfs target in @tl */
444 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
445 rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit));
454 static int reconnect_dfs_server(struct TCP_Server_Info *server)
457 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
458 struct dfs_cache_tgt_iterator *target_hint = NULL;
462 * Determine the number of dfs targets the referral path in @cifs_sb resolves to.
464 * smb2_reconnect() needs to know how long it should wait based upon the number of dfs
465 * targets (server->nr_targets). It's also possible that the cached referral was cleared
466 * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after
467 * refreshing the referral, so, in this case, default it to 1.
469 mutex_lock(&server->refpath_lock);
470 if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl))
471 num_targets = dfs_cache_get_nr_tgts(&tl);
472 mutex_unlock(&server->refpath_lock);
476 if (!cifs_tcp_ses_needs_reconnect(server, num_targets))
480 * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a
481 * different server or share during failover. It could be improved by adding some logic to
482 * only do that in case it connects to a different server or share, though.
484 cifs_mark_tcp_ses_conns_for_reconnect(server, true);
486 cifs_abort_connection(server);
490 cifs_server_lock(server);
492 rc = reconnect_target_unlocked(server, &tl, &target_hint);
494 /* Failed to reconnect socket */
495 cifs_server_unlock(server);
496 cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc);
501 * Socket was created. Update tcp session status to CifsNeedNegotiate so that a
502 * process waiting for reconnect will know it needs to re-establish session and tcon
503 * through the reconnected target server.
505 atomic_inc(&tcpSesReconnectCount);
506 set_credits(server, 1);
507 spin_lock(&server->srv_lock);
508 if (server->tcpStatus != CifsExiting)
509 server->tcpStatus = CifsNeedNegotiate;
510 spin_unlock(&server->srv_lock);
511 cifs_swn_reset_server_dstaddr(server);
512 cifs_server_unlock(server);
513 mod_delayed_work(cifsiod_wq, &server->reconnect, 0);
514 } while (server->tcpStatus == CifsNeedReconnect);
516 mutex_lock(&server->refpath_lock);
517 dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint);
518 mutex_unlock(&server->refpath_lock);
519 dfs_cache_free_tgts(&tl);
521 /* Need to set up echo worker again once connection has been established */
522 spin_lock(&server->srv_lock);
523 if (server->tcpStatus == CifsNeedNegotiate)
524 mod_delayed_work(cifsiod_wq, &server->echo, 0);
525 spin_unlock(&server->srv_lock);
527 wake_up(&server->response_q);
531 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
533 mutex_lock(&server->refpath_lock);
534 if (!server->leaf_fullpath) {
535 mutex_unlock(&server->refpath_lock);
536 return __cifs_reconnect(server, mark_smb_session);
538 mutex_unlock(&server->refpath_lock);
540 return reconnect_dfs_server(server);
543 int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session)
545 return __cifs_reconnect(server, mark_smb_session);
550 cifs_echo_request(struct work_struct *work)
553 struct TCP_Server_Info *server = container_of(work,
554 struct TCP_Server_Info, echo.work);
557 * We cannot send an echo if it is disabled.
558 * Also, no need to ping if we got a response recently.
561 if (server->tcpStatus == CifsNeedReconnect ||
562 server->tcpStatus == CifsExiting ||
563 server->tcpStatus == CifsNew ||
564 (server->ops->can_echo && !server->ops->can_echo(server)) ||
565 time_before(jiffies, server->lstrp + server->echo_interval - HZ))
568 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
569 cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc);
571 /* Check witness registrations */
575 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
579 allocate_buffers(struct TCP_Server_Info *server)
581 if (!server->bigbuf) {
582 server->bigbuf = (char *)cifs_buf_get();
583 if (!server->bigbuf) {
584 cifs_server_dbg(VFS, "No memory for large SMB response\n");
586 /* retry will check if exiting */
589 } else if (server->large_buf) {
590 /* we are reusing a dirty large buf, clear its start */
591 memset(server->bigbuf, 0, HEADER_SIZE(server));
594 if (!server->smallbuf) {
595 server->smallbuf = (char *)cifs_small_buf_get();
596 if (!server->smallbuf) {
597 cifs_server_dbg(VFS, "No memory for SMB response\n");
599 /* retry will check if exiting */
602 /* beginning of smb buffer is cleared in our buf_get */
604 /* if existing small buf clear beginning */
605 memset(server->smallbuf, 0, HEADER_SIZE(server));
612 server_unresponsive(struct TCP_Server_Info *server)
615 * We need to wait 3 echo intervals to make sure we handle such
617 * 1s client sends a normal SMB request
618 * 2s client gets a response
619 * 30s echo workqueue job pops, and decides we got a response recently
620 * and don't need to send another
622 * 65s kernel_recvmsg times out, and we see that we haven't gotten
623 * a response in >60s.
625 spin_lock(&server->srv_lock);
626 if ((server->tcpStatus == CifsGood ||
627 server->tcpStatus == CifsNeedNegotiate) &&
628 (!server->ops->can_echo || server->ops->can_echo(server)) &&
629 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
630 spin_unlock(&server->srv_lock);
631 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
632 (3 * server->echo_interval) / HZ);
633 cifs_reconnect(server, false);
636 spin_unlock(&server->srv_lock);
642 zero_credits(struct TCP_Server_Info *server)
646 spin_lock(&server->req_lock);
647 val = server->credits + server->echo_credits + server->oplock_credits;
648 if (server->in_flight == 0 && val == 0) {
649 spin_unlock(&server->req_lock);
652 spin_unlock(&server->req_lock);
657 cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
662 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
665 /* reconnect if no credits and no requests in flight */
666 if (zero_credits(server)) {
667 cifs_reconnect(server, false);
668 return -ECONNABORTED;
671 if (server_unresponsive(server))
672 return -ECONNABORTED;
673 if (cifs_rdma_enabled(server) && server->smbd_conn)
674 length = smbd_recv(server->smbd_conn, smb_msg);
676 length = sock_recvmsg(server->ssocket, smb_msg, 0);
678 spin_lock(&server->srv_lock);
679 if (server->tcpStatus == CifsExiting) {
680 spin_unlock(&server->srv_lock);
684 if (server->tcpStatus == CifsNeedReconnect) {
685 spin_unlock(&server->srv_lock);
686 cifs_reconnect(server, false);
687 return -ECONNABORTED;
689 spin_unlock(&server->srv_lock);
691 if (length == -ERESTARTSYS ||
695 * Minimum sleep to prevent looping, allowing socket
696 * to clear and app threads to set tcpStatus
697 * CifsNeedReconnect if server hung.
699 usleep_range(1000, 2000);
705 cifs_dbg(FYI, "Received no data or error: %d\n", length);
706 cifs_reconnect(server, false);
707 return -ECONNABORTED;
714 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
715 unsigned int to_read)
717 struct msghdr smb_msg = {};
718 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
719 iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read);
721 return cifs_readv_from_socket(server, &smb_msg);
725 cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read)
727 struct msghdr smb_msg = {};
730 * iov_iter_discard already sets smb_msg.type and count and iov_offset
731 * and cifs_readv_from_socket sets msg_control and msg_controllen
732 * so little to initialize in struct msghdr
734 iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read);
736 return cifs_readv_from_socket(server, &smb_msg);
740 cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
741 unsigned int page_offset, unsigned int to_read)
743 struct msghdr smb_msg = {};
746 bvec_set_page(&bv, page, to_read, page_offset);
747 iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read);
748 return cifs_readv_from_socket(server, &smb_msg);
752 cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter,
753 unsigned int to_read)
755 struct msghdr smb_msg = { .msg_iter = *iter };
758 iov_iter_truncate(&smb_msg.msg_iter, to_read);
759 ret = cifs_readv_from_socket(server, &smb_msg);
761 iov_iter_advance(iter, ret);
766 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
769 * The first byte big endian of the length field,
770 * is actually not part of the length but the type
771 * with the most common, zero, as regular data.
774 case RFC1002_SESSION_MESSAGE:
775 /* Regular SMB response */
777 case RFC1002_SESSION_KEEP_ALIVE:
778 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
780 case RFC1002_POSITIVE_SESSION_RESPONSE:
781 cifs_dbg(FYI, "RFC 1002 positive session response\n");
783 case RFC1002_NEGATIVE_SESSION_RESPONSE:
785 * We get this from Windows 98 instead of an error on
786 * SMB negprot response.
788 cifs_dbg(FYI, "RFC 1002 negative session response\n");
789 /* give server a second to clean up */
792 * Always try 445 first on reconnect since we get NACK
793 * on some if we ever connected to port 139 (the NACK
794 * is since we do not begin with RFC1001 session
797 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
798 cifs_reconnect(server, true);
801 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
802 cifs_reconnect(server, true);
809 dequeue_mid(struct mid_q_entry *mid, bool malformed)
811 #ifdef CONFIG_CIFS_STATS2
812 mid->when_received = jiffies;
814 spin_lock(&mid->server->mid_lock);
816 mid->mid_state = MID_RESPONSE_RECEIVED;
818 mid->mid_state = MID_RESPONSE_MALFORMED;
820 * Trying to handle/dequeue a mid after the send_recv()
821 * function has finished processing it is a bug.
823 if (mid->mid_flags & MID_DELETED) {
824 spin_unlock(&mid->server->mid_lock);
825 pr_warn_once("trying to dequeue a deleted mid\n");
827 list_del_init(&mid->qhead);
828 mid->mid_flags |= MID_DELETED;
829 spin_unlock(&mid->server->mid_lock);
834 smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
836 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
839 * SMB1 does not use credits.
844 return le16_to_cpu(shdr->CreditRequest);
848 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
849 char *buf, int malformed)
851 if (server->ops->check_trans2 &&
852 server->ops->check_trans2(mid, server, buf, malformed))
854 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
856 mid->large_buf = server->large_buf;
857 /* Was previous buf put in mpx struct for multi-rsp? */
858 if (!mid->multiRsp) {
859 /* smb buffer will be freed by user thread */
860 if (server->large_buf)
861 server->bigbuf = NULL;
863 server->smallbuf = NULL;
865 dequeue_mid(mid, malformed);
869 cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required)
871 bool srv_sign_required = server->sec_mode & server->vals->signing_required;
872 bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled;
873 bool mnt_sign_enabled;
876 * Is signing required by mnt options? If not then check
877 * global_secflags to see if it is there.
879 if (!mnt_sign_required)
880 mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) ==
884 * If signing is required then it's automatically enabled too,
885 * otherwise, check to see if the secflags allow it.
887 mnt_sign_enabled = mnt_sign_required ? mnt_sign_required :
888 (global_secflags & CIFSSEC_MAY_SIGN);
890 /* If server requires signing, does client allow it? */
891 if (srv_sign_required) {
892 if (!mnt_sign_enabled) {
893 cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n");
899 /* If client requires signing, does server allow it? */
900 if (mnt_sign_required) {
901 if (!srv_sign_enabled) {
902 cifs_dbg(VFS, "Server does not support signing!\n");
908 if (cifs_rdma_enabled(server) && server->sign)
909 cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n");
915 static void clean_demultiplex_info(struct TCP_Server_Info *server)
919 /* take it off the list, if it's not already */
920 spin_lock(&server->srv_lock);
921 list_del_init(&server->tcp_ses_list);
922 spin_unlock(&server->srv_lock);
924 cancel_delayed_work_sync(&server->echo);
926 spin_lock(&server->srv_lock);
927 server->tcpStatus = CifsExiting;
928 spin_unlock(&server->srv_lock);
929 wake_up_all(&server->response_q);
931 /* check if we have blocked requests that need to free */
932 spin_lock(&server->req_lock);
933 if (server->credits <= 0)
935 spin_unlock(&server->req_lock);
937 * Although there should not be any requests blocked on this queue it
938 * can not hurt to be paranoid and try to wake up requests that may
939 * haven been blocked when more than 50 at time were on the wire to the
940 * same server - they now will see the session is in exit state and get
941 * out of SendReceive.
943 wake_up_all(&server->request_q);
944 /* give those requests time to exit */
946 if (cifs_rdma_enabled(server))
947 smbd_destroy(server);
948 if (server->ssocket) {
949 sock_release(server->ssocket);
950 server->ssocket = NULL;
953 if (!list_empty(&server->pending_mid_q)) {
954 struct list_head dispose_list;
955 struct mid_q_entry *mid_entry;
956 struct list_head *tmp, *tmp2;
958 INIT_LIST_HEAD(&dispose_list);
959 spin_lock(&server->mid_lock);
960 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
961 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
962 cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid);
963 kref_get(&mid_entry->refcount);
964 mid_entry->mid_state = MID_SHUTDOWN;
965 list_move(&mid_entry->qhead, &dispose_list);
966 mid_entry->mid_flags |= MID_DELETED;
968 spin_unlock(&server->mid_lock);
970 /* now walk dispose list and issue callbacks */
971 list_for_each_safe(tmp, tmp2, &dispose_list) {
972 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
973 cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid);
974 list_del_init(&mid_entry->qhead);
975 mid_entry->callback(mid_entry);
976 release_mid(mid_entry);
978 /* 1/8th of sec is more than enough time for them to exit */
982 if (!list_empty(&server->pending_mid_q)) {
984 * mpx threads have not exited yet give them at least the smb
985 * send timeout time for long ops.
987 * Due to delays on oplock break requests, we need to wait at
988 * least 45 seconds before giving up on a request getting a
989 * response and going ahead and killing cifsd.
991 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
994 * If threads still have not exited they are probably never
995 * coming home not much else we can do but free the memory.
999 kfree(server->origin_fullpath);
1000 kfree(server->leaf_fullpath);
1003 length = atomic_dec_return(&tcpSesAllocCount);
1005 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1009 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1012 char *buf = server->smallbuf;
1013 unsigned int pdu_length = server->pdu_size;
1015 /* make sure this will fit in a large buffer */
1016 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
1017 HEADER_PREAMBLE_SIZE(server)) {
1018 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
1019 cifs_reconnect(server, true);
1020 return -ECONNABORTED;
1023 /* switch to large buffer if too big for a small one */
1024 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
1025 server->large_buf = true;
1026 memcpy(server->bigbuf, buf, server->total_read);
1027 buf = server->bigbuf;
1030 /* now read the rest */
1031 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
1032 pdu_length - MID_HEADER_SIZE(server));
1036 server->total_read += length;
1038 dump_smb(buf, server->total_read);
1040 return cifs_handle_standard(server, mid);
1044 cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1046 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1050 * We know that we received enough to get to the MID as we
1051 * checked the pdu_length earlier. Now check to see
1052 * if the rest of the header is OK.
1054 * 48 bytes is enough to display the header and a little bit
1055 * into the payload for debugging purposes.
1057 rc = server->ops->check_message(buf, server->total_read, server);
1059 cifs_dump_mem("Bad SMB: ", buf,
1060 min_t(unsigned int, server->total_read, 48));
1062 if (server->ops->is_session_expired &&
1063 server->ops->is_session_expired(buf)) {
1064 cifs_reconnect(server, true);
1068 if (server->ops->is_status_pending &&
1069 server->ops->is_status_pending(buf, server))
1075 handle_mid(mid, server, buf, rc);
1080 smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1082 struct smb2_hdr *shdr = (struct smb2_hdr *)buffer;
1083 int scredits, in_flight;
1086 * SMB1 does not use credits.
1088 if (is_smb1(server))
1091 if (shdr->CreditRequest) {
1092 spin_lock(&server->req_lock);
1093 server->credits += le16_to_cpu(shdr->CreditRequest);
1094 scredits = server->credits;
1095 in_flight = server->in_flight;
1096 spin_unlock(&server->req_lock);
1097 wake_up(&server->request_q);
1099 trace_smb3_hdr_credits(server->CurrentMid,
1100 server->conn_id, server->hostname, scredits,
1101 le16_to_cpu(shdr->CreditRequest), in_flight);
1102 cifs_server_dbg(FYI, "%s: added %u credits total=%d\n",
1103 __func__, le16_to_cpu(shdr->CreditRequest),
1110 cifs_demultiplex_thread(void *p)
1112 int i, num_mids, length;
1113 struct TCP_Server_Info *server = p;
1114 unsigned int pdu_length;
1115 unsigned int next_offset;
1117 struct task_struct *task_to_wake = NULL;
1118 struct mid_q_entry *mids[MAX_COMPOUND];
1119 char *bufs[MAX_COMPOUND];
1120 unsigned int noreclaim_flag, num_io_timeout = 0;
1122 noreclaim_flag = memalloc_noreclaim_save();
1123 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
1125 length = atomic_inc_return(&tcpSesAllocCount);
1127 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
1130 allow_kernel_signal(SIGKILL);
1131 while (server->tcpStatus != CifsExiting) {
1132 if (try_to_freeze())
1135 if (!allocate_buffers(server))
1138 server->large_buf = false;
1139 buf = server->smallbuf;
1140 pdu_length = 4; /* enough to get RFC1001 header */
1142 length = cifs_read_from_socket(server, buf, pdu_length);
1146 if (is_smb1(server))
1147 server->total_read = length;
1149 server->total_read = 0;
1152 * The right amount was read from socket - 4 bytes,
1153 * so we can now interpret the length field.
1155 pdu_length = get_rfc1002_length(buf);
1157 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
1158 if (!is_smb_response(server, buf[0]))
1161 server->pdu_size = pdu_length;
1163 /* make sure we have enough to get to the MID */
1164 if (server->pdu_size < MID_HEADER_SIZE(server)) {
1165 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
1167 cifs_reconnect(server, true);
1171 /* read down to the MID */
1172 length = cifs_read_from_socket(server,
1173 buf + HEADER_PREAMBLE_SIZE(server),
1174 MID_HEADER_SIZE(server));
1177 server->total_read += length;
1179 if (server->ops->next_header) {
1180 next_offset = server->ops->next_header(buf);
1182 server->pdu_size = next_offset;
1185 memset(mids, 0, sizeof(mids));
1186 memset(bufs, 0, sizeof(bufs));
1189 if (server->ops->is_transform_hdr &&
1190 server->ops->receive_transform &&
1191 server->ops->is_transform_hdr(buf)) {
1192 length = server->ops->receive_transform(server,
1197 mids[0] = server->ops->find_mid(server, buf);
1201 if (!mids[0] || !mids[0]->receive)
1202 length = standard_receive3(server, mids[0]);
1204 length = mids[0]->receive(server, mids[0]);
1208 for (i = 0; i < num_mids; i++)
1210 release_mid(mids[i]);
1214 if (server->ops->is_status_io_timeout &&
1215 server->ops->is_status_io_timeout(buf)) {
1217 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1218 cifs_reconnect(server, false);
1224 server->lstrp = jiffies;
1226 for (i = 0; i < num_mids; i++) {
1227 if (mids[i] != NULL) {
1228 mids[i]->resp_buf_size = server->pdu_size;
1230 if (bufs[i] && server->ops->is_network_name_deleted)
1231 server->ops->is_network_name_deleted(bufs[i],
1234 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1235 mids[i]->callback(mids[i]);
1237 release_mid(mids[i]);
1238 } else if (server->ops->is_oplock_break &&
1239 server->ops->is_oplock_break(bufs[i],
1241 smb2_add_credits_from_hdr(bufs[i], server);
1242 cifs_dbg(FYI, "Received oplock break\n");
1244 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1245 atomic_read(&mid_count));
1246 cifs_dump_mem("Received Data is: ", bufs[i],
1247 HEADER_SIZE(server));
1248 smb2_add_credits_from_hdr(bufs[i], server);
1249 #ifdef CONFIG_CIFS_DEBUG2
1250 if (server->ops->dump_detail)
1251 server->ops->dump_detail(bufs[i],
1253 cifs_dump_mids(server);
1254 #endif /* CIFS_DEBUG2 */
1258 if (pdu_length > server->pdu_size) {
1259 if (!allocate_buffers(server))
1261 pdu_length -= server->pdu_size;
1262 server->total_read = 0;
1263 server->large_buf = false;
1264 buf = server->smallbuf;
1267 } /* end while !EXITING */
1269 /* buffer usually freed in free_mid - need to free it here on exit */
1270 cifs_buf_release(server->bigbuf);
1271 if (server->smallbuf) /* no sense logging a debug message if NULL */
1272 cifs_small_buf_release(server->smallbuf);
1274 task_to_wake = xchg(&server->tsk, NULL);
1275 clean_demultiplex_info(server);
1277 /* if server->tsk was NULL then wait for a signal before exiting */
1278 if (!task_to_wake) {
1279 set_current_state(TASK_INTERRUPTIBLE);
1280 while (!signal_pending(current)) {
1282 set_current_state(TASK_INTERRUPTIBLE);
1284 set_current_state(TASK_RUNNING);
1287 memalloc_noreclaim_restore(noreclaim_flag);
1288 module_put_and_kthread_exit(0);
1292 * Returns true if srcaddr isn't specified and rhs isn't specified, or
1293 * if srcaddr is specified and matches the IP address of the rhs argument
1296 cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
1298 switch (srcaddr->sa_family) {
1300 return (rhs->sa_family == AF_UNSPEC);
1302 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1303 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1304 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1307 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1308 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1309 return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr)
1310 && saddr6->sin6_scope_id == vaddr6->sin6_scope_id);
1314 return false; /* don't expect to be here */
1319 * If no port is specified in addr structure, we try to match with 445 port
1320 * and if it fails - with 139 ports. It should be called only if address
1321 * families of server and addr are equal.
1324 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1326 __be16 port, *sport;
1328 /* SMBDirect manages its own ports, don't match it here */
1332 switch (addr->sa_family) {
1334 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1335 port = ((struct sockaddr_in *) addr)->sin_port;
1338 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1339 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1347 port = htons(CIFS_PORT);
1351 port = htons(RFC1001_PORT);
1354 return port == *sport;
1357 static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr)
1359 if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr))
1366 match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1369 * The select_sectype function should either return the ctx->sectype
1370 * that was specified, or "Unspecified" if that sectype was not
1371 * compatible with the given NEGOTIATE request.
1373 if (server->ops->select_sectype(server, ctx->sectype)
1378 * Now check if signing mode is acceptable. No need to check
1379 * global_secflags at this point since if MUST_SIGN is set then
1380 * the server->sign had better be too.
1382 if (ctx->sign && !server->sign)
1388 /* this function must be called with srv_lock held */
1389 static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1391 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
1393 lockdep_assert_held(&server->srv_lock);
1395 if (ctx->nosharesock)
1398 /* this server does not share socket */
1399 if (server->nosharesock)
1402 /* If multidialect negotiation see if existing sessions match one */
1403 if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
1404 if (server->vals->protocol_id < SMB30_PROT_ID)
1406 } else if (strcmp(ctx->vals->version_string,
1407 SMBDEFAULT_VERSION_STRING) == 0) {
1408 if (server->vals->protocol_id < SMB21_PROT_ID)
1410 } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops))
1413 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1416 if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr,
1417 (struct sockaddr *)&server->srcaddr))
1420 * - Match for an DFS tcon (@server->origin_fullpath).
1421 * - Match for an DFS root server connection (@server->leaf_fullpath).
1422 * - If none of the above and @ctx->leaf_fullpath is set, then
1423 * it is a new DFS connection.
1424 * - If 'nodfs' mount option was passed, then match only connections
1425 * that have no DFS referrals set
1426 * (e.g. can't failover to other targets).
1429 if (ctx->source && server->origin_fullpath) {
1430 if (!dfs_src_pathname_equal(ctx->source,
1431 server->origin_fullpath))
1433 } else if (server->leaf_fullpath) {
1434 if (!ctx->leaf_fullpath ||
1435 strcasecmp(server->leaf_fullpath,
1436 ctx->leaf_fullpath))
1438 } else if (ctx->leaf_fullpath) {
1441 } else if (server->origin_fullpath || server->leaf_fullpath) {
1446 * Match for a regular connection (address/hostname/port) which has no
1447 * DFS referrals set.
1449 if (!server->origin_fullpath && !server->leaf_fullpath &&
1450 (strcasecmp(server->hostname, ctx->server_hostname) ||
1451 !match_server_address(server, addr) ||
1452 !match_port(server, addr)))
1455 if (!match_security(server, ctx))
1458 if (server->echo_interval != ctx->echo_interval * HZ)
1461 if (server->rdma != ctx->rdma)
1464 if (server->ignore_signature != ctx->ignore_signature)
1467 if (server->min_offload != ctx->min_offload)
1473 struct TCP_Server_Info *
1474 cifs_find_tcp_session(struct smb3_fs_context *ctx)
1476 struct TCP_Server_Info *server;
1478 spin_lock(&cifs_tcp_ses_lock);
1479 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1480 spin_lock(&server->srv_lock);
1482 * Skip ses channels since they're only handled in lower layers
1483 * (e.g. cifs_send_recv).
1485 if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx)) {
1486 spin_unlock(&server->srv_lock);
1489 spin_unlock(&server->srv_lock);
1491 ++server->srv_count;
1492 spin_unlock(&cifs_tcp_ses_lock);
1493 cifs_dbg(FYI, "Existing tcp session with server found\n");
1496 spin_unlock(&cifs_tcp_ses_lock);
1501 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
1503 struct task_struct *task;
1505 spin_lock(&cifs_tcp_ses_lock);
1506 if (--server->srv_count > 0) {
1507 spin_unlock(&cifs_tcp_ses_lock);
1511 /* srv_count can never go negative */
1512 WARN_ON(server->srv_count < 0);
1514 put_net(cifs_net_ns(server));
1516 list_del_init(&server->tcp_ses_list);
1517 spin_unlock(&cifs_tcp_ses_lock);
1519 /* For secondary channels, we pick up ref-count on the primary server */
1520 if (CIFS_SERVER_IS_CHAN(server))
1521 cifs_put_tcp_session(server->primary_server, from_reconnect);
1523 cancel_delayed_work_sync(&server->echo);
1527 * Avoid deadlock here: reconnect work calls
1528 * cifs_put_tcp_session() at its end. Need to be sure
1529 * that reconnect work does nothing with server pointer after
1532 cancel_delayed_work(&server->reconnect);
1534 cancel_delayed_work_sync(&server->reconnect);
1536 spin_lock(&server->srv_lock);
1537 server->tcpStatus = CifsExiting;
1538 spin_unlock(&server->srv_lock);
1540 cifs_crypto_secmech_release(server);
1542 kfree_sensitive(server->session_key.response);
1543 server->session_key.response = NULL;
1544 server->session_key.len = 0;
1545 kfree(server->hostname);
1546 server->hostname = NULL;
1548 task = xchg(&server->tsk, NULL);
1550 send_sig(SIGKILL, task, 1);
1553 struct TCP_Server_Info *
1554 cifs_get_tcp_session(struct smb3_fs_context *ctx,
1555 struct TCP_Server_Info *primary_server)
1557 struct TCP_Server_Info *tcp_ses = NULL;
1560 cifs_dbg(FYI, "UNC: %s\n", ctx->UNC);
1562 /* see if we already have a matching tcp_ses */
1563 tcp_ses = cifs_find_tcp_session(ctx);
1567 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
1573 tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL);
1574 if (!tcp_ses->hostname) {
1579 if (ctx->leaf_fullpath) {
1580 tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL);
1581 if (!tcp_ses->leaf_fullpath) {
1587 if (ctx->nosharesock)
1588 tcp_ses->nosharesock = true;
1590 tcp_ses->ops = ctx->ops;
1591 tcp_ses->vals = ctx->vals;
1592 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1594 tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId);
1595 tcp_ses->noblockcnt = ctx->rootfs;
1596 tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs;
1597 tcp_ses->noautotune = ctx->noautotune;
1598 tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay;
1599 tcp_ses->rdma = ctx->rdma;
1600 tcp_ses->in_flight = 0;
1601 tcp_ses->max_in_flight = 0;
1602 tcp_ses->credits = 1;
1603 if (primary_server) {
1604 spin_lock(&cifs_tcp_ses_lock);
1605 ++primary_server->srv_count;
1606 spin_unlock(&cifs_tcp_ses_lock);
1607 tcp_ses->primary_server = primary_server;
1609 init_waitqueue_head(&tcp_ses->response_q);
1610 init_waitqueue_head(&tcp_ses->request_q);
1611 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
1612 mutex_init(&tcp_ses->_srv_mutex);
1613 memcpy(tcp_ses->workstation_RFC1001_name,
1614 ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1615 memcpy(tcp_ses->server_RFC1001_name,
1616 ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1617 tcp_ses->session_estab = false;
1618 tcp_ses->sequence_number = 0;
1619 tcp_ses->reconnect_instance = 1;
1620 tcp_ses->lstrp = jiffies;
1621 tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression);
1622 spin_lock_init(&tcp_ses->req_lock);
1623 spin_lock_init(&tcp_ses->srv_lock);
1624 spin_lock_init(&tcp_ses->mid_lock);
1625 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1626 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
1627 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
1628 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
1629 mutex_init(&tcp_ses->reconnect_mutex);
1630 #ifdef CONFIG_CIFS_DFS_UPCALL
1631 mutex_init(&tcp_ses->refpath_lock);
1633 memcpy(&tcp_ses->srcaddr, &ctx->srcaddr,
1634 sizeof(tcp_ses->srcaddr));
1635 memcpy(&tcp_ses->dstaddr, &ctx->dstaddr,
1636 sizeof(tcp_ses->dstaddr));
1637 if (ctx->use_client_guid)
1638 memcpy(tcp_ses->client_guid, ctx->client_guid,
1639 SMB2_CLIENT_GUID_SIZE);
1641 generate_random_uuid(tcp_ses->client_guid);
1643 * at this point we are the only ones with the pointer
1644 * to the struct since the kernel thread not created yet
1645 * no need to spinlock this init of tcpStatus or srv_count
1647 tcp_ses->tcpStatus = CifsNew;
1648 ++tcp_ses->srv_count;
1650 if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
1651 ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX)
1652 tcp_ses->echo_interval = ctx->echo_interval * HZ;
1654 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
1655 if (tcp_ses->rdma) {
1656 #ifndef CONFIG_CIFS_SMB_DIRECT
1657 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
1659 goto out_err_crypto_release;
1661 tcp_ses->smbd_conn = smbd_get_connection(
1662 tcp_ses, (struct sockaddr *)&ctx->dstaddr);
1663 if (tcp_ses->smbd_conn) {
1664 cifs_dbg(VFS, "RDMA transport established\n");
1666 goto smbd_connected;
1669 goto out_err_crypto_release;
1672 rc = ip_connect(tcp_ses);
1674 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
1675 goto out_err_crypto_release;
1679 * since we're in a cifs function already, we know that
1680 * this will succeed. No need for try_module_get().
1682 __module_get(THIS_MODULE);
1683 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
1685 if (IS_ERR(tcp_ses->tsk)) {
1686 rc = PTR_ERR(tcp_ses->tsk);
1687 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
1688 module_put(THIS_MODULE);
1689 goto out_err_crypto_release;
1691 tcp_ses->min_offload = ctx->min_offload;
1693 * at this point we are the only ones with the pointer
1694 * to the struct since the kernel thread not created yet
1695 * no need to spinlock this update of tcpStatus
1697 spin_lock(&tcp_ses->srv_lock);
1698 tcp_ses->tcpStatus = CifsNeedNegotiate;
1699 spin_unlock(&tcp_ses->srv_lock);
1701 if ((ctx->max_credits < 20) || (ctx->max_credits > 60000))
1702 tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
1704 tcp_ses->max_credits = ctx->max_credits;
1706 tcp_ses->nr_targets = 1;
1707 tcp_ses->ignore_signature = ctx->ignore_signature;
1708 /* thread spawned, put it on the list */
1709 spin_lock(&cifs_tcp_ses_lock);
1710 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
1711 spin_unlock(&cifs_tcp_ses_lock);
1713 /* queue echo request delayed work */
1714 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
1718 out_err_crypto_release:
1719 cifs_crypto_secmech_release(tcp_ses);
1721 put_net(cifs_net_ns(tcp_ses));
1725 if (CIFS_SERVER_IS_CHAN(tcp_ses))
1726 cifs_put_tcp_session(tcp_ses->primary_server, false);
1727 kfree(tcp_ses->hostname);
1728 kfree(tcp_ses->leaf_fullpath);
1729 if (tcp_ses->ssocket)
1730 sock_release(tcp_ses->ssocket);
1736 /* this function must be called with ses_lock and chan_lock held */
1737 static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1739 if (ctx->sectype != Unspecified &&
1740 ctx->sectype != ses->sectype)
1744 * If an existing session is limited to less channels than
1745 * requested, it should not be reused
1747 if (ses->chan_max < ctx->max_channels)
1750 switch (ses->sectype) {
1752 if (!uid_eq(ctx->cred_uid, ses->cred_uid))
1756 /* NULL username means anonymous session */
1757 if (ses->user_name == NULL) {
1763 /* anything else takes username/password */
1764 if (strncmp(ses->user_name,
1765 ctx->username ? ctx->username : "",
1766 CIFS_MAX_USERNAME_LEN))
1768 if ((ctx->username && strlen(ctx->username) != 0) &&
1769 ses->password != NULL &&
1770 strncmp(ses->password,
1771 ctx->password ? ctx->password : "",
1772 CIFS_MAX_PASSWORD_LEN))
1779 * cifs_setup_ipc - helper to setup the IPC tcon for the session
1780 * @ses: smb session to issue the request on
1781 * @ctx: the superblock configuration context to use for building the
1782 * new tree connection for the IPC (interprocess communication RPC)
1784 * A new IPC connection is made and stored in the session
1785 * tcon_ipc. The IPC tcon has the same lifetime as the session.
1788 cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx)
1791 struct cifs_tcon *tcon;
1792 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
1794 struct TCP_Server_Info *server = ses->server;
1797 * If the mount request that resulted in the creation of the
1798 * session requires encryption, force IPC to be encrypted too.
1801 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
1804 cifs_server_dbg(VFS,
1805 "IPC: server doesn't support encryption\n");
1810 tcon = tconInfoAlloc();
1814 spin_lock(&server->srv_lock);
1815 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
1816 spin_unlock(&server->srv_lock);
1822 rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls);
1826 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
1831 cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid);
1833 spin_lock(&tcon->tc_lock);
1834 tcon->status = TID_GOOD;
1835 spin_unlock(&tcon->tc_lock);
1836 ses->tcon_ipc = tcon;
1842 * cifs_free_ipc - helper to release the session IPC tcon
1843 * @ses: smb session to unmount the IPC from
1845 * Needs to be called everytime a session is destroyed.
1847 * On session close, the IPC is closed and the server must release all tcons of the session.
1848 * No need to send a tree disconnect here.
1850 * Besides, it will make the server to not close durable and resilient files on session close, as
1851 * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request.
1854 cifs_free_ipc(struct cifs_ses *ses)
1856 struct cifs_tcon *tcon = ses->tcon_ipc;
1862 ses->tcon_ipc = NULL;
1866 static struct cifs_ses *
1867 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
1869 struct cifs_ses *ses, *ret = NULL;
1871 spin_lock(&cifs_tcp_ses_lock);
1872 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1873 spin_lock(&ses->ses_lock);
1874 if (ses->ses_status == SES_EXITING) {
1875 spin_unlock(&ses->ses_lock);
1878 spin_lock(&ses->chan_lock);
1879 if (match_session(ses, ctx)) {
1880 spin_unlock(&ses->chan_lock);
1881 spin_unlock(&ses->ses_lock);
1885 spin_unlock(&ses->chan_lock);
1886 spin_unlock(&ses->ses_lock);
1889 cifs_smb_ses_inc_refcount(ret);
1890 spin_unlock(&cifs_tcp_ses_lock);
1894 void __cifs_put_smb_ses(struct cifs_ses *ses)
1896 unsigned int rc, xid;
1897 unsigned int chan_count;
1898 struct TCP_Server_Info *server = ses->server;
1900 spin_lock(&ses->ses_lock);
1901 if (ses->ses_status == SES_EXITING) {
1902 spin_unlock(&ses->ses_lock);
1905 spin_unlock(&ses->ses_lock);
1907 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
1909 "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->tree_name : "NONE");
1911 spin_lock(&cifs_tcp_ses_lock);
1912 if (--ses->ses_count > 0) {
1913 spin_unlock(&cifs_tcp_ses_lock);
1916 spin_unlock(&cifs_tcp_ses_lock);
1918 /* ses_count can never go negative */
1919 WARN_ON(ses->ses_count < 0);
1921 spin_lock(&ses->ses_lock);
1922 if (ses->ses_status == SES_GOOD)
1923 ses->ses_status = SES_EXITING;
1925 if (ses->ses_status == SES_EXITING && server->ops->logoff) {
1926 spin_unlock(&ses->ses_lock);
1929 rc = server->ops->logoff(xid, ses);
1931 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
1935 spin_unlock(&ses->ses_lock);
1939 spin_lock(&cifs_tcp_ses_lock);
1940 list_del_init(&ses->smb_ses_list);
1941 spin_unlock(&cifs_tcp_ses_lock);
1943 chan_count = ses->chan_count;
1945 /* close any extra channels */
1946 if (chan_count > 1) {
1949 for (i = 1; i < chan_count; i++) {
1950 if (ses->chans[i].iface) {
1951 kref_put(&ses->chans[i].iface->refcount, release_iface);
1952 ses->chans[i].iface = NULL;
1954 cifs_put_tcp_session(ses->chans[i].server, 0);
1955 ses->chans[i].server = NULL;
1960 cifs_put_tcp_session(server, 0);
1965 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
1966 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
1968 /* Populate username and pw fields from keyring if possible */
1970 cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
1974 const char *delim, *payload;
1978 struct TCP_Server_Info *server = ses->server;
1979 struct sockaddr_in *sa;
1980 struct sockaddr_in6 *sa6;
1981 const struct user_key_payload *upayload;
1983 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
1987 /* try to find an address key first */
1988 switch (server->dstaddr.ss_family) {
1990 sa = (struct sockaddr_in *)&server->dstaddr;
1991 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
1994 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
1995 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
1998 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
1999 server->dstaddr.ss_family);
2004 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2005 key = request_key(&key_type_logon, desc, "");
2007 if (!ses->domainName) {
2008 cifs_dbg(FYI, "domainName is NULL\n");
2013 /* didn't work, try to find a domain key */
2014 sprintf(desc, "cifs:d:%s", ses->domainName);
2015 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2016 key = request_key(&key_type_logon, desc, "");
2024 down_read(&key->sem);
2025 upayload = user_key_payload_locked(key);
2026 if (IS_ERR_OR_NULL(upayload)) {
2027 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2031 /* find first : in payload */
2032 payload = upayload->data;
2033 delim = strnchr(payload, upayload->datalen, ':');
2034 cifs_dbg(FYI, "payload=%s\n", payload);
2036 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2042 len = delim - payload;
2043 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2044 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2050 ctx->username = kstrndup(payload, len, GFP_KERNEL);
2051 if (!ctx->username) {
2052 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2057 cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username);
2059 len = key->datalen - (len + 1);
2060 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2061 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2063 kfree(ctx->username);
2064 ctx->username = NULL;
2069 ctx->password = kstrndup(delim, len, GFP_KERNEL);
2070 if (!ctx->password) {
2071 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2074 kfree(ctx->username);
2075 ctx->username = NULL;
2080 * If we have a domain key then we must set the domainName in the
2083 if (is_domain && ses->domainName) {
2084 ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL);
2085 if (!ctx->domainname) {
2086 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
2089 kfree(ctx->username);
2090 ctx->username = NULL;
2091 kfree_sensitive(ctx->password);
2092 ctx->password = NULL;
2097 strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name));
2104 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2107 #else /* ! CONFIG_KEYS */
2109 cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)),
2110 struct cifs_ses *ses __attribute__((unused)))
2114 #endif /* CONFIG_KEYS */
2117 * cifs_get_smb_ses - get a session matching @ctx data from @server
2118 * @server: server to setup the session to
2119 * @ctx: superblock configuration context to use to setup the session
2121 * This function assumes it is being called from cifs_mount() where we
2122 * already got a server reference (server refcount +1). See
2123 * cifs_get_tcon() for refcount explanations.
2126 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx)
2130 struct cifs_ses *ses;
2131 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2132 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2136 ses = cifs_find_smb_ses(server, ctx);
2138 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2141 spin_lock(&ses->chan_lock);
2142 if (cifs_chan_needs_reconnect(ses, server)) {
2143 spin_unlock(&ses->chan_lock);
2144 cifs_dbg(FYI, "Session needs reconnect\n");
2146 mutex_lock(&ses->session_mutex);
2147 rc = cifs_negotiate_protocol(xid, ses, server);
2149 mutex_unlock(&ses->session_mutex);
2150 /* problem -- put our ses reference */
2151 cifs_put_smb_ses(ses);
2156 rc = cifs_setup_session(xid, ses, server,
2159 mutex_unlock(&ses->session_mutex);
2160 /* problem -- put our reference */
2161 cifs_put_smb_ses(ses);
2165 mutex_unlock(&ses->session_mutex);
2167 spin_lock(&ses->chan_lock);
2169 spin_unlock(&ses->chan_lock);
2171 /* existing SMB ses has a server reference already */
2172 cifs_put_tcp_session(server, 0);
2179 cifs_dbg(FYI, "Existing smb sess not found\n");
2180 ses = sesInfoAlloc();
2184 /* new SMB session uses our server ref */
2185 ses->server = server;
2186 if (server->dstaddr.ss_family == AF_INET6)
2187 sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr);
2189 sprintf(ses->ip_addr, "%pI4", &addr->sin_addr);
2191 if (ctx->username) {
2192 ses->user_name = kstrdup(ctx->username, GFP_KERNEL);
2193 if (!ses->user_name)
2197 /* ctx->password freed at unmount */
2198 if (ctx->password) {
2199 ses->password = kstrdup(ctx->password, GFP_KERNEL);
2203 if (ctx->domainname) {
2204 ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL);
2205 if (!ses->domainName)
2209 strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name));
2211 if (ctx->domainauto)
2212 ses->domainAuto = ctx->domainauto;
2213 ses->cred_uid = ctx->cred_uid;
2214 ses->linux_uid = ctx->linux_uid;
2216 ses->sectype = ctx->sectype;
2217 ses->sign = ctx->sign;
2219 /* add server as first channel */
2220 spin_lock(&ses->chan_lock);
2221 ses->chans[0].server = server;
2222 ses->chan_count = 1;
2223 ses->chan_max = ctx->multichannel ? ctx->max_channels:1;
2224 ses->chans_need_reconnect = 1;
2225 spin_unlock(&ses->chan_lock);
2227 mutex_lock(&ses->session_mutex);
2228 rc = cifs_negotiate_protocol(xid, ses, server);
2230 rc = cifs_setup_session(xid, ses, server, ctx->local_nls);
2231 mutex_unlock(&ses->session_mutex);
2233 /* each channel uses a different signing key */
2234 spin_lock(&ses->chan_lock);
2235 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
2236 sizeof(ses->smb3signingkey));
2237 spin_unlock(&ses->chan_lock);
2243 * success, put it on the list and add it as first channel
2244 * note: the session becomes active soon after this. So you'll
2245 * need to lock before changing something in the session.
2247 spin_lock(&cifs_tcp_ses_lock);
2248 ses->dfs_root_ses = ctx->dfs_root_ses;
2249 if (ses->dfs_root_ses)
2250 ses->dfs_root_ses->ses_count++;
2251 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2252 spin_unlock(&cifs_tcp_ses_lock);
2254 cifs_setup_ipc(ses, ctx);
2266 /* this function must be called with tc_lock held */
2267 static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
2269 struct TCP_Server_Info *server = tcon->ses->server;
2271 if (tcon->status == TID_EXITING)
2273 /* Skip UNC validation when matching DFS connections or superblocks */
2274 if (!server->origin_fullpath && !server->leaf_fullpath &&
2275 strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE))
2277 if (tcon->seal != ctx->seal)
2279 if (tcon->snapshot_time != ctx->snapshot_time)
2281 if (tcon->handle_timeout != ctx->handle_timeout)
2283 if (tcon->no_lease != ctx->no_lease)
2285 if (tcon->nodelete != ctx->nodelete)
2290 static struct cifs_tcon *
2291 cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2293 struct cifs_tcon *tcon;
2295 spin_lock(&cifs_tcp_ses_lock);
2296 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2297 spin_lock(&tcon->tc_lock);
2298 if (!match_tcon(tcon, ctx)) {
2299 spin_unlock(&tcon->tc_lock);
2303 spin_unlock(&tcon->tc_lock);
2304 spin_unlock(&cifs_tcp_ses_lock);
2307 spin_unlock(&cifs_tcp_ses_lock);
2312 cifs_put_tcon(struct cifs_tcon *tcon)
2315 struct cifs_ses *ses;
2318 * IPC tcon share the lifetime of their session and are
2319 * destroyed in the session put function
2321 if (tcon == NULL || tcon->ipc)
2325 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2326 spin_lock(&cifs_tcp_ses_lock);
2327 spin_lock(&tcon->tc_lock);
2328 if (--tcon->tc_count > 0) {
2329 spin_unlock(&tcon->tc_lock);
2330 spin_unlock(&cifs_tcp_ses_lock);
2334 /* tc_count can never go negative */
2335 WARN_ON(tcon->tc_count < 0);
2337 list_del_init(&tcon->tcon_list);
2338 tcon->status = TID_EXITING;
2339 spin_unlock(&tcon->tc_lock);
2340 spin_unlock(&cifs_tcp_ses_lock);
2342 /* cancel polling of interfaces */
2343 cancel_delayed_work_sync(&tcon->query_interfaces);
2344 #ifdef CONFIG_CIFS_DFS_UPCALL
2345 cancel_delayed_work_sync(&tcon->dfs_cache_work);
2348 if (tcon->use_witness) {
2351 rc = cifs_swn_unregister(tcon);
2353 cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n",
2359 if (ses->server->ops->tree_disconnect)
2360 ses->server->ops->tree_disconnect(xid, tcon);
2363 cifs_fscache_release_super_cookie(tcon);
2365 cifs_put_smb_ses(ses);
2369 * cifs_get_tcon - get a tcon matching @ctx data from @ses
2370 * @ses: smb session to issue the request on
2371 * @ctx: the superblock configuration context to use for building the
2373 * - tcon refcount is the number of mount points using the tcon.
2374 * - ses refcount is the number of tcon using the session.
2376 * 1. This function assumes it is being called from cifs_mount() where
2377 * we already got a session reference (ses refcount +1).
2379 * 2. Since we're in the context of adding a mount point, the end
2380 * result should be either:
2382 * a) a new tcon already allocated with refcount=1 (1 mount point) and
2383 * its session refcount incremented (1 new tcon). This +1 was
2384 * already done in (1).
2386 * b) an existing tcon with refcount+1 (add a mount point to it) and
2387 * identical ses refcount (no new tcon). Because of (1) we need to
2388 * decrement the ses refcount.
2390 static struct cifs_tcon *
2391 cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx)
2394 struct cifs_tcon *tcon;
2396 tcon = cifs_find_tcon(ses, ctx);
2399 * tcon has refcount already incremented but we need to
2400 * decrement extra ses reference gotten by caller (case b)
2402 cifs_dbg(FYI, "Found match on UNC path\n");
2403 cifs_put_smb_ses(ses);
2407 if (!ses->server->ops->tree_connect) {
2412 tcon = tconInfoAlloc();
2418 if (ctx->snapshot_time) {
2419 if (ses->server->vals->protocol_id == 0) {
2421 "Use SMB2 or later for snapshot mount option\n");
2425 tcon->snapshot_time = ctx->snapshot_time;
2428 if (ctx->handle_timeout) {
2429 if (ses->server->vals->protocol_id == 0) {
2431 "Use SMB2.1 or later for handle timeout option\n");
2435 tcon->handle_timeout = ctx->handle_timeout;
2439 if (ctx->password) {
2440 tcon->password = kstrdup(ctx->password, GFP_KERNEL);
2441 if (!tcon->password) {
2448 if (ses->server->vals->protocol_id == 0) {
2450 "SMB3 or later required for encryption\n");
2453 } else if (tcon->ses->server->capabilities &
2454 SMB2_GLOBAL_CAP_ENCRYPTION)
2457 cifs_dbg(VFS, "Encryption is not supported on share\n");
2463 if (ctx->linux_ext) {
2464 if (ses->server->posix_ext_supported) {
2465 tcon->posix_extensions = true;
2466 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
2467 } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) ||
2468 (strcmp(ses->server->vals->version_string,
2469 SMB3ANY_VERSION_STRING) == 0) ||
2470 (strcmp(ses->server->vals->version_string,
2471 SMBDEFAULT_VERSION_STRING) == 0)) {
2472 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
2476 cifs_dbg(VFS, "Check vers= mount option. SMB3.11 "
2477 "disabled but required for POSIX extensions\n");
2484 rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon,
2487 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2491 tcon->use_persistent = false;
2492 /* check if SMB2 or later, CIFS does not support persistent handles */
2493 if (ctx->persistent) {
2494 if (ses->server->vals->protocol_id == 0) {
2496 "SMB3 or later required for persistent handles\n");
2499 } else if (ses->server->capabilities &
2500 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2501 tcon->use_persistent = true;
2502 else /* persistent handles requested but not supported */ {
2504 "Persistent handles not supported on share\n");
2508 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2509 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2510 && (ctx->nopersistent == false)) {
2511 cifs_dbg(FYI, "enabling persistent handles\n");
2512 tcon->use_persistent = true;
2513 } else if (ctx->resilient) {
2514 if (ses->server->vals->protocol_id == 0) {
2516 "SMB2.1 or later required for resilient handles\n");
2520 tcon->use_resilient = true;
2523 tcon->use_witness = false;
2524 if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) {
2525 if (ses->server->vals->protocol_id >= SMB30_PROT_ID) {
2526 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) {
2528 * Set witness in use flag in first place
2529 * to retry registration in the echo task
2531 tcon->use_witness = true;
2532 /* And try to register immediately */
2533 rc = cifs_swn_register(tcon);
2535 cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc);
2539 /* TODO: try to extend for non-cluster uses (eg multichannel) */
2540 cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n");
2545 cifs_dbg(VFS, "SMB3 or later required for witness option\n");
2551 /* If the user really knows what they are doing they can override */
2552 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
2554 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
2555 else if (ctx->cache_rw)
2556 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
2559 if (ctx->no_lease) {
2560 if (ses->server->vals->protocol_id == 0) {
2562 "SMB2 or later required for nolease option\n");
2566 tcon->no_lease = ctx->no_lease;
2570 * We can have only one retry value for a connection to a share so for
2571 * resources mounted more than once to the same server share the last
2572 * value passed in for the retry flag is used.
2574 tcon->retry = ctx->retry;
2575 tcon->nocase = ctx->nocase;
2576 tcon->broken_sparse_sup = ctx->no_sparse;
2577 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
2578 tcon->nohandlecache = ctx->nohandlecache;
2580 tcon->nohandlecache = true;
2581 tcon->nodelete = ctx->nodelete;
2582 tcon->local_lease = ctx->local_lease;
2583 INIT_LIST_HEAD(&tcon->pending_opens);
2584 tcon->status = TID_GOOD;
2586 INIT_DELAYED_WORK(&tcon->query_interfaces,
2587 smb2_query_server_interfaces);
2588 if (ses->server->dialect >= SMB30_PROT_ID &&
2589 (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) {
2590 /* schedule query interfaces poll */
2591 queue_delayed_work(cifsiod_wq, &tcon->query_interfaces,
2592 (SMB_INTERFACE_POLL_INTERVAL * HZ));
2594 #ifdef CONFIG_CIFS_DFS_UPCALL
2595 INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh);
2597 spin_lock(&cifs_tcp_ses_lock);
2598 list_add(&tcon->tcon_list, &ses->tcon_list);
2599 spin_unlock(&cifs_tcp_ses_lock);
2609 cifs_put_tlink(struct tcon_link *tlink)
2611 if (!tlink || IS_ERR(tlink))
2614 if (!atomic_dec_and_test(&tlink->tl_count) ||
2615 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2616 tlink->tl_time = jiffies;
2620 if (!IS_ERR(tlink_tcon(tlink)))
2621 cifs_put_tcon(tlink_tcon(tlink));
2627 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2629 struct cifs_sb_info *old = CIFS_SB(sb);
2630 struct cifs_sb_info *new = mnt_data->cifs_sb;
2631 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
2632 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
2634 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2637 if (old->mnt_cifs_serverino_autodisabled)
2638 newflags &= ~CIFS_MOUNT_SERVER_INUM;
2640 if (oldflags != newflags)
2644 * We want to share sb only if we don't specify an r/wsize or
2645 * specified r/wsize is greater than or equal to existing one.
2647 if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize)
2650 if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize)
2653 if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) ||
2654 !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid))
2657 if (old->ctx->file_mode != new->ctx->file_mode ||
2658 old->ctx->dir_mode != new->ctx->dir_mode)
2661 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2664 if (old->ctx->acregmax != new->ctx->acregmax)
2666 if (old->ctx->acdirmax != new->ctx->acdirmax)
2668 if (old->ctx->closetimeo != new->ctx->closetimeo)
2674 static int match_prepath(struct super_block *sb,
2675 struct TCP_Server_Info *server,
2676 struct cifs_mnt_data *mnt_data)
2678 struct smb3_fs_context *ctx = mnt_data->ctx;
2679 struct cifs_sb_info *old = CIFS_SB(sb);
2680 struct cifs_sb_info *new = mnt_data->cifs_sb;
2681 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2683 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
2686 if (server->origin_fullpath &&
2687 dfs_src_pathname_equal(server->origin_fullpath, ctx->source))
2690 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
2692 else if (!old_set && !new_set)
2699 cifs_match_super(struct super_block *sb, void *data)
2701 struct cifs_mnt_data *mnt_data = data;
2702 struct smb3_fs_context *ctx;
2703 struct cifs_sb_info *cifs_sb;
2704 struct TCP_Server_Info *tcp_srv;
2705 struct cifs_ses *ses;
2706 struct cifs_tcon *tcon;
2707 struct tcon_link *tlink;
2710 spin_lock(&cifs_tcp_ses_lock);
2711 cifs_sb = CIFS_SB(sb);
2713 /* We do not want to use a superblock that has been shutdown */
2714 if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) {
2715 spin_unlock(&cifs_tcp_ses_lock);
2719 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2720 if (tlink == NULL) {
2721 /* can not match superblock if tlink were ever null */
2722 spin_unlock(&cifs_tcp_ses_lock);
2725 tcon = tlink_tcon(tlink);
2727 tcp_srv = ses->server;
2729 ctx = mnt_data->ctx;
2731 spin_lock(&tcp_srv->srv_lock);
2732 spin_lock(&ses->ses_lock);
2733 spin_lock(&ses->chan_lock);
2734 spin_lock(&tcon->tc_lock);
2735 if (!match_server(tcp_srv, ctx) ||
2736 !match_session(ses, ctx) ||
2737 !match_tcon(tcon, ctx) ||
2738 !match_prepath(sb, tcp_srv, mnt_data)) {
2743 rc = compare_mount_options(sb, mnt_data);
2745 spin_unlock(&tcon->tc_lock);
2746 spin_unlock(&ses->chan_lock);
2747 spin_unlock(&ses->ses_lock);
2748 spin_unlock(&tcp_srv->srv_lock);
2750 spin_unlock(&cifs_tcp_ses_lock);
2751 cifs_put_tlink(tlink);
2755 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2756 static struct lock_class_key cifs_key[2];
2757 static struct lock_class_key cifs_slock_key[2];
2760 cifs_reclassify_socket4(struct socket *sock)
2762 struct sock *sk = sock->sk;
2763 BUG_ON(!sock_allow_reclassification(sk));
2764 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2765 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2769 cifs_reclassify_socket6(struct socket *sock)
2771 struct sock *sk = sock->sk;
2772 BUG_ON(!sock_allow_reclassification(sk));
2773 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2774 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2778 cifs_reclassify_socket4(struct socket *sock)
2783 cifs_reclassify_socket6(struct socket *sock)
2788 /* See RFC1001 section 14 on representation of Netbios names */
2789 static void rfc1002mangle(char *target, char *source, unsigned int length)
2793 for (i = 0, j = 0; i < (length); i++) {
2794 /* mask a nibble at a time and encode */
2795 target[j] = 'A' + (0x0F & (source[i] >> 4));
2796 target[j+1] = 'A' + (0x0F & source[i]);
2803 bind_socket(struct TCP_Server_Info *server)
2806 if (server->srcaddr.ss_family != AF_UNSPEC) {
2807 /* Bind to the specified local IP address */
2808 struct socket *socket = server->ssocket;
2809 rc = socket->ops->bind(socket,
2810 (struct sockaddr *) &server->srcaddr,
2811 sizeof(server->srcaddr));
2813 struct sockaddr_in *saddr4;
2814 struct sockaddr_in6 *saddr6;
2815 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2816 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2817 if (saddr6->sin6_family == AF_INET6)
2818 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
2819 &saddr6->sin6_addr, rc);
2821 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
2822 &saddr4->sin_addr.s_addr, rc);
2829 ip_rfc1001_connect(struct TCP_Server_Info *server)
2833 * some servers require RFC1001 sessinit before sending
2834 * negprot - BB check reconnection in case where second
2835 * sessinit is sent but no second negprot
2837 struct rfc1002_session_packet req = {};
2838 struct smb_hdr *smb_buf = (struct smb_hdr *)&req;
2841 req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name);
2843 if (server->server_RFC1001_name[0] != 0)
2844 rfc1002mangle(req.trailer.session_req.called_name,
2845 server->server_RFC1001_name,
2846 RFC1001_NAME_LEN_WITH_NULL);
2848 rfc1002mangle(req.trailer.session_req.called_name,
2849 DEFAULT_CIFS_CALLED_NAME,
2850 RFC1001_NAME_LEN_WITH_NULL);
2852 req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name);
2854 /* calling name ends in null (byte 16) from old smb convention */
2855 if (server->workstation_RFC1001_name[0] != 0)
2856 rfc1002mangle(req.trailer.session_req.calling_name,
2857 server->workstation_RFC1001_name,
2858 RFC1001_NAME_LEN_WITH_NULL);
2860 rfc1002mangle(req.trailer.session_req.calling_name,
2862 RFC1001_NAME_LEN_WITH_NULL);
2865 * As per rfc1002, @len must be the number of bytes that follows the
2866 * length field of a rfc1002 session request payload.
2868 len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req);
2870 smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len);
2871 rc = smb_send(server, smb_buf, len);
2873 * RFC1001 layer in at least one server requires very short break before
2874 * negprot presumably because not expecting negprot to follow so fast.
2875 * This is a simple solution that works without complicating the code
2876 * and causes no significant slowing down on mount for everyone else
2878 usleep_range(1000, 2000);
2884 generic_ip_connect(struct TCP_Server_Info *server)
2889 struct socket *socket = server->ssocket;
2890 struct sockaddr *saddr;
2892 saddr = (struct sockaddr *) &server->dstaddr;
2894 if (server->dstaddr.ss_family == AF_INET6) {
2895 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
2897 sport = ipv6->sin6_port;
2898 slen = sizeof(struct sockaddr_in6);
2900 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
2903 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
2905 sport = ipv4->sin_port;
2906 slen = sizeof(struct sockaddr_in);
2908 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
2912 if (socket == NULL) {
2913 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2914 IPPROTO_TCP, &socket, 1);
2916 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
2917 server->ssocket = NULL;
2921 /* BB other socket options to set KEEPALIVE, NODELAY? */
2922 cifs_dbg(FYI, "Socket created\n");
2923 server->ssocket = socket;
2924 socket->sk->sk_allocation = GFP_NOFS;
2925 socket->sk->sk_use_task_frag = false;
2926 if (sfamily == AF_INET6)
2927 cifs_reclassify_socket6(socket);
2929 cifs_reclassify_socket4(socket);
2932 rc = bind_socket(server);
2937 * Eventually check for other socket options to change from
2938 * the default. sock_setsockopt not used because it expects
2941 socket->sk->sk_rcvtimeo = 7 * HZ;
2942 socket->sk->sk_sndtimeo = 5 * HZ;
2944 /* make the bufsizes depend on wsize/rsize and max requests */
2945 if (server->noautotune) {
2946 if (socket->sk->sk_sndbuf < (200 * 1024))
2947 socket->sk->sk_sndbuf = 200 * 1024;
2948 if (socket->sk->sk_rcvbuf < (140 * 1024))
2949 socket->sk->sk_rcvbuf = 140 * 1024;
2952 if (server->tcp_nodelay)
2953 tcp_sock_set_nodelay(socket->sk);
2955 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
2956 socket->sk->sk_sndbuf,
2957 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2959 rc = socket->ops->connect(socket, saddr, slen,
2960 server->noblockcnt ? O_NONBLOCK : 0);
2962 * When mounting SMB root file systems, we do not want to block in
2963 * connect. Otherwise bail out and then let cifs_reconnect() perform
2964 * reconnect failover - if possible.
2966 if (server->noblockcnt && rc == -EINPROGRESS)
2969 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
2970 trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc);
2971 sock_release(socket);
2972 server->ssocket = NULL;
2975 trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr);
2976 if (sport == htons(RFC1001_PORT))
2977 rc = ip_rfc1001_connect(server);
2983 ip_connect(struct TCP_Server_Info *server)
2986 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2987 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2989 if (server->dstaddr.ss_family == AF_INET6)
2990 sport = &addr6->sin6_port;
2992 sport = &addr->sin_port;
2997 /* try with 445 port at first */
2998 *sport = htons(CIFS_PORT);
3000 rc = generic_ip_connect(server);
3004 /* if it failed, try with 139 port */
3005 *sport = htons(RFC1001_PORT);
3008 return generic_ip_connect(server);
3011 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3012 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3013 struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3016 * If we are reconnecting then should we check to see if
3017 * any requested capabilities changed locally e.g. via
3018 * remount but we can not do much about it here
3019 * if they have (even if we could detect it by the following)
3020 * Perhaps we could add a backpointer to array of sb from tcon
3021 * or if we change to make all sb to same share the same
3022 * sb as NFS - then we only have one backpointer to sb.
3023 * What if we wanted to mount the server share twice once with
3024 * and once without posixacls or posix paths?
3026 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3028 if (ctx && ctx->no_linux_ext) {
3029 tcon->fsUnixInfo.Capability = 0;
3030 tcon->unix_ext = 0; /* Unix Extensions disabled */
3031 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3034 tcon->unix_ext = 1; /* Unix Extensions supported */
3036 if (!tcon->unix_ext) {
3037 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3041 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3042 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3043 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3045 * check for reconnect case in which we do not
3046 * want to change the mount behavior if we can avoid it
3050 * turn off POSIX ACL and PATHNAMES if not set
3051 * originally at mount time
3053 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3054 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3055 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3056 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3057 cifs_dbg(VFS, "POSIXPATH support change\n");
3058 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3059 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3060 cifs_dbg(VFS, "possible reconnect error\n");
3061 cifs_dbg(VFS, "server disabled POSIX path support\n");
3065 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3066 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3068 cap &= CIFS_UNIX_CAP_MASK;
3069 if (ctx && ctx->no_psx_acl)
3070 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3071 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3072 cifs_dbg(FYI, "negotiated posix acl support\n");
3074 cifs_sb->mnt_cifs_flags |=
3075 CIFS_MOUNT_POSIXACL;
3078 if (ctx && ctx->posix_paths == 0)
3079 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3080 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3081 cifs_dbg(FYI, "negotiate posix pathnames\n");
3083 cifs_sb->mnt_cifs_flags |=
3084 CIFS_MOUNT_POSIX_PATHS;
3087 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3088 #ifdef CONFIG_CIFS_DEBUG2
3089 if (cap & CIFS_UNIX_FCNTL_CAP)
3090 cifs_dbg(FYI, "FCNTL cap\n");
3091 if (cap & CIFS_UNIX_EXTATTR_CAP)
3092 cifs_dbg(FYI, "EXTATTR cap\n");
3093 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3094 cifs_dbg(FYI, "POSIX path cap\n");
3095 if (cap & CIFS_UNIX_XATTR_CAP)
3096 cifs_dbg(FYI, "XATTR cap\n");
3097 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3098 cifs_dbg(FYI, "POSIX ACL cap\n");
3099 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3100 cifs_dbg(FYI, "very large read cap\n");
3101 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3102 cifs_dbg(FYI, "very large write cap\n");
3103 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3104 cifs_dbg(FYI, "transport encryption cap\n");
3105 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3106 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3107 #endif /* CIFS_DEBUG2 */
3108 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3110 cifs_dbg(FYI, "resetting capabilities failed\n");
3112 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");
3117 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3119 int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb)
3121 struct smb3_fs_context *ctx = cifs_sb->ctx;
3123 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3125 spin_lock_init(&cifs_sb->tlink_tree_lock);
3126 cifs_sb->tlink_tree = RB_ROOT;
3128 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
3129 ctx->file_mode, ctx->dir_mode);
3131 /* this is needed for ASCII cp to Unicode converts */
3132 if (ctx->iocharset == NULL) {
3133 /* load_nls_default cannot return null */
3134 cifs_sb->local_nls = load_nls_default();
3136 cifs_sb->local_nls = load_nls(ctx->iocharset);
3137 if (cifs_sb->local_nls == NULL) {
3138 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3143 ctx->local_nls = cifs_sb->local_nls;
3145 smb3_update_mnt_flags(cifs_sb);
3148 cifs_dbg(FYI, "mounting share using direct i/o\n");
3149 if (ctx->cache_ro) {
3150 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
3151 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
3152 } else if (ctx->cache_rw) {
3153 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
3154 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
3155 CIFS_MOUNT_RW_CACHE);
3158 if ((ctx->cifs_acl) && (ctx->dynperm))
3159 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3162 cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL);
3163 if (cifs_sb->prepath == NULL)
3165 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3171 /* Release all succeed connections */
3172 void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx)
3177 cifs_put_tcon(mnt_ctx->tcon);
3178 else if (mnt_ctx->ses)
3179 cifs_put_smb_ses(mnt_ctx->ses);
3180 else if (mnt_ctx->server)
3181 cifs_put_tcp_session(mnt_ctx->server, 0);
3182 mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3183 free_xid(mnt_ctx->xid);
3186 int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx)
3188 struct TCP_Server_Info *server = NULL;
3189 struct smb3_fs_context *ctx;
3190 struct cifs_ses *ses = NULL;
3196 if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) {
3200 ctx = mnt_ctx->fs_ctx;
3202 /* get a reference to a tcp session */
3203 server = cifs_get_tcp_session(ctx, NULL);
3204 if (IS_ERR(server)) {
3205 rc = PTR_ERR(server);
3210 /* get a reference to a SMB session */
3211 ses = cifs_get_smb_ses(server, ctx);
3218 if ((ctx->persistent == true) && (!(ses->server->capabilities &
3219 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3220 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
3226 mnt_ctx->server = server;
3228 mnt_ctx->tcon = NULL;
3233 int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx)
3235 struct TCP_Server_Info *server;
3236 struct cifs_sb_info *cifs_sb;
3237 struct smb3_fs_context *ctx;
3238 struct cifs_tcon *tcon = NULL;
3241 if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx ||
3242 !mnt_ctx->cifs_sb)) {
3246 server = mnt_ctx->server;
3247 ctx = mnt_ctx->fs_ctx;
3248 cifs_sb = mnt_ctx->cifs_sb;
3250 /* search for existing tcon to this server share */
3251 tcon = cifs_get_tcon(mnt_ctx->ses, ctx);
3258 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3259 if (tcon->posix_extensions)
3260 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3262 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3263 /* tell server which Unix caps we support */
3264 if (cap_unix(tcon->ses)) {
3266 * reset of caps checks mount to see if unix extensions disabled
3267 * for just this mount.
3269 reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx);
3270 spin_lock(&tcon->ses->server->srv_lock);
3271 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3272 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3273 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3274 spin_unlock(&tcon->ses->server->srv_lock);
3278 spin_unlock(&tcon->ses->server->srv_lock);
3280 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3281 tcon->unix_ext = 0; /* server does not support them */
3283 /* do not care if a following call succeed - informational */
3284 if (!tcon->pipe && server->ops->qfs_tcon) {
3285 server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb);
3286 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
3287 if (tcon->fsDevInfo.DeviceCharacteristics &
3288 cpu_to_le32(FILE_READ_ONLY_DEVICE))
3289 cifs_dbg(VFS, "mounted to read only share\n");
3290 else if ((cifs_sb->mnt_cifs_flags &
3291 CIFS_MOUNT_RW_CACHE) == 0)
3292 cifs_dbg(VFS, "read only mount of RW share\n");
3293 /* no need to log a RW mount of a typical RW share */
3298 * Clamp the rsize/wsize mount arguments if they are too big for the server
3299 * and set the rsize/wsize to the negotiated values if not passed in by
3302 if ((cifs_sb->ctx->wsize == 0) ||
3303 (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx)))
3304 cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx);
3305 if ((cifs_sb->ctx->rsize == 0) ||
3306 (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx)))
3307 cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx);
3310 * The cookie is initialized from volume info returned above.
3311 * Inside cifs_fscache_get_super_cookie it checks
3312 * that we do not get super cookie twice.
3314 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE)
3315 cifs_fscache_get_super_cookie(tcon);
3318 mnt_ctx->tcon = tcon;
3322 static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3323 struct cifs_tcon *tcon)
3325 struct tcon_link *tlink;
3327 /* hang the tcon off of the superblock */
3328 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3332 tlink->tl_uid = ses->linux_uid;
3333 tlink->tl_tcon = tcon;
3334 tlink->tl_time = jiffies;
3335 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3336 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3338 cifs_sb->master_tlink = tlink;
3339 spin_lock(&cifs_sb->tlink_tree_lock);
3340 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3341 spin_unlock(&cifs_sb->tlink_tree_lock);
3343 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3349 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3351 struct cifs_tcon *tcon,
3352 struct cifs_sb_info *cifs_sb,
3359 int skip = added_treename ? 1 : 0;
3361 sep = CIFS_DIR_SEP(cifs_sb);
3364 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3366 /* skip separators */
3371 /* next separator */
3372 while (*s && *s != sep)
3375 * if the treename is added, we then have to skip the first
3376 * part within the separators
3383 * temporarily null-terminate the path at the end of
3384 * the current component
3388 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3396 * Check if path is remote (i.e. a DFS share).
3398 * Return -EREMOTE if it is, otherwise 0 or -errno.
3400 int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx)
3403 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
3404 struct TCP_Server_Info *server = mnt_ctx->server;
3405 unsigned int xid = mnt_ctx->xid;
3406 struct cifs_tcon *tcon = mnt_ctx->tcon;
3407 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
3410 if (!server->ops->is_path_accessible)
3414 * cifs_build_path_to_root works only when we have a valid tcon
3416 full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon,
3417 tcon->Flags & SMB_SHARE_IS_IN_DFS);
3418 if (full_path == NULL)
3421 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
3423 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3425 if (rc != 0 && rc != -EREMOTE)
3428 if (rc != -EREMOTE) {
3429 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
3430 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
3432 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3433 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3443 #ifdef CONFIG_CIFS_DFS_UPCALL
3444 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3446 struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3450 INIT_LIST_HEAD(&mnt_ctx.dfs_ses_list);
3452 rc = dfs_mount_share(&mnt_ctx, &isdfs);
3459 * After reconnecting to a different server, unique ids won't match anymore, so we disable
3460 * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE).
3462 cifs_autodisable_serverino(cifs_sb);
3464 * Force the use of prefix path to support failover on DFS paths that resolve to targets
3465 * that have different prefix paths.
3467 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3468 kfree(cifs_sb->prepath);
3469 cifs_sb->prepath = ctx->prepath;
3470 ctx->prepath = NULL;
3473 cifs_try_adding_channels(cifs_sb, mnt_ctx.ses);
3474 rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3478 free_xid(mnt_ctx.xid);
3482 dfs_put_root_smb_sessions(&mnt_ctx.dfs_ses_list);
3483 cifs_mount_put_conns(&mnt_ctx);
3487 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx)
3490 struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, };
3492 rc = cifs_mount_get_session(&mnt_ctx);
3496 rc = cifs_mount_get_tcon(&mnt_ctx);
3500 rc = cifs_is_path_remote(&mnt_ctx);
3506 rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon);
3510 free_xid(mnt_ctx.xid);
3514 cifs_mount_put_conns(&mnt_ctx);
3520 * Issue a TREE_CONNECT request.
3523 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3524 const char *tree, struct cifs_tcon *tcon,
3525 const struct nls_table *nls_codepage)
3527 struct smb_hdr *smb_buffer;
3528 struct smb_hdr *smb_buffer_response;
3531 unsigned char *bcc_ptr;
3534 __u16 bytes_left, count;
3539 smb_buffer = cifs_buf_get();
3540 if (smb_buffer == NULL)
3543 smb_buffer_response = smb_buffer;
3545 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3546 NULL /*no tid */ , 4 /*wct */ );
3548 smb_buffer->Mid = get_next_mid(ses->server);
3549 smb_buffer->Uid = ses->Suid;
3550 pSMB = (TCONX_REQ *) smb_buffer;
3551 pSMBr = (TCONX_RSP *) smb_buffer_response;
3553 pSMB->AndXCommand = 0xFF;
3554 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3555 bcc_ptr = &pSMB->Password[0];
3557 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
3558 *bcc_ptr = 0; /* password is null byte */
3559 bcc_ptr++; /* skip password */
3560 /* already aligned so no need to do it below */
3562 if (ses->server->sign)
3563 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3565 if (ses->capabilities & CAP_STATUS32) {
3566 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3568 if (ses->capabilities & CAP_DFS) {
3569 smb_buffer->Flags2 |= SMBFLG2_DFS;
3571 if (ses->capabilities & CAP_UNICODE) {
3572 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3574 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3575 6 /* max utf8 char length in bytes */ *
3576 (/* server len*/ + 256 /* share len */), nls_codepage);
3577 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
3578 bcc_ptr += 2; /* skip trailing null */
3579 } else { /* ASCII */
3580 strcpy(bcc_ptr, tree);
3581 bcc_ptr += strlen(tree) + 1;
3583 strcpy(bcc_ptr, "?????");
3584 bcc_ptr += strlen("?????");
3586 count = bcc_ptr - &pSMB->Password[0];
3587 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
3588 pSMB->ByteCount = cpu_to_le16(count);
3590 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3593 /* above now done in SendReceive */
3597 tcon->tid = smb_buffer_response->Tid;
3598 bcc_ptr = pByteArea(smb_buffer_response);
3599 bytes_left = get_bcc(smb_buffer_response);
3600 length = strnlen(bcc_ptr, bytes_left - 2);
3601 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3607 /* skip service field (NB: this field is always ASCII) */
3609 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3610 (bcc_ptr[2] == 'C')) {
3611 cifs_dbg(FYI, "IPC connection\n");
3615 } else if (length == 2) {
3616 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3617 /* the most common case */
3618 cifs_dbg(FYI, "disk share connection\n");
3621 bcc_ptr += length + 1;
3622 bytes_left -= (length + 1);
3623 strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name));
3625 /* mostly informational -- no need to fail on error here */
3626 kfree(tcon->nativeFileSystem);
3627 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
3628 bytes_left, is_unicode,
3631 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
3633 if ((smb_buffer_response->WordCount == 3) ||
3634 (smb_buffer_response->WordCount == 7))
3635 /* field is in same location */
3636 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3639 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
3642 cifs_buf_release(smb_buffer);
3646 static void delayed_free(struct rcu_head *p)
3648 struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu);
3650 unload_nls(cifs_sb->local_nls);
3651 smb3_cleanup_fs_context(cifs_sb->ctx);
3656 cifs_umount(struct cifs_sb_info *cifs_sb)
3658 struct rb_root *root = &cifs_sb->tlink_tree;
3659 struct rb_node *node;
3660 struct tcon_link *tlink;
3662 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3664 spin_lock(&cifs_sb->tlink_tree_lock);
3665 while ((node = rb_first(root))) {
3666 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3667 cifs_get_tlink(tlink);
3668 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3669 rb_erase(node, root);
3671 spin_unlock(&cifs_sb->tlink_tree_lock);
3672 cifs_put_tlink(tlink);
3673 spin_lock(&cifs_sb->tlink_tree_lock);
3675 spin_unlock(&cifs_sb->tlink_tree_lock);
3677 kfree(cifs_sb->prepath);
3678 call_rcu(&cifs_sb->rcu, delayed_free);
3682 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses,
3683 struct TCP_Server_Info *server)
3687 if (!server->ops->need_neg || !server->ops->negotiate)
3690 /* only send once per connect */
3691 spin_lock(&server->srv_lock);
3692 if (server->tcpStatus != CifsGood &&
3693 server->tcpStatus != CifsNew &&
3694 server->tcpStatus != CifsNeedNegotiate) {
3695 spin_unlock(&server->srv_lock);
3699 if (!server->ops->need_neg(server) &&
3700 server->tcpStatus == CifsGood) {
3701 spin_unlock(&server->srv_lock);
3705 server->tcpStatus = CifsInNegotiate;
3706 spin_unlock(&server->srv_lock);
3708 rc = server->ops->negotiate(xid, ses, server);
3710 spin_lock(&server->srv_lock);
3711 if (server->tcpStatus == CifsInNegotiate)
3712 server->tcpStatus = CifsGood;
3715 spin_unlock(&server->srv_lock);
3717 spin_lock(&server->srv_lock);
3718 if (server->tcpStatus == CifsInNegotiate)
3719 server->tcpStatus = CifsNeedNegotiate;
3720 spin_unlock(&server->srv_lock);
3727 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3728 struct TCP_Server_Info *server,
3729 struct nls_table *nls_info)
3732 struct TCP_Server_Info *pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server;
3733 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr;
3734 struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr;
3735 bool is_binding = false;
3737 spin_lock(&ses->ses_lock);
3738 cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n",
3739 __func__, ses->chans_need_reconnect);
3741 if (ses->ses_status != SES_GOOD &&
3742 ses->ses_status != SES_NEW &&
3743 ses->ses_status != SES_NEED_RECON) {
3744 spin_unlock(&ses->ses_lock);
3748 /* only send once per connect */
3749 spin_lock(&ses->chan_lock);
3750 if (CIFS_ALL_CHANS_GOOD(ses)) {
3751 if (ses->ses_status == SES_NEED_RECON)
3752 ses->ses_status = SES_GOOD;
3753 spin_unlock(&ses->chan_lock);
3754 spin_unlock(&ses->ses_lock);
3758 cifs_chan_set_in_reconnect(ses, server);
3759 is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses);
3760 spin_unlock(&ses->chan_lock);
3763 ses->ses_status = SES_IN_SETUP;
3764 spin_unlock(&ses->ses_lock);
3766 /* update ses ip_addr only for primary chan */
3767 if (server == pserver) {
3768 if (server->dstaddr.ss_family == AF_INET6)
3769 scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr);
3771 scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr);
3775 ses->capabilities = server->capabilities;
3776 if (!linuxExtEnabled)
3777 ses->capabilities &= (~server->vals->cap_unix);
3779 if (ses->auth_key.response) {
3780 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
3781 ses->auth_key.response);
3782 kfree_sensitive(ses->auth_key.response);
3783 ses->auth_key.response = NULL;
3784 ses->auth_key.len = 0;
3788 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
3789 server->sec_mode, server->capabilities, server->timeAdj);
3791 if (server->ops->sess_setup)
3792 rc = server->ops->sess_setup(xid, ses, server, nls_info);
3795 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
3796 spin_lock(&ses->ses_lock);
3797 if (ses->ses_status == SES_IN_SETUP)
3798 ses->ses_status = SES_NEED_RECON;
3799 spin_lock(&ses->chan_lock);
3800 cifs_chan_clear_in_reconnect(ses, server);
3801 spin_unlock(&ses->chan_lock);
3802 spin_unlock(&ses->ses_lock);
3804 spin_lock(&ses->ses_lock);
3805 if (ses->ses_status == SES_IN_SETUP)
3806 ses->ses_status = SES_GOOD;
3807 spin_lock(&ses->chan_lock);
3808 cifs_chan_clear_in_reconnect(ses, server);
3809 cifs_chan_clear_need_reconnect(ses, server);
3810 spin_unlock(&ses->chan_lock);
3811 spin_unlock(&ses->ses_lock);
3818 cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses)
3820 ctx->sectype = ses->sectype;
3822 /* krb5 is special, since we don't need username or pw */
3823 if (ctx->sectype == Kerberos)
3826 return cifs_set_cifscreds(ctx, ses);
3829 static struct cifs_tcon *
3830 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
3833 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3834 struct cifs_ses *ses;
3835 struct cifs_tcon *tcon = NULL;
3836 struct smb3_fs_context *ctx;
3838 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
3840 return ERR_PTR(-ENOMEM);
3842 ctx->local_nls = cifs_sb->local_nls;
3843 ctx->linux_uid = fsuid;
3844 ctx->cred_uid = fsuid;
3845 ctx->UNC = master_tcon->tree_name;
3846 ctx->retry = master_tcon->retry;
3847 ctx->nocase = master_tcon->nocase;
3848 ctx->nohandlecache = master_tcon->nohandlecache;
3849 ctx->local_lease = master_tcon->local_lease;
3850 ctx->no_lease = master_tcon->no_lease;
3851 ctx->resilient = master_tcon->use_resilient;
3852 ctx->persistent = master_tcon->use_persistent;
3853 ctx->handle_timeout = master_tcon->handle_timeout;
3854 ctx->no_linux_ext = !master_tcon->unix_ext;
3855 ctx->linux_ext = master_tcon->posix_extensions;
3856 ctx->sectype = master_tcon->ses->sectype;
3857 ctx->sign = master_tcon->ses->sign;
3858 ctx->seal = master_tcon->seal;
3859 ctx->witness = master_tcon->use_witness;
3861 rc = cifs_set_vol_auth(ctx, master_tcon->ses);
3867 /* get a reference for the same TCP session */
3868 spin_lock(&cifs_tcp_ses_lock);
3869 ++master_tcon->ses->server->srv_count;
3870 spin_unlock(&cifs_tcp_ses_lock);
3872 ses = cifs_get_smb_ses(master_tcon->ses->server, ctx);
3874 tcon = (struct cifs_tcon *)ses;
3875 cifs_put_tcp_session(master_tcon->ses->server, 0);
3879 tcon = cifs_get_tcon(ses, ctx);
3881 cifs_put_smb_ses(ses);
3885 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3887 reset_cifs_unix_caps(0, tcon, NULL, ctx);
3888 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3891 kfree(ctx->username);
3892 kfree_sensitive(ctx->password);
3899 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3901 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3904 /* find and return a tlink with given uid */
3905 static struct tcon_link *
3906 tlink_rb_search(struct rb_root *root, kuid_t uid)
3908 struct rb_node *node = root->rb_node;
3909 struct tcon_link *tlink;
3912 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3914 if (uid_gt(tlink->tl_uid, uid))
3915 node = node->rb_left;
3916 else if (uid_lt(tlink->tl_uid, uid))
3917 node = node->rb_right;
3924 /* insert a tcon_link into the tree */
3926 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3928 struct rb_node **new = &(root->rb_node), *parent = NULL;
3929 struct tcon_link *tlink;
3932 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3935 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
3936 new = &((*new)->rb_left);
3938 new = &((*new)->rb_right);
3941 rb_link_node(&new_tlink->tl_rbnode, parent, new);
3942 rb_insert_color(&new_tlink->tl_rbnode, root);
3946 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
3949 * If the superblock doesn't refer to a multiuser mount, then just return
3950 * the master tcon for the mount.
3952 * First, search the rbtree for an existing tcon for this fsuid. If one
3953 * exists, then check to see if it's pending construction. If it is then wait
3954 * for construction to complete. Once it's no longer pending, check to see if
3955 * it failed and either return an error or retry construction, depending on
3958 * If one doesn't exist then insert a new tcon_link struct into the tree and
3959 * try to construct a new one.
3962 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3965 kuid_t fsuid = current_fsuid();
3966 struct tcon_link *tlink, *newtlink;
3968 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
3969 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3971 spin_lock(&cifs_sb->tlink_tree_lock);
3972 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3974 cifs_get_tlink(tlink);
3975 spin_unlock(&cifs_sb->tlink_tree_lock);
3977 if (tlink == NULL) {
3978 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3979 if (newtlink == NULL)
3980 return ERR_PTR(-ENOMEM);
3981 newtlink->tl_uid = fsuid;
3982 newtlink->tl_tcon = ERR_PTR(-EACCES);
3983 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
3984 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
3985 cifs_get_tlink(newtlink);
3987 spin_lock(&cifs_sb->tlink_tree_lock);
3988 /* was one inserted after previous search? */
3989 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3991 cifs_get_tlink(tlink);
3992 spin_unlock(&cifs_sb->tlink_tree_lock);
3994 goto wait_for_construction;
3997 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3998 spin_unlock(&cifs_sb->tlink_tree_lock);
4000 wait_for_construction:
4001 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4002 TASK_INTERRUPTIBLE);
4004 cifs_put_tlink(tlink);
4005 return ERR_PTR(-ERESTARTSYS);
4008 /* if it's good, return it */
4009 if (!IS_ERR(tlink->tl_tcon))
4012 /* return error if we tried this already recently */
4013 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4014 cifs_put_tlink(tlink);
4015 return ERR_PTR(-EACCES);
4018 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4019 goto wait_for_construction;
4022 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4023 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4024 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4026 if (IS_ERR(tlink->tl_tcon)) {
4027 cifs_put_tlink(tlink);
4028 return ERR_PTR(-EACCES);
4035 * periodic workqueue job that scans tcon_tree for a superblock and closes
4039 cifs_prune_tlinks(struct work_struct *work)
4041 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4043 struct rb_root *root = &cifs_sb->tlink_tree;
4044 struct rb_node *node;
4045 struct rb_node *tmp;
4046 struct tcon_link *tlink;
4049 * Because we drop the spinlock in the loop in order to put the tlink
4050 * it's not guarded against removal of links from the tree. The only
4051 * places that remove entries from the tree are this function and
4052 * umounts. Because this function is non-reentrant and is canceled
4053 * before umount can proceed, this is safe.
4055 spin_lock(&cifs_sb->tlink_tree_lock);
4056 node = rb_first(root);
4057 while (node != NULL) {
4059 node = rb_next(tmp);
4060 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4062 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4063 atomic_read(&tlink->tl_count) != 0 ||
4064 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4067 cifs_get_tlink(tlink);
4068 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4069 rb_erase(tmp, root);
4071 spin_unlock(&cifs_sb->tlink_tree_lock);
4072 cifs_put_tlink(tlink);
4073 spin_lock(&cifs_sb->tlink_tree_lock);
4075 spin_unlock(&cifs_sb->tlink_tree_lock);
4077 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4081 #ifndef CONFIG_CIFS_DFS_UPCALL
4082 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
4085 const struct smb_version_operations *ops = tcon->ses->server->ops;
4087 /* only send once per connect */
4088 spin_lock(&tcon->tc_lock);
4089 if (tcon->status != TID_NEW &&
4090 tcon->status != TID_NEED_TCON) {
4091 spin_unlock(&tcon->tc_lock);
4095 if (tcon->status == TID_GOOD) {
4096 spin_unlock(&tcon->tc_lock);
4099 tcon->status = TID_IN_TCON;
4100 spin_unlock(&tcon->tc_lock);
4102 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc);
4104 spin_lock(&tcon->tc_lock);
4105 if (tcon->status == TID_IN_TCON)
4106 tcon->status = TID_NEED_TCON;
4107 spin_unlock(&tcon->tc_lock);
4109 spin_lock(&tcon->tc_lock);
4110 if (tcon->status == TID_IN_TCON)
4111 tcon->status = TID_GOOD;
4112 tcon->need_reconnect = false;
4113 spin_unlock(&tcon->tc_lock);