aboutsummaryrefslogtreecommitdiffstats
path: root/fs/cifs/connect.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/cifs/connect.c')
-rw-r--r--fs/cifs/connect.c2362
1 files changed, 1535 insertions, 827 deletions
diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c
index 88c84a38bccb..ccc1afa0bf3b 100644
--- a/fs/cifs/connect.c
+++ b/fs/cifs/connect.c
@@ -47,69 +47,26 @@
47#include "ntlmssp.h" 47#include "ntlmssp.h"
48#include "nterr.h" 48#include "nterr.h"
49#include "rfc1002pdu.h" 49#include "rfc1002pdu.h"
50#include "cn_cifs.h"
51#include "fscache.h" 50#include "fscache.h"
52 51
53#define CIFS_PORT 445 52#define CIFS_PORT 445
54#define RFC1001_PORT 139 53#define RFC1001_PORT 139
55 54
56extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8, 55/* SMB echo "timeout" -- FIXME: tunable? */
57 unsigned char *p24); 56#define SMB_ECHO_INTERVAL (60 * HZ)
58 57
59extern mempool_t *cifs_req_poolp; 58extern mempool_t *cifs_req_poolp;
60 59
61struct smb_vol { 60/* FIXME: should these be tunable? */
62 char *username; 61#define TLINK_ERROR_EXPIRE (1 * HZ)
63 char *password; 62#define TLINK_IDLE_EXPIRE (600 * HZ)
64 char *domainname; 63
65 char *UNC; 64static int ip_connect(struct TCP_Server_Info *server);
66 char *UNCip; 65static int generic_ip_connect(struct TCP_Server_Info *server);
67 char *iocharset; /* local code page for mapping to and from Unicode */ 66static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
68 char source_rfc1001_name[16]; /* netbios name of client */ 67static void cifs_prune_tlinks(struct work_struct *work);
69 char target_rfc1001_name[16]; /* netbios name of server for Win9x/ME */ 68static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
70 uid_t cred_uid; 69 const char *devname);
71 uid_t linux_uid;
72 gid_t linux_gid;
73 mode_t file_mode;
74 mode_t dir_mode;
75 unsigned secFlg;
76 bool retry:1;
77 bool intr:1;
78 bool setuids:1;
79 bool override_uid:1;
80 bool override_gid:1;
81 bool dynperm:1;
82 bool noperm:1;
83 bool no_psx_acl:1; /* set if posix acl support should be disabled */
84 bool cifs_acl:1;
85 bool no_xattr:1; /* set if xattr (EA) support should be disabled*/
86 bool server_ino:1; /* use inode numbers from server ie UniqueId */
87 bool direct_io:1;
88 bool remap:1; /* set to remap seven reserved chars in filenames */
89 bool posix_paths:1; /* unset to not ask for posix pathnames. */
90 bool no_linux_ext:1;
91 bool sfu_emul:1;
92 bool nullauth:1; /* attempt to authenticate with null user */
93 bool nocase:1; /* request case insensitive filenames */
94 bool nobrl:1; /* disable sending byte range locks to srv */
95 bool mand_lock:1; /* send mandatory not posix byte range lock reqs */
96 bool seal:1; /* request transport encryption on share */
97 bool nodfs:1; /* Do not request DFS, even if available */
98 bool local_lease:1; /* check leases only on local system, not remote */
99 bool noblocksnd:1;
100 bool noautotune:1;
101 bool nostrictsync:1; /* do not force expensive SMBflush on every sync */
102 bool fsc:1; /* enable fscache */
103 unsigned int rsize;
104 unsigned int wsize;
105 bool sockopt_tcp_nodelay:1;
106 unsigned short int port;
107 char *prepath;
108 struct nls_table *local_nls;
109};
110
111static int ipv4_connect(struct TCP_Server_Info *server);
112static int ipv6_connect(struct TCP_Server_Info *server);
113 70
114/* 71/*
115 * cifs tcp session reconnection 72 * cifs tcp session reconnection
@@ -124,9 +81,10 @@ cifs_reconnect(struct TCP_Server_Info *server)
124{ 81{
125 int rc = 0; 82 int rc = 0;
126 struct list_head *tmp, *tmp2; 83 struct list_head *tmp, *tmp2;
127 struct cifsSesInfo *ses; 84 struct cifs_ses *ses;
128 struct cifsTconInfo *tcon; 85 struct cifs_tcon *tcon;
129 struct mid_q_entry *mid_entry; 86 struct mid_q_entry *mid_entry;
87 struct list_head retry_list;
130 88
131 spin_lock(&GlobalMid_Lock); 89 spin_lock(&GlobalMid_Lock);
132 if (server->tcpStatus == CifsExiting) { 90 if (server->tcpStatus == CifsExiting) {
@@ -143,18 +101,21 @@ cifs_reconnect(struct TCP_Server_Info *server)
143 101
144 /* before reconnecting the tcp session, mark the smb session (uid) 102 /* before reconnecting the tcp session, mark the smb session (uid)
145 and the tid bad so they are not used until reconnected */ 103 and the tid bad so they are not used until reconnected */
146 read_lock(&cifs_tcp_ses_lock); 104 cFYI(1, "%s: marking sessions and tcons for reconnect", __func__);
105 spin_lock(&cifs_tcp_ses_lock);
147 list_for_each(tmp, &server->smb_ses_list) { 106 list_for_each(tmp, &server->smb_ses_list) {
148 ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list); 107 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
149 ses->need_reconnect = true; 108 ses->need_reconnect = true;
150 ses->ipc_tid = 0; 109 ses->ipc_tid = 0;
151 list_for_each(tmp2, &ses->tcon_list) { 110 list_for_each(tmp2, &ses->tcon_list) {
152 tcon = list_entry(tmp2, struct cifsTconInfo, tcon_list); 111 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
153 tcon->need_reconnect = true; 112 tcon->need_reconnect = true;
154 } 113 }
155 } 114 }
156 read_unlock(&cifs_tcp_ses_lock); 115 spin_unlock(&cifs_tcp_ses_lock);
116
157 /* do not want to be sending data on a socket we are freeing */ 117 /* do not want to be sending data on a socket we are freeing */
118 cFYI(1, "%s: tearing down socket", __func__);
158 mutex_lock(&server->srv_mutex); 119 mutex_lock(&server->srv_mutex);
159 if (server->ssocket) { 120 if (server->ssocket) {
160 cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state, 121 cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
@@ -166,30 +127,38 @@ cifs_reconnect(struct TCP_Server_Info *server)
166 sock_release(server->ssocket); 127 sock_release(server->ssocket);
167 server->ssocket = NULL; 128 server->ssocket = NULL;
168 } 129 }
130 server->sequence_number = 0;
131 server->session_estab = false;
132 kfree(server->session_key.response);
133 server->session_key.response = NULL;
134 server->session_key.len = 0;
135 server->lstrp = jiffies;
136 mutex_unlock(&server->srv_mutex);
169 137
138 /* mark submitted MIDs for retry and issue callback */
139 INIT_LIST_HEAD(&retry_list);
140 cFYI(1, "%s: moving mids to private list", __func__);
170 spin_lock(&GlobalMid_Lock); 141 spin_lock(&GlobalMid_Lock);
171 list_for_each(tmp, &server->pending_mid_q) { 142 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
172 mid_entry = list_entry(tmp, struct 143 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
173 mid_q_entry, 144 if (mid_entry->midState == MID_REQUEST_SUBMITTED)
174 qhead);
175 if (mid_entry->midState == MID_REQUEST_SUBMITTED) {
176 /* Mark other intransit requests as needing
177 retry so we do not immediately mark the
178 session bad again (ie after we reconnect
179 below) as they timeout too */
180 mid_entry->midState = MID_RETRY_NEEDED; 145 mid_entry->midState = MID_RETRY_NEEDED;
181 } 146 list_move(&mid_entry->qhead, &retry_list);
182 } 147 }
183 spin_unlock(&GlobalMid_Lock); 148 spin_unlock(&GlobalMid_Lock);
184 mutex_unlock(&server->srv_mutex);
185 149
186 while ((server->tcpStatus != CifsExiting) && 150 cFYI(1, "%s: issuing mid callbacks", __func__);
187 (server->tcpStatus != CifsGood)) { 151 list_for_each_safe(tmp, tmp2, &retry_list) {
152 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
153 list_del_init(&mid_entry->qhead);
154 mid_entry->callback(mid_entry);
155 }
156
157 do {
188 try_to_freeze(); 158 try_to_freeze();
189 if (server->addr.sockAddr6.sin6_family == AF_INET6) 159
190 rc = ipv6_connect(server); 160 /* we should try only the port we connected to before */
191 else 161 rc = generic_ip_connect(server);
192 rc = ipv4_connect(server);
193 if (rc) { 162 if (rc) {
194 cFYI(1, "reconnect error %d", rc); 163 cFYI(1, "reconnect error %d", rc);
195 msleep(3000); 164 msleep(3000);
@@ -197,13 +166,11 @@ cifs_reconnect(struct TCP_Server_Info *server)
197 atomic_inc(&tcpSesReconnectCount); 166 atomic_inc(&tcpSesReconnectCount);
198 spin_lock(&GlobalMid_Lock); 167 spin_lock(&GlobalMid_Lock);
199 if (server->tcpStatus != CifsExiting) 168 if (server->tcpStatus != CifsExiting)
200 server->tcpStatus = CifsGood; 169 server->tcpStatus = CifsNeedNegotiate;
201 server->sequence_number = 0;
202 spin_unlock(&GlobalMid_Lock); 170 spin_unlock(&GlobalMid_Lock);
203 /* atomic_set(&server->inFlight,0);*/
204 wake_up(&server->response_q);
205 } 171 }
206 } 172 } while (server->tcpStatus == CifsNeedReconnect);
173
207 return rc; 174 return rc;
208} 175}
209 176
@@ -217,9 +184,8 @@ cifs_reconnect(struct TCP_Server_Info *server)
217static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize) 184static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
218{ 185{
219 struct smb_t2_rsp *pSMBt; 186 struct smb_t2_rsp *pSMBt;
220 int total_data_size;
221 int data_in_this_rsp;
222 int remaining; 187 int remaining;
188 __u16 total_data_size, data_in_this_rsp;
223 189
224 if (pSMB->Command != SMB_COM_TRANSACTION2) 190 if (pSMB->Command != SMB_COM_TRANSACTION2)
225 return 0; 191 return 0;
@@ -233,93 +199,124 @@ static int check2ndT2(struct smb_hdr *pSMB, unsigned int maxBufSize)
233 199
234 pSMBt = (struct smb_t2_rsp *)pSMB; 200 pSMBt = (struct smb_t2_rsp *)pSMB;
235 201
236 total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount); 202 total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
237 data_in_this_rsp = le16_to_cpu(pSMBt->t2_rsp.DataCount); 203 data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
238
239 remaining = total_data_size - data_in_this_rsp;
240 204
241 if (remaining == 0) 205 if (total_data_size == data_in_this_rsp)
242 return 0; 206 return 0;
243 else if (remaining < 0) { 207 else if (total_data_size < data_in_this_rsp) {
244 cFYI(1, "total data %d smaller than data in frame %d", 208 cFYI(1, "total data %d smaller than data in frame %d",
245 total_data_size, data_in_this_rsp); 209 total_data_size, data_in_this_rsp);
246 return -EINVAL; 210 return -EINVAL;
247 } else {
248 cFYI(1, "missing %d bytes from transact2, check next response",
249 remaining);
250 if (total_data_size > maxBufSize) {
251 cERROR(1, "TotalDataSize %d is over maximum buffer %d",
252 total_data_size, maxBufSize);
253 return -EINVAL;
254 }
255 return remaining;
256 } 211 }
212
213 remaining = total_data_size - data_in_this_rsp;
214
215 cFYI(1, "missing %d bytes from transact2, check next response",
216 remaining);
217 if (total_data_size > maxBufSize) {
218 cERROR(1, "TotalDataSize %d is over maximum buffer %d",
219 total_data_size, maxBufSize);
220 return -EINVAL;
221 }
222 return remaining;
257} 223}
258 224
259static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB) 225static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
260{ 226{
261 struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond; 227 struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
262 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB; 228 struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
263 int total_data_size;
264 int total_in_buf;
265 int remaining;
266 int total_in_buf2;
267 char *data_area_of_target; 229 char *data_area_of_target;
268 char *data_area_of_buf2; 230 char *data_area_of_buf2;
269 __u16 byte_count; 231 int remaining;
232 unsigned int byte_count, total_in_buf;
233 __u16 total_data_size, total_in_buf2;
270 234
271 total_data_size = le16_to_cpu(pSMBt->t2_rsp.TotalDataCount); 235 total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
272 236
273 if (total_data_size != le16_to_cpu(pSMB2->t2_rsp.TotalDataCount)) { 237 if (total_data_size !=
238 get_unaligned_le16(&pSMB2->t2_rsp.TotalDataCount))
274 cFYI(1, "total data size of primary and secondary t2 differ"); 239 cFYI(1, "total data size of primary and secondary t2 differ");
275 }
276 240
277 total_in_buf = le16_to_cpu(pSMBt->t2_rsp.DataCount); 241 total_in_buf = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
278 242
279 remaining = total_data_size - total_in_buf; 243 remaining = total_data_size - total_in_buf;
280 244
281 if (remaining < 0) 245 if (remaining < 0)
282 return -EINVAL; 246 return -EPROTO;
283 247
284 if (remaining == 0) /* nothing to do, ignore */ 248 if (remaining == 0) /* nothing to do, ignore */
285 return 0; 249 return 0;
286 250
287 total_in_buf2 = le16_to_cpu(pSMB2->t2_rsp.DataCount); 251 total_in_buf2 = get_unaligned_le16(&pSMB2->t2_rsp.DataCount);
288 if (remaining < total_in_buf2) { 252 if (remaining < total_in_buf2) {
289 cFYI(1, "transact2 2nd response contains too much data"); 253 cFYI(1, "transact2 2nd response contains too much data");
290 } 254 }
291 255
292 /* find end of first SMB data area */ 256 /* find end of first SMB data area */
293 data_area_of_target = (char *)&pSMBt->hdr.Protocol + 257 data_area_of_target = (char *)&pSMBt->hdr.Protocol +
294 le16_to_cpu(pSMBt->t2_rsp.DataOffset); 258 get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
295 /* validate target area */ 259 /* validate target area */
296 260
297 data_area_of_buf2 = (char *) &pSMB2->hdr.Protocol + 261 data_area_of_buf2 = (char *)&pSMB2->hdr.Protocol +
298 le16_to_cpu(pSMB2->t2_rsp.DataOffset); 262 get_unaligned_le16(&pSMB2->t2_rsp.DataOffset);
299 263
300 data_area_of_target += total_in_buf; 264 data_area_of_target += total_in_buf;
301 265
302 /* copy second buffer into end of first buffer */ 266 /* copy second buffer into end of first buffer */
303 memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
304 total_in_buf += total_in_buf2; 267 total_in_buf += total_in_buf2;
305 pSMBt->t2_rsp.DataCount = cpu_to_le16(total_in_buf); 268 /* is the result too big for the field? */
306 byte_count = le16_to_cpu(BCC_LE(pTargetSMB)); 269 if (total_in_buf > USHRT_MAX)
307 byte_count += total_in_buf2; 270 return -EPROTO;
308 BCC_LE(pTargetSMB) = cpu_to_le16(byte_count); 271 put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
309 272
310 byte_count = pTargetSMB->smb_buf_length; 273 /* fix up the BCC */
274 byte_count = get_bcc(pTargetSMB);
311 byte_count += total_in_buf2; 275 byte_count += total_in_buf2;
276 /* is the result too big for the field? */
277 if (byte_count > USHRT_MAX)
278 return -EPROTO;
279 put_bcc(byte_count, pTargetSMB);
312 280
313 /* BB also add check that we are not beyond maximum buffer size */ 281 byte_count = be32_to_cpu(pTargetSMB->smb_buf_length);
282 byte_count += total_in_buf2;
283 /* don't allow buffer to overflow */
284 if (byte_count > CIFSMaxBufSize)
285 return -ENOBUFS;
286 pTargetSMB->smb_buf_length = cpu_to_be32(byte_count);
314 287
315 pTargetSMB->smb_buf_length = byte_count; 288 memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
316 289
317 if (remaining == total_in_buf2) { 290 if (remaining == total_in_buf2) {
318 cFYI(1, "found the last secondary response"); 291 cFYI(1, "found the last secondary response");
319 return 0; /* we are done */ 292 return 0; /* we are done */
320 } else /* more responses to go */ 293 } else /* more responses to go */
321 return 1; 294 return 1;
295}
296
297static void
298cifs_echo_request(struct work_struct *work)
299{
300 int rc;
301 struct TCP_Server_Info *server = container_of(work,
302 struct TCP_Server_Info, echo.work);
303
304 /*
305 * We cannot send an echo until the NEGOTIATE_PROTOCOL request is
306 * done, which is indicated by maxBuf != 0. Also, no need to ping if
307 * we got a response recently
308 */
309 if (server->maxBuf == 0 ||
310 time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
311 goto requeue_echo;
312
313 rc = CIFSSMBEcho(server);
314 if (rc)
315 cFYI(1, "Unable to send echo request to server: %s",
316 server->hostname);
322 317
318requeue_echo:
319 queue_delayed_work(system_nrt_wq, &server->echo, SMB_ECHO_INTERVAL);
323} 320}
324 321
325static int 322static int
@@ -333,8 +330,7 @@ cifs_demultiplex_thread(struct TCP_Server_Info *server)
333 struct msghdr smb_msg; 330 struct msghdr smb_msg;
334 struct kvec iov; 331 struct kvec iov;
335 struct socket *csocket = server->ssocket; 332 struct socket *csocket = server->ssocket;
336 struct list_head *tmp; 333 struct list_head *tmp, *tmp2;
337 struct cifsSesInfo *ses;
338 struct task_struct *task_to_wake = NULL; 334 struct task_struct *task_to_wake = NULL;
339 struct mid_q_entry *mid_entry; 335 struct mid_q_entry *mid_entry;
340 char temp; 336 char temp;
@@ -387,7 +383,20 @@ cifs_demultiplex_thread(struct TCP_Server_Info *server)
387 smb_msg.msg_control = NULL; 383 smb_msg.msg_control = NULL;
388 smb_msg.msg_controllen = 0; 384 smb_msg.msg_controllen = 0;
389 pdu_length = 4; /* enough to get RFC1001 header */ 385 pdu_length = 4; /* enough to get RFC1001 header */
386
390incomplete_rcv: 387incomplete_rcv:
388 if (echo_retries > 0 && server->tcpStatus == CifsGood &&
389 time_after(jiffies, server->lstrp +
390 (echo_retries * SMB_ECHO_INTERVAL))) {
391 cERROR(1, "Server %s has not responded in %d seconds. "
392 "Reconnecting...", server->hostname,
393 (echo_retries * SMB_ECHO_INTERVAL / HZ));
394 cifs_reconnect(server);
395 csocket = server->ssocket;
396 wake_up(&server->response_q);
397 continue;
398 }
399
391 length = 400 length =
392 kernel_recvmsg(csocket, &smb_msg, 401 kernel_recvmsg(csocket, &smb_msg,
393 &iov, 1, pdu_length, 0 /* BB other flags? */); 402 &iov, 1, pdu_length, 0 /* BB other flags? */);
@@ -441,8 +450,7 @@ incomplete_rcv:
441 /* Note that FC 1001 length is big endian on the wire, 450 /* Note that FC 1001 length is big endian on the wire,
442 but we convert it here so it is always manipulated 451 but we convert it here so it is always manipulated
443 as host byte order */ 452 as host byte order */
444 pdu_length = be32_to_cpu((__force __be32)smb_buffer->smb_buf_length); 453 pdu_length = be32_to_cpu(smb_buffer->smb_buf_length);
445 smb_buffer->smb_buf_length = pdu_length;
446 454
447 cFYI(1, "rfc1002 length 0x%x", pdu_length+4); 455 cFYI(1, "rfc1002 length 0x%x", pdu_length+4);
448 456
@@ -464,7 +472,7 @@ incomplete_rcv:
464 * initialize frame) 472 * initialize frame)
465 */ 473 */
466 cifs_set_port((struct sockaddr *) 474 cifs_set_port((struct sockaddr *)
467 &server->addr.sockAddr, CIFS_PORT); 475 &server->dstaddr, CIFS_PORT);
468 cifs_reconnect(server); 476 cifs_reconnect(server);
469 csocket = server->ssocket; 477 csocket = server->ssocket;
470 wake_up(&server->response_q); 478 wake_up(&server->response_q);
@@ -538,73 +546,92 @@ incomplete_rcv:
538 else if (reconnect == 1) 546 else if (reconnect == 1)
539 continue; 547 continue;
540 548
541 length += 4; /* account for rfc1002 hdr */ 549 total_read += 4; /* account for rfc1002 hdr */
542 550
551 dump_smb(smb_buffer, total_read);
543 552
544 dump_smb(smb_buffer, length); 553 /*
545 if (checkSMB(smb_buffer, smb_buffer->Mid, total_read+4)) { 554 * We know that we received enough to get to the MID as we
546 cifs_dump_mem("Bad SMB: ", smb_buffer, 48); 555 * checked the pdu_length earlier. Now check to see
547 continue; 556 * if the rest of the header is OK. We borrow the length
548 } 557 * var for the rest of the loop to avoid a new stack var.
558 *
559 * 48 bytes is enough to display the header and a little bit
560 * into the payload for debugging purposes.
561 */
562 length = checkSMB(smb_buffer, smb_buffer->Mid, total_read);
563 if (length != 0)
564 cifs_dump_mem("Bad SMB: ", smb_buffer,
565 min_t(unsigned int, total_read, 48));
549 566
567 mid_entry = NULL;
568 server->lstrp = jiffies;
550 569
551 task_to_wake = NULL;
552 spin_lock(&GlobalMid_Lock); 570 spin_lock(&GlobalMid_Lock);
553 list_for_each(tmp, &server->pending_mid_q) { 571 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
554 mid_entry = list_entry(tmp, struct mid_q_entry, qhead); 572 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
555 573
556 if ((mid_entry->mid == smb_buffer->Mid) && 574 if (mid_entry->mid != smb_buffer->Mid ||
557 (mid_entry->midState == MID_REQUEST_SUBMITTED) && 575 mid_entry->midState != MID_REQUEST_SUBMITTED ||
558 (mid_entry->command == smb_buffer->Command)) { 576 mid_entry->command != smb_buffer->Command) {
559 if (check2ndT2(smb_buffer,server->maxBuf) > 0) { 577 mid_entry = NULL;
560 /* We have a multipart transact2 resp */ 578 continue;
561 isMultiRsp = true; 579 }
562 if (mid_entry->resp_buf) { 580
563 /* merge response - fix up 1st*/ 581 if (length == 0 &&
564 if (coalesce_t2(smb_buffer, 582 check2ndT2(smb_buffer, server->maxBuf) > 0) {
565 mid_entry->resp_buf)) { 583 /* We have a multipart transact2 resp */
566 mid_entry->multiRsp = 584 isMultiRsp = true;
567 true; 585 if (mid_entry->resp_buf) {
568 break; 586 /* merge response - fix up 1st*/
569 } else { 587 length = coalesce_t2(smb_buffer,
570 /* all parts received */ 588 mid_entry->resp_buf);
571 mid_entry->multiEnd = 589 if (length > 0) {
572 true; 590 length = 0;
573 goto multi_t2_fnd; 591 mid_entry->multiRsp = true;
574 } 592 break;
575 } else { 593 } else {
576 if (!isLargeBuf) { 594 /* all parts received or
577 cERROR(1, "1st trans2 resp needs bigbuf"); 595 * packet is malformed
578 /* BB maybe we can fix this up, switch 596 */
579 to already allocated large buffer? */ 597 mid_entry->multiEnd = true;
580 } else { 598 goto multi_t2_fnd;
581 /* Have first buffer */ 599 }
582 mid_entry->resp_buf = 600 } else {
583 smb_buffer; 601 if (!isLargeBuf) {
584 mid_entry->largeBuf = 602 /*
585 true; 603 * FIXME: switch to already
586 bigbuf = NULL; 604 * allocated largebuf?
587 } 605 */
606 cERROR(1, "1st trans2 resp "
607 "needs bigbuf");
608 } else {
609 /* Have first buffer */
610 mid_entry->resp_buf =
611 smb_buffer;
612 mid_entry->largeBuf = true;
613 bigbuf = NULL;
588 } 614 }
589 break;
590 } 615 }
591 mid_entry->resp_buf = smb_buffer; 616 break;
592 mid_entry->largeBuf = isLargeBuf; 617 }
618 mid_entry->resp_buf = smb_buffer;
619 mid_entry->largeBuf = isLargeBuf;
593multi_t2_fnd: 620multi_t2_fnd:
594 task_to_wake = mid_entry->tsk; 621 if (length == 0)
595 mid_entry->midState = MID_RESPONSE_RECEIVED; 622 mid_entry->midState = MID_RESPONSE_RECEIVED;
623 else
624 mid_entry->midState = MID_RESPONSE_MALFORMED;
596#ifdef CONFIG_CIFS_STATS2 625#ifdef CONFIG_CIFS_STATS2
597 mid_entry->when_received = jiffies; 626 mid_entry->when_received = jiffies;
598#endif 627#endif
599 /* so we do not time out requests to server 628 list_del_init(&mid_entry->qhead);
600 which is still responding (since server could 629 break;
601 be busy but not dead) */
602 server->lstrp = jiffies;
603 break;
604 }
605 } 630 }
606 spin_unlock(&GlobalMid_Lock); 631 spin_unlock(&GlobalMid_Lock);
607 if (task_to_wake) { 632
633 if (mid_entry != NULL) {
634 mid_entry->callback(mid_entry);
608 /* Was previous buf put in mpx struct for multi-rsp? */ 635 /* Was previous buf put in mpx struct for multi-rsp? */
609 if (!isMultiRsp) { 636 if (!isMultiRsp) {
610 /* smb buffer will be freed by user thread */ 637 /* smb buffer will be freed by user thread */
@@ -613,11 +640,13 @@ multi_t2_fnd:
613 else 640 else
614 smallbuf = NULL; 641 smallbuf = NULL;
615 } 642 }
616 wake_up_process(task_to_wake); 643 } else if (length != 0) {
644 /* response sanity checks failed */
645 continue;
617 } else if (!is_valid_oplock_break(smb_buffer, server) && 646 } else if (!is_valid_oplock_break(smb_buffer, server) &&
618 !isMultiRsp) { 647 !isMultiRsp) {
619 cERROR(1, "No task to wake, unknown frame received! " 648 cERROR(1, "No task to wake, unknown frame received! "
620 "NumMids %d", midCount.counter); 649 "NumMids %d", atomic_read(&midCount));
621 cifs_dump_mem("Received Data is: ", (char *)smb_buffer, 650 cifs_dump_mem("Received Data is: ", (char *)smb_buffer,
622 sizeof(struct smb_hdr)); 651 sizeof(struct smb_hdr));
623#ifdef CONFIG_CIFS_DEBUG2 652#ifdef CONFIG_CIFS_DEBUG2
@@ -629,9 +658,9 @@ multi_t2_fnd:
629 } /* end while !EXITING */ 658 } /* end while !EXITING */
630 659
631 /* take it off the list, if it's not already */ 660 /* take it off the list, if it's not already */
632 write_lock(&cifs_tcp_ses_lock); 661 spin_lock(&cifs_tcp_ses_lock);
633 list_del_init(&server->tcp_ses_list); 662 list_del_init(&server->tcp_ses_list);
634 write_unlock(&cifs_tcp_ses_lock); 663 spin_unlock(&cifs_tcp_ses_lock);
635 664
636 spin_lock(&GlobalMid_Lock); 665 spin_lock(&GlobalMid_Lock);
637 server->tcpStatus = CifsExiting; 666 server->tcpStatus = CifsExiting;
@@ -660,49 +689,31 @@ multi_t2_fnd:
660 sock_release(csocket); 689 sock_release(csocket);
661 server->ssocket = NULL; 690 server->ssocket = NULL;
662 } 691 }
663 /* buffer usuallly freed in free_mid - need to free it here on exit */ 692 /* buffer usually freed in free_mid - need to free it here on exit */
664 cifs_buf_release(bigbuf); 693 cifs_buf_release(bigbuf);
665 if (smallbuf) /* no sense logging a debug message if NULL */ 694 if (smallbuf) /* no sense logging a debug message if NULL */
666 cifs_small_buf_release(smallbuf); 695 cifs_small_buf_release(smallbuf);
667 696
668 /* 697 if (!list_empty(&server->pending_mid_q)) {
669 * BB: we shouldn't have to do any of this. It shouldn't be 698 struct list_head dispose_list;
670 * possible to exit from the thread with active SMB sessions
671 */
672 read_lock(&cifs_tcp_ses_lock);
673 if (list_empty(&server->pending_mid_q)) {
674 /* loop through server session structures attached to this and
675 mark them dead */
676 list_for_each(tmp, &server->smb_ses_list) {
677 ses = list_entry(tmp, struct cifsSesInfo,
678 smb_ses_list);
679 ses->status = CifsExiting;
680 ses->server = NULL;
681 }
682 read_unlock(&cifs_tcp_ses_lock);
683 } else {
684 /* although we can not zero the server struct pointer yet,
685 since there are active requests which may depnd on them,
686 mark the corresponding SMB sessions as exiting too */
687 list_for_each(tmp, &server->smb_ses_list) {
688 ses = list_entry(tmp, struct cifsSesInfo,
689 smb_ses_list);
690 ses->status = CifsExiting;
691 }
692 699
700 INIT_LIST_HEAD(&dispose_list);
693 spin_lock(&GlobalMid_Lock); 701 spin_lock(&GlobalMid_Lock);
694 list_for_each(tmp, &server->pending_mid_q) { 702 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
695 mid_entry = list_entry(tmp, struct mid_q_entry, qhead); 703 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
696 if (mid_entry->midState == MID_REQUEST_SUBMITTED) { 704 cFYI(1, "Clearing mid 0x%x", mid_entry->mid);
697 cFYI(1, "Clearing Mid 0x%x - waking up ", 705 mid_entry->midState = MID_SHUTDOWN;
698 mid_entry->mid); 706 list_move(&mid_entry->qhead, &dispose_list);
699 task_to_wake = mid_entry->tsk;
700 if (task_to_wake)
701 wake_up_process(task_to_wake);
702 }
703 } 707 }
704 spin_unlock(&GlobalMid_Lock); 708 spin_unlock(&GlobalMid_Lock);
705 read_unlock(&cifs_tcp_ses_lock); 709
710 /* now walk dispose list and issue callbacks */
711 list_for_each_safe(tmp, tmp2, &dispose_list) {
712 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
713 cFYI(1, "Callback mid 0x%x", mid_entry->mid);
714 list_del_init(&mid_entry->qhead);
715 mid_entry->callback(mid_entry);
716 }
706 /* 1/8th of sec is more than enough time for them to exit */ 717 /* 1/8th of sec is more than enough time for them to exit */
707 msleep(125); 718 msleep(125);
708 } 719 }
@@ -720,18 +731,6 @@ multi_t2_fnd:
720 coming home not much else we can do but free the memory */ 731 coming home not much else we can do but free the memory */
721 } 732 }
722 733
723 /* last chance to mark ses pointers invalid
724 if there are any pointing to this (e.g
725 if a crazy root user tried to kill cifsd
726 kernel thread explicitly this might happen) */
727 /* BB: This shouldn't be necessary, see above */
728 read_lock(&cifs_tcp_ses_lock);
729 list_for_each(tmp, &server->smb_ses_list) {
730 ses = list_entry(tmp, struct cifsSesInfo, smb_ses_list);
731 ses->server = NULL;
732 }
733 read_unlock(&cifs_tcp_ses_lock);
734
735 kfree(server->hostname); 734 kfree(server->hostname);
736 task_to_wake = xchg(&server->tsk, NULL); 735 task_to_wake = xchg(&server->tsk, NULL);
737 kfree(server); 736 kfree(server);
@@ -783,35 +782,32 @@ extract_hostname(const char *unc)
783} 782}
784 783
785static int 784static int
786cifs_parse_mount_options(char *options, const char *devname, 785cifs_parse_mount_options(const char *mountdata, const char *devname,
787 struct smb_vol *vol) 786 struct smb_vol *vol)
788{ 787{
789 char *value; 788 char *value, *data, *end;
790 char *data; 789 char *mountdata_copy = NULL, *options;
791 unsigned int temp_len, i, j; 790 unsigned int temp_len, i, j;
792 char separator[2]; 791 char separator[2];
793 short int override_uid = -1; 792 short int override_uid = -1;
794 short int override_gid = -1; 793 short int override_gid = -1;
795 bool uid_specified = false; 794 bool uid_specified = false;
796 bool gid_specified = false; 795 bool gid_specified = false;
796 char *nodename = utsname()->nodename;
797 797
798 separator[0] = ','; 798 separator[0] = ',';
799 separator[1] = 0; 799 separator[1] = 0;
800 800
801 if (Local_System_Name[0] != 0) 801 /*
802 memcpy(vol->source_rfc1001_name, Local_System_Name, 15); 802 * does not have to be perfect mapping since field is
803 else { 803 * informational, only used for servers that do not support
804 char *nodename = utsname()->nodename; 804 * port 445 and it can be overridden at mount time
805 int n = strnlen(nodename, 15); 805 */
806 memset(vol->source_rfc1001_name, 0x20, 15); 806 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
807 for (i = 0; i < n; i++) { 807 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
808 /* does not have to be perfect mapping since field is 808 vol->source_rfc1001_name[i] = toupper(nodename[i]);
809 informational, only used for servers that do not support 809
810 port 445 and it can be overridden at mount time */ 810 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
811 vol->source_rfc1001_name[i] = toupper(nodename[i]);
812 }
813 }
814 vol->source_rfc1001_name[15] = 0;
815 /* null target name indicates to use *SMBSERVR default called name 811 /* null target name indicates to use *SMBSERVR default called name
816 if we end up sending RFC1001 session initialize */ 812 if we end up sending RFC1001 session initialize */
817 vol->target_rfc1001_name[0] = 0; 813 vol->target_rfc1001_name[0] = 0;
@@ -828,9 +824,17 @@ cifs_parse_mount_options(char *options, const char *devname,
828 /* default to using server inode numbers where available */ 824 /* default to using server inode numbers where available */
829 vol->server_ino = 1; 825 vol->server_ino = 1;
830 826
831 if (!options) 827 vol->actimeo = CIFS_DEF_ACTIMEO;
832 return 1; 828
829 if (!mountdata)
830 goto cifs_parse_mount_err;
833 831
832 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
833 if (!mountdata_copy)
834 goto cifs_parse_mount_err;
835
836 options = mountdata_copy;
837 end = options + strlen(options);
834 if (strncmp(options, "sep=", 4) == 0) { 838 if (strncmp(options, "sep=", 4) == 0) {
835 if (options[4] != 0) { 839 if (options[4] != 0) {
836 separator[0] = options[4]; 840 separator[0] = options[4];
@@ -855,16 +859,22 @@ cifs_parse_mount_options(char *options, const char *devname,
855 if (!value) { 859 if (!value) {
856 printk(KERN_WARNING 860 printk(KERN_WARNING
857 "CIFS: invalid or missing username\n"); 861 "CIFS: invalid or missing username\n");
858 return 1; /* needs_arg; */ 862 goto cifs_parse_mount_err;
859 } else if (!*value) { 863 } else if (!*value) {
860 /* null user, ie anonymous, authentication */ 864 /* null user, ie anonymous, authentication */
861 vol->nullauth = 1; 865 vol->nullauth = 1;
862 } 866 }
863 if (strnlen(value, 200) < 200) { 867 if (strnlen(value, MAX_USERNAME_SIZE) <
864 vol->username = value; 868 MAX_USERNAME_SIZE) {
869 vol->username = kstrdup(value, GFP_KERNEL);
870 if (!vol->username) {
871 printk(KERN_WARNING "CIFS: no memory "
872 "for username\n");
873 goto cifs_parse_mount_err;
874 }
865 } else { 875 } else {
866 printk(KERN_WARNING "CIFS: username too long\n"); 876 printk(KERN_WARNING "CIFS: username too long\n");
867 return 1; 877 goto cifs_parse_mount_err;
868 } 878 }
869 } else if (strnicmp(data, "pass", 4) == 0) { 879 } else if (strnicmp(data, "pass", 4) == 0) {
870 if (!value) { 880 if (!value) {
@@ -895,6 +905,7 @@ cifs_parse_mount_options(char *options, const char *devname,
895 the only illegal character in a password is null */ 905 the only illegal character in a password is null */
896 906
897 if ((value[temp_len] == 0) && 907 if ((value[temp_len] == 0) &&
908 (value + temp_len < end) &&
898 (value[temp_len+1] == separator[0])) { 909 (value[temp_len+1] == separator[0])) {
899 /* reinsert comma */ 910 /* reinsert comma */
900 value[temp_len] = separator[0]; 911 value[temp_len] = separator[0];
@@ -927,7 +938,7 @@ cifs_parse_mount_options(char *options, const char *devname,
927 if (vol->password == NULL) { 938 if (vol->password == NULL) {
928 printk(KERN_WARNING "CIFS: no memory " 939 printk(KERN_WARNING "CIFS: no memory "
929 "for password\n"); 940 "for password\n");
930 return 1; 941 goto cifs_parse_mount_err;
931 } 942 }
932 for (i = 0, j = 0; i < temp_len; i++, j++) { 943 for (i = 0, j = 0; i < temp_len; i++, j++) {
933 vol->password[j] = value[i]; 944 vol->password[j] = value[i];
@@ -943,7 +954,7 @@ cifs_parse_mount_options(char *options, const char *devname,
943 if (vol->password == NULL) { 954 if (vol->password == NULL) {
944 printk(KERN_WARNING "CIFS: no memory " 955 printk(KERN_WARNING "CIFS: no memory "
945 "for password\n"); 956 "for password\n");
946 return 1; 957 goto cifs_parse_mount_err;
947 } 958 }
948 strcpy(vol->password, value); 959 strcpy(vol->password, value);
949 } 960 }
@@ -953,11 +964,16 @@ cifs_parse_mount_options(char *options, const char *devname,
953 vol->UNCip = NULL; 964 vol->UNCip = NULL;
954 } else if (strnlen(value, INET6_ADDRSTRLEN) < 965 } else if (strnlen(value, INET6_ADDRSTRLEN) <
955 INET6_ADDRSTRLEN) { 966 INET6_ADDRSTRLEN) {
956 vol->UNCip = value; 967 vol->UNCip = kstrdup(value, GFP_KERNEL);
968 if (!vol->UNCip) {
969 printk(KERN_WARNING "CIFS: no memory "
970 "for UNC IP\n");
971 goto cifs_parse_mount_err;
972 }
957 } else { 973 } else {
958 printk(KERN_WARNING "CIFS: ip address " 974 printk(KERN_WARNING "CIFS: ip address "
959 "too long\n"); 975 "too long\n");
960 return 1; 976 goto cifs_parse_mount_err;
961 } 977 }
962 } else if (strnicmp(data, "sec", 3) == 0) { 978 } else if (strnicmp(data, "sec", 3) == 0) {
963 if (!value || !*value) { 979 if (!value || !*value) {
@@ -970,16 +986,14 @@ cifs_parse_mount_options(char *options, const char *devname,
970 /* vol->secFlg |= CIFSSEC_MUST_SEAL | 986 /* vol->secFlg |= CIFSSEC_MUST_SEAL |
971 CIFSSEC_MAY_KRB5; */ 987 CIFSSEC_MAY_KRB5; */
972 cERROR(1, "Krb5 cifs privacy not supported"); 988 cERROR(1, "Krb5 cifs privacy not supported");
973 return 1; 989 goto cifs_parse_mount_err;
974 } else if (strnicmp(value, "krb5", 4) == 0) { 990 } else if (strnicmp(value, "krb5", 4) == 0) {
975 vol->secFlg |= CIFSSEC_MAY_KRB5; 991 vol->secFlg |= CIFSSEC_MAY_KRB5;
976#ifdef CONFIG_CIFS_EXPERIMENTAL
977 } else if (strnicmp(value, "ntlmsspi", 8) == 0) { 992 } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
978 vol->secFlg |= CIFSSEC_MAY_NTLMSSP | 993 vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
979 CIFSSEC_MUST_SIGN; 994 CIFSSEC_MUST_SIGN;
980 } else if (strnicmp(value, "ntlmssp", 7) == 0) { 995 } else if (strnicmp(value, "ntlmssp", 7) == 0) {
981 vol->secFlg |= CIFSSEC_MAY_NTLMSSP; 996 vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
982#endif
983 } else if (strnicmp(value, "ntlmv2i", 7) == 0) { 997 } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
984 vol->secFlg |= CIFSSEC_MAY_NTLMV2 | 998 vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
985 CIFSSEC_MUST_SIGN; 999 CIFSSEC_MUST_SIGN;
@@ -1002,7 +1016,16 @@ cifs_parse_mount_options(char *options, const char *devname,
1002 vol->nullauth = 1; 1016 vol->nullauth = 1;
1003 } else { 1017 } else {
1004 cERROR(1, "bad security option: %s", value); 1018 cERROR(1, "bad security option: %s", value);
1005 return 1; 1019 goto cifs_parse_mount_err;
1020 }
1021 } else if (strnicmp(data, "vers", 3) == 0) {
1022 if (!value || !*value) {
1023 cERROR(1, "no protocol version specified"
1024 " after vers= mount option");
1025 } else if ((strnicmp(value, "cifs", 4) == 0) ||
1026 (strnicmp(value, "1", 1) == 0)) {
1027 /* this is the default */
1028 continue;
1006 } 1029 }
1007 } else if ((strnicmp(data, "unc", 3) == 0) 1030 } else if ((strnicmp(data, "unc", 3) == 0)
1008 || (strnicmp(data, "target", 6) == 0) 1031 || (strnicmp(data, "target", 6) == 0)
@@ -1010,12 +1033,12 @@ cifs_parse_mount_options(char *options, const char *devname,
1010 if (!value || !*value) { 1033 if (!value || !*value) {
1011 printk(KERN_WARNING "CIFS: invalid path to " 1034 printk(KERN_WARNING "CIFS: invalid path to "
1012 "network resource\n"); 1035 "network resource\n");
1013 return 1; /* needs_arg; */ 1036 goto cifs_parse_mount_err;
1014 } 1037 }
1015 if ((temp_len = strnlen(value, 300)) < 300) { 1038 if ((temp_len = strnlen(value, 300)) < 300) {
1016 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL); 1039 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
1017 if (vol->UNC == NULL) 1040 if (vol->UNC == NULL)
1018 return 1; 1041 goto cifs_parse_mount_err;
1019 strcpy(vol->UNC, value); 1042 strcpy(vol->UNC, value);
1020 if (strncmp(vol->UNC, "//", 2) == 0) { 1043 if (strncmp(vol->UNC, "//", 2) == 0) {
1021 vol->UNC[0] = '\\'; 1044 vol->UNC[0] = '\\';
@@ -1024,40 +1047,61 @@ cifs_parse_mount_options(char *options, const char *devname,
1024 printk(KERN_WARNING 1047 printk(KERN_WARNING
1025 "CIFS: UNC Path does not begin " 1048 "CIFS: UNC Path does not begin "
1026 "with // or \\\\ \n"); 1049 "with // or \\\\ \n");
1027 return 1; 1050 goto cifs_parse_mount_err;
1028 } 1051 }
1029 } else { 1052 } else {
1030 printk(KERN_WARNING "CIFS: UNC name too long\n"); 1053 printk(KERN_WARNING "CIFS: UNC name too long\n");
1031 return 1; 1054 goto cifs_parse_mount_err;
1032 } 1055 }
1033 } else if ((strnicmp(data, "domain", 3) == 0) 1056 } else if ((strnicmp(data, "domain", 3) == 0)
1034 || (strnicmp(data, "workgroup", 5) == 0)) { 1057 || (strnicmp(data, "workgroup", 5) == 0)) {
1035 if (!value || !*value) { 1058 if (!value || !*value) {
1036 printk(KERN_WARNING "CIFS: invalid domain name\n"); 1059 printk(KERN_WARNING "CIFS: invalid domain name\n");
1037 return 1; /* needs_arg; */ 1060 goto cifs_parse_mount_err;
1038 } 1061 }
1039 /* BB are there cases in which a comma can be valid in 1062 /* BB are there cases in which a comma can be valid in
1040 a domain name and need special handling? */ 1063 a domain name and need special handling? */
1041 if (strnlen(value, 256) < 256) { 1064 if (strnlen(value, 256) < 256) {
1042 vol->domainname = value; 1065 vol->domainname = kstrdup(value, GFP_KERNEL);
1066 if (!vol->domainname) {
1067 printk(KERN_WARNING "CIFS: no memory "
1068 "for domainname\n");
1069 goto cifs_parse_mount_err;
1070 }
1043 cFYI(1, "Domain name set"); 1071 cFYI(1, "Domain name set");
1044 } else { 1072 } else {
1045 printk(KERN_WARNING "CIFS: domain name too " 1073 printk(KERN_WARNING "CIFS: domain name too "
1046 "long\n"); 1074 "long\n");
1047 return 1; 1075 goto cifs_parse_mount_err;
1076 }
1077 } else if (strnicmp(data, "srcaddr", 7) == 0) {
1078 vol->srcaddr.ss_family = AF_UNSPEC;
1079
1080 if (!value || !*value) {
1081 printk(KERN_WARNING "CIFS: srcaddr value"
1082 " not specified.\n");
1083 goto cifs_parse_mount_err;
1084 }
1085 i = cifs_convert_address((struct sockaddr *)&vol->srcaddr,
1086 value, strlen(value));
1087 if (i == 0) {
1088 printk(KERN_WARNING "CIFS: Could not parse"
1089 " srcaddr: %s\n",
1090 value);
1091 goto cifs_parse_mount_err;
1048 } 1092 }
1049 } else if (strnicmp(data, "prefixpath", 10) == 0) { 1093 } else if (strnicmp(data, "prefixpath", 10) == 0) {
1050 if (!value || !*value) { 1094 if (!value || !*value) {
1051 printk(KERN_WARNING 1095 printk(KERN_WARNING
1052 "CIFS: invalid path prefix\n"); 1096 "CIFS: invalid path prefix\n");
1053 return 1; /* needs_argument */ 1097 goto cifs_parse_mount_err;
1054 } 1098 }
1055 if ((temp_len = strnlen(value, 1024)) < 1024) { 1099 if ((temp_len = strnlen(value, 1024)) < 1024) {
1056 if (value[0] != '/') 1100 if (value[0] != '/')
1057 temp_len++; /* missing leading slash */ 1101 temp_len++; /* missing leading slash */
1058 vol->prepath = kmalloc(temp_len+1, GFP_KERNEL); 1102 vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
1059 if (vol->prepath == NULL) 1103 if (vol->prepath == NULL)
1060 return 1; 1104 goto cifs_parse_mount_err;
1061 if (value[0] != '/') { 1105 if (value[0] != '/') {
1062 vol->prepath[0] = '/'; 1106 vol->prepath[0] = '/';
1063 strcpy(vol->prepath+1, value); 1107 strcpy(vol->prepath+1, value);
@@ -1066,28 +1110,39 @@ cifs_parse_mount_options(char *options, const char *devname,
1066 cFYI(1, "prefix path %s", vol->prepath); 1110 cFYI(1, "prefix path %s", vol->prepath);
1067 } else { 1111 } else {
1068 printk(KERN_WARNING "CIFS: prefix too long\n"); 1112 printk(KERN_WARNING "CIFS: prefix too long\n");
1069 return 1; 1113 goto cifs_parse_mount_err;
1070 } 1114 }
1071 } else if (strnicmp(data, "iocharset", 9) == 0) { 1115 } else if (strnicmp(data, "iocharset", 9) == 0) {
1072 if (!value || !*value) { 1116 if (!value || !*value) {
1073 printk(KERN_WARNING "CIFS: invalid iocharset " 1117 printk(KERN_WARNING "CIFS: invalid iocharset "
1074 "specified\n"); 1118 "specified\n");
1075 return 1; /* needs_arg; */ 1119 goto cifs_parse_mount_err;
1076 } 1120 }
1077 if (strnlen(value, 65) < 65) { 1121 if (strnlen(value, 65) < 65) {
1078 if (strnicmp(value, "default", 7)) 1122 if (strnicmp(value, "default", 7)) {
1079 vol->iocharset = value; 1123 vol->iocharset = kstrdup(value,
1124 GFP_KERNEL);
1125
1126 if (!vol->iocharset) {
1127 printk(KERN_WARNING "CIFS: no "
1128 "memory for"
1129 "charset\n");
1130 goto cifs_parse_mount_err;
1131 }
1132 }
1080 /* if iocharset not set then load_nls_default 1133 /* if iocharset not set then load_nls_default
1081 is used by caller */ 1134 is used by caller */
1082 cFYI(1, "iocharset set to %s", value); 1135 cFYI(1, "iocharset set to %s", value);
1083 } else { 1136 } else {
1084 printk(KERN_WARNING "CIFS: iocharset name " 1137 printk(KERN_WARNING "CIFS: iocharset name "
1085 "too long.\n"); 1138 "too long.\n");
1086 return 1; 1139 goto cifs_parse_mount_err;
1087 } 1140 }
1088 } else if (!strnicmp(data, "uid", 3) && value && *value) { 1141 } else if (!strnicmp(data, "uid", 3) && value && *value) {
1089 vol->linux_uid = simple_strtoul(value, &value, 0); 1142 vol->linux_uid = simple_strtoul(value, &value, 0);
1090 uid_specified = true; 1143 uid_specified = true;
1144 } else if (!strnicmp(data, "cruid", 5) && value && *value) {
1145 vol->cred_uid = simple_strtoul(value, &value, 0);
1091 } else if (!strnicmp(data, "forceuid", 8)) { 1146 } else if (!strnicmp(data, "forceuid", 8)) {
1092 override_uid = 1; 1147 override_uid = 1;
1093 } else if (!strnicmp(data, "noforceuid", 10)) { 1148 } else if (!strnicmp(data, "noforceuid", 10)) {
@@ -1140,22 +1195,22 @@ cifs_parse_mount_options(char *options, const char *devname,
1140 if (!value || !*value || (*value == ' ')) { 1195 if (!value || !*value || (*value == ' ')) {
1141 cFYI(1, "invalid (empty) netbiosname"); 1196 cFYI(1, "invalid (empty) netbiosname");
1142 } else { 1197 } else {
1143 memset(vol->source_rfc1001_name, 0x20, 15); 1198 memset(vol->source_rfc1001_name, 0x20,
1144 for (i = 0; i < 15; i++) { 1199 RFC1001_NAME_LEN);
1145 /* BB are there cases in which a comma can be 1200 /*
1146 valid in this workstation netbios name (and need 1201 * FIXME: are there cases in which a comma can
1147 special handling)? */ 1202 * be valid in workstation netbios name (and
1148 1203 * need special handling)?
1149 /* We do not uppercase netbiosname for user */ 1204 */
1205 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1206 /* don't ucase netbiosname for user */
1150 if (value[i] == 0) 1207 if (value[i] == 0)
1151 break; 1208 break;
1152 else 1209 vol->source_rfc1001_name[i] = value[i];
1153 vol->source_rfc1001_name[i] =
1154 value[i];
1155 } 1210 }
1156 /* The string has 16th byte zero still from 1211 /* The string has 16th byte zero still from
1157 set at top of the function */ 1212 set at top of the function */
1158 if ((i == 15) && (value[i] != 0)) 1213 if (i == RFC1001_NAME_LEN && value[i] != 0)
1159 printk(KERN_WARNING "CIFS: netbiosname" 1214 printk(KERN_WARNING "CIFS: netbiosname"
1160 " longer than 15 truncated.\n"); 1215 " longer than 15 truncated.\n");
1161 } 1216 }
@@ -1165,7 +1220,8 @@ cifs_parse_mount_options(char *options, const char *devname,
1165 cFYI(1, "empty server netbiosname specified"); 1220 cFYI(1, "empty server netbiosname specified");
1166 } else { 1221 } else {
1167 /* last byte, type, is 0x20 for servr type */ 1222 /* last byte, type, is 0x20 for servr type */
1168 memset(vol->target_rfc1001_name, 0x20, 16); 1223 memset(vol->target_rfc1001_name, 0x20,
1224 RFC1001_NAME_LEN_WITH_NULL);
1169 1225
1170 for (i = 0; i < 15; i++) { 1226 for (i = 0; i < 15; i++) {
1171 /* BB are there cases in which a comma can be 1227 /* BB are there cases in which a comma can be
@@ -1182,10 +1238,20 @@ cifs_parse_mount_options(char *options, const char *devname,
1182 } 1238 }
1183 /* The string has 16th byte zero still from 1239 /* The string has 16th byte zero still from
1184 set at top of the function */ 1240 set at top of the function */
1185 if ((i == 15) && (value[i] != 0)) 1241 if (i == RFC1001_NAME_LEN && value[i] != 0)
1186 printk(KERN_WARNING "CIFS: server net" 1242 printk(KERN_WARNING "CIFS: server net"
1187 "biosname longer than 15 truncated.\n"); 1243 "biosname longer than 15 truncated.\n");
1188 } 1244 }
1245 } else if (strnicmp(data, "actimeo", 7) == 0) {
1246 if (value && *value) {
1247 vol->actimeo = HZ * simple_strtoul(value,
1248 &value, 0);
1249 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
1250 cERROR(1, "CIFS: attribute cache"
1251 "timeout too large");
1252 goto cifs_parse_mount_err;
1253 }
1254 }
1189 } else if (strnicmp(data, "credentials", 4) == 0) { 1255 } else if (strnicmp(data, "credentials", 4) == 0) {
1190 /* ignore */ 1256 /* ignore */
1191 } else if (strnicmp(data, "version", 3) == 0) { 1257 } else if (strnicmp(data, "version", 3) == 0) {
@@ -1295,6 +1361,8 @@ cifs_parse_mount_options(char *options, const char *devname,
1295 vol->server_ino = 1; 1361 vol->server_ino = 1;
1296 } else if (strnicmp(data, "noserverino", 9) == 0) { 1362 } else if (strnicmp(data, "noserverino", 9) == 0) {
1297 vol->server_ino = 0; 1363 vol->server_ino = 0;
1364 } else if (strnicmp(data, "rwpidforward", 4) == 0) {
1365 vol->rwpidforward = 1;
1298 } else if (strnicmp(data, "cifsacl", 7) == 0) { 1366 } else if (strnicmp(data, "cifsacl", 7) == 0) {
1299 vol->cifs_acl = 1; 1367 vol->cifs_acl = 1;
1300 } else if (strnicmp(data, "nocifsacl", 9) == 0) { 1368 } else if (strnicmp(data, "nocifsacl", 9) == 0) {
@@ -1303,10 +1371,8 @@ cifs_parse_mount_options(char *options, const char *devname,
1303 vol->no_psx_acl = 0; 1371 vol->no_psx_acl = 0;
1304 } else if (strnicmp(data, "noacl", 5) == 0) { 1372 } else if (strnicmp(data, "noacl", 5) == 0) {
1305 vol->no_psx_acl = 1; 1373 vol->no_psx_acl = 1;
1306#ifdef CONFIG_CIFS_EXPERIMENTAL
1307 } else if (strnicmp(data, "locallease", 6) == 0) { 1374 } else if (strnicmp(data, "locallease", 6) == 0) {
1308 vol->local_lease = 1; 1375 vol->local_lease = 1;
1309#endif
1310 } else if (strnicmp(data, "sign", 4) == 0) { 1376 } else if (strnicmp(data, "sign", 4) == 0) {
1311 vol->secFlg |= CIFSSEC_MUST_SIGN; 1377 vol->secFlg |= CIFSSEC_MUST_SIGN;
1312 } else if (strnicmp(data, "seal", 4) == 0) { 1378 } else if (strnicmp(data, "seal", 4) == 0) {
@@ -1319,12 +1385,23 @@ cifs_parse_mount_options(char *options, const char *devname,
1319 vol->direct_io = 1; 1385 vol->direct_io = 1;
1320 } else if (strnicmp(data, "forcedirectio", 13) == 0) { 1386 } else if (strnicmp(data, "forcedirectio", 13) == 0) {
1321 vol->direct_io = 1; 1387 vol->direct_io = 1;
1388 } else if (strnicmp(data, "strictcache", 11) == 0) {
1389 vol->strict_io = 1;
1322 } else if (strnicmp(data, "noac", 4) == 0) { 1390 } else if (strnicmp(data, "noac", 4) == 0) {
1323 printk(KERN_WARNING "CIFS: Mount option noac not " 1391 printk(KERN_WARNING "CIFS: Mount option noac not "
1324 "supported. Instead set " 1392 "supported. Instead set "
1325 "/proc/fs/cifs/LookupCacheEnabled to 0\n"); 1393 "/proc/fs/cifs/LookupCacheEnabled to 0\n");
1326 } else if (strnicmp(data, "fsc", 3) == 0) { 1394 } else if (strnicmp(data, "fsc", 3) == 0) {
1395#ifndef CONFIG_CIFS_FSCACHE
1396 cERROR(1, "FS-Cache support needs CONFIG_CIFS_FSCACHE "
1397 "kernel config option set");
1398 goto cifs_parse_mount_err;
1399#endif
1327 vol->fsc = true; 1400 vol->fsc = true;
1401 } else if (strnicmp(data, "mfsymlinks", 10) == 0) {
1402 vol->mfsymlinks = true;
1403 } else if (strnicmp(data, "multiuser", 8) == 0) {
1404 vol->multiuser = true;
1328 } else 1405 } else
1329 printk(KERN_WARNING "CIFS: Unknown mount option %s\n", 1406 printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
1330 data); 1407 data);
@@ -1333,12 +1410,12 @@ cifs_parse_mount_options(char *options, const char *devname,
1333 if (devname == NULL) { 1410 if (devname == NULL) {
1334 printk(KERN_WARNING "CIFS: Missing UNC name for mount " 1411 printk(KERN_WARNING "CIFS: Missing UNC name for mount "
1335 "target\n"); 1412 "target\n");
1336 return 1; 1413 goto cifs_parse_mount_err;
1337 } 1414 }
1338 if ((temp_len = strnlen(devname, 300)) < 300) { 1415 if ((temp_len = strnlen(devname, 300)) < 300) {
1339 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL); 1416 vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
1340 if (vol->UNC == NULL) 1417 if (vol->UNC == NULL)
1341 return 1; 1418 goto cifs_parse_mount_err;
1342 strcpy(vol->UNC, devname); 1419 strcpy(vol->UNC, devname);
1343 if (strncmp(vol->UNC, "//", 2) == 0) { 1420 if (strncmp(vol->UNC, "//", 2) == 0) {
1344 vol->UNC[0] = '\\'; 1421 vol->UNC[0] = '\\';
@@ -1346,16 +1423,23 @@ cifs_parse_mount_options(char *options, const char *devname,
1346 } else if (strncmp(vol->UNC, "\\\\", 2) != 0) { 1423 } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
1347 printk(KERN_WARNING "CIFS: UNC Path does not " 1424 printk(KERN_WARNING "CIFS: UNC Path does not "
1348 "begin with // or \\\\ \n"); 1425 "begin with // or \\\\ \n");
1349 return 1; 1426 goto cifs_parse_mount_err;
1350 } 1427 }
1351 value = strpbrk(vol->UNC+2, "/\\"); 1428 value = strpbrk(vol->UNC+2, "/\\");
1352 if (value) 1429 if (value)
1353 *value = '\\'; 1430 *value = '\\';
1354 } else { 1431 } else {
1355 printk(KERN_WARNING "CIFS: UNC name too long\n"); 1432 printk(KERN_WARNING "CIFS: UNC name too long\n");
1356 return 1; 1433 goto cifs_parse_mount_err;
1357 } 1434 }
1358 } 1435 }
1436
1437 if (vol->multiuser && !(vol->secFlg & CIFSSEC_MAY_KRB5)) {
1438 cERROR(1, "Multiuser mounts currently require krb5 "
1439 "authentication!");
1440 goto cifs_parse_mount_err;
1441 }
1442
1359 if (vol->UNCip == NULL) 1443 if (vol->UNCip == NULL)
1360 vol->UNCip = &vol->UNC[2]; 1444 vol->UNCip = &vol->UNC[2];
1361 1445
@@ -1371,36 +1455,108 @@ cifs_parse_mount_options(char *options, const char *devname,
1371 printk(KERN_NOTICE "CIFS: ignoring forcegid mount option " 1455 printk(KERN_NOTICE "CIFS: ignoring forcegid mount option "
1372 "specified with no gid= option.\n"); 1456 "specified with no gid= option.\n");
1373 1457
1458 kfree(mountdata_copy);
1374 return 0; 1459 return 0;
1460
1461cifs_parse_mount_err:
1462 kfree(mountdata_copy);
1463 return 1;
1375} 1464}
1376 1465
1466/** Returns true if srcaddr isn't specified and rhs isn't
1467 * specified, or if srcaddr is specified and
1468 * matches the IP address of the rhs argument.
1469 */
1377static bool 1470static bool
1378match_address(struct TCP_Server_Info *server, struct sockaddr *addr) 1471srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
1379{ 1472{
1380 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr; 1473 switch (srcaddr->sa_family) {
1381 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr; 1474 case AF_UNSPEC:
1475 return (rhs->sa_family == AF_UNSPEC);
1476 case AF_INET: {
1477 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
1478 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
1479 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
1480 }
1481 case AF_INET6: {
1482 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
1483 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)&rhs;
1484 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
1485 }
1486 default:
1487 WARN_ON(1);
1488 return false; /* don't expect to be here */
1489 }
1490}
1491
1492/*
1493 * If no port is specified in addr structure, we try to match with 445 port
1494 * and if it fails - with 139 ports. It should be called only if address
1495 * families of server and addr are equal.
1496 */
1497static bool
1498match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
1499{
1500 __be16 port, *sport;
1382 1501
1383 switch (addr->sa_family) { 1502 switch (addr->sa_family) {
1384 case AF_INET: 1503 case AF_INET:
1385 if (addr4->sin_addr.s_addr != 1504 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
1386 server->addr.sockAddr.sin_addr.s_addr) 1505 port = ((struct sockaddr_in *) addr)->sin_port;
1387 return false;
1388 if (addr4->sin_port &&
1389 addr4->sin_port != server->addr.sockAddr.sin_port)
1390 return false;
1391 break; 1506 break;
1392 case AF_INET6: 1507 case AF_INET6:
1393 if (!ipv6_addr_equal(&addr6->sin6_addr, 1508 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
1394 &server->addr.sockAddr6.sin6_addr)) 1509 port = ((struct sockaddr_in6 *) addr)->sin6_port;
1510 break;
1511 default:
1512 WARN_ON(1);
1513 return false;
1514 }
1515
1516 if (!port) {
1517 port = htons(CIFS_PORT);
1518 if (port == *sport)
1519 return true;
1520
1521 port = htons(RFC1001_PORT);
1522 }
1523
1524 return port == *sport;
1525}
1526
1527static bool
1528match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
1529 struct sockaddr *srcaddr)
1530{
1531 switch (addr->sa_family) {
1532 case AF_INET: {
1533 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
1534 struct sockaddr_in *srv_addr4 =
1535 (struct sockaddr_in *)&server->dstaddr;
1536
1537 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
1395 return false; 1538 return false;
1396 if (addr6->sin6_scope_id != 1539 break;
1397 server->addr.sockAddr6.sin6_scope_id) 1540 }
1541 case AF_INET6: {
1542 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
1543 struct sockaddr_in6 *srv_addr6 =
1544 (struct sockaddr_in6 *)&server->dstaddr;
1545
1546 if (!ipv6_addr_equal(&addr6->sin6_addr,
1547 &srv_addr6->sin6_addr))
1398 return false; 1548 return false;
1399 if (addr6->sin6_port && 1549 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
1400 addr6->sin6_port != server->addr.sockAddr6.sin6_port)
1401 return false; 1550 return false;
1402 break; 1551 break;
1403 } 1552 }
1553 default:
1554 WARN_ON(1);
1555 return false; /* don't expect to be here */
1556 }
1557
1558 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
1559 return false;
1404 1560
1405 return true; 1561 return true;
1406} 1562}
@@ -1441,46 +1597,53 @@ match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
1441 return false; 1597 return false;
1442 } 1598 }
1443 1599
1444 /* now check if signing mode is acceptible */ 1600 /* now check if signing mode is acceptable */
1445 if ((secFlags & CIFSSEC_MAY_SIGN) == 0 && 1601 if ((secFlags & CIFSSEC_MAY_SIGN) == 0 &&
1446 (server->secMode & SECMODE_SIGN_REQUIRED)) 1602 (server->sec_mode & SECMODE_SIGN_REQUIRED))
1447 return false; 1603 return false;
1448 else if (((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) && 1604 else if (((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) &&
1449 (server->secMode & 1605 (server->sec_mode &
1450 (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED)) == 0) 1606 (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED)) == 0)
1451 return false; 1607 return false;
1452 1608
1453 return true; 1609 return true;
1454} 1610}
1455 1611
1612static int match_server(struct TCP_Server_Info *server, struct sockaddr *addr,
1613 struct smb_vol *vol)
1614{
1615 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
1616 return 0;
1617
1618 if (!match_address(server, addr,
1619 (struct sockaddr *)&vol->srcaddr))
1620 return 0;
1621
1622 if (!match_port(server, addr))
1623 return 0;
1624
1625 if (!match_security(server, vol))
1626 return 0;
1627
1628 return 1;
1629}
1630
1456static struct TCP_Server_Info * 1631static struct TCP_Server_Info *
1457cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol) 1632cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol)
1458{ 1633{
1459 struct TCP_Server_Info *server; 1634 struct TCP_Server_Info *server;
1460 1635
1461 write_lock(&cifs_tcp_ses_lock); 1636 spin_lock(&cifs_tcp_ses_lock);
1462 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) { 1637 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
1463 /* 1638 if (!match_server(server, addr, vol))
1464 * the demux thread can exit on its own while still in CifsNew
1465 * so don't accept any sockets in that state. Since the
1466 * tcpStatus never changes back to CifsNew it's safe to check
1467 * for this without a lock.
1468 */
1469 if (server->tcpStatus == CifsNew)
1470 continue;
1471
1472 if (!match_address(server, addr))
1473 continue;
1474
1475 if (!match_security(server, vol))
1476 continue; 1639 continue;
1477 1640
1478 ++server->srv_count; 1641 ++server->srv_count;
1479 write_unlock(&cifs_tcp_ses_lock); 1642 spin_unlock(&cifs_tcp_ses_lock);
1480 cFYI(1, "Existing tcp session with server found"); 1643 cFYI(1, "Existing tcp session with server found");
1481 return server; 1644 return server;
1482 } 1645 }
1483 write_unlock(&cifs_tcp_ses_lock); 1646 spin_unlock(&cifs_tcp_ses_lock);
1484 return NULL; 1647 return NULL;
1485} 1648}
1486 1649
@@ -1489,21 +1652,30 @@ cifs_put_tcp_session(struct TCP_Server_Info *server)
1489{ 1652{
1490 struct task_struct *task; 1653 struct task_struct *task;
1491 1654
1492 write_lock(&cifs_tcp_ses_lock); 1655 spin_lock(&cifs_tcp_ses_lock);
1493 if (--server->srv_count > 0) { 1656 if (--server->srv_count > 0) {
1494 write_unlock(&cifs_tcp_ses_lock); 1657 spin_unlock(&cifs_tcp_ses_lock);
1495 return; 1658 return;
1496 } 1659 }
1497 1660
1661 put_net(cifs_net_ns(server));
1662
1498 list_del_init(&server->tcp_ses_list); 1663 list_del_init(&server->tcp_ses_list);
1499 write_unlock(&cifs_tcp_ses_lock); 1664 spin_unlock(&cifs_tcp_ses_lock);
1665
1666 cancel_delayed_work_sync(&server->echo);
1500 1667
1501 spin_lock(&GlobalMid_Lock); 1668 spin_lock(&GlobalMid_Lock);
1502 server->tcpStatus = CifsExiting; 1669 server->tcpStatus = CifsExiting;
1503 spin_unlock(&GlobalMid_Lock); 1670 spin_unlock(&GlobalMid_Lock);
1504 1671
1672 cifs_crypto_shash_release(server);
1505 cifs_fscache_release_client_cookie(server); 1673 cifs_fscache_release_client_cookie(server);
1506 1674
1675 kfree(server->session_key.response);
1676 server->session_key.response = NULL;
1677 server->session_key.len = 0;
1678
1507 task = xchg(&server->tsk, NULL); 1679 task = xchg(&server->tsk, NULL);
1508 if (task) 1680 if (task)
1509 force_sig(SIGKILL, task); 1681 force_sig(SIGKILL, task);
@@ -1556,10 +1728,17 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
1556 goto out_err; 1728 goto out_err;
1557 } 1729 }
1558 1730
1731 rc = cifs_crypto_shash_allocate(tcp_ses);
1732 if (rc) {
1733 cERROR(1, "could not setup hash structures rc %d", rc);
1734 goto out_err;
1735 }
1736
1737 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
1559 tcp_ses->hostname = extract_hostname(volume_info->UNC); 1738 tcp_ses->hostname = extract_hostname(volume_info->UNC);
1560 if (IS_ERR(tcp_ses->hostname)) { 1739 if (IS_ERR(tcp_ses->hostname)) {
1561 rc = PTR_ERR(tcp_ses->hostname); 1740 rc = PTR_ERR(tcp_ses->hostname);
1562 goto out_err; 1741 goto out_err_crypto_release;
1563 } 1742 }
1564 1743
1565 tcp_ses->noblocksnd = volume_info->noblocksnd; 1744 tcp_ses->noblocksnd = volume_info->noblocksnd;
@@ -1574,9 +1753,12 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
1574 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); 1753 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1575 memcpy(tcp_ses->server_RFC1001_name, 1754 memcpy(tcp_ses->server_RFC1001_name,
1576 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL); 1755 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
1756 tcp_ses->session_estab = false;
1577 tcp_ses->sequence_number = 0; 1757 tcp_ses->sequence_number = 0;
1758 tcp_ses->lstrp = jiffies;
1578 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list); 1759 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
1579 INIT_LIST_HEAD(&tcp_ses->smb_ses_list); 1760 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
1761 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
1580 1762
1581 /* 1763 /*
1582 * at this point we are the only ones with the pointer 1764 * at this point we are the only ones with the pointer
@@ -1584,23 +1766,24 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
1584 * no need to spinlock this init of tcpStatus or srv_count 1766 * no need to spinlock this init of tcpStatus or srv_count
1585 */ 1767 */
1586 tcp_ses->tcpStatus = CifsNew; 1768 tcp_ses->tcpStatus = CifsNew;
1769 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
1770 sizeof(tcp_ses->srcaddr));
1587 ++tcp_ses->srv_count; 1771 ++tcp_ses->srv_count;
1588 1772
1589 if (addr.ss_family == AF_INET6) { 1773 if (addr.ss_family == AF_INET6) {
1590 cFYI(1, "attempting ipv6 connect"); 1774 cFYI(1, "attempting ipv6 connect");
1591 /* BB should we allow ipv6 on port 139? */ 1775 /* BB should we allow ipv6 on port 139? */
1592 /* other OS never observed in Wild doing 139 with v6 */ 1776 /* other OS never observed in Wild doing 139 with v6 */
1593 memcpy(&tcp_ses->addr.sockAddr6, sin_server6, 1777 memcpy(&tcp_ses->dstaddr, sin_server6,
1594 sizeof(struct sockaddr_in6)); 1778 sizeof(struct sockaddr_in6));
1595 rc = ipv6_connect(tcp_ses); 1779 } else
1596 } else { 1780 memcpy(&tcp_ses->dstaddr, sin_server,
1597 memcpy(&tcp_ses->addr.sockAddr, sin_server, 1781 sizeof(struct sockaddr_in));
1598 sizeof(struct sockaddr_in)); 1782
1599 rc = ipv4_connect(tcp_ses); 1783 rc = ip_connect(tcp_ses);
1600 }
1601 if (rc < 0) { 1784 if (rc < 0) {
1602 cERROR(1, "Error connecting to socket. Aborting operation"); 1785 cERROR(1, "Error connecting to socket. Aborting operation");
1603 goto out_err; 1786 goto out_err_crypto_release;
1604 } 1787 }
1605 1788
1606 /* 1789 /*
@@ -1614,18 +1797,27 @@ cifs_get_tcp_session(struct smb_vol *volume_info)
1614 rc = PTR_ERR(tcp_ses->tsk); 1797 rc = PTR_ERR(tcp_ses->tsk);
1615 cERROR(1, "error %d create cifsd thread", rc); 1798 cERROR(1, "error %d create cifsd thread", rc);
1616 module_put(THIS_MODULE); 1799 module_put(THIS_MODULE);
1617 goto out_err; 1800 goto out_err_crypto_release;
1618 } 1801 }
1802 tcp_ses->tcpStatus = CifsNeedNegotiate;
1619 1803
1620 /* thread spawned, put it on the list */ 1804 /* thread spawned, put it on the list */
1621 write_lock(&cifs_tcp_ses_lock); 1805 spin_lock(&cifs_tcp_ses_lock);
1622 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list); 1806 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
1623 write_unlock(&cifs_tcp_ses_lock); 1807 spin_unlock(&cifs_tcp_ses_lock);
1624 1808
1625 cifs_fscache_get_client_cookie(tcp_ses); 1809 cifs_fscache_get_client_cookie(tcp_ses);
1626 1810
1811 /* queue echo request delayed work */
1812 queue_delayed_work(system_nrt_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
1813
1627 return tcp_ses; 1814 return tcp_ses;
1628 1815
1816out_err_crypto_release:
1817 cifs_crypto_shash_release(tcp_ses);
1818
1819 put_net(cifs_net_ns(tcp_ses));
1820
1629out_err: 1821out_err:
1630 if (tcp_ses) { 1822 if (tcp_ses) {
1631 if (!IS_ERR(tcp_ses->hostname)) 1823 if (!IS_ERR(tcp_ses->hostname))
@@ -1637,53 +1829,62 @@ out_err:
1637 return ERR_PTR(rc); 1829 return ERR_PTR(rc);
1638} 1830}
1639 1831
1640static struct cifsSesInfo * 1832static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
1833{
1834 switch (ses->server->secType) {
1835 case Kerberos:
1836 if (vol->cred_uid != ses->cred_uid)
1837 return 0;
1838 break;
1839 default:
1840 /* anything else takes username/password */
1841 if (ses->user_name == NULL)
1842 return 0;
1843 if (strncmp(ses->user_name, vol->username,
1844 MAX_USERNAME_SIZE))
1845 return 0;
1846 if (strlen(vol->username) != 0 &&
1847 ses->password != NULL &&
1848 strncmp(ses->password,
1849 vol->password ? vol->password : "",
1850 MAX_PASSWORD_SIZE))
1851 return 0;
1852 }
1853 return 1;
1854}
1855
1856static struct cifs_ses *
1641cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol) 1857cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
1642{ 1858{
1643 struct cifsSesInfo *ses; 1859 struct cifs_ses *ses;
1644 1860
1645 write_lock(&cifs_tcp_ses_lock); 1861 spin_lock(&cifs_tcp_ses_lock);
1646 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) { 1862 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1647 switch (server->secType) { 1863 if (!match_session(ses, vol))
1648 case Kerberos: 1864 continue;
1649 if (vol->cred_uid != ses->cred_uid)
1650 continue;
1651 break;
1652 default:
1653 /* anything else takes username/password */
1654 if (strncmp(ses->userName, vol->username,
1655 MAX_USERNAME_SIZE))
1656 continue;
1657 if (strlen(vol->username) != 0 &&
1658 ses->password != NULL &&
1659 strncmp(ses->password,
1660 vol->password ? vol->password : "",
1661 MAX_PASSWORD_SIZE))
1662 continue;
1663 }
1664 ++ses->ses_count; 1865 ++ses->ses_count;
1665 write_unlock(&cifs_tcp_ses_lock); 1866 spin_unlock(&cifs_tcp_ses_lock);
1666 return ses; 1867 return ses;
1667 } 1868 }
1668 write_unlock(&cifs_tcp_ses_lock); 1869 spin_unlock(&cifs_tcp_ses_lock);
1669 return NULL; 1870 return NULL;
1670} 1871}
1671 1872
1672static void 1873static void
1673cifs_put_smb_ses(struct cifsSesInfo *ses) 1874cifs_put_smb_ses(struct cifs_ses *ses)
1674{ 1875{
1675 int xid; 1876 int xid;
1676 struct TCP_Server_Info *server = ses->server; 1877 struct TCP_Server_Info *server = ses->server;
1677 1878
1678 cFYI(1, "%s: ses_count=%d\n", __func__, ses->ses_count); 1879 cFYI(1, "%s: ses_count=%d\n", __func__, ses->ses_count);
1679 write_lock(&cifs_tcp_ses_lock); 1880 spin_lock(&cifs_tcp_ses_lock);
1680 if (--ses->ses_count > 0) { 1881 if (--ses->ses_count > 0) {
1681 write_unlock(&cifs_tcp_ses_lock); 1882 spin_unlock(&cifs_tcp_ses_lock);
1682 return; 1883 return;
1683 } 1884 }
1684 1885
1685 list_del_init(&ses->smb_ses_list); 1886 list_del_init(&ses->smb_ses_list);
1686 write_unlock(&cifs_tcp_ses_lock); 1887 spin_unlock(&cifs_tcp_ses_lock);
1687 1888
1688 if (ses->status == CifsGood) { 1889 if (ses->status == CifsGood) {
1689 xid = GetXid(); 1890 xid = GetXid();
@@ -1694,11 +1895,15 @@ cifs_put_smb_ses(struct cifsSesInfo *ses)
1694 cifs_put_tcp_session(server); 1895 cifs_put_tcp_session(server);
1695} 1896}
1696 1897
1697static struct cifsSesInfo * 1898static bool warned_on_ntlm; /* globals init to false automatically */
1899
1900static struct cifs_ses *
1698cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info) 1901cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
1699{ 1902{
1700 int rc = -ENOMEM, xid; 1903 int rc = -ENOMEM, xid;
1701 struct cifsSesInfo *ses; 1904 struct cifs_ses *ses;
1905 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
1906 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
1702 1907
1703 xid = GetXid(); 1908 xid = GetXid();
1704 1909
@@ -1742,16 +1947,16 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
1742 1947
1743 /* new SMB session uses our server ref */ 1948 /* new SMB session uses our server ref */
1744 ses->server = server; 1949 ses->server = server;
1745 if (server->addr.sockAddr6.sin6_family == AF_INET6) 1950 if (server->dstaddr.ss_family == AF_INET6)
1746 sprintf(ses->serverName, "%pI6", 1951 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
1747 &server->addr.sockAddr6.sin6_addr);
1748 else 1952 else
1749 sprintf(ses->serverName, "%pI4", 1953 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
1750 &server->addr.sockAddr.sin_addr.s_addr);
1751 1954
1752 if (volume_info->username) 1955 if (volume_info->username) {
1753 strncpy(ses->userName, volume_info->username, 1956 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
1754 MAX_USERNAME_SIZE); 1957 if (!ses->user_name)
1958 goto get_ses_fail;
1959 }
1755 1960
1756 /* volume_info->password freed at unmount */ 1961 /* volume_info->password freed at unmount */
1757 if (volume_info->password) { 1962 if (volume_info->password) {
@@ -1760,13 +1965,21 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
1760 goto get_ses_fail; 1965 goto get_ses_fail;
1761 } 1966 }
1762 if (volume_info->domainname) { 1967 if (volume_info->domainname) {
1763 int len = strlen(volume_info->domainname); 1968 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
1764 ses->domainName = kmalloc(len + 1, GFP_KERNEL); 1969 if (!ses->domainName)
1765 if (ses->domainName) 1970 goto get_ses_fail;
1766 strcpy(ses->domainName, volume_info->domainname);
1767 } 1971 }
1768 ses->cred_uid = volume_info->cred_uid; 1972 ses->cred_uid = volume_info->cred_uid;
1769 ses->linux_uid = volume_info->linux_uid; 1973 ses->linux_uid = volume_info->linux_uid;
1974
1975 /* ntlmv2 is much stronger than ntlm security, and has been broadly
1976 supported for many years, time to update default security mechanism */
1977 if ((volume_info->secFlg == 0) && warned_on_ntlm == false) {
1978 warned_on_ntlm = true;
1979 cERROR(1, "default security mechanism requested. The default "
1980 "security mechanism will be upgraded from ntlm to "
1981 "ntlmv2 in kernel release 3.1");
1982 }
1770 ses->overrideSecFlg = volume_info->secFlg; 1983 ses->overrideSecFlg = volume_info->secFlg;
1771 1984
1772 mutex_lock(&ses->session_mutex); 1985 mutex_lock(&ses->session_mutex);
@@ -1778,9 +1991,9 @@ cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
1778 goto get_ses_fail; 1991 goto get_ses_fail;
1779 1992
1780 /* success, put it on the list */ 1993 /* success, put it on the list */
1781 write_lock(&cifs_tcp_ses_lock); 1994 spin_lock(&cifs_tcp_ses_lock);
1782 list_add(&ses->smb_ses_list, &server->smb_ses_list); 1995 list_add(&ses->smb_ses_list, &server->smb_ses_list);
1783 write_unlock(&cifs_tcp_ses_lock); 1996 spin_unlock(&cifs_tcp_ses_lock);
1784 1997
1785 FreeXid(xid); 1998 FreeXid(xid);
1786 return ses; 1999 return ses;
@@ -1791,43 +2004,49 @@ get_ses_fail:
1791 return ERR_PTR(rc); 2004 return ERR_PTR(rc);
1792} 2005}
1793 2006
1794static struct cifsTconInfo * 2007static int match_tcon(struct cifs_tcon *tcon, const char *unc)
1795cifs_find_tcon(struct cifsSesInfo *ses, const char *unc) 2008{
2009 if (tcon->tidStatus == CifsExiting)
2010 return 0;
2011 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
2012 return 0;
2013 return 1;
2014}
2015
2016static struct cifs_tcon *
2017cifs_find_tcon(struct cifs_ses *ses, const char *unc)
1796{ 2018{
1797 struct list_head *tmp; 2019 struct list_head *tmp;
1798 struct cifsTconInfo *tcon; 2020 struct cifs_tcon *tcon;
1799 2021
1800 write_lock(&cifs_tcp_ses_lock); 2022 spin_lock(&cifs_tcp_ses_lock);
1801 list_for_each(tmp, &ses->tcon_list) { 2023 list_for_each(tmp, &ses->tcon_list) {
1802 tcon = list_entry(tmp, struct cifsTconInfo, tcon_list); 2024 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
1803 if (tcon->tidStatus == CifsExiting) 2025 if (!match_tcon(tcon, unc))
1804 continue;
1805 if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
1806 continue; 2026 continue;
1807
1808 ++tcon->tc_count; 2027 ++tcon->tc_count;
1809 write_unlock(&cifs_tcp_ses_lock); 2028 spin_unlock(&cifs_tcp_ses_lock);
1810 return tcon; 2029 return tcon;
1811 } 2030 }
1812 write_unlock(&cifs_tcp_ses_lock); 2031 spin_unlock(&cifs_tcp_ses_lock);
1813 return NULL; 2032 return NULL;
1814} 2033}
1815 2034
1816static void 2035static void
1817cifs_put_tcon(struct cifsTconInfo *tcon) 2036cifs_put_tcon(struct cifs_tcon *tcon)
1818{ 2037{
1819 int xid; 2038 int xid;
1820 struct cifsSesInfo *ses = tcon->ses; 2039 struct cifs_ses *ses = tcon->ses;
1821 2040
1822 cFYI(1, "%s: tc_count=%d\n", __func__, tcon->tc_count); 2041 cFYI(1, "%s: tc_count=%d\n", __func__, tcon->tc_count);
1823 write_lock(&cifs_tcp_ses_lock); 2042 spin_lock(&cifs_tcp_ses_lock);
1824 if (--tcon->tc_count > 0) { 2043 if (--tcon->tc_count > 0) {
1825 write_unlock(&cifs_tcp_ses_lock); 2044 spin_unlock(&cifs_tcp_ses_lock);
1826 return; 2045 return;
1827 } 2046 }
1828 2047
1829 list_del_init(&tcon->tcon_list); 2048 list_del_init(&tcon->tcon_list);
1830 write_unlock(&cifs_tcp_ses_lock); 2049 spin_unlock(&cifs_tcp_ses_lock);
1831 2050
1832 xid = GetXid(); 2051 xid = GetXid();
1833 CIFSSMBTDis(xid, tcon); 2052 CIFSSMBTDis(xid, tcon);
@@ -1838,11 +2057,11 @@ cifs_put_tcon(struct cifsTconInfo *tcon)
1838 cifs_put_smb_ses(ses); 2057 cifs_put_smb_ses(ses);
1839} 2058}
1840 2059
1841static struct cifsTconInfo * 2060static struct cifs_tcon *
1842cifs_get_tcon(struct cifsSesInfo *ses, struct smb_vol *volume_info) 2061cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
1843{ 2062{
1844 int rc, xid; 2063 int rc, xid;
1845 struct cifsTconInfo *tcon; 2064 struct cifs_tcon *tcon;
1846 2065
1847 tcon = cifs_find_tcon(ses, volume_info->UNC); 2066 tcon = cifs_find_tcon(ses, volume_info->UNC);
1848 if (tcon) { 2067 if (tcon) {
@@ -1900,9 +2119,9 @@ cifs_get_tcon(struct cifsSesInfo *ses, struct smb_vol *volume_info)
1900 tcon->nocase = volume_info->nocase; 2119 tcon->nocase = volume_info->nocase;
1901 tcon->local_lease = volume_info->local_lease; 2120 tcon->local_lease = volume_info->local_lease;
1902 2121
1903 write_lock(&cifs_tcp_ses_lock); 2122 spin_lock(&cifs_tcp_ses_lock);
1904 list_add(&tcon->tcon_list, &ses->tcon_list); 2123 list_add(&tcon->tcon_list, &ses->tcon_list);
1905 write_unlock(&cifs_tcp_ses_lock); 2124 spin_unlock(&cifs_tcp_ses_lock);
1906 2125
1907 cifs_fscache_get_super_cookie(tcon); 2126 cifs_fscache_get_super_cookie(tcon);
1908 2127
@@ -1913,9 +2132,123 @@ out_fail:
1913 return ERR_PTR(rc); 2132 return ERR_PTR(rc);
1914} 2133}
1915 2134
2135void
2136cifs_put_tlink(struct tcon_link *tlink)
2137{
2138 if (!tlink || IS_ERR(tlink))
2139 return;
2140
2141 if (!atomic_dec_and_test(&tlink->tl_count) ||
2142 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
2143 tlink->tl_time = jiffies;
2144 return;
2145 }
2146
2147 if (!IS_ERR(tlink_tcon(tlink)))
2148 cifs_put_tcon(tlink_tcon(tlink));
2149 kfree(tlink);
2150 return;
2151}
2152
2153static inline struct tcon_link *
2154cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
2155{
2156 return cifs_sb->master_tlink;
2157}
2158
2159static int
2160compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
2161{
2162 struct cifs_sb_info *old = CIFS_SB(sb);
2163 struct cifs_sb_info *new = mnt_data->cifs_sb;
2164
2165 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
2166 return 0;
2167
2168 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
2169 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
2170 return 0;
2171
2172 if (old->rsize != new->rsize)
2173 return 0;
2174
2175 /*
2176 * We want to share sb only if we don't specify wsize or specified wsize
2177 * is greater or equal than existing one.
2178 */
2179 if (new->wsize && new->wsize < old->wsize)
2180 return 0;
2181
2182 if (old->mnt_uid != new->mnt_uid || old->mnt_gid != new->mnt_gid)
2183 return 0;
2184
2185 if (old->mnt_file_mode != new->mnt_file_mode ||
2186 old->mnt_dir_mode != new->mnt_dir_mode)
2187 return 0;
2188
2189 if (strcmp(old->local_nls->charset, new->local_nls->charset))
2190 return 0;
2191
2192 if (old->actimeo != new->actimeo)
2193 return 0;
2194
2195 return 1;
2196}
2197
2198int
2199cifs_match_super(struct super_block *sb, void *data)
2200{
2201 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
2202 struct smb_vol *volume_info;
2203 struct cifs_sb_info *cifs_sb;
2204 struct TCP_Server_Info *tcp_srv;
2205 struct cifs_ses *ses;
2206 struct cifs_tcon *tcon;
2207 struct tcon_link *tlink;
2208 struct sockaddr_storage addr;
2209 int rc = 0;
2210
2211 memset(&addr, 0, sizeof(struct sockaddr_storage));
2212
2213 spin_lock(&cifs_tcp_ses_lock);
2214 cifs_sb = CIFS_SB(sb);
2215 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
2216 if (IS_ERR(tlink)) {
2217 spin_unlock(&cifs_tcp_ses_lock);
2218 return rc;
2219 }
2220 tcon = tlink_tcon(tlink);
2221 ses = tcon->ses;
2222 tcp_srv = ses->server;
2223
2224 volume_info = mnt_data->vol;
2225
2226 if (!volume_info->UNCip || !volume_info->UNC)
2227 goto out;
2228
2229 rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
2230 volume_info->UNCip,
2231 strlen(volume_info->UNCip),
2232 volume_info->port);
2233 if (!rc)
2234 goto out;
2235
2236 if (!match_server(tcp_srv, (struct sockaddr *)&addr, volume_info) ||
2237 !match_session(ses, volume_info) ||
2238 !match_tcon(tcon, volume_info->UNC)) {
2239 rc = 0;
2240 goto out;
2241 }
2242
2243 rc = compare_mount_options(sb, mnt_data);
2244out:
2245 spin_unlock(&cifs_tcp_ses_lock);
2246 cifs_put_tlink(tlink);
2247 return rc;
2248}
1916 2249
1917int 2250int
1918get_dfs_path(int xid, struct cifsSesInfo *pSesInfo, const char *old_path, 2251get_dfs_path(int xid, struct cifs_ses *pSesInfo, const char *old_path,
1919 const struct nls_table *nls_codepage, unsigned int *pnum_referrals, 2252 const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
1920 struct dfs_info3_param **preferrals, int remap) 2253 struct dfs_info3_param **preferrals, int remap)
1921{ 2254{
@@ -1997,21 +2330,135 @@ static void rfc1002mangle(char *target, char *source, unsigned int length)
1997 2330
1998} 2331}
1999 2332
2333static int
2334bind_socket(struct TCP_Server_Info *server)
2335{
2336 int rc = 0;
2337 if (server->srcaddr.ss_family != AF_UNSPEC) {
2338 /* Bind to the specified local IP address */
2339 struct socket *socket = server->ssocket;
2340 rc = socket->ops->bind(socket,
2341 (struct sockaddr *) &server->srcaddr,
2342 sizeof(server->srcaddr));
2343 if (rc < 0) {
2344 struct sockaddr_in *saddr4;
2345 struct sockaddr_in6 *saddr6;
2346 saddr4 = (struct sockaddr_in *)&server->srcaddr;
2347 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
2348 if (saddr6->sin6_family == AF_INET6)
2349 cERROR(1, "cifs: "
2350 "Failed to bind to: %pI6c, error: %d\n",
2351 &saddr6->sin6_addr, rc);
2352 else
2353 cERROR(1, "cifs: "
2354 "Failed to bind to: %pI4, error: %d\n",
2355 &saddr4->sin_addr.s_addr, rc);
2356 }
2357 }
2358 return rc;
2359}
2360
2361static int
2362ip_rfc1001_connect(struct TCP_Server_Info *server)
2363{
2364 int rc = 0;
2365 /*
2366 * some servers require RFC1001 sessinit before sending
2367 * negprot - BB check reconnection in case where second
2368 * sessinit is sent but no second negprot
2369 */
2370 struct rfc1002_session_packet *ses_init_buf;
2371 struct smb_hdr *smb_buf;
2372 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2373 GFP_KERNEL);
2374 if (ses_init_buf) {
2375 ses_init_buf->trailer.session_req.called_len = 32;
2376
2377 if (server->server_RFC1001_name &&
2378 server->server_RFC1001_name[0] != 0)
2379 rfc1002mangle(ses_init_buf->trailer.
2380 session_req.called_name,
2381 server->server_RFC1001_name,
2382 RFC1001_NAME_LEN_WITH_NULL);
2383 else
2384 rfc1002mangle(ses_init_buf->trailer.
2385 session_req.called_name,
2386 DEFAULT_CIFS_CALLED_NAME,
2387 RFC1001_NAME_LEN_WITH_NULL);
2388
2389 ses_init_buf->trailer.session_req.calling_len = 32;
2390
2391 /*
2392 * calling name ends in null (byte 16) from old smb
2393 * convention.
2394 */
2395 if (server->workstation_RFC1001_name &&
2396 server->workstation_RFC1001_name[0] != 0)
2397 rfc1002mangle(ses_init_buf->trailer.
2398 session_req.calling_name,
2399 server->workstation_RFC1001_name,
2400 RFC1001_NAME_LEN_WITH_NULL);
2401 else
2402 rfc1002mangle(ses_init_buf->trailer.
2403 session_req.calling_name,
2404 "LINUX_CIFS_CLNT",
2405 RFC1001_NAME_LEN_WITH_NULL);
2406
2407 ses_init_buf->trailer.session_req.scope1 = 0;
2408 ses_init_buf->trailer.session_req.scope2 = 0;
2409 smb_buf = (struct smb_hdr *)ses_init_buf;
2410
2411 /* sizeof RFC1002_SESSION_REQUEST with no scope */
2412 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
2413 rc = smb_send(server, smb_buf, 0x44);
2414 kfree(ses_init_buf);
2415 /*
2416 * RFC1001 layer in at least one server
2417 * requires very short break before negprot
2418 * presumably because not expecting negprot
2419 * to follow so fast. This is a simple
2420 * solution that works without
2421 * complicating the code and causes no
2422 * significant slowing down on mount
2423 * for everyone else
2424 */
2425 usleep_range(1000, 2000);
2426 }
2427 /*
2428 * else the negprot may still work without this
2429 * even though malloc failed
2430 */
2431
2432 return rc;
2433}
2000 2434
2001static int 2435static int
2002ipv4_connect(struct TCP_Server_Info *server) 2436generic_ip_connect(struct TCP_Server_Info *server)
2003{ 2437{
2004 int rc = 0; 2438 int rc = 0;
2005 int val; 2439 __be16 sport;
2006 bool connected = false; 2440 int slen, sfamily;
2007 __be16 orig_port = 0;
2008 struct socket *socket = server->ssocket; 2441 struct socket *socket = server->ssocket;
2442 struct sockaddr *saddr;
2443
2444 saddr = (struct sockaddr *) &server->dstaddr;
2445
2446 if (server->dstaddr.ss_family == AF_INET6) {
2447 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
2448 slen = sizeof(struct sockaddr_in6);
2449 sfamily = AF_INET6;
2450 } else {
2451 sport = ((struct sockaddr_in *) saddr)->sin_port;
2452 slen = sizeof(struct sockaddr_in);
2453 sfamily = AF_INET;
2454 }
2009 2455
2010 if (socket == NULL) { 2456 if (socket == NULL) {
2011 rc = sock_create_kern(PF_INET, SOCK_STREAM, 2457 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
2012 IPPROTO_TCP, &socket); 2458 IPPROTO_TCP, &socket, 1);
2013 if (rc < 0) { 2459 if (rc < 0) {
2014 cERROR(1, "Error %d creating socket", rc); 2460 cERROR(1, "Error %d creating socket", rc);
2461 server->ssocket = NULL;
2015 return rc; 2462 return rc;
2016 } 2463 }
2017 2464
@@ -2019,59 +2466,20 @@ ipv4_connect(struct TCP_Server_Info *server)
2019 cFYI(1, "Socket created"); 2466 cFYI(1, "Socket created");
2020 server->ssocket = socket; 2467 server->ssocket = socket;
2021 socket->sk->sk_allocation = GFP_NOFS; 2468 socket->sk->sk_allocation = GFP_NOFS;
2022 cifs_reclassify_socket4(socket); 2469 if (sfamily == AF_INET6)
2023 } 2470 cifs_reclassify_socket6(socket);
2024 2471 else
2025 /* user overrode default port */ 2472 cifs_reclassify_socket4(socket);
2026 if (server->addr.sockAddr.sin_port) {
2027 rc = socket->ops->connect(socket, (struct sockaddr *)
2028 &server->addr.sockAddr,
2029 sizeof(struct sockaddr_in), 0);
2030 if (rc >= 0)
2031 connected = true;
2032 }
2033
2034 if (!connected) {
2035 /* save original port so we can retry user specified port
2036 later if fall back ports fail this time */
2037 orig_port = server->addr.sockAddr.sin_port;
2038
2039 /* do not retry on the same port we just failed on */
2040 if (server->addr.sockAddr.sin_port != htons(CIFS_PORT)) {
2041 server->addr.sockAddr.sin_port = htons(CIFS_PORT);
2042 rc = socket->ops->connect(socket,
2043 (struct sockaddr *)
2044 &server->addr.sockAddr,
2045 sizeof(struct sockaddr_in), 0);
2046 if (rc >= 0)
2047 connected = true;
2048 }
2049 }
2050 if (!connected) {
2051 server->addr.sockAddr.sin_port = htons(RFC1001_PORT);
2052 rc = socket->ops->connect(socket, (struct sockaddr *)
2053 &server->addr.sockAddr,
2054 sizeof(struct sockaddr_in), 0);
2055 if (rc >= 0)
2056 connected = true;
2057 } 2473 }
2058 2474
2059 /* give up here - unless we want to retry on different 2475 rc = bind_socket(server);
2060 protocol families some day */ 2476 if (rc < 0)
2061 if (!connected) {
2062 if (orig_port)
2063 server->addr.sockAddr.sin_port = orig_port;
2064 cFYI(1, "Error %d connecting to server via ipv4", rc);
2065 sock_release(socket);
2066 server->ssocket = NULL;
2067 return rc; 2477 return rc;
2068 }
2069
2070 2478
2071 /* 2479 /*
2072 * Eventually check for other socket options to change from 2480 * Eventually check for other socket options to change from
2073 * the default. sock_setsockopt not used because it expects 2481 * the default. sock_setsockopt not used because it expects
2074 * user space buffer 2482 * user space buffer
2075 */ 2483 */
2076 socket->sk->sk_rcvtimeo = 7 * HZ; 2484 socket->sk->sk_rcvtimeo = 7 * HZ;
2077 socket->sk->sk_sndtimeo = 5 * HZ; 2485 socket->sk->sk_sndtimeo = 5 * HZ;
@@ -2085,7 +2493,7 @@ ipv4_connect(struct TCP_Server_Info *server)
2085 } 2493 }
2086 2494
2087 if (server->tcp_nodelay) { 2495 if (server->tcp_nodelay) {
2088 val = 1; 2496 int val = 1;
2089 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY, 2497 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
2090 (char *)&val, sizeof(val)); 2498 (char *)&val, sizeof(val));
2091 if (rc) 2499 if (rc)
@@ -2096,161 +2504,51 @@ ipv4_connect(struct TCP_Server_Info *server)
2096 socket->sk->sk_sndbuf, 2504 socket->sk->sk_sndbuf,
2097 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo); 2505 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
2098 2506
2099 /* send RFC1001 sessinit */ 2507 rc = socket->ops->connect(socket, saddr, slen, 0);
2100 if (server->addr.sockAddr.sin_port == htons(RFC1001_PORT)) { 2508 if (rc < 0) {
2101 /* some servers require RFC1001 sessinit before sending 2509 cFYI(1, "Error %d connecting to server", rc);
2102 negprot - BB check reconnection in case where second 2510 sock_release(socket);
2103 sessinit is sent but no second negprot */ 2511 server->ssocket = NULL;
2104 struct rfc1002_session_packet *ses_init_buf; 2512 return rc;
2105 struct smb_hdr *smb_buf;
2106 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
2107 GFP_KERNEL);
2108 if (ses_init_buf) {
2109 ses_init_buf->trailer.session_req.called_len = 32;
2110 if (server->server_RFC1001_name &&
2111 server->server_RFC1001_name[0] != 0)
2112 rfc1002mangle(ses_init_buf->trailer.
2113 session_req.called_name,
2114 server->server_RFC1001_name,
2115 RFC1001_NAME_LEN_WITH_NULL);
2116 else
2117 rfc1002mangle(ses_init_buf->trailer.
2118 session_req.called_name,
2119 DEFAULT_CIFS_CALLED_NAME,
2120 RFC1001_NAME_LEN_WITH_NULL);
2121
2122 ses_init_buf->trailer.session_req.calling_len = 32;
2123
2124 /* calling name ends in null (byte 16) from old smb
2125 convention. */
2126 if (server->workstation_RFC1001_name &&
2127 server->workstation_RFC1001_name[0] != 0)
2128 rfc1002mangle(ses_init_buf->trailer.
2129 session_req.calling_name,
2130 server->workstation_RFC1001_name,
2131 RFC1001_NAME_LEN_WITH_NULL);
2132 else
2133 rfc1002mangle(ses_init_buf->trailer.
2134 session_req.calling_name,
2135 "LINUX_CIFS_CLNT",
2136 RFC1001_NAME_LEN_WITH_NULL);
2137
2138 ses_init_buf->trailer.session_req.scope1 = 0;
2139 ses_init_buf->trailer.session_req.scope2 = 0;
2140 smb_buf = (struct smb_hdr *)ses_init_buf;
2141 /* sizeof RFC1002_SESSION_REQUEST with no scope */
2142 smb_buf->smb_buf_length = 0x81000044;
2143 rc = smb_send(server, smb_buf, 0x44);
2144 kfree(ses_init_buf);
2145 msleep(1); /* RFC1001 layer in at least one server
2146 requires very short break before negprot
2147 presumably because not expecting negprot
2148 to follow so fast. This is a simple
2149 solution that works without
2150 complicating the code and causes no
2151 significant slowing down on mount
2152 for everyone else */
2153 }
2154 /* else the negprot may still work without this
2155 even though malloc failed */
2156
2157 } 2513 }
2158 2514
2515 if (sport == htons(RFC1001_PORT))
2516 rc = ip_rfc1001_connect(server);
2517
2159 return rc; 2518 return rc;
2160} 2519}
2161 2520
2162static int 2521static int
2163ipv6_connect(struct TCP_Server_Info *server) 2522ip_connect(struct TCP_Server_Info *server)
2164{ 2523{
2165 int rc = 0; 2524 __be16 *sport;
2166 int val; 2525 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
2167 bool connected = false; 2526 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2168 __be16 orig_port = 0;
2169 struct socket *socket = server->ssocket;
2170 2527
2171 if (socket == NULL) { 2528 if (server->dstaddr.ss_family == AF_INET6)
2172 rc = sock_create_kern(PF_INET6, SOCK_STREAM, 2529 sport = &addr6->sin6_port;
2173 IPPROTO_TCP, &socket); 2530 else
2174 if (rc < 0) { 2531 sport = &addr->sin_port;
2175 cERROR(1, "Error %d creating ipv6 socket", rc);
2176 socket = NULL;
2177 return rc;
2178 }
2179 2532
2180 /* BB other socket options to set KEEPALIVE, NODELAY? */ 2533 if (*sport == 0) {
2181 cFYI(1, "ipv6 Socket created"); 2534 int rc;
2182 server->ssocket = socket;
2183 socket->sk->sk_allocation = GFP_NOFS;
2184 cifs_reclassify_socket6(socket);
2185 }
2186 2535
2187 /* user overrode default port */ 2536 /* try with 445 port at first */
2188 if (server->addr.sockAddr6.sin6_port) { 2537 *sport = htons(CIFS_PORT);
2189 rc = socket->ops->connect(socket,
2190 (struct sockaddr *) &server->addr.sockAddr6,
2191 sizeof(struct sockaddr_in6), 0);
2192 if (rc >= 0)
2193 connected = true;
2194 }
2195
2196 if (!connected) {
2197 /* save original port so we can retry user specified port
2198 later if fall back ports fail this time */
2199
2200 orig_port = server->addr.sockAddr6.sin6_port;
2201 /* do not retry on the same port we just failed on */
2202 if (server->addr.sockAddr6.sin6_port != htons(CIFS_PORT)) {
2203 server->addr.sockAddr6.sin6_port = htons(CIFS_PORT);
2204 rc = socket->ops->connect(socket, (struct sockaddr *)
2205 &server->addr.sockAddr6,
2206 sizeof(struct sockaddr_in6), 0);
2207 if (rc >= 0)
2208 connected = true;
2209 }
2210 }
2211 if (!connected) {
2212 server->addr.sockAddr6.sin6_port = htons(RFC1001_PORT);
2213 rc = socket->ops->connect(socket, (struct sockaddr *)
2214 &server->addr.sockAddr6,
2215 sizeof(struct sockaddr_in6), 0);
2216 if (rc >= 0)
2217 connected = true;
2218 }
2219
2220 /* give up here - unless we want to retry on different
2221 protocol families some day */
2222 if (!connected) {
2223 if (orig_port)
2224 server->addr.sockAddr6.sin6_port = orig_port;
2225 cFYI(1, "Error %d connecting to server via ipv6", rc);
2226 sock_release(socket);
2227 server->ssocket = NULL;
2228 return rc;
2229 }
2230 2538
2231 /* 2539 rc = generic_ip_connect(server);
2232 * Eventually check for other socket options to change from 2540 if (rc >= 0)
2233 * the default. sock_setsockopt not used because it expects 2541 return rc;
2234 * user space buffer
2235 */
2236 socket->sk->sk_rcvtimeo = 7 * HZ;
2237 socket->sk->sk_sndtimeo = 5 * HZ;
2238 2542
2239 if (server->tcp_nodelay) { 2543 /* if it failed, try with 139 port */
2240 val = 1; 2544 *sport = htons(RFC1001_PORT);
2241 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
2242 (char *)&val, sizeof(val));
2243 if (rc)
2244 cFYI(1, "set TCP_NODELAY socket option error %d", rc);
2245 } 2545 }
2246 2546
2247 server->ssocket = socket; 2547 return generic_ip_connect(server);
2248
2249 return rc;
2250} 2548}
2251 2549
2252void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon, 2550void reset_cifs_unix_caps(int xid, struct cifs_tcon *tcon,
2253 struct super_block *sb, struct smb_vol *vol_info) 2551 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
2254{ 2552{
2255 /* if we are reconnecting then should we check to see if 2553 /* if we are reconnecting then should we check to see if
2256 * any requested capabilities changed locally e.g. via 2554 * any requested capabilities changed locally e.g. via
@@ -2278,7 +2576,7 @@ void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
2278 2576
2279 if (!CIFSSMBQFSUnixInfo(xid, tcon)) { 2577 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
2280 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability); 2578 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
2281 2579 cFYI(1, "unix caps which server supports %lld", cap);
2282 /* check for reconnect case in which we do not 2580 /* check for reconnect case in which we do not
2283 want to change the mount behavior if we can avoid it */ 2581 want to change the mount behavior if we can avoid it */
2284 if (vol_info == NULL) { 2582 if (vol_info == NULL) {
@@ -2296,33 +2594,31 @@ void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
2296 } 2594 }
2297 } 2595 }
2298 2596
2597 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
2598 cERROR(1, "per-share encryption not supported yet");
2599
2299 cap &= CIFS_UNIX_CAP_MASK; 2600 cap &= CIFS_UNIX_CAP_MASK;
2300 if (vol_info && vol_info->no_psx_acl) 2601 if (vol_info && vol_info->no_psx_acl)
2301 cap &= ~CIFS_UNIX_POSIX_ACL_CAP; 2602 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
2302 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) { 2603 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
2303 cFYI(1, "negotiated posix acl support"); 2604 cFYI(1, "negotiated posix acl support");
2304 if (sb) 2605 if (cifs_sb)
2305 sb->s_flags |= MS_POSIXACL; 2606 cifs_sb->mnt_cifs_flags |=
2607 CIFS_MOUNT_POSIXACL;
2306 } 2608 }
2307 2609
2308 if (vol_info && vol_info->posix_paths == 0) 2610 if (vol_info && vol_info->posix_paths == 0)
2309 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP; 2611 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
2310 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) { 2612 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
2311 cFYI(1, "negotiate posix pathnames"); 2613 cFYI(1, "negotiate posix pathnames");
2312 if (sb) 2614 if (cifs_sb)
2313 CIFS_SB(sb)->mnt_cifs_flags |= 2615 cifs_sb->mnt_cifs_flags |=
2314 CIFS_MOUNT_POSIX_PATHS; 2616 CIFS_MOUNT_POSIX_PATHS;
2315 } 2617 }
2316 2618
2317 /* We might be setting the path sep back to a different 2619 if (cifs_sb && (cifs_sb->rsize > 127 * 1024)) {
2318 form if we are reconnecting and the server switched its
2319 posix path capability for this share */
2320 if (sb && (CIFS_SB(sb)->prepathlen > 0))
2321 CIFS_SB(sb)->prepath[0] = CIFS_DIR_SEP(CIFS_SB(sb));
2322
2323 if (sb && (CIFS_SB(sb)->rsize > 127 * 1024)) {
2324 if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) { 2620 if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
2325 CIFS_SB(sb)->rsize = 127 * 1024; 2621 cifs_sb->rsize = 127 * 1024;
2326 cFYI(DBG2, "larger reads not supported by srv"); 2622 cFYI(DBG2, "larger reads not supported by srv");
2327 } 2623 }
2328 } 2624 }
@@ -2344,6 +2640,10 @@ void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
2344 cFYI(1, "very large read cap"); 2640 cFYI(1, "very large read cap");
2345 if (cap & CIFS_UNIX_LARGE_WRITE_CAP) 2641 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
2346 cFYI(1, "very large write cap"); 2642 cFYI(1, "very large write cap");
2643 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
2644 cFYI(1, "transport encryption cap");
2645 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
2646 cFYI(1, "mandatory transport encryption cap");
2347#endif /* CIFS_DEBUG2 */ 2647#endif /* CIFS_DEBUG2 */
2348 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) { 2648 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
2349 if (vol_info == NULL) { 2649 if (vol_info == NULL) {
@@ -2360,29 +2660,14 @@ void reset_cifs_unix_caps(int xid, struct cifsTconInfo *tcon,
2360 } 2660 }
2361} 2661}
2362 2662
2363static void 2663void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
2364convert_delimiter(char *path, char delim) 2664 struct cifs_sb_info *cifs_sb)
2365{ 2665{
2366 int i; 2666 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
2367 char old_delim;
2368 2667
2369 if (path == NULL) 2668 spin_lock_init(&cifs_sb->tlink_tree_lock);
2370 return; 2669 cifs_sb->tlink_tree = RB_ROOT;
2371 2670
2372 if (delim == '/')
2373 old_delim = '\\';
2374 else
2375 old_delim = '/';
2376
2377 for (i = 0; path[i] != '\0'; i++) {
2378 if (path[i] == old_delim)
2379 path[i] = delim;
2380 }
2381}
2382
2383static void setup_cifs_sb(struct smb_vol *pvolume_info,
2384 struct cifs_sb_info *cifs_sb)
2385{
2386 if (pvolume_info->rsize > CIFSMaxBufSize) { 2671 if (pvolume_info->rsize > CIFSMaxBufSize) {
2387 cERROR(1, "rsize %d too large, using MaxBufSize", 2672 cERROR(1, "rsize %d too large, using MaxBufSize",
2388 pvolume_info->rsize); 2673 pvolume_info->rsize);
@@ -2393,40 +2678,19 @@ static void setup_cifs_sb(struct smb_vol *pvolume_info,
2393 else /* default */ 2678 else /* default */
2394 cifs_sb->rsize = CIFSMaxBufSize; 2679 cifs_sb->rsize = CIFSMaxBufSize;
2395 2680
2396 if (pvolume_info->wsize > PAGEVEC_SIZE * PAGE_CACHE_SIZE) {
2397 cERROR(1, "wsize %d too large, using 4096 instead",
2398 pvolume_info->wsize);
2399 cifs_sb->wsize = 4096;
2400 } else if (pvolume_info->wsize)
2401 cifs_sb->wsize = pvolume_info->wsize;
2402 else
2403 cifs_sb->wsize = min_t(const int,
2404 PAGEVEC_SIZE * PAGE_CACHE_SIZE,
2405 127*1024);
2406 /* old default of CIFSMaxBufSize was too small now
2407 that SMB Write2 can send multiple pages in kvec.
2408 RFC1001 does not describe what happens when frame
2409 bigger than 128K is sent so use that as max in
2410 conjunction with 52K kvec constraint on arch with 4K
2411 page size */
2412
2413 if (cifs_sb->rsize < 2048) { 2681 if (cifs_sb->rsize < 2048) {
2414 cifs_sb->rsize = 2048; 2682 cifs_sb->rsize = 2048;
2415 /* Windows ME may prefer this */ 2683 /* Windows ME may prefer this */
2416 cFYI(1, "readsize set to minimum: 2048"); 2684 cFYI(1, "readsize set to minimum: 2048");
2417 } 2685 }
2418 /* calculate prepath */ 2686
2419 cifs_sb->prepath = pvolume_info->prepath; 2687 /*
2420 if (cifs_sb->prepath) { 2688 * Temporarily set wsize for matching superblock. If we end up using
2421 cifs_sb->prepathlen = strlen(cifs_sb->prepath); 2689 * new sb then cifs_negotiate_wsize will later negotiate it downward
2422 /* we can not convert the / to \ in the path 2690 * if needed.
2423 separators in the prefixpath yet because we do not 2691 */
2424 know (until reset_cifs_unix_caps is called later) 2692 cifs_sb->wsize = pvolume_info->wsize;
2425 whether POSIX PATH CAP is available. We normalize 2693
2426 the / to \ after reset_cifs_unix_caps is called */
2427 pvolume_info->prepath = NULL;
2428 } else
2429 cifs_sb->prepathlen = 0;
2430 cifs_sb->mnt_uid = pvolume_info->linux_uid; 2694 cifs_sb->mnt_uid = pvolume_info->linux_uid;
2431 cifs_sb->mnt_gid = pvolume_info->linux_gid; 2695 cifs_sb->mnt_gid = pvolume_info->linux_gid;
2432 cifs_sb->mnt_file_mode = pvolume_info->file_mode; 2696 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
@@ -2434,6 +2698,9 @@ static void setup_cifs_sb(struct smb_vol *pvolume_info,
2434 cFYI(1, "file mode: 0x%x dir mode: 0x%x", 2698 cFYI(1, "file mode: 0x%x dir mode: 0x%x",
2435 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode); 2699 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
2436 2700
2701 cifs_sb->actimeo = pvolume_info->actimeo;
2702 cifs_sb->local_nls = pvolume_info->local_nls;
2703
2437 if (pvolume_info->noperm) 2704 if (pvolume_info->noperm)
2438 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM; 2705 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
2439 if (pvolume_info->setuids) 2706 if (pvolume_info->setuids)
@@ -2452,6 +2719,8 @@ static void setup_cifs_sb(struct smb_vol *pvolume_info,
2452 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC; 2719 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
2453 if (pvolume_info->mand_lock) 2720 if (pvolume_info->mand_lock)
2454 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL; 2721 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
2722 if (pvolume_info->rwpidforward)
2723 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
2455 if (pvolume_info->cifs_acl) 2724 if (pvolume_info->cifs_acl)
2456 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL; 2725 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
2457 if (pvolume_info->override_uid) 2726 if (pvolume_info->override_uid)
@@ -2462,18 +2731,85 @@ static void setup_cifs_sb(struct smb_vol *pvolume_info,
2462 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM; 2731 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
2463 if (pvolume_info->fsc) 2732 if (pvolume_info->fsc)
2464 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE; 2733 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
2734 if (pvolume_info->multiuser)
2735 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
2736 CIFS_MOUNT_NO_PERM);
2737 if (pvolume_info->strict_io)
2738 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
2465 if (pvolume_info->direct_io) { 2739 if (pvolume_info->direct_io) {
2466 cFYI(1, "mounting share using direct i/o"); 2740 cFYI(1, "mounting share using direct i/o");
2467 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO; 2741 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
2468 } 2742 }
2743 if (pvolume_info->mfsymlinks) {
2744 if (pvolume_info->sfu_emul) {
2745 cERROR(1, "mount option mfsymlinks ignored if sfu "
2746 "mount option is used");
2747 } else {
2748 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
2749 }
2750 }
2469 2751
2470 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm)) 2752 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
2471 cERROR(1, "mount option dynperm ignored if cifsacl " 2753 cERROR(1, "mount option dynperm ignored if cifsacl "
2472 "mount option supported"); 2754 "mount option supported");
2473} 2755}
2474 2756
2757/*
2758 * When the server supports very large writes via POSIX extensions, we can
2759 * allow up to 2^24-1, minus the size of a WRITE_AND_X header, not including
2760 * the RFC1001 length.
2761 *
2762 * Note that this might make for "interesting" allocation problems during
2763 * writeback however as we have to allocate an array of pointers for the
2764 * pages. A 16M write means ~32kb page array with PAGE_CACHE_SIZE == 4096.
2765 */
2766#define CIFS_MAX_WSIZE ((1<<24) - 1 - sizeof(WRITE_REQ) + 4)
2767
2768/*
2769 * When the server doesn't allow large posix writes, only allow a wsize of
2770 * 128k minus the size of the WRITE_AND_X header. That allows for a write up
2771 * to the maximum size described by RFC1002.
2772 */
2773#define CIFS_MAX_RFC1002_WSIZE (128 * 1024 - sizeof(WRITE_REQ) + 4)
2774
2775/*
2776 * The default wsize is 1M. find_get_pages seems to return a maximum of 256
2777 * pages in a single call. With PAGE_CACHE_SIZE == 4k, this means we can fill
2778 * a single wsize request with a single call.
2779 */
2780#define CIFS_DEFAULT_WSIZE (1024 * 1024)
2781
2782static unsigned int
2783cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
2784{
2785 __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
2786 struct TCP_Server_Info *server = tcon->ses->server;
2787 unsigned int wsize = pvolume_info->wsize ? pvolume_info->wsize :
2788 CIFS_DEFAULT_WSIZE;
2789
2790 /* can server support 24-bit write sizes? (via UNIX extensions) */
2791 if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
2792 wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
2793
2794 /*
2795 * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
2796 * Limit it to max buffer offered by the server, minus the size of the
2797 * WRITEX header, not including the 4 byte RFC1001 length.
2798 */
2799 if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
2800 (!(server->capabilities & CAP_UNIX) &&
2801 (server->sec_mode & (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED))))
2802 wsize = min_t(unsigned int, wsize,
2803 server->maxBuf - sizeof(WRITE_REQ) + 4);
2804
2805 /* hard limit of CIFS_MAX_WSIZE */
2806 wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
2807
2808 return wsize;
2809}
2810
2475static int 2811static int
2476is_path_accessible(int xid, struct cifsTconInfo *tcon, 2812is_path_accessible(int xid, struct cifs_tcon *tcon,
2477 struct cifs_sb_info *cifs_sb, const char *full_path) 2813 struct cifs_sb_info *cifs_sb, const char *full_path)
2478{ 2814{
2479 int rc; 2815 int rc;
@@ -2487,99 +2823,138 @@ is_path_accessible(int xid, struct cifsTconInfo *tcon,
2487 0 /* not legacy */, cifs_sb->local_nls, 2823 0 /* not legacy */, cifs_sb->local_nls,
2488 cifs_sb->mnt_cifs_flags & 2824 cifs_sb->mnt_cifs_flags &
2489 CIFS_MOUNT_MAP_SPECIAL_CHR); 2825 CIFS_MOUNT_MAP_SPECIAL_CHR);
2826
2827 if (rc == -EOPNOTSUPP || rc == -EINVAL)
2828 rc = SMBQueryInformation(xid, tcon, full_path, pfile_info,
2829 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
2830 CIFS_MOUNT_MAP_SPECIAL_CHR);
2490 kfree(pfile_info); 2831 kfree(pfile_info);
2491 return rc; 2832 return rc;
2492} 2833}
2493 2834
2494static void 2835static void
2495cleanup_volume_info(struct smb_vol **pvolume_info) 2836cleanup_volume_info_contents(struct smb_vol *volume_info)
2496{ 2837{
2497 struct smb_vol *volume_info; 2838 kfree(volume_info->username);
2498
2499 if (!pvolume_info || !*pvolume_info)
2500 return;
2501
2502 volume_info = *pvolume_info;
2503 kzfree(volume_info->password); 2839 kzfree(volume_info->password);
2504 kfree(volume_info->UNC); 2840 kfree(volume_info->UNC);
2841 kfree(volume_info->UNCip);
2842 kfree(volume_info->domainname);
2843 kfree(volume_info->iocharset);
2505 kfree(volume_info->prepath); 2844 kfree(volume_info->prepath);
2845}
2846
2847void
2848cifs_cleanup_volume_info(struct smb_vol *volume_info)
2849{
2850 if (!volume_info)
2851 return;
2852 cleanup_volume_info_contents(volume_info);
2506 kfree(volume_info); 2853 kfree(volume_info);
2507 *pvolume_info = NULL;
2508 return;
2509} 2854}
2510 2855
2856
2511#ifdef CONFIG_CIFS_DFS_UPCALL 2857#ifdef CONFIG_CIFS_DFS_UPCALL
2512/* build_path_to_root returns full path to root when 2858/* build_path_to_root returns full path to root when
2513 * we do not have an exiting connection (tcon) */ 2859 * we do not have an exiting connection (tcon) */
2514static char * 2860static char *
2515build_unc_path_to_root(const struct smb_vol *volume_info, 2861build_unc_path_to_root(const struct smb_vol *vol,
2516 const struct cifs_sb_info *cifs_sb) 2862 const struct cifs_sb_info *cifs_sb)
2517{ 2863{
2518 char *full_path; 2864 char *full_path, *pos;
2865 unsigned int pplen = vol->prepath ? strlen(vol->prepath) : 0;
2866 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
2519 2867
2520 int unc_len = strnlen(volume_info->UNC, MAX_TREE_SIZE + 1); 2868 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
2521 full_path = kmalloc(unc_len + cifs_sb->prepathlen + 1, GFP_KERNEL);
2522 if (full_path == NULL) 2869 if (full_path == NULL)
2523 return ERR_PTR(-ENOMEM); 2870 return ERR_PTR(-ENOMEM);
2524 2871
2525 strncpy(full_path, volume_info->UNC, unc_len); 2872 strncpy(full_path, vol->UNC, unc_len);
2526 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { 2873 pos = full_path + unc_len;
2527 int i;
2528 for (i = 0; i < unc_len; i++) {
2529 if (full_path[i] == '\\')
2530 full_path[i] = '/';
2531 }
2532 }
2533 2874
2534 if (cifs_sb->prepathlen) 2875 if (pplen) {
2535 strncpy(full_path + unc_len, cifs_sb->prepath, 2876 strncpy(pos, vol->prepath, pplen);
2536 cifs_sb->prepathlen); 2877 pos += pplen;
2878 }
2537 2879
2538 full_path[unc_len + cifs_sb->prepathlen] = 0; /* add trailing null */ 2880 *pos = '\0'; /* add trailing null */
2881 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
2882 cFYI(1, "%s: full_path=%s", __func__, full_path);
2539 return full_path; 2883 return full_path;
2540} 2884}
2541#endif
2542 2885
2543int 2886/*
2544cifs_mount(struct super_block *sb, struct cifs_sb_info *cifs_sb, 2887 * Perform a dfs referral query for a share and (optionally) prefix
2545 char *mount_data_global, const char *devname) 2888 *
2889 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
2890 * to a string containing updated options for the submount. Otherwise it
2891 * will be left untouched.
2892 *
2893 * Returns the rc from get_dfs_path to the caller, which can be used to
2894 * determine whether there were referrals.
2895 */
2896static int
2897expand_dfs_referral(int xid, struct cifs_ses *pSesInfo,
2898 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
2899 int check_prefix)
2546{ 2900{
2547 int rc; 2901 int rc;
2548 int xid;
2549 struct smb_vol *volume_info;
2550 struct cifsSesInfo *pSesInfo;
2551 struct cifsTconInfo *tcon;
2552 struct TCP_Server_Info *srvTcp;
2553 char *full_path;
2554 char *mount_data = mount_data_global;
2555#ifdef CONFIG_CIFS_DFS_UPCALL
2556 struct dfs_info3_param *referrals = NULL;
2557 unsigned int num_referrals = 0; 2902 unsigned int num_referrals = 0;
2558 int referral_walks_count = 0; 2903 struct dfs_info3_param *referrals = NULL;
2559try_mount_again: 2904 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
2560#endif
2561 rc = 0;
2562 tcon = NULL;
2563 pSesInfo = NULL;
2564 srvTcp = NULL;
2565 full_path = NULL;
2566 2905
2567 xid = GetXid(); 2906 full_path = build_unc_path_to_root(volume_info, cifs_sb);
2907 if (IS_ERR(full_path))
2908 return PTR_ERR(full_path);
2568 2909
2569 volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL); 2910 /* For DFS paths, skip the first '\' of the UNC */
2570 if (!volume_info) { 2911 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
2571 rc = -ENOMEM;
2572 goto out;
2573 }
2574 2912
2575 if (cifs_parse_mount_options(mount_data, devname, volume_info)) { 2913 rc = get_dfs_path(xid, pSesInfo , ref_path, cifs_sb->local_nls,
2576 rc = -EINVAL; 2914 &num_referrals, &referrals,
2577 goto out; 2915 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
2916
2917 if (!rc && num_referrals > 0) {
2918 char *fake_devname = NULL;
2919
2920 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
2921 full_path + 1, referrals,
2922 &fake_devname);
2923
2924 free_dfs_info_array(referrals, num_referrals);
2925
2926 if (IS_ERR(mdata)) {
2927 rc = PTR_ERR(mdata);
2928 mdata = NULL;
2929 } else {
2930 cleanup_volume_info_contents(volume_info);
2931 memset(volume_info, '\0', sizeof(*volume_info));
2932 rc = cifs_setup_volume_info(volume_info, mdata,
2933 fake_devname);
2934 }
2935 kfree(fake_devname);
2936 kfree(cifs_sb->mountdata);
2937 cifs_sb->mountdata = mdata;
2578 } 2938 }
2939 kfree(full_path);
2940 return rc;
2941}
2942#endif
2943
2944static int
2945cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
2946 const char *devname)
2947{
2948 int rc = 0;
2949
2950 if (cifs_parse_mount_options(mount_data, devname, volume_info))
2951 return -EINVAL;
2579 2952
2580 if (volume_info->nullauth) { 2953 if (volume_info->nullauth) {
2581 cFYI(1, "null user"); 2954 cFYI(1, "null user");
2582 volume_info->username = ""; 2955 volume_info->username = kzalloc(1, GFP_KERNEL);
2956 if (volume_info->username == NULL)
2957 return -ENOMEM;
2583 } else if (volume_info->username) { 2958 } else if (volume_info->username) {
2584 /* BB fixme parse for domain name here */ 2959 /* BB fixme parse for domain name here */
2585 cFYI(1, "Username: %s", volume_info->username); 2960 cFYI(1, "Username: %s", volume_info->username);
@@ -2587,8 +2962,7 @@ try_mount_again:
2587 cifserror("No username specified"); 2962 cifserror("No username specified");
2588 /* In userspace mount helper we can get user name from alternate 2963 /* In userspace mount helper we can get user name from alternate
2589 locations such as env variables and files on disk */ 2964 locations such as env variables and files on disk */
2590 rc = -EINVAL; 2965 return -EINVAL;
2591 goto out;
2592 } 2966 }
2593 2967
2594 /* this is needed for ASCII cp to Unicode converts */ 2968 /* this is needed for ASCII cp to Unicode converts */
@@ -2600,16 +2974,77 @@ try_mount_again:
2600 if (volume_info->local_nls == NULL) { 2974 if (volume_info->local_nls == NULL) {
2601 cERROR(1, "CIFS mount error: iocharset %s not found", 2975 cERROR(1, "CIFS mount error: iocharset %s not found",
2602 volume_info->iocharset); 2976 volume_info->iocharset);
2603 rc = -ELIBACC; 2977 return -ELIBACC;
2604 goto out;
2605 } 2978 }
2606 } 2979 }
2607 cifs_sb->local_nls = volume_info->local_nls; 2980
2981 return rc;
2982}
2983
2984struct smb_vol *
2985cifs_get_volume_info(char *mount_data, const char *devname)
2986{
2987 int rc;
2988 struct smb_vol *volume_info;
2989
2990 volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
2991 if (!volume_info)
2992 return ERR_PTR(-ENOMEM);
2993
2994 rc = cifs_setup_volume_info(volume_info, mount_data, devname);
2995 if (rc) {
2996 cifs_cleanup_volume_info(volume_info);
2997 volume_info = ERR_PTR(rc);
2998 }
2999
3000 return volume_info;
3001}
3002
3003int
3004cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
3005{
3006 int rc = 0;
3007 int xid;
3008 struct cifs_ses *pSesInfo;
3009 struct cifs_tcon *tcon;
3010 struct TCP_Server_Info *srvTcp;
3011 char *full_path;
3012 struct tcon_link *tlink;
3013#ifdef CONFIG_CIFS_DFS_UPCALL
3014 int referral_walks_count = 0;
3015#endif
3016
3017 rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
3018 if (rc)
3019 return rc;
3020
3021 cifs_sb->bdi.ra_pages = default_backing_dev_info.ra_pages;
3022
3023#ifdef CONFIG_CIFS_DFS_UPCALL
3024try_mount_again:
3025 /* cleanup activities if we're chasing a referral */
3026 if (referral_walks_count) {
3027 if (tcon)
3028 cifs_put_tcon(tcon);
3029 else if (pSesInfo)
3030 cifs_put_smb_ses(pSesInfo);
3031
3032 FreeXid(xid);
3033 }
3034#endif
3035 tcon = NULL;
3036 pSesInfo = NULL;
3037 srvTcp = NULL;
3038 full_path = NULL;
3039 tlink = NULL;
3040
3041 xid = GetXid();
2608 3042
2609 /* get a reference to a tcp session */ 3043 /* get a reference to a tcp session */
2610 srvTcp = cifs_get_tcp_session(volume_info); 3044 srvTcp = cifs_get_tcp_session(volume_info);
2611 if (IS_ERR(srvTcp)) { 3045 if (IS_ERR(srvTcp)) {
2612 rc = PTR_ERR(srvTcp); 3046 rc = PTR_ERR(srvTcp);
3047 bdi_destroy(&cifs_sb->bdi);
2613 goto out; 3048 goto out;
2614 } 3049 }
2615 3050
@@ -2621,15 +3056,6 @@ try_mount_again:
2621 goto mount_fail_check; 3056 goto mount_fail_check;
2622 } 3057 }
2623 3058
2624 setup_cifs_sb(volume_info, cifs_sb);
2625 if (pSesInfo->capabilities & CAP_LARGE_FILES)
2626 sb->s_maxbytes = MAX_LFS_FILESIZE;
2627 else
2628 sb->s_maxbytes = MAX_NON_LFS;
2629
2630 /* BB FIXME fix time_gran to be larger for LANMAN sessions */
2631 sb->s_time_gran = 100;
2632
2633 /* search for existing tcon to this server share */ 3059 /* search for existing tcon to this server share */
2634 tcon = cifs_get_tcon(pSesInfo, volume_info); 3060 tcon = cifs_get_tcon(pSesInfo, volume_info);
2635 if (IS_ERR(tcon)) { 3061 if (IS_ERR(tcon)) {
@@ -2638,7 +3064,19 @@ try_mount_again:
2638 goto remote_path_check; 3064 goto remote_path_check;
2639 } 3065 }
2640 3066
2641 cifs_sb->tcon = tcon; 3067 /* tell server which Unix caps we support */
3068 if (tcon->ses->capabilities & CAP_UNIX) {
3069 /* reset of caps checks mount to see if unix extensions
3070 disabled for just this mount */
3071 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
3072 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3073 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3074 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
3075 rc = -EACCES;
3076 goto mount_fail_check;
3077 }
3078 } else
3079 tcon->unix_ext = 0; /* server does not support them */
2642 3080
2643 /* do not care if following two calls succeed - informational */ 3081 /* do not care if following two calls succeed - informational */
2644 if (!tcon->ipc) { 3082 if (!tcon->ipc) {
@@ -2646,40 +3084,45 @@ try_mount_again:
2646 CIFSSMBQFSAttributeInfo(xid, tcon); 3084 CIFSSMBQFSAttributeInfo(xid, tcon);
2647 } 3085 }
2648 3086
2649 /* tell server which Unix caps we support */
2650 if (tcon->ses->capabilities & CAP_UNIX)
2651 /* reset of caps checks mount to see if unix extensions
2652 disabled for just this mount */
2653 reset_cifs_unix_caps(xid, tcon, sb, volume_info);
2654 else
2655 tcon->unix_ext = 0; /* server does not support them */
2656
2657 /* convert forward to back slashes in prepath here if needed */
2658 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
2659 convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
2660
2661 if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) { 3087 if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
2662 cifs_sb->rsize = 1024 * 127; 3088 cifs_sb->rsize = 1024 * 127;
2663 cFYI(DBG2, "no very large read support, rsize now 127K"); 3089 cFYI(DBG2, "no very large read support, rsize now 127K");
2664 } 3090 }
2665 if (!(tcon->ses->capabilities & CAP_LARGE_WRITE_X))
2666 cifs_sb->wsize = min(cifs_sb->wsize,
2667 (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
2668 if (!(tcon->ses->capabilities & CAP_LARGE_READ_X)) 3091 if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
2669 cifs_sb->rsize = min(cifs_sb->rsize, 3092 cifs_sb->rsize = min(cifs_sb->rsize,
2670 (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE)); 3093 (tcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE));
2671 3094
3095 cifs_sb->wsize = cifs_negotiate_wsize(tcon, volume_info);
3096
2672remote_path_check: 3097remote_path_check:
2673 /* check if a whole path (including prepath) is not remote */ 3098#ifdef CONFIG_CIFS_DFS_UPCALL
2674 if (!rc && cifs_sb->prepathlen && tcon) { 3099 /*
3100 * Perform an unconditional check for whether there are DFS
3101 * referrals for this path without prefix, to provide support
3102 * for DFS referrals from w2k8 servers which don't seem to respond
3103 * with PATH_NOT_COVERED to requests that include the prefix.
3104 * Chase the referral if found, otherwise continue normally.
3105 */
3106 if (referral_walks_count == 0) {
3107 int refrc = expand_dfs_referral(xid, pSesInfo, volume_info,
3108 cifs_sb, false);
3109 if (!refrc) {
3110 referral_walks_count++;
3111 goto try_mount_again;
3112 }
3113 }
3114#endif
3115
3116 /* check if a whole path is not remote */
3117 if (!rc && tcon) {
2675 /* build_path_to_root works only when we have a valid tcon */ 3118 /* build_path_to_root works only when we have a valid tcon */
2676 full_path = cifs_build_path_to_root(cifs_sb); 3119 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
2677 if (full_path == NULL) { 3120 if (full_path == NULL) {
2678 rc = -ENOMEM; 3121 rc = -ENOMEM;
2679 goto mount_fail_check; 3122 goto mount_fail_check;
2680 } 3123 }
2681 rc = is_path_accessible(xid, tcon, cifs_sb, full_path); 3124 rc = is_path_accessible(xid, tcon, cifs_sb, full_path);
2682 if (rc != -EREMOTE) { 3125 if (rc != 0 && rc != -EREMOTE) {
2683 kfree(full_path); 3126 kfree(full_path);
2684 goto mount_fail_check; 3127 goto mount_fail_check;
2685 } 3128 }
@@ -2699,68 +3142,56 @@ remote_path_check:
2699 rc = -ELOOP; 3142 rc = -ELOOP;
2700 goto mount_fail_check; 3143 goto mount_fail_check;
2701 } 3144 }
2702 /* convert forward to back slashes in prepath here if needed */
2703 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) == 0)
2704 convert_delimiter(cifs_sb->prepath,
2705 CIFS_DIR_SEP(cifs_sb));
2706 full_path = build_unc_path_to_root(volume_info, cifs_sb);
2707 if (IS_ERR(full_path)) {
2708 rc = PTR_ERR(full_path);
2709 goto mount_fail_check;
2710 }
2711
2712 cFYI(1, "Getting referral for: %s", full_path);
2713 rc = get_dfs_path(xid, pSesInfo , full_path + 1,
2714 cifs_sb->local_nls, &num_referrals, &referrals,
2715 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
2716 if (!rc && num_referrals > 0) {
2717 char *fake_devname = NULL;
2718
2719 if (mount_data != mount_data_global)
2720 kfree(mount_data);
2721 3145
2722 mount_data = cifs_compose_mount_options( 3146 rc = expand_dfs_referral(xid, pSesInfo, volume_info, cifs_sb,
2723 cifs_sb->mountdata, full_path + 1, 3147 true);
2724 referrals, &fake_devname);
2725
2726 free_dfs_info_array(referrals, num_referrals);
2727 kfree(fake_devname);
2728 kfree(full_path);
2729 3148
2730 if (IS_ERR(mount_data)) { 3149 if (!rc) {
2731 rc = PTR_ERR(mount_data);
2732 mount_data = NULL;
2733 goto mount_fail_check;
2734 }
2735
2736 if (tcon)
2737 cifs_put_tcon(tcon);
2738 else if (pSesInfo)
2739 cifs_put_smb_ses(pSesInfo);
2740
2741 cleanup_volume_info(&volume_info);
2742 referral_walks_count++; 3150 referral_walks_count++;
2743 FreeXid(xid);
2744 goto try_mount_again; 3151 goto try_mount_again;
2745 } 3152 }
3153 goto mount_fail_check;
2746#else /* No DFS support, return error on mount */ 3154#else /* No DFS support, return error on mount */
2747 rc = -EOPNOTSUPP; 3155 rc = -EOPNOTSUPP;
2748#endif 3156#endif
2749 } 3157 }
2750 3158
3159 if (rc)
3160 goto mount_fail_check;
3161
3162 /* now, hang the tcon off of the superblock */
3163 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
3164 if (tlink == NULL) {
3165 rc = -ENOMEM;
3166 goto mount_fail_check;
3167 }
3168
3169 tlink->tl_uid = pSesInfo->linux_uid;
3170 tlink->tl_tcon = tcon;
3171 tlink->tl_time = jiffies;
3172 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3173 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3174
3175 cifs_sb->master_tlink = tlink;
3176 spin_lock(&cifs_sb->tlink_tree_lock);
3177 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3178 spin_unlock(&cifs_sb->tlink_tree_lock);
3179
3180 queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
3181 TLINK_IDLE_EXPIRE);
3182
2751mount_fail_check: 3183mount_fail_check:
2752 /* on error free sesinfo and tcon struct if needed */ 3184 /* on error free sesinfo and tcon struct if needed */
2753 if (rc) { 3185 if (rc) {
2754 if (mount_data != mount_data_global)
2755 kfree(mount_data);
2756 /* If find_unc succeeded then rc == 0 so we can not end */ 3186 /* If find_unc succeeded then rc == 0 so we can not end */
2757 /* up accidently freeing someone elses tcon struct */ 3187 /* up accidentally freeing someone elses tcon struct */
2758 if (tcon) 3188 if (tcon)
2759 cifs_put_tcon(tcon); 3189 cifs_put_tcon(tcon);
2760 else if (pSesInfo) 3190 else if (pSesInfo)
2761 cifs_put_smb_ses(pSesInfo); 3191 cifs_put_smb_ses(pSesInfo);
2762 else 3192 else
2763 cifs_put_tcp_session(srvTcp); 3193 cifs_put_tcp_session(srvTcp);
3194 bdi_destroy(&cifs_sb->bdi);
2764 goto out; 3195 goto out;
2765 } 3196 }
2766 3197
@@ -2770,14 +3201,17 @@ mount_fail_check:
2770 password will be freed at unmount time) */ 3201 password will be freed at unmount time) */
2771out: 3202out:
2772 /* zero out password before freeing */ 3203 /* zero out password before freeing */
2773 cleanup_volume_info(&volume_info);
2774 FreeXid(xid); 3204 FreeXid(xid);
2775 return rc; 3205 return rc;
2776} 3206}
2777 3207
3208/*
3209 * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
3210 * pointer may be NULL.
3211 */
2778int 3212int
2779CIFSTCon(unsigned int xid, struct cifsSesInfo *ses, 3213CIFSTCon(unsigned int xid, struct cifs_ses *ses,
2780 const char *tree, struct cifsTconInfo *tcon, 3214 const char *tree, struct cifs_tcon *tcon,
2781 const struct nls_table *nls_codepage) 3215 const struct nls_table *nls_codepage)
2782{ 3216{
2783 struct smb_hdr *smb_buffer; 3217 struct smb_hdr *smb_buffer;
@@ -2786,8 +3220,8 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2786 TCONX_RSP *pSMBr; 3220 TCONX_RSP *pSMBr;
2787 unsigned char *bcc_ptr; 3221 unsigned char *bcc_ptr;
2788 int rc = 0; 3222 int rc = 0;
2789 int length, bytes_left; 3223 int length;
2790 __u16 count; 3224 __u16 bytes_left, count;
2791 3225
2792 if (ses == NULL) 3226 if (ses == NULL)
2793 return -EIO; 3227 return -EIO;
@@ -2809,13 +3243,13 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2809 pSMB->AndXCommand = 0xFF; 3243 pSMB->AndXCommand = 0xFF;
2810 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO); 3244 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
2811 bcc_ptr = &pSMB->Password[0]; 3245 bcc_ptr = &pSMB->Password[0];
2812 if ((ses->server->secMode) & SECMODE_USER) { 3246 if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
2813 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */ 3247 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
2814 *bcc_ptr = 0; /* password is null byte */ 3248 *bcc_ptr = 0; /* password is null byte */
2815 bcc_ptr++; /* skip password */ 3249 bcc_ptr++; /* skip password */
2816 /* already aligned so no need to do it below */ 3250 /* already aligned so no need to do it below */
2817 } else { 3251 } else {
2818 pSMB->PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE); 3252 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
2819 /* BB FIXME add code to fail this if NTLMv2 or Kerberos 3253 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
2820 specified as required (when that support is added to 3254 specified as required (when that support is added to
2821 the vfs in the future) as only NTLM or the much 3255 the vfs in the future) as only NTLM or the much
@@ -2825,16 +3259,16 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2825#ifdef CONFIG_CIFS_WEAK_PW_HASH 3259#ifdef CONFIG_CIFS_WEAK_PW_HASH
2826 if ((global_secflags & CIFSSEC_MAY_LANMAN) && 3260 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
2827 (ses->server->secType == LANMAN)) 3261 (ses->server->secType == LANMAN))
2828 calc_lanman_hash(tcon->password, ses->server->cryptKey, 3262 calc_lanman_hash(tcon->password, ses->server->cryptkey,
2829 ses->server->secMode & 3263 ses->server->sec_mode &
2830 SECMODE_PW_ENCRYPT ? true : false, 3264 SECMODE_PW_ENCRYPT ? true : false,
2831 bcc_ptr); 3265 bcc_ptr);
2832 else 3266 else
2833#endif /* CIFS_WEAK_PW_HASH */ 3267#endif /* CIFS_WEAK_PW_HASH */
2834 SMBNTencrypt(tcon->password, ses->server->cryptKey, 3268 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
2835 bcc_ptr); 3269 bcc_ptr);
2836 3270
2837 bcc_ptr += CIFS_SESS_KEY_SIZE; 3271 bcc_ptr += CIFS_AUTH_RESP_SIZE;
2838 if (ses->capabilities & CAP_UNICODE) { 3272 if (ses->capabilities & CAP_UNICODE) {
2839 /* must align unicode strings */ 3273 /* must align unicode strings */
2840 *bcc_ptr = 0; /* null byte password */ 3274 *bcc_ptr = 0; /* null byte password */
@@ -2842,7 +3276,7 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2842 } 3276 }
2843 } 3277 }
2844 3278
2845 if (ses->server->secMode & 3279 if (ses->server->sec_mode &
2846 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) 3280 (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
2847 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE; 3281 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
2848 3282
@@ -2868,11 +3302,12 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2868 bcc_ptr += strlen("?????"); 3302 bcc_ptr += strlen("?????");
2869 bcc_ptr += 1; 3303 bcc_ptr += 1;
2870 count = bcc_ptr - &pSMB->Password[0]; 3304 count = bcc_ptr - &pSMB->Password[0];
2871 pSMB->hdr.smb_buf_length += count; 3305 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
3306 pSMB->hdr.smb_buf_length) + count);
2872 pSMB->ByteCount = cpu_to_le16(count); 3307 pSMB->ByteCount = cpu_to_le16(count);
2873 3308
2874 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length, 3309 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
2875 CIFS_STD_OP); 3310 0);
2876 3311
2877 /* above now done in SendReceive */ 3312 /* above now done in SendReceive */
2878 if ((rc == 0) && (tcon != NULL)) { 3313 if ((rc == 0) && (tcon != NULL)) {
@@ -2882,7 +3317,7 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2882 tcon->need_reconnect = false; 3317 tcon->need_reconnect = false;
2883 tcon->tid = smb_buffer_response->Tid; 3318 tcon->tid = smb_buffer_response->Tid;
2884 bcc_ptr = pByteArea(smb_buffer_response); 3319 bcc_ptr = pByteArea(smb_buffer_response);
2885 bytes_left = BCC(smb_buffer_response); 3320 bytes_left = get_bcc(smb_buffer_response);
2886 length = strnlen(bcc_ptr, bytes_left - 2); 3321 length = strnlen(bcc_ptr, bytes_left - 2);
2887 if (smb_buffer->Flags2 & SMBFLG2_UNICODE) 3322 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
2888 is_unicode = true; 3323 is_unicode = true;
@@ -2931,25 +3366,35 @@ CIFSTCon(unsigned int xid, struct cifsSesInfo *ses,
2931 return rc; 3366 return rc;
2932} 3367}
2933 3368
2934int 3369void
2935cifs_umount(struct super_block *sb, struct cifs_sb_info *cifs_sb) 3370cifs_umount(struct cifs_sb_info *cifs_sb)
2936{ 3371{
2937 int rc = 0; 3372 struct rb_root *root = &cifs_sb->tlink_tree;
2938 char *tmp; 3373 struct rb_node *node;
3374 struct tcon_link *tlink;
2939 3375
2940 if (cifs_sb->tcon) 3376 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
2941 cifs_put_tcon(cifs_sb->tcon);
2942 3377
2943 cifs_sb->tcon = NULL; 3378 spin_lock(&cifs_sb->tlink_tree_lock);
2944 tmp = cifs_sb->prepath; 3379 while ((node = rb_first(root))) {
2945 cifs_sb->prepathlen = 0; 3380 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
2946 cifs_sb->prepath = NULL; 3381 cifs_get_tlink(tlink);
2947 kfree(tmp); 3382 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3383 rb_erase(node, root);
2948 3384
2949 return rc; 3385 spin_unlock(&cifs_sb->tlink_tree_lock);
3386 cifs_put_tlink(tlink);
3387 spin_lock(&cifs_sb->tlink_tree_lock);
3388 }
3389 spin_unlock(&cifs_sb->tlink_tree_lock);
3390
3391 bdi_destroy(&cifs_sb->bdi);
3392 kfree(cifs_sb->mountdata);
3393 unload_nls(cifs_sb->local_nls);
3394 kfree(cifs_sb);
2950} 3395}
2951 3396
2952int cifs_negotiate_protocol(unsigned int xid, struct cifsSesInfo *ses) 3397int cifs_negotiate_protocol(unsigned int xid, struct cifs_ses *ses)
2953{ 3398{
2954 int rc = 0; 3399 int rc = 0;
2955 struct TCP_Server_Info *server = ses->server; 3400 struct TCP_Server_Info *server = ses->server;
@@ -2967,7 +3412,7 @@ int cifs_negotiate_protocol(unsigned int xid, struct cifsSesInfo *ses)
2967 } 3412 }
2968 if (rc == 0) { 3413 if (rc == 0) {
2969 spin_lock(&GlobalMid_Lock); 3414 spin_lock(&GlobalMid_Lock);
2970 if (server->tcpStatus != CifsExiting) 3415 if (server->tcpStatus == CifsNeedNegotiate)
2971 server->tcpStatus = CifsGood; 3416 server->tcpStatus = CifsGood;
2972 else 3417 else
2973 rc = -EHOSTDOWN; 3418 rc = -EHOSTDOWN;
@@ -2979,7 +3424,7 @@ int cifs_negotiate_protocol(unsigned int xid, struct cifsSesInfo *ses)
2979} 3424}
2980 3425
2981 3426
2982int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses, 3427int cifs_setup_session(unsigned int xid, struct cifs_ses *ses,
2983 struct nls_table *nls_info) 3428 struct nls_table *nls_info)
2984{ 3429{
2985 int rc = 0; 3430 int rc = 0;
@@ -2991,12 +3436,22 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses,
2991 ses->capabilities &= (~CAP_UNIX); 3436 ses->capabilities &= (~CAP_UNIX);
2992 3437
2993 cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d", 3438 cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
2994 server->secMode, server->capabilities, server->timeAdj); 3439 server->sec_mode, server->capabilities, server->timeAdj);
2995 3440
2996 rc = CIFS_SessSetup(xid, ses, nls_info); 3441 rc = CIFS_SessSetup(xid, ses, nls_info);
2997 if (rc) { 3442 if (rc) {
2998 cERROR(1, "Send error in SessSetup = %d", rc); 3443 cERROR(1, "Send error in SessSetup = %d", rc);
2999 } else { 3444 } else {
3445 mutex_lock(&ses->server->srv_mutex);
3446 if (!server->session_estab) {
3447 server->session_key.response = ses->auth_key.response;
3448 server->session_key.len = ses->auth_key.len;
3449 server->sequence_number = 0x2;
3450 server->session_estab = true;
3451 ses->auth_key.response = NULL;
3452 }
3453 mutex_unlock(&server->srv_mutex);
3454
3000 cFYI(1, "CIFS Session Established successfully"); 3455 cFYI(1, "CIFS Session Established successfully");
3001 spin_lock(&GlobalMid_Lock); 3456 spin_lock(&GlobalMid_Lock);
3002 ses->status = CifsGood; 3457 ses->status = CifsGood;
@@ -3004,6 +3459,259 @@ int cifs_setup_session(unsigned int xid, struct cifsSesInfo *ses,
3004 spin_unlock(&GlobalMid_Lock); 3459 spin_unlock(&GlobalMid_Lock);
3005 } 3460 }
3006 3461
3462 kfree(ses->auth_key.response);
3463 ses->auth_key.response = NULL;
3464 ses->auth_key.len = 0;
3465 kfree(ses->ntlmssp);
3466 ses->ntlmssp = NULL;
3467
3007 return rc; 3468 return rc;
3008} 3469}
3009 3470
3471static struct cifs_tcon *
3472cifs_construct_tcon(struct cifs_sb_info *cifs_sb, uid_t fsuid)
3473{
3474 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
3475 struct cifs_ses *ses;
3476 struct cifs_tcon *tcon = NULL;
3477 struct smb_vol *vol_info;
3478 char username[28]; /* big enough for "krb50x" + hex of ULONG_MAX 6+16 */
3479 /* We used to have this as MAX_USERNAME which is */
3480 /* way too big now (256 instead of 32) */
3481
3482 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
3483 if (vol_info == NULL) {
3484 tcon = ERR_PTR(-ENOMEM);
3485 goto out;
3486 }
3487
3488 snprintf(username, sizeof(username), "krb50x%x", fsuid);
3489 vol_info->username = username;
3490 vol_info->local_nls = cifs_sb->local_nls;
3491 vol_info->linux_uid = fsuid;
3492 vol_info->cred_uid = fsuid;
3493 vol_info->UNC = master_tcon->treeName;
3494 vol_info->retry = master_tcon->retry;
3495 vol_info->nocase = master_tcon->nocase;
3496 vol_info->local_lease = master_tcon->local_lease;
3497 vol_info->no_linux_ext = !master_tcon->unix_ext;
3498
3499 /* FIXME: allow for other secFlg settings */
3500 vol_info->secFlg = CIFSSEC_MUST_KRB5;
3501
3502 /* get a reference for the same TCP session */
3503 spin_lock(&cifs_tcp_ses_lock);
3504 ++master_tcon->ses->server->srv_count;
3505 spin_unlock(&cifs_tcp_ses_lock);
3506
3507 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
3508 if (IS_ERR(ses)) {
3509 tcon = (struct cifs_tcon *)ses;
3510 cifs_put_tcp_session(master_tcon->ses->server);
3511 goto out;
3512 }
3513
3514 tcon = cifs_get_tcon(ses, vol_info);
3515 if (IS_ERR(tcon)) {
3516 cifs_put_smb_ses(ses);
3517 goto out;
3518 }
3519
3520 if (ses->capabilities & CAP_UNIX)
3521 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
3522out:
3523 kfree(vol_info);
3524
3525 return tcon;
3526}
3527
3528struct cifs_tcon *
3529cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
3530{
3531 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
3532}
3533
3534static int
3535cifs_sb_tcon_pending_wait(void *unused)
3536{
3537 schedule();
3538 return signal_pending(current) ? -ERESTARTSYS : 0;
3539}
3540
3541/* find and return a tlink with given uid */
3542static struct tcon_link *
3543tlink_rb_search(struct rb_root *root, uid_t uid)
3544{
3545 struct rb_node *node = root->rb_node;
3546 struct tcon_link *tlink;
3547
3548 while (node) {
3549 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
3550
3551 if (tlink->tl_uid > uid)
3552 node = node->rb_left;
3553 else if (tlink->tl_uid < uid)
3554 node = node->rb_right;
3555 else
3556 return tlink;
3557 }
3558 return NULL;
3559}
3560
3561/* insert a tcon_link into the tree */
3562static void
3563tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
3564{
3565 struct rb_node **new = &(root->rb_node), *parent = NULL;
3566 struct tcon_link *tlink;
3567
3568 while (*new) {
3569 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
3570 parent = *new;
3571
3572 if (tlink->tl_uid > new_tlink->tl_uid)
3573 new = &((*new)->rb_left);
3574 else
3575 new = &((*new)->rb_right);
3576 }
3577
3578 rb_link_node(&new_tlink->tl_rbnode, parent, new);
3579 rb_insert_color(&new_tlink->tl_rbnode, root);
3580}
3581
3582/*
3583 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
3584 * current task.
3585 *
3586 * If the superblock doesn't refer to a multiuser mount, then just return
3587 * the master tcon for the mount.
3588 *
3589 * First, search the rbtree for an existing tcon for this fsuid. If one
3590 * exists, then check to see if it's pending construction. If it is then wait
3591 * for construction to complete. Once it's no longer pending, check to see if
3592 * it failed and either return an error or retry construction, depending on
3593 * the timeout.
3594 *
3595 * If one doesn't exist then insert a new tcon_link struct into the tree and
3596 * try to construct a new one.
3597 */
3598struct tcon_link *
3599cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
3600{
3601 int ret;
3602 uid_t fsuid = current_fsuid();
3603 struct tcon_link *tlink, *newtlink;
3604
3605 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
3606 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3607
3608 spin_lock(&cifs_sb->tlink_tree_lock);
3609 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3610 if (tlink)
3611 cifs_get_tlink(tlink);
3612 spin_unlock(&cifs_sb->tlink_tree_lock);
3613
3614 if (tlink == NULL) {
3615 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3616 if (newtlink == NULL)
3617 return ERR_PTR(-ENOMEM);
3618 newtlink->tl_uid = fsuid;
3619 newtlink->tl_tcon = ERR_PTR(-EACCES);
3620 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
3621 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
3622 cifs_get_tlink(newtlink);
3623
3624 spin_lock(&cifs_sb->tlink_tree_lock);
3625 /* was one inserted after previous search? */
3626 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
3627 if (tlink) {
3628 cifs_get_tlink(tlink);
3629 spin_unlock(&cifs_sb->tlink_tree_lock);
3630 kfree(newtlink);
3631 goto wait_for_construction;
3632 }
3633 tlink = newtlink;
3634 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3635 spin_unlock(&cifs_sb->tlink_tree_lock);
3636 } else {
3637wait_for_construction:
3638 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
3639 cifs_sb_tcon_pending_wait,
3640 TASK_INTERRUPTIBLE);
3641 if (ret) {
3642 cifs_put_tlink(tlink);
3643 return ERR_PTR(ret);
3644 }
3645
3646 /* if it's good, return it */
3647 if (!IS_ERR(tlink->tl_tcon))
3648 return tlink;
3649
3650 /* return error if we tried this already recently */
3651 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
3652 cifs_put_tlink(tlink);
3653 return ERR_PTR(-EACCES);
3654 }
3655
3656 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
3657 goto wait_for_construction;
3658 }
3659
3660 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
3661 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
3662 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
3663
3664 if (IS_ERR(tlink->tl_tcon)) {
3665 cifs_put_tlink(tlink);
3666 return ERR_PTR(-EACCES);
3667 }
3668
3669 return tlink;
3670}
3671
3672/*
3673 * periodic workqueue job that scans tcon_tree for a superblock and closes
3674 * out tcons.
3675 */
3676static void
3677cifs_prune_tlinks(struct work_struct *work)
3678{
3679 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
3680 prune_tlinks.work);
3681 struct rb_root *root = &cifs_sb->tlink_tree;
3682 struct rb_node *node = rb_first(root);
3683 struct rb_node *tmp;
3684 struct tcon_link *tlink;
3685
3686 /*
3687 * Because we drop the spinlock in the loop in order to put the tlink
3688 * it's not guarded against removal of links from the tree. The only
3689 * places that remove entries from the tree are this function and
3690 * umounts. Because this function is non-reentrant and is canceled
3691 * before umount can proceed, this is safe.
3692 */
3693 spin_lock(&cifs_sb->tlink_tree_lock);
3694 node = rb_first(root);
3695 while (node != NULL) {
3696 tmp = node;
3697 node = rb_next(tmp);
3698 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
3699
3700 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
3701 atomic_read(&tlink->tl_count) != 0 ||
3702 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
3703 continue;
3704
3705 cifs_get_tlink(tlink);
3706 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3707 rb_erase(tmp, root);
3708
3709 spin_unlock(&cifs_sb->tlink_tree_lock);
3710 cifs_put_tlink(tlink);
3711 spin_lock(&cifs_sb->tlink_tree_lock);
3712 }
3713 spin_unlock(&cifs_sb->tlink_tree_lock);
3714
3715 queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
3716 TLINK_IDLE_EXPIRE);
3717}