gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / fs / cifs / connect.c
blob7d7bd466520b12d88a5ff051425e6953dc545165
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,
94 /* Mount options which take numeric value */
95 Opt_backupuid, Opt_backupgid, Opt_uid,
96 Opt_cruid, Opt_gid, Opt_file_mode,
97 Opt_dirmode, Opt_port,
98 Opt_rsize, Opt_wsize, Opt_actimeo,
100 /* Mount options which take string value */
101 Opt_user, Opt_pass, Opt_ip,
102 Opt_domain, Opt_srcaddr, Opt_iocharset,
103 Opt_netbiosname, Opt_servern,
104 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
106 /* Mount options to be ignored */
107 Opt_ignore,
109 /* Options which could be blank */
110 Opt_blank_pass,
111 Opt_blank_user,
112 Opt_blank_ip,
114 Opt_err
117 static const match_table_t cifs_mount_option_tokens = {
119 { Opt_user_xattr, "user_xattr" },
120 { Opt_nouser_xattr, "nouser_xattr" },
121 { Opt_forceuid, "forceuid" },
122 { Opt_noforceuid, "noforceuid" },
123 { Opt_forcegid, "forcegid" },
124 { Opt_noforcegid, "noforcegid" },
125 { Opt_noblocksend, "noblocksend" },
126 { Opt_noautotune, "noautotune" },
127 { Opt_hard, "hard" },
128 { Opt_soft, "soft" },
129 { Opt_perm, "perm" },
130 { Opt_noperm, "noperm" },
131 { Opt_mapchars, "mapchars" }, /* SFU style */
132 { Opt_nomapchars, "nomapchars" },
133 { Opt_mapposix, "mapposix" }, /* SFM style */
134 { Opt_nomapposix, "nomapposix" },
135 { Opt_sfu, "sfu" },
136 { Opt_nosfu, "nosfu" },
137 { Opt_nodfs, "nodfs" },
138 { Opt_posixpaths, "posixpaths" },
139 { Opt_noposixpaths, "noposixpaths" },
140 { Opt_nounix, "nounix" },
141 { Opt_nounix, "nolinux" },
142 { Opt_nocase, "nocase" },
143 { Opt_nocase, "ignorecase" },
144 { Opt_brl, "brl" },
145 { Opt_nobrl, "nobrl" },
146 { Opt_nobrl, "nolock" },
147 { Opt_forcemandatorylock, "forcemandatorylock" },
148 { Opt_forcemandatorylock, "forcemand" },
149 { Opt_setuids, "setuids" },
150 { Opt_nosetuids, "nosetuids" },
151 { Opt_dynperm, "dynperm" },
152 { Opt_nodynperm, "nodynperm" },
153 { Opt_nohard, "nohard" },
154 { Opt_nosoft, "nosoft" },
155 { Opt_nointr, "nointr" },
156 { Opt_intr, "intr" },
157 { Opt_nostrictsync, "nostrictsync" },
158 { Opt_strictsync, "strictsync" },
159 { Opt_serverino, "serverino" },
160 { Opt_noserverino, "noserverino" },
161 { Opt_rwpidforward, "rwpidforward" },
162 { Opt_cifsacl, "cifsacl" },
163 { Opt_nocifsacl, "nocifsacl" },
164 { Opt_acl, "acl" },
165 { Opt_noacl, "noacl" },
166 { Opt_locallease, "locallease" },
167 { Opt_sign, "sign" },
168 { Opt_seal, "seal" },
169 { Opt_noac, "noac" },
170 { Opt_fsc, "fsc" },
171 { Opt_mfsymlinks, "mfsymlinks" },
172 { Opt_multiuser, "multiuser" },
173 { Opt_sloppy, "sloppy" },
174 { Opt_nosharesock, "nosharesock" },
176 { Opt_backupuid, "backupuid=%s" },
177 { Opt_backupgid, "backupgid=%s" },
178 { Opt_uid, "uid=%s" },
179 { Opt_cruid, "cruid=%s" },
180 { Opt_gid, "gid=%s" },
181 { Opt_file_mode, "file_mode=%s" },
182 { Opt_dirmode, "dirmode=%s" },
183 { Opt_dirmode, "dir_mode=%s" },
184 { Opt_port, "port=%s" },
185 { Opt_rsize, "rsize=%s" },
186 { Opt_wsize, "wsize=%s" },
187 { Opt_actimeo, "actimeo=%s" },
189 { Opt_blank_user, "user=" },
190 { Opt_blank_user, "username=" },
191 { Opt_user, "user=%s" },
192 { Opt_user, "username=%s" },
193 { Opt_blank_pass, "pass=" },
194 { Opt_blank_pass, "password=" },
195 { Opt_pass, "pass=%s" },
196 { Opt_pass, "password=%s" },
197 { Opt_blank_ip, "ip=" },
198 { Opt_blank_ip, "addr=" },
199 { Opt_ip, "ip=%s" },
200 { Opt_ip, "addr=%s" },
201 { Opt_ignore, "unc=%s" },
202 { Opt_ignore, "target=%s" },
203 { Opt_ignore, "path=%s" },
204 { Opt_domain, "dom=%s" },
205 { Opt_domain, "domain=%s" },
206 { Opt_domain, "workgroup=%s" },
207 { Opt_srcaddr, "srcaddr=%s" },
208 { Opt_ignore, "prefixpath=%s" },
209 { Opt_iocharset, "iocharset=%s" },
210 { Opt_netbiosname, "netbiosname=%s" },
211 { Opt_servern, "servern=%s" },
212 { Opt_ver, "ver=%s" },
213 { Opt_vers, "vers=%s" },
214 { Opt_sec, "sec=%s" },
215 { Opt_cache, "cache=%s" },
217 { Opt_ignore, "cred" },
218 { Opt_ignore, "credentials" },
219 { Opt_ignore, "cred=%s" },
220 { Opt_ignore, "credentials=%s" },
221 { Opt_ignore, "guest" },
222 { Opt_ignore, "rw" },
223 { Opt_ignore, "ro" },
224 { Opt_ignore, "suid" },
225 { Opt_ignore, "nosuid" },
226 { Opt_ignore, "exec" },
227 { Opt_ignore, "noexec" },
228 { Opt_ignore, "nodev" },
229 { Opt_ignore, "noauto" },
230 { Opt_ignore, "dev" },
231 { Opt_ignore, "mand" },
232 { Opt_ignore, "nomand" },
233 { Opt_ignore, "_netdev" },
235 { Opt_err, NULL }
238 enum {
239 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
240 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
241 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
242 Opt_sec_ntlmv2i, Opt_sec_lanman,
243 Opt_sec_none,
245 Opt_sec_err
248 static const match_table_t cifs_secflavor_tokens = {
249 { Opt_sec_krb5, "krb5" },
250 { Opt_sec_krb5i, "krb5i" },
251 { Opt_sec_krb5p, "krb5p" },
252 { Opt_sec_ntlmsspi, "ntlmsspi" },
253 { Opt_sec_ntlmssp, "ntlmssp" },
254 { Opt_ntlm, "ntlm" },
255 { Opt_sec_ntlmi, "ntlmi" },
256 { Opt_sec_ntlmv2, "nontlm" },
257 { Opt_sec_ntlmv2, "ntlmv2" },
258 { Opt_sec_ntlmv2i, "ntlmv2i" },
259 { Opt_sec_lanman, "lanman" },
260 { Opt_sec_none, "none" },
262 { Opt_sec_err, NULL }
265 /* cache flavors */
266 enum {
267 Opt_cache_loose,
268 Opt_cache_strict,
269 Opt_cache_none,
270 Opt_cache_err
273 static const match_table_t cifs_cacheflavor_tokens = {
274 { Opt_cache_loose, "loose" },
275 { Opt_cache_strict, "strict" },
276 { Opt_cache_none, "none" },
277 { Opt_cache_err, NULL }
280 static const match_table_t cifs_smb_version_tokens = {
281 { Smb_1, SMB1_VERSION_STRING },
282 { Smb_20, SMB20_VERSION_STRING},
283 { Smb_21, SMB21_VERSION_STRING },
284 { Smb_30, SMB30_VERSION_STRING },
285 { Smb_302, SMB302_VERSION_STRING },
288 static int ip_connect(struct TCP_Server_Info *server);
289 static int generic_ip_connect(struct TCP_Server_Info *server);
290 static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
291 static void cifs_prune_tlinks(struct work_struct *work);
292 static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
293 const char *devname);
296 * cifs tcp session reconnection
298 * mark tcp session as reconnecting so temporarily locked
299 * mark all smb sessions as reconnecting for tcp session
300 * reconnect tcp session
301 * wake up waiters on reconnection? - (not needed currently)
304 cifs_reconnect(struct TCP_Server_Info *server)
306 int rc = 0;
307 struct list_head *tmp, *tmp2;
308 struct cifs_ses *ses;
309 struct cifs_tcon *tcon;
310 struct mid_q_entry *mid_entry;
311 struct list_head retry_list;
313 spin_lock(&GlobalMid_Lock);
314 if (server->tcpStatus == CifsExiting) {
315 /* the demux thread will exit normally
316 next time through the loop */
317 spin_unlock(&GlobalMid_Lock);
318 return rc;
319 } else
320 server->tcpStatus = CifsNeedReconnect;
321 spin_unlock(&GlobalMid_Lock);
322 server->maxBuf = 0;
323 #ifdef CONFIG_CIFS_SMB2
324 server->max_read = 0;
325 #endif
327 cifs_dbg(FYI, "Reconnecting tcp session\n");
329 /* before reconnecting the tcp session, mark the smb session (uid)
330 and the tid bad so they are not used until reconnected */
331 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
332 __func__);
333 spin_lock(&cifs_tcp_ses_lock);
334 list_for_each(tmp, &server->smb_ses_list) {
335 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
336 ses->need_reconnect = true;
337 ses->ipc_tid = 0;
338 list_for_each(tmp2, &ses->tcon_list) {
339 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
340 tcon->need_reconnect = true;
343 spin_unlock(&cifs_tcp_ses_lock);
345 /* do not want to be sending data on a socket we are freeing */
346 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
347 mutex_lock(&server->srv_mutex);
348 if (server->ssocket) {
349 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
350 server->ssocket->state, server->ssocket->flags);
351 kernel_sock_shutdown(server->ssocket, SHUT_WR);
352 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
353 server->ssocket->state, server->ssocket->flags);
354 sock_release(server->ssocket);
355 server->ssocket = NULL;
357 server->sequence_number = 0;
358 server->session_estab = false;
359 kfree(server->session_key.response);
360 server->session_key.response = NULL;
361 server->session_key.len = 0;
362 server->lstrp = jiffies;
364 /* mark submitted MIDs for retry and issue callback */
365 INIT_LIST_HEAD(&retry_list);
366 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
367 spin_lock(&GlobalMid_Lock);
368 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
369 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
370 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
371 mid_entry->mid_state = MID_RETRY_NEEDED;
372 list_move(&mid_entry->qhead, &retry_list);
374 spin_unlock(&GlobalMid_Lock);
375 mutex_unlock(&server->srv_mutex);
377 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
378 list_for_each_safe(tmp, tmp2, &retry_list) {
379 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
380 list_del_init(&mid_entry->qhead);
381 mid_entry->callback(mid_entry);
384 do {
385 try_to_freeze();
387 /* we should try only the port we connected to before */
388 mutex_lock(&server->srv_mutex);
389 rc = generic_ip_connect(server);
390 if (rc) {
391 cifs_dbg(FYI, "reconnect error %d\n", rc);
392 mutex_unlock(&server->srv_mutex);
393 msleep(3000);
394 } else {
395 atomic_inc(&tcpSesReconnectCount);
396 spin_lock(&GlobalMid_Lock);
397 if (server->tcpStatus != CifsExiting)
398 server->tcpStatus = CifsNeedNegotiate;
399 spin_unlock(&GlobalMid_Lock);
400 mutex_unlock(&server->srv_mutex);
402 } while (server->tcpStatus == CifsNeedReconnect);
404 return rc;
407 static void
408 cifs_echo_request(struct work_struct *work)
410 int rc;
411 struct TCP_Server_Info *server = container_of(work,
412 struct TCP_Server_Info, echo.work);
415 * We cannot send an echo if it is disabled or until the
416 * NEGOTIATE_PROTOCOL request is done, which is indicated by
417 * server->ops->need_neg() == true. Also, no need to ping if
418 * we got a response recently.
421 if (server->tcpStatus == CifsNeedReconnect ||
422 server->tcpStatus == CifsExiting || server->tcpStatus == CifsNew ||
423 (server->ops->can_echo && !server->ops->can_echo(server)) ||
424 time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
425 goto requeue_echo;
427 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
428 if (rc)
429 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
430 server->hostname);
432 requeue_echo:
433 queue_delayed_work(cifsiod_wq, &server->echo, SMB_ECHO_INTERVAL);
436 static bool
437 allocate_buffers(struct TCP_Server_Info *server)
439 if (!server->bigbuf) {
440 server->bigbuf = (char *)cifs_buf_get();
441 if (!server->bigbuf) {
442 cifs_dbg(VFS, "No memory for large SMB response\n");
443 msleep(3000);
444 /* retry will check if exiting */
445 return false;
447 } else if (server->large_buf) {
448 /* we are reusing a dirty large buf, clear its start */
449 memset(server->bigbuf, 0, HEADER_SIZE(server));
452 if (!server->smallbuf) {
453 server->smallbuf = (char *)cifs_small_buf_get();
454 if (!server->smallbuf) {
455 cifs_dbg(VFS, "No memory for SMB response\n");
456 msleep(1000);
457 /* retry will check if exiting */
458 return false;
460 /* beginning of smb buffer is cleared in our buf_get */
461 } else {
462 /* if existing small buf clear beginning */
463 memset(server->smallbuf, 0, HEADER_SIZE(server));
466 return true;
469 static bool
470 server_unresponsive(struct TCP_Server_Info *server)
473 * We need to wait 2 echo intervals to make sure we handle such
474 * situations right:
475 * 1s client sends a normal SMB request
476 * 2s client gets a response
477 * 30s echo workqueue job pops, and decides we got a response recently
478 * and don't need to send another
479 * ...
480 * 65s kernel_recvmsg times out, and we see that we haven't gotten
481 * a response in >60s.
483 if (server->tcpStatus == CifsGood &&
484 time_after(jiffies, server->lstrp + 2 * SMB_ECHO_INTERVAL)) {
485 cifs_dbg(VFS, "Server %s has not responded in %d seconds. Reconnecting...\n",
486 server->hostname, (2 * SMB_ECHO_INTERVAL) / HZ);
487 cifs_reconnect(server);
488 wake_up(&server->response_q);
489 return true;
492 return false;
496 * kvec_array_init - clone a kvec array, and advance into it
497 * @new: pointer to memory for cloned array
498 * @iov: pointer to original array
499 * @nr_segs: number of members in original array
500 * @bytes: number of bytes to advance into the cloned array
502 * This function will copy the array provided in iov to a section of memory
503 * and advance the specified number of bytes into the new array. It returns
504 * the number of segments in the new array. "new" must be at least as big as
505 * the original iov array.
507 static unsigned int
508 kvec_array_init(struct kvec *new, struct kvec *iov, unsigned int nr_segs,
509 size_t bytes)
511 size_t base = 0;
513 while (bytes || !iov->iov_len) {
514 int copy = min(bytes, iov->iov_len);
516 bytes -= copy;
517 base += copy;
518 if (iov->iov_len == base) {
519 iov++;
520 nr_segs--;
521 base = 0;
524 memcpy(new, iov, sizeof(*iov) * nr_segs);
525 new->iov_base += base;
526 new->iov_len -= base;
527 return nr_segs;
530 static struct kvec *
531 get_server_iovec(struct TCP_Server_Info *server, unsigned int nr_segs)
533 struct kvec *new_iov;
535 if (server->iov && nr_segs <= server->nr_iov)
536 return server->iov;
538 /* not big enough -- allocate a new one and release the old */
539 new_iov = kmalloc(sizeof(*new_iov) * nr_segs, GFP_NOFS);
540 if (new_iov) {
541 kfree(server->iov);
542 server->iov = new_iov;
543 server->nr_iov = nr_segs;
545 return new_iov;
549 cifs_readv_from_socket(struct TCP_Server_Info *server, struct kvec *iov_orig,
550 unsigned int nr_segs, unsigned int to_read)
552 int length = 0;
553 int total_read;
554 unsigned int segs;
555 struct msghdr smb_msg;
556 struct kvec *iov;
558 iov = get_server_iovec(server, nr_segs);
559 if (!iov)
560 return -ENOMEM;
562 smb_msg.msg_control = NULL;
563 smb_msg.msg_controllen = 0;
565 for (total_read = 0; to_read; total_read += length, to_read -= length) {
566 try_to_freeze();
568 if (server_unresponsive(server)) {
569 total_read = -ECONNABORTED;
570 break;
573 segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
575 length = kernel_recvmsg(server->ssocket, &smb_msg,
576 iov, segs, to_read, 0);
578 if (server->tcpStatus == CifsExiting) {
579 total_read = -ESHUTDOWN;
580 break;
581 } else if (server->tcpStatus == CifsNeedReconnect) {
582 cifs_reconnect(server);
583 total_read = -ECONNABORTED;
584 break;
585 } else if (length == -ERESTARTSYS ||
586 length == -EAGAIN ||
587 length == -EINTR) {
589 * Minimum sleep to prevent looping, allowing socket
590 * to clear and app threads to set tcpStatus
591 * CifsNeedReconnect if server hung.
593 usleep_range(1000, 2000);
594 length = 0;
595 continue;
596 } else if (length <= 0) {
597 cifs_dbg(FYI, "Received no data or error: expecting %d\n"
598 "got %d", to_read, length);
599 cifs_reconnect(server);
600 total_read = -ECONNABORTED;
601 break;
604 return total_read;
608 cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
609 unsigned int to_read)
611 struct kvec iov;
613 iov.iov_base = buf;
614 iov.iov_len = to_read;
616 return cifs_readv_from_socket(server, &iov, 1, to_read);
619 static bool
620 is_smb_response(struct TCP_Server_Info *server, unsigned char type)
623 * The first byte big endian of the length field,
624 * is actually not part of the length but the type
625 * with the most common, zero, as regular data.
627 switch (type) {
628 case RFC1002_SESSION_MESSAGE:
629 /* Regular SMB response */
630 return true;
631 case RFC1002_SESSION_KEEP_ALIVE:
632 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
633 break;
634 case RFC1002_POSITIVE_SESSION_RESPONSE:
635 cifs_dbg(FYI, "RFC 1002 positive session response\n");
636 break;
637 case RFC1002_NEGATIVE_SESSION_RESPONSE:
639 * We get this from Windows 98 instead of an error on
640 * SMB negprot response.
642 cifs_dbg(FYI, "RFC 1002 negative session response\n");
643 /* give server a second to clean up */
644 msleep(1000);
646 * Always try 445 first on reconnect since we get NACK
647 * on some if we ever connected to port 139 (the NACK
648 * is since we do not begin with RFC1001 session
649 * initialize frame).
651 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
652 cifs_reconnect(server);
653 wake_up(&server->response_q);
654 break;
655 default:
656 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
657 cifs_reconnect(server);
660 return false;
663 void
664 dequeue_mid(struct mid_q_entry *mid, bool malformed)
666 #ifdef CONFIG_CIFS_STATS2
667 mid->when_received = jiffies;
668 #endif
669 spin_lock(&GlobalMid_Lock);
670 if (!malformed)
671 mid->mid_state = MID_RESPONSE_RECEIVED;
672 else
673 mid->mid_state = MID_RESPONSE_MALFORMED;
674 list_del_init(&mid->qhead);
675 spin_unlock(&GlobalMid_Lock);
678 static void
679 handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
680 char *buf, int malformed)
682 if (server->ops->check_trans2 &&
683 server->ops->check_trans2(mid, server, buf, malformed))
684 return;
685 mid->resp_buf = buf;
686 mid->large_buf = server->large_buf;
687 /* Was previous buf put in mpx struct for multi-rsp? */
688 if (!mid->multiRsp) {
689 /* smb buffer will be freed by user thread */
690 if (server->large_buf)
691 server->bigbuf = NULL;
692 else
693 server->smallbuf = NULL;
695 dequeue_mid(mid, malformed);
698 static void clean_demultiplex_info(struct TCP_Server_Info *server)
700 int length;
702 /* take it off the list, if it's not already */
703 spin_lock(&cifs_tcp_ses_lock);
704 list_del_init(&server->tcp_ses_list);
705 spin_unlock(&cifs_tcp_ses_lock);
707 spin_lock(&GlobalMid_Lock);
708 server->tcpStatus = CifsExiting;
709 spin_unlock(&GlobalMid_Lock);
710 wake_up_all(&server->response_q);
712 /* check if we have blocked requests that need to free */
713 spin_lock(&server->req_lock);
714 if (server->credits <= 0)
715 server->credits = 1;
716 spin_unlock(&server->req_lock);
718 * Although there should not be any requests blocked on this queue it
719 * can not hurt to be paranoid and try to wake up requests that may
720 * haven been blocked when more than 50 at time were on the wire to the
721 * same server - they now will see the session is in exit state and get
722 * out of SendReceive.
724 wake_up_all(&server->request_q);
725 /* give those requests time to exit */
726 msleep(125);
728 if (server->ssocket) {
729 sock_release(server->ssocket);
730 server->ssocket = NULL;
733 if (!list_empty(&server->pending_mid_q)) {
734 struct list_head dispose_list;
735 struct mid_q_entry *mid_entry;
736 struct list_head *tmp, *tmp2;
738 INIT_LIST_HEAD(&dispose_list);
739 spin_lock(&GlobalMid_Lock);
740 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
741 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
742 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
743 mid_entry->mid_state = MID_SHUTDOWN;
744 list_move(&mid_entry->qhead, &dispose_list);
746 spin_unlock(&GlobalMid_Lock);
748 /* now walk dispose list and issue callbacks */
749 list_for_each_safe(tmp, tmp2, &dispose_list) {
750 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
751 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
752 list_del_init(&mid_entry->qhead);
753 mid_entry->callback(mid_entry);
755 /* 1/8th of sec is more than enough time for them to exit */
756 msleep(125);
759 if (!list_empty(&server->pending_mid_q)) {
761 * mpx threads have not exited yet give them at least the smb
762 * send timeout time for long ops.
764 * Due to delays on oplock break requests, we need to wait at
765 * least 45 seconds before giving up on a request getting a
766 * response and going ahead and killing cifsd.
768 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
769 msleep(46000);
771 * If threads still have not exited they are probably never
772 * coming home not much else we can do but free the memory.
776 kfree(server->hostname);
777 kfree(server->iov);
778 kfree(server);
780 length = atomic_dec_return(&tcpSesAllocCount);
781 if (length > 0)
782 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
785 static int
786 standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
788 int length;
789 char *buf = server->smallbuf;
790 unsigned int pdu_length = get_rfc1002_length(buf);
792 /* make sure this will fit in a large buffer */
793 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
794 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
795 cifs_reconnect(server);
796 wake_up(&server->response_q);
797 return -ECONNABORTED;
800 /* switch to large buffer if too big for a small one */
801 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
802 server->large_buf = true;
803 memcpy(server->bigbuf, buf, server->total_read);
804 buf = server->bigbuf;
807 /* now read the rest */
808 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
809 pdu_length - HEADER_SIZE(server) + 1 + 4);
810 if (length < 0)
811 return length;
812 server->total_read += length;
814 dump_smb(buf, server->total_read);
817 * We know that we received enough to get to the MID as we
818 * checked the pdu_length earlier. Now check to see
819 * if the rest of the header is OK. We borrow the length
820 * var for the rest of the loop to avoid a new stack var.
822 * 48 bytes is enough to display the header and a little bit
823 * into the payload for debugging purposes.
825 length = server->ops->check_message(buf, server->total_read);
826 if (length != 0)
827 cifs_dump_mem("Bad SMB: ", buf,
828 min_t(unsigned int, server->total_read, 48));
830 if (server->ops->is_status_pending &&
831 server->ops->is_status_pending(buf, server, length))
832 return -1;
834 if (!mid)
835 return length;
837 handle_mid(mid, server, buf, length);
838 return 0;
841 static int
842 cifs_demultiplex_thread(void *p)
844 int length;
845 struct TCP_Server_Info *server = p;
846 unsigned int pdu_length;
847 char *buf = NULL;
848 struct task_struct *task_to_wake = NULL;
849 struct mid_q_entry *mid_entry;
851 current->flags |= PF_MEMALLOC;
852 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
854 length = atomic_inc_return(&tcpSesAllocCount);
855 if (length > 1)
856 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
858 set_freezable();
859 while (server->tcpStatus != CifsExiting) {
860 if (try_to_freeze())
861 continue;
863 if (!allocate_buffers(server))
864 continue;
866 server->large_buf = false;
867 buf = server->smallbuf;
868 pdu_length = 4; /* enough to get RFC1001 header */
870 length = cifs_read_from_socket(server, buf, pdu_length);
871 if (length < 0)
872 continue;
873 server->total_read = length;
876 * The right amount was read from socket - 4 bytes,
877 * so we can now interpret the length field.
879 pdu_length = get_rfc1002_length(buf);
881 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
882 if (!is_smb_response(server, buf[0]))
883 continue;
885 /* make sure we have enough to get to the MID */
886 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
887 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
888 pdu_length);
889 cifs_reconnect(server);
890 wake_up(&server->response_q);
891 continue;
894 /* read down to the MID */
895 length = cifs_read_from_socket(server, buf + 4,
896 HEADER_SIZE(server) - 1 - 4);
897 if (length < 0)
898 continue;
899 server->total_read += length;
901 mid_entry = server->ops->find_mid(server, buf);
903 if (!mid_entry || !mid_entry->receive)
904 length = standard_receive3(server, mid_entry);
905 else
906 length = mid_entry->receive(server, mid_entry);
908 if (length < 0)
909 continue;
911 if (server->large_buf)
912 buf = server->bigbuf;
914 server->lstrp = jiffies;
915 if (mid_entry != NULL) {
916 if (!mid_entry->multiRsp || mid_entry->multiEnd)
917 mid_entry->callback(mid_entry);
918 } else if (!server->ops->is_oplock_break ||
919 !server->ops->is_oplock_break(buf, server)) {
920 cifs_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
921 atomic_read(&midCount));
922 cifs_dump_mem("Received Data is: ", buf,
923 HEADER_SIZE(server));
924 #ifdef CONFIG_CIFS_DEBUG2
925 if (server->ops->dump_detail)
926 server->ops->dump_detail(buf);
927 cifs_dump_mids(server);
928 #endif /* CIFS_DEBUG2 */
931 } /* end while !EXITING */
933 /* buffer usually freed in free_mid - need to free it here on exit */
934 cifs_buf_release(server->bigbuf);
935 if (server->smallbuf) /* no sense logging a debug message if NULL */
936 cifs_small_buf_release(server->smallbuf);
938 task_to_wake = xchg(&server->tsk, NULL);
939 clean_demultiplex_info(server);
941 /* if server->tsk was NULL then wait for a signal before exiting */
942 if (!task_to_wake) {
943 set_current_state(TASK_INTERRUPTIBLE);
944 while (!signal_pending(current)) {
945 schedule();
946 set_current_state(TASK_INTERRUPTIBLE);
948 set_current_state(TASK_RUNNING);
951 module_put_and_exit(0);
954 /* extract the host portion of the UNC string */
955 static char *
956 extract_hostname(const char *unc)
958 const char *src;
959 char *dst, *delim;
960 unsigned int len;
962 /* skip double chars at beginning of string */
963 /* BB: check validity of these bytes? */
964 src = unc + 2;
966 /* delimiter between hostname and sharename is always '\\' now */
967 delim = strchr(src, '\\');
968 if (!delim)
969 return ERR_PTR(-EINVAL);
971 len = delim - src;
972 dst = kmalloc((len + 1), GFP_KERNEL);
973 if (dst == NULL)
974 return ERR_PTR(-ENOMEM);
976 memcpy(dst, src, len);
977 dst[len] = '\0';
979 return dst;
982 static int get_option_ul(substring_t args[], unsigned long *option)
984 int rc;
985 char *string;
987 string = match_strdup(args);
988 if (string == NULL)
989 return -ENOMEM;
990 rc = kstrtoul(string, 0, option);
991 kfree(string);
993 return rc;
996 static int get_option_uid(substring_t args[], kuid_t *result)
998 unsigned long value;
999 kuid_t uid;
1000 int rc;
1002 rc = get_option_ul(args, &value);
1003 if (rc)
1004 return rc;
1006 uid = make_kuid(current_user_ns(), value);
1007 if (!uid_valid(uid))
1008 return -EINVAL;
1010 *result = uid;
1011 return 0;
1014 static int get_option_gid(substring_t args[], kgid_t *result)
1016 unsigned long value;
1017 kgid_t gid;
1018 int rc;
1020 rc = get_option_ul(args, &value);
1021 if (rc)
1022 return rc;
1024 gid = make_kgid(current_user_ns(), value);
1025 if (!gid_valid(gid))
1026 return -EINVAL;
1028 *result = gid;
1029 return 0;
1032 static int cifs_parse_security_flavors(char *value,
1033 struct smb_vol *vol)
1036 substring_t args[MAX_OPT_ARGS];
1039 * With mount options, the last one should win. Reset any existing
1040 * settings back to default.
1042 vol->sectype = Unspecified;
1043 vol->sign = false;
1045 switch (match_token(value, cifs_secflavor_tokens, args)) {
1046 case Opt_sec_krb5p:
1047 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1048 return 1;
1049 case Opt_sec_krb5i:
1050 vol->sign = true;
1051 /* Fallthrough */
1052 case Opt_sec_krb5:
1053 vol->sectype = Kerberos;
1054 break;
1055 case Opt_sec_ntlmsspi:
1056 vol->sign = true;
1057 /* Fallthrough */
1058 case Opt_sec_ntlmssp:
1059 vol->sectype = RawNTLMSSP;
1060 break;
1061 case Opt_sec_ntlmi:
1062 vol->sign = true;
1063 /* Fallthrough */
1064 case Opt_ntlm:
1065 vol->sectype = NTLM;
1066 break;
1067 case Opt_sec_ntlmv2i:
1068 vol->sign = true;
1069 /* Fallthrough */
1070 case Opt_sec_ntlmv2:
1071 vol->sectype = NTLMv2;
1072 break;
1073 #ifdef CONFIG_CIFS_WEAK_PW_HASH
1074 case Opt_sec_lanman:
1075 vol->sectype = LANMAN;
1076 break;
1077 #endif
1078 case Opt_sec_none:
1079 vol->nullauth = 1;
1080 break;
1081 default:
1082 cifs_dbg(VFS, "bad security option: %s\n", value);
1083 return 1;
1086 return 0;
1089 static int
1090 cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1092 substring_t args[MAX_OPT_ARGS];
1094 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1095 case Opt_cache_loose:
1096 vol->direct_io = false;
1097 vol->strict_io = false;
1098 break;
1099 case Opt_cache_strict:
1100 vol->direct_io = false;
1101 vol->strict_io = true;
1102 break;
1103 case Opt_cache_none:
1104 vol->direct_io = true;
1105 vol->strict_io = false;
1106 break;
1107 default:
1108 cifs_dbg(VFS, "bad cache= option: %s\n", value);
1109 return 1;
1111 return 0;
1114 static int
1115 cifs_parse_smb_version(char *value, struct smb_vol *vol)
1117 substring_t args[MAX_OPT_ARGS];
1119 switch (match_token(value, cifs_smb_version_tokens, args)) {
1120 case Smb_1:
1121 vol->ops = &smb1_operations;
1122 vol->vals = &smb1_values;
1123 break;
1124 #ifdef CONFIG_CIFS_SMB2
1125 case Smb_20:
1126 vol->ops = &smb20_operations;
1127 vol->vals = &smb20_values;
1128 break;
1129 case Smb_21:
1130 vol->ops = &smb21_operations;
1131 vol->vals = &smb21_values;
1132 break;
1133 case Smb_30:
1134 vol->ops = &smb30_operations;
1135 vol->vals = &smb30_values;
1136 break;
1137 case Smb_302:
1138 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1139 vol->vals = &smb302_values;
1140 break;
1141 #endif
1142 default:
1143 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
1144 return 1;
1146 return 0;
1150 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1151 * fields with the result. Returns 0 on success and an error otherwise.
1153 static int
1154 cifs_parse_devname(const char *devname, struct smb_vol *vol)
1156 char *pos;
1157 const char *delims = "/\\";
1158 size_t len;
1160 /* make sure we have a valid UNC double delimiter prefix */
1161 len = strspn(devname, delims);
1162 if (len != 2)
1163 return -EINVAL;
1165 /* find delimiter between host and sharename */
1166 pos = strpbrk(devname + 2, delims);
1167 if (!pos)
1168 return -EINVAL;
1170 /* skip past delimiter */
1171 ++pos;
1173 /* now go until next delimiter or end of string */
1174 len = strcspn(pos, delims);
1176 /* move "pos" up to delimiter or NULL */
1177 pos += len;
1178 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1179 if (!vol->UNC)
1180 return -ENOMEM;
1182 convert_delimiter(vol->UNC, '\\');
1184 /* If pos is NULL, or is a bogus trailing delimiter then no prepath */
1185 if (!*pos++ || !*pos)
1186 return 0;
1188 vol->prepath = kstrdup(pos, GFP_KERNEL);
1189 if (!vol->prepath)
1190 return -ENOMEM;
1192 return 0;
1195 static int
1196 cifs_parse_mount_options(const char *mountdata, const char *devname,
1197 struct smb_vol *vol)
1199 char *data, *end;
1200 char *mountdata_copy = NULL, *options;
1201 unsigned int temp_len, i, j;
1202 char separator[2];
1203 short int override_uid = -1;
1204 short int override_gid = -1;
1205 bool uid_specified = false;
1206 bool gid_specified = false;
1207 bool sloppy = false;
1208 char *invalid = NULL;
1209 char *nodename = utsname()->nodename;
1210 char *string = NULL;
1211 char *tmp_end, *value;
1212 char delim;
1213 bool got_ip = false;
1214 unsigned short port = 0;
1215 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
1217 separator[0] = ',';
1218 separator[1] = 0;
1219 delim = separator[0];
1221 /* ensure we always start with zeroed-out smb_vol */
1222 memset(vol, 0, sizeof(*vol));
1225 * does not have to be perfect mapping since field is
1226 * informational, only used for servers that do not support
1227 * port 445 and it can be overridden at mount time
1229 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1230 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
1231 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1233 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
1234 /* null target name indicates to use *SMBSERVR default called name
1235 if we end up sending RFC1001 session initialize */
1236 vol->target_rfc1001_name[0] = 0;
1237 vol->cred_uid = current_uid();
1238 vol->linux_uid = current_uid();
1239 vol->linux_gid = current_gid();
1242 * default to SFM style remapping of seven reserved characters
1243 * unless user overrides it or we negotiate CIFS POSIX where
1244 * it is unnecessary. Can not simultaneously use more than one mapping
1245 * since then readdir could list files that open could not open
1247 vol->remap = true;
1249 /* default to only allowing write access to owner of the mount */
1250 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
1252 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
1253 /* default is always to request posix paths. */
1254 vol->posix_paths = 1;
1255 /* default to using server inode numbers where available */
1256 vol->server_ino = 1;
1258 /* default is to use strict cifs caching semantics */
1259 vol->strict_io = true;
1261 vol->actimeo = CIFS_DEF_ACTIMEO;
1263 /* FIXME: add autonegotiation -- for now, SMB1 is default */
1264 vol->ops = &smb1_operations;
1265 vol->vals = &smb1_values;
1267 if (!mountdata)
1268 goto cifs_parse_mount_err;
1270 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1271 if (!mountdata_copy)
1272 goto cifs_parse_mount_err;
1274 options = mountdata_copy;
1275 end = options + strlen(options);
1277 if (strncmp(options, "sep=", 4) == 0) {
1278 if (options[4] != 0) {
1279 separator[0] = options[4];
1280 options += 5;
1281 } else {
1282 cifs_dbg(FYI, "Null separator not allowed\n");
1285 vol->backupuid_specified = false; /* no backup intent for a user */
1286 vol->backupgid_specified = false; /* no backup intent for a group */
1288 switch (cifs_parse_devname(devname, vol)) {
1289 case 0:
1290 break;
1291 case -ENOMEM:
1292 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1293 goto cifs_parse_mount_err;
1294 case -EINVAL:
1295 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1296 goto cifs_parse_mount_err;
1297 default:
1298 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1299 goto cifs_parse_mount_err;
1302 while ((data = strsep(&options, separator)) != NULL) {
1303 substring_t args[MAX_OPT_ARGS];
1304 unsigned long option;
1305 int token;
1307 if (!*data)
1308 continue;
1310 token = match_token(data, cifs_mount_option_tokens, args);
1312 switch (token) {
1314 /* Ingnore the following */
1315 case Opt_ignore:
1316 break;
1318 /* Boolean values */
1319 case Opt_user_xattr:
1320 vol->no_xattr = 0;
1321 break;
1322 case Opt_nouser_xattr:
1323 vol->no_xattr = 1;
1324 break;
1325 case Opt_forceuid:
1326 override_uid = 1;
1327 break;
1328 case Opt_noforceuid:
1329 override_uid = 0;
1330 break;
1331 case Opt_forcegid:
1332 override_gid = 1;
1333 break;
1334 case Opt_noforcegid:
1335 override_gid = 0;
1336 break;
1337 case Opt_noblocksend:
1338 vol->noblocksnd = 1;
1339 break;
1340 case Opt_noautotune:
1341 vol->noautotune = 1;
1342 break;
1343 case Opt_hard:
1344 vol->retry = 1;
1345 break;
1346 case Opt_soft:
1347 vol->retry = 0;
1348 break;
1349 case Opt_perm:
1350 vol->noperm = 0;
1351 break;
1352 case Opt_noperm:
1353 vol->noperm = 1;
1354 break;
1355 case Opt_mapchars:
1356 vol->sfu_remap = true;
1357 vol->remap = false; /* disable SFM mapping */
1358 break;
1359 case Opt_nomapchars:
1360 vol->sfu_remap = false;
1361 break;
1362 case Opt_mapposix:
1363 vol->remap = true;
1364 vol->sfu_remap = false; /* disable SFU mapping */
1365 break;
1366 case Opt_nomapposix:
1367 vol->remap = false;
1368 break;
1369 case Opt_sfu:
1370 vol->sfu_emul = 1;
1371 break;
1372 case Opt_nosfu:
1373 vol->sfu_emul = 0;
1374 break;
1375 case Opt_nodfs:
1376 vol->nodfs = 1;
1377 break;
1378 case Opt_posixpaths:
1379 vol->posix_paths = 1;
1380 break;
1381 case Opt_noposixpaths:
1382 vol->posix_paths = 0;
1383 break;
1384 case Opt_nounix:
1385 vol->no_linux_ext = 1;
1386 break;
1387 case Opt_nocase:
1388 vol->nocase = 1;
1389 break;
1390 case Opt_brl:
1391 vol->nobrl = 0;
1392 break;
1393 case Opt_nobrl:
1394 vol->nobrl = 1;
1396 * turn off mandatory locking in mode
1397 * if remote locking is turned off since the
1398 * local vfs will do advisory
1400 if (vol->file_mode ==
1401 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
1402 vol->file_mode = S_IALLUGO;
1403 break;
1404 case Opt_forcemandatorylock:
1405 vol->mand_lock = 1;
1406 break;
1407 case Opt_setuids:
1408 vol->setuids = 1;
1409 break;
1410 case Opt_nosetuids:
1411 vol->setuids = 0;
1412 break;
1413 case Opt_dynperm:
1414 vol->dynperm = true;
1415 break;
1416 case Opt_nodynperm:
1417 vol->dynperm = false;
1418 break;
1419 case Opt_nohard:
1420 vol->retry = 0;
1421 break;
1422 case Opt_nosoft:
1423 vol->retry = 1;
1424 break;
1425 case Opt_nointr:
1426 vol->intr = 0;
1427 break;
1428 case Opt_intr:
1429 vol->intr = 1;
1430 break;
1431 case Opt_nostrictsync:
1432 vol->nostrictsync = 1;
1433 break;
1434 case Opt_strictsync:
1435 vol->nostrictsync = 0;
1436 break;
1437 case Opt_serverino:
1438 vol->server_ino = 1;
1439 break;
1440 case Opt_noserverino:
1441 vol->server_ino = 0;
1442 break;
1443 case Opt_rwpidforward:
1444 vol->rwpidforward = 1;
1445 break;
1446 case Opt_cifsacl:
1447 vol->cifs_acl = 1;
1448 break;
1449 case Opt_nocifsacl:
1450 vol->cifs_acl = 0;
1451 break;
1452 case Opt_acl:
1453 vol->no_psx_acl = 0;
1454 break;
1455 case Opt_noacl:
1456 vol->no_psx_acl = 1;
1457 break;
1458 case Opt_locallease:
1459 vol->local_lease = 1;
1460 break;
1461 case Opt_sign:
1462 vol->sign = true;
1463 break;
1464 case Opt_seal:
1465 /* we do not do the following in secFlags because seal
1466 * is a per tree connection (mount) not a per socket
1467 * or per-smb connection option in the protocol
1468 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1470 vol->seal = 1;
1471 break;
1472 case Opt_noac:
1473 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
1474 break;
1475 case Opt_fsc:
1476 #ifndef CONFIG_CIFS_FSCACHE
1477 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
1478 goto cifs_parse_mount_err;
1479 #endif
1480 vol->fsc = true;
1481 break;
1482 case Opt_mfsymlinks:
1483 vol->mfsymlinks = true;
1484 break;
1485 case Opt_multiuser:
1486 vol->multiuser = true;
1487 break;
1488 case Opt_sloppy:
1489 sloppy = true;
1490 break;
1491 case Opt_nosharesock:
1492 vol->nosharesock = true;
1493 break;
1495 /* Numeric Values */
1496 case Opt_backupuid:
1497 if (get_option_uid(args, &vol->backupuid)) {
1498 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1499 __func__);
1500 goto cifs_parse_mount_err;
1502 vol->backupuid_specified = true;
1503 break;
1504 case Opt_backupgid:
1505 if (get_option_gid(args, &vol->backupgid)) {
1506 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1507 __func__);
1508 goto cifs_parse_mount_err;
1510 vol->backupgid_specified = true;
1511 break;
1512 case Opt_uid:
1513 if (get_option_uid(args, &vol->linux_uid)) {
1514 cifs_dbg(VFS, "%s: Invalid uid value\n",
1515 __func__);
1516 goto cifs_parse_mount_err;
1518 uid_specified = true;
1519 break;
1520 case Opt_cruid:
1521 if (get_option_uid(args, &vol->cred_uid)) {
1522 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1523 __func__);
1524 goto cifs_parse_mount_err;
1526 break;
1527 case Opt_gid:
1528 if (get_option_gid(args, &vol->linux_gid)) {
1529 cifs_dbg(VFS, "%s: Invalid gid value\n",
1530 __func__);
1531 goto cifs_parse_mount_err;
1533 gid_specified = true;
1534 break;
1535 case Opt_file_mode:
1536 if (get_option_ul(args, &option)) {
1537 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1538 __func__);
1539 goto cifs_parse_mount_err;
1541 vol->file_mode = option;
1542 break;
1543 case Opt_dirmode:
1544 if (get_option_ul(args, &option)) {
1545 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1546 __func__);
1547 goto cifs_parse_mount_err;
1549 vol->dir_mode = option;
1550 break;
1551 case Opt_port:
1552 if (get_option_ul(args, &option) ||
1553 option > USHRT_MAX) {
1554 cifs_dbg(VFS, "%s: Invalid port value\n",
1555 __func__);
1556 goto cifs_parse_mount_err;
1558 port = (unsigned short)option;
1559 break;
1560 case Opt_rsize:
1561 if (get_option_ul(args, &option)) {
1562 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1563 __func__);
1564 goto cifs_parse_mount_err;
1566 vol->rsize = option;
1567 break;
1568 case Opt_wsize:
1569 if (get_option_ul(args, &option)) {
1570 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1571 __func__);
1572 goto cifs_parse_mount_err;
1574 vol->wsize = option;
1575 break;
1576 case Opt_actimeo:
1577 if (get_option_ul(args, &option)) {
1578 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1579 __func__);
1580 goto cifs_parse_mount_err;
1582 vol->actimeo = HZ * option;
1583 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1584 cifs_dbg(VFS, "attribute cache timeout too large\n");
1585 goto cifs_parse_mount_err;
1587 break;
1589 /* String Arguments */
1591 case Opt_blank_user:
1592 /* null user, ie. anonymous authentication */
1593 vol->nullauth = 1;
1594 vol->username = NULL;
1595 break;
1596 case Opt_user:
1597 string = match_strdup(args);
1598 if (string == NULL)
1599 goto out_nomem;
1601 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1602 CIFS_MAX_USERNAME_LEN) {
1603 pr_warn("CIFS: username too long\n");
1604 goto cifs_parse_mount_err;
1607 kfree(vol->username);
1608 vol->username = kstrdup(string, GFP_KERNEL);
1609 if (!vol->username)
1610 goto cifs_parse_mount_err;
1611 break;
1612 case Opt_blank_pass:
1613 /* passwords have to be handled differently
1614 * to allow the character used for deliminator
1615 * to be passed within them
1619 * Check if this is a case where the password
1620 * starts with a delimiter
1622 tmp_end = strchr(data, '=');
1623 tmp_end++;
1624 if (!(tmp_end < end && tmp_end[1] == delim)) {
1625 /* No it is not. Set the password to NULL */
1626 kfree(vol->password);
1627 vol->password = NULL;
1628 break;
1630 /* Yes it is. Drop down to Opt_pass below.*/
1631 case Opt_pass:
1632 /* Obtain the value string */
1633 value = strchr(data, '=');
1634 value++;
1636 /* Set tmp_end to end of the string */
1637 tmp_end = (char *) value + strlen(value);
1639 /* Check if following character is the deliminator
1640 * If yes, we have encountered a double deliminator
1641 * reset the NULL character to the deliminator
1643 if (tmp_end < end && tmp_end[1] == delim) {
1644 tmp_end[0] = delim;
1646 /* Keep iterating until we get to a single
1647 * deliminator OR the end
1649 while ((tmp_end = strchr(tmp_end, delim))
1650 != NULL && (tmp_end[1] == delim)) {
1651 tmp_end = (char *) &tmp_end[2];
1654 /* Reset var options to point to next element */
1655 if (tmp_end) {
1656 tmp_end[0] = '\0';
1657 options = (char *) &tmp_end[1];
1658 } else
1659 /* Reached the end of the mount option
1660 * string */
1661 options = end;
1664 kfree(vol->password);
1665 /* Now build new password string */
1666 temp_len = strlen(value);
1667 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1668 if (vol->password == NULL) {
1669 pr_warn("CIFS: no memory for password\n");
1670 goto cifs_parse_mount_err;
1673 for (i = 0, j = 0; i < temp_len; i++, j++) {
1674 vol->password[j] = value[i];
1675 if ((value[i] == delim) &&
1676 value[i+1] == delim)
1677 /* skip the second deliminator */
1678 i++;
1680 vol->password[j] = '\0';
1681 break;
1682 case Opt_blank_ip:
1683 /* FIXME: should this be an error instead? */
1684 got_ip = false;
1685 break;
1686 case Opt_ip:
1687 string = match_strdup(args);
1688 if (string == NULL)
1689 goto out_nomem;
1691 if (!cifs_convert_address(dstaddr, string,
1692 strlen(string))) {
1693 pr_err("CIFS: bad ip= option (%s).\n", string);
1694 goto cifs_parse_mount_err;
1696 got_ip = true;
1697 break;
1698 case Opt_domain:
1699 string = match_strdup(args);
1700 if (string == NULL)
1701 goto out_nomem;
1703 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1704 == CIFS_MAX_DOMAINNAME_LEN) {
1705 pr_warn("CIFS: domain name too long\n");
1706 goto cifs_parse_mount_err;
1709 kfree(vol->domainname);
1710 vol->domainname = kstrdup(string, GFP_KERNEL);
1711 if (!vol->domainname) {
1712 pr_warn("CIFS: no memory for domainname\n");
1713 goto cifs_parse_mount_err;
1715 cifs_dbg(FYI, "Domain name set\n");
1716 break;
1717 case Opt_srcaddr:
1718 string = match_strdup(args);
1719 if (string == NULL)
1720 goto out_nomem;
1722 if (!cifs_convert_address(
1723 (struct sockaddr *)&vol->srcaddr,
1724 string, strlen(string))) {
1725 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1726 string);
1727 goto cifs_parse_mount_err;
1729 break;
1730 case Opt_iocharset:
1731 string = match_strdup(args);
1732 if (string == NULL)
1733 goto out_nomem;
1735 if (strnlen(string, 1024) >= 65) {
1736 pr_warn("CIFS: iocharset name too long.\n");
1737 goto cifs_parse_mount_err;
1740 if (strncasecmp(string, "default", 7) != 0) {
1741 kfree(vol->iocharset);
1742 vol->iocharset = kstrdup(string,
1743 GFP_KERNEL);
1744 if (!vol->iocharset) {
1745 pr_warn("CIFS: no memory for charset\n");
1746 goto cifs_parse_mount_err;
1749 /* if iocharset not set then load_nls_default
1750 * is used by caller
1752 cifs_dbg(FYI, "iocharset set to %s\n", string);
1753 break;
1754 case Opt_netbiosname:
1755 string = match_strdup(args);
1756 if (string == NULL)
1757 goto out_nomem;
1759 memset(vol->source_rfc1001_name, 0x20,
1760 RFC1001_NAME_LEN);
1762 * FIXME: are there cases in which a comma can
1763 * be valid in workstation netbios name (and
1764 * need special handling)?
1766 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1767 /* don't ucase netbiosname for user */
1768 if (string[i] == 0)
1769 break;
1770 vol->source_rfc1001_name[i] = string[i];
1772 /* The string has 16th byte zero still from
1773 * set at top of the function
1775 if (i == RFC1001_NAME_LEN && string[i] != 0)
1776 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
1777 break;
1778 case Opt_servern:
1779 /* servernetbiosname specified override *SMBSERVER */
1780 string = match_strdup(args);
1781 if (string == NULL)
1782 goto out_nomem;
1784 /* last byte, type, is 0x20 for servr type */
1785 memset(vol->target_rfc1001_name, 0x20,
1786 RFC1001_NAME_LEN_WITH_NULL);
1788 /* BB are there cases in which a comma can be
1789 valid in this workstation netbios name
1790 (and need special handling)? */
1792 /* user or mount helper must uppercase the
1793 netbios name */
1794 for (i = 0; i < 15; i++) {
1795 if (string[i] == 0)
1796 break;
1797 vol->target_rfc1001_name[i] = string[i];
1799 /* The string has 16th byte zero still from
1800 set at top of the function */
1801 if (i == RFC1001_NAME_LEN && string[i] != 0)
1802 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
1803 break;
1804 case Opt_ver:
1805 string = match_strdup(args);
1806 if (string == NULL)
1807 goto out_nomem;
1809 if (strncasecmp(string, "1", 1) == 0) {
1810 /* This is the default */
1811 break;
1813 /* For all other value, error */
1814 pr_warn("CIFS: Invalid version specified\n");
1815 goto cifs_parse_mount_err;
1816 case Opt_vers:
1817 string = match_strdup(args);
1818 if (string == NULL)
1819 goto out_nomem;
1821 if (cifs_parse_smb_version(string, vol) != 0)
1822 goto cifs_parse_mount_err;
1823 break;
1824 case Opt_sec:
1825 string = match_strdup(args);
1826 if (string == NULL)
1827 goto out_nomem;
1829 if (cifs_parse_security_flavors(string, vol) != 0)
1830 goto cifs_parse_mount_err;
1831 break;
1832 case Opt_cache:
1833 string = match_strdup(args);
1834 if (string == NULL)
1835 goto out_nomem;
1837 if (cifs_parse_cache_flavor(string, vol) != 0)
1838 goto cifs_parse_mount_err;
1839 break;
1840 default:
1842 * An option we don't recognize. Save it off for later
1843 * if we haven't already found one
1845 if (!invalid)
1846 invalid = data;
1847 break;
1849 /* Free up any allocated string */
1850 kfree(string);
1851 string = NULL;
1854 if (!sloppy && invalid) {
1855 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
1856 goto cifs_parse_mount_err;
1859 #ifndef CONFIG_KEYS
1860 /* Muliuser mounts require CONFIG_KEYS support */
1861 if (vol->multiuser) {
1862 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
1863 goto cifs_parse_mount_err;
1865 #endif
1866 if (!vol->UNC) {
1867 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
1868 goto cifs_parse_mount_err;
1871 /* make sure UNC has a share name */
1872 if (!strchr(vol->UNC + 3, '\\')) {
1873 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
1874 goto cifs_parse_mount_err;
1877 if (!got_ip) {
1878 /* No ip= option specified? Try to get it from UNC */
1879 if (!cifs_convert_address(dstaddr, &vol->UNC[2],
1880 strlen(&vol->UNC[2]))) {
1881 pr_err("Unable to determine destination address.\n");
1882 goto cifs_parse_mount_err;
1886 /* set the port that we got earlier */
1887 cifs_set_port(dstaddr, port);
1889 if (uid_specified)
1890 vol->override_uid = override_uid;
1891 else if (override_uid == 1)
1892 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
1894 if (gid_specified)
1895 vol->override_gid = override_gid;
1896 else if (override_gid == 1)
1897 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
1899 kfree(mountdata_copy);
1900 return 0;
1902 out_nomem:
1903 pr_warn("Could not allocate temporary buffer\n");
1904 cifs_parse_mount_err:
1905 kfree(string);
1906 kfree(mountdata_copy);
1907 return 1;
1910 /** Returns true if srcaddr isn't specified and rhs isn't
1911 * specified, or if srcaddr is specified and
1912 * matches the IP address of the rhs argument.
1914 static bool
1915 srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
1917 switch (srcaddr->sa_family) {
1918 case AF_UNSPEC:
1919 return (rhs->sa_family == AF_UNSPEC);
1920 case AF_INET: {
1921 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1922 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1923 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1925 case AF_INET6: {
1926 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1927 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
1928 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1930 default:
1931 WARN_ON(1);
1932 return false; /* don't expect to be here */
1937 * If no port is specified in addr structure, we try to match with 445 port
1938 * and if it fails - with 139 ports. It should be called only if address
1939 * families of server and addr are equal.
1941 static bool
1942 match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1944 __be16 port, *sport;
1946 switch (addr->sa_family) {
1947 case AF_INET:
1948 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1949 port = ((struct sockaddr_in *) addr)->sin_port;
1950 break;
1951 case AF_INET6:
1952 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1953 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1954 break;
1955 default:
1956 WARN_ON(1);
1957 return false;
1960 if (!port) {
1961 port = htons(CIFS_PORT);
1962 if (port == *sport)
1963 return true;
1965 port = htons(RFC1001_PORT);
1968 return port == *sport;
1971 static bool
1972 match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1973 struct sockaddr *srcaddr)
1975 switch (addr->sa_family) {
1976 case AF_INET: {
1977 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1978 struct sockaddr_in *srv_addr4 =
1979 (struct sockaddr_in *)&server->dstaddr;
1981 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
1982 return false;
1983 break;
1985 case AF_INET6: {
1986 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1987 struct sockaddr_in6 *srv_addr6 =
1988 (struct sockaddr_in6 *)&server->dstaddr;
1990 if (!ipv6_addr_equal(&addr6->sin6_addr,
1991 &srv_addr6->sin6_addr))
1992 return false;
1993 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
1994 return false;
1995 break;
1997 default:
1998 WARN_ON(1);
1999 return false; /* don't expect to be here */
2002 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2003 return false;
2005 return true;
2008 static bool
2009 match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2012 * The select_sectype function should either return the vol->sectype
2013 * that was specified, or "Unspecified" if that sectype was not
2014 * compatible with the given NEGOTIATE request.
2016 if (select_sectype(server, vol->sectype) == Unspecified)
2017 return false;
2020 * Now check if signing mode is acceptable. No need to check
2021 * global_secflags at this point since if MUST_SIGN is set then
2022 * the server->sign had better be too.
2024 if (vol->sign && !server->sign)
2025 return false;
2027 return true;
2030 static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
2032 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2034 if (vol->nosharesock)
2035 return 0;
2037 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2038 return 0;
2040 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2041 return 0;
2043 if (!match_address(server, addr,
2044 (struct sockaddr *)&vol->srcaddr))
2045 return 0;
2047 if (!match_port(server, addr))
2048 return 0;
2050 if (!match_security(server, vol))
2051 return 0;
2053 return 1;
2056 static struct TCP_Server_Info *
2057 cifs_find_tcp_session(struct smb_vol *vol)
2059 struct TCP_Server_Info *server;
2061 spin_lock(&cifs_tcp_ses_lock);
2062 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
2063 if (!match_server(server, vol))
2064 continue;
2066 ++server->srv_count;
2067 spin_unlock(&cifs_tcp_ses_lock);
2068 cifs_dbg(FYI, "Existing tcp session with server found\n");
2069 return server;
2071 spin_unlock(&cifs_tcp_ses_lock);
2072 return NULL;
2075 void
2076 cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
2078 struct task_struct *task;
2080 spin_lock(&cifs_tcp_ses_lock);
2081 if (--server->srv_count > 0) {
2082 spin_unlock(&cifs_tcp_ses_lock);
2083 return;
2086 put_net(cifs_net_ns(server));
2088 list_del_init(&server->tcp_ses_list);
2089 spin_unlock(&cifs_tcp_ses_lock);
2091 cancel_delayed_work_sync(&server->echo);
2093 #ifdef CONFIG_CIFS_SMB2
2094 if (from_reconnect)
2096 * Avoid deadlock here: reconnect work calls
2097 * cifs_put_tcp_session() at its end. Need to be sure
2098 * that reconnect work does nothing with server pointer after
2099 * that step.
2101 cancel_delayed_work(&server->reconnect);
2102 else
2103 cancel_delayed_work_sync(&server->reconnect);
2104 #endif
2106 spin_lock(&GlobalMid_Lock);
2107 server->tcpStatus = CifsExiting;
2108 spin_unlock(&GlobalMid_Lock);
2110 cifs_crypto_shash_release(server);
2111 cifs_fscache_release_client_cookie(server);
2113 kfree(server->session_key.response);
2114 server->session_key.response = NULL;
2115 server->session_key.len = 0;
2117 task = xchg(&server->tsk, NULL);
2118 if (task)
2119 force_sig(SIGKILL, task);
2122 static struct TCP_Server_Info *
2123 cifs_get_tcp_session(struct smb_vol *volume_info)
2125 struct TCP_Server_Info *tcp_ses = NULL;
2126 int rc;
2128 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
2130 /* see if we already have a matching tcp_ses */
2131 tcp_ses = cifs_find_tcp_session(volume_info);
2132 if (tcp_ses)
2133 return tcp_ses;
2135 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2136 if (!tcp_ses) {
2137 rc = -ENOMEM;
2138 goto out_err;
2141 tcp_ses->ops = volume_info->ops;
2142 tcp_ses->vals = volume_info->vals;
2143 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
2144 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2145 if (IS_ERR(tcp_ses->hostname)) {
2146 rc = PTR_ERR(tcp_ses->hostname);
2147 goto out_err_crypto_release;
2150 tcp_ses->noblocksnd = volume_info->noblocksnd;
2151 tcp_ses->noautotune = volume_info->noautotune;
2152 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
2153 tcp_ses->in_flight = 0;
2154 tcp_ses->credits = 1;
2155 init_waitqueue_head(&tcp_ses->response_q);
2156 init_waitqueue_head(&tcp_ses->request_q);
2157 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2158 mutex_init(&tcp_ses->srv_mutex);
2159 memcpy(tcp_ses->workstation_RFC1001_name,
2160 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2161 memcpy(tcp_ses->server_RFC1001_name,
2162 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2163 tcp_ses->session_estab = false;
2164 tcp_ses->sequence_number = 0;
2165 tcp_ses->lstrp = jiffies;
2166 spin_lock_init(&tcp_ses->req_lock);
2167 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2168 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
2169 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
2170 #ifdef CONFIG_CIFS_SMB2
2171 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2172 mutex_init(&tcp_ses->reconnect_mutex);
2173 #endif
2174 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2175 sizeof(tcp_ses->srcaddr));
2176 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2177 sizeof(tcp_ses->dstaddr));
2178 #ifdef CONFIG_CIFS_SMB2
2179 get_random_bytes(tcp_ses->client_guid, SMB2_CLIENT_GUID_SIZE);
2180 #endif
2182 * at this point we are the only ones with the pointer
2183 * to the struct since the kernel thread not created yet
2184 * no need to spinlock this init of tcpStatus or srv_count
2186 tcp_ses->tcpStatus = CifsNew;
2187 ++tcp_ses->srv_count;
2189 rc = ip_connect(tcp_ses);
2190 if (rc < 0) {
2191 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
2192 goto out_err_crypto_release;
2196 * since we're in a cifs function already, we know that
2197 * this will succeed. No need for try_module_get().
2199 __module_get(THIS_MODULE);
2200 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
2201 tcp_ses, "cifsd");
2202 if (IS_ERR(tcp_ses->tsk)) {
2203 rc = PTR_ERR(tcp_ses->tsk);
2204 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
2205 module_put(THIS_MODULE);
2206 goto out_err_crypto_release;
2208 tcp_ses->tcpStatus = CifsNeedNegotiate;
2210 /* thread spawned, put it on the list */
2211 spin_lock(&cifs_tcp_ses_lock);
2212 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
2213 spin_unlock(&cifs_tcp_ses_lock);
2215 cifs_fscache_get_client_cookie(tcp_ses);
2217 /* queue echo request delayed work */
2218 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
2220 return tcp_ses;
2222 out_err_crypto_release:
2223 cifs_crypto_shash_release(tcp_ses);
2225 put_net(cifs_net_ns(tcp_ses));
2227 out_err:
2228 if (tcp_ses) {
2229 if (!IS_ERR(tcp_ses->hostname))
2230 kfree(tcp_ses->hostname);
2231 if (tcp_ses->ssocket)
2232 sock_release(tcp_ses->ssocket);
2233 kfree(tcp_ses);
2235 return ERR_PTR(rc);
2238 static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
2240 if (vol->sectype != Unspecified &&
2241 vol->sectype != ses->sectype)
2242 return 0;
2244 switch (ses->sectype) {
2245 case Kerberos:
2246 if (!uid_eq(vol->cred_uid, ses->cred_uid))
2247 return 0;
2248 break;
2249 default:
2250 /* NULL username means anonymous session */
2251 if (ses->user_name == NULL) {
2252 if (!vol->nullauth)
2253 return 0;
2254 break;
2257 /* anything else takes username/password */
2258 if (strncmp(ses->user_name,
2259 vol->username ? vol->username : "",
2260 CIFS_MAX_USERNAME_LEN))
2261 return 0;
2262 if ((vol->username && strlen(vol->username) != 0) &&
2263 ses->password != NULL &&
2264 strncmp(ses->password,
2265 vol->password ? vol->password : "",
2266 CIFS_MAX_PASSWORD_LEN))
2267 return 0;
2269 return 1;
2272 static struct cifs_ses *
2273 cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
2275 struct cifs_ses *ses;
2277 spin_lock(&cifs_tcp_ses_lock);
2278 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
2279 if (ses->status == CifsExiting)
2280 continue;
2281 if (!match_session(ses, vol))
2282 continue;
2283 ++ses->ses_count;
2284 spin_unlock(&cifs_tcp_ses_lock);
2285 return ses;
2287 spin_unlock(&cifs_tcp_ses_lock);
2288 return NULL;
2291 static void
2292 cifs_put_smb_ses(struct cifs_ses *ses)
2294 unsigned int rc, xid;
2295 struct TCP_Server_Info *server = ses->server;
2297 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
2299 spin_lock(&cifs_tcp_ses_lock);
2300 if (ses->status == CifsExiting) {
2301 spin_unlock(&cifs_tcp_ses_lock);
2302 return;
2304 if (--ses->ses_count > 0) {
2305 spin_unlock(&cifs_tcp_ses_lock);
2306 return;
2308 if (ses->status == CifsGood)
2309 ses->status = CifsExiting;
2310 spin_unlock(&cifs_tcp_ses_lock);
2312 if (ses->status == CifsExiting && server->ops->logoff) {
2313 xid = get_xid();
2314 rc = server->ops->logoff(xid, ses);
2315 if (rc)
2316 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2317 __func__, rc);
2318 _free_xid(xid);
2321 spin_lock(&cifs_tcp_ses_lock);
2322 list_del_init(&ses->smb_ses_list);
2323 spin_unlock(&cifs_tcp_ses_lock);
2325 sesInfoFree(ses);
2326 cifs_put_tcp_session(server, 0);
2329 #ifdef CONFIG_KEYS
2331 /* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2332 #define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
2334 /* Populate username and pw fields from keyring if possible */
2335 static int
2336 cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2338 int rc = 0;
2339 char *desc, *delim, *payload;
2340 ssize_t len;
2341 struct key *key;
2342 struct TCP_Server_Info *server = ses->server;
2343 struct sockaddr_in *sa;
2344 struct sockaddr_in6 *sa6;
2345 struct user_key_payload *upayload;
2347 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2348 if (!desc)
2349 return -ENOMEM;
2351 /* try to find an address key first */
2352 switch (server->dstaddr.ss_family) {
2353 case AF_INET:
2354 sa = (struct sockaddr_in *)&server->dstaddr;
2355 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2356 break;
2357 case AF_INET6:
2358 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2359 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2360 break;
2361 default:
2362 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2363 server->dstaddr.ss_family);
2364 rc = -EINVAL;
2365 goto out_err;
2368 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2369 key = request_key(&key_type_logon, desc, "");
2370 if (IS_ERR(key)) {
2371 if (!ses->domainName) {
2372 cifs_dbg(FYI, "domainName is NULL\n");
2373 rc = PTR_ERR(key);
2374 goto out_err;
2377 /* didn't work, try to find a domain key */
2378 sprintf(desc, "cifs:d:%s", ses->domainName);
2379 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
2380 key = request_key(&key_type_logon, desc, "");
2381 if (IS_ERR(key)) {
2382 rc = PTR_ERR(key);
2383 goto out_err;
2387 down_read(&key->sem);
2388 upayload = key->payload.data;
2389 if (IS_ERR_OR_NULL(upayload)) {
2390 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
2391 goto out_key_put;
2394 /* find first : in payload */
2395 payload = (char *)upayload->data;
2396 delim = strnchr(payload, upayload->datalen, ':');
2397 cifs_dbg(FYI, "payload=%s\n", payload);
2398 if (!delim) {
2399 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2400 upayload->datalen);
2401 rc = -EINVAL;
2402 goto out_key_put;
2405 len = delim - payload;
2406 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
2407 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2408 len);
2409 rc = -EINVAL;
2410 goto out_key_put;
2413 vol->username = kstrndup(payload, len, GFP_KERNEL);
2414 if (!vol->username) {
2415 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2416 len);
2417 rc = -ENOMEM;
2418 goto out_key_put;
2420 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
2422 len = key->datalen - (len + 1);
2423 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
2424 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
2425 rc = -EINVAL;
2426 kfree(vol->username);
2427 vol->username = NULL;
2428 goto out_key_put;
2431 ++delim;
2432 vol->password = kstrndup(delim, len, GFP_KERNEL);
2433 if (!vol->password) {
2434 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2435 len);
2436 rc = -ENOMEM;
2437 kfree(vol->username);
2438 vol->username = NULL;
2439 goto out_key_put;
2442 out_key_put:
2443 up_read(&key->sem);
2444 key_put(key);
2445 out_err:
2446 kfree(desc);
2447 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
2448 return rc;
2450 #else /* ! CONFIG_KEYS */
2451 static inline int
2452 cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2453 struct cifs_ses *ses __attribute__((unused)))
2455 return -ENOSYS;
2457 #endif /* CONFIG_KEYS */
2459 static struct cifs_ses *
2460 cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2462 int rc = -ENOMEM;
2463 unsigned int xid;
2464 struct cifs_ses *ses;
2465 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2466 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2468 xid = get_xid();
2470 ses = cifs_find_smb_ses(server, volume_info);
2471 if (ses) {
2472 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2473 ses->status);
2475 mutex_lock(&ses->session_mutex);
2476 rc = cifs_negotiate_protocol(xid, ses);
2477 if (rc) {
2478 mutex_unlock(&ses->session_mutex);
2479 /* problem -- put our ses reference */
2480 cifs_put_smb_ses(ses);
2481 free_xid(xid);
2482 return ERR_PTR(rc);
2484 if (ses->need_reconnect) {
2485 cifs_dbg(FYI, "Session needs reconnect\n");
2486 rc = cifs_setup_session(xid, ses,
2487 volume_info->local_nls);
2488 if (rc) {
2489 mutex_unlock(&ses->session_mutex);
2490 /* problem -- put our reference */
2491 cifs_put_smb_ses(ses);
2492 free_xid(xid);
2493 return ERR_PTR(rc);
2496 mutex_unlock(&ses->session_mutex);
2498 /* existing SMB ses has a server reference already */
2499 cifs_put_tcp_session(server, 0);
2500 free_xid(xid);
2501 return ses;
2504 cifs_dbg(FYI, "Existing smb sess not found\n");
2505 ses = sesInfoAlloc();
2506 if (ses == NULL)
2507 goto get_ses_fail;
2509 /* new SMB session uses our server ref */
2510 ses->server = server;
2511 if (server->dstaddr.ss_family == AF_INET6)
2512 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
2513 else
2514 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
2516 if (volume_info->username) {
2517 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2518 if (!ses->user_name)
2519 goto get_ses_fail;
2522 /* volume_info->password freed at unmount */
2523 if (volume_info->password) {
2524 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2525 if (!ses->password)
2526 goto get_ses_fail;
2528 if (volume_info->domainname) {
2529 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2530 if (!ses->domainName)
2531 goto get_ses_fail;
2533 ses->cred_uid = volume_info->cred_uid;
2534 ses->linux_uid = volume_info->linux_uid;
2536 ses->sectype = volume_info->sectype;
2537 ses->sign = volume_info->sign;
2539 mutex_lock(&ses->session_mutex);
2540 rc = cifs_negotiate_protocol(xid, ses);
2541 if (!rc)
2542 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
2543 mutex_unlock(&ses->session_mutex);
2544 if (rc)
2545 goto get_ses_fail;
2547 /* success, put it on the list */
2548 spin_lock(&cifs_tcp_ses_lock);
2549 list_add(&ses->smb_ses_list, &server->smb_ses_list);
2550 spin_unlock(&cifs_tcp_ses_lock);
2552 free_xid(xid);
2553 return ses;
2555 get_ses_fail:
2556 sesInfoFree(ses);
2557 free_xid(xid);
2558 return ERR_PTR(rc);
2561 static int match_tcon(struct cifs_tcon *tcon, const char *unc)
2563 if (tcon->tidStatus == CifsExiting)
2564 return 0;
2565 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
2566 return 0;
2567 return 1;
2570 static struct cifs_tcon *
2571 cifs_find_tcon(struct cifs_ses *ses, const char *unc)
2573 struct list_head *tmp;
2574 struct cifs_tcon *tcon;
2576 spin_lock(&cifs_tcp_ses_lock);
2577 list_for_each(tmp, &ses->tcon_list) {
2578 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
2579 if (!match_tcon(tcon, unc))
2580 continue;
2581 ++tcon->tc_count;
2582 spin_unlock(&cifs_tcp_ses_lock);
2583 return tcon;
2585 spin_unlock(&cifs_tcp_ses_lock);
2586 return NULL;
2589 void
2590 cifs_put_tcon(struct cifs_tcon *tcon)
2592 unsigned int xid;
2593 struct cifs_ses *ses = tcon->ses;
2595 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
2596 spin_lock(&cifs_tcp_ses_lock);
2597 if (--tcon->tc_count > 0) {
2598 spin_unlock(&cifs_tcp_ses_lock);
2599 return;
2602 list_del_init(&tcon->tcon_list);
2603 spin_unlock(&cifs_tcp_ses_lock);
2605 xid = get_xid();
2606 if (ses->server->ops->tree_disconnect)
2607 ses->server->ops->tree_disconnect(xid, tcon);
2608 _free_xid(xid);
2610 cifs_fscache_release_super_cookie(tcon);
2611 tconInfoFree(tcon);
2612 cifs_put_smb_ses(ses);
2615 static struct cifs_tcon *
2616 cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
2618 int rc, xid;
2619 struct cifs_tcon *tcon;
2621 tcon = cifs_find_tcon(ses, volume_info->UNC);
2622 if (tcon) {
2623 cifs_dbg(FYI, "Found match on UNC path\n");
2624 /* existing tcon already has a reference */
2625 cifs_put_smb_ses(ses);
2626 if (tcon->seal != volume_info->seal)
2627 cifs_dbg(VFS, "transport encryption setting conflicts with existing tid\n");
2628 return tcon;
2631 if (!ses->server->ops->tree_connect) {
2632 rc = -ENOSYS;
2633 goto out_fail;
2636 tcon = tconInfoAlloc();
2637 if (tcon == NULL) {
2638 rc = -ENOMEM;
2639 goto out_fail;
2642 tcon->ses = ses;
2643 if (volume_info->password) {
2644 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2645 if (!tcon->password) {
2646 rc = -ENOMEM;
2647 goto out_fail;
2652 * BB Do we need to wrap session_mutex around this TCon call and Unix
2653 * SetFS as we do on SessSetup and reconnect?
2655 xid = get_xid();
2656 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2657 volume_info->local_nls);
2658 free_xid(xid);
2659 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
2660 if (rc)
2661 goto out_fail;
2663 if (volume_info->nodfs) {
2664 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
2665 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
2667 tcon->seal = volume_info->seal;
2669 * We can have only one retry value for a connection to a share so for
2670 * resources mounted more than once to the same server share the last
2671 * value passed in for the retry flag is used.
2673 tcon->retry = volume_info->retry;
2674 tcon->nocase = volume_info->nocase;
2675 tcon->local_lease = volume_info->local_lease;
2676 INIT_LIST_HEAD(&tcon->pending_opens);
2678 spin_lock(&cifs_tcp_ses_lock);
2679 list_add(&tcon->tcon_list, &ses->tcon_list);
2680 spin_unlock(&cifs_tcp_ses_lock);
2682 cifs_fscache_get_super_cookie(tcon);
2684 return tcon;
2686 out_fail:
2687 tconInfoFree(tcon);
2688 return ERR_PTR(rc);
2691 void
2692 cifs_put_tlink(struct tcon_link *tlink)
2694 if (!tlink || IS_ERR(tlink))
2695 return;
2697 if (!atomic_dec_and_test(&tlink->tl_count) ||
2698 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2699 tlink->tl_time = jiffies;
2700 return;
2703 if (!IS_ERR(tlink_tcon(tlink)))
2704 cifs_put_tcon(tlink_tcon(tlink));
2705 kfree(tlink);
2706 return;
2709 static inline struct tcon_link *
2710 cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2712 return cifs_sb->master_tlink;
2715 static int
2716 compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2718 struct cifs_sb_info *old = CIFS_SB(sb);
2719 struct cifs_sb_info *new = mnt_data->cifs_sb;
2721 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2722 return 0;
2724 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2725 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2726 return 0;
2729 * We want to share sb only if we don't specify an r/wsize or
2730 * specified r/wsize is greater than or equal to existing one.
2732 if (new->wsize && new->wsize < old->wsize)
2733 return 0;
2735 if (new->rsize && new->rsize < old->rsize)
2736 return 0;
2738 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
2739 return 0;
2741 if (old->mnt_file_mode != new->mnt_file_mode ||
2742 old->mnt_dir_mode != new->mnt_dir_mode)
2743 return 0;
2745 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2746 return 0;
2748 if (old->actimeo != new->actimeo)
2749 return 0;
2751 return 1;
2755 cifs_match_super(struct super_block *sb, void *data)
2757 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2758 struct smb_vol *volume_info;
2759 struct cifs_sb_info *cifs_sb;
2760 struct TCP_Server_Info *tcp_srv;
2761 struct cifs_ses *ses;
2762 struct cifs_tcon *tcon;
2763 struct tcon_link *tlink;
2764 int rc = 0;
2766 spin_lock(&cifs_tcp_ses_lock);
2767 cifs_sb = CIFS_SB(sb);
2768 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2769 if (IS_ERR(tlink)) {
2770 spin_unlock(&cifs_tcp_ses_lock);
2771 return rc;
2773 tcon = tlink_tcon(tlink);
2774 ses = tcon->ses;
2775 tcp_srv = ses->server;
2777 volume_info = mnt_data->vol;
2779 if (!match_server(tcp_srv, volume_info) ||
2780 !match_session(ses, volume_info) ||
2781 !match_tcon(tcon, volume_info->UNC)) {
2782 rc = 0;
2783 goto out;
2786 rc = compare_mount_options(sb, mnt_data);
2787 out:
2788 spin_unlock(&cifs_tcp_ses_lock);
2789 cifs_put_tlink(tlink);
2790 return rc;
2794 get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
2795 const struct nls_table *nls_codepage, unsigned int *num_referrals,
2796 struct dfs_info3_param **referrals, int remap)
2798 char *temp_unc;
2799 int rc = 0;
2801 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
2802 return -ENOSYS;
2804 *num_referrals = 0;
2805 *referrals = NULL;
2807 if (ses->ipc_tid == 0) {
2808 temp_unc = kmalloc(2 /* for slashes */ +
2809 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
2810 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
2811 if (temp_unc == NULL)
2812 return -ENOMEM;
2813 temp_unc[0] = '\\';
2814 temp_unc[1] = '\\';
2815 strcpy(temp_unc + 2, ses->serverName);
2816 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
2817 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
2818 nls_codepage);
2819 cifs_dbg(FYI, "Tcon rc = %d ipc_tid = %d\n", rc, ses->ipc_tid);
2820 kfree(temp_unc);
2822 if (rc == 0)
2823 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
2824 referrals, num_referrals,
2825 nls_codepage, remap);
2827 * BB - map targetUNCs to dfs_info3 structures, here or in
2828 * ses->server->ops->get_dfs_refer.
2831 return rc;
2834 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2835 static struct lock_class_key cifs_key[2];
2836 static struct lock_class_key cifs_slock_key[2];
2838 static inline void
2839 cifs_reclassify_socket4(struct socket *sock)
2841 struct sock *sk = sock->sk;
2842 BUG_ON(sock_owned_by_user(sk));
2843 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2844 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2847 static inline void
2848 cifs_reclassify_socket6(struct socket *sock)
2850 struct sock *sk = sock->sk;
2851 BUG_ON(sock_owned_by_user(sk));
2852 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2853 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2855 #else
2856 static inline void
2857 cifs_reclassify_socket4(struct socket *sock)
2861 static inline void
2862 cifs_reclassify_socket6(struct socket *sock)
2865 #endif
2867 /* See RFC1001 section 14 on representation of Netbios names */
2868 static void rfc1002mangle(char *target, char *source, unsigned int length)
2870 unsigned int i, j;
2872 for (i = 0, j = 0; i < (length); i++) {
2873 /* mask a nibble at a time and encode */
2874 target[j] = 'A' + (0x0F & (source[i] >> 4));
2875 target[j+1] = 'A' + (0x0F & source[i]);
2876 j += 2;
2881 static int
2882 bind_socket(struct TCP_Server_Info *server)
2884 int rc = 0;
2885 if (server->srcaddr.ss_family != AF_UNSPEC) {
2886 /* Bind to the specified local IP address */
2887 struct socket *socket = server->ssocket;
2888 rc = socket->ops->bind(socket,
2889 (struct sockaddr *) &server->srcaddr,
2890 sizeof(server->srcaddr));
2891 if (rc < 0) {
2892 struct sockaddr_in *saddr4;
2893 struct sockaddr_in6 *saddr6;
2894 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2895 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2896 if (saddr6->sin6_family == AF_INET6)
2897 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
2898 &saddr6->sin6_addr, rc);
2899 else
2900 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
2901 &saddr4->sin_addr.s_addr, rc);
2904 return rc;
2907 static int
2908 ip_rfc1001_connect(struct TCP_Server_Info *server)
2910 int rc = 0;
2912 * some servers require RFC1001 sessinit before sending
2913 * negprot - BB check reconnection in case where second
2914 * sessinit is sent but no second negprot
2916 struct rfc1002_session_packet *ses_init_buf;
2917 struct smb_hdr *smb_buf;
2918 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2919 GFP_KERNEL);
2920 if (ses_init_buf) {
2921 ses_init_buf->trailer.session_req.called_len = 32;
2923 if (server->server_RFC1001_name &&
2924 server->server_RFC1001_name[0] != 0)
2925 rfc1002mangle(ses_init_buf->trailer.
2926 session_req.called_name,
2927 server->server_RFC1001_name,
2928 RFC1001_NAME_LEN_WITH_NULL);
2929 else
2930 rfc1002mangle(ses_init_buf->trailer.
2931 session_req.called_name,
2932 DEFAULT_CIFS_CALLED_NAME,
2933 RFC1001_NAME_LEN_WITH_NULL);
2935 ses_init_buf->trailer.session_req.calling_len = 32;
2938 * calling name ends in null (byte 16) from old smb
2939 * convention.
2941 if (server->workstation_RFC1001_name[0] != 0)
2942 rfc1002mangle(ses_init_buf->trailer.
2943 session_req.calling_name,
2944 server->workstation_RFC1001_name,
2945 RFC1001_NAME_LEN_WITH_NULL);
2946 else
2947 rfc1002mangle(ses_init_buf->trailer.
2948 session_req.calling_name,
2949 "LINUX_CIFS_CLNT",
2950 RFC1001_NAME_LEN_WITH_NULL);
2952 ses_init_buf->trailer.session_req.scope1 = 0;
2953 ses_init_buf->trailer.session_req.scope2 = 0;
2954 smb_buf = (struct smb_hdr *)ses_init_buf;
2956 /* sizeof RFC1002_SESSION_REQUEST with no scope */
2957 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
2958 rc = smb_send(server, smb_buf, 0x44);
2959 kfree(ses_init_buf);
2961 * RFC1001 layer in at least one server
2962 * requires very short break before negprot
2963 * presumably because not expecting negprot
2964 * to follow so fast. This is a simple
2965 * solution that works without
2966 * complicating the code and causes no
2967 * significant slowing down on mount
2968 * for everyone else
2970 usleep_range(1000, 2000);
2973 * else the negprot may still work without this
2974 * even though malloc failed
2977 return rc;
2980 static int
2981 generic_ip_connect(struct TCP_Server_Info *server)
2983 int rc = 0;
2984 __be16 sport;
2985 int slen, sfamily;
2986 struct socket *socket = server->ssocket;
2987 struct sockaddr *saddr;
2989 saddr = (struct sockaddr *) &server->dstaddr;
2991 if (server->dstaddr.ss_family == AF_INET6) {
2992 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
2993 slen = sizeof(struct sockaddr_in6);
2994 sfamily = AF_INET6;
2995 } else {
2996 sport = ((struct sockaddr_in *) saddr)->sin_port;
2997 slen = sizeof(struct sockaddr_in);
2998 sfamily = AF_INET;
3001 if (socket == NULL) {
3002 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3003 IPPROTO_TCP, &socket, 1);
3004 if (rc < 0) {
3005 cifs_dbg(VFS, "Error %d creating socket\n", rc);
3006 server->ssocket = NULL;
3007 return rc;
3010 /* BB other socket options to set KEEPALIVE, NODELAY? */
3011 cifs_dbg(FYI, "Socket created\n");
3012 server->ssocket = socket;
3013 socket->sk->sk_allocation = GFP_NOFS;
3014 if (sfamily == AF_INET6)
3015 cifs_reclassify_socket6(socket);
3016 else
3017 cifs_reclassify_socket4(socket);
3020 rc = bind_socket(server);
3021 if (rc < 0)
3022 return rc;
3025 * Eventually check for other socket options to change from
3026 * the default. sock_setsockopt not used because it expects
3027 * user space buffer
3029 socket->sk->sk_rcvtimeo = 7 * HZ;
3030 socket->sk->sk_sndtimeo = 5 * HZ;
3032 /* make the bufsizes depend on wsize/rsize and max requests */
3033 if (server->noautotune) {
3034 if (socket->sk->sk_sndbuf < (200 * 1024))
3035 socket->sk->sk_sndbuf = 200 * 1024;
3036 if (socket->sk->sk_rcvbuf < (140 * 1024))
3037 socket->sk->sk_rcvbuf = 140 * 1024;
3040 if (server->tcp_nodelay) {
3041 int val = 1;
3042 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3043 (char *)&val, sizeof(val));
3044 if (rc)
3045 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3046 rc);
3049 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
3050 socket->sk->sk_sndbuf,
3051 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3053 rc = socket->ops->connect(socket, saddr, slen, 0);
3054 if (rc < 0) {
3055 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
3056 sock_release(socket);
3057 server->ssocket = NULL;
3058 return rc;
3061 if (sport == htons(RFC1001_PORT))
3062 rc = ip_rfc1001_connect(server);
3064 return rc;
3067 static int
3068 ip_connect(struct TCP_Server_Info *server)
3070 __be16 *sport;
3071 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3072 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3074 if (server->dstaddr.ss_family == AF_INET6)
3075 sport = &addr6->sin6_port;
3076 else
3077 sport = &addr->sin_port;
3079 if (*sport == 0) {
3080 int rc;
3082 /* try with 445 port at first */
3083 *sport = htons(CIFS_PORT);
3085 rc = generic_ip_connect(server);
3086 if (rc >= 0)
3087 return rc;
3089 /* if it failed, try with 139 port */
3090 *sport = htons(RFC1001_PORT);
3093 return generic_ip_connect(server);
3096 void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
3097 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
3099 /* if we are reconnecting then should we check to see if
3100 * any requested capabilities changed locally e.g. via
3101 * remount but we can not do much about it here
3102 * if they have (even if we could detect it by the following)
3103 * Perhaps we could add a backpointer to array of sb from tcon
3104 * or if we change to make all sb to same share the same
3105 * sb as NFS - then we only have one backpointer to sb.
3106 * What if we wanted to mount the server share twice once with
3107 * and once without posixacls or posix paths? */
3108 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3110 if (vol_info && vol_info->no_linux_ext) {
3111 tcon->fsUnixInfo.Capability = 0;
3112 tcon->unix_ext = 0; /* Unix Extensions disabled */
3113 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
3114 return;
3115 } else if (vol_info)
3116 tcon->unix_ext = 1; /* Unix Extensions supported */
3118 if (tcon->unix_ext == 0) {
3119 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
3120 return;
3123 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
3124 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3125 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
3126 /* check for reconnect case in which we do not
3127 want to change the mount behavior if we can avoid it */
3128 if (vol_info == NULL) {
3129 /* turn off POSIX ACL and PATHNAMES if not set
3130 originally at mount time */
3131 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3132 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3133 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3134 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3135 cifs_dbg(VFS, "POSIXPATH support change\n");
3136 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3137 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3138 cifs_dbg(VFS, "possible reconnect error\n");
3139 cifs_dbg(VFS, "server disabled POSIX path support\n");
3143 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3144 cifs_dbg(VFS, "per-share encryption not supported yet\n");
3146 cap &= CIFS_UNIX_CAP_MASK;
3147 if (vol_info && vol_info->no_psx_acl)
3148 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
3149 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
3150 cifs_dbg(FYI, "negotiated posix acl support\n");
3151 if (cifs_sb)
3152 cifs_sb->mnt_cifs_flags |=
3153 CIFS_MOUNT_POSIXACL;
3156 if (vol_info && vol_info->posix_paths == 0)
3157 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
3158 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
3159 cifs_dbg(FYI, "negotiate posix pathnames\n");
3160 if (cifs_sb)
3161 cifs_sb->mnt_cifs_flags |=
3162 CIFS_MOUNT_POSIX_PATHS;
3165 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
3166 #ifdef CONFIG_CIFS_DEBUG2
3167 if (cap & CIFS_UNIX_FCNTL_CAP)
3168 cifs_dbg(FYI, "FCNTL cap\n");
3169 if (cap & CIFS_UNIX_EXTATTR_CAP)
3170 cifs_dbg(FYI, "EXTATTR cap\n");
3171 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
3172 cifs_dbg(FYI, "POSIX path cap\n");
3173 if (cap & CIFS_UNIX_XATTR_CAP)
3174 cifs_dbg(FYI, "XATTR cap\n");
3175 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
3176 cifs_dbg(FYI, "POSIX ACL cap\n");
3177 if (cap & CIFS_UNIX_LARGE_READ_CAP)
3178 cifs_dbg(FYI, "very large read cap\n");
3179 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
3180 cifs_dbg(FYI, "very large write cap\n");
3181 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3182 cifs_dbg(FYI, "transport encryption cap\n");
3183 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3184 cifs_dbg(FYI, "mandatory transport encryption cap\n");
3185 #endif /* CIFS_DEBUG2 */
3186 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
3187 if (vol_info == NULL) {
3188 cifs_dbg(FYI, "resetting capabilities failed\n");
3189 } else
3190 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");
3196 void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
3197 struct cifs_sb_info *cifs_sb)
3199 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3201 spin_lock_init(&cifs_sb->tlink_tree_lock);
3202 cifs_sb->tlink_tree = RB_ROOT;
3205 * Temporarily set r/wsize for matching superblock. If we end up using
3206 * new sb then client will later negotiate it downward if needed.
3208 cifs_sb->rsize = pvolume_info->rsize;
3209 cifs_sb->wsize = pvolume_info->wsize;
3211 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3212 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3213 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3214 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
3215 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3216 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
3218 cifs_sb->actimeo = pvolume_info->actimeo;
3219 cifs_sb->local_nls = pvolume_info->local_nls;
3221 if (pvolume_info->noperm)
3222 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3223 if (pvolume_info->setuids)
3224 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
3225 if (pvolume_info->server_ino)
3226 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3227 if (pvolume_info->remap)
3228 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3229 if (pvolume_info->sfu_remap)
3230 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3231 if (pvolume_info->no_xattr)
3232 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3233 if (pvolume_info->sfu_emul)
3234 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3235 if (pvolume_info->nobrl)
3236 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
3237 if (pvolume_info->nostrictsync)
3238 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
3239 if (pvolume_info->mand_lock)
3240 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
3241 if (pvolume_info->rwpidforward)
3242 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
3243 if (pvolume_info->cifs_acl)
3244 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
3245 if (pvolume_info->backupuid_specified) {
3246 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
3247 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3249 if (pvolume_info->backupgid_specified) {
3250 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
3251 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3253 if (pvolume_info->override_uid)
3254 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3255 if (pvolume_info->override_gid)
3256 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3257 if (pvolume_info->dynperm)
3258 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
3259 if (pvolume_info->fsc)
3260 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
3261 if (pvolume_info->multiuser)
3262 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3263 CIFS_MOUNT_NO_PERM);
3264 if (pvolume_info->strict_io)
3265 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
3266 if (pvolume_info->direct_io) {
3267 cifs_dbg(FYI, "mounting share using direct i/o\n");
3268 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3270 if (pvolume_info->mfsymlinks) {
3271 if (pvolume_info->sfu_emul) {
3273 * Our SFU ("Services for Unix" emulation does not allow
3274 * creating symlinks but does allow reading existing SFU
3275 * symlinks (it does allow both creating and reading SFU
3276 * style mknod and FIFOs though). When "mfsymlinks" and
3277 * "sfu" are both enabled at the same time, it allows
3278 * reading both types of symlinks, but will only create
3279 * them with mfsymlinks format. This allows better
3280 * Apple compatibility (probably better for Samba too)
3281 * while still recognizing old Windows style symlinks.
3283 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
3285 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
3288 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
3289 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
3292 static void
3293 cleanup_volume_info_contents(struct smb_vol *volume_info)
3295 kfree(volume_info->username);
3296 kzfree(volume_info->password);
3297 kfree(volume_info->UNC);
3298 kfree(volume_info->domainname);
3299 kfree(volume_info->iocharset);
3300 kfree(volume_info->prepath);
3303 void
3304 cifs_cleanup_volume_info(struct smb_vol *volume_info)
3306 if (!volume_info)
3307 return;
3308 cleanup_volume_info_contents(volume_info);
3309 kfree(volume_info);
3313 #ifdef CONFIG_CIFS_DFS_UPCALL
3315 * cifs_build_path_to_root returns full path to root when we do not have an
3316 * exiting connection (tcon)
3318 static char *
3319 build_unc_path_to_root(const struct smb_vol *vol,
3320 const struct cifs_sb_info *cifs_sb)
3322 char *full_path, *pos;
3323 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
3324 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
3326 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
3327 if (full_path == NULL)
3328 return ERR_PTR(-ENOMEM);
3330 strncpy(full_path, vol->UNC, unc_len);
3331 pos = full_path + unc_len;
3333 if (pplen) {
3334 *pos = CIFS_DIR_SEP(cifs_sb);
3335 strncpy(pos + 1, vol->prepath, pplen);
3336 pos += pplen;
3339 *pos = '\0'; /* add trailing null */
3340 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
3341 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
3342 return full_path;
3346 * Perform a dfs referral query for a share and (optionally) prefix
3348 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3349 * to a string containing updated options for the submount. Otherwise it
3350 * will be left untouched.
3352 * Returns the rc from get_dfs_path to the caller, which can be used to
3353 * determine whether there were referrals.
3355 static int
3356 expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
3357 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
3358 int check_prefix)
3360 int rc;
3361 unsigned int num_referrals = 0;
3362 struct dfs_info3_param *referrals = NULL;
3363 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3365 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3366 if (IS_ERR(full_path))
3367 return PTR_ERR(full_path);
3369 /* For DFS paths, skip the first '\' of the UNC */
3370 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3372 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
3373 &num_referrals, &referrals, cifs_remap(cifs_sb));
3375 if (!rc && num_referrals > 0) {
3376 char *fake_devname = NULL;
3378 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3379 full_path + 1, referrals,
3380 &fake_devname);
3382 free_dfs_info_array(referrals, num_referrals);
3384 if (IS_ERR(mdata)) {
3385 rc = PTR_ERR(mdata);
3386 mdata = NULL;
3387 } else {
3388 cleanup_volume_info_contents(volume_info);
3389 rc = cifs_setup_volume_info(volume_info, mdata,
3390 fake_devname);
3392 kfree(fake_devname);
3393 kfree(cifs_sb->mountdata);
3394 cifs_sb->mountdata = mdata;
3396 kfree(full_path);
3397 return rc;
3399 #endif
3401 static int
3402 cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3403 const char *devname)
3405 int rc = 0;
3407 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3408 return -EINVAL;
3410 if (volume_info->nullauth) {
3411 cifs_dbg(FYI, "Anonymous login\n");
3412 kfree(volume_info->username);
3413 volume_info->username = NULL;
3414 } else if (volume_info->username) {
3415 /* BB fixme parse for domain name here */
3416 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
3417 } else {
3418 cifs_dbg(VFS, "No username specified\n");
3419 /* In userspace mount helper we can get user name from alternate
3420 locations such as env variables and files on disk */
3421 return -EINVAL;
3424 /* this is needed for ASCII cp to Unicode converts */
3425 if (volume_info->iocharset == NULL) {
3426 /* load_nls_default cannot return null */
3427 volume_info->local_nls = load_nls_default();
3428 } else {
3429 volume_info->local_nls = load_nls(volume_info->iocharset);
3430 if (volume_info->local_nls == NULL) {
3431 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
3432 volume_info->iocharset);
3433 return -ELIBACC;
3437 return rc;
3440 struct smb_vol *
3441 cifs_get_volume_info(char *mount_data, const char *devname)
3443 int rc;
3444 struct smb_vol *volume_info;
3446 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
3447 if (!volume_info)
3448 return ERR_PTR(-ENOMEM);
3450 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3451 if (rc) {
3452 cifs_cleanup_volume_info(volume_info);
3453 volume_info = ERR_PTR(rc);
3456 return volume_info;
3459 static int
3460 cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3461 unsigned int xid,
3462 struct cifs_tcon *tcon,
3463 struct cifs_sb_info *cifs_sb,
3464 char *full_path)
3466 int rc;
3467 char *s;
3468 char sep, tmp;
3470 sep = CIFS_DIR_SEP(cifs_sb);
3471 s = full_path;
3473 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
3474 while (rc == 0) {
3475 /* skip separators */
3476 while (*s == sep)
3477 s++;
3478 if (!*s)
3479 break;
3480 /* next separator */
3481 while (*s && *s != sep)
3482 s++;
3485 * temporarily null-terminate the path at the end of
3486 * the current component
3488 tmp = *s;
3489 *s = 0;
3490 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3491 full_path);
3492 *s = tmp;
3494 return rc;
3498 cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
3500 int rc;
3501 unsigned int xid;
3502 struct cifs_ses *ses;
3503 struct cifs_tcon *tcon;
3504 struct TCP_Server_Info *server;
3505 char *full_path;
3506 struct tcon_link *tlink;
3507 #ifdef CONFIG_CIFS_DFS_UPCALL
3508 int referral_walks_count = 0;
3509 #endif
3511 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs");
3512 if (rc)
3513 return rc;
3515 #ifdef CONFIG_CIFS_DFS_UPCALL
3516 try_mount_again:
3517 /* cleanup activities if we're chasing a referral */
3518 if (referral_walks_count) {
3519 if (tcon)
3520 cifs_put_tcon(tcon);
3521 else if (ses)
3522 cifs_put_smb_ses(ses);
3524 free_xid(xid);
3526 #endif
3527 rc = 0;
3528 tcon = NULL;
3529 ses = NULL;
3530 server = NULL;
3531 full_path = NULL;
3532 tlink = NULL;
3534 xid = get_xid();
3536 /* get a reference to a tcp session */
3537 server = cifs_get_tcp_session(volume_info);
3538 if (IS_ERR(server)) {
3539 rc = PTR_ERR(server);
3540 bdi_destroy(&cifs_sb->bdi);
3541 goto out;
3544 /* get a reference to a SMB session */
3545 ses = cifs_get_smb_ses(server, volume_info);
3546 if (IS_ERR(ses)) {
3547 rc = PTR_ERR(ses);
3548 ses = NULL;
3549 goto mount_fail_check;
3552 /* search for existing tcon to this server share */
3553 tcon = cifs_get_tcon(ses, volume_info);
3554 if (IS_ERR(tcon)) {
3555 rc = PTR_ERR(tcon);
3556 tcon = NULL;
3557 goto remote_path_check;
3560 /* tell server which Unix caps we support */
3561 if (cap_unix(tcon->ses)) {
3562 /* reset of caps checks mount to see if unix extensions
3563 disabled for just this mount */
3564 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
3565 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3566 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3567 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3568 rc = -EACCES;
3569 goto mount_fail_check;
3571 } else
3572 tcon->unix_ext = 0; /* server does not support them */
3574 /* do not care if a following call succeed - informational */
3575 if (!tcon->ipc && server->ops->qfs_tcon)
3576 server->ops->qfs_tcon(xid, tcon);
3578 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
3579 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
3581 /* tune readahead according to rsize */
3582 cifs_sb->bdi.ra_pages = cifs_sb->rsize / PAGE_CACHE_SIZE;
3584 remote_path_check:
3585 #ifdef CONFIG_CIFS_DFS_UPCALL
3587 * Perform an unconditional check for whether there are DFS
3588 * referrals for this path without prefix, to provide support
3589 * for DFS referrals from w2k8 servers which don't seem to respond
3590 * with PATH_NOT_COVERED to requests that include the prefix.
3591 * Chase the referral if found, otherwise continue normally.
3593 if (referral_walks_count == 0) {
3594 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3595 false);
3596 if (!refrc) {
3597 referral_walks_count++;
3598 goto try_mount_again;
3601 #endif
3603 /* check if a whole path is not remote */
3604 if (!rc && tcon) {
3605 if (!server->ops->is_path_accessible) {
3606 rc = -ENOSYS;
3607 goto mount_fail_check;
3610 * cifs_build_path_to_root works only when we have a valid tcon
3612 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
3613 if (full_path == NULL) {
3614 rc = -ENOMEM;
3615 goto mount_fail_check;
3617 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3618 full_path);
3619 if (rc != 0 && rc != -EREMOTE) {
3620 kfree(full_path);
3621 goto mount_fail_check;
3624 rc = cifs_are_all_path_components_accessible(server,
3625 xid, tcon, cifs_sb,
3626 full_path);
3627 if (rc != 0) {
3628 cifs_dbg(VFS, "cannot query dirs between root and final path, "
3629 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
3630 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
3631 rc = 0;
3633 kfree(full_path);
3636 /* get referral if needed */
3637 if (rc == -EREMOTE) {
3638 #ifdef CONFIG_CIFS_DFS_UPCALL
3639 if (referral_walks_count > MAX_NESTED_LINKS) {
3641 * BB: when we implement proper loop detection,
3642 * we will remove this check. But now we need it
3643 * to prevent an indefinite loop if 'DFS tree' is
3644 * misconfigured (i.e. has loops).
3646 rc = -ELOOP;
3647 goto mount_fail_check;
3650 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
3652 if (!rc) {
3653 referral_walks_count++;
3654 goto try_mount_again;
3656 goto mount_fail_check;
3657 #else /* No DFS support, return error on mount */
3658 rc = -EOPNOTSUPP;
3659 #endif
3662 if (rc)
3663 goto mount_fail_check;
3665 /* now, hang the tcon off of the superblock */
3666 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3667 if (tlink == NULL) {
3668 rc = -ENOMEM;
3669 goto mount_fail_check;
3672 tlink->tl_uid = ses->linux_uid;
3673 tlink->tl_tcon = tcon;
3674 tlink->tl_time = jiffies;
3675 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3676 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3678 cifs_sb->master_tlink = tlink;
3679 spin_lock(&cifs_sb->tlink_tree_lock);
3680 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3681 spin_unlock(&cifs_sb->tlink_tree_lock);
3683 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3684 TLINK_IDLE_EXPIRE);
3686 mount_fail_check:
3687 /* on error free sesinfo and tcon struct if needed */
3688 if (rc) {
3689 /* If find_unc succeeded then rc == 0 so we can not end */
3690 /* up accidentally freeing someone elses tcon struct */
3691 if (tcon)
3692 cifs_put_tcon(tcon);
3693 else if (ses)
3694 cifs_put_smb_ses(ses);
3695 else
3696 cifs_put_tcp_session(server, 0);
3697 bdi_destroy(&cifs_sb->bdi);
3700 out:
3701 free_xid(xid);
3702 return rc;
3706 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3707 * pointer may be NULL.
3710 CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
3711 const char *tree, struct cifs_tcon *tcon,
3712 const struct nls_table *nls_codepage)
3714 struct smb_hdr *smb_buffer;
3715 struct smb_hdr *smb_buffer_response;
3716 TCONX_REQ *pSMB;
3717 TCONX_RSP *pSMBr;
3718 unsigned char *bcc_ptr;
3719 int rc = 0;
3720 int length;
3721 __u16 bytes_left, count;
3723 if (ses == NULL)
3724 return -EIO;
3726 smb_buffer = cifs_buf_get();
3727 if (smb_buffer == NULL)
3728 return -ENOMEM;
3730 smb_buffer_response = smb_buffer;
3732 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3733 NULL /*no tid */ , 4 /*wct */ );
3735 smb_buffer->Mid = get_next_mid(ses->server);
3736 smb_buffer->Uid = ses->Suid;
3737 pSMB = (TCONX_REQ *) smb_buffer;
3738 pSMBr = (TCONX_RSP *) smb_buffer_response;
3740 pSMB->AndXCommand = 0xFF;
3741 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
3742 bcc_ptr = &pSMB->Password[0];
3743 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
3744 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
3745 *bcc_ptr = 0; /* password is null byte */
3746 bcc_ptr++; /* skip password */
3747 /* already aligned so no need to do it below */
3748 } else {
3749 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
3750 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3751 specified as required (when that support is added to
3752 the vfs in the future) as only NTLM or the much
3753 weaker LANMAN (which we do not send by default) is accepted
3754 by Samba (not sure whether other servers allow
3755 NTLMv2 password here) */
3756 #ifdef CONFIG_CIFS_WEAK_PW_HASH
3757 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
3758 (ses->sectype == LANMAN))
3759 calc_lanman_hash(tcon->password, ses->server->cryptkey,
3760 ses->server->sec_mode &
3761 SECMODE_PW_ENCRYPT ? true : false,
3762 bcc_ptr);
3763 else
3764 #endif /* CIFS_WEAK_PW_HASH */
3765 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
3766 bcc_ptr, nls_codepage);
3767 if (rc) {
3768 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
3769 __func__, rc);
3770 cifs_buf_release(smb_buffer);
3771 return rc;
3774 bcc_ptr += CIFS_AUTH_RESP_SIZE;
3775 if (ses->capabilities & CAP_UNICODE) {
3776 /* must align unicode strings */
3777 *bcc_ptr = 0; /* null byte password */
3778 bcc_ptr++;
3782 if (ses->server->sign)
3783 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3785 if (ses->capabilities & CAP_STATUS32) {
3786 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3788 if (ses->capabilities & CAP_DFS) {
3789 smb_buffer->Flags2 |= SMBFLG2_DFS;
3791 if (ses->capabilities & CAP_UNICODE) {
3792 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3793 length =
3794 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
3795 6 /* max utf8 char length in bytes */ *
3796 (/* server len*/ + 256 /* share len */), nls_codepage);
3797 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
3798 bcc_ptr += 2; /* skip trailing null */
3799 } else { /* ASCII */
3800 strcpy(bcc_ptr, tree);
3801 bcc_ptr += strlen(tree) + 1;
3803 strcpy(bcc_ptr, "?????");
3804 bcc_ptr += strlen("?????");
3805 bcc_ptr += 1;
3806 count = bcc_ptr - &pSMB->Password[0];
3807 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
3808 pSMB->hdr.smb_buf_length) + count);
3809 pSMB->ByteCount = cpu_to_le16(count);
3811 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
3814 /* above now done in SendReceive */
3815 if ((rc == 0) && (tcon != NULL)) {
3816 bool is_unicode;
3818 tcon->tidStatus = CifsGood;
3819 tcon->need_reconnect = false;
3820 tcon->tid = smb_buffer_response->Tid;
3821 bcc_ptr = pByteArea(smb_buffer_response);
3822 bytes_left = get_bcc(smb_buffer_response);
3823 length = strnlen(bcc_ptr, bytes_left - 2);
3824 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3825 is_unicode = true;
3826 else
3827 is_unicode = false;
3830 /* skip service field (NB: this field is always ASCII) */
3831 if (length == 3) {
3832 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3833 (bcc_ptr[2] == 'C')) {
3834 cifs_dbg(FYI, "IPC connection\n");
3835 tcon->ipc = 1;
3837 } else if (length == 2) {
3838 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3839 /* the most common case */
3840 cifs_dbg(FYI, "disk share connection\n");
3843 bcc_ptr += length + 1;
3844 bytes_left -= (length + 1);
3845 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
3847 /* mostly informational -- no need to fail on error here */
3848 kfree(tcon->nativeFileSystem);
3849 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
3850 bytes_left, is_unicode,
3851 nls_codepage);
3853 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
3855 if ((smb_buffer_response->WordCount == 3) ||
3856 (smb_buffer_response->WordCount == 7))
3857 /* field is in same location */
3858 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3859 else
3860 tcon->Flags = 0;
3861 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
3862 } else if ((rc == 0) && tcon == NULL) {
3863 /* all we need to save for IPC$ connection */
3864 ses->ipc_tid = smb_buffer_response->Tid;
3867 cifs_buf_release(smb_buffer);
3868 return rc;
3871 static void delayed_free(struct rcu_head *p)
3873 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
3874 unload_nls(sbi->local_nls);
3875 kfree(sbi);
3878 void
3879 cifs_umount(struct cifs_sb_info *cifs_sb)
3881 struct rb_root *root = &cifs_sb->tlink_tree;
3882 struct rb_node *node;
3883 struct tcon_link *tlink;
3885 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3887 spin_lock(&cifs_sb->tlink_tree_lock);
3888 while ((node = rb_first(root))) {
3889 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3890 cifs_get_tlink(tlink);
3891 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3892 rb_erase(node, root);
3894 spin_unlock(&cifs_sb->tlink_tree_lock);
3895 cifs_put_tlink(tlink);
3896 spin_lock(&cifs_sb->tlink_tree_lock);
3898 spin_unlock(&cifs_sb->tlink_tree_lock);
3900 bdi_destroy(&cifs_sb->bdi);
3901 kfree(cifs_sb->mountdata);
3902 kfree(cifs_sb->prepath);
3903 call_rcu(&cifs_sb->rcu, delayed_free);
3907 cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
3909 int rc = 0;
3910 struct TCP_Server_Info *server = ses->server;
3912 if (!server->ops->need_neg || !server->ops->negotiate)
3913 return -ENOSYS;
3915 /* only send once per connect */
3916 if (!server->ops->need_neg(server))
3917 return 0;
3919 set_credits(server, 1);
3921 rc = server->ops->negotiate(xid, ses);
3922 if (rc == 0) {
3923 spin_lock(&GlobalMid_Lock);
3924 if (server->tcpStatus == CifsNeedNegotiate)
3925 server->tcpStatus = CifsGood;
3926 else
3927 rc = -EHOSTDOWN;
3928 spin_unlock(&GlobalMid_Lock);
3931 return rc;
3935 cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3936 struct nls_table *nls_info)
3938 int rc = -ENOSYS;
3939 struct TCP_Server_Info *server = ses->server;
3941 ses->capabilities = server->capabilities;
3942 if (linuxExtEnabled == 0)
3943 ses->capabilities &= (~server->vals->cap_unix);
3945 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
3946 server->sec_mode, server->capabilities, server->timeAdj);
3948 if (server->ops->sess_setup)
3949 rc = server->ops->sess_setup(xid, ses, nls_info);
3951 if (rc)
3952 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
3954 return rc;
3957 static int
3958 cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
3960 vol->sectype = ses->sectype;
3962 /* krb5 is special, since we don't need username or pw */
3963 if (vol->sectype == Kerberos)
3964 return 0;
3966 return cifs_set_cifscreds(vol, ses);
3969 static struct cifs_tcon *
3970 cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
3972 int rc;
3973 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3974 struct cifs_ses *ses;
3975 struct cifs_tcon *tcon = NULL;
3976 struct smb_vol *vol_info;
3978 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
3979 if (vol_info == NULL)
3980 return ERR_PTR(-ENOMEM);
3982 vol_info->local_nls = cifs_sb->local_nls;
3983 vol_info->linux_uid = fsuid;
3984 vol_info->cred_uid = fsuid;
3985 vol_info->UNC = master_tcon->treeName;
3986 vol_info->retry = master_tcon->retry;
3987 vol_info->nocase = master_tcon->nocase;
3988 vol_info->local_lease = master_tcon->local_lease;
3989 vol_info->no_linux_ext = !master_tcon->unix_ext;
3990 vol_info->sectype = master_tcon->ses->sectype;
3991 vol_info->sign = master_tcon->ses->sign;
3993 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
3994 if (rc) {
3995 tcon = ERR_PTR(rc);
3996 goto out;
3999 /* get a reference for the same TCP session */
4000 spin_lock(&cifs_tcp_ses_lock);
4001 ++master_tcon->ses->server->srv_count;
4002 spin_unlock(&cifs_tcp_ses_lock);
4004 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4005 if (IS_ERR(ses)) {
4006 tcon = (struct cifs_tcon *)ses;
4007 cifs_put_tcp_session(master_tcon->ses->server, 0);
4008 goto out;
4011 tcon = cifs_get_tcon(ses, vol_info);
4012 if (IS_ERR(tcon)) {
4013 cifs_put_smb_ses(ses);
4014 goto out;
4017 if (cap_unix(ses))
4018 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4019 out:
4020 kfree(vol_info->username);
4021 kfree(vol_info->password);
4022 kfree(vol_info);
4024 return tcon;
4027 struct cifs_tcon *
4028 cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4030 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4033 /* find and return a tlink with given uid */
4034 static struct tcon_link *
4035 tlink_rb_search(struct rb_root *root, kuid_t uid)
4037 struct rb_node *node = root->rb_node;
4038 struct tcon_link *tlink;
4040 while (node) {
4041 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4043 if (uid_gt(tlink->tl_uid, uid))
4044 node = node->rb_left;
4045 else if (uid_lt(tlink->tl_uid, uid))
4046 node = node->rb_right;
4047 else
4048 return tlink;
4050 return NULL;
4053 /* insert a tcon_link into the tree */
4054 static void
4055 tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4057 struct rb_node **new = &(root->rb_node), *parent = NULL;
4058 struct tcon_link *tlink;
4060 while (*new) {
4061 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4062 parent = *new;
4064 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
4065 new = &((*new)->rb_left);
4066 else
4067 new = &((*new)->rb_right);
4070 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4071 rb_insert_color(&new_tlink->tl_rbnode, root);
4075 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4076 * current task.
4078 * If the superblock doesn't refer to a multiuser mount, then just return
4079 * the master tcon for the mount.
4081 * First, search the rbtree for an existing tcon for this fsuid. If one
4082 * exists, then check to see if it's pending construction. If it is then wait
4083 * for construction to complete. Once it's no longer pending, check to see if
4084 * it failed and either return an error or retry construction, depending on
4085 * the timeout.
4087 * If one doesn't exist then insert a new tcon_link struct into the tree and
4088 * try to construct a new one.
4090 struct tcon_link *
4091 cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4093 int ret;
4094 kuid_t fsuid = current_fsuid();
4095 struct tcon_link *tlink, *newtlink;
4097 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4098 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4100 spin_lock(&cifs_sb->tlink_tree_lock);
4101 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4102 if (tlink)
4103 cifs_get_tlink(tlink);
4104 spin_unlock(&cifs_sb->tlink_tree_lock);
4106 if (tlink == NULL) {
4107 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4108 if (newtlink == NULL)
4109 return ERR_PTR(-ENOMEM);
4110 newtlink->tl_uid = fsuid;
4111 newtlink->tl_tcon = ERR_PTR(-EACCES);
4112 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4113 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4114 cifs_get_tlink(newtlink);
4116 spin_lock(&cifs_sb->tlink_tree_lock);
4117 /* was one inserted after previous search? */
4118 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
4119 if (tlink) {
4120 cifs_get_tlink(tlink);
4121 spin_unlock(&cifs_sb->tlink_tree_lock);
4122 kfree(newtlink);
4123 goto wait_for_construction;
4125 tlink = newtlink;
4126 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4127 spin_unlock(&cifs_sb->tlink_tree_lock);
4128 } else {
4129 wait_for_construction:
4130 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4131 TASK_INTERRUPTIBLE);
4132 if (ret) {
4133 cifs_put_tlink(tlink);
4134 return ERR_PTR(-ERESTARTSYS);
4137 /* if it's good, return it */
4138 if (!IS_ERR(tlink->tl_tcon))
4139 return tlink;
4141 /* return error if we tried this already recently */
4142 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4143 cifs_put_tlink(tlink);
4144 return ERR_PTR(-EACCES);
4147 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4148 goto wait_for_construction;
4151 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4152 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4153 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4155 if (IS_ERR(tlink->tl_tcon)) {
4156 cifs_put_tlink(tlink);
4157 return ERR_PTR(-EACCES);
4160 return tlink;
4164 * periodic workqueue job that scans tcon_tree for a superblock and closes
4165 * out tcons.
4167 static void
4168 cifs_prune_tlinks(struct work_struct *work)
4170 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4171 prune_tlinks.work);
4172 struct rb_root *root = &cifs_sb->tlink_tree;
4173 struct rb_node *node = rb_first(root);
4174 struct rb_node *tmp;
4175 struct tcon_link *tlink;
4178 * Because we drop the spinlock in the loop in order to put the tlink
4179 * it's not guarded against removal of links from the tree. The only
4180 * places that remove entries from the tree are this function and
4181 * umounts. Because this function is non-reentrant and is canceled
4182 * before umount can proceed, this is safe.
4184 spin_lock(&cifs_sb->tlink_tree_lock);
4185 node = rb_first(root);
4186 while (node != NULL) {
4187 tmp = node;
4188 node = rb_next(tmp);
4189 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4191 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4192 atomic_read(&tlink->tl_count) != 0 ||
4193 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4194 continue;
4196 cifs_get_tlink(tlink);
4197 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4198 rb_erase(tmp, root);
4200 spin_unlock(&cifs_sb->tlink_tree_lock);
4201 cifs_put_tlink(tlink);
4202 spin_lock(&cifs_sb->tlink_tree_lock);
4204 spin_unlock(&cifs_sb->tlink_tree_lock);
4206 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4207 TLINK_IDLE_EXPIRE);