Steve French | 929be90 | 2021-06-18 00:31:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Steve French | 1080ef7 | 2011-02-24 18:07:19 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2011 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/net.h> |
| 10 | #include <linux/string.h> |
Eric Biggers | dc92027 | 2020-03-08 22:58:20 -0700 | [diff] [blame] | 11 | #include <linux/sched/mm.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 12 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/list.h> |
| 14 | #include <linux/wait.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/utsname.h> |
| 19 | #include <linux/mempool.h> |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 20 | #include <linux/delay.h> |
Steve French | f191401 | 2005-08-18 09:37:34 -0700 | [diff] [blame] | 21 | #include <linux/completion.h> |
Igor Mammedov | aaf737a | 2007-04-03 19:16:43 +0000 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Steve French | 0ae0efa | 2005-10-10 10:57:19 -0700 | [diff] [blame] | 23 | #include <linux/pagevec.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 24 | #include <linux/freezer.h> |
Igor Mammedov | 5c2503a | 2009-04-21 19:31:05 +0400 | [diff] [blame] | 25 | #include <linux/namei.h> |
Andrew Lunn | c6e970a | 2017-03-28 23:45:06 +0200 | [diff] [blame] | 26 | #include <linux/uuid.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/processor.h> |
Jeff Layton | 50b64e3 | 2009-06-02 06:55:20 -0400 | [diff] [blame] | 29 | #include <linux/inet.h> |
Paul Gortmaker | 143cb49 | 2011-07-01 14:23:34 -0400 | [diff] [blame] | 30 | #include <linux/module.h> |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 31 | #include <keys/user-type.h> |
Steve French | 0e2beda | 2009-01-30 21:24:41 +0000 | [diff] [blame] | 32 | #include <net/ipv6.h> |
Sachin Prabhu | 8830d7e | 2012-03-23 14:40:56 -0400 | [diff] [blame] | 33 | #include <linux/parser.h> |
Christoph Hellwig | 2f8b544 | 2016-11-01 07:40:13 -0600 | [diff] [blame] | 34 | #include <linux/bvec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "cifspdu.h" |
| 36 | #include "cifsglob.h" |
| 37 | #include "cifsproto.h" |
| 38 | #include "cifs_unicode.h" |
| 39 | #include "cifs_debug.h" |
| 40 | #include "cifs_fs_sb.h" |
| 41 | #include "ntlmssp.h" |
| 42 | #include "nterr.h" |
| 43 | #include "rfc1002pdu.h" |
Suresh Jayaraman | 488f1d2d | 2010-07-05 18:12:15 +0530 | [diff] [blame] | 44 | #include "fscache.h" |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 45 | #include "smb2proto.h" |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 46 | #include "smbdirect.h" |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 47 | #include "dns_resolve.h" |
| 48 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 49 | #include "dfs_cache.h" |
| 50 | #endif |
Ronnie Sahlberg | 5c6e5aa | 2020-10-21 10:37:11 +1000 | [diff] [blame] | 51 | #include "fs_context.h" |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 52 | #include "cifs_swn.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | extern mempool_t *cifs_req_poolp; |
Steve French | f92a720 | 2018-05-24 04:11:07 -0500 | [diff] [blame] | 55 | extern bool disable_legacy_dialects; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 57 | /* FIXME: should these be tunable? */ |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 58 | #define TLINK_ERROR_EXPIRE (1 * HZ) |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 59 | #define TLINK_IDLE_EXPIRE (600 * HZ) |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 60 | |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 61 | /* Drop the connection to not overload the server */ |
| 62 | #define NUM_STATUS_IO_TIMEOUT 5 |
| 63 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 64 | struct mount_ctx { |
| 65 | struct cifs_sb_info *cifs_sb; |
| 66 | struct smb3_fs_context *fs_ctx; |
| 67 | unsigned int xid; |
| 68 | struct TCP_Server_Info *server; |
| 69 | struct cifs_ses *ses; |
| 70 | struct cifs_tcon *tcon; |
| 71 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 72 | struct cifs_ses *root_ses; |
| 73 | uuid_t mount_id; |
| 74 | char *origin_fullpath, *leaf_fullpath; |
| 75 | #endif |
| 76 | }; |
| 77 | |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 78 | static int ip_connect(struct TCP_Server_Info *server); |
| 79 | static int generic_ip_connect(struct TCP_Server_Info *server); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 80 | static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink); |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 81 | static void cifs_prune_tlinks(struct work_struct *work); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 82 | |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 83 | /* |
| 84 | * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may |
| 85 | * get their ip addresses changed at some point. |
| 86 | * |
| 87 | * This should be called with server->srv_mutex held. |
| 88 | */ |
Samuel Cabrero | 7d6535b | 2020-11-30 19:02:55 +0100 | [diff] [blame] | 89 | static int reconn_set_ipaddr_from_hostname(struct TCP_Server_Info *server) |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 90 | { |
| 91 | int rc; |
| 92 | int len; |
| 93 | char *unc, *ipaddr = NULL; |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 94 | time64_t expiry, now; |
| 95 | unsigned long ttl = SMB_DNS_RESOLVE_INTERVAL_DEFAULT; |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 96 | |
| 97 | if (!server->hostname) |
| 98 | return -EINVAL; |
| 99 | |
| 100 | len = strlen(server->hostname) + 3; |
| 101 | |
| 102 | unc = kmalloc(len, GFP_KERNEL); |
| 103 | if (!unc) { |
| 104 | cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__); |
| 105 | return -ENOMEM; |
| 106 | } |
Ronnie Sahlberg | 74ea5f9 | 2019-02-09 09:51:11 +1000 | [diff] [blame] | 107 | scnprintf(unc, len, "\\\\%s", server->hostname); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 108 | |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 109 | rc = dns_resolve_server_name_to_ip(unc, &ipaddr, &expiry); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 110 | kfree(unc); |
| 111 | |
| 112 | if (rc < 0) { |
| 113 | cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n", |
| 114 | __func__, server->hostname, rc); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 115 | goto requeue_resolve; |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 116 | } |
| 117 | |
Ronnie Sahlberg | fada37f | 2020-04-21 12:37:39 +1000 | [diff] [blame] | 118 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 119 | rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr, |
| 120 | strlen(ipaddr)); |
Ronnie Sahlberg | fada37f | 2020-04-21 12:37:39 +1000 | [diff] [blame] | 121 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 122 | kfree(ipaddr); |
| 123 | |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 124 | /* rc == 1 means success here */ |
| 125 | if (rc) { |
| 126 | now = ktime_get_real_seconds(); |
| 127 | if (expiry && expiry > now) |
| 128 | /* |
| 129 | * To make sure we don't use the cached entry, retry 1s |
| 130 | * after expiry. |
| 131 | */ |
Paulo Alcantara | 4ac0536 | 2021-11-04 13:13:28 -0300 | [diff] [blame] | 132 | ttl = max_t(unsigned long, expiry - now, SMB_DNS_RESOLVE_INTERVAL_MIN) + 1; |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 133 | } |
| 134 | rc = !rc ? -1 : 0; |
| 135 | |
| 136 | requeue_resolve: |
| 137 | cifs_dbg(FYI, "%s: next dns resolution scheduled for %lu seconds in the future\n", |
| 138 | __func__, ttl); |
| 139 | mod_delayed_work(cifsiod_wq, &server->resolve, (ttl * HZ)); |
| 140 | |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static void cifs_resolve_server(struct work_struct *work) |
| 146 | { |
| 147 | int rc; |
| 148 | struct TCP_Server_Info *server = container_of(work, |
| 149 | struct TCP_Server_Info, resolve.work); |
| 150 | |
| 151 | mutex_lock(&server->srv_mutex); |
| 152 | |
| 153 | /* |
| 154 | * Resolve the hostname again to make sure that IP address is up-to-date. |
| 155 | */ |
| 156 | rc = reconn_set_ipaddr_from_hostname(server); |
| 157 | if (rc) { |
| 158 | cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n", |
| 159 | __func__, rc); |
| 160 | } |
| 161 | |
| 162 | mutex_unlock(&server->srv_mutex); |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 163 | } |
Paulo Alcantara | 28eb24f | 2018-11-20 15:16:36 -0200 | [diff] [blame] | 164 | |
David Howells | 483529f | 2022-01-27 16:02:34 +0000 | [diff] [blame] | 165 | /* |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 166 | * Mark all sessions and tcons for reconnect. |
Jeff Layton | d5c5605 | 2008-12-01 18:42:33 -0500 | [diff] [blame] | 167 | * |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 168 | * @server needs to be previously set to CifsNeedReconnect. |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 169 | * |
Jeff Layton | d5c5605 | 2008-12-01 18:42:33 -0500 | [diff] [blame] | 170 | */ |
Shyam Prasad N | ba978e8 | 2022-01-17 07:15:02 +0000 | [diff] [blame] | 171 | void |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 172 | cifs_mark_tcp_ses_conns_for_reconnect(struct TCP_Server_Info *server, |
| 173 | bool mark_smb_session) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Shyam Prasad N | bda487a | 2021-10-25 05:44:10 +0000 | [diff] [blame] | 175 | struct TCP_Server_Info *pserver; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 176 | struct cifs_ses *ses; |
| 177 | struct cifs_tcon *tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 179 | /* |
| 180 | * before reconnecting the tcp session, mark the smb session (uid) and the tid bad so they |
| 181 | * are not used until reconnected. |
| 182 | */ |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 183 | cifs_dbg(FYI, "%s: marking necessary sessions and tcons for reconnect\n", __func__); |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 184 | |
| 185 | /* If server is a channel, select the primary channel */ |
| 186 | pserver = CIFS_SERVER_IS_CHAN(server) ? server->primary_server : server; |
| 187 | |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 188 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 189 | spin_lock(&cifs_tcp_ses_lock); |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 190 | list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) { |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 191 | spin_lock(&ses->chan_lock); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 192 | if (!mark_smb_session && cifs_chan_needs_reconnect(ses, server)) |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 193 | goto next_session; |
| 194 | |
Shyam Prasad N | ba978e8 | 2022-01-17 07:15:02 +0000 | [diff] [blame] | 195 | if (mark_smb_session) |
| 196 | CIFS_SET_ALL_CHANS_NEED_RECONNECT(ses); |
| 197 | else |
| 198 | cifs_chan_set_need_reconnect(ses, server); |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 199 | |
| 200 | /* If all channels need reconnect, then tcon needs reconnect */ |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 201 | if (!mark_smb_session && !CIFS_ALL_CHANS_NEED_RECONNECT(ses)) |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 202 | goto next_session; |
| 203 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 204 | ses->status = CifsNeedReconnect; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 205 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 206 | list_for_each_entry(tcon, &ses->tcon_list, tcon_list) { |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 207 | tcon->need_reconnect = true; |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 208 | tcon->tidStatus = CifsNeedReconnect; |
| 209 | } |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 210 | if (ses->tcon_ipc) |
| 211 | ses->tcon_ipc->need_reconnect = true; |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 212 | |
| 213 | next_session: |
| 214 | spin_unlock(&ses->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 216 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | 2a05137 | 2022-02-08 17:10:02 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static void |
| 220 | cifs_abort_connection(struct TCP_Server_Info *server) |
| 221 | { |
| 222 | struct mid_q_entry *mid, *nmid; |
| 223 | struct list_head retry_list; |
| 224 | |
| 225 | server->maxBuf = 0; |
| 226 | server->max_read = 0; |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* do not want to be sending data on a socket we are freeing */ |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 229 | cifs_dbg(FYI, "%s: tearing down socket\n", __func__); |
Jeff Layton | 72ca545 | 2008-12-01 07:09:36 -0500 | [diff] [blame] | 230 | mutex_lock(&server->srv_mutex); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 231 | if (server->ssocket) { |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 232 | cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n", server->ssocket->state, |
| 233 | server->ssocket->flags); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 234 | kernel_sock_shutdown(server->ssocket, SHUT_WR); |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 235 | cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n", server->ssocket->state, |
| 236 | server->ssocket->flags); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 237 | sock_release(server->ssocket); |
| 238 | server->ssocket = NULL; |
| 239 | } |
| 240 | server->sequence_number = 0; |
| 241 | server->session_estab = false; |
| 242 | kfree(server->session_key.response); |
| 243 | server->session_key.response = NULL; |
| 244 | server->session_key.len = 0; |
| 245 | server->lstrp = jiffies; |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 246 | |
| 247 | /* mark submitted MIDs for retry and issue callback */ |
| 248 | INIT_LIST_HEAD(&retry_list); |
| 249 | cifs_dbg(FYI, "%s: moving mids to private list\n", __func__); |
| 250 | spin_lock(&GlobalMid_Lock); |
Paulo Alcantara | ae0abb4 | 2021-10-14 14:19:17 -0300 | [diff] [blame] | 251 | list_for_each_entry_safe(mid, nmid, &server->pending_mid_q, qhead) { |
| 252 | kref_get(&mid->refcount); |
| 253 | if (mid->mid_state == MID_REQUEST_SUBMITTED) |
| 254 | mid->mid_state = MID_RETRY_NEEDED; |
| 255 | list_move(&mid->qhead, &retry_list); |
| 256 | mid->mid_flags |= MID_DELETED; |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 257 | } |
| 258 | spin_unlock(&GlobalMid_Lock); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 259 | mutex_unlock(&server->srv_mutex); |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 260 | |
| 261 | cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__); |
Paulo Alcantara | ae0abb4 | 2021-10-14 14:19:17 -0300 | [diff] [blame] | 262 | list_for_each_entry_safe(mid, nmid, &retry_list, qhead) { |
| 263 | list_del_init(&mid->qhead); |
| 264 | mid->callback(mid); |
| 265 | cifs_mid_q_entry_release(mid); |
Long Li | 214bab4 | 2019-04-05 21:36:35 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 268 | if (cifs_rdma_enabled(server)) { |
| 269 | mutex_lock(&server->srv_mutex); |
Long Li | 050b8c3 | 2019-04-04 11:35:42 -0500 | [diff] [blame] | 270 | smbd_destroy(server); |
Long Li | 1d2a4f5 | 2019-05-13 21:01:28 -0700 | [diff] [blame] | 271 | mutex_unlock(&server->srv_mutex); |
| 272 | } |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 273 | } |
| 274 | |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 275 | static bool cifs_tcp_ses_needs_reconnect(struct TCP_Server_Info *server, int num_targets) |
| 276 | { |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 277 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 278 | server->nr_targets = num_targets; |
| 279 | if (server->tcpStatus == CifsExiting) { |
| 280 | /* the demux thread will exit normally next time through the loop */ |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 281 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 282 | wake_up(&server->response_q); |
| 283 | return false; |
| 284 | } |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 285 | |
| 286 | cifs_dbg(FYI, "Mark tcp session as need reconnect\n"); |
| 287 | trace_smb3_reconnect(server->CurrentMid, server->conn_id, |
| 288 | server->hostname); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 289 | server->tcpStatus = CifsNeedReconnect; |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 290 | |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 291 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 295 | /* |
| 296 | * cifs tcp session reconnection |
| 297 | * |
| 298 | * mark tcp session as reconnecting so temporarily locked |
| 299 | * mark all smb sessions as reconnecting for tcp session |
| 300 | * reconnect tcp session |
| 301 | * wake up waiters on reconnection? - (not needed currently) |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 302 | * |
| 303 | * if mark_smb_session is passed as true, unconditionally mark |
| 304 | * the smb session (and tcon) for reconnect as well. This value |
| 305 | * doesn't really matter for non-multichannel scenario. |
| 306 | * |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 307 | */ |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 308 | static int __cifs_reconnect(struct TCP_Server_Info *server, |
| 309 | bool mark_smb_session) |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 310 | { |
| 311 | int rc = 0; |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 312 | |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 313 | if (!cifs_tcp_ses_needs_reconnect(server, 1)) |
| 314 | return 0; |
Paulo Alcantara | 43b459a | 2021-10-14 13:57:21 -0300 | [diff] [blame] | 315 | |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 316 | cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session); |
Jeff Layton | 3c1105d | 2011-05-22 07:09:13 -0400 | [diff] [blame] | 317 | |
Shyam Prasad N | 2a05137 | 2022-02-08 17:10:02 +0000 | [diff] [blame] | 318 | cifs_abort_connection(server); |
| 319 | |
Jeff Layton | 7fdbaa1 | 2011-06-10 16:14:57 -0400 | [diff] [blame] | 320 | do { |
Steve French | 6c3d890 | 2006-07-31 22:46:20 +0000 | [diff] [blame] | 321 | try_to_freeze(); |
Jeff Layton | 73e216a | 2013-09-05 08:38:10 -0400 | [diff] [blame] | 322 | mutex_lock(&server->srv_mutex); |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 323 | |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 324 | if (!cifs_swn_set_server_dstaddr(server)) { |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 325 | /* resolve the hostname again to make sure that IP address is up-to-date */ |
Shyam Prasad N | 4e456b3 | 2021-03-31 14:35:24 +0000 | [diff] [blame] | 326 | rc = reconn_set_ipaddr_from_hostname(server); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 327 | cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); |
Samuel Cabrero | 121d947 | 2020-11-30 19:02:56 +0100 | [diff] [blame] | 328 | } |
Paulo Alcantara | aaa3aef | 2020-05-19 15:38:27 -0300 | [diff] [blame] | 329 | |
Long Li | 781a805 | 2017-11-22 17:38:36 -0700 | [diff] [blame] | 330 | if (cifs_rdma_enabled(server)) |
| 331 | rc = smbd_reconnect(server); |
| 332 | else |
| 333 | rc = generic_ip_connect(server); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 334 | if (rc) { |
Federico Sauter | 4afe260 | 2015-03-17 17:45:28 +0100 | [diff] [blame] | 335 | mutex_unlock(&server->srv_mutex); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 336 | cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); |
Steve French | 0cb766a | 2005-04-28 22:41:11 -0700 | [diff] [blame] | 337 | msleep(3000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } else { |
| 339 | atomic_inc(&tcpSesReconnectCount); |
Pavel Shilovsky | 335b7b6 | 2019-01-16 11:12:41 -0800 | [diff] [blame] | 340 | set_credits(server, 1); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 341 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 469ee61 | 2008-10-16 18:46:39 +0000 | [diff] [blame] | 342 | if (server->tcpStatus != CifsExiting) |
Steve French | fd88ce9 | 2011-04-12 01:01:14 +0000 | [diff] [blame] | 343 | server->tcpStatus = CifsNeedNegotiate; |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 344 | spin_unlock(&cifs_tcp_ses_lock); |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 345 | cifs_swn_reset_server_dstaddr(server); |
Federico Sauter | 4afe260 | 2015-03-17 17:45:28 +0100 | [diff] [blame] | 346 | mutex_unlock(&server->srv_mutex); |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 347 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
Jeff Layton | 7fdbaa1 | 2011-06-10 16:14:57 -0400 | [diff] [blame] | 349 | } while (server->tcpStatus == CifsNeedReconnect); |
Jeff Layton | 2b84a36c | 2011-01-11 07:24:21 -0500 | [diff] [blame] | 350 | |
Shyam Prasad N | e154cb7 | 2022-01-16 11:19:36 +0000 | [diff] [blame] | 351 | spin_lock(&cifs_tcp_ses_lock); |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 352 | if (server->tcpStatus == CifsNeedNegotiate) |
| 353 | mod_delayed_work(cifsiod_wq, &server->echo, 0); |
Shyam Prasad N | e154cb7 | 2022-01-16 11:19:36 +0000 | [diff] [blame] | 354 | spin_unlock(&cifs_tcp_ses_lock); |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 355 | |
Stefan Metzmacher | e2e8751 | 2020-02-24 14:31:02 -0600 | [diff] [blame] | 356 | wake_up(&server->response_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return rc; |
| 358 | } |
| 359 | |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 360 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 361 | static int __reconnect_target_unlocked(struct TCP_Server_Info *server, const char *target) |
| 362 | { |
| 363 | int rc; |
| 364 | char *hostname; |
| 365 | |
| 366 | if (!cifs_swn_set_server_dstaddr(server)) { |
| 367 | if (server->hostname != target) { |
| 368 | hostname = extract_hostname(target); |
| 369 | if (!IS_ERR(hostname)) { |
| 370 | kfree(server->hostname); |
| 371 | server->hostname = hostname; |
| 372 | } else { |
| 373 | cifs_dbg(FYI, "%s: couldn't extract hostname or address from dfs target: %ld\n", |
| 374 | __func__, PTR_ERR(hostname)); |
| 375 | cifs_dbg(FYI, "%s: default to last target server: %s\n", __func__, |
| 376 | server->hostname); |
| 377 | } |
| 378 | } |
| 379 | /* resolve the hostname again to make sure that IP address is up-to-date. */ |
| 380 | rc = reconn_set_ipaddr_from_hostname(server); |
| 381 | cifs_dbg(FYI, "%s: reconn_set_ipaddr_from_hostname: rc=%d\n", __func__, rc); |
| 382 | } |
| 383 | /* Reconnect the socket */ |
| 384 | if (cifs_rdma_enabled(server)) |
| 385 | rc = smbd_reconnect(server); |
| 386 | else |
| 387 | rc = generic_ip_connect(server); |
| 388 | |
| 389 | return rc; |
| 390 | } |
| 391 | |
| 392 | static int reconnect_target_unlocked(struct TCP_Server_Info *server, struct dfs_cache_tgt_list *tl, |
| 393 | struct dfs_cache_tgt_iterator **target_hint) |
| 394 | { |
| 395 | int rc; |
| 396 | struct dfs_cache_tgt_iterator *tit; |
| 397 | |
| 398 | *target_hint = NULL; |
| 399 | |
| 400 | /* If dfs target list is empty, then reconnect to last server */ |
| 401 | tit = dfs_cache_get_tgt_iterator(tl); |
| 402 | if (!tit) |
| 403 | return __reconnect_target_unlocked(server, server->hostname); |
| 404 | |
| 405 | /* Otherwise, try every dfs target in @tl */ |
| 406 | for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) { |
| 407 | rc = __reconnect_target_unlocked(server, dfs_cache_get_tgt_name(tit)); |
| 408 | if (!rc) { |
| 409 | *target_hint = tit; |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | return rc; |
| 414 | } |
| 415 | |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 416 | static int |
| 417 | reconnect_dfs_server(struct TCP_Server_Info *server, |
| 418 | bool mark_smb_session) |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 419 | { |
| 420 | int rc = 0; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 421 | const char *refpath = server->current_fullpath + 1; |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 422 | struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 423 | struct dfs_cache_tgt_iterator *target_hint = NULL; |
| 424 | int num_targets = 0; |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 425 | |
| 426 | /* |
| 427 | * Determine the number of dfs targets the referral path in @cifs_sb resolves to. |
| 428 | * |
| 429 | * smb2_reconnect() needs to know how long it should wait based upon the number of dfs |
| 430 | * targets (server->nr_targets). It's also possible that the cached referral was cleared |
| 431 | * through /proc/fs/cifs/dfscache or the target list is empty due to server settings after |
| 432 | * refreshing the referral, so, in this case, default it to 1. |
| 433 | */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 434 | if (!dfs_cache_noreq_find(refpath, NULL, &tl)) |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 435 | num_targets = dfs_cache_get_nr_tgts(&tl); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 436 | if (!num_targets) |
| 437 | num_targets = 1; |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 438 | |
| 439 | if (!cifs_tcp_ses_needs_reconnect(server, num_targets)) |
| 440 | return 0; |
| 441 | |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 442 | cifs_mark_tcp_ses_conns_for_reconnect(server, mark_smb_session); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 443 | |
Shyam Prasad N | 2a05137 | 2022-02-08 17:10:02 +0000 | [diff] [blame] | 444 | cifs_abort_connection(server); |
| 445 | |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 446 | do { |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 447 | try_to_freeze(); |
| 448 | mutex_lock(&server->srv_mutex); |
| 449 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 450 | rc = reconnect_target_unlocked(server, &tl, &target_hint); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 451 | if (rc) { |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 452 | /* Failed to reconnect socket */ |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 453 | mutex_unlock(&server->srv_mutex); |
| 454 | cifs_dbg(FYI, "%s: reconnect error %d\n", __func__, rc); |
| 455 | msleep(3000); |
| 456 | continue; |
| 457 | } |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 458 | /* |
| 459 | * Socket was created. Update tcp session status to CifsNeedNegotiate so that a |
| 460 | * process waiting for reconnect will know it needs to re-establish session and tcon |
| 461 | * through the reconnected target server. |
| 462 | */ |
| 463 | atomic_inc(&tcpSesReconnectCount); |
| 464 | set_credits(server, 1); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 465 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 466 | if (server->tcpStatus != CifsExiting) |
| 467 | server->tcpStatus = CifsNeedNegotiate; |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 468 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 469 | cifs_swn_reset_server_dstaddr(server); |
| 470 | mutex_unlock(&server->srv_mutex); |
Shyam Prasad N | e154cb7 | 2022-01-16 11:19:36 +0000 | [diff] [blame] | 471 | mod_delayed_work(cifsiod_wq, &server->reconnect, 0); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 472 | } while (server->tcpStatus == CifsNeedReconnect); |
| 473 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 474 | if (target_hint) |
| 475 | dfs_cache_noreq_update_tgthint(refpath, target_hint); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 476 | |
| 477 | dfs_cache_free_tgts(&tl); |
| 478 | |
| 479 | /* Need to set up echo worker again once connection has been established */ |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 480 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 481 | if (server->tcpStatus == CifsNeedNegotiate) |
| 482 | mod_delayed_work(cifsiod_wq, &server->echo, 0); |
| 483 | |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 484 | spin_unlock(&cifs_tcp_ses_lock); |
| 485 | |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 486 | wake_up(&server->response_q); |
| 487 | return rc; |
| 488 | } |
| 489 | |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 490 | int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 491 | { |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 492 | /* If tcp session is not an dfs connection, then reconnect to last target server */ |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 493 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 494 | if (!server->is_dfs_conn || !server->origin_fullpath || !server->leaf_fullpath) { |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 495 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 496 | return __cifs_reconnect(server, mark_smb_session); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 497 | } |
| 498 | spin_unlock(&cifs_tcp_ses_lock); |
| 499 | |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 500 | return reconnect_dfs_server(server, mark_smb_session); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 501 | } |
| 502 | #else |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 503 | int cifs_reconnect(struct TCP_Server_Info *server, bool mark_smb_session) |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 504 | { |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 505 | return __cifs_reconnect(server, mark_smb_session); |
Paulo Alcantara | bbcce36 | 2021-10-14 17:49:54 -0300 | [diff] [blame] | 506 | } |
| 507 | #endif |
| 508 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 509 | static void |
| 510 | cifs_echo_request(struct work_struct *work) |
| 511 | { |
| 512 | int rc; |
| 513 | struct TCP_Server_Info *server = container_of(work, |
| 514 | struct TCP_Server_Info, echo.work); |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * We cannot send an echo if it is disabled. |
| 518 | * Also, no need to ping if we got a response recently. |
Jeff Layton | 247ec9b | 2011-02-04 17:09:50 -0500 | [diff] [blame] | 519 | */ |
Steve French | 4fcd181 | 2016-06-22 20:12:05 -0500 | [diff] [blame] | 520 | |
| 521 | if (server->tcpStatus == CifsNeedReconnect || |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 522 | server->tcpStatus == CifsExiting || |
| 523 | server->tcpStatus == CifsNew || |
Pavel Shilovsky | f6d7617 | 2012-05-25 14:47:16 +0400 | [diff] [blame] | 524 | (server->ops->can_echo && !server->ops->can_echo(server)) || |
Shyam Prasad N | 5b2abda | 2021-05-01 16:17:07 +0000 | [diff] [blame] | 525 | time_before(jiffies, server->lstrp + server->echo_interval - HZ)) |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 526 | goto requeue_echo; |
| 527 | |
Pavel Shilovsky | f6d7617 | 2012-05-25 14:47:16 +0400 | [diff] [blame] | 528 | rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS; |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 529 | if (rc) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 530 | cifs_dbg(FYI, "Unable to send echo request to server: %s\n", |
| 531 | server->hostname); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 532 | |
Samuel Cabrero | 21077c6 | 2020-11-30 19:02:54 +0100 | [diff] [blame] | 533 | /* Check witness registrations */ |
| 534 | cifs_swn_check(); |
Samuel Cabrero | 21077c6 | 2020-11-30 19:02:54 +0100 | [diff] [blame] | 535 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 536 | requeue_echo: |
Sachin Prabhu | b8c6001 | 2016-10-20 19:52:24 -0400 | [diff] [blame] | 537 | queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 538 | } |
| 539 | |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 540 | static bool |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 541 | allocate_buffers(struct TCP_Server_Info *server) |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 542 | { |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 543 | if (!server->bigbuf) { |
| 544 | server->bigbuf = (char *)cifs_buf_get(); |
| 545 | if (!server->bigbuf) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 546 | cifs_server_dbg(VFS, "No memory for large SMB response\n"); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 547 | msleep(3000); |
| 548 | /* retry will check if exiting */ |
| 549 | return false; |
| 550 | } |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 551 | } else if (server->large_buf) { |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 552 | /* we are reusing a dirty large buf, clear its start */ |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 553 | memset(server->bigbuf, 0, HEADER_SIZE(server)); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 554 | } |
| 555 | |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 556 | if (!server->smallbuf) { |
| 557 | server->smallbuf = (char *)cifs_small_buf_get(); |
| 558 | if (!server->smallbuf) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 559 | cifs_server_dbg(VFS, "No memory for SMB response\n"); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 560 | msleep(1000); |
| 561 | /* retry will check if exiting */ |
| 562 | return false; |
| 563 | } |
| 564 | /* beginning of smb buffer is cleared in our buf_get */ |
| 565 | } else { |
| 566 | /* if existing small buf clear beginning */ |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 567 | memset(server->smallbuf, 0, HEADER_SIZE(server)); |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 568 | } |
| 569 | |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 570 | return true; |
| 571 | } |
| 572 | |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 573 | static bool |
| 574 | server_unresponsive(struct TCP_Server_Info *server) |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 575 | { |
Pavel Shilovsky | 6dae51a | 2012-02-21 16:50:23 +0300 | [diff] [blame] | 576 | /* |
Ronnie Sahlberg | f2caf90 | 2019-07-06 06:52:46 +1000 | [diff] [blame] | 577 | * We need to wait 3 echo intervals to make sure we handle such |
Pavel Shilovsky | 6dae51a | 2012-02-21 16:50:23 +0300 | [diff] [blame] | 578 | * situations right: |
| 579 | * 1s client sends a normal SMB request |
Ronnie Sahlberg | becc2ba | 2019-07-24 11:43:49 +1000 | [diff] [blame] | 580 | * 2s client gets a response |
Pavel Shilovsky | 6dae51a | 2012-02-21 16:50:23 +0300 | [diff] [blame] | 581 | * 30s echo workqueue job pops, and decides we got a response recently |
| 582 | * and don't need to send another |
| 583 | * ... |
| 584 | * 65s kernel_recvmsg times out, and we see that we haven't gotten |
| 585 | * a response in >60s. |
| 586 | */ |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 587 | spin_lock(&cifs_tcp_ses_lock); |
Samuel Cabrero | 76e7527 | 2017-07-11 12:44:39 +0200 | [diff] [blame] | 588 | if ((server->tcpStatus == CifsGood || |
| 589 | server->tcpStatus == CifsNeedNegotiate) && |
Shyam Prasad N | f491664 | 2021-04-29 07:53:18 +0000 | [diff] [blame] | 590 | (!server->ops->can_echo || server->ops->can_echo(server)) && |
Ronnie Sahlberg | f2caf90 | 2019-07-06 06:52:46 +1000 | [diff] [blame] | 591 | time_after(jiffies, server->lstrp + 3 * server->echo_interval)) { |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 592 | spin_unlock(&cifs_tcp_ses_lock); |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 593 | cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n", |
| 594 | (3 * server->echo_interval) / HZ); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 595 | cifs_reconnect(server, false); |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 596 | return true; |
| 597 | } |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 598 | spin_unlock(&cifs_tcp_ses_lock); |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 599 | |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 600 | return false; |
| 601 | } |
| 602 | |
Pavel Shilovsky | ef68e83 | 2019-01-18 17:25:36 -0800 | [diff] [blame] | 603 | static inline bool |
| 604 | zero_credits(struct TCP_Server_Info *server) |
| 605 | { |
| 606 | int val; |
| 607 | |
| 608 | spin_lock(&server->req_lock); |
| 609 | val = server->credits + server->echo_credits + server->oplock_credits; |
| 610 | if (server->in_flight == 0 && val == 0) { |
| 611 | spin_unlock(&server->req_lock); |
| 612 | return true; |
| 613 | } |
| 614 | spin_unlock(&server->req_lock); |
| 615 | return false; |
| 616 | } |
| 617 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 618 | static int |
| 619 | cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg) |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 620 | { |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 621 | int length = 0; |
| 622 | int total_read; |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 623 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 624 | smb_msg->msg_control = NULL; |
| 625 | smb_msg->msg_controllen = 0; |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 626 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 627 | for (total_read = 0; msg_data_left(smb_msg); total_read += length) { |
Jeff Layton | 95edcff | 2011-12-01 20:22:41 -0500 | [diff] [blame] | 628 | try_to_freeze(); |
| 629 | |
Pavel Shilovsky | ef68e83 | 2019-01-18 17:25:36 -0800 | [diff] [blame] | 630 | /* reconnect if no credits and no requests in flight */ |
| 631 | if (zero_credits(server)) { |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 632 | cifs_reconnect(server, false); |
Pavel Shilovsky | ef68e83 | 2019-01-18 17:25:36 -0800 | [diff] [blame] | 633 | return -ECONNABORTED; |
| 634 | } |
| 635 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 636 | if (server_unresponsive(server)) |
| 637 | return -ECONNABORTED; |
Long Li | 2fef137 | 2017-11-22 17:38:41 -0700 | [diff] [blame] | 638 | if (cifs_rdma_enabled(server) && server->smbd_conn) |
| 639 | length = smbd_recv(server->smbd_conn, smb_msg); |
| 640 | else |
| 641 | length = sock_recvmsg(server->ssocket, smb_msg, 0); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 642 | |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 643 | spin_lock(&cifs_tcp_ses_lock); |
| 644 | if (server->tcpStatus == CifsExiting) { |
| 645 | spin_unlock(&cifs_tcp_ses_lock); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 646 | return -ESHUTDOWN; |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 647 | } |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 648 | |
| 649 | if (server->tcpStatus == CifsNeedReconnect) { |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 650 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | a81da65 | 2022-02-08 19:28:28 +0000 | [diff] [blame] | 651 | cifs_reconnect(server, false); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 652 | return -ECONNABORTED; |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 653 | } |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 654 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | ba749e6 | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 655 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 656 | if (length == -ERESTARTSYS || |
| 657 | length == -EAGAIN || |
| 658 | length == -EINTR) { |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 659 | /* |
| 660 | * Minimum sleep to prevent looping, allowing socket |
| 661 | * to clear and app threads to set tcpStatus |
| 662 | * CifsNeedReconnect if server hung. |
| 663 | */ |
| 664 | usleep_range(1000, 2000); |
| 665 | length = 0; |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 666 | continue; |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | if (length <= 0) { |
Al Viro | 09aab88 | 2015-11-13 03:00:17 -0500 | [diff] [blame] | 670 | cifs_dbg(FYI, "Received no data or error: %d\n", length); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 671 | cifs_reconnect(server, false); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 672 | return -ECONNABORTED; |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 673 | } |
| 674 | } |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 675 | return total_read; |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 676 | } |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 677 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 678 | int |
| 679 | cifs_read_from_socket(struct TCP_Server_Info *server, char *buf, |
| 680 | unsigned int to_read) |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 681 | { |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 682 | struct msghdr smb_msg; |
| 683 | struct kvec iov = {.iov_base = buf, .iov_len = to_read}; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 684 | iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read); |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 685 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 686 | return cifs_readv_from_socket(server, &smb_msg); |
| 687 | } |
Jeff Layton | 42c4dfc | 2011-10-19 15:28:17 -0400 | [diff] [blame] | 688 | |
David Howells | cf0604a | 2021-02-04 00:15:21 -0600 | [diff] [blame] | 689 | ssize_t |
| 690 | cifs_discard_from_socket(struct TCP_Server_Info *server, size_t to_read) |
| 691 | { |
| 692 | struct msghdr smb_msg; |
| 693 | |
| 694 | /* |
| 695 | * iov_iter_discard already sets smb_msg.type and count and iov_offset |
| 696 | * and cifs_readv_from_socket sets msg_control and msg_controllen |
| 697 | * so little to initialize in struct msghdr |
| 698 | */ |
| 699 | smb_msg.msg_name = NULL; |
| 700 | smb_msg.msg_namelen = 0; |
| 701 | iov_iter_discard(&smb_msg.msg_iter, READ, to_read); |
| 702 | |
| 703 | return cifs_readv_from_socket(server, &smb_msg); |
| 704 | } |
| 705 | |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 706 | int |
| 707 | cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page, |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 708 | unsigned int page_offset, unsigned int to_read) |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 709 | { |
| 710 | struct msghdr smb_msg; |
Long Li | 1dbe346 | 2018-05-30 12:47:55 -0700 | [diff] [blame] | 711 | struct bio_vec bv = { |
| 712 | .bv_page = page, .bv_len = to_read, .bv_offset = page_offset}; |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 713 | iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read); |
Al Viro | 7133566 | 2016-01-09 19:54:50 -0500 | [diff] [blame] | 714 | return cifs_readv_from_socket(server, &smb_msg); |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 715 | } |
| 716 | |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 717 | static bool |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 718 | is_smb_response(struct TCP_Server_Info *server, unsigned char type) |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 719 | { |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 720 | /* |
| 721 | * The first byte big endian of the length field, |
| 722 | * is actually not part of the length but the type |
| 723 | * with the most common, zero, as regular data. |
| 724 | */ |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 725 | switch (type) { |
| 726 | case RFC1002_SESSION_MESSAGE: |
| 727 | /* Regular SMB response */ |
| 728 | return true; |
| 729 | case RFC1002_SESSION_KEEP_ALIVE: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 730 | cifs_dbg(FYI, "RFC 1002 session keep alive\n"); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 731 | break; |
| 732 | case RFC1002_POSITIVE_SESSION_RESPONSE: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 733 | cifs_dbg(FYI, "RFC 1002 positive session response\n"); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 734 | break; |
| 735 | case RFC1002_NEGATIVE_SESSION_RESPONSE: |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 736 | /* |
| 737 | * We get this from Windows 98 instead of an error on |
| 738 | * SMB negprot response. |
| 739 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 740 | cifs_dbg(FYI, "RFC 1002 negative session response\n"); |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 741 | /* give server a second to clean up */ |
| 742 | msleep(1000); |
| 743 | /* |
| 744 | * Always try 445 first on reconnect since we get NACK |
| 745 | * on some if we ever connected to port 139 (the NACK |
| 746 | * is since we do not begin with RFC1001 session |
| 747 | * initialize frame). |
| 748 | */ |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 749 | cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 750 | cifs_reconnect(server, true); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 751 | break; |
| 752 | default: |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 753 | cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 754 | cifs_reconnect(server, true); |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 755 | } |
| 756 | |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 757 | return false; |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 758 | } |
| 759 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 760 | void |
| 761 | dequeue_mid(struct mid_q_entry *mid, bool malformed) |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 762 | { |
| 763 | #ifdef CONFIG_CIFS_STATS2 |
| 764 | mid->when_received = jiffies; |
| 765 | #endif |
| 766 | spin_lock(&GlobalMid_Lock); |
| 767 | if (!malformed) |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 768 | mid->mid_state = MID_RESPONSE_RECEIVED; |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 769 | else |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 770 | mid->mid_state = MID_RESPONSE_MALFORMED; |
Ronnie Sahlberg | ddf83af | 2018-08-30 10:12:59 +1000 | [diff] [blame] | 771 | /* |
| 772 | * Trying to handle/dequeue a mid after the send_recv() |
| 773 | * function has finished processing it is a bug. |
| 774 | */ |
Steve French | 9e7ffa7 | 2021-11-12 09:55:03 -0600 | [diff] [blame] | 775 | if (mid->mid_flags & MID_DELETED) { |
| 776 | spin_unlock(&GlobalMid_Lock); |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 777 | pr_warn_once("trying to dequeue a deleted mid\n"); |
Steve French | 9e7ffa7 | 2021-11-12 09:55:03 -0600 | [diff] [blame] | 778 | } else { |
Ronnie Sahlberg | ddf83af | 2018-08-30 10:12:59 +1000 | [diff] [blame] | 779 | list_del_init(&mid->qhead); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 780 | mid->mid_flags |= MID_DELETED; |
Steve French | 9e7ffa7 | 2021-11-12 09:55:03 -0600 | [diff] [blame] | 781 | spin_unlock(&GlobalMid_Lock); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 782 | } |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 783 | } |
| 784 | |
Pavel Shilovsky | 86a7964 | 2019-11-21 11:35:13 -0800 | [diff] [blame] | 785 | static unsigned int |
| 786 | smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) |
| 787 | { |
Ronnie Sahlberg | 0d35e38 | 2021-11-05 08:39:01 +0900 | [diff] [blame] | 788 | struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; |
Pavel Shilovsky | 86a7964 | 2019-11-21 11:35:13 -0800 | [diff] [blame] | 789 | |
| 790 | /* |
| 791 | * SMB1 does not use credits. |
| 792 | */ |
| 793 | if (server->vals->header_preamble_size) |
| 794 | return 0; |
| 795 | |
| 796 | return le16_to_cpu(shdr->CreditRequest); |
| 797 | } |
| 798 | |
Jeff Layton | c8054eb | 2011-10-19 15:29:31 -0400 | [diff] [blame] | 799 | static void |
| 800 | handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server, |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 801 | char *buf, int malformed) |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 802 | { |
Pavel Shilovsky | 316cf94 | 2012-05-23 14:31:03 +0400 | [diff] [blame] | 803 | if (server->ops->check_trans2 && |
| 804 | server->ops->check_trans2(mid, server, buf, malformed)) |
Jeff Layton | c8054eb | 2011-10-19 15:29:31 -0400 | [diff] [blame] | 805 | return; |
Pavel Shilovsky | 86a7964 | 2019-11-21 11:35:13 -0800 | [diff] [blame] | 806 | mid->credits_received = smb2_get_credits_from_hdr(buf, server); |
Jeff Layton | ea1f450 | 2011-10-19 15:29:05 -0400 | [diff] [blame] | 807 | mid->resp_buf = buf; |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 808 | mid->large_buf = server->large_buf; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 809 | /* Was previous buf put in mpx struct for multi-rsp? */ |
| 810 | if (!mid->multiRsp) { |
| 811 | /* smb buffer will be freed by user thread */ |
| 812 | if (server->large_buf) |
| 813 | server->bigbuf = NULL; |
| 814 | else |
| 815 | server->smallbuf = NULL; |
| 816 | } |
Jeff Layton | ffc00e2 | 2011-10-19 15:29:13 -0400 | [diff] [blame] | 817 | dequeue_mid(mid, malformed); |
Pavel Shilovsky | ad69bae | 2011-08-01 13:19:43 +0400 | [diff] [blame] | 818 | } |
| 819 | |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 820 | static void clean_demultiplex_info(struct TCP_Server_Info *server) |
| 821 | { |
| 822 | int length; |
| 823 | |
| 824 | /* take it off the list, if it's not already */ |
| 825 | spin_lock(&cifs_tcp_ses_lock); |
| 826 | list_del_init(&server->tcp_ses_list); |
| 827 | spin_unlock(&cifs_tcp_ses_lock); |
| 828 | |
Paulo Alcantara | 2122533 | 2020-11-28 16:54:02 -0300 | [diff] [blame] | 829 | cancel_delayed_work_sync(&server->echo); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 830 | cancel_delayed_work_sync(&server->resolve); |
Paulo Alcantara | 2122533 | 2020-11-28 16:54:02 -0300 | [diff] [blame] | 831 | |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 832 | spin_lock(&cifs_tcp_ses_lock); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 833 | server->tcpStatus = CifsExiting; |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 834 | spin_unlock(&cifs_tcp_ses_lock); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 835 | wake_up_all(&server->response_q); |
| 836 | |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 837 | /* check if we have blocked requests that need to free */ |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 838 | spin_lock(&server->req_lock); |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 839 | if (server->credits <= 0) |
| 840 | server->credits = 1; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 841 | spin_unlock(&server->req_lock); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 842 | /* |
| 843 | * Although there should not be any requests blocked on this queue it |
| 844 | * can not hurt to be paranoid and try to wake up requests that may |
| 845 | * haven been blocked when more than 50 at time were on the wire to the |
| 846 | * same server - they now will see the session is in exit state and get |
| 847 | * out of SendReceive. |
| 848 | */ |
| 849 | wake_up_all(&server->request_q); |
| 850 | /* give those requests time to exit */ |
| 851 | msleep(125); |
Long Li | 050b8c3 | 2019-04-04 11:35:42 -0500 | [diff] [blame] | 852 | if (cifs_rdma_enabled(server)) |
| 853 | smbd_destroy(server); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 854 | if (server->ssocket) { |
| 855 | sock_release(server->ssocket); |
| 856 | server->ssocket = NULL; |
| 857 | } |
| 858 | |
| 859 | if (!list_empty(&server->pending_mid_q)) { |
| 860 | struct list_head dispose_list; |
| 861 | struct mid_q_entry *mid_entry; |
| 862 | struct list_head *tmp, *tmp2; |
| 863 | |
| 864 | INIT_LIST_HEAD(&dispose_list); |
| 865 | spin_lock(&GlobalMid_Lock); |
| 866 | list_for_each_safe(tmp, tmp2, &server->pending_mid_q) { |
| 867 | mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
Paulo Alcantara | bf1bc694 | 2021-03-08 12:00:47 -0300 | [diff] [blame] | 868 | cifs_dbg(FYI, "Clearing mid %llu\n", mid_entry->mid); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 869 | kref_get(&mid_entry->refcount); |
Pavel Shilovsky | 7c9421e | 2012-03-23 14:28:03 -0400 | [diff] [blame] | 870 | mid_entry->mid_state = MID_SHUTDOWN; |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 871 | list_move(&mid_entry->qhead, &dispose_list); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 872 | mid_entry->mid_flags |= MID_DELETED; |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 873 | } |
| 874 | spin_unlock(&GlobalMid_Lock); |
| 875 | |
| 876 | /* now walk dispose list and issue callbacks */ |
| 877 | list_for_each_safe(tmp, tmp2, &dispose_list) { |
| 878 | mid_entry = list_entry(tmp, struct mid_q_entry, qhead); |
Paulo Alcantara | bf1bc694 | 2021-03-08 12:00:47 -0300 | [diff] [blame] | 879 | cifs_dbg(FYI, "Callback mid %llu\n", mid_entry->mid); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 880 | list_del_init(&mid_entry->qhead); |
| 881 | mid_entry->callback(mid_entry); |
Pavel Shilovsky | abe5707 | 2019-10-22 08:41:42 -0700 | [diff] [blame] | 882 | cifs_mid_q_entry_release(mid_entry); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 883 | } |
| 884 | /* 1/8th of sec is more than enough time for them to exit */ |
| 885 | msleep(125); |
| 886 | } |
| 887 | |
| 888 | if (!list_empty(&server->pending_mid_q)) { |
| 889 | /* |
| 890 | * mpx threads have not exited yet give them at least the smb |
| 891 | * send timeout time for long ops. |
| 892 | * |
| 893 | * Due to delays on oplock break requests, we need to wait at |
| 894 | * least 45 seconds before giving up on a request getting a |
| 895 | * response and going ahead and killing cifsd. |
| 896 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 897 | cifs_dbg(FYI, "Wait for exit from demultiplex thread\n"); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 898 | msleep(46000); |
| 899 | /* |
| 900 | * If threads still have not exited they are probably never |
| 901 | * coming home not much else we can do but free the memory. |
| 902 | */ |
| 903 | } |
| 904 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 905 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 906 | kfree(server->origin_fullpath); |
| 907 | kfree(server->leaf_fullpath); |
| 908 | #endif |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 909 | kfree(server); |
| 910 | |
| 911 | length = atomic_dec_return(&tcpSesAllocCount); |
| 912 | if (length > 0) |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 913 | mempool_resize(cifs_req_poolp, length + cifs_min_rcv); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 914 | } |
| 915 | |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 916 | static int |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 917 | standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 918 | { |
| 919 | int length; |
| 920 | char *buf = server->smallbuf; |
Ronnie Sahlberg | 2e96467 | 2018-04-09 18:06:26 +1000 | [diff] [blame] | 921 | unsigned int pdu_length = server->pdu_size; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 922 | |
| 923 | /* make sure this will fit in a large buffer */ |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 924 | if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - |
| 925 | server->vals->header_preamble_size) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 926 | cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 927 | cifs_reconnect(server, true); |
Pavel Shilovsky | 3fabaa2 | 2014-07-10 09:55:52 +0400 | [diff] [blame] | 928 | return -ECONNABORTED; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /* switch to large buffer if too big for a small one */ |
| 932 | if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) { |
| 933 | server->large_buf = true; |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 934 | memcpy(server->bigbuf, buf, server->total_read); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 935 | buf = server->bigbuf; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /* now read the rest */ |
Pavel Shilovsky | 1887f60 | 2012-05-17 12:45:31 +0400 | [diff] [blame] | 939 | length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 940 | pdu_length - HEADER_SIZE(server) + 1 |
| 941 | + server->vals->header_preamble_size); |
| 942 | |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 943 | if (length < 0) |
| 944 | return length; |
| 945 | server->total_read += length; |
| 946 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 947 | dump_smb(buf, server->total_read); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 948 | |
Pavel Shilovsky | 4326ed2 | 2016-11-17 15:24:46 -0800 | [diff] [blame] | 949 | return cifs_handle_standard(server, mid); |
| 950 | } |
| 951 | |
| 952 | int |
| 953 | cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid) |
| 954 | { |
| 955 | char *buf = server->large_buf ? server->bigbuf : server->smallbuf; |
| 956 | int length; |
| 957 | |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 958 | /* |
| 959 | * We know that we received enough to get to the MID as we |
| 960 | * checked the pdu_length earlier. Now check to see |
| 961 | * if the rest of the header is OK. We borrow the length |
| 962 | * var for the rest of the loop to avoid a new stack var. |
| 963 | * |
| 964 | * 48 bytes is enough to display the header and a little bit |
| 965 | * into the payload for debugging purposes. |
| 966 | */ |
Steve French | 373512e | 2015-12-18 13:05:30 -0600 | [diff] [blame] | 967 | length = server->ops->check_message(buf, server->total_read, server); |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 968 | if (length != 0) |
| 969 | cifs_dump_mem("Bad SMB: ", buf, |
| 970 | min_t(unsigned int, server->total_read, 48)); |
| 971 | |
Pavel Shilovsky | 511c54a | 2017-07-08 14:32:00 -0700 | [diff] [blame] | 972 | if (server->ops->is_session_expired && |
| 973 | server->ops->is_session_expired(buf)) { |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 974 | cifs_reconnect(server, true); |
Pavel Shilovsky | 511c54a | 2017-07-08 14:32:00 -0700 | [diff] [blame] | 975 | return -1; |
| 976 | } |
| 977 | |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 978 | if (server->ops->is_status_pending && |
Pavel Shilovsky | 66265f1 | 2019-01-23 17:11:16 -0800 | [diff] [blame] | 979 | server->ops->is_status_pending(buf, server)) |
Pavel Shilovsky | 2e44b28 | 2012-09-18 16:20:33 -0700 | [diff] [blame] | 980 | return -1; |
| 981 | |
Jeff Layton | ff4fa4a | 2012-02-07 06:31:05 -0500 | [diff] [blame] | 982 | if (!mid) |
| 983 | return length; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 984 | |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 985 | handle_mid(mid, server, buf, length); |
Jeff Layton | ff4fa4a | 2012-02-07 06:31:05 -0500 | [diff] [blame] | 986 | return 0; |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 987 | } |
| 988 | |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 989 | static void |
| 990 | smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server) |
| 991 | { |
Ronnie Sahlberg | 0d35e38 | 2021-11-05 08:39:01 +0900 | [diff] [blame] | 992 | struct smb2_hdr *shdr = (struct smb2_hdr *)buffer; |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 993 | int scredits, in_flight; |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * SMB1 does not use credits. |
| 997 | */ |
| 998 | if (server->vals->header_preamble_size) |
| 999 | return; |
| 1000 | |
| 1001 | if (shdr->CreditRequest) { |
| 1002 | spin_lock(&server->req_lock); |
| 1003 | server->credits += le16_to_cpu(shdr->CreditRequest); |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 1004 | scredits = server->credits; |
| 1005 | in_flight = server->in_flight; |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 1006 | spin_unlock(&server->req_lock); |
| 1007 | wake_up(&server->request_q); |
Shyam Prasad N | cd7b699 | 2020-11-12 08:56:49 -0800 | [diff] [blame] | 1008 | |
| 1009 | trace_smb3_add_credits(server->CurrentMid, |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 1010 | server->conn_id, server->hostname, scredits, |
| 1011 | le16_to_cpu(shdr->CreditRequest), in_flight); |
Shyam Prasad N | cd7b699 | 2020-11-12 08:56:49 -0800 | [diff] [blame] | 1012 | cifs_server_dbg(FYI, "%s: added %u credits total=%d\n", |
| 1013 | __func__, le16_to_cpu(shdr->CreditRequest), |
| 1014 | scredits); |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | |
Jeff Layton | e9097ab | 2011-10-19 15:29:40 -0400 | [diff] [blame] | 1019 | static int |
Al Viro | 7c97c20 | 2011-06-21 08:51:28 -0400 | [diff] [blame] | 1020 | cifs_demultiplex_thread(void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | { |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1022 | int i, num_mids, length; |
Al Viro | 7c97c20 | 2011-06-21 08:51:28 -0400 | [diff] [blame] | 1023 | struct TCP_Server_Info *server = p; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1024 | unsigned int pdu_length; |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1025 | unsigned int next_offset; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1026 | char *buf = NULL; |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1027 | struct task_struct *task_to_wake = NULL; |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1028 | struct mid_q_entry *mids[MAX_COMPOUND]; |
| 1029 | char *bufs[MAX_COMPOUND]; |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 1030 | unsigned int noreclaim_flag, num_io_timeout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
Eric Biggers | dc92027 | 2020-03-08 22:58:20 -0700 | [diff] [blame] | 1032 | noreclaim_flag = memalloc_noreclaim_save(); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1033 | cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current)); |
Jeff Layton | 93d0ec8 | 2008-08-02 08:00:48 -0400 | [diff] [blame] | 1034 | |
| 1035 | length = atomic_inc_return(&tcpSesAllocCount); |
| 1036 | if (length > 1) |
David Rientjes | 11d8336 | 2015-04-14 15:48:21 -0700 | [diff] [blame] | 1037 | mempool_resize(cifs_req_poolp, length + cifs_min_rcv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 1039 | set_freezable(); |
Eric W. Biederman | 33da8e7 | 2019-08-16 12:33:54 -0500 | [diff] [blame] | 1040 | allow_kernel_signal(SIGKILL); |
Jeff Layton | 469ee61 | 2008-10-16 18:46:39 +0000 | [diff] [blame] | 1041 | while (server->tcpStatus != CifsExiting) { |
Steve French | ede1327 | 2005-08-30 20:10:14 -0700 | [diff] [blame] | 1042 | if (try_to_freeze()) |
| 1043 | continue; |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 1044 | |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1045 | if (!allocate_buffers(server)) |
Pavel Shilovsky | 3d9c247 | 2011-08-01 13:19:40 +0400 | [diff] [blame] | 1046 | continue; |
Steve French | b8643e1 | 2005-04-28 22:41:07 -0700 | [diff] [blame] | 1047 | |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1048 | server->large_buf = false; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1049 | buf = server->smallbuf; |
Steve French | f01d5e1 | 2007-08-30 21:13:31 +0000 | [diff] [blame] | 1050 | pdu_length = 4; /* enough to get RFC1001 header */ |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 1051 | |
Jeff Layton | e28bc5b | 2011-10-19 15:30:07 -0400 | [diff] [blame] | 1052 | length = cifs_read_from_socket(server, buf, pdu_length); |
Jeff Layton | a52c1eb | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 1053 | if (length < 0) |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 1054 | continue; |
Ronnie Sahlberg | 977b617 | 2018-06-01 10:53:02 +1000 | [diff] [blame] | 1055 | |
| 1056 | if (server->vals->header_preamble_size == 0) |
| 1057 | server->total_read = 0; |
| 1058 | else |
| 1059 | server->total_read = length; |
Steve French | 67010fb | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1060 | |
Pavel Shilovsky | 98bac62 | 2011-08-01 13:19:42 +0400 | [diff] [blame] | 1061 | /* |
| 1062 | * The right amount was read from socket - 4 bytes, |
| 1063 | * so we can now interpret the length field. |
| 1064 | */ |
Pavel Shilovsky | d4e4854 | 2012-03-23 14:28:02 -0400 | [diff] [blame] | 1065 | pdu_length = get_rfc1002_length(buf); |
Steve French | 46810cb | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1066 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1067 | cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length); |
Jeff Layton | fe11e4c | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 1068 | if (!is_smb_response(server, buf[0])) |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1069 | continue; |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1070 | next_pdu: |
| 1071 | server->pdu_size = pdu_length; |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1072 | |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 1073 | /* make sure we have enough to get to the MID */ |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1074 | if (server->pdu_size < HEADER_SIZE(server) - 1 - |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 1075 | server->vals->header_preamble_size) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1076 | cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n", |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1077 | server->pdu_size); |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 1078 | cifs_reconnect(server, true); |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 1079 | continue; |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1080 | } |
Pavel Shilovsky | e7015fb | 2011-08-01 13:19:41 +0400 | [diff] [blame] | 1081 | |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 1082 | /* read down to the MID */ |
Ronnie Sahlberg | 93012bf | 2018-03-31 11:45:31 +1100 | [diff] [blame] | 1083 | length = cifs_read_from_socket(server, |
| 1084 | buf + server->vals->header_preamble_size, |
| 1085 | HEADER_SIZE(server) - 1 |
| 1086 | - server->vals->header_preamble_size); |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 1087 | if (length < 0) |
| 1088 | continue; |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1089 | server->total_read += length; |
Jeff Layton | 89482a5 | 2011-10-19 15:28:57 -0400 | [diff] [blame] | 1090 | |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1091 | if (server->ops->next_header) { |
| 1092 | next_offset = server->ops->next_header(buf); |
| 1093 | if (next_offset) |
| 1094 | server->pdu_size = next_offset; |
| 1095 | } |
| 1096 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1097 | memset(mids, 0, sizeof(mids)); |
| 1098 | memset(bufs, 0, sizeof(bufs)); |
| 1099 | num_mids = 0; |
| 1100 | |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1101 | if (server->ops->is_transform_hdr && |
| 1102 | server->ops->receive_transform && |
| 1103 | server->ops->is_transform_hdr(buf)) { |
| 1104 | length = server->ops->receive_transform(server, |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1105 | mids, |
| 1106 | bufs, |
| 1107 | &num_mids); |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1108 | } else { |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1109 | mids[0] = server->ops->find_mid(server, buf); |
| 1110 | bufs[0] = buf; |
Steve French | 7af929d | 2018-10-02 18:54:09 -0500 | [diff] [blame] | 1111 | num_mids = 1; |
Jeff Layton | c8054eb | 2011-10-19 15:29:31 -0400 | [diff] [blame] | 1112 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1113 | if (!mids[0] || !mids[0]->receive) |
| 1114 | length = standard_receive3(server, mids[0]); |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1115 | else |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1116 | length = mids[0]->receive(server, mids[0]); |
Pavel Shilovsky | 9bb17e0 | 2016-11-17 15:24:34 -0800 | [diff] [blame] | 1117 | } |
Jeff Layton | 44d22d8 | 2011-10-19 15:29:49 -0400 | [diff] [blame] | 1118 | |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 1119 | if (length < 0) { |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1120 | for (i = 0; i < num_mids; i++) |
| 1121 | if (mids[i]) |
| 1122 | cifs_mid_q_entry_release(mids[i]); |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1123 | continue; |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 1124 | } |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1125 | |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 1126 | if (server->ops->is_status_io_timeout && |
| 1127 | server->ops->is_status_io_timeout(buf)) { |
| 1128 | num_io_timeout++; |
| 1129 | if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) { |
Shyam Prasad N | 183eea2 | 2021-07-19 14:14:46 +0000 | [diff] [blame] | 1130 | cifs_reconnect(server, false); |
Rohith Surabattula | 8e670f7 | 2020-09-18 05:37:28 +0000 | [diff] [blame] | 1131 | num_io_timeout = 0; |
| 1132 | continue; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 1136 | server->lstrp = jiffies; |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1137 | |
| 1138 | for (i = 0; i < num_mids; i++) { |
| 1139 | if (mids[i] != NULL) { |
| 1140 | mids[i]->resp_buf_size = server->pdu_size; |
Sachin Prabhu | 38bd490 | 2017-03-03 15:41:38 -0800 | [diff] [blame] | 1141 | |
Rohith Surabattula | 9e550b0 | 2021-02-16 10:40:45 +0000 | [diff] [blame] | 1142 | if (bufs[i] && server->ops->is_network_name_deleted) |
| 1143 | server->ops->is_network_name_deleted(bufs[i], |
| 1144 | server); |
| 1145 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1146 | if (!mids[i]->multiRsp || mids[i]->multiEnd) |
| 1147 | mids[i]->callback(mids[i]); |
Lars Persson | 696e420 | 2018-06-25 14:05:25 +0200 | [diff] [blame] | 1148 | |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1149 | cifs_mid_q_entry_release(mids[i]); |
| 1150 | } else if (server->ops->is_oplock_break && |
| 1151 | server->ops->is_oplock_break(bufs[i], |
| 1152 | server)) { |
Ronnie Sahlberg | eca0045 | 2019-02-05 12:56:44 +1000 | [diff] [blame] | 1153 | smb2_add_credits_from_hdr(bufs[i], server); |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1154 | cifs_dbg(FYI, "Received oplock break\n"); |
| 1155 | } else { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1156 | cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n", |
| 1157 | atomic_read(&midCount)); |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1158 | cifs_dump_mem("Received Data is: ", bufs[i], |
| 1159 | HEADER_SIZE(server)); |
Ronnie Sahlberg | 3e27257 | 2019-07-06 06:43:08 +1000 | [diff] [blame] | 1160 | smb2_add_credits_from_hdr(bufs[i], server); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1161 | #ifdef CONFIG_CIFS_DEBUG2 |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1162 | if (server->ops->dump_detail) |
| 1163 | server->ops->dump_detail(bufs[i], |
| 1164 | server); |
| 1165 | cifs_dump_mids(server); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1166 | #endif /* CIFS_DEBUG2 */ |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1167 | } |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1168 | } |
Ronnie Sahlberg | b24df3e | 2018-08-08 15:07:45 +1000 | [diff] [blame] | 1169 | |
Ronnie Sahlberg | 8ce79ec | 2018-06-01 10:53:08 +1000 | [diff] [blame] | 1170 | if (pdu_length > server->pdu_size) { |
| 1171 | if (!allocate_buffers(server)) |
| 1172 | continue; |
| 1173 | pdu_length -= server->pdu_size; |
| 1174 | server->total_read = 0; |
| 1175 | server->large_buf = false; |
| 1176 | buf = server->smallbuf; |
| 1177 | goto next_pdu; |
Steve French | e4eb295 | 2005-04-28 22:41:09 -0700 | [diff] [blame] | 1178 | } |
| 1179 | } /* end while !EXITING */ |
| 1180 | |
Justin P. Mattock | fd62cb7 | 2011-02-24 22:15:02 -0800 | [diff] [blame] | 1181 | /* buffer usually freed in free_mid - need to free it here on exit */ |
Jeff Layton | 2a37ef9 | 2011-10-19 15:29:23 -0400 | [diff] [blame] | 1182 | cifs_buf_release(server->bigbuf); |
| 1183 | if (server->smallbuf) /* no sense logging a debug message if NULL */ |
| 1184 | cifs_small_buf_release(server->smallbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1186 | task_to_wake = xchg(&server->tsk, NULL); |
Pavel Shilovsky | 762dfd1 | 2011-08-01 13:19:44 +0400 | [diff] [blame] | 1187 | clean_demultiplex_info(server); |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1188 | |
| 1189 | /* if server->tsk was NULL then wait for a signal before exiting */ |
| 1190 | if (!task_to_wake) { |
| 1191 | set_current_state(TASK_INTERRUPTIBLE); |
| 1192 | while (!signal_pending(current)) { |
| 1193 | schedule(); |
| 1194 | set_current_state(TASK_INTERRUPTIBLE); |
| 1195 | } |
| 1196 | set_current_state(TASK_RUNNING); |
| 1197 | } |
| 1198 | |
Eric Biggers | dc92027 | 2020-03-08 22:58:20 -0700 | [diff] [blame] | 1199 | memalloc_noreclaim_restore(noreclaim_flag); |
Eric W. Biederman | ca3574b | 2021-12-03 11:00:19 -0600 | [diff] [blame] | 1200 | module_put_and_kthread_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1203 | /* |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 1204 | * Returns true if srcaddr isn't specified and rhs isn't specified, or |
| 1205 | * if srcaddr is specified and matches the IP address of the rhs argument |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1206 | */ |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 1207 | bool |
| 1208 | cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs) |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1209 | { |
| 1210 | switch (srcaddr->sa_family) { |
| 1211 | case AF_UNSPEC: |
| 1212 | return (rhs->sa_family == AF_UNSPEC); |
| 1213 | case AF_INET: { |
| 1214 | struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr; |
| 1215 | struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs; |
| 1216 | return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr); |
| 1217 | } |
| 1218 | case AF_INET6: { |
| 1219 | struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr; |
Nickolai Zeldovich | e3e2775 | 2013-01-16 21:36:17 -0500 | [diff] [blame] | 1220 | struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs; |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1221 | return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr); |
| 1222 | } |
| 1223 | default: |
| 1224 | WARN_ON(1); |
| 1225 | return false; /* don't expect to be here */ |
| 1226 | } |
| 1227 | } |
| 1228 | |
Pavel Shilovsky | 4b88613 | 2010-12-13 22:18:07 +0300 | [diff] [blame] | 1229 | /* |
| 1230 | * If no port is specified in addr structure, we try to match with 445 port |
| 1231 | * and if it fails - with 139 ports. It should be called only if address |
| 1232 | * families of server and addr are equal. |
| 1233 | */ |
| 1234 | static bool |
| 1235 | match_port(struct TCP_Server_Info *server, struct sockaddr *addr) |
| 1236 | { |
Steve French | 6da9791 | 2011-03-13 18:55:55 +0000 | [diff] [blame] | 1237 | __be16 port, *sport; |
Pavel Shilovsky | 4b88613 | 2010-12-13 22:18:07 +0300 | [diff] [blame] | 1238 | |
Long Li | 3b24911 | 2019-05-15 14:09:04 -0700 | [diff] [blame] | 1239 | /* SMBDirect manages its own ports, don't match it here */ |
| 1240 | if (server->rdma) |
| 1241 | return true; |
| 1242 | |
Pavel Shilovsky | 4b88613 | 2010-12-13 22:18:07 +0300 | [diff] [blame] | 1243 | switch (addr->sa_family) { |
| 1244 | case AF_INET: |
| 1245 | sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port; |
| 1246 | port = ((struct sockaddr_in *) addr)->sin_port; |
| 1247 | break; |
| 1248 | case AF_INET6: |
| 1249 | sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port; |
| 1250 | port = ((struct sockaddr_in6 *) addr)->sin6_port; |
| 1251 | break; |
| 1252 | default: |
| 1253 | WARN_ON(1); |
| 1254 | return false; |
| 1255 | } |
| 1256 | |
| 1257 | if (!port) { |
| 1258 | port = htons(CIFS_PORT); |
| 1259 | if (port == *sport) |
| 1260 | return true; |
| 1261 | |
| 1262 | port = htons(RFC1001_PORT); |
| 1263 | } |
| 1264 | |
| 1265 | return port == *sport; |
| 1266 | } |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1267 | |
| 1268 | static bool |
| 1269 | match_address(struct TCP_Server_Info *server, struct sockaddr *addr, |
| 1270 | struct sockaddr *srcaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | { |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1272 | switch (addr->sa_family) { |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1273 | case AF_INET: { |
| 1274 | struct sockaddr_in *addr4 = (struct sockaddr_in *)addr; |
| 1275 | struct sockaddr_in *srv_addr4 = |
| 1276 | (struct sockaddr_in *)&server->dstaddr; |
| 1277 | |
| 1278 | if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1279 | return false; |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1280 | break; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1281 | } |
| 1282 | case AF_INET6: { |
| 1283 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr; |
| 1284 | struct sockaddr_in6 *srv_addr6 = |
| 1285 | (struct sockaddr_in6 *)&server->dstaddr; |
| 1286 | |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1287 | if (!ipv6_addr_equal(&addr6->sin6_addr, |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1288 | &srv_addr6->sin6_addr)) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1289 | return false; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1290 | if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1291 | return false; |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1292 | break; |
| 1293 | } |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1294 | default: |
| 1295 | WARN_ON(1); |
| 1296 | return false; /* don't expect to be here */ |
| 1297 | } |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1298 | |
Paulo Alcantara | e4af35f | 2020-05-19 15:38:28 -0300 | [diff] [blame] | 1299 | if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr)) |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 1300 | return false; |
| 1301 | |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1302 | return true; |
| 1303 | } |
| 1304 | |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1305 | static bool |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1306 | match_security(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1307 | { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1308 | /* |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1309 | * The select_sectype function should either return the ctx->sectype |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1310 | * that was specified, or "Unspecified" if that sectype was not |
| 1311 | * compatible with the given NEGOTIATE request. |
| 1312 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1313 | if (server->ops->select_sectype(server, ctx->sectype) |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 1314 | == Unspecified) |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1315 | return false; |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1316 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1317 | /* |
| 1318 | * Now check if signing mode is acceptable. No need to check |
| 1319 | * global_secflags at this point since if MUST_SIGN is set then |
| 1320 | * the server->sign had better be too. |
| 1321 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1322 | if (ctx->sign && !server->sign) |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 1323 | return false; |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1324 | |
| 1325 | return true; |
| 1326 | } |
| 1327 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1328 | static int match_server(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1329 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1330 | struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr; |
Jeff Layton | 9fa114f | 2012-11-26 11:09:57 -0500 | [diff] [blame] | 1331 | |
Shyam Prasad N | b9ad6b5 | 2021-11-21 16:45:44 +0000 | [diff] [blame] | 1332 | if (ctx->nosharesock) |
Shyam Prasad N | c9f1c19 | 2021-11-06 11:31:53 +0000 | [diff] [blame] | 1333 | return 0; |
Shyam Prasad N | c9f1c19 | 2021-11-06 11:31:53 +0000 | [diff] [blame] | 1334 | |
| 1335 | /* this server does not share socket */ |
| 1336 | if (server->nosharesock) |
Jeff Layton | a0b3df5 | 2013-05-24 07:40:59 -0400 | [diff] [blame] | 1337 | return 0; |
| 1338 | |
Steve French | 43cdae8 | 2019-06-13 14:26:49 -0500 | [diff] [blame] | 1339 | /* If multidialect negotiation see if existing sessions match one */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1340 | if (strcmp(ctx->vals->version_string, SMB3ANY_VERSION_STRING) == 0) { |
Steve French | 43cdae8 | 2019-06-13 14:26:49 -0500 | [diff] [blame] | 1341 | if (server->vals->protocol_id < SMB30_PROT_ID) |
| 1342 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1343 | } else if (strcmp(ctx->vals->version_string, |
Steve French | 43cdae8 | 2019-06-13 14:26:49 -0500 | [diff] [blame] | 1344 | SMBDEFAULT_VERSION_STRING) == 0) { |
| 1345 | if (server->vals->protocol_id < SMB21_PROT_ID) |
| 1346 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1347 | } else if ((server->vals != ctx->vals) || (server->ops != ctx->ops)) |
Jeff Layton | 23db65f | 2012-05-15 12:20:51 -0400 | [diff] [blame] | 1348 | return 0; |
| 1349 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1350 | if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns)) |
| 1351 | return 0; |
| 1352 | |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1353 | if (strcasecmp(server->hostname, ctx->server_hostname)) |
| 1354 | return 0; |
| 1355 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1356 | if (!match_address(server, addr, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1357 | (struct sockaddr *)&ctx->srcaddr)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1358 | return 0; |
| 1359 | |
| 1360 | if (!match_port(server, addr)) |
| 1361 | return 0; |
| 1362 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1363 | if (!match_security(server, ctx)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1364 | return 0; |
| 1365 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1366 | if (server->echo_interval != ctx->echo_interval * HZ) |
Steve French | adfeb3e | 2015-12-18 12:31:36 -0600 | [diff] [blame] | 1367 | return 0; |
| 1368 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1369 | if (server->rdma != ctx->rdma) |
Long Li | 8339dd3 | 2017-11-07 01:54:55 -0700 | [diff] [blame] | 1370 | return 0; |
| 1371 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1372 | if (server->ignore_signature != ctx->ignore_signature) |
Steve French | 4f5c10f | 2019-09-03 21:18:49 -0500 | [diff] [blame] | 1373 | return 0; |
| 1374 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1375 | if (server->min_offload != ctx->min_offload) |
Steve French | 563317e | 2019-09-08 23:22:02 -0500 | [diff] [blame] | 1376 | return 0; |
| 1377 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1378 | return 1; |
| 1379 | } |
| 1380 | |
Paulo Alcantara | 54be1f6 | 2018-11-14 16:01:21 -0200 | [diff] [blame] | 1381 | struct TCP_Server_Info * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1382 | cifs_find_tcp_session(struct smb3_fs_context *ctx) |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1383 | { |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1384 | struct TCP_Server_Info *server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1386 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 4515148 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1387 | list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) { |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 1388 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 1389 | /* |
| 1390 | * DFS failover implementation in cifs_reconnect() requires unique tcp sessions for |
| 1391 | * DFS connections to do failover properly, so avoid sharing them with regular |
| 1392 | * shares or even links that may connect to same server but having completely |
| 1393 | * different failover targets. |
| 1394 | */ |
| 1395 | if (server->is_dfs_conn) |
| 1396 | continue; |
| 1397 | #endif |
Paulo Alcantara (SUSE) | 3345bb4 | 2019-12-04 11:25:06 -0300 | [diff] [blame] | 1398 | /* |
| 1399 | * Skip ses channels since they're only handled in lower layers |
| 1400 | * (e.g. cifs_send_recv). |
| 1401 | */ |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1402 | if (CIFS_SERVER_IS_CHAN(server) || !match_server(server, ctx)) |
Jeff Layton | daf5b0b | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1403 | continue; |
| 1404 | |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1405 | ++server->srv_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1406 | spin_unlock(&cifs_tcp_ses_lock); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1407 | cifs_dbg(FYI, "Existing tcp session with server found\n"); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1408 | return server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1410 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | return NULL; |
| 1412 | } |
| 1413 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1414 | void |
| 1415 | cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | { |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1417 | struct task_struct *task; |
| 1418 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1419 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1420 | if (--server->srv_count > 0) { |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1421 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1422 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
Steve French | dea570e0 | 2008-05-06 22:05:51 +0000 | [diff] [blame] | 1424 | |
Shyam Prasad N | 16dd9b8 | 2021-07-14 23:00:00 -0500 | [diff] [blame] | 1425 | /* srv_count can never go negative */ |
| 1426 | WARN_ON(server->srv_count < 0); |
| 1427 | |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 1428 | put_net(cifs_net_ns(server)); |
| 1429 | |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1430 | list_del_init(&server->tcp_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1431 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1432 | |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1433 | /* For secondary channels, we pick up ref-count on the primary server */ |
| 1434 | if (CIFS_SERVER_IS_CHAN(server)) |
| 1435 | cifs_put_tcp_session(server->primary_server, from_reconnect); |
| 1436 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1437 | cancel_delayed_work_sync(&server->echo); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 1438 | cancel_delayed_work_sync(&server->resolve); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1439 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1440 | if (from_reconnect) |
| 1441 | /* |
| 1442 | * Avoid deadlock here: reconnect work calls |
| 1443 | * cifs_put_tcp_session() at its end. Need to be sure |
| 1444 | * that reconnect work does nothing with server pointer after |
| 1445 | * that step. |
| 1446 | */ |
| 1447 | cancel_delayed_work(&server->reconnect); |
| 1448 | else |
| 1449 | cancel_delayed_work_sync(&server->reconnect); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1450 | |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 1451 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1452 | server->tcpStatus = CifsExiting; |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 1453 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | e7ddee9 | 2008-11-14 13:44:38 -0500 | [diff] [blame] | 1454 | |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 1455 | cifs_crypto_secmech_release(server); |
Shyam Prasad N | 46bb1b9 | 2021-07-19 13:59:14 +0000 | [diff] [blame] | 1456 | |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 1457 | kfree(server->session_key.response); |
| 1458 | server->session_key.response = NULL; |
| 1459 | server->session_key.len = 0; |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1460 | kfree(server->hostname); |
Steve French | a5c3e1c | 2014-09-16 04:16:19 -0500 | [diff] [blame] | 1461 | |
| 1462 | task = xchg(&server->tsk, NULL); |
| 1463 | if (task) |
Eric W. Biederman | 72abe3b | 2019-05-15 12:33:50 -0500 | [diff] [blame] | 1464 | send_sig(SIGKILL, task, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1467 | struct TCP_Server_Info * |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1468 | cifs_get_tcp_session(struct smb3_fs_context *ctx, |
| 1469 | struct TCP_Server_Info *primary_server) |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1470 | { |
| 1471 | struct TCP_Server_Info *tcp_ses = NULL; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1472 | int rc; |
| 1473 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1474 | cifs_dbg(FYI, "UNC: %s\n", ctx->UNC); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1475 | |
| 1476 | /* see if we already have a matching tcp_ses */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1477 | tcp_ses = cifs_find_tcp_session(ctx); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1478 | if (tcp_ses) |
| 1479 | return tcp_ses; |
| 1480 | |
| 1481 | tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL); |
| 1482 | if (!tcp_ses) { |
| 1483 | rc = -ENOMEM; |
| 1484 | goto out_err; |
| 1485 | } |
| 1486 | |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1487 | tcp_ses->hostname = kstrdup(ctx->server_hostname, GFP_KERNEL); |
| 1488 | if (!tcp_ses->hostname) { |
| 1489 | rc = -ENOMEM; |
| 1490 | goto out_err; |
| 1491 | } |
| 1492 | |
Shyam Prasad N | b9ad6b5 | 2021-11-21 16:45:44 +0000 | [diff] [blame] | 1493 | if (ctx->nosharesock) |
| 1494 | tcp_ses->nosharesock = true; |
| 1495 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1496 | tcp_ses->ops = ctx->ops; |
| 1497 | tcp_ses->vals = ctx->vals; |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 1498 | cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns)); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1499 | |
Shyam Prasad N | 6d82c27 | 2021-02-03 23:20:46 -0800 | [diff] [blame] | 1500 | tcp_ses->conn_id = atomic_inc_return(&tcpSesNextId); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1501 | tcp_ses->noblockcnt = ctx->rootfs; |
| 1502 | tcp_ses->noblocksnd = ctx->noblocksnd || ctx->rootfs; |
| 1503 | tcp_ses->noautotune = ctx->noautotune; |
| 1504 | tcp_ses->tcp_nodelay = ctx->sockopt_tcp_nodelay; |
| 1505 | tcp_ses->rdma = ctx->rdma; |
Pavel Shilovsky | fc40f9c | 2012-02-17 17:09:12 +0300 | [diff] [blame] | 1506 | tcp_ses->in_flight = 0; |
Steve French | 1b63f18 | 2019-09-09 22:57:11 -0500 | [diff] [blame] | 1507 | tcp_ses->max_in_flight = 0; |
Pavel Shilovsky | 2d86dbc | 2012-02-06 15:59:18 +0400 | [diff] [blame] | 1508 | tcp_ses->credits = 1; |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1509 | if (primary_server) { |
Steve French | 446e214 | 2021-11-15 18:02:27 -0600 | [diff] [blame] | 1510 | spin_lock(&cifs_tcp_ses_lock); |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1511 | ++primary_server->srv_count; |
| 1512 | tcp_ses->primary_server = primary_server; |
Steve French | 446e214 | 2021-11-15 18:02:27 -0600 | [diff] [blame] | 1513 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1514 | } |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1515 | init_waitqueue_head(&tcp_ses->response_q); |
| 1516 | init_waitqueue_head(&tcp_ses->request_q); |
| 1517 | INIT_LIST_HEAD(&tcp_ses->pending_mid_q); |
| 1518 | mutex_init(&tcp_ses->srv_mutex); |
| 1519 | memcpy(tcp_ses->workstation_RFC1001_name, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1520 | ctx->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1521 | memcpy(tcp_ses->server_RFC1001_name, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1522 | ctx->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); |
Shirish Pargaonkar | 5d0d288 | 2010-10-13 18:15:00 -0500 | [diff] [blame] | 1523 | tcp_ses->session_estab = false; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1524 | tcp_ses->sequence_number = 0; |
Pavel Shilovsky | 5b96485 | 2019-01-18 11:30:26 -0800 | [diff] [blame] | 1525 | tcp_ses->reconnect_instance = 1; |
Steve French | fda3594 | 2011-01-20 18:06:34 +0000 | [diff] [blame] | 1526 | tcp_ses->lstrp = jiffies; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1527 | tcp_ses->compress_algorithm = cpu_to_le16(ctx->compression); |
Jeff Layton | 58fa015 | 2012-05-01 17:41:16 -0400 | [diff] [blame] | 1528 | spin_lock_init(&tcp_ses->req_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1529 | INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); |
| 1530 | INIT_LIST_HEAD(&tcp_ses->smb_ses_list); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1531 | INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request); |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 1532 | INIT_DELAYED_WORK(&tcp_ses->resolve, cifs_resolve_server); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1533 | INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server); |
| 1534 | mutex_init(&tcp_ses->reconnect_mutex); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 1535 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 1536 | mutex_init(&tcp_ses->refpath_lock); |
| 1537 | #endif |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1538 | memcpy(&tcp_ses->srcaddr, &ctx->srcaddr, |
Jeff Layton | 9fa114f | 2012-11-26 11:09:57 -0500 | [diff] [blame] | 1539 | sizeof(tcp_ses->srcaddr)); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1540 | memcpy(&tcp_ses->dstaddr, &ctx->dstaddr, |
Jeff Layton | 9fa114f | 2012-11-26 11:09:57 -0500 | [diff] [blame] | 1541 | sizeof(tcp_ses->dstaddr)); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1542 | if (ctx->use_client_guid) |
| 1543 | memcpy(tcp_ses->client_guid, ctx->client_guid, |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1544 | SMB2_CLIENT_GUID_SIZE); |
| 1545 | else |
| 1546 | generate_random_uuid(tcp_ses->client_guid); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1547 | /* |
| 1548 | * at this point we are the only ones with the pointer |
| 1549 | * to the struct since the kernel thread not created yet |
| 1550 | * no need to spinlock this init of tcpStatus or srv_count |
| 1551 | */ |
| 1552 | tcp_ses->tcpStatus = CifsNew; |
| 1553 | ++tcp_ses->srv_count; |
| 1554 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1555 | if (ctx->echo_interval >= SMB_ECHO_INTERVAL_MIN && |
| 1556 | ctx->echo_interval <= SMB_ECHO_INTERVAL_MAX) |
| 1557 | tcp_ses->echo_interval = ctx->echo_interval * HZ; |
Steve French | adfeb3e | 2015-12-18 12:31:36 -0600 | [diff] [blame] | 1558 | else |
| 1559 | tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ; |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 1560 | if (tcp_ses->rdma) { |
| 1561 | #ifndef CONFIG_CIFS_SMB_DIRECT |
| 1562 | cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n"); |
| 1563 | rc = -ENOENT; |
| 1564 | goto out_err_crypto_release; |
| 1565 | #endif |
| 1566 | tcp_ses->smbd_conn = smbd_get_connection( |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1567 | tcp_ses, (struct sockaddr *)&ctx->dstaddr); |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 1568 | if (tcp_ses->smbd_conn) { |
| 1569 | cifs_dbg(VFS, "RDMA transport established\n"); |
| 1570 | rc = 0; |
| 1571 | goto smbd_connected; |
| 1572 | } else { |
| 1573 | rc = -ENOENT; |
| 1574 | goto out_err_crypto_release; |
| 1575 | } |
| 1576 | } |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 1577 | rc = ip_connect(tcp_ses); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1578 | if (rc < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1579 | cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n"); |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 1580 | goto out_err_crypto_release; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1581 | } |
Long Li | 2f89464 | 2017-11-22 17:38:34 -0700 | [diff] [blame] | 1582 | smbd_connected: |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1583 | /* |
| 1584 | * since we're in a cifs function already, we know that |
| 1585 | * this will succeed. No need for try_module_get(). |
| 1586 | */ |
| 1587 | __module_get(THIS_MODULE); |
Al Viro | 7c97c20 | 2011-06-21 08:51:28 -0400 | [diff] [blame] | 1588 | tcp_ses->tsk = kthread_run(cifs_demultiplex_thread, |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1589 | tcp_ses, "cifsd"); |
| 1590 | if (IS_ERR(tcp_ses->tsk)) { |
| 1591 | rc = PTR_ERR(tcp_ses->tsk); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1592 | cifs_dbg(VFS, "error %d create cifsd thread\n", rc); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1593 | module_put(THIS_MODULE); |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 1594 | goto out_err_crypto_release; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1595 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1596 | tcp_ses->min_offload = ctx->min_offload; |
Steve French | 01cf308 | 2021-07-01 12:22:47 -0500 | [diff] [blame] | 1597 | /* |
| 1598 | * at this point we are the only ones with the pointer |
| 1599 | * to the struct since the kernel thread not created yet |
| 1600 | * no need to spinlock this update of tcpStatus |
| 1601 | */ |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 1602 | spin_lock(&cifs_tcp_ses_lock); |
Steve French | fd88ce9 | 2011-04-12 01:01:14 +0000 | [diff] [blame] | 1603 | tcp_ses->tcpStatus = CifsNeedNegotiate; |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 1604 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1605 | |
Aurelien Aptel | a249cc8 | 2021-03-04 17:42:21 +0000 | [diff] [blame] | 1606 | if ((ctx->max_credits < 20) || (ctx->max_credits > 60000)) |
| 1607 | tcp_ses->max_credits = SMB2_MAX_CREDITS_AVAILABLE; |
| 1608 | else |
| 1609 | tcp_ses->max_credits = ctx->max_credits; |
| 1610 | |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 1611 | tcp_ses->nr_targets = 1; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1612 | tcp_ses->ignore_signature = ctx->ignore_signature; |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1613 | /* thread spawned, put it on the list */ |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1614 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1615 | list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1616 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1617 | |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1618 | /* queue echo request delayed work */ |
Steve French | adfeb3e | 2015-12-18 12:31:36 -0600 | [diff] [blame] | 1619 | queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval); |
Jeff Layton | c74093b | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 1620 | |
Shyam Prasad N | 506c1da | 2021-05-18 15:05:50 +0000 | [diff] [blame] | 1621 | /* queue dns resolution delayed work */ |
| 1622 | cifs_dbg(FYI, "%s: next dns resolution scheduled for %d seconds in the future\n", |
| 1623 | __func__, SMB_DNS_RESOLVE_INTERVAL_DEFAULT); |
| 1624 | |
| 1625 | queue_delayed_work(cifsiod_wq, &tcp_ses->resolve, (SMB_DNS_RESOLVE_INTERVAL_DEFAULT * HZ)); |
| 1626 | |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1627 | return tcp_ses; |
| 1628 | |
Shirish Pargaonkar | f7c5445a | 2010-10-26 18:10:24 -0500 | [diff] [blame] | 1629 | out_err_crypto_release: |
Pavel Shilovsky | 026e93d | 2016-11-03 16:47:37 -0700 | [diff] [blame] | 1630 | cifs_crypto_secmech_release(tcp_ses); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 1631 | |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 1632 | put_net(cifs_net_ns(tcp_ses)); |
| 1633 | |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1634 | out_err: |
| 1635 | if (tcp_ses) { |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 1636 | if (CIFS_SERVER_IS_CHAN(tcp_ses)) |
| 1637 | cifs_put_tcp_session(tcp_ses->primary_server, false); |
Shyam Prasad N | 7be3248 | 2021-10-14 11:52:39 +0000 | [diff] [blame] | 1638 | kfree(tcp_ses->hostname); |
Jeff Layton | 63c038c | 2008-12-01 18:41:46 -0500 | [diff] [blame] | 1639 | if (tcp_ses->ssocket) |
| 1640 | sock_release(tcp_ses->ssocket); |
| 1641 | kfree(tcp_ses); |
| 1642 | } |
| 1643 | return ERR_PTR(rc); |
| 1644 | } |
| 1645 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1646 | static int match_session(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1647 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1648 | if (ctx->sectype != Unspecified && |
| 1649 | ctx->sectype != ses->sectype) |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1650 | return 0; |
| 1651 | |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1652 | /* |
| 1653 | * If an existing session is limited to less channels than |
| 1654 | * requested, it should not be reused |
| 1655 | */ |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1656 | spin_lock(&ses->chan_lock); |
| 1657 | if (ses->chan_max < ctx->max_channels) { |
| 1658 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1659 | return 0; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1660 | } |
| 1661 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | bcc8880 | 2019-09-20 04:32:20 +0200 | [diff] [blame] | 1662 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1663 | switch (ses->sectype) { |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1664 | case Kerberos: |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1665 | if (!uid_eq(ctx->cred_uid, ses->cred_uid)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1666 | return 0; |
| 1667 | break; |
| 1668 | default: |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1669 | /* NULL username means anonymous session */ |
| 1670 | if (ses->user_name == NULL) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1671 | if (!ctx->nullauth) |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1672 | return 0; |
| 1673 | break; |
| 1674 | } |
| 1675 | |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1676 | /* anything else takes username/password */ |
Jeff Layton | 04febab | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1677 | if (strncmp(ses->user_name, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1678 | ctx->username ? ctx->username : "", |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1679 | CIFS_MAX_USERNAME_LEN)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1680 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1681 | if ((ctx->username && strlen(ctx->username) != 0) && |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1682 | ses->password != NULL && |
| 1683 | strncmp(ses->password, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1684 | ctx->password ? ctx->password : "", |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1685 | CIFS_MAX_PASSWORD_LEN)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1686 | return 0; |
| 1687 | } |
| 1688 | return 1; |
| 1689 | } |
| 1690 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1691 | /** |
| 1692 | * cifs_setup_ipc - helper to setup the IPC tcon for the session |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1693 | * @ses: smb session to issue the request on |
| 1694 | * @ctx: the superblock configuration context to use for building the |
| 1695 | * new tree connection for the IPC (interprocess communication RPC) |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1696 | * |
| 1697 | * A new IPC connection is made and stored in the session |
| 1698 | * tcon_ipc. The IPC tcon has the same lifetime as the session. |
| 1699 | */ |
| 1700 | static int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1701 | cifs_setup_ipc(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1702 | { |
| 1703 | int rc = 0, xid; |
| 1704 | struct cifs_tcon *tcon; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1705 | char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0}; |
| 1706 | bool seal = false; |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1707 | struct TCP_Server_Info *server = ses->server; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * If the mount request that resulted in the creation of the |
| 1711 | * session requires encryption, force IPC to be encrypted too. |
| 1712 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1713 | if (ctx->seal) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1714 | if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION) |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1715 | seal = true; |
| 1716 | else { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1717 | cifs_server_dbg(VFS, |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1718 | "IPC: server doesn't support encryption\n"); |
| 1719 | return -EOPNOTSUPP; |
| 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | tcon = tconInfoAlloc(); |
| 1724 | if (tcon == NULL) |
| 1725 | return -ENOMEM; |
| 1726 | |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1727 | scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1728 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1729 | xid = get_xid(); |
| 1730 | tcon->ses = ses; |
| 1731 | tcon->ipc = true; |
| 1732 | tcon->seal = seal; |
Ronnie Sahlberg | 6fd4ea8 | 2020-12-14 16:40:21 +1000 | [diff] [blame] | 1733 | rc = server->ops->tree_connect(xid, ses, unc, tcon, ctx->local_nls); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1734 | free_xid(xid); |
| 1735 | |
| 1736 | if (rc) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1737 | cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1738 | tconInfoFree(tcon); |
| 1739 | goto out; |
| 1740 | } |
| 1741 | |
| 1742 | cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid); |
| 1743 | |
| 1744 | ses->tcon_ipc = tcon; |
| 1745 | out: |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1746 | return rc; |
| 1747 | } |
| 1748 | |
| 1749 | /** |
| 1750 | * cifs_free_ipc - helper to release the session IPC tcon |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 1751 | * @ses: smb session to unmount the IPC from |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1752 | * |
Paulo Alcantara | f3191fc | 2021-06-04 19:25:27 -0300 | [diff] [blame] | 1753 | * Needs to be called everytime a session is destroyed. |
| 1754 | * |
| 1755 | * On session close, the IPC is closed and the server must release all tcons of the session. |
| 1756 | * No need to send a tree disconnect here. |
| 1757 | * |
| 1758 | * Besides, it will make the server to not close durable and resilient files on session close, as |
| 1759 | * specified in MS-SMB2 3.3.5.6 Receiving an SMB2 LOGOFF Request. |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1760 | */ |
| 1761 | static int |
| 1762 | cifs_free_ipc(struct cifs_ses *ses) |
| 1763 | { |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1764 | struct cifs_tcon *tcon = ses->tcon_ipc; |
| 1765 | |
| 1766 | if (tcon == NULL) |
| 1767 | return 0; |
| 1768 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1769 | tconInfoFree(tcon); |
| 1770 | ses->tcon_ipc = NULL; |
Paulo Alcantara | f3191fc | 2021-06-04 19:25:27 -0300 | [diff] [blame] | 1771 | return 0; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1772 | } |
| 1773 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1774 | static struct cifs_ses * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1775 | cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 1777 | struct cifs_ses *ses; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1779 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 4ff67b7 | 2010-07-06 20:43:02 -0400 | [diff] [blame] | 1780 | list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1781 | if (ses->status == CifsExiting) |
| 1782 | continue; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1783 | if (!match_session(ses, ctx)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 1784 | continue; |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1785 | ++ses->ses_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1786 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1787 | return ses; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1789 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | return NULL; |
| 1791 | } |
| 1792 | |
Paulo Alcantara (SUSE) | 5072010 | 2019-03-19 16:54:29 -0300 | [diff] [blame] | 1793 | void cifs_put_smb_ses(struct cifs_ses *ses) |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1794 | { |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1795 | unsigned int rc, xid; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1796 | unsigned int chan_count; |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1797 | struct TCP_Server_Info *server = ses->server; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1798 | cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count); |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1799 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1800 | spin_lock(&cifs_tcp_ses_lock); |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1801 | if (ses->status == CifsExiting) { |
| 1802 | spin_unlock(&cifs_tcp_ses_lock); |
| 1803 | return; |
| 1804 | } |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 1805 | |
| 1806 | cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count); |
| 1807 | cifs_dbg(FYI, "%s: ses ipc: %s\n", __func__, ses->tcon_ipc ? ses->tcon_ipc->treeName : "NONE"); |
| 1808 | |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1809 | if (--ses->ses_count > 0) { |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1810 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1811 | return; |
| 1812 | } |
Steve French | 0060a4f | 2021-06-24 15:28:04 -0500 | [diff] [blame] | 1813 | |
Shyam Prasad N | 16dd9b8 | 2021-07-14 23:00:00 -0500 | [diff] [blame] | 1814 | /* ses_count can never go negative */ |
| 1815 | WARN_ON(ses->ses_count < 0); |
| 1816 | |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1817 | if (ses->status == CifsGood) |
| 1818 | ses->status = CifsExiting; |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 1819 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1820 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 1821 | cifs_free_ipc(ses); |
| 1822 | |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1823 | if (ses->status == CifsExiting && server->ops->logoff) { |
| 1824 | xid = get_xid(); |
| 1825 | rc = server->ops->logoff(xid, ses); |
| 1826 | if (rc) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 1827 | cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n", |
Shirish Pargaonkar | 7f48558 | 2013-10-12 10:06:03 -0500 | [diff] [blame] | 1828 | __func__, rc); |
| 1829 | _free_xid(xid); |
| 1830 | } |
| 1831 | |
| 1832 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1833 | list_del_init(&ses->smb_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 1834 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1835 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1836 | spin_lock(&ses->chan_lock); |
| 1837 | chan_count = ses->chan_count; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1838 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1839 | /* close any extra channels */ |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1840 | if (chan_count > 1) { |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1841 | int i; |
| 1842 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1843 | for (i = 1; i < chan_count; i++) { |
Shyam Prasad N | 489f710 | 2022-01-29 09:32:33 +0000 | [diff] [blame] | 1844 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1845 | cifs_put_tcp_session(ses->chans[i].server, 0); |
Shyam Prasad N | 489f710 | 2022-01-29 09:32:33 +0000 | [diff] [blame] | 1846 | spin_lock(&ses->chan_lock); |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 1847 | ses->chans[i].server = NULL; |
| 1848 | } |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1849 | } |
Shyam Prasad N | 88b024f | 2021-11-19 14:16:57 +0000 | [diff] [blame] | 1850 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 1851 | |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1852 | sesInfoFree(ses); |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 1853 | cifs_put_tcp_session(server, 0); |
Jeff Layton | 14fbf50 | 2008-11-14 13:53:46 -0500 | [diff] [blame] | 1854 | } |
| 1855 | |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1856 | #ifdef CONFIG_KEYS |
| 1857 | |
Chen Gang | 057d633 | 2013-07-19 09:01:36 +0800 | [diff] [blame] | 1858 | /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */ |
| 1859 | #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1860 | |
| 1861 | /* Populate username and pw fields from keyring if possible */ |
| 1862 | static int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1863 | cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1864 | { |
| 1865 | int rc = 0; |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1866 | int is_domain = 0; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1867 | const char *delim, *payload; |
| 1868 | char *desc; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1869 | ssize_t len; |
| 1870 | struct key *key; |
| 1871 | struct TCP_Server_Info *server = ses->server; |
| 1872 | struct sockaddr_in *sa; |
| 1873 | struct sockaddr_in6 *sa6; |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1874 | const struct user_key_payload *upayload; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1875 | |
| 1876 | desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL); |
| 1877 | if (!desc) |
| 1878 | return -ENOMEM; |
| 1879 | |
| 1880 | /* try to find an address key first */ |
| 1881 | switch (server->dstaddr.ss_family) { |
| 1882 | case AF_INET: |
| 1883 | sa = (struct sockaddr_in *)&server->dstaddr; |
| 1884 | sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr); |
| 1885 | break; |
| 1886 | case AF_INET6: |
| 1887 | sa6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 1888 | sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr); |
| 1889 | break; |
| 1890 | default: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1891 | cifs_dbg(FYI, "Bad ss_family (%hu)\n", |
| 1892 | server->dstaddr.ss_family); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1893 | rc = -EINVAL; |
| 1894 | goto out_err; |
| 1895 | } |
| 1896 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1897 | cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 1898 | key = request_key(&key_type_logon, desc, ""); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1899 | if (IS_ERR(key)) { |
| 1900 | if (!ses->domainName) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1901 | cifs_dbg(FYI, "domainName is NULL\n"); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1902 | rc = PTR_ERR(key); |
| 1903 | goto out_err; |
| 1904 | } |
| 1905 | |
| 1906 | /* didn't work, try to find a domain key */ |
| 1907 | sprintf(desc, "cifs:d:%s", ses->domainName); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1908 | cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc); |
Linus Torvalds | 028db3e | 2019-07-10 18:43:43 -0700 | [diff] [blame] | 1909 | key = request_key(&key_type_logon, desc, ""); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1910 | if (IS_ERR(key)) { |
| 1911 | rc = PTR_ERR(key); |
| 1912 | goto out_err; |
| 1913 | } |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1914 | is_domain = 1; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | down_read(&key->sem); |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 1918 | upayload = user_key_payload_locked(key); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1919 | if (IS_ERR_OR_NULL(upayload)) { |
Jeff Layton | 4edc53c | 2012-02-07 06:30:51 -0500 | [diff] [blame] | 1920 | rc = upayload ? PTR_ERR(upayload) : -EINVAL; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1921 | goto out_key_put; |
| 1922 | } |
| 1923 | |
| 1924 | /* find first : in payload */ |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1925 | payload = upayload->data; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1926 | delim = strnchr(payload, upayload->datalen, ':'); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1927 | cifs_dbg(FYI, "payload=%s\n", payload); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1928 | if (!delim) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1929 | cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n", |
| 1930 | upayload->datalen); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1931 | rc = -EINVAL; |
| 1932 | goto out_key_put; |
| 1933 | } |
| 1934 | |
| 1935 | len = delim - payload; |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1936 | if (len > CIFS_MAX_USERNAME_LEN || len <= 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1937 | cifs_dbg(FYI, "Bad value from username search (len=%zd)\n", |
| 1938 | len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1939 | rc = -EINVAL; |
| 1940 | goto out_key_put; |
| 1941 | } |
| 1942 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1943 | ctx->username = kstrndup(payload, len, GFP_KERNEL); |
| 1944 | if (!ctx->username) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1945 | cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n", |
| 1946 | len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1947 | rc = -ENOMEM; |
| 1948 | goto out_key_put; |
| 1949 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1950 | cifs_dbg(FYI, "%s: username=%s\n", __func__, ctx->username); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1951 | |
| 1952 | len = key->datalen - (len + 1); |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 1953 | if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1954 | cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1955 | rc = -EINVAL; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1956 | kfree(ctx->username); |
| 1957 | ctx->username = NULL; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1958 | goto out_key_put; |
| 1959 | } |
| 1960 | |
| 1961 | ++delim; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1962 | ctx->password = kstrndup(delim, len, GFP_KERNEL); |
| 1963 | if (!ctx->password) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1964 | cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n", |
| 1965 | len); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1966 | rc = -ENOMEM; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1967 | kfree(ctx->username); |
| 1968 | ctx->username = NULL; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 1969 | goto out_key_put; |
| 1970 | } |
| 1971 | |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1972 | /* |
| 1973 | * If we have a domain key then we must set the domainName in the |
| 1974 | * for the request. |
| 1975 | */ |
| 1976 | if (is_domain && ses->domainName) { |
Al Viro | 8d76722 | 2021-03-05 15:02:34 -0500 | [diff] [blame] | 1977 | ctx->domainname = kstrdup(ses->domainName, GFP_KERNEL); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1978 | if (!ctx->domainname) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1979 | cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n", |
| 1980 | len); |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1981 | rc = -ENOMEM; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 1982 | kfree(ctx->username); |
| 1983 | ctx->username = NULL; |
| 1984 | kfree_sensitive(ctx->password); |
| 1985 | ctx->password = NULL; |
Ronnie Sahlberg | f2aee32 | 2019-08-22 08:09:50 +1000 | [diff] [blame] | 1986 | goto out_key_put; |
| 1987 | } |
| 1988 | } |
| 1989 | |
Ryan Bair | d3b331f | 2021-12-22 11:04:05 -0500 | [diff] [blame] | 1990 | ctx->workstation_name = kstrdup(ses->workstation_name, GFP_KERNEL); |
| 1991 | if (!ctx->workstation_name) { |
| 1992 | cifs_dbg(FYI, "Unable to allocate memory for workstation_name\n"); |
| 1993 | rc = -ENOMEM; |
| 1994 | kfree(ctx->username); |
| 1995 | ctx->username = NULL; |
| 1996 | kfree_sensitive(ctx->password); |
| 1997 | ctx->password = NULL; |
| 1998 | kfree(ctx->domainname); |
| 1999 | ctx->domainname = NULL; |
| 2000 | goto out_key_put; |
| 2001 | } |
| 2002 | |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 2003 | out_key_put: |
| 2004 | up_read(&key->sem); |
| 2005 | key_put(key); |
| 2006 | out_err: |
| 2007 | kfree(desc); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2008 | cifs_dbg(FYI, "%s: returning %d\n", __func__, rc); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 2009 | return rc; |
| 2010 | } |
| 2011 | #else /* ! CONFIG_KEYS */ |
| 2012 | static inline int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2013 | cifs_set_cifscreds(struct smb3_fs_context *ctx __attribute__((unused)), |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 2014 | struct cifs_ses *ses __attribute__((unused))) |
| 2015 | { |
| 2016 | return -ENOSYS; |
| 2017 | } |
| 2018 | #endif /* CONFIG_KEYS */ |
| 2019 | |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2020 | /** |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2021 | * cifs_get_smb_ses - get a session matching @ctx data from @server |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 2022 | * @server: server to setup the session to |
| 2023 | * @ctx: superblock configuration context to use to setup the session |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2024 | * |
| 2025 | * This function assumes it is being called from cifs_mount() where we |
| 2026 | * already got a server reference (server refcount +1). See |
| 2027 | * cifs_get_tcon() for refcount explanations. |
| 2028 | */ |
Paulo Alcantara (SUSE) | 5072010 | 2019-03-19 16:54:29 -0300 | [diff] [blame] | 2029 | struct cifs_ses * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2030 | cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb3_fs_context *ctx) |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2031 | { |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 2032 | int rc = -ENOMEM; |
| 2033 | unsigned int xid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2034 | struct cifs_ses *ses; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2035 | struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; |
| 2036 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2037 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2038 | xid = get_xid(); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2039 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2040 | ses = cifs_find_smb_ses(server, ctx); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2041 | if (ses) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2042 | cifs_dbg(FYI, "Existing smb sess found (status=%d)\n", |
| 2043 | ses->status); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2044 | |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 2045 | spin_lock(&ses->chan_lock); |
| 2046 | if (cifs_chan_needs_reconnect(ses, server)) { |
| 2047 | spin_unlock(&ses->chan_lock); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2048 | cifs_dbg(FYI, "Session needs reconnect\n"); |
Shyam Prasad N | 8e07757 | 2021-07-19 10:03:38 +0000 | [diff] [blame] | 2049 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 2050 | mutex_lock(&ses->session_mutex); |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 2051 | rc = cifs_negotiate_protocol(xid, ses, server); |
Shyam Prasad N | 8e07757 | 2021-07-19 10:03:38 +0000 | [diff] [blame] | 2052 | if (rc) { |
| 2053 | mutex_unlock(&ses->session_mutex); |
| 2054 | /* problem -- put our ses reference */ |
| 2055 | cifs_put_smb_ses(ses); |
| 2056 | free_xid(xid); |
| 2057 | return ERR_PTR(rc); |
| 2058 | } |
| 2059 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 2060 | rc = cifs_setup_session(xid, ses, server, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2061 | ctx->local_nls); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2062 | if (rc) { |
| 2063 | mutex_unlock(&ses->session_mutex); |
| 2064 | /* problem -- put our reference */ |
| 2065 | cifs_put_smb_ses(ses); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2066 | free_xid(xid); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2067 | return ERR_PTR(rc); |
| 2068 | } |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 2069 | mutex_unlock(&ses->session_mutex); |
| 2070 | |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 2071 | spin_lock(&ses->chan_lock); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2072 | } |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 2073 | spin_unlock(&ses->chan_lock); |
Jeff Layton | 460cf34 | 2010-09-14 11:38:24 -0400 | [diff] [blame] | 2074 | |
| 2075 | /* existing SMB ses has a server reference already */ |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 2076 | cifs_put_tcp_session(server, 0); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2077 | free_xid(xid); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2078 | return ses; |
| 2079 | } |
| 2080 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2081 | cifs_dbg(FYI, "Existing smb sess not found\n"); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2082 | ses = sesInfoAlloc(); |
| 2083 | if (ses == NULL) |
| 2084 | goto get_ses_fail; |
| 2085 | |
| 2086 | /* new SMB session uses our server ref */ |
| 2087 | ses->server = server; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2088 | if (server->dstaddr.ss_family == AF_INET6) |
Steve French | b438fcf | 2021-02-20 19:24:11 -0600 | [diff] [blame] | 2089 | sprintf(ses->ip_addr, "%pI6", &addr6->sin6_addr); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2090 | else |
Steve French | b438fcf | 2021-02-20 19:24:11 -0600 | [diff] [blame] | 2091 | sprintf(ses->ip_addr, "%pI4", &addr->sin_addr); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2092 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2093 | if (ctx->username) { |
| 2094 | ses->user_name = kstrdup(ctx->username, GFP_KERNEL); |
Steve French | 8727c8a | 2011-02-25 01:11:56 -0600 | [diff] [blame] | 2095 | if (!ses->user_name) |
| 2096 | goto get_ses_fail; |
| 2097 | } |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2098 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2099 | /* ctx->password freed at unmount */ |
| 2100 | if (ctx->password) { |
| 2101 | ses->password = kstrdup(ctx->password, GFP_KERNEL); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2102 | if (!ses->password) |
| 2103 | goto get_ses_fail; |
| 2104 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2105 | if (ctx->domainname) { |
| 2106 | ses->domainName = kstrdup(ctx->domainname, GFP_KERNEL); |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 2107 | if (!ses->domainName) |
| 2108 | goto get_ses_fail; |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2109 | } |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 2110 | if (ctx->workstation_name) { |
| 2111 | ses->workstation_name = kstrdup(ctx->workstation_name, |
| 2112 | GFP_KERNEL); |
| 2113 | if (!ses->workstation_name) |
| 2114 | goto get_ses_fail; |
| 2115 | } |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2116 | if (ctx->domainauto) |
| 2117 | ses->domainAuto = ctx->domainauto; |
| 2118 | ses->cred_uid = ctx->cred_uid; |
| 2119 | ses->linux_uid = ctx->linux_uid; |
Steve French | d9b9420 | 2011-04-12 01:24:57 +0000 | [diff] [blame] | 2120 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2121 | ses->sectype = ctx->sectype; |
| 2122 | ses->sign = ctx->sign; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 2123 | |
| 2124 | /* add server as first channel */ |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 2125 | spin_lock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 2126 | ses->chans[0].server = server; |
| 2127 | ses->chan_count = 1; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2128 | ses->chan_max = ctx->multichannel ? ctx->max_channels:1; |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 2129 | ses->chans_need_reconnect = 1; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 2130 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 2131 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 2132 | mutex_lock(&ses->session_mutex); |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 2133 | rc = cifs_negotiate_protocol(xid, ses, server); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 2134 | if (!rc) |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 2135 | rc = cifs_setup_session(xid, ses, server, ctx->local_nls); |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 2136 | mutex_unlock(&ses->session_mutex); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 2137 | |
| 2138 | /* each channel uses a different signing key */ |
Shyam Prasad N | 88b024f | 2021-11-19 14:16:57 +0000 | [diff] [blame] | 2139 | spin_lock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 2140 | memcpy(ses->chans[0].signkey, ses->smb3signingkey, |
| 2141 | sizeof(ses->smb3signingkey)); |
Shyam Prasad N | 88b024f | 2021-11-19 14:16:57 +0000 | [diff] [blame] | 2142 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 2143 | |
Steve French | c8e56f1 | 2010-09-08 21:10:58 +0000 | [diff] [blame] | 2144 | if (rc) |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2145 | goto get_ses_fail; |
| 2146 | |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 2147 | /* |
| 2148 | * success, put it on the list and add it as first channel |
| 2149 | * note: the session becomes active soon after this. So you'll |
| 2150 | * need to lock before changing something in the session. |
| 2151 | */ |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2152 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2153 | list_add(&ses->smb_ses_list, &server->smb_ses_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2154 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2155 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2156 | free_xid(xid); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2157 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2158 | cifs_setup_ipc(ses, ctx); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2159 | |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2160 | return ses; |
| 2161 | |
| 2162 | get_ses_fail: |
| 2163 | sesInfoFree(ses); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2164 | free_xid(xid); |
Jeff Layton | 36988c7 | 2010-04-24 07:57:43 -0400 | [diff] [blame] | 2165 | return ERR_PTR(rc); |
| 2166 | } |
| 2167 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2168 | static int match_tcon(struct cifs_tcon *tcon, struct smb3_fs_context *ctx) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 2169 | { |
| 2170 | if (tcon->tidStatus == CifsExiting) |
| 2171 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2172 | if (strncmp(tcon->treeName, ctx->UNC, MAX_TREE_SIZE)) |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 2173 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2174 | if (tcon->seal != ctx->seal) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 2175 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2176 | if (tcon->snapshot_time != ctx->snapshot_time) |
Pavel Shilovsky | ae6f8dd | 2016-11-17 13:59:23 -0800 | [diff] [blame] | 2177 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2178 | if (tcon->handle_timeout != ctx->handle_timeout) |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2179 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2180 | if (tcon->no_lease != ctx->no_lease) |
Steve French | 3e7a02d | 2019-09-11 21:46:20 -0500 | [diff] [blame] | 2181 | return 0; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2182 | if (tcon->nodelete != ctx->nodelete) |
Steve French | 82e9367 | 2020-05-19 03:06:57 -0500 | [diff] [blame] | 2183 | return 0; |
Pavel Shilovsky | 37bb04e | 2011-05-05 09:55:11 +0000 | [diff] [blame] | 2184 | return 1; |
| 2185 | } |
| 2186 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2187 | static struct cifs_tcon * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2188 | cifs_find_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | { |
| 2190 | struct list_head *tmp; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2191 | struct cifs_tcon *tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2193 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2194 | list_for_each(tmp, &ses->tcon_list) { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2195 | tcon = list_entry(tmp, struct cifs_tcon, tcon_list); |
Paulo Alcantara | f3c852b | 2021-06-04 19:25:33 -0300 | [diff] [blame] | 2196 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2197 | if (!match_tcon(tcon, ctx)) |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2198 | continue; |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2199 | ++tcon->tc_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2200 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | return tcon; |
| 2202 | } |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2203 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | return NULL; |
| 2205 | } |
| 2206 | |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 2207 | void |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2208 | cifs_put_tcon(struct cifs_tcon *tcon) |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2209 | { |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2210 | unsigned int xid; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2211 | struct cifs_ses *ses; |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2212 | |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 2213 | /* |
| 2214 | * IPC tcon share the lifetime of their session and are |
| 2215 | * destroyed in the session put function |
| 2216 | */ |
| 2217 | if (tcon == NULL || tcon->ipc) |
| 2218 | return; |
| 2219 | |
| 2220 | ses = tcon->ses; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2221 | cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2222 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2223 | if (--tcon->tc_count > 0) { |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2224 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2225 | return; |
| 2226 | } |
| 2227 | |
Shyam Prasad N | 16dd9b8 | 2021-07-14 23:00:00 -0500 | [diff] [blame] | 2228 | /* tc_count can never go negative */ |
| 2229 | WARN_ON(tcon->tc_count < 0); |
| 2230 | |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 2231 | list_del_init(&tcon->tcon_list); |
| 2232 | spin_unlock(&cifs_tcp_ses_lock); |
| 2233 | |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 2234 | if (tcon->use_witness) { |
| 2235 | int rc; |
| 2236 | |
| 2237 | rc = cifs_swn_unregister(tcon); |
| 2238 | if (rc < 0) { |
| 2239 | cifs_dbg(VFS, "%s: Failed to unregister for witness notifications: %d\n", |
| 2240 | __func__, rc); |
| 2241 | } |
| 2242 | } |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2243 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2244 | xid = get_xid(); |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2245 | if (ses->server->ops->tree_disconnect) |
| 2246 | ses->server->ops->tree_disconnect(xid, tcon); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2247 | _free_xid(xid); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2248 | |
Suresh Jayaraman | d03382c | 2010-07-05 18:12:27 +0530 | [diff] [blame] | 2249 | cifs_fscache_release_super_cookie(tcon); |
Steve French | 9f84159 | 2010-07-23 20:37:53 +0000 | [diff] [blame] | 2250 | tconInfoFree(tcon); |
Jeff Layton | f1987b4 | 2008-11-15 11:12:47 -0500 | [diff] [blame] | 2251 | cifs_put_smb_ses(ses); |
| 2252 | } |
| 2253 | |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2254 | /** |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2255 | * cifs_get_tcon - get a tcon matching @ctx data from @ses |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 2256 | * @ses: smb session to issue the request on |
| 2257 | * @ctx: the superblock configuration context to use for building the |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2258 | * |
| 2259 | * - tcon refcount is the number of mount points using the tcon. |
| 2260 | * - ses refcount is the number of tcon using the session. |
| 2261 | * |
| 2262 | * 1. This function assumes it is being called from cifs_mount() where |
| 2263 | * we already got a session reference (ses refcount +1). |
| 2264 | * |
| 2265 | * 2. Since we're in the context of adding a mount point, the end |
| 2266 | * result should be either: |
| 2267 | * |
| 2268 | * a) a new tcon already allocated with refcount=1 (1 mount point) and |
| 2269 | * its session refcount incremented (1 new tcon). This +1 was |
| 2270 | * already done in (1). |
| 2271 | * |
| 2272 | * b) an existing tcon with refcount+1 (add a mount point to it) and |
| 2273 | * identical ses refcount (no new tcon). Because of (1) we need to |
| 2274 | * decrement the ses refcount. |
| 2275 | */ |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2276 | static struct cifs_tcon * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2277 | cifs_get_tcon(struct cifs_ses *ses, struct smb3_fs_context *ctx) |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2278 | { |
| 2279 | int rc, xid; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2280 | struct cifs_tcon *tcon; |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2281 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2282 | tcon = cifs_find_tcon(ses, ctx); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2283 | if (tcon) { |
Aurelien Aptel | 4a1360d | 2018-01-25 18:47:52 +0100 | [diff] [blame] | 2284 | /* |
| 2285 | * tcon has refcount already incremented but we need to |
| 2286 | * decrement extra ses reference gotten by caller (case b) |
| 2287 | */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2288 | cifs_dbg(FYI, "Found match on UNC path\n"); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2289 | cifs_put_smb_ses(ses); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2290 | return tcon; |
| 2291 | } |
| 2292 | |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2293 | if (!ses->server->ops->tree_connect) { |
| 2294 | rc = -ENOSYS; |
| 2295 | goto out_fail; |
| 2296 | } |
| 2297 | |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2298 | tcon = tconInfoAlloc(); |
| 2299 | if (tcon == NULL) { |
| 2300 | rc = -ENOMEM; |
| 2301 | goto out_fail; |
| 2302 | } |
| 2303 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2304 | if (ctx->snapshot_time) { |
Steve French | 8b217fe | 2016-11-11 22:36:20 -0600 | [diff] [blame] | 2305 | if (ses->server->vals->protocol_id == 0) { |
| 2306 | cifs_dbg(VFS, |
| 2307 | "Use SMB2 or later for snapshot mount option\n"); |
| 2308 | rc = -EOPNOTSUPP; |
| 2309 | goto out_fail; |
| 2310 | } else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2311 | tcon->snapshot_time = ctx->snapshot_time; |
Steve French | 8b217fe | 2016-11-11 22:36:20 -0600 | [diff] [blame] | 2312 | } |
| 2313 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2314 | if (ctx->handle_timeout) { |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2315 | if (ses->server->vals->protocol_id == 0) { |
| 2316 | cifs_dbg(VFS, |
| 2317 | "Use SMB2.1 or later for handle timeout option\n"); |
| 2318 | rc = -EOPNOTSUPP; |
| 2319 | goto out_fail; |
| 2320 | } else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2321 | tcon->handle_timeout = ctx->handle_timeout; |
Steve French | ca567eb | 2019-03-29 16:31:07 -0500 | [diff] [blame] | 2322 | } |
| 2323 | |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2324 | tcon->ses = ses; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2325 | if (ctx->password) { |
| 2326 | tcon->password = kstrdup(ctx->password, GFP_KERNEL); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2327 | if (!tcon->password) { |
| 2328 | rc = -ENOMEM; |
| 2329 | goto out_fail; |
| 2330 | } |
| 2331 | } |
| 2332 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2333 | if (ctx->seal) { |
Steve French | 23657ad | 2018-04-22 15:14:58 -0500 | [diff] [blame] | 2334 | if (ses->server->vals->protocol_id == 0) { |
| 2335 | cifs_dbg(VFS, |
| 2336 | "SMB3 or later required for encryption\n"); |
| 2337 | rc = -EOPNOTSUPP; |
| 2338 | goto out_fail; |
| 2339 | } else if (tcon->ses->server->capabilities & |
| 2340 | SMB2_GLOBAL_CAP_ENCRYPTION) |
| 2341 | tcon->seal = true; |
| 2342 | else { |
| 2343 | cifs_dbg(VFS, "Encryption is not supported on share\n"); |
| 2344 | rc = -EOPNOTSUPP; |
| 2345 | goto out_fail; |
| 2346 | } |
| 2347 | } |
| 2348 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2349 | if (ctx->linux_ext) { |
Steve French | 8505c8b | 2018-06-18 14:01:59 -0500 | [diff] [blame] | 2350 | if (ses->server->posix_ext_supported) { |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 2351 | tcon->posix_extensions = true; |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 2352 | pr_warn_once("SMB3.11 POSIX Extensions are experimental\n"); |
Steve French | d0cbe56a | 2022-02-06 18:59:57 -0600 | [diff] [blame] | 2353 | } else if ((ses->server->vals->protocol_id == SMB311_PROT_ID) || |
| 2354 | (strcmp(ses->server->vals->version_string, |
| 2355 | SMB3ANY_VERSION_STRING) == 0) || |
| 2356 | (strcmp(ses->server->vals->version_string, |
| 2357 | SMBDEFAULT_VERSION_STRING) == 0)) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 2358 | cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n"); |
Steve French | 8505c8b | 2018-06-18 14:01:59 -0500 | [diff] [blame] | 2359 | rc = -EOPNOTSUPP; |
| 2360 | goto out_fail; |
Steve French | d0cbe56a | 2022-02-06 18:59:57 -0600 | [diff] [blame] | 2361 | } else { |
| 2362 | cifs_dbg(VFS, "Check vers= mount option. SMB3.11 " |
| 2363 | "disabled but required for POSIX extensions\n"); |
| 2364 | rc = -EOPNOTSUPP; |
| 2365 | goto out_fail; |
Steve French | 2fbb564 | 2018-06-12 12:11:31 -0500 | [diff] [blame] | 2366 | } |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 2367 | } |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 2368 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2369 | xid = get_xid(); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2370 | rc = ses->server->ops->tree_connect(xid, ses, ctx->UNC, tcon, |
| 2371 | ctx->local_nls); |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2372 | free_xid(xid); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2373 | cifs_dbg(FYI, "Tcon rc = %d\n", rc); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2374 | if (rc) |
| 2375 | goto out_fail; |
| 2376 | |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2377 | tcon->use_persistent = false; |
| 2378 | /* check if SMB2 or later, CIFS does not support persistent handles */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2379 | if (ctx->persistent) { |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2380 | if (ses->server->vals->protocol_id == 0) { |
| 2381 | cifs_dbg(VFS, |
| 2382 | "SMB3 or later required for persistent handles\n"); |
| 2383 | rc = -EOPNOTSUPP; |
| 2384 | goto out_fail; |
| 2385 | } else if (ses->server->capabilities & |
| 2386 | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) |
| 2387 | tcon->use_persistent = true; |
| 2388 | else /* persistent handles requested but not supported */ { |
| 2389 | cifs_dbg(VFS, |
| 2390 | "Persistent handles not supported on share\n"); |
| 2391 | rc = -EOPNOTSUPP; |
| 2392 | goto out_fail; |
| 2393 | } |
| 2394 | } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY) |
| 2395 | && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2396 | && (ctx->nopersistent == false)) { |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2397 | cifs_dbg(FYI, "enabling persistent handles\n"); |
| 2398 | tcon->use_persistent = true; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2399 | } else if (ctx->resilient) { |
Steve French | 592fafe | 2015-11-03 10:08:53 -0600 | [diff] [blame] | 2400 | if (ses->server->vals->protocol_id == 0) { |
| 2401 | cifs_dbg(VFS, |
| 2402 | "SMB2.1 or later required for resilient handles\n"); |
| 2403 | rc = -EOPNOTSUPP; |
| 2404 | goto out_fail; |
| 2405 | } |
| 2406 | tcon->use_resilient = true; |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2407 | } |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 2408 | |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2409 | tcon->use_witness = false; |
Aurelien Aptel | b7fd0fa | 2021-04-09 16:31:37 +0200 | [diff] [blame] | 2410 | if (IS_ENABLED(CONFIG_CIFS_SWN_UPCALL) && ctx->witness) { |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2411 | if (ses->server->vals->protocol_id >= SMB30_PROT_ID) { |
| 2412 | if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER) { |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 2413 | /* |
| 2414 | * Set witness in use flag in first place |
| 2415 | * to retry registration in the echo task |
| 2416 | */ |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2417 | tcon->use_witness = true; |
Samuel Cabrero | bf80e5d | 2020-11-30 19:02:51 +0100 | [diff] [blame] | 2418 | /* And try to register immediately */ |
| 2419 | rc = cifs_swn_register(tcon); |
| 2420 | if (rc < 0) { |
| 2421 | cifs_dbg(VFS, "Failed to register for witness notifications: %d\n", rc); |
| 2422 | goto out_fail; |
| 2423 | } |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 2424 | } else { |
| 2425 | /* TODO: try to extend for non-cluster uses (eg multichannel) */ |
| 2426 | cifs_dbg(VFS, "witness requested on mount but no CLUSTER capability on share\n"); |
| 2427 | rc = -EOPNOTSUPP; |
| 2428 | goto out_fail; |
| 2429 | } |
| 2430 | } else { |
| 2431 | cifs_dbg(VFS, "SMB3 or later required for witness option\n"); |
| 2432 | rc = -EOPNOTSUPP; |
| 2433 | goto out_fail; |
| 2434 | } |
| 2435 | } |
Steve French | b618f00 | 2015-11-03 09:15:03 -0600 | [diff] [blame] | 2436 | |
Steve French | cae53f7 | 2019-09-03 17:49:46 -0500 | [diff] [blame] | 2437 | /* If the user really knows what they are doing they can override */ |
| 2438 | if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2439 | if (ctx->cache_ro) |
Steve French | cae53f7 | 2019-09-03 17:49:46 -0500 | [diff] [blame] | 2440 | cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2441 | else if (ctx->cache_rw) |
Steve French | cae53f7 | 2019-09-03 17:49:46 -0500 | [diff] [blame] | 2442 | cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n"); |
| 2443 | } |
| 2444 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2445 | if (ctx->no_lease) { |
Kenneth D'souza | 8fd6e1d | 2020-05-18 13:01:34 +0530 | [diff] [blame] | 2446 | if (ses->server->vals->protocol_id == 0) { |
| 2447 | cifs_dbg(VFS, |
| 2448 | "SMB2 or later required for nolease option\n"); |
| 2449 | rc = -EOPNOTSUPP; |
| 2450 | goto out_fail; |
| 2451 | } else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2452 | tcon->no_lease = ctx->no_lease; |
Kenneth D'souza | 8fd6e1d | 2020-05-18 13:01:34 +0530 | [diff] [blame] | 2453 | } |
| 2454 | |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 2455 | /* |
| 2456 | * We can have only one retry value for a connection to a share so for |
| 2457 | * resources mounted more than once to the same server share the last |
| 2458 | * value passed in for the retry flag is used. |
| 2459 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2460 | tcon->retry = ctx->retry; |
| 2461 | tcon->nocase = ctx->nocase; |
Steve French | 3c6e65e | 2020-10-21 00:15:42 -0500 | [diff] [blame] | 2462 | if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2463 | tcon->nohandlecache = ctx->nohandlecache; |
Steve French | 3c6e65e | 2020-10-21 00:15:42 -0500 | [diff] [blame] | 2464 | else |
Jiapeng Zhong | 2be449f | 2021-01-14 17:09:20 +0800 | [diff] [blame] | 2465 | tcon->nohandlecache = true; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2466 | tcon->nodelete = ctx->nodelete; |
| 2467 | tcon->local_lease = ctx->local_lease; |
Pavel Shilovsky | 233839b | 2012-09-19 06:22:45 -0700 | [diff] [blame] | 2468 | INIT_LIST_HEAD(&tcon->pending_opens); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2469 | |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2470 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2471 | list_add(&tcon->tcon_list, &ses->tcon_list); |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 2472 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2473 | |
| 2474 | return tcon; |
| 2475 | |
| 2476 | out_fail: |
| 2477 | tconInfoFree(tcon); |
| 2478 | return ERR_PTR(rc); |
| 2479 | } |
| 2480 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 2481 | void |
| 2482 | cifs_put_tlink(struct tcon_link *tlink) |
| 2483 | { |
| 2484 | if (!tlink || IS_ERR(tlink)) |
| 2485 | return; |
| 2486 | |
| 2487 | if (!atomic_dec_and_test(&tlink->tl_count) || |
| 2488 | test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) { |
| 2489 | tlink->tl_time = jiffies; |
| 2490 | return; |
| 2491 | } |
| 2492 | |
| 2493 | if (!IS_ERR(tlink_tcon(tlink))) |
| 2494 | cifs_put_tcon(tlink_tcon(tlink)); |
| 2495 | kfree(tlink); |
| 2496 | return; |
| 2497 | } |
Jeff Layton | d00c28d | 2010-04-24 07:57:44 -0400 | [diff] [blame] | 2498 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2499 | static int |
| 2500 | compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data) |
| 2501 | { |
| 2502 | struct cifs_sb_info *old = CIFS_SB(sb); |
| 2503 | struct cifs_sb_info *new = mnt_data->cifs_sb; |
Paulo Alcantara (SUSE) | 29fbeb7 | 2019-06-18 16:16:02 -0300 | [diff] [blame] | 2504 | unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK; |
| 2505 | unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2506 | |
| 2507 | if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK)) |
| 2508 | return 0; |
| 2509 | |
Paulo Alcantara (SUSE) | 29fbeb7 | 2019-06-18 16:16:02 -0300 | [diff] [blame] | 2510 | if (old->mnt_cifs_serverino_autodisabled) |
| 2511 | newflags &= ~CIFS_MOUNT_SERVER_INUM; |
| 2512 | |
| 2513 | if (oldflags != newflags) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2514 | return 0; |
| 2515 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2516 | /* |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 2517 | * We want to share sb only if we don't specify an r/wsize or |
| 2518 | * specified r/wsize is greater than or equal to existing one. |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2519 | */ |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2520 | if (new->ctx->wsize && new->ctx->wsize < old->ctx->wsize) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2521 | return 0; |
| 2522 | |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 2523 | if (new->ctx->rsize && new->ctx->rsize < old->ctx->rsize) |
Jeff Layton | 5eba8ab | 2011-10-19 15:30:26 -0400 | [diff] [blame] | 2524 | return 0; |
| 2525 | |
Ronnie Sahlberg | 8401e93 | 2020-12-12 13:40:50 -0600 | [diff] [blame] | 2526 | if (!uid_eq(old->ctx->linux_uid, new->ctx->linux_uid) || |
| 2527 | !gid_eq(old->ctx->linux_gid, new->ctx->linux_gid)) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2528 | return 0; |
| 2529 | |
Ronnie Sahlberg | 8401e93 | 2020-12-12 13:40:50 -0600 | [diff] [blame] | 2530 | if (old->ctx->file_mode != new->ctx->file_mode || |
| 2531 | old->ctx->dir_mode != new->ctx->dir_mode) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2532 | return 0; |
| 2533 | |
| 2534 | if (strcmp(old->local_nls->charset, new->local_nls->charset)) |
| 2535 | return 0; |
| 2536 | |
Steve French | 5780464 | 2021-02-24 12:12:53 -0600 | [diff] [blame] | 2537 | if (old->ctx->acregmax != new->ctx->acregmax) |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2538 | return 0; |
Steve French | 4c9f948 | 2021-02-23 15:50:57 -0600 | [diff] [blame] | 2539 | if (old->ctx->acdirmax != new->ctx->acdirmax) |
| 2540 | return 0; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2541 | |
| 2542 | return 1; |
| 2543 | } |
| 2544 | |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2545 | static int |
| 2546 | match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data) |
| 2547 | { |
| 2548 | struct cifs_sb_info *old = CIFS_SB(sb); |
| 2549 | struct cifs_sb_info *new = mnt_data->cifs_sb; |
Ronnie Sahlberg | fe12926 | 2020-01-22 11:07:56 +1000 | [diff] [blame] | 2550 | bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && |
| 2551 | old->prepath; |
| 2552 | bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) && |
| 2553 | new->prepath; |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2554 | |
Sachin Prabhu | cd8c429 | 2017-04-26 14:05:46 +0100 | [diff] [blame] | 2555 | if (old_set && new_set && !strcmp(new->prepath, old->prepath)) |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2556 | return 1; |
Sachin Prabhu | cd8c429 | 2017-04-26 14:05:46 +0100 | [diff] [blame] | 2557 | else if (!old_set && !new_set) |
| 2558 | return 1; |
| 2559 | |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2560 | return 0; |
| 2561 | } |
| 2562 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2563 | int |
| 2564 | cifs_match_super(struct super_block *sb, void *data) |
| 2565 | { |
| 2566 | struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2567 | struct smb3_fs_context *ctx; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2568 | struct cifs_sb_info *cifs_sb; |
| 2569 | struct TCP_Server_Info *tcp_srv; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 2570 | struct cifs_ses *ses; |
| 2571 | struct cifs_tcon *tcon; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2572 | struct tcon_link *tlink; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2573 | int rc = 0; |
| 2574 | |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2575 | spin_lock(&cifs_tcp_ses_lock); |
| 2576 | cifs_sb = CIFS_SB(sb); |
| 2577 | tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); |
Steve French | 9ed38fd | 2021-09-23 19:18:37 -0500 | [diff] [blame] | 2578 | if (tlink == NULL) { |
| 2579 | /* can not match superblock if tlink were ever null */ |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2580 | spin_unlock(&cifs_tcp_ses_lock); |
Steve French | 9ed38fd | 2021-09-23 19:18:37 -0500 | [diff] [blame] | 2581 | return 0; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2582 | } |
| 2583 | tcon = tlink_tcon(tlink); |
| 2584 | ses = tcon->ses; |
| 2585 | tcp_srv = ses->server; |
| 2586 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2587 | ctx = mnt_data->ctx; |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2588 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2589 | if (!match_server(tcp_srv, ctx) || |
| 2590 | !match_session(ses, ctx) || |
| 2591 | !match_tcon(tcon, ctx) || |
Sachin Prabhu | c1d8b24 | 2016-07-29 22:38:20 +0100 | [diff] [blame] | 2592 | !match_prepath(sb, mnt_data)) { |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2593 | rc = 0; |
| 2594 | goto out; |
| 2595 | } |
| 2596 | |
| 2597 | rc = compare_mount_options(sb, mnt_data); |
| 2598 | out: |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2599 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | f484b5d0 | 2011-07-11 10:16:34 -0400 | [diff] [blame] | 2600 | cifs_put_tlink(tlink); |
Pavel Shilovsky | 25c7f41 | 2011-05-26 23:35:47 +0400 | [diff] [blame] | 2601 | return rc; |
| 2602 | } |
| 2603 | |
Jeff Layton | 09e50d5 | 2008-07-23 10:11:19 -0400 | [diff] [blame] | 2604 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 2605 | static struct lock_class_key cifs_key[2]; |
| 2606 | static struct lock_class_key cifs_slock_key[2]; |
| 2607 | |
| 2608 | static inline void |
| 2609 | cifs_reclassify_socket4(struct socket *sock) |
| 2610 | { |
| 2611 | struct sock *sk = sock->sk; |
Hannes Frederic Sowa | fafc4e1 | 2016-04-08 15:11:27 +0200 | [diff] [blame] | 2612 | BUG_ON(!sock_allow_reclassification(sk)); |
Jeff Layton | 09e50d5 | 2008-07-23 10:11:19 -0400 | [diff] [blame] | 2613 | sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS", |
| 2614 | &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]); |
| 2615 | } |
| 2616 | |
| 2617 | static inline void |
| 2618 | cifs_reclassify_socket6(struct socket *sock) |
| 2619 | { |
| 2620 | struct sock *sk = sock->sk; |
Hannes Frederic Sowa | fafc4e1 | 2016-04-08 15:11:27 +0200 | [diff] [blame] | 2621 | BUG_ON(!sock_allow_reclassification(sk)); |
Jeff Layton | 09e50d5 | 2008-07-23 10:11:19 -0400 | [diff] [blame] | 2622 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS", |
| 2623 | &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]); |
| 2624 | } |
| 2625 | #else |
| 2626 | static inline void |
| 2627 | cifs_reclassify_socket4(struct socket *sock) |
| 2628 | { |
| 2629 | } |
| 2630 | |
| 2631 | static inline void |
| 2632 | cifs_reclassify_socket6(struct socket *sock) |
| 2633 | { |
| 2634 | } |
| 2635 | #endif |
| 2636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | /* See RFC1001 section 14 on representation of Netbios names */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2638 | static void rfc1002mangle(char *target, char *source, unsigned int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | { |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2640 | unsigned int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2642 | for (i = 0, j = 0; i < (length); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | /* mask a nibble at a time and encode */ |
| 2644 | target[j] = 'A' + (0x0F & (source[i] >> 4)); |
| 2645 | target[j+1] = 'A' + (0x0F & source[i]); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2646 | j += 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | } |
| 2650 | |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2651 | static int |
| 2652 | bind_socket(struct TCP_Server_Info *server) |
| 2653 | { |
| 2654 | int rc = 0; |
| 2655 | if (server->srcaddr.ss_family != AF_UNSPEC) { |
| 2656 | /* Bind to the specified local IP address */ |
| 2657 | struct socket *socket = server->ssocket; |
| 2658 | rc = socket->ops->bind(socket, |
| 2659 | (struct sockaddr *) &server->srcaddr, |
| 2660 | sizeof(server->srcaddr)); |
| 2661 | if (rc < 0) { |
| 2662 | struct sockaddr_in *saddr4; |
| 2663 | struct sockaddr_in6 *saddr6; |
| 2664 | saddr4 = (struct sockaddr_in *)&server->srcaddr; |
| 2665 | saddr6 = (struct sockaddr_in6 *)&server->srcaddr; |
| 2666 | if (saddr6->sin6_family == AF_INET6) |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2667 | cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2668 | &saddr6->sin6_addr, rc); |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2669 | else |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2670 | cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n", |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2671 | &saddr4->sin_addr.s_addr, rc); |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2672 | } |
| 2673 | } |
| 2674 | return rc; |
| 2675 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2676 | |
| 2677 | static int |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2678 | ip_rfc1001_connect(struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | { |
| 2680 | int rc = 0; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2681 | /* |
| 2682 | * some servers require RFC1001 sessinit before sending |
| 2683 | * negprot - BB check reconnection in case where second |
| 2684 | * sessinit is sent but no second negprot |
| 2685 | */ |
| 2686 | struct rfc1002_session_packet *ses_init_buf; |
| 2687 | struct smb_hdr *smb_buf; |
| 2688 | ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet), |
| 2689 | GFP_KERNEL); |
| 2690 | if (ses_init_buf) { |
| 2691 | ses_init_buf->trailer.session_req.called_len = 32; |
| 2692 | |
Colin Ian King | 997152f | 2016-01-25 16:25:54 +0000 | [diff] [blame] | 2693 | if (server->server_RFC1001_name[0] != 0) |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2694 | rfc1002mangle(ses_init_buf->trailer. |
| 2695 | session_req.called_name, |
| 2696 | server->server_RFC1001_name, |
| 2697 | RFC1001_NAME_LEN_WITH_NULL); |
| 2698 | else |
| 2699 | rfc1002mangle(ses_init_buf->trailer. |
| 2700 | session_req.called_name, |
| 2701 | DEFAULT_CIFS_CALLED_NAME, |
| 2702 | RFC1001_NAME_LEN_WITH_NULL); |
| 2703 | |
| 2704 | ses_init_buf->trailer.session_req.calling_len = 32; |
| 2705 | |
| 2706 | /* |
| 2707 | * calling name ends in null (byte 16) from old smb |
| 2708 | * convention. |
| 2709 | */ |
Steve French | c85c35f | 2015-03-27 01:15:02 -0500 | [diff] [blame] | 2710 | if (server->workstation_RFC1001_name[0] != 0) |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2711 | rfc1002mangle(ses_init_buf->trailer. |
| 2712 | session_req.calling_name, |
| 2713 | server->workstation_RFC1001_name, |
| 2714 | RFC1001_NAME_LEN_WITH_NULL); |
| 2715 | else |
| 2716 | rfc1002mangle(ses_init_buf->trailer. |
| 2717 | session_req.calling_name, |
| 2718 | "LINUX_CIFS_CLNT", |
| 2719 | RFC1001_NAME_LEN_WITH_NULL); |
| 2720 | |
| 2721 | ses_init_buf->trailer.session_req.scope1 = 0; |
| 2722 | ses_init_buf->trailer.session_req.scope2 = 0; |
| 2723 | smb_buf = (struct smb_hdr *)ses_init_buf; |
| 2724 | |
| 2725 | /* sizeof RFC1002_SESSION_REQUEST with no scope */ |
Steve French | be8e3b0 | 2011-04-29 05:40:20 +0000 | [diff] [blame] | 2726 | smb_buf->smb_buf_length = cpu_to_be32(0x81000044); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2727 | rc = smb_send(server, smb_buf, 0x44); |
| 2728 | kfree(ses_init_buf); |
| 2729 | /* |
| 2730 | * RFC1001 layer in at least one server |
| 2731 | * requires very short break before negprot |
| 2732 | * presumably because not expecting negprot |
| 2733 | * to follow so fast. This is a simple |
| 2734 | * solution that works without |
| 2735 | * complicating the code and causes no |
| 2736 | * significant slowing down on mount |
| 2737 | * for everyone else |
| 2738 | */ |
| 2739 | usleep_range(1000, 2000); |
| 2740 | } |
| 2741 | /* |
| 2742 | * else the negprot may still work without this |
| 2743 | * even though malloc failed |
| 2744 | */ |
| 2745 | |
| 2746 | return rc; |
| 2747 | } |
| 2748 | |
| 2749 | static int |
| 2750 | generic_ip_connect(struct TCP_Server_Info *server) |
| 2751 | { |
| 2752 | int rc = 0; |
Steve French | 6da9791 | 2011-03-13 18:55:55 +0000 | [diff] [blame] | 2753 | __be16 sport; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2754 | int slen, sfamily; |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2755 | struct socket *socket = server->ssocket; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2756 | struct sockaddr *saddr; |
| 2757 | |
| 2758 | saddr = (struct sockaddr *) &server->dstaddr; |
| 2759 | |
| 2760 | if (server->dstaddr.ss_family == AF_INET6) { |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2761 | struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 2762 | |
| 2763 | sport = ipv6->sin6_port; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2764 | slen = sizeof(struct sockaddr_in6); |
| 2765 | sfamily = AF_INET6; |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2766 | cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr, |
| 2767 | ntohs(sport)); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2768 | } else { |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2769 | struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr; |
| 2770 | |
| 2771 | sport = ipv4->sin_port; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2772 | slen = sizeof(struct sockaddr_in); |
| 2773 | sfamily = AF_INET; |
Samuel Cabrero | def6e1d | 2020-10-16 11:54:55 +0200 | [diff] [blame] | 2774 | cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr, |
| 2775 | ntohs(sport)); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2778 | if (socket == NULL) { |
Rob Landley | f1d0c99 | 2011-01-22 15:44:05 -0600 | [diff] [blame] | 2779 | rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM, |
| 2780 | IPPROTO_TCP, &socket, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | if (rc < 0) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 2782 | cifs_server_dbg(VFS, "Error %d creating socket\n", rc); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2783 | server->ssocket = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | } |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2786 | |
| 2787 | /* BB other socket options to set KEEPALIVE, NODELAY? */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2788 | cifs_dbg(FYI, "Socket created\n"); |
Jeff Layton | bcf4b10 | 2008-12-01 18:42:15 -0500 | [diff] [blame] | 2789 | server->ssocket = socket; |
| 2790 | socket->sk->sk_allocation = GFP_NOFS; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2791 | if (sfamily == AF_INET6) |
| 2792 | cifs_reclassify_socket6(socket); |
| 2793 | else |
| 2794 | cifs_reclassify_socket4(socket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Ben Greear | 3eb9a88 | 2010-09-01 17:06:02 -0700 | [diff] [blame] | 2797 | rc = bind_socket(server); |
| 2798 | if (rc < 0) |
| 2799 | return rc; |
| 2800 | |
Jeff Layton | d5c5605 | 2008-12-01 18:42:33 -0500 | [diff] [blame] | 2801 | /* |
| 2802 | * Eventually check for other socket options to change from |
| 2803 | * the default. sock_setsockopt not used because it expects |
| 2804 | * user space buffer |
| 2805 | */ |
| 2806 | socket->sk->sk_rcvtimeo = 7 * HZ; |
Steve French | da505c3 | 2009-01-19 03:49:35 +0000 | [diff] [blame] | 2807 | socket->sk->sk_sndtimeo = 5 * HZ; |
Steve French | 6a5fa236 | 2010-01-01 01:28:43 +0000 | [diff] [blame] | 2808 | |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2809 | /* make the bufsizes depend on wsize/rsize and max requests */ |
| 2810 | if (server->noautotune) { |
| 2811 | if (socket->sk->sk_sndbuf < (200 * 1024)) |
| 2812 | socket->sk->sk_sndbuf = 200 * 1024; |
| 2813 | if (socket->sk->sk_rcvbuf < (140 * 1024)) |
| 2814 | socket->sk->sk_rcvbuf = 140 * 1024; |
| 2815 | } |
| 2816 | |
Christoph Hellwig | 12abc5e | 2020-05-28 07:12:19 +0200 | [diff] [blame] | 2817 | if (server->tcp_nodelay) |
| 2818 | tcp_sock_set_nodelay(socket->sk); |
Steve French | 6a5fa236 | 2010-01-01 01:28:43 +0000 | [diff] [blame] | 2819 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2820 | cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n", |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2821 | socket->sk->sk_sndbuf, |
| 2822 | socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo); |
| 2823 | |
Paulo Alcantara (SUSE) | 8eecd1c | 2019-07-16 19:04:50 -0300 | [diff] [blame] | 2824 | rc = socket->ops->connect(socket, saddr, slen, |
| 2825 | server->noblockcnt ? O_NONBLOCK : 0); |
Paulo Alcantara (SUSE) | d532cc7 | 2019-10-10 12:31:58 -0300 | [diff] [blame] | 2826 | /* |
| 2827 | * When mounting SMB root file systems, we do not want to block in |
| 2828 | * connect. Otherwise bail out and then let cifs_reconnect() perform |
| 2829 | * reconnect failover - if possible. |
| 2830 | */ |
| 2831 | if (server->noblockcnt && rc == -EINPROGRESS) |
Paulo Alcantara (SUSE) | 8eecd1c | 2019-07-16 19:04:50 -0300 | [diff] [blame] | 2832 | rc = 0; |
Jeff Layton | ee1b3ea | 2011-06-21 07:18:26 -0400 | [diff] [blame] | 2833 | if (rc < 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2834 | cifs_dbg(FYI, "Error %d connecting to server\n", rc); |
Steve French | d7171cd | 2021-11-04 15:56:37 -0500 | [diff] [blame] | 2835 | trace_smb3_connect_err(server->hostname, server->conn_id, &server->dstaddr, rc); |
Jeff Layton | ee1b3ea | 2011-06-21 07:18:26 -0400 | [diff] [blame] | 2836 | sock_release(socket); |
| 2837 | server->ssocket = NULL; |
| 2838 | return rc; |
| 2839 | } |
Steve French | d7171cd | 2021-11-04 15:56:37 -0500 | [diff] [blame] | 2840 | trace_smb3_connect_done(server->hostname, server->conn_id, &server->dstaddr); |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2841 | if (sport == htons(RFC1001_PORT)) |
| 2842 | rc = ip_rfc1001_connect(server); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | return rc; |
| 2845 | } |
| 2846 | |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2847 | static int |
| 2848 | ip_connect(struct TCP_Server_Info *server) |
| 2849 | { |
Steve French | 6da9791 | 2011-03-13 18:55:55 +0000 | [diff] [blame] | 2850 | __be16 *sport; |
Pavel Shilovsky | a9f1b85 | 2010-12-13 19:08:35 +0300 | [diff] [blame] | 2851 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 2852 | struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr; |
| 2853 | |
| 2854 | if (server->dstaddr.ss_family == AF_INET6) |
| 2855 | sport = &addr6->sin6_port; |
| 2856 | else |
| 2857 | sport = &addr->sin_port; |
| 2858 | |
| 2859 | if (*sport == 0) { |
| 2860 | int rc; |
| 2861 | |
| 2862 | /* try with 445 port at first */ |
| 2863 | *sport = htons(CIFS_PORT); |
| 2864 | |
| 2865 | rc = generic_ip_connect(server); |
| 2866 | if (rc >= 0) |
| 2867 | return rc; |
| 2868 | |
| 2869 | /* if it failed, try with 139 port */ |
| 2870 | *sport = htons(RFC1001_PORT); |
| 2871 | } |
| 2872 | |
| 2873 | return generic_ip_connect(server); |
| 2874 | } |
| 2875 | |
Pavel Shilovsky | 6d5786a | 2012-06-20 11:21:16 +0400 | [diff] [blame] | 2876 | void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon, |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2877 | struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2878 | { |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2879 | /* |
| 2880 | * If we are reconnecting then should we check to see if |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2881 | * any requested capabilities changed locally e.g. via |
| 2882 | * remount but we can not do much about it here |
| 2883 | * if they have (even if we could detect it by the following) |
| 2884 | * Perhaps we could add a backpointer to array of sb from tcon |
| 2885 | * or if we change to make all sb to same share the same |
| 2886 | * sb as NFS - then we only have one backpointer to sb. |
| 2887 | * What if we wanted to mount the server share twice once with |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2888 | * and once without posixacls or posix paths? |
| 2889 | */ |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2890 | __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2891 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2892 | if (ctx && ctx->no_linux_ext) { |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2893 | tcon->fsUnixInfo.Capability = 0; |
| 2894 | tcon->unix_ext = 0; /* Unix Extensions disabled */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2895 | cifs_dbg(FYI, "Linux protocol extensions disabled\n"); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2896 | return; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2897 | } else if (ctx) |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2898 | tcon->unix_ext = 1; /* Unix Extensions supported */ |
| 2899 | |
Jiapeng Zhong | 16a7885 | 2021-01-14 18:02:23 +0800 | [diff] [blame] | 2900 | if (!tcon->unix_ext) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2901 | cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n"); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 2902 | return; |
| 2903 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2904 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2905 | if (!CIFSSMBQFSUnixInfo(xid, tcon)) { |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2906 | __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2907 | cifs_dbg(FYI, "unix caps which server supports %lld\n", cap); |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2908 | /* |
| 2909 | * check for reconnect case in which we do not |
| 2910 | * want to change the mount behavior if we can avoid it |
| 2911 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2912 | if (ctx == NULL) { |
Steve French | bc04499 | 2020-12-11 19:48:26 -0600 | [diff] [blame] | 2913 | /* |
| 2914 | * turn off POSIX ACL and PATHNAMES if not set |
| 2915 | * originally at mount time |
| 2916 | */ |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2917 | if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0) |
| 2918 | cap &= ~CIFS_UNIX_POSIX_ACL_CAP; |
Igor Mammedov | 11b6d64 | 2008-02-15 19:06:04 +0000 | [diff] [blame] | 2919 | if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { |
| 2920 | if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2921 | cifs_dbg(VFS, "POSIXPATH support change\n"); |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2922 | cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; |
Igor Mammedov | 11b6d64 | 2008-02-15 19:06:04 +0000 | [diff] [blame] | 2923 | } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2924 | cifs_dbg(VFS, "possible reconnect error\n"); |
| 2925 | cifs_dbg(VFS, "server disabled POSIX path support\n"); |
Igor Mammedov | 11b6d64 | 2008-02-15 19:06:04 +0000 | [diff] [blame] | 2926 | } |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2927 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2928 | |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2929 | if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2930 | cifs_dbg(VFS, "per-share encryption not supported yet\n"); |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2931 | |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2932 | cap &= CIFS_UNIX_CAP_MASK; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2933 | if (ctx && ctx->no_psx_acl) |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2934 | cap &= ~CIFS_UNIX_POSIX_ACL_CAP; |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2935 | else if (CIFS_UNIX_POSIX_ACL_CAP & cap) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2936 | cifs_dbg(FYI, "negotiated posix acl support\n"); |
Al Viro | 2c6292a | 2011-06-17 09:05:48 -0400 | [diff] [blame] | 2937 | if (cifs_sb) |
| 2938 | cifs_sb->mnt_cifs_flags |= |
| 2939 | CIFS_MOUNT_POSIXACL; |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2942 | if (ctx && ctx->posix_paths == 0) |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2943 | cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2944 | else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2945 | cifs_dbg(FYI, "negotiate posix pathnames\n"); |
Al Viro | 2c6292a | 2011-06-17 09:05:48 -0400 | [diff] [blame] | 2946 | if (cifs_sb) |
| 2947 | cifs_sb->mnt_cifs_flags |= |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2948 | CIFS_MOUNT_POSIX_PATHS; |
| 2949 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 2950 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2951 | cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap); |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2952 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2953 | if (cap & CIFS_UNIX_FCNTL_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2954 | cifs_dbg(FYI, "FCNTL cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2955 | if (cap & CIFS_UNIX_EXTATTR_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2956 | cifs_dbg(FYI, "EXTATTR cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2957 | if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2958 | cifs_dbg(FYI, "POSIX path cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2959 | if (cap & CIFS_UNIX_XATTR_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2960 | cifs_dbg(FYI, "XATTR cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2961 | if (cap & CIFS_UNIX_POSIX_ACL_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2962 | cifs_dbg(FYI, "POSIX ACL cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2963 | if (cap & CIFS_UNIX_LARGE_READ_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2964 | cifs_dbg(FYI, "very large read cap\n"); |
Steve French | 75865f8c | 2007-06-24 18:30:48 +0000 | [diff] [blame] | 2965 | if (cap & CIFS_UNIX_LARGE_WRITE_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2966 | cifs_dbg(FYI, "very large write cap\n"); |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2967 | if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2968 | cifs_dbg(FYI, "transport encryption cap\n"); |
Steve French | 6848b73 | 2011-05-26 18:38:54 +0000 | [diff] [blame] | 2969 | if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2970 | cifs_dbg(FYI, "mandatory transport encryption cap\n"); |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2971 | #endif /* CIFS_DEBUG2 */ |
| 2972 | if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2973 | if (ctx == NULL) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2974 | cifs_dbg(FYI, "resetting capabilities failed\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 2975 | else |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 2976 | 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"); |
Steve French | 5a44b31 | 2007-09-20 15:16:24 +0000 | [diff] [blame] | 2977 | |
Steve French | 8af1897 | 2007-02-14 04:42:51 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | |
Ronnie Sahlberg | 51acd20 | 2020-12-14 16:40:24 +1000 | [diff] [blame] | 2982 | int cifs_setup_cifs_sb(struct cifs_sb_info *cifs_sb) |
Jeff Layton | b1c8d2b | 2008-10-22 13:57:07 -0400 | [diff] [blame] | 2983 | { |
Ronnie Sahlberg | 51acd20 | 2020-12-14 16:40:24 +1000 | [diff] [blame] | 2984 | struct smb3_fs_context *ctx = cifs_sb->ctx; |
| 2985 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 2986 | INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks); |
| 2987 | |
Al Viro | 2ced6f6 | 2011-06-17 09:20:04 -0400 | [diff] [blame] | 2988 | spin_lock_init(&cifs_sb->tlink_tree_lock); |
| 2989 | cifs_sb->tlink_tree = RB_ROOT; |
| 2990 | |
Frank Sorenson | f52aa79 | 2020-02-12 15:31:48 -0600 | [diff] [blame] | 2991 | cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n", |
Ronnie Sahlberg | 51acd20 | 2020-12-14 16:40:24 +1000 | [diff] [blame] | 2992 | ctx->file_mode, ctx->dir_mode); |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 2993 | |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 2994 | /* this is needed for ASCII cp to Unicode converts */ |
| 2995 | if (ctx->iocharset == NULL) { |
| 2996 | /* load_nls_default cannot return null */ |
| 2997 | cifs_sb->local_nls = load_nls_default(); |
| 2998 | } else { |
| 2999 | cifs_sb->local_nls = load_nls(ctx->iocharset); |
| 3000 | if (cifs_sb->local_nls == NULL) { |
| 3001 | cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n", |
| 3002 | ctx->iocharset); |
| 3003 | return -ELIBACC; |
| 3004 | } |
| 3005 | } |
| 3006 | ctx->local_nls = cifs_sb->local_nls; |
Suresh Jayaraman | 6d20e84 | 2010-12-01 14:42:28 +0530 | [diff] [blame] | 3007 | |
Ronnie Sahlberg | 2d39f50 | 2020-12-14 16:40:25 +1000 | [diff] [blame] | 3008 | smb3_update_mnt_flags(cifs_sb); |
| 3009 | |
| 3010 | if (ctx->direct_io) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3011 | cifs_dbg(FYI, "mounting share using direct i/o\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3012 | if (ctx->cache_ro) { |
Steve French | 83bbfa7 | 2019-08-27 23:58:54 -0500 | [diff] [blame] | 3013 | cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n"); |
| 3014 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3015 | } else if (ctx->cache_rw) { |
Steve French | 41e033f | 2019-08-30 02:12:41 -0500 | [diff] [blame] | 3016 | cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n"); |
| 3017 | cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE | |
| 3018 | CIFS_MOUNT_RW_CACHE); |
Steve French | 83bbfa7 | 2019-08-27 23:58:54 -0500 | [diff] [blame] | 3019 | } |
Steve French | 3b79521 | 2008-11-13 19:45:32 +0000 | [diff] [blame] | 3020 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3021 | if ((ctx->cifs_acl) && (ctx->dynperm)) |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3022 | cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n"); |
Sachin Prabhu | 4214ebf | 2016-07-29 22:38:19 +0100 | [diff] [blame] | 3023 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3024 | if (ctx->prepath) { |
| 3025 | cifs_sb->prepath = kstrdup(ctx->prepath, GFP_KERNEL); |
Sachin Prabhu | 4214ebf | 2016-07-29 22:38:19 +0100 | [diff] [blame] | 3026 | if (cifs_sb->prepath == NULL) |
| 3027 | return -ENOMEM; |
Shyam Prasad N | a738c93 | 2021-02-11 03:26:54 -0800 | [diff] [blame] | 3028 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; |
Sachin Prabhu | 4214ebf | 2016-07-29 22:38:19 +0100 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | return 0; |
Jeff Layton | b1c8d2b | 2008-10-22 13:57:07 -0400 | [diff] [blame] | 3032 | } |
| 3033 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3034 | /* Release all succeed connections */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3035 | static inline void mount_put_conns(struct mount_ctx *mnt_ctx) |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3036 | { |
| 3037 | int rc = 0; |
| 3038 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3039 | if (mnt_ctx->tcon) |
| 3040 | cifs_put_tcon(mnt_ctx->tcon); |
| 3041 | else if (mnt_ctx->ses) |
| 3042 | cifs_put_smb_ses(mnt_ctx->ses); |
| 3043 | else if (mnt_ctx->server) |
| 3044 | cifs_put_tcp_session(mnt_ctx->server, 0); |
| 3045 | mnt_ctx->cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS; |
| 3046 | free_xid(mnt_ctx->xid); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /* Get connections for tcp, ses and tcon */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3050 | static int mount_get_conns(struct mount_ctx *mnt_ctx) |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3051 | { |
| 3052 | int rc = 0; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3053 | struct TCP_Server_Info *server = NULL; |
| 3054 | struct cifs_ses *ses = NULL; |
| 3055 | struct cifs_tcon *tcon = NULL; |
| 3056 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
| 3057 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3058 | unsigned int xid; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3059 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3060 | xid = get_xid(); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3061 | |
| 3062 | /* get a reference to a tcp session */ |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 3063 | server = cifs_get_tcp_session(ctx, NULL); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3064 | if (IS_ERR(server)) { |
| 3065 | rc = PTR_ERR(server); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3066 | server = NULL; |
| 3067 | goto out; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3068 | } |
| 3069 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3070 | /* get a reference to a SMB session */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3071 | ses = cifs_get_smb_ses(server, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3072 | if (IS_ERR(ses)) { |
| 3073 | rc = PTR_ERR(ses); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3074 | ses = NULL; |
| 3075 | goto out; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3076 | } |
| 3077 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3078 | if ((ctx->persistent == true) && (!(ses->server->capabilities & |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3079 | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 3080 | cifs_server_dbg(VFS, "persistent handles not supported by server\n"); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3081 | rc = -EOPNOTSUPP; |
| 3082 | goto out; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | /* search for existing tcon to this server share */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3086 | tcon = cifs_get_tcon(ses, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3087 | if (IS_ERR(tcon)) { |
| 3088 | rc = PTR_ERR(tcon); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3089 | tcon = NULL; |
| 3090 | goto out; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3091 | } |
| 3092 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3093 | /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */ |
| 3094 | if (tcon->posix_extensions) |
| 3095 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS; |
| 3096 | |
| 3097 | /* tell server which Unix caps we support */ |
| 3098 | if (cap_unix(tcon->ses)) { |
| 3099 | /* |
| 3100 | * reset of caps checks mount to see if unix extensions disabled |
| 3101 | * for just this mount. |
| 3102 | */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3103 | reset_cifs_unix_caps(xid, tcon, cifs_sb, ctx); |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 3104 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3105 | if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) && |
| 3106 | (le64_to_cpu(tcon->fsUnixInfo.Capability) & |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3107 | CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) { |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 3108 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3109 | rc = -EACCES; |
| 3110 | goto out; |
| 3111 | } |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 3112 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3113 | } else |
| 3114 | tcon->unix_ext = 0; /* server does not support them */ |
| 3115 | |
| 3116 | /* do not care if a following call succeed - informational */ |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 3117 | if (!tcon->pipe && server->ops->qfs_tcon) { |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3118 | server->ops->qfs_tcon(xid, tcon, cifs_sb); |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 3119 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) { |
| 3120 | if (tcon->fsDevInfo.DeviceCharacteristics & |
Steve French | 52870d5 | 2019-10-01 21:25:46 -0500 | [diff] [blame] | 3121 | cpu_to_le32(FILE_READ_ONLY_DEVICE)) |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 3122 | cifs_dbg(VFS, "mounted to read only share\n"); |
Steve French | 41e033f | 2019-08-30 02:12:41 -0500 | [diff] [blame] | 3123 | else if ((cifs_sb->mnt_cifs_flags & |
| 3124 | CIFS_MOUNT_RW_CACHE) == 0) |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 3125 | cifs_dbg(VFS, "read only mount of RW share\n"); |
Steve French | 41e033f | 2019-08-30 02:12:41 -0500 | [diff] [blame] | 3126 | /* no need to log a RW mount of a typical RW share */ |
Steve French | 1981eba | 2019-08-29 22:33:38 -0500 | [diff] [blame] | 3127 | } |
| 3128 | } |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3129 | |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 3130 | /* |
| 3131 | * Clamp the rsize/wsize mount arguments if they are too big for the server |
Steve French | 0c2b5f7 | 2020-12-15 13:28:50 -0600 | [diff] [blame] | 3132 | * and set the rsize/wsize to the negotiated values if not passed in by |
| 3133 | * the user on mount |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 3134 | */ |
Steve French | 0c2b5f7 | 2020-12-15 13:28:50 -0600 | [diff] [blame] | 3135 | if ((cifs_sb->ctx->wsize == 0) || |
| 3136 | (cifs_sb->ctx->wsize > server->ops->negotiate_wsize(tcon, ctx))) |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 3137 | cifs_sb->ctx->wsize = server->ops->negotiate_wsize(tcon, ctx); |
Steve French | 0c2b5f7 | 2020-12-15 13:28:50 -0600 | [diff] [blame] | 3138 | if ((cifs_sb->ctx->rsize == 0) || |
| 3139 | (cifs_sb->ctx->rsize > server->ops->negotiate_rsize(tcon, ctx))) |
Ronnie Sahlberg | 522aa3b | 2020-12-14 16:40:17 +1000 | [diff] [blame] | 3140 | cifs_sb->ctx->rsize = server->ops->negotiate_rsize(tcon, ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3141 | |
Shyam Prasad N | b774302 | 2021-12-08 16:33:19 +0000 | [diff] [blame] | 3142 | /* |
| 3143 | * The cookie is initialized from volume info returned above. |
| 3144 | * Inside cifs_fscache_get_super_cookie it checks |
| 3145 | * that we do not get super cookie twice. |
| 3146 | */ |
David Howells | 70431bf | 2020-11-17 15:56:59 +0000 | [diff] [blame] | 3147 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_FSCACHE) |
| 3148 | cifs_fscache_get_super_cookie(tcon); |
Shyam Prasad N | b774302 | 2021-12-08 16:33:19 +0000 | [diff] [blame] | 3149 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3150 | out: |
| 3151 | mnt_ctx->server = server; |
| 3152 | mnt_ctx->ses = ses; |
| 3153 | mnt_ctx->tcon = tcon; |
| 3154 | mnt_ctx->xid = xid; |
| 3155 | |
| 3156 | return rc; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, |
| 3160 | struct cifs_tcon *tcon) |
| 3161 | { |
| 3162 | struct tcon_link *tlink; |
| 3163 | |
| 3164 | /* hang the tcon off of the superblock */ |
| 3165 | tlink = kzalloc(sizeof(*tlink), GFP_KERNEL); |
| 3166 | if (tlink == NULL) |
| 3167 | return -ENOMEM; |
| 3168 | |
| 3169 | tlink->tl_uid = ses->linux_uid; |
| 3170 | tlink->tl_tcon = tcon; |
| 3171 | tlink->tl_time = jiffies; |
| 3172 | set_bit(TCON_LINK_MASTER, &tlink->tl_flags); |
| 3173 | set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 3174 | |
| 3175 | cifs_sb->master_tlink = tlink; |
| 3176 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 3177 | tlink_rb_insert(&cifs_sb->tlink_tree, tlink); |
| 3178 | spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3179 | |
| 3180 | queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, |
| 3181 | TLINK_IDLE_EXPIRE); |
| 3182 | return 0; |
| 3183 | } |
Jeff Layton | b9bce2e | 2011-07-06 08:10:39 -0400 | [diff] [blame] | 3184 | |
Steve French | 2d6d589 | 2009-04-09 00:36:44 +0000 | [diff] [blame] | 3185 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3186 | /* Get unique dfs connections */ |
| 3187 | static int mount_get_dfs_conns(struct mount_ctx *mnt_ctx) |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3188 | { |
| 3189 | int rc; |
| 3190 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3191 | mnt_ctx->fs_ctx->nosharesock = true; |
| 3192 | rc = mount_get_conns(mnt_ctx); |
| 3193 | if (mnt_ctx->server) { |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3194 | cifs_dbg(FYI, "%s: marking tcp session as a dfs connection\n", __func__); |
| 3195 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3196 | mnt_ctx->server->is_dfs_conn = true; |
Paulo Alcantara | cdc3363 | 2021-07-15 21:53:53 -0300 | [diff] [blame] | 3197 | spin_unlock(&cifs_tcp_ses_lock); |
| 3198 | } |
| 3199 | return rc; |
| 3200 | } |
| 3201 | |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 3202 | /* |
| 3203 | * cifs_build_path_to_root returns full path to root when we do not have an |
Steve French | 201023c | 2021-02-15 11:03:45 -0600 | [diff] [blame] | 3204 | * existing connection (tcon) |
Steve French | 6d3ea7e | 2012-11-28 22:34:41 -0600 | [diff] [blame] | 3205 | */ |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3206 | static char * |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3207 | build_unc_path_to_root(const struct smb3_fs_context *ctx, |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3208 | const struct cifs_sb_info *cifs_sb, bool useppath) |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3209 | { |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3210 | char *full_path, *pos; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3211 | unsigned int pplen = useppath && ctx->prepath ? |
| 3212 | strlen(ctx->prepath) + 1 : 0; |
| 3213 | unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3214 | |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 3215 | if (unc_len > MAX_TREE_SIZE) |
| 3216 | return ERR_PTR(-EINVAL); |
| 3217 | |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3218 | full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3219 | if (full_path == NULL) |
| 3220 | return ERR_PTR(-ENOMEM); |
| 3221 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3222 | memcpy(full_path, ctx->UNC, unc_len); |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3223 | pos = full_path + unc_len; |
| 3224 | |
| 3225 | if (pplen) { |
Jeff Layton | 1fc29ba | 2013-05-31 10:00:18 -0400 | [diff] [blame] | 3226 | *pos = CIFS_DIR_SEP(cifs_sb); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3227 | memcpy(pos + 1, ctx->prepath, pplen); |
Jeff Layton | b2a0fa1 | 2011-07-06 08:10:36 -0400 | [diff] [blame] | 3228 | pos += pplen; |
| 3229 | } |
| 3230 | |
| 3231 | *pos = '\0'; /* add trailing null */ |
Steve French | f87d39d | 2011-05-27 03:50:55 +0000 | [diff] [blame] | 3232 | convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb)); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3233 | cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path); |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3234 | return full_path; |
| 3235 | } |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3236 | |
Steve French | 4c51de1 | 2021-09-13 18:29:46 -0500 | [diff] [blame] | 3237 | /* |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3238 | * expand_dfs_referral - Update cifs_sb from dfs referral path |
Paulo Alcantara | 1c78022 | 2018-11-14 16:24:03 -0200 | [diff] [blame] | 3239 | * |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3240 | * cifs_sb->ctx->mount_options will be (re-)allocated to a string containing updated options for the |
| 3241 | * submount. Otherwise it will be left untouched. |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3242 | */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3243 | static int expand_dfs_referral(struct mount_ctx *mnt_ctx, const char *full_path, |
| 3244 | struct dfs_info3_param *referral) |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3245 | { |
| 3246 | int rc; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3247 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3248 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
| 3249 | char *fake_devname = NULL, *mdata = NULL; |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3250 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3251 | mdata = cifs_compose_mount_options(cifs_sb->ctx->mount_options, full_path + 1, referral, |
| 3252 | &fake_devname); |
| 3253 | if (IS_ERR(mdata)) { |
| 3254 | rc = PTR_ERR(mdata); |
| 3255 | mdata = NULL; |
| 3256 | } else { |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3257 | /* |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3258 | * We can not clear out the whole structure since we no longer have an explicit |
| 3259 | * function to parse a mount-string. Instead we need to clear out the individual |
| 3260 | * fields that are no longer valid. |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3261 | */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3262 | kfree(ctx->prepath); |
| 3263 | ctx->prepath = NULL; |
| 3264 | rc = cifs_setup_volume_info(ctx, mdata, fake_devname); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3265 | } |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3266 | kfree(fake_devname); |
| 3267 | kfree(cifs_sb->ctx->mount_options); |
| 3268 | cifs_sb->ctx->mount_options = mdata; |
Paulo Alcantara | 8513222 | 2021-02-24 20:59:21 -0300 | [diff] [blame] | 3269 | |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3270 | return rc; |
| 3271 | } |
Steve French | 2d6d589 | 2009-04-09 00:36:44 +0000 | [diff] [blame] | 3272 | #endif |
Igor Mammedov | 1bfe73c | 2009-04-01 17:54:42 +0400 | [diff] [blame] | 3273 | |
Ronnie Sahlberg | 24e0a1e | 2020-12-10 00:06:02 -0600 | [diff] [blame] | 3274 | /* TODO: all callers to this are broken. We are not parsing mount_options here |
| 3275 | * we should pass a clone of the original context? |
| 3276 | */ |
Paulo Alcantara (SUSE) | 5072010 | 2019-03-19 16:54:29 -0300 | [diff] [blame] | 3277 | int |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3278 | cifs_setup_volume_info(struct smb3_fs_context *ctx, const char *mntopts, const char *devname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | { |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3280 | int rc; |
Sean Finney | dd61394 | 2011-04-11 13:19:30 +0000 | [diff] [blame] | 3281 | |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3282 | if (devname) { |
| 3283 | cifs_dbg(FYI, "%s: devname=%s\n", __func__, devname); |
| 3284 | rc = smb3_parse_devname(devname, ctx); |
| 3285 | if (rc) { |
| 3286 | cifs_dbg(VFS, "%s: failed to parse %s: %d\n", __func__, devname, rc); |
| 3287 | return rc; |
| 3288 | } |
| 3289 | } |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3290 | |
| 3291 | if (mntopts) { |
| 3292 | char *ip; |
| 3293 | |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3294 | rc = smb3_parse_opt(mntopts, "ip", &ip); |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3295 | if (rc) { |
| 3296 | cifs_dbg(VFS, "%s: failed to parse ip options: %d\n", __func__, rc); |
| 3297 | return rc; |
| 3298 | } |
| 3299 | |
| 3300 | rc = cifs_convert_address((struct sockaddr *)&ctx->dstaddr, ip, strlen(ip)); |
| 3301 | kfree(ip); |
| 3302 | if (!rc) { |
Ronnie Sahlberg | 0d4873f | 2021-01-28 21:35:10 -0600 | [diff] [blame] | 3303 | cifs_dbg(VFS, "%s: failed to convert ip address\n", __func__); |
| 3304 | return -EINVAL; |
| 3305 | } |
| 3306 | } |
| 3307 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3308 | if (ctx->nullauth) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3309 | cifs_dbg(FYI, "Anonymous login\n"); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3310 | kfree(ctx->username); |
| 3311 | ctx->username = NULL; |
| 3312 | } else if (ctx->username) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | /* BB fixme parse for domain name here */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3314 | cifs_dbg(FYI, "Username: %s\n", ctx->username); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3315 | } else { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3316 | cifs_dbg(VFS, "No username specified\n"); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3317 | /* In userspace mount helper we can get user name from alternate |
| 3318 | locations such as env variables and files on disk */ |
Jeff Layton | 04db79b | 2011-07-06 08:10:38 -0400 | [diff] [blame] | 3319 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Paulo Alcantara | 5c1acf3 | 2021-05-03 11:55:26 -0300 | [diff] [blame] | 3322 | return 0; |
Jeff Layton | 04db79b | 2011-07-06 08:10:38 -0400 | [diff] [blame] | 3323 | } |
| 3324 | |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3325 | static int |
| 3326 | cifs_are_all_path_components_accessible(struct TCP_Server_Info *server, |
| 3327 | unsigned int xid, |
| 3328 | struct cifs_tcon *tcon, |
| 3329 | struct cifs_sb_info *cifs_sb, |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3330 | char *full_path, |
| 3331 | int added_treename) |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3332 | { |
| 3333 | int rc; |
| 3334 | char *s; |
| 3335 | char sep, tmp; |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3336 | int skip = added_treename ? 1 : 0; |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3337 | |
| 3338 | sep = CIFS_DIR_SEP(cifs_sb); |
| 3339 | s = full_path; |
| 3340 | |
| 3341 | rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, ""); |
| 3342 | while (rc == 0) { |
| 3343 | /* skip separators */ |
| 3344 | while (*s == sep) |
| 3345 | s++; |
| 3346 | if (!*s) |
| 3347 | break; |
| 3348 | /* next separator */ |
| 3349 | while (*s && *s != sep) |
| 3350 | s++; |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3351 | /* |
| 3352 | * if the treename is added, we then have to skip the first |
| 3353 | * part within the separators |
| 3354 | */ |
| 3355 | if (skip) { |
| 3356 | skip = 0; |
| 3357 | continue; |
| 3358 | } |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3359 | /* |
| 3360 | * temporarily null-terminate the path at the end of |
| 3361 | * the current component |
| 3362 | */ |
| 3363 | tmp = *s; |
| 3364 | *s = 0; |
| 3365 | rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, |
| 3366 | full_path); |
| 3367 | *s = tmp; |
| 3368 | } |
| 3369 | return rc; |
| 3370 | } |
| 3371 | |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3372 | /* |
| 3373 | * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is, |
| 3374 | * otherwise 0. |
| 3375 | */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3376 | static int is_path_remote(struct mount_ctx *mnt_ctx) |
Pavel Shilovsky | 724d9f1 | 2011-05-05 09:55:12 +0000 | [diff] [blame] | 3377 | { |
Jeff Layton | 1daaae8 | 2012-03-21 06:30:40 -0400 | [diff] [blame] | 3378 | int rc; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3379 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3380 | struct TCP_Server_Info *server = mnt_ctx->server; |
| 3381 | unsigned int xid = mnt_ctx->xid; |
| 3382 | struct cifs_tcon *tcon = mnt_ctx->tcon; |
| 3383 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3384 | char *full_path; |
| 3385 | |
| 3386 | if (!server->ops->is_path_accessible) |
| 3387 | return -EOPNOTSUPP; |
| 3388 | |
| 3389 | /* |
| 3390 | * cifs_build_path_to_root works only when we have a valid tcon |
| 3391 | */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3392 | full_path = cifs_build_path_to_root(ctx, cifs_sb, tcon, |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3393 | tcon->Flags & SMB_SHARE_IS_IN_DFS); |
| 3394 | if (full_path == NULL) |
| 3395 | return -ENOMEM; |
| 3396 | |
| 3397 | cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path); |
| 3398 | |
| 3399 | rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, |
| 3400 | full_path); |
Eugene Korenevsky | a2809d0 | 2022-01-14 22:53:40 +0300 | [diff] [blame] | 3401 | #ifdef CONFIG_CIFS_DFS_UPCALL |
| 3402 | if (rc == -ENOENT && is_tcon_dfs(tcon)) |
| 3403 | rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon, cifs_sb, |
| 3404 | full_path); |
| 3405 | #endif |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3406 | if (rc != 0 && rc != -EREMOTE) { |
| 3407 | kfree(full_path); |
| 3408 | return rc; |
| 3409 | } |
| 3410 | |
| 3411 | if (rc != -EREMOTE) { |
| 3412 | rc = cifs_are_all_path_components_accessible(server, xid, tcon, |
Ronnie Sahlberg | ce465bf | 2019-07-11 13:46:58 +1000 | [diff] [blame] | 3413 | cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3414 | if (rc != 0) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 3415 | cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n"); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3416 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; |
| 3417 | rc = 0; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | kfree(full_path); |
| 3422 | return rc; |
| 3423 | } |
| 3424 | |
| 3425 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3426 | static void set_root_ses(struct mount_ctx *mnt_ctx) |
Paulo Alcantara (SUSE) | 5bb30a4 | 2019-11-22 12:30:56 -0300 | [diff] [blame] | 3427 | { |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3428 | if (mnt_ctx->ses) { |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3429 | spin_lock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3430 | mnt_ctx->ses->ses_count++; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3431 | spin_unlock(&cifs_tcp_ses_lock); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3432 | dfs_cache_add_refsrv_session(&mnt_ctx->mount_id, mnt_ctx->ses); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3433 | } |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3434 | mnt_ctx->root_ses = mnt_ctx->ses; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3435 | } |
| 3436 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3437 | static int is_dfs_mount(struct mount_ctx *mnt_ctx, bool *isdfs, struct dfs_cache_tgt_list *root_tl) |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3438 | { |
| 3439 | int rc; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3440 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3441 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3442 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3443 | *isdfs = true; |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3444 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3445 | rc = mount_get_conns(mnt_ctx); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3446 | /* |
Paulo Alcantara | d01132a | 2021-02-24 20:59:24 -0300 | [diff] [blame] | 3447 | * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally |
| 3448 | * try to get an DFS referral (even cached) to determine whether it is an DFS mount. |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3449 | * |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3450 | * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem |
| 3451 | * to respond with PATH_NOT_COVERED to requests that include the prefix. |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3452 | */ |
Paulo Alcantara | d01132a | 2021-02-24 20:59:24 -0300 | [diff] [blame] | 3453 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) || |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3454 | dfs_cache_find(mnt_ctx->xid, mnt_ctx->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), |
| 3455 | ctx->UNC + 1, NULL, root_tl)) { |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3456 | if (rc) |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3457 | return rc; |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3458 | /* Check if it is fully accessible and then mount it */ |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3459 | rc = is_path_remote(mnt_ctx); |
Paulo Alcantara | 7efd081 | 2020-07-21 09:36:44 -0300 | [diff] [blame] | 3460 | if (!rc) |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3461 | *isdfs = false; |
| 3462 | else if (rc != -EREMOTE) |
| 3463 | return rc; |
| 3464 | } |
| 3465 | return 0; |
| 3466 | } |
| 3467 | |
| 3468 | static int connect_dfs_target(struct mount_ctx *mnt_ctx, const char *full_path, |
| 3469 | const char *ref_path, struct dfs_cache_tgt_iterator *tit) |
| 3470 | { |
| 3471 | int rc; |
| 3472 | struct dfs_info3_param ref = {}; |
| 3473 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3474 | char *oldmnt = cifs_sb->ctx->mount_options; |
| 3475 | |
| 3476 | rc = dfs_cache_get_tgt_referral(ref_path, tit, &ref); |
| 3477 | if (rc) |
| 3478 | goto out; |
| 3479 | |
| 3480 | rc = expand_dfs_referral(mnt_ctx, full_path, &ref); |
| 3481 | if (rc) |
| 3482 | goto out; |
| 3483 | |
| 3484 | /* Connect to new target only if we were redirected (e.g. mount options changed) */ |
| 3485 | if (oldmnt != cifs_sb->ctx->mount_options) { |
| 3486 | mount_put_conns(mnt_ctx); |
| 3487 | rc = mount_get_dfs_conns(mnt_ctx); |
| 3488 | } |
| 3489 | if (!rc) { |
| 3490 | if (cifs_is_referral_server(mnt_ctx->tcon, &ref)) |
| 3491 | set_root_ses(mnt_ctx); |
| 3492 | rc = dfs_cache_update_tgthint(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls, |
| 3493 | cifs_remap(cifs_sb), ref_path, tit); |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3494 | } |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3495 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3496 | out: |
| 3497 | free_dfs_info_param(&ref); |
| 3498 | return rc; |
| 3499 | } |
Paulo Alcantara | f3c852b | 2021-06-04 19:25:33 -0300 | [diff] [blame] | 3500 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3501 | static int connect_dfs_root(struct mount_ctx *mnt_ctx, struct dfs_cache_tgt_list *root_tl) |
| 3502 | { |
| 3503 | int rc; |
| 3504 | char *full_path; |
| 3505 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3506 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
| 3507 | struct dfs_cache_tgt_iterator *tit; |
| 3508 | |
| 3509 | /* Put initial connections as they might be shared with other mounts. We need unique dfs |
| 3510 | * connections per mount to properly failover, so mount_get_dfs_conns() must be used from |
| 3511 | * now on. |
| 3512 | */ |
| 3513 | mount_put_conns(mnt_ctx); |
| 3514 | mount_get_dfs_conns(mnt_ctx); |
Paulo Alcantara | 65de262 | 2021-12-02 15:29:35 -0300 | [diff] [blame] | 3515 | set_root_ses(mnt_ctx); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3516 | |
| 3517 | full_path = build_unc_path_to_root(ctx, cifs_sb, true); |
| 3518 | if (IS_ERR(full_path)) |
| 3519 | return PTR_ERR(full_path); |
| 3520 | |
| 3521 | mnt_ctx->origin_fullpath = dfs_cache_canonical_path(ctx->UNC, cifs_sb->local_nls, |
| 3522 | cifs_remap(cifs_sb)); |
| 3523 | if (IS_ERR(mnt_ctx->origin_fullpath)) { |
| 3524 | rc = PTR_ERR(mnt_ctx->origin_fullpath); |
| 3525 | mnt_ctx->origin_fullpath = NULL; |
| 3526 | goto out; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3527 | } |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3528 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3529 | /* Try all dfs root targets */ |
| 3530 | for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(root_tl); |
| 3531 | tit; tit = dfs_cache_get_next_tgt(root_tl, tit)) { |
| 3532 | rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->origin_fullpath + 1, tit); |
| 3533 | if (!rc) { |
| 3534 | mnt_ctx->leaf_fullpath = kstrdup(mnt_ctx->origin_fullpath, GFP_KERNEL); |
| 3535 | if (!mnt_ctx->leaf_fullpath) |
| 3536 | rc = -ENOMEM; |
Paulo Alcantara | 4a367dc | 2018-11-14 16:53:52 -0200 | [diff] [blame] | 3537 | break; |
| 3538 | } |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3539 | } |
Paulo Alcantara | 5ff2836 | 2021-02-24 20:59:23 -0300 | [diff] [blame] | 3540 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3541 | out: |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3542 | kfree(full_path); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3543 | return rc; |
| 3544 | } |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3545 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3546 | static int __follow_dfs_link(struct mount_ctx *mnt_ctx) |
| 3547 | { |
| 3548 | int rc; |
| 3549 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3550 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
| 3551 | char *full_path; |
| 3552 | struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl); |
| 3553 | struct dfs_cache_tgt_iterator *tit; |
| 3554 | |
| 3555 | full_path = build_unc_path_to_root(ctx, cifs_sb, true); |
| 3556 | if (IS_ERR(full_path)) |
| 3557 | return PTR_ERR(full_path); |
| 3558 | |
| 3559 | kfree(mnt_ctx->leaf_fullpath); |
| 3560 | mnt_ctx->leaf_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls, |
| 3561 | cifs_remap(cifs_sb)); |
| 3562 | if (IS_ERR(mnt_ctx->leaf_fullpath)) { |
| 3563 | rc = PTR_ERR(mnt_ctx->leaf_fullpath); |
| 3564 | mnt_ctx->leaf_fullpath = NULL; |
| 3565 | goto out; |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3566 | } |
Paulo Alcantara | c870a8e | 2021-06-04 19:25:30 -0300 | [diff] [blame] | 3567 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3568 | /* Get referral from dfs link */ |
| 3569 | rc = dfs_cache_find(mnt_ctx->xid, mnt_ctx->root_ses, cifs_sb->local_nls, |
| 3570 | cifs_remap(cifs_sb), mnt_ctx->leaf_fullpath + 1, NULL, &tl); |
| 3571 | if (rc) |
| 3572 | goto out; |
| 3573 | |
| 3574 | /* Try all dfs link targets */ |
| 3575 | for (rc = -ENOENT, tit = dfs_cache_get_tgt_iterator(&tl); |
| 3576 | tit; tit = dfs_cache_get_next_tgt(&tl, tit)) { |
| 3577 | rc = connect_dfs_target(mnt_ctx, full_path, mnt_ctx->leaf_fullpath + 1, tit); |
| 3578 | if (!rc) { |
| 3579 | rc = is_path_remote(mnt_ctx); |
| 3580 | break; |
| 3581 | } |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3582 | } |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3583 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3584 | out: |
| 3585 | kfree(full_path); |
| 3586 | dfs_cache_free_tgts(&tl); |
| 3587 | return rc; |
| 3588 | } |
| 3589 | |
| 3590 | static int follow_dfs_link(struct mount_ctx *mnt_ctx) |
| 3591 | { |
| 3592 | int rc; |
| 3593 | struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; |
| 3594 | struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; |
| 3595 | char *full_path; |
| 3596 | int num_links = 0; |
| 3597 | |
| 3598 | full_path = build_unc_path_to_root(ctx, cifs_sb, true); |
| 3599 | if (IS_ERR(full_path)) |
| 3600 | return PTR_ERR(full_path); |
| 3601 | |
| 3602 | kfree(mnt_ctx->origin_fullpath); |
| 3603 | mnt_ctx->origin_fullpath = dfs_cache_canonical_path(full_path, cifs_sb->local_nls, |
| 3604 | cifs_remap(cifs_sb)); |
| 3605 | kfree(full_path); |
| 3606 | |
| 3607 | if (IS_ERR(mnt_ctx->origin_fullpath)) { |
| 3608 | rc = PTR_ERR(mnt_ctx->origin_fullpath); |
| 3609 | mnt_ctx->origin_fullpath = NULL; |
| 3610 | return rc; |
| 3611 | } |
| 3612 | |
| 3613 | do { |
| 3614 | rc = __follow_dfs_link(mnt_ctx); |
| 3615 | if (!rc || rc != -EREMOTE) |
| 3616 | break; |
| 3617 | } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS); |
| 3618 | |
| 3619 | return rc; |
| 3620 | } |
| 3621 | |
| 3622 | /* Set up DFS referral paths for failover */ |
| 3623 | static void setup_server_referral_paths(struct mount_ctx *mnt_ctx) |
| 3624 | { |
| 3625 | struct TCP_Server_Info *server = mnt_ctx->server; |
| 3626 | |
| 3627 | server->origin_fullpath = mnt_ctx->origin_fullpath; |
| 3628 | server->leaf_fullpath = mnt_ctx->leaf_fullpath; |
| 3629 | server->current_fullpath = mnt_ctx->leaf_fullpath; |
| 3630 | mnt_ctx->origin_fullpath = mnt_ctx->leaf_fullpath = NULL; |
| 3631 | } |
| 3632 | |
| 3633 | int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
| 3634 | { |
| 3635 | int rc; |
| 3636 | struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; |
| 3637 | struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl); |
| 3638 | bool isdfs; |
| 3639 | |
| 3640 | rc = is_dfs_mount(&mnt_ctx, &isdfs, &tl); |
| 3641 | if (rc) |
| 3642 | goto error; |
| 3643 | if (!isdfs) |
| 3644 | goto out; |
| 3645 | |
| 3646 | uuid_gen(&mnt_ctx.mount_id); |
| 3647 | rc = connect_dfs_root(&mnt_ctx, &tl); |
| 3648 | dfs_cache_free_tgts(&tl); |
| 3649 | |
| 3650 | if (rc) |
| 3651 | goto error; |
| 3652 | |
| 3653 | rc = is_path_remote(&mnt_ctx); |
| 3654 | if (rc == -EREMOTE) |
| 3655 | rc = follow_dfs_link(&mnt_ctx); |
| 3656 | if (rc) |
| 3657 | goto error; |
| 3658 | |
| 3659 | setup_server_referral_paths(&mnt_ctx); |
Aurelien Aptel | 5fc7fcd | 2018-11-16 16:13:25 +0100 | [diff] [blame] | 3660 | /* |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3661 | * After reconnecting to a different server, unique ids won't match anymore, so we disable |
| 3662 | * serverino. This prevents dentry revalidation to think the dentry are stale (ESTALE). |
Aurelien Aptel | 5fc7fcd | 2018-11-16 16:13:25 +0100 | [diff] [blame] | 3663 | */ |
| 3664 | cifs_autodisable_serverino(cifs_sb); |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 3665 | /* |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3666 | * Force the use of prefix path to support failover on DFS paths that resolve to targets |
| 3667 | * that have different prefix paths. |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 3668 | */ |
| 3669 | cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH; |
| 3670 | kfree(cifs_sb->prepath); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3671 | cifs_sb->prepath = ctx->prepath; |
| 3672 | ctx->prepath = NULL; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3673 | uuid_copy(&cifs_sb->dfs_mount_id, &mnt_ctx.mount_id); |
Paulo Alcantara (SUSE) | bacd704 | 2020-02-20 19:49:34 -0300 | [diff] [blame] | 3674 | |
Jeff Layton | 70fe7dc | 2007-11-16 22:21:07 +0000 | [diff] [blame] | 3675 | out: |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3676 | free_xid(mnt_ctx.xid); |
| 3677 | cifs_try_adding_channels(cifs_sb, mnt_ctx.ses); |
| 3678 | return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3679 | |
| 3680 | error: |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3681 | dfs_cache_put_refsrv_sessions(&mnt_ctx.mount_id); |
| 3682 | kfree(mnt_ctx.origin_fullpath); |
| 3683 | kfree(mnt_ctx.leaf_fullpath); |
| 3684 | mount_put_conns(&mnt_ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3685 | return rc; |
| 3686 | } |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3687 | #else |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3688 | int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb3_fs_context *ctx) |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3689 | { |
| 3690 | int rc = 0; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3691 | struct mount_ctx mnt_ctx = { .cifs_sb = cifs_sb, .fs_ctx = ctx, }; |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3692 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3693 | rc = mount_get_conns(&mnt_ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3694 | if (rc) |
| 3695 | goto error; |
| 3696 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3697 | if (mnt_ctx.tcon) { |
| 3698 | rc = is_path_remote(&mnt_ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3699 | if (rc == -EREMOTE) |
| 3700 | rc = -EOPNOTSUPP; |
| 3701 | if (rc) |
| 3702 | goto error; |
| 3703 | } |
| 3704 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3705 | free_xid(mnt_ctx.xid); |
| 3706 | return mount_setup_tlink(cifs_sb, mnt_ctx.ses, mnt_ctx.tcon); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3707 | |
| 3708 | error: |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 3709 | mount_put_conns(&mnt_ctx); |
Paulo Alcantara | 56c762e | 2018-11-14 13:03:14 -0200 | [diff] [blame] | 3710 | return rc; |
| 3711 | } |
| 3712 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | |
Jeff Layton | 8d1bca3 | 2011-06-11 21:17:10 -0400 | [diff] [blame] | 3714 | /* |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3715 | * Issue a TREE_CONNECT request. |
Jeff Layton | 8d1bca3 | 2011-06-11 21:17:10 -0400 | [diff] [blame] | 3716 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | int |
Pavel Shilovsky | 2e6e02a | 2012-05-25 11:11:39 +0400 | [diff] [blame] | 3718 | CIFSTCon(const unsigned int xid, struct cifs_ses *ses, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3719 | const char *tree, struct cifs_tcon *tcon, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | const struct nls_table *nls_codepage) |
| 3721 | { |
| 3722 | struct smb_hdr *smb_buffer; |
| 3723 | struct smb_hdr *smb_buffer_response; |
| 3724 | TCONX_REQ *pSMB; |
| 3725 | TCONX_RSP *pSMBr; |
| 3726 | unsigned char *bcc_ptr; |
| 3727 | int rc = 0; |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 3728 | int length; |
| 3729 | __u16 bytes_left, count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3730 | |
| 3731 | if (ses == NULL) |
| 3732 | return -EIO; |
| 3733 | |
| 3734 | smb_buffer = cifs_buf_get(); |
Steve French | ca43e3b | 2009-09-01 17:20:50 +0000 | [diff] [blame] | 3735 | if (smb_buffer == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | return -ENOMEM; |
Steve French | ca43e3b | 2009-09-01 17:20:50 +0000 | [diff] [blame] | 3737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | smb_buffer_response = smb_buffer; |
| 3739 | |
| 3740 | header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX, |
| 3741 | NULL /*no tid */ , 4 /*wct */ ); |
Steve French | 1982c34 | 2005-08-17 12:38:22 -0700 | [diff] [blame] | 3742 | |
Pavel Shilovsky | 8825736 | 2012-05-23 14:01:59 +0400 | [diff] [blame] | 3743 | smb_buffer->Mid = get_next_mid(ses->server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | smb_buffer->Uid = ses->Suid; |
| 3745 | pSMB = (TCONX_REQ *) smb_buffer; |
| 3746 | pSMBr = (TCONX_RSP *) smb_buffer_response; |
| 3747 | |
| 3748 | pSMB->AndXCommand = 0xFF; |
| 3749 | pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | bcc_ptr = &pSMB->Password[0]; |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3751 | if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) { |
Steve French | eeac804 | 2006-01-13 21:34:58 -0800 | [diff] [blame] | 3752 | pSMB->PasswordLength = cpu_to_le16(1); /* minimum */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 3753 | *bcc_ptr = 0; /* password is null byte */ |
Steve French | eeac804 | 2006-01-13 21:34:58 -0800 | [diff] [blame] | 3754 | bcc_ptr++; /* skip password */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 3755 | /* already aligned so no need to do it below */ |
Steve French | eeac804 | 2006-01-13 21:34:58 -0800 | [diff] [blame] | 3756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | |
Jeff Layton | 38d77c5 | 2013-05-26 07:01:00 -0400 | [diff] [blame] | 3758 | if (ses->server->sign) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
| 3760 | |
| 3761 | if (ses->capabilities & CAP_STATUS32) { |
| 3762 | smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS; |
| 3763 | } |
| 3764 | if (ses->capabilities & CAP_DFS) { |
| 3765 | smb_buffer->Flags2 |= SMBFLG2_DFS; |
| 3766 | } |
| 3767 | if (ses->capabilities & CAP_UNICODE) { |
| 3768 | smb_buffer->Flags2 |= SMBFLG2_UNICODE; |
| 3769 | length = |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3770 | cifs_strtoUTF16((__le16 *) bcc_ptr, tree, |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3771 | 6 /* max utf8 char length in bytes */ * |
Steve French | a878fb2 | 2006-05-30 18:04:19 +0000 | [diff] [blame] | 3772 | (/* server len*/ + 256 /* share len */), nls_codepage); |
| 3773 | bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3774 | bcc_ptr += 2; /* skip trailing null */ |
| 3775 | } else { /* ASCII */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | strcpy(bcc_ptr, tree); |
| 3777 | bcc_ptr += strlen(tree) + 1; |
| 3778 | } |
| 3779 | strcpy(bcc_ptr, "?????"); |
| 3780 | bcc_ptr += strlen("?????"); |
| 3781 | bcc_ptr += 1; |
| 3782 | count = bcc_ptr - &pSMB->Password[0]; |
Qinglang Miao | 1a0e7f7 | 2020-07-25 16:56:01 +0800 | [diff] [blame] | 3783 | be32_add_cpu(&pSMB->hdr.smb_buf_length, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | pSMB->ByteCount = cpu_to_le16(count); |
| 3785 | |
Steve French | 133672e | 2007-11-13 22:41:37 +0000 | [diff] [blame] | 3786 | rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length, |
Jeff Layton | 7749981 | 2011-01-11 07:24:23 -0500 | [diff] [blame] | 3787 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | /* above now done in SendReceive */ |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3790 | if (rc == 0) { |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3791 | bool is_unicode; |
| 3792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | tcon->tid = smb_buffer_response->Tid; |
| 3794 | bcc_ptr = pByteArea(smb_buffer_response); |
Jeff Layton | 690c522 | 2011-01-20 13:36:51 -0500 | [diff] [blame] | 3795 | bytes_left = get_bcc(smb_buffer_response); |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3796 | length = strnlen(bcc_ptr, bytes_left - 2); |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3797 | if (smb_buffer->Flags2 & SMBFLG2_UNICODE) |
| 3798 | is_unicode = true; |
| 3799 | else |
| 3800 | is_unicode = false; |
| 3801 | |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3802 | |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3803 | /* skip service field (NB: this field is always ASCII) */ |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 3804 | if (length == 3) { |
| 3805 | if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') && |
| 3806 | (bcc_ptr[2] == 'C')) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3807 | cifs_dbg(FYI, "IPC connection\n"); |
Aurelien Aptel | b327a71 | 2018-01-24 13:46:10 +0100 | [diff] [blame] | 3808 | tcon->ipc = true; |
| 3809 | tcon->pipe = true; |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 3810 | } |
| 3811 | } else if (length == 2) { |
| 3812 | if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) { |
| 3813 | /* the most common case */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3814 | cifs_dbg(FYI, "disk share connection\n"); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 3815 | } |
| 3816 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3817 | bcc_ptr += length + 1; |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3818 | bytes_left -= (length + 1); |
Zhao Hongjiang | 46b51d0 | 2013-06-24 01:57:47 -0500 | [diff] [blame] | 3819 | strlcpy(tcon->treeName, tree, sizeof(tcon->treeName)); |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3820 | |
| 3821 | /* mostly informational -- no need to fail on error here */ |
Jeff Layton | 90a98b2 | 2009-07-20 13:40:52 -0400 | [diff] [blame] | 3822 | kfree(tcon->nativeFileSystem); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 3823 | tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr, |
Steve French | 0e0d2cf | 2009-05-01 05:27:32 +0000 | [diff] [blame] | 3824 | bytes_left, is_unicode, |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3825 | nls_codepage); |
| 3826 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3827 | cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem); |
Jeff Layton | cc20c03 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 3828 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 3829 | if ((smb_buffer_response->WordCount == 3) || |
Steve French | 1a4e15a | 2006-10-12 21:33:51 +0000 | [diff] [blame] | 3830 | (smb_buffer_response->WordCount == 7)) |
| 3831 | /* field is in same location */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 3832 | tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport); |
| 3833 | else |
| 3834 | tcon->Flags = 0; |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3835 | cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | } |
| 3837 | |
Mariusz Kozlowski | a8a11d3 | 2007-10-03 16:41:24 +0000 | [diff] [blame] | 3838 | cifs_buf_release(smb_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | return rc; |
| 3840 | } |
| 3841 | |
Al Viro | 2e32cf5 | 2013-10-03 12:53:37 -0400 | [diff] [blame] | 3842 | static void delayed_free(struct rcu_head *p) |
| 3843 | { |
Ronnie Sahlberg | d17abdf7 | 2020-11-10 08:59:26 +1000 | [diff] [blame] | 3844 | struct cifs_sb_info *cifs_sb = container_of(p, struct cifs_sb_info, rcu); |
| 3845 | |
| 3846 | unload_nls(cifs_sb->local_nls); |
Ronnie Sahlberg | c741cba | 2020-12-14 16:40:16 +1000 | [diff] [blame] | 3847 | smb3_cleanup_fs_context(cifs_sb->ctx); |
Ronnie Sahlberg | d17abdf7 | 2020-11-10 08:59:26 +1000 | [diff] [blame] | 3848 | kfree(cifs_sb); |
Al Viro | 2e32cf5 | 2013-10-03 12:53:37 -0400 | [diff] [blame] | 3849 | } |
| 3850 | |
Al Viro | 2a9b995 | 2011-06-17 09:27:16 -0400 | [diff] [blame] | 3851 | void |
| 3852 | cifs_umount(struct cifs_sb_info *cifs_sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | { |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3854 | struct rb_root *root = &cifs_sb->tlink_tree; |
| 3855 | struct rb_node *node; |
| 3856 | struct tcon_link *tlink; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 3858 | cancel_delayed_work_sync(&cifs_sb->prune_tlinks); |
| 3859 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3860 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 3861 | while ((node = rb_first(root))) { |
| 3862 | tlink = rb_entry(node, struct tcon_link, tl_rbnode); |
| 3863 | cifs_get_tlink(tlink); |
| 3864 | clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 3865 | rb_erase(node, root); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3866 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 3867 | spin_unlock(&cifs_sb->tlink_tree_lock); |
| 3868 | cifs_put_tlink(tlink); |
| 3869 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 3870 | } |
| 3871 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3872 | |
Aurelien Aptel | a6b5058 | 2016-05-25 19:59:09 +0200 | [diff] [blame] | 3873 | kfree(cifs_sb->prepath); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3874 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c9f7110 | 2021-06-04 19:25:29 -0300 | [diff] [blame] | 3875 | dfs_cache_put_refsrv_sessions(&cifs_sb->dfs_mount_id); |
Paulo Alcantara | 93d5cb5 | 2018-11-14 17:13:25 -0200 | [diff] [blame] | 3876 | #endif |
Al Viro | 2e32cf5 | 2013-10-03 12:53:37 -0400 | [diff] [blame] | 3877 | call_rcu(&cifs_sb->rcu, delayed_free); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 3878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 3880 | int |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 3881 | cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses, |
| 3882 | struct TCP_Server_Info *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | { |
| 3884 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | |
Pavel Shilovsky | 286170a | 2012-05-25 10:43:58 +0400 | [diff] [blame] | 3886 | if (!server->ops->need_neg || !server->ops->negotiate) |
| 3887 | return -ENOSYS; |
| 3888 | |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3889 | /* only send once per connect */ |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 3890 | spin_lock(&cifs_tcp_ses_lock); |
| 3891 | if (!server->ops->need_neg(server) || |
| 3892 | server->tcpStatus != CifsNeedNegotiate) { |
| 3893 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3894 | return 0; |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 3895 | } |
| 3896 | server->tcpStatus = CifsInNegotiate; |
| 3897 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 3899 | rc = server->ops->negotiate(xid, ses, server); |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3900 | if (rc == 0) { |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 3901 | spin_lock(&cifs_tcp_ses_lock); |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 3902 | if (server->tcpStatus == CifsInNegotiate) |
| 3903 | server->tcpStatus = CifsNeedSessSetup; |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3904 | else |
| 3905 | rc = -EHOSTDOWN; |
Shyam Prasad N | 080dc5e | 2021-07-19 17:05:53 +0000 | [diff] [blame] | 3906 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 3907 | } else { |
| 3908 | spin_lock(&cifs_tcp_ses_lock); |
| 3909 | if (server->tcpStatus == CifsInNegotiate) |
| 3910 | server->tcpStatus = CifsNeedNegotiate; |
| 3911 | spin_unlock(&cifs_tcp_ses_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | } |
Steve French | 26b994f | 2008-08-06 05:11:33 +0000 | [diff] [blame] | 3913 | |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3914 | return rc; |
| 3915 | } |
Steve French | 26b994f | 2008-08-06 05:11:33 +0000 | [diff] [blame] | 3916 | |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3917 | int |
| 3918 | cifs_setup_session(const unsigned int xid, struct cifs_ses *ses, |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 3919 | struct TCP_Server_Info *server, |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3920 | struct nls_table *nls_info) |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3921 | { |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3922 | int rc = -ENOSYS; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 3923 | bool is_binding = false; |
Jeff Layton | 198b568 | 2010-04-24 07:57:48 -0400 | [diff] [blame] | 3924 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 3925 | /* only send once per connect */ |
| 3926 | spin_lock(&cifs_tcp_ses_lock); |
| 3927 | if (server->tcpStatus != CifsNeedSessSetup) { |
| 3928 | spin_unlock(&cifs_tcp_ses_lock); |
| 3929 | return 0; |
| 3930 | } |
Shyam Prasad N | 47de760 | 2022-01-18 09:24:08 +0000 | [diff] [blame] | 3931 | server->tcpStatus = CifsInSessSetup; |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 3932 | spin_unlock(&cifs_tcp_ses_lock); |
| 3933 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 3934 | spin_lock(&ses->chan_lock); |
| 3935 | is_binding = !CIFS_ALL_CHANS_NEED_RECONNECT(ses); |
| 3936 | spin_unlock(&ses->chan_lock); |
| 3937 | |
| 3938 | if (!is_binding) { |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 3939 | ses->capabilities = server->capabilities; |
YANG LI | ed6b192 | 2021-01-11 17:15:28 +0800 | [diff] [blame] | 3940 | if (!linuxExtEnabled) |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 3941 | ses->capabilities &= (~server->vals->cap_unix); |
| 3942 | |
| 3943 | if (ses->auth_key.response) { |
| 3944 | cifs_dbg(FYI, "Free previous auth_key.response = %p\n", |
| 3945 | ses->auth_key.response); |
| 3946 | kfree(ses->auth_key.response); |
| 3947 | ses->auth_key.response = NULL; |
| 3948 | ses->auth_key.len = 0; |
| 3949 | } |
| 3950 | } |
Steve French | 20418ac | 2009-04-30 16:13:32 +0000 | [diff] [blame] | 3951 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 3952 | cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n", |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3953 | server->sec_mode, server->capabilities, server->timeAdj); |
Jeff Layton | cb7691b | 2008-08-18 15:41:05 -0400 | [diff] [blame] | 3954 | |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3955 | if (server->ops->sess_setup) |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 3956 | rc = server->ops->sess_setup(xid, ses, server, nls_info); |
Pavel Shilovsky | 58c45c5 | 2012-05-25 10:54:49 +0400 | [diff] [blame] | 3957 | |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 3958 | if (rc) { |
Ronnie Sahlberg | afe6f65 | 2019-08-28 17:15:35 +1000 | [diff] [blame] | 3959 | cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc); |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 3960 | spin_lock(&cifs_tcp_ses_lock); |
| 3961 | if (server->tcpStatus == CifsInSessSetup) |
| 3962 | server->tcpStatus = CifsNeedSessSetup; |
| 3963 | spin_unlock(&cifs_tcp_ses_lock); |
| 3964 | } else { |
| 3965 | spin_lock(&cifs_tcp_ses_lock); |
| 3966 | if (server->tcpStatus == CifsInSessSetup) |
| 3967 | server->tcpStatus = CifsGood; |
Shyam Prasad N | c1604da | 2022-01-16 13:38:14 +0000 | [diff] [blame] | 3968 | /* Even if one channel is active, session is in good state */ |
Shyam Prasad N | 47de760 | 2022-01-18 09:24:08 +0000 | [diff] [blame] | 3969 | ses->status = CifsGood; |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 3970 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | c1604da | 2022-01-16 13:38:14 +0000 | [diff] [blame] | 3971 | |
| 3972 | spin_lock(&ses->chan_lock); |
| 3973 | cifs_chan_clear_need_reconnect(ses, server); |
| 3974 | spin_unlock(&ses->chan_lock); |
Shyam Prasad N | a05885c | 2021-11-17 15:57:22 +0000 | [diff] [blame] | 3975 | } |
Shirish Pargaonkar | 21e7339 | 2010-10-21 06:42:55 -0500 | [diff] [blame] | 3976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | return rc; |
| 3978 | } |
| 3979 | |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3980 | static int |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3981 | cifs_set_vol_auth(struct smb3_fs_context *ctx, struct cifs_ses *ses) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3982 | { |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3983 | ctx->sectype = ses->sectype; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 3984 | |
| 3985 | /* krb5 is special, since we don't need username or pw */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3986 | if (ctx->sectype == Kerberos) |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3987 | return 0; |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3988 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3989 | return cifs_set_cifscreds(ctx, ses); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3990 | } |
| 3991 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3992 | static struct cifs_tcon * |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 3993 | cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid) |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 3994 | { |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 3995 | int rc; |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 3996 | struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb); |
| 3997 | struct cifs_ses *ses; |
| 3998 | struct cifs_tcon *tcon = NULL; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 3999 | struct smb3_fs_context *ctx; |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4000 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4001 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 4002 | if (ctx == NULL) |
Dan Carpenter | 803ab97 | 2012-01-24 11:39:22 +0300 | [diff] [blame] | 4003 | return ERR_PTR(-ENOMEM); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4004 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4005 | ctx->local_nls = cifs_sb->local_nls; |
| 4006 | ctx->linux_uid = fsuid; |
| 4007 | ctx->cred_uid = fsuid; |
| 4008 | ctx->UNC = master_tcon->treeName; |
| 4009 | ctx->retry = master_tcon->retry; |
| 4010 | ctx->nocase = master_tcon->nocase; |
| 4011 | ctx->nohandlecache = master_tcon->nohandlecache; |
| 4012 | ctx->local_lease = master_tcon->local_lease; |
| 4013 | ctx->no_lease = master_tcon->no_lease; |
| 4014 | ctx->resilient = master_tcon->use_resilient; |
| 4015 | ctx->persistent = master_tcon->use_persistent; |
| 4016 | ctx->handle_timeout = master_tcon->handle_timeout; |
| 4017 | ctx->no_linux_ext = !master_tcon->unix_ext; |
| 4018 | ctx->linux_ext = master_tcon->posix_extensions; |
| 4019 | ctx->sectype = master_tcon->ses->sectype; |
| 4020 | ctx->sign = master_tcon->ses->sign; |
| 4021 | ctx->seal = master_tcon->seal; |
Samuel Cabrero | 0ac4e29 | 2020-12-11 22:59:29 -0600 | [diff] [blame] | 4022 | ctx->witness = master_tcon->use_witness; |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4023 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4024 | rc = cifs_set_vol_auth(ctx, master_tcon->ses); |
Jeff Layton | 8a8798a | 2012-01-17 16:09:15 -0500 | [diff] [blame] | 4025 | if (rc) { |
| 4026 | tcon = ERR_PTR(rc); |
| 4027 | goto out; |
| 4028 | } |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4029 | |
| 4030 | /* get a reference for the same TCP session */ |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 4031 | spin_lock(&cifs_tcp_ses_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4032 | ++master_tcon->ses->server->srv_count; |
Suresh Jayaraman | 3f9bcca | 2010-10-18 23:29:37 +0530 | [diff] [blame] | 4033 | spin_unlock(&cifs_tcp_ses_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4034 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4035 | ses = cifs_get_smb_ses(master_tcon->ses->server, ctx); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4036 | if (IS_ERR(ses)) { |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 4037 | tcon = (struct cifs_tcon *)ses; |
Pavel Shilovsky | 53e0e11 | 2016-11-04 11:50:31 -0700 | [diff] [blame] | 4038 | cifs_put_tcp_session(master_tcon->ses->server, 0); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4039 | goto out; |
| 4040 | } |
| 4041 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4042 | tcon = cifs_get_tcon(ses, ctx); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4043 | if (IS_ERR(tcon)) { |
| 4044 | cifs_put_smb_ses(ses); |
| 4045 | goto out; |
| 4046 | } |
| 4047 | |
Pavel Shilovsky | 29e20f9 | 2012-07-13 13:58:14 +0400 | [diff] [blame] | 4048 | if (cap_unix(ses)) |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4049 | reset_cifs_unix_caps(0, tcon, NULL, ctx); |
Steve French | b326614 | 2018-05-20 23:41:10 -0500 | [diff] [blame] | 4050 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4051 | out: |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 4052 | kfree(ctx->username); |
| 4053 | kfree_sensitive(ctx->password); |
| 4054 | kfree(ctx); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4055 | |
| 4056 | return tcon; |
| 4057 | } |
| 4058 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 4059 | struct cifs_tcon * |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4060 | cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb) |
| 4061 | { |
| 4062 | return tlink_tcon(cifs_sb_master_tlink(cifs_sb)); |
| 4063 | } |
| 4064 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4065 | /* find and return a tlink with given uid */ |
| 4066 | static struct tcon_link * |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4067 | tlink_rb_search(struct rb_root *root, kuid_t uid) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4068 | { |
| 4069 | struct rb_node *node = root->rb_node; |
| 4070 | struct tcon_link *tlink; |
| 4071 | |
| 4072 | while (node) { |
| 4073 | tlink = rb_entry(node, struct tcon_link, tl_rbnode); |
| 4074 | |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4075 | if (uid_gt(tlink->tl_uid, uid)) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4076 | node = node->rb_left; |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4077 | else if (uid_lt(tlink->tl_uid, uid)) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4078 | node = node->rb_right; |
| 4079 | else |
| 4080 | return tlink; |
| 4081 | } |
| 4082 | return NULL; |
| 4083 | } |
| 4084 | |
| 4085 | /* insert a tcon_link into the tree */ |
| 4086 | static void |
| 4087 | tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink) |
| 4088 | { |
| 4089 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 4090 | struct tcon_link *tlink; |
| 4091 | |
| 4092 | while (*new) { |
| 4093 | tlink = rb_entry(*new, struct tcon_link, tl_rbnode); |
| 4094 | parent = *new; |
| 4095 | |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4096 | if (uid_gt(tlink->tl_uid, new_tlink->tl_uid)) |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4097 | new = &((*new)->rb_left); |
| 4098 | else |
| 4099 | new = &((*new)->rb_right); |
| 4100 | } |
| 4101 | |
| 4102 | rb_link_node(&new_tlink->tl_rbnode, parent, new); |
| 4103 | rb_insert_color(&new_tlink->tl_rbnode, root); |
| 4104 | } |
| 4105 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4106 | /* |
| 4107 | * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the |
| 4108 | * current task. |
| 4109 | * |
| 4110 | * If the superblock doesn't refer to a multiuser mount, then just return |
| 4111 | * the master tcon for the mount. |
| 4112 | * |
Suresh Jayaraman | 6ef933a | 2010-11-03 10:53:49 +0530 | [diff] [blame] | 4113 | * First, search the rbtree for an existing tcon for this fsuid. If one |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4114 | * exists, then check to see if it's pending construction. If it is then wait |
| 4115 | * for construction to complete. Once it's no longer pending, check to see if |
| 4116 | * it failed and either return an error or retry construction, depending on |
| 4117 | * the timeout. |
| 4118 | * |
| 4119 | * If one doesn't exist then insert a new tcon_link struct into the tree and |
| 4120 | * try to construct a new one. |
| 4121 | */ |
| 4122 | struct tcon_link * |
| 4123 | cifs_sb_tlink(struct cifs_sb_info *cifs_sb) |
| 4124 | { |
| 4125 | int ret; |
Eric W. Biederman | 6d4a083 | 2013-02-06 01:48:56 -0800 | [diff] [blame] | 4126 | kuid_t fsuid = current_fsuid(); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4127 | struct tcon_link *tlink, *newtlink; |
| 4128 | |
| 4129 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) |
| 4130 | return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb)); |
| 4131 | |
| 4132 | spin_lock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4133 | tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4134 | if (tlink) |
| 4135 | cifs_get_tlink(tlink); |
| 4136 | spin_unlock(&cifs_sb->tlink_tree_lock); |
| 4137 | |
| 4138 | if (tlink == NULL) { |
| 4139 | newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL); |
| 4140 | if (newtlink == NULL) |
| 4141 | return ERR_PTR(-ENOMEM); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4142 | newtlink->tl_uid = fsuid; |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4143 | newtlink->tl_tcon = ERR_PTR(-EACCES); |
| 4144 | set_bit(TCON_LINK_PENDING, &newtlink->tl_flags); |
| 4145 | set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags); |
| 4146 | cifs_get_tlink(newtlink); |
| 4147 | |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4148 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 4149 | /* was one inserted after previous search? */ |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4150 | tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4151 | if (tlink) { |
| 4152 | cifs_get_tlink(tlink); |
| 4153 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4154 | kfree(newtlink); |
| 4155 | goto wait_for_construction; |
| 4156 | } |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4157 | tlink = newtlink; |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4158 | tlink_rb_insert(&cifs_sb->tlink_tree, tlink); |
| 4159 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4160 | } else { |
| 4161 | wait_for_construction: |
| 4162 | ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING, |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4163 | TASK_INTERRUPTIBLE); |
| 4164 | if (ret) { |
| 4165 | cifs_put_tlink(tlink); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 4166 | return ERR_PTR(-ERESTARTSYS); |
Jeff Layton | 9d002df | 2010-10-06 19:51:11 -0400 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | /* if it's good, return it */ |
| 4170 | if (!IS_ERR(tlink->tl_tcon)) |
| 4171 | return tlink; |
| 4172 | |
| 4173 | /* return error if we tried this already recently */ |
| 4174 | if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) { |
| 4175 | cifs_put_tlink(tlink); |
| 4176 | return ERR_PTR(-EACCES); |
| 4177 | } |
| 4178 | |
| 4179 | if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags)) |
| 4180 | goto wait_for_construction; |
| 4181 | } |
| 4182 | |
| 4183 | tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid); |
| 4184 | clear_bit(TCON_LINK_PENDING, &tlink->tl_flags); |
| 4185 | wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING); |
| 4186 | |
| 4187 | if (IS_ERR(tlink->tl_tcon)) { |
| 4188 | cifs_put_tlink(tlink); |
| 4189 | return ERR_PTR(-EACCES); |
| 4190 | } |
| 4191 | |
| 4192 | return tlink; |
| 4193 | } |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4194 | |
| 4195 | /* |
| 4196 | * periodic workqueue job that scans tcon_tree for a superblock and closes |
| 4197 | * out tcons. |
| 4198 | */ |
| 4199 | static void |
| 4200 | cifs_prune_tlinks(struct work_struct *work) |
| 4201 | { |
| 4202 | struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info, |
| 4203 | prune_tlinks.work); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4204 | struct rb_root *root = &cifs_sb->tlink_tree; |
Colin Ian King | 37e12f5 | 2018-01-17 09:52:39 +0000 | [diff] [blame] | 4205 | struct rb_node *node; |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4206 | struct rb_node *tmp; |
| 4207 | struct tcon_link *tlink; |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4208 | |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4209 | /* |
| 4210 | * Because we drop the spinlock in the loop in order to put the tlink |
| 4211 | * it's not guarded against removal of links from the tree. The only |
| 4212 | * places that remove entries from the tree are this function and |
| 4213 | * umounts. Because this function is non-reentrant and is canceled |
| 4214 | * before umount can proceed, this is safe. |
| 4215 | */ |
| 4216 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 4217 | node = rb_first(root); |
| 4218 | while (node != NULL) { |
| 4219 | tmp = node; |
| 4220 | node = rb_next(tmp); |
| 4221 | tlink = rb_entry(tmp, struct tcon_link, tl_rbnode); |
| 4222 | |
| 4223 | if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) || |
| 4224 | atomic_read(&tlink->tl_count) != 0 || |
| 4225 | time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies)) |
| 4226 | continue; |
| 4227 | |
| 4228 | cifs_get_tlink(tlink); |
| 4229 | clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags); |
| 4230 | rb_erase(tmp, root); |
| 4231 | |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4232 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | b647c35 | 2010-10-28 11:16:44 -0400 | [diff] [blame] | 4233 | cifs_put_tlink(tlink); |
| 4234 | spin_lock(&cifs_sb->tlink_tree_lock); |
| 4235 | } |
| 4236 | spin_unlock(&cifs_sb->tlink_tree_lock); |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4237 | |
Jeff Layton | da472fc | 2012-03-23 14:40:53 -0400 | [diff] [blame] | 4238 | queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks, |
Jeff Layton | 2de970f | 2010-10-06 19:51:12 -0400 | [diff] [blame] | 4239 | TLINK_IDLE_EXPIRE); |
| 4240 | } |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4241 | |
| 4242 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4243 | /* Update dfs referral path of superblock */ |
| 4244 | static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb, |
| 4245 | const char *target) |
| 4246 | { |
| 4247 | int rc = 0; |
| 4248 | size_t len = strlen(target); |
| 4249 | char *refpath, *npath; |
| 4250 | |
| 4251 | if (unlikely(len < 2 || *target != '\\')) |
| 4252 | return -EINVAL; |
| 4253 | |
| 4254 | if (target[1] == '\\') { |
| 4255 | len += 1; |
| 4256 | refpath = kmalloc(len, GFP_KERNEL); |
| 4257 | if (!refpath) |
| 4258 | return -ENOMEM; |
| 4259 | |
| 4260 | scnprintf(refpath, len, "%s", target); |
| 4261 | } else { |
| 4262 | len += sizeof("\\"); |
| 4263 | refpath = kmalloc(len, GFP_KERNEL); |
| 4264 | if (!refpath) |
| 4265 | return -ENOMEM; |
| 4266 | |
| 4267 | scnprintf(refpath, len, "\\%s", target); |
| 4268 | } |
| 4269 | |
| 4270 | npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb)); |
| 4271 | kfree(refpath); |
| 4272 | |
| 4273 | if (IS_ERR(npath)) { |
| 4274 | rc = PTR_ERR(npath); |
| 4275 | } else { |
| 4276 | mutex_lock(&server->refpath_lock); |
| 4277 | kfree(server->leaf_fullpath); |
| 4278 | server->leaf_fullpath = npath; |
| 4279 | mutex_unlock(&server->refpath_lock); |
| 4280 | server->current_fullpath = server->leaf_fullpath; |
| 4281 | } |
| 4282 | return rc; |
| 4283 | } |
| 4284 | |
| 4285 | static int target_share_matches_server(struct TCP_Server_Info *server, const char *tcp_host, |
| 4286 | size_t tcp_host_len, char *share, bool *target_match) |
| 4287 | { |
| 4288 | int rc = 0; |
| 4289 | const char *dfs_host; |
| 4290 | size_t dfs_host_len; |
| 4291 | |
| 4292 | *target_match = true; |
| 4293 | extract_unc_hostname(share, &dfs_host, &dfs_host_len); |
| 4294 | |
| 4295 | /* Check if hostnames or addresses match */ |
| 4296 | if (dfs_host_len != tcp_host_len || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) { |
| 4297 | cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len, |
| 4298 | dfs_host, (int)tcp_host_len, tcp_host); |
| 4299 | rc = match_target_ip(server, dfs_host, dfs_host_len, target_match); |
| 4300 | if (rc) |
| 4301 | cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc); |
| 4302 | } |
| 4303 | return rc; |
| 4304 | } |
| 4305 | |
Steve French | 0e62904 | 2021-11-10 03:09:52 -0600 | [diff] [blame] | 4306 | static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4307 | struct cifs_sb_info *cifs_sb, char *tree, bool islink, |
| 4308 | struct dfs_cache_tgt_list *tl) |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4309 | { |
| 4310 | int rc; |
| 4311 | struct TCP_Server_Info *server = tcon->ses->server; |
| 4312 | const struct smb_version_operations *ops = server->ops; |
| 4313 | struct cifs_tcon *ipc = tcon->ses->tcon_ipc; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4314 | char *share = NULL, *prefix = NULL; |
| 4315 | const char *tcp_host; |
| 4316 | size_t tcp_host_len; |
| 4317 | struct dfs_cache_tgt_iterator *tit; |
| 4318 | bool target_match; |
| 4319 | |
| 4320 | extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len); |
| 4321 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4322 | tit = dfs_cache_get_tgt_iterator(tl); |
| 4323 | if (!tit) { |
| 4324 | rc = -ENOENT; |
| 4325 | goto out; |
| 4326 | } |
| 4327 | |
| 4328 | /* Try to tree connect to all dfs targets */ |
| 4329 | for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) { |
| 4330 | const char *target = dfs_cache_get_tgt_name(tit); |
| 4331 | struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl); |
| 4332 | |
| 4333 | kfree(share); |
| 4334 | kfree(prefix); |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4335 | share = prefix = NULL; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4336 | |
| 4337 | /* Check if share matches with tcp ses */ |
| 4338 | rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix); |
| 4339 | if (rc) { |
| 4340 | cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc); |
| 4341 | break; |
| 4342 | } |
| 4343 | |
| 4344 | rc = target_share_matches_server(server, tcp_host, tcp_host_len, share, |
| 4345 | &target_match); |
| 4346 | if (rc) |
| 4347 | break; |
| 4348 | if (!target_match) { |
| 4349 | rc = -EHOSTUNREACH; |
| 4350 | continue; |
| 4351 | } |
| 4352 | |
| 4353 | if (ipc->need_reconnect) { |
| 4354 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); |
| 4355 | rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls); |
| 4356 | if (rc) |
| 4357 | break; |
| 4358 | } |
| 4359 | |
| 4360 | scnprintf(tree, MAX_TREE_SIZE, "\\%s", share); |
| 4361 | if (!islink) { |
| 4362 | rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls); |
| 4363 | break; |
| 4364 | } |
| 4365 | /* |
| 4366 | * If no dfs referrals were returned from link target, then just do a TREE_CONNECT |
| 4367 | * to it. Otherwise, cache the dfs referral and then mark current tcp ses for |
| 4368 | * reconnect so either the demultiplex thread or the echo worker will reconnect to |
| 4369 | * newly resolved target. |
| 4370 | */ |
| 4371 | if (dfs_cache_find(xid, tcon->ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target, |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4372 | NULL, &ntl)) { |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4373 | rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls); |
| 4374 | if (rc) |
| 4375 | continue; |
| 4376 | rc = dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit); |
| 4377 | if (!rc) |
| 4378 | rc = cifs_update_super_prepath(cifs_sb, prefix); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4379 | } else { |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4380 | /* Target is another dfs share */ |
| 4381 | rc = update_server_fullpath(server, cifs_sb, target); |
| 4382 | dfs_cache_free_tgts(tl); |
| 4383 | |
| 4384 | if (!rc) { |
| 4385 | rc = -EREMOTE; |
| 4386 | list_replace_init(&ntl.tl_list, &tl->tl_list); |
| 4387 | } else |
| 4388 | dfs_cache_free_tgts(&ntl); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4389 | } |
| 4390 | break; |
| 4391 | } |
| 4392 | |
| 4393 | out: |
| 4394 | kfree(share); |
| 4395 | kfree(prefix); |
| 4396 | |
| 4397 | return rc; |
| 4398 | } |
| 4399 | |
Steve French | 0e62904 | 2021-11-10 03:09:52 -0600 | [diff] [blame] | 4400 | static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4401 | struct cifs_sb_info *cifs_sb, char *tree, bool islink, |
| 4402 | struct dfs_cache_tgt_list *tl) |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4403 | { |
| 4404 | int rc; |
| 4405 | int num_links = 0; |
| 4406 | struct TCP_Server_Info *server = tcon->ses->server; |
| 4407 | |
| 4408 | do { |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4409 | rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4410 | if (!rc || rc != -EREMOTE) |
| 4411 | break; |
| 4412 | } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS); |
| 4413 | /* |
| 4414 | * If we couldn't tree connect to any targets from last referral path, then retry from |
| 4415 | * original referral path. |
| 4416 | */ |
| 4417 | if (rc && server->current_fullpath != server->origin_fullpath) { |
| 4418 | server->current_fullpath = server->origin_fullpath; |
Shyam Prasad N | ba978e8 | 2022-01-17 07:15:02 +0000 | [diff] [blame] | 4419 | cifs_reconnect(tcon->ses->server, true); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | dfs_cache_free_tgts(tl); |
| 4423 | return rc; |
| 4424 | } |
| 4425 | |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4426 | int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) |
| 4427 | { |
| 4428 | int rc; |
| 4429 | struct TCP_Server_Info *server = tcon->ses->server; |
| 4430 | const struct smb_version_operations *ops = server->ops; |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4431 | struct super_block *sb = NULL; |
| 4432 | struct cifs_sb_info *cifs_sb; |
| 4433 | struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4434 | char *tree; |
Paulo Alcantara | 11375a5 | 2020-07-21 09:36:43 -0300 | [diff] [blame] | 4435 | struct dfs_info3_param ref = {0}; |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4436 | |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4437 | /* only send once per connect */ |
| 4438 | spin_lock(&cifs_tcp_ses_lock); |
| 4439 | if (tcon->ses->status != CifsGood || |
| 4440 | (tcon->tidStatus != CifsNew && |
| 4441 | tcon->tidStatus != CifsNeedTcon)) { |
| 4442 | spin_unlock(&cifs_tcp_ses_lock); |
| 4443 | return 0; |
| 4444 | } |
| 4445 | tcon->tidStatus = CifsInTcon; |
| 4446 | spin_unlock(&cifs_tcp_ses_lock); |
| 4447 | |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4448 | tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL); |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4449 | if (!tree) { |
| 4450 | rc = -ENOMEM; |
| 4451 | goto out; |
| 4452 | } |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4453 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4454 | if (tcon->ipc) { |
| 4455 | scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); |
| 4456 | rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc); |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4457 | goto out; |
| 4458 | } |
| 4459 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4460 | sb = cifs_get_tcp_super(server); |
| 4461 | if (IS_ERR(sb)) { |
| 4462 | rc = PTR_ERR(sb); |
| 4463 | cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc); |
| 4464 | goto out; |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4465 | } |
| 4466 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4467 | cifs_sb = CIFS_SB(sb); |
Paulo Alcantara | 7548e1d | 2020-07-21 09:36:42 -0300 | [diff] [blame] | 4468 | |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4469 | /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */ |
| 4470 | if (!server->current_fullpath || |
| 4471 | dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) { |
| 4472 | rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, cifs_sb->local_nls); |
| 4473 | goto out; |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4474 | } |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4475 | |
Paulo Alcantara | 7f28af9 | 2021-11-12 15:16:08 -0300 | [diff] [blame] | 4476 | rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK, |
| 4477 | &tl); |
| 4478 | free_dfs_info_param(&ref); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4479 | |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4480 | out: |
| 4481 | kfree(tree); |
Paulo Alcantara | c88f7dc | 2021-11-03 13:53:29 -0300 | [diff] [blame] | 4482 | cifs_put_tcp_super(sb); |
| 4483 | |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4484 | if (rc) { |
| 4485 | spin_lock(&cifs_tcp_ses_lock); |
Shyam Prasad N | c1604da | 2022-01-16 13:38:14 +0000 | [diff] [blame] | 4486 | if (tcon->tidStatus == CifsInTcon) |
| 4487 | tcon->tidStatus = CifsNeedTcon; |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4488 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | c1604da | 2022-01-16 13:38:14 +0000 | [diff] [blame] | 4489 | } else { |
| 4490 | spin_lock(&cifs_tcp_ses_lock); |
| 4491 | if (tcon->tidStatus == CifsInTcon) |
| 4492 | tcon->tidStatus = CifsGood; |
| 4493 | spin_unlock(&cifs_tcp_ses_lock); |
| 4494 | tcon->need_reconnect = false; |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4497 | return rc; |
| 4498 | } |
| 4499 | #else |
| 4500 | int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) |
| 4501 | { |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4502 | int rc; |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4503 | const struct smb_version_operations *ops = tcon->ses->server->ops; |
| 4504 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 4505 | /* only send once per connect */ |
| 4506 | spin_lock(&cifs_tcp_ses_lock); |
| 4507 | if (tcon->ses->status != CifsGood || |
| 4508 | (tcon->tidStatus != CifsNew && |
| 4509 | tcon->tidStatus != CifsNeedTcon)) { |
| 4510 | spin_unlock(&cifs_tcp_ses_lock); |
| 4511 | return 0; |
| 4512 | } |
| 4513 | tcon->tidStatus = CifsInTcon; |
| 4514 | spin_unlock(&cifs_tcp_ses_lock); |
| 4515 | |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4516 | rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc); |
| 4517 | if (rc) { |
| 4518 | spin_lock(&cifs_tcp_ses_lock); |
Shyam Prasad N | c1604da | 2022-01-16 13:38:14 +0000 | [diff] [blame] | 4519 | if (tcon->tidStatus == CifsInTcon) |
| 4520 | tcon->tidStatus = CifsNeedTcon; |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4521 | spin_unlock(&cifs_tcp_ses_lock); |
Shyam Prasad N | c1604da | 2022-01-16 13:38:14 +0000 | [diff] [blame] | 4522 | } else { |
| 4523 | spin_lock(&cifs_tcp_ses_lock); |
| 4524 | if (tcon->tidStatus == CifsInTcon) |
| 4525 | tcon->tidStatus = CifsGood; |
| 4526 | spin_unlock(&cifs_tcp_ses_lock); |
| 4527 | tcon->need_reconnect = false; |
Shyam Prasad N | 3663c90 | 2021-10-30 04:51:35 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | return rc; |
Stefan Metzmacher | 565674d | 2020-07-21 09:36:38 -0300 | [diff] [blame] | 4531 | } |
| 4532 | #endif |