blob: a83ed766aa945d8533506708905f9d475858a3ec [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
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.
11 *
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.
16 *
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
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/pagemap.h>
28#include <linux/ctype.h>
29#include <linux/utsname.h>
30#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070031#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070032#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000033#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070034#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080035#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040036#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040039#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040040#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050041#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000042#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040043#include <linux/parser.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#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"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053054#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define CIFS_PORT 445
57#define RFC1001_PORT 139
58
Jeff Laytonc74093b2011-01-11 07:24:23 -050059/* SMB echo "timeout" -- FIXME: tunable? */
60#define SMB_ECHO_INTERVAL (60 * HZ)
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062extern mempool_t *cifs_req_poolp;
63
Jeff Layton2de970f2010-10-06 19:51:12 -040064/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040065#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040066#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040067
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040068enum {
69
70 /* Mount options that take no arguments */
71 Opt_user_xattr, Opt_nouser_xattr,
72 Opt_forceuid, Opt_noforceuid,
73 Opt_noblocksend, Opt_noautotune,
74 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
75 Opt_mapchars, Opt_nomapchars, Opt_sfu,
76 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
77 Opt_noposixpaths, Opt_nounix,
78 Opt_nocase,
79 Opt_brl, Opt_nobrl,
80 Opt_forcemandatorylock, Opt_setuids,
81 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
82 Opt_nohard, Opt_nosoft,
83 Opt_nointr, Opt_intr,
84 Opt_nostrictsync, Opt_strictsync,
85 Opt_serverino, Opt_noserverino,
86 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
87 Opt_acl, Opt_noacl, Opt_locallease,
88 Opt_sign, Opt_seal, Opt_direct,
89 Opt_strictcache, Opt_noac,
90 Opt_fsc, Opt_mfsymlinks,
Jeff Laytond8162552012-03-23 14:40:56 -040091 Opt_multiuser, Opt_sloppy,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040092
93 /* Mount options which take numeric value */
94 Opt_backupuid, Opt_backupgid, Opt_uid,
95 Opt_cruid, Opt_gid, Opt_file_mode,
96 Opt_dirmode, Opt_port,
97 Opt_rsize, Opt_wsize, Opt_actimeo,
98
99 /* Mount options which take string value */
100 Opt_user, Opt_pass, Opt_ip,
101 Opt_unc, Opt_domain,
102 Opt_srcaddr, Opt_prefixpath,
103 Opt_iocharset, Opt_sockopt,
104 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400105 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400106
107 /* Mount options to be ignored */
108 Opt_ignore,
109
110 /* Options which could be blank */
111 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100112 Opt_blank_user,
113 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400114
115 Opt_err
116};
117
118static const match_table_t cifs_mount_option_tokens = {
119
120 { Opt_user_xattr, "user_xattr" },
121 { Opt_nouser_xattr, "nouser_xattr" },
122 { Opt_forceuid, "forceuid" },
123 { Opt_noforceuid, "noforceuid" },
124 { Opt_noblocksend, "noblocksend" },
125 { Opt_noautotune, "noautotune" },
126 { Opt_hard, "hard" },
127 { Opt_soft, "soft" },
128 { Opt_perm, "perm" },
129 { Opt_noperm, "noperm" },
130 { Opt_mapchars, "mapchars" },
131 { Opt_nomapchars, "nomapchars" },
132 { Opt_sfu, "sfu" },
133 { Opt_nosfu, "nosfu" },
134 { Opt_nodfs, "nodfs" },
135 { Opt_posixpaths, "posixpaths" },
136 { Opt_noposixpaths, "noposixpaths" },
137 { Opt_nounix, "nounix" },
138 { Opt_nounix, "nolinux" },
139 { Opt_nocase, "nocase" },
140 { Opt_nocase, "ignorecase" },
141 { Opt_brl, "brl" },
142 { Opt_nobrl, "nobrl" },
143 { Opt_nobrl, "nolock" },
144 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400145 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400146 { Opt_setuids, "setuids" },
147 { Opt_nosetuids, "nosetuids" },
148 { Opt_dynperm, "dynperm" },
149 { Opt_nodynperm, "nodynperm" },
150 { Opt_nohard, "nohard" },
151 { Opt_nosoft, "nosoft" },
152 { Opt_nointr, "nointr" },
153 { Opt_intr, "intr" },
154 { Opt_nostrictsync, "nostrictsync" },
155 { Opt_strictsync, "strictsync" },
156 { Opt_serverino, "serverino" },
157 { Opt_noserverino, "noserverino" },
158 { Opt_rwpidforward, "rwpidforward" },
159 { Opt_cifsacl, "cifsacl" },
160 { Opt_nocifsacl, "nocifsacl" },
161 { Opt_acl, "acl" },
162 { Opt_noacl, "noacl" },
163 { Opt_locallease, "locallease" },
164 { Opt_sign, "sign" },
165 { Opt_seal, "seal" },
166 { Opt_direct, "direct" },
Jeff Layton531c8ff2012-05-16 07:12:26 -0400167 { Opt_direct, "directio" },
168 { Opt_direct, "forcedirectio" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400169 { Opt_strictcache, "strictcache" },
170 { Opt_noac, "noac" },
171 { Opt_fsc, "fsc" },
172 { Opt_mfsymlinks, "mfsymlinks" },
173 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400174 { Opt_sloppy, "sloppy" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400175
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" },
188
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100189 { Opt_blank_user, "user=" },
190 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400191 { Opt_user, "user=%s" },
192 { Opt_user, "username=%s" },
193 { Opt_blank_pass, "pass=" },
194 { Opt_pass, "pass=%s" },
195 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100196 { Opt_blank_ip, "ip=" },
197 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400198 { Opt_ip, "ip=%s" },
199 { Opt_ip, "addr=%s" },
200 { Opt_unc, "unc=%s" },
201 { Opt_unc, "target=%s" },
202 { Opt_unc, "path=%s" },
203 { Opt_domain, "dom=%s" },
204 { Opt_domain, "domain=%s" },
205 { Opt_domain, "workgroup=%s" },
206 { Opt_srcaddr, "srcaddr=%s" },
207 { Opt_prefixpath, "prefixpath=%s" },
208 { Opt_iocharset, "iocharset=%s" },
209 { Opt_sockopt, "sockopt=%s" },
210 { Opt_netbiosname, "netbiosname=%s" },
211 { Opt_servern, "servern=%s" },
212 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400213 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400214 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400215 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400216
217 { Opt_ignore, "cred" },
218 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400219 { Opt_ignore, "cred=%s" },
220 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400221 { 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" },
234
235 { Opt_err, NULL }
236};
237
238enum {
239 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
240 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400241 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
242 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400243 Opt_sec_none,
244
245 Opt_sec_err
246};
247
248static 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" },
Jeff Layton76596242012-07-23 20:34:17 -0400256 { Opt_sec_ntlmv2, "nontlm" },
257 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400258 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400259 { Opt_sec_lanman, "lanman" },
260 { Opt_sec_none, "none" },
261
262 { Opt_sec_err, NULL }
263};
264
Jeff Layton15b6a472012-05-16 07:50:15 -0400265/* cache flavors */
266enum {
267 Opt_cache_loose,
268 Opt_cache_strict,
269 Opt_cache_none,
270 Opt_cache_err
271};
272
273static 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 }
278};
279
Jeff Layton23db65f2012-05-15 12:20:51 -0400280static const match_table_t cifs_smb_version_tokens = {
281 { Smb_1, SMB1_VERSION_STRING },
Steve French1080ef72011-02-24 18:07:19 +0000282 { Smb_21, SMB21_VERSION_STRING },
Jeff Layton23db65f2012-05-15 12:20:51 -0400283};
284
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300285static int ip_connect(struct TCP_Server_Info *server);
286static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400287static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400288static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400289static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
290 const char *devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Jeff Laytond5c56052008-12-01 18:42:33 -0500292/*
293 * cifs tcp session reconnection
294 *
295 * mark tcp session as reconnecting so temporarily locked
296 * mark all smb sessions as reconnecting for tcp session
297 * reconnect tcp session
298 * wake up waiters on reconnection? - (not needed currently)
299 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400300int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301cifs_reconnect(struct TCP_Server_Info *server)
302{
303 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500304 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000305 struct cifs_ses *ses;
306 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000307 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400308 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000311 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000312 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 next time through the loop */
314 spin_unlock(&GlobalMid_Lock);
315 return rc;
316 } else
317 server->tcpStatus = CifsNeedReconnect;
318 spin_unlock(&GlobalMid_Lock);
319 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400320#ifdef CONFIG_CIFS_SMB2
321 server->max_read = 0;
322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Joe Perchesb6b38f72010-04-21 03:50:45 +0000324 cFYI(1, "Reconnecting tcp session");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* before reconnecting the tcp session, mark the smb session (uid)
327 and the tid bad so they are not used until reconnected */
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500328 cFYI(1, "%s: marking sessions and tcons for reconnect", __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530329 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500330 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000331 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500332 ses->need_reconnect = true;
333 ses->ipc_tid = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500334 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000335 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500336 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530339 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* do not want to be sending data on a socket we are freeing */
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500342 cFYI(1, "%s: tearing down socket", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500343 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000344 if (server->ssocket) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000345 cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
346 server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800347 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesb6b38f72010-04-21 03:50:45 +0000348 cFYI(1, "Post shutdown state: 0x%x Flags: 0x%lx",
Steve French467a8f82007-06-27 22:41:32 +0000349 server->ssocket->state,
Joe Perchesb6b38f72010-04-21 03:50:45 +0000350 server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 sock_release(server->ssocket);
352 server->ssocket = NULL;
353 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500354 server->sequence_number = 0;
355 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500356 kfree(server->session_key.response);
357 server->session_key.response = NULL;
358 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000359 server->lstrp = jiffies;
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500360 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500362 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400363 INIT_LIST_HEAD(&retry_list);
364 cFYI(1, "%s: moving mids to private list", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500366 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
367 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400368 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
369 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400370 list_move(&mid_entry->qhead, &retry_list);
371 }
372 spin_unlock(&GlobalMid_Lock);
373
374 cFYI(1, "%s: issuing mid callbacks", __func__);
375 list_for_each_safe(tmp, tmp2, &retry_list) {
376 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500377 list_del_init(&mid_entry->qhead);
378 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400381 do {
Steve French6c3d8902006-07-31 22:46:20 +0000382 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300383
384 /* we should try only the port we connected to before */
385 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000386 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000387 cFYI(1, "reconnect error %d", rc);
Steve French0cb766a2005-04-28 22:41:11 -0700388 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 } else {
390 atomic_inc(&tcpSesReconnectCount);
391 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000392 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000393 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000394 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400396 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return rc;
399}
400
Jeff Laytonc74093b2011-01-11 07:24:23 -0500401static void
402cifs_echo_request(struct work_struct *work)
403{
404 int rc;
405 struct TCP_Server_Info *server = container_of(work,
406 struct TCP_Server_Info, echo.work);
407
Jeff Layton247ec9b2011-02-04 17:09:50 -0500408 /*
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400409 * We cannot send an echo if it is disabled or until the
410 * NEGOTIATE_PROTOCOL request is done, which is indicated by
411 * server->ops->need_neg() == true. Also, no need to ping if
412 * we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500413 */
Pavel Shilovsky286170a2012-05-25 10:43:58 +0400414 if (!server->ops->need_neg || server->ops->need_neg(server) ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400415 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Jeff Layton247ec9b2011-02-04 17:09:50 -0500416 time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500417 goto requeue_echo;
418
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400419 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500420 if (rc)
421 cFYI(1, "Unable to send echo request to server: %s",
422 server->hostname);
423
424requeue_echo:
Jeff Laytonda472fc2012-03-23 14:40:53 -0400425 queue_delayed_work(cifsiod_wq, &server->echo, SMB_ECHO_INTERVAL);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500426}
427
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400428static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400429allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400430{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400431 if (!server->bigbuf) {
432 server->bigbuf = (char *)cifs_buf_get();
433 if (!server->bigbuf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400434 cERROR(1, "No memory for large SMB response");
435 msleep(3000);
436 /* retry will check if exiting */
437 return false;
438 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400439 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400440 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400441 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400442 }
443
Jeff Layton2a37ef92011-10-19 15:29:23 -0400444 if (!server->smallbuf) {
445 server->smallbuf = (char *)cifs_small_buf_get();
446 if (!server->smallbuf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400447 cERROR(1, "No memory for SMB response");
448 msleep(1000);
449 /* retry will check if exiting */
450 return false;
451 }
452 /* beginning of smb buffer is cleared in our buf_get */
453 } else {
454 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400455 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400456 }
457
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400458 return true;
459}
460
Jeff Laytonba749e62011-10-11 06:41:32 -0400461static bool
462server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400463{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300464 /*
465 * We need to wait 2 echo intervals to make sure we handle such
466 * situations right:
467 * 1s client sends a normal SMB request
468 * 2s client gets a response
469 * 30s echo workqueue job pops, and decides we got a response recently
470 * and don't need to send another
471 * ...
472 * 65s kernel_recvmsg times out, and we see that we haven't gotten
473 * a response in >60s.
474 */
475 if (server->tcpStatus == CifsGood &&
476 time_after(jiffies, server->lstrp + 2 * SMB_ECHO_INTERVAL)) {
Jeff Laytonba749e62011-10-11 06:41:32 -0400477 cERROR(1, "Server %s has not responded in %d seconds. "
478 "Reconnecting...", server->hostname,
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300479 (2 * SMB_ECHO_INTERVAL) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400480 cifs_reconnect(server);
481 wake_up(&server->response_q);
482 return true;
483 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400484
Jeff Laytonba749e62011-10-11 06:41:32 -0400485 return false;
486}
487
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400488/*
489 * kvec_array_init - clone a kvec array, and advance into it
490 * @new: pointer to memory for cloned array
491 * @iov: pointer to original array
492 * @nr_segs: number of members in original array
493 * @bytes: number of bytes to advance into the cloned array
494 *
495 * This function will copy the array provided in iov to a section of memory
496 * and advance the specified number of bytes into the new array. It returns
497 * the number of segments in the new array. "new" must be at least as big as
498 * the original iov array.
499 */
500static unsigned int
501kvec_array_init(struct kvec *new, struct kvec *iov, unsigned int nr_segs,
502 size_t bytes)
503{
504 size_t base = 0;
505
506 while (bytes || !iov->iov_len) {
507 int copy = min(bytes, iov->iov_len);
508
509 bytes -= copy;
510 base += copy;
511 if (iov->iov_len == base) {
512 iov++;
513 nr_segs--;
514 base = 0;
515 }
516 }
517 memcpy(new, iov, sizeof(*iov) * nr_segs);
518 new->iov_base += base;
519 new->iov_len -= base;
520 return nr_segs;
521}
522
Jeff Layton1041e3f2011-10-19 15:28:27 -0400523static struct kvec *
524get_server_iovec(struct TCP_Server_Info *server, unsigned int nr_segs)
525{
526 struct kvec *new_iov;
527
528 if (server->iov && nr_segs <= server->nr_iov)
529 return server->iov;
530
531 /* not big enough -- allocate a new one and release the old */
532 new_iov = kmalloc(sizeof(*new_iov) * nr_segs, GFP_NOFS);
533 if (new_iov) {
534 kfree(server->iov);
535 server->iov = new_iov;
536 server->nr_iov = nr_segs;
537 }
538 return new_iov;
539}
540
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400541int
542cifs_readv_from_socket(struct TCP_Server_Info *server, struct kvec *iov_orig,
543 unsigned int nr_segs, unsigned int to_read)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400544{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400545 int length = 0;
546 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400547 unsigned int segs;
Jeff Laytone831e6c2011-10-11 06:41:32 -0400548 struct msghdr smb_msg;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400549 struct kvec *iov;
550
Jeff Layton1041e3f2011-10-19 15:28:27 -0400551 iov = get_server_iovec(server, nr_segs);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400552 if (!iov)
553 return -ENOMEM;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400554
Jeff Laytone831e6c2011-10-11 06:41:32 -0400555 smb_msg.msg_control = NULL;
556 smb_msg.msg_controllen = 0;
557
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400558 for (total_read = 0; to_read; total_read += length, to_read -= length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500559 try_to_freeze();
560
Jeff Laytonba749e62011-10-11 06:41:32 -0400561 if (server_unresponsive(server)) {
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400562 total_read = -EAGAIN;
Jeff Laytonba749e62011-10-11 06:41:32 -0400563 break;
564 }
565
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400566 segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
567
568 length = kernel_recvmsg(server->ssocket, &smb_msg,
569 iov, segs, to_read, 0);
570
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400571 if (server->tcpStatus == CifsExiting) {
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400572 total_read = -ESHUTDOWN;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400573 break;
574 } else if (server->tcpStatus == CifsNeedReconnect) {
575 cifs_reconnect(server);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400576 total_read = -EAGAIN;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400577 break;
578 } else if (length == -ERESTARTSYS ||
579 length == -EAGAIN ||
580 length == -EINTR) {
581 /*
582 * Minimum sleep to prevent looping, allowing socket
583 * to clear and app threads to set tcpStatus
584 * CifsNeedReconnect if server hung.
585 */
586 usleep_range(1000, 2000);
587 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400588 continue;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400589 } else if (length <= 0) {
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400590 cFYI(1, "Received no data or error: expecting %d "
591 "got %d", to_read, length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400592 cifs_reconnect(server);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400593 total_read = -EAGAIN;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400594 break;
595 }
596 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400597 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400598}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400599
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400600int
601cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
602 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400603{
604 struct kvec iov;
605
606 iov.iov_base = buf;
607 iov.iov_len = to_read;
608
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400609 return cifs_readv_from_socket(server, &iov, 1, to_read);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400610}
611
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400612static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400613is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400614{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400615 /*
616 * The first byte big endian of the length field,
617 * is actually not part of the length but the type
618 * with the most common, zero, as regular data.
619 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400620 switch (type) {
621 case RFC1002_SESSION_MESSAGE:
622 /* Regular SMB response */
623 return true;
624 case RFC1002_SESSION_KEEP_ALIVE:
625 cFYI(1, "RFC 1002 session keep alive");
626 break;
627 case RFC1002_POSITIVE_SESSION_RESPONSE:
628 cFYI(1, "RFC 1002 positive session response");
629 break;
630 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400631 /*
632 * We get this from Windows 98 instead of an error on
633 * SMB negprot response.
634 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400635 cFYI(1, "RFC 1002 negative session response");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400636 /* give server a second to clean up */
637 msleep(1000);
638 /*
639 * Always try 445 first on reconnect since we get NACK
640 * on some if we ever connected to port 139 (the NACK
641 * is since we do not begin with RFC1001 session
642 * initialize frame).
643 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400644 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400645 cifs_reconnect(server);
646 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400647 break;
648 default:
649 cERROR(1, "RFC 1002 unknown response type 0x%x", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400650 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400651 }
652
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400653 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400654}
655
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400656void
657dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400658{
659#ifdef CONFIG_CIFS_STATS2
660 mid->when_received = jiffies;
661#endif
662 spin_lock(&GlobalMid_Lock);
663 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400664 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400665 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400666 mid->mid_state = MID_RESPONSE_MALFORMED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400667 list_del_init(&mid->qhead);
668 spin_unlock(&GlobalMid_Lock);
669}
670
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400671static void
672handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400673 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400674{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400675 if (server->ops->check_trans2 &&
676 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400677 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400678 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400679 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400680 /* Was previous buf put in mpx struct for multi-rsp? */
681 if (!mid->multiRsp) {
682 /* smb buffer will be freed by user thread */
683 if (server->large_buf)
684 server->bigbuf = NULL;
685 else
686 server->smallbuf = NULL;
687 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400688 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400689}
690
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400691static void clean_demultiplex_info(struct TCP_Server_Info *server)
692{
693 int length;
694
695 /* take it off the list, if it's not already */
696 spin_lock(&cifs_tcp_ses_lock);
697 list_del_init(&server->tcp_ses_list);
698 spin_unlock(&cifs_tcp_ses_lock);
699
700 spin_lock(&GlobalMid_Lock);
701 server->tcpStatus = CifsExiting;
702 spin_unlock(&GlobalMid_Lock);
703 wake_up_all(&server->response_q);
704
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400705 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300706 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400707 if (server->credits <= 0)
708 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300709 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400710 /*
711 * Although there should not be any requests blocked on this queue it
712 * can not hurt to be paranoid and try to wake up requests that may
713 * haven been blocked when more than 50 at time were on the wire to the
714 * same server - they now will see the session is in exit state and get
715 * out of SendReceive.
716 */
717 wake_up_all(&server->request_q);
718 /* give those requests time to exit */
719 msleep(125);
720
721 if (server->ssocket) {
722 sock_release(server->ssocket);
723 server->ssocket = NULL;
724 }
725
726 if (!list_empty(&server->pending_mid_q)) {
727 struct list_head dispose_list;
728 struct mid_q_entry *mid_entry;
729 struct list_head *tmp, *tmp2;
730
731 INIT_LIST_HEAD(&dispose_list);
732 spin_lock(&GlobalMid_Lock);
733 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
734 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400735 cFYI(1, "Clearing mid 0x%llx", mid_entry->mid);
736 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400737 list_move(&mid_entry->qhead, &dispose_list);
738 }
739 spin_unlock(&GlobalMid_Lock);
740
741 /* now walk dispose list and issue callbacks */
742 list_for_each_safe(tmp, tmp2, &dispose_list) {
743 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400744 cFYI(1, "Callback mid 0x%llx", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400745 list_del_init(&mid_entry->qhead);
746 mid_entry->callback(mid_entry);
747 }
748 /* 1/8th of sec is more than enough time for them to exit */
749 msleep(125);
750 }
751
752 if (!list_empty(&server->pending_mid_q)) {
753 /*
754 * mpx threads have not exited yet give them at least the smb
755 * send timeout time for long ops.
756 *
757 * Due to delays on oplock break requests, we need to wait at
758 * least 45 seconds before giving up on a request getting a
759 * response and going ahead and killing cifsd.
760 */
761 cFYI(1, "Wait for exit from demultiplex thread");
762 msleep(46000);
763 /*
764 * If threads still have not exited they are probably never
765 * coming home not much else we can do but free the memory.
766 */
767 }
768
769 kfree(server->hostname);
Jeff Layton1041e3f2011-10-19 15:28:27 -0400770 kfree(server->iov);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400771 kfree(server);
772
773 length = atomic_dec_return(&tcpSesAllocCount);
774 if (length > 0)
775 mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
776 GFP_KERNEL);
777}
778
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400779static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400780standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
781{
782 int length;
783 char *buf = server->smallbuf;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400784 unsigned int pdu_length = get_rfc1002_length(buf);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400785
786 /* make sure this will fit in a large buffer */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400787 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) - 4) {
Jeff Laytone9097ab2011-10-19 15:29:40 -0400788 cERROR(1, "SMB response too long (%u bytes)",
789 pdu_length);
790 cifs_reconnect(server);
791 wake_up(&server->response_q);
792 return -EAGAIN;
793 }
794
795 /* switch to large buffer if too big for a small one */
796 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
797 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400798 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400799 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400800 }
801
802 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400803 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
804 pdu_length - HEADER_SIZE(server) + 1 + 4);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400805 if (length < 0)
806 return length;
807 server->total_read += length;
808
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400809 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400810
811 /*
812 * We know that we received enough to get to the MID as we
813 * checked the pdu_length earlier. Now check to see
814 * if the rest of the header is OK. We borrow the length
815 * var for the rest of the loop to avoid a new stack var.
816 *
817 * 48 bytes is enough to display the header and a little bit
818 * into the payload for debugging purposes.
819 */
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +0400820 length = server->ops->check_message(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400821 if (length != 0)
822 cifs_dump_mem("Bad SMB: ", buf,
823 min_t(unsigned int, server->total_read, 48));
824
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500825 if (!mid)
826 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400827
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400828 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500829 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400830}
831
832static int
Al Viro7c97c202011-06-21 08:51:28 -0400833cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int length;
Al Viro7c97c202011-06-21 08:51:28 -0400836 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400837 unsigned int pdu_length;
838 char *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct task_struct *task_to_wake = NULL;
840 struct mid_q_entry *mid_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 current->flags |= PF_MEMALLOC;
Joe Perchesb6b38f72010-04-21 03:50:45 +0000843 cFYI(1, "Demultiplex PID: %d", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400844
845 length = atomic_inc_return(&tcpSesAllocCount);
846 if (length > 1)
Steve French26f57362007-08-30 22:09:15 +0000847 mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
848 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700850 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000851 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700852 if (try_to_freeze())
853 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700854
Jeff Layton2a37ef92011-10-19 15:29:23 -0400855 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400856 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700857
Jeff Layton2a37ef92011-10-19 15:29:23 -0400858 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400859 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000860 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000861
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400862 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400863 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000864 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400865 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700866
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400867 /*
868 * The right amount was read from socket - 4 bytes,
869 * so we can now interpret the length field.
870 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400871 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700872
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400873 cFYI(1, "RFC1002 header 0x%x", pdu_length);
874 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000875 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700876
Jeff Layton89482a52011-10-19 15:28:57 -0400877 /* make sure we have enough to get to the MID */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400878 if (pdu_length < HEADER_SIZE(server) - 1 - 4) {
Jeff Layton89482a52011-10-19 15:28:57 -0400879 cERROR(1, "SMB response too short (%u bytes)",
880 pdu_length);
881 cifs_reconnect(server);
882 wake_up(&server->response_q);
883 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700884 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400885
Jeff Layton89482a52011-10-19 15:28:57 -0400886 /* read down to the MID */
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400887 length = cifs_read_from_socket(server, buf + 4,
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400888 HEADER_SIZE(server) - 1 - 4);
Jeff Layton89482a52011-10-19 15:28:57 -0400889 if (length < 0)
890 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400891 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400892
Pavel Shilovsky8aa26f32012-05-17 13:25:35 +0400893 mid_entry = server->ops->find_mid(server, buf);
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400894
Jeff Layton44d22d82011-10-19 15:29:49 -0400895 if (!mid_entry || !mid_entry->receive)
896 length = standard_receive3(server, mid_entry);
897 else
898 length = mid_entry->receive(server, mid_entry);
899
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400900 if (length < 0)
Steve Frenche4eb2952005-04-28 22:41:09 -0700901 continue;
902
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400903 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400904 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700905
Steve Frenchfda35942011-01-20 18:06:34 +0000906 server->lstrp = jiffies;
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500907 if (mid_entry != NULL) {
Jeff Layton2a37ef92011-10-19 15:29:23 -0400908 if (!mid_entry->multiRsp || mid_entry->multiEnd)
909 mid_entry->callback(mid_entry);
Pavel Shilovsky7f0adb52012-05-28 15:50:10 +0400910 } else if (!server->ops->is_oplock_break ||
911 !server->ops->is_oplock_break(buf, server)) {
Joe Perchesb6b38f72010-04-21 03:50:45 +0000912 cERROR(1, "No task to wake, unknown frame received! "
Jeff Layton80975312011-01-11 07:24:02 -0500913 "NumMids %d", atomic_read(&midCount));
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400914 cifs_dump_mem("Received Data is: ", buf,
915 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +0000916#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky7f0adb52012-05-28 15:50:10 +0400917 if (server->ops->dump_detail)
918 server->ops->dump_detail(buf);
Steve French39798772006-05-31 22:40:51 +0000919 cifs_dump_mids(server);
920#endif /* CIFS_DEBUG2 */
Steve French50c2f752007-07-13 00:33:32 +0000921
Steve Frenche4eb2952005-04-28 22:41:09 -0700922 }
923 } /* end while !EXITING */
924
Justin P. Mattockfd62cb72011-02-24 22:15:02 -0800925 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -0400926 cifs_buf_release(server->bigbuf);
927 if (server->smallbuf) /* no sense logging a debug message if NULL */
928 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -0400930 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400931 clean_demultiplex_info(server);
Steve French50c2f752007-07-13 00:33:32 +0000932
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -0400933 /* if server->tsk was NULL then wait for a signal before exiting */
934 if (!task_to_wake) {
935 set_current_state(TASK_INTERRUPTIBLE);
936 while (!signal_pending(current)) {
937 schedule();
938 set_current_state(TASK_INTERRUPTIBLE);
939 }
940 set_current_state(TASK_RUNNING);
941 }
942
Jeff Layton0468a2c2008-12-01 07:09:35 -0500943 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Jeff Laytonc359cf32007-11-16 22:22:06 +0000946/* extract the host portion of the UNC string */
947static char *
948extract_hostname(const char *unc)
949{
950 const char *src;
951 char *dst, *delim;
952 unsigned int len;
953
954 /* skip double chars at beginning of string */
955 /* BB: check validity of these bytes? */
956 src = unc + 2;
957
958 /* delimiter between hostname and sharename is always '\\' now */
959 delim = strchr(src, '\\');
960 if (!delim)
961 return ERR_PTR(-EINVAL);
962
963 len = delim - src;
964 dst = kmalloc((len + 1), GFP_KERNEL);
965 if (dst == NULL)
966 return ERR_PTR(-ENOMEM);
967
968 memcpy(dst, src, len);
969 dst[len] = '\0';
970
971 return dst;
972}
973
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400974static int get_option_ul(substring_t args[], unsigned long *option)
975{
976 int rc;
977 char *string;
978
979 string = match_strdup(args);
980 if (string == NULL)
981 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +0100982 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400983 kfree(string);
984
985 return rc;
986}
987
988
989static int cifs_parse_security_flavors(char *value,
990 struct smb_vol *vol)
991{
992
993 substring_t args[MAX_OPT_ARGS];
994
995 switch (match_token(value, cifs_secflavor_tokens, args)) {
996 case Opt_sec_krb5:
997 vol->secFlg |= CIFSSEC_MAY_KRB5;
998 break;
999 case Opt_sec_krb5i:
1000 vol->secFlg |= CIFSSEC_MAY_KRB5 | CIFSSEC_MUST_SIGN;
1001 break;
1002 case Opt_sec_krb5p:
1003 /* vol->secFlg |= CIFSSEC_MUST_SEAL | CIFSSEC_MAY_KRB5; */
1004 cERROR(1, "Krb5 cifs privacy not supported");
1005 break;
1006 case Opt_sec_ntlmssp:
1007 vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
1008 break;
1009 case Opt_sec_ntlmsspi:
1010 vol->secFlg |= CIFSSEC_MAY_NTLMSSP | CIFSSEC_MUST_SIGN;
1011 break;
1012 case Opt_ntlm:
1013 /* ntlm is default so can be turned off too */
1014 vol->secFlg |= CIFSSEC_MAY_NTLM;
1015 break;
1016 case Opt_sec_ntlmi:
1017 vol->secFlg |= CIFSSEC_MAY_NTLM | CIFSSEC_MUST_SIGN;
1018 break;
Jeff Layton76596242012-07-23 20:34:17 -04001019 case Opt_sec_ntlmv2:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001020 vol->secFlg |= CIFSSEC_MAY_NTLMV2;
1021 break;
1022 case Opt_sec_ntlmv2i:
1023 vol->secFlg |= CIFSSEC_MAY_NTLMV2 | CIFSSEC_MUST_SIGN;
1024 break;
1025#ifdef CONFIG_CIFS_WEAK_PW_HASH
1026 case Opt_sec_lanman:
1027 vol->secFlg |= CIFSSEC_MAY_LANMAN;
1028 break;
1029#endif
1030 case Opt_sec_none:
1031 vol->nullauth = 1;
1032 break;
1033 default:
1034 cERROR(1, "bad security option: %s", value);
1035 return 1;
1036 }
1037
1038 return 0;
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001042cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1043{
1044 substring_t args[MAX_OPT_ARGS];
1045
1046 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1047 case Opt_cache_loose:
1048 vol->direct_io = false;
1049 vol->strict_io = false;
1050 break;
1051 case Opt_cache_strict:
1052 vol->direct_io = false;
1053 vol->strict_io = true;
1054 break;
1055 case Opt_cache_none:
1056 vol->direct_io = true;
1057 vol->strict_io = false;
1058 break;
1059 default:
1060 cERROR(1, "bad cache= option: %s", value);
1061 return 1;
1062 }
1063 return 0;
1064}
1065
1066static int
Jeff Layton23db65f2012-05-15 12:20:51 -04001067cifs_parse_smb_version(char *value, struct smb_vol *vol)
1068{
1069 substring_t args[MAX_OPT_ARGS];
1070
1071 switch (match_token(value, cifs_smb_version_tokens, args)) {
1072 case Smb_1:
1073 vol->ops = &smb1_operations;
1074 vol->vals = &smb1_values;
1075 break;
Steve French1080ef72011-02-24 18:07:19 +00001076#ifdef CONFIG_CIFS_SMB2
1077 case Smb_21:
1078 vol->ops = &smb21_operations;
1079 vol->vals = &smb21_values;
1080 break;
1081#endif
Jeff Layton23db65f2012-05-15 12:20:51 -04001082 default:
1083 cERROR(1, "Unknown vers= option specified: %s", value);
1084 return 1;
1085 }
1086 return 0;
1087}
1088
1089static int
Sean Finneyb9468452011-04-11 13:19:32 +00001090cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve French50c2f752007-07-13 00:33:32 +00001091 struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001093 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001094 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 unsigned int temp_len, i, j;
1096 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001097 short int override_uid = -1;
1098 short int override_gid = -1;
1099 bool uid_specified = false;
1100 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001101 bool sloppy = false;
1102 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001103 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001104 char *string = NULL;
1105 char *tmp_end, *value;
1106 char delim;
Jeff Layton296838b2012-05-16 07:53:01 -04001107 bool cache_specified = false;
1108 static bool cache_warned = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001111 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001112 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Jeff Layton88463992010-11-22 15:31:03 -05001114 /*
1115 * does not have to be perfect mapping since field is
1116 * informational, only used for servers that do not support
1117 * port 445 and it can be overridden at mount time
1118 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001119 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1120 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001121 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1122
Jeff Layton1397f2e2011-01-07 11:30:28 -05001123 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001124 /* null target name indicates to use *SMBSERVR default called name
1125 if we end up sending RFC1001 session initialize */
1126 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001127 vol->cred_uid = current_uid();
1128 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001129 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001130
1131 /* default to only allowing write access to owner of the mount */
1132 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001135 /* default is always to request posix paths. */
1136 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001137 /* default to using server inode numbers where available */
1138 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001139
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301140 vol->actimeo = CIFS_DEF_ACTIMEO;
1141
Jeff Layton23db65f2012-05-15 12:20:51 -04001142 /* FIXME: add autonegotiation -- for now, SMB1 is default */
1143 vol->ops = &smb1_operations;
1144 vol->vals = &smb1_values;
1145
Sean Finneyb9468452011-04-11 13:19:32 +00001146 if (!mountdata)
1147 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Sean Finneyb9468452011-04-11 13:19:32 +00001149 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1150 if (!mountdata_copy)
1151 goto cifs_parse_mount_err;
1152
1153 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001154 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001155
Steve French50c2f752007-07-13 00:33:32 +00001156 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001157 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 separator[0] = options[4];
1159 options += 5;
1160 } else {
Joe Perchesb6b38f72010-04-21 03:50:45 +00001161 cFYI(1, "Null separator not allowed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001164 vol->backupuid_specified = false; /* no backup intent for a user */
1165 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001168 substring_t args[MAX_OPT_ARGS];
1169 unsigned long option;
1170 int token;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!*data)
1173 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001175 token = match_token(data, cifs_mount_option_tokens, args);
1176
1177 switch (token) {
1178
1179 /* Ingnore the following */
1180 case Opt_ignore:
1181 break;
1182
1183 /* Boolean values */
1184 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001186 break;
1187 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001189 break;
1190 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001191 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001192 break;
1193 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001194 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001195 break;
1196 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001197 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001198 break;
1199 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001200 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001201 break;
1202 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001204 break;
1205 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001207 break;
1208 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001210 break;
1211 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001213 break;
1214 case Opt_mapchars:
Steve French6a0b4822005-04-28 22:41:05 -07001215 vol->remap = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001216 break;
1217 case Opt_nomapchars:
Steve French6a0b4822005-04-28 22:41:05 -07001218 vol->remap = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001219 break;
1220 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001221 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001222 break;
1223 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001224 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001225 break;
1226 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001227 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001228 break;
1229 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001230 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001231 break;
1232 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001233 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001234 break;
1235 case Opt_nounix:
Steve Frenchc18c8422007-07-18 23:21:09 +00001236 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001237 break;
1238 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001239 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001240 break;
1241 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001242 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001243 break;
1244 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001245 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001246 /*
1247 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001248 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001249 * local vfs will do advisory
1250 */
Steve French50c2f752007-07-13 00:33:32 +00001251 if (vol->file_mode ==
1252 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001253 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001254 break;
1255 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001256 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001257 break;
1258 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001260 break;
1261 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001263 break;
1264 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001265 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001266 break;
1267 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001268 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001269 break;
1270 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001272 break;
1273 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001275 break;
1276 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001278 break;
1279 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001281 break;
1282 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001283 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001284 break;
1285 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001286 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001287 break;
1288 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001290 break;
1291 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001293 break;
1294 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001295 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001296 break;
1297 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001298 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001299 break;
1300 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001301 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001302 break;
1303 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001305 break;
1306 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001308 break;
1309 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001310 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001311 break;
1312 case Opt_sign:
Steve French750d1152006-06-27 06:28:30 +00001313 vol->secFlg |= CIFSSEC_MUST_SIGN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001314 break;
1315 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001316 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001317 * is a per tree connection (mount) not a per socket
1318 * or per-smb connection option in the protocol
1319 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1320 */
Steve French95b1cb92008-05-15 16:44:38 +00001321 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001322 break;
1323 case Opt_direct:
Jeff Layton296838b2012-05-16 07:53:01 -04001324 cache_specified = true;
Jeff Layton15b6a472012-05-16 07:50:15 -04001325 vol->direct_io = true;
1326 vol->strict_io = false;
Jeff Layton09983b22012-05-16 07:53:00 -04001327 cERROR(1, "The \"directio\" option will be removed in "
1328 "3.7. Please switch to the \"cache=none\" "
1329 "option.");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001330 break;
1331 case Opt_strictcache:
Jeff Layton296838b2012-05-16 07:53:01 -04001332 cache_specified = true;
Jeff Layton15b6a472012-05-16 07:50:15 -04001333 vol->direct_io = false;
1334 vol->strict_io = true;
Jeff Layton09983b22012-05-16 07:53:00 -04001335 cERROR(1, "The \"strictcache\" option will be removed "
1336 "in 3.7. Please switch to the \"cache=strict\" "
1337 "option.");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001338 break;
1339 case Opt_noac:
Steve French50c2f752007-07-13 00:33:32 +00001340 printk(KERN_WARNING "CIFS: Mount option noac not "
1341 "supported. Instead set "
1342 "/proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001343 break;
1344 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301345#ifndef CONFIG_CIFS_FSCACHE
Jeff Layton83fb0862011-06-08 07:35:24 -04001346 cERROR(1, "FS-Cache support needs CONFIG_CIFS_FSCACHE "
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301347 "kernel config option set");
Sean Finneyb9468452011-04-11 13:19:32 +00001348 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301349#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301350 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001351 break;
1352 case Opt_mfsymlinks:
Stefan Metzmacher736a3322010-07-30 14:56:00 +02001353 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001354 break;
1355 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001356 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001357 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001358 case Opt_sloppy:
1359 sloppy = true;
1360 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001361
1362 /* Numeric Values */
1363 case Opt_backupuid:
1364 if (get_option_ul(args, &option)) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001365 cERROR(1, "%s: Invalid backupuid value",
1366 __func__);
1367 goto cifs_parse_mount_err;
1368 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001369 vol->backupuid = option;
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001370 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001371 break;
1372 case Opt_backupgid:
1373 if (get_option_ul(args, &option)) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001374 cERROR(1, "%s: Invalid backupgid value",
1375 __func__);
1376 goto cifs_parse_mount_err;
1377 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001378 vol->backupgid = option;
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001379 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001380 break;
1381 case Opt_uid:
1382 if (get_option_ul(args, &option)) {
1383 cERROR(1, "%s: Invalid uid value",
1384 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001385 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001387 vol->linux_uid = option;
1388 uid_specified = true;
1389 break;
1390 case Opt_cruid:
1391 if (get_option_ul(args, &option)) {
1392 cERROR(1, "%s: Invalid cruid value",
1393 __func__);
1394 goto cifs_parse_mount_err;
1395 }
1396 vol->cred_uid = option;
1397 break;
1398 case Opt_gid:
1399 if (get_option_ul(args, &option)) {
1400 cERROR(1, "%s: Invalid gid value",
1401 __func__);
1402 goto cifs_parse_mount_err;
1403 }
1404 vol->linux_gid = option;
1405 gid_specified = true;
1406 break;
1407 case Opt_file_mode:
1408 if (get_option_ul(args, &option)) {
1409 cERROR(1, "%s: Invalid file_mode value",
1410 __func__);
1411 goto cifs_parse_mount_err;
1412 }
1413 vol->file_mode = option;
1414 break;
1415 case Opt_dirmode:
1416 if (get_option_ul(args, &option)) {
1417 cERROR(1, "%s: Invalid dir_mode value",
1418 __func__);
1419 goto cifs_parse_mount_err;
1420 }
1421 vol->dir_mode = option;
1422 break;
1423 case Opt_port:
1424 if (get_option_ul(args, &option)) {
1425 cERROR(1, "%s: Invalid port value",
1426 __func__);
1427 goto cifs_parse_mount_err;
1428 }
1429 vol->port = option;
1430 break;
1431 case Opt_rsize:
1432 if (get_option_ul(args, &option)) {
1433 cERROR(1, "%s: Invalid rsize value",
1434 __func__);
1435 goto cifs_parse_mount_err;
1436 }
1437 vol->rsize = option;
1438 break;
1439 case Opt_wsize:
1440 if (get_option_ul(args, &option)) {
1441 cERROR(1, "%s: Invalid wsize value",
1442 __func__);
1443 goto cifs_parse_mount_err;
1444 }
1445 vol->wsize = option;
1446 break;
1447 case Opt_actimeo:
1448 if (get_option_ul(args, &option)) {
1449 cERROR(1, "%s: Invalid actimeo value",
1450 __func__);
1451 goto cifs_parse_mount_err;
1452 }
1453 vol->actimeo = HZ * option;
1454 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1455 cERROR(1, "CIFS: attribute cache"
1456 "timeout too large");
1457 goto cifs_parse_mount_err;
1458 }
1459 break;
1460
1461 /* String Arguments */
1462
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001463 case Opt_blank_user:
1464 /* null user, ie. anonymous authentication */
1465 vol->nullauth = 1;
1466 vol->username = NULL;
1467 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001468 case Opt_user:
1469 string = match_strdup(args);
1470 if (string == NULL)
1471 goto out_nomem;
1472
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001473 if (strnlen(string, MAX_USERNAME_SIZE) >
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001474 MAX_USERNAME_SIZE) {
1475 printk(KERN_WARNING "CIFS: username too long\n");
1476 goto cifs_parse_mount_err;
1477 }
1478 vol->username = kstrdup(string, GFP_KERNEL);
1479 if (!vol->username) {
1480 printk(KERN_WARNING "CIFS: no memory "
1481 "for username\n");
1482 goto cifs_parse_mount_err;
1483 }
1484 break;
1485 case Opt_blank_pass:
1486 vol->password = NULL;
1487 break;
1488 case Opt_pass:
1489 /* passwords have to be handled differently
1490 * to allow the character used for deliminator
1491 * to be passed within them
1492 */
1493
1494 /* Obtain the value string */
1495 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001496 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001497
1498 /* Set tmp_end to end of the string */
1499 tmp_end = (char *) value + strlen(value);
1500
1501 /* Check if following character is the deliminator
1502 * If yes, we have encountered a double deliminator
1503 * reset the NULL character to the deliminator
1504 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301505 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001506 tmp_end[0] = delim;
1507
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301508 /* Keep iterating until we get to a single
1509 * deliminator OR the end
1510 */
1511 while ((tmp_end = strchr(tmp_end, delim))
1512 != NULL && (tmp_end[1] == delim)) {
1513 tmp_end = (char *) &tmp_end[2];
1514 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001515
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301516 /* Reset var options to point to next element */
1517 if (tmp_end) {
1518 tmp_end[0] = '\0';
1519 options = (char *) &tmp_end[1];
1520 } else
1521 /* Reached the end of the mount option
1522 * string */
1523 options = end;
1524 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001525
1526 /* Now build new password string */
1527 temp_len = strlen(value);
1528 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1529 if (vol->password == NULL) {
1530 printk(KERN_WARNING "CIFS: no memory "
1531 "for password\n");
1532 goto cifs_parse_mount_err;
1533 }
1534
1535 for (i = 0, j = 0; i < temp_len; i++, j++) {
1536 vol->password[j] = value[i];
1537 if ((value[i] == delim) &&
1538 value[i+1] == delim)
1539 /* skip the second deliminator */
1540 i++;
1541 }
1542 vol->password[j] = '\0';
1543 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001544 case Opt_blank_ip:
1545 vol->UNCip = NULL;
1546 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001547 case Opt_ip:
1548 string = match_strdup(args);
1549 if (string == NULL)
1550 goto out_nomem;
1551
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001552 if (strnlen(string, INET6_ADDRSTRLEN) >
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001553 INET6_ADDRSTRLEN) {
1554 printk(KERN_WARNING "CIFS: ip address "
1555 "too long\n");
1556 goto cifs_parse_mount_err;
1557 }
1558 vol->UNCip = kstrdup(string, GFP_KERNEL);
1559 if (!vol->UNCip) {
1560 printk(KERN_WARNING "CIFS: no memory "
1561 "for UNC IP\n");
1562 goto cifs_parse_mount_err;
1563 }
1564 break;
1565 case Opt_unc:
1566 string = match_strdup(args);
1567 if (string == NULL)
1568 goto out_nomem;
1569
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001570 temp_len = strnlen(string, 300);
1571 if (temp_len == 300) {
1572 printk(KERN_WARNING "CIFS: UNC name too long\n");
1573 goto cifs_parse_mount_err;
1574 }
1575
Sachin Prabhue4b41fb2012-04-04 01:58:56 +01001576 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
1577 if (vol->UNC == NULL) {
1578 printk(KERN_WARNING "CIFS: no memory for UNC\n");
1579 goto cifs_parse_mount_err;
1580 }
1581 strcpy(vol->UNC, string);
1582
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001583 if (strncmp(string, "//", 2) == 0) {
1584 vol->UNC[0] = '\\';
1585 vol->UNC[1] = '\\';
1586 } else if (strncmp(string, "\\\\", 2) != 0) {
1587 printk(KERN_WARNING "CIFS: UNC Path does not "
1588 "begin with // or \\\\\n");
1589 goto cifs_parse_mount_err;
1590 }
1591
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001592 break;
1593 case Opt_domain:
1594 string = match_strdup(args);
1595 if (string == NULL)
1596 goto out_nomem;
1597
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001598 if (strnlen(string, 256) == 256) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001599 printk(KERN_WARNING "CIFS: domain name too"
1600 " long\n");
1601 goto cifs_parse_mount_err;
1602 }
1603
1604 vol->domainname = kstrdup(string, GFP_KERNEL);
1605 if (!vol->domainname) {
1606 printk(KERN_WARNING "CIFS: no memory "
1607 "for domainname\n");
1608 goto cifs_parse_mount_err;
1609 }
1610 cFYI(1, "Domain name set");
1611 break;
1612 case Opt_srcaddr:
1613 string = match_strdup(args);
1614 if (string == NULL)
1615 goto out_nomem;
1616
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001617 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001618 (struct sockaddr *)&vol->srcaddr,
1619 string, strlen(string))) {
1620 printk(KERN_WARNING "CIFS: Could not parse"
1621 " srcaddr: %s\n", string);
1622 goto cifs_parse_mount_err;
1623 }
1624 break;
1625 case Opt_prefixpath:
1626 string = match_strdup(args);
1627 if (string == NULL)
1628 goto out_nomem;
1629
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001630 temp_len = strnlen(string, 1024);
1631 if (string[0] != '/')
1632 temp_len++; /* missing leading slash */
1633 if (temp_len > 1024) {
1634 printk(KERN_WARNING "CIFS: prefix too long\n");
1635 goto cifs_parse_mount_err;
1636 }
1637
1638 vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
1639 if (vol->prepath == NULL) {
1640 printk(KERN_WARNING "CIFS: no memory "
1641 "for path prefix\n");
1642 goto cifs_parse_mount_err;
1643 }
1644
1645 if (string[0] != '/') {
1646 vol->prepath[0] = '/';
1647 strcpy(vol->prepath+1, string);
1648 } else
1649 strcpy(vol->prepath, string);
1650
1651 break;
1652 case Opt_iocharset:
1653 string = match_strdup(args);
1654 if (string == NULL)
1655 goto out_nomem;
1656
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001657 if (strnlen(string, 1024) >= 65) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001658 printk(KERN_WARNING "CIFS: iocharset name "
1659 "too long.\n");
1660 goto cifs_parse_mount_err;
1661 }
1662
1663 if (strnicmp(string, "default", 7) != 0) {
1664 vol->iocharset = kstrdup(string,
1665 GFP_KERNEL);
1666 if (!vol->iocharset) {
1667 printk(KERN_WARNING "CIFS: no memory"
1668 "for charset\n");
1669 goto cifs_parse_mount_err;
1670 }
1671 }
1672 /* if iocharset not set then load_nls_default
1673 * is used by caller
1674 */
1675 cFYI(1, "iocharset set to %s", string);
1676 break;
1677 case Opt_sockopt:
1678 string = match_strdup(args);
1679 if (string == NULL)
1680 goto out_nomem;
1681
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001682 if (strnicmp(string, "TCP_NODELAY", 11) == 0)
1683 vol->sockopt_tcp_nodelay = 1;
1684 break;
1685 case Opt_netbiosname:
1686 string = match_strdup(args);
1687 if (string == NULL)
1688 goto out_nomem;
1689
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001690 memset(vol->source_rfc1001_name, 0x20,
1691 RFC1001_NAME_LEN);
1692 /*
1693 * FIXME: are there cases in which a comma can
1694 * be valid in workstation netbios name (and
1695 * need special handling)?
1696 */
1697 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1698 /* don't ucase netbiosname for user */
1699 if (string[i] == 0)
1700 break;
1701 vol->source_rfc1001_name[i] = string[i];
1702 }
1703 /* The string has 16th byte zero still from
1704 * set at top of the function
1705 */
1706 if (i == RFC1001_NAME_LEN && string[i] != 0)
1707 printk(KERN_WARNING "CIFS: netbiosname"
1708 " longer than 15 truncated.\n");
1709
1710 break;
1711 case Opt_servern:
1712 /* servernetbiosname specified override *SMBSERVER */
1713 string = match_strdup(args);
1714 if (string == NULL)
1715 goto out_nomem;
1716
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001717 /* last byte, type, is 0x20 for servr type */
1718 memset(vol->target_rfc1001_name, 0x20,
1719 RFC1001_NAME_LEN_WITH_NULL);
1720
1721 /* BB are there cases in which a comma can be
1722 valid in this workstation netbios name
1723 (and need special handling)? */
1724
1725 /* user or mount helper must uppercase the
1726 netbios name */
1727 for (i = 0; i < 15; i++) {
1728 if (string[i] == 0)
1729 break;
1730 vol->target_rfc1001_name[i] = string[i];
1731 }
1732 /* The string has 16th byte zero still from
1733 set at top of the function */
1734 if (i == RFC1001_NAME_LEN && string[i] != 0)
1735 printk(KERN_WARNING "CIFS: server net"
1736 "biosname longer than 15 truncated.\n");
1737 break;
1738 case Opt_ver:
1739 string = match_strdup(args);
1740 if (string == NULL)
1741 goto out_nomem;
1742
Jeff Layton5249af32012-05-15 12:04:03 -04001743 if (strnicmp(string, "1", 1) == 0) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001744 /* This is the default */
1745 break;
1746 }
1747 /* For all other value, error */
1748 printk(KERN_WARNING "CIFS: Invalid version"
1749 " specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001750 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04001751 case Opt_vers:
1752 string = match_strdup(args);
1753 if (string == NULL)
1754 goto out_nomem;
1755
1756 if (cifs_parse_smb_version(string, vol) != 0)
1757 goto cifs_parse_mount_err;
1758 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001759 case Opt_sec:
1760 string = match_strdup(args);
1761 if (string == NULL)
1762 goto out_nomem;
1763
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001764 if (cifs_parse_security_flavors(string, vol) != 0)
1765 goto cifs_parse_mount_err;
1766 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04001767 case Opt_cache:
Jeff Layton296838b2012-05-16 07:53:01 -04001768 cache_specified = true;
Jeff Layton15b6a472012-05-16 07:50:15 -04001769 string = match_strdup(args);
1770 if (string == NULL)
1771 goto out_nomem;
1772
1773 if (cifs_parse_cache_flavor(string, vol) != 0)
1774 goto cifs_parse_mount_err;
1775 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001776 default:
Jeff Laytond8162552012-03-23 14:40:56 -04001777 /*
1778 * An option we don't recognize. Save it off for later
1779 * if we haven't already found one
1780 */
1781 if (!invalid)
1782 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001783 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001785 /* Free up any allocated string */
1786 kfree(string);
1787 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04001789
Jeff Laytond8162552012-03-23 14:40:56 -04001790 if (!sloppy && invalid) {
1791 printk(KERN_ERR "CIFS: Unknown mount option \"%s\"\n", invalid);
1792 goto cifs_parse_mount_err;
1793 }
1794
Jeff Layton8a8798a2012-01-17 16:09:15 -05001795#ifndef CONFIG_KEYS
1796 /* Muliuser mounts require CONFIG_KEYS support */
1797 if (vol->multiuser) {
1798 cERROR(1, "Multiuser mounts require kernels with "
1799 "CONFIG_KEYS enabled.");
Sean Finneyb9468452011-04-11 13:19:32 +00001800 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04001801 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05001802#endif
Jeff Layton0eb8a132010-10-06 19:51:12 -04001803
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001804 if (vol->UNCip == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 vol->UNCip = &vol->UNC[2];
1806
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001807 if (uid_specified)
1808 vol->override_uid = override_uid;
1809 else if (override_uid == 1)
1810 printk(KERN_NOTICE "CIFS: ignoring forceuid mount option "
1811 "specified with no uid= option.\n");
1812
1813 if (gid_specified)
1814 vol->override_gid = override_gid;
1815 else if (override_gid == 1)
1816 printk(KERN_NOTICE "CIFS: ignoring forcegid mount option "
1817 "specified with no gid= option.\n");
1818
Jeff Layton296838b2012-05-16 07:53:01 -04001819 /* FIXME: remove this block in 3.7 */
1820 if (!cache_specified && !cache_warned) {
1821 cache_warned = true;
1822 printk(KERN_NOTICE "CIFS: no cache= option specified, using "
1823 "\"cache=loose\". This default will change "
1824 "to \"cache=strict\" in 3.7.\n");
1825 }
1826
Sean Finneyb9468452011-04-11 13:19:32 +00001827 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00001829
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001830out_nomem:
1831 printk(KERN_WARNING "Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001832cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00001834 kfree(mountdata_copy);
1835 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Ben Greear3eb9a882010-09-01 17:06:02 -07001838/** Returns true if srcaddr isn't specified and rhs isn't
1839 * specified, or if srcaddr is specified and
1840 * matches the IP address of the rhs argument.
1841 */
Jeff Layton45151482010-07-06 20:43:02 -04001842static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07001843srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
1844{
1845 switch (srcaddr->sa_family) {
1846 case AF_UNSPEC:
1847 return (rhs->sa_family == AF_UNSPEC);
1848 case AF_INET: {
1849 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1850 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1851 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1852 }
1853 case AF_INET6: {
1854 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1855 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)&rhs;
1856 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1857 }
1858 default:
1859 WARN_ON(1);
1860 return false; /* don't expect to be here */
1861 }
1862}
1863
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001864/*
1865 * If no port is specified in addr structure, we try to match with 445 port
1866 * and if it fails - with 139 ports. It should be called only if address
1867 * families of server and addr are equal.
1868 */
1869static bool
1870match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1871{
Steve French6da97912011-03-13 18:55:55 +00001872 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03001873
1874 switch (addr->sa_family) {
1875 case AF_INET:
1876 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1877 port = ((struct sockaddr_in *) addr)->sin_port;
1878 break;
1879 case AF_INET6:
1880 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1881 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1882 break;
1883 default:
1884 WARN_ON(1);
1885 return false;
1886 }
1887
1888 if (!port) {
1889 port = htons(CIFS_PORT);
1890 if (port == *sport)
1891 return true;
1892
1893 port = htons(RFC1001_PORT);
1894 }
1895
1896 return port == *sport;
1897}
Ben Greear3eb9a882010-09-01 17:06:02 -07001898
1899static bool
1900match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1901 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Jeff Layton45151482010-07-06 20:43:02 -04001903 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001904 case AF_INET: {
1905 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1906 struct sockaddr_in *srv_addr4 =
1907 (struct sockaddr_in *)&server->dstaddr;
1908
1909 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04001910 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001911 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001912 }
1913 case AF_INET6: {
1914 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1915 struct sockaddr_in6 *srv_addr6 =
1916 (struct sockaddr_in6 *)&server->dstaddr;
1917
Jeff Layton45151482010-07-06 20:43:02 -04001918 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001919 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04001920 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001921 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04001922 return false;
Jeff Layton45151482010-07-06 20:43:02 -04001923 break;
1924 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03001925 default:
1926 WARN_ON(1);
1927 return false; /* don't expect to be here */
1928 }
Jeff Layton45151482010-07-06 20:43:02 -04001929
Ben Greear3eb9a882010-09-01 17:06:02 -07001930 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
1931 return false;
1932
Jeff Layton45151482010-07-06 20:43:02 -04001933 return true;
1934}
1935
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001936static bool
1937match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
1938{
1939 unsigned int secFlags;
1940
1941 if (vol->secFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
1942 secFlags = vol->secFlg;
1943 else
1944 secFlags = global_secflags | vol->secFlg;
1945
1946 switch (server->secType) {
1947 case LANMAN:
1948 if (!(secFlags & (CIFSSEC_MAY_LANMAN|CIFSSEC_MAY_PLNTXT)))
1949 return false;
1950 break;
1951 case NTLMv2:
1952 if (!(secFlags & CIFSSEC_MAY_NTLMV2))
1953 return false;
1954 break;
1955 case NTLM:
1956 if (!(secFlags & CIFSSEC_MAY_NTLM))
1957 return false;
1958 break;
1959 case Kerberos:
1960 if (!(secFlags & CIFSSEC_MAY_KRB5))
1961 return false;
1962 break;
1963 case RawNTLMSSP:
1964 if (!(secFlags & CIFSSEC_MAY_NTLMSSP))
1965 return false;
1966 break;
1967 default:
1968 /* shouldn't happen */
1969 return false;
1970 }
1971
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001972 /* now check if signing mode is acceptable */
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001973 if ((secFlags & CIFSSEC_MAY_SIGN) == 0 &&
Steve French96daf2b2011-05-27 04:34:02 +00001974 (server->sec_mode & SECMODE_SIGN_REQUIRED))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001975 return false;
1976 else if (((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) &&
Steve French96daf2b2011-05-27 04:34:02 +00001977 (server->sec_mode &
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04001978 (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED)) == 0)
1979 return false;
1980
1981 return true;
1982}
1983
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001984static int match_server(struct TCP_Server_Info *server, struct sockaddr *addr,
1985 struct smb_vol *vol)
1986{
Jeff Layton23db65f2012-05-15 12:20:51 -04001987 if ((server->vals != vol->vals) || (server->ops != vol->ops))
1988 return 0;
1989
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00001990 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1991 return 0;
1992
1993 if (!match_address(server, addr,
1994 (struct sockaddr *)&vol->srcaddr))
1995 return 0;
1996
1997 if (!match_port(server, addr))
1998 return 0;
1999
2000 if (!match_security(server, vol))
2001 return 0;
2002
2003 return 1;
2004}
2005
Jeff Layton45151482010-07-06 20:43:02 -04002006static struct TCP_Server_Info *
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002007cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002008{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002009 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302011 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002012 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002013 if (!match_server(server, addr, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002014 continue;
2015
Jeff Laytone7ddee92008-11-14 13:44:38 -05002016 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302017 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesb6b38f72010-04-21 03:50:45 +00002018 cFYI(1, "Existing tcp session with server found");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002019 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302021 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return NULL;
2023}
2024
Jeff Layton14fbf502008-11-14 13:53:46 -05002025static void
Jeff Laytone7ddee92008-11-14 13:44:38 -05002026cifs_put_tcp_session(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002028 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302030 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002031 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302032 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002035
Rob Landleyf1d0c992011-01-22 15:44:05 -06002036 put_net(cifs_net_ns(server));
2037
Jeff Laytone7ddee92008-11-14 13:44:38 -05002038 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302039 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002040
Jeff Laytonc74093b2011-01-11 07:24:23 -05002041 cancel_delayed_work_sync(&server->echo);
2042
Jeff Laytone7ddee92008-11-14 13:44:38 -05002043 spin_lock(&GlobalMid_Lock);
2044 server->tcpStatus = CifsExiting;
2045 spin_unlock(&GlobalMid_Lock);
2046
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002047 cifs_crypto_shash_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302048 cifs_fscache_release_client_cookie(server);
2049
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002050 kfree(server->session_key.response);
2051 server->session_key.response = NULL;
2052 server->session_key.len = 0;
2053
Jeff Laytone7ddee92008-11-14 13:44:38 -05002054 task = xchg(&server->tsk, NULL);
2055 if (task)
2056 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Jeff Layton63c038c2008-12-01 18:41:46 -05002059static struct TCP_Server_Info *
2060cifs_get_tcp_session(struct smb_vol *volume_info)
2061{
2062 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Laytona9ac49d2009-01-22 14:43:21 -05002063 struct sockaddr_storage addr;
Jeff Layton63c038c2008-12-01 18:41:46 -05002064 struct sockaddr_in *sin_server = (struct sockaddr_in *) &addr;
2065 struct sockaddr_in6 *sin_server6 = (struct sockaddr_in6 *) &addr;
2066 int rc;
2067
Jeff Laytona9ac49d2009-01-22 14:43:21 -05002068 memset(&addr, 0, sizeof(struct sockaddr_storage));
Jeff Layton63c038c2008-12-01 18:41:46 -05002069
Joe Perchesb6b38f72010-04-21 03:50:45 +00002070 cFYI(1, "UNC: %s ip: %s", volume_info->UNC, volume_info->UNCip);
Jeff Layton1e68b2b2009-06-11 10:27:30 -04002071
Jeff Layton63c038c2008-12-01 18:41:46 -05002072 if (volume_info->UNCip && volume_info->UNC) {
Jeff Layton50d97162010-07-06 20:43:01 -04002073 rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
2074 volume_info->UNCip,
David Howells67b76262010-07-22 18:33:01 +01002075 strlen(volume_info->UNCip),
Jeff Layton50d97162010-07-06 20:43:01 -04002076 volume_info->port);
Jeff Layton1e68b2b2009-06-11 10:27:30 -04002077 if (!rc) {
Jeff Layton63c038c2008-12-01 18:41:46 -05002078 /* we failed translating address */
2079 rc = -EINVAL;
2080 goto out_err;
2081 }
Jeff Layton63c038c2008-12-01 18:41:46 -05002082 } else if (volume_info->UNCip) {
2083 /* BB using ip addr as tcp_ses name to connect to the
2084 DFS root below */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002085 cERROR(1, "Connecting to DFS root not implemented yet");
Jeff Layton63c038c2008-12-01 18:41:46 -05002086 rc = -EINVAL;
2087 goto out_err;
2088 } else /* which tcp_sess DFS root would we conect to */ {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002089 cERROR(1, "CIFS mount error: No UNC path (e.g. -o "
2090 "unc=//192.168.1.100/public) specified");
Jeff Layton63c038c2008-12-01 18:41:46 -05002091 rc = -EINVAL;
2092 goto out_err;
2093 }
2094
2095 /* see if we already have a matching tcp_ses */
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002096 tcp_ses = cifs_find_tcp_session((struct sockaddr *)&addr, volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002097 if (tcp_ses)
2098 return tcp_ses;
2099
2100 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2101 if (!tcp_ses) {
2102 rc = -ENOMEM;
2103 goto out_err;
2104 }
2105
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002106 rc = cifs_crypto_shash_allocate(tcp_ses);
2107 if (rc) {
2108 cERROR(1, "could not setup hash structures rc %d", rc);
2109 goto out_err;
2110 }
2111
Jeff Layton23db65f2012-05-15 12:20:51 -04002112 tcp_ses->ops = volume_info->ops;
2113 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002114 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002115 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2116 if (IS_ERR(tcp_ses->hostname)) {
2117 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002118 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002119 }
2120
2121 tcp_ses->noblocksnd = volume_info->noblocksnd;
2122 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002123 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002124 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002125 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002126 init_waitqueue_head(&tcp_ses->response_q);
2127 init_waitqueue_head(&tcp_ses->request_q);
2128 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2129 mutex_init(&tcp_ses->srv_mutex);
2130 memcpy(tcp_ses->workstation_RFC1001_name,
2131 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2132 memcpy(tcp_ses->server_RFC1001_name,
2133 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002134 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002135 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002136 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002137 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002138 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2139 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002140 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Jeff Layton63c038c2008-12-01 18:41:46 -05002141
2142 /*
2143 * at this point we are the only ones with the pointer
2144 * to the struct since the kernel thread not created yet
2145 * no need to spinlock this init of tcpStatus or srv_count
2146 */
2147 tcp_ses->tcpStatus = CifsNew;
Ben Greear3eb9a882010-09-01 17:06:02 -07002148 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2149 sizeof(tcp_ses->srcaddr));
Jeff Layton63c038c2008-12-01 18:41:46 -05002150 ++tcp_ses->srv_count;
2151
Jeff Laytona9ac49d2009-01-22 14:43:21 -05002152 if (addr.ss_family == AF_INET6) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002153 cFYI(1, "attempting ipv6 connect");
Jeff Layton63c038c2008-12-01 18:41:46 -05002154 /* BB should we allow ipv6 on port 139? */
2155 /* other OS never observed in Wild doing 139 with v6 */
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002156 memcpy(&tcp_ses->dstaddr, sin_server6,
2157 sizeof(struct sockaddr_in6));
2158 } else
2159 memcpy(&tcp_ses->dstaddr, sin_server,
2160 sizeof(struct sockaddr_in));
2161
2162 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002163 if (rc < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002164 cERROR(1, "Error connecting to socket. Aborting operation");
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002165 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002166 }
2167
2168 /*
2169 * since we're in a cifs function already, we know that
2170 * this will succeed. No need for try_module_get().
2171 */
2172 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002173 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002174 tcp_ses, "cifsd");
2175 if (IS_ERR(tcp_ses->tsk)) {
2176 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesb6b38f72010-04-21 03:50:45 +00002177 cERROR(1, "error %d create cifsd thread", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002178 module_put(THIS_MODULE);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002179 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002180 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002181 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002182
2183 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302184 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002185 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302186 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002187
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302188 cifs_fscache_get_client_cookie(tcp_ses);
2189
Jeff Laytonc74093b2011-01-11 07:24:23 -05002190 /* queue echo request delayed work */
Jeff Laytonda472fc2012-03-23 14:40:53 -04002191 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002192
Jeff Layton63c038c2008-12-01 18:41:46 -05002193 return tcp_ses;
2194
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002195out_err_crypto_release:
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002196 cifs_crypto_shash_release(tcp_ses);
2197
Rob Landleyf1d0c992011-01-22 15:44:05 -06002198 put_net(cifs_net_ns(tcp_ses));
2199
Jeff Layton63c038c2008-12-01 18:41:46 -05002200out_err:
2201 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002202 if (!IS_ERR(tcp_ses->hostname))
2203 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002204 if (tcp_ses->ssocket)
2205 sock_release(tcp_ses->ssocket);
2206 kfree(tcp_ses);
2207 }
2208 return ERR_PTR(rc);
2209}
2210
Steve French96daf2b2011-05-27 04:34:02 +00002211static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002212{
2213 switch (ses->server->secType) {
2214 case Kerberos:
2215 if (vol->cred_uid != ses->cred_uid)
2216 return 0;
2217 break;
2218 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002219 /* NULL username means anonymous session */
2220 if (ses->user_name == NULL) {
2221 if (!vol->nullauth)
2222 return 0;
2223 break;
2224 }
2225
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002226 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002227 if (strncmp(ses->user_name,
2228 vol->username ? vol->username : "",
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002229 MAX_USERNAME_SIZE))
2230 return 0;
2231 if (strlen(vol->username) != 0 &&
2232 ses->password != NULL &&
2233 strncmp(ses->password,
2234 vol->password ? vol->password : "",
2235 MAX_PASSWORD_SIZE))
2236 return 0;
2237 }
2238 return 1;
2239}
2240
Steve French96daf2b2011-05-27 04:34:02 +00002241static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002242cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Steve French96daf2b2011-05-27 04:34:02 +00002244 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302246 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002247 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002248 if (!match_session(ses, vol))
2249 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002250 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302251 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002252 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302254 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return NULL;
2256}
2257
Jeff Layton14fbf502008-11-14 13:53:46 -05002258static void
Steve French96daf2b2011-05-27 04:34:02 +00002259cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002260{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04002261 unsigned int xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002262 struct TCP_Server_Info *server = ses->server;
2263
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04002264 cFYI(1, "%s: ses_count=%d", __func__, ses->ses_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302265 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002266 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302267 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002268 return;
2269 }
2270
2271 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302272 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002273
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04002274 if (ses->status == CifsGood && server->ops->logoff) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002275 xid = get_xid();
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04002276 server->ops->logoff(xid, ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002277 _free_xid(xid);
Jeff Layton14fbf502008-11-14 13:53:46 -05002278 }
2279 sesInfoFree(ses);
2280 cifs_put_tcp_session(server);
2281}
2282
Jeff Layton8a8798a2012-01-17 16:09:15 -05002283#ifdef CONFIG_KEYS
2284
2285/* strlen("cifs:a:") + INET6_ADDRSTRLEN + 1 */
2286#define CIFSCREDS_DESC_SIZE (7 + INET6_ADDRSTRLEN + 1)
2287
2288/* Populate username and pw fields from keyring if possible */
2289static int
2290cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2291{
2292 int rc = 0;
2293 char *desc, *delim, *payload;
2294 ssize_t len;
2295 struct key *key;
2296 struct TCP_Server_Info *server = ses->server;
2297 struct sockaddr_in *sa;
2298 struct sockaddr_in6 *sa6;
2299 struct user_key_payload *upayload;
2300
2301 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2302 if (!desc)
2303 return -ENOMEM;
2304
2305 /* try to find an address key first */
2306 switch (server->dstaddr.ss_family) {
2307 case AF_INET:
2308 sa = (struct sockaddr_in *)&server->dstaddr;
2309 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2310 break;
2311 case AF_INET6:
2312 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2313 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2314 break;
2315 default:
2316 cFYI(1, "Bad ss_family (%hu)", server->dstaddr.ss_family);
2317 rc = -EINVAL;
2318 goto out_err;
2319 }
2320
2321 cFYI(1, "%s: desc=%s", __func__, desc);
2322 key = request_key(&key_type_logon, desc, "");
2323 if (IS_ERR(key)) {
2324 if (!ses->domainName) {
2325 cFYI(1, "domainName is NULL");
2326 rc = PTR_ERR(key);
2327 goto out_err;
2328 }
2329
2330 /* didn't work, try to find a domain key */
2331 sprintf(desc, "cifs:d:%s", ses->domainName);
2332 cFYI(1, "%s: desc=%s", __func__, desc);
2333 key = request_key(&key_type_logon, desc, "");
2334 if (IS_ERR(key)) {
2335 rc = PTR_ERR(key);
2336 goto out_err;
2337 }
2338 }
2339
2340 down_read(&key->sem);
2341 upayload = key->payload.data;
2342 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002343 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002344 goto out_key_put;
2345 }
2346
2347 /* find first : in payload */
2348 payload = (char *)upayload->data;
2349 delim = strnchr(payload, upayload->datalen, ':');
2350 cFYI(1, "payload=%s", payload);
2351 if (!delim) {
2352 cFYI(1, "Unable to find ':' in payload (datalen=%d)",
2353 upayload->datalen);
2354 rc = -EINVAL;
2355 goto out_key_put;
2356 }
2357
2358 len = delim - payload;
2359 if (len > MAX_USERNAME_SIZE || len <= 0) {
Randy Dunlap000f9bb2012-01-30 19:50:01 -08002360 cFYI(1, "Bad value from username search (len=%zd)", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002361 rc = -EINVAL;
2362 goto out_key_put;
2363 }
2364
2365 vol->username = kstrndup(payload, len, GFP_KERNEL);
2366 if (!vol->username) {
Randy Dunlap000f9bb2012-01-30 19:50:01 -08002367 cFYI(1, "Unable to allocate %zd bytes for username", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002368 rc = -ENOMEM;
2369 goto out_key_put;
2370 }
2371 cFYI(1, "%s: username=%s", __func__, vol->username);
2372
2373 len = key->datalen - (len + 1);
2374 if (len > MAX_PASSWORD_SIZE || len <= 0) {
Randy Dunlap000f9bb2012-01-30 19:50:01 -08002375 cFYI(1, "Bad len for password search (len=%zd)", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002376 rc = -EINVAL;
2377 kfree(vol->username);
2378 vol->username = NULL;
2379 goto out_key_put;
2380 }
2381
2382 ++delim;
2383 vol->password = kstrndup(delim, len, GFP_KERNEL);
2384 if (!vol->password) {
Randy Dunlap000f9bb2012-01-30 19:50:01 -08002385 cFYI(1, "Unable to allocate %zd bytes for password", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002386 rc = -ENOMEM;
2387 kfree(vol->username);
2388 vol->username = NULL;
2389 goto out_key_put;
2390 }
2391
2392out_key_put:
2393 up_read(&key->sem);
2394 key_put(key);
2395out_err:
2396 kfree(desc);
2397 cFYI(1, "%s: returning %d", __func__, rc);
2398 return rc;
2399}
2400#else /* ! CONFIG_KEYS */
2401static inline int
2402cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2403 struct cifs_ses *ses __attribute__((unused)))
2404{
2405 return -ENOSYS;
2406}
2407#endif /* CONFIG_KEYS */
2408
Steve Frenchd9b94202011-04-12 01:24:57 +00002409static bool warned_on_ntlm; /* globals init to false automatically */
2410
Steve French96daf2b2011-05-27 04:34:02 +00002411static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002412cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2413{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002414 int rc = -ENOMEM;
2415 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002416 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002417 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2418 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002419
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002420 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002421
Jeff Layton4ff67b72010-07-06 20:43:02 -04002422 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002423 if (ses) {
2424 cFYI(1, "Existing smb sess found (status=%d)", ses->status);
2425
Jeff Layton36988c72010-04-24 07:57:43 -04002426 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002427 rc = cifs_negotiate_protocol(xid, ses);
2428 if (rc) {
2429 mutex_unlock(&ses->session_mutex);
2430 /* problem -- put our ses reference */
2431 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002432 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002433 return ERR_PTR(rc);
2434 }
Jeff Layton36988c72010-04-24 07:57:43 -04002435 if (ses->need_reconnect) {
2436 cFYI(1, "Session needs reconnect");
2437 rc = cifs_setup_session(xid, ses,
2438 volume_info->local_nls);
2439 if (rc) {
2440 mutex_unlock(&ses->session_mutex);
2441 /* problem -- put our reference */
2442 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002443 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002444 return ERR_PTR(rc);
2445 }
2446 }
2447 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002448
2449 /* existing SMB ses has a server reference already */
2450 cifs_put_tcp_session(server);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002451 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002452 return ses;
2453 }
2454
2455 cFYI(1, "Existing smb sess not found");
2456 ses = sesInfoAlloc();
2457 if (ses == NULL)
2458 goto get_ses_fail;
2459
2460 /* new SMB session uses our server ref */
2461 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002462 if (server->dstaddr.ss_family == AF_INET6)
2463 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002464 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002465 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002466
Steve French8727c8a2011-02-25 01:11:56 -06002467 if (volume_info->username) {
2468 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2469 if (!ses->user_name)
2470 goto get_ses_fail;
2471 }
Jeff Layton36988c72010-04-24 07:57:43 -04002472
2473 /* volume_info->password freed at unmount */
2474 if (volume_info->password) {
2475 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2476 if (!ses->password)
2477 goto get_ses_fail;
2478 }
2479 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002480 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2481 if (!ses->domainName)
2482 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002483 }
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002484 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002485 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002486
2487 /* ntlmv2 is much stronger than ntlm security, and has been broadly
2488 supported for many years, time to update default security mechanism */
2489 if ((volume_info->secFlg == 0) && warned_on_ntlm == false) {
2490 warned_on_ntlm = true;
2491 cERROR(1, "default security mechanism requested. The default "
2492 "security mechanism will be upgraded from ntlm to "
Steve French225de112012-01-03 23:08:24 -06002493 "ntlmv2 in kernel release 3.3");
Steve Frenchd9b94202011-04-12 01:24:57 +00002494 }
Jeff Layton36988c72010-04-24 07:57:43 -04002495 ses->overrideSecFlg = volume_info->secFlg;
2496
2497 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002498 rc = cifs_negotiate_protocol(xid, ses);
2499 if (!rc)
2500 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002501 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002502 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002503 goto get_ses_fail;
2504
2505 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302506 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002507 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302508 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002509
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002510 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002511 return ses;
2512
2513get_ses_fail:
2514 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002515 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002516 return ERR_PTR(rc);
2517}
2518
Steve French96daf2b2011-05-27 04:34:02 +00002519static int match_tcon(struct cifs_tcon *tcon, const char *unc)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002520{
2521 if (tcon->tidStatus == CifsExiting)
2522 return 0;
2523 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
2524 return 0;
2525 return 1;
2526}
2527
Steve French96daf2b2011-05-27 04:34:02 +00002528static struct cifs_tcon *
2529cifs_find_tcon(struct cifs_ses *ses, const char *unc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002532 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302534 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002535 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002536 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002537 if (!match_tcon(tcon, unc))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002538 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002539 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302540 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return tcon;
2542 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302543 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return NULL;
2545}
2546
Jeff Laytonf1987b42008-11-15 11:12:47 -05002547static void
Steve French96daf2b2011-05-27 04:34:02 +00002548cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002549{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002550 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002551 struct cifs_ses *ses = tcon->ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002552
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04002553 cFYI(1, "%s: tc_count=%d", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302554 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002555 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302556 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002557 return;
2558 }
2559
2560 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302561 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002562
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002563 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002564 if (ses->server->ops->tree_disconnect)
2565 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002566 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002567
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302568 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002569 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002570 cifs_put_smb_ses(ses);
2571}
2572
Steve French96daf2b2011-05-27 04:34:02 +00002573static struct cifs_tcon *
2574cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04002575{
2576 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00002577 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04002578
2579 tcon = cifs_find_tcon(ses, volume_info->UNC);
2580 if (tcon) {
2581 cFYI(1, "Found match on UNC path");
2582 /* existing tcon already has a reference */
2583 cifs_put_smb_ses(ses);
2584 if (tcon->seal != volume_info->seal)
2585 cERROR(1, "transport encryption setting "
2586 "conflicts with existing tid");
2587 return tcon;
2588 }
2589
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002590 if (!ses->server->ops->tree_connect) {
2591 rc = -ENOSYS;
2592 goto out_fail;
2593 }
2594
Jeff Laytond00c28d2010-04-24 07:57:44 -04002595 tcon = tconInfoAlloc();
2596 if (tcon == NULL) {
2597 rc = -ENOMEM;
2598 goto out_fail;
2599 }
2600
2601 tcon->ses = ses;
2602 if (volume_info->password) {
2603 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
2604 if (!tcon->password) {
2605 rc = -ENOMEM;
2606 goto out_fail;
2607 }
2608 }
2609
2610 if (strchr(volume_info->UNC + 3, '\\') == NULL
2611 && strchr(volume_info->UNC + 3, '/') == NULL) {
2612 cERROR(1, "Missing share name");
2613 rc = -ENODEV;
2614 goto out_fail;
2615 }
2616
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002617 /*
2618 * BB Do we need to wrap session_mutex around this TCon call and Unix
2619 * SetFS as we do on SessSetup and reconnect?
2620 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002621 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002622 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
2623 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002624 free_xid(xid);
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002625 cFYI(1, "Tcon rc = %d", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002626 if (rc)
2627 goto out_fail;
2628
2629 if (volume_info->nodfs) {
2630 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
2631 cFYI(1, "DFS disabled (%d)", tcon->Flags);
2632 }
2633 tcon->seal = volume_info->seal;
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002634 /*
2635 * We can have only one retry value for a connection to a share so for
2636 * resources mounted more than once to the same server share the last
2637 * value passed in for the retry flag is used.
2638 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04002639 tcon->retry = volume_info->retry;
2640 tcon->nocase = volume_info->nocase;
2641 tcon->local_lease = volume_info->local_lease;
2642
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302643 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002644 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302645 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04002646
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302647 cifs_fscache_get_super_cookie(tcon);
2648
Jeff Laytond00c28d2010-04-24 07:57:44 -04002649 return tcon;
2650
2651out_fail:
2652 tconInfoFree(tcon);
2653 return ERR_PTR(rc);
2654}
2655
Jeff Layton9d002df2010-10-06 19:51:11 -04002656void
2657cifs_put_tlink(struct tcon_link *tlink)
2658{
2659 if (!tlink || IS_ERR(tlink))
2660 return;
2661
2662 if (!atomic_dec_and_test(&tlink->tl_count) ||
2663 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2664 tlink->tl_time = jiffies;
2665 return;
2666 }
2667
2668 if (!IS_ERR(tlink_tcon(tlink)))
2669 cifs_put_tcon(tlink_tcon(tlink));
2670 kfree(tlink);
2671 return;
2672}
Jeff Laytond00c28d2010-04-24 07:57:44 -04002673
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002674static inline struct tcon_link *
Pavel Shilovskycd518752011-06-09 12:58:53 +04002675cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2676{
2677 return cifs_sb->master_tlink;
2678}
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002679
2680static int
2681compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2682{
2683 struct cifs_sb_info *old = CIFS_SB(sb);
2684 struct cifs_sb_info *new = mnt_data->cifs_sb;
2685
2686 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2687 return 0;
2688
2689 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2690 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2691 return 0;
2692
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002693 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002694 * We want to share sb only if we don't specify an r/wsize or
2695 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002696 */
2697 if (new->wsize && new->wsize < old->wsize)
2698 return 0;
2699
Jeff Layton5eba8ab2011-10-19 15:30:26 -04002700 if (new->rsize && new->rsize < old->rsize)
2701 return 0;
2702
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002703 if (old->mnt_uid != new->mnt_uid || old->mnt_gid != new->mnt_gid)
2704 return 0;
2705
2706 if (old->mnt_file_mode != new->mnt_file_mode ||
2707 old->mnt_dir_mode != new->mnt_dir_mode)
2708 return 0;
2709
2710 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2711 return 0;
2712
2713 if (old->actimeo != new->actimeo)
2714 return 0;
2715
2716 return 1;
2717}
2718
2719int
2720cifs_match_super(struct super_block *sb, void *data)
2721{
2722 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2723 struct smb_vol *volume_info;
2724 struct cifs_sb_info *cifs_sb;
2725 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00002726 struct cifs_ses *ses;
2727 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002728 struct tcon_link *tlink;
2729 struct sockaddr_storage addr;
2730 int rc = 0;
2731
2732 memset(&addr, 0, sizeof(struct sockaddr_storage));
2733
2734 spin_lock(&cifs_tcp_ses_lock);
2735 cifs_sb = CIFS_SB(sb);
2736 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2737 if (IS_ERR(tlink)) {
2738 spin_unlock(&cifs_tcp_ses_lock);
2739 return rc;
2740 }
2741 tcon = tlink_tcon(tlink);
2742 ses = tcon->ses;
2743 tcp_srv = ses->server;
2744
2745 volume_info = mnt_data->vol;
2746
2747 if (!volume_info->UNCip || !volume_info->UNC)
2748 goto out;
2749
2750 rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
2751 volume_info->UNCip,
2752 strlen(volume_info->UNCip),
2753 volume_info->port);
2754 if (!rc)
2755 goto out;
2756
2757 if (!match_server(tcp_srv, (struct sockaddr *)&addr, volume_info) ||
2758 !match_session(ses, volume_info) ||
2759 !match_tcon(tcon, volume_info->UNC)) {
2760 rc = 0;
2761 goto out;
2762 }
2763
2764 rc = compare_mount_options(sb, mnt_data);
2765out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002766 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04002767 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04002768 return rc;
2769}
2770
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002772get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002773 const struct nls_table *nls_codepage, unsigned int *num_referrals,
2774 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 char *temp_unc;
2777 int rc = 0;
2778
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002779 if (!ses->server->ops->tree_connect || !ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002780 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002782 *num_referrals = 0;
2783 *referrals = NULL;
2784
2785 if (ses->ipc_tid == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 temp_unc = kmalloc(2 /* for slashes */ +
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002787 strnlen(ses->serverName, SERVER_NAME_LEN_WITH_NULL * 2)
2788 + 1 + 4 /* slash IPC$ */ + 2, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (temp_unc == NULL)
2790 return -ENOMEM;
2791 temp_unc[0] = '\\';
2792 temp_unc[1] = '\\';
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002793 strcpy(temp_unc + 2, ses->serverName);
2794 strcpy(temp_unc + 2 + strlen(ses->serverName), "\\IPC$");
2795 rc = ses->server->ops->tree_connect(xid, ses, temp_unc, NULL,
2796 nls_codepage);
2797 cFYI(1, "Tcon rc = %d ipc_tid = %d", rc, ses->ipc_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 kfree(temp_unc);
2799 }
2800 if (rc == 0)
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002801 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
2802 referrals, num_referrals,
2803 nls_codepage, remap);
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002804 /*
2805 * BB - map targetUNCs to dfs_info3 structures, here or in
Pavel Shilovskyb669f332012-05-27 20:21:53 +04002806 * ses->server->ops->get_dfs_refer.
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 return rc;
2810}
2811
Jeff Layton09e50d52008-07-23 10:11:19 -04002812#ifdef CONFIG_DEBUG_LOCK_ALLOC
2813static struct lock_class_key cifs_key[2];
2814static struct lock_class_key cifs_slock_key[2];
2815
2816static inline void
2817cifs_reclassify_socket4(struct socket *sock)
2818{
2819 struct sock *sk = sock->sk;
2820 BUG_ON(sock_owned_by_user(sk));
2821 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
2822 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
2823}
2824
2825static inline void
2826cifs_reclassify_socket6(struct socket *sock)
2827{
2828 struct sock *sk = sock->sk;
2829 BUG_ON(sock_owned_by_user(sk));
2830 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
2831 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
2832}
2833#else
2834static inline void
2835cifs_reclassify_socket4(struct socket *sock)
2836{
2837}
2838
2839static inline void
2840cifs_reclassify_socket6(struct socket *sock)
2841{
2842}
2843#endif
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00002846static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
Steve French50c2f752007-07-13 00:33:32 +00002848 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Steve French50c2f752007-07-13 00:33:32 +00002850 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 /* mask a nibble at a time and encode */
2852 target[j] = 'A' + (0x0F & (source[i] >> 4));
2853 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00002854 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856
2857}
2858
Ben Greear3eb9a882010-09-01 17:06:02 -07002859static int
2860bind_socket(struct TCP_Server_Info *server)
2861{
2862 int rc = 0;
2863 if (server->srcaddr.ss_family != AF_UNSPEC) {
2864 /* Bind to the specified local IP address */
2865 struct socket *socket = server->ssocket;
2866 rc = socket->ops->bind(socket,
2867 (struct sockaddr *) &server->srcaddr,
2868 sizeof(server->srcaddr));
2869 if (rc < 0) {
2870 struct sockaddr_in *saddr4;
2871 struct sockaddr_in6 *saddr6;
2872 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2873 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2874 if (saddr6->sin6_family == AF_INET6)
2875 cERROR(1, "cifs: "
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04002876 "Failed to bind to: %pI6c, error: %d",
Ben Greear3eb9a882010-09-01 17:06:02 -07002877 &saddr6->sin6_addr, rc);
2878 else
2879 cERROR(1, "cifs: "
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04002880 "Failed to bind to: %pI4, error: %d",
Ben Greear3eb9a882010-09-01 17:06:02 -07002881 &saddr4->sin_addr.s_addr, rc);
2882 }
2883 }
2884 return rc;
2885}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002888ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
2890 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002891 /*
2892 * some servers require RFC1001 sessinit before sending
2893 * negprot - BB check reconnection in case where second
2894 * sessinit is sent but no second negprot
2895 */
2896 struct rfc1002_session_packet *ses_init_buf;
2897 struct smb_hdr *smb_buf;
2898 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2899 GFP_KERNEL);
2900 if (ses_init_buf) {
2901 ses_init_buf->trailer.session_req.called_len = 32;
2902
2903 if (server->server_RFC1001_name &&
2904 server->server_RFC1001_name[0] != 0)
2905 rfc1002mangle(ses_init_buf->trailer.
2906 session_req.called_name,
2907 server->server_RFC1001_name,
2908 RFC1001_NAME_LEN_WITH_NULL);
2909 else
2910 rfc1002mangle(ses_init_buf->trailer.
2911 session_req.called_name,
2912 DEFAULT_CIFS_CALLED_NAME,
2913 RFC1001_NAME_LEN_WITH_NULL);
2914
2915 ses_init_buf->trailer.session_req.calling_len = 32;
2916
2917 /*
2918 * calling name ends in null (byte 16) from old smb
2919 * convention.
2920 */
2921 if (server->workstation_RFC1001_name &&
2922 server->workstation_RFC1001_name[0] != 0)
2923 rfc1002mangle(ses_init_buf->trailer.
2924 session_req.calling_name,
2925 server->workstation_RFC1001_name,
2926 RFC1001_NAME_LEN_WITH_NULL);
2927 else
2928 rfc1002mangle(ses_init_buf->trailer.
2929 session_req.calling_name,
2930 "LINUX_CIFS_CLNT",
2931 RFC1001_NAME_LEN_WITH_NULL);
2932
2933 ses_init_buf->trailer.session_req.scope1 = 0;
2934 ses_init_buf->trailer.session_req.scope2 = 0;
2935 smb_buf = (struct smb_hdr *)ses_init_buf;
2936
2937 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00002938 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002939 rc = smb_send(server, smb_buf, 0x44);
2940 kfree(ses_init_buf);
2941 /*
2942 * RFC1001 layer in at least one server
2943 * requires very short break before negprot
2944 * presumably because not expecting negprot
2945 * to follow so fast. This is a simple
2946 * solution that works without
2947 * complicating the code and causes no
2948 * significant slowing down on mount
2949 * for everyone else
2950 */
2951 usleep_range(1000, 2000);
2952 }
2953 /*
2954 * else the negprot may still work without this
2955 * even though malloc failed
2956 */
2957
2958 return rc;
2959}
2960
2961static int
2962generic_ip_connect(struct TCP_Server_Info *server)
2963{
2964 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00002965 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002966 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002967 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002968 struct sockaddr *saddr;
2969
2970 saddr = (struct sockaddr *) &server->dstaddr;
2971
2972 if (server->dstaddr.ss_family == AF_INET6) {
2973 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
2974 slen = sizeof(struct sockaddr_in6);
2975 sfamily = AF_INET6;
2976 } else {
2977 sport = ((struct sockaddr_in *) saddr)->sin_port;
2978 slen = sizeof(struct sockaddr_in);
2979 sfamily = AF_INET;
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002982 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06002983 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2984 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 if (rc < 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00002986 cERROR(1, "Error %d creating socket", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002987 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002990
2991 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesb6b38f72010-04-21 03:50:45 +00002992 cFYI(1, "Socket created");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05002993 server->ssocket = socket;
2994 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002995 if (sfamily == AF_INET6)
2996 cifs_reclassify_socket6(socket);
2997 else
2998 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
3000
Ben Greear3eb9a882010-09-01 17:06:02 -07003001 rc = bind_socket(server);
3002 if (rc < 0)
3003 return rc;
3004
Jeff Laytond5c56052008-12-01 18:42:33 -05003005 /*
3006 * Eventually check for other socket options to change from
3007 * the default. sock_setsockopt not used because it expects
3008 * user space buffer
3009 */
3010 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003011 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003012
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003013 /* make the bufsizes depend on wsize/rsize and max requests */
3014 if (server->noautotune) {
3015 if (socket->sk->sk_sndbuf < (200 * 1024))
3016 socket->sk->sk_sndbuf = 200 * 1024;
3017 if (socket->sk->sk_rcvbuf < (140 * 1024))
3018 socket->sk->sk_rcvbuf = 140 * 1024;
3019 }
3020
Steve French6a5fa2362010-01-01 01:28:43 +00003021 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003022 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003023 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3024 (char *)&val, sizeof(val));
3025 if (rc)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003026 cFYI(1, "set TCP_NODELAY socket option error %d", rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003027 }
3028
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003029 cFYI(1, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
3030 socket->sk->sk_sndbuf,
3031 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3032
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003033 rc = socket->ops->connect(socket, saddr, slen, 0);
3034 if (rc < 0) {
3035 cFYI(1, "Error %d connecting to server", rc);
3036 sock_release(socket);
3037 server->ssocket = NULL;
3038 return rc;
3039 }
3040
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003041 if (sport == htons(RFC1001_PORT))
3042 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return rc;
3045}
3046
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003047static int
3048ip_connect(struct TCP_Server_Info *server)
3049{
Steve French6da97912011-03-13 18:55:55 +00003050 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003051 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3052 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3053
3054 if (server->dstaddr.ss_family == AF_INET6)
3055 sport = &addr6->sin6_port;
3056 else
3057 sport = &addr->sin_port;
3058
3059 if (*sport == 0) {
3060 int rc;
3061
3062 /* try with 445 port at first */
3063 *sport = htons(CIFS_PORT);
3064
3065 rc = generic_ip_connect(server);
3066 if (rc >= 0)
3067 return rc;
3068
3069 /* if it failed, try with 139 port */
3070 *sport = htons(RFC1001_PORT);
3071 }
3072
3073 return generic_ip_connect(server);
3074}
3075
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003076void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003077 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003078{
3079 /* if we are reconnecting then should we check to see if
3080 * any requested capabilities changed locally e.g. via
3081 * remount but we can not do much about it here
3082 * if they have (even if we could detect it by the following)
3083 * Perhaps we could add a backpointer to array of sb from tcon
3084 * or if we change to make all sb to same share the same
3085 * sb as NFS - then we only have one backpointer to sb.
3086 * What if we wanted to mount the server share twice once with
3087 * and once without posixacls or posix paths? */
3088 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003089
Steve Frenchc18c8422007-07-18 23:21:09 +00003090 if (vol_info && vol_info->no_linux_ext) {
3091 tcon->fsUnixInfo.Capability = 0;
3092 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003093 cFYI(1, "Linux protocol extensions disabled");
Steve Frenchc18c8422007-07-18 23:21:09 +00003094 return;
3095 } else if (vol_info)
3096 tcon->unix_ext = 1; /* Unix Extensions supported */
3097
3098 if (tcon->unix_ext == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003099 cFYI(1, "Unix extensions disabled so not set on reconnect");
Steve Frenchc18c8422007-07-18 23:21:09 +00003100 return;
3101 }
Steve French50c2f752007-07-13 00:33:32 +00003102
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003103 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003104 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French6848b732011-05-26 18:38:54 +00003105 cFYI(1, "unix caps which server supports %lld", cap);
Steve French8af18972007-02-14 04:42:51 +00003106 /* check for reconnect case in which we do not
3107 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003108 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003109 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003110 originally at mount time */
3111 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3112 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003113 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3114 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003115 cERROR(1, "POSIXPATH support change");
Steve French8af18972007-02-14 04:42:51 +00003116 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003117 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003118 cERROR(1, "possible reconnect error");
3119 cERROR(1, "server disabled POSIX path support");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003120 }
Steve French8af18972007-02-14 04:42:51 +00003121 }
Steve French50c2f752007-07-13 00:33:32 +00003122
Steve French6848b732011-05-26 18:38:54 +00003123 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3124 cERROR(1, "per-share encryption not supported yet");
3125
Steve French8af18972007-02-14 04:42:51 +00003126 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003127 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003128 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003129 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003130 cFYI(1, "negotiated posix acl support");
Al Viro2c6292a2011-06-17 09:05:48 -04003131 if (cifs_sb)
3132 cifs_sb->mnt_cifs_flags |=
3133 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003134 }
3135
Steve French75865f8c2007-06-24 18:30:48 +00003136 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003137 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003138 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003139 cFYI(1, "negotiate posix pathnames");
Al Viro2c6292a2011-06-17 09:05:48 -04003140 if (cifs_sb)
3141 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003142 CIFS_MOUNT_POSIX_PATHS;
3143 }
Steve French50c2f752007-07-13 00:33:32 +00003144
Joe Perchesb6b38f72010-04-21 03:50:45 +00003145 cFYI(1, "Negotiate caps 0x%x", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003146#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003147 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003148 cFYI(1, "FCNTL cap");
Steve French75865f8c2007-06-24 18:30:48 +00003149 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003150 cFYI(1, "EXTATTR cap");
Steve French75865f8c2007-06-24 18:30:48 +00003151 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003152 cFYI(1, "POSIX path cap");
Steve French75865f8c2007-06-24 18:30:48 +00003153 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003154 cFYI(1, "XATTR cap");
Steve French75865f8c2007-06-24 18:30:48 +00003155 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003156 cFYI(1, "POSIX ACL cap");
Steve French75865f8c2007-06-24 18:30:48 +00003157 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003158 cFYI(1, "very large read cap");
Steve French75865f8c2007-06-24 18:30:48 +00003159 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesb6b38f72010-04-21 03:50:45 +00003160 cFYI(1, "very large write cap");
Steve French6848b732011-05-26 18:38:54 +00003161 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
3162 cFYI(1, "transport encryption cap");
3163 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
3164 cFYI(1, "mandatory transport encryption cap");
Steve French8af18972007-02-14 04:42:51 +00003165#endif /* CIFS_DEBUG2 */
3166 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003167 if (vol_info == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003168 cFYI(1, "resetting capabilities failed");
Steve French442aa312007-09-24 20:25:46 +00003169 } else
Joe Perchesb6b38f72010-04-21 03:50:45 +00003170 cERROR(1, "Negotiating Unix capabilities "
Jeff Laytonac3aa2f2012-07-23 13:14:28 -04003171 "with the server failed. Consider "
3172 "mounting with the Unix Extensions "
3173 "disabled if problems are found "
Steve French5a44b312007-09-20 15:16:24 +00003174 "by specifying the nounix mount "
Joe Perchesb6b38f72010-04-21 03:50:45 +00003175 "option.");
Steve French5a44b312007-09-20 15:16:24 +00003176
Steve French8af18972007-02-14 04:42:51 +00003177 }
3178 }
3179}
3180
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003181void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
3182 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003183{
Jeff Layton2de970f2010-10-06 19:51:12 -04003184 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3185
Al Viro2ced6f62011-06-17 09:20:04 -04003186 spin_lock_init(&cifs_sb->tlink_tree_lock);
3187 cifs_sb->tlink_tree = RB_ROOT;
3188
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003189 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003190 * Temporarily set r/wsize for matching superblock. If we end up using
3191 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003192 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003193 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003194 cifs_sb->wsize = pvolume_info->wsize;
3195
Steve French3b795212008-11-13 19:45:32 +00003196 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3197 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3198 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3199 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Al Viro5206efd2011-07-26 03:22:14 -04003200 cFYI(1, "file mode: 0x%hx dir mode: 0x%hx",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003201 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003202
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303203 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003204 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303205
Steve French3b795212008-11-13 19:45:32 +00003206 if (pvolume_info->noperm)
3207 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3208 if (pvolume_info->setuids)
3209 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
3210 if (pvolume_info->server_ino)
3211 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3212 if (pvolume_info->remap)
3213 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3214 if (pvolume_info->no_xattr)
3215 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3216 if (pvolume_info->sfu_emul)
3217 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3218 if (pvolume_info->nobrl)
3219 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve Frenchbe652442009-02-23 15:21:59 +00003220 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003221 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003222 if (pvolume_info->mand_lock)
3223 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003224 if (pvolume_info->rwpidforward)
3225 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003226 if (pvolume_info->cifs_acl)
3227 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003228 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003229 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003230 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3231 }
3232 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003233 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003234 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3235 }
Steve French3b795212008-11-13 19:45:32 +00003236 if (pvolume_info->override_uid)
3237 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3238 if (pvolume_info->override_gid)
3239 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3240 if (pvolume_info->dynperm)
3241 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303242 if (pvolume_info->fsc)
3243 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003244 if (pvolume_info->multiuser)
3245 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3246 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003247 if (pvolume_info->strict_io)
3248 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003249 if (pvolume_info->direct_io) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003250 cFYI(1, "mounting share using direct i/o");
Steve French3b795212008-11-13 19:45:32 +00003251 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3252 }
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003253 if (pvolume_info->mfsymlinks) {
3254 if (pvolume_info->sfu_emul) {
3255 cERROR(1, "mount option mfsymlinks ignored if sfu "
3256 "mount option is used");
3257 } else {
3258 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
3259 }
3260 }
Steve French3b795212008-11-13 19:45:32 +00003261
3262 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesb6b38f72010-04-21 03:50:45 +00003263 cERROR(1, "mount option dynperm ignored if cifsacl "
3264 "mount option supported");
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003265}
3266
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003267/*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003268 * When the server supports very large reads and writes via POSIX extensions,
3269 * we can allow up to 2^24-1, minus the size of a READ/WRITE_AND_X header, not
3270 * including the RFC1001 length.
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003271 *
3272 * Note that this might make for "interesting" allocation problems during
Jeff Layton1190f6a2011-06-22 17:33:57 -04003273 * writeback however as we have to allocate an array of pointers for the
3274 * pages. A 16M write means ~32kb page array with PAGE_CACHE_SIZE == 4096.
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003275 *
3276 * For reads, there is a similar problem as we need to allocate an array
3277 * of kvecs to handle the receive, though that should only need to be done
3278 * once.
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003279 */
Jeff Layton1190f6a2011-06-22 17:33:57 -04003280#define CIFS_MAX_WSIZE ((1<<24) - 1 - sizeof(WRITE_REQ) + 4)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003281#define CIFS_MAX_RSIZE ((1<<24) - sizeof(READ_RSP) + 4)
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003282
3283/*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003284 * When the server doesn't allow large posix writes, only allow a rsize/wsize
3285 * of 2^17-1 minus the size of the call header. That allows for a read or
3286 * write up to the maximum size described by RFC1002.
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003287 */
Pavel Shilovsky94443f42011-10-07 18:57:45 +04003288#define CIFS_MAX_RFC1002_WSIZE ((1<<17) - 1 - sizeof(WRITE_REQ) + 4)
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003289#define CIFS_MAX_RFC1002_RSIZE ((1<<17) - 1 - sizeof(READ_RSP) + 4)
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003290
3291/*
3292 * The default wsize is 1M. find_get_pages seems to return a maximum of 256
3293 * pages in a single call. With PAGE_CACHE_SIZE == 4k, this means we can fill
3294 * a single wsize request with a single call.
3295 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003296#define CIFS_DEFAULT_IOSIZE (1024 * 1024)
3297
3298/*
Jeff Laytonce91acb2012-01-17 16:08:51 -05003299 * Windows only supports a max of 60kb reads and 65535 byte writes. Default to
3300 * those values when posix extensions aren't in force. In actuality here, we
3301 * use 65536 to allow for a write that is a multiple of 4k. Most servers seem
3302 * to be ok with the extra byte even though Windows doesn't send writes that
3303 * are that large.
3304 *
3305 * Citation:
3306 *
3307 * http://blogs.msdn.com/b/openspecification/archive/2009/04/10/smb-maximum-transmit-buffer-size-and-performance-tuning.aspx
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003308 */
3309#define CIFS_DEFAULT_NON_POSIX_RSIZE (60 * 1024)
Jeff Laytonce91acb2012-01-17 16:08:51 -05003310#define CIFS_DEFAULT_NON_POSIX_WSIZE (65536)
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003311
Jeff Layton3ae629d2012-07-11 09:09:35 -04003312/*
3313 * On hosts with high memory, we can't currently support wsize/rsize that are
3314 * larger than we can kmap at once. Cap the rsize/wsize at
3315 * LAST_PKMAP * PAGE_SIZE. We'll never be able to fill a read or write request
3316 * larger than that anyway.
3317 */
3318#ifdef CONFIG_HIGHMEM
3319#define CIFS_KMAP_SIZE_LIMIT (LAST_PKMAP * PAGE_CACHE_SIZE)
3320#else /* CONFIG_HIGHMEM */
3321#define CIFS_KMAP_SIZE_LIMIT (1<<24)
3322#endif /* CONFIG_HIGHMEM */
3323
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003324static unsigned int
Steve French96daf2b2011-05-27 04:34:02 +00003325cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003326{
3327 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3328 struct TCP_Server_Info *server = tcon->ses->server;
Jeff Laytonce91acb2012-01-17 16:08:51 -05003329 unsigned int wsize;
3330
3331 /* start with specified wsize, or default */
3332 if (pvolume_info->wsize)
3333 wsize = pvolume_info->wsize;
3334 else if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
3335 wsize = CIFS_DEFAULT_IOSIZE;
3336 else
3337 wsize = CIFS_DEFAULT_NON_POSIX_WSIZE;
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003338
3339 /* can server support 24-bit write sizes? (via UNIX extensions) */
3340 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
Jeff Layton1190f6a2011-06-22 17:33:57 -04003341 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003342
Jeff Layton1190f6a2011-06-22 17:33:57 -04003343 /*
3344 * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
3345 * Limit it to max buffer offered by the server, minus the size of the
3346 * WRITEX header, not including the 4 byte RFC1001 length.
3347 */
3348 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
3349 (!(server->capabilities & CAP_UNIX) &&
3350 (server->sec_mode & (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED))))
3351 wsize = min_t(unsigned int, wsize,
3352 server->maxBuf - sizeof(WRITE_REQ) + 4);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003353
Jeff Layton3ae629d2012-07-11 09:09:35 -04003354 /* limit to the amount that we can kmap at once */
3355 wsize = min_t(unsigned int, wsize, CIFS_KMAP_SIZE_LIMIT);
3356
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003357 /* hard limit of CIFS_MAX_WSIZE */
3358 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
3359
3360 return wsize;
3361}
3362
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003363static unsigned int
3364cifs_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
3365{
3366 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
3367 struct TCP_Server_Info *server = tcon->ses->server;
3368 unsigned int rsize, defsize;
3369
3370 /*
3371 * Set default value...
3372 *
3373 * HACK alert! Ancient servers have very small buffers. Even though
3374 * MS-CIFS indicates that servers are only limited by the client's
3375 * bufsize for reads, testing against win98se shows that it throws
3376 * INVALID_PARAMETER errors if you try to request too large a read.
Jeff Laytonec01d732012-07-02 07:24:25 -04003377 * OS/2 just sends back short reads.
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003378 *
Jeff Laytonec01d732012-07-02 07:24:25 -04003379 * If the server doesn't advertise CAP_LARGE_READ_X, then assume that
3380 * it can't handle a read request larger than its MaxBufferSize either.
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003381 */
3382 if (tcon->unix_ext && (unix_cap & CIFS_UNIX_LARGE_READ_CAP))
3383 defsize = CIFS_DEFAULT_IOSIZE;
3384 else if (server->capabilities & CAP_LARGE_READ_X)
3385 defsize = CIFS_DEFAULT_NON_POSIX_RSIZE;
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003386 else
3387 defsize = server->maxBuf - sizeof(READ_RSP);
3388
3389 rsize = pvolume_info->rsize ? pvolume_info->rsize : defsize;
3390
3391 /*
3392 * no CAP_LARGE_READ_X? Then MS-CIFS states that we must limit this to
3393 * the client's MaxBufferSize.
3394 */
3395 if (!(server->capabilities & CAP_LARGE_READ_X))
3396 rsize = min_t(unsigned int, CIFSMaxBufSize, rsize);
3397
Jeff Layton3ae629d2012-07-11 09:09:35 -04003398 /* limit to the amount that we can kmap at once */
3399 rsize = min_t(unsigned int, rsize, CIFS_KMAP_SIZE_LIMIT);
3400
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003401 /* hard limit of CIFS_MAX_RSIZE */
3402 rsize = min_t(unsigned int, rsize, CIFS_MAX_RSIZE);
3403
3404 return rsize;
3405}
3406
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003407static void
3408cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003409{
Sean Finneyb9468452011-04-11 13:19:32 +00003410 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003411 kzfree(volume_info->password);
Steve French13589c42011-08-18 04:41:55 +00003412 if (volume_info->UNCip != volume_info->UNC + 2)
3413 kfree(volume_info->UNCip);
Jesper Juhl95c75452011-08-27 18:58:34 +02003414 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003415 kfree(volume_info->domainname);
3416 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003417 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003418}
3419
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003420void
3421cifs_cleanup_volume_info(struct smb_vol *volume_info)
3422{
3423 if (!volume_info)
3424 return;
3425 cleanup_volume_info_contents(volume_info);
3426 kfree(volume_info);
3427}
3428
3429
Steve French2d6d5892009-04-09 00:36:44 +00003430#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003431/* build_path_to_root returns full path to root when
3432 * we do not have an exiting connection (tcon) */
3433static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003434build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003435 const struct cifs_sb_info *cifs_sb)
3436{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003437 char *full_path, *pos;
3438 unsigned int pplen = vol->prepath ? strlen(vol->prepath) : 0;
3439 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003440
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003441 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003442 if (full_path == NULL)
3443 return ERR_PTR(-ENOMEM);
3444
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003445 strncpy(full_path, vol->UNC, unc_len);
3446 pos = full_path + unc_len;
3447
3448 if (pplen) {
3449 strncpy(pos, vol->prepath, pplen);
3450 pos += pplen;
3451 }
3452
3453 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003454 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003455 cFYI(1, "%s: full_path=%s", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003456 return full_path;
3457}
Sean Finneydd613942011-04-11 13:19:30 +00003458
3459/*
3460 * Perform a dfs referral query for a share and (optionally) prefix
3461 *
Sean Finney046462a2011-04-11 13:19:33 +00003462 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3463 * to a string containing updated options for the submount. Otherwise it
3464 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003465 *
3466 * Returns the rc from get_dfs_path to the caller, which can be used to
3467 * determine whether there were referrals.
3468 */
3469static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003470expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003471 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003472 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003473{
3474 int rc;
3475 unsigned int num_referrals = 0;
3476 struct dfs_info3_param *referrals = NULL;
3477 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3478
3479 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3480 if (IS_ERR(full_path))
3481 return PTR_ERR(full_path);
3482
3483 /* For DFS paths, skip the first '\' of the UNC */
3484 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3485
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003486 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Sean Finneydd613942011-04-11 13:19:30 +00003487 &num_referrals, &referrals,
3488 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
3489
3490 if (!rc && num_referrals > 0) {
3491 char *fake_devname = NULL;
3492
3493 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3494 full_path + 1, referrals,
3495 &fake_devname);
3496
3497 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003498
Sean Finneydd613942011-04-11 13:19:30 +00003499 if (IS_ERR(mdata)) {
3500 rc = PTR_ERR(mdata);
3501 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003502 } else {
3503 cleanup_volume_info_contents(volume_info);
3504 memset(volume_info, '\0', sizeof(*volume_info));
3505 rc = cifs_setup_volume_info(volume_info, mdata,
3506 fake_devname);
Sean Finneydd613942011-04-11 13:19:30 +00003507 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003508 kfree(fake_devname);
3509 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003510 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003511 }
3512 kfree(full_path);
3513 return rc;
3514}
Steve French2d6d5892009-04-09 00:36:44 +00003515#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003516
Jeff Layton04db79b2011-07-06 08:10:38 -04003517static int
3518cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
3519 const char *devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003521 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003522
Jeff Layton04db79b2011-07-06 08:10:38 -04003523 if (cifs_parse_mount_options(mount_data, devname, volume_info))
3524 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Jeff Layton23db65f2012-05-15 12:20:51 -04003526
Jeff Layton7586b762008-12-01 18:41:49 -05003527 if (volume_info->nullauth) {
Jeff Layton04febab2012-01-17 16:09:15 -05003528 cFYI(1, "Anonymous login");
3529 kfree(volume_info->username);
3530 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003531 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 /* BB fixme parse for domain name here */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003533 cFYI(1, "Username: %s", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 } else {
Steve Frenchbf820672005-12-01 22:32:42 -08003535 cifserror("No username specified");
Steve French50c2f752007-07-13 00:33:32 +00003536 /* In userspace mount helper we can get user name from alternate
3537 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003542 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003543 /* load_nls_default cannot return null */
3544 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003546 volume_info->local_nls = load_nls(volume_info->iocharset);
3547 if (volume_info->local_nls == NULL) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003548 cERROR(1, "CIFS mount error: iocharset %s not found",
3549 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003550 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
3552 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003553
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003554 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003555}
3556
3557struct smb_vol *
3558cifs_get_volume_info(char *mount_data, const char *devname)
3559{
3560 int rc;
3561 struct smb_vol *volume_info;
3562
3563 volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
3564 if (!volume_info)
3565 return ERR_PTR(-ENOMEM);
3566
3567 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
3568 if (rc) {
3569 cifs_cleanup_volume_info(volume_info);
3570 volume_info = ERR_PTR(rc);
3571 }
3572
3573 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003574}
3575
3576int
Al Viro2c6292a2011-06-17 09:05:48 -04003577cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003578{
Jeff Layton1daaae82012-03-21 06:30:40 -04003579 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003580 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003581 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00003582 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003583 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003584 char *full_path;
3585 struct tcon_link *tlink;
3586#ifdef CONFIG_CIFS_DFS_UPCALL
3587 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04003588#endif
Al Virodd854462011-06-17 08:24:42 -04003589
3590 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
3591 if (rc)
3592 return rc;
3593
Jeff Layton20547492011-07-09 12:21:07 -04003594#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003595try_mount_again:
3596 /* cleanup activities if we're chasing a referral */
3597 if (referral_walks_count) {
3598 if (tcon)
3599 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003600 else if (ses)
3601 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003602
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003603 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003604 }
3605#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04003606 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003607 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003608 ses = NULL;
3609 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003610 full_path = NULL;
3611 tlink = NULL;
3612
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003613 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
Jeff Layton63c038c2008-12-01 18:41:46 -05003615 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003616 server = cifs_get_tcp_session(volume_info);
3617 if (IS_ERR(server)) {
3618 rc = PTR_ERR(server);
Al Virodd854462011-06-17 08:24:42 -04003619 bdi_destroy(&cifs_sb->bdi);
Jeff Layton63c038c2008-12-01 18:41:46 -05003620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
3622
Jeff Layton36988c72010-04-24 07:57:43 -04003623 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003624 ses = cifs_get_smb_ses(server, volume_info);
3625 if (IS_ERR(ses)) {
3626 rc = PTR_ERR(ses);
3627 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04003628 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
Steve French50c2f752007-07-13 00:33:32 +00003630
Jeff Laytond00c28d2010-04-24 07:57:44 -04003631 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003632 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003633 if (IS_ERR(tcon)) {
3634 rc = PTR_ERR(tcon);
3635 tcon = NULL;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003636 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003637 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003638
Steve French6848b732011-05-26 18:38:54 +00003639 /* tell server which Unix caps we support */
3640 if (tcon->ses->capabilities & CAP_UNIX) {
3641 /* reset of caps checks mount to see if unix extensions
3642 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04003643 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00003644 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3645 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3646 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3647 rc = -EACCES;
3648 goto mount_fail_check;
3649 }
3650 } else
3651 tcon->unix_ext = 0; /* server does not support them */
3652
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003653 /* do not care if a following call succeed - informational */
3654 if (!tcon->ipc && server->ops->qfs_tcon)
3655 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00003656
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003657 cifs_sb->wsize = cifs_negotiate_wsize(tcon, volume_info);
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003658 cifs_sb->rsize = cifs_negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04003659
Jeff Layton66bfaad2011-10-19 15:30:35 -04003660 /* tune readahead according to rsize */
Jeff Layton8f714652012-05-01 17:41:49 -04003661 cifs_sb->bdi.ra_pages = cifs_sb->rsize / PAGE_CACHE_SIZE;
Jeff Layton03ceace2010-12-06 21:07:33 -05003662
Igor Mammedove4cce942009-02-10 14:10:26 +03003663remote_path_check:
3664#ifdef CONFIG_CIFS_DFS_UPCALL
3665 /*
3666 * Perform an unconditional check for whether there are DFS
3667 * referrals for this path without prefix, to provide support
3668 * for DFS referrals from w2k8 servers which don't seem to respond
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003669 * with PATH_NOT_COVERED to requests that include the prefix.
3670 * Chase the referral if found, otherwise continue normally.
Steve Frenchd036f502009-04-03 03:12:08 +00003671 */
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003672 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003673 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
3674 false);
Steve Frencheeac8042006-01-13 21:34:58 -08003675 if (!refrc) {
Steve French4523cc32007-04-30 20:13:06 +00003676 referral_walks_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 goto try_mount_again;
3678 }
3679 }
3680#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003681
Steve Frenchf87d39d2011-05-27 03:50:55 +00003682 /* check if a whole path is not remote */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003684 if (!server->ops->is_path_accessible) {
3685 rc = -ENOSYS;
3686 goto mount_fail_check;
3687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 /* build_path_to_root works only when we have a valid tcon */
Pavel Shilovsky9224dfc2012-05-27 20:39:52 +04003689 full_path = build_path_to_root(volume_info, cifs_sb, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 if (full_path == NULL) {
3691 rc = -ENOMEM;
3692 goto mount_fail_check;
3693 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04003694 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
3695 full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 if (rc != 0 && rc != -EREMOTE) {
3697 kfree(full_path);
3698 goto mount_fail_check;
3699 }
3700 kfree(full_path);
3701 }
3702
3703 /* get referral if needed */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003704 if (rc == -EREMOTE) {
3705#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003706 if (referral_walks_count > MAX_NESTED_LINKS) {
3707 /*
3708 * BB: when we implement proper loop detection,
3709 * we will remove this check. But now we need it
3710 * to prevent an indefinite loop if 'DFS tree' is
3711 * misconfigured (i.e. has loops).
3712 */
3713 rc = -ELOOP;
3714 goto mount_fail_check;
3715 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003716
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003717 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04003718
Sean Finneydd613942011-04-11 13:19:30 +00003719 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04003720 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003721 goto try_mount_again;
3722 }
Sean Finneydd613942011-04-11 13:19:30 +00003723 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00003724#else /* No DFS support, return error on mount */
3725 rc = -EOPNOTSUPP;
3726#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003727 }
3728
Jeff Layton9d002df2010-10-06 19:51:11 -04003729 if (rc)
3730 goto mount_fail_check;
3731
3732 /* now, hang the tcon off of the superblock */
3733 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3734 if (tlink == NULL) {
3735 rc = -ENOMEM;
3736 goto mount_fail_check;
3737 }
3738
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003739 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04003740 tlink->tl_tcon = tcon;
3741 tlink->tl_time = jiffies;
3742 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3743 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3744
Jeff Layton413e6612010-10-28 13:33:38 -04003745 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04003746 spin_lock(&cifs_sb->tlink_tree_lock);
3747 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3748 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04003749
Jeff Laytonda472fc2012-03-23 14:40:53 -04003750 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04003751 TLINK_IDLE_EXPIRE);
3752
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003753mount_fail_check:
3754 /* on error free sesinfo and tcon struct if needed */
3755 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003756 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003757 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003758 if (tcon)
3759 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003760 else if (ses)
3761 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003762 else
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04003763 cifs_put_tcp_session(server);
Al Virodd854462011-06-17 08:24:42 -04003764 bdi_destroy(&cifs_sb->bdi);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003765 }
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003768 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return rc;
3770}
3771
Jeff Layton8d1bca32011-06-11 21:17:10 -04003772/*
3773 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3774 * pointer may be NULL.
3775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003777CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00003778 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 const struct nls_table *nls_codepage)
3780{
3781 struct smb_hdr *smb_buffer;
3782 struct smb_hdr *smb_buffer_response;
3783 TCONX_REQ *pSMB;
3784 TCONX_RSP *pSMBr;
3785 unsigned char *bcc_ptr;
3786 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05003787 int length;
3788 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 if (ses == NULL)
3791 return -EIO;
3792
3793 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00003794 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00003796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 smb_buffer_response = smb_buffer;
3798
3799 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
3800 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07003801
Pavel Shilovsky88257362012-05-23 14:01:59 +04003802 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 smb_buffer->Uid = ses->Suid;
3804 pSMB = (TCONX_REQ *) smb_buffer;
3805 pSMBr = (TCONX_RSP *) smb_buffer_response;
3806
3807 pSMB->AndXCommand = 0xFF;
3808 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 bcc_ptr = &pSMB->Password[0];
Jeff Layton8d1bca32011-06-11 21:17:10 -04003810 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08003811 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00003812 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08003813 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00003814 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08003815 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003816 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08003817 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
3818 specified as required (when that support is added to
3819 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00003820 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08003821 by Samba (not sure whether other servers allow
3822 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00003823#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04003824 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton00e485b2008-12-05 20:41:21 -05003825 (ses->server->secType == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05003826 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00003827 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05003828 SECMODE_PW_ENCRYPT ? true : false,
3829 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00003830 else
3831#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06003832 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05003833 bcc_ptr, nls_codepage);
Steve Frencheeac8042006-01-13 21:34:58 -08003834
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06003835 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003836 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00003837 /* must align unicode strings */
3838 *bcc_ptr = 0; /* null byte password */
3839 bcc_ptr++;
3840 }
Steve Frencheeac8042006-01-13 21:34:58 -08003841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Steve French96daf2b2011-05-27 04:34:02 +00003843 if (ses->server->sec_mode &
Steve Frencha878fb22006-05-30 18:04:19 +00003844 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
3846
3847 if (ses->capabilities & CAP_STATUS32) {
3848 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
3849 }
3850 if (ses->capabilities & CAP_DFS) {
3851 smb_buffer->Flags2 |= SMBFLG2_DFS;
3852 }
3853 if (ses->capabilities & CAP_UNICODE) {
3854 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
3855 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06003856 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00003857 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00003858 (/* server len*/ + 256 /* share len */), nls_codepage);
3859 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 bcc_ptr += 2; /* skip trailing null */
3861 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 strcpy(bcc_ptr, tree);
3863 bcc_ptr += strlen(tree) + 1;
3864 }
3865 strcpy(bcc_ptr, "?????");
3866 bcc_ptr += strlen("?????");
3867 bcc_ptr += 1;
3868 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003869 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
3870 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 pSMB->ByteCount = cpu_to_le16(count);
3872
Steve French133672e2007-11-13 22:41:37 +00003873 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05003874 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 /* above now done in SendReceive */
3877 if ((rc == 0) && (tcon != NULL)) {
Steve French0e0d2cf2009-05-01 05:27:32 +00003878 bool is_unicode;
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00003881 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 tcon->tid = smb_buffer_response->Tid;
3883 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05003884 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003885 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00003886 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
3887 is_unicode = true;
3888 else
3889 is_unicode = false;
3890
Jeff Laytoncc20c032009-04-30 07:16:21 -04003891
Steve French50c2f752007-07-13 00:33:32 +00003892 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00003893 if (length == 3) {
3894 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
3895 (bcc_ptr[2] == 'C')) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00003896 cFYI(1, "IPC connection");
Steve French7f8ed422007-09-28 22:28:55 +00003897 tcon->ipc = 1;
3898 }
3899 } else if (length == 2) {
3900 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
3901 /* the most common case */
Joe Perchesb6b38f72010-04-21 03:50:45 +00003902 cFYI(1, "disk share connection");
Steve French7f8ed422007-09-28 22:28:55 +00003903 }
3904 }
Steve French50c2f752007-07-13 00:33:32 +00003905 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04003906 bytes_left -= (length + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003908
3909 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04003910 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06003911 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00003912 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04003913 nls_codepage);
3914
Joe Perchesb6b38f72010-04-21 03:50:45 +00003915 cFYI(1, "nativeFileSystem=%s", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04003916
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003917 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00003918 (smb_buffer_response->WordCount == 7))
3919 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00003920 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
3921 else
3922 tcon->Flags = 0;
Joe Perchesb6b38f72010-04-21 03:50:45 +00003923 cFYI(1, "Tcon flags: 0x%x ", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 } else if ((rc == 0) && tcon == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003925 /* all we need to save for IPC$ connection */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 ses->ipc_tid = smb_buffer_response->Tid;
3927 }
3928
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00003929 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 return rc;
3931}
3932
Al Viro2a9b9952011-06-17 09:27:16 -04003933void
3934cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
Jeff Laytonb647c352010-10-28 11:16:44 -04003936 struct rb_root *root = &cifs_sb->tlink_tree;
3937 struct rb_node *node;
3938 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Jeff Layton2de970f2010-10-06 19:51:12 -04003940 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
3941
Jeff Laytonb647c352010-10-28 11:16:44 -04003942 spin_lock(&cifs_sb->tlink_tree_lock);
3943 while ((node = rb_first(root))) {
3944 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3945 cifs_get_tlink(tlink);
3946 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3947 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00003948
Jeff Laytonb647c352010-10-28 11:16:44 -04003949 spin_unlock(&cifs_sb->tlink_tree_lock);
3950 cifs_put_tlink(tlink);
3951 spin_lock(&cifs_sb->tlink_tree_lock);
3952 }
3953 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04003954
Al Virodd854462011-06-17 08:24:42 -04003955 bdi_destroy(&cifs_sb->bdi);
Al Virod757d712011-06-17 09:42:43 -04003956 kfree(cifs_sb->mountdata);
3957 unload_nls(cifs_sb->local_nls);
3958 kfree(cifs_sb);
Steve French50c2f752007-07-13 00:33:32 +00003959}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003961int
3962cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963{
3964 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04003965 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003967 if (!server->ops->need_neg || !server->ops->negotiate)
3968 return -ENOSYS;
3969
Jeff Layton198b5682010-04-24 07:57:48 -04003970 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003971 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04003972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Pavel Shilovsky45275782012-05-17 17:53:29 +04003974 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003975
3976 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04003977 if (rc == 0) {
3978 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04003979 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04003980 server->tcpStatus = CifsGood;
3981 else
3982 rc = -EHOSTDOWN;
3983 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
Steve French26b994f2008-08-06 05:11:33 +00003985
Jeff Layton198b5682010-04-24 07:57:48 -04003986 return rc;
3987}
Steve French26b994f2008-08-06 05:11:33 +00003988
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003989int
3990cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
3991 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04003992{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04003993 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04003994 struct TCP_Server_Info *server = ses->server;
3995
3996 ses->flags = 0;
3997 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00003998 if (linuxExtEnabled == 0)
Jeff Layton198b5682010-04-24 07:57:48 -04003999 ses->capabilities &= (~CAP_UNIX);
Steve French20418ac2009-04-30 16:13:32 +00004000
Joe Perchesb6b38f72010-04-21 03:50:45 +00004001 cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
Steve French96daf2b2011-05-27 04:34:02 +00004002 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004003
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004004 if (server->ops->sess_setup)
4005 rc = server->ops->sess_setup(xid, ses, nls_info);
4006
Steve French26b994f2008-08-06 05:11:33 +00004007 if (rc) {
Joe Perchesb6b38f72010-04-21 03:50:45 +00004008 cERROR(1, "Send error in SessSetup = %d", rc);
Steve French26b994f2008-08-06 05:11:33 +00004009 } else {
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05004010 mutex_lock(&ses->server->srv_mutex);
4011 if (!server->session_estab) {
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004012 server->session_key.response = ses->auth_key.response;
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05004013 server->session_key.len = ses->auth_key.len;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004014 server->sequence_number = 0x2;
4015 server->session_estab = true;
4016 ses->auth_key.response = NULL;
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05004017 }
4018 mutex_unlock(&server->srv_mutex);
4019
Joe Perchesb6b38f72010-04-21 03:50:45 +00004020 cFYI(1, "CIFS Session Established successfully");
Steve French20418ac2009-04-30 16:13:32 +00004021 spin_lock(&GlobalMid_Lock);
Jeff Layton198b5682010-04-24 07:57:48 -04004022 ses->status = CifsGood;
4023 ses->need_reconnect = false;
Steve French20418ac2009-04-30 16:13:32 +00004024 spin_unlock(&GlobalMid_Lock);
Steve French26b994f2008-08-06 05:11:33 +00004025 }
4026
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004027 kfree(ses->auth_key.response);
4028 ses->auth_key.response = NULL;
4029 ses->auth_key.len = 0;
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05004030 kfree(ses->ntlmssp);
4031 ses->ntlmssp = NULL;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return rc;
4034}
4035
Jeff Layton8a8798a2012-01-17 16:09:15 -05004036static int
4037cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4038{
4039 switch (ses->server->secType) {
4040 case Kerberos:
4041 vol->secFlg = CIFSSEC_MUST_KRB5;
4042 return 0;
4043 case NTLMv2:
4044 vol->secFlg = CIFSSEC_MUST_NTLMV2;
4045 break;
4046 case NTLM:
4047 vol->secFlg = CIFSSEC_MUST_NTLM;
4048 break;
4049 case RawNTLMSSP:
4050 vol->secFlg = CIFSSEC_MUST_NTLMSSP;
4051 break;
4052 case LANMAN:
4053 vol->secFlg = CIFSSEC_MUST_LANMAN;
4054 break;
4055 }
4056
4057 return cifs_set_cifscreds(vol, ses);
4058}
4059
Steve French96daf2b2011-05-27 04:34:02 +00004060static struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004061cifs_construct_tcon(struct cifs_sb_info *cifs_sb, uid_t fsuid)
4062{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004063 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004064 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4065 struct cifs_ses *ses;
4066 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004067 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004068
4069 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004070 if (vol_info == NULL)
4071 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004072
Jeff Layton9d002df2010-10-06 19:51:11 -04004073 vol_info->local_nls = cifs_sb->local_nls;
4074 vol_info->linux_uid = fsuid;
4075 vol_info->cred_uid = fsuid;
4076 vol_info->UNC = master_tcon->treeName;
4077 vol_info->retry = master_tcon->retry;
4078 vol_info->nocase = master_tcon->nocase;
4079 vol_info->local_lease = master_tcon->local_lease;
4080 vol_info->no_linux_ext = !master_tcon->unix_ext;
4081
Jeff Layton8a8798a2012-01-17 16:09:15 -05004082 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4083 if (rc) {
4084 tcon = ERR_PTR(rc);
4085 goto out;
4086 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004087
4088 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304089 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004090 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304091 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004092
4093 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4094 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004095 tcon = (struct cifs_tcon *)ses;
Jeff Layton9d002df2010-10-06 19:51:11 -04004096 cifs_put_tcp_session(master_tcon->ses->server);
4097 goto out;
4098 }
4099
4100 tcon = cifs_get_tcon(ses, vol_info);
4101 if (IS_ERR(tcon)) {
4102 cifs_put_smb_ses(ses);
4103 goto out;
4104 }
4105
4106 if (ses->capabilities & CAP_UNIX)
4107 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
4108out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004109 kfree(vol_info->username);
4110 kfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004111 kfree(vol_info);
4112
4113 return tcon;
4114}
4115
Steve French96daf2b2011-05-27 04:34:02 +00004116struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004117cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4118{
4119 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4120}
4121
4122static int
4123cifs_sb_tcon_pending_wait(void *unused)
4124{
4125 schedule();
4126 return signal_pending(current) ? -ERESTARTSYS : 0;
4127}
4128
Jeff Laytonb647c352010-10-28 11:16:44 -04004129/* find and return a tlink with given uid */
4130static struct tcon_link *
4131tlink_rb_search(struct rb_root *root, uid_t uid)
4132{
4133 struct rb_node *node = root->rb_node;
4134 struct tcon_link *tlink;
4135
4136 while (node) {
4137 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4138
4139 if (tlink->tl_uid > uid)
4140 node = node->rb_left;
4141 else if (tlink->tl_uid < uid)
4142 node = node->rb_right;
4143 else
4144 return tlink;
4145 }
4146 return NULL;
4147}
4148
4149/* insert a tcon_link into the tree */
4150static void
4151tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4152{
4153 struct rb_node **new = &(root->rb_node), *parent = NULL;
4154 struct tcon_link *tlink;
4155
4156 while (*new) {
4157 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4158 parent = *new;
4159
4160 if (tlink->tl_uid > new_tlink->tl_uid)
4161 new = &((*new)->rb_left);
4162 else
4163 new = &((*new)->rb_right);
4164 }
4165
4166 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4167 rb_insert_color(&new_tlink->tl_rbnode, root);
4168}
4169
Jeff Layton9d002df2010-10-06 19:51:11 -04004170/*
4171 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4172 * current task.
4173 *
4174 * If the superblock doesn't refer to a multiuser mount, then just return
4175 * the master tcon for the mount.
4176 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304177 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004178 * exists, then check to see if it's pending construction. If it is then wait
4179 * for construction to complete. Once it's no longer pending, check to see if
4180 * it failed and either return an error or retry construction, depending on
4181 * the timeout.
4182 *
4183 * If one doesn't exist then insert a new tcon_link struct into the tree and
4184 * try to construct a new one.
4185 */
4186struct tcon_link *
4187cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4188{
4189 int ret;
Jeff Laytonb647c352010-10-28 11:16:44 -04004190 uid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004191 struct tcon_link *tlink, *newtlink;
4192
4193 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4194 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4195
4196 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004197 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004198 if (tlink)
4199 cifs_get_tlink(tlink);
4200 spin_unlock(&cifs_sb->tlink_tree_lock);
4201
4202 if (tlink == NULL) {
4203 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4204 if (newtlink == NULL)
4205 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004206 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004207 newtlink->tl_tcon = ERR_PTR(-EACCES);
4208 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4209 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4210 cifs_get_tlink(newtlink);
4211
Jeff Layton9d002df2010-10-06 19:51:11 -04004212 spin_lock(&cifs_sb->tlink_tree_lock);
4213 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004214 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004215 if (tlink) {
4216 cifs_get_tlink(tlink);
4217 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004218 kfree(newtlink);
4219 goto wait_for_construction;
4220 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004221 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004222 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4223 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004224 } else {
4225wait_for_construction:
4226 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
4227 cifs_sb_tcon_pending_wait,
4228 TASK_INTERRUPTIBLE);
4229 if (ret) {
4230 cifs_put_tlink(tlink);
4231 return ERR_PTR(ret);
4232 }
4233
4234 /* if it's good, return it */
4235 if (!IS_ERR(tlink->tl_tcon))
4236 return tlink;
4237
4238 /* return error if we tried this already recently */
4239 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4240 cifs_put_tlink(tlink);
4241 return ERR_PTR(-EACCES);
4242 }
4243
4244 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4245 goto wait_for_construction;
4246 }
4247
4248 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4249 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4250 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4251
4252 if (IS_ERR(tlink->tl_tcon)) {
4253 cifs_put_tlink(tlink);
4254 return ERR_PTR(-EACCES);
4255 }
4256
4257 return tlink;
4258}
Jeff Layton2de970f2010-10-06 19:51:12 -04004259
4260/*
4261 * periodic workqueue job that scans tcon_tree for a superblock and closes
4262 * out tcons.
4263 */
4264static void
4265cifs_prune_tlinks(struct work_struct *work)
4266{
4267 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4268 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004269 struct rb_root *root = &cifs_sb->tlink_tree;
4270 struct rb_node *node = rb_first(root);
4271 struct rb_node *tmp;
4272 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004273
Jeff Laytonb647c352010-10-28 11:16:44 -04004274 /*
4275 * Because we drop the spinlock in the loop in order to put the tlink
4276 * it's not guarded against removal of links from the tree. The only
4277 * places that remove entries from the tree are this function and
4278 * umounts. Because this function is non-reentrant and is canceled
4279 * before umount can proceed, this is safe.
4280 */
4281 spin_lock(&cifs_sb->tlink_tree_lock);
4282 node = rb_first(root);
4283 while (node != NULL) {
4284 tmp = node;
4285 node = rb_next(tmp);
4286 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4287
4288 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4289 atomic_read(&tlink->tl_count) != 0 ||
4290 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4291 continue;
4292
4293 cifs_get_tlink(tlink);
4294 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4295 rb_erase(tmp, root);
4296
Jeff Layton2de970f2010-10-06 19:51:12 -04004297 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004298 cifs_put_tlink(tlink);
4299 spin_lock(&cifs_sb->tlink_tree_lock);
4300 }
4301 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004302
Jeff Laytonda472fc2012-03-23 14:40:53 -04004303 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004304 TLINK_IDLE_EXPIRE);
4305}