USB: serial: option: add support for D-Link DWM-157 C1
[linux/fpc-iii.git] / fs / cifs / connect.c
blob53a827c6d8b10b4417b8d7a0016eeedb447a43bd
1 /*
2 * fs/cifs/connect.c
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
21 #include <linux/fs.h>
22 #include <linux/net.h>
23 #include <linux/string.h>
24 #include <linux/list.h>
25 #include <linux/wait.h>
26 #include <linux/slab.h>
27 #include <linux/pagemap.h>
28 #include <linux/ctype.h>
29 #include <linux/utsname.h>
30 #include <linux/mempool.h>
31 #include <linux/delay.h>
32 #include <linux/completion.h>
33 #include <linux/kthread.h>
34 #include <linux/pagevec.h>
35 #include <linux/freezer.h>
36 #include <linux/namei.h>
37 #include <asm/uaccess.h>
38 #include <asm/processor.h>
39 #include <linux/inet.h>
40 #include <linux/module.h>
41 #include <keys/user-type.h>
42 #include <net/ipv6.h>
43 #include <linux/parser.h>
45 #include "cifspdu.h"
46 #include "cifsglob.h"
47 #include "cifsproto.h"
48 #include "cifs_unicode.h"
49 #include "cifs_debug.h"
50 #include "cifs_fs_sb.h"
51 #include "ntlmssp.h"
52 #include "nterr.h"
53 #include "rfc1002pdu.h"
54 #include "fscache.h"
55 #ifdef CONFIG_CIFS_SMB2
56 #include "smb2proto.h"
57 #endif
59 #define CIFS_PORT 445
60 #define RFC1001_PORT 139
62 extern mempool_t *cifs_req_poolp;
64 /* FIXME: should these be tunable? */
65 #define TLINK_ERROR_EXPIRE (1 * HZ)
66 #define TLINK_IDLE_EXPIRE (600 * HZ)
68 enum {
70 /* Mount options that take no arguments */
71 Opt_user_xattr, Opt_nouser_xattr,
72 Opt_forceuid, Opt_noforceuid,
73 Opt_forcegid, Opt_noforcegid,
74 Opt_noblocksend, Opt_noautotune,
75 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
76 Opt_mapposix, Opt_nomapposix,
77 Opt_mapchars, Opt_nomapchars, Opt_sfu,
78 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
79 Opt_noposixpaths, Opt_nounix,
80 Opt_nocase,
81 Opt_brl, Opt_nobrl,
82 Opt_forcemandatorylock, Opt_setuids,
83 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
84 Opt_nohard, Opt_nosoft,
85 Opt_nointr, Opt_intr,
86 Opt_nostrictsync, Opt_strictsync,
87 Opt_serverino, Opt_noserverino,
88 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
89 Opt_acl, Opt_noacl, Opt_locallease,
90 Opt_sign, Opt_seal, Opt_noac,
91 Opt_fsc, Opt_mfsymlinks,
92 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
93 Opt_persistent, Opt_nopersistent,
94 Opt_resilient, Opt_noresilient,
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,
102 /* Mount options which take string value */
103 Opt_user, Opt_pass, Opt_ip,
104 Opt_domain, Opt_srcaddr, Opt_iocharset,
105 Opt_netbiosname, Opt_servern,
106 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
108 /* Mount options to be ignored */
109 Opt_ignore,
111 /* Options which could be blank */
112 Opt_blank_pass,
113 Opt_blank_user,
114 Opt_blank_ip,
116 Opt_err
119 static const match_table_t cifs_mount_option_tokens = {
121 { Opt_user_xattr, "user_xattr" },
122 { Opt_nouser_xattr, "nouser_xattr" },
123 { Opt_forceuid, "forceuid" },
124 { Opt_noforceuid, "noforceuid" },
125 { Opt_forcegid, "forcegid" },
126 { Opt_noforcegid, "noforcegid" },
127 { Opt_noblocksend, "noblocksend" },
128 { Opt_noautotune, "noautotune" },
129 { Opt_hard, "hard" },
130 { Opt_soft, "soft" },
131 { Opt_perm, "perm" },
132 { Opt_noperm, "noperm" },
133 { Opt_mapchars, "mapchars" }, /* SFU style */
134 { Opt_nomapchars, "nomapchars" },
135 { Opt_mapposix, "mapposix" }, /* SFM style */
136 { Opt_nomapposix, "nomapposix" },
137 { Opt_sfu, "sfu" },
138 { Opt_nosfu, "nosfu" },
139 { Opt_nodfs, "nodfs" },
140 { Opt_posixpaths, "posixpaths" },
141 { Opt_noposixpaths, "noposixpaths" },
142 { Opt_nounix, "nounix" },
143 { Opt_nounix, "nolinux" },
144 { Opt_nocase, "nocase" },
145 { Opt_nocase, "ignorecase" },
146 { Opt_brl, "brl" },
147 { Opt_nobrl, "nobrl" },
148 { Opt_nobrl, "nolock" },
149 { Opt_forcemandatorylock, "forcemandatorylock" },
150 { Opt_forcemandatorylock, "forcemand" },
151 { Opt_setuids, "setuids" },
152 { Opt_nosetuids, "nosetuids" },
153 { Opt_dynperm, "dynperm" },
154 { Opt_nodynperm, "nodynperm" },
155 { Opt_nohard, "nohard" },
156 { Opt_nosoft, "nosoft" },
157 { Opt_nointr, "nointr" },
158 { Opt_intr, "intr" },
159 { Opt_nostrictsync, "nostrictsync" },
160 { Opt_strictsync, "strictsync" },
161 { Opt_serverino, "serverino" },
162 { Opt_noserverino, "noserverino" },
163 { Opt_rwpidforward, "rwpidforward" },
164 { Opt_cifsacl, "cifsacl" },
165 { Opt_nocifsacl, "nocifsacl" },
166 { Opt_acl, "acl" },
167 { Opt_noacl, "noacl" },
168 { Opt_locallease, "locallease" },
169 { Opt_sign, "sign" },
170 { Opt_seal, "seal" },
171 { Opt_noac, "noac" },
172 { Opt_fsc, "fsc" },
173 { Opt_mfsymlinks, "mfsymlinks" },
174 { Opt_multiuser, "multiuser" },
175 { Opt_sloppy, "sloppy" },
176 { Opt_nosharesock, "nosharesock" },
177 { Opt_persistent, "persistenthandles"},
178 { Opt_nopersistent, "nopersistenthandles"},
179 { Opt_resilient, "resilienthandles"},
180 { Opt_noresilient, "noresilienthandles"},
182 { Opt_backupuid, "backupuid=%s" },
183 { Opt_backupgid, "backupgid=%s" },
184 { Opt_uid, "uid=%s" },
185 { Opt_cruid, "cruid=%s" },
186 { Opt_gid, "gid=%s" },
187 { Opt_file_mode, "file_mode=%s" },
188 { Opt_dirmode, "dirmode=%s" },
189 { Opt_dirmode, "dir_mode=%s" },
190 { Opt_port, "port=%s" },
191 { Opt_rsize, "rsize=%s" },
192 { Opt_wsize, "wsize=%s" },
193 { Opt_actimeo, "actimeo=%s" },
195 { Opt_blank_user, "user=" },
196 { Opt_blank_user, "username=" },
197 { Opt_user, "user=%s" },
198 { Opt_user, "username=%s" },
199 { Opt_blank_pass, "pass=" },
200 { Opt_blank_pass, "password=" },
201 { Opt_pass, "pass=%s" },
202 { Opt_pass, "password=%s" },
203 { Opt_blank_ip, "ip=" },
204 { Opt_blank_ip, "addr=" },
205 { Opt_ip, "ip=%s" },
206 { Opt_ip, "addr=%s" },
207 { Opt_ignore, "unc=%s" },
208 { Opt_ignore, "target=%s" },
209 { Opt_ignore, "path=%s" },
210 { Opt_domain, "dom=%s" },
211 { Opt_domain, "domain=%s" },
212 { Opt_domain, "workgroup=%s" },
213 { Opt_srcaddr, "srcaddr=%s" },
214 { Opt_ignore, "prefixpath=%s" },
215 { Opt_iocharset, "iocharset=%s" },
216 { Opt_netbiosname, "netbiosname=%s" },
217 { Opt_servern, "servern=%s" },
218 { Opt_ver, "ver=%s" },
219 { Opt_vers, "vers=%s" },
220 { Opt_sec, "sec=%s" },
221 { Opt_cache, "cache=%s" },
223 { Opt_ignore, "cred" },
224 { Opt_ignore, "credentials" },
225 { Opt_ignore, "cred=%s" },
226 { Opt_ignore, "credentials=%s" },
227 { Opt_ignore, "guest" },
228 { Opt_ignore, "rw" },
229 { Opt_ignore, "ro" },
230 { Opt_ignore, "suid" },
231 { Opt_ignore, "nosuid" },
232 { Opt_ignore, "exec" },
233 { Opt_ignore, "noexec" },
234 { Opt_ignore, "nodev" },
235 { Opt_ignore, "noauto" },
236 { Opt_ignore, "dev" },
237 { Opt_ignore, "mand" },
238 { Opt_ignore, "nomand" },
239 { Opt_ignore, "_netdev" },
241 { Opt_err, NULL }
244 enum {
245 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
246 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
247 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
248 Opt_sec_ntlmv2i, Opt_sec_lanman,
249 Opt_sec_none,
251 Opt_sec_err
254 static const match_table_t cifs_secflavor_tokens = {
255 { Opt_sec_krb5, "krb5" },
256 { Opt_sec_krb5i, "krb5i" },
257 { Opt_sec_krb5p, "krb5p" },
258 { Opt_sec_ntlmsspi, "ntlmsspi" },
259 { Opt_sec_ntlmssp, "ntlmssp" },
260 { Opt_ntlm, "ntlm" },
261 { Opt_sec_ntlmi, "ntlmi" },
262 { Opt_sec_ntlmv2, "nontlm" },
263 { Opt_sec_ntlmv2, "ntlmv2" },
264 { Opt_sec_ntlmv2i, "ntlmv2i" },
265 { Opt_sec_lanman, "lanman" },
266 { Opt_sec_none, "none" },
268 { Opt_sec_err, NULL }
271 /* cache flavors */
272 enum {
273 Opt_cache_loose,
274 Opt_cache_strict,
275 Opt_cache_none,
276 Opt_cache_err
279 static const match_table_t cifs_cacheflavor_tokens = {
280 { Opt_cache_loose, "loose" },
281 { Opt_cache_strict, "strict" },
282 { Opt_cache_none, "none" },
283 { Opt_cache_err, NULL }
286 static const match_table_t cifs_smb_version_tokens = {
287 { Smb_1, SMB1_VERSION_STRING },
288 { Smb_20, SMB20_VERSION_STRING},
289 { Smb_21, SMB21_VERSION_STRING },
290 { Smb_30, SMB30_VERSION_STRING },
291 { Smb_302, SMB302_VERSION_STRING },
292 #ifdef CONFIG_CIFS_SMB311
293 { Smb_311, SMB311_VERSION_STRING },
294 { Smb_311, ALT_SMB311_VERSION_STRING },
295 #endif /* SMB311 */
296 { Smb_version_err, NULL }
299 static int ip_connect(struct TCP_Server_Info *server);
300 static int generic_ip_connect(struct TCP_Server_Info *server);
301 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
302 static void cifs_prune_tlinks(struct work_struct *work);
303 static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
304 const char *devname);
307 * cifs tcp session reconnection
309 * mark tcp session as reconnecting so temporarily locked
310 * mark all smb sessions as reconnecting for tcp session
311 * reconnect tcp session
312 * wake up waiters on reconnection? - (not needed currently)
315 cifs_reconnect(struct TCP_Server_Info *server)
317 int rc = 0;
318 struct list_head *tmp, *tmp2;
319 struct cifs_ses *ses;
320 struct cifs_tcon *tcon;
321 struct mid_q_entry *mid_entry;
322 struct list_head retry_list;
324 spin_lock(&GlobalMid_Lock);
325 if (server->tcpStatus == CifsExiting) {
326 /* the demux thread will exit normally
327 next time through the loop */
328 spin_unlock(&GlobalMid_Lock);
329 return rc;
330 } else
331 server->tcpStatus = CifsNeedReconnect;
332 spin_unlock(&GlobalMid_Lock);
333 server->maxBuf = 0;
334 #ifdef CONFIG_CIFS_SMB2
335 server->max_read = 0;
336 #endif
338 cifs_dbg(FYI, "Reconnecting tcp session\n");
340 /* before reconnecting the tcp session, mark the smb session (uid)
341 and the tid bad so they are not used until reconnected */
342 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
343 __func__);
344 spin_lock(&cifs_tcp_ses_lock);
345 list_for_each(tmp, &server->smb_ses_list) {
346 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
347 ses->need_reconnect = true;
348 ses->ipc_tid = 0;
349 list_for_each(tmp2, &ses->tcon_list) {
350 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
351 tcon->need_reconnect = true;
354 spin_unlock(&cifs_tcp_ses_lock);
356 /* do not want to be sending data on a socket we are freeing */
357 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
358 mutex_lock(&server->srv_mutex);
359 if (server->ssocket) {
360 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
361 server->ssocket->state, server->ssocket->flags);
362 kernel_sock_shutdown(server->ssocket, SHUT_WR);
363 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
364 server->ssocket->state, server->ssocket->flags);
365 sock_release(server->ssocket);
366 server->ssocket = NULL;
368 server->sequence_number = 0;
369 server->session_estab = false;
370 kfree(server->session_key.response);
371 server->session_key.response = NULL;
372 server->session_key.len = 0;
373 server->lstrp = jiffies;
375 /* mark submitted MIDs for retry and issue callback */
376 INIT_LIST_HEAD(&retry_list);
377 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
378 spin_lock(&GlobalMid_Lock);
379 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
380 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
381 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
382 mid_entry->mid_state = MID_RETRY_NEEDED;
383 list_move(&mid_entry->qhead, &retry_list);
385 spin_unlock(&GlobalMid_Lock);
386 mutex_unlock(&server->srv_mutex);
388 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
389 list_for_each_safe(tmp, tmp2, &retry_list) {
390 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
391 list_del_init(&mid_entry->qhead);
392 mid_entry->callback(mid_entry);
395 do {
396 try_to_freeze();
398 /* we should try only the port we connected to before */
399 mutex_lock(&server->srv_mutex);
400 rc = generic_ip_connect(server);
401 if (rc) {
402 cifs_dbg(FYI, "reconnect error %d\n", rc);
403 mutex_unlock(&server->srv_mutex);
404 msleep(3000);
405 } else {
406 atomic_inc(&tcpSesReconnectCount);
407 spin_lock(&GlobalMid_Lock);
408 if (server->tcpStatus != CifsExiting)
409 server->tcpStatus = CifsNeedNegotiate;
410 spin_unlock(&GlobalMid_Lock);
411 mutex_unlock(&server->srv_mutex);
413 } while (server->tcpStatus == CifsNeedReconnect);
415 if (server->tcpStatus == CifsNeedNegotiate)
416 mod_delayed_work(cifsiod_wq, &server->echo, 0);
418 return rc;
421 static void
422 cifs_echo_request(struct work_struct *work)
424 int rc;
425 struct TCP_Server_Info *server = container_of(work,
426 struct TCP_Server_Info, echo.work);
427 unsigned long echo_interval;
430 * If we need to renegotiate, set echo interval to zero to
431 * immediately call echo service where we can renegotiate.
433 if (server->tcpStatus == CifsNeedNegotiate)
434 echo_interval = 0;
435 else
436 echo_interval = SMB_ECHO_INTERVAL;
439 * We cannot send an echo if it is disabled.
440 * Also, no need to ping if we got a response recently.
443 if (server->tcpStatus == CifsNeedReconnect ||
444 server->tcpStatus == CifsExiting ||
445 server->tcpStatus == CifsNew ||
446 (server->ops->can_echo && !server->ops->can_echo(server)) ||
447 time_before(jiffies, server->lstrp + echo_interval - HZ))
448 goto requeue_echo;
450 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
451 if (rc)
452 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
453 server->hostname);
455 requeue_echo:
456 queue_delayed_work(cifsiod_wq, &server->echo, SMB_ECHO_INTERVAL);
459 static bool
460 allocate_buffers(struct TCP_Server_Info *server)
462 if (!server->bigbuf) {
463 server->bigbuf = (char *)cifs_buf_get();
464 if (!server->bigbuf) {
465 cifs_dbg(VFS, "No memory for large SMB response\n");
466 msleep(3000);
467 /* retry will check if exiting */
468 return false;
470 } else if (server->large_buf) {
471 /* we are reusing a dirty large buf, clear its start */
472 memset(server->bigbuf, 0, HEADER_SIZE(server));
475 if (!server->smallbuf) {
476 server->smallbuf = (char *)cifs_small_buf_get();
477 if (!server->smallbuf) {
478 cifs_dbg(VFS, "No memory for SMB response\n");
479 msleep(1000);
480 /* retry will check if exiting */
481 return false;
483 /* beginning of smb buffer is cleared in our buf_get */
484 } else {
485 /* if existing small buf clear beginning */
486 memset(server->smallbuf, 0, HEADER_SIZE(server));
489 return true;
492 static bool
493 server_unresponsive(struct TCP_Server_Info *server)
496 * We need to wait 2 echo intervals to make sure we handle such
497 * situations right:
498 * 1s client sends a normal SMB request
499 * 2s client gets a response
500 * 30s echo workqueue job pops, and decides we got a response recently
501 * and don't need to send another
502 * ...
503 * 65s kernel_recvmsg times out, and we see that we haven't gotten
504 * a response in >60s.
506 if (server->tcpStatus == CifsGood &&
507 time_after(jiffies, server->lstrp + 2 * SMB_ECHO_INTERVAL)) {
508 cifs_dbg(VFS, "Server %s has not responded in %d seconds. Reconnecting...\n",
509 server->hostname, (2 * SMB_ECHO_INTERVAL) / HZ);
510 cifs_reconnect(server);
511 wake_up(&server->response_q);
512 return true;
515 return false;
519 * kvec_array_init - clone a kvec array, and advance into it
520 * @new: pointer to memory for cloned array
521 * @iov: pointer to original array
522 * @nr_segs: number of members in original array
523 * @bytes: number of bytes to advance into the cloned array
525 * This function will copy the array provided in iov to a section of memory
526 * and advance the specified number of bytes into the new array. It returns
527 * the number of segments in the new array. "new" must be at least as big as
528 * the original iov array.
530 static unsigned int
531 kvec_array_init(struct kvec *new, struct kvec *iov, unsigned int nr_segs,
532 size_t bytes)
534 size_t base = 0;
536 while (bytes || !iov->iov_len) {
537 int copy = min(bytes, iov->iov_len);
539 bytes -= copy;
540 base += copy;
541 if (iov->iov_len == base) {
542 iov++;
543 nr_segs--;
544 base = 0;
547 memcpy(new, iov, sizeof(*iov) * nr_segs);
548 new->iov_base += base;
549 new->iov_len -= base;
550 return nr_segs;
553 static struct kvec *
554 get_server_iovec(struct TCP_Server_Info *server, unsigned int nr_segs)
556 struct kvec *new_iov;
558 if (server->iov && nr_segs <= server->nr_iov)
559 return server->iov;
561 /* not big enough -- allocate a new one and release the old */
562 new_iov = kmalloc(sizeof(*new_iov) * nr_segs, GFP_NOFS);
563 if (new_iov) {
564 kfree(server->iov);
565 server->iov = new_iov;
566 server->nr_iov = nr_segs;
568 return new_iov;
572 cifs_readv_from_socket(struct TCP_Server_Info *server, struct kvec *iov_orig,
573 unsigned int nr_segs, unsigned int to_read)
575 int length = 0;
576 int total_read;
577 unsigned int segs;
578 struct msghdr smb_msg;
579 struct kvec *iov;
581 iov = get_server_iovec(server, nr_segs);
582 if (!iov)
583 return -ENOMEM;
585 smb_msg.msg_control = NULL;
586 smb_msg.msg_controllen = 0;
588 for (total_read = 0; to_read; total_read += length, to_read -= length) {
589 try_to_freeze();
591 if (server_unresponsive(server)) {
592 total_read = -ECONNABORTED;
593 break;
596 segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
598 length = kernel_recvmsg(server->ssocket, &smb_msg,
599 iov, segs, to_read, 0);
601 if (server->tcpStatus == CifsExiting) {
602 total_read = -ESHUTDOWN;
603 break;
604 } else if (server->tcpStatus == CifsNeedReconnect) {
605 cifs_reconnect(server);
606 total_read = -ECONNABORTED;
607 break;
608 } else if (length == -ERESTARTSYS ||
609 length == -EAGAIN ||
610 length == -EINTR) {
612 * Minimum sleep to prevent looping, allowing socket
613 * to clear and app threads to set tcpStatus
614 * CifsNeedReconnect if server hung.
616 usleep_range(1000, 2000);
617 length = 0;
618 continue;
619 } else if (length <= 0) {
620 cifs_dbg(FYI, "Received no data or error: expecting %d\n"
621 "got %d", to_read, length);
622 cifs_reconnect(server);
623 total_read = -ECONNABORTED;
624 break;
627 return total_read;
631 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
632 unsigned int to_read)
634 struct kvec iov;
636 iov.iov_base = buf;
637 iov.iov_len = to_read;
639 return cifs_readv_from_socket(server, &iov, 1, to_read);
642 static bool
643 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
646 * The first byte big endian of the length field,
647 * is actually not part of the length but the type
648 * with the most common, zero, as regular data.
650 switch (type) {
651 case RFC1002_SESSION_MESSAGE:
652 /* Regular SMB response */
653 return true;
654 case RFC1002_SESSION_KEEP_ALIVE:
655 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
656 break;
657 case RFC1002_POSITIVE_SESSION_RESPONSE:
658 cifs_dbg(FYI, "RFC 1002 positive session response\n");
659 break;
660 case RFC1002_NEGATIVE_SESSION_RESPONSE:
662 * We get this from Windows 98 instead of an error on
663 * SMB negprot response.
665 cifs_dbg(FYI, "RFC 1002 negative session response\n");
666 /* give server a second to clean up */
667 msleep(1000);
669 * Always try 445 first on reconnect since we get NACK
670 * on some if we ever connected to port 139 (the NACK
671 * is since we do not begin with RFC1001 session
672 * initialize frame).
674 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
675 cifs_reconnect(server);
676 wake_up(&server->response_q);
677 break;
678 default:
679 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
680 cifs_reconnect(server);
683 return false;
686 void
687 dequeue_mid(struct mid_q_entry *mid, bool malformed)
689 #ifdef CONFIG_CIFS_STATS2
690 mid->when_received = jiffies;
691 #endif
692 spin_lock(&GlobalMid_Lock);
693 if (!malformed)
694 mid->mid_state = MID_RESPONSE_RECEIVED;
695 else
696 mid->mid_state = MID_RESPONSE_MALFORMED;
697 list_del_init(&mid->qhead);
698 spin_unlock(&GlobalMid_Lock);
701 static void
702 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
703 char *buf, int malformed)
705 if (server->ops->check_trans2 &&
706 server->ops->check_trans2(mid, server, buf, malformed))
707 return;
708 mid->resp_buf = buf;
709 mid->large_buf = server->large_buf;
710 /* Was previous buf put in mpx struct for multi-rsp? */
711 if (!mid->multiRsp) {
712 /* smb buffer will be freed by user thread */
713 if (server->large_buf)
714 server->bigbuf = NULL;
715 else
716 server->smallbuf = NULL;
718 dequeue_mid(mid, malformed);
721 static void clean_demultiplex_info(struct TCP_Server_Info *server)
723 int length;
725 /* take it off the list, if it's not already */
726 spin_lock(&cifs_tcp_ses_lock);
727 list_del_init(&server->tcp_ses_list);
728 spin_unlock(&cifs_tcp_ses_lock);
730 spin_lock(&GlobalMid_Lock);
731 server->tcpStatus = CifsExiting;
732 spin_unlock(&GlobalMid_Lock);
733 wake_up_all(&server->response_q);
735 /* check if we have blocked requests that need to free */
736 spin_lock(&server->req_lock);
737 if (server->credits <= 0)
738 server->credits = 1;
739 spin_unlock(&server->req_lock);
741 * Although there should not be any requests blocked on this queue it
742 * can not hurt to be paranoid and try to wake up requests that may
743 * haven been blocked when more than 50 at time were on the wire to the
744 * same server - they now will see the session is in exit state and get
745 * out of SendReceive.
747 wake_up_all(&server->request_q);
748 /* give those requests time to exit */
749 msleep(125);
751 if (server->ssocket) {
752 sock_release(server->ssocket);
753 server->ssocket = NULL;
756 if (!list_empty(&server->pending_mid_q)) {
757 struct list_head dispose_list;
758 struct mid_q_entry *mid_entry;
759 struct list_head *tmp, *tmp2;
761 INIT_LIST_HEAD(&dispose_list);
762 spin_lock(&GlobalMid_Lock);
763 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
764 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
765 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
766 mid_entry->mid_state = MID_SHUTDOWN;
767 list_move(&mid_entry->qhead, &dispose_list);
769 spin_unlock(&GlobalMid_Lock);
771 /* now walk dispose list and issue callbacks */
772 list_for_each_safe(tmp, tmp2, &dispose_list) {
773 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
774 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
775 list_del_init(&mid_entry->qhead);
776 mid_entry->callback(mid_entry);
778 /* 1/8th of sec is more than enough time for them to exit */
779 msleep(125);
782 if (!list_empty(&server->pending_mid_q)) {
784 * mpx threads have not exited yet give them at least the smb
785 * send timeout time for long ops.
787 * Due to delays on oplock break requests, we need to wait at
788 * least 45 seconds before giving up on a request getting a
789 * response and going ahead and killing cifsd.
791 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
792 msleep(46000);
794 * If threads still have not exited they are probably never
795 * coming home not much else we can do but free the memory.
799 kfree(server->hostname);
800 kfree(server->iov);
801 kfree(server);
803 length = atomic_dec_return(&tcpSesAllocCount);
804 if (length > 0)
805 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
808 static int
809 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
811 int length;
812 char *buf = server->smallbuf;
813 unsigned int pdu_length = get_rfc1002_length(buf);
815 /* make sure this will fit in a large buffer */
816 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
817 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
818 cifs_reconnect(server);
819 wake_up(&server->response_q);
820 return -ECONNABORTED;
823 /* switch to large buffer if too big for a small one */
824 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
825 server->large_buf = true;
826 memcpy(server->bigbuf, buf, server->total_read);
827 buf = server->bigbuf;
830 /* now read the rest */
831 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
832 pdu_length - HEADER_SIZE(server) + 1 + 4);
833 if (length < 0)
834 return length;
835 server->total_read += length;
837 dump_smb(buf, server->total_read);
840 * We know that we received enough to get to the MID as we
841 * checked the pdu_length earlier. Now check to see
842 * if the rest of the header is OK. We borrow the length
843 * var for the rest of the loop to avoid a new stack var.
845 * 48 bytes is enough to display the header and a little bit
846 * into the payload for debugging purposes.
848 length = server->ops->check_message(buf, server->total_read);
849 if (length != 0)
850 cifs_dump_mem("Bad SMB: ", buf,
851 min_t(unsigned int, server->total_read, 48));
853 if (server->ops->is_status_pending &&
854 server->ops->is_status_pending(buf, server, length))
855 return -1;
857 if (!mid)
858 return length;
860 handle_mid(mid, server, buf, length);
861 return 0;
864 static int
865 cifs_demultiplex_thread(void *p)
867 int length;
868 struct TCP_Server_Info *server = p;
869 unsigned int pdu_length;
870 char *buf = NULL;
871 struct task_struct *task_to_wake = NULL;
872 struct mid_q_entry *mid_entry;
874 current->flags |= PF_MEMALLOC;
875 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
877 length = atomic_inc_return(&tcpSesAllocCount);
878 if (length > 1)
879 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
881 set_freezable();
882 while (server->tcpStatus != CifsExiting) {
883 if (try_to_freeze())
884 continue;
886 if (!allocate_buffers(server))
887 continue;
889 server->large_buf = false;
890 buf = server->smallbuf;
891 pdu_length = 4; /* enough to get RFC1001 header */
893 length = cifs_read_from_socket(server, buf, pdu_length);
894 if (length < 0)
895 continue;
896 server->total_read = length;
899 * The right amount was read from socket - 4 bytes,
900 * so we can now interpret the length field.
902 pdu_length = get_rfc1002_length(buf);
904 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
905 if (!is_smb_response(server, buf[0]))
906 continue;
908 /* make sure we have enough to get to the MID */
909 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
910 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
911 pdu_length);
912 cifs_reconnect(server);
913 wake_up(&server->response_q);
914 continue;
917 /* read down to the MID */
918 length = cifs_read_from_socket(server, buf + 4,
919 HEADER_SIZE(server) - 1 - 4);
920 if (length < 0)
921 continue;
922 server->total_read += length;
924 mid_entry = server->ops->find_mid(server, buf);
926 if (!mid_entry || !mid_entry->receive)
927 length = standard_receive3(server, mid_entry);
928 else
929 length = mid_entry->receive(server, mid_entry);
931 if (length < 0)
932 continue;
934 if (server->large_buf)
935 buf = server->bigbuf;
937 server->lstrp = jiffies;
938 if (mid_entry != NULL) {
939 if ((mid_entry->mid_flags & MID_WAIT_CANCELLED) &&
940 mid_entry->mid_state == MID_RESPONSE_RECEIVED &&
941 server->ops->handle_cancelled_mid)
942 server->ops->handle_cancelled_mid(
943 mid_entry->resp_buf,
944 server);
946 if (!mid_entry->multiRsp || mid_entry->multiEnd)
947 mid_entry->callback(mid_entry);
948 } else if (server->ops->is_oplock_break &&
949 server->ops->is_oplock_break(buf, server)) {
950 cifs_dbg(FYI, "Received oplock break\n");
951 } else {
952 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
953 atomic_read(&midCount));
954 cifs_dump_mem("Received Data is: ", buf,
955 HEADER_SIZE(server));
956 #ifdef CONFIG_CIFS_DEBUG2
957 if (server->ops->dump_detail)
958 server->ops->dump_detail(buf);
959 cifs_dump_mids(server);
960 #endif /* CIFS_DEBUG2 */
963 } /* end while !EXITING */
965 /* buffer usually freed in free_mid - need to free it here on exit */
966 cifs_buf_release(server->bigbuf);
967 if (server->smallbuf) /* no sense logging a debug message if NULL */
968 cifs_small_buf_release(server->smallbuf);
970 task_to_wake = xchg(&server->tsk, NULL);
971 clean_demultiplex_info(server);
973 /* if server->tsk was NULL then wait for a signal before exiting */
974 if (!task_to_wake) {
975 set_current_state(TASK_INTERRUPTIBLE);
976 while (!signal_pending(current)) {
977 schedule();
978 set_current_state(TASK_INTERRUPTIBLE);
980 set_current_state(TASK_RUNNING);
983 module_put_and_exit(0);
986 /* extract the host portion of the UNC string */
987 static char *
988 extract_hostname(const char *unc)
990 const char *src;
991 char *dst, *delim;
992 unsigned int len;
994 /* skip double chars at beginning of string */
995 /* BB: check validity of these bytes? */
996 src = unc + 2;
998 /* delimiter between hostname and sharename is always '\\' now */
999 delim = strchr(src, '\\');
1000 if (!delim)
1001 return ERR_PTR(-EINVAL);
1003 len = delim - src;
1004 dst = kmalloc((len + 1), GFP_KERNEL);
1005 if (dst == NULL)
1006 return ERR_PTR(-ENOMEM);
1008 memcpy(dst, src, len);
1009 dst[len] = '\0';
1011 return dst;
1014 static int get_option_ul(substring_t args[], unsigned long *option)
1016 int rc;
1017 char *string;
1019 string = match_strdup(args);
1020 if (string == NULL)
1021 return -ENOMEM;
1022 rc = kstrtoul(string, 0, option);
1023 kfree(string);
1025 return rc;
1028 static int get_option_uid(substring_t args[], kuid_t *result)
1030 unsigned long value;
1031 kuid_t uid;
1032 int rc;
1034 rc = get_option_ul(args, &value);
1035 if (rc)
1036 return rc;
1038 uid = make_kuid(current_user_ns(), value);
1039 if (!uid_valid(uid))
1040 return -EINVAL;
1042 *result = uid;
1043 return 0;
1046 static int get_option_gid(substring_t args[], kgid_t *result)
1048 unsigned long value;
1049 kgid_t gid;
1050 int rc;
1052 rc = get_option_ul(args, &value);
1053 if (rc)
1054 return rc;
1056 gid = make_kgid(current_user_ns(), value);
1057 if (!gid_valid(gid))
1058 return -EINVAL;
1060 *result = gid;
1061 return 0;
1064 static int cifs_parse_security_flavors(char *value,
1065 struct smb_vol *vol)
1068 substring_t args[MAX_OPT_ARGS];
1071 * With mount options, the last one should win. Reset any existing
1072 * settings back to default.
1074 vol->sectype = Unspecified;
1075 vol->sign = false;
1077 switch (match_token(value, cifs_secflavor_tokens, args)) {
1078 case Opt_sec_krb5p:
1079 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1080 return 1;
1081 case Opt_sec_krb5i:
1082 vol->sign = true;
1083 /* Fallthrough */
1084 case Opt_sec_krb5:
1085 vol->sectype = Kerberos;
1086 break;
1087 case Opt_sec_ntlmsspi:
1088 vol->sign = true;
1089 /* Fallthrough */
1090 case Opt_sec_ntlmssp:
1091 vol->sectype = RawNTLMSSP;
1092 break;
1093 case Opt_sec_ntlmi:
1094 vol->sign = true;
1095 /* Fallthrough */
1096 case Opt_ntlm:
1097 vol->sectype = NTLM;
1098 break;
1099 case Opt_sec_ntlmv2i:
1100 vol->sign = true;
1101 /* Fallthrough */
1102 case Opt_sec_ntlmv2:
1103 vol->sectype = NTLMv2;
1104 break;
1105 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1106 case Opt_sec_lanman:
1107 vol->sectype = LANMAN;
1108 break;
1109 #endif
1110 case Opt_sec_none:
1111 vol->nullauth = 1;
1112 break;
1113 default:
1114 cifs_dbg(VFS, "bad security option: %s\n", value);
1115 return 1;
1118 return 0;
1121 static int
1122 cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1124 substring_t args[MAX_OPT_ARGS];
1126 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1127 case Opt_cache_loose:
1128 vol->direct_io = false;
1129 vol->strict_io = false;
1130 break;
1131 case Opt_cache_strict:
1132 vol->direct_io = false;
1133 vol->strict_io = true;
1134 break;
1135 case Opt_cache_none:
1136 vol->direct_io = true;
1137 vol->strict_io = false;
1138 break;
1139 default:
1140 cifs_dbg(VFS, "bad cache= option: %s\n", value);
1141 return 1;
1143 return 0;
1146 static int
1147 cifs_parse_smb_version(char *value, struct smb_vol *vol)
1149 substring_t args[MAX_OPT_ARGS];
1151 switch (match_token(value, cifs_smb_version_tokens, args)) {
1152 case Smb_1:
1153 vol->ops = &smb1_operations;
1154 vol->vals = &smb1_values;
1155 break;
1156 #ifdef CONFIG_CIFS_SMB2
1157 case Smb_20:
1158 vol->ops = &smb20_operations;
1159 vol->vals = &smb20_values;
1160 break;
1161 case Smb_21:
1162 vol->ops = &smb21_operations;
1163 vol->vals = &smb21_values;
1164 break;
1165 case Smb_30:
1166 vol->ops = &smb30_operations;
1167 vol->vals = &smb30_values;
1168 break;
1169 case Smb_302:
1170 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1171 vol->vals = &smb302_values;
1172 break;
1173 #ifdef CONFIG_CIFS_SMB311
1174 case Smb_311:
1175 vol->ops = &smb311_operations;
1176 vol->vals = &smb311_values;
1177 break;
1178 #endif /* SMB311 */
1179 #endif
1180 default:
1181 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
1182 return 1;
1184 return 0;
1188 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1189 * fields with the result. Returns 0 on success and an error otherwise.
1191 static int
1192 cifs_parse_devname(const char *devname, struct smb_vol *vol)
1194 char *pos;
1195 const char *delims = "/\\";
1196 size_t len;
1198 /* make sure we have a valid UNC double delimiter prefix */
1199 len = strspn(devname, delims);
1200 if (len != 2)
1201 return -EINVAL;
1203 /* find delimiter between host and sharename */
1204 pos = strpbrk(devname + 2, delims);
1205 if (!pos)
1206 return -EINVAL;
1208 /* skip past delimiter */
1209 ++pos;
1211 /* now go until next delimiter or end of string */
1212 len = strcspn(pos, delims);
1214 /* move "pos" up to delimiter or NULL */
1215 pos += len;
1216 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1217 if (!vol->UNC)
1218 return -ENOMEM;
1220 convert_delimiter(vol->UNC, '\\');
1222 /* If pos is NULL, or is a bogus trailing delimiter then no prepath */
1223 if (!*pos++ || !*pos)
1224 return 0;
1226 vol->prepath = kstrdup(pos, GFP_KERNEL);
1227 if (!vol->prepath)
1228 return -ENOMEM;
1230 return 0;
1233 static int
1234 cifs_parse_mount_options(const char *mountdata, const char *devname,
1235 struct smb_vol *vol)
1237 char *data, *end;
1238 char *mountdata_copy = NULL, *options;
1239 unsigned int temp_len, i, j;
1240 char separator[2];
1241 short int override_uid = -1;
1242 short int override_gid = -1;
1243 bool uid_specified = false;
1244 bool gid_specified = false;
1245 bool sloppy = false;
1246 char *invalid = NULL;
1247 char *nodename = utsname()->nodename;
1248 char *string = NULL;
1249 char *tmp_end, *value;
1250 char delim;
1251 bool got_ip = false;
1252 unsigned short port = 0;
1253 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1255 separator[0] = ',';
1256 separator[1] = 0;
1257 delim = separator[0];
1259 /* ensure we always start with zeroed-out smb_vol */
1260 memset(vol, 0, sizeof(*vol));
1263 * does not have to be perfect mapping since field is
1264 * informational, only used for servers that do not support
1265 * port 445 and it can be overridden at mount time
1267 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1268 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1269 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1271 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1272 /* null target name indicates to use *SMBSERVR default called name
1273 if we end up sending RFC1001 session initialize */
1274 vol->target_rfc1001_name[0] = 0;
1275 vol->cred_uid = current_uid();
1276 vol->linux_uid = current_uid();
1277 vol->linux_gid = current_gid();
1280 * default to SFM style remapping of seven reserved characters
1281 * unless user overrides it or we negotiate CIFS POSIX where
1282 * it is unnecessary. Can not simultaneously use more than one mapping
1283 * since then readdir could list files that open could not open
1285 vol->remap = true;
1287 /* default to only allowing write access to owner of the mount */
1288 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1290 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1291 /* default is always to request posix paths. */
1292 vol->posix_paths = 1;
1293 /* default to using server inode numbers where available */
1294 vol->server_ino = 1;
1296 /* default is to use strict cifs caching semantics */
1297 vol->strict_io = true;
1299 vol->actimeo = CIFS_DEF_ACTIMEO;
1301 /* FIXME: add autonegotiation -- for now, SMB1 is default */
1302 vol->ops = &smb1_operations;
1303 vol->vals = &smb1_values;
1305 if (!mountdata)
1306 goto cifs_parse_mount_err;
1308 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1309 if (!mountdata_copy)
1310 goto cifs_parse_mount_err;
1312 options = mountdata_copy;
1313 end = options + strlen(options);
1315 if (strncmp(options, "sep=", 4) == 0) {
1316 if (options[4] != 0) {
1317 separator[0] = options[4];
1318 options += 5;
1319 } else {
1320 cifs_dbg(FYI, "Null separator not allowed\n");
1323 vol->backupuid_specified = false; /* no backup intent for a user */
1324 vol->backupgid_specified = false; /* no backup intent for a group */
1326 switch (cifs_parse_devname(devname, vol)) {
1327 case 0:
1328 break;
1329 case -ENOMEM:
1330 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1331 goto cifs_parse_mount_err;
1332 case -EINVAL:
1333 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1334 goto cifs_parse_mount_err;
1335 default:
1336 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1337 goto cifs_parse_mount_err;
1340 while ((data = strsep(&options, separator)) != NULL) {
1341 substring_t args[MAX_OPT_ARGS];
1342 unsigned long option;
1343 int token;
1345 if (!*data)
1346 continue;
1348 token = match_token(data, cifs_mount_option_tokens, args);
1350 switch (token) {
1352 /* Ingnore the following */
1353 case Opt_ignore:
1354 break;
1356 /* Boolean values */
1357 case Opt_user_xattr:
1358 vol->no_xattr = 0;
1359 break;
1360 case Opt_nouser_xattr:
1361 vol->no_xattr = 1;
1362 break;
1363 case Opt_forceuid:
1364 override_uid = 1;
1365 break;
1366 case Opt_noforceuid:
1367 override_uid = 0;
1368 break;
1369 case Opt_forcegid:
1370 override_gid = 1;
1371 break;
1372 case Opt_noforcegid:
1373 override_gid = 0;
1374 break;
1375 case Opt_noblocksend:
1376 vol->noblocksnd = 1;
1377 break;
1378 case Opt_noautotune:
1379 vol->noautotune = 1;
1380 break;
1381 case Opt_hard:
1382 vol->retry = 1;
1383 break;
1384 case Opt_soft:
1385 vol->retry = 0;
1386 break;
1387 case Opt_perm:
1388 vol->noperm = 0;
1389 break;
1390 case Opt_noperm:
1391 vol->noperm = 1;
1392 break;
1393 case Opt_mapchars:
1394 vol->sfu_remap = true;
1395 vol->remap = false; /* disable SFM mapping */
1396 break;
1397 case Opt_nomapchars:
1398 vol->sfu_remap = false;
1399 break;
1400 case Opt_mapposix:
1401 vol->remap = true;
1402 vol->sfu_remap = false; /* disable SFU mapping */
1403 break;
1404 case Opt_nomapposix:
1405 vol->remap = false;
1406 break;
1407 case Opt_sfu:
1408 vol->sfu_emul = 1;
1409 break;
1410 case Opt_nosfu:
1411 vol->sfu_emul = 0;
1412 break;
1413 case Opt_nodfs:
1414 vol->nodfs = 1;
1415 break;
1416 case Opt_posixpaths:
1417 vol->posix_paths = 1;
1418 break;
1419 case Opt_noposixpaths:
1420 vol->posix_paths = 0;
1421 break;
1422 case Opt_nounix:
1423 vol->no_linux_ext = 1;
1424 break;
1425 case Opt_nocase:
1426 vol->nocase = 1;
1427 break;
1428 case Opt_brl:
1429 vol->nobrl = 0;
1430 break;
1431 case Opt_nobrl:
1432 vol->nobrl = 1;
1434 * turn off mandatory locking in mode
1435 * if remote locking is turned off since the
1436 * local vfs will do advisory
1438 if (vol->file_mode ==
1439 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1440 vol->file_mode = S_IALLUGO;
1441 break;
1442 case Opt_forcemandatorylock:
1443 vol->mand_lock = 1;
1444 break;
1445 case Opt_setuids:
1446 vol->setuids = 1;
1447 break;
1448 case Opt_nosetuids:
1449 vol->setuids = 0;
1450 break;
1451 case Opt_dynperm:
1452 vol->dynperm = true;
1453 break;
1454 case Opt_nodynperm:
1455 vol->dynperm = false;
1456 break;
1457 case Opt_nohard:
1458 vol->retry = 0;
1459 break;
1460 case Opt_nosoft:
1461 vol->retry = 1;
1462 break;
1463 case Opt_nointr:
1464 vol->intr = 0;
1465 break;
1466 case Opt_intr:
1467 vol->intr = 1;
1468 break;
1469 case Opt_nostrictsync:
1470 vol->nostrictsync = 1;
1471 break;
1472 case Opt_strictsync:
1473 vol->nostrictsync = 0;
1474 break;
1475 case Opt_serverino:
1476 vol->server_ino = 1;
1477 break;
1478 case Opt_noserverino:
1479 vol->server_ino = 0;
1480 break;
1481 case Opt_rwpidforward:
1482 vol->rwpidforward = 1;
1483 break;
1484 case Opt_cifsacl:
1485 vol->cifs_acl = 1;
1486 break;
1487 case Opt_nocifsacl:
1488 vol->cifs_acl = 0;
1489 break;
1490 case Opt_acl:
1491 vol->no_psx_acl = 0;
1492 break;
1493 case Opt_noacl:
1494 vol->no_psx_acl = 1;
1495 break;
1496 case Opt_locallease:
1497 vol->local_lease = 1;
1498 break;
1499 case Opt_sign:
1500 vol->sign = true;
1501 break;
1502 case Opt_seal:
1503 /* we do not do the following in secFlags because seal
1504 * is a per tree connection (mount) not a per socket
1505 * or per-smb connection option in the protocol
1506 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1508 vol->seal = 1;
1509 break;
1510 case Opt_noac:
1511 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1512 break;
1513 case Opt_fsc:
1514 #ifndef CONFIG_CIFS_FSCACHE
1515 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1516 goto cifs_parse_mount_err;
1517 #endif
1518 vol->fsc = true;
1519 break;
1520 case Opt_mfsymlinks:
1521 vol->mfsymlinks = true;
1522 break;
1523 case Opt_multiuser:
1524 vol->multiuser = true;
1525 break;
1526 case Opt_sloppy:
1527 sloppy = true;
1528 break;
1529 case Opt_nosharesock:
1530 vol->nosharesock = true;
1531 break;
1532 case Opt_nopersistent:
1533 vol->nopersistent = true;
1534 if (vol->persistent) {
1535 cifs_dbg(VFS,
1536 "persistenthandles mount options conflict\n");
1537 goto cifs_parse_mount_err;
1539 break;
1540 case Opt_persistent:
1541 vol->persistent = true;
1542 if ((vol->nopersistent) || (vol->resilient)) {
1543 cifs_dbg(VFS,
1544 "persistenthandles mount options conflict\n");
1545 goto cifs_parse_mount_err;
1547 break;
1548 case Opt_resilient:
1549 vol->resilient = true;
1550 if (vol->persistent) {
1551 cifs_dbg(VFS,
1552 "persistenthandles mount options conflict\n");
1553 goto cifs_parse_mount_err;
1555 break;
1556 case Opt_noresilient:
1557 vol->resilient = false; /* already the default */
1558 break;
1560 /* Numeric Values */
1561 case Opt_backupuid:
1562 if (get_option_uid(args, &vol->backupuid)) {
1563 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1564 __func__);
1565 goto cifs_parse_mount_err;
1567 vol->backupuid_specified = true;
1568 break;
1569 case Opt_backupgid:
1570 if (get_option_gid(args, &vol->backupgid)) {
1571 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1572 __func__);
1573 goto cifs_parse_mount_err;
1575 vol->backupgid_specified = true;
1576 break;
1577 case Opt_uid:
1578 if (get_option_uid(args, &vol->linux_uid)) {
1579 cifs_dbg(VFS, "%s: Invalid uid value\n",
1580 __func__);
1581 goto cifs_parse_mount_err;
1583 uid_specified = true;
1584 break;
1585 case Opt_cruid:
1586 if (get_option_uid(args, &vol->cred_uid)) {
1587 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1588 __func__);
1589 goto cifs_parse_mount_err;
1591 break;
1592 case Opt_gid:
1593 if (get_option_gid(args, &vol->linux_gid)) {
1594 cifs_dbg(VFS, "%s: Invalid gid value\n",
1595 __func__);
1596 goto cifs_parse_mount_err;
1598 gid_specified = true;
1599 break;
1600 case Opt_file_mode:
1601 if (get_option_ul(args, &option)) {
1602 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1603 __func__);
1604 goto cifs_parse_mount_err;
1606 vol->file_mode = option;
1607 break;
1608 case Opt_dirmode:
1609 if (get_option_ul(args, &option)) {
1610 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1611 __func__);
1612 goto cifs_parse_mount_err;
1614 vol->dir_mode = option;
1615 break;
1616 case Opt_port:
1617 if (get_option_ul(args, &option) ||
1618 option > USHRT_MAX) {
1619 cifs_dbg(VFS, "%s: Invalid port value\n",
1620 __func__);
1621 goto cifs_parse_mount_err;
1623 port = (unsigned short)option;
1624 break;
1625 case Opt_rsize:
1626 if (get_option_ul(args, &option)) {
1627 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1628 __func__);
1629 goto cifs_parse_mount_err;
1631 vol->rsize = option;
1632 break;
1633 case Opt_wsize:
1634 if (get_option_ul(args, &option)) {
1635 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1636 __func__);
1637 goto cifs_parse_mount_err;
1639 vol->wsize = option;
1640 break;
1641 case Opt_actimeo:
1642 if (get_option_ul(args, &option)) {
1643 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1644 __func__);
1645 goto cifs_parse_mount_err;
1647 vol->actimeo = HZ * option;
1648 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1649 cifs_dbg(VFS, "attribute cache timeout too large\n");
1650 goto cifs_parse_mount_err;
1652 break;
1654 /* String Arguments */
1656 case Opt_blank_user:
1657 /* null user, ie. anonymous authentication */
1658 vol->nullauth = 1;
1659 vol->username = NULL;
1660 break;
1661 case Opt_user:
1662 string = match_strdup(args);
1663 if (string == NULL)
1664 goto out_nomem;
1666 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1667 CIFS_MAX_USERNAME_LEN) {
1668 pr_warn("CIFS: username too long\n");
1669 goto cifs_parse_mount_err;
1672 kfree(vol->username);
1673 vol->username = kstrdup(string, GFP_KERNEL);
1674 if (!vol->username)
1675 goto cifs_parse_mount_err;
1676 break;
1677 case Opt_blank_pass:
1678 /* passwords have to be handled differently
1679 * to allow the character used for deliminator
1680 * to be passed within them
1684 * Check if this is a case where the password
1685 * starts with a delimiter
1687 tmp_end = strchr(data, '=');
1688 tmp_end++;
1689 if (!(tmp_end < end && tmp_end[1] == delim)) {
1690 /* No it is not. Set the password to NULL */
1691 kfree(vol->password);
1692 vol->password = NULL;
1693 break;
1695 /* Yes it is. Drop down to Opt_pass below.*/
1696 case Opt_pass:
1697 /* Obtain the value string */
1698 value = strchr(data, '=');
1699 value++;
1701 /* Set tmp_end to end of the string */
1702 tmp_end = (char *) value + strlen(value);
1704 /* Check if following character is the deliminator
1705 * If yes, we have encountered a double deliminator
1706 * reset the NULL character to the deliminator
1708 if (tmp_end < end && tmp_end[1] == delim) {
1709 tmp_end[0] = delim;
1711 /* Keep iterating until we get to a single
1712 * deliminator OR the end
1714 while ((tmp_end = strchr(tmp_end, delim))
1715 != NULL && (tmp_end[1] == delim)) {
1716 tmp_end = (char *) &tmp_end[2];
1719 /* Reset var options to point to next element */
1720 if (tmp_end) {
1721 tmp_end[0] = '\0';
1722 options = (char *) &tmp_end[1];
1723 } else
1724 /* Reached the end of the mount option
1725 * string */
1726 options = end;
1729 kfree(vol->password);
1730 /* Now build new password string */
1731 temp_len = strlen(value);
1732 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1733 if (vol->password == NULL) {
1734 pr_warn("CIFS: no memory for password\n");
1735 goto cifs_parse_mount_err;
1738 for (i = 0, j = 0; i < temp_len; i++, j++) {
1739 vol->password[j] = value[i];
1740 if ((value[i] == delim) &&
1741 value[i+1] == delim)
1742 /* skip the second deliminator */
1743 i++;
1745 vol->password[j] = '\0';
1746 break;
1747 case Opt_blank_ip:
1748 /* FIXME: should this be an error instead? */
1749 got_ip = false;
1750 break;
1751 case Opt_ip:
1752 string = match_strdup(args);
1753 if (string == NULL)
1754 goto out_nomem;
1756 if (!cifs_convert_address(dstaddr, string,
1757 strlen(string))) {
1758 pr_err("CIFS: bad ip= option (%s).\n", string);
1759 goto cifs_parse_mount_err;
1761 got_ip = true;
1762 break;
1763 case Opt_domain:
1764 string = match_strdup(args);
1765 if (string == NULL)
1766 goto out_nomem;
1768 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1769 == CIFS_MAX_DOMAINNAME_LEN) {
1770 pr_warn("CIFS: domain name too long\n");
1771 goto cifs_parse_mount_err;
1774 kfree(vol->domainname);
1775 vol->domainname = kstrdup(string, GFP_KERNEL);
1776 if (!vol->domainname) {
1777 pr_warn("CIFS: no memory for domainname\n");
1778 goto cifs_parse_mount_err;
1780 cifs_dbg(FYI, "Domain name set\n");
1781 break;
1782 case Opt_srcaddr:
1783 string = match_strdup(args);
1784 if (string == NULL)
1785 goto out_nomem;
1787 if (!cifs_convert_address(
1788 (struct sockaddr *)&vol->srcaddr,
1789 string, strlen(string))) {
1790 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1791 string);
1792 goto cifs_parse_mount_err;
1794 break;
1795 case Opt_iocharset:
1796 string = match_strdup(args);
1797 if (string == NULL)
1798 goto out_nomem;
1800 if (strnlen(string, 1024) >= 65) {
1801 pr_warn("CIFS: iocharset name too long.\n");
1802 goto cifs_parse_mount_err;
1805 if (strncasecmp(string, "default", 7) != 0) {
1806 kfree(vol->iocharset);
1807 vol->iocharset = kstrdup(string,
1808 GFP_KERNEL);
1809 if (!vol->iocharset) {
1810 pr_warn("CIFS: no memory for charset\n");
1811 goto cifs_parse_mount_err;
1814 /* if iocharset not set then load_nls_default
1815 * is used by caller
1817 cifs_dbg(FYI, "iocharset set to %s\n", string);
1818 break;
1819 case Opt_netbiosname:
1820 string = match_strdup(args);
1821 if (string == NULL)
1822 goto out_nomem;
1824 memset(vol->source_rfc1001_name, 0x20,
1825 RFC1001_NAME_LEN);
1827 * FIXME: are there cases in which a comma can
1828 * be valid in workstation netbios name (and
1829 * need special handling)?
1831 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1832 /* don't ucase netbiosname for user */
1833 if (string[i] == 0)
1834 break;
1835 vol->source_rfc1001_name[i] = string[i];
1837 /* The string has 16th byte zero still from
1838 * set at top of the function
1840 if (i == RFC1001_NAME_LEN && string[i] != 0)
1841 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
1842 break;
1843 case Opt_servern:
1844 /* servernetbiosname specified override *SMBSERVER */
1845 string = match_strdup(args);
1846 if (string == NULL)
1847 goto out_nomem;
1849 /* last byte, type, is 0x20 for servr type */
1850 memset(vol->target_rfc1001_name, 0x20,
1851 RFC1001_NAME_LEN_WITH_NULL);
1853 /* BB are there cases in which a comma can be
1854 valid in this workstation netbios name
1855 (and need special handling)? */
1857 /* user or mount helper must uppercase the
1858 netbios name */
1859 for (i = 0; i < 15; i++) {
1860 if (string[i] == 0)
1861 break;
1862 vol->target_rfc1001_name[i] = string[i];
1864 /* The string has 16th byte zero still from
1865 set at top of the function */
1866 if (i == RFC1001_NAME_LEN && string[i] != 0)
1867 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
1868 break;
1869 case Opt_ver:
1870 string = match_strdup(args);
1871 if (string == NULL)
1872 goto out_nomem;
1874 if (strncasecmp(string, "1", 1) == 0) {
1875 /* This is the default */
1876 break;
1878 /* For all other value, error */
1879 pr_warn("CIFS: Invalid version specified\n");
1880 goto cifs_parse_mount_err;
1881 case Opt_vers:
1882 string = match_strdup(args);
1883 if (string == NULL)
1884 goto out_nomem;
1886 if (cifs_parse_smb_version(string, vol) != 0)
1887 goto cifs_parse_mount_err;
1888 break;
1889 case Opt_sec:
1890 string = match_strdup(args);
1891 if (string == NULL)
1892 goto out_nomem;
1894 if (cifs_parse_security_flavors(string, vol) != 0)
1895 goto cifs_parse_mount_err;
1896 break;
1897 case Opt_cache:
1898 string = match_strdup(args);
1899 if (string == NULL)
1900 goto out_nomem;
1902 if (cifs_parse_cache_flavor(string, vol) != 0)
1903 goto cifs_parse_mount_err;
1904 break;
1905 default:
1907 * An option we don't recognize. Save it off for later
1908 * if we haven't already found one
1910 if (!invalid)
1911 invalid = data;
1912 break;
1914 /* Free up any allocated string */
1915 kfree(string);
1916 string = NULL;
1919 if (!sloppy && invalid) {
1920 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
1921 goto cifs_parse_mount_err;
1924 #ifndef CONFIG_KEYS
1925 /* Muliuser mounts require CONFIG_KEYS support */
1926 if (vol->multiuser) {
1927 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
1928 goto cifs_parse_mount_err;
1930 #endif
1931 if (!vol->UNC) {
1932 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
1933 goto cifs_parse_mount_err;
1936 /* make sure UNC has a share name */
1937 if (!strchr(vol->UNC + 3, '\\')) {
1938 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
1939 goto cifs_parse_mount_err;
1942 if (!got_ip) {
1943 /* No ip= option specified? Try to get it from UNC */
1944 if (!cifs_convert_address(dstaddr, &vol->UNC[2],
1945 strlen(&vol->UNC[2]))) {
1946 pr_err("Unable to determine destination address.\n");
1947 goto cifs_parse_mount_err;
1951 /* set the port that we got earlier */
1952 cifs_set_port(dstaddr, port);
1954 if (uid_specified)
1955 vol->override_uid = override_uid;
1956 else if (override_uid == 1)
1957 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
1959 if (gid_specified)
1960 vol->override_gid = override_gid;
1961 else if (override_gid == 1)
1962 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
1964 kfree(mountdata_copy);
1965 return 0;
1967 out_nomem:
1968 pr_warn("Could not allocate temporary buffer\n");
1969 cifs_parse_mount_err:
1970 kfree(string);
1971 kfree(mountdata_copy);
1972 return 1;
1975 /** Returns true if srcaddr isn't specified and rhs isn't
1976 * specified, or if srcaddr is specified and
1977 * matches the IP address of the rhs argument.
1979 static bool
1980 srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
1982 switch (srcaddr->sa_family) {
1983 case AF_UNSPEC:
1984 return (rhs->sa_family == AF_UNSPEC);
1985 case AF_INET: {
1986 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1987 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1988 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1990 case AF_INET6: {
1991 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1992 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1993 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1995 default:
1996 WARN_ON(1);
1997 return false; /* don't expect to be here */
2002 * If no port is specified in addr structure, we try to match with 445 port
2003 * and if it fails - with 139 ports. It should be called only if address
2004 * families of server and addr are equal.
2006 static bool
2007 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2009 __be16 port, *sport;
2011 switch (addr->sa_family) {
2012 case AF_INET:
2013 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2014 port = ((struct sockaddr_in *) addr)->sin_port;
2015 break;
2016 case AF_INET6:
2017 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2018 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2019 break;
2020 default:
2021 WARN_ON(1);
2022 return false;
2025 if (!port) {
2026 port = htons(CIFS_PORT);
2027 if (port == *sport)
2028 return true;
2030 port = htons(RFC1001_PORT);
2033 return port == *sport;
2036 static bool
2037 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2038 struct sockaddr *srcaddr)
2040 switch (addr->sa_family) {
2041 case AF_INET: {
2042 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2043 struct sockaddr_in *srv_addr4 =
2044 (struct sockaddr_in *)&server->dstaddr;
2046 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
2047 return false;
2048 break;
2050 case AF_INET6: {
2051 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2052 struct sockaddr_in6 *srv_addr6 =
2053 (struct sockaddr_in6 *)&server->dstaddr;
2055 if (!ipv6_addr_equal(&addr6->sin6_addr,
2056 &srv_addr6->sin6_addr))
2057 return false;
2058 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
2059 return false;
2060 break;
2062 default:
2063 WARN_ON(1);
2064 return false; /* don't expect to be here */
2067 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2068 return false;
2070 return true;
2073 static bool
2074 match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2077 * The select_sectype function should either return the vol->sectype
2078 * that was specified, or "Unspecified" if that sectype was not
2079 * compatible with the given NEGOTIATE request.
2081 if (select_sectype(server, vol->sectype) == Unspecified)
2082 return false;
2085 * Now check if signing mode is acceptable. No need to check
2086 * global_secflags at this point since if MUST_SIGN is set then
2087 * the server->sign had better be too.
2089 if (vol->sign && !server->sign)
2090 return false;
2092 return true;
2095 static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
2097 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2099 if (vol->nosharesock)
2100 return 0;
2102 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2103 return 0;
2105 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2106 return 0;
2108 if (!match_address(server, addr,
2109 (struct sockaddr *)&vol->srcaddr))
2110 return 0;
2112 if (!match_port(server, addr))
2113 return 0;
2115 if (!match_security(server, vol))
2116 return 0;
2118 return 1;
2121 static struct TCP_Server_Info *
2122 cifs_find_tcp_session(struct smb_vol *vol)
2124 struct TCP_Server_Info *server;
2126 spin_lock(&cifs_tcp_ses_lock);
2127 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
2128 if (!match_server(server, vol))
2129 continue;
2131 ++server->srv_count;
2132 spin_unlock(&cifs_tcp_ses_lock);
2133 cifs_dbg(FYI, "Existing tcp session with server found\n");
2134 return server;
2136 spin_unlock(&cifs_tcp_ses_lock);
2137 return NULL;
2140 void
2141 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
2143 struct task_struct *task;
2145 spin_lock(&cifs_tcp_ses_lock);
2146 if (--server->srv_count > 0) {
2147 spin_unlock(&cifs_tcp_ses_lock);
2148 return;
2151 put_net(cifs_net_ns(server));
2153 list_del_init(&server->tcp_ses_list);
2154 spin_unlock(&cifs_tcp_ses_lock);
2156 cancel_delayed_work_sync(&server->echo);
2158 #ifdef CONFIG_CIFS_SMB2
2159 if (from_reconnect)
2161 * Avoid deadlock here: reconnect work calls
2162 * cifs_put_tcp_session() at its end. Need to be sure
2163 * that reconnect work does nothing with server pointer after
2164 * that step.
2166 cancel_delayed_work(&server->reconnect);
2167 else
2168 cancel_delayed_work_sync(&server->reconnect);
2169 #endif
2171 spin_lock(&GlobalMid_Lock);
2172 server->tcpStatus = CifsExiting;
2173 spin_unlock(&GlobalMid_Lock);
2175 cifs_crypto_shash_release(server);
2176 cifs_fscache_release_client_cookie(server);
2178 kfree(server->session_key.response);
2179 server->session_key.response = NULL;
2180 server->session_key.len = 0;
2182 task = xchg(&server->tsk, NULL);
2183 if (task)
2184 force_sig(SIGKILL, task);
2187 static struct TCP_Server_Info *
2188 cifs_get_tcp_session(struct smb_vol *volume_info)
2190 struct TCP_Server_Info *tcp_ses = NULL;
2191 int rc;
2193 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
2195 /* see if we already have a matching tcp_ses */
2196 tcp_ses = cifs_find_tcp_session(volume_info);
2197 if (tcp_ses)
2198 return tcp_ses;
2200 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2201 if (!tcp_ses) {
2202 rc = -ENOMEM;
2203 goto out_err;
2206 tcp_ses->ops = volume_info->ops;
2207 tcp_ses->vals = volume_info->vals;
2208 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
2209 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2210 if (IS_ERR(tcp_ses->hostname)) {
2211 rc = PTR_ERR(tcp_ses->hostname);
2212 goto out_err_crypto_release;
2215 tcp_ses->noblocksnd = volume_info->noblocksnd;
2216 tcp_ses->noautotune = volume_info->noautotune;
2217 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
2218 tcp_ses->in_flight = 0;
2219 tcp_ses->credits = 1;
2220 init_waitqueue_head(&tcp_ses->response_q);
2221 init_waitqueue_head(&tcp_ses->request_q);
2222 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2223 mutex_init(&tcp_ses->srv_mutex);
2224 memcpy(tcp_ses->workstation_RFC1001_name,
2225 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2226 memcpy(tcp_ses->server_RFC1001_name,
2227 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2228 tcp_ses->session_estab = false;
2229 tcp_ses->sequence_number = 0;
2230 tcp_ses->lstrp = jiffies;
2231 spin_lock_init(&tcp_ses->req_lock);
2232 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2233 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
2234 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
2235 #ifdef CONFIG_CIFS_SMB2
2236 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2237 mutex_init(&tcp_ses->reconnect_mutex);
2238 #endif
2239 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2240 sizeof(tcp_ses->srcaddr));
2241 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2242 sizeof(tcp_ses->dstaddr));
2243 #ifdef CONFIG_CIFS_SMB2
2244 generate_random_uuid(tcp_ses->client_guid);
2245 #endif
2247 * at this point we are the only ones with the pointer
2248 * to the struct since the kernel thread not created yet
2249 * no need to spinlock this init of tcpStatus or srv_count
2251 tcp_ses->tcpStatus = CifsNew;
2252 ++tcp_ses->srv_count;
2254 rc = ip_connect(tcp_ses);
2255 if (rc < 0) {
2256 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
2257 goto out_err_crypto_release;
2261 * since we're in a cifs function already, we know that
2262 * this will succeed. No need for try_module_get().
2264 __module_get(THIS_MODULE);
2265 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
2266 tcp_ses, "cifsd");
2267 if (IS_ERR(tcp_ses->tsk)) {
2268 rc = PTR_ERR(tcp_ses->tsk);
2269 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
2270 module_put(THIS_MODULE);
2271 goto out_err_crypto_release;
2273 tcp_ses->tcpStatus = CifsNeedNegotiate;
2275 /* thread spawned, put it on the list */
2276 spin_lock(&cifs_tcp_ses_lock);
2277 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
2278 spin_unlock(&cifs_tcp_ses_lock);
2280 cifs_fscache_get_client_cookie(tcp_ses);
2282 /* queue echo request delayed work */
2283 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
2285 return tcp_ses;
2287 out_err_crypto_release:
2288 cifs_crypto_shash_release(tcp_ses);
2290 put_net(cifs_net_ns(tcp_ses));
2292 out_err:
2293 if (tcp_ses) {
2294 if (!IS_ERR(tcp_ses->hostname))
2295 kfree(tcp_ses->hostname);
2296 if (tcp_ses->ssocket)
2297 sock_release(tcp_ses->ssocket);
2298 kfree(tcp_ses);
2300 return ERR_PTR(rc);
2303 static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
2305 if (vol->sectype != Unspecified &&
2306 vol->sectype != ses->sectype)
2307 return 0;
2309 switch (ses->sectype) {
2310 case Kerberos:
2311 if (!uid_eq(vol->cred_uid, ses->cred_uid))
2312 return 0;
2313 break;
2314 default:
2315 /* NULL username means anonymous session */
2316 if (ses->user_name == NULL) {
2317 if (!vol->nullauth)
2318 return 0;
2319 break;
2322 /* anything else takes username/password */
2323 if (strncmp(ses->user_name,
2324 vol->username ? vol->username : "",
2325 CIFS_MAX_USERNAME_LEN))
2326 return 0;
2327 if ((vol->username && strlen(vol->username) != 0) &&
2328 ses->password != NULL &&
2329 strncmp(ses->password,
2330 vol->password ? vol->password : "",
2331 CIFS_MAX_PASSWORD_LEN))
2332 return 0;
2334 return 1;
2337 static struct cifs_ses *
2338 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
2340 struct cifs_ses *ses;
2342 spin_lock(&cifs_tcp_ses_lock);
2343 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2344 if (ses->status == CifsExiting)
2345 continue;
2346 if (!match_session(ses, vol))
2347 continue;
2348 ++ses->ses_count;
2349 spin_unlock(&cifs_tcp_ses_lock);
2350 return ses;
2352 spin_unlock(&cifs_tcp_ses_lock);
2353 return NULL;
2356 static void
2357 cifs_put_smb_ses(struct cifs_ses *ses)
2359 unsigned int rc, xid;
2360 struct TCP_Server_Info *server = ses->server;
2362 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
2364 spin_lock(&cifs_tcp_ses_lock);
2365 if (ses->status == CifsExiting) {
2366 spin_unlock(&cifs_tcp_ses_lock);
2367 return;
2369 if (--ses->ses_count > 0) {
2370 spin_unlock(&cifs_tcp_ses_lock);
2371 return;
2373 if (ses->status == CifsGood)
2374 ses->status = CifsExiting;
2375 spin_unlock(&cifs_tcp_ses_lock);
2377 if (ses->status == CifsExiting && server->ops->logoff) {
2378 xid = get_xid();
2379 rc = server->ops->logoff(xid, ses);
2380 if (rc)
2381 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2382 __func__, rc);
2383 _free_xid(xid);
2386 spin_lock(&cifs_tcp_ses_lock);
2387 list_del_init(&ses->smb_ses_list);
2388 spin_unlock(&cifs_tcp_ses_lock);
2390 sesInfoFree(ses);
2391 cifs_put_tcp_session(server, 0);
2394 #ifdef CONFIG_KEYS
2396 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2397 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
2399 /* Populate username and pw fields from keyring if possible */
2400 static int
2401 cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2403 int rc = 0;
2404 const char *delim, *payload;
2405 char *desc;
2406 ssize_t len;
2407 struct key *key;
2408 struct TCP_Server_Info *server = ses->server;
2409 struct sockaddr_in *sa;
2410 struct sockaddr_in6 *sa6;
2411 const struct user_key_payload *upayload;
2413 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2414 if (!desc)
2415 return -ENOMEM;
2417 /* try to find an address key first */
2418 switch (server->dstaddr.ss_family) {
2419 case AF_INET:
2420 sa = (struct sockaddr_in *)&server->dstaddr;
2421 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2422 break;
2423 case AF_INET6:
2424 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2425 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2426 break;
2427 default:
2428 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2429 server->dstaddr.ss_family);
2430 rc = -EINVAL;
2431 goto out_err;
2434 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2435 key = request_key(&key_type_logon, desc, "");
2436 if (IS_ERR(key)) {
2437 if (!ses->domainName) {
2438 cifs_dbg(FYI, "domainName is NULL\n");
2439 rc = PTR_ERR(key);
2440 goto out_err;
2443 /* didn't work, try to find a domain key */
2444 sprintf(desc, "cifs:d:%s", ses->domainName);
2445 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2446 key = request_key(&key_type_logon, desc, "");
2447 if (IS_ERR(key)) {
2448 rc = PTR_ERR(key);
2449 goto out_err;
2453 down_read(&key->sem);
2454 upayload = user_key_payload(key);
2455 if (IS_ERR_OR_NULL(upayload)) {
2456 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2457 goto out_key_put;
2460 /* find first : in payload */
2461 payload = upayload->data;
2462 delim = strnchr(payload, upayload->datalen, ':');
2463 cifs_dbg(FYI, "payload=%s\n", payload);
2464 if (!delim) {
2465 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2466 upayload->datalen);
2467 rc = -EINVAL;
2468 goto out_key_put;
2471 len = delim - payload;
2472 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2473 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2474 len);
2475 rc = -EINVAL;
2476 goto out_key_put;
2479 vol->username = kstrndup(payload, len, GFP_KERNEL);
2480 if (!vol->username) {
2481 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2482 len);
2483 rc = -ENOMEM;
2484 goto out_key_put;
2486 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
2488 len = key->datalen - (len + 1);
2489 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2490 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2491 rc = -EINVAL;
2492 kfree(vol->username);
2493 vol->username = NULL;
2494 goto out_key_put;
2497 ++delim;
2498 vol->password = kstrndup(delim, len, GFP_KERNEL);
2499 if (!vol->password) {
2500 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2501 len);
2502 rc = -ENOMEM;
2503 kfree(vol->username);
2504 vol->username = NULL;
2505 goto out_key_put;
2508 out_key_put:
2509 up_read(&key->sem);
2510 key_put(key);
2511 out_err:
2512 kfree(desc);
2513 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2514 return rc;
2516 #else /* ! CONFIG_KEYS */
2517 static inline int
2518 cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2519 struct cifs_ses *ses __attribute__((unused)))
2521 return -ENOSYS;
2523 #endif /* CONFIG_KEYS */
2525 static struct cifs_ses *
2526 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2528 int rc = -ENOMEM;
2529 unsigned int xid;
2530 struct cifs_ses *ses;
2531 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2532 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2534 xid = get_xid();
2536 ses = cifs_find_smb_ses(server, volume_info);
2537 if (ses) {
2538 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2539 ses->status);
2541 mutex_lock(&ses->session_mutex);
2542 rc = cifs_negotiate_protocol(xid, ses);
2543 if (rc) {
2544 mutex_unlock(&ses->session_mutex);
2545 /* problem -- put our ses reference */
2546 cifs_put_smb_ses(ses);
2547 free_xid(xid);
2548 return ERR_PTR(rc);
2550 if (ses->need_reconnect) {
2551 cifs_dbg(FYI, "Session needs reconnect\n");
2552 rc = cifs_setup_session(xid, ses,
2553 volume_info->local_nls);
2554 if (rc) {
2555 mutex_unlock(&ses->session_mutex);
2556 /* problem -- put our reference */
2557 cifs_put_smb_ses(ses);
2558 free_xid(xid);
2559 return ERR_PTR(rc);
2562 mutex_unlock(&ses->session_mutex);
2564 /* existing SMB ses has a server reference already */
2565 cifs_put_tcp_session(server, 0);
2566 free_xid(xid);
2567 return ses;
2570 cifs_dbg(FYI, "Existing smb sess not found\n");
2571 ses = sesInfoAlloc();
2572 if (ses == NULL)
2573 goto get_ses_fail;
2575 /* new SMB session uses our server ref */
2576 ses->server = server;
2577 if (server->dstaddr.ss_family == AF_INET6)
2578 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
2579 else
2580 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
2582 if (volume_info->username) {
2583 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2584 if (!ses->user_name)
2585 goto get_ses_fail;
2588 /* volume_info->password freed at unmount */
2589 if (volume_info->password) {
2590 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2591 if (!ses->password)
2592 goto get_ses_fail;
2594 if (volume_info->domainname) {
2595 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2596 if (!ses->domainName)
2597 goto get_ses_fail;
2599 ses->cred_uid = volume_info->cred_uid;
2600 ses->linux_uid = volume_info->linux_uid;
2602 ses->sectype = volume_info->sectype;
2603 ses->sign = volume_info->sign;
2605 mutex_lock(&ses->session_mutex);
2606 rc = cifs_negotiate_protocol(xid, ses);
2607 if (!rc)
2608 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
2609 mutex_unlock(&ses->session_mutex);
2610 if (rc)
2611 goto get_ses_fail;
2613 /* success, put it on the list */
2614 spin_lock(&cifs_tcp_ses_lock);
2615 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2616 spin_unlock(&cifs_tcp_ses_lock);
2618 free_xid(xid);
2619 return ses;
2621 get_ses_fail:
2622 sesInfoFree(ses);
2623 free_xid(xid);
2624 return ERR_PTR(rc);
2627 static int match_tcon(struct cifs_tcon *tcon, const char *unc)
2629 if (tcon->tidStatus == CifsExiting)
2630 return 0;
2631 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
2632 return 0;
2633 return 1;
2636 static struct cifs_tcon *
2637 cifs_find_tcon(struct cifs_ses *ses, const char *unc)
2639 struct list_head *tmp;
2640 struct cifs_tcon *tcon;
2642 spin_lock(&cifs_tcp_ses_lock);
2643 list_for_each(tmp, &ses->tcon_list) {
2644 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
2645 if (!match_tcon(tcon, unc))
2646 continue;
2647 ++tcon->tc_count;
2648 spin_unlock(&cifs_tcp_ses_lock);
2649 return tcon;
2651 spin_unlock(&cifs_tcp_ses_lock);
2652 return NULL;
2655 void
2656 cifs_put_tcon(struct cifs_tcon *tcon)
2658 unsigned int xid;
2659 struct cifs_ses *ses = tcon->ses;
2661 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2662 spin_lock(&cifs_tcp_ses_lock);
2663 if (--tcon->tc_count > 0) {
2664 spin_unlock(&cifs_tcp_ses_lock);
2665 return;
2668 list_del_init(&tcon->tcon_list);
2669 spin_unlock(&cifs_tcp_ses_lock);
2671 xid = get_xid();
2672 if (ses->server->ops->tree_disconnect)
2673 ses->server->ops->tree_disconnect(xid, tcon);
2674 _free_xid(xid);
2676 cifs_fscache_release_super_cookie(tcon);
2677 tconInfoFree(tcon);
2678 cifs_put_smb_ses(ses);
2681 static struct cifs_tcon *
2682 cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
2684 int rc, xid;
2685 struct cifs_tcon *tcon;
2687 tcon = cifs_find_tcon(ses, volume_info->UNC);
2688 if (tcon) {
2689 cifs_dbg(FYI, "Found match on UNC path\n");
2690 /* existing tcon already has a reference */
2691 cifs_put_smb_ses(ses);
2692 if (tcon->seal != volume_info->seal)
2693 cifs_dbg(VFS, "transport encryption setting conflicts with existing tid\n");
2694 return tcon;
2697 if (!ses->server->ops->tree_connect) {
2698 rc = -ENOSYS;
2699 goto out_fail;
2702 tcon = tconInfoAlloc();
2703 if (tcon == NULL) {
2704 rc = -ENOMEM;
2705 goto out_fail;
2708 tcon->ses = ses;
2709 if (volume_info->password) {
2710 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2711 if (!tcon->password) {
2712 rc = -ENOMEM;
2713 goto out_fail;
2718 * BB Do we need to wrap session_mutex around this TCon call and Unix
2719 * SetFS as we do on SessSetup and reconnect?
2721 xid = get_xid();
2722 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2723 volume_info->local_nls);
2724 free_xid(xid);
2725 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2726 if (rc)
2727 goto out_fail;
2729 if (volume_info->nodfs) {
2730 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
2731 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
2733 tcon->seal = volume_info->seal;
2734 tcon->use_persistent = false;
2735 /* check if SMB2 or later, CIFS does not support persistent handles */
2736 if (volume_info->persistent) {
2737 if (ses->server->vals->protocol_id == 0) {
2738 cifs_dbg(VFS,
2739 "SMB3 or later required for persistent handles\n");
2740 rc = -EOPNOTSUPP;
2741 goto out_fail;
2742 #ifdef CONFIG_CIFS_SMB2
2743 } else if (ses->server->capabilities &
2744 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2745 tcon->use_persistent = true;
2746 else /* persistent handles requested but not supported */ {
2747 cifs_dbg(VFS,
2748 "Persistent handles not supported on share\n");
2749 rc = -EOPNOTSUPP;
2750 goto out_fail;
2751 #endif /* CONFIG_CIFS_SMB2 */
2753 #ifdef CONFIG_CIFS_SMB2
2754 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
2755 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
2756 && (volume_info->nopersistent == false)) {
2757 cifs_dbg(FYI, "enabling persistent handles\n");
2758 tcon->use_persistent = true;
2759 #endif /* CONFIG_CIFS_SMB2 */
2760 } else if (volume_info->resilient) {
2761 if (ses->server->vals->protocol_id == 0) {
2762 cifs_dbg(VFS,
2763 "SMB2.1 or later required for resilient handles\n");
2764 rc = -EOPNOTSUPP;
2765 goto out_fail;
2767 tcon->use_resilient = true;
2771 * We can have only one retry value for a connection to a share so for
2772 * resources mounted more than once to the same server share the last
2773 * value passed in for the retry flag is used.
2775 tcon->retry = volume_info->retry;
2776 tcon->nocase = volume_info->nocase;
2777 tcon->local_lease = volume_info->local_lease;
2778 INIT_LIST_HEAD(&tcon->pending_opens);
2780 spin_lock(&cifs_tcp_ses_lock);
2781 list_add(&tcon->tcon_list, &ses->tcon_list);
2782 spin_unlock(&cifs_tcp_ses_lock);
2784 cifs_fscache_get_super_cookie(tcon);
2786 return tcon;
2788 out_fail:
2789 tconInfoFree(tcon);
2790 return ERR_PTR(rc);
2793 void
2794 cifs_put_tlink(struct tcon_link *tlink)
2796 if (!tlink || IS_ERR(tlink))
2797 return;
2799 if (!atomic_dec_and_test(&tlink->tl_count) ||
2800 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2801 tlink->tl_time = jiffies;
2802 return;
2805 if (!IS_ERR(tlink_tcon(tlink)))
2806 cifs_put_tcon(tlink_tcon(tlink));
2807 kfree(tlink);
2808 return;
2811 static inline struct tcon_link *
2812 cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2814 return cifs_sb->master_tlink;
2817 static int
2818 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2820 struct cifs_sb_info *old = CIFS_SB(sb);
2821 struct cifs_sb_info *new = mnt_data->cifs_sb;
2823 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2824 return 0;
2826 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2827 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2828 return 0;
2831 * We want to share sb only if we don't specify an r/wsize or
2832 * specified r/wsize is greater than or equal to existing one.
2834 if (new->wsize && new->wsize < old->wsize)
2835 return 0;
2837 if (new->rsize && new->rsize < old->rsize)
2838 return 0;
2840 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
2841 return 0;
2843 if (old->mnt_file_mode != new->mnt_file_mode ||
2844 old->mnt_dir_mode != new->mnt_dir_mode)
2845 return 0;
2847 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2848 return 0;
2850 if (old->actimeo != new->actimeo)
2851 return 0;
2853 return 1;
2857 cifs_match_super(struct super_block *sb, void *data)
2859 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2860 struct smb_vol *volume_info;
2861 struct cifs_sb_info *cifs_sb;
2862 struct TCP_Server_Info *tcp_srv;
2863 struct cifs_ses *ses;
2864 struct cifs_tcon *tcon;
2865 struct tcon_link *tlink;
2866 int rc = 0;
2868 spin_lock(&cifs_tcp_ses_lock);
2869 cifs_sb = CIFS_SB(sb);
2870 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2871 if (IS_ERR(tlink)) {
2872 spin_unlock(&cifs_tcp_ses_lock);
2873 return rc;
2875 tcon = tlink_tcon(tlink);
2876 ses = tcon->ses;
2877 tcp_srv = ses->server;
2879 volume_info = mnt_data->vol;
2881 if (!match_server(tcp_srv, volume_info) ||
2882 !match_session(ses, volume_info) ||
2883 !match_tcon(tcon, volume_info->UNC)) {
2884 rc = 0;
2885 goto out;
2888 rc = compare_mount_options(sb, mnt_data);
2889 out:
2890 spin_unlock(&cifs_tcp_ses_lock);
2891 cifs_put_tlink(tlink);
2892 return rc;
2896 get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
2897 const struct nls_table *nls_codepage, unsigned int *num_referrals,
2898 struct dfs_info3_param **referrals, int remap)
2900 char *temp_unc;
2901 int rc = 0;
2903 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
2904 return -ENOSYS;
2906 *num_referrals = 0;
2907 *referrals = NULL;
2909 if (ses->ipc_tid == 0) {
2910 temp_unc = kmalloc(2 /* for slashes */ +
2911 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
2912 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
2913 if (temp_unc == NULL)
2914 return -ENOMEM;
2915 temp_unc[0] = '\\';
2916 temp_unc[1] = '\\';
2917 strcpy(temp_unc + 2, ses->serverName);
2918 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
2919 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
2920 nls_codepage);
2921 cifs_dbg(FYI, "Tcon rc = %d ipc_tid = %d\n", rc, ses->ipc_tid);
2922 kfree(temp_unc);
2924 if (rc == 0)
2925 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
2926 referrals, num_referrals,
2927 nls_codepage, remap);
2929 * BB - map targetUNCs to dfs_info3 structures, here or in
2930 * ses->server->ops->get_dfs_refer.
2933 return rc;
2936 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2937 static struct lock_class_key cifs_key[2];
2938 static struct lock_class_key cifs_slock_key[2];
2940 static inline void
2941 cifs_reclassify_socket4(struct socket *sock)
2943 struct sock *sk = sock->sk;
2944 BUG_ON(sock_owned_by_user(sk));
2945 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2946 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2949 static inline void
2950 cifs_reclassify_socket6(struct socket *sock)
2952 struct sock *sk = sock->sk;
2953 BUG_ON(sock_owned_by_user(sk));
2954 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2955 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2957 #else
2958 static inline void
2959 cifs_reclassify_socket4(struct socket *sock)
2963 static inline void
2964 cifs_reclassify_socket6(struct socket *sock)
2967 #endif
2969 /* See RFC1001 section 14 on representation of Netbios names */
2970 static void rfc1002mangle(char *target, char *source, unsigned int length)
2972 unsigned int i, j;
2974 for (i = 0, j = 0; i < (length); i++) {
2975 /* mask a nibble at a time and encode */
2976 target[j] = 'A' + (0x0F & (source[i] >> 4));
2977 target[j+1] = 'A' + (0x0F & source[i]);
2978 j += 2;
2983 static int
2984 bind_socket(struct TCP_Server_Info *server)
2986 int rc = 0;
2987 if (server->srcaddr.ss_family != AF_UNSPEC) {
2988 /* Bind to the specified local IP address */
2989 struct socket *socket = server->ssocket;
2990 rc = socket->ops->bind(socket,
2991 (struct sockaddr *) &server->srcaddr,
2992 sizeof(server->srcaddr));
2993 if (rc < 0) {
2994 struct sockaddr_in *saddr4;
2995 struct sockaddr_in6 *saddr6;
2996 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2997 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2998 if (saddr6->sin6_family == AF_INET6)
2999 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3000 &saddr6->sin6_addr, rc);
3001 else
3002 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3003 &saddr4->sin_addr.s_addr, rc);
3006 return rc;
3009 static int
3010 ip_rfc1001_connect(struct TCP_Server_Info *server)
3012 int rc = 0;
3014 * some servers require RFC1001 sessinit before sending
3015 * negprot - BB check reconnection in case where second
3016 * sessinit is sent but no second negprot
3018 struct rfc1002_session_packet *ses_init_buf;
3019 struct smb_hdr *smb_buf;
3020 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3021 GFP_KERNEL);
3022 if (ses_init_buf) {
3023 ses_init_buf->trailer.session_req.called_len = 32;
3025 if (server->server_RFC1001_name &&
3026 server->server_RFC1001_name[0] != 0)
3027 rfc1002mangle(ses_init_buf->trailer.
3028 session_req.called_name,
3029 server->server_RFC1001_name,
3030 RFC1001_NAME_LEN_WITH_NULL);
3031 else
3032 rfc1002mangle(ses_init_buf->trailer.
3033 session_req.called_name,
3034 DEFAULT_CIFS_CALLED_NAME,
3035 RFC1001_NAME_LEN_WITH_NULL);
3037 ses_init_buf->trailer.session_req.calling_len = 32;
3040 * calling name ends in null (byte 16) from old smb
3041 * convention.
3043 if (server->workstation_RFC1001_name[0] != 0)
3044 rfc1002mangle(ses_init_buf->trailer.
3045 session_req.calling_name,
3046 server->workstation_RFC1001_name,
3047 RFC1001_NAME_LEN_WITH_NULL);
3048 else
3049 rfc1002mangle(ses_init_buf->trailer.
3050 session_req.calling_name,
3051 "LINUX_CIFS_CLNT",
3052 RFC1001_NAME_LEN_WITH_NULL);
3054 ses_init_buf->trailer.session_req.scope1 = 0;
3055 ses_init_buf->trailer.session_req.scope2 = 0;
3056 smb_buf = (struct smb_hdr *)ses_init_buf;
3058 /* sizeof RFC1002_SESSION_REQUEST with no scope */
3059 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
3060 rc = smb_send(server, smb_buf, 0x44);
3061 kfree(ses_init_buf);
3063 * RFC1001 layer in at least one server
3064 * requires very short break before negprot
3065 * presumably because not expecting negprot
3066 * to follow so fast. This is a simple
3067 * solution that works without
3068 * complicating the code and causes no
3069 * significant slowing down on mount
3070 * for everyone else
3072 usleep_range(1000, 2000);
3075 * else the negprot may still work without this
3076 * even though malloc failed
3079 return rc;
3082 static int
3083 generic_ip_connect(struct TCP_Server_Info *server)
3085 int rc = 0;
3086 __be16 sport;
3087 int slen, sfamily;
3088 struct socket *socket = server->ssocket;
3089 struct sockaddr *saddr;
3091 saddr = (struct sockaddr *) &server->dstaddr;
3093 if (server->dstaddr.ss_family == AF_INET6) {
3094 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3095 slen = sizeof(struct sockaddr_in6);
3096 sfamily = AF_INET6;
3097 } else {
3098 sport = ((struct sockaddr_in *) saddr)->sin_port;
3099 slen = sizeof(struct sockaddr_in);
3100 sfamily = AF_INET;
3103 if (socket == NULL) {
3104 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3105 IPPROTO_TCP, &socket, 1);
3106 if (rc < 0) {
3107 cifs_dbg(VFS, "Error %d creating socket\n", rc);
3108 server->ssocket = NULL;
3109 return rc;
3112 /* BB other socket options to set KEEPALIVE, NODELAY? */
3113 cifs_dbg(FYI, "Socket created\n");
3114 server->ssocket = socket;
3115 socket->sk->sk_allocation = GFP_NOFS;
3116 if (sfamily == AF_INET6)
3117 cifs_reclassify_socket6(socket);
3118 else
3119 cifs_reclassify_socket4(socket);
3122 rc = bind_socket(server);
3123 if (rc < 0)
3124 return rc;
3127 * Eventually check for other socket options to change from
3128 * the default. sock_setsockopt not used because it expects
3129 * user space buffer
3131 socket->sk->sk_rcvtimeo = 7 * HZ;
3132 socket->sk->sk_sndtimeo = 5 * HZ;
3134 /* make the bufsizes depend on wsize/rsize and max requests */
3135 if (server->noautotune) {
3136 if (socket->sk->sk_sndbuf < (200 * 1024))
3137 socket->sk->sk_sndbuf = 200 * 1024;
3138 if (socket->sk->sk_rcvbuf < (140 * 1024))
3139 socket->sk->sk_rcvbuf = 140 * 1024;
3142 if (server->tcp_nodelay) {
3143 int val = 1;
3144 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3145 (char *)&val, sizeof(val));
3146 if (rc)
3147 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3148 rc);
3151 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
3152 socket->sk->sk_sndbuf,
3153 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3155 rc = socket->ops->connect(socket, saddr, slen, 0);
3156 if (rc < 0) {
3157 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
3158 sock_release(socket);
3159 server->ssocket = NULL;
3160 return rc;
3163 if (sport == htons(RFC1001_PORT))
3164 rc = ip_rfc1001_connect(server);
3166 return rc;
3169 static int
3170 ip_connect(struct TCP_Server_Info *server)
3172 __be16 *sport;
3173 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3174 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3176 if (server->dstaddr.ss_family == AF_INET6)
3177 sport = &addr6->sin6_port;
3178 else
3179 sport = &addr->sin_port;
3181 if (*sport == 0) {
3182 int rc;
3184 /* try with 445 port at first */
3185 *sport = htons(CIFS_PORT);
3187 rc = generic_ip_connect(server);
3188 if (rc >= 0)
3189 return rc;
3191 /* if it failed, try with 139 port */
3192 *sport = htons(RFC1001_PORT);
3195 return generic_ip_connect(server);
3198 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3199 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
3201 /* if we are reconnecting then should we check to see if
3202 * any requested capabilities changed locally e.g. via
3203 * remount but we can not do much about it here
3204 * if they have (even if we could detect it by the following)
3205 * Perhaps we could add a backpointer to array of sb from tcon
3206 * or if we change to make all sb to same share the same
3207 * sb as NFS - then we only have one backpointer to sb.
3208 * What if we wanted to mount the server share twice once with
3209 * and once without posixacls or posix paths? */
3210 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3212 if (vol_info && vol_info->no_linux_ext) {
3213 tcon->fsUnixInfo.Capability = 0;
3214 tcon->unix_ext = 0; /* Unix Extensions disabled */
3215 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3216 return;
3217 } else if (vol_info)
3218 tcon->unix_ext = 1; /* Unix Extensions supported */
3220 if (tcon->unix_ext == 0) {
3221 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3222 return;
3225 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3226 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3227 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3228 /* check for reconnect case in which we do not
3229 want to change the mount behavior if we can avoid it */
3230 if (vol_info == NULL) {
3231 /* turn off POSIX ACL and PATHNAMES if not set
3232 originally at mount time */
3233 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3234 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3235 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3236 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3237 cifs_dbg(VFS, "POSIXPATH support change\n");
3238 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3239 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3240 cifs_dbg(VFS, "possible reconnect error\n");
3241 cifs_dbg(VFS, "server disabled POSIX path support\n");
3245 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3246 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3248 cap &= CIFS_UNIX_CAP_MASK;
3249 if (vol_info && vol_info->no_psx_acl)
3250 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3251 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3252 cifs_dbg(FYI, "negotiated posix acl support\n");
3253 if (cifs_sb)
3254 cifs_sb->mnt_cifs_flags |=
3255 CIFS_MOUNT_POSIXACL;
3258 if (vol_info && vol_info->posix_paths == 0)
3259 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3260 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3261 cifs_dbg(FYI, "negotiate posix pathnames\n");
3262 if (cifs_sb)
3263 cifs_sb->mnt_cifs_flags |=
3264 CIFS_MOUNT_POSIX_PATHS;
3267 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3268 #ifdef CONFIG_CIFS_DEBUG2
3269 if (cap & CIFS_UNIX_FCNTL_CAP)
3270 cifs_dbg(FYI, "FCNTL cap\n");
3271 if (cap & CIFS_UNIX_EXTATTR_CAP)
3272 cifs_dbg(FYI, "EXTATTR cap\n");
3273 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3274 cifs_dbg(FYI, "POSIX path cap\n");
3275 if (cap & CIFS_UNIX_XATTR_CAP)
3276 cifs_dbg(FYI, "XATTR cap\n");
3277 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3278 cifs_dbg(FYI, "POSIX ACL cap\n");
3279 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3280 cifs_dbg(FYI, "very large read cap\n");
3281 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3282 cifs_dbg(FYI, "very large write cap\n");
3283 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3284 cifs_dbg(FYI, "transport encryption cap\n");
3285 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3286 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3287 #endif /* CIFS_DEBUG2 */
3288 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3289 if (vol_info == NULL) {
3290 cifs_dbg(FYI, "resetting capabilities failed\n");
3291 } else
3292 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");
3298 void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
3299 struct cifs_sb_info *cifs_sb)
3301 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3303 spin_lock_init(&cifs_sb->tlink_tree_lock);
3304 cifs_sb->tlink_tree = RB_ROOT;
3307 * Temporarily set r/wsize for matching superblock. If we end up using
3308 * new sb then client will later negotiate it downward if needed.
3310 cifs_sb->rsize = pvolume_info->rsize;
3311 cifs_sb->wsize = pvolume_info->wsize;
3313 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3314 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3315 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3316 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
3317 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3318 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3320 cifs_sb->actimeo = pvolume_info->actimeo;
3321 cifs_sb->local_nls = pvolume_info->local_nls;
3323 if (pvolume_info->noperm)
3324 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3325 if (pvolume_info->setuids)
3326 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
3327 if (pvolume_info->server_ino)
3328 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3329 if (pvolume_info->remap)
3330 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3331 if (pvolume_info->sfu_remap)
3332 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3333 if (pvolume_info->no_xattr)
3334 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3335 if (pvolume_info->sfu_emul)
3336 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3337 if (pvolume_info->nobrl)
3338 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
3339 if (pvolume_info->nostrictsync)
3340 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
3341 if (pvolume_info->mand_lock)
3342 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
3343 if (pvolume_info->rwpidforward)
3344 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3345 if (pvolume_info->cifs_acl)
3346 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3347 if (pvolume_info->backupuid_specified) {
3348 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3349 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3351 if (pvolume_info->backupgid_specified) {
3352 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3353 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3355 if (pvolume_info->override_uid)
3356 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3357 if (pvolume_info->override_gid)
3358 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3359 if (pvolume_info->dynperm)
3360 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
3361 if (pvolume_info->fsc)
3362 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
3363 if (pvolume_info->multiuser)
3364 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3365 CIFS_MOUNT_NO_PERM);
3366 if (pvolume_info->strict_io)
3367 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3368 if (pvolume_info->direct_io) {
3369 cifs_dbg(FYI, "mounting share using direct i/o\n");
3370 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3372 if (pvolume_info->mfsymlinks) {
3373 if (pvolume_info->sfu_emul) {
3375 * Our SFU ("Services for Unix" emulation does not allow
3376 * creating symlinks but does allow reading existing SFU
3377 * symlinks (it does allow both creating and reading SFU
3378 * style mknod and FIFOs though). When "mfsymlinks" and
3379 * "sfu" are both enabled at the same time, it allows
3380 * reading both types of symlinks, but will only create
3381 * them with mfsymlinks format. This allows better
3382 * Apple compatibility (probably better for Samba too)
3383 * while still recognizing old Windows style symlinks.
3385 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
3387 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
3390 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
3391 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3394 static void
3395 cleanup_volume_info_contents(struct smb_vol *volume_info)
3397 kfree(volume_info->username);
3398 kzfree(volume_info->password);
3399 kfree(volume_info->UNC);
3400 kfree(volume_info->domainname);
3401 kfree(volume_info->iocharset);
3402 kfree(volume_info->prepath);
3405 void
3406 cifs_cleanup_volume_info(struct smb_vol *volume_info)
3408 if (!volume_info)
3409 return;
3410 cleanup_volume_info_contents(volume_info);
3411 kfree(volume_info);
3415 #ifdef CONFIG_CIFS_DFS_UPCALL
3417 * cifs_build_path_to_root returns full path to root when we do not have an
3418 * exiting connection (tcon)
3420 static char *
3421 build_unc_path_to_root(const struct smb_vol *vol,
3422 const struct cifs_sb_info *cifs_sb)
3424 char *full_path, *pos;
3425 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
3426 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
3428 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3429 if (full_path == NULL)
3430 return ERR_PTR(-ENOMEM);
3432 strncpy(full_path, vol->UNC, unc_len);
3433 pos = full_path + unc_len;
3435 if (pplen) {
3436 *pos = CIFS_DIR_SEP(cifs_sb);
3437 strncpy(pos + 1, vol->prepath, pplen);
3438 pos += pplen;
3441 *pos = '\0'; /* add trailing null */
3442 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3443 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3444 return full_path;
3448 * Perform a dfs referral query for a share and (optionally) prefix
3450 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3451 * to a string containing updated options for the submount. Otherwise it
3452 * will be left untouched.
3454 * Returns the rc from get_dfs_path to the caller, which can be used to
3455 * determine whether there were referrals.
3457 static int
3458 expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
3459 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
3460 int check_prefix)
3462 int rc;
3463 unsigned int num_referrals = 0;
3464 struct dfs_info3_param *referrals = NULL;
3465 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3467 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3468 if (IS_ERR(full_path))
3469 return PTR_ERR(full_path);
3471 /* For DFS paths, skip the first '\' of the UNC */
3472 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3474 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
3475 &num_referrals, &referrals, cifs_remap(cifs_sb));
3477 if (!rc && num_referrals > 0) {
3478 char *fake_devname = NULL;
3480 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3481 full_path + 1, referrals,
3482 &fake_devname);
3484 free_dfs_info_array(referrals, num_referrals);
3486 if (IS_ERR(mdata)) {
3487 rc = PTR_ERR(mdata);
3488 mdata = NULL;
3489 } else {
3490 cleanup_volume_info_contents(volume_info);
3491 rc = cifs_setup_volume_info(volume_info, mdata,
3492 fake_devname);
3494 kfree(fake_devname);
3495 kfree(cifs_sb->mountdata);
3496 cifs_sb->mountdata = mdata;
3498 kfree(full_path);
3499 return rc;
3501 #endif
3503 static int
3504 cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3505 const char *devname)
3507 int rc = 0;
3509 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3510 return -EINVAL;
3512 if (volume_info->nullauth) {
3513 cifs_dbg(FYI, "Anonymous login\n");
3514 kfree(volume_info->username);
3515 volume_info->username = NULL;
3516 } else if (volume_info->username) {
3517 /* BB fixme parse for domain name here */
3518 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
3519 } else {
3520 cifs_dbg(VFS, "No username specified\n");
3521 /* In userspace mount helper we can get user name from alternate
3522 locations such as env variables and files on disk */
3523 return -EINVAL;
3526 /* this is needed for ASCII cp to Unicode converts */
3527 if (volume_info->iocharset == NULL) {
3528 /* load_nls_default cannot return null */
3529 volume_info->local_nls = load_nls_default();
3530 } else {
3531 volume_info->local_nls = load_nls(volume_info->iocharset);
3532 if (volume_info->local_nls == NULL) {
3533 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3534 volume_info->iocharset);
3535 return -ELIBACC;
3539 return rc;
3542 struct smb_vol *
3543 cifs_get_volume_info(char *mount_data, const char *devname)
3545 int rc;
3546 struct smb_vol *volume_info;
3548 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
3549 if (!volume_info)
3550 return ERR_PTR(-ENOMEM);
3552 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3553 if (rc) {
3554 cifs_cleanup_volume_info(volume_info);
3555 volume_info = ERR_PTR(rc);
3558 return volume_info;
3561 static int
3562 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3563 unsigned int xid,
3564 struct cifs_tcon *tcon,
3565 struct cifs_sb_info *cifs_sb,
3566 char *full_path)
3568 int rc;
3569 char *s;
3570 char sep, tmp;
3572 sep = CIFS_DIR_SEP(cifs_sb);
3573 s = full_path;
3575 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3576 while (rc == 0) {
3577 /* skip separators */
3578 while (*s == sep)
3579 s++;
3580 if (!*s)
3581 break;
3582 /* next separator */
3583 while (*s && *s != sep)
3584 s++;
3587 * temporarily null-terminate the path at the end of
3588 * the current component
3590 tmp = *s;
3591 *s = 0;
3592 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3593 full_path);
3594 *s = tmp;
3596 return rc;
3600 cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
3602 int rc;
3603 unsigned int xid;
3604 struct cifs_ses *ses;
3605 struct cifs_tcon *tcon;
3606 struct TCP_Server_Info *server;
3607 char *full_path;
3608 struct tcon_link *tlink;
3609 #ifdef CONFIG_CIFS_DFS_UPCALL
3610 int referral_walks_count = 0;
3611 #endif
3613 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs");
3614 if (rc)
3615 return rc;
3617 #ifdef CONFIG_CIFS_DFS_UPCALL
3618 try_mount_again:
3619 /* cleanup activities if we're chasing a referral */
3620 if (referral_walks_count) {
3621 if (tcon)
3622 cifs_put_tcon(tcon);
3623 else if (ses)
3624 cifs_put_smb_ses(ses);
3626 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3628 free_xid(xid);
3630 #endif
3631 rc = 0;
3632 tcon = NULL;
3633 ses = NULL;
3634 server = NULL;
3635 full_path = NULL;
3636 tlink = NULL;
3638 xid = get_xid();
3640 /* get a reference to a tcp session */
3641 server = cifs_get_tcp_session(volume_info);
3642 if (IS_ERR(server)) {
3643 rc = PTR_ERR(server);
3644 bdi_destroy(&cifs_sb->bdi);
3645 goto out;
3648 /* get a reference to a SMB session */
3649 ses = cifs_get_smb_ses(server, volume_info);
3650 if (IS_ERR(ses)) {
3651 rc = PTR_ERR(ses);
3652 ses = NULL;
3653 goto mount_fail_check;
3656 #ifdef CONFIG_CIFS_SMB2
3657 if ((volume_info->persistent == true) && ((ses->server->capabilities &
3658 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
3659 cifs_dbg(VFS, "persistent handles not supported by server\n");
3660 rc = -EOPNOTSUPP;
3661 goto mount_fail_check;
3663 #endif /* CONFIG_CIFS_SMB2*/
3665 /* search for existing tcon to this server share */
3666 tcon = cifs_get_tcon(ses, volume_info);
3667 if (IS_ERR(tcon)) {
3668 rc = PTR_ERR(tcon);
3669 tcon = NULL;
3670 goto remote_path_check;
3673 /* tell server which Unix caps we support */
3674 if (cap_unix(tcon->ses)) {
3675 /* reset of caps checks mount to see if unix extensions
3676 disabled for just this mount */
3677 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
3678 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3679 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3680 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3681 rc = -EACCES;
3682 goto mount_fail_check;
3684 } else
3685 tcon->unix_ext = 0; /* server does not support them */
3687 /* do not care if a following call succeed - informational */
3688 if (!tcon->ipc && server->ops->qfs_tcon)
3689 server->ops->qfs_tcon(xid, tcon);
3691 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3692 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
3694 /* tune readahead according to rsize */
3695 cifs_sb->bdi.ra_pages = cifs_sb->rsize / PAGE_CACHE_SIZE;
3697 remote_path_check:
3698 #ifdef CONFIG_CIFS_DFS_UPCALL
3700 * Perform an unconditional check for whether there are DFS
3701 * referrals for this path without prefix, to provide support
3702 * for DFS referrals from w2k8 servers which don't seem to respond
3703 * with PATH_NOT_COVERED to requests that include the prefix.
3704 * Chase the referral if found, otherwise continue normally.
3706 if (referral_walks_count == 0) {
3707 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3708 false);
3709 if (!refrc) {
3710 referral_walks_count++;
3711 goto try_mount_again;
3714 #endif
3716 /* check if a whole path is not remote */
3717 if (!rc && tcon) {
3718 if (!server->ops->is_path_accessible) {
3719 rc = -ENOSYS;
3720 goto mount_fail_check;
3723 * cifs_build_path_to_root works only when we have a valid tcon
3725 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
3726 if (full_path == NULL) {
3727 rc = -ENOMEM;
3728 goto mount_fail_check;
3730 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3731 full_path);
3732 if (rc != 0 && rc != -EREMOTE) {
3733 kfree(full_path);
3734 goto mount_fail_check;
3737 if (rc != -EREMOTE) {
3738 rc = cifs_are_all_path_components_accessible(server,
3739 xid, tcon, cifs_sb,
3740 full_path);
3741 if (rc != 0) {
3742 cifs_dbg(VFS, "cannot query dirs between root and final path, "
3743 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3744 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3745 rc = 0;
3748 kfree(full_path);
3751 /* get referral if needed */
3752 if (rc == -EREMOTE) {
3753 #ifdef CONFIG_CIFS_DFS_UPCALL
3754 if (referral_walks_count > MAX_NESTED_LINKS) {
3756 * BB: when we implement proper loop detection,
3757 * we will remove this check. But now we need it
3758 * to prevent an indefinite loop if 'DFS tree' is
3759 * misconfigured (i.e. has loops).
3761 rc = -ELOOP;
3762 goto mount_fail_check;
3765 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
3767 if (!rc) {
3768 referral_walks_count++;
3769 goto try_mount_again;
3771 goto mount_fail_check;
3772 #else /* No DFS support, return error on mount */
3773 rc = -EOPNOTSUPP;
3774 #endif
3777 if (rc)
3778 goto mount_fail_check;
3780 /* now, hang the tcon off of the superblock */
3781 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3782 if (tlink == NULL) {
3783 rc = -ENOMEM;
3784 goto mount_fail_check;
3787 tlink->tl_uid = ses->linux_uid;
3788 tlink->tl_tcon = tcon;
3789 tlink->tl_time = jiffies;
3790 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3791 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3793 cifs_sb->master_tlink = tlink;
3794 spin_lock(&cifs_sb->tlink_tree_lock);
3795 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3796 spin_unlock(&cifs_sb->tlink_tree_lock);
3798 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3799 TLINK_IDLE_EXPIRE);
3801 mount_fail_check:
3802 /* on error free sesinfo and tcon struct if needed */
3803 if (rc) {
3804 /* If find_unc succeeded then rc == 0 so we can not end */
3805 /* up accidentally freeing someone elses tcon struct */
3806 if (tcon)
3807 cifs_put_tcon(tcon);
3808 else if (ses)
3809 cifs_put_smb_ses(ses);
3810 else
3811 cifs_put_tcp_session(server, 0);
3812 bdi_destroy(&cifs_sb->bdi);
3815 out:
3816 free_xid(xid);
3817 return rc;
3821 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3822 * pointer may be NULL.
3825 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3826 const char *tree, struct cifs_tcon *tcon,
3827 const struct nls_table *nls_codepage)
3829 struct smb_hdr *smb_buffer;
3830 struct smb_hdr *smb_buffer_response;
3831 TCONX_REQ *pSMB;
3832 TCONX_RSP *pSMBr;
3833 unsigned char *bcc_ptr;
3834 int rc = 0;
3835 int length;
3836 __u16 bytes_left, count;
3838 if (ses == NULL)
3839 return -EIO;
3841 smb_buffer = cifs_buf_get();
3842 if (smb_buffer == NULL)
3843 return -ENOMEM;
3845 smb_buffer_response = smb_buffer;
3847 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3848 NULL /*no tid */ , 4 /*wct */ );
3850 smb_buffer->Mid = get_next_mid(ses->server);
3851 smb_buffer->Uid = ses->Suid;
3852 pSMB = (TCONX_REQ *) smb_buffer;
3853 pSMBr = (TCONX_RSP *) smb_buffer_response;
3855 pSMB->AndXCommand = 0xFF;
3856 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3857 bcc_ptr = &pSMB->Password[0];
3858 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
3859 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
3860 *bcc_ptr = 0; /* password is null byte */
3861 bcc_ptr++; /* skip password */
3862 /* already aligned so no need to do it below */
3863 } else {
3864 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
3865 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3866 specified as required (when that support is added to
3867 the vfs in the future) as only NTLM or the much
3868 weaker LANMAN (which we do not send by default) is accepted
3869 by Samba (not sure whether other servers allow
3870 NTLMv2 password here) */
3871 #ifdef CONFIG_CIFS_WEAK_PW_HASH
3872 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
3873 (ses->sectype == LANMAN))
3874 calc_lanman_hash(tcon->password, ses->server->cryptkey,
3875 ses->server->sec_mode &
3876 SECMODE_PW_ENCRYPT ? true : false,
3877 bcc_ptr);
3878 else
3879 #endif /* CIFS_WEAK_PW_HASH */
3880 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
3881 bcc_ptr, nls_codepage);
3882 if (rc) {
3883 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
3884 __func__, rc);
3885 cifs_buf_release(smb_buffer);
3886 return rc;
3889 bcc_ptr += CIFS_AUTH_RESP_SIZE;
3890 if (ses->capabilities & CAP_UNICODE) {
3891 /* must align unicode strings */
3892 *bcc_ptr = 0; /* null byte password */
3893 bcc_ptr++;
3897 if (ses->server->sign)
3898 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3900 if (ses->capabilities & CAP_STATUS32) {
3901 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3903 if (ses->capabilities & CAP_DFS) {
3904 smb_buffer->Flags2 |= SMBFLG2_DFS;
3906 if (ses->capabilities & CAP_UNICODE) {
3907 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3908 length =
3909 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3910 6 /* max utf8 char length in bytes */ *
3911 (/* server len*/ + 256 /* share len */), nls_codepage);
3912 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
3913 bcc_ptr += 2; /* skip trailing null */
3914 } else { /* ASCII */
3915 strcpy(bcc_ptr, tree);
3916 bcc_ptr += strlen(tree) + 1;
3918 strcpy(bcc_ptr, "?????");
3919 bcc_ptr += strlen("?????");
3920 bcc_ptr += 1;
3921 count = bcc_ptr - &pSMB->Password[0];
3922 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
3923 pSMB->hdr.smb_buf_length) + count);
3924 pSMB->ByteCount = cpu_to_le16(count);
3926 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3929 /* above now done in SendReceive */
3930 if ((rc == 0) && (tcon != NULL)) {
3931 bool is_unicode;
3933 tcon->tidStatus = CifsGood;
3934 tcon->need_reconnect = false;
3935 tcon->tid = smb_buffer_response->Tid;
3936 bcc_ptr = pByteArea(smb_buffer_response);
3937 bytes_left = get_bcc(smb_buffer_response);
3938 length = strnlen(bcc_ptr, bytes_left - 2);
3939 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3940 is_unicode = true;
3941 else
3942 is_unicode = false;
3945 /* skip service field (NB: this field is always ASCII) */
3946 if (length == 3) {
3947 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3948 (bcc_ptr[2] == 'C')) {
3949 cifs_dbg(FYI, "IPC connection\n");
3950 tcon->ipc = 1;
3952 } else if (length == 2) {
3953 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3954 /* the most common case */
3955 cifs_dbg(FYI, "disk share connection\n");
3958 bcc_ptr += length + 1;
3959 bytes_left -= (length + 1);
3960 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
3962 /* mostly informational -- no need to fail on error here */
3963 kfree(tcon->nativeFileSystem);
3964 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
3965 bytes_left, is_unicode,
3966 nls_codepage);
3968 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
3970 if ((smb_buffer_response->WordCount == 3) ||
3971 (smb_buffer_response->WordCount == 7))
3972 /* field is in same location */
3973 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3974 else
3975 tcon->Flags = 0;
3976 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
3977 } else if ((rc == 0) && tcon == NULL) {
3978 /* all we need to save for IPC$ connection */
3979 ses->ipc_tid = smb_buffer_response->Tid;
3982 cifs_buf_release(smb_buffer);
3983 return rc;
3986 static void delayed_free(struct rcu_head *p)
3988 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
3989 unload_nls(sbi->local_nls);
3990 kfree(sbi);
3993 void
3994 cifs_umount(struct cifs_sb_info *cifs_sb)
3996 struct rb_root *root = &cifs_sb->tlink_tree;
3997 struct rb_node *node;
3998 struct tcon_link *tlink;
4000 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4002 spin_lock(&cifs_sb->tlink_tree_lock);
4003 while ((node = rb_first(root))) {
4004 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4005 cifs_get_tlink(tlink);
4006 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4007 rb_erase(node, root);
4009 spin_unlock(&cifs_sb->tlink_tree_lock);
4010 cifs_put_tlink(tlink);
4011 spin_lock(&cifs_sb->tlink_tree_lock);
4013 spin_unlock(&cifs_sb->tlink_tree_lock);
4015 bdi_destroy(&cifs_sb->bdi);
4016 kfree(cifs_sb->mountdata);
4017 kfree(cifs_sb->prepath);
4018 call_rcu(&cifs_sb->rcu, delayed_free);
4022 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
4024 int rc = 0;
4025 struct TCP_Server_Info *server = ses->server;
4027 if (!server->ops->need_neg || !server->ops->negotiate)
4028 return -ENOSYS;
4030 /* only send once per connect */
4031 if (!server->ops->need_neg(server))
4032 return 0;
4034 set_credits(server, 1);
4036 rc = server->ops->negotiate(xid, ses);
4037 if (rc == 0) {
4038 spin_lock(&GlobalMid_Lock);
4039 if (server->tcpStatus == CifsNeedNegotiate)
4040 server->tcpStatus = CifsGood;
4041 else
4042 rc = -EHOSTDOWN;
4043 spin_unlock(&GlobalMid_Lock);
4046 return rc;
4050 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4051 struct nls_table *nls_info)
4053 int rc = -ENOSYS;
4054 struct TCP_Server_Info *server = ses->server;
4056 ses->capabilities = server->capabilities;
4057 if (linuxExtEnabled == 0)
4058 ses->capabilities &= (~server->vals->cap_unix);
4060 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
4061 server->sec_mode, server->capabilities, server->timeAdj);
4063 if (server->ops->sess_setup)
4064 rc = server->ops->sess_setup(xid, ses, nls_info);
4066 if (rc)
4067 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
4069 return rc;
4072 static int
4073 cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4075 vol->sectype = ses->sectype;
4077 /* krb5 is special, since we don't need username or pw */
4078 if (vol->sectype == Kerberos)
4079 return 0;
4081 return cifs_set_cifscreds(vol, ses);
4084 static struct cifs_tcon *
4085 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
4087 int rc;
4088 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4089 struct cifs_ses *ses;
4090 struct cifs_tcon *tcon = NULL;
4091 struct smb_vol *vol_info;
4093 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
4094 if (vol_info == NULL)
4095 return ERR_PTR(-ENOMEM);
4097 vol_info->local_nls = cifs_sb->local_nls;
4098 vol_info->linux_uid = fsuid;
4099 vol_info->cred_uid = fsuid;
4100 vol_info->UNC = master_tcon->treeName;
4101 vol_info->retry = master_tcon->retry;
4102 vol_info->nocase = master_tcon->nocase;
4103 vol_info->local_lease = master_tcon->local_lease;
4104 vol_info->no_linux_ext = !master_tcon->unix_ext;
4105 vol_info->sectype = master_tcon->ses->sectype;
4106 vol_info->sign = master_tcon->ses->sign;
4108 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4109 if (rc) {
4110 tcon = ERR_PTR(rc);
4111 goto out;
4114 /* get a reference for the same TCP session */
4115 spin_lock(&cifs_tcp_ses_lock);
4116 ++master_tcon->ses->server->srv_count;
4117 spin_unlock(&cifs_tcp_ses_lock);
4119 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4120 if (IS_ERR(ses)) {
4121 tcon = (struct cifs_tcon *)ses;
4122 cifs_put_tcp_session(master_tcon->ses->server, 0);
4123 goto out;
4126 tcon = cifs_get_tcon(ses, vol_info);
4127 if (IS_ERR(tcon)) {
4128 cifs_put_smb_ses(ses);
4129 goto out;
4132 if (cap_unix(ses))
4133 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4134 out:
4135 kfree(vol_info->username);
4136 kfree(vol_info->password);
4137 kfree(vol_info);
4139 return tcon;
4142 struct cifs_tcon *
4143 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4145 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4148 /* find and return a tlink with given uid */
4149 static struct tcon_link *
4150 tlink_rb_search(struct rb_root *root, kuid_t uid)
4152 struct rb_node *node = root->rb_node;
4153 struct tcon_link *tlink;
4155 while (node) {
4156 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4158 if (uid_gt(tlink->tl_uid, uid))
4159 node = node->rb_left;
4160 else if (uid_lt(tlink->tl_uid, uid))
4161 node = node->rb_right;
4162 else
4163 return tlink;
4165 return NULL;
4168 /* insert a tcon_link into the tree */
4169 static void
4170 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4172 struct rb_node **new = &(root->rb_node), *parent = NULL;
4173 struct tcon_link *tlink;
4175 while (*new) {
4176 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4177 parent = *new;
4179 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4180 new = &((*new)->rb_left);
4181 else
4182 new = &((*new)->rb_right);
4185 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4186 rb_insert_color(&new_tlink->tl_rbnode, root);
4190 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4191 * current task.
4193 * If the superblock doesn't refer to a multiuser mount, then just return
4194 * the master tcon for the mount.
4196 * First, search the rbtree for an existing tcon for this fsuid. If one
4197 * exists, then check to see if it's pending construction. If it is then wait
4198 * for construction to complete. Once it's no longer pending, check to see if
4199 * it failed and either return an error or retry construction, depending on
4200 * the timeout.
4202 * If one doesn't exist then insert a new tcon_link struct into the tree and
4203 * try to construct a new one.
4205 struct tcon_link *
4206 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4208 int ret;
4209 kuid_t fsuid = current_fsuid();
4210 struct tcon_link *tlink, *newtlink;
4212 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4213 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4215 spin_lock(&cifs_sb->tlink_tree_lock);
4216 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4217 if (tlink)
4218 cifs_get_tlink(tlink);
4219 spin_unlock(&cifs_sb->tlink_tree_lock);
4221 if (tlink == NULL) {
4222 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4223 if (newtlink == NULL)
4224 return ERR_PTR(-ENOMEM);
4225 newtlink->tl_uid = fsuid;
4226 newtlink->tl_tcon = ERR_PTR(-EACCES);
4227 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4228 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4229 cifs_get_tlink(newtlink);
4231 spin_lock(&cifs_sb->tlink_tree_lock);
4232 /* was one inserted after previous search? */
4233 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4234 if (tlink) {
4235 cifs_get_tlink(tlink);
4236 spin_unlock(&cifs_sb->tlink_tree_lock);
4237 kfree(newtlink);
4238 goto wait_for_construction;
4240 tlink = newtlink;
4241 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4242 spin_unlock(&cifs_sb->tlink_tree_lock);
4243 } else {
4244 wait_for_construction:
4245 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4246 TASK_INTERRUPTIBLE);
4247 if (ret) {
4248 cifs_put_tlink(tlink);
4249 return ERR_PTR(-ERESTARTSYS);
4252 /* if it's good, return it */
4253 if (!IS_ERR(tlink->tl_tcon))
4254 return tlink;
4256 /* return error if we tried this already recently */
4257 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4258 cifs_put_tlink(tlink);
4259 return ERR_PTR(-EACCES);
4262 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4263 goto wait_for_construction;
4266 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4267 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4268 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4270 if (IS_ERR(tlink->tl_tcon)) {
4271 cifs_put_tlink(tlink);
4272 return ERR_PTR(-EACCES);
4275 return tlink;
4279 * periodic workqueue job that scans tcon_tree for a superblock and closes
4280 * out tcons.
4282 static void
4283 cifs_prune_tlinks(struct work_struct *work)
4285 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4286 prune_tlinks.work);
4287 struct rb_root *root = &cifs_sb->tlink_tree;
4288 struct rb_node *node = rb_first(root);
4289 struct rb_node *tmp;
4290 struct tcon_link *tlink;
4293 * Because we drop the spinlock in the loop in order to put the tlink
4294 * it's not guarded against removal of links from the tree. The only
4295 * places that remove entries from the tree are this function and
4296 * umounts. Because this function is non-reentrant and is canceled
4297 * before umount can proceed, this is safe.
4299 spin_lock(&cifs_sb->tlink_tree_lock);
4300 node = rb_first(root);
4301 while (node != NULL) {
4302 tmp = node;
4303 node = rb_next(tmp);
4304 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4306 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4307 atomic_read(&tlink->tl_count) != 0 ||
4308 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4309 continue;
4311 cifs_get_tlink(tlink);
4312 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4313 rb_erase(tmp, root);
4315 spin_unlock(&cifs_sb->tlink_tree_lock);
4316 cifs_put_tlink(tlink);
4317 spin_lock(&cifs_sb->tlink_tree_lock);
4319 spin_unlock(&cifs_sb->tlink_tree_lock);
4321 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4322 TLINK_IDLE_EXPIRE);