138c8a9a5SSteve French // SPDX-License-Identifier: LGPL-2.1 238c8a9a5SSteve French /* 338c8a9a5SSteve French * 438c8a9a5SSteve French * Copyright (C) International Business Machines Corp., 2002,2011 538c8a9a5SSteve French * Author(s): Steve French (sfrench@us.ibm.com) 638c8a9a5SSteve French * 738c8a9a5SSteve French */ 838c8a9a5SSteve French #include <linux/fs.h> 938c8a9a5SSteve French #include <linux/net.h> 1038c8a9a5SSteve French #include <linux/string.h> 1138c8a9a5SSteve French #include <linux/sched/mm.h> 1238c8a9a5SSteve French #include <linux/sched/signal.h> 1338c8a9a5SSteve French #include <linux/list.h> 1438c8a9a5SSteve French #include <linux/wait.h> 1538c8a9a5SSteve French #include <linux/slab.h> 1638c8a9a5SSteve French #include <linux/pagemap.h> 1738c8a9a5SSteve French #include <linux/ctype.h> 1838c8a9a5SSteve French #include <linux/utsname.h> 1938c8a9a5SSteve French #include <linux/mempool.h> 2038c8a9a5SSteve French #include <linux/delay.h> 2138c8a9a5SSteve French #include <linux/completion.h> 2238c8a9a5SSteve French #include <linux/kthread.h> 2338c8a9a5SSteve French #include <linux/pagevec.h> 2438c8a9a5SSteve French #include <linux/freezer.h> 2538c8a9a5SSteve French #include <linux/namei.h> 2638c8a9a5SSteve French #include <linux/uuid.h> 2738c8a9a5SSteve French #include <linux/uaccess.h> 2838c8a9a5SSteve French #include <asm/processor.h> 2938c8a9a5SSteve French #include <linux/inet.h> 3038c8a9a5SSteve French #include <linux/module.h> 3138c8a9a5SSteve French #include <keys/user-type.h> 3238c8a9a5SSteve French #include <net/ipv6.h> 3338c8a9a5SSteve French #include <linux/parser.h> 3438c8a9a5SSteve French #include <linux/bvec.h> 3538c8a9a5SSteve French #include "cifspdu.h" 3638c8a9a5SSteve French #include "cifsglob.h" 3738c8a9a5SSteve French #include "cifsproto.h" 3838c8a9a5SSteve French #include "cifs_unicode.h" 3938c8a9a5SSteve French #include "cifs_debug.h" 4038c8a9a5SSteve French #include "cifs_fs_sb.h" 4138c8a9a5SSteve French #include "ntlmssp.h" 4238c8a9a5SSteve French #include "nterr.h" 4338c8a9a5SSteve French #include "rfc1002pdu.h" 4438c8a9a5SSteve French #include "fscache.h" 4538c8a9a5SSteve French #include "smb2proto.h" 4638c8a9a5SSteve French #include "smbdirect.h" 4738c8a9a5SSteve French #include "dns_resolve.h" 4838c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL 4938c8a9a5SSteve French #include "dfs.h" 5038c8a9a5SSteve French #include "dfs_cache.h" 5138c8a9a5SSteve French #endif 5238c8a9a5SSteve French #include "fs_context.h" 5338c8a9a5SSteve French #include "cifs_swn.h" 5438c8a9a5SSteve French 5538c8a9a5SSteve French extern mempool_t *cifs_req_poolp; 5638c8a9a5SSteve French extern bool disable_legacy_dialects; 5738c8a9a5SSteve French 5838c8a9a5SSteve French /* FIXME: should these be tunable? */ 5938c8a9a5SSteve French #define TLINK_ERROR_EXPIRE (1 * HZ) 6038c8a9a5SSteve French #define TLINK_IDLE_EXPIRE (600 * HZ) 6138c8a9a5SSteve French 6238c8a9a5SSteve French /* Drop the connection to not overload the server */ 6369cba9d3SShyam Prasad N #define MAX_STATUS_IO_TIMEOUT 5 6438c8a9a5SSteve French 6538c8a9a5SSteve French static int ip_connect(struct TCP_Server_Info *server); 6638c8a9a5SSteve French static int generic_ip_connect(struct TCP_Server_Info *server); 6738c8a9a5SSteve French static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink); 6838c8a9a5SSteve French static void cifs_prune_tlinks(struct work_struct *work); 6938c8a9a5SSteve French 7038c8a9a5SSteve French /* 7138c8a9a5SSteve French * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may 7238c8a9a5SSteve French * get their ip addresses changed at some point. 7338c8a9a5SSteve French * 7438c8a9a5SSteve French * This should be called with server->srv_mutex held. 7538c8a9a5SSteve French */ 7638c8a9a5SSteve French static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server) 7738c8a9a5SSteve French { 7838c8a9a5SSteve French int rc; 7938c8a9a5SSteve French int len; 8038c8a9a5SSteve French char *unc; 8138c8a9a5SSteve French struct sockaddr_storage ss; 8238c8a9a5SSteve French 8338c8a9a5SSteve French if (!server->hostname) 8438c8a9a5SSteve French return -EINVAL; 8538c8a9a5SSteve French 8638c8a9a5SSteve French /* if server hostname isn't populated, there's nothing to do here */ 8738c8a9a5SSteve French if (server->hostname[0] == '\0') 8838c8a9a5SSteve French return 0; 8938c8a9a5SSteve French 9038c8a9a5SSteve French len = strlen(server->hostname) + 3; 9138c8a9a5SSteve French 9238c8a9a5SSteve French unc = kmalloc(len, GFP_KERNEL); 9338c8a9a5SSteve French if (!unc) { 9438c8a9a5SSteve French cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__); 9538c8a9a5SSteve French return -ENOMEM; 9638c8a9a5SSteve French } 9738c8a9a5SSteve French scnprintf(unc, len, "\\\\%s", server->hostname); 9838c8a9a5SSteve French 9938c8a9a5SSteve French spin_lock(&server->srv_lock); 10038c8a9a5SSteve French ss = server->dstaddr; 10138c8a9a5SSteve French spin_unlock(&server->srv_lock); 10238c8a9a5SSteve French 10338c8a9a5SSteve French rc = dns_resolve_server_name_to_ip(unc, (struct sockaddr *)&ss, NULL); 10438c8a9a5SSteve French kfree(unc); 10538c8a9a5SSteve French 10638c8a9a5SSteve French if (rc < 0) { 10738c8a9a5SSteve French cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n", 10838c8a9a5SSteve French __func__, server->hostname, rc); 10938c8a9a5SSteve French } else { 11038c8a9a5SSteve French spin_lock(&server->srv_lock); 11138c8a9a5SSteve French memcpy(&server->dstaddr, &ss, sizeof(server->dstaddr)); 11238c8a9a5SSteve French spin_unlock(&server->srv_lock); 11338c8a9a5SSteve French rc = 0; 11438c8a9a5SSteve French } 11538c8a9a5SSteve French 11638c8a9a5SSteve French return rc; 11738c8a9a5SSteve French } 11838c8a9a5SSteve French 11938c8a9a5SSteve French static void smb2_query_server_interfaces(struct work_struct *work) 12038c8a9a5SSteve French { 12138c8a9a5SSteve French int rc; 122407ced4eSShyam Prasad N int xid; 12338c8a9a5SSteve French struct cifs_tcon *tcon = container_of(work, 12438c8a9a5SSteve French struct cifs_tcon, 12538c8a9a5SSteve French query_interfaces.work); 12666c2940cSShyam Prasad N struct TCP_Server_Info *server = tcon->ses->server; 12738c8a9a5SSteve French 12838c8a9a5SSteve French /* 12938c8a9a5SSteve French * query server network interfaces, in case they change 13038c8a9a5SSteve French */ 13166c2940cSShyam Prasad N if (!server->ops->query_server_interfaces) 13266c2940cSShyam Prasad N return; 13366c2940cSShyam Prasad N 134407ced4eSShyam Prasad N xid = get_xid(); 13566c2940cSShyam Prasad N rc = server->ops->query_server_interfaces(xid, tcon, false); 136407ced4eSShyam Prasad N free_xid(xid); 137407ced4eSShyam Prasad N 13838c8a9a5SSteve French if (rc) { 1395075e9f4SShyam Prasad N if (rc == -EOPNOTSUPP) 1405075e9f4SShyam Prasad N return; 1415075e9f4SShyam Prasad N 14238c8a9a5SSteve French cifs_dbg(FYI, "%s: failed to query server interfaces: %d\n", 14338c8a9a5SSteve French __func__, rc); 14438c8a9a5SSteve French } 14538c8a9a5SSteve French 14638c8a9a5SSteve French queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, 14738c8a9a5SSteve French (SMB_INTERFACE_POLL_INTERVAL * HZ)); 14838c8a9a5SSteve French } 14938c8a9a5SSteve French 15038c8a9a5SSteve French /* 15138c8a9a5SSteve French * Update the tcpStatus for the server. 15238c8a9a5SSteve French * This is used to signal the cifsd thread to call cifs_reconnect 15338c8a9a5SSteve French * ONLY cifsd thread should call cifs_reconnect. For any other 15438c8a9a5SSteve French * thread, use this function 15538c8a9a5SSteve French * 15638c8a9a5SSteve French * @server: the tcp ses for which reconnect is needed 15738c8a9a5SSteve French * @all_channels: if this needs to be done for all channels 15838c8a9a5SSteve French */ 15938c8a9a5SSteve French void 16038c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(struct TCP_Server_Info *server, 16138c8a9a5SSteve French bool all_channels) 16238c8a9a5SSteve French { 16338c8a9a5SSteve French struct TCP_Server_Info *pserver; 16438c8a9a5SSteve French struct cifs_ses *ses; 16538c8a9a5SSteve French int i; 16638c8a9a5SSteve French 16738c8a9a5SSteve French /* If server is a channel, select the primary channel */ 168b3773b19SSteve French pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 16938c8a9a5SSteve French 170e42c5730SShyam Prasad N /* if we need to signal just this channel */ 17138c8a9a5SSteve French if (!all_channels) { 172e42c5730SShyam Prasad N spin_lock(&server->srv_lock); 173e42c5730SShyam Prasad N if (server->tcpStatus != CifsExiting) 174e42c5730SShyam Prasad N server->tcpStatus = CifsNeedReconnect; 175e42c5730SShyam Prasad N spin_unlock(&server->srv_lock); 17638c8a9a5SSteve French return; 17738c8a9a5SSteve French } 17838c8a9a5SSteve French 17938c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 18038c8a9a5SSteve French list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { 18138c8a9a5SSteve French spin_lock(&ses->chan_lock); 18238c8a9a5SSteve French for (i = 0; i < ses->chan_count; i++) { 183c395f798SShyam Prasad N if (!ses->chans[i].server) 184c395f798SShyam Prasad N continue; 185c395f798SShyam Prasad N 18638c8a9a5SSteve French spin_lock(&ses->chans[i].server->srv_lock); 187c395f798SShyam Prasad N if (ses->chans[i].server->tcpStatus != CifsExiting) 18838c8a9a5SSteve French ses->chans[i].server->tcpStatus = CifsNeedReconnect; 18938c8a9a5SSteve French spin_unlock(&ses->chans[i].server->srv_lock); 19038c8a9a5SSteve French } 19138c8a9a5SSteve French spin_unlock(&ses->chan_lock); 19238c8a9a5SSteve French } 19338c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 19438c8a9a5SSteve French } 19538c8a9a5SSteve French 19638c8a9a5SSteve French /* 19738c8a9a5SSteve French * Mark all sessions and tcons for reconnect. 19838c8a9a5SSteve French * IMPORTANT: make sure that this gets called only from 19938c8a9a5SSteve French * cifsd thread. For any other thread, use 20038c8a9a5SSteve French * cifs_signal_cifsd_for_reconnect 20138c8a9a5SSteve French * 20238c8a9a5SSteve French * @server: the tcp ses for which reconnect is needed 20338c8a9a5SSteve French * @server needs to be previously set to CifsNeedReconnect. 20438c8a9a5SSteve French * @mark_smb_session: whether even sessions need to be marked 20538c8a9a5SSteve French */ 20638c8a9a5SSteve French void 20738c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server, 20838c8a9a5SSteve French bool mark_smb_session) 20938c8a9a5SSteve French { 21038c8a9a5SSteve French struct TCP_Server_Info *pserver; 21138c8a9a5SSteve French struct cifs_ses *ses, *nses; 21238c8a9a5SSteve French struct cifs_tcon *tcon; 21338c8a9a5SSteve French 21438c8a9a5SSteve French /* 21538c8a9a5SSteve French * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they 21638c8a9a5SSteve French * are not used until reconnected. 21738c8a9a5SSteve French */ 21838c8a9a5SSteve French cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__); 21938c8a9a5SSteve French 22038c8a9a5SSteve French /* If server is a channel, select the primary channel */ 221b3773b19SSteve French pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 22238c8a9a5SSteve French 22314bafd19SShyam Prasad N /* 22414bafd19SShyam Prasad N * if the server has been marked for termination, there is a 22514bafd19SShyam Prasad N * chance that the remaining channels all need reconnect. To be 22614bafd19SShyam Prasad N * on the safer side, mark the session and trees for reconnect 22714bafd19SShyam Prasad N * for this scenario. This might cause a few redundant session 22814bafd19SShyam Prasad N * setup and tree connect requests, but it is better than not doing 22914bafd19SShyam Prasad N * a tree connect when needed, and all following requests failing 23014bafd19SShyam Prasad N */ 23114bafd19SShyam Prasad N if (server->terminate) { 23214bafd19SShyam Prasad N mark_smb_session = true; 23314bafd19SShyam Prasad N server = pserver; 23414bafd19SShyam Prasad N } 23538c8a9a5SSteve French 23638c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 23738c8a9a5SSteve French list_for_each_entry_safe(ses, nses, &pserver->smb_ses_list, smb_ses_list) { 23838c8a9a5SSteve French /* check if iface is still active */ 23938c8a9a5SSteve French spin_lock(&ses->chan_lock); 240dd40cbafSShyam Prasad N if (cifs_ses_get_chan_index(ses, server) == 241dd40cbafSShyam Prasad N CIFS_INVAL_CHAN_INDEX) { 242dd40cbafSShyam Prasad N spin_unlock(&ses->chan_lock); 243dd40cbafSShyam Prasad N continue; 244dd40cbafSShyam Prasad N } 245dd40cbafSShyam Prasad N 24640fc7dfdSShyam Prasad N if (!cifs_chan_is_iface_active(ses, server)) { 24740fc7dfdSShyam Prasad N spin_unlock(&ses->chan_lock); 24840fc7dfdSShyam Prasad N cifs_chan_update_iface(ses, server); 24940fc7dfdSShyam Prasad N spin_lock(&ses->chan_lock); 25040fc7dfdSShyam Prasad N } 25140fc7dfdSShyam Prasad N 25238c8a9a5SSteve French if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) { 25338c8a9a5SSteve French spin_unlock(&ses->chan_lock); 25438c8a9a5SSteve French continue; 25538c8a9a5SSteve French } 25638c8a9a5SSteve French 25738c8a9a5SSteve French if (mark_smb_session) 25838c8a9a5SSteve French CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses); 25938c8a9a5SSteve French else 26038c8a9a5SSteve French cifs_chan_set_need_reconnect(ses, server); 26138c8a9a5SSteve French 26238c8a9a5SSteve French cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n", 26338c8a9a5SSteve French __func__, ses->chans_need_reconnect); 26438c8a9a5SSteve French 26538c8a9a5SSteve French /* If all channels need reconnect, then tcon needs reconnect */ 26638c8a9a5SSteve French if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) { 26738c8a9a5SSteve French spin_unlock(&ses->chan_lock); 26838c8a9a5SSteve French continue; 26938c8a9a5SSteve French } 27038c8a9a5SSteve French spin_unlock(&ses->chan_lock); 27138c8a9a5SSteve French 27238c8a9a5SSteve French spin_lock(&ses->ses_lock); 27338c8a9a5SSteve French ses->ses_status = SES_NEED_RECON; 27438c8a9a5SSteve French spin_unlock(&ses->ses_lock); 27538c8a9a5SSteve French 27638c8a9a5SSteve French list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 27738c8a9a5SSteve French tcon->need_reconnect = true; 27838c8a9a5SSteve French spin_lock(&tcon->tc_lock); 27938c8a9a5SSteve French tcon->status = TID_NEED_RECON; 28038c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 2813e161536SShyam Prasad N 2823e161536SShyam Prasad N cancel_delayed_work(&tcon->query_interfaces); 28338c8a9a5SSteve French } 28438c8a9a5SSteve French if (ses->tcon_ipc) { 28538c8a9a5SSteve French ses->tcon_ipc->need_reconnect = true; 28638c8a9a5SSteve French spin_lock(&ses->tcon_ipc->tc_lock); 28738c8a9a5SSteve French ses->tcon_ipc->status = TID_NEED_RECON; 28838c8a9a5SSteve French spin_unlock(&ses->tcon_ipc->tc_lock); 28938c8a9a5SSteve French } 29038c8a9a5SSteve French } 29138c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 29238c8a9a5SSteve French } 29338c8a9a5SSteve French 29438c8a9a5SSteve French static void 29538c8a9a5SSteve French cifs_abort_connection(struct TCP_Server_Info *server) 29638c8a9a5SSteve French { 29738c8a9a5SSteve French struct mid_q_entry *mid, *nmid; 29838c8a9a5SSteve French struct list_head retry_list; 29938c8a9a5SSteve French 30038c8a9a5SSteve French server->maxBuf = 0; 30138c8a9a5SSteve French server->max_read = 0; 30238c8a9a5SSteve French 30338c8a9a5SSteve French /* do not want to be sending data on a socket we are freeing */ 30438c8a9a5SSteve French cifs_dbg(FYI, "%s: tearing down socket\n", __func__); 30538c8a9a5SSteve French cifs_server_lock(server); 30638c8a9a5SSteve French if (server->ssocket) { 30738c8a9a5SSteve French cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state, 30838c8a9a5SSteve French server->ssocket->flags); 30938c8a9a5SSteve French kernel_sock_shutdown(server->ssocket, SHUT_WR); 31038c8a9a5SSteve French cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state, 31138c8a9a5SSteve French server->ssocket->flags); 31238c8a9a5SSteve French sock_release(server->ssocket); 31338c8a9a5SSteve French server->ssocket = NULL; 31438c8a9a5SSteve French } 31538c8a9a5SSteve French server->sequence_number = 0; 31638c8a9a5SSteve French server->session_estab = false; 31738c8a9a5SSteve French kfree_sensitive(server->session_key.response); 31838c8a9a5SSteve French server->session_key.response = NULL; 31938c8a9a5SSteve French server->session_key.len = 0; 32038c8a9a5SSteve French server->lstrp = jiffies; 32138c8a9a5SSteve French 32238c8a9a5SSteve French /* mark submitted MIDs for retry and issue callback */ 32338c8a9a5SSteve French INIT_LIST_HEAD(&retry_list); 32438c8a9a5SSteve French cifs_dbg(FYI, "%s: moving mids to private list\n", __func__); 32538c8a9a5SSteve French spin_lock(&server->mid_lock); 32638c8a9a5SSteve French list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) { 32738c8a9a5SSteve French kref_get(&mid->refcount); 32838c8a9a5SSteve French if (mid->mid_state == MID_REQUEST_SUBMITTED) 32938c8a9a5SSteve French mid->mid_state = MID_RETRY_NEEDED; 33038c8a9a5SSteve French list_move(&mid->qhead, &retry_list); 33138c8a9a5SSteve French mid->mid_flags |= MID_DELETED; 33238c8a9a5SSteve French } 33338c8a9a5SSteve French spin_unlock(&server->mid_lock); 33438c8a9a5SSteve French cifs_server_unlock(server); 33538c8a9a5SSteve French 33638c8a9a5SSteve French cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__); 33738c8a9a5SSteve French list_for_each_entry_safe(mid, nmid, &retry_list, qhead) { 33838c8a9a5SSteve French list_del_init(&mid->qhead); 33938c8a9a5SSteve French mid->callback(mid); 34038c8a9a5SSteve French release_mid(mid); 34138c8a9a5SSteve French } 34238c8a9a5SSteve French 34338c8a9a5SSteve French if (cifs_rdma_enabled(server)) { 34438c8a9a5SSteve French cifs_server_lock(server); 34538c8a9a5SSteve French smbd_destroy(server); 34638c8a9a5SSteve French cifs_server_unlock(server); 34738c8a9a5SSteve French } 34838c8a9a5SSteve French } 34938c8a9a5SSteve French 35038c8a9a5SSteve French static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets) 35138c8a9a5SSteve French { 35238c8a9a5SSteve French spin_lock(&server->srv_lock); 35338c8a9a5SSteve French server->nr_targets = num_targets; 35438c8a9a5SSteve French if (server->tcpStatus == CifsExiting) { 35538c8a9a5SSteve French /* the demux thread will exit normally next time through the loop */ 35638c8a9a5SSteve French spin_unlock(&server->srv_lock); 35738c8a9a5SSteve French wake_up(&server->response_q); 35838c8a9a5SSteve French return false; 35938c8a9a5SSteve French } 36038c8a9a5SSteve French 36138c8a9a5SSteve French cifs_dbg(FYI, "Mark tcp session as need reconnect\n"); 36238c8a9a5SSteve French trace_smb3_reconnect(server->CurrentMid, server->conn_id, 36338c8a9a5SSteve French server->hostname); 36438c8a9a5SSteve French server->tcpStatus = CifsNeedReconnect; 36538c8a9a5SSteve French 36638c8a9a5SSteve French spin_unlock(&server->srv_lock); 36738c8a9a5SSteve French return true; 36838c8a9a5SSteve French } 36938c8a9a5SSteve French 37038c8a9a5SSteve French /* 37138c8a9a5SSteve French * cifs tcp session reconnection 37238c8a9a5SSteve French * 37338c8a9a5SSteve French * mark tcp session as reconnecting so temporarily locked 37438c8a9a5SSteve French * mark all smb sessions as reconnecting for tcp session 37538c8a9a5SSteve French * reconnect tcp session 37638c8a9a5SSteve French * wake up waiters on reconnection? - (not needed currently) 37738c8a9a5SSteve French * 37838c8a9a5SSteve French * if mark_smb_session is passed as true, unconditionally mark 37938c8a9a5SSteve French * the smb session (and tcon) for reconnect as well. This value 38038c8a9a5SSteve French * doesn't really matter for non-multichannel scenario. 38138c8a9a5SSteve French * 38238c8a9a5SSteve French */ 38338c8a9a5SSteve French static int __cifs_reconnect(struct TCP_Server_Info *server, 38438c8a9a5SSteve French bool mark_smb_session) 38538c8a9a5SSteve French { 38638c8a9a5SSteve French int rc = 0; 38738c8a9a5SSteve French 38838c8a9a5SSteve French if (!cifs_tcp_ses_needs_reconnect(server, 1)) 38938c8a9a5SSteve French return 0; 39038c8a9a5SSteve French 39138c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session); 39238c8a9a5SSteve French 39338c8a9a5SSteve French cifs_abort_connection(server); 39438c8a9a5SSteve French 39538c8a9a5SSteve French do { 39638c8a9a5SSteve French try_to_freeze(); 39738c8a9a5SSteve French cifs_server_lock(server); 39838c8a9a5SSteve French 39938c8a9a5SSteve French if (!cifs_swn_set_server_dstaddr(server)) { 40038c8a9a5SSteve French /* resolve the hostname again to make sure that IP address is up-to-date */ 40138c8a9a5SSteve French rc = reconn_set_ipaddr_from_hostname(server); 40238c8a9a5SSteve French cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); 40338c8a9a5SSteve French } 40438c8a9a5SSteve French 40538c8a9a5SSteve French if (cifs_rdma_enabled(server)) 40638c8a9a5SSteve French rc = smbd_reconnect(server); 40738c8a9a5SSteve French else 40838c8a9a5SSteve French rc = generic_ip_connect(server); 40938c8a9a5SSteve French if (rc) { 41038c8a9a5SSteve French cifs_server_unlock(server); 41138c8a9a5SSteve French cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); 41238c8a9a5SSteve French msleep(3000); 41338c8a9a5SSteve French } else { 41438c8a9a5SSteve French atomic_inc(&tcpSesReconnectCount); 41538c8a9a5SSteve French set_credits(server, 1); 41638c8a9a5SSteve French spin_lock(&server->srv_lock); 41738c8a9a5SSteve French if (server->tcpStatus != CifsExiting) 41838c8a9a5SSteve French server->tcpStatus = CifsNeedNegotiate; 41938c8a9a5SSteve French spin_unlock(&server->srv_lock); 42038c8a9a5SSteve French cifs_swn_reset_server_dstaddr(server); 42138c8a9a5SSteve French cifs_server_unlock(server); 4223a0ef868SShyam Prasad N mod_delayed_work(cifsiod_wq, &server->reconnect, 0); 42338c8a9a5SSteve French } 42438c8a9a5SSteve French } while (server->tcpStatus == CifsNeedReconnect); 42538c8a9a5SSteve French 42638c8a9a5SSteve French spin_lock(&server->srv_lock); 42738c8a9a5SSteve French if (server->tcpStatus == CifsNeedNegotiate) 42838c8a9a5SSteve French mod_delayed_work(cifsiod_wq, &server->echo, 0); 42938c8a9a5SSteve French spin_unlock(&server->srv_lock); 43038c8a9a5SSteve French 43138c8a9a5SSteve French wake_up(&server->response_q); 43238c8a9a5SSteve French return rc; 43338c8a9a5SSteve French } 43438c8a9a5SSteve French 43538c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL 43638c8a9a5SSteve French static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target) 43738c8a9a5SSteve French { 43838c8a9a5SSteve French int rc; 43938c8a9a5SSteve French char *hostname; 44038c8a9a5SSteve French 44138c8a9a5SSteve French if (!cifs_swn_set_server_dstaddr(server)) { 44238c8a9a5SSteve French if (server->hostname != target) { 44338c8a9a5SSteve French hostname = extract_hostname(target); 44438c8a9a5SSteve French if (!IS_ERR(hostname)) { 44538c8a9a5SSteve French spin_lock(&server->srv_lock); 44638c8a9a5SSteve French kfree(server->hostname); 44738c8a9a5SSteve French server->hostname = hostname; 44838c8a9a5SSteve French spin_unlock(&server->srv_lock); 44938c8a9a5SSteve French } else { 45038c8a9a5SSteve French cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n", 45138c8a9a5SSteve French __func__, PTR_ERR(hostname)); 45238c8a9a5SSteve French cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__, 45338c8a9a5SSteve French server->hostname); 45438c8a9a5SSteve French } 45538c8a9a5SSteve French } 45638c8a9a5SSteve French /* resolve the hostname again to make sure that IP address is up-to-date. */ 45738c8a9a5SSteve French rc = reconn_set_ipaddr_from_hostname(server); 45838c8a9a5SSteve French cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); 45938c8a9a5SSteve French } 46038c8a9a5SSteve French /* Reconnect the socket */ 46138c8a9a5SSteve French if (cifs_rdma_enabled(server)) 46238c8a9a5SSteve French rc = smbd_reconnect(server); 46338c8a9a5SSteve French else 46438c8a9a5SSteve French rc = generic_ip_connect(server); 46538c8a9a5SSteve French 46638c8a9a5SSteve French return rc; 46738c8a9a5SSteve French } 46838c8a9a5SSteve French 46938c8a9a5SSteve French static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl, 47038c8a9a5SSteve French struct dfs_cache_tgt_iterator **target_hint) 47138c8a9a5SSteve French { 47238c8a9a5SSteve French int rc; 47338c8a9a5SSteve French struct dfs_cache_tgt_iterator *tit; 47438c8a9a5SSteve French 47538c8a9a5SSteve French *target_hint = NULL; 47638c8a9a5SSteve French 47738c8a9a5SSteve French /* If dfs target list is empty, then reconnect to last server */ 47838c8a9a5SSteve French tit = dfs_cache_get_tgt_iterator(tl); 47938c8a9a5SSteve French if (!tit) 48038c8a9a5SSteve French return __reconnect_target_unlocked(server, server->hostname); 48138c8a9a5SSteve French 48238c8a9a5SSteve French /* Otherwise, try every dfs target in @tl */ 48338c8a9a5SSteve French for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) { 48438c8a9a5SSteve French rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit)); 48538c8a9a5SSteve French if (!rc) { 48638c8a9a5SSteve French *target_hint = tit; 48738c8a9a5SSteve French break; 48838c8a9a5SSteve French } 48938c8a9a5SSteve French } 49038c8a9a5SSteve French return rc; 49138c8a9a5SSteve French } 49238c8a9a5SSteve French 49338c8a9a5SSteve French static int reconnect_dfs_server(struct TCP_Server_Info *server) 49438c8a9a5SSteve French { 49538c8a9a5SSteve French struct dfs_cache_tgt_iterator *target_hint = NULL; 4963fea12f3SPaulo Alcantara DFS_CACHE_TGT_LIST(tl); 49738c8a9a5SSteve French int num_targets = 0; 4983fea12f3SPaulo Alcantara int rc = 0; 49938c8a9a5SSteve French 50038c8a9a5SSteve French /* 50138c8a9a5SSteve French * Determine the number of dfs targets the referral path in @cifs_sb resolves to. 50238c8a9a5SSteve French * 50338c8a9a5SSteve French * smb2_reconnect() needs to know how long it should wait based upon the number of dfs 50438c8a9a5SSteve French * targets (server->nr_targets). It's also possible that the cached referral was cleared 50538c8a9a5SSteve French * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after 50638c8a9a5SSteve French * refreshing the referral, so, in this case, default it to 1. 50738c8a9a5SSteve French */ 50838c8a9a5SSteve French mutex_lock(&server->refpath_lock); 50938c8a9a5SSteve French if (!dfs_cache_noreq_find(server->leaf_fullpath + 1, NULL, &tl)) 51038c8a9a5SSteve French num_targets = dfs_cache_get_nr_tgts(&tl); 51138c8a9a5SSteve French mutex_unlock(&server->refpath_lock); 51238c8a9a5SSteve French if (!num_targets) 51338c8a9a5SSteve French num_targets = 1; 51438c8a9a5SSteve French 51538c8a9a5SSteve French if (!cifs_tcp_ses_needs_reconnect(server, num_targets)) 51638c8a9a5SSteve French return 0; 51738c8a9a5SSteve French 51838c8a9a5SSteve French /* 51938c8a9a5SSteve French * Unconditionally mark all sessions & tcons for reconnect as we might be connecting to a 52038c8a9a5SSteve French * different server or share during failover. It could be improved by adding some logic to 52138c8a9a5SSteve French * only do that in case it connects to a different server or share, though. 52238c8a9a5SSteve French */ 52338c8a9a5SSteve French cifs_mark_tcp_ses_conns_for_reconnect(server, true); 52438c8a9a5SSteve French 52538c8a9a5SSteve French cifs_abort_connection(server); 52638c8a9a5SSteve French 52738c8a9a5SSteve French do { 52838c8a9a5SSteve French try_to_freeze(); 52938c8a9a5SSteve French cifs_server_lock(server); 53038c8a9a5SSteve French 53138c8a9a5SSteve French rc = reconnect_target_unlocked(server, &tl, &target_hint); 53238c8a9a5SSteve French if (rc) { 53338c8a9a5SSteve French /* Failed to reconnect socket */ 53438c8a9a5SSteve French cifs_server_unlock(server); 53538c8a9a5SSteve French cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); 53638c8a9a5SSteve French msleep(3000); 53738c8a9a5SSteve French continue; 53838c8a9a5SSteve French } 53938c8a9a5SSteve French /* 54038c8a9a5SSteve French * Socket was created. Update tcp session status to CifsNeedNegotiate so that a 54138c8a9a5SSteve French * process waiting for reconnect will know it needs to re-establish session and tcon 54238c8a9a5SSteve French * through the reconnected target server. 54338c8a9a5SSteve French */ 54438c8a9a5SSteve French atomic_inc(&tcpSesReconnectCount); 54538c8a9a5SSteve French set_credits(server, 1); 54638c8a9a5SSteve French spin_lock(&server->srv_lock); 54738c8a9a5SSteve French if (server->tcpStatus != CifsExiting) 54838c8a9a5SSteve French server->tcpStatus = CifsNeedNegotiate; 54938c8a9a5SSteve French spin_unlock(&server->srv_lock); 55038c8a9a5SSteve French cifs_swn_reset_server_dstaddr(server); 55138c8a9a5SSteve French cifs_server_unlock(server); 5523a0ef868SShyam Prasad N mod_delayed_work(cifsiod_wq, &server->reconnect, 0); 55338c8a9a5SSteve French } while (server->tcpStatus == CifsNeedReconnect); 55438c8a9a5SSteve French 55538c8a9a5SSteve French mutex_lock(&server->refpath_lock); 55638c8a9a5SSteve French dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, target_hint); 55738c8a9a5SSteve French mutex_unlock(&server->refpath_lock); 55838c8a9a5SSteve French dfs_cache_free_tgts(&tl); 55938c8a9a5SSteve French 56038c8a9a5SSteve French /* Need to set up echo worker again once connection has been established */ 56138c8a9a5SSteve French spin_lock(&server->srv_lock); 56238c8a9a5SSteve French if (server->tcpStatus == CifsNeedNegotiate) 56338c8a9a5SSteve French mod_delayed_work(cifsiod_wq, &server->echo, 0); 56438c8a9a5SSteve French spin_unlock(&server->srv_lock); 56538c8a9a5SSteve French 56638c8a9a5SSteve French wake_up(&server->response_q); 56738c8a9a5SSteve French return rc; 56838c8a9a5SSteve French } 56938c8a9a5SSteve French 57038c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) 57138c8a9a5SSteve French { 57238c8a9a5SSteve French mutex_lock(&server->refpath_lock); 57338c8a9a5SSteve French if (!server->leaf_fullpath) { 57438c8a9a5SSteve French mutex_unlock(&server->refpath_lock); 57538c8a9a5SSteve French return __cifs_reconnect(server, mark_smb_session); 57638c8a9a5SSteve French } 57738c8a9a5SSteve French mutex_unlock(&server->refpath_lock); 57838c8a9a5SSteve French 57938c8a9a5SSteve French return reconnect_dfs_server(server); 58038c8a9a5SSteve French } 58138c8a9a5SSteve French #else 58238c8a9a5SSteve French int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) 58338c8a9a5SSteve French { 58438c8a9a5SSteve French return __cifs_reconnect(server, mark_smb_session); 58538c8a9a5SSteve French } 58638c8a9a5SSteve French #endif 58738c8a9a5SSteve French 58838c8a9a5SSteve French static void 58938c8a9a5SSteve French cifs_echo_request(struct work_struct *work) 59038c8a9a5SSteve French { 59138c8a9a5SSteve French int rc; 59238c8a9a5SSteve French struct TCP_Server_Info *server = container_of(work, 59338c8a9a5SSteve French struct TCP_Server_Info, echo.work); 59438c8a9a5SSteve French 59538c8a9a5SSteve French /* 59638c8a9a5SSteve French * We cannot send an echo if it is disabled. 59738c8a9a5SSteve French * Also, no need to ping if we got a response recently. 59838c8a9a5SSteve French */ 59938c8a9a5SSteve French 60038c8a9a5SSteve French if (server->tcpStatus == CifsNeedReconnect || 60138c8a9a5SSteve French server->tcpStatus == CifsExiting || 60238c8a9a5SSteve French server->tcpStatus == CifsNew || 60338c8a9a5SSteve French (server->ops->can_echo && !server->ops->can_echo(server)) || 60438c8a9a5SSteve French time_before(jiffies, server->lstrp + server->echo_interval - HZ)) 60538c8a9a5SSteve French goto requeue_echo; 60638c8a9a5SSteve French 60738c8a9a5SSteve French rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS; 60838c8a9a5SSteve French cifs_server_dbg(FYI, "send echo request: rc = %d\n", rc); 60938c8a9a5SSteve French 61038c8a9a5SSteve French /* Check witness registrations */ 61138c8a9a5SSteve French cifs_swn_check(); 61238c8a9a5SSteve French 61338c8a9a5SSteve French requeue_echo: 61438c8a9a5SSteve French queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval); 61538c8a9a5SSteve French } 61638c8a9a5SSteve French 61738c8a9a5SSteve French static bool 61838c8a9a5SSteve French allocate_buffers(struct TCP_Server_Info *server) 61938c8a9a5SSteve French { 62038c8a9a5SSteve French if (!server->bigbuf) { 62138c8a9a5SSteve French server->bigbuf = (char *)cifs_buf_get(); 62238c8a9a5SSteve French if (!server->bigbuf) { 62338c8a9a5SSteve French cifs_server_dbg(VFS, "No memory for large SMB response\n"); 62438c8a9a5SSteve French msleep(3000); 62538c8a9a5SSteve French /* retry will check if exiting */ 62638c8a9a5SSteve French return false; 62738c8a9a5SSteve French } 62838c8a9a5SSteve French } else if (server->large_buf) { 62938c8a9a5SSteve French /* we are reusing a dirty large buf, clear its start */ 63038c8a9a5SSteve French memset(server->bigbuf, 0, HEADER_SIZE(server)); 63138c8a9a5SSteve French } 63238c8a9a5SSteve French 63338c8a9a5SSteve French if (!server->smallbuf) { 63438c8a9a5SSteve French server->smallbuf = (char *)cifs_small_buf_get(); 63538c8a9a5SSteve French if (!server->smallbuf) { 63638c8a9a5SSteve French cifs_server_dbg(VFS, "No memory for SMB response\n"); 63738c8a9a5SSteve French msleep(1000); 63838c8a9a5SSteve French /* retry will check if exiting */ 63938c8a9a5SSteve French return false; 64038c8a9a5SSteve French } 64138c8a9a5SSteve French /* beginning of smb buffer is cleared in our buf_get */ 64238c8a9a5SSteve French } else { 64338c8a9a5SSteve French /* if existing small buf clear beginning */ 64438c8a9a5SSteve French memset(server->smallbuf, 0, HEADER_SIZE(server)); 64538c8a9a5SSteve French } 64638c8a9a5SSteve French 64738c8a9a5SSteve French return true; 64838c8a9a5SSteve French } 64938c8a9a5SSteve French 65038c8a9a5SSteve French static bool 65138c8a9a5SSteve French server_unresponsive(struct TCP_Server_Info *server) 65238c8a9a5SSteve French { 65338c8a9a5SSteve French /* 65438c8a9a5SSteve French * We need to wait 3 echo intervals to make sure we handle such 65538c8a9a5SSteve French * situations right: 65638c8a9a5SSteve French * 1s client sends a normal SMB request 65738c8a9a5SSteve French * 2s client gets a response 65838c8a9a5SSteve French * 30s echo workqueue job pops, and decides we got a response recently 65938c8a9a5SSteve French * and don't need to send another 66038c8a9a5SSteve French * ... 66138c8a9a5SSteve French * 65s kernel_recvmsg times out, and we see that we haven't gotten 66238c8a9a5SSteve French * a response in >60s. 66338c8a9a5SSteve French */ 66438c8a9a5SSteve French spin_lock(&server->srv_lock); 66538c8a9a5SSteve French if ((server->tcpStatus == CifsGood || 66638c8a9a5SSteve French server->tcpStatus == CifsNeedNegotiate) && 66738c8a9a5SSteve French (!server->ops->can_echo || server->ops->can_echo(server)) && 66838c8a9a5SSteve French time_after(jiffies, server->lstrp + 3 * server->echo_interval)) { 66938c8a9a5SSteve French spin_unlock(&server->srv_lock); 67038c8a9a5SSteve French cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n", 67138c8a9a5SSteve French (3 * server->echo_interval) / HZ); 67238c8a9a5SSteve French cifs_reconnect(server, false); 67338c8a9a5SSteve French return true; 67438c8a9a5SSteve French } 67538c8a9a5SSteve French spin_unlock(&server->srv_lock); 67638c8a9a5SSteve French 67738c8a9a5SSteve French return false; 67838c8a9a5SSteve French } 67938c8a9a5SSteve French 68038c8a9a5SSteve French static inline bool 68138c8a9a5SSteve French zero_credits(struct TCP_Server_Info *server) 68238c8a9a5SSteve French { 68338c8a9a5SSteve French int val; 68438c8a9a5SSteve French 68538c8a9a5SSteve French spin_lock(&server->req_lock); 68638c8a9a5SSteve French val = server->credits + server->echo_credits + server->oplock_credits; 68738c8a9a5SSteve French if (server->in_flight == 0 && val == 0) { 68838c8a9a5SSteve French spin_unlock(&server->req_lock); 68938c8a9a5SSteve French return true; 69038c8a9a5SSteve French } 69138c8a9a5SSteve French spin_unlock(&server->req_lock); 69238c8a9a5SSteve French return false; 69338c8a9a5SSteve French } 69438c8a9a5SSteve French 69538c8a9a5SSteve French static int 69638c8a9a5SSteve French cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) 69738c8a9a5SSteve French { 69838c8a9a5SSteve French int length = 0; 69938c8a9a5SSteve French int total_read; 70038c8a9a5SSteve French 70138c8a9a5SSteve French for (total_read = 0; msg_data_left(smb_msg); total_read += length) { 70238c8a9a5SSteve French try_to_freeze(); 70338c8a9a5SSteve French 70438c8a9a5SSteve French /* reconnect if no credits and no requests in flight */ 70538c8a9a5SSteve French if (zero_credits(server)) { 70638c8a9a5SSteve French cifs_reconnect(server, false); 70738c8a9a5SSteve French return -ECONNABORTED; 70838c8a9a5SSteve French } 70938c8a9a5SSteve French 71038c8a9a5SSteve French if (server_unresponsive(server)) 71138c8a9a5SSteve French return -ECONNABORTED; 71238c8a9a5SSteve French if (cifs_rdma_enabled(server) && server->smbd_conn) 71338c8a9a5SSteve French length = smbd_recv(server->smbd_conn, smb_msg); 71438c8a9a5SSteve French else 71538c8a9a5SSteve French length = sock_recvmsg(server->ssocket, smb_msg, 0); 71638c8a9a5SSteve French 71738c8a9a5SSteve French spin_lock(&server->srv_lock); 71838c8a9a5SSteve French if (server->tcpStatus == CifsExiting) { 71938c8a9a5SSteve French spin_unlock(&server->srv_lock); 72038c8a9a5SSteve French return -ESHUTDOWN; 72138c8a9a5SSteve French } 72238c8a9a5SSteve French 72338c8a9a5SSteve French if (server->tcpStatus == CifsNeedReconnect) { 72438c8a9a5SSteve French spin_unlock(&server->srv_lock); 72538c8a9a5SSteve French cifs_reconnect(server, false); 72638c8a9a5SSteve French return -ECONNABORTED; 72738c8a9a5SSteve French } 72838c8a9a5SSteve French spin_unlock(&server->srv_lock); 72938c8a9a5SSteve French 73038c8a9a5SSteve French if (length == -ERESTARTSYS || 73138c8a9a5SSteve French length == -EAGAIN || 73238c8a9a5SSteve French length == -EINTR) { 73338c8a9a5SSteve French /* 73438c8a9a5SSteve French * Minimum sleep to prevent looping, allowing socket 73538c8a9a5SSteve French * to clear and app threads to set tcpStatus 73638c8a9a5SSteve French * CifsNeedReconnect if server hung. 73738c8a9a5SSteve French */ 73838c8a9a5SSteve French usleep_range(1000, 2000); 73938c8a9a5SSteve French length = 0; 74038c8a9a5SSteve French continue; 74138c8a9a5SSteve French } 74238c8a9a5SSteve French 74338c8a9a5SSteve French if (length <= 0) { 74438c8a9a5SSteve French cifs_dbg(FYI, "Received no data or error: %d\n", length); 74538c8a9a5SSteve French cifs_reconnect(server, false); 74638c8a9a5SSteve French return -ECONNABORTED; 74738c8a9a5SSteve French } 74838c8a9a5SSteve French } 74938c8a9a5SSteve French return total_read; 75038c8a9a5SSteve French } 75138c8a9a5SSteve French 75238c8a9a5SSteve French int 75338c8a9a5SSteve French cifs_read_from_socket(struct TCP_Server_Info *server, char *buf, 75438c8a9a5SSteve French unsigned int to_read) 75538c8a9a5SSteve French { 75638c8a9a5SSteve French struct msghdr smb_msg = {}; 75738c8a9a5SSteve French struct kvec iov = {.iov_base = buf, .iov_len = to_read}; 75838c8a9a5SSteve French iov_iter_kvec(&smb_msg.msg_iter, ITER_DEST, &iov, 1, to_read); 75938c8a9a5SSteve French 76038c8a9a5SSteve French return cifs_readv_from_socket(server, &smb_msg); 76138c8a9a5SSteve French } 76238c8a9a5SSteve French 76338c8a9a5SSteve French ssize_t 76438c8a9a5SSteve French cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read) 76538c8a9a5SSteve French { 76638c8a9a5SSteve French struct msghdr smb_msg = {}; 76738c8a9a5SSteve French 76838c8a9a5SSteve French /* 76938c8a9a5SSteve French * iov_iter_discard already sets smb_msg.type and count and iov_offset 77038c8a9a5SSteve French * and cifs_readv_from_socket sets msg_control and msg_controllen 77138c8a9a5SSteve French * so little to initialize in struct msghdr 77238c8a9a5SSteve French */ 77338c8a9a5SSteve French iov_iter_discard(&smb_msg.msg_iter, ITER_DEST, to_read); 77438c8a9a5SSteve French 77538c8a9a5SSteve French return cifs_readv_from_socket(server, &smb_msg); 77638c8a9a5SSteve French } 77738c8a9a5SSteve French 77838c8a9a5SSteve French int 77938c8a9a5SSteve French cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page, 78038c8a9a5SSteve French unsigned int page_offset, unsigned int to_read) 78138c8a9a5SSteve French { 78238c8a9a5SSteve French struct msghdr smb_msg = {}; 78338c8a9a5SSteve French struct bio_vec bv; 78438c8a9a5SSteve French 78538c8a9a5SSteve French bvec_set_page(&bv, page, to_read, page_offset); 78638c8a9a5SSteve French iov_iter_bvec(&smb_msg.msg_iter, ITER_DEST, &bv, 1, to_read); 78738c8a9a5SSteve French return cifs_readv_from_socket(server, &smb_msg); 78838c8a9a5SSteve French } 78938c8a9a5SSteve French 79038c8a9a5SSteve French int 79138c8a9a5SSteve French cifs_read_iter_from_socket(struct TCP_Server_Info *server, struct iov_iter *iter, 79238c8a9a5SSteve French unsigned int to_read) 79338c8a9a5SSteve French { 79438c8a9a5SSteve French struct msghdr smb_msg = { .msg_iter = *iter }; 79538c8a9a5SSteve French int ret; 79638c8a9a5SSteve French 79738c8a9a5SSteve French iov_iter_truncate(&smb_msg.msg_iter, to_read); 79838c8a9a5SSteve French ret = cifs_readv_from_socket(server, &smb_msg); 79938c8a9a5SSteve French if (ret > 0) 80038c8a9a5SSteve French iov_iter_advance(iter, ret); 80138c8a9a5SSteve French return ret; 80238c8a9a5SSteve French } 80338c8a9a5SSteve French 80438c8a9a5SSteve French static bool 80538c8a9a5SSteve French is_smb_response(struct TCP_Server_Info *server, unsigned char type) 80638c8a9a5SSteve French { 80738c8a9a5SSteve French /* 80838c8a9a5SSteve French * The first byte big endian of the length field, 80938c8a9a5SSteve French * is actually not part of the length but the type 81038c8a9a5SSteve French * with the most common, zero, as regular data. 81138c8a9a5SSteve French */ 81238c8a9a5SSteve French switch (type) { 81338c8a9a5SSteve French case RFC1002_SESSION_MESSAGE: 81438c8a9a5SSteve French /* Regular SMB response */ 81538c8a9a5SSteve French return true; 81638c8a9a5SSteve French case RFC1002_SESSION_KEEP_ALIVE: 81738c8a9a5SSteve French cifs_dbg(FYI, "RFC 1002 session keep alive\n"); 81838c8a9a5SSteve French break; 81938c8a9a5SSteve French case RFC1002_POSITIVE_SESSION_RESPONSE: 82038c8a9a5SSteve French cifs_dbg(FYI, "RFC 1002 positive session response\n"); 82138c8a9a5SSteve French break; 82238c8a9a5SSteve French case RFC1002_NEGATIVE_SESSION_RESPONSE: 82338c8a9a5SSteve French /* 82438c8a9a5SSteve French * We get this from Windows 98 instead of an error on 82538c8a9a5SSteve French * SMB negprot response. 82638c8a9a5SSteve French */ 82738c8a9a5SSteve French cifs_dbg(FYI, "RFC 1002 negative session response\n"); 82838c8a9a5SSteve French /* give server a second to clean up */ 82938c8a9a5SSteve French msleep(1000); 83038c8a9a5SSteve French /* 83138c8a9a5SSteve French * Always try 445 first on reconnect since we get NACK 83238c8a9a5SSteve French * on some if we ever connected to port 139 (the NACK 83338c8a9a5SSteve French * is since we do not begin with RFC1001 session 83438c8a9a5SSteve French * initialize frame). 83538c8a9a5SSteve French */ 83638c8a9a5SSteve French cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT); 83738c8a9a5SSteve French cifs_reconnect(server, true); 83838c8a9a5SSteve French break; 83938c8a9a5SSteve French default: 84038c8a9a5SSteve French cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type); 84138c8a9a5SSteve French cifs_reconnect(server, true); 84238c8a9a5SSteve French } 84338c8a9a5SSteve French 84438c8a9a5SSteve French return false; 84538c8a9a5SSteve French } 84638c8a9a5SSteve French 84738c8a9a5SSteve French void 84838c8a9a5SSteve French dequeue_mid(struct mid_q_entry *mid, bool malformed) 84938c8a9a5SSteve French { 85038c8a9a5SSteve French #ifdef CONFIG_CIFS_STATS2 85138c8a9a5SSteve French mid->when_received = jiffies; 85238c8a9a5SSteve French #endif 85338c8a9a5SSteve French spin_lock(&mid->server->mid_lock); 85438c8a9a5SSteve French if (!malformed) 85538c8a9a5SSteve French mid->mid_state = MID_RESPONSE_RECEIVED; 85638c8a9a5SSteve French else 85738c8a9a5SSteve French mid->mid_state = MID_RESPONSE_MALFORMED; 85838c8a9a5SSteve French /* 85938c8a9a5SSteve French * Trying to handle/dequeue a mid after the send_recv() 86038c8a9a5SSteve French * function has finished processing it is a bug. 86138c8a9a5SSteve French */ 86238c8a9a5SSteve French if (mid->mid_flags & MID_DELETED) { 86338c8a9a5SSteve French spin_unlock(&mid->server->mid_lock); 86438c8a9a5SSteve French pr_warn_once("trying to dequeue a deleted mid\n"); 86538c8a9a5SSteve French } else { 86638c8a9a5SSteve French list_del_init(&mid->qhead); 86738c8a9a5SSteve French mid->mid_flags |= MID_DELETED; 86838c8a9a5SSteve French spin_unlock(&mid->server->mid_lock); 86938c8a9a5SSteve French } 87038c8a9a5SSteve French } 87138c8a9a5SSteve French 87238c8a9a5SSteve French static unsigned int 87338c8a9a5SSteve French smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) 87438c8a9a5SSteve French { 87538c8a9a5SSteve French struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; 87638c8a9a5SSteve French 87738c8a9a5SSteve French /* 87838c8a9a5SSteve French * SMB1 does not use credits. 87938c8a9a5SSteve French */ 88038c8a9a5SSteve French if (is_smb1(server)) 88138c8a9a5SSteve French return 0; 88238c8a9a5SSteve French 88338c8a9a5SSteve French return le16_to_cpu(shdr->CreditRequest); 88438c8a9a5SSteve French } 88538c8a9a5SSteve French 88638c8a9a5SSteve French static void 88738c8a9a5SSteve French handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server, 88838c8a9a5SSteve French char *buf, int malformed) 88938c8a9a5SSteve French { 89038c8a9a5SSteve French if (server->ops->check_trans2 && 89138c8a9a5SSteve French server->ops->check_trans2(mid, server, buf, malformed)) 89238c8a9a5SSteve French return; 89338c8a9a5SSteve French mid->credits_received = smb2_get_credits_from_hdr(buf, server); 89438c8a9a5SSteve French mid->resp_buf = buf; 89538c8a9a5SSteve French mid->large_buf = server->large_buf; 89638c8a9a5SSteve French /* Was previous buf put in mpx struct for multi-rsp? */ 89738c8a9a5SSteve French if (!mid->multiRsp) { 89838c8a9a5SSteve French /* smb buffer will be freed by user thread */ 89938c8a9a5SSteve French if (server->large_buf) 90038c8a9a5SSteve French server->bigbuf = NULL; 90138c8a9a5SSteve French else 90238c8a9a5SSteve French server->smallbuf = NULL; 90338c8a9a5SSteve French } 90438c8a9a5SSteve French dequeue_mid(mid, malformed); 90538c8a9a5SSteve French } 90638c8a9a5SSteve French 90738c8a9a5SSteve French int 90838c8a9a5SSteve French cifs_enable_signing(struct TCP_Server_Info *server, bool mnt_sign_required) 90938c8a9a5SSteve French { 91038c8a9a5SSteve French bool srv_sign_required = server->sec_mode & server->vals->signing_required; 91138c8a9a5SSteve French bool srv_sign_enabled = server->sec_mode & server->vals->signing_enabled; 91238c8a9a5SSteve French bool mnt_sign_enabled; 91338c8a9a5SSteve French 91438c8a9a5SSteve French /* 91538c8a9a5SSteve French * Is signing required by mnt options? If not then check 91638c8a9a5SSteve French * global_secflags to see if it is there. 91738c8a9a5SSteve French */ 91838c8a9a5SSteve French if (!mnt_sign_required) 91938c8a9a5SSteve French mnt_sign_required = ((global_secflags & CIFSSEC_MUST_SIGN) == 92038c8a9a5SSteve French CIFSSEC_MUST_SIGN); 92138c8a9a5SSteve French 92238c8a9a5SSteve French /* 92338c8a9a5SSteve French * If signing is required then it's automatically enabled too, 92438c8a9a5SSteve French * otherwise, check to see if the secflags allow it. 92538c8a9a5SSteve French */ 92638c8a9a5SSteve French mnt_sign_enabled = mnt_sign_required ? mnt_sign_required : 92738c8a9a5SSteve French (global_secflags & CIFSSEC_MAY_SIGN); 92838c8a9a5SSteve French 92938c8a9a5SSteve French /* If server requires signing, does client allow it? */ 93038c8a9a5SSteve French if (srv_sign_required) { 93138c8a9a5SSteve French if (!mnt_sign_enabled) { 93238c8a9a5SSteve French cifs_dbg(VFS, "Server requires signing, but it's disabled in SecurityFlags!\n"); 93338c8a9a5SSteve French return -EOPNOTSUPP; 93438c8a9a5SSteve French } 93538c8a9a5SSteve French server->sign = true; 93638c8a9a5SSteve French } 93738c8a9a5SSteve French 93838c8a9a5SSteve French /* If client requires signing, does server allow it? */ 93938c8a9a5SSteve French if (mnt_sign_required) { 94038c8a9a5SSteve French if (!srv_sign_enabled) { 94138c8a9a5SSteve French cifs_dbg(VFS, "Server does not support signing!\n"); 94238c8a9a5SSteve French return -EOPNOTSUPP; 94338c8a9a5SSteve French } 94438c8a9a5SSteve French server->sign = true; 94538c8a9a5SSteve French } 94638c8a9a5SSteve French 94738c8a9a5SSteve French if (cifs_rdma_enabled(server) && server->sign) 94838c8a9a5SSteve French cifs_dbg(VFS, "Signing is enabled, and RDMA read/write will be disabled\n"); 94938c8a9a5SSteve French 95038c8a9a5SSteve French return 0; 95138c8a9a5SSteve French } 95238c8a9a5SSteve French 953946ad1b8SPaulo Alcantara static noinline_for_stack void 954946ad1b8SPaulo Alcantara clean_demultiplex_info(struct TCP_Server_Info *server) 95538c8a9a5SSteve French { 95638c8a9a5SSteve French int length; 95738c8a9a5SSteve French 95838c8a9a5SSteve French /* take it off the list, if it's not already */ 95938c8a9a5SSteve French spin_lock(&server->srv_lock); 96038c8a9a5SSteve French list_del_init(&server->tcp_ses_list); 96138c8a9a5SSteve French spin_unlock(&server->srv_lock); 96238c8a9a5SSteve French 96338c8a9a5SSteve French cancel_delayed_work_sync(&server->echo); 96438c8a9a5SSteve French 96538c8a9a5SSteve French spin_lock(&server->srv_lock); 96638c8a9a5SSteve French server->tcpStatus = CifsExiting; 96738c8a9a5SSteve French spin_unlock(&server->srv_lock); 96838c8a9a5SSteve French wake_up_all(&server->response_q); 96938c8a9a5SSteve French 97038c8a9a5SSteve French /* check if we have blocked requests that need to free */ 97138c8a9a5SSteve French spin_lock(&server->req_lock); 97238c8a9a5SSteve French if (server->credits <= 0) 97338c8a9a5SSteve French server->credits = 1; 97438c8a9a5SSteve French spin_unlock(&server->req_lock); 97538c8a9a5SSteve French /* 97638c8a9a5SSteve French * Although there should not be any requests blocked on this queue it 97738c8a9a5SSteve French * can not hurt to be paranoid and try to wake up requests that may 97838c8a9a5SSteve French * haven been blocked when more than 50 at time were on the wire to the 97938c8a9a5SSteve French * same server - they now will see the session is in exit state and get 98038c8a9a5SSteve French * out of SendReceive. 98138c8a9a5SSteve French */ 98238c8a9a5SSteve French wake_up_all(&server->request_q); 98338c8a9a5SSteve French /* give those requests time to exit */ 98438c8a9a5SSteve French msleep(125); 98538c8a9a5SSteve French if (cifs_rdma_enabled(server)) 98638c8a9a5SSteve French smbd_destroy(server); 98738c8a9a5SSteve French if (server->ssocket) { 98838c8a9a5SSteve French sock_release(server->ssocket); 98938c8a9a5SSteve French server->ssocket = NULL; 99038c8a9a5SSteve French } 99138c8a9a5SSteve French 99238c8a9a5SSteve French if (!list_empty(&server->pending_mid_q)) { 99338c8a9a5SSteve French struct list_head dispose_list; 99438c8a9a5SSteve French struct mid_q_entry *mid_entry; 99538c8a9a5SSteve French struct list_head *tmp, *tmp2; 99638c8a9a5SSteve French 99738c8a9a5SSteve French INIT_LIST_HEAD(&dispose_list); 99838c8a9a5SSteve French spin_lock(&server->mid_lock); 99938c8a9a5SSteve French list_for_each_safe(tmp, tmp2, &server->pending_mid_q) { 100038c8a9a5SSteve French mid_entry = list_entry(tmp, struct mid_q_entry, qhead); 100138c8a9a5SSteve French cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid); 100238c8a9a5SSteve French kref_get(&mid_entry->refcount); 100338c8a9a5SSteve French mid_entry->mid_state = MID_SHUTDOWN; 100438c8a9a5SSteve French list_move(&mid_entry->qhead, &dispose_list); 100538c8a9a5SSteve French mid_entry->mid_flags |= MID_DELETED; 100638c8a9a5SSteve French } 100738c8a9a5SSteve French spin_unlock(&server->mid_lock); 100838c8a9a5SSteve French 100938c8a9a5SSteve French /* now walk dispose list and issue callbacks */ 101038c8a9a5SSteve French list_for_each_safe(tmp, tmp2, &dispose_list) { 101138c8a9a5SSteve French mid_entry = list_entry(tmp, struct mid_q_entry, qhead); 101238c8a9a5SSteve French cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid); 101338c8a9a5SSteve French list_del_init(&mid_entry->qhead); 101438c8a9a5SSteve French mid_entry->callback(mid_entry); 101538c8a9a5SSteve French release_mid(mid_entry); 101638c8a9a5SSteve French } 101738c8a9a5SSteve French /* 1/8th of sec is more than enough time for them to exit */ 101838c8a9a5SSteve French msleep(125); 101938c8a9a5SSteve French } 102038c8a9a5SSteve French 102138c8a9a5SSteve French if (!list_empty(&server->pending_mid_q)) { 102238c8a9a5SSteve French /* 102338c8a9a5SSteve French * mpx threads have not exited yet give them at least the smb 102438c8a9a5SSteve French * send timeout time for long ops. 102538c8a9a5SSteve French * 102638c8a9a5SSteve French * Due to delays on oplock break requests, we need to wait at 102738c8a9a5SSteve French * least 45 seconds before giving up on a request getting a 102838c8a9a5SSteve French * response and going ahead and killing cifsd. 102938c8a9a5SSteve French */ 103038c8a9a5SSteve French cifs_dbg(FYI, "Wait for exit from demultiplex thread\n"); 103138c8a9a5SSteve French msleep(46000); 103238c8a9a5SSteve French /* 103338c8a9a5SSteve French * If threads still have not exited they are probably never 103438c8a9a5SSteve French * coming home not much else we can do but free the memory. 103538c8a9a5SSteve French */ 103638c8a9a5SSteve French } 103738c8a9a5SSteve French 103838c8a9a5SSteve French kfree(server->leaf_fullpath); 103938c8a9a5SSteve French kfree(server); 104038c8a9a5SSteve French 104138c8a9a5SSteve French length = atomic_dec_return(&tcpSesAllocCount); 104238c8a9a5SSteve French if (length > 0) 104338c8a9a5SSteve French mempool_resize(cifs_req_poolp, length + cifs_min_rcv); 104438c8a9a5SSteve French } 104538c8a9a5SSteve French 104638c8a9a5SSteve French static int 104738c8a9a5SSteve French standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid) 104838c8a9a5SSteve French { 104938c8a9a5SSteve French int length; 105038c8a9a5SSteve French char *buf = server->smallbuf; 105138c8a9a5SSteve French unsigned int pdu_length = server->pdu_size; 105238c8a9a5SSteve French 105338c8a9a5SSteve French /* make sure this will fit in a large buffer */ 105438c8a9a5SSteve French if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 105538c8a9a5SSteve French HEADER_PREAMBLE_SIZE(server)) { 105638c8a9a5SSteve French cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length); 105738c8a9a5SSteve French cifs_reconnect(server, true); 105838c8a9a5SSteve French return -ECONNABORTED; 105938c8a9a5SSteve French } 106038c8a9a5SSteve French 106138c8a9a5SSteve French /* switch to large buffer if too big for a small one */ 106238c8a9a5SSteve French if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) { 106338c8a9a5SSteve French server->large_buf = true; 106438c8a9a5SSteve French memcpy(server->bigbuf, buf, server->total_read); 106538c8a9a5SSteve French buf = server->bigbuf; 106638c8a9a5SSteve French } 106738c8a9a5SSteve French 106838c8a9a5SSteve French /* now read the rest */ 106938c8a9a5SSteve French length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, 107038c8a9a5SSteve French pdu_length - MID_HEADER_SIZE(server)); 107138c8a9a5SSteve French 107238c8a9a5SSteve French if (length < 0) 107338c8a9a5SSteve French return length; 107438c8a9a5SSteve French server->total_read += length; 107538c8a9a5SSteve French 107638c8a9a5SSteve French dump_smb(buf, server->total_read); 107738c8a9a5SSteve French 107838c8a9a5SSteve French return cifs_handle_standard(server, mid); 107938c8a9a5SSteve French } 108038c8a9a5SSteve French 108138c8a9a5SSteve French int 108238c8a9a5SSteve French cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid) 108338c8a9a5SSteve French { 108438c8a9a5SSteve French char *buf = server->large_buf ? server->bigbuf : server->smallbuf; 108538c8a9a5SSteve French int rc; 108638c8a9a5SSteve French 108738c8a9a5SSteve French /* 108838c8a9a5SSteve French * We know that we received enough to get to the MID as we 108938c8a9a5SSteve French * checked the pdu_length earlier. Now check to see 109038c8a9a5SSteve French * if the rest of the header is OK. 109138c8a9a5SSteve French * 109238c8a9a5SSteve French * 48 bytes is enough to display the header and a little bit 109338c8a9a5SSteve French * into the payload for debugging purposes. 109438c8a9a5SSteve French */ 109538c8a9a5SSteve French rc = server->ops->check_message(buf, server->total_read, server); 109638c8a9a5SSteve French if (rc) 109738c8a9a5SSteve French cifs_dump_mem("Bad SMB: ", buf, 109838c8a9a5SSteve French min_t(unsigned int, server->total_read, 48)); 109938c8a9a5SSteve French 110038c8a9a5SSteve French if (server->ops->is_session_expired && 110138c8a9a5SSteve French server->ops->is_session_expired(buf)) { 110238c8a9a5SSteve French cifs_reconnect(server, true); 110338c8a9a5SSteve French return -1; 110438c8a9a5SSteve French } 110538c8a9a5SSteve French 110638c8a9a5SSteve French if (server->ops->is_status_pending && 110738c8a9a5SSteve French server->ops->is_status_pending(buf, server)) 110838c8a9a5SSteve French return -1; 110938c8a9a5SSteve French 111038c8a9a5SSteve French if (!mid) 111138c8a9a5SSteve French return rc; 111238c8a9a5SSteve French 111338c8a9a5SSteve French handle_mid(mid, server, buf, rc); 111438c8a9a5SSteve French return 0; 111538c8a9a5SSteve French } 111638c8a9a5SSteve French 111738c8a9a5SSteve French static void 111838c8a9a5SSteve French smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) 111938c8a9a5SSteve French { 112038c8a9a5SSteve French struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; 112138c8a9a5SSteve French int scredits, in_flight; 112238c8a9a5SSteve French 112338c8a9a5SSteve French /* 112438c8a9a5SSteve French * SMB1 does not use credits. 112538c8a9a5SSteve French */ 112638c8a9a5SSteve French if (is_smb1(server)) 112738c8a9a5SSteve French return; 112838c8a9a5SSteve French 112938c8a9a5SSteve French if (shdr->CreditRequest) { 113038c8a9a5SSteve French spin_lock(&server->req_lock); 113138c8a9a5SSteve French server->credits += le16_to_cpu(shdr->CreditRequest); 113238c8a9a5SSteve French scredits = server->credits; 113338c8a9a5SSteve French in_flight = server->in_flight; 113438c8a9a5SSteve French spin_unlock(&server->req_lock); 113538c8a9a5SSteve French wake_up(&server->request_q); 113638c8a9a5SSteve French 113738c8a9a5SSteve French trace_smb3_hdr_credits(server->CurrentMid, 113838c8a9a5SSteve French server->conn_id, server->hostname, scredits, 113938c8a9a5SSteve French le16_to_cpu(shdr->CreditRequest), in_flight); 114038c8a9a5SSteve French cifs_server_dbg(FYI, "%s: added %u credits total=%d\n", 114138c8a9a5SSteve French __func__, le16_to_cpu(shdr->CreditRequest), 114238c8a9a5SSteve French scredits); 114338c8a9a5SSteve French } 114438c8a9a5SSteve French } 114538c8a9a5SSteve French 114638c8a9a5SSteve French 114738c8a9a5SSteve French static int 114838c8a9a5SSteve French cifs_demultiplex_thread(void *p) 114938c8a9a5SSteve French { 115038c8a9a5SSteve French int i, num_mids, length; 115138c8a9a5SSteve French struct TCP_Server_Info *server = p; 115238c8a9a5SSteve French unsigned int pdu_length; 115338c8a9a5SSteve French unsigned int next_offset; 115438c8a9a5SSteve French char *buf = NULL; 115538c8a9a5SSteve French struct task_struct *task_to_wake = NULL; 115638c8a9a5SSteve French struct mid_q_entry *mids[MAX_COMPOUND]; 115738c8a9a5SSteve French char *bufs[MAX_COMPOUND]; 115838c8a9a5SSteve French unsigned int noreclaim_flag, num_io_timeout = 0; 115969cba9d3SShyam Prasad N bool pending_reconnect = false; 116038c8a9a5SSteve French 116138c8a9a5SSteve French noreclaim_flag = memalloc_noreclaim_save(); 116238c8a9a5SSteve French cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current)); 116338c8a9a5SSteve French 116438c8a9a5SSteve French length = atomic_inc_return(&tcpSesAllocCount); 116538c8a9a5SSteve French if (length > 1) 116638c8a9a5SSteve French mempool_resize(cifs_req_poolp, length + cifs_min_rcv); 116738c8a9a5SSteve French 116838c8a9a5SSteve French set_freezable(); 116938c8a9a5SSteve French allow_kernel_signal(SIGKILL); 117038c8a9a5SSteve French while (server->tcpStatus != CifsExiting) { 117138c8a9a5SSteve French if (try_to_freeze()) 117238c8a9a5SSteve French continue; 117338c8a9a5SSteve French 117438c8a9a5SSteve French if (!allocate_buffers(server)) 117538c8a9a5SSteve French continue; 117638c8a9a5SSteve French 117738c8a9a5SSteve French server->large_buf = false; 117838c8a9a5SSteve French buf = server->smallbuf; 117938c8a9a5SSteve French pdu_length = 4; /* enough to get RFC1001 header */ 118038c8a9a5SSteve French 118138c8a9a5SSteve French length = cifs_read_from_socket(server, buf, pdu_length); 118238c8a9a5SSteve French if (length < 0) 118338c8a9a5SSteve French continue; 118438c8a9a5SSteve French 118538c8a9a5SSteve French if (is_smb1(server)) 118638c8a9a5SSteve French server->total_read = length; 118738c8a9a5SSteve French else 118838c8a9a5SSteve French server->total_read = 0; 118938c8a9a5SSteve French 119038c8a9a5SSteve French /* 119138c8a9a5SSteve French * The right amount was read from socket - 4 bytes, 119238c8a9a5SSteve French * so we can now interpret the length field. 119338c8a9a5SSteve French */ 119438c8a9a5SSteve French pdu_length = get_rfc1002_length(buf); 119538c8a9a5SSteve French 119638c8a9a5SSteve French cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length); 119738c8a9a5SSteve French if (!is_smb_response(server, buf[0])) 119838c8a9a5SSteve French continue; 119969cba9d3SShyam Prasad N 120069cba9d3SShyam Prasad N pending_reconnect = false; 120138c8a9a5SSteve French next_pdu: 120238c8a9a5SSteve French server->pdu_size = pdu_length; 120338c8a9a5SSteve French 120438c8a9a5SSteve French /* make sure we have enough to get to the MID */ 120538c8a9a5SSteve French if (server->pdu_size < MID_HEADER_SIZE(server)) { 120638c8a9a5SSteve French cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n", 120738c8a9a5SSteve French server->pdu_size); 120838c8a9a5SSteve French cifs_reconnect(server, true); 120938c8a9a5SSteve French continue; 121038c8a9a5SSteve French } 121138c8a9a5SSteve French 121238c8a9a5SSteve French /* read down to the MID */ 121338c8a9a5SSteve French length = cifs_read_from_socket(server, 121438c8a9a5SSteve French buf + HEADER_PREAMBLE_SIZE(server), 121538c8a9a5SSteve French MID_HEADER_SIZE(server)); 121638c8a9a5SSteve French if (length < 0) 121738c8a9a5SSteve French continue; 121838c8a9a5SSteve French server->total_read += length; 121938c8a9a5SSteve French 122038c8a9a5SSteve French if (server->ops->next_header) { 1221c0e98de9SPaulo Alcantara if (server->ops->next_header(server, buf, &next_offset)) { 1222c0e98de9SPaulo Alcantara cifs_dbg(VFS, "%s: malformed response (next_offset=%u)\n", 1223c0e98de9SPaulo Alcantara __func__, next_offset); 1224c0e98de9SPaulo Alcantara cifs_reconnect(server, true); 1225c0e98de9SPaulo Alcantara continue; 1226c0e98de9SPaulo Alcantara } 122738c8a9a5SSteve French if (next_offset) 122838c8a9a5SSteve French server->pdu_size = next_offset; 122938c8a9a5SSteve French } 123038c8a9a5SSteve French 123138c8a9a5SSteve French memset(mids, 0, sizeof(mids)); 123238c8a9a5SSteve French memset(bufs, 0, sizeof(bufs)); 123338c8a9a5SSteve French num_mids = 0; 123438c8a9a5SSteve French 123538c8a9a5SSteve French if (server->ops->is_transform_hdr && 123638c8a9a5SSteve French server->ops->receive_transform && 123738c8a9a5SSteve French server->ops->is_transform_hdr(buf)) { 123838c8a9a5SSteve French length = server->ops->receive_transform(server, 123938c8a9a5SSteve French mids, 124038c8a9a5SSteve French bufs, 124138c8a9a5SSteve French &num_mids); 124238c8a9a5SSteve French } else { 124338c8a9a5SSteve French mids[0] = server->ops->find_mid(server, buf); 124438c8a9a5SSteve French bufs[0] = buf; 124538c8a9a5SSteve French num_mids = 1; 124638c8a9a5SSteve French 124738c8a9a5SSteve French if (!mids[0] || !mids[0]->receive) 124838c8a9a5SSteve French length = standard_receive3(server, mids[0]); 124938c8a9a5SSteve French else 125038c8a9a5SSteve French length = mids[0]->receive(server, mids[0]); 125138c8a9a5SSteve French } 125238c8a9a5SSteve French 125338c8a9a5SSteve French if (length < 0) { 125438c8a9a5SSteve French for (i = 0; i < num_mids; i++) 125538c8a9a5SSteve French if (mids[i]) 125638c8a9a5SSteve French release_mid(mids[i]); 125738c8a9a5SSteve French continue; 125838c8a9a5SSteve French } 125938c8a9a5SSteve French 126038c8a9a5SSteve French if (server->ops->is_status_io_timeout && 126138c8a9a5SSteve French server->ops->is_status_io_timeout(buf)) { 126238c8a9a5SSteve French num_io_timeout++; 126369cba9d3SShyam Prasad N if (num_io_timeout > MAX_STATUS_IO_TIMEOUT) { 126469cba9d3SShyam Prasad N cifs_server_dbg(VFS, 126569cba9d3SShyam Prasad N "Number of request timeouts exceeded %d. Reconnecting", 126669cba9d3SShyam Prasad N MAX_STATUS_IO_TIMEOUT); 126769cba9d3SShyam Prasad N 126869cba9d3SShyam Prasad N pending_reconnect = true; 126938c8a9a5SSteve French num_io_timeout = 0; 127038c8a9a5SSteve French } 127138c8a9a5SSteve French } 127238c8a9a5SSteve French 127338c8a9a5SSteve French server->lstrp = jiffies; 127438c8a9a5SSteve French 127538c8a9a5SSteve French for (i = 0; i < num_mids; i++) { 127638c8a9a5SSteve French if (mids[i] != NULL) { 127738c8a9a5SSteve French mids[i]->resp_buf_size = server->pdu_size; 127838c8a9a5SSteve French 1279c071b34fSShyam Prasad N if (bufs[i] != NULL) { 1280c071b34fSShyam Prasad N if (server->ops->is_network_name_deleted && 128138c8a9a5SSteve French server->ops->is_network_name_deleted(bufs[i], 1282c071b34fSShyam Prasad N server)) { 1283c071b34fSShyam Prasad N cifs_server_dbg(FYI, 1284c071b34fSShyam Prasad N "Share deleted. Reconnect needed"); 1285c071b34fSShyam Prasad N } 1286c071b34fSShyam Prasad N } 128738c8a9a5SSteve French 128838c8a9a5SSteve French if (!mids[i]->multiRsp || mids[i]->multiEnd) 128938c8a9a5SSteve French mids[i]->callback(mids[i]); 129038c8a9a5SSteve French 129138c8a9a5SSteve French release_mid(mids[i]); 129238c8a9a5SSteve French } else if (server->ops->is_oplock_break && 129338c8a9a5SSteve French server->ops->is_oplock_break(bufs[i], 129438c8a9a5SSteve French server)) { 129538c8a9a5SSteve French smb2_add_credits_from_hdr(bufs[i], server); 129638c8a9a5SSteve French cifs_dbg(FYI, "Received oplock break\n"); 129738c8a9a5SSteve French } else { 129838c8a9a5SSteve French cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n", 129938c8a9a5SSteve French atomic_read(&mid_count)); 130038c8a9a5SSteve French cifs_dump_mem("Received Data is: ", bufs[i], 130138c8a9a5SSteve French HEADER_SIZE(server)); 130238c8a9a5SSteve French smb2_add_credits_from_hdr(bufs[i], server); 130338c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2 130438c8a9a5SSteve French if (server->ops->dump_detail) 130538c8a9a5SSteve French server->ops->dump_detail(bufs[i], 130638c8a9a5SSteve French server); 130738c8a9a5SSteve French cifs_dump_mids(server); 130838c8a9a5SSteve French #endif /* CIFS_DEBUG2 */ 130938c8a9a5SSteve French } 131038c8a9a5SSteve French } 131138c8a9a5SSteve French 131238c8a9a5SSteve French if (pdu_length > server->pdu_size) { 131338c8a9a5SSteve French if (!allocate_buffers(server)) 131438c8a9a5SSteve French continue; 131538c8a9a5SSteve French pdu_length -= server->pdu_size; 131638c8a9a5SSteve French server->total_read = 0; 131738c8a9a5SSteve French server->large_buf = false; 131838c8a9a5SSteve French buf = server->smallbuf; 131938c8a9a5SSteve French goto next_pdu; 132038c8a9a5SSteve French } 132169cba9d3SShyam Prasad N 132269cba9d3SShyam Prasad N /* do this reconnect at the very end after processing all MIDs */ 132369cba9d3SShyam Prasad N if (pending_reconnect) 132469cba9d3SShyam Prasad N cifs_reconnect(server, true); 132569cba9d3SShyam Prasad N 132638c8a9a5SSteve French } /* end while !EXITING */ 132738c8a9a5SSteve French 132838c8a9a5SSteve French /* buffer usually freed in free_mid - need to free it here on exit */ 132938c8a9a5SSteve French cifs_buf_release(server->bigbuf); 133038c8a9a5SSteve French if (server->smallbuf) /* no sense logging a debug message if NULL */ 133138c8a9a5SSteve French cifs_small_buf_release(server->smallbuf); 133238c8a9a5SSteve French 133338c8a9a5SSteve French task_to_wake = xchg(&server->tsk, NULL); 133438c8a9a5SSteve French clean_demultiplex_info(server); 133538c8a9a5SSteve French 133638c8a9a5SSteve French /* if server->tsk was NULL then wait for a signal before exiting */ 133738c8a9a5SSteve French if (!task_to_wake) { 133838c8a9a5SSteve French set_current_state(TASK_INTERRUPTIBLE); 133938c8a9a5SSteve French while (!signal_pending(current)) { 134038c8a9a5SSteve French schedule(); 134138c8a9a5SSteve French set_current_state(TASK_INTERRUPTIBLE); 134238c8a9a5SSteve French } 134338c8a9a5SSteve French set_current_state(TASK_RUNNING); 134438c8a9a5SSteve French } 134538c8a9a5SSteve French 134638c8a9a5SSteve French memalloc_noreclaim_restore(noreclaim_flag); 134738c8a9a5SSteve French module_put_and_kthread_exit(0); 134838c8a9a5SSteve French } 134938c8a9a5SSteve French 13502991b774SShyam Prasad N int 13512991b774SShyam Prasad N cifs_ipaddr_cmp(struct sockaddr *srcaddr, struct sockaddr *rhs) 13522991b774SShyam Prasad N { 13532991b774SShyam Prasad N struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; 13542991b774SShyam Prasad N struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; 13552991b774SShyam Prasad N struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; 13562991b774SShyam Prasad N struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; 13572991b774SShyam Prasad N 13582991b774SShyam Prasad N switch (srcaddr->sa_family) { 13592991b774SShyam Prasad N case AF_UNSPEC: 13602991b774SShyam Prasad N switch (rhs->sa_family) { 13612991b774SShyam Prasad N case AF_UNSPEC: 13622991b774SShyam Prasad N return 0; 13632991b774SShyam Prasad N case AF_INET: 13642991b774SShyam Prasad N case AF_INET6: 13652991b774SShyam Prasad N return 1; 13662991b774SShyam Prasad N default: 13672991b774SShyam Prasad N return -1; 13682991b774SShyam Prasad N } 13692991b774SShyam Prasad N case AF_INET: { 13702991b774SShyam Prasad N switch (rhs->sa_family) { 13712991b774SShyam Prasad N case AF_UNSPEC: 13722991b774SShyam Prasad N return -1; 13732991b774SShyam Prasad N case AF_INET: 13742991b774SShyam Prasad N return memcmp(saddr4, vaddr4, 13752991b774SShyam Prasad N sizeof(struct sockaddr_in)); 13762991b774SShyam Prasad N case AF_INET6: 13772991b774SShyam Prasad N return 1; 13782991b774SShyam Prasad N default: 13792991b774SShyam Prasad N return -1; 13802991b774SShyam Prasad N } 13812991b774SShyam Prasad N } 13822991b774SShyam Prasad N case AF_INET6: { 13832991b774SShyam Prasad N switch (rhs->sa_family) { 13842991b774SShyam Prasad N case AF_UNSPEC: 13852991b774SShyam Prasad N case AF_INET: 13862991b774SShyam Prasad N return -1; 13872991b774SShyam Prasad N case AF_INET6: 13882991b774SShyam Prasad N return memcmp(saddr6, 13892991b774SShyam Prasad N vaddr6, 13902991b774SShyam Prasad N sizeof(struct sockaddr_in6)); 13912991b774SShyam Prasad N default: 13922991b774SShyam Prasad N return -1; 13932991b774SShyam Prasad N } 13942991b774SShyam Prasad N } 13952991b774SShyam Prasad N default: 13962991b774SShyam Prasad N return -1; /* don't expect to be here */ 13972991b774SShyam Prasad N } 13982991b774SShyam Prasad N } 13992991b774SShyam Prasad N 140038c8a9a5SSteve French /* 140138c8a9a5SSteve French * Returns true if srcaddr isn't specified and rhs isn't specified, or 140238c8a9a5SSteve French * if srcaddr is specified and matches the IP address of the rhs argument 140338c8a9a5SSteve French */ 140438c8a9a5SSteve French bool 140538c8a9a5SSteve French cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs) 140638c8a9a5SSteve French { 140738c8a9a5SSteve French switch (srcaddr->sa_family) { 140838c8a9a5SSteve French case AF_UNSPEC: 140938c8a9a5SSteve French return (rhs->sa_family == AF_UNSPEC); 141038c8a9a5SSteve French case AF_INET: { 141138c8a9a5SSteve French struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; 141238c8a9a5SSteve French struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; 141338c8a9a5SSteve French return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr); 141438c8a9a5SSteve French } 141538c8a9a5SSteve French case AF_INET6: { 141638c8a9a5SSteve French struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; 141738c8a9a5SSteve French struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; 141838c8a9a5SSteve French return (ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr) 141938c8a9a5SSteve French && saddr6->sin6_scope_id == vaddr6->sin6_scope_id); 142038c8a9a5SSteve French } 142138c8a9a5SSteve French default: 142238c8a9a5SSteve French WARN_ON(1); 142338c8a9a5SSteve French return false; /* don't expect to be here */ 142438c8a9a5SSteve French } 142538c8a9a5SSteve French } 142638c8a9a5SSteve French 142738c8a9a5SSteve French /* 142838c8a9a5SSteve French * If no port is specified in addr structure, we try to match with 445 port 142938c8a9a5SSteve French * and if it fails - with 139 ports. It should be called only if address 143038c8a9a5SSteve French * families of server and addr are equal. 143138c8a9a5SSteve French */ 143238c8a9a5SSteve French static bool 143338c8a9a5SSteve French match_port(struct TCP_Server_Info *server, struct sockaddr *addr) 143438c8a9a5SSteve French { 143538c8a9a5SSteve French __be16 port, *sport; 143638c8a9a5SSteve French 143738c8a9a5SSteve French /* SMBDirect manages its own ports, don't match it here */ 143838c8a9a5SSteve French if (server->rdma) 143938c8a9a5SSteve French return true; 144038c8a9a5SSteve French 144138c8a9a5SSteve French switch (addr->sa_family) { 144238c8a9a5SSteve French case AF_INET: 144338c8a9a5SSteve French sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port; 144438c8a9a5SSteve French port = ((struct sockaddr_in *) addr)->sin_port; 144538c8a9a5SSteve French break; 144638c8a9a5SSteve French case AF_INET6: 144738c8a9a5SSteve French sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port; 144838c8a9a5SSteve French port = ((struct sockaddr_in6 *) addr)->sin6_port; 144938c8a9a5SSteve French break; 145038c8a9a5SSteve French default: 145138c8a9a5SSteve French WARN_ON(1); 145238c8a9a5SSteve French return false; 145338c8a9a5SSteve French } 145438c8a9a5SSteve French 145538c8a9a5SSteve French if (!port) { 145638c8a9a5SSteve French port = htons(CIFS_PORT); 145738c8a9a5SSteve French if (port == *sport) 145838c8a9a5SSteve French return true; 145938c8a9a5SSteve French 146038c8a9a5SSteve French port = htons(RFC1001_PORT); 146138c8a9a5SSteve French } 146238c8a9a5SSteve French 146338c8a9a5SSteve French return port == *sport; 146438c8a9a5SSteve French } 146538c8a9a5SSteve French 146638c8a9a5SSteve French static bool match_server_address(struct TCP_Server_Info *server, struct sockaddr *addr) 146738c8a9a5SSteve French { 146838c8a9a5SSteve French if (!cifs_match_ipaddr(addr, (struct sockaddr *)&server->dstaddr)) 146938c8a9a5SSteve French return false; 147038c8a9a5SSteve French 147138c8a9a5SSteve French return true; 147238c8a9a5SSteve French } 147338c8a9a5SSteve French 147438c8a9a5SSteve French static bool 147538c8a9a5SSteve French match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) 147638c8a9a5SSteve French { 147738c8a9a5SSteve French /* 147838c8a9a5SSteve French * The select_sectype function should either return the ctx->sectype 147938c8a9a5SSteve French * that was specified, or "Unspecified" if that sectype was not 148038c8a9a5SSteve French * compatible with the given NEGOTIATE request. 148138c8a9a5SSteve French */ 148238c8a9a5SSteve French if (server->ops->select_sectype(server, ctx->sectype) 148338c8a9a5SSteve French == Unspecified) 148438c8a9a5SSteve French return false; 148538c8a9a5SSteve French 148638c8a9a5SSteve French /* 148738c8a9a5SSteve French * Now check if signing mode is acceptable. No need to check 148838c8a9a5SSteve French * global_secflags at this point since if MUST_SIGN is set then 148938c8a9a5SSteve French * the server->sign had better be too. 149038c8a9a5SSteve French */ 149138c8a9a5SSteve French if (ctx->sign && !server->sign) 149238c8a9a5SSteve French return false; 149338c8a9a5SSteve French 149438c8a9a5SSteve French return true; 149538c8a9a5SSteve French } 149638c8a9a5SSteve French 149738c8a9a5SSteve French /* this function must be called with srv_lock held */ 14983ae872deSPaulo Alcantara static int match_server(struct TCP_Server_Info *server, 14993ae872deSPaulo Alcantara struct smb3_fs_context *ctx, 15003ae872deSPaulo Alcantara bool match_super) 150138c8a9a5SSteve French { 150238c8a9a5SSteve French struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr; 150338c8a9a5SSteve French 150438c8a9a5SSteve French lockdep_assert_held(&server->srv_lock); 150538c8a9a5SSteve French 150638c8a9a5SSteve French if (ctx->nosharesock) 150738c8a9a5SSteve French return 0; 150838c8a9a5SSteve French 150938c8a9a5SSteve French /* this server does not share socket */ 151038c8a9a5SSteve French if (server->nosharesock) 151138c8a9a5SSteve French return 0; 151238c8a9a5SSteve French 151338c8a9a5SSteve French /* If multidialect negotiation see if existing sessions match one */ 151438c8a9a5SSteve French if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) { 151538c8a9a5SSteve French if (server->vals->protocol_id < SMB30_PROT_ID) 151638c8a9a5SSteve French return 0; 151738c8a9a5SSteve French } else if (strcmp(ctx->vals->version_string, 151838c8a9a5SSteve French SMBDEFAULT_VERSION_STRING) == 0) { 151938c8a9a5SSteve French if (server->vals->protocol_id < SMB21_PROT_ID) 152038c8a9a5SSteve French return 0; 152138c8a9a5SSteve French } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops)) 152238c8a9a5SSteve French return 0; 152338c8a9a5SSteve French 152438c8a9a5SSteve French if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns)) 152538c8a9a5SSteve French return 0; 152638c8a9a5SSteve French 152738c8a9a5SSteve French if (!cifs_match_ipaddr((struct sockaddr *)&ctx->srcaddr, 152838c8a9a5SSteve French (struct sockaddr *)&server->srcaddr)) 152938c8a9a5SSteve French return 0; 153038c8a9a5SSteve French /* 15313ae872deSPaulo Alcantara * When matching cifs.ko superblocks (@match_super == true), we can't 15323ae872deSPaulo Alcantara * really match either @server->leaf_fullpath or @server->dstaddr 15333ae872deSPaulo Alcantara * directly since this @server might belong to a completely different 15343ae872deSPaulo Alcantara * server -- in case of domain-based DFS referrals or DFS links -- as 15353ae872deSPaulo Alcantara * provided earlier by mount(2) through 'source' and 'ip' options. 15363ae872deSPaulo Alcantara * 15373ae872deSPaulo Alcantara * Otherwise, match the DFS referral in @server->leaf_fullpath or the 15383ae872deSPaulo Alcantara * destination address in @server->dstaddr. 15393ae872deSPaulo Alcantara * 15403ae872deSPaulo Alcantara * When using 'nodfs' mount option, we avoid sharing it with DFS 15413ae872deSPaulo Alcantara * connections as they might failover. 154238c8a9a5SSteve French */ 15433ae872deSPaulo Alcantara if (!match_super) { 154438c8a9a5SSteve French if (!ctx->nodfs) { 15453ae872deSPaulo Alcantara if (server->leaf_fullpath) { 154638c8a9a5SSteve French if (!ctx->leaf_fullpath || 154738c8a9a5SSteve French strcasecmp(server->leaf_fullpath, 154838c8a9a5SSteve French ctx->leaf_fullpath)) 154938c8a9a5SSteve French return 0; 155038c8a9a5SSteve French } else if (ctx->leaf_fullpath) { 155138c8a9a5SSteve French return 0; 155238c8a9a5SSteve French } 15533ae872deSPaulo Alcantara } else if (server->leaf_fullpath) { 155438c8a9a5SSteve French return 0; 155538c8a9a5SSteve French } 15563ae872deSPaulo Alcantara } 155738c8a9a5SSteve French 155838c8a9a5SSteve French /* 155938c8a9a5SSteve French * Match for a regular connection (address/hostname/port) which has no 156038c8a9a5SSteve French * DFS referrals set. 156138c8a9a5SSteve French */ 15623ae872deSPaulo Alcantara if (!server->leaf_fullpath && 156338c8a9a5SSteve French (strcasecmp(server->hostname, ctx->server_hostname) || 156438c8a9a5SSteve French !match_server_address(server, addr) || 156538c8a9a5SSteve French !match_port(server, addr))) 156638c8a9a5SSteve French return 0; 156738c8a9a5SSteve French 156838c8a9a5SSteve French if (!match_security(server, ctx)) 156938c8a9a5SSteve French return 0; 157038c8a9a5SSteve French 157138c8a9a5SSteve French if (server->echo_interval != ctx->echo_interval * HZ) 157238c8a9a5SSteve French return 0; 157338c8a9a5SSteve French 157438c8a9a5SSteve French if (server->rdma != ctx->rdma) 157538c8a9a5SSteve French return 0; 157638c8a9a5SSteve French 157738c8a9a5SSteve French if (server->ignore_signature != ctx->ignore_signature) 157838c8a9a5SSteve French return 0; 157938c8a9a5SSteve French 158038c8a9a5SSteve French if (server->min_offload != ctx->min_offload) 158138c8a9a5SSteve French return 0; 158238c8a9a5SSteve French 158338c8a9a5SSteve French return 1; 158438c8a9a5SSteve French } 158538c8a9a5SSteve French 158638c8a9a5SSteve French struct TCP_Server_Info * 158738c8a9a5SSteve French cifs_find_tcp_session(struct smb3_fs_context *ctx) 158838c8a9a5SSteve French { 158938c8a9a5SSteve French struct TCP_Server_Info *server; 159038c8a9a5SSteve French 159138c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 159238c8a9a5SSteve French list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) { 159338c8a9a5SSteve French spin_lock(&server->srv_lock); 159438c8a9a5SSteve French /* 159538c8a9a5SSteve French * Skip ses channels since they're only handled in lower layers 159638c8a9a5SSteve French * (e.g. cifs_send_recv). 159738c8a9a5SSteve French */ 1598b3773b19SSteve French if (SERVER_IS_CHAN(server) || 15993ae872deSPaulo Alcantara !match_server(server, ctx, false)) { 160038c8a9a5SSteve French spin_unlock(&server->srv_lock); 160138c8a9a5SSteve French continue; 160238c8a9a5SSteve French } 160338c8a9a5SSteve French spin_unlock(&server->srv_lock); 160438c8a9a5SSteve French 160538c8a9a5SSteve French ++server->srv_count; 160638c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 160738c8a9a5SSteve French cifs_dbg(FYI, "Existing tcp session with server found\n"); 160838c8a9a5SSteve French return server; 160938c8a9a5SSteve French } 161038c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 161138c8a9a5SSteve French return NULL; 161238c8a9a5SSteve French } 161338c8a9a5SSteve French 161438c8a9a5SSteve French void 161538c8a9a5SSteve French cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect) 161638c8a9a5SSteve French { 161738c8a9a5SSteve French struct task_struct *task; 161838c8a9a5SSteve French 161938c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 162038c8a9a5SSteve French if (--server->srv_count > 0) { 162138c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 162238c8a9a5SSteve French return; 162338c8a9a5SSteve French } 162438c8a9a5SSteve French 162538c8a9a5SSteve French /* srv_count can never go negative */ 162638c8a9a5SSteve French WARN_ON(server->srv_count < 0); 162738c8a9a5SSteve French 162838c8a9a5SSteve French put_net(cifs_net_ns(server)); 162938c8a9a5SSteve French 163038c8a9a5SSteve French list_del_init(&server->tcp_ses_list); 163138c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 163238c8a9a5SSteve French 163338c8a9a5SSteve French cancel_delayed_work_sync(&server->echo); 163438c8a9a5SSteve French 16353a0ef868SShyam Prasad N if (from_reconnect) 163638c8a9a5SSteve French /* 163738c8a9a5SSteve French * Avoid deadlock here: reconnect work calls 163838c8a9a5SSteve French * cifs_put_tcp_session() at its end. Need to be sure 163938c8a9a5SSteve French * that reconnect work does nothing with server pointer after 164038c8a9a5SSteve French * that step. 164138c8a9a5SSteve French */ 16423a0ef868SShyam Prasad N cancel_delayed_work(&server->reconnect); 16433a0ef868SShyam Prasad N else 16443a0ef868SShyam Prasad N cancel_delayed_work_sync(&server->reconnect); 164538c8a9a5SSteve French 164607e86408SShyam Prasad N /* For secondary channels, we pick up ref-count on the primary server */ 164707e86408SShyam Prasad N if (SERVER_IS_CHAN(server)) 164807e86408SShyam Prasad N cifs_put_tcp_session(server->primary_server, from_reconnect); 164907e86408SShyam Prasad N 165038c8a9a5SSteve French spin_lock(&server->srv_lock); 165138c8a9a5SSteve French server->tcpStatus = CifsExiting; 165238c8a9a5SSteve French spin_unlock(&server->srv_lock); 165338c8a9a5SSteve French 165438c8a9a5SSteve French cifs_crypto_secmech_release(server); 165538c8a9a5SSteve French 165638c8a9a5SSteve French kfree_sensitive(server->session_key.response); 165738c8a9a5SSteve French server->session_key.response = NULL; 165838c8a9a5SSteve French server->session_key.len = 0; 165938c8a9a5SSteve French kfree(server->hostname); 166038c8a9a5SSteve French server->hostname = NULL; 166138c8a9a5SSteve French 166238c8a9a5SSteve French task = xchg(&server->tsk, NULL); 166338c8a9a5SSteve French if (task) 166438c8a9a5SSteve French send_sig(SIGKILL, task, 1); 166538c8a9a5SSteve French } 166638c8a9a5SSteve French 166738c8a9a5SSteve French struct TCP_Server_Info * 166838c8a9a5SSteve French cifs_get_tcp_session(struct smb3_fs_context *ctx, 166938c8a9a5SSteve French struct TCP_Server_Info *primary_server) 167038c8a9a5SSteve French { 167138c8a9a5SSteve French struct TCP_Server_Info *tcp_ses = NULL; 167238c8a9a5SSteve French int rc; 167338c8a9a5SSteve French 167438c8a9a5SSteve French cifs_dbg(FYI, "UNC: %s\n", ctx->UNC); 167538c8a9a5SSteve French 167638c8a9a5SSteve French /* see if we already have a matching tcp_ses */ 167738c8a9a5SSteve French tcp_ses = cifs_find_tcp_session(ctx); 167838c8a9a5SSteve French if (tcp_ses) 167938c8a9a5SSteve French return tcp_ses; 168038c8a9a5SSteve French 168138c8a9a5SSteve French tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL); 168238c8a9a5SSteve French if (!tcp_ses) { 168338c8a9a5SSteve French rc = -ENOMEM; 168438c8a9a5SSteve French goto out_err; 168538c8a9a5SSteve French } 168638c8a9a5SSteve French 168738c8a9a5SSteve French tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL); 168838c8a9a5SSteve French if (!tcp_ses->hostname) { 168938c8a9a5SSteve French rc = -ENOMEM; 169038c8a9a5SSteve French goto out_err; 169138c8a9a5SSteve French } 169238c8a9a5SSteve French 169338c8a9a5SSteve French if (ctx->leaf_fullpath) { 169438c8a9a5SSteve French tcp_ses->leaf_fullpath = kstrdup(ctx->leaf_fullpath, GFP_KERNEL); 169538c8a9a5SSteve French if (!tcp_ses->leaf_fullpath) { 169638c8a9a5SSteve French rc = -ENOMEM; 169738c8a9a5SSteve French goto out_err; 169838c8a9a5SSteve French } 169938c8a9a5SSteve French } 170038c8a9a5SSteve French 170138c8a9a5SSteve French if (ctx->nosharesock) 170238c8a9a5SSteve French tcp_ses->nosharesock = true; 170338c8a9a5SSteve French 170438c8a9a5SSteve French tcp_ses->ops = ctx->ops; 170538c8a9a5SSteve French tcp_ses->vals = ctx->vals; 170638c8a9a5SSteve French cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns)); 170738c8a9a5SSteve French 170838c8a9a5SSteve French tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId); 170938c8a9a5SSteve French tcp_ses->noblockcnt = ctx->rootfs; 171038c8a9a5SSteve French tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs; 171138c8a9a5SSteve French tcp_ses->noautotune = ctx->noautotune; 171238c8a9a5SSteve French tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay; 171338c8a9a5SSteve French tcp_ses->rdma = ctx->rdma; 171438c8a9a5SSteve French tcp_ses->in_flight = 0; 171538c8a9a5SSteve French tcp_ses->max_in_flight = 0; 171638c8a9a5SSteve French tcp_ses->credits = 1; 171738c8a9a5SSteve French if (primary_server) { 171838c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 171938c8a9a5SSteve French ++primary_server->srv_count; 172038c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 172138c8a9a5SSteve French tcp_ses->primary_server = primary_server; 172238c8a9a5SSteve French } 172338c8a9a5SSteve French init_waitqueue_head(&tcp_ses->response_q); 172438c8a9a5SSteve French init_waitqueue_head(&tcp_ses->request_q); 172538c8a9a5SSteve French INIT_LIST_HEAD(&tcp_ses->pending_mid_q); 172638c8a9a5SSteve French mutex_init(&tcp_ses->_srv_mutex); 172738c8a9a5SSteve French memcpy(tcp_ses->workstation_RFC1001_name, 172838c8a9a5SSteve French ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); 172938c8a9a5SSteve French memcpy(tcp_ses->server_RFC1001_name, 173038c8a9a5SSteve French ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); 173138c8a9a5SSteve French tcp_ses->session_estab = false; 173238c8a9a5SSteve French tcp_ses->sequence_number = 0; 173309ee7a3bSSteve French tcp_ses->channel_sequence_num = 0; /* only tracked for primary channel */ 173438c8a9a5SSteve French tcp_ses->reconnect_instance = 1; 173538c8a9a5SSteve French tcp_ses->lstrp = jiffies; 173638c8a9a5SSteve French tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression); 173738c8a9a5SSteve French spin_lock_init(&tcp_ses->req_lock); 173838c8a9a5SSteve French spin_lock_init(&tcp_ses->srv_lock); 173938c8a9a5SSteve French spin_lock_init(&tcp_ses->mid_lock); 174038c8a9a5SSteve French INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); 174138c8a9a5SSteve French INIT_LIST_HEAD(&tcp_ses->smb_ses_list); 174238c8a9a5SSteve French INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request); 174338c8a9a5SSteve French INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server); 174438c8a9a5SSteve French mutex_init(&tcp_ses->reconnect_mutex); 174538c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL 174638c8a9a5SSteve French mutex_init(&tcp_ses->refpath_lock); 174738c8a9a5SSteve French #endif 174838c8a9a5SSteve French memcpy(&tcp_ses->srcaddr, &ctx->srcaddr, 174938c8a9a5SSteve French sizeof(tcp_ses->srcaddr)); 175038c8a9a5SSteve French memcpy(&tcp_ses->dstaddr, &ctx->dstaddr, 175138c8a9a5SSteve French sizeof(tcp_ses->dstaddr)); 175238c8a9a5SSteve French if (ctx->use_client_guid) 175338c8a9a5SSteve French memcpy(tcp_ses->client_guid, ctx->client_guid, 175438c8a9a5SSteve French SMB2_CLIENT_GUID_SIZE); 175538c8a9a5SSteve French else 175638c8a9a5SSteve French generate_random_uuid(tcp_ses->client_guid); 175738c8a9a5SSteve French /* 175838c8a9a5SSteve French * at this point we are the only ones with the pointer 175938c8a9a5SSteve French * to the struct since the kernel thread not created yet 176038c8a9a5SSteve French * no need to spinlock this init of tcpStatus or srv_count 176138c8a9a5SSteve French */ 176238c8a9a5SSteve French tcp_ses->tcpStatus = CifsNew; 176338c8a9a5SSteve French ++tcp_ses->srv_count; 176438c8a9a5SSteve French 176538c8a9a5SSteve French if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN && 176638c8a9a5SSteve French ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX) 176738c8a9a5SSteve French tcp_ses->echo_interval = ctx->echo_interval * HZ; 176838c8a9a5SSteve French else 176938c8a9a5SSteve French tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ; 177038c8a9a5SSteve French if (tcp_ses->rdma) { 177138c8a9a5SSteve French #ifndef CONFIG_CIFS_SMB_DIRECT 177238c8a9a5SSteve French cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n"); 177338c8a9a5SSteve French rc = -ENOENT; 177438c8a9a5SSteve French goto out_err_crypto_release; 177538c8a9a5SSteve French #endif 177638c8a9a5SSteve French tcp_ses->smbd_conn = smbd_get_connection( 177738c8a9a5SSteve French tcp_ses, (struct sockaddr *)&ctx->dstaddr); 177838c8a9a5SSteve French if (tcp_ses->smbd_conn) { 177938c8a9a5SSteve French cifs_dbg(VFS, "RDMA transport established\n"); 178038c8a9a5SSteve French rc = 0; 178138c8a9a5SSteve French goto smbd_connected; 178238c8a9a5SSteve French } else { 178338c8a9a5SSteve French rc = -ENOENT; 178438c8a9a5SSteve French goto out_err_crypto_release; 178538c8a9a5SSteve French } 178638c8a9a5SSteve French } 178738c8a9a5SSteve French rc = ip_connect(tcp_ses); 178838c8a9a5SSteve French if (rc < 0) { 178938c8a9a5SSteve French cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n"); 179038c8a9a5SSteve French goto out_err_crypto_release; 179138c8a9a5SSteve French } 179238c8a9a5SSteve French smbd_connected: 179338c8a9a5SSteve French /* 179438c8a9a5SSteve French * since we're in a cifs function already, we know that 179538c8a9a5SSteve French * this will succeed. No need for try_module_get(). 179638c8a9a5SSteve French */ 179738c8a9a5SSteve French __module_get(THIS_MODULE); 179838c8a9a5SSteve French tcp_ses->tsk = kthread_run(cifs_demultiplex_thread, 179938c8a9a5SSteve French tcp_ses, "cifsd"); 180038c8a9a5SSteve French if (IS_ERR(tcp_ses->tsk)) { 180138c8a9a5SSteve French rc = PTR_ERR(tcp_ses->tsk); 180238c8a9a5SSteve French cifs_dbg(VFS, "error %d create cifsd thread\n", rc); 180338c8a9a5SSteve French module_put(THIS_MODULE); 180438c8a9a5SSteve French goto out_err_crypto_release; 180538c8a9a5SSteve French } 180638c8a9a5SSteve French tcp_ses->min_offload = ctx->min_offload; 180738c8a9a5SSteve French /* 180838c8a9a5SSteve French * at this point we are the only ones with the pointer 180938c8a9a5SSteve French * to the struct since the kernel thread not created yet 181038c8a9a5SSteve French * no need to spinlock this update of tcpStatus 181138c8a9a5SSteve French */ 181238c8a9a5SSteve French spin_lock(&tcp_ses->srv_lock); 181338c8a9a5SSteve French tcp_ses->tcpStatus = CifsNeedNegotiate; 181438c8a9a5SSteve French spin_unlock(&tcp_ses->srv_lock); 181538c8a9a5SSteve French 181638c8a9a5SSteve French if ((ctx->max_credits < 20) || (ctx->max_credits > 60000)) 181738c8a9a5SSteve French tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE; 181838c8a9a5SSteve French else 181938c8a9a5SSteve French tcp_ses->max_credits = ctx->max_credits; 182038c8a9a5SSteve French 182138c8a9a5SSteve French tcp_ses->nr_targets = 1; 182238c8a9a5SSteve French tcp_ses->ignore_signature = ctx->ignore_signature; 182338c8a9a5SSteve French /* thread spawned, put it on the list */ 182438c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 182538c8a9a5SSteve French list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); 182638c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 182738c8a9a5SSteve French 182838c8a9a5SSteve French /* queue echo request delayed work */ 182938c8a9a5SSteve French queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval); 183038c8a9a5SSteve French 183138c8a9a5SSteve French return tcp_ses; 183238c8a9a5SSteve French 183338c8a9a5SSteve French out_err_crypto_release: 183438c8a9a5SSteve French cifs_crypto_secmech_release(tcp_ses); 183538c8a9a5SSteve French 183638c8a9a5SSteve French put_net(cifs_net_ns(tcp_ses)); 183738c8a9a5SSteve French 183838c8a9a5SSteve French out_err: 183938c8a9a5SSteve French if (tcp_ses) { 1840b3773b19SSteve French if (SERVER_IS_CHAN(tcp_ses)) 184138c8a9a5SSteve French cifs_put_tcp_session(tcp_ses->primary_server, false); 184238c8a9a5SSteve French kfree(tcp_ses->hostname); 184338c8a9a5SSteve French kfree(tcp_ses->leaf_fullpath); 184438c8a9a5SSteve French if (tcp_ses->ssocket) 184538c8a9a5SSteve French sock_release(tcp_ses->ssocket); 184638c8a9a5SSteve French kfree(tcp_ses); 184738c8a9a5SSteve French } 184838c8a9a5SSteve French return ERR_PTR(rc); 184938c8a9a5SSteve French } 185038c8a9a5SSteve French 185138c8a9a5SSteve French /* this function must be called with ses_lock and chan_lock held */ 185238c8a9a5SSteve French static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx) 185338c8a9a5SSteve French { 185438c8a9a5SSteve French if (ctx->sectype != Unspecified && 185538c8a9a5SSteve French ctx->sectype != ses->sectype) 185638c8a9a5SSteve French return 0; 185738c8a9a5SSteve French 185838c8a9a5SSteve French /* 185938c8a9a5SSteve French * If an existing session is limited to less channels than 186038c8a9a5SSteve French * requested, it should not be reused 186138c8a9a5SSteve French */ 186238c8a9a5SSteve French if (ses->chan_max < ctx->max_channels) 186338c8a9a5SSteve French return 0; 186438c8a9a5SSteve French 186538c8a9a5SSteve French switch (ses->sectype) { 186638c8a9a5SSteve French case Kerberos: 186738c8a9a5SSteve French if (!uid_eq(ctx->cred_uid, ses->cred_uid)) 186838c8a9a5SSteve French return 0; 186938c8a9a5SSteve French break; 187038c8a9a5SSteve French default: 187138c8a9a5SSteve French /* NULL username means anonymous session */ 187238c8a9a5SSteve French if (ses->user_name == NULL) { 187338c8a9a5SSteve French if (!ctx->nullauth) 187438c8a9a5SSteve French return 0; 187538c8a9a5SSteve French break; 187638c8a9a5SSteve French } 187738c8a9a5SSteve French 187838c8a9a5SSteve French /* anything else takes username/password */ 187938c8a9a5SSteve French if (strncmp(ses->user_name, 188038c8a9a5SSteve French ctx->username ? ctx->username : "", 188138c8a9a5SSteve French CIFS_MAX_USERNAME_LEN)) 188238c8a9a5SSteve French return 0; 188338c8a9a5SSteve French if ((ctx->username && strlen(ctx->username) != 0) && 188438c8a9a5SSteve French ses->password != NULL && 188538c8a9a5SSteve French strncmp(ses->password, 188638c8a9a5SSteve French ctx->password ? ctx->password : "", 188738c8a9a5SSteve French CIFS_MAX_PASSWORD_LEN)) 188838c8a9a5SSteve French return 0; 188938c8a9a5SSteve French } 1890a43f95fdSWinston Wen 1891a43f95fdSWinston Wen if (strcmp(ctx->local_nls->charset, ses->local_nls->charset)) 1892a43f95fdSWinston Wen return 0; 1893a43f95fdSWinston Wen 189438c8a9a5SSteve French return 1; 189538c8a9a5SSteve French } 189638c8a9a5SSteve French 189738c8a9a5SSteve French /** 189838c8a9a5SSteve French * cifs_setup_ipc - helper to setup the IPC tcon for the session 189938c8a9a5SSteve French * @ses: smb session to issue the request on 190038c8a9a5SSteve French * @ctx: the superblock configuration context to use for building the 190138c8a9a5SSteve French * new tree connection for the IPC (interprocess communication RPC) 190238c8a9a5SSteve French * 190338c8a9a5SSteve French * A new IPC connection is made and stored in the session 190438c8a9a5SSteve French * tcon_ipc. The IPC tcon has the same lifetime as the session. 190538c8a9a5SSteve French */ 190638c8a9a5SSteve French static int 190738c8a9a5SSteve French cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx) 190838c8a9a5SSteve French { 190938c8a9a5SSteve French int rc = 0, xid; 191038c8a9a5SSteve French struct cifs_tcon *tcon; 191138c8a9a5SSteve French char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0}; 191238c8a9a5SSteve French bool seal = false; 191338c8a9a5SSteve French struct TCP_Server_Info *server = ses->server; 191438c8a9a5SSteve French 191538c8a9a5SSteve French /* 191638c8a9a5SSteve French * If the mount request that resulted in the creation of the 191738c8a9a5SSteve French * session requires encryption, force IPC to be encrypted too. 191838c8a9a5SSteve French */ 191938c8a9a5SSteve French if (ctx->seal) { 192038c8a9a5SSteve French if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) 192138c8a9a5SSteve French seal = true; 192238c8a9a5SSteve French else { 192338c8a9a5SSteve French cifs_server_dbg(VFS, 192438c8a9a5SSteve French "IPC: server doesn't support encryption\n"); 192538c8a9a5SSteve French return -EOPNOTSUPP; 192638c8a9a5SSteve French } 192738c8a9a5SSteve French } 192838c8a9a5SSteve French 19292da338ffSSteve French /* no need to setup directory caching on IPC share, so pass in false */ 19302da338ffSSteve French tcon = tcon_info_alloc(false); 193138c8a9a5SSteve French if (tcon == NULL) 193238c8a9a5SSteve French return -ENOMEM; 193338c8a9a5SSteve French 193438c8a9a5SSteve French spin_lock(&server->srv_lock); 193538c8a9a5SSteve French scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname); 193638c8a9a5SSteve French spin_unlock(&server->srv_lock); 193738c8a9a5SSteve French 193838c8a9a5SSteve French xid = get_xid(); 193938c8a9a5SSteve French tcon->ses = ses; 194038c8a9a5SSteve French tcon->ipc = true; 194138c8a9a5SSteve French tcon->seal = seal; 194238c8a9a5SSteve French rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls); 194338c8a9a5SSteve French free_xid(xid); 194438c8a9a5SSteve French 194538c8a9a5SSteve French if (rc) { 194638c8a9a5SSteve French cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc); 194738c8a9a5SSteve French tconInfoFree(tcon); 194838c8a9a5SSteve French goto out; 194938c8a9a5SSteve French } 195038c8a9a5SSteve French 195138c8a9a5SSteve French cifs_dbg(FYI, "IPC tcon rc=%d ipc tid=0x%x\n", rc, tcon->tid); 195238c8a9a5SSteve French 195338c8a9a5SSteve French spin_lock(&tcon->tc_lock); 195438c8a9a5SSteve French tcon->status = TID_GOOD; 195538c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 195638c8a9a5SSteve French ses->tcon_ipc = tcon; 195738c8a9a5SSteve French out: 195838c8a9a5SSteve French return rc; 195938c8a9a5SSteve French } 196038c8a9a5SSteve French 196138c8a9a5SSteve French /** 196238c8a9a5SSteve French * cifs_free_ipc - helper to release the session IPC tcon 196338c8a9a5SSteve French * @ses: smb session to unmount the IPC from 196438c8a9a5SSteve French * 196538c8a9a5SSteve French * Needs to be called everytime a session is destroyed. 196638c8a9a5SSteve French * 196738c8a9a5SSteve French * On session close, the IPC is closed and the server must release all tcons of the session. 196838c8a9a5SSteve French * No need to send a tree disconnect here. 196938c8a9a5SSteve French * 197038c8a9a5SSteve French * Besides, it will make the server to not close durable and resilient files on session close, as 197138c8a9a5SSteve French * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request. 197238c8a9a5SSteve French */ 197338c8a9a5SSteve French static int 197438c8a9a5SSteve French cifs_free_ipc(struct cifs_ses *ses) 197538c8a9a5SSteve French { 197638c8a9a5SSteve French struct cifs_tcon *tcon = ses->tcon_ipc; 197738c8a9a5SSteve French 197838c8a9a5SSteve French if (tcon == NULL) 197938c8a9a5SSteve French return 0; 198038c8a9a5SSteve French 198138c8a9a5SSteve French tconInfoFree(tcon); 198238c8a9a5SSteve French ses->tcon_ipc = NULL; 198338c8a9a5SSteve French return 0; 198438c8a9a5SSteve French } 198538c8a9a5SSteve French 198638c8a9a5SSteve French static struct cifs_ses * 198738c8a9a5SSteve French cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) 198838c8a9a5SSteve French { 198938c8a9a5SSteve French struct cifs_ses *ses, *ret = NULL; 199038c8a9a5SSteve French 199138c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 199238c8a9a5SSteve French list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { 199338c8a9a5SSteve French spin_lock(&ses->ses_lock); 199438c8a9a5SSteve French if (ses->ses_status == SES_EXITING) { 199538c8a9a5SSteve French spin_unlock(&ses->ses_lock); 199638c8a9a5SSteve French continue; 199738c8a9a5SSteve French } 199838c8a9a5SSteve French spin_lock(&ses->chan_lock); 199938c8a9a5SSteve French if (match_session(ses, ctx)) { 200038c8a9a5SSteve French spin_unlock(&ses->chan_lock); 200138c8a9a5SSteve French spin_unlock(&ses->ses_lock); 200238c8a9a5SSteve French ret = ses; 200338c8a9a5SSteve French break; 200438c8a9a5SSteve French } 200538c8a9a5SSteve French spin_unlock(&ses->chan_lock); 200638c8a9a5SSteve French spin_unlock(&ses->ses_lock); 200738c8a9a5SSteve French } 200838c8a9a5SSteve French if (ret) 200938c8a9a5SSteve French cifs_smb_ses_inc_refcount(ret); 201038c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 201138c8a9a5SSteve French return ret; 201238c8a9a5SSteve French } 201338c8a9a5SSteve French 201438c8a9a5SSteve French void __cifs_put_smb_ses(struct cifs_ses *ses) 201538c8a9a5SSteve French { 201638c8a9a5SSteve French unsigned int rc, xid; 201738c8a9a5SSteve French unsigned int chan_count; 201838c8a9a5SSteve French struct TCP_Server_Info *server = ses->server; 201938c8a9a5SSteve French 202038c8a9a5SSteve French spin_lock(&ses->ses_lock); 202138c8a9a5SSteve French if (ses->ses_status == SES_EXITING) { 202238c8a9a5SSteve French spin_unlock(&ses->ses_lock); 202338c8a9a5SSteve French return; 202438c8a9a5SSteve French } 202538c8a9a5SSteve French spin_unlock(&ses->ses_lock); 202638c8a9a5SSteve French 202738c8a9a5SSteve French cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count); 202838c8a9a5SSteve French cifs_dbg(FYI, 202938c8a9a5SSteve French "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->tree_name : "NONE"); 203038c8a9a5SSteve French 203138c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 203238c8a9a5SSteve French if (--ses->ses_count > 0) { 203338c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 203438c8a9a5SSteve French return; 203538c8a9a5SSteve French } 2036ff7d80a9SWinston Wen spin_lock(&ses->ses_lock); 2037ff7d80a9SWinston Wen if (ses->ses_status == SES_GOOD) 2038ff7d80a9SWinston Wen ses->ses_status = SES_EXITING; 2039ff7d80a9SWinston Wen spin_unlock(&ses->ses_lock); 204038c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 204138c8a9a5SSteve French 204238c8a9a5SSteve French /* ses_count can never go negative */ 204338c8a9a5SSteve French WARN_ON(ses->ses_count < 0); 204438c8a9a5SSteve French 204538c8a9a5SSteve French spin_lock(&ses->ses_lock); 204638c8a9a5SSteve French if (ses->ses_status == SES_EXITING && server->ops->logoff) { 204738c8a9a5SSteve French spin_unlock(&ses->ses_lock); 204838c8a9a5SSteve French cifs_free_ipc(ses); 204938c8a9a5SSteve French xid = get_xid(); 205038c8a9a5SSteve French rc = server->ops->logoff(xid, ses); 205138c8a9a5SSteve French if (rc) 205238c8a9a5SSteve French cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n", 205338c8a9a5SSteve French __func__, rc); 205438c8a9a5SSteve French _free_xid(xid); 205538c8a9a5SSteve French } else { 205638c8a9a5SSteve French spin_unlock(&ses->ses_lock); 205738c8a9a5SSteve French cifs_free_ipc(ses); 205838c8a9a5SSteve French } 205938c8a9a5SSteve French 206038c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 206138c8a9a5SSteve French list_del_init(&ses->smb_ses_list); 206238c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 206338c8a9a5SSteve French 206438c8a9a5SSteve French chan_count = ses->chan_count; 206538c8a9a5SSteve French 206638c8a9a5SSteve French /* close any extra channels */ 206738c8a9a5SSteve French if (chan_count > 1) { 206838c8a9a5SSteve French int i; 206938c8a9a5SSteve French 207038c8a9a5SSteve French for (i = 1; i < chan_count; i++) { 207138c8a9a5SSteve French if (ses->chans[i].iface) { 207238c8a9a5SSteve French kref_put(&ses->chans[i].iface->refcount, release_iface); 207338c8a9a5SSteve French ses->chans[i].iface = NULL; 207438c8a9a5SSteve French } 207538c8a9a5SSteve French cifs_put_tcp_session(ses->chans[i].server, 0); 207638c8a9a5SSteve French ses->chans[i].server = NULL; 207738c8a9a5SSteve French } 207838c8a9a5SSteve French } 207938c8a9a5SSteve French 20804875ef3eSShyam Prasad N /* we now account for primary channel in iface->refcount */ 20814875ef3eSShyam Prasad N if (ses->chans[0].iface) { 20824875ef3eSShyam Prasad N kref_put(&ses->chans[0].iface->refcount, release_iface); 20834875ef3eSShyam Prasad N ses->chans[0].server = NULL; 20844875ef3eSShyam Prasad N } 20854875ef3eSShyam Prasad N 208638c8a9a5SSteve French sesInfoFree(ses); 208738c8a9a5SSteve French cifs_put_tcp_session(server, 0); 208838c8a9a5SSteve French } 208938c8a9a5SSteve French 209038c8a9a5SSteve French #ifdef CONFIG_KEYS 209138c8a9a5SSteve French 209238c8a9a5SSteve French /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */ 209338c8a9a5SSteve French #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1) 209438c8a9a5SSteve French 209538c8a9a5SSteve French /* Populate username and pw fields from keyring if possible */ 209638c8a9a5SSteve French static int 209738c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses) 209838c8a9a5SSteve French { 209938c8a9a5SSteve French int rc = 0; 210038c8a9a5SSteve French int is_domain = 0; 210138c8a9a5SSteve French const char *delim, *payload; 210238c8a9a5SSteve French char *desc; 210338c8a9a5SSteve French ssize_t len; 210438c8a9a5SSteve French struct key *key; 210538c8a9a5SSteve French struct TCP_Server_Info *server = ses->server; 210638c8a9a5SSteve French struct sockaddr_in *sa; 210738c8a9a5SSteve French struct sockaddr_in6 *sa6; 210838c8a9a5SSteve French const struct user_key_payload *upayload; 210938c8a9a5SSteve French 211038c8a9a5SSteve French desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL); 211138c8a9a5SSteve French if (!desc) 211238c8a9a5SSteve French return -ENOMEM; 211338c8a9a5SSteve French 211438c8a9a5SSteve French /* try to find an address key first */ 211538c8a9a5SSteve French switch (server->dstaddr.ss_family) { 211638c8a9a5SSteve French case AF_INET: 211738c8a9a5SSteve French sa = (struct sockaddr_in *)&server->dstaddr; 211838c8a9a5SSteve French sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr); 211938c8a9a5SSteve French break; 212038c8a9a5SSteve French case AF_INET6: 212138c8a9a5SSteve French sa6 = (struct sockaddr_in6 *)&server->dstaddr; 212238c8a9a5SSteve French sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr); 212338c8a9a5SSteve French break; 212438c8a9a5SSteve French default: 212538c8a9a5SSteve French cifs_dbg(FYI, "Bad ss_family (%hu)\n", 212638c8a9a5SSteve French server->dstaddr.ss_family); 212738c8a9a5SSteve French rc = -EINVAL; 212838c8a9a5SSteve French goto out_err; 212938c8a9a5SSteve French } 213038c8a9a5SSteve French 213138c8a9a5SSteve French cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); 213238c8a9a5SSteve French key = request_key(&key_type_logon, desc, ""); 213338c8a9a5SSteve French if (IS_ERR(key)) { 213438c8a9a5SSteve French if (!ses->domainName) { 213538c8a9a5SSteve French cifs_dbg(FYI, "domainName is NULL\n"); 213638c8a9a5SSteve French rc = PTR_ERR(key); 213738c8a9a5SSteve French goto out_err; 213838c8a9a5SSteve French } 213938c8a9a5SSteve French 214038c8a9a5SSteve French /* didn't work, try to find a domain key */ 214138c8a9a5SSteve French sprintf(desc, "cifs:d:%s", ses->domainName); 214238c8a9a5SSteve French cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); 214338c8a9a5SSteve French key = request_key(&key_type_logon, desc, ""); 214438c8a9a5SSteve French if (IS_ERR(key)) { 214538c8a9a5SSteve French rc = PTR_ERR(key); 214638c8a9a5SSteve French goto out_err; 214738c8a9a5SSteve French } 214838c8a9a5SSteve French is_domain = 1; 214938c8a9a5SSteve French } 215038c8a9a5SSteve French 215138c8a9a5SSteve French down_read(&key->sem); 215238c8a9a5SSteve French upayload = user_key_payload_locked(key); 215338c8a9a5SSteve French if (IS_ERR_OR_NULL(upayload)) { 215438c8a9a5SSteve French rc = upayload ? PTR_ERR(upayload) : -EINVAL; 215538c8a9a5SSteve French goto out_key_put; 215638c8a9a5SSteve French } 215738c8a9a5SSteve French 215838c8a9a5SSteve French /* find first : in payload */ 215938c8a9a5SSteve French payload = upayload->data; 216038c8a9a5SSteve French delim = strnchr(payload, upayload->datalen, ':'); 216138c8a9a5SSteve French cifs_dbg(FYI, "payload=%s\n", payload); 216238c8a9a5SSteve French if (!delim) { 216338c8a9a5SSteve French cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n", 216438c8a9a5SSteve French upayload->datalen); 216538c8a9a5SSteve French rc = -EINVAL; 216638c8a9a5SSteve French goto out_key_put; 216738c8a9a5SSteve French } 216838c8a9a5SSteve French 216938c8a9a5SSteve French len = delim - payload; 217038c8a9a5SSteve French if (len > CIFS_MAX_USERNAME_LEN || len <= 0) { 217138c8a9a5SSteve French cifs_dbg(FYI, "Bad value from username search (len=%zd)\n", 217238c8a9a5SSteve French len); 217338c8a9a5SSteve French rc = -EINVAL; 217438c8a9a5SSteve French goto out_key_put; 217538c8a9a5SSteve French } 217638c8a9a5SSteve French 217738c8a9a5SSteve French ctx->username = kstrndup(payload, len, GFP_KERNEL); 217838c8a9a5SSteve French if (!ctx->username) { 217938c8a9a5SSteve French cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n", 218038c8a9a5SSteve French len); 218138c8a9a5SSteve French rc = -ENOMEM; 218238c8a9a5SSteve French goto out_key_put; 218338c8a9a5SSteve French } 218438c8a9a5SSteve French cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username); 218538c8a9a5SSteve French 218638c8a9a5SSteve French len = key->datalen - (len + 1); 218738c8a9a5SSteve French if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) { 218838c8a9a5SSteve French cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len); 218938c8a9a5SSteve French rc = -EINVAL; 219038c8a9a5SSteve French kfree(ctx->username); 219138c8a9a5SSteve French ctx->username = NULL; 219238c8a9a5SSteve French goto out_key_put; 219338c8a9a5SSteve French } 219438c8a9a5SSteve French 219538c8a9a5SSteve French ++delim; 219638c8a9a5SSteve French ctx->password = kstrndup(delim, len, GFP_KERNEL); 219738c8a9a5SSteve French if (!ctx->password) { 219838c8a9a5SSteve French cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n", 219938c8a9a5SSteve French len); 220038c8a9a5SSteve French rc = -ENOMEM; 220138c8a9a5SSteve French kfree(ctx->username); 220238c8a9a5SSteve French ctx->username = NULL; 220338c8a9a5SSteve French goto out_key_put; 220438c8a9a5SSteve French } 220538c8a9a5SSteve French 220638c8a9a5SSteve French /* 220738c8a9a5SSteve French * If we have a domain key then we must set the domainName in the 220838c8a9a5SSteve French * for the request. 220938c8a9a5SSteve French */ 221038c8a9a5SSteve French if (is_domain && ses->domainName) { 221138c8a9a5SSteve French ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL); 221238c8a9a5SSteve French if (!ctx->domainname) { 221338c8a9a5SSteve French cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", 221438c8a9a5SSteve French len); 221538c8a9a5SSteve French rc = -ENOMEM; 221638c8a9a5SSteve French kfree(ctx->username); 221738c8a9a5SSteve French ctx->username = NULL; 221838c8a9a5SSteve French kfree_sensitive(ctx->password); 221938c8a9a5SSteve French ctx->password = NULL; 222038c8a9a5SSteve French goto out_key_put; 222138c8a9a5SSteve French } 222238c8a9a5SSteve French } 222338c8a9a5SSteve French 222438c8a9a5SSteve French strscpy(ctx->workstation_name, ses->workstation_name, sizeof(ctx->workstation_name)); 222538c8a9a5SSteve French 222638c8a9a5SSteve French out_key_put: 222738c8a9a5SSteve French up_read(&key->sem); 222838c8a9a5SSteve French key_put(key); 222938c8a9a5SSteve French out_err: 223038c8a9a5SSteve French kfree(desc); 223138c8a9a5SSteve French cifs_dbg(FYI, "%s: returning %d\n", __func__, rc); 223238c8a9a5SSteve French return rc; 223338c8a9a5SSteve French } 223438c8a9a5SSteve French #else /* ! CONFIG_KEYS */ 223538c8a9a5SSteve French static inline int 223638c8a9a5SSteve French cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)), 223738c8a9a5SSteve French struct cifs_ses *ses __attribute__((unused))) 223838c8a9a5SSteve French { 223938c8a9a5SSteve French return -ENOSYS; 224038c8a9a5SSteve French } 224138c8a9a5SSteve French #endif /* CONFIG_KEYS */ 224238c8a9a5SSteve French 224338c8a9a5SSteve French /** 224438c8a9a5SSteve French * cifs_get_smb_ses - get a session matching @ctx data from @server 224538c8a9a5SSteve French * @server: server to setup the session to 224638c8a9a5SSteve French * @ctx: superblock configuration context to use to setup the session 224738c8a9a5SSteve French * 224838c8a9a5SSteve French * This function assumes it is being called from cifs_mount() where we 224938c8a9a5SSteve French * already got a server reference (server refcount +1). See 225038c8a9a5SSteve French * cifs_get_tcon() for refcount explanations. 225138c8a9a5SSteve French */ 225238c8a9a5SSteve French struct cifs_ses * 225338c8a9a5SSteve French cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) 225438c8a9a5SSteve French { 225538c8a9a5SSteve French int rc = 0; 225638c8a9a5SSteve French unsigned int xid; 225738c8a9a5SSteve French struct cifs_ses *ses; 225838c8a9a5SSteve French struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; 225938c8a9a5SSteve French struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; 226038c8a9a5SSteve French 226138c8a9a5SSteve French xid = get_xid(); 226238c8a9a5SSteve French 226338c8a9a5SSteve French ses = cifs_find_smb_ses(server, ctx); 226438c8a9a5SSteve French if (ses) { 226538c8a9a5SSteve French cifs_dbg(FYI, "Existing smb sess found (status=%d)\n", 226638c8a9a5SSteve French ses->ses_status); 226738c8a9a5SSteve French 226838c8a9a5SSteve French spin_lock(&ses->chan_lock); 226938c8a9a5SSteve French if (cifs_chan_needs_reconnect(ses, server)) { 227038c8a9a5SSteve French spin_unlock(&ses->chan_lock); 227138c8a9a5SSteve French cifs_dbg(FYI, "Session needs reconnect\n"); 227238c8a9a5SSteve French 227338c8a9a5SSteve French mutex_lock(&ses->session_mutex); 227438c8a9a5SSteve French rc = cifs_negotiate_protocol(xid, ses, server); 227538c8a9a5SSteve French if (rc) { 227638c8a9a5SSteve French mutex_unlock(&ses->session_mutex); 227738c8a9a5SSteve French /* problem -- put our ses reference */ 227838c8a9a5SSteve French cifs_put_smb_ses(ses); 227938c8a9a5SSteve French free_xid(xid); 228038c8a9a5SSteve French return ERR_PTR(rc); 228138c8a9a5SSteve French } 228238c8a9a5SSteve French 228338c8a9a5SSteve French rc = cifs_setup_session(xid, ses, server, 228438c8a9a5SSteve French ctx->local_nls); 228538c8a9a5SSteve French if (rc) { 228638c8a9a5SSteve French mutex_unlock(&ses->session_mutex); 228738c8a9a5SSteve French /* problem -- put our reference */ 228838c8a9a5SSteve French cifs_put_smb_ses(ses); 228938c8a9a5SSteve French free_xid(xid); 229038c8a9a5SSteve French return ERR_PTR(rc); 229138c8a9a5SSteve French } 229238c8a9a5SSteve French mutex_unlock(&ses->session_mutex); 229338c8a9a5SSteve French 229438c8a9a5SSteve French spin_lock(&ses->chan_lock); 229538c8a9a5SSteve French } 229638c8a9a5SSteve French spin_unlock(&ses->chan_lock); 229738c8a9a5SSteve French 229838c8a9a5SSteve French /* existing SMB ses has a server reference already */ 229938c8a9a5SSteve French cifs_put_tcp_session(server, 0); 230038c8a9a5SSteve French free_xid(xid); 230138c8a9a5SSteve French return ses; 230238c8a9a5SSteve French } 230338c8a9a5SSteve French 230438c8a9a5SSteve French rc = -ENOMEM; 230538c8a9a5SSteve French 230638c8a9a5SSteve French cifs_dbg(FYI, "Existing smb sess not found\n"); 230738c8a9a5SSteve French ses = sesInfoAlloc(); 230838c8a9a5SSteve French if (ses == NULL) 230938c8a9a5SSteve French goto get_ses_fail; 231038c8a9a5SSteve French 231138c8a9a5SSteve French /* new SMB session uses our server ref */ 231238c8a9a5SSteve French ses->server = server; 231338c8a9a5SSteve French if (server->dstaddr.ss_family == AF_INET6) 231438c8a9a5SSteve French sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr); 231538c8a9a5SSteve French else 231638c8a9a5SSteve French sprintf(ses->ip_addr, "%pI4", &addr->sin_addr); 231738c8a9a5SSteve French 231838c8a9a5SSteve French if (ctx->username) { 231938c8a9a5SSteve French ses->user_name = kstrdup(ctx->username, GFP_KERNEL); 232038c8a9a5SSteve French if (!ses->user_name) 232138c8a9a5SSteve French goto get_ses_fail; 232238c8a9a5SSteve French } 232338c8a9a5SSteve French 232438c8a9a5SSteve French /* ctx->password freed at unmount */ 232538c8a9a5SSteve French if (ctx->password) { 232638c8a9a5SSteve French ses->password = kstrdup(ctx->password, GFP_KERNEL); 232738c8a9a5SSteve French if (!ses->password) 232838c8a9a5SSteve French goto get_ses_fail; 232938c8a9a5SSteve French } 233038c8a9a5SSteve French if (ctx->domainname) { 233138c8a9a5SSteve French ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL); 233238c8a9a5SSteve French if (!ses->domainName) 233338c8a9a5SSteve French goto get_ses_fail; 233438c8a9a5SSteve French } 233538c8a9a5SSteve French 233638c8a9a5SSteve French strscpy(ses->workstation_name, ctx->workstation_name, sizeof(ses->workstation_name)); 233738c8a9a5SSteve French 233838c8a9a5SSteve French if (ctx->domainauto) 233938c8a9a5SSteve French ses->domainAuto = ctx->domainauto; 234038c8a9a5SSteve French ses->cred_uid = ctx->cred_uid; 234138c8a9a5SSteve French ses->linux_uid = ctx->linux_uid; 234238c8a9a5SSteve French 234338c8a9a5SSteve French ses->sectype = ctx->sectype; 234438c8a9a5SSteve French ses->sign = ctx->sign; 2345a43f95fdSWinston Wen ses->local_nls = load_nls(ctx->local_nls->charset); 234638c8a9a5SSteve French 234738c8a9a5SSteve French /* add server as first channel */ 234838c8a9a5SSteve French spin_lock(&ses->chan_lock); 234938c8a9a5SSteve French ses->chans[0].server = server; 235038c8a9a5SSteve French ses->chan_count = 1; 235138c8a9a5SSteve French ses->chan_max = ctx->multichannel ? ctx->max_channels:1; 235238c8a9a5SSteve French ses->chans_need_reconnect = 1; 235338c8a9a5SSteve French spin_unlock(&ses->chan_lock); 235438c8a9a5SSteve French 235538c8a9a5SSteve French mutex_lock(&ses->session_mutex); 235638c8a9a5SSteve French rc = cifs_negotiate_protocol(xid, ses, server); 235738c8a9a5SSteve French if (!rc) 235838c8a9a5SSteve French rc = cifs_setup_session(xid, ses, server, ctx->local_nls); 235938c8a9a5SSteve French mutex_unlock(&ses->session_mutex); 236038c8a9a5SSteve French 236138c8a9a5SSteve French /* each channel uses a different signing key */ 236238c8a9a5SSteve French spin_lock(&ses->chan_lock); 236338c8a9a5SSteve French memcpy(ses->chans[0].signkey, ses->smb3signingkey, 236438c8a9a5SSteve French sizeof(ses->smb3signingkey)); 236538c8a9a5SSteve French spin_unlock(&ses->chan_lock); 236638c8a9a5SSteve French 236738c8a9a5SSteve French if (rc) 236838c8a9a5SSteve French goto get_ses_fail; 236938c8a9a5SSteve French 237038c8a9a5SSteve French /* 237138c8a9a5SSteve French * success, put it on the list and add it as first channel 237238c8a9a5SSteve French * note: the session becomes active soon after this. So you'll 237338c8a9a5SSteve French * need to lock before changing something in the session. 237438c8a9a5SSteve French */ 237538c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 237638c8a9a5SSteve French ses->dfs_root_ses = ctx->dfs_root_ses; 237738c8a9a5SSteve French if (ses->dfs_root_ses) 237838c8a9a5SSteve French ses->dfs_root_ses->ses_count++; 237938c8a9a5SSteve French list_add(&ses->smb_ses_list, &server->smb_ses_list); 238038c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 238138c8a9a5SSteve French 238238c8a9a5SSteve French cifs_setup_ipc(ses, ctx); 238338c8a9a5SSteve French 238438c8a9a5SSteve French free_xid(xid); 238538c8a9a5SSteve French 238638c8a9a5SSteve French return ses; 238738c8a9a5SSteve French 238838c8a9a5SSteve French get_ses_fail: 238938c8a9a5SSteve French sesInfoFree(ses); 239038c8a9a5SSteve French free_xid(xid); 239138c8a9a5SSteve French return ERR_PTR(rc); 239238c8a9a5SSteve French } 239338c8a9a5SSteve French 239438c8a9a5SSteve French /* this function must be called with tc_lock held */ 239538c8a9a5SSteve French static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) 239638c8a9a5SSteve French { 239738c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 239838c8a9a5SSteve French 239938c8a9a5SSteve French if (tcon->status == TID_EXITING) 240038c8a9a5SSteve French return 0; 24013ae872deSPaulo Alcantara 24023ae872deSPaulo Alcantara if (tcon->origin_fullpath) { 24033ae872deSPaulo Alcantara if (!ctx->source || 24043ae872deSPaulo Alcantara !dfs_src_pathname_equal(ctx->source, 24053ae872deSPaulo Alcantara tcon->origin_fullpath)) 240638c8a9a5SSteve French return 0; 24073ae872deSPaulo Alcantara } else if (!server->leaf_fullpath && 24083ae872deSPaulo Alcantara strncmp(tcon->tree_name, ctx->UNC, MAX_TREE_SIZE)) { 24093ae872deSPaulo Alcantara return 0; 24103ae872deSPaulo Alcantara } 241138c8a9a5SSteve French if (tcon->seal != ctx->seal) 241238c8a9a5SSteve French return 0; 241338c8a9a5SSteve French if (tcon->snapshot_time != ctx->snapshot_time) 241438c8a9a5SSteve French return 0; 241538c8a9a5SSteve French if (tcon->handle_timeout != ctx->handle_timeout) 241638c8a9a5SSteve French return 0; 241738c8a9a5SSteve French if (tcon->no_lease != ctx->no_lease) 241838c8a9a5SSteve French return 0; 241938c8a9a5SSteve French if (tcon->nodelete != ctx->nodelete) 242038c8a9a5SSteve French return 0; 242138c8a9a5SSteve French return 1; 242238c8a9a5SSteve French } 242338c8a9a5SSteve French 242438c8a9a5SSteve French static struct cifs_tcon * 242538c8a9a5SSteve French cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) 242638c8a9a5SSteve French { 242738c8a9a5SSteve French struct cifs_tcon *tcon; 242838c8a9a5SSteve French 242938c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 243038c8a9a5SSteve French list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { 243138c8a9a5SSteve French spin_lock(&tcon->tc_lock); 243238c8a9a5SSteve French if (!match_tcon(tcon, ctx)) { 243338c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 243438c8a9a5SSteve French continue; 243538c8a9a5SSteve French } 243638c8a9a5SSteve French ++tcon->tc_count; 243738c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 243838c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 243938c8a9a5SSteve French return tcon; 244038c8a9a5SSteve French } 244138c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 244238c8a9a5SSteve French return NULL; 244338c8a9a5SSteve French } 244438c8a9a5SSteve French 244538c8a9a5SSteve French void 244638c8a9a5SSteve French cifs_put_tcon(struct cifs_tcon *tcon) 244738c8a9a5SSteve French { 244838c8a9a5SSteve French unsigned int xid; 244938c8a9a5SSteve French struct cifs_ses *ses; 245038c8a9a5SSteve French 245138c8a9a5SSteve French /* 245238c8a9a5SSteve French * IPC tcon share the lifetime of their session and are 245338c8a9a5SSteve French * destroyed in the session put function 245438c8a9a5SSteve French */ 245538c8a9a5SSteve French if (tcon == NULL || tcon->ipc) 245638c8a9a5SSteve French return; 245738c8a9a5SSteve French 245838c8a9a5SSteve French ses = tcon->ses; 245938c8a9a5SSteve French cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); 246038c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 246138c8a9a5SSteve French spin_lock(&tcon->tc_lock); 246238c8a9a5SSteve French if (--tcon->tc_count > 0) { 246338c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 246438c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 246538c8a9a5SSteve French return; 246638c8a9a5SSteve French } 246738c8a9a5SSteve French 246838c8a9a5SSteve French /* tc_count can never go negative */ 246938c8a9a5SSteve French WARN_ON(tcon->tc_count < 0); 247038c8a9a5SSteve French 247138c8a9a5SSteve French list_del_init(&tcon->tcon_list); 247238c8a9a5SSteve French tcon->status = TID_EXITING; 247338c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 247438c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 247538c8a9a5SSteve French 247638c8a9a5SSteve French /* cancel polling of interfaces */ 247738c8a9a5SSteve French cancel_delayed_work_sync(&tcon->query_interfaces); 247838c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL 247938c8a9a5SSteve French cancel_delayed_work_sync(&tcon->dfs_cache_work); 248038c8a9a5SSteve French #endif 248138c8a9a5SSteve French 248238c8a9a5SSteve French if (tcon->use_witness) { 248338c8a9a5SSteve French int rc; 248438c8a9a5SSteve French 248538c8a9a5SSteve French rc = cifs_swn_unregister(tcon); 248638c8a9a5SSteve French if (rc < 0) { 248738c8a9a5SSteve French cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n", 248838c8a9a5SSteve French __func__, rc); 248938c8a9a5SSteve French } 249038c8a9a5SSteve French } 249138c8a9a5SSteve French 249238c8a9a5SSteve French xid = get_xid(); 249338c8a9a5SSteve French if (ses->server->ops->tree_disconnect) 249438c8a9a5SSteve French ses->server->ops->tree_disconnect(xid, tcon); 249538c8a9a5SSteve French _free_xid(xid); 249638c8a9a5SSteve French 249738c8a9a5SSteve French cifs_fscache_release_super_cookie(tcon); 249838c8a9a5SSteve French tconInfoFree(tcon); 249938c8a9a5SSteve French cifs_put_smb_ses(ses); 250038c8a9a5SSteve French } 250138c8a9a5SSteve French 250238c8a9a5SSteve French /** 250338c8a9a5SSteve French * cifs_get_tcon - get a tcon matching @ctx data from @ses 250438c8a9a5SSteve French * @ses: smb session to issue the request on 250538c8a9a5SSteve French * @ctx: the superblock configuration context to use for building the 250638c8a9a5SSteve French * 250738c8a9a5SSteve French * - tcon refcount is the number of mount points using the tcon. 250838c8a9a5SSteve French * - ses refcount is the number of tcon using the session. 250938c8a9a5SSteve French * 251038c8a9a5SSteve French * 1. This function assumes it is being called from cifs_mount() where 251138c8a9a5SSteve French * we already got a session reference (ses refcount +1). 251238c8a9a5SSteve French * 251338c8a9a5SSteve French * 2. Since we're in the context of adding a mount point, the end 251438c8a9a5SSteve French * result should be either: 251538c8a9a5SSteve French * 251638c8a9a5SSteve French * a) a new tcon already allocated with refcount=1 (1 mount point) and 251738c8a9a5SSteve French * its session refcount incremented (1 new tcon). This +1 was 251838c8a9a5SSteve French * already done in (1). 251938c8a9a5SSteve French * 252038c8a9a5SSteve French * b) an existing tcon with refcount+1 (add a mount point to it) and 252138c8a9a5SSteve French * identical ses refcount (no new tcon). Because of (1) we need to 252238c8a9a5SSteve French * decrement the ses refcount. 252338c8a9a5SSteve French */ 252438c8a9a5SSteve French static struct cifs_tcon * 252538c8a9a5SSteve French cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) 252638c8a9a5SSteve French { 252738c8a9a5SSteve French struct cifs_tcon *tcon; 25283b8bb317SPaulo Alcantara bool nohandlecache; 25293b8bb317SPaulo Alcantara int rc, xid; 253038c8a9a5SSteve French 253138c8a9a5SSteve French tcon = cifs_find_tcon(ses, ctx); 253238c8a9a5SSteve French if (tcon) { 253338c8a9a5SSteve French /* 253438c8a9a5SSteve French * tcon has refcount already incremented but we need to 253538c8a9a5SSteve French * decrement extra ses reference gotten by caller (case b) 253638c8a9a5SSteve French */ 253738c8a9a5SSteve French cifs_dbg(FYI, "Found match on UNC path\n"); 253838c8a9a5SSteve French cifs_put_smb_ses(ses); 253938c8a9a5SSteve French return tcon; 254038c8a9a5SSteve French } 254138c8a9a5SSteve French 254238c8a9a5SSteve French if (!ses->server->ops->tree_connect) { 254338c8a9a5SSteve French rc = -ENOSYS; 254438c8a9a5SSteve French goto out_fail; 254538c8a9a5SSteve French } 254638c8a9a5SSteve French 25473b8bb317SPaulo Alcantara if (ses->server->dialect >= SMB20_PROT_ID && 25483b8bb317SPaulo Alcantara (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)) 25493b8bb317SPaulo Alcantara nohandlecache = ctx->nohandlecache; 25502da338ffSSteve French else 25513b8bb317SPaulo Alcantara nohandlecache = true; 25523b8bb317SPaulo Alcantara tcon = tcon_info_alloc(!nohandlecache); 255338c8a9a5SSteve French if (tcon == NULL) { 255438c8a9a5SSteve French rc = -ENOMEM; 255538c8a9a5SSteve French goto out_fail; 255638c8a9a5SSteve French } 25573b8bb317SPaulo Alcantara tcon->nohandlecache = nohandlecache; 255838c8a9a5SSteve French 255938c8a9a5SSteve French if (ctx->snapshot_time) { 256038c8a9a5SSteve French if (ses->server->vals->protocol_id == 0) { 256138c8a9a5SSteve French cifs_dbg(VFS, 256238c8a9a5SSteve French "Use SMB2 or later for snapshot mount option\n"); 256338c8a9a5SSteve French rc = -EOPNOTSUPP; 256438c8a9a5SSteve French goto out_fail; 256538c8a9a5SSteve French } else 256638c8a9a5SSteve French tcon->snapshot_time = ctx->snapshot_time; 256738c8a9a5SSteve French } 256838c8a9a5SSteve French 256938c8a9a5SSteve French if (ctx->handle_timeout) { 257038c8a9a5SSteve French if (ses->server->vals->protocol_id == 0) { 257138c8a9a5SSteve French cifs_dbg(VFS, 257238c8a9a5SSteve French "Use SMB2.1 or later for handle timeout option\n"); 257338c8a9a5SSteve French rc = -EOPNOTSUPP; 257438c8a9a5SSteve French goto out_fail; 257538c8a9a5SSteve French } else 257638c8a9a5SSteve French tcon->handle_timeout = ctx->handle_timeout; 257738c8a9a5SSteve French } 257838c8a9a5SSteve French 257938c8a9a5SSteve French tcon->ses = ses; 258038c8a9a5SSteve French if (ctx->password) { 258138c8a9a5SSteve French tcon->password = kstrdup(ctx->password, GFP_KERNEL); 258238c8a9a5SSteve French if (!tcon->password) { 258338c8a9a5SSteve French rc = -ENOMEM; 258438c8a9a5SSteve French goto out_fail; 258538c8a9a5SSteve French } 258638c8a9a5SSteve French } 258738c8a9a5SSteve French 258838c8a9a5SSteve French if (ctx->seal) { 258938c8a9a5SSteve French if (ses->server->vals->protocol_id == 0) { 259038c8a9a5SSteve French cifs_dbg(VFS, 259138c8a9a5SSteve French "SMB3 or later required for encryption\n"); 259238c8a9a5SSteve French rc = -EOPNOTSUPP; 259338c8a9a5SSteve French goto out_fail; 259438c8a9a5SSteve French } else if (tcon->ses->server->capabilities & 259538c8a9a5SSteve French SMB2_GLOBAL_CAP_ENCRYPTION) 259638c8a9a5SSteve French tcon->seal = true; 259738c8a9a5SSteve French else { 259838c8a9a5SSteve French cifs_dbg(VFS, "Encryption is not supported on share\n"); 259938c8a9a5SSteve French rc = -EOPNOTSUPP; 260038c8a9a5SSteve French goto out_fail; 260138c8a9a5SSteve French } 260238c8a9a5SSteve French } 260338c8a9a5SSteve French 260438c8a9a5SSteve French if (ctx->linux_ext) { 260538c8a9a5SSteve French if (ses->server->posix_ext_supported) { 260638c8a9a5SSteve French tcon->posix_extensions = true; 260738c8a9a5SSteve French pr_warn_once("SMB3.11 POSIX Extensions are experimental\n"); 260838c8a9a5SSteve French } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) || 260938c8a9a5SSteve French (strcmp(ses->server->vals->version_string, 261038c8a9a5SSteve French SMB3ANY_VERSION_STRING) == 0) || 261138c8a9a5SSteve French (strcmp(ses->server->vals->version_string, 261238c8a9a5SSteve French SMBDEFAULT_VERSION_STRING) == 0)) { 261338c8a9a5SSteve French cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n"); 261438c8a9a5SSteve French rc = -EOPNOTSUPP; 261538c8a9a5SSteve French goto out_fail; 261638c8a9a5SSteve French } else { 261738c8a9a5SSteve French cifs_dbg(VFS, "Check vers= mount option. SMB3.11 " 261838c8a9a5SSteve French "disabled but required for POSIX extensions\n"); 261938c8a9a5SSteve French rc = -EOPNOTSUPP; 262038c8a9a5SSteve French goto out_fail; 262138c8a9a5SSteve French } 262238c8a9a5SSteve French } 262338c8a9a5SSteve French 262438c8a9a5SSteve French xid = get_xid(); 262538c8a9a5SSteve French rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon, 262638c8a9a5SSteve French ctx->local_nls); 262738c8a9a5SSteve French free_xid(xid); 262838c8a9a5SSteve French cifs_dbg(FYI, "Tcon rc = %d\n", rc); 262938c8a9a5SSteve French if (rc) 263038c8a9a5SSteve French goto out_fail; 263138c8a9a5SSteve French 263238c8a9a5SSteve French tcon->use_persistent = false; 263338c8a9a5SSteve French /* check if SMB2 or later, CIFS does not support persistent handles */ 263438c8a9a5SSteve French if (ctx->persistent) { 263538c8a9a5SSteve French if (ses->server->vals->protocol_id == 0) { 263638c8a9a5SSteve French cifs_dbg(VFS, 263738c8a9a5SSteve French "SMB3 or later required for persistent handles\n"); 263838c8a9a5SSteve French rc = -EOPNOTSUPP; 263938c8a9a5SSteve French goto out_fail; 264038c8a9a5SSteve French } else if (ses->server->capabilities & 264138c8a9a5SSteve French SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) 264238c8a9a5SSteve French tcon->use_persistent = true; 264338c8a9a5SSteve French else /* persistent handles requested but not supported */ { 264438c8a9a5SSteve French cifs_dbg(VFS, 264538c8a9a5SSteve French "Persistent handles not supported on share\n"); 264638c8a9a5SSteve French rc = -EOPNOTSUPP; 264738c8a9a5SSteve French goto out_fail; 264838c8a9a5SSteve French } 264938c8a9a5SSteve French } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY) 265038c8a9a5SSteve French && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) 265138c8a9a5SSteve French && (ctx->nopersistent == false)) { 265238c8a9a5SSteve French cifs_dbg(FYI, "enabling persistent handles\n"); 265338c8a9a5SSteve French tcon->use_persistent = true; 265438c8a9a5SSteve French } else if (ctx->resilient) { 265538c8a9a5SSteve French if (ses->server->vals->protocol_id == 0) { 265638c8a9a5SSteve French cifs_dbg(VFS, 265738c8a9a5SSteve French "SMB2.1 or later required for resilient handles\n"); 265838c8a9a5SSteve French rc = -EOPNOTSUPP; 265938c8a9a5SSteve French goto out_fail; 266038c8a9a5SSteve French } 266138c8a9a5SSteve French tcon->use_resilient = true; 266238c8a9a5SSteve French } 266338c8a9a5SSteve French 266438c8a9a5SSteve French tcon->use_witness = false; 266538c8a9a5SSteve French if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) { 266638c8a9a5SSteve French if (ses->server->vals->protocol_id >= SMB30_PROT_ID) { 266738c8a9a5SSteve French if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) { 266838c8a9a5SSteve French /* 266938c8a9a5SSteve French * Set witness in use flag in first place 267038c8a9a5SSteve French * to retry registration in the echo task 267138c8a9a5SSteve French */ 267238c8a9a5SSteve French tcon->use_witness = true; 267338c8a9a5SSteve French /* And try to register immediately */ 267438c8a9a5SSteve French rc = cifs_swn_register(tcon); 267538c8a9a5SSteve French if (rc < 0) { 267638c8a9a5SSteve French cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc); 267738c8a9a5SSteve French goto out_fail; 267838c8a9a5SSteve French } 267938c8a9a5SSteve French } else { 268038c8a9a5SSteve French /* TODO: try to extend for non-cluster uses (eg multichannel) */ 268138c8a9a5SSteve French cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n"); 268238c8a9a5SSteve French rc = -EOPNOTSUPP; 268338c8a9a5SSteve French goto out_fail; 268438c8a9a5SSteve French } 268538c8a9a5SSteve French } else { 268638c8a9a5SSteve French cifs_dbg(VFS, "SMB3 or later required for witness option\n"); 268738c8a9a5SSteve French rc = -EOPNOTSUPP; 268838c8a9a5SSteve French goto out_fail; 268938c8a9a5SSteve French } 269038c8a9a5SSteve French } 269138c8a9a5SSteve French 269238c8a9a5SSteve French /* If the user really knows what they are doing they can override */ 269338c8a9a5SSteve French if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) { 269438c8a9a5SSteve French if (ctx->cache_ro) 269538c8a9a5SSteve French cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n"); 269638c8a9a5SSteve French else if (ctx->cache_rw) 269738c8a9a5SSteve French cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n"); 269838c8a9a5SSteve French } 269938c8a9a5SSteve French 270038c8a9a5SSteve French if (ctx->no_lease) { 270138c8a9a5SSteve French if (ses->server->vals->protocol_id == 0) { 270238c8a9a5SSteve French cifs_dbg(VFS, 270338c8a9a5SSteve French "SMB2 or later required for nolease option\n"); 270438c8a9a5SSteve French rc = -EOPNOTSUPP; 270538c8a9a5SSteve French goto out_fail; 270638c8a9a5SSteve French } else 270738c8a9a5SSteve French tcon->no_lease = ctx->no_lease; 270838c8a9a5SSteve French } 270938c8a9a5SSteve French 271038c8a9a5SSteve French /* 271138c8a9a5SSteve French * We can have only one retry value for a connection to a share so for 271238c8a9a5SSteve French * resources mounted more than once to the same server share the last 271338c8a9a5SSteve French * value passed in for the retry flag is used. 271438c8a9a5SSteve French */ 271538c8a9a5SSteve French tcon->retry = ctx->retry; 271638c8a9a5SSteve French tcon->nocase = ctx->nocase; 271738c8a9a5SSteve French tcon->broken_sparse_sup = ctx->no_sparse; 27186a50d71dSSteve French tcon->max_cached_dirs = ctx->max_cached_dirs; 271938c8a9a5SSteve French tcon->nodelete = ctx->nodelete; 272038c8a9a5SSteve French tcon->local_lease = ctx->local_lease; 272138c8a9a5SSteve French INIT_LIST_HEAD(&tcon->pending_opens); 272238c8a9a5SSteve French tcon->status = TID_GOOD; 272338c8a9a5SSteve French 272438c8a9a5SSteve French INIT_DELAYED_WORK(&tcon->query_interfaces, 272538c8a9a5SSteve French smb2_query_server_interfaces); 272638c8a9a5SSteve French if (ses->server->dialect >= SMB30_PROT_ID && 272738c8a9a5SSteve French (ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) { 272838c8a9a5SSteve French /* schedule query interfaces poll */ 272938c8a9a5SSteve French queue_delayed_work(cifsiod_wq, &tcon->query_interfaces, 273038c8a9a5SSteve French (SMB_INTERFACE_POLL_INTERVAL * HZ)); 273138c8a9a5SSteve French } 273238c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL 273338c8a9a5SSteve French INIT_DELAYED_WORK(&tcon->dfs_cache_work, dfs_cache_refresh); 273438c8a9a5SSteve French #endif 273538c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 273638c8a9a5SSteve French list_add(&tcon->tcon_list, &ses->tcon_list); 273738c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 273838c8a9a5SSteve French 273938c8a9a5SSteve French return tcon; 274038c8a9a5SSteve French 274138c8a9a5SSteve French out_fail: 274238c8a9a5SSteve French tconInfoFree(tcon); 274338c8a9a5SSteve French return ERR_PTR(rc); 274438c8a9a5SSteve French } 274538c8a9a5SSteve French 274638c8a9a5SSteve French void 274738c8a9a5SSteve French cifs_put_tlink(struct tcon_link *tlink) 274838c8a9a5SSteve French { 274938c8a9a5SSteve French if (!tlink || IS_ERR(tlink)) 275038c8a9a5SSteve French return; 275138c8a9a5SSteve French 275238c8a9a5SSteve French if (!atomic_dec_and_test(&tlink->tl_count) || 275338c8a9a5SSteve French test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) { 275438c8a9a5SSteve French tlink->tl_time = jiffies; 275538c8a9a5SSteve French return; 275638c8a9a5SSteve French } 275738c8a9a5SSteve French 275838c8a9a5SSteve French if (!IS_ERR(tlink_tcon(tlink))) 275938c8a9a5SSteve French cifs_put_tcon(tlink_tcon(tlink)); 276038c8a9a5SSteve French kfree(tlink); 276138c8a9a5SSteve French return; 276238c8a9a5SSteve French } 276338c8a9a5SSteve French 276438c8a9a5SSteve French static int 276538c8a9a5SSteve French compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data) 276638c8a9a5SSteve French { 276738c8a9a5SSteve French struct cifs_sb_info *old = CIFS_SB(sb); 276838c8a9a5SSteve French struct cifs_sb_info *new = mnt_data->cifs_sb; 276938c8a9a5SSteve French unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK; 277038c8a9a5SSteve French unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK; 277138c8a9a5SSteve French 277238c8a9a5SSteve French if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK)) 277338c8a9a5SSteve French return 0; 277438c8a9a5SSteve French 277538c8a9a5SSteve French if (old->mnt_cifs_serverino_autodisabled) 277638c8a9a5SSteve French newflags &= ~CIFS_MOUNT_SERVER_INUM; 277738c8a9a5SSteve French 277838c8a9a5SSteve French if (oldflags != newflags) 277938c8a9a5SSteve French return 0; 278038c8a9a5SSteve French 278138c8a9a5SSteve French /* 278238c8a9a5SSteve French * We want to share sb only if we don't specify an r/wsize or 278338c8a9a5SSteve French * specified r/wsize is greater than or equal to existing one. 278438c8a9a5SSteve French */ 278538c8a9a5SSteve French if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize) 278638c8a9a5SSteve French return 0; 278738c8a9a5SSteve French 278838c8a9a5SSteve French if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize) 278938c8a9a5SSteve French return 0; 279038c8a9a5SSteve French 279138c8a9a5SSteve French if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) || 279238c8a9a5SSteve French !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid)) 279338c8a9a5SSteve French return 0; 279438c8a9a5SSteve French 279538c8a9a5SSteve French if (old->ctx->file_mode != new->ctx->file_mode || 279638c8a9a5SSteve French old->ctx->dir_mode != new->ctx->dir_mode) 279738c8a9a5SSteve French return 0; 279838c8a9a5SSteve French 279938c8a9a5SSteve French if (strcmp(old->local_nls->charset, new->local_nls->charset)) 280038c8a9a5SSteve French return 0; 280138c8a9a5SSteve French 280238c8a9a5SSteve French if (old->ctx->acregmax != new->ctx->acregmax) 280338c8a9a5SSteve French return 0; 280438c8a9a5SSteve French if (old->ctx->acdirmax != new->ctx->acdirmax) 280538c8a9a5SSteve French return 0; 280638c8a9a5SSteve French if (old->ctx->closetimeo != new->ctx->closetimeo) 280738c8a9a5SSteve French return 0; 280838c8a9a5SSteve French 280938c8a9a5SSteve French return 1; 281038c8a9a5SSteve French } 281138c8a9a5SSteve French 281238c8a9a5SSteve French static int match_prepath(struct super_block *sb, 28133ae872deSPaulo Alcantara struct cifs_tcon *tcon, 281438c8a9a5SSteve French struct cifs_mnt_data *mnt_data) 281538c8a9a5SSteve French { 281638c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_data->ctx; 281738c8a9a5SSteve French struct cifs_sb_info *old = CIFS_SB(sb); 281838c8a9a5SSteve French struct cifs_sb_info *new = mnt_data->cifs_sb; 281938c8a9a5SSteve French bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && 282038c8a9a5SSteve French old->prepath; 282138c8a9a5SSteve French bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && 282238c8a9a5SSteve French new->prepath; 282338c8a9a5SSteve French 28243ae872deSPaulo Alcantara if (tcon->origin_fullpath && 28253ae872deSPaulo Alcantara dfs_src_pathname_equal(tcon->origin_fullpath, ctx->source)) 282638c8a9a5SSteve French return 1; 282738c8a9a5SSteve French 282838c8a9a5SSteve French if (old_set && new_set && !strcmp(new->prepath, old->prepath)) 282938c8a9a5SSteve French return 1; 283038c8a9a5SSteve French else if (!old_set && !new_set) 283138c8a9a5SSteve French return 1; 283238c8a9a5SSteve French 283338c8a9a5SSteve French return 0; 283438c8a9a5SSteve French } 283538c8a9a5SSteve French 283638c8a9a5SSteve French int 283738c8a9a5SSteve French cifs_match_super(struct super_block *sb, void *data) 283838c8a9a5SSteve French { 283938c8a9a5SSteve French struct cifs_mnt_data *mnt_data = data; 284038c8a9a5SSteve French struct smb3_fs_context *ctx; 284138c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 284238c8a9a5SSteve French struct TCP_Server_Info *tcp_srv; 284338c8a9a5SSteve French struct cifs_ses *ses; 284438c8a9a5SSteve French struct cifs_tcon *tcon; 284538c8a9a5SSteve French struct tcon_link *tlink; 284638c8a9a5SSteve French int rc = 0; 284738c8a9a5SSteve French 284838c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 284938c8a9a5SSteve French cifs_sb = CIFS_SB(sb); 285038c8a9a5SSteve French 285138c8a9a5SSteve French /* We do not want to use a superblock that has been shutdown */ 285238c8a9a5SSteve French if (CIFS_MOUNT_SHUTDOWN & cifs_sb->mnt_cifs_flags) { 285338c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 285438c8a9a5SSteve French return 0; 285538c8a9a5SSteve French } 285638c8a9a5SSteve French 285738c8a9a5SSteve French tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); 2858fc1bd51dSPaulo Alcantara if (IS_ERR_OR_NULL(tlink)) { 2859fc1bd51dSPaulo Alcantara pr_warn_once("%s: skip super matching due to bad tlink(%p)\n", 2860fc1bd51dSPaulo Alcantara __func__, tlink); 286138c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 286238c8a9a5SSteve French return 0; 286338c8a9a5SSteve French } 286438c8a9a5SSteve French tcon = tlink_tcon(tlink); 286538c8a9a5SSteve French ses = tcon->ses; 286638c8a9a5SSteve French tcp_srv = ses->server; 286738c8a9a5SSteve French 286838c8a9a5SSteve French ctx = mnt_data->ctx; 286938c8a9a5SSteve French 287038c8a9a5SSteve French spin_lock(&tcp_srv->srv_lock); 287138c8a9a5SSteve French spin_lock(&ses->ses_lock); 287238c8a9a5SSteve French spin_lock(&ses->chan_lock); 287338c8a9a5SSteve French spin_lock(&tcon->tc_lock); 28743ae872deSPaulo Alcantara if (!match_server(tcp_srv, ctx, true) || 287538c8a9a5SSteve French !match_session(ses, ctx) || 287638c8a9a5SSteve French !match_tcon(tcon, ctx) || 28773ae872deSPaulo Alcantara !match_prepath(sb, tcon, mnt_data)) { 287838c8a9a5SSteve French rc = 0; 287938c8a9a5SSteve French goto out; 288038c8a9a5SSteve French } 288138c8a9a5SSteve French 288238c8a9a5SSteve French rc = compare_mount_options(sb, mnt_data); 288338c8a9a5SSteve French out: 288438c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 288538c8a9a5SSteve French spin_unlock(&ses->chan_lock); 288638c8a9a5SSteve French spin_unlock(&ses->ses_lock); 288738c8a9a5SSteve French spin_unlock(&tcp_srv->srv_lock); 288838c8a9a5SSteve French 288938c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 289038c8a9a5SSteve French cifs_put_tlink(tlink); 289138c8a9a5SSteve French return rc; 289238c8a9a5SSteve French } 289338c8a9a5SSteve French 289438c8a9a5SSteve French #ifdef CONFIG_DEBUG_LOCK_ALLOC 289538c8a9a5SSteve French static struct lock_class_key cifs_key[2]; 289638c8a9a5SSteve French static struct lock_class_key cifs_slock_key[2]; 289738c8a9a5SSteve French 289838c8a9a5SSteve French static inline void 289938c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock) 290038c8a9a5SSteve French { 290138c8a9a5SSteve French struct sock *sk = sock->sk; 290238c8a9a5SSteve French BUG_ON(!sock_allow_reclassification(sk)); 290338c8a9a5SSteve French sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS", 290438c8a9a5SSteve French &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]); 290538c8a9a5SSteve French } 290638c8a9a5SSteve French 290738c8a9a5SSteve French static inline void 290838c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock) 290938c8a9a5SSteve French { 291038c8a9a5SSteve French struct sock *sk = sock->sk; 291138c8a9a5SSteve French BUG_ON(!sock_allow_reclassification(sk)); 291238c8a9a5SSteve French sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS", 291338c8a9a5SSteve French &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]); 291438c8a9a5SSteve French } 291538c8a9a5SSteve French #else 291638c8a9a5SSteve French static inline void 291738c8a9a5SSteve French cifs_reclassify_socket4(struct socket *sock) 291838c8a9a5SSteve French { 291938c8a9a5SSteve French } 292038c8a9a5SSteve French 292138c8a9a5SSteve French static inline void 292238c8a9a5SSteve French cifs_reclassify_socket6(struct socket *sock) 292338c8a9a5SSteve French { 292438c8a9a5SSteve French } 292538c8a9a5SSteve French #endif 292638c8a9a5SSteve French 292738c8a9a5SSteve French /* See RFC1001 section 14 on representation of Netbios names */ 292838c8a9a5SSteve French static void rfc1002mangle(char *target, char *source, unsigned int length) 292938c8a9a5SSteve French { 293038c8a9a5SSteve French unsigned int i, j; 293138c8a9a5SSteve French 293238c8a9a5SSteve French for (i = 0, j = 0; i < (length); i++) { 293338c8a9a5SSteve French /* mask a nibble at a time and encode */ 293438c8a9a5SSteve French target[j] = 'A' + (0x0F & (source[i] >> 4)); 293538c8a9a5SSteve French target[j+1] = 'A' + (0x0F & source[i]); 293638c8a9a5SSteve French j += 2; 293738c8a9a5SSteve French } 293838c8a9a5SSteve French 293938c8a9a5SSteve French } 294038c8a9a5SSteve French 294138c8a9a5SSteve French static int 294238c8a9a5SSteve French bind_socket(struct TCP_Server_Info *server) 294338c8a9a5SSteve French { 294438c8a9a5SSteve French int rc = 0; 294538c8a9a5SSteve French if (server->srcaddr.ss_family != AF_UNSPEC) { 294638c8a9a5SSteve French /* Bind to the specified local IP address */ 294738c8a9a5SSteve French struct socket *socket = server->ssocket; 2948cedc019bSJordan Rife rc = kernel_bind(socket, 294938c8a9a5SSteve French (struct sockaddr *) &server->srcaddr, 295038c8a9a5SSteve French sizeof(server->srcaddr)); 295138c8a9a5SSteve French if (rc < 0) { 295238c8a9a5SSteve French struct sockaddr_in *saddr4; 295338c8a9a5SSteve French struct sockaddr_in6 *saddr6; 295438c8a9a5SSteve French saddr4 = (struct sockaddr_in *)&server->srcaddr; 295538c8a9a5SSteve French saddr6 = (struct sockaddr_in6 *)&server->srcaddr; 295638c8a9a5SSteve French if (saddr6->sin6_family == AF_INET6) 295738c8a9a5SSteve French cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n", 295838c8a9a5SSteve French &saddr6->sin6_addr, rc); 295938c8a9a5SSteve French else 296038c8a9a5SSteve French cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n", 296138c8a9a5SSteve French &saddr4->sin_addr.s_addr, rc); 296238c8a9a5SSteve French } 296338c8a9a5SSteve French } 296438c8a9a5SSteve French return rc; 296538c8a9a5SSteve French } 296638c8a9a5SSteve French 296738c8a9a5SSteve French static int 296838c8a9a5SSteve French ip_rfc1001_connect(struct TCP_Server_Info *server) 296938c8a9a5SSteve French { 297038c8a9a5SSteve French int rc = 0; 297138c8a9a5SSteve French /* 297238c8a9a5SSteve French * some servers require RFC1001 sessinit before sending 297338c8a9a5SSteve French * negprot - BB check reconnection in case where second 297438c8a9a5SSteve French * sessinit is sent but no second negprot 297538c8a9a5SSteve French */ 297638c8a9a5SSteve French struct rfc1002_session_packet req = {}; 297738c8a9a5SSteve French struct smb_hdr *smb_buf = (struct smb_hdr *)&req; 297838c8a9a5SSteve French unsigned int len; 297938c8a9a5SSteve French 298038c8a9a5SSteve French req.trailer.session_req.called_len = sizeof(req.trailer.session_req.called_name); 298138c8a9a5SSteve French 298238c8a9a5SSteve French if (server->server_RFC1001_name[0] != 0) 298338c8a9a5SSteve French rfc1002mangle(req.trailer.session_req.called_name, 298438c8a9a5SSteve French server->server_RFC1001_name, 298538c8a9a5SSteve French RFC1001_NAME_LEN_WITH_NULL); 298638c8a9a5SSteve French else 298738c8a9a5SSteve French rfc1002mangle(req.trailer.session_req.called_name, 298838c8a9a5SSteve French DEFAULT_CIFS_CALLED_NAME, 298938c8a9a5SSteve French RFC1001_NAME_LEN_WITH_NULL); 299038c8a9a5SSteve French 299138c8a9a5SSteve French req.trailer.session_req.calling_len = sizeof(req.trailer.session_req.calling_name); 299238c8a9a5SSteve French 299338c8a9a5SSteve French /* calling name ends in null (byte 16) from old smb convention */ 299438c8a9a5SSteve French if (server->workstation_RFC1001_name[0] != 0) 299538c8a9a5SSteve French rfc1002mangle(req.trailer.session_req.calling_name, 299638c8a9a5SSteve French server->workstation_RFC1001_name, 299738c8a9a5SSteve French RFC1001_NAME_LEN_WITH_NULL); 299838c8a9a5SSteve French else 299938c8a9a5SSteve French rfc1002mangle(req.trailer.session_req.calling_name, 300038c8a9a5SSteve French "LINUX_CIFS_CLNT", 300138c8a9a5SSteve French RFC1001_NAME_LEN_WITH_NULL); 300238c8a9a5SSteve French 300338c8a9a5SSteve French /* 300438c8a9a5SSteve French * As per rfc1002, @len must be the number of bytes that follows the 300538c8a9a5SSteve French * length field of a rfc1002 session request payload. 300638c8a9a5SSteve French */ 300738c8a9a5SSteve French len = sizeof(req) - offsetof(struct rfc1002_session_packet, trailer.session_req); 300838c8a9a5SSteve French 300938c8a9a5SSteve French smb_buf->smb_buf_length = cpu_to_be32((RFC1002_SESSION_REQUEST << 24) | len); 301038c8a9a5SSteve French rc = smb_send(server, smb_buf, len); 301138c8a9a5SSteve French /* 301238c8a9a5SSteve French * RFC1001 layer in at least one server requires very short break before 301338c8a9a5SSteve French * negprot presumably because not expecting negprot to follow so fast. 301438c8a9a5SSteve French * This is a simple solution that works without complicating the code 301538c8a9a5SSteve French * and causes no significant slowing down on mount for everyone else 301638c8a9a5SSteve French */ 301738c8a9a5SSteve French usleep_range(1000, 2000); 301838c8a9a5SSteve French 301938c8a9a5SSteve French return rc; 302038c8a9a5SSteve French } 302138c8a9a5SSteve French 302238c8a9a5SSteve French static int 302338c8a9a5SSteve French generic_ip_connect(struct TCP_Server_Info *server) 302438c8a9a5SSteve French { 302538c8a9a5SSteve French struct sockaddr *saddr; 3026f0b6a834SPaulo Alcantara struct socket *socket; 3027f0b6a834SPaulo Alcantara int slen, sfamily; 3028f0b6a834SPaulo Alcantara __be16 sport; 3029f0b6a834SPaulo Alcantara int rc = 0; 303038c8a9a5SSteve French 303138c8a9a5SSteve French saddr = (struct sockaddr *) &server->dstaddr; 303238c8a9a5SSteve French 303338c8a9a5SSteve French if (server->dstaddr.ss_family == AF_INET6) { 303438c8a9a5SSteve French struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr; 303538c8a9a5SSteve French 303638c8a9a5SSteve French sport = ipv6->sin6_port; 303738c8a9a5SSteve French slen = sizeof(struct sockaddr_in6); 303838c8a9a5SSteve French sfamily = AF_INET6; 303938c8a9a5SSteve French cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr, 304038c8a9a5SSteve French ntohs(sport)); 304138c8a9a5SSteve French } else { 304238c8a9a5SSteve French struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr; 304338c8a9a5SSteve French 304438c8a9a5SSteve French sport = ipv4->sin_port; 304538c8a9a5SSteve French slen = sizeof(struct sockaddr_in); 304638c8a9a5SSteve French sfamily = AF_INET; 304738c8a9a5SSteve French cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr, 304838c8a9a5SSteve French ntohs(sport)); 304938c8a9a5SSteve French } 305038c8a9a5SSteve French 3051f0b6a834SPaulo Alcantara if (server->ssocket) { 3052f0b6a834SPaulo Alcantara socket = server->ssocket; 3053f0b6a834SPaulo Alcantara } else { 305438c8a9a5SSteve French rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM, 3055f0b6a834SPaulo Alcantara IPPROTO_TCP, &server->ssocket, 1); 305638c8a9a5SSteve French if (rc < 0) { 305738c8a9a5SSteve French cifs_server_dbg(VFS, "Error %d creating socket\n", rc); 305838c8a9a5SSteve French return rc; 305938c8a9a5SSteve French } 306038c8a9a5SSteve French 306138c8a9a5SSteve French /* BB other socket options to set KEEPALIVE, NODELAY? */ 306238c8a9a5SSteve French cifs_dbg(FYI, "Socket created\n"); 3063f0b6a834SPaulo Alcantara socket = server->ssocket; 306438c8a9a5SSteve French socket->sk->sk_allocation = GFP_NOFS; 306538c8a9a5SSteve French socket->sk->sk_use_task_frag = false; 306638c8a9a5SSteve French if (sfamily == AF_INET6) 306738c8a9a5SSteve French cifs_reclassify_socket6(socket); 306838c8a9a5SSteve French else 306938c8a9a5SSteve French cifs_reclassify_socket4(socket); 307038c8a9a5SSteve French } 307138c8a9a5SSteve French 307238c8a9a5SSteve French rc = bind_socket(server); 307338c8a9a5SSteve French if (rc < 0) 307438c8a9a5SSteve French return rc; 307538c8a9a5SSteve French 307638c8a9a5SSteve French /* 307738c8a9a5SSteve French * Eventually check for other socket options to change from 307838c8a9a5SSteve French * the default. sock_setsockopt not used because it expects 307938c8a9a5SSteve French * user space buffer 308038c8a9a5SSteve French */ 308138c8a9a5SSteve French socket->sk->sk_rcvtimeo = 7 * HZ; 308238c8a9a5SSteve French socket->sk->sk_sndtimeo = 5 * HZ; 308338c8a9a5SSteve French 308438c8a9a5SSteve French /* make the bufsizes depend on wsize/rsize and max requests */ 308538c8a9a5SSteve French if (server->noautotune) { 308638c8a9a5SSteve French if (socket->sk->sk_sndbuf < (200 * 1024)) 308738c8a9a5SSteve French socket->sk->sk_sndbuf = 200 * 1024; 308838c8a9a5SSteve French if (socket->sk->sk_rcvbuf < (140 * 1024)) 308938c8a9a5SSteve French socket->sk->sk_rcvbuf = 140 * 1024; 309038c8a9a5SSteve French } 309138c8a9a5SSteve French 309238c8a9a5SSteve French if (server->tcp_nodelay) 309338c8a9a5SSteve French tcp_sock_set_nodelay(socket->sk); 309438c8a9a5SSteve French 309538c8a9a5SSteve French cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n", 309638c8a9a5SSteve French socket->sk->sk_sndbuf, 309738c8a9a5SSteve French socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo); 309838c8a9a5SSteve French 3099cedc019bSJordan Rife rc = kernel_connect(socket, saddr, slen, 310038c8a9a5SSteve French server->noblockcnt ? O_NONBLOCK : 0); 310138c8a9a5SSteve French /* 310238c8a9a5SSteve French * When mounting SMB root file systems, we do not want to block in 310338c8a9a5SSteve French * connect. Otherwise bail out and then let cifs_reconnect() perform 310438c8a9a5SSteve French * reconnect failover - if possible. 310538c8a9a5SSteve French */ 310638c8a9a5SSteve French if (server->noblockcnt && rc == -EINPROGRESS) 310738c8a9a5SSteve French rc = 0; 310838c8a9a5SSteve French if (rc < 0) { 310938c8a9a5SSteve French cifs_dbg(FYI, "Error %d connecting to server\n", rc); 311038c8a9a5SSteve French trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc); 311138c8a9a5SSteve French sock_release(socket); 311238c8a9a5SSteve French server->ssocket = NULL; 311338c8a9a5SSteve French return rc; 311438c8a9a5SSteve French } 311538c8a9a5SSteve French trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr); 311638c8a9a5SSteve French if (sport == htons(RFC1001_PORT)) 311738c8a9a5SSteve French rc = ip_rfc1001_connect(server); 311838c8a9a5SSteve French 311938c8a9a5SSteve French return rc; 312038c8a9a5SSteve French } 312138c8a9a5SSteve French 312238c8a9a5SSteve French static int 312338c8a9a5SSteve French ip_connect(struct TCP_Server_Info *server) 312438c8a9a5SSteve French { 312538c8a9a5SSteve French __be16 *sport; 312638c8a9a5SSteve French struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; 312738c8a9a5SSteve French struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; 312838c8a9a5SSteve French 312938c8a9a5SSteve French if (server->dstaddr.ss_family == AF_INET6) 313038c8a9a5SSteve French sport = &addr6->sin6_port; 313138c8a9a5SSteve French else 313238c8a9a5SSteve French sport = &addr->sin_port; 313338c8a9a5SSteve French 313438c8a9a5SSteve French if (*sport == 0) { 313538c8a9a5SSteve French int rc; 313638c8a9a5SSteve French 313738c8a9a5SSteve French /* try with 445 port at first */ 313838c8a9a5SSteve French *sport = htons(CIFS_PORT); 313938c8a9a5SSteve French 314038c8a9a5SSteve French rc = generic_ip_connect(server); 314138c8a9a5SSteve French if (rc >= 0) 314238c8a9a5SSteve French return rc; 314338c8a9a5SSteve French 314438c8a9a5SSteve French /* if it failed, try with 139 port */ 314538c8a9a5SSteve French *sport = htons(RFC1001_PORT); 314638c8a9a5SSteve French } 314738c8a9a5SSteve French 314838c8a9a5SSteve French return generic_ip_connect(server); 314938c8a9a5SSteve French } 315038c8a9a5SSteve French 315138c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 315238c8a9a5SSteve French void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon, 315338c8a9a5SSteve French struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) 315438c8a9a5SSteve French { 315538c8a9a5SSteve French /* 315638c8a9a5SSteve French * If we are reconnecting then should we check to see if 315738c8a9a5SSteve French * any requested capabilities changed locally e.g. via 315838c8a9a5SSteve French * remount but we can not do much about it here 315938c8a9a5SSteve French * if they have (even if we could detect it by the following) 316038c8a9a5SSteve French * Perhaps we could add a backpointer to array of sb from tcon 316138c8a9a5SSteve French * or if we change to make all sb to same share the same 316238c8a9a5SSteve French * sb as NFS - then we only have one backpointer to sb. 316338c8a9a5SSteve French * What if we wanted to mount the server share twice once with 316438c8a9a5SSteve French * and once without posixacls or posix paths? 316538c8a9a5SSteve French */ 316638c8a9a5SSteve French __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability); 316738c8a9a5SSteve French 316838c8a9a5SSteve French if (ctx && ctx->no_linux_ext) { 316938c8a9a5SSteve French tcon->fsUnixInfo.Capability = 0; 317038c8a9a5SSteve French tcon->unix_ext = 0; /* Unix Extensions disabled */ 317138c8a9a5SSteve French cifs_dbg(FYI, "Linux protocol extensions disabled\n"); 317238c8a9a5SSteve French return; 317338c8a9a5SSteve French } else if (ctx) 317438c8a9a5SSteve French tcon->unix_ext = 1; /* Unix Extensions supported */ 317538c8a9a5SSteve French 317638c8a9a5SSteve French if (!tcon->unix_ext) { 317738c8a9a5SSteve French cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n"); 317838c8a9a5SSteve French return; 317938c8a9a5SSteve French } 318038c8a9a5SSteve French 318138c8a9a5SSteve French if (!CIFSSMBQFSUnixInfo(xid, tcon)) { 318238c8a9a5SSteve French __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability); 318338c8a9a5SSteve French cifs_dbg(FYI, "unix caps which server supports %lld\n", cap); 318438c8a9a5SSteve French /* 318538c8a9a5SSteve French * check for reconnect case in which we do not 318638c8a9a5SSteve French * want to change the mount behavior if we can avoid it 318738c8a9a5SSteve French */ 318838c8a9a5SSteve French if (ctx == NULL) { 318938c8a9a5SSteve French /* 319038c8a9a5SSteve French * turn off POSIX ACL and PATHNAMES if not set 319138c8a9a5SSteve French * originally at mount time 319238c8a9a5SSteve French */ 319338c8a9a5SSteve French if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0) 319438c8a9a5SSteve French cap &= ~CIFS_UNIX_POSIX_ACL_CAP; 319538c8a9a5SSteve French if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { 319638c8a9a5SSteve French if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) 319738c8a9a5SSteve French cifs_dbg(VFS, "POSIXPATH support change\n"); 319838c8a9a5SSteve French cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; 319938c8a9a5SSteve French } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { 320038c8a9a5SSteve French cifs_dbg(VFS, "possible reconnect error\n"); 320138c8a9a5SSteve French cifs_dbg(VFS, "server disabled POSIX path support\n"); 320238c8a9a5SSteve French } 320338c8a9a5SSteve French } 320438c8a9a5SSteve French 320538c8a9a5SSteve French if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) 320638c8a9a5SSteve French cifs_dbg(VFS, "per-share encryption not supported yet\n"); 320738c8a9a5SSteve French 320838c8a9a5SSteve French cap &= CIFS_UNIX_CAP_MASK; 320938c8a9a5SSteve French if (ctx && ctx->no_psx_acl) 321038c8a9a5SSteve French cap &= ~CIFS_UNIX_POSIX_ACL_CAP; 321138c8a9a5SSteve French else if (CIFS_UNIX_POSIX_ACL_CAP & cap) { 321238c8a9a5SSteve French cifs_dbg(FYI, "negotiated posix acl support\n"); 321338c8a9a5SSteve French if (cifs_sb) 321438c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= 321538c8a9a5SSteve French CIFS_MOUNT_POSIXACL; 321638c8a9a5SSteve French } 321738c8a9a5SSteve French 321838c8a9a5SSteve French if (ctx && ctx->posix_paths == 0) 321938c8a9a5SSteve French cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; 322038c8a9a5SSteve French else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) { 322138c8a9a5SSteve French cifs_dbg(FYI, "negotiate posix pathnames\n"); 322238c8a9a5SSteve French if (cifs_sb) 322338c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= 322438c8a9a5SSteve French CIFS_MOUNT_POSIX_PATHS; 322538c8a9a5SSteve French } 322638c8a9a5SSteve French 322738c8a9a5SSteve French cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap); 322838c8a9a5SSteve French #ifdef CONFIG_CIFS_DEBUG2 322938c8a9a5SSteve French if (cap & CIFS_UNIX_FCNTL_CAP) 323038c8a9a5SSteve French cifs_dbg(FYI, "FCNTL cap\n"); 323138c8a9a5SSteve French if (cap & CIFS_UNIX_EXTATTR_CAP) 323238c8a9a5SSteve French cifs_dbg(FYI, "EXTATTR cap\n"); 323338c8a9a5SSteve French if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) 323438c8a9a5SSteve French cifs_dbg(FYI, "POSIX path cap\n"); 323538c8a9a5SSteve French if (cap & CIFS_UNIX_XATTR_CAP) 323638c8a9a5SSteve French cifs_dbg(FYI, "XATTR cap\n"); 323738c8a9a5SSteve French if (cap & CIFS_UNIX_POSIX_ACL_CAP) 323838c8a9a5SSteve French cifs_dbg(FYI, "POSIX ACL cap\n"); 323938c8a9a5SSteve French if (cap & CIFS_UNIX_LARGE_READ_CAP) 324038c8a9a5SSteve French cifs_dbg(FYI, "very large read cap\n"); 324138c8a9a5SSteve French if (cap & CIFS_UNIX_LARGE_WRITE_CAP) 324238c8a9a5SSteve French cifs_dbg(FYI, "very large write cap\n"); 324338c8a9a5SSteve French if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) 324438c8a9a5SSteve French cifs_dbg(FYI, "transport encryption cap\n"); 324538c8a9a5SSteve French if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) 324638c8a9a5SSteve French cifs_dbg(FYI, "mandatory transport encryption cap\n"); 324738c8a9a5SSteve French #endif /* CIFS_DEBUG2 */ 324838c8a9a5SSteve French if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) { 324938c8a9a5SSteve French if (ctx == NULL) 325038c8a9a5SSteve French cifs_dbg(FYI, "resetting capabilities failed\n"); 325138c8a9a5SSteve French else 325238c8a9a5SSteve French 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"); 325338c8a9a5SSteve French 325438c8a9a5SSteve French } 325538c8a9a5SSteve French } 325638c8a9a5SSteve French } 325738c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 325838c8a9a5SSteve French 325938c8a9a5SSteve French int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb) 326038c8a9a5SSteve French { 326138c8a9a5SSteve French struct smb3_fs_context *ctx = cifs_sb->ctx; 326238c8a9a5SSteve French 326338c8a9a5SSteve French INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks); 326438c8a9a5SSteve French 326538c8a9a5SSteve French spin_lock_init(&cifs_sb->tlink_tree_lock); 326638c8a9a5SSteve French cifs_sb->tlink_tree = RB_ROOT; 326738c8a9a5SSteve French 326838c8a9a5SSteve French cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n", 326938c8a9a5SSteve French ctx->file_mode, ctx->dir_mode); 327038c8a9a5SSteve French 327138c8a9a5SSteve French /* this is needed for ASCII cp to Unicode converts */ 327238c8a9a5SSteve French if (ctx->iocharset == NULL) { 327338c8a9a5SSteve French /* load_nls_default cannot return null */ 327438c8a9a5SSteve French cifs_sb->local_nls = load_nls_default(); 327538c8a9a5SSteve French } else { 327638c8a9a5SSteve French cifs_sb->local_nls = load_nls(ctx->iocharset); 327738c8a9a5SSteve French if (cifs_sb->local_nls == NULL) { 327838c8a9a5SSteve French cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n", 327938c8a9a5SSteve French ctx->iocharset); 328038c8a9a5SSteve French return -ELIBACC; 328138c8a9a5SSteve French } 328238c8a9a5SSteve French } 328338c8a9a5SSteve French ctx->local_nls = cifs_sb->local_nls; 328438c8a9a5SSteve French 328538c8a9a5SSteve French smb3_update_mnt_flags(cifs_sb); 328638c8a9a5SSteve French 328738c8a9a5SSteve French if (ctx->direct_io) 328838c8a9a5SSteve French cifs_dbg(FYI, "mounting share using direct i/o\n"); 328938c8a9a5SSteve French if (ctx->cache_ro) { 329038c8a9a5SSteve French cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n"); 329138c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE; 329238c8a9a5SSteve French } else if (ctx->cache_rw) { 329338c8a9a5SSteve French cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n"); 329438c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE | 329538c8a9a5SSteve French CIFS_MOUNT_RW_CACHE); 329638c8a9a5SSteve French } 329738c8a9a5SSteve French 329838c8a9a5SSteve French if ((ctx->cifs_acl) && (ctx->dynperm)) 329938c8a9a5SSteve French cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n"); 330038c8a9a5SSteve French 330138c8a9a5SSteve French if (ctx->prepath) { 330238c8a9a5SSteve French cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL); 330338c8a9a5SSteve French if (cifs_sb->prepath == NULL) 330438c8a9a5SSteve French return -ENOMEM; 330538c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 330638c8a9a5SSteve French } 330738c8a9a5SSteve French 330838c8a9a5SSteve French return 0; 330938c8a9a5SSteve French } 331038c8a9a5SSteve French 331138c8a9a5SSteve French /* Release all succeed connections */ 331238c8a9a5SSteve French void cifs_mount_put_conns(struct cifs_mount_ctx *mnt_ctx) 331338c8a9a5SSteve French { 331438c8a9a5SSteve French int rc = 0; 331538c8a9a5SSteve French 331638c8a9a5SSteve French if (mnt_ctx->tcon) 331738c8a9a5SSteve French cifs_put_tcon(mnt_ctx->tcon); 331838c8a9a5SSteve French else if (mnt_ctx->ses) 331938c8a9a5SSteve French cifs_put_smb_ses(mnt_ctx->ses); 332038c8a9a5SSteve French else if (mnt_ctx->server) 332138c8a9a5SSteve French cifs_put_tcp_session(mnt_ctx->server, 0); 332238c8a9a5SSteve French mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS; 332338c8a9a5SSteve French free_xid(mnt_ctx->xid); 332438c8a9a5SSteve French } 332538c8a9a5SSteve French 332638c8a9a5SSteve French int cifs_mount_get_session(struct cifs_mount_ctx *mnt_ctx) 332738c8a9a5SSteve French { 332838c8a9a5SSteve French struct TCP_Server_Info *server = NULL; 332938c8a9a5SSteve French struct smb3_fs_context *ctx; 333038c8a9a5SSteve French struct cifs_ses *ses = NULL; 333138c8a9a5SSteve French unsigned int xid; 333238c8a9a5SSteve French int rc = 0; 333338c8a9a5SSteve French 333438c8a9a5SSteve French xid = get_xid(); 333538c8a9a5SSteve French 333638c8a9a5SSteve French if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->fs_ctx)) { 333738c8a9a5SSteve French rc = -EINVAL; 333838c8a9a5SSteve French goto out; 333938c8a9a5SSteve French } 334038c8a9a5SSteve French ctx = mnt_ctx->fs_ctx; 334138c8a9a5SSteve French 334238c8a9a5SSteve French /* get a reference to a tcp session */ 334338c8a9a5SSteve French server = cifs_get_tcp_session(ctx, NULL); 334438c8a9a5SSteve French if (IS_ERR(server)) { 334538c8a9a5SSteve French rc = PTR_ERR(server); 334638c8a9a5SSteve French server = NULL; 334738c8a9a5SSteve French goto out; 334838c8a9a5SSteve French } 334938c8a9a5SSteve French 335038c8a9a5SSteve French /* get a reference to a SMB session */ 335138c8a9a5SSteve French ses = cifs_get_smb_ses(server, ctx); 335238c8a9a5SSteve French if (IS_ERR(ses)) { 335338c8a9a5SSteve French rc = PTR_ERR(ses); 335438c8a9a5SSteve French ses = NULL; 335538c8a9a5SSteve French goto out; 335638c8a9a5SSteve French } 335738c8a9a5SSteve French 335838c8a9a5SSteve French if ((ctx->persistent == true) && (!(ses->server->capabilities & 335938c8a9a5SSteve French SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) { 336038c8a9a5SSteve French cifs_server_dbg(VFS, "persistent handles not supported by server\n"); 336138c8a9a5SSteve French rc = -EOPNOTSUPP; 336238c8a9a5SSteve French } 336338c8a9a5SSteve French 336438c8a9a5SSteve French out: 336538c8a9a5SSteve French mnt_ctx->xid = xid; 336638c8a9a5SSteve French mnt_ctx->server = server; 336738c8a9a5SSteve French mnt_ctx->ses = ses; 336838c8a9a5SSteve French mnt_ctx->tcon = NULL; 336938c8a9a5SSteve French 337038c8a9a5SSteve French return rc; 337138c8a9a5SSteve French } 337238c8a9a5SSteve French 337338c8a9a5SSteve French int cifs_mount_get_tcon(struct cifs_mount_ctx *mnt_ctx) 337438c8a9a5SSteve French { 337538c8a9a5SSteve French struct TCP_Server_Info *server; 337638c8a9a5SSteve French struct cifs_sb_info *cifs_sb; 337738c8a9a5SSteve French struct smb3_fs_context *ctx; 337838c8a9a5SSteve French struct cifs_tcon *tcon = NULL; 337938c8a9a5SSteve French int rc = 0; 338038c8a9a5SSteve French 338138c8a9a5SSteve French if (WARN_ON_ONCE(!mnt_ctx || !mnt_ctx->server || !mnt_ctx->ses || !mnt_ctx->fs_ctx || 338238c8a9a5SSteve French !mnt_ctx->cifs_sb)) { 338338c8a9a5SSteve French rc = -EINVAL; 338438c8a9a5SSteve French goto out; 338538c8a9a5SSteve French } 338638c8a9a5SSteve French server = mnt_ctx->server; 338738c8a9a5SSteve French ctx = mnt_ctx->fs_ctx; 338838c8a9a5SSteve French cifs_sb = mnt_ctx->cifs_sb; 338938c8a9a5SSteve French 339038c8a9a5SSteve French /* search for existing tcon to this server share */ 339138c8a9a5SSteve French tcon = cifs_get_tcon(mnt_ctx->ses, ctx); 339238c8a9a5SSteve French if (IS_ERR(tcon)) { 339338c8a9a5SSteve French rc = PTR_ERR(tcon); 339438c8a9a5SSteve French tcon = NULL; 339538c8a9a5SSteve French goto out; 339638c8a9a5SSteve French } 339738c8a9a5SSteve French 339838c8a9a5SSteve French /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */ 339938c8a9a5SSteve French if (tcon->posix_extensions) 340038c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS; 340138c8a9a5SSteve French 340238c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 340338c8a9a5SSteve French /* tell server which Unix caps we support */ 340438c8a9a5SSteve French if (cap_unix(tcon->ses)) { 340538c8a9a5SSteve French /* 340638c8a9a5SSteve French * reset of caps checks mount to see if unix extensions disabled 340738c8a9a5SSteve French * for just this mount. 340838c8a9a5SSteve French */ 340938c8a9a5SSteve French reset_cifs_unix_caps(mnt_ctx->xid, tcon, cifs_sb, ctx); 341038c8a9a5SSteve French spin_lock(&tcon->ses->server->srv_lock); 341138c8a9a5SSteve French if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) && 341238c8a9a5SSteve French (le64_to_cpu(tcon->fsUnixInfo.Capability) & 341338c8a9a5SSteve French CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) { 341438c8a9a5SSteve French spin_unlock(&tcon->ses->server->srv_lock); 341538c8a9a5SSteve French rc = -EACCES; 341638c8a9a5SSteve French goto out; 341738c8a9a5SSteve French } 341838c8a9a5SSteve French spin_unlock(&tcon->ses->server->srv_lock); 341938c8a9a5SSteve French } else 342038c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 342138c8a9a5SSteve French tcon->unix_ext = 0; /* server does not support them */ 342238c8a9a5SSteve French 342338c8a9a5SSteve French /* do not care if a following call succeed - informational */ 342438c8a9a5SSteve French if (!tcon->pipe && server->ops->qfs_tcon) { 342538c8a9a5SSteve French server->ops->qfs_tcon(mnt_ctx->xid, tcon, cifs_sb); 342638c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) { 342738c8a9a5SSteve French if (tcon->fsDevInfo.DeviceCharacteristics & 342838c8a9a5SSteve French cpu_to_le32(FILE_READ_ONLY_DEVICE)) 342938c8a9a5SSteve French cifs_dbg(VFS, "mounted to read only share\n"); 343038c8a9a5SSteve French else if ((cifs_sb->mnt_cifs_flags & 343138c8a9a5SSteve French CIFS_MOUNT_RW_CACHE) == 0) 343238c8a9a5SSteve French cifs_dbg(VFS, "read only mount of RW share\n"); 343338c8a9a5SSteve French /* no need to log a RW mount of a typical RW share */ 343438c8a9a5SSteve French } 343538c8a9a5SSteve French } 343638c8a9a5SSteve French 343738c8a9a5SSteve French /* 343838c8a9a5SSteve French * Clamp the rsize/wsize mount arguments if they are too big for the server 343938c8a9a5SSteve French * and set the rsize/wsize to the negotiated values if not passed in by 344038c8a9a5SSteve French * the user on mount 344138c8a9a5SSteve French */ 344238c8a9a5SSteve French if ((cifs_sb->ctx->wsize == 0) || 34434145ccffSSteve French (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx))) { 34444145ccffSSteve French cifs_sb->ctx->wsize = 34454145ccffSSteve French round_down(server->ops->negotiate_wsize(tcon, ctx), PAGE_SIZE); 34464145ccffSSteve French /* 34474145ccffSSteve French * in the very unlikely event that the server sent a max write size under PAGE_SIZE, 34484145ccffSSteve French * (which would get rounded down to 0) then reset wsize to absolute minimum eg 4096 34494145ccffSSteve French */ 34504145ccffSSteve French if (cifs_sb->ctx->wsize == 0) { 34514145ccffSSteve French cifs_sb->ctx->wsize = PAGE_SIZE; 34524145ccffSSteve French cifs_dbg(VFS, "wsize too small, reset to minimum ie PAGE_SIZE, usually 4096\n"); 34534145ccffSSteve French } 34544145ccffSSteve French } 345538c8a9a5SSteve French if ((cifs_sb->ctx->rsize == 0) || 345638c8a9a5SSteve French (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx))) 345738c8a9a5SSteve French cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx); 345838c8a9a5SSteve French 345938c8a9a5SSteve French /* 346038c8a9a5SSteve French * The cookie is initialized from volume info returned above. 346138c8a9a5SSteve French * Inside cifs_fscache_get_super_cookie it checks 346238c8a9a5SSteve French * that we do not get super cookie twice. 346338c8a9a5SSteve French */ 346438c8a9a5SSteve French if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE) 346538c8a9a5SSteve French cifs_fscache_get_super_cookie(tcon); 346638c8a9a5SSteve French 346738c8a9a5SSteve French out: 346838c8a9a5SSteve French mnt_ctx->tcon = tcon; 346938c8a9a5SSteve French return rc; 347038c8a9a5SSteve French } 347138c8a9a5SSteve French 347238c8a9a5SSteve French static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, 347338c8a9a5SSteve French struct cifs_tcon *tcon) 347438c8a9a5SSteve French { 347538c8a9a5SSteve French struct tcon_link *tlink; 347638c8a9a5SSteve French 347738c8a9a5SSteve French /* hang the tcon off of the superblock */ 347838c8a9a5SSteve French tlink = kzalloc(sizeof(*tlink), GFP_KERNEL); 347938c8a9a5SSteve French if (tlink == NULL) 348038c8a9a5SSteve French return -ENOMEM; 348138c8a9a5SSteve French 348238c8a9a5SSteve French tlink->tl_uid = ses->linux_uid; 348338c8a9a5SSteve French tlink->tl_tcon = tcon; 348438c8a9a5SSteve French tlink->tl_time = jiffies; 348538c8a9a5SSteve French set_bit(TCON_LINK_MASTER, &tlink->tl_flags); 348638c8a9a5SSteve French set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); 348738c8a9a5SSteve French 348838c8a9a5SSteve French cifs_sb->master_tlink = tlink; 348938c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 349038c8a9a5SSteve French tlink_rb_insert(&cifs_sb->tlink_tree, tlink); 349138c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 349238c8a9a5SSteve French 349338c8a9a5SSteve French queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, 349438c8a9a5SSteve French TLINK_IDLE_EXPIRE); 349538c8a9a5SSteve French return 0; 349638c8a9a5SSteve French } 349738c8a9a5SSteve French 349838c8a9a5SSteve French static int 349938c8a9a5SSteve French cifs_are_all_path_components_accessible(struct TCP_Server_Info *server, 350038c8a9a5SSteve French unsigned int xid, 350138c8a9a5SSteve French struct cifs_tcon *tcon, 350238c8a9a5SSteve French struct cifs_sb_info *cifs_sb, 350338c8a9a5SSteve French char *full_path, 350438c8a9a5SSteve French int added_treename) 350538c8a9a5SSteve French { 350638c8a9a5SSteve French int rc; 350738c8a9a5SSteve French char *s; 350838c8a9a5SSteve French char sep, tmp; 350938c8a9a5SSteve French int skip = added_treename ? 1 : 0; 351038c8a9a5SSteve French 351138c8a9a5SSteve French sep = CIFS_DIR_SEP(cifs_sb); 351238c8a9a5SSteve French s = full_path; 351338c8a9a5SSteve French 351438c8a9a5SSteve French rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, ""); 351538c8a9a5SSteve French while (rc == 0) { 351638c8a9a5SSteve French /* skip separators */ 351738c8a9a5SSteve French while (*s == sep) 351838c8a9a5SSteve French s++; 351938c8a9a5SSteve French if (!*s) 352038c8a9a5SSteve French break; 352138c8a9a5SSteve French /* next separator */ 352238c8a9a5SSteve French while (*s && *s != sep) 352338c8a9a5SSteve French s++; 352438c8a9a5SSteve French /* 352538c8a9a5SSteve French * if the treename is added, we then have to skip the first 352638c8a9a5SSteve French * part within the separators 352738c8a9a5SSteve French */ 352838c8a9a5SSteve French if (skip) { 352938c8a9a5SSteve French skip = 0; 353038c8a9a5SSteve French continue; 353138c8a9a5SSteve French } 353238c8a9a5SSteve French /* 353338c8a9a5SSteve French * temporarily null-terminate the path at the end of 353438c8a9a5SSteve French * the current component 353538c8a9a5SSteve French */ 353638c8a9a5SSteve French tmp = *s; 353738c8a9a5SSteve French *s = 0; 353838c8a9a5SSteve French rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, 353938c8a9a5SSteve French full_path); 354038c8a9a5SSteve French *s = tmp; 354138c8a9a5SSteve French } 354238c8a9a5SSteve French return rc; 354338c8a9a5SSteve French } 354438c8a9a5SSteve French 354538c8a9a5SSteve French /* 354638c8a9a5SSteve French * Check if path is remote (i.e. a DFS share). 354738c8a9a5SSteve French * 354838c8a9a5SSteve French * Return -EREMOTE if it is, otherwise 0 or -errno. 354938c8a9a5SSteve French */ 355038c8a9a5SSteve French int cifs_is_path_remote(struct cifs_mount_ctx *mnt_ctx) 355138c8a9a5SSteve French { 355238c8a9a5SSteve French int rc; 355338c8a9a5SSteve French struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; 355438c8a9a5SSteve French struct TCP_Server_Info *server = mnt_ctx->server; 355538c8a9a5SSteve French unsigned int xid = mnt_ctx->xid; 355638c8a9a5SSteve French struct cifs_tcon *tcon = mnt_ctx->tcon; 355738c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 355838c8a9a5SSteve French char *full_path; 355938c8a9a5SSteve French 356038c8a9a5SSteve French if (!server->ops->is_path_accessible) 356138c8a9a5SSteve French return -EOPNOTSUPP; 356238c8a9a5SSteve French 356338c8a9a5SSteve French /* 356438c8a9a5SSteve French * cifs_build_path_to_root works only when we have a valid tcon 356538c8a9a5SSteve French */ 356638c8a9a5SSteve French full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon, 356738c8a9a5SSteve French tcon->Flags & SMB_SHARE_IS_IN_DFS); 356838c8a9a5SSteve French if (full_path == NULL) 356938c8a9a5SSteve French return -ENOMEM; 357038c8a9a5SSteve French 357138c8a9a5SSteve French cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path); 357238c8a9a5SSteve French 357338c8a9a5SSteve French rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, 357438c8a9a5SSteve French full_path); 357538c8a9a5SSteve French if (rc != 0 && rc != -EREMOTE) 357638c8a9a5SSteve French goto out; 357738c8a9a5SSteve French 357838c8a9a5SSteve French if (rc != -EREMOTE) { 357938c8a9a5SSteve French rc = cifs_are_all_path_components_accessible(server, xid, tcon, 358038c8a9a5SSteve French cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS); 358138c8a9a5SSteve French if (rc != 0) { 358238c8a9a5SSteve French cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n"); 358338c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 358438c8a9a5SSteve French rc = 0; 358538c8a9a5SSteve French } 358638c8a9a5SSteve French } 358738c8a9a5SSteve French 358838c8a9a5SSteve French out: 358938c8a9a5SSteve French kfree(full_path); 359038c8a9a5SSteve French return rc; 359138c8a9a5SSteve French } 359238c8a9a5SSteve French 359338c8a9a5SSteve French #ifdef CONFIG_CIFS_DFS_UPCALL 359438c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) 359538c8a9a5SSteve French { 359638c8a9a5SSteve French struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; 359738c8a9a5SSteve French bool isdfs; 359838c8a9a5SSteve French int rc; 359938c8a9a5SSteve French 360038c8a9a5SSteve French INIT_LIST_HEAD(&mnt_ctx.dfs_ses_list); 360138c8a9a5SSteve French 360238c8a9a5SSteve French rc = dfs_mount_share(&mnt_ctx, &isdfs); 360338c8a9a5SSteve French if (rc) 360438c8a9a5SSteve French goto error; 360538c8a9a5SSteve French if (!isdfs) 360638c8a9a5SSteve French goto out; 360738c8a9a5SSteve French 360838c8a9a5SSteve French /* 360938c8a9a5SSteve French * After reconnecting to a different server, unique ids won't match anymore, so we disable 361038c8a9a5SSteve French * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE). 361138c8a9a5SSteve French */ 361238c8a9a5SSteve French cifs_autodisable_serverino(cifs_sb); 361338c8a9a5SSteve French /* 361438c8a9a5SSteve French * Force the use of prefix path to support failover on DFS paths that resolve to targets 361538c8a9a5SSteve French * that have different prefix paths. 361638c8a9a5SSteve French */ 361738c8a9a5SSteve French cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; 361838c8a9a5SSteve French kfree(cifs_sb->prepath); 361938c8a9a5SSteve French cifs_sb->prepath = ctx->prepath; 362038c8a9a5SSteve French ctx->prepath = NULL; 362138c8a9a5SSteve French 362238c8a9a5SSteve French out: 3623739bf98cSShyam Prasad N cifs_try_adding_channels(mnt_ctx.ses); 362438c8a9a5SSteve French rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); 362538c8a9a5SSteve French if (rc) 362638c8a9a5SSteve French goto error; 362738c8a9a5SSteve French 362838c8a9a5SSteve French free_xid(mnt_ctx.xid); 362938c8a9a5SSteve French return rc; 363038c8a9a5SSteve French 363138c8a9a5SSteve French error: 363238c8a9a5SSteve French dfs_put_root_smb_sessions(&mnt_ctx.dfs_ses_list); 363338c8a9a5SSteve French cifs_mount_put_conns(&mnt_ctx); 363438c8a9a5SSteve French return rc; 363538c8a9a5SSteve French } 363638c8a9a5SSteve French #else 363738c8a9a5SSteve French int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) 363838c8a9a5SSteve French { 363938c8a9a5SSteve French int rc = 0; 364038c8a9a5SSteve French struct cifs_mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; 364138c8a9a5SSteve French 364238c8a9a5SSteve French rc = cifs_mount_get_session(&mnt_ctx); 364338c8a9a5SSteve French if (rc) 364438c8a9a5SSteve French goto error; 364538c8a9a5SSteve French 364638c8a9a5SSteve French rc = cifs_mount_get_tcon(&mnt_ctx); 364738c8a9a5SSteve French if (rc) 364838c8a9a5SSteve French goto error; 364938c8a9a5SSteve French 365038c8a9a5SSteve French rc = cifs_is_path_remote(&mnt_ctx); 365138c8a9a5SSteve French if (rc == -EREMOTE) 365238c8a9a5SSteve French rc = -EOPNOTSUPP; 365338c8a9a5SSteve French if (rc) 365438c8a9a5SSteve French goto error; 365538c8a9a5SSteve French 365638c8a9a5SSteve French rc = mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); 365738c8a9a5SSteve French if (rc) 365838c8a9a5SSteve French goto error; 365938c8a9a5SSteve French 366038c8a9a5SSteve French free_xid(mnt_ctx.xid); 366138c8a9a5SSteve French return rc; 366238c8a9a5SSteve French 366338c8a9a5SSteve French error: 366438c8a9a5SSteve French cifs_mount_put_conns(&mnt_ctx); 366538c8a9a5SSteve French return rc; 366638c8a9a5SSteve French } 366738c8a9a5SSteve French #endif 366838c8a9a5SSteve French 366938c8a9a5SSteve French /* 367038c8a9a5SSteve French * Issue a TREE_CONNECT request. 367138c8a9a5SSteve French */ 367238c8a9a5SSteve French int 367338c8a9a5SSteve French CIFSTCon(const unsigned int xid, struct cifs_ses *ses, 367438c8a9a5SSteve French const char *tree, struct cifs_tcon *tcon, 367538c8a9a5SSteve French const struct nls_table *nls_codepage) 367638c8a9a5SSteve French { 367738c8a9a5SSteve French struct smb_hdr *smb_buffer; 367838c8a9a5SSteve French struct smb_hdr *smb_buffer_response; 367938c8a9a5SSteve French TCONX_REQ *pSMB; 368038c8a9a5SSteve French TCONX_RSP *pSMBr; 368138c8a9a5SSteve French unsigned char *bcc_ptr; 368238c8a9a5SSteve French int rc = 0; 368338c8a9a5SSteve French int length; 368438c8a9a5SSteve French __u16 bytes_left, count; 368538c8a9a5SSteve French 368638c8a9a5SSteve French if (ses == NULL) 368738c8a9a5SSteve French return -EIO; 368838c8a9a5SSteve French 368938c8a9a5SSteve French smb_buffer = cifs_buf_get(); 369038c8a9a5SSteve French if (smb_buffer == NULL) 369138c8a9a5SSteve French return -ENOMEM; 369238c8a9a5SSteve French 369338c8a9a5SSteve French smb_buffer_response = smb_buffer; 369438c8a9a5SSteve French 369538c8a9a5SSteve French header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX, 369638c8a9a5SSteve French NULL /*no tid */ , 4 /*wct */ ); 369738c8a9a5SSteve French 369838c8a9a5SSteve French smb_buffer->Mid = get_next_mid(ses->server); 369938c8a9a5SSteve French smb_buffer->Uid = ses->Suid; 370038c8a9a5SSteve French pSMB = (TCONX_REQ *) smb_buffer; 370138c8a9a5SSteve French pSMBr = (TCONX_RSP *) smb_buffer_response; 370238c8a9a5SSteve French 370338c8a9a5SSteve French pSMB->AndXCommand = 0xFF; 370438c8a9a5SSteve French pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO); 370538c8a9a5SSteve French bcc_ptr = &pSMB->Password[0]; 370638c8a9a5SSteve French 370738c8a9a5SSteve French pSMB->PasswordLength = cpu_to_le16(1); /* minimum */ 370838c8a9a5SSteve French *bcc_ptr = 0; /* password is null byte */ 370938c8a9a5SSteve French bcc_ptr++; /* skip password */ 371038c8a9a5SSteve French /* already aligned so no need to do it below */ 371138c8a9a5SSteve French 371238c8a9a5SSteve French if (ses->server->sign) 371338c8a9a5SSteve French smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; 371438c8a9a5SSteve French 371538c8a9a5SSteve French if (ses->capabilities & CAP_STATUS32) { 371638c8a9a5SSteve French smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS; 371738c8a9a5SSteve French } 371838c8a9a5SSteve French if (ses->capabilities & CAP_DFS) { 371938c8a9a5SSteve French smb_buffer->Flags2 |= SMBFLG2_DFS; 372038c8a9a5SSteve French } 372138c8a9a5SSteve French if (ses->capabilities & CAP_UNICODE) { 372238c8a9a5SSteve French smb_buffer->Flags2 |= SMBFLG2_UNICODE; 372338c8a9a5SSteve French length = 372438c8a9a5SSteve French cifs_strtoUTF16((__le16 *) bcc_ptr, tree, 372538c8a9a5SSteve French 6 /* max utf8 char length in bytes */ * 372638c8a9a5SSteve French (/* server len*/ + 256 /* share len */), nls_codepage); 372738c8a9a5SSteve French bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */ 372838c8a9a5SSteve French bcc_ptr += 2; /* skip trailing null */ 372938c8a9a5SSteve French } else { /* ASCII */ 373038c8a9a5SSteve French strcpy(bcc_ptr, tree); 373138c8a9a5SSteve French bcc_ptr += strlen(tree) + 1; 373238c8a9a5SSteve French } 373338c8a9a5SSteve French strcpy(bcc_ptr, "?????"); 373438c8a9a5SSteve French bcc_ptr += strlen("?????"); 373538c8a9a5SSteve French bcc_ptr += 1; 373638c8a9a5SSteve French count = bcc_ptr - &pSMB->Password[0]; 373738c8a9a5SSteve French be32_add_cpu(&pSMB->hdr.smb_buf_length, count); 373838c8a9a5SSteve French pSMB->ByteCount = cpu_to_le16(count); 373938c8a9a5SSteve French 374038c8a9a5SSteve French rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length, 374138c8a9a5SSteve French 0); 374238c8a9a5SSteve French 374338c8a9a5SSteve French /* above now done in SendReceive */ 374438c8a9a5SSteve French if (rc == 0) { 374538c8a9a5SSteve French bool is_unicode; 374638c8a9a5SSteve French 374738c8a9a5SSteve French tcon->tid = smb_buffer_response->Tid; 374838c8a9a5SSteve French bcc_ptr = pByteArea(smb_buffer_response); 374938c8a9a5SSteve French bytes_left = get_bcc(smb_buffer_response); 375038c8a9a5SSteve French length = strnlen(bcc_ptr, bytes_left - 2); 375138c8a9a5SSteve French if (smb_buffer->Flags2 & SMBFLG2_UNICODE) 375238c8a9a5SSteve French is_unicode = true; 375338c8a9a5SSteve French else 375438c8a9a5SSteve French is_unicode = false; 375538c8a9a5SSteve French 375638c8a9a5SSteve French 375738c8a9a5SSteve French /* skip service field (NB: this field is always ASCII) */ 375838c8a9a5SSteve French if (length == 3) { 375938c8a9a5SSteve French if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') && 376038c8a9a5SSteve French (bcc_ptr[2] == 'C')) { 376138c8a9a5SSteve French cifs_dbg(FYI, "IPC connection\n"); 376238c8a9a5SSteve French tcon->ipc = true; 376338c8a9a5SSteve French tcon->pipe = true; 376438c8a9a5SSteve French } 376538c8a9a5SSteve French } else if (length == 2) { 376638c8a9a5SSteve French if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) { 376738c8a9a5SSteve French /* the most common case */ 376838c8a9a5SSteve French cifs_dbg(FYI, "disk share connection\n"); 376938c8a9a5SSteve French } 377038c8a9a5SSteve French } 377138c8a9a5SSteve French bcc_ptr += length + 1; 377238c8a9a5SSteve French bytes_left -= (length + 1); 377338c8a9a5SSteve French strscpy(tcon->tree_name, tree, sizeof(tcon->tree_name)); 377438c8a9a5SSteve French 377538c8a9a5SSteve French /* mostly informational -- no need to fail on error here */ 377638c8a9a5SSteve French kfree(tcon->nativeFileSystem); 377738c8a9a5SSteve French tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr, 377838c8a9a5SSteve French bytes_left, is_unicode, 377938c8a9a5SSteve French nls_codepage); 378038c8a9a5SSteve French 378138c8a9a5SSteve French cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem); 378238c8a9a5SSteve French 378338c8a9a5SSteve French if ((smb_buffer_response->WordCount == 3) || 378438c8a9a5SSteve French (smb_buffer_response->WordCount == 7)) 378538c8a9a5SSteve French /* field is in same location */ 378638c8a9a5SSteve French tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport); 378738c8a9a5SSteve French else 378838c8a9a5SSteve French tcon->Flags = 0; 378938c8a9a5SSteve French cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags); 379038c8a9a5SSteve French } 379138c8a9a5SSteve French 379238c8a9a5SSteve French cifs_buf_release(smb_buffer); 379338c8a9a5SSteve French return rc; 379438c8a9a5SSteve French } 379538c8a9a5SSteve French 379638c8a9a5SSteve French static void delayed_free(struct rcu_head *p) 379738c8a9a5SSteve French { 379838c8a9a5SSteve French struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu); 379938c8a9a5SSteve French 380038c8a9a5SSteve French unload_nls(cifs_sb->local_nls); 380138c8a9a5SSteve French smb3_cleanup_fs_context(cifs_sb->ctx); 380238c8a9a5SSteve French kfree(cifs_sb); 380338c8a9a5SSteve French } 380438c8a9a5SSteve French 380538c8a9a5SSteve French void 380638c8a9a5SSteve French cifs_umount(struct cifs_sb_info *cifs_sb) 380738c8a9a5SSteve French { 380838c8a9a5SSteve French struct rb_root *root = &cifs_sb->tlink_tree; 380938c8a9a5SSteve French struct rb_node *node; 381038c8a9a5SSteve French struct tcon_link *tlink; 381138c8a9a5SSteve French 381238c8a9a5SSteve French cancel_delayed_work_sync(&cifs_sb->prune_tlinks); 381338c8a9a5SSteve French 381438c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 381538c8a9a5SSteve French while ((node = rb_first(root))) { 381638c8a9a5SSteve French tlink = rb_entry(node, struct tcon_link, tl_rbnode); 381738c8a9a5SSteve French cifs_get_tlink(tlink); 381838c8a9a5SSteve French clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); 381938c8a9a5SSteve French rb_erase(node, root); 382038c8a9a5SSteve French 382138c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 382238c8a9a5SSteve French cifs_put_tlink(tlink); 382338c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 382438c8a9a5SSteve French } 382538c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 382638c8a9a5SSteve French 382738c8a9a5SSteve French kfree(cifs_sb->prepath); 382838c8a9a5SSteve French call_rcu(&cifs_sb->rcu, delayed_free); 382938c8a9a5SSteve French } 383038c8a9a5SSteve French 383138c8a9a5SSteve French int 383238c8a9a5SSteve French cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses, 383338c8a9a5SSteve French struct TCP_Server_Info *server) 383438c8a9a5SSteve French { 383538c8a9a5SSteve French int rc = 0; 383638c8a9a5SSteve French 383738c8a9a5SSteve French if (!server->ops->need_neg || !server->ops->negotiate) 383838c8a9a5SSteve French return -ENOSYS; 383938c8a9a5SSteve French 384038c8a9a5SSteve French /* only send once per connect */ 384138c8a9a5SSteve French spin_lock(&server->srv_lock); 384238c8a9a5SSteve French if (server->tcpStatus != CifsGood && 384338c8a9a5SSteve French server->tcpStatus != CifsNew && 384438c8a9a5SSteve French server->tcpStatus != CifsNeedNegotiate) { 384538c8a9a5SSteve French spin_unlock(&server->srv_lock); 384638c8a9a5SSteve French return -EHOSTDOWN; 384738c8a9a5SSteve French } 384838c8a9a5SSteve French 384938c8a9a5SSteve French if (!server->ops->need_neg(server) && 385038c8a9a5SSteve French server->tcpStatus == CifsGood) { 385138c8a9a5SSteve French spin_unlock(&server->srv_lock); 385238c8a9a5SSteve French return 0; 385338c8a9a5SSteve French } 385438c8a9a5SSteve French 385538c8a9a5SSteve French server->tcpStatus = CifsInNegotiate; 385638c8a9a5SSteve French spin_unlock(&server->srv_lock); 385738c8a9a5SSteve French 385838c8a9a5SSteve French rc = server->ops->negotiate(xid, ses, server); 385938c8a9a5SSteve French if (rc == 0) { 386038c8a9a5SSteve French spin_lock(&server->srv_lock); 386138c8a9a5SSteve French if (server->tcpStatus == CifsInNegotiate) 386238c8a9a5SSteve French server->tcpStatus = CifsGood; 386338c8a9a5SSteve French else 386438c8a9a5SSteve French rc = -EHOSTDOWN; 386538c8a9a5SSteve French spin_unlock(&server->srv_lock); 386638c8a9a5SSteve French } else { 386738c8a9a5SSteve French spin_lock(&server->srv_lock); 386838c8a9a5SSteve French if (server->tcpStatus == CifsInNegotiate) 386938c8a9a5SSteve French server->tcpStatus = CifsNeedNegotiate; 387038c8a9a5SSteve French spin_unlock(&server->srv_lock); 387138c8a9a5SSteve French } 387238c8a9a5SSteve French 387338c8a9a5SSteve French return rc; 387438c8a9a5SSteve French } 387538c8a9a5SSteve French 387638c8a9a5SSteve French int 387738c8a9a5SSteve French cifs_setup_session(const unsigned int xid, struct cifs_ses *ses, 387838c8a9a5SSteve French struct TCP_Server_Info *server, 387938c8a9a5SSteve French struct nls_table *nls_info) 388038c8a9a5SSteve French { 388138c8a9a5SSteve French int rc = -ENOSYS; 3882b3773b19SSteve French struct TCP_Server_Info *pserver = SERVER_IS_CHAN(server) ? server->primary_server : server; 388338c8a9a5SSteve French struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&pserver->dstaddr; 388438c8a9a5SSteve French struct sockaddr_in *addr = (struct sockaddr_in *)&pserver->dstaddr; 388538c8a9a5SSteve French bool is_binding = false; 388638c8a9a5SSteve French 388738c8a9a5SSteve French spin_lock(&ses->ses_lock); 388838c8a9a5SSteve French cifs_dbg(FYI, "%s: channel connect bitmap: 0x%lx\n", 388938c8a9a5SSteve French __func__, ses->chans_need_reconnect); 389038c8a9a5SSteve French 389138c8a9a5SSteve French if (ses->ses_status != SES_GOOD && 389238c8a9a5SSteve French ses->ses_status != SES_NEW && 389338c8a9a5SSteve French ses->ses_status != SES_NEED_RECON) { 389438c8a9a5SSteve French spin_unlock(&ses->ses_lock); 389538c8a9a5SSteve French return -EHOSTDOWN; 389638c8a9a5SSteve French } 389738c8a9a5SSteve French 389838c8a9a5SSteve French /* only send once per connect */ 389938c8a9a5SSteve French spin_lock(&ses->chan_lock); 390038c8a9a5SSteve French if (CIFS_ALL_CHANS_GOOD(ses)) { 390138c8a9a5SSteve French if (ses->ses_status == SES_NEED_RECON) 390238c8a9a5SSteve French ses->ses_status = SES_GOOD; 390338c8a9a5SSteve French spin_unlock(&ses->chan_lock); 390438c8a9a5SSteve French spin_unlock(&ses->ses_lock); 390538c8a9a5SSteve French return 0; 390638c8a9a5SSteve French } 390738c8a9a5SSteve French 390838c8a9a5SSteve French cifs_chan_set_in_reconnect(ses, server); 390938c8a9a5SSteve French is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses); 391038c8a9a5SSteve French spin_unlock(&ses->chan_lock); 391138c8a9a5SSteve French 3912aabf4851SShyam Prasad N if (!is_binding) { 391338c8a9a5SSteve French ses->ses_status = SES_IN_SETUP; 3914aabf4851SShyam Prasad N 3915aabf4851SShyam Prasad N /* force iface_list refresh */ 3916aabf4851SShyam Prasad N ses->iface_last_update = 0; 3917aabf4851SShyam Prasad N } 391838c8a9a5SSteve French spin_unlock(&ses->ses_lock); 391938c8a9a5SSteve French 392038c8a9a5SSteve French /* update ses ip_addr only for primary chan */ 392138c8a9a5SSteve French if (server == pserver) { 392238c8a9a5SSteve French if (server->dstaddr.ss_family == AF_INET6) 392338c8a9a5SSteve French scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI6", &addr6->sin6_addr); 392438c8a9a5SSteve French else 392538c8a9a5SSteve French scnprintf(ses->ip_addr, sizeof(ses->ip_addr), "%pI4", &addr->sin_addr); 392638c8a9a5SSteve French } 392738c8a9a5SSteve French 392838c8a9a5SSteve French if (!is_binding) { 392938c8a9a5SSteve French ses->capabilities = server->capabilities; 393038c8a9a5SSteve French if (!linuxExtEnabled) 393138c8a9a5SSteve French ses->capabilities &= (~server->vals->cap_unix); 393238c8a9a5SSteve French 393338c8a9a5SSteve French if (ses->auth_key.response) { 393438c8a9a5SSteve French cifs_dbg(FYI, "Free previous auth_key.response = %p\n", 393538c8a9a5SSteve French ses->auth_key.response); 393638c8a9a5SSteve French kfree_sensitive(ses->auth_key.response); 393738c8a9a5SSteve French ses->auth_key.response = NULL; 393838c8a9a5SSteve French ses->auth_key.len = 0; 393938c8a9a5SSteve French } 394038c8a9a5SSteve French } 394138c8a9a5SSteve French 394238c8a9a5SSteve French cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n", 394338c8a9a5SSteve French server->sec_mode, server->capabilities, server->timeAdj); 394438c8a9a5SSteve French 394538c8a9a5SSteve French if (server->ops->sess_setup) 394638c8a9a5SSteve French rc = server->ops->sess_setup(xid, ses, server, nls_info); 394738c8a9a5SSteve French 394838c8a9a5SSteve French if (rc) { 394938c8a9a5SSteve French cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc); 395038c8a9a5SSteve French spin_lock(&ses->ses_lock); 395138c8a9a5SSteve French if (ses->ses_status == SES_IN_SETUP) 395238c8a9a5SSteve French ses->ses_status = SES_NEED_RECON; 395338c8a9a5SSteve French spin_lock(&ses->chan_lock); 395438c8a9a5SSteve French cifs_chan_clear_in_reconnect(ses, server); 395538c8a9a5SSteve French spin_unlock(&ses->chan_lock); 395638c8a9a5SSteve French spin_unlock(&ses->ses_lock); 395738c8a9a5SSteve French } else { 395838c8a9a5SSteve French spin_lock(&ses->ses_lock); 395938c8a9a5SSteve French if (ses->ses_status == SES_IN_SETUP) 396038c8a9a5SSteve French ses->ses_status = SES_GOOD; 396138c8a9a5SSteve French spin_lock(&ses->chan_lock); 396238c8a9a5SSteve French cifs_chan_clear_in_reconnect(ses, server); 396338c8a9a5SSteve French cifs_chan_clear_need_reconnect(ses, server); 396438c8a9a5SSteve French spin_unlock(&ses->chan_lock); 396538c8a9a5SSteve French spin_unlock(&ses->ses_lock); 396638c8a9a5SSteve French } 396738c8a9a5SSteve French 396838c8a9a5SSteve French return rc; 396938c8a9a5SSteve French } 397038c8a9a5SSteve French 397138c8a9a5SSteve French static int 397238c8a9a5SSteve French cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses) 397338c8a9a5SSteve French { 397438c8a9a5SSteve French ctx->sectype = ses->sectype; 397538c8a9a5SSteve French 397638c8a9a5SSteve French /* krb5 is special, since we don't need username or pw */ 397738c8a9a5SSteve French if (ctx->sectype == Kerberos) 397838c8a9a5SSteve French return 0; 397938c8a9a5SSteve French 398038c8a9a5SSteve French return cifs_set_cifscreds(ctx, ses); 398138c8a9a5SSteve French } 398238c8a9a5SSteve French 398338c8a9a5SSteve French static struct cifs_tcon * 3984*ba55f8a9SPaulo Alcantara __cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) 398538c8a9a5SSteve French { 398638c8a9a5SSteve French int rc; 398738c8a9a5SSteve French struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb); 398838c8a9a5SSteve French struct cifs_ses *ses; 398938c8a9a5SSteve French struct cifs_tcon *tcon = NULL; 399038c8a9a5SSteve French struct smb3_fs_context *ctx; 39919b2ee27eSPaulo Alcantara char *origin_fullpath = NULL; 399238c8a9a5SSteve French 399338c8a9a5SSteve French ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 399438c8a9a5SSteve French if (ctx == NULL) 399538c8a9a5SSteve French return ERR_PTR(-ENOMEM); 399638c8a9a5SSteve French 399738c8a9a5SSteve French ctx->local_nls = cifs_sb->local_nls; 399838c8a9a5SSteve French ctx->linux_uid = fsuid; 399938c8a9a5SSteve French ctx->cred_uid = fsuid; 400038c8a9a5SSteve French ctx->UNC = master_tcon->tree_name; 400138c8a9a5SSteve French ctx->retry = master_tcon->retry; 400238c8a9a5SSteve French ctx->nocase = master_tcon->nocase; 400338c8a9a5SSteve French ctx->nohandlecache = master_tcon->nohandlecache; 400438c8a9a5SSteve French ctx->local_lease = master_tcon->local_lease; 400538c8a9a5SSteve French ctx->no_lease = master_tcon->no_lease; 400638c8a9a5SSteve French ctx->resilient = master_tcon->use_resilient; 400738c8a9a5SSteve French ctx->persistent = master_tcon->use_persistent; 400838c8a9a5SSteve French ctx->handle_timeout = master_tcon->handle_timeout; 400938c8a9a5SSteve French ctx->no_linux_ext = !master_tcon->unix_ext; 401038c8a9a5SSteve French ctx->linux_ext = master_tcon->posix_extensions; 401138c8a9a5SSteve French ctx->sectype = master_tcon->ses->sectype; 401238c8a9a5SSteve French ctx->sign = master_tcon->ses->sign; 401338c8a9a5SSteve French ctx->seal = master_tcon->seal; 401438c8a9a5SSteve French ctx->witness = master_tcon->use_witness; 40159b2ee27eSPaulo Alcantara ctx->dfs_root_ses = master_tcon->ses->dfs_root_ses; 401638c8a9a5SSteve French 401738c8a9a5SSteve French rc = cifs_set_vol_auth(ctx, master_tcon->ses); 401838c8a9a5SSteve French if (rc) { 401938c8a9a5SSteve French tcon = ERR_PTR(rc); 402038c8a9a5SSteve French goto out; 402138c8a9a5SSteve French } 402238c8a9a5SSteve French 402338c8a9a5SSteve French /* get a reference for the same TCP session */ 402438c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 402538c8a9a5SSteve French ++master_tcon->ses->server->srv_count; 402638c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 402738c8a9a5SSteve French 402838c8a9a5SSteve French ses = cifs_get_smb_ses(master_tcon->ses->server, ctx); 402938c8a9a5SSteve French if (IS_ERR(ses)) { 403038c8a9a5SSteve French tcon = (struct cifs_tcon *)ses; 403138c8a9a5SSteve French cifs_put_tcp_session(master_tcon->ses->server, 0); 403238c8a9a5SSteve French goto out; 403338c8a9a5SSteve French } 403438c8a9a5SSteve French 40359b2ee27eSPaulo Alcantara #ifdef CONFIG_CIFS_DFS_UPCALL 40369b2ee27eSPaulo Alcantara spin_lock(&master_tcon->tc_lock); 40379b2ee27eSPaulo Alcantara if (master_tcon->origin_fullpath) { 40389b2ee27eSPaulo Alcantara spin_unlock(&master_tcon->tc_lock); 40399b2ee27eSPaulo Alcantara origin_fullpath = dfs_get_path(cifs_sb, cifs_sb->ctx->source); 40409b2ee27eSPaulo Alcantara if (IS_ERR(origin_fullpath)) { 40419b2ee27eSPaulo Alcantara tcon = ERR_CAST(origin_fullpath); 40429b2ee27eSPaulo Alcantara origin_fullpath = NULL; 40439b2ee27eSPaulo Alcantara cifs_put_smb_ses(ses); 40449b2ee27eSPaulo Alcantara goto out; 40459b2ee27eSPaulo Alcantara } 40469b2ee27eSPaulo Alcantara } else { 40479b2ee27eSPaulo Alcantara spin_unlock(&master_tcon->tc_lock); 40489b2ee27eSPaulo Alcantara } 40499b2ee27eSPaulo Alcantara #endif 40509b2ee27eSPaulo Alcantara 405138c8a9a5SSteve French tcon = cifs_get_tcon(ses, ctx); 405238c8a9a5SSteve French if (IS_ERR(tcon)) { 405338c8a9a5SSteve French cifs_put_smb_ses(ses); 405438c8a9a5SSteve French goto out; 405538c8a9a5SSteve French } 405638c8a9a5SSteve French 40579b2ee27eSPaulo Alcantara #ifdef CONFIG_CIFS_DFS_UPCALL 40589b2ee27eSPaulo Alcantara if (origin_fullpath) { 40599b2ee27eSPaulo Alcantara spin_lock(&tcon->tc_lock); 40609b2ee27eSPaulo Alcantara tcon->origin_fullpath = origin_fullpath; 40619b2ee27eSPaulo Alcantara spin_unlock(&tcon->tc_lock); 40629b2ee27eSPaulo Alcantara origin_fullpath = NULL; 40639b2ee27eSPaulo Alcantara queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work, 40649b2ee27eSPaulo Alcantara dfs_cache_get_ttl() * HZ); 40659b2ee27eSPaulo Alcantara } 40669b2ee27eSPaulo Alcantara #endif 40679b2ee27eSPaulo Alcantara 406838c8a9a5SSteve French #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY 406938c8a9a5SSteve French if (cap_unix(ses)) 407038c8a9a5SSteve French reset_cifs_unix_caps(0, tcon, NULL, ctx); 407138c8a9a5SSteve French #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ 407238c8a9a5SSteve French 407338c8a9a5SSteve French out: 407438c8a9a5SSteve French kfree(ctx->username); 407538c8a9a5SSteve French kfree_sensitive(ctx->password); 40769b2ee27eSPaulo Alcantara kfree(origin_fullpath); 407738c8a9a5SSteve French kfree(ctx); 407838c8a9a5SSteve French 407938c8a9a5SSteve French return tcon; 408038c8a9a5SSteve French } 408138c8a9a5SSteve French 4082*ba55f8a9SPaulo Alcantara static struct cifs_tcon * 4083*ba55f8a9SPaulo Alcantara cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) 4084*ba55f8a9SPaulo Alcantara { 4085*ba55f8a9SPaulo Alcantara struct cifs_tcon *ret; 4086*ba55f8a9SPaulo Alcantara 4087*ba55f8a9SPaulo Alcantara cifs_mount_lock(); 4088*ba55f8a9SPaulo Alcantara ret = __cifs_construct_tcon(cifs_sb, fsuid); 4089*ba55f8a9SPaulo Alcantara cifs_mount_unlock(); 4090*ba55f8a9SPaulo Alcantara return ret; 4091*ba55f8a9SPaulo Alcantara } 4092*ba55f8a9SPaulo Alcantara 409338c8a9a5SSteve French struct cifs_tcon * 409438c8a9a5SSteve French cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb) 409538c8a9a5SSteve French { 409638c8a9a5SSteve French return tlink_tcon(cifs_sb_master_tlink(cifs_sb)); 409738c8a9a5SSteve French } 409838c8a9a5SSteve French 409938c8a9a5SSteve French /* find and return a tlink with given uid */ 410038c8a9a5SSteve French static struct tcon_link * 410138c8a9a5SSteve French tlink_rb_search(struct rb_root *root, kuid_t uid) 410238c8a9a5SSteve French { 410338c8a9a5SSteve French struct rb_node *node = root->rb_node; 410438c8a9a5SSteve French struct tcon_link *tlink; 410538c8a9a5SSteve French 410638c8a9a5SSteve French while (node) { 410738c8a9a5SSteve French tlink = rb_entry(node, struct tcon_link, tl_rbnode); 410838c8a9a5SSteve French 410938c8a9a5SSteve French if (uid_gt(tlink->tl_uid, uid)) 411038c8a9a5SSteve French node = node->rb_left; 411138c8a9a5SSteve French else if (uid_lt(tlink->tl_uid, uid)) 411238c8a9a5SSteve French node = node->rb_right; 411338c8a9a5SSteve French else 411438c8a9a5SSteve French return tlink; 411538c8a9a5SSteve French } 411638c8a9a5SSteve French return NULL; 411738c8a9a5SSteve French } 411838c8a9a5SSteve French 411938c8a9a5SSteve French /* insert a tcon_link into the tree */ 412038c8a9a5SSteve French static void 412138c8a9a5SSteve French tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink) 412238c8a9a5SSteve French { 412338c8a9a5SSteve French struct rb_node **new = &(root->rb_node), *parent = NULL; 412438c8a9a5SSteve French struct tcon_link *tlink; 412538c8a9a5SSteve French 412638c8a9a5SSteve French while (*new) { 412738c8a9a5SSteve French tlink = rb_entry(*new, struct tcon_link, tl_rbnode); 412838c8a9a5SSteve French parent = *new; 412938c8a9a5SSteve French 413038c8a9a5SSteve French if (uid_gt(tlink->tl_uid, new_tlink->tl_uid)) 413138c8a9a5SSteve French new = &((*new)->rb_left); 413238c8a9a5SSteve French else 413338c8a9a5SSteve French new = &((*new)->rb_right); 413438c8a9a5SSteve French } 413538c8a9a5SSteve French 413638c8a9a5SSteve French rb_link_node(&new_tlink->tl_rbnode, parent, new); 413738c8a9a5SSteve French rb_insert_color(&new_tlink->tl_rbnode, root); 413838c8a9a5SSteve French } 413938c8a9a5SSteve French 414038c8a9a5SSteve French /* 414138c8a9a5SSteve French * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the 414238c8a9a5SSteve French * current task. 414338c8a9a5SSteve French * 414438c8a9a5SSteve French * If the superblock doesn't refer to a multiuser mount, then just return 414538c8a9a5SSteve French * the master tcon for the mount. 414638c8a9a5SSteve French * 414738c8a9a5SSteve French * First, search the rbtree for an existing tcon for this fsuid. If one 414838c8a9a5SSteve French * exists, then check to see if it's pending construction. If it is then wait 414938c8a9a5SSteve French * for construction to complete. Once it's no longer pending, check to see if 415038c8a9a5SSteve French * it failed and either return an error or retry construction, depending on 415138c8a9a5SSteve French * the timeout. 415238c8a9a5SSteve French * 415338c8a9a5SSteve French * If one doesn't exist then insert a new tcon_link struct into the tree and 415438c8a9a5SSteve French * try to construct a new one. 415538c8a9a5SSteve French */ 415638c8a9a5SSteve French struct tcon_link * 415738c8a9a5SSteve French cifs_sb_tlink(struct cifs_sb_info *cifs_sb) 415838c8a9a5SSteve French { 415938c8a9a5SSteve French int ret; 416038c8a9a5SSteve French kuid_t fsuid = current_fsuid(); 416138c8a9a5SSteve French struct tcon_link *tlink, *newtlink; 416238c8a9a5SSteve French 416338c8a9a5SSteve French if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) 416438c8a9a5SSteve French return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); 416538c8a9a5SSteve French 416638c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 416738c8a9a5SSteve French tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); 416838c8a9a5SSteve French if (tlink) 416938c8a9a5SSteve French cifs_get_tlink(tlink); 417038c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 417138c8a9a5SSteve French 417238c8a9a5SSteve French if (tlink == NULL) { 417338c8a9a5SSteve French newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL); 417438c8a9a5SSteve French if (newtlink == NULL) 417538c8a9a5SSteve French return ERR_PTR(-ENOMEM); 417638c8a9a5SSteve French newtlink->tl_uid = fsuid; 417738c8a9a5SSteve French newtlink->tl_tcon = ERR_PTR(-EACCES); 417838c8a9a5SSteve French set_bit(TCON_LINK_PENDING, &newtlink->tl_flags); 417938c8a9a5SSteve French set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags); 418038c8a9a5SSteve French cifs_get_tlink(newtlink); 418138c8a9a5SSteve French 418238c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 418338c8a9a5SSteve French /* was one inserted after previous search? */ 418438c8a9a5SSteve French tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); 418538c8a9a5SSteve French if (tlink) { 418638c8a9a5SSteve French cifs_get_tlink(tlink); 418738c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 418838c8a9a5SSteve French kfree(newtlink); 418938c8a9a5SSteve French goto wait_for_construction; 419038c8a9a5SSteve French } 419138c8a9a5SSteve French tlink = newtlink; 419238c8a9a5SSteve French tlink_rb_insert(&cifs_sb->tlink_tree, tlink); 419338c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 419438c8a9a5SSteve French } else { 419538c8a9a5SSteve French wait_for_construction: 419638c8a9a5SSteve French ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING, 419738c8a9a5SSteve French TASK_INTERRUPTIBLE); 419838c8a9a5SSteve French if (ret) { 419938c8a9a5SSteve French cifs_put_tlink(tlink); 420038c8a9a5SSteve French return ERR_PTR(-ERESTARTSYS); 420138c8a9a5SSteve French } 420238c8a9a5SSteve French 420338c8a9a5SSteve French /* if it's good, return it */ 420438c8a9a5SSteve French if (!IS_ERR(tlink->tl_tcon)) 420538c8a9a5SSteve French return tlink; 420638c8a9a5SSteve French 420738c8a9a5SSteve French /* return error if we tried this already recently */ 420838c8a9a5SSteve French if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) { 420938c8a9a5SSteve French cifs_put_tlink(tlink); 421038c8a9a5SSteve French return ERR_PTR(-EACCES); 421138c8a9a5SSteve French } 421238c8a9a5SSteve French 421338c8a9a5SSteve French if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags)) 421438c8a9a5SSteve French goto wait_for_construction; 421538c8a9a5SSteve French } 421638c8a9a5SSteve French 421738c8a9a5SSteve French tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid); 421838c8a9a5SSteve French clear_bit(TCON_LINK_PENDING, &tlink->tl_flags); 421938c8a9a5SSteve French wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING); 422038c8a9a5SSteve French 422138c8a9a5SSteve French if (IS_ERR(tlink->tl_tcon)) { 422238c8a9a5SSteve French cifs_put_tlink(tlink); 422338c8a9a5SSteve French return ERR_PTR(-EACCES); 422438c8a9a5SSteve French } 422538c8a9a5SSteve French 422638c8a9a5SSteve French return tlink; 422738c8a9a5SSteve French } 422838c8a9a5SSteve French 422938c8a9a5SSteve French /* 423038c8a9a5SSteve French * periodic workqueue job that scans tcon_tree for a superblock and closes 423138c8a9a5SSteve French * out tcons. 423238c8a9a5SSteve French */ 423338c8a9a5SSteve French static void 423438c8a9a5SSteve French cifs_prune_tlinks(struct work_struct *work) 423538c8a9a5SSteve French { 423638c8a9a5SSteve French struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info, 423738c8a9a5SSteve French prune_tlinks.work); 423838c8a9a5SSteve French struct rb_root *root = &cifs_sb->tlink_tree; 423938c8a9a5SSteve French struct rb_node *node; 424038c8a9a5SSteve French struct rb_node *tmp; 424138c8a9a5SSteve French struct tcon_link *tlink; 424238c8a9a5SSteve French 424338c8a9a5SSteve French /* 424438c8a9a5SSteve French * Because we drop the spinlock in the loop in order to put the tlink 424538c8a9a5SSteve French * it's not guarded against removal of links from the tree. The only 424638c8a9a5SSteve French * places that remove entries from the tree are this function and 424738c8a9a5SSteve French * umounts. Because this function is non-reentrant and is canceled 424838c8a9a5SSteve French * before umount can proceed, this is safe. 424938c8a9a5SSteve French */ 425038c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 425138c8a9a5SSteve French node = rb_first(root); 425238c8a9a5SSteve French while (node != NULL) { 425338c8a9a5SSteve French tmp = node; 425438c8a9a5SSteve French node = rb_next(tmp); 425538c8a9a5SSteve French tlink = rb_entry(tmp, struct tcon_link, tl_rbnode); 425638c8a9a5SSteve French 425738c8a9a5SSteve French if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) || 425838c8a9a5SSteve French atomic_read(&tlink->tl_count) != 0 || 425938c8a9a5SSteve French time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies)) 426038c8a9a5SSteve French continue; 426138c8a9a5SSteve French 426238c8a9a5SSteve French cifs_get_tlink(tlink); 426338c8a9a5SSteve French clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); 426438c8a9a5SSteve French rb_erase(tmp, root); 426538c8a9a5SSteve French 426638c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 426738c8a9a5SSteve French cifs_put_tlink(tlink); 426838c8a9a5SSteve French spin_lock(&cifs_sb->tlink_tree_lock); 426938c8a9a5SSteve French } 427038c8a9a5SSteve French spin_unlock(&cifs_sb->tlink_tree_lock); 427138c8a9a5SSteve French 427238c8a9a5SSteve French queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, 427338c8a9a5SSteve French TLINK_IDLE_EXPIRE); 427438c8a9a5SSteve French } 427538c8a9a5SSteve French 427638c8a9a5SSteve French #ifndef CONFIG_CIFS_DFS_UPCALL 427738c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) 427838c8a9a5SSteve French { 427938c8a9a5SSteve French int rc; 428038c8a9a5SSteve French const struct smb_version_operations *ops = tcon->ses->server->ops; 428138c8a9a5SSteve French 428238c8a9a5SSteve French /* only send once per connect */ 428338c8a9a5SSteve French spin_lock(&tcon->tc_lock); 4284cd743cfeSShyam Prasad N 4285cd743cfeSShyam Prasad N /* if tcon is marked for needing reconnect, update state */ 4286cd743cfeSShyam Prasad N if (tcon->need_reconnect) 4287cd743cfeSShyam Prasad N tcon->status = TID_NEED_TCON; 4288cd743cfeSShyam Prasad N 428991f4480cSShyam Prasad N if (tcon->status == TID_GOOD) { 429091f4480cSShyam Prasad N spin_unlock(&tcon->tc_lock); 429191f4480cSShyam Prasad N return 0; 429291f4480cSShyam Prasad N } 429391f4480cSShyam Prasad N 429438c8a9a5SSteve French if (tcon->status != TID_NEW && 429538c8a9a5SSteve French tcon->status != TID_NEED_TCON) { 429638c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 429738c8a9a5SSteve French return -EHOSTDOWN; 429838c8a9a5SSteve French } 429938c8a9a5SSteve French 430038c8a9a5SSteve French tcon->status = TID_IN_TCON; 430138c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 430238c8a9a5SSteve French 430338c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, nlsc); 430438c8a9a5SSteve French if (rc) { 430538c8a9a5SSteve French spin_lock(&tcon->tc_lock); 430638c8a9a5SSteve French if (tcon->status == TID_IN_TCON) 430738c8a9a5SSteve French tcon->status = TID_NEED_TCON; 430838c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 430938c8a9a5SSteve French } else { 431038c8a9a5SSteve French spin_lock(&tcon->tc_lock); 431138c8a9a5SSteve French if (tcon->status == TID_IN_TCON) 431238c8a9a5SSteve French tcon->status = TID_GOOD; 431338c8a9a5SSteve French tcon->need_reconnect = false; 431438c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 431538c8a9a5SSteve French } 431638c8a9a5SSteve French 431738c8a9a5SSteve French return rc; 431838c8a9a5SSteve French } 431938c8a9a5SSteve French #endif 4320