4 * Copyright (C) International Business Machines Corp., 2002,2011
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/net.h>
23 #include <linux/string.h>
24 #include <linux/sched/signal.h>
25 #include <linux/list.h>
26 #include <linux/wait.h>
27 #include <linux/slab.h>
28 #include <linux/pagemap.h>
29 #include <linux/ctype.h>
30 #include <linux/utsname.h>
31 #include <linux/mempool.h>
32 #include <linux/delay.h>
33 #include <linux/completion.h>
34 #include <linux/kthread.h>
35 #include <linux/pagevec.h>
36 #include <linux/freezer.h>
37 #include <linux/namei.h>
38 #include <linux/uuid.h>
39 #include <linux/uaccess.h>
40 #include <asm/processor.h>
41 #include <linux/inet.h>
42 #include <linux/module.h>
43 #include <keys/user-type.h>
45 #include <linux/parser.h>
46 #include <linux/bvec.h>
49 #include "cifsproto.h"
50 #include "cifs_unicode.h"
51 #include "cifs_debug.h"
52 #include "cifs_fs_sb.h"
55 #include "rfc1002pdu.h"
57 #include "smb2proto.h"
58 #include "smbdirect.h"
60 extern mempool_t *cifs_req_poolp;
61 extern bool disable_legacy_dialects;
63 /* FIXME: should these be tunable? */
64 #define TLINK_ERROR_EXPIRE (1 * HZ)
65 #define TLINK_IDLE_EXPIRE (600 * HZ)
68 /* Mount options that take no arguments */
69 Opt_user_xattr, Opt_nouser_xattr,
70 Opt_forceuid, Opt_noforceuid,
71 Opt_forcegid, Opt_noforcegid,
72 Opt_noblocksend, Opt_noautotune,
73 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
74 Opt_mapposix, Opt_nomapposix,
75 Opt_mapchars, Opt_nomapchars, Opt_sfu,
76 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
77 Opt_noposixpaths, Opt_nounix, Opt_unix,
80 Opt_handlecache, Opt_nohandlecache,
81 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
82 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
83 Opt_nohard, Opt_nosoft,
85 Opt_nostrictsync, Opt_strictsync,
86 Opt_serverino, Opt_noserverino,
87 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
88 Opt_acl, Opt_noacl, Opt_locallease,
89 Opt_sign, Opt_seal, Opt_noac,
90 Opt_fsc, Opt_mfsymlinks,
91 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
92 Opt_persistent, Opt_nopersistent,
93 Opt_resilient, Opt_noresilient,
94 Opt_domainauto, Opt_rdma,
96 /* Mount options which take numeric value */
97 Opt_backupuid, Opt_backupgid, Opt_uid,
98 Opt_cruid, Opt_gid, Opt_file_mode,
99 Opt_dirmode, Opt_port,
100 Opt_rsize, Opt_wsize, Opt_actimeo,
101 Opt_echo_interval, Opt_max_credits,
104 /* Mount options which take string value */
105 Opt_user, Opt_pass, Opt_ip,
106 Opt_domain, Opt_srcaddr, Opt_iocharset,
107 Opt_netbiosname, Opt_servern,
108 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
110 /* Mount options to be ignored */
113 /* Options which could be blank */
121 static const match_table_t cifs_mount_option_tokens = {
123 { Opt_user_xattr, "user_xattr" },
124 { Opt_nouser_xattr, "nouser_xattr" },
125 { Opt_forceuid, "forceuid" },
126 { Opt_noforceuid, "noforceuid" },
127 { Opt_forcegid, "forcegid" },
128 { Opt_noforcegid, "noforcegid" },
129 { Opt_noblocksend, "noblocksend" },
130 { Opt_noautotune, "noautotune" },
131 { Opt_hard, "hard" },
132 { Opt_soft, "soft" },
133 { Opt_perm, "perm" },
134 { Opt_noperm, "noperm" },
135 { Opt_mapchars, "mapchars" }, /* SFU style */
136 { Opt_nomapchars, "nomapchars" },
137 { Opt_mapposix, "mapposix" }, /* SFM style */
138 { Opt_nomapposix, "nomapposix" },
140 { Opt_nosfu, "nosfu" },
141 { Opt_nodfs, "nodfs" },
142 { Opt_posixpaths, "posixpaths" },
143 { Opt_noposixpaths, "noposixpaths" },
144 { Opt_nounix, "nounix" },
145 { Opt_nounix, "nolinux" },
146 { Opt_nounix, "noposix" },
147 { Opt_unix, "unix" },
148 { Opt_unix, "linux" },
149 { Opt_unix, "posix" },
150 { Opt_nocase, "nocase" },
151 { Opt_nocase, "ignorecase" },
153 { Opt_nobrl, "nobrl" },
154 { Opt_handlecache, "handlecache" },
155 { Opt_nohandlecache, "nohandlecache" },
156 { Opt_nobrl, "nolock" },
157 { Opt_forcemandatorylock, "forcemandatorylock" },
158 { Opt_forcemandatorylock, "forcemand" },
159 { Opt_setuids, "setuids" },
160 { Opt_nosetuids, "nosetuids" },
161 { Opt_setuidfromacl, "idsfromsid" },
162 { Opt_dynperm, "dynperm" },
163 { Opt_nodynperm, "nodynperm" },
164 { Opt_nohard, "nohard" },
165 { Opt_nosoft, "nosoft" },
166 { Opt_nointr, "nointr" },
167 { Opt_intr, "intr" },
168 { Opt_nostrictsync, "nostrictsync" },
169 { Opt_strictsync, "strictsync" },
170 { Opt_serverino, "serverino" },
171 { Opt_noserverino, "noserverino" },
172 { Opt_rwpidforward, "rwpidforward" },
173 { Opt_cifsacl, "cifsacl" },
174 { Opt_nocifsacl, "nocifsacl" },
176 { Opt_noacl, "noacl" },
177 { Opt_locallease, "locallease" },
178 { Opt_sign, "sign" },
179 { Opt_seal, "seal" },
180 { Opt_noac, "noac" },
182 { Opt_mfsymlinks, "mfsymlinks" },
183 { Opt_multiuser, "multiuser" },
184 { Opt_sloppy, "sloppy" },
185 { Opt_nosharesock, "nosharesock" },
186 { Opt_persistent, "persistenthandles"},
187 { Opt_nopersistent, "nopersistenthandles"},
188 { Opt_resilient, "resilienthandles"},
189 { Opt_noresilient, "noresilienthandles"},
190 { Opt_domainauto, "domainauto"},
193 { Opt_backupuid, "backupuid=%s" },
194 { Opt_backupgid, "backupgid=%s" },
195 { Opt_uid, "uid=%s" },
196 { Opt_cruid, "cruid=%s" },
197 { Opt_gid, "gid=%s" },
198 { Opt_file_mode, "file_mode=%s" },
199 { Opt_dirmode, "dirmode=%s" },
200 { Opt_dirmode, "dir_mode=%s" },
201 { Opt_port, "port=%s" },
202 { Opt_rsize, "rsize=%s" },
203 { Opt_wsize, "wsize=%s" },
204 { Opt_actimeo, "actimeo=%s" },
205 { Opt_echo_interval, "echo_interval=%s" },
206 { Opt_max_credits, "max_credits=%s" },
207 { Opt_snapshot, "snapshot=%s" },
209 { Opt_blank_user, "user=" },
210 { Opt_blank_user, "username=" },
211 { Opt_user, "user=%s" },
212 { Opt_user, "username=%s" },
213 { Opt_blank_pass, "pass=" },
214 { Opt_blank_pass, "password=" },
215 { Opt_pass, "pass=%s" },
216 { Opt_pass, "password=%s" },
217 { Opt_blank_ip, "ip=" },
218 { Opt_blank_ip, "addr=" },
220 { Opt_ip, "addr=%s" },
221 { Opt_ignore, "unc=%s" },
222 { Opt_ignore, "target=%s" },
223 { Opt_ignore, "path=%s" },
224 { Opt_domain, "dom=%s" },
225 { Opt_domain, "domain=%s" },
226 { Opt_domain, "workgroup=%s" },
227 { Opt_srcaddr, "srcaddr=%s" },
228 { Opt_ignore, "prefixpath=%s" },
229 { Opt_iocharset, "iocharset=%s" },
230 { Opt_netbiosname, "netbiosname=%s" },
231 { Opt_servern, "servern=%s" },
232 { Opt_ver, "ver=%s" },
233 { Opt_vers, "vers=%s" },
234 { Opt_sec, "sec=%s" },
235 { Opt_cache, "cache=%s" },
237 { Opt_ignore, "cred" },
238 { Opt_ignore, "credentials" },
239 { Opt_ignore, "cred=%s" },
240 { Opt_ignore, "credentials=%s" },
241 { Opt_ignore, "guest" },
242 { Opt_ignore, "rw" },
243 { Opt_ignore, "ro" },
244 { Opt_ignore, "suid" },
245 { Opt_ignore, "nosuid" },
246 { Opt_ignore, "exec" },
247 { Opt_ignore, "noexec" },
248 { Opt_ignore, "nodev" },
249 { Opt_ignore, "noauto" },
250 { Opt_ignore, "dev" },
251 { Opt_ignore, "mand" },
252 { Opt_ignore, "nomand" },
253 { Opt_ignore, "relatime" },
254 { Opt_ignore, "_netdev" },
260 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
261 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
262 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
263 Opt_sec_ntlmv2i, Opt_sec_lanman,
269 static const match_table_t cifs_secflavor_tokens = {
270 { Opt_sec_krb5, "krb5" },
271 { Opt_sec_krb5i, "krb5i" },
272 { Opt_sec_krb5p, "krb5p" },
273 { Opt_sec_ntlmsspi, "ntlmsspi" },
274 { Opt_sec_ntlmssp, "ntlmssp" },
275 { Opt_ntlm, "ntlm" },
276 { Opt_sec_ntlmi, "ntlmi" },
277 { Opt_sec_ntlmv2, "nontlm" },
278 { Opt_sec_ntlmv2, "ntlmv2" },
279 { Opt_sec_ntlmv2i, "ntlmv2i" },
280 { Opt_sec_lanman, "lanman" },
281 { Opt_sec_none, "none" },
283 { Opt_sec_err, NULL }
294 static const match_table_t cifs_cacheflavor_tokens = {
295 { Opt_cache_loose, "loose" },
296 { Opt_cache_strict, "strict" },
297 { Opt_cache_none, "none" },
298 { Opt_cache_err, NULL }
301 static const match_table_t cifs_smb_version_tokens = {
302 { Smb_1, SMB1_VERSION_STRING },
303 { Smb_20, SMB20_VERSION_STRING},
304 { Smb_21, SMB21_VERSION_STRING },
305 { Smb_30, SMB30_VERSION_STRING },
306 { Smb_302, SMB302_VERSION_STRING },
307 { Smb_302, ALT_SMB302_VERSION_STRING },
308 { Smb_311, SMB311_VERSION_STRING },
309 { Smb_311, ALT_SMB311_VERSION_STRING },
310 { Smb_3any, SMB3ANY_VERSION_STRING },
311 { Smb_default, SMBDEFAULT_VERSION_STRING },
312 { Smb_version_err, NULL }
315 static int ip_connect(struct TCP_Server_Info *server);
316 static int generic_ip_connect(struct TCP_Server_Info *server);
317 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
318 static void cifs_prune_tlinks(struct work_struct *work);
319 static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
320 const char *devname, bool is_smb3);
323 * cifs tcp session reconnection
325 * mark tcp session as reconnecting so temporarily locked
326 * mark all smb sessions as reconnecting for tcp session
327 * reconnect tcp session
328 * wake up waiters on reconnection? - (not needed currently)
331 cifs_reconnect(struct TCP_Server_Info *server)
334 struct list_head *tmp, *tmp2;
335 struct cifs_ses *ses;
336 struct cifs_tcon *tcon;
337 struct mid_q_entry *mid_entry;
338 struct list_head retry_list;
340 spin_lock(&GlobalMid_Lock);
341 if (server->tcpStatus == CifsExiting) {
342 /* the demux thread will exit normally
343 next time through the loop */
344 spin_unlock(&GlobalMid_Lock);
347 server->tcpStatus = CifsNeedReconnect;
348 spin_unlock(&GlobalMid_Lock);
350 server->max_read = 0;
352 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
353 trace_smb3_reconnect(server->CurrentMid, server->hostname);
355 /* before reconnecting the tcp session, mark the smb session (uid)
356 and the tid bad so they are not used until reconnected */
357 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
359 spin_lock(&cifs_tcp_ses_lock);
360 list_for_each(tmp, &server->smb_ses_list) {
361 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
362 ses->need_reconnect = true;
363 list_for_each(tmp2, &ses->tcon_list) {
364 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
365 tcon->need_reconnect = true;
368 ses->tcon_ipc->need_reconnect = true;
370 spin_unlock(&cifs_tcp_ses_lock);
372 /* do not want to be sending data on a socket we are freeing */
373 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
374 mutex_lock(&server->srv_mutex);
375 if (server->ssocket) {
376 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
377 server->ssocket->state, server->ssocket->flags);
378 kernel_sock_shutdown(server->ssocket, SHUT_WR);
379 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
380 server->ssocket->state, server->ssocket->flags);
381 sock_release(server->ssocket);
382 server->ssocket = NULL;
384 server->sequence_number = 0;
385 server->session_estab = false;
386 kfree(server->session_key.response);
387 server->session_key.response = NULL;
388 server->session_key.len = 0;
389 server->lstrp = jiffies;
391 /* mark submitted MIDs for retry and issue callback */
392 INIT_LIST_HEAD(&retry_list);
393 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
394 spin_lock(&GlobalMid_Lock);
395 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
396 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
397 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
398 mid_entry->mid_state = MID_RETRY_NEEDED;
399 list_move(&mid_entry->qhead, &retry_list);
401 spin_unlock(&GlobalMid_Lock);
402 mutex_unlock(&server->srv_mutex);
404 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
405 list_for_each_safe(tmp, tmp2, &retry_list) {
406 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
407 list_del_init(&mid_entry->qhead);
408 mid_entry->callback(mid_entry);
414 /* we should try only the port we connected to before */
415 mutex_lock(&server->srv_mutex);
416 if (cifs_rdma_enabled(server))
417 rc = smbd_reconnect(server);
419 rc = generic_ip_connect(server);
421 cifs_dbg(FYI, "reconnect error %d\n", rc);
422 mutex_unlock(&server->srv_mutex);
425 atomic_inc(&tcpSesReconnectCount);
426 spin_lock(&GlobalMid_Lock);
427 if (server->tcpStatus != CifsExiting)
428 server->tcpStatus = CifsNeedNegotiate;
429 spin_unlock(&GlobalMid_Lock);
430 mutex_unlock(&server->srv_mutex);
432 } while (server->tcpStatus == CifsNeedReconnect);
434 if (server->tcpStatus == CifsNeedNegotiate)
435 mod_delayed_work(cifsiod_wq, &server->echo, 0);
441 cifs_echo_request(struct work_struct *work)
444 struct TCP_Server_Info *server = container_of(work,
445 struct TCP_Server_Info, echo.work);
446 unsigned long echo_interval;
449 * If we need to renegotiate, set echo interval to zero to
450 * immediately call echo service where we can renegotiate.
452 if (server->tcpStatus == CifsNeedNegotiate)
455 echo_interval = server->echo_interval;
458 * We cannot send an echo if it is disabled.
459 * Also, no need to ping if we got a response recently.
462 if (server->tcpStatus == CifsNeedReconnect ||
463 server->tcpStatus == CifsExiting ||
464 server->tcpStatus == CifsNew ||
465 (server->ops->can_echo && !server->ops->can_echo(server)) ||
466 time_before(jiffies, server->lstrp + echo_interval - HZ))
469 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
471 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
475 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
479 allocate_buffers(struct TCP_Server_Info *server)
481 if (!server->bigbuf) {
482 server->bigbuf = (char *)cifs_buf_get();
483 if (!server->bigbuf) {
484 cifs_dbg(VFS, "No memory for large SMB response\n");
486 /* retry will check if exiting */
489 } else if (server->large_buf) {
490 /* we are reusing a dirty large buf, clear its start */
491 memset(server->bigbuf, 0, HEADER_SIZE(server));
494 if (!server->smallbuf) {
495 server->smallbuf = (char *)cifs_small_buf_get();
496 if (!server->smallbuf) {
497 cifs_dbg(VFS, "No memory for SMB response\n");
499 /* retry will check if exiting */
502 /* beginning of smb buffer is cleared in our buf_get */
504 /* if existing small buf clear beginning */
505 memset(server->smallbuf, 0, HEADER_SIZE(server));
512 server_unresponsive(struct TCP_Server_Info *server)
515 * We need to wait 2 echo intervals to make sure we handle such
517 * 1s client sends a normal SMB request
518 * 2s client gets a response
519 * 30s echo workqueue job pops, and decides we got a response recently
520 * and don't need to send another
522 * 65s kernel_recvmsg times out, and we see that we haven't gotten
523 * a response in >60s.
525 if ((server->tcpStatus == CifsGood ||
526 server->tcpStatus == CifsNeedNegotiate) &&
527 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
528 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
529 server->hostname, (2 * server->echo_interval) / HZ);
530 cifs_reconnect(server);
531 wake_up(&server->response_q);
539 cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
544 smb_msg->msg_control = NULL;
545 smb_msg->msg_controllen = 0;
547 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
550 if (server_unresponsive(server))
551 return -ECONNABORTED;
552 if (cifs_rdma_enabled(server) && server->smbd_conn)
553 length = smbd_recv(server->smbd_conn, smb_msg);
555 length = sock_recvmsg(server->ssocket, smb_msg, 0);
557 if (server->tcpStatus == CifsExiting)
560 if (server->tcpStatus == CifsNeedReconnect) {
561 cifs_reconnect(server);
562 return -ECONNABORTED;
565 if (length == -ERESTARTSYS ||
569 * Minimum sleep to prevent looping, allowing socket
570 * to clear and app threads to set tcpStatus
571 * CifsNeedReconnect if server hung.
573 usleep_range(1000, 2000);
579 cifs_dbg(FYI, "Received no data or error: %d\n", length);
580 cifs_reconnect(server);
581 return -ECONNABORTED;
588 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
589 unsigned int to_read)
591 struct msghdr smb_msg;
592 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
593 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
595 return cifs_readv_from_socket(server, &smb_msg);
599 cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
600 unsigned int page_offset, unsigned int to_read)
602 struct msghdr smb_msg;
603 struct bio_vec bv = {
604 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
605 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
606 return cifs_readv_from_socket(server, &smb_msg);
610 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
613 * The first byte big endian of the length field,
614 * is actually not part of the length but the type
615 * with the most common, zero, as regular data.
618 case RFC1002_SESSION_MESSAGE:
619 /* Regular SMB response */
621 case RFC1002_SESSION_KEEP_ALIVE:
622 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
624 case RFC1002_POSITIVE_SESSION_RESPONSE:
625 cifs_dbg(FYI, "RFC 1002 positive session response\n");
627 case RFC1002_NEGATIVE_SESSION_RESPONSE:
629 * We get this from Windows 98 instead of an error on
630 * SMB negprot response.
632 cifs_dbg(FYI, "RFC 1002 negative session response\n");
633 /* give server a second to clean up */
636 * Always try 445 first on reconnect since we get NACK
637 * on some if we ever connected to port 139 (the NACK
638 * is since we do not begin with RFC1001 session
641 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
642 cifs_reconnect(server);
643 wake_up(&server->response_q);
646 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
647 cifs_reconnect(server);
654 dequeue_mid(struct mid_q_entry *mid, bool malformed)
656 #ifdef CONFIG_CIFS_STATS2
657 mid->when_received = jiffies;
659 spin_lock(&GlobalMid_Lock);
661 mid->mid_state = MID_RESPONSE_RECEIVED;
663 mid->mid_state = MID_RESPONSE_MALFORMED;
665 * Trying to handle/dequeue a mid after the send_recv()
666 * function has finished processing it is a bug.
668 if (mid->mid_flags & MID_DELETED)
669 printk_once(KERN_WARNING
670 "trying to dequeue a deleted mid\n");
672 list_del_init(&mid->qhead);
673 spin_unlock(&GlobalMid_Lock);
677 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
678 char *buf, int malformed)
680 if (server->ops->check_trans2 &&
681 server->ops->check_trans2(mid, server, buf, malformed))
684 mid->large_buf = server->large_buf;
685 /* Was previous buf put in mpx struct for multi-rsp? */
686 if (!mid->multiRsp) {
687 /* smb buffer will be freed by user thread */
688 if (server->large_buf)
689 server->bigbuf = NULL;
691 server->smallbuf = NULL;
693 dequeue_mid(mid, malformed);
696 static void clean_demultiplex_info(struct TCP_Server_Info *server)
700 /* take it off the list, if it's not already */
701 spin_lock(&cifs_tcp_ses_lock);
702 list_del_init(&server->tcp_ses_list);
703 spin_unlock(&cifs_tcp_ses_lock);
705 spin_lock(&GlobalMid_Lock);
706 server->tcpStatus = CifsExiting;
707 spin_unlock(&GlobalMid_Lock);
708 wake_up_all(&server->response_q);
710 /* check if we have blocked requests that need to free */
711 spin_lock(&server->req_lock);
712 if (server->credits <= 0)
714 spin_unlock(&server->req_lock);
716 * Although there should not be any requests blocked on this queue it
717 * can not hurt to be paranoid and try to wake up requests that may
718 * haven been blocked when more than 50 at time were on the wire to the
719 * same server - they now will see the session is in exit state and get
720 * out of SendReceive.
722 wake_up_all(&server->request_q);
723 /* give those requests time to exit */
725 if (cifs_rdma_enabled(server) && server->smbd_conn) {
726 smbd_destroy(server->smbd_conn);
727 server->smbd_conn = NULL;
729 if (server->ssocket) {
730 sock_release(server->ssocket);
731 server->ssocket = NULL;
734 if (!list_empty(&server->pending_mid_q)) {
735 struct list_head dispose_list;
736 struct mid_q_entry *mid_entry;
737 struct list_head *tmp, *tmp2;
739 INIT_LIST_HEAD(&dispose_list);
740 spin_lock(&GlobalMid_Lock);
741 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
742 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
743 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
744 mid_entry->mid_state = MID_SHUTDOWN;
745 list_move(&mid_entry->qhead, &dispose_list);
747 spin_unlock(&GlobalMid_Lock);
749 /* now walk dispose list and issue callbacks */
750 list_for_each_safe(tmp, tmp2, &dispose_list) {
751 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
752 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
753 list_del_init(&mid_entry->qhead);
754 mid_entry->callback(mid_entry);
756 /* 1/8th of sec is more than enough time for them to exit */
760 if (!list_empty(&server->pending_mid_q)) {
762 * mpx threads have not exited yet give them at least the smb
763 * send timeout time for long ops.
765 * Due to delays on oplock break requests, we need to wait at
766 * least 45 seconds before giving up on a request getting a
767 * response and going ahead and killing cifsd.
769 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
772 * If threads still have not exited they are probably never
773 * coming home not much else we can do but free the memory.
777 kfree(server->hostname);
780 length = atomic_dec_return(&tcpSesAllocCount);
782 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
786 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
789 char *buf = server->smallbuf;
790 unsigned int pdu_length = server->pdu_size;
792 /* make sure this will fit in a large buffer */
793 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
794 server->vals->header_preamble_size) {
795 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
796 cifs_reconnect(server);
797 wake_up(&server->response_q);
798 return -ECONNABORTED;
801 /* switch to large buffer if too big for a small one */
802 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
803 server->large_buf = true;
804 memcpy(server->bigbuf, buf, server->total_read);
805 buf = server->bigbuf;
808 /* now read the rest */
809 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
810 pdu_length - HEADER_SIZE(server) + 1
811 + server->vals->header_preamble_size);
815 server->total_read += length;
817 dump_smb(buf, server->total_read);
819 return cifs_handle_standard(server, mid);
823 cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
825 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
829 * We know that we received enough to get to the MID as we
830 * checked the pdu_length earlier. Now check to see
831 * if the rest of the header is OK. We borrow the length
832 * var for the rest of the loop to avoid a new stack var.
834 * 48 bytes is enough to display the header and a little bit
835 * into the payload for debugging purposes.
837 length = server->ops->check_message(buf, server->total_read, server);
839 cifs_dump_mem("Bad SMB: ", buf,
840 min_t(unsigned int, server->total_read, 48));
842 if (server->ops->is_session_expired &&
843 server->ops->is_session_expired(buf)) {
844 cifs_reconnect(server);
845 wake_up(&server->response_q);
849 if (server->ops->is_status_pending &&
850 server->ops->is_status_pending(buf, server, length))
856 handle_mid(mid, server, buf, length);
861 cifs_demultiplex_thread(void *p)
863 int i, num_mids, length;
864 struct TCP_Server_Info *server = p;
865 unsigned int pdu_length;
866 unsigned int next_offset;
868 struct task_struct *task_to_wake = NULL;
869 struct mid_q_entry *mids[MAX_COMPOUND];
870 char *bufs[MAX_COMPOUND];
872 current->flags |= PF_MEMALLOC;
873 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
875 length = atomic_inc_return(&tcpSesAllocCount);
877 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
880 while (server->tcpStatus != CifsExiting) {
884 if (!allocate_buffers(server))
887 server->large_buf = false;
888 buf = server->smallbuf;
889 pdu_length = 4; /* enough to get RFC1001 header */
891 length = cifs_read_from_socket(server, buf, pdu_length);
895 if (server->vals->header_preamble_size == 0)
896 server->total_read = 0;
898 server->total_read = length;
901 * The right amount was read from socket - 4 bytes,
902 * so we can now interpret the length field.
904 pdu_length = get_rfc1002_length(buf);
906 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
907 if (!is_smb_response(server, buf[0]))
910 server->pdu_size = pdu_length;
912 /* make sure we have enough to get to the MID */
913 if (server->pdu_size < HEADER_SIZE(server) - 1 -
914 server->vals->header_preamble_size) {
915 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
917 cifs_reconnect(server);
918 wake_up(&server->response_q);
922 /* read down to the MID */
923 length = cifs_read_from_socket(server,
924 buf + server->vals->header_preamble_size,
925 HEADER_SIZE(server) - 1
926 - server->vals->header_preamble_size);
929 server->total_read += length;
931 if (server->ops->next_header) {
932 next_offset = server->ops->next_header(buf);
934 server->pdu_size = next_offset;
937 memset(mids, 0, sizeof(mids));
938 memset(bufs, 0, sizeof(bufs));
941 if (server->ops->is_transform_hdr &&
942 server->ops->receive_transform &&
943 server->ops->is_transform_hdr(buf)) {
944 length = server->ops->receive_transform(server,
949 mids[0] = server->ops->find_mid(server, buf);
953 if (!mids[0] || !mids[0]->receive)
954 length = standard_receive3(server, mids[0]);
956 length = mids[0]->receive(server, mids[0]);
960 for (i = 0; i < num_mids; i++)
962 cifs_mid_q_entry_release(mids[i]);
966 if (server->large_buf)
967 buf = server->bigbuf;
970 server->lstrp = jiffies;
972 for (i = 0; i < num_mids; i++) {
973 if (mids[i] != NULL) {
974 mids[i]->resp_buf_size = server->pdu_size;
975 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
976 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
977 server->ops->handle_cancelled_mid)
978 server->ops->handle_cancelled_mid(
982 if (!mids[i]->multiRsp || mids[i]->multiEnd)
983 mids[i]->callback(mids[i]);
985 cifs_mid_q_entry_release(mids[i]);
986 } else if (server->ops->is_oplock_break &&
987 server->ops->is_oplock_break(bufs[i],
989 cifs_dbg(FYI, "Received oplock break\n");
991 cifs_dbg(VFS, "No task to wake, unknown frame "
992 "received! NumMids %d\n",
993 atomic_read(&midCount));
994 cifs_dump_mem("Received Data is: ", bufs[i],
995 HEADER_SIZE(server));
996 #ifdef CONFIG_CIFS_DEBUG2
997 if (server->ops->dump_detail)
998 server->ops->dump_detail(bufs[i],
1000 cifs_dump_mids(server);
1001 #endif /* CIFS_DEBUG2 */
1005 if (pdu_length > server->pdu_size) {
1006 if (!allocate_buffers(server))
1008 pdu_length -= server->pdu_size;
1009 server->total_read = 0;
1010 server->large_buf = false;
1011 buf = server->smallbuf;
1014 } /* end while !EXITING */
1016 /* buffer usually freed in free_mid - need to free it here on exit */
1017 cifs_buf_release(server->bigbuf);
1018 if (server->smallbuf) /* no sense logging a debug message if NULL */
1019 cifs_small_buf_release(server->smallbuf);
1021 task_to_wake = xchg(&server->tsk, NULL);
1022 clean_demultiplex_info(server);
1024 /* if server->tsk was NULL then wait for a signal before exiting */
1025 if (!task_to_wake) {
1026 set_current_state(TASK_INTERRUPTIBLE);
1027 while (!signal_pending(current)) {
1029 set_current_state(TASK_INTERRUPTIBLE);
1031 set_current_state(TASK_RUNNING);
1034 module_put_and_exit(0);
1037 /* extract the host portion of the UNC string */
1039 extract_hostname(const char *unc)
1045 /* skip double chars at beginning of string */
1046 /* BB: check validity of these bytes? */
1047 if (strlen(unc) < 3)
1048 return ERR_PTR(-EINVAL);
1049 for (src = unc; *src && *src == '\\'; src++)
1052 return ERR_PTR(-EINVAL);
1054 /* delimiter between hostname and sharename is always '\\' now */
1055 delim = strchr(src, '\\');
1057 return ERR_PTR(-EINVAL);
1060 dst = kmalloc((len + 1), GFP_KERNEL);
1062 return ERR_PTR(-ENOMEM);
1064 memcpy(dst, src, len);
1070 static int get_option_ul(substring_t args[], unsigned long *option)
1075 string = match_strdup(args);
1078 rc = kstrtoul(string, 0, option);
1084 static int get_option_uid(substring_t args[], kuid_t *result)
1086 unsigned long value;
1090 rc = get_option_ul(args, &value);
1094 uid = make_kuid(current_user_ns(), value);
1095 if (!uid_valid(uid))
1102 static int get_option_gid(substring_t args[], kgid_t *result)
1104 unsigned long value;
1108 rc = get_option_ul(args, &value);
1112 gid = make_kgid(current_user_ns(), value);
1113 if (!gid_valid(gid))
1120 static int cifs_parse_security_flavors(char *value,
1121 struct smb_vol *vol)
1124 substring_t args[MAX_OPT_ARGS];
1127 * With mount options, the last one should win. Reset any existing
1128 * settings back to default.
1130 vol->sectype = Unspecified;
1133 switch (match_token(value, cifs_secflavor_tokens, args)) {
1135 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1141 vol->sectype = Kerberos;
1143 case Opt_sec_ntlmsspi:
1146 case Opt_sec_ntlmssp:
1147 vol->sectype = RawNTLMSSP;
1153 vol->sectype = NTLM;
1155 case Opt_sec_ntlmv2i:
1158 case Opt_sec_ntlmv2:
1159 vol->sectype = NTLMv2;
1161 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1162 case Opt_sec_lanman:
1163 vol->sectype = LANMAN;
1170 cifs_dbg(VFS, "bad security option: %s\n", value);
1178 cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1180 substring_t args[MAX_OPT_ARGS];
1182 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1183 case Opt_cache_loose:
1184 vol->direct_io = false;
1185 vol->strict_io = false;
1187 case Opt_cache_strict:
1188 vol->direct_io = false;
1189 vol->strict_io = true;
1191 case Opt_cache_none:
1192 vol->direct_io = true;
1193 vol->strict_io = false;
1196 cifs_dbg(VFS, "bad cache= option: %s\n", value);
1203 cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
1205 substring_t args[MAX_OPT_ARGS];
1207 switch (match_token(value, cifs_smb_version_tokens, args)) {
1208 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1210 if (disable_legacy_dialects) {
1211 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1215 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1218 vol->ops = &smb1_operations;
1219 vol->vals = &smb1_values;
1222 if (disable_legacy_dialects) {
1223 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1227 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1230 vol->ops = &smb20_operations;
1231 vol->vals = &smb20_values;
1235 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1238 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1240 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
1242 vol->ops = &smb21_operations;
1243 vol->vals = &smb21_values;
1246 vol->ops = &smb30_operations;
1247 vol->vals = &smb30_values;
1250 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1251 vol->vals = &smb302_values;
1254 vol->ops = &smb311_operations;
1255 vol->vals = &smb311_values;
1258 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1259 vol->vals = &smb3any_values;
1262 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1263 vol->vals = &smbdefault_values;
1266 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
1273 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1274 * fields with the result. Returns 0 on success and an error otherwise.
1277 cifs_parse_devname(const char *devname, struct smb_vol *vol)
1280 const char *delims = "/\\";
1283 /* make sure we have a valid UNC double delimiter prefix */
1284 len = strspn(devname, delims);
1288 /* find delimiter between host and sharename */
1289 pos = strpbrk(devname + 2, delims);
1293 /* skip past delimiter */
1296 /* now go until next delimiter or end of string */
1297 len = strcspn(pos, delims);
1299 /* move "pos" up to delimiter or NULL */
1301 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1305 convert_delimiter(vol->UNC, '\\');
1307 /* skip any delimiter */
1308 if (*pos == '/' || *pos == '\\')
1311 /* If pos is NULL then no prepath */
1315 vol->prepath = kstrdup(pos, GFP_KERNEL);
1323 cifs_parse_mount_options(const char *mountdata, const char *devname,
1324 struct smb_vol *vol, bool is_smb3)
1327 char *mountdata_copy = NULL, *options;
1328 unsigned int temp_len, i, j;
1330 short int override_uid = -1;
1331 short int override_gid = -1;
1332 bool uid_specified = false;
1333 bool gid_specified = false;
1334 bool sloppy = false;
1335 char *invalid = NULL;
1336 char *nodename = utsname()->nodename;
1337 char *string = NULL;
1338 char *tmp_end, *value;
1340 bool got_ip = false;
1341 bool got_version = false;
1342 unsigned short port = 0;
1343 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1347 delim = separator[0];
1349 /* ensure we always start with zeroed-out smb_vol */
1350 memset(vol, 0, sizeof(*vol));
1353 * does not have to be perfect mapping since field is
1354 * informational, only used for servers that do not support
1355 * port 445 and it can be overridden at mount time
1357 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1358 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1359 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1361 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1362 /* null target name indicates to use *SMBSERVR default called name
1363 if we end up sending RFC1001 session initialize */
1364 vol->target_rfc1001_name[0] = 0;
1365 vol->cred_uid = current_uid();
1366 vol->linux_uid = current_uid();
1367 vol->linux_gid = current_gid();
1370 * default to SFM style remapping of seven reserved characters
1371 * unless user overrides it or we negotiate CIFS POSIX where
1372 * it is unnecessary. Can not simultaneously use more than one mapping
1373 * since then readdir could list files that open could not open
1377 /* default to only allowing write access to owner of the mount */
1378 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1380 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1381 /* default is always to request posix paths. */
1382 vol->posix_paths = 1;
1383 /* default to using server inode numbers where available */
1384 vol->server_ino = 1;
1386 /* default is to use strict cifs caching semantics */
1387 vol->strict_io = true;
1389 vol->actimeo = CIFS_DEF_ACTIMEO;
1391 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1392 vol->ops = &smb30_operations;
1393 vol->vals = &smbdefault_values;
1395 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1398 goto cifs_parse_mount_err;
1400 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1401 if (!mountdata_copy)
1402 goto cifs_parse_mount_err;
1404 options = mountdata_copy;
1405 end = options + strlen(options);
1407 if (strncmp(options, "sep=", 4) == 0) {
1408 if (options[4] != 0) {
1409 separator[0] = options[4];
1412 cifs_dbg(FYI, "Null separator not allowed\n");
1415 vol->backupuid_specified = false; /* no backup intent for a user */
1416 vol->backupgid_specified = false; /* no backup intent for a group */
1418 switch (cifs_parse_devname(devname, vol)) {
1422 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1423 goto cifs_parse_mount_err;
1425 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1426 goto cifs_parse_mount_err;
1428 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1429 goto cifs_parse_mount_err;
1432 while ((data = strsep(&options, separator)) != NULL) {
1433 substring_t args[MAX_OPT_ARGS];
1434 unsigned long option;
1440 token = match_token(data, cifs_mount_option_tokens, args);
1444 /* Ingnore the following */
1448 /* Boolean values */
1449 case Opt_user_xattr:
1452 case Opt_nouser_xattr:
1458 case Opt_noforceuid:
1464 case Opt_noforcegid:
1467 case Opt_noblocksend:
1468 vol->noblocksnd = 1;
1470 case Opt_noautotune:
1471 vol->noautotune = 1;
1486 vol->sfu_remap = true;
1487 vol->remap = false; /* disable SFM mapping */
1489 case Opt_nomapchars:
1490 vol->sfu_remap = false;
1494 vol->sfu_remap = false; /* disable SFU mapping */
1496 case Opt_nomapposix:
1508 case Opt_posixpaths:
1509 vol->posix_paths = 1;
1511 case Opt_noposixpaths:
1512 vol->posix_paths = 0;
1517 "conflicting unix mount options\n");
1518 vol->no_linux_ext = 1;
1521 if (vol->no_linux_ext)
1523 "conflicting unix mount options\n");
1535 * turn off mandatory locking in mode
1536 * if remote locking is turned off since the
1537 * local vfs will do advisory
1539 if (vol->file_mode ==
1540 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1541 vol->file_mode = S_IALLUGO;
1543 case Opt_nohandlecache:
1544 vol->nohandlecache = 1;
1546 case Opt_handlecache:
1547 vol->nohandlecache = 0;
1549 case Opt_forcemandatorylock:
1558 case Opt_setuidfromacl:
1559 vol->setuidfromacl = 1;
1562 vol->dynperm = true;
1565 vol->dynperm = false;
1579 case Opt_nostrictsync:
1580 vol->nostrictsync = 1;
1582 case Opt_strictsync:
1583 vol->nostrictsync = 0;
1586 vol->server_ino = 1;
1588 case Opt_noserverino:
1589 vol->server_ino = 0;
1591 case Opt_rwpidforward:
1592 vol->rwpidforward = 1;
1601 vol->no_psx_acl = 0;
1604 vol->no_psx_acl = 1;
1606 case Opt_locallease:
1607 vol->local_lease = 1;
1613 /* we do not do the following in secFlags because seal
1614 * is a per tree connection (mount) not a per socket
1615 * or per-smb connection option in the protocol
1616 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1621 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1624 #ifndef CONFIG_CIFS_FSCACHE
1625 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1626 goto cifs_parse_mount_err;
1630 case Opt_mfsymlinks:
1631 vol->mfsymlinks = true;
1634 vol->multiuser = true;
1639 case Opt_nosharesock:
1640 vol->nosharesock = true;
1642 case Opt_nopersistent:
1643 vol->nopersistent = true;
1644 if (vol->persistent) {
1646 "persistenthandles mount options conflict\n");
1647 goto cifs_parse_mount_err;
1650 case Opt_persistent:
1651 vol->persistent = true;
1652 if ((vol->nopersistent) || (vol->resilient)) {
1654 "persistenthandles mount options conflict\n");
1655 goto cifs_parse_mount_err;
1659 vol->resilient = true;
1660 if (vol->persistent) {
1662 "persistenthandles mount options conflict\n");
1663 goto cifs_parse_mount_err;
1666 case Opt_noresilient:
1667 vol->resilient = false; /* already the default */
1669 case Opt_domainauto:
1670 vol->domainauto = true;
1676 /* Numeric Values */
1678 if (get_option_uid(args, &vol->backupuid)) {
1679 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1681 goto cifs_parse_mount_err;
1683 vol->backupuid_specified = true;
1686 if (get_option_gid(args, &vol->backupgid)) {
1687 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1689 goto cifs_parse_mount_err;
1691 vol->backupgid_specified = true;
1694 if (get_option_uid(args, &vol->linux_uid)) {
1695 cifs_dbg(VFS, "%s: Invalid uid value\n",
1697 goto cifs_parse_mount_err;
1699 uid_specified = true;
1702 if (get_option_uid(args, &vol->cred_uid)) {
1703 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1705 goto cifs_parse_mount_err;
1709 if (get_option_gid(args, &vol->linux_gid)) {
1710 cifs_dbg(VFS, "%s: Invalid gid value\n",
1712 goto cifs_parse_mount_err;
1714 gid_specified = true;
1717 if (get_option_ul(args, &option)) {
1718 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1720 goto cifs_parse_mount_err;
1722 vol->file_mode = option;
1725 if (get_option_ul(args, &option)) {
1726 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1728 goto cifs_parse_mount_err;
1730 vol->dir_mode = option;
1733 if (get_option_ul(args, &option) ||
1734 option > USHRT_MAX) {
1735 cifs_dbg(VFS, "%s: Invalid port value\n",
1737 goto cifs_parse_mount_err;
1739 port = (unsigned short)option;
1742 if (get_option_ul(args, &option)) {
1743 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1745 goto cifs_parse_mount_err;
1747 vol->rsize = option;
1750 if (get_option_ul(args, &option)) {
1751 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1753 goto cifs_parse_mount_err;
1755 vol->wsize = option;
1758 if (get_option_ul(args, &option)) {
1759 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1761 goto cifs_parse_mount_err;
1763 vol->actimeo = HZ * option;
1764 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1765 cifs_dbg(VFS, "attribute cache timeout too large\n");
1766 goto cifs_parse_mount_err;
1769 case Opt_echo_interval:
1770 if (get_option_ul(args, &option)) {
1771 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1773 goto cifs_parse_mount_err;
1775 vol->echo_interval = option;
1778 if (get_option_ul(args, &option)) {
1779 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1781 goto cifs_parse_mount_err;
1783 vol->snapshot_time = option;
1785 case Opt_max_credits:
1786 if (get_option_ul(args, &option) || (option < 20) ||
1788 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1790 goto cifs_parse_mount_err;
1792 vol->max_credits = option;
1795 /* String Arguments */
1797 case Opt_blank_user:
1798 /* null user, ie. anonymous authentication */
1800 vol->username = NULL;
1803 string = match_strdup(args);
1807 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1808 CIFS_MAX_USERNAME_LEN) {
1809 pr_warn("CIFS: username too long\n");
1810 goto cifs_parse_mount_err;
1813 kfree(vol->username);
1814 vol->username = kstrdup(string, GFP_KERNEL);
1816 goto cifs_parse_mount_err;
1818 case Opt_blank_pass:
1819 /* passwords have to be handled differently
1820 * to allow the character used for deliminator
1821 * to be passed within them
1825 * Check if this is a case where the password
1826 * starts with a delimiter
1828 tmp_end = strchr(data, '=');
1830 if (!(tmp_end < end && tmp_end[1] == delim)) {
1831 /* No it is not. Set the password to NULL */
1832 kzfree(vol->password);
1833 vol->password = NULL;
1836 /* Fallthrough - to Opt_pass below.*/
1838 /* Obtain the value string */
1839 value = strchr(data, '=');
1842 /* Set tmp_end to end of the string */
1843 tmp_end = (char *) value + strlen(value);
1845 /* Check if following character is the deliminator
1846 * If yes, we have encountered a double deliminator
1847 * reset the NULL character to the deliminator
1849 if (tmp_end < end && tmp_end[1] == delim) {
1852 /* Keep iterating until we get to a single
1853 * deliminator OR the end
1855 while ((tmp_end = strchr(tmp_end, delim))
1856 != NULL && (tmp_end[1] == delim)) {
1857 tmp_end = (char *) &tmp_end[2];
1860 /* Reset var options to point to next element */
1863 options = (char *) &tmp_end[1];
1865 /* Reached the end of the mount option
1870 kzfree(vol->password);
1871 /* Now build new password string */
1872 temp_len = strlen(value);
1873 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1874 if (vol->password == NULL) {
1875 pr_warn("CIFS: no memory for password\n");
1876 goto cifs_parse_mount_err;
1879 for (i = 0, j = 0; i < temp_len; i++, j++) {
1880 vol->password[j] = value[i];
1881 if ((value[i] == delim) &&
1882 value[i+1] == delim)
1883 /* skip the second deliminator */
1886 vol->password[j] = '\0';
1889 /* FIXME: should this be an error instead? */
1893 string = match_strdup(args);
1897 if (!cifs_convert_address(dstaddr, string,
1899 pr_err("CIFS: bad ip= option (%s).\n", string);
1900 goto cifs_parse_mount_err;
1905 string = match_strdup(args);
1909 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1910 == CIFS_MAX_DOMAINNAME_LEN) {
1911 pr_warn("CIFS: domain name too long\n");
1912 goto cifs_parse_mount_err;
1915 kfree(vol->domainname);
1916 vol->domainname = kstrdup(string, GFP_KERNEL);
1917 if (!vol->domainname) {
1918 pr_warn("CIFS: no memory for domainname\n");
1919 goto cifs_parse_mount_err;
1921 cifs_dbg(FYI, "Domain name set\n");
1924 string = match_strdup(args);
1928 if (!cifs_convert_address(
1929 (struct sockaddr *)&vol->srcaddr,
1930 string, strlen(string))) {
1931 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1933 goto cifs_parse_mount_err;
1937 string = match_strdup(args);
1941 if (strnlen(string, 1024) >= 65) {
1942 pr_warn("CIFS: iocharset name too long.\n");
1943 goto cifs_parse_mount_err;
1946 if (strncasecmp(string, "default", 7) != 0) {
1947 kfree(vol->iocharset);
1948 vol->iocharset = kstrdup(string,
1950 if (!vol->iocharset) {
1951 pr_warn("CIFS: no memory for charset\n");
1952 goto cifs_parse_mount_err;
1955 /* if iocharset not set then load_nls_default
1958 cifs_dbg(FYI, "iocharset set to %s\n", string);
1960 case Opt_netbiosname:
1961 string = match_strdup(args);
1965 memset(vol->source_rfc1001_name, 0x20,
1968 * FIXME: are there cases in which a comma can
1969 * be valid in workstation netbios name (and
1970 * need special handling)?
1972 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1973 /* don't ucase netbiosname for user */
1976 vol->source_rfc1001_name[i] = string[i];
1978 /* The string has 16th byte zero still from
1979 * set at top of the function
1981 if (i == RFC1001_NAME_LEN && string[i] != 0)
1982 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
1985 /* servernetbiosname specified override *SMBSERVER */
1986 string = match_strdup(args);
1990 /* last byte, type, is 0x20 for servr type */
1991 memset(vol->target_rfc1001_name, 0x20,
1992 RFC1001_NAME_LEN_WITH_NULL);
1994 /* BB are there cases in which a comma can be
1995 valid in this workstation netbios name
1996 (and need special handling)? */
1998 /* user or mount helper must uppercase the
2000 for (i = 0; i < 15; i++) {
2003 vol->target_rfc1001_name[i] = string[i];
2005 /* The string has 16th byte zero still from
2006 set at top of the function */
2007 if (i == RFC1001_NAME_LEN && string[i] != 0)
2008 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
2011 /* version of mount userspace tools, not dialect */
2012 string = match_strdup(args);
2016 /* If interface changes in mount.cifs bump to new ver */
2017 if (strncasecmp(string, "1", 1) == 0) {
2018 if (strlen(string) > 1) {
2019 pr_warn("Bad mount helper ver=%s. Did "
2020 "you want SMB1 (CIFS) dialect "
2021 "and mean to type vers=1.0 "
2022 "instead?\n", string);
2023 goto cifs_parse_mount_err;
2025 /* This is the default */
2028 /* For all other value, error */
2029 pr_warn("CIFS: Invalid mount helper version specified\n");
2030 goto cifs_parse_mount_err;
2032 /* protocol version (dialect) */
2033 string = match_strdup(args);
2037 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
2038 goto cifs_parse_mount_err;
2042 string = match_strdup(args);
2046 if (cifs_parse_security_flavors(string, vol) != 0)
2047 goto cifs_parse_mount_err;
2050 string = match_strdup(args);
2054 if (cifs_parse_cache_flavor(string, vol) != 0)
2055 goto cifs_parse_mount_err;
2059 * An option we don't recognize. Save it off for later
2060 * if we haven't already found one
2066 /* Free up any allocated string */
2071 if (!sloppy && invalid) {
2072 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
2073 goto cifs_parse_mount_err;
2076 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2077 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2078 goto cifs_parse_mount_err;
2082 /* Muliuser mounts require CONFIG_KEYS support */
2083 if (vol->multiuser) {
2084 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
2085 goto cifs_parse_mount_err;
2089 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
2090 goto cifs_parse_mount_err;
2093 /* make sure UNC has a share name */
2094 if (!strchr(vol->UNC + 3, '\\')) {
2095 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
2096 goto cifs_parse_mount_err;
2103 /* No ip= option specified? Try to get it from UNC */
2104 /* Use the address part of the UNC. */
2105 slash = strchr(&vol->UNC[2], '\\');
2106 len = slash - &vol->UNC[2];
2107 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
2108 pr_err("Unable to determine destination address.\n");
2109 goto cifs_parse_mount_err;
2113 /* set the port that we got earlier */
2114 cifs_set_port(dstaddr, port);
2117 vol->override_uid = override_uid;
2118 else if (override_uid == 1)
2119 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
2122 vol->override_gid = override_gid;
2123 else if (override_gid == 1)
2124 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
2126 if (got_version == false)
2127 pr_warn("No dialect specified on mount. Default has changed to "
2128 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
2129 "(SMB1). To use the less secure SMB1 dialect to access "
2130 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2133 kfree(mountdata_copy);
2137 pr_warn("Could not allocate temporary buffer\n");
2138 cifs_parse_mount_err:
2140 kfree(mountdata_copy);
2144 /** Returns true if srcaddr isn't specified and rhs isn't
2145 * specified, or if srcaddr is specified and
2146 * matches the IP address of the rhs argument.
2149 srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2151 switch (srcaddr->sa_family) {
2153 return (rhs->sa_family == AF_UNSPEC);
2155 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2156 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2157 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2160 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
2161 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
2162 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2166 return false; /* don't expect to be here */
2171 * If no port is specified in addr structure, we try to match with 445 port
2172 * and if it fails - with 139 ports. It should be called only if address
2173 * families of server and addr are equal.
2176 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2178 __be16 port, *sport;
2180 switch (addr->sa_family) {
2182 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2183 port = ((struct sockaddr_in *) addr)->sin_port;
2186 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2187 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2195 port = htons(CIFS_PORT);
2199 port = htons(RFC1001_PORT);
2202 return port == *sport;
2206 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2207 struct sockaddr *srcaddr)
2209 switch (addr->sa_family) {
2211 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2212 struct sockaddr_in *srv_addr4 =
2213 (struct sockaddr_in *)&server->dstaddr;
2215 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
2220 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2221 struct sockaddr_in6 *srv_addr6 =
2222 (struct sockaddr_in6 *)&server->dstaddr;
2224 if (!ipv6_addr_equal(&addr6->sin6_addr,
2225 &srv_addr6->sin6_addr))
2227 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
2233 return false; /* don't expect to be here */
2236 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2243 match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2246 * The select_sectype function should either return the vol->sectype
2247 * that was specified, or "Unspecified" if that sectype was not
2248 * compatible with the given NEGOTIATE request.
2250 if (server->ops->select_sectype(server, vol->sectype)
2255 * Now check if signing mode is acceptable. No need to check
2256 * global_secflags at this point since if MUST_SIGN is set then
2257 * the server->sign had better be too.
2259 if (vol->sign && !server->sign)
2265 static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
2267 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2269 if (vol->nosharesock)
2272 /* BB update this for smb3any and default case */
2273 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2276 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2279 if (!match_address(server, addr,
2280 (struct sockaddr *)&vol->srcaddr))
2283 if (!match_port(server, addr))
2286 if (!match_security(server, vol))
2289 if (server->echo_interval != vol->echo_interval * HZ)
2292 if (server->rdma != vol->rdma)
2298 static struct TCP_Server_Info *
2299 cifs_find_tcp_session(struct smb_vol *vol)
2301 struct TCP_Server_Info *server;
2303 spin_lock(&cifs_tcp_ses_lock);
2304 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
2305 if (!match_server(server, vol))
2308 ++server->srv_count;
2309 spin_unlock(&cifs_tcp_ses_lock);
2310 cifs_dbg(FYI, "Existing tcp session with server found\n");
2313 spin_unlock(&cifs_tcp_ses_lock);
2318 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
2320 struct task_struct *task;
2322 spin_lock(&cifs_tcp_ses_lock);
2323 if (--server->srv_count > 0) {
2324 spin_unlock(&cifs_tcp_ses_lock);
2328 put_net(cifs_net_ns(server));
2330 list_del_init(&server->tcp_ses_list);
2331 spin_unlock(&cifs_tcp_ses_lock);
2333 cancel_delayed_work_sync(&server->echo);
2337 * Avoid deadlock here: reconnect work calls
2338 * cifs_put_tcp_session() at its end. Need to be sure
2339 * that reconnect work does nothing with server pointer after
2342 cancel_delayed_work(&server->reconnect);
2344 cancel_delayed_work_sync(&server->reconnect);
2346 spin_lock(&GlobalMid_Lock);
2347 server->tcpStatus = CifsExiting;
2348 spin_unlock(&GlobalMid_Lock);
2350 cifs_crypto_secmech_release(server);
2351 cifs_fscache_release_client_cookie(server);
2353 kfree(server->session_key.response);
2354 server->session_key.response = NULL;
2355 server->session_key.len = 0;
2357 task = xchg(&server->tsk, NULL);
2359 force_sig(SIGKILL, task);
2362 static struct TCP_Server_Info *
2363 cifs_get_tcp_session(struct smb_vol *volume_info)
2365 struct TCP_Server_Info *tcp_ses = NULL;
2368 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
2370 /* see if we already have a matching tcp_ses */
2371 tcp_ses = cifs_find_tcp_session(volume_info);
2375 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2381 tcp_ses->ops = volume_info->ops;
2382 tcp_ses->vals = volume_info->vals;
2383 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
2384 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2385 if (IS_ERR(tcp_ses->hostname)) {
2386 rc = PTR_ERR(tcp_ses->hostname);
2387 goto out_err_crypto_release;
2390 tcp_ses->noblocksnd = volume_info->noblocksnd;
2391 tcp_ses->noautotune = volume_info->noautotune;
2392 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
2393 tcp_ses->rdma = volume_info->rdma;
2394 tcp_ses->in_flight = 0;
2395 tcp_ses->credits = 1;
2396 init_waitqueue_head(&tcp_ses->response_q);
2397 init_waitqueue_head(&tcp_ses->request_q);
2398 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2399 mutex_init(&tcp_ses->srv_mutex);
2400 memcpy(tcp_ses->workstation_RFC1001_name,
2401 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2402 memcpy(tcp_ses->server_RFC1001_name,
2403 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2404 tcp_ses->session_estab = false;
2405 tcp_ses->sequence_number = 0;
2406 tcp_ses->reconnect_instance = 0;
2407 tcp_ses->lstrp = jiffies;
2408 spin_lock_init(&tcp_ses->req_lock);
2409 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2410 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
2411 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
2412 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2413 mutex_init(&tcp_ses->reconnect_mutex);
2414 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2415 sizeof(tcp_ses->srcaddr));
2416 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2417 sizeof(tcp_ses->dstaddr));
2418 generate_random_uuid(tcp_ses->client_guid);
2420 * at this point we are the only ones with the pointer
2421 * to the struct since the kernel thread not created yet
2422 * no need to spinlock this init of tcpStatus or srv_count
2424 tcp_ses->tcpStatus = CifsNew;
2425 ++tcp_ses->srv_count;
2427 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2428 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2429 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2431 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
2432 if (tcp_ses->rdma) {
2433 #ifndef CONFIG_CIFS_SMB_DIRECT
2434 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2436 goto out_err_crypto_release;
2438 tcp_ses->smbd_conn = smbd_get_connection(
2439 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2440 if (tcp_ses->smbd_conn) {
2441 cifs_dbg(VFS, "RDMA transport established\n");
2443 goto smbd_connected;
2446 goto out_err_crypto_release;
2449 rc = ip_connect(tcp_ses);
2451 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
2452 goto out_err_crypto_release;
2456 * since we're in a cifs function already, we know that
2457 * this will succeed. No need for try_module_get().
2459 __module_get(THIS_MODULE);
2460 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
2462 if (IS_ERR(tcp_ses->tsk)) {
2463 rc = PTR_ERR(tcp_ses->tsk);
2464 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
2465 module_put(THIS_MODULE);
2466 goto out_err_crypto_release;
2468 tcp_ses->tcpStatus = CifsNeedNegotiate;
2470 /* thread spawned, put it on the list */
2471 spin_lock(&cifs_tcp_ses_lock);
2472 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
2473 spin_unlock(&cifs_tcp_ses_lock);
2475 cifs_fscache_get_client_cookie(tcp_ses);
2477 /* queue echo request delayed work */
2478 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
2482 out_err_crypto_release:
2483 cifs_crypto_secmech_release(tcp_ses);
2485 put_net(cifs_net_ns(tcp_ses));
2489 if (!IS_ERR(tcp_ses->hostname))
2490 kfree(tcp_ses->hostname);
2491 if (tcp_ses->ssocket)
2492 sock_release(tcp_ses->ssocket);
2498 static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
2500 if (vol->sectype != Unspecified &&
2501 vol->sectype != ses->sectype)
2504 switch (ses->sectype) {
2506 if (!uid_eq(vol->cred_uid, ses->cred_uid))
2510 /* NULL username means anonymous session */
2511 if (ses->user_name == NULL) {
2517 /* anything else takes username/password */
2518 if (strncmp(ses->user_name,
2519 vol->username ? vol->username : "",
2520 CIFS_MAX_USERNAME_LEN))
2522 if ((vol->username && strlen(vol->username) != 0) &&
2523 ses->password != NULL &&
2524 strncmp(ses->password,
2525 vol->password ? vol->password : "",
2526 CIFS_MAX_PASSWORD_LEN))
2533 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2535 * A new IPC connection is made and stored in the session
2536 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2539 cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2542 struct cifs_tcon *tcon;
2543 struct nls_table *nls_codepage;
2544 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2548 * If the mount request that resulted in the creation of the
2549 * session requires encryption, force IPC to be encrypted too.
2551 if (volume_info->seal) {
2552 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2556 "IPC: server doesn't support encryption\n");
2561 tcon = tconInfoAlloc();
2565 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
2568 nls_codepage = load_nls_default();
2574 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2578 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2583 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2585 ses->tcon_ipc = tcon;
2587 unload_nls(nls_codepage);
2592 * cifs_free_ipc - helper to release the session IPC tcon
2594 * Needs to be called everytime a session is destroyed
2597 cifs_free_ipc(struct cifs_ses *ses)
2600 struct cifs_tcon *tcon = ses->tcon_ipc;
2605 if (ses->server->ops->tree_disconnect) {
2607 rc = ses->server->ops->tree_disconnect(xid, tcon);
2612 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2615 ses->tcon_ipc = NULL;
2619 static struct cifs_ses *
2620 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
2622 struct cifs_ses *ses;
2624 spin_lock(&cifs_tcp_ses_lock);
2625 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2626 if (ses->status == CifsExiting)
2628 if (!match_session(ses, vol))
2631 spin_unlock(&cifs_tcp_ses_lock);
2634 spin_unlock(&cifs_tcp_ses_lock);
2639 cifs_put_smb_ses(struct cifs_ses *ses)
2641 unsigned int rc, xid;
2642 struct TCP_Server_Info *server = ses->server;
2644 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
2646 spin_lock(&cifs_tcp_ses_lock);
2647 if (ses->status == CifsExiting) {
2648 spin_unlock(&cifs_tcp_ses_lock);
2651 if (--ses->ses_count > 0) {
2652 spin_unlock(&cifs_tcp_ses_lock);
2655 if (ses->status == CifsGood)
2656 ses->status = CifsExiting;
2657 spin_unlock(&cifs_tcp_ses_lock);
2661 if (ses->status == CifsExiting && server->ops->logoff) {
2663 rc = server->ops->logoff(xid, ses);
2665 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2670 spin_lock(&cifs_tcp_ses_lock);
2671 list_del_init(&ses->smb_ses_list);
2672 spin_unlock(&cifs_tcp_ses_lock);
2675 cifs_put_tcp_session(server, 0);
2680 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2681 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
2683 /* Populate username and pw fields from keyring if possible */
2685 cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2688 const char *delim, *payload;
2692 struct TCP_Server_Info *server = ses->server;
2693 struct sockaddr_in *sa;
2694 struct sockaddr_in6 *sa6;
2695 const struct user_key_payload *upayload;
2697 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2701 /* try to find an address key first */
2702 switch (server->dstaddr.ss_family) {
2704 sa = (struct sockaddr_in *)&server->dstaddr;
2705 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2708 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2709 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2712 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2713 server->dstaddr.ss_family);
2718 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2719 key = request_key(&key_type_logon, desc, "");
2721 if (!ses->domainName) {
2722 cifs_dbg(FYI, "domainName is NULL\n");
2727 /* didn't work, try to find a domain key */
2728 sprintf(desc, "cifs:d:%s", ses->domainName);
2729 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2730 key = request_key(&key_type_logon, desc, "");
2737 down_read(&key->sem);
2738 upayload = user_key_payload_locked(key);
2739 if (IS_ERR_OR_NULL(upayload)) {
2740 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2744 /* find first : in payload */
2745 payload = upayload->data;
2746 delim = strnchr(payload, upayload->datalen, ':');
2747 cifs_dbg(FYI, "payload=%s\n", payload);
2749 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2755 len = delim - payload;
2756 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2757 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2763 vol->username = kstrndup(payload, len, GFP_KERNEL);
2764 if (!vol->username) {
2765 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2770 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
2772 len = key->datalen - (len + 1);
2773 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2774 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2776 kfree(vol->username);
2777 vol->username = NULL;
2782 vol->password = kstrndup(delim, len, GFP_KERNEL);
2783 if (!vol->password) {
2784 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2787 kfree(vol->username);
2788 vol->username = NULL;
2797 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2800 #else /* ! CONFIG_KEYS */
2802 cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2803 struct cifs_ses *ses __attribute__((unused)))
2807 #endif /* CONFIG_KEYS */
2810 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2812 * This function assumes it is being called from cifs_mount() where we
2813 * already got a server reference (server refcount +1). See
2814 * cifs_get_tcon() for refcount explanations.
2816 static struct cifs_ses *
2817 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2821 struct cifs_ses *ses;
2822 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2823 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2827 ses = cifs_find_smb_ses(server, volume_info);
2829 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2832 mutex_lock(&ses->session_mutex);
2833 rc = cifs_negotiate_protocol(xid, ses);
2835 mutex_unlock(&ses->session_mutex);
2836 /* problem -- put our ses reference */
2837 cifs_put_smb_ses(ses);
2841 if (ses->need_reconnect) {
2842 cifs_dbg(FYI, "Session needs reconnect\n");
2843 rc = cifs_setup_session(xid, ses,
2844 volume_info->local_nls);
2846 mutex_unlock(&ses->session_mutex);
2847 /* problem -- put our reference */
2848 cifs_put_smb_ses(ses);
2853 mutex_unlock(&ses->session_mutex);
2855 /* existing SMB ses has a server reference already */
2856 cifs_put_tcp_session(server, 0);
2861 cifs_dbg(FYI, "Existing smb sess not found\n");
2862 ses = sesInfoAlloc();
2866 /* new SMB session uses our server ref */
2867 ses->server = server;
2868 if (server->dstaddr.ss_family == AF_INET6)
2869 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
2871 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
2873 if (volume_info->username) {
2874 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2875 if (!ses->user_name)
2879 /* volume_info->password freed at unmount */
2880 if (volume_info->password) {
2881 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2885 if (volume_info->domainname) {
2886 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2887 if (!ses->domainName)
2890 if (volume_info->domainauto)
2891 ses->domainAuto = volume_info->domainauto;
2892 ses->cred_uid = volume_info->cred_uid;
2893 ses->linux_uid = volume_info->linux_uid;
2895 ses->sectype = volume_info->sectype;
2896 ses->sign = volume_info->sign;
2898 mutex_lock(&ses->session_mutex);
2899 rc = cifs_negotiate_protocol(xid, ses);
2901 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
2902 mutex_unlock(&ses->session_mutex);
2906 /* success, put it on the list */
2907 spin_lock(&cifs_tcp_ses_lock);
2908 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2909 spin_unlock(&cifs_tcp_ses_lock);
2913 cifs_setup_ipc(ses, volume_info);
2923 static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
2925 if (tcon->tidStatus == CifsExiting)
2927 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
2929 if (tcon->seal != volume_info->seal)
2931 if (tcon->snapshot_time != volume_info->snapshot_time)
2936 static struct cifs_tcon *
2937 cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
2939 struct list_head *tmp;
2940 struct cifs_tcon *tcon;
2942 spin_lock(&cifs_tcp_ses_lock);
2943 list_for_each(tmp, &ses->tcon_list) {
2944 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
2945 if (!match_tcon(tcon, volume_info))
2948 spin_unlock(&cifs_tcp_ses_lock);
2951 spin_unlock(&cifs_tcp_ses_lock);
2956 cifs_put_tcon(struct cifs_tcon *tcon)
2959 struct cifs_ses *ses;
2962 * IPC tcon share the lifetime of their session and are
2963 * destroyed in the session put function
2965 if (tcon == NULL || tcon->ipc)
2969 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2970 spin_lock(&cifs_tcp_ses_lock);
2971 if (--tcon->tc_count > 0) {
2972 spin_unlock(&cifs_tcp_ses_lock);
2976 list_del_init(&tcon->tcon_list);
2977 spin_unlock(&cifs_tcp_ses_lock);
2980 if (ses->server->ops->tree_disconnect)
2981 ses->server->ops->tree_disconnect(xid, tcon);
2984 cifs_fscache_release_super_cookie(tcon);
2986 cifs_put_smb_ses(ses);
2990 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2992 * - tcon refcount is the number of mount points using the tcon.
2993 * - ses refcount is the number of tcon using the session.
2995 * 1. This function assumes it is being called from cifs_mount() where
2996 * we already got a session reference (ses refcount +1).
2998 * 2. Since we're in the context of adding a mount point, the end
2999 * result should be either:
3001 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3002 * its session refcount incremented (1 new tcon). This +1 was
3003 * already done in (1).
3005 * b) an existing tcon with refcount+1 (add a mount point to it) and
3006 * identical ses refcount (no new tcon). Because of (1) we need to
3007 * decrement the ses refcount.
3009 static struct cifs_tcon *
3010 cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
3013 struct cifs_tcon *tcon;
3015 tcon = cifs_find_tcon(ses, volume_info);
3018 * tcon has refcount already incremented but we need to
3019 * decrement extra ses reference gotten by caller (case b)
3021 cifs_dbg(FYI, "Found match on UNC path\n");
3022 cifs_put_smb_ses(ses);
3026 if (!ses->server->ops->tree_connect) {
3031 tcon = tconInfoAlloc();
3037 if (volume_info->snapshot_time) {
3038 if (ses->server->vals->protocol_id == 0) {
3040 "Use SMB2 or later for snapshot mount option\n");
3044 tcon->snapshot_time = volume_info->snapshot_time;
3048 if (volume_info->password) {
3049 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3050 if (!tcon->password) {
3056 if (volume_info->seal) {
3057 if (ses->server->vals->protocol_id == 0) {
3059 "SMB3 or later required for encryption\n");
3062 } else if (tcon->ses->server->capabilities &
3063 SMB2_GLOBAL_CAP_ENCRYPTION)
3066 cifs_dbg(VFS, "Encryption is not supported on share\n");
3072 if (volume_info->linux_ext) {
3073 if (ses->server->posix_ext_supported) {
3074 tcon->posix_extensions = true;
3075 printk_once(KERN_WARNING
3076 "SMB3.11 POSIX Extensions are experimental\n");
3078 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3085 * BB Do we need to wrap session_mutex around this TCon call and Unix
3086 * SetFS as we do on SessSetup and reconnect?
3089 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3090 volume_info->local_nls);
3092 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
3096 tcon->use_persistent = false;
3097 /* check if SMB2 or later, CIFS does not support persistent handles */
3098 if (volume_info->persistent) {
3099 if (ses->server->vals->protocol_id == 0) {
3101 "SMB3 or later required for persistent handles\n");
3104 } else if (ses->server->capabilities &
3105 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3106 tcon->use_persistent = true;
3107 else /* persistent handles requested but not supported */ {
3109 "Persistent handles not supported on share\n");
3113 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3114 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3115 && (volume_info->nopersistent == false)) {
3116 cifs_dbg(FYI, "enabling persistent handles\n");
3117 tcon->use_persistent = true;
3118 } else if (volume_info->resilient) {
3119 if (ses->server->vals->protocol_id == 0) {
3121 "SMB2.1 or later required for resilient handles\n");
3125 tcon->use_resilient = true;
3129 * We can have only one retry value for a connection to a share so for
3130 * resources mounted more than once to the same server share the last
3131 * value passed in for the retry flag is used.
3133 tcon->retry = volume_info->retry;
3134 tcon->nocase = volume_info->nocase;
3135 tcon->nohandlecache = volume_info->nohandlecache;
3136 tcon->local_lease = volume_info->local_lease;
3137 INIT_LIST_HEAD(&tcon->pending_opens);
3139 spin_lock(&cifs_tcp_ses_lock);
3140 list_add(&tcon->tcon_list, &ses->tcon_list);
3141 spin_unlock(&cifs_tcp_ses_lock);
3143 cifs_fscache_get_super_cookie(tcon);
3153 cifs_put_tlink(struct tcon_link *tlink)
3155 if (!tlink || IS_ERR(tlink))
3158 if (!atomic_dec_and_test(&tlink->tl_count) ||
3159 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3160 tlink->tl_time = jiffies;
3164 if (!IS_ERR(tlink_tcon(tlink)))
3165 cifs_put_tcon(tlink_tcon(tlink));
3171 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3173 struct cifs_sb_info *old = CIFS_SB(sb);
3174 struct cifs_sb_info *new = mnt_data->cifs_sb;
3176 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3179 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3180 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3184 * We want to share sb only if we don't specify an r/wsize or
3185 * specified r/wsize is greater than or equal to existing one.
3187 if (new->wsize && new->wsize < old->wsize)
3190 if (new->rsize && new->rsize < old->rsize)
3193 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
3196 if (old->mnt_file_mode != new->mnt_file_mode ||
3197 old->mnt_dir_mode != new->mnt_dir_mode)
3200 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3203 if (old->actimeo != new->actimeo)
3210 match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3212 struct cifs_sb_info *old = CIFS_SB(sb);
3213 struct cifs_sb_info *new = mnt_data->cifs_sb;
3214 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3215 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3217 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
3219 else if (!old_set && !new_set)
3226 cifs_match_super(struct super_block *sb, void *data)
3228 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3229 struct smb_vol *volume_info;
3230 struct cifs_sb_info *cifs_sb;
3231 struct TCP_Server_Info *tcp_srv;
3232 struct cifs_ses *ses;
3233 struct cifs_tcon *tcon;
3234 struct tcon_link *tlink;
3237 spin_lock(&cifs_tcp_ses_lock);
3238 cifs_sb = CIFS_SB(sb);
3239 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3240 if (IS_ERR(tlink)) {
3241 spin_unlock(&cifs_tcp_ses_lock);
3244 tcon = tlink_tcon(tlink);
3246 tcp_srv = ses->server;
3248 volume_info = mnt_data->vol;
3250 if (!match_server(tcp_srv, volume_info) ||
3251 !match_session(ses, volume_info) ||
3252 !match_tcon(tcon, volume_info) ||
3253 !match_prepath(sb, mnt_data)) {
3258 rc = compare_mount_options(sb, mnt_data);
3260 spin_unlock(&cifs_tcp_ses_lock);
3261 cifs_put_tlink(tlink);
3266 get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
3267 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3268 struct dfs_info3_param **referrals, int remap)
3272 if (!ses->server->ops->get_dfs_refer)
3278 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3279 referrals, num_referrals,
3280 nls_codepage, remap);
3284 #ifdef CONFIG_DEBUG_LOCK_ALLOC
3285 static struct lock_class_key cifs_key[2];
3286 static struct lock_class_key cifs_slock_key[2];
3289 cifs_reclassify_socket4(struct socket *sock)
3291 struct sock *sk = sock->sk;
3292 BUG_ON(!sock_allow_reclassification(sk));
3293 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3294 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3298 cifs_reclassify_socket6(struct socket *sock)
3300 struct sock *sk = sock->sk;
3301 BUG_ON(!sock_allow_reclassification(sk));
3302 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3303 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3307 cifs_reclassify_socket4(struct socket *sock)
3312 cifs_reclassify_socket6(struct socket *sock)
3317 /* See RFC1001 section 14 on representation of Netbios names */
3318 static void rfc1002mangle(char *target, char *source, unsigned int length)
3322 for (i = 0, j = 0; i < (length); i++) {
3323 /* mask a nibble at a time and encode */
3324 target[j] = 'A' + (0x0F & (source[i] >> 4));
3325 target[j+1] = 'A' + (0x0F & source[i]);
3332 bind_socket(struct TCP_Server_Info *server)
3335 if (server->srcaddr.ss_family != AF_UNSPEC) {
3336 /* Bind to the specified local IP address */
3337 struct socket *socket = server->ssocket;
3338 rc = socket->ops->bind(socket,
3339 (struct sockaddr *) &server->srcaddr,
3340 sizeof(server->srcaddr));
3342 struct sockaddr_in *saddr4;
3343 struct sockaddr_in6 *saddr6;
3344 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3345 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3346 if (saddr6->sin6_family == AF_INET6)
3347 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3348 &saddr6->sin6_addr, rc);
3350 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3351 &saddr4->sin_addr.s_addr, rc);
3358 ip_rfc1001_connect(struct TCP_Server_Info *server)
3362 * some servers require RFC1001 sessinit before sending
3363 * negprot - BB check reconnection in case where second
3364 * sessinit is sent but no second negprot
3366 struct rfc1002_session_packet *ses_init_buf;
3367 struct smb_hdr *smb_buf;
3368 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3371 ses_init_buf->trailer.session_req.called_len = 32;
3373 if (server->server_RFC1001_name[0] != 0)
3374 rfc1002mangle(ses_init_buf->trailer.
3375 session_req.called_name,
3376 server->server_RFC1001_name,
3377 RFC1001_NAME_LEN_WITH_NULL);
3379 rfc1002mangle(ses_init_buf->trailer.
3380 session_req.called_name,
3381 DEFAULT_CIFS_CALLED_NAME,
3382 RFC1001_NAME_LEN_WITH_NULL);
3384 ses_init_buf->trailer.session_req.calling_len = 32;
3387 * calling name ends in null (byte 16) from old smb
3390 if (server->workstation_RFC1001_name[0] != 0)
3391 rfc1002mangle(ses_init_buf->trailer.
3392 session_req.calling_name,
3393 server->workstation_RFC1001_name,
3394 RFC1001_NAME_LEN_WITH_NULL);
3396 rfc1002mangle(ses_init_buf->trailer.
3397 session_req.calling_name,
3399 RFC1001_NAME_LEN_WITH_NULL);
3401 ses_init_buf->trailer.session_req.scope1 = 0;
3402 ses_init_buf->trailer.session_req.scope2 = 0;
3403 smb_buf = (struct smb_hdr *)ses_init_buf;
3405 /* sizeof RFC1002_SESSION_REQUEST with no scope */
3406 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
3407 rc = smb_send(server, smb_buf, 0x44);
3408 kfree(ses_init_buf);
3410 * RFC1001 layer in at least one server
3411 * requires very short break before negprot
3412 * presumably because not expecting negprot
3413 * to follow so fast. This is a simple
3414 * solution that works without
3415 * complicating the code and causes no
3416 * significant slowing down on mount
3419 usleep_range(1000, 2000);
3422 * else the negprot may still work without this
3423 * even though malloc failed
3430 generic_ip_connect(struct TCP_Server_Info *server)
3435 struct socket *socket = server->ssocket;
3436 struct sockaddr *saddr;
3438 saddr = (struct sockaddr *) &server->dstaddr;
3440 if (server->dstaddr.ss_family == AF_INET6) {
3441 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3442 slen = sizeof(struct sockaddr_in6);
3445 sport = ((struct sockaddr_in *) saddr)->sin_port;
3446 slen = sizeof(struct sockaddr_in);
3450 if (socket == NULL) {
3451 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3452 IPPROTO_TCP, &socket, 1);
3454 cifs_dbg(VFS, "Error %d creating socket\n", rc);
3455 server->ssocket = NULL;
3459 /* BB other socket options to set KEEPALIVE, NODELAY? */
3460 cifs_dbg(FYI, "Socket created\n");
3461 server->ssocket = socket;
3462 socket->sk->sk_allocation = GFP_NOFS;
3463 if (sfamily == AF_INET6)
3464 cifs_reclassify_socket6(socket);
3466 cifs_reclassify_socket4(socket);
3469 rc = bind_socket(server);
3474 * Eventually check for other socket options to change from
3475 * the default. sock_setsockopt not used because it expects
3478 socket->sk->sk_rcvtimeo = 7 * HZ;
3479 socket->sk->sk_sndtimeo = 5 * HZ;
3481 /* make the bufsizes depend on wsize/rsize and max requests */
3482 if (server->noautotune) {
3483 if (socket->sk->sk_sndbuf < (200 * 1024))
3484 socket->sk->sk_sndbuf = 200 * 1024;
3485 if (socket->sk->sk_rcvbuf < (140 * 1024))
3486 socket->sk->sk_rcvbuf = 140 * 1024;
3489 if (server->tcp_nodelay) {
3491 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3492 (char *)&val, sizeof(val));
3494 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3498 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
3499 socket->sk->sk_sndbuf,
3500 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3502 rc = socket->ops->connect(socket, saddr, slen, 0);
3504 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
3505 sock_release(socket);
3506 server->ssocket = NULL;
3510 if (sport == htons(RFC1001_PORT))
3511 rc = ip_rfc1001_connect(server);
3517 ip_connect(struct TCP_Server_Info *server)
3520 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3521 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3523 if (server->dstaddr.ss_family == AF_INET6)
3524 sport = &addr6->sin6_port;
3526 sport = &addr->sin_port;
3531 /* try with 445 port at first */
3532 *sport = htons(CIFS_PORT);
3534 rc = generic_ip_connect(server);
3538 /* if it failed, try with 139 port */
3539 *sport = htons(RFC1001_PORT);
3542 return generic_ip_connect(server);
3545 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3546 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
3548 /* if we are reconnecting then should we check to see if
3549 * any requested capabilities changed locally e.g. via
3550 * remount but we can not do much about it here
3551 * if they have (even if we could detect it by the following)
3552 * Perhaps we could add a backpointer to array of sb from tcon
3553 * or if we change to make all sb to same share the same
3554 * sb as NFS - then we only have one backpointer to sb.
3555 * What if we wanted to mount the server share twice once with
3556 * and once without posixacls or posix paths? */
3557 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3559 if (vol_info && vol_info->no_linux_ext) {
3560 tcon->fsUnixInfo.Capability = 0;
3561 tcon->unix_ext = 0; /* Unix Extensions disabled */
3562 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3564 } else if (vol_info)
3565 tcon->unix_ext = 1; /* Unix Extensions supported */
3567 if (tcon->unix_ext == 0) {
3568 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3572 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3573 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3574 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3575 /* check for reconnect case in which we do not
3576 want to change the mount behavior if we can avoid it */
3577 if (vol_info == NULL) {
3578 /* turn off POSIX ACL and PATHNAMES if not set
3579 originally at mount time */
3580 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3581 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3582 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3583 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3584 cifs_dbg(VFS, "POSIXPATH support change\n");
3585 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3586 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3587 cifs_dbg(VFS, "possible reconnect error\n");
3588 cifs_dbg(VFS, "server disabled POSIX path support\n");
3592 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3593 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3595 cap &= CIFS_UNIX_CAP_MASK;
3596 if (vol_info && vol_info->no_psx_acl)
3597 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3598 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3599 cifs_dbg(FYI, "negotiated posix acl support\n");
3601 cifs_sb->mnt_cifs_flags |=
3602 CIFS_MOUNT_POSIXACL;
3605 if (vol_info && vol_info->posix_paths == 0)
3606 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3607 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3608 cifs_dbg(FYI, "negotiate posix pathnames\n");
3610 cifs_sb->mnt_cifs_flags |=
3611 CIFS_MOUNT_POSIX_PATHS;
3614 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3615 #ifdef CONFIG_CIFS_DEBUG2
3616 if (cap & CIFS_UNIX_FCNTL_CAP)
3617 cifs_dbg(FYI, "FCNTL cap\n");
3618 if (cap & CIFS_UNIX_EXTATTR_CAP)
3619 cifs_dbg(FYI, "EXTATTR cap\n");
3620 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3621 cifs_dbg(FYI, "POSIX path cap\n");
3622 if (cap & CIFS_UNIX_XATTR_CAP)
3623 cifs_dbg(FYI, "XATTR cap\n");
3624 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3625 cifs_dbg(FYI, "POSIX ACL cap\n");
3626 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3627 cifs_dbg(FYI, "very large read cap\n");
3628 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3629 cifs_dbg(FYI, "very large write cap\n");
3630 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3631 cifs_dbg(FYI, "transport encryption cap\n");
3632 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3633 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3634 #endif /* CIFS_DEBUG2 */
3635 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3636 if (vol_info == NULL) {
3637 cifs_dbg(FYI, "resetting capabilities failed\n");
3639 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");
3645 int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
3646 struct cifs_sb_info *cifs_sb)
3648 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3650 spin_lock_init(&cifs_sb->tlink_tree_lock);
3651 cifs_sb->tlink_tree = RB_ROOT;
3654 * Temporarily set r/wsize for matching superblock. If we end up using
3655 * new sb then client will later negotiate it downward if needed.
3657 cifs_sb->rsize = pvolume_info->rsize;
3658 cifs_sb->wsize = pvolume_info->wsize;
3660 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3661 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3662 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3663 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
3664 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3665 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3667 cifs_sb->actimeo = pvolume_info->actimeo;
3668 cifs_sb->local_nls = pvolume_info->local_nls;
3670 if (pvolume_info->nodfs)
3671 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
3672 if (pvolume_info->noperm)
3673 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3674 if (pvolume_info->setuids)
3675 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
3676 if (pvolume_info->setuidfromacl)
3677 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
3678 if (pvolume_info->server_ino)
3679 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3680 if (pvolume_info->remap)
3681 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3682 if (pvolume_info->sfu_remap)
3683 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3684 if (pvolume_info->no_xattr)
3685 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3686 if (pvolume_info->sfu_emul)
3687 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3688 if (pvolume_info->nobrl)
3689 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
3690 if (pvolume_info->nohandlecache)
3691 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
3692 if (pvolume_info->nostrictsync)
3693 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
3694 if (pvolume_info->mand_lock)
3695 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
3696 if (pvolume_info->rwpidforward)
3697 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3698 if (pvolume_info->cifs_acl)
3699 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3700 if (pvolume_info->backupuid_specified) {
3701 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3702 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3704 if (pvolume_info->backupgid_specified) {
3705 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3706 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3708 if (pvolume_info->override_uid)
3709 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3710 if (pvolume_info->override_gid)
3711 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3712 if (pvolume_info->dynperm)
3713 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
3714 if (pvolume_info->fsc)
3715 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
3716 if (pvolume_info->multiuser)
3717 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3718 CIFS_MOUNT_NO_PERM);
3719 if (pvolume_info->strict_io)
3720 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3721 if (pvolume_info->direct_io) {
3722 cifs_dbg(FYI, "mounting share using direct i/o\n");
3723 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3725 if (pvolume_info->mfsymlinks) {
3726 if (pvolume_info->sfu_emul) {
3728 * Our SFU ("Services for Unix" emulation does not allow
3729 * creating symlinks but does allow reading existing SFU
3730 * symlinks (it does allow both creating and reading SFU
3731 * style mknod and FIFOs though). When "mfsymlinks" and
3732 * "sfu" are both enabled at the same time, it allows
3733 * reading both types of symlinks, but will only create
3734 * them with mfsymlinks format. This allows better
3735 * Apple compatibility (probably better for Samba too)
3736 * while still recognizing old Windows style symlinks.
3738 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
3740 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
3743 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
3744 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3746 if (pvolume_info->prepath) {
3747 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3748 if (cifs_sb->prepath == NULL)
3756 cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
3758 kfree(volume_info->username);
3759 kzfree(volume_info->password);
3760 kfree(volume_info->UNC);
3761 kfree(volume_info->domainname);
3762 kfree(volume_info->iocharset);
3763 kfree(volume_info->prepath);
3767 cifs_cleanup_volume_info(struct smb_vol *volume_info)
3771 cifs_cleanup_volume_info_contents(volume_info);
3775 /* Release all succeed connections */
3776 static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
3778 struct TCP_Server_Info *server,
3779 struct cifs_ses *ses, struct cifs_tcon *tcon)
3784 cifs_put_tcon(tcon);
3786 cifs_put_smb_ses(ses);
3788 cifs_put_tcp_session(server, 0);
3789 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3793 /* Get connections for tcp, ses and tcon */
3794 static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
3796 struct TCP_Server_Info **nserver,
3797 struct cifs_ses **nses, struct cifs_tcon **ntcon)
3800 struct TCP_Server_Info *server;
3801 struct cifs_ses *ses;
3802 struct cifs_tcon *tcon;
3810 /* get a reference to a tcp session */
3811 server = cifs_get_tcp_session(vol);
3812 if (IS_ERR(server)) {
3813 rc = PTR_ERR(server);
3819 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
3820 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3822 server->max_credits = vol->max_credits;
3824 /* get a reference to a SMB session */
3825 ses = cifs_get_smb_ses(server, vol);
3833 if ((vol->persistent == true) && (!(ses->server->capabilities &
3834 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3835 cifs_dbg(VFS, "persistent handles not supported by server\n");
3839 /* search for existing tcon to this server share */
3840 tcon = cifs_get_tcon(ses, vol);
3848 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3849 if (tcon->posix_extensions)
3850 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3852 /* tell server which Unix caps we support */
3853 if (cap_unix(tcon->ses)) {
3855 * reset of caps checks mount to see if unix extensions disabled
3856 * for just this mount.
3858 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
3859 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3860 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3861 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
3864 tcon->unix_ext = 0; /* server does not support them */
3866 /* do not care if a following call succeed - informational */
3867 if (!tcon->pipe && server->ops->qfs_tcon)
3868 server->ops->qfs_tcon(*xid, tcon);
3870 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
3871 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
3876 static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3877 struct cifs_tcon *tcon)
3879 struct tcon_link *tlink;
3881 /* hang the tcon off of the superblock */
3882 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3886 tlink->tl_uid = ses->linux_uid;
3887 tlink->tl_tcon = tcon;
3888 tlink->tl_time = jiffies;
3889 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3890 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3892 cifs_sb->master_tlink = tlink;
3893 spin_lock(&cifs_sb->tlink_tree_lock);
3894 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3895 spin_unlock(&cifs_sb->tlink_tree_lock);
3897 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3902 #ifdef CONFIG_CIFS_DFS_UPCALL
3904 * cifs_build_path_to_root returns full path to root when we do not have an
3905 * exiting connection (tcon)
3908 build_unc_path_to_root(const struct smb_vol *vol,
3909 const struct cifs_sb_info *cifs_sb)
3911 char *full_path, *pos;
3912 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
3913 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
3915 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3916 if (full_path == NULL)
3917 return ERR_PTR(-ENOMEM);
3919 strncpy(full_path, vol->UNC, unc_len);
3920 pos = full_path + unc_len;
3923 *pos = CIFS_DIR_SEP(cifs_sb);
3924 strncpy(pos + 1, vol->prepath, pplen);
3928 *pos = '\0'; /* add trailing null */
3929 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3930 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3935 * Perform a dfs referral query for a share and (optionally) prefix
3937 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3938 * to a string containing updated options for the submount. Otherwise it
3939 * will be left untouched.
3941 * Returns the rc from get_dfs_path to the caller, which can be used to
3942 * determine whether there were referrals.
3945 expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
3946 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
3950 unsigned int num_referrals = 0;
3951 struct dfs_info3_param *referrals = NULL;
3952 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3954 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3957 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3958 if (IS_ERR(full_path))
3959 return PTR_ERR(full_path);
3961 /* For DFS paths, skip the first '\' of the UNC */
3962 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3964 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
3965 &num_referrals, &referrals, cifs_remap(cifs_sb));
3967 if (!rc && num_referrals > 0) {
3968 char *fake_devname = NULL;
3970 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3971 full_path + 1, referrals,
3974 free_dfs_info_array(referrals, num_referrals);
3976 if (IS_ERR(mdata)) {
3977 rc = PTR_ERR(mdata);
3980 cifs_cleanup_volume_info_contents(volume_info);
3981 rc = cifs_setup_volume_info(volume_info, mdata,
3982 fake_devname, false);
3984 kfree(fake_devname);
3985 kfree(cifs_sb->mountdata);
3986 cifs_sb->mountdata = mdata;
3994 cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3995 const char *devname, bool is_smb3)
3999 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
4002 if (volume_info->nullauth) {
4003 cifs_dbg(FYI, "Anonymous login\n");
4004 kfree(volume_info->username);
4005 volume_info->username = NULL;
4006 } else if (volume_info->username) {
4007 /* BB fixme parse for domain name here */
4008 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
4010 cifs_dbg(VFS, "No username specified\n");
4011 /* In userspace mount helper we can get user name from alternate
4012 locations such as env variables and files on disk */
4016 /* this is needed for ASCII cp to Unicode converts */
4017 if (volume_info->iocharset == NULL) {
4018 /* load_nls_default cannot return null */
4019 volume_info->local_nls = load_nls_default();
4021 volume_info->local_nls = load_nls(volume_info->iocharset);
4022 if (volume_info->local_nls == NULL) {
4023 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
4024 volume_info->iocharset);
4033 cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
4036 struct smb_vol *volume_info;
4038 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
4040 return ERR_PTR(-ENOMEM);
4042 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
4044 cifs_cleanup_volume_info(volume_info);
4045 volume_info = ERR_PTR(rc);
4052 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4054 struct cifs_tcon *tcon,
4055 struct cifs_sb_info *cifs_sb,
4062 sep = CIFS_DIR_SEP(cifs_sb);
4065 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4067 /* skip separators */
4072 /* next separator */
4073 while (*s && *s != sep)
4077 * temporarily null-terminate the path at the end of
4078 * the current component
4082 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4090 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4093 static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4094 const unsigned int xid,
4095 struct TCP_Server_Info *server,
4096 struct cifs_tcon *tcon)
4101 if (!server->ops->is_path_accessible)
4105 * cifs_build_path_to_root works only when we have a valid tcon
4107 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4108 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4109 if (full_path == NULL)
4112 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4114 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4116 if (rc != 0 && rc != -EREMOTE) {
4121 if (rc != -EREMOTE) {
4122 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
4126 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4127 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4128 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4137 #ifdef CONFIG_CIFS_DFS_UPCALL
4138 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4142 struct cifs_ses *ses;
4143 struct cifs_tcon *tcon = NULL;
4144 struct TCP_Server_Info *server;
4145 char *old_mountdata;
4148 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4150 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4156 if ((rc == -EACCES) || (rc == -EOPNOTSUPP) || (ses == NULL) || (server == NULL))
4161 * Perform an unconditional check for whether there are DFS
4162 * referrals for this path without prefix, to provide support
4163 * for DFS referrals from w2k8 servers which don't seem to respond
4164 * with PATH_NOT_COVERED to requests that include the prefix.
4165 * Chase the referral if found, otherwise continue normally.
4167 old_mountdata = cifs_sb->mountdata;
4168 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
4170 if (cifs_sb->mountdata == NULL) {
4175 if (cifs_sb->mountdata != old_mountdata) {
4176 /* If we were redirected, reconnect to new target server */
4177 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4178 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4181 if (rc == -EACCES || rc == -EOPNOTSUPP)
4185 for (count = 1; ;) {
4187 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4188 if (!rc || rc != -EREMOTE)
4192 * BB: when we implement proper loop detection,
4193 * we will remove this check. But now we need it
4194 * to prevent an indefinite loop if 'DFS tree' is
4195 * misconfigured (i.e. has loops).
4197 if (count++ > MAX_NESTED_LINKS) {
4202 old_mountdata = cifs_sb->mountdata;
4203 rc = expand_dfs_referral(xid, tcon->ses, vol, cifs_sb,
4208 if (cifs_sb->mountdata != old_mountdata) {
4209 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4210 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4214 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4224 * After reconnecting to a different server, unique ids won't
4225 * match anymore, so we disable serverino. This prevents
4226 * dentry revalidation to think the dentry are stale (ESTALE).
4228 cifs_autodisable_serverino(cifs_sb);
4231 return mount_setup_tlink(cifs_sb, ses, tcon);
4234 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4238 int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4242 struct cifs_ses *ses;
4243 struct cifs_tcon *tcon;
4244 struct TCP_Server_Info *server;
4246 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4251 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4260 return mount_setup_tlink(cifs_sb, ses, tcon);
4263 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4269 * Issue a TREE_CONNECT request.
4272 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
4273 const char *tree, struct cifs_tcon *tcon,
4274 const struct nls_table *nls_codepage)
4276 struct smb_hdr *smb_buffer;
4277 struct smb_hdr *smb_buffer_response;
4280 unsigned char *bcc_ptr;
4283 __u16 bytes_left, count;
4288 smb_buffer = cifs_buf_get();
4289 if (smb_buffer == NULL)
4292 smb_buffer_response = smb_buffer;
4294 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4295 NULL /*no tid */ , 4 /*wct */ );
4297 smb_buffer->Mid = get_next_mid(ses->server);
4298 smb_buffer->Uid = ses->Suid;
4299 pSMB = (TCONX_REQ *) smb_buffer;
4300 pSMBr = (TCONX_RSP *) smb_buffer_response;
4302 pSMB->AndXCommand = 0xFF;
4303 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
4304 bcc_ptr = &pSMB->Password[0];
4305 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
4306 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
4307 *bcc_ptr = 0; /* password is null byte */
4308 bcc_ptr++; /* skip password */
4309 /* already aligned so no need to do it below */
4311 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
4312 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4313 specified as required (when that support is added to
4314 the vfs in the future) as only NTLM or the much
4315 weaker LANMAN (which we do not send by default) is accepted
4316 by Samba (not sure whether other servers allow
4317 NTLMv2 password here) */
4318 #ifdef CONFIG_CIFS_WEAK_PW_HASH
4319 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
4320 (ses->sectype == LANMAN))
4321 calc_lanman_hash(tcon->password, ses->server->cryptkey,
4322 ses->server->sec_mode &
4323 SECMODE_PW_ENCRYPT ? true : false,
4326 #endif /* CIFS_WEAK_PW_HASH */
4327 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
4328 bcc_ptr, nls_codepage);
4330 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4332 cifs_buf_release(smb_buffer);
4336 bcc_ptr += CIFS_AUTH_RESP_SIZE;
4337 if (ses->capabilities & CAP_UNICODE) {
4338 /* must align unicode strings */
4339 *bcc_ptr = 0; /* null byte password */
4344 if (ses->server->sign)
4345 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4347 if (ses->capabilities & CAP_STATUS32) {
4348 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4350 if (ses->capabilities & CAP_DFS) {
4351 smb_buffer->Flags2 |= SMBFLG2_DFS;
4353 if (ses->capabilities & CAP_UNICODE) {
4354 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4356 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
4357 6 /* max utf8 char length in bytes */ *
4358 (/* server len*/ + 256 /* share len */), nls_codepage);
4359 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
4360 bcc_ptr += 2; /* skip trailing null */
4361 } else { /* ASCII */
4362 strcpy(bcc_ptr, tree);
4363 bcc_ptr += strlen(tree) + 1;
4365 strcpy(bcc_ptr, "?????");
4366 bcc_ptr += strlen("?????");
4368 count = bcc_ptr - &pSMB->Password[0];
4369 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4370 pSMB->hdr.smb_buf_length) + count);
4371 pSMB->ByteCount = cpu_to_le16(count);
4373 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
4376 /* above now done in SendReceive */
4380 tcon->tidStatus = CifsGood;
4381 tcon->need_reconnect = false;
4382 tcon->tid = smb_buffer_response->Tid;
4383 bcc_ptr = pByteArea(smb_buffer_response);
4384 bytes_left = get_bcc(smb_buffer_response);
4385 length = strnlen(bcc_ptr, bytes_left - 2);
4386 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4392 /* skip service field (NB: this field is always ASCII) */
4394 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4395 (bcc_ptr[2] == 'C')) {
4396 cifs_dbg(FYI, "IPC connection\n");
4400 } else if (length == 2) {
4401 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4402 /* the most common case */
4403 cifs_dbg(FYI, "disk share connection\n");
4406 bcc_ptr += length + 1;
4407 bytes_left -= (length + 1);
4408 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
4410 /* mostly informational -- no need to fail on error here */
4411 kfree(tcon->nativeFileSystem);
4412 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
4413 bytes_left, is_unicode,
4416 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
4418 if ((smb_buffer_response->WordCount == 3) ||
4419 (smb_buffer_response->WordCount == 7))
4420 /* field is in same location */
4421 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4424 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
4427 cifs_buf_release(smb_buffer);
4431 static void delayed_free(struct rcu_head *p)
4433 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4434 unload_nls(sbi->local_nls);
4439 cifs_umount(struct cifs_sb_info *cifs_sb)
4441 struct rb_root *root = &cifs_sb->tlink_tree;
4442 struct rb_node *node;
4443 struct tcon_link *tlink;
4445 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4447 spin_lock(&cifs_sb->tlink_tree_lock);
4448 while ((node = rb_first(root))) {
4449 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4450 cifs_get_tlink(tlink);
4451 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4452 rb_erase(node, root);
4454 spin_unlock(&cifs_sb->tlink_tree_lock);
4455 cifs_put_tlink(tlink);
4456 spin_lock(&cifs_sb->tlink_tree_lock);
4458 spin_unlock(&cifs_sb->tlink_tree_lock);
4460 kfree(cifs_sb->mountdata);
4461 kfree(cifs_sb->prepath);
4462 call_rcu(&cifs_sb->rcu, delayed_free);
4466 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
4469 struct TCP_Server_Info *server = ses->server;
4471 if (!server->ops->need_neg || !server->ops->negotiate)
4474 /* only send once per connect */
4475 if (!server->ops->need_neg(server))
4478 set_credits(server, 1);
4480 rc = server->ops->negotiate(xid, ses);
4482 spin_lock(&GlobalMid_Lock);
4483 if (server->tcpStatus == CifsNeedNegotiate)
4484 server->tcpStatus = CifsGood;
4487 spin_unlock(&GlobalMid_Lock);
4494 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4495 struct nls_table *nls_info)
4498 struct TCP_Server_Info *server = ses->server;
4500 ses->capabilities = server->capabilities;
4501 if (linuxExtEnabled == 0)
4502 ses->capabilities &= (~server->vals->cap_unix);
4504 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
4505 server->sec_mode, server->capabilities, server->timeAdj);
4507 if (ses->auth_key.response) {
4508 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
4509 ses->auth_key.response);
4510 kfree(ses->auth_key.response);
4511 ses->auth_key.response = NULL;
4512 ses->auth_key.len = 0;
4515 if (server->ops->sess_setup)
4516 rc = server->ops->sess_setup(xid, ses, nls_info);
4519 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
4525 cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4527 vol->sectype = ses->sectype;
4529 /* krb5 is special, since we don't need username or pw */
4530 if (vol->sectype == Kerberos)
4533 return cifs_set_cifscreds(vol, ses);
4536 static struct cifs_tcon *
4537 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
4540 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4541 struct cifs_ses *ses;
4542 struct cifs_tcon *tcon = NULL;
4543 struct smb_vol *vol_info;
4545 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
4546 if (vol_info == NULL)
4547 return ERR_PTR(-ENOMEM);
4549 vol_info->local_nls = cifs_sb->local_nls;
4550 vol_info->linux_uid = fsuid;
4551 vol_info->cred_uid = fsuid;
4552 vol_info->UNC = master_tcon->treeName;
4553 vol_info->retry = master_tcon->retry;
4554 vol_info->nocase = master_tcon->nocase;
4555 vol_info->nohandlecache = master_tcon->nohandlecache;
4556 vol_info->local_lease = master_tcon->local_lease;
4557 vol_info->no_linux_ext = !master_tcon->unix_ext;
4558 vol_info->sectype = master_tcon->ses->sectype;
4559 vol_info->sign = master_tcon->ses->sign;
4561 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4567 /* get a reference for the same TCP session */
4568 spin_lock(&cifs_tcp_ses_lock);
4569 ++master_tcon->ses->server->srv_count;
4570 spin_unlock(&cifs_tcp_ses_lock);
4572 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4574 tcon = (struct cifs_tcon *)ses;
4575 cifs_put_tcp_session(master_tcon->ses->server, 0);
4579 tcon = cifs_get_tcon(ses, vol_info);
4581 cifs_put_smb_ses(ses);
4585 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4586 if (tcon->posix_extensions)
4587 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4590 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4593 kfree(vol_info->username);
4594 kzfree(vol_info->password);
4601 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4603 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4606 /* find and return a tlink with given uid */
4607 static struct tcon_link *
4608 tlink_rb_search(struct rb_root *root, kuid_t uid)
4610 struct rb_node *node = root->rb_node;
4611 struct tcon_link *tlink;
4614 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4616 if (uid_gt(tlink->tl_uid, uid))
4617 node = node->rb_left;
4618 else if (uid_lt(tlink->tl_uid, uid))
4619 node = node->rb_right;
4626 /* insert a tcon_link into the tree */
4628 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4630 struct rb_node **new = &(root->rb_node), *parent = NULL;
4631 struct tcon_link *tlink;
4634 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4637 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4638 new = &((*new)->rb_left);
4640 new = &((*new)->rb_right);
4643 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4644 rb_insert_color(&new_tlink->tl_rbnode, root);
4648 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4651 * If the superblock doesn't refer to a multiuser mount, then just return
4652 * the master tcon for the mount.
4654 * First, search the rbtree for an existing tcon for this fsuid. If one
4655 * exists, then check to see if it's pending construction. If it is then wait
4656 * for construction to complete. Once it's no longer pending, check to see if
4657 * it failed and either return an error or retry construction, depending on
4660 * If one doesn't exist then insert a new tcon_link struct into the tree and
4661 * try to construct a new one.
4664 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4667 kuid_t fsuid = current_fsuid();
4668 struct tcon_link *tlink, *newtlink;
4670 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4671 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4673 spin_lock(&cifs_sb->tlink_tree_lock);
4674 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4676 cifs_get_tlink(tlink);
4677 spin_unlock(&cifs_sb->tlink_tree_lock);
4679 if (tlink == NULL) {
4680 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4681 if (newtlink == NULL)
4682 return ERR_PTR(-ENOMEM);
4683 newtlink->tl_uid = fsuid;
4684 newtlink->tl_tcon = ERR_PTR(-EACCES);
4685 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4686 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4687 cifs_get_tlink(newtlink);
4689 spin_lock(&cifs_sb->tlink_tree_lock);
4690 /* was one inserted after previous search? */
4691 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4693 cifs_get_tlink(tlink);
4694 spin_unlock(&cifs_sb->tlink_tree_lock);
4696 goto wait_for_construction;
4699 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4700 spin_unlock(&cifs_sb->tlink_tree_lock);
4702 wait_for_construction:
4703 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4704 TASK_INTERRUPTIBLE);
4706 cifs_put_tlink(tlink);
4707 return ERR_PTR(-ERESTARTSYS);
4710 /* if it's good, return it */
4711 if (!IS_ERR(tlink->tl_tcon))
4714 /* return error if we tried this already recently */
4715 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4716 cifs_put_tlink(tlink);
4717 return ERR_PTR(-EACCES);
4720 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4721 goto wait_for_construction;
4724 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4725 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4726 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4728 if (IS_ERR(tlink->tl_tcon)) {
4729 cifs_put_tlink(tlink);
4730 return ERR_PTR(-EACCES);
4737 * periodic workqueue job that scans tcon_tree for a superblock and closes
4741 cifs_prune_tlinks(struct work_struct *work)
4743 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4745 struct rb_root *root = &cifs_sb->tlink_tree;
4746 struct rb_node *node;
4747 struct rb_node *tmp;
4748 struct tcon_link *tlink;
4751 * Because we drop the spinlock in the loop in order to put the tlink
4752 * it's not guarded against removal of links from the tree. The only
4753 * places that remove entries from the tree are this function and
4754 * umounts. Because this function is non-reentrant and is canceled
4755 * before umount can proceed, this is safe.
4757 spin_lock(&cifs_sb->tlink_tree_lock);
4758 node = rb_first(root);
4759 while (node != NULL) {
4761 node = rb_next(tmp);
4762 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4764 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4765 atomic_read(&tlink->tl_count) != 0 ||
4766 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4769 cifs_get_tlink(tlink);
4770 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4771 rb_erase(tmp, root);
4773 spin_unlock(&cifs_sb->tlink_tree_lock);
4774 cifs_put_tlink(tlink);
4775 spin_lock(&cifs_sb->tlink_tree_lock);
4777 spin_unlock(&cifs_sb->tlink_tree_lock);
4779 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,