xref: /OK3568_Linux_fs/kernel/fs/cifs/misc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   fs/cifs/misc.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *   Copyright (C) International Business Machines  Corp., 2002,2008
5*4882a593Smuzhiyun  *   Author(s): Steve French (sfrench@us.ibm.com)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
8*4882a593Smuzhiyun  *   it under the terms of the GNU Lesser General Public License as published
9*4882a593Smuzhiyun  *   by the Free Software Foundation; either version 2.1 of the License, or
10*4882a593Smuzhiyun  *   (at your option) any later version.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *   This library is distributed in the hope that it will be useful,
13*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15*4882a593Smuzhiyun  *   the GNU Lesser General Public License for more details.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  *   You should have received a copy of the GNU Lesser General Public License
18*4882a593Smuzhiyun  *   along with this library; if not, write to the Free Software
19*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <linux/ctype.h>
24*4882a593Smuzhiyun #include <linux/mempool.h>
25*4882a593Smuzhiyun #include <linux/vmalloc.h>
26*4882a593Smuzhiyun #include "cifspdu.h"
27*4882a593Smuzhiyun #include "cifsglob.h"
28*4882a593Smuzhiyun #include "cifsproto.h"
29*4882a593Smuzhiyun #include "cifs_debug.h"
30*4882a593Smuzhiyun #include "smberr.h"
31*4882a593Smuzhiyun #include "nterr.h"
32*4882a593Smuzhiyun #include "cifs_unicode.h"
33*4882a593Smuzhiyun #include "smb2pdu.h"
34*4882a593Smuzhiyun #include "cifsfs.h"
35*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DFS_UPCALL
36*4882a593Smuzhiyun #include "dns_resolve.h"
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun extern mempool_t *cifs_sm_req_poolp;
40*4882a593Smuzhiyun extern mempool_t *cifs_req_poolp;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* The xid serves as a useful identifier for each incoming vfs request,
43*4882a593Smuzhiyun    in a similar way to the mid which is useful to track each sent smb,
44*4882a593Smuzhiyun    and CurrentXid can also provide a running counter (although it
45*4882a593Smuzhiyun    will eventually wrap past zero) of the total vfs operations handled
46*4882a593Smuzhiyun    since the cifs fs was mounted */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun unsigned int
_get_xid(void)49*4882a593Smuzhiyun _get_xid(void)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	unsigned int xid;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
54*4882a593Smuzhiyun 	GlobalTotalActiveXid++;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	/* keep high water mark for number of simultaneous ops in filesystem */
57*4882a593Smuzhiyun 	if (GlobalTotalActiveXid > GlobalMaxActiveXid)
58*4882a593Smuzhiyun 		GlobalMaxActiveXid = GlobalTotalActiveXid;
59*4882a593Smuzhiyun 	if (GlobalTotalActiveXid > 65000)
60*4882a593Smuzhiyun 		cifs_dbg(FYI, "warning: more than 65000 requests active\n");
61*4882a593Smuzhiyun 	xid = GlobalCurrentXid++;
62*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
63*4882a593Smuzhiyun 	return xid;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun void
_free_xid(unsigned int xid)67*4882a593Smuzhiyun _free_xid(unsigned int xid)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun 	spin_lock(&GlobalMid_Lock);
70*4882a593Smuzhiyun 	/* if (GlobalTotalActiveXid == 0)
71*4882a593Smuzhiyun 		BUG(); */
72*4882a593Smuzhiyun 	GlobalTotalActiveXid--;
73*4882a593Smuzhiyun 	spin_unlock(&GlobalMid_Lock);
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun struct cifs_ses *
sesInfoAlloc(void)77*4882a593Smuzhiyun sesInfoAlloc(void)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	struct cifs_ses *ret_buf;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	ret_buf = kzalloc(sizeof(struct cifs_ses), GFP_KERNEL);
82*4882a593Smuzhiyun 	if (ret_buf) {
83*4882a593Smuzhiyun 		atomic_inc(&sesInfoAllocCount);
84*4882a593Smuzhiyun 		ret_buf->status = CifsNew;
85*4882a593Smuzhiyun 		++ret_buf->ses_count;
86*4882a593Smuzhiyun 		INIT_LIST_HEAD(&ret_buf->smb_ses_list);
87*4882a593Smuzhiyun 		INIT_LIST_HEAD(&ret_buf->tcon_list);
88*4882a593Smuzhiyun 		mutex_init(&ret_buf->session_mutex);
89*4882a593Smuzhiyun 		spin_lock_init(&ret_buf->iface_lock);
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 	return ret_buf;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun void
sesInfoFree(struct cifs_ses * buf_to_free)95*4882a593Smuzhiyun sesInfoFree(struct cifs_ses *buf_to_free)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	if (buf_to_free == NULL) {
98*4882a593Smuzhiyun 		cifs_dbg(FYI, "Null buffer passed to sesInfoFree\n");
99*4882a593Smuzhiyun 		return;
100*4882a593Smuzhiyun 	}
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	atomic_dec(&sesInfoAllocCount);
103*4882a593Smuzhiyun 	kfree(buf_to_free->serverOS);
104*4882a593Smuzhiyun 	kfree(buf_to_free->serverDomain);
105*4882a593Smuzhiyun 	kfree(buf_to_free->serverNOS);
106*4882a593Smuzhiyun 	kfree_sensitive(buf_to_free->password);
107*4882a593Smuzhiyun 	kfree(buf_to_free->user_name);
108*4882a593Smuzhiyun 	kfree(buf_to_free->domainName);
109*4882a593Smuzhiyun 	kfree_sensitive(buf_to_free->auth_key.response);
110*4882a593Smuzhiyun 	kfree(buf_to_free->iface_list);
111*4882a593Smuzhiyun 	kfree_sensitive(buf_to_free);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun struct cifs_tcon *
tconInfoAlloc(void)115*4882a593Smuzhiyun tconInfoAlloc(void)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	struct cifs_tcon *ret_buf;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	ret_buf = kzalloc(sizeof(*ret_buf), GFP_KERNEL);
120*4882a593Smuzhiyun 	if (!ret_buf)
121*4882a593Smuzhiyun 		return NULL;
122*4882a593Smuzhiyun 	ret_buf->crfid.fid = kzalloc(sizeof(*ret_buf->crfid.fid), GFP_KERNEL);
123*4882a593Smuzhiyun 	if (!ret_buf->crfid.fid) {
124*4882a593Smuzhiyun 		kfree(ret_buf);
125*4882a593Smuzhiyun 		return NULL;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	atomic_inc(&tconInfoAllocCount);
129*4882a593Smuzhiyun 	ret_buf->tidStatus = CifsNew;
130*4882a593Smuzhiyun 	++ret_buf->tc_count;
131*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ret_buf->openFileList);
132*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ret_buf->tcon_list);
133*4882a593Smuzhiyun 	spin_lock_init(&ret_buf->open_file_lock);
134*4882a593Smuzhiyun 	mutex_init(&ret_buf->crfid.fid_mutex);
135*4882a593Smuzhiyun 	spin_lock_init(&ret_buf->stat_lock);
136*4882a593Smuzhiyun 	atomic_set(&ret_buf->num_local_opens, 0);
137*4882a593Smuzhiyun 	atomic_set(&ret_buf->num_remote_opens, 0);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	return ret_buf;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun void
tconInfoFree(struct cifs_tcon * buf_to_free)143*4882a593Smuzhiyun tconInfoFree(struct cifs_tcon *buf_to_free)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	if (buf_to_free == NULL) {
146*4882a593Smuzhiyun 		cifs_dbg(FYI, "Null buffer passed to tconInfoFree\n");
147*4882a593Smuzhiyun 		return;
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 	atomic_dec(&tconInfoAllocCount);
150*4882a593Smuzhiyun 	kfree(buf_to_free->nativeFileSystem);
151*4882a593Smuzhiyun 	kfree_sensitive(buf_to_free->password);
152*4882a593Smuzhiyun 	kfree(buf_to_free->crfid.fid);
153*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DFS_UPCALL
154*4882a593Smuzhiyun 	kfree(buf_to_free->dfs_path);
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun 	kfree(buf_to_free);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun struct smb_hdr *
cifs_buf_get(void)160*4882a593Smuzhiyun cifs_buf_get(void)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct smb_hdr *ret_buf = NULL;
163*4882a593Smuzhiyun 	/*
164*4882a593Smuzhiyun 	 * SMB2 header is bigger than CIFS one - no problems to clean some
165*4882a593Smuzhiyun 	 * more bytes for CIFS.
166*4882a593Smuzhiyun 	 */
167*4882a593Smuzhiyun 	size_t buf_size = sizeof(struct smb2_sync_hdr);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/*
170*4882a593Smuzhiyun 	 * We could use negotiated size instead of max_msgsize -
171*4882a593Smuzhiyun 	 * but it may be more efficient to always alloc same size
172*4882a593Smuzhiyun 	 * albeit slightly larger than necessary and maxbuffersize
173*4882a593Smuzhiyun 	 * defaults to this and can not be bigger.
174*4882a593Smuzhiyun 	 */
175*4882a593Smuzhiyun 	ret_buf = mempool_alloc(cifs_req_poolp, GFP_NOFS);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* clear the first few header bytes */
178*4882a593Smuzhiyun 	/* for most paths, more is cleared in header_assemble */
179*4882a593Smuzhiyun 	memset(ret_buf, 0, buf_size + 3);
180*4882a593Smuzhiyun 	atomic_inc(&bufAllocCount);
181*4882a593Smuzhiyun #ifdef CONFIG_CIFS_STATS2
182*4882a593Smuzhiyun 	atomic_inc(&totBufAllocCount);
183*4882a593Smuzhiyun #endif /* CONFIG_CIFS_STATS2 */
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	return ret_buf;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun void
cifs_buf_release(void * buf_to_free)189*4882a593Smuzhiyun cifs_buf_release(void *buf_to_free)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	if (buf_to_free == NULL) {
192*4882a593Smuzhiyun 		/* cifs_dbg(FYI, "Null buffer passed to cifs_buf_release\n");*/
193*4882a593Smuzhiyun 		return;
194*4882a593Smuzhiyun 	}
195*4882a593Smuzhiyun 	mempool_free(buf_to_free, cifs_req_poolp);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	atomic_dec(&bufAllocCount);
198*4882a593Smuzhiyun 	return;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun struct smb_hdr *
cifs_small_buf_get(void)202*4882a593Smuzhiyun cifs_small_buf_get(void)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	struct smb_hdr *ret_buf = NULL;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun /* We could use negotiated size instead of max_msgsize -
207*4882a593Smuzhiyun    but it may be more efficient to always alloc same size
208*4882a593Smuzhiyun    albeit slightly larger than necessary and maxbuffersize
209*4882a593Smuzhiyun    defaults to this and can not be bigger */
210*4882a593Smuzhiyun 	ret_buf = mempool_alloc(cifs_sm_req_poolp, GFP_NOFS);
211*4882a593Smuzhiyun 	/* No need to clear memory here, cleared in header assemble */
212*4882a593Smuzhiyun 	/*	memset(ret_buf, 0, sizeof(struct smb_hdr) + 27);*/
213*4882a593Smuzhiyun 	atomic_inc(&smBufAllocCount);
214*4882a593Smuzhiyun #ifdef CONFIG_CIFS_STATS2
215*4882a593Smuzhiyun 	atomic_inc(&totSmBufAllocCount);
216*4882a593Smuzhiyun #endif /* CONFIG_CIFS_STATS2 */
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	return ret_buf;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun void
cifs_small_buf_release(void * buf_to_free)222*4882a593Smuzhiyun cifs_small_buf_release(void *buf_to_free)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	if (buf_to_free == NULL) {
226*4882a593Smuzhiyun 		cifs_dbg(FYI, "Null buffer passed to cifs_small_buf_release\n");
227*4882a593Smuzhiyun 		return;
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun 	mempool_free(buf_to_free, cifs_sm_req_poolp);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	atomic_dec(&smBufAllocCount);
232*4882a593Smuzhiyun 	return;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun void
free_rsp_buf(int resp_buftype,void * rsp)236*4882a593Smuzhiyun free_rsp_buf(int resp_buftype, void *rsp)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	if (resp_buftype == CIFS_SMALL_BUFFER)
239*4882a593Smuzhiyun 		cifs_small_buf_release(rsp);
240*4882a593Smuzhiyun 	else if (resp_buftype == CIFS_LARGE_BUFFER)
241*4882a593Smuzhiyun 		cifs_buf_release(rsp);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /* NB: MID can not be set if treeCon not passed in, in that
245*4882a593Smuzhiyun    case it is responsbility of caller to set the mid */
246*4882a593Smuzhiyun void
header_assemble(struct smb_hdr * buffer,char smb_command,const struct cifs_tcon * treeCon,int word_count)247*4882a593Smuzhiyun header_assemble(struct smb_hdr *buffer, char smb_command /* command */ ,
248*4882a593Smuzhiyun 		const struct cifs_tcon *treeCon, int word_count
249*4882a593Smuzhiyun 		/* length of fixed section (word count) in two byte units  */)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun 	char *temp = (char *) buffer;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	memset(temp, 0, 256); /* bigger than MAX_CIFS_HDR_SIZE */
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	buffer->smb_buf_length = cpu_to_be32(
256*4882a593Smuzhiyun 	    (2 * word_count) + sizeof(struct smb_hdr) -
257*4882a593Smuzhiyun 	    4 /*  RFC 1001 length field does not count */  +
258*4882a593Smuzhiyun 	    2 /* for bcc field itself */) ;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	buffer->Protocol[0] = 0xFF;
261*4882a593Smuzhiyun 	buffer->Protocol[1] = 'S';
262*4882a593Smuzhiyun 	buffer->Protocol[2] = 'M';
263*4882a593Smuzhiyun 	buffer->Protocol[3] = 'B';
264*4882a593Smuzhiyun 	buffer->Command = smb_command;
265*4882a593Smuzhiyun 	buffer->Flags = 0x00;	/* case sensitive */
266*4882a593Smuzhiyun 	buffer->Flags2 = SMBFLG2_KNOWS_LONG_NAMES;
267*4882a593Smuzhiyun 	buffer->Pid = cpu_to_le16((__u16)current->tgid);
268*4882a593Smuzhiyun 	buffer->PidHigh = cpu_to_le16((__u16)(current->tgid >> 16));
269*4882a593Smuzhiyun 	if (treeCon) {
270*4882a593Smuzhiyun 		buffer->Tid = treeCon->tid;
271*4882a593Smuzhiyun 		if (treeCon->ses) {
272*4882a593Smuzhiyun 			if (treeCon->ses->capabilities & CAP_UNICODE)
273*4882a593Smuzhiyun 				buffer->Flags2 |= SMBFLG2_UNICODE;
274*4882a593Smuzhiyun 			if (treeCon->ses->capabilities & CAP_STATUS32)
275*4882a593Smuzhiyun 				buffer->Flags2 |= SMBFLG2_ERR_STATUS;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 			/* Uid is not converted */
278*4882a593Smuzhiyun 			buffer->Uid = treeCon->ses->Suid;
279*4882a593Smuzhiyun 			buffer->Mid = get_next_mid(treeCon->ses->server);
280*4882a593Smuzhiyun 		}
281*4882a593Smuzhiyun 		if (treeCon->Flags & SMB_SHARE_IS_IN_DFS)
282*4882a593Smuzhiyun 			buffer->Flags2 |= SMBFLG2_DFS;
283*4882a593Smuzhiyun 		if (treeCon->nocase)
284*4882a593Smuzhiyun 			buffer->Flags  |= SMBFLG_CASELESS;
285*4882a593Smuzhiyun 		if ((treeCon->ses) && (treeCon->ses->server))
286*4882a593Smuzhiyun 			if (treeCon->ses->server->sign)
287*4882a593Smuzhiyun 				buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun /*  endian conversion of flags is now done just before sending */
291*4882a593Smuzhiyun 	buffer->WordCount = (char) word_count;
292*4882a593Smuzhiyun 	return;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun static int
check_smb_hdr(struct smb_hdr * smb)296*4882a593Smuzhiyun check_smb_hdr(struct smb_hdr *smb)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	/* does it have the right SMB "signature" ? */
299*4882a593Smuzhiyun 	if (*(__le32 *) smb->Protocol != cpu_to_le32(0x424d53ff)) {
300*4882a593Smuzhiyun 		cifs_dbg(VFS, "Bad protocol string signature header 0x%x\n",
301*4882a593Smuzhiyun 			 *(unsigned int *)smb->Protocol);
302*4882a593Smuzhiyun 		return 1;
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	/* if it's a response then accept */
306*4882a593Smuzhiyun 	if (smb->Flags & SMBFLG_RESPONSE)
307*4882a593Smuzhiyun 		return 0;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/* only one valid case where server sends us request */
310*4882a593Smuzhiyun 	if (smb->Command == SMB_COM_LOCKING_ANDX)
311*4882a593Smuzhiyun 		return 0;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	cifs_dbg(VFS, "Server sent request, not response. mid=%u\n",
314*4882a593Smuzhiyun 		 get_mid(smb));
315*4882a593Smuzhiyun 	return 1;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun int
checkSMB(char * buf,unsigned int total_read,struct TCP_Server_Info * server)319*4882a593Smuzhiyun checkSMB(char *buf, unsigned int total_read, struct TCP_Server_Info *server)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	struct smb_hdr *smb = (struct smb_hdr *)buf;
322*4882a593Smuzhiyun 	__u32 rfclen = be32_to_cpu(smb->smb_buf_length);
323*4882a593Smuzhiyun 	__u32 clc_len;  /* calculated length */
324*4882a593Smuzhiyun 	cifs_dbg(FYI, "checkSMB Length: 0x%x, smb_buf_length: 0x%x\n",
325*4882a593Smuzhiyun 		 total_read, rfclen);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/* is this frame too small to even get to a BCC? */
328*4882a593Smuzhiyun 	if (total_read < 2 + sizeof(struct smb_hdr)) {
329*4882a593Smuzhiyun 		if ((total_read >= sizeof(struct smb_hdr) - 1)
330*4882a593Smuzhiyun 			    && (smb->Status.CifsError != 0)) {
331*4882a593Smuzhiyun 			/* it's an error return */
332*4882a593Smuzhiyun 			smb->WordCount = 0;
333*4882a593Smuzhiyun 			/* some error cases do not return wct and bcc */
334*4882a593Smuzhiyun 			return 0;
335*4882a593Smuzhiyun 		} else if ((total_read == sizeof(struct smb_hdr) + 1) &&
336*4882a593Smuzhiyun 				(smb->WordCount == 0)) {
337*4882a593Smuzhiyun 			char *tmp = (char *)smb;
338*4882a593Smuzhiyun 			/* Need to work around a bug in two servers here */
339*4882a593Smuzhiyun 			/* First, check if the part of bcc they sent was zero */
340*4882a593Smuzhiyun 			if (tmp[sizeof(struct smb_hdr)] == 0) {
341*4882a593Smuzhiyun 				/* some servers return only half of bcc
342*4882a593Smuzhiyun 				 * on simple responses (wct, bcc both zero)
343*4882a593Smuzhiyun 				 * in particular have seen this on
344*4882a593Smuzhiyun 				 * ulogoffX and FindClose. This leaves
345*4882a593Smuzhiyun 				 * one byte of bcc potentially unitialized
346*4882a593Smuzhiyun 				 */
347*4882a593Smuzhiyun 				/* zero rest of bcc */
348*4882a593Smuzhiyun 				tmp[sizeof(struct smb_hdr)+1] = 0;
349*4882a593Smuzhiyun 				return 0;
350*4882a593Smuzhiyun 			}
351*4882a593Smuzhiyun 			cifs_dbg(VFS, "rcvd invalid byte count (bcc)\n");
352*4882a593Smuzhiyun 		} else {
353*4882a593Smuzhiyun 			cifs_dbg(VFS, "Length less than smb header size\n");
354*4882a593Smuzhiyun 		}
355*4882a593Smuzhiyun 		return -EIO;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/* otherwise, there is enough to get to the BCC */
359*4882a593Smuzhiyun 	if (check_smb_hdr(smb))
360*4882a593Smuzhiyun 		return -EIO;
361*4882a593Smuzhiyun 	clc_len = smbCalcSize(smb, server);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	if (4 + rfclen != total_read) {
364*4882a593Smuzhiyun 		cifs_dbg(VFS, "Length read does not match RFC1001 length %d\n",
365*4882a593Smuzhiyun 			 rfclen);
366*4882a593Smuzhiyun 		return -EIO;
367*4882a593Smuzhiyun 	}
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	if (4 + rfclen != clc_len) {
370*4882a593Smuzhiyun 		__u16 mid = get_mid(smb);
371*4882a593Smuzhiyun 		/* check if bcc wrapped around for large read responses */
372*4882a593Smuzhiyun 		if ((rfclen > 64 * 1024) && (rfclen > clc_len)) {
373*4882a593Smuzhiyun 			/* check if lengths match mod 64K */
374*4882a593Smuzhiyun 			if (((4 + rfclen) & 0xFFFF) == (clc_len & 0xFFFF))
375*4882a593Smuzhiyun 				return 0; /* bcc wrapped */
376*4882a593Smuzhiyun 		}
377*4882a593Smuzhiyun 		cifs_dbg(FYI, "Calculated size %u vs length %u mismatch for mid=%u\n",
378*4882a593Smuzhiyun 			 clc_len, 4 + rfclen, mid);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 		if (4 + rfclen < clc_len) {
381*4882a593Smuzhiyun 			cifs_dbg(VFS, "RFC1001 size %u smaller than SMB for mid=%u\n",
382*4882a593Smuzhiyun 				 rfclen, mid);
383*4882a593Smuzhiyun 			return -EIO;
384*4882a593Smuzhiyun 		} else if (rfclen > clc_len + 512) {
385*4882a593Smuzhiyun 			/*
386*4882a593Smuzhiyun 			 * Some servers (Windows XP in particular) send more
387*4882a593Smuzhiyun 			 * data than the lengths in the SMB packet would
388*4882a593Smuzhiyun 			 * indicate on certain calls (byte range locks and
389*4882a593Smuzhiyun 			 * trans2 find first calls in particular). While the
390*4882a593Smuzhiyun 			 * client can handle such a frame by ignoring the
391*4882a593Smuzhiyun 			 * trailing data, we choose limit the amount of extra
392*4882a593Smuzhiyun 			 * data to 512 bytes.
393*4882a593Smuzhiyun 			 */
394*4882a593Smuzhiyun 			cifs_dbg(VFS, "RFC1001 size %u more than 512 bytes larger than SMB for mid=%u\n",
395*4882a593Smuzhiyun 				 rfclen, mid);
396*4882a593Smuzhiyun 			return -EIO;
397*4882a593Smuzhiyun 		}
398*4882a593Smuzhiyun 	}
399*4882a593Smuzhiyun 	return 0;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun bool
is_valid_oplock_break(char * buffer,struct TCP_Server_Info * srv)403*4882a593Smuzhiyun is_valid_oplock_break(char *buffer, struct TCP_Server_Info *srv)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	struct smb_hdr *buf = (struct smb_hdr *)buffer;
406*4882a593Smuzhiyun 	struct smb_com_lock_req *pSMB = (struct smb_com_lock_req *)buf;
407*4882a593Smuzhiyun 	struct list_head *tmp, *tmp1, *tmp2;
408*4882a593Smuzhiyun 	struct cifs_ses *ses;
409*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
410*4882a593Smuzhiyun 	struct cifsInodeInfo *pCifsInode;
411*4882a593Smuzhiyun 	struct cifsFileInfo *netfile;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	cifs_dbg(FYI, "Checking for oplock break or dnotify response\n");
414*4882a593Smuzhiyun 	if ((pSMB->hdr.Command == SMB_COM_NT_TRANSACT) &&
415*4882a593Smuzhiyun 	   (pSMB->hdr.Flags & SMBFLG_RESPONSE)) {
416*4882a593Smuzhiyun 		struct smb_com_transaction_change_notify_rsp *pSMBr =
417*4882a593Smuzhiyun 			(struct smb_com_transaction_change_notify_rsp *)buf;
418*4882a593Smuzhiyun 		struct file_notify_information *pnotify;
419*4882a593Smuzhiyun 		__u32 data_offset = 0;
420*4882a593Smuzhiyun 		size_t len = srv->total_read - sizeof(pSMBr->hdr.smb_buf_length);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 		if (get_bcc(buf) > sizeof(struct file_notify_information)) {
423*4882a593Smuzhiyun 			data_offset = le32_to_cpu(pSMBr->DataOffset);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 			if (data_offset >
426*4882a593Smuzhiyun 			    len - sizeof(struct file_notify_information)) {
427*4882a593Smuzhiyun 				cifs_dbg(FYI, "Invalid data_offset %u\n",
428*4882a593Smuzhiyun 					 data_offset);
429*4882a593Smuzhiyun 				return true;
430*4882a593Smuzhiyun 			}
431*4882a593Smuzhiyun 			pnotify = (struct file_notify_information *)
432*4882a593Smuzhiyun 				((char *)&pSMBr->hdr.Protocol + data_offset);
433*4882a593Smuzhiyun 			cifs_dbg(FYI, "dnotify on %s Action: 0x%x\n",
434*4882a593Smuzhiyun 				 pnotify->FileName, pnotify->Action);
435*4882a593Smuzhiyun 			/*   cifs_dump_mem("Rcvd notify Data: ",buf,
436*4882a593Smuzhiyun 				sizeof(struct smb_hdr)+60); */
437*4882a593Smuzhiyun 			return true;
438*4882a593Smuzhiyun 		}
439*4882a593Smuzhiyun 		if (pSMBr->hdr.Status.CifsError) {
440*4882a593Smuzhiyun 			cifs_dbg(FYI, "notify err 0x%x\n",
441*4882a593Smuzhiyun 				 pSMBr->hdr.Status.CifsError);
442*4882a593Smuzhiyun 			return true;
443*4882a593Smuzhiyun 		}
444*4882a593Smuzhiyun 		return false;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun 	if (pSMB->hdr.Command != SMB_COM_LOCKING_ANDX)
447*4882a593Smuzhiyun 		return false;
448*4882a593Smuzhiyun 	if (pSMB->hdr.Flags & SMBFLG_RESPONSE) {
449*4882a593Smuzhiyun 		/* no sense logging error on invalid handle on oplock
450*4882a593Smuzhiyun 		   break - harmless race between close request and oplock
451*4882a593Smuzhiyun 		   break response is expected from time to time writing out
452*4882a593Smuzhiyun 		   large dirty files cached on the client */
453*4882a593Smuzhiyun 		if ((NT_STATUS_INVALID_HANDLE) ==
454*4882a593Smuzhiyun 		   le32_to_cpu(pSMB->hdr.Status.CifsError)) {
455*4882a593Smuzhiyun 			cifs_dbg(FYI, "Invalid handle on oplock break\n");
456*4882a593Smuzhiyun 			return true;
457*4882a593Smuzhiyun 		} else if (ERRbadfid ==
458*4882a593Smuzhiyun 		   le16_to_cpu(pSMB->hdr.Status.DosError.Error)) {
459*4882a593Smuzhiyun 			return true;
460*4882a593Smuzhiyun 		} else {
461*4882a593Smuzhiyun 			return false; /* on valid oplock brk we get "request" */
462*4882a593Smuzhiyun 		}
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 	if (pSMB->hdr.WordCount != 8)
465*4882a593Smuzhiyun 		return false;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	cifs_dbg(FYI, "oplock type 0x%x level 0x%x\n",
468*4882a593Smuzhiyun 		 pSMB->LockType, pSMB->OplockLevel);
469*4882a593Smuzhiyun 	if (!(pSMB->LockType & LOCKING_ANDX_OPLOCK_RELEASE))
470*4882a593Smuzhiyun 		return false;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	/* look up tcon based on tid & uid */
473*4882a593Smuzhiyun 	spin_lock(&cifs_tcp_ses_lock);
474*4882a593Smuzhiyun 	list_for_each(tmp, &srv->smb_ses_list) {
475*4882a593Smuzhiyun 		ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
476*4882a593Smuzhiyun 		list_for_each(tmp1, &ses->tcon_list) {
477*4882a593Smuzhiyun 			tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
478*4882a593Smuzhiyun 			if (tcon->tid != buf->Tid)
479*4882a593Smuzhiyun 				continue;
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 			cifs_stats_inc(&tcon->stats.cifs_stats.num_oplock_brks);
482*4882a593Smuzhiyun 			spin_lock(&tcon->open_file_lock);
483*4882a593Smuzhiyun 			list_for_each(tmp2, &tcon->openFileList) {
484*4882a593Smuzhiyun 				netfile = list_entry(tmp2, struct cifsFileInfo,
485*4882a593Smuzhiyun 						     tlist);
486*4882a593Smuzhiyun 				if (pSMB->Fid != netfile->fid.netfid)
487*4882a593Smuzhiyun 					continue;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 				cifs_dbg(FYI, "file id match, oplock break\n");
490*4882a593Smuzhiyun 				pCifsInode = CIFS_I(d_inode(netfile->dentry));
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 				set_bit(CIFS_INODE_PENDING_OPLOCK_BREAK,
493*4882a593Smuzhiyun 					&pCifsInode->flags);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 				netfile->oplock_epoch = 0;
496*4882a593Smuzhiyun 				netfile->oplock_level = pSMB->OplockLevel;
497*4882a593Smuzhiyun 				netfile->oplock_break_cancelled = false;
498*4882a593Smuzhiyun 				cifs_queue_oplock_break(netfile);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 				spin_unlock(&tcon->open_file_lock);
501*4882a593Smuzhiyun 				spin_unlock(&cifs_tcp_ses_lock);
502*4882a593Smuzhiyun 				return true;
503*4882a593Smuzhiyun 			}
504*4882a593Smuzhiyun 			spin_unlock(&tcon->open_file_lock);
505*4882a593Smuzhiyun 			spin_unlock(&cifs_tcp_ses_lock);
506*4882a593Smuzhiyun 			cifs_dbg(FYI, "No matching file for oplock break\n");
507*4882a593Smuzhiyun 			return true;
508*4882a593Smuzhiyun 		}
509*4882a593Smuzhiyun 	}
510*4882a593Smuzhiyun 	spin_unlock(&cifs_tcp_ses_lock);
511*4882a593Smuzhiyun 	cifs_dbg(FYI, "Can not process oplock break for non-existent connection\n");
512*4882a593Smuzhiyun 	return true;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun void
dump_smb(void * buf,int smb_buf_length)516*4882a593Smuzhiyun dump_smb(void *buf, int smb_buf_length)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun 	if (traceSMB == 0)
519*4882a593Smuzhiyun 		return;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE, 8, 2, buf,
522*4882a593Smuzhiyun 		       smb_buf_length, true);
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun void
cifs_autodisable_serverino(struct cifs_sb_info * cifs_sb)526*4882a593Smuzhiyun cifs_autodisable_serverino(struct cifs_sb_info *cifs_sb)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
529*4882a593Smuzhiyun 		struct cifs_tcon *tcon = NULL;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		if (cifs_sb->master_tlink)
532*4882a593Smuzhiyun 			tcon = cifs_sb_master_tcon(cifs_sb);
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 		cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
535*4882a593Smuzhiyun 		cifs_sb->mnt_cifs_serverino_autodisabled = true;
536*4882a593Smuzhiyun 		cifs_dbg(VFS, "Autodisabling the use of server inode numbers on %s\n",
537*4882a593Smuzhiyun 			 tcon ? tcon->treeName : "new server");
538*4882a593Smuzhiyun 		cifs_dbg(VFS, "The server doesn't seem to support them properly or the files might be on different servers (DFS)\n");
539*4882a593Smuzhiyun 		cifs_dbg(VFS, "Hardlinks will not be recognized on this mount. Consider mounting with the \"noserverino\" option to silence this message.\n");
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun 
cifs_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock)544*4882a593Smuzhiyun void cifs_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	oplock &= 0xF;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	if (oplock == OPLOCK_EXCLUSIVE) {
549*4882a593Smuzhiyun 		cinode->oplock = CIFS_CACHE_WRITE_FLG | CIFS_CACHE_READ_FLG;
550*4882a593Smuzhiyun 		cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
551*4882a593Smuzhiyun 			 &cinode->vfs_inode);
552*4882a593Smuzhiyun 	} else if (oplock == OPLOCK_READ) {
553*4882a593Smuzhiyun 		cinode->oplock = CIFS_CACHE_READ_FLG;
554*4882a593Smuzhiyun 		cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
555*4882a593Smuzhiyun 			 &cinode->vfs_inode);
556*4882a593Smuzhiyun 	} else
557*4882a593Smuzhiyun 		cinode->oplock = 0;
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun /*
561*4882a593Smuzhiyun  * We wait for oplock breaks to be processed before we attempt to perform
562*4882a593Smuzhiyun  * writes.
563*4882a593Smuzhiyun  */
cifs_get_writer(struct cifsInodeInfo * cinode)564*4882a593Smuzhiyun int cifs_get_writer(struct cifsInodeInfo *cinode)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun 	int rc;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun start:
569*4882a593Smuzhiyun 	rc = wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK,
570*4882a593Smuzhiyun 			 TASK_KILLABLE);
571*4882a593Smuzhiyun 	if (rc)
572*4882a593Smuzhiyun 		return rc;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	spin_lock(&cinode->writers_lock);
575*4882a593Smuzhiyun 	if (!cinode->writers)
576*4882a593Smuzhiyun 		set_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
577*4882a593Smuzhiyun 	cinode->writers++;
578*4882a593Smuzhiyun 	/* Check to see if we have started servicing an oplock break */
579*4882a593Smuzhiyun 	if (test_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags)) {
580*4882a593Smuzhiyun 		cinode->writers--;
581*4882a593Smuzhiyun 		if (cinode->writers == 0) {
582*4882a593Smuzhiyun 			clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
583*4882a593Smuzhiyun 			wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
584*4882a593Smuzhiyun 		}
585*4882a593Smuzhiyun 		spin_unlock(&cinode->writers_lock);
586*4882a593Smuzhiyun 		goto start;
587*4882a593Smuzhiyun 	}
588*4882a593Smuzhiyun 	spin_unlock(&cinode->writers_lock);
589*4882a593Smuzhiyun 	return 0;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun 
cifs_put_writer(struct cifsInodeInfo * cinode)592*4882a593Smuzhiyun void cifs_put_writer(struct cifsInodeInfo *cinode)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	spin_lock(&cinode->writers_lock);
595*4882a593Smuzhiyun 	cinode->writers--;
596*4882a593Smuzhiyun 	if (cinode->writers == 0) {
597*4882a593Smuzhiyun 		clear_bit(CIFS_INODE_PENDING_WRITERS, &cinode->flags);
598*4882a593Smuzhiyun 		wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS);
599*4882a593Smuzhiyun 	}
600*4882a593Smuzhiyun 	spin_unlock(&cinode->writers_lock);
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun  * cifs_queue_oplock_break - queue the oplock break handler for cfile
605*4882a593Smuzhiyun  *
606*4882a593Smuzhiyun  * This function is called from the demultiplex thread when it
607*4882a593Smuzhiyun  * receives an oplock break for @cfile.
608*4882a593Smuzhiyun  *
609*4882a593Smuzhiyun  * Assumes the tcon->open_file_lock is held.
610*4882a593Smuzhiyun  * Assumes cfile->file_info_lock is NOT held.
611*4882a593Smuzhiyun  */
cifs_queue_oplock_break(struct cifsFileInfo * cfile)612*4882a593Smuzhiyun void cifs_queue_oplock_break(struct cifsFileInfo *cfile)
613*4882a593Smuzhiyun {
614*4882a593Smuzhiyun 	/*
615*4882a593Smuzhiyun 	 * Bump the handle refcount now while we hold the
616*4882a593Smuzhiyun 	 * open_file_lock to enforce the validity of it for the oplock
617*4882a593Smuzhiyun 	 * break handler. The matching put is done at the end of the
618*4882a593Smuzhiyun 	 * handler.
619*4882a593Smuzhiyun 	 */
620*4882a593Smuzhiyun 	cifsFileInfo_get(cfile);
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	queue_work(cifsoplockd_wq, &cfile->oplock_break);
623*4882a593Smuzhiyun }
624*4882a593Smuzhiyun 
cifs_done_oplock_break(struct cifsInodeInfo * cinode)625*4882a593Smuzhiyun void cifs_done_oplock_break(struct cifsInodeInfo *cinode)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	clear_bit(CIFS_INODE_PENDING_OPLOCK_BREAK, &cinode->flags);
628*4882a593Smuzhiyun 	wake_up_bit(&cinode->flags, CIFS_INODE_PENDING_OPLOCK_BREAK);
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun bool
backup_cred(struct cifs_sb_info * cifs_sb)632*4882a593Smuzhiyun backup_cred(struct cifs_sb_info *cifs_sb)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPUID) {
635*4882a593Smuzhiyun 		if (uid_eq(cifs_sb->mnt_backupuid, current_fsuid()))
636*4882a593Smuzhiyun 			return true;
637*4882a593Smuzhiyun 	}
638*4882a593Smuzhiyun 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_BACKUPGID) {
639*4882a593Smuzhiyun 		if (in_group_p(cifs_sb->mnt_backupgid))
640*4882a593Smuzhiyun 			return true;
641*4882a593Smuzhiyun 	}
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	return false;
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun void
cifs_del_pending_open(struct cifs_pending_open * open)647*4882a593Smuzhiyun cifs_del_pending_open(struct cifs_pending_open *open)
648*4882a593Smuzhiyun {
649*4882a593Smuzhiyun 	spin_lock(&tlink_tcon(open->tlink)->open_file_lock);
650*4882a593Smuzhiyun 	list_del(&open->olist);
651*4882a593Smuzhiyun 	spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
652*4882a593Smuzhiyun }
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun void
cifs_add_pending_open_locked(struct cifs_fid * fid,struct tcon_link * tlink,struct cifs_pending_open * open)655*4882a593Smuzhiyun cifs_add_pending_open_locked(struct cifs_fid *fid, struct tcon_link *tlink,
656*4882a593Smuzhiyun 			     struct cifs_pending_open *open)
657*4882a593Smuzhiyun {
658*4882a593Smuzhiyun 	memcpy(open->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
659*4882a593Smuzhiyun 	open->oplock = CIFS_OPLOCK_NO_CHANGE;
660*4882a593Smuzhiyun 	open->tlink = tlink;
661*4882a593Smuzhiyun 	fid->pending_open = open;
662*4882a593Smuzhiyun 	list_add_tail(&open->olist, &tlink_tcon(tlink)->pending_opens);
663*4882a593Smuzhiyun }
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun void
cifs_add_pending_open(struct cifs_fid * fid,struct tcon_link * tlink,struct cifs_pending_open * open)666*4882a593Smuzhiyun cifs_add_pending_open(struct cifs_fid *fid, struct tcon_link *tlink,
667*4882a593Smuzhiyun 		      struct cifs_pending_open *open)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	spin_lock(&tlink_tcon(tlink)->open_file_lock);
670*4882a593Smuzhiyun 	cifs_add_pending_open_locked(fid, tlink, open);
671*4882a593Smuzhiyun 	spin_unlock(&tlink_tcon(open->tlink)->open_file_lock);
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun /* parses DFS refferal V3 structure
675*4882a593Smuzhiyun  * caller is responsible for freeing target_nodes
676*4882a593Smuzhiyun  * returns:
677*4882a593Smuzhiyun  * - on success - 0
678*4882a593Smuzhiyun  * - on failure - errno
679*4882a593Smuzhiyun  */
680*4882a593Smuzhiyun int
parse_dfs_referrals(struct get_dfs_referral_rsp * rsp,u32 rsp_size,unsigned int * num_of_nodes,struct dfs_info3_param ** target_nodes,const struct nls_table * nls_codepage,int remap,const char * searchName,bool is_unicode)681*4882a593Smuzhiyun parse_dfs_referrals(struct get_dfs_referral_rsp *rsp, u32 rsp_size,
682*4882a593Smuzhiyun 		    unsigned int *num_of_nodes,
683*4882a593Smuzhiyun 		    struct dfs_info3_param **target_nodes,
684*4882a593Smuzhiyun 		    const struct nls_table *nls_codepage, int remap,
685*4882a593Smuzhiyun 		    const char *searchName, bool is_unicode)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	int i, rc = 0;
688*4882a593Smuzhiyun 	char *data_end;
689*4882a593Smuzhiyun 	struct dfs_referral_level_3 *ref;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	*num_of_nodes = le16_to_cpu(rsp->NumberOfReferrals);
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	if (*num_of_nodes < 1) {
694*4882a593Smuzhiyun 		cifs_dbg(VFS, "num_referrals: must be at least > 0, but we get num_referrals = %d\n",
695*4882a593Smuzhiyun 			 *num_of_nodes);
696*4882a593Smuzhiyun 		rc = -EINVAL;
697*4882a593Smuzhiyun 		goto parse_DFS_referrals_exit;
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	ref = (struct dfs_referral_level_3 *) &(rsp->referrals);
701*4882a593Smuzhiyun 	if (ref->VersionNumber != cpu_to_le16(3)) {
702*4882a593Smuzhiyun 		cifs_dbg(VFS, "Referrals of V%d version are not supported, should be V3\n",
703*4882a593Smuzhiyun 			 le16_to_cpu(ref->VersionNumber));
704*4882a593Smuzhiyun 		rc = -EINVAL;
705*4882a593Smuzhiyun 		goto parse_DFS_referrals_exit;
706*4882a593Smuzhiyun 	}
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	/* get the upper boundary of the resp buffer */
709*4882a593Smuzhiyun 	data_end = (char *)rsp + rsp_size;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	cifs_dbg(FYI, "num_referrals: %d dfs flags: 0x%x ...\n",
712*4882a593Smuzhiyun 		 *num_of_nodes, le32_to_cpu(rsp->DFSFlags));
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	*target_nodes = kcalloc(*num_of_nodes, sizeof(struct dfs_info3_param),
715*4882a593Smuzhiyun 				GFP_KERNEL);
716*4882a593Smuzhiyun 	if (*target_nodes == NULL) {
717*4882a593Smuzhiyun 		rc = -ENOMEM;
718*4882a593Smuzhiyun 		goto parse_DFS_referrals_exit;
719*4882a593Smuzhiyun 	}
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	/* collect necessary data from referrals */
722*4882a593Smuzhiyun 	for (i = 0; i < *num_of_nodes; i++) {
723*4882a593Smuzhiyun 		char *temp;
724*4882a593Smuzhiyun 		int max_len;
725*4882a593Smuzhiyun 		struct dfs_info3_param *node = (*target_nodes)+i;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 		node->flags = le32_to_cpu(rsp->DFSFlags);
728*4882a593Smuzhiyun 		if (is_unicode) {
729*4882a593Smuzhiyun 			__le16 *tmp = kmalloc(strlen(searchName)*2 + 2,
730*4882a593Smuzhiyun 						GFP_KERNEL);
731*4882a593Smuzhiyun 			if (tmp == NULL) {
732*4882a593Smuzhiyun 				rc = -ENOMEM;
733*4882a593Smuzhiyun 				goto parse_DFS_referrals_exit;
734*4882a593Smuzhiyun 			}
735*4882a593Smuzhiyun 			cifsConvertToUTF16((__le16 *) tmp, searchName,
736*4882a593Smuzhiyun 					   PATH_MAX, nls_codepage, remap);
737*4882a593Smuzhiyun 			node->path_consumed = cifs_utf16_bytes(tmp,
738*4882a593Smuzhiyun 					le16_to_cpu(rsp->PathConsumed),
739*4882a593Smuzhiyun 					nls_codepage);
740*4882a593Smuzhiyun 			kfree(tmp);
741*4882a593Smuzhiyun 		} else
742*4882a593Smuzhiyun 			node->path_consumed = le16_to_cpu(rsp->PathConsumed);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 		node->server_type = le16_to_cpu(ref->ServerType);
745*4882a593Smuzhiyun 		node->ref_flag = le16_to_cpu(ref->ReferralEntryFlags);
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 		/* copy DfsPath */
748*4882a593Smuzhiyun 		temp = (char *)ref + le16_to_cpu(ref->DfsPathOffset);
749*4882a593Smuzhiyun 		max_len = data_end - temp;
750*4882a593Smuzhiyun 		node->path_name = cifs_strndup_from_utf16(temp, max_len,
751*4882a593Smuzhiyun 						is_unicode, nls_codepage);
752*4882a593Smuzhiyun 		if (!node->path_name) {
753*4882a593Smuzhiyun 			rc = -ENOMEM;
754*4882a593Smuzhiyun 			goto parse_DFS_referrals_exit;
755*4882a593Smuzhiyun 		}
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 		/* copy link target UNC */
758*4882a593Smuzhiyun 		temp = (char *)ref + le16_to_cpu(ref->NetworkAddressOffset);
759*4882a593Smuzhiyun 		max_len = data_end - temp;
760*4882a593Smuzhiyun 		node->node_name = cifs_strndup_from_utf16(temp, max_len,
761*4882a593Smuzhiyun 						is_unicode, nls_codepage);
762*4882a593Smuzhiyun 		if (!node->node_name) {
763*4882a593Smuzhiyun 			rc = -ENOMEM;
764*4882a593Smuzhiyun 			goto parse_DFS_referrals_exit;
765*4882a593Smuzhiyun 		}
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 		node->ttl = le32_to_cpu(ref->TimeToLive);
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 		ref++;
770*4882a593Smuzhiyun 	}
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun parse_DFS_referrals_exit:
773*4882a593Smuzhiyun 	if (rc) {
774*4882a593Smuzhiyun 		free_dfs_info_array(*target_nodes, *num_of_nodes);
775*4882a593Smuzhiyun 		*target_nodes = NULL;
776*4882a593Smuzhiyun 		*num_of_nodes = 0;
777*4882a593Smuzhiyun 	}
778*4882a593Smuzhiyun 	return rc;
779*4882a593Smuzhiyun }
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun struct cifs_aio_ctx *
cifs_aio_ctx_alloc(void)782*4882a593Smuzhiyun cifs_aio_ctx_alloc(void)
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun 	struct cifs_aio_ctx *ctx;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	/*
787*4882a593Smuzhiyun 	 * Must use kzalloc to initialize ctx->bv to NULL and ctx->direct_io
788*4882a593Smuzhiyun 	 * to false so that we know when we have to unreference pages within
789*4882a593Smuzhiyun 	 * cifs_aio_ctx_release()
790*4882a593Smuzhiyun 	 */
791*4882a593Smuzhiyun 	ctx = kzalloc(sizeof(struct cifs_aio_ctx), GFP_KERNEL);
792*4882a593Smuzhiyun 	if (!ctx)
793*4882a593Smuzhiyun 		return NULL;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	INIT_LIST_HEAD(&ctx->list);
796*4882a593Smuzhiyun 	mutex_init(&ctx->aio_mutex);
797*4882a593Smuzhiyun 	init_completion(&ctx->done);
798*4882a593Smuzhiyun 	kref_init(&ctx->refcount);
799*4882a593Smuzhiyun 	return ctx;
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun void
cifs_aio_ctx_release(struct kref * refcount)803*4882a593Smuzhiyun cifs_aio_ctx_release(struct kref *refcount)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun 	struct cifs_aio_ctx *ctx = container_of(refcount,
806*4882a593Smuzhiyun 					struct cifs_aio_ctx, refcount);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	cifsFileInfo_put(ctx->cfile);
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	/*
811*4882a593Smuzhiyun 	 * ctx->bv is only set if setup_aio_ctx_iter() was call successfuly
812*4882a593Smuzhiyun 	 * which means that iov_iter_get_pages() was a success and thus that
813*4882a593Smuzhiyun 	 * we have taken reference on pages.
814*4882a593Smuzhiyun 	 */
815*4882a593Smuzhiyun 	if (ctx->bv) {
816*4882a593Smuzhiyun 		unsigned i;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 		for (i = 0; i < ctx->npages; i++) {
819*4882a593Smuzhiyun 			if (ctx->should_dirty)
820*4882a593Smuzhiyun 				set_page_dirty(ctx->bv[i].bv_page);
821*4882a593Smuzhiyun 			put_page(ctx->bv[i].bv_page);
822*4882a593Smuzhiyun 		}
823*4882a593Smuzhiyun 		kvfree(ctx->bv);
824*4882a593Smuzhiyun 	}
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	kfree(ctx);
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun #define CIFS_AIO_KMALLOC_LIMIT (1024 * 1024)
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun int
setup_aio_ctx_iter(struct cifs_aio_ctx * ctx,struct iov_iter * iter,int rw)832*4882a593Smuzhiyun setup_aio_ctx_iter(struct cifs_aio_ctx *ctx, struct iov_iter *iter, int rw)
833*4882a593Smuzhiyun {
834*4882a593Smuzhiyun 	ssize_t rc;
835*4882a593Smuzhiyun 	unsigned int cur_npages;
836*4882a593Smuzhiyun 	unsigned int npages = 0;
837*4882a593Smuzhiyun 	unsigned int i;
838*4882a593Smuzhiyun 	size_t len;
839*4882a593Smuzhiyun 	size_t count = iov_iter_count(iter);
840*4882a593Smuzhiyun 	unsigned int saved_len;
841*4882a593Smuzhiyun 	size_t start;
842*4882a593Smuzhiyun 	unsigned int max_pages = iov_iter_npages(iter, INT_MAX);
843*4882a593Smuzhiyun 	struct page **pages = NULL;
844*4882a593Smuzhiyun 	struct bio_vec *bv = NULL;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	if (iov_iter_is_kvec(iter)) {
847*4882a593Smuzhiyun 		memcpy(&ctx->iter, iter, sizeof(*iter));
848*4882a593Smuzhiyun 		ctx->len = count;
849*4882a593Smuzhiyun 		iov_iter_advance(iter, count);
850*4882a593Smuzhiyun 		return 0;
851*4882a593Smuzhiyun 	}
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	if (array_size(max_pages, sizeof(*bv)) <= CIFS_AIO_KMALLOC_LIMIT)
854*4882a593Smuzhiyun 		bv = kmalloc_array(max_pages, sizeof(*bv), GFP_KERNEL);
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	if (!bv) {
857*4882a593Smuzhiyun 		bv = vmalloc(array_size(max_pages, sizeof(*bv)));
858*4882a593Smuzhiyun 		if (!bv)
859*4882a593Smuzhiyun 			return -ENOMEM;
860*4882a593Smuzhiyun 	}
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	if (array_size(max_pages, sizeof(*pages)) <= CIFS_AIO_KMALLOC_LIMIT)
863*4882a593Smuzhiyun 		pages = kmalloc_array(max_pages, sizeof(*pages), GFP_KERNEL);
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	if (!pages) {
866*4882a593Smuzhiyun 		pages = vmalloc(array_size(max_pages, sizeof(*pages)));
867*4882a593Smuzhiyun 		if (!pages) {
868*4882a593Smuzhiyun 			kvfree(bv);
869*4882a593Smuzhiyun 			return -ENOMEM;
870*4882a593Smuzhiyun 		}
871*4882a593Smuzhiyun 	}
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	saved_len = count;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	while (count && npages < max_pages) {
876*4882a593Smuzhiyun 		rc = iov_iter_get_pages(iter, pages, count, max_pages, &start);
877*4882a593Smuzhiyun 		if (rc < 0) {
878*4882a593Smuzhiyun 			cifs_dbg(VFS, "Couldn't get user pages (rc=%zd)\n", rc);
879*4882a593Smuzhiyun 			break;
880*4882a593Smuzhiyun 		}
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 		if (rc > count) {
883*4882a593Smuzhiyun 			cifs_dbg(VFS, "get pages rc=%zd more than %zu\n", rc,
884*4882a593Smuzhiyun 				 count);
885*4882a593Smuzhiyun 			break;
886*4882a593Smuzhiyun 		}
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 		iov_iter_advance(iter, rc);
889*4882a593Smuzhiyun 		count -= rc;
890*4882a593Smuzhiyun 		rc += start;
891*4882a593Smuzhiyun 		cur_npages = DIV_ROUND_UP(rc, PAGE_SIZE);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 		if (npages + cur_npages > max_pages) {
894*4882a593Smuzhiyun 			cifs_dbg(VFS, "out of vec array capacity (%u vs %u)\n",
895*4882a593Smuzhiyun 				 npages + cur_npages, max_pages);
896*4882a593Smuzhiyun 			break;
897*4882a593Smuzhiyun 		}
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 		for (i = 0; i < cur_npages; i++) {
900*4882a593Smuzhiyun 			len = rc > PAGE_SIZE ? PAGE_SIZE : rc;
901*4882a593Smuzhiyun 			bv[npages + i].bv_page = pages[i];
902*4882a593Smuzhiyun 			bv[npages + i].bv_offset = start;
903*4882a593Smuzhiyun 			bv[npages + i].bv_len = len - start;
904*4882a593Smuzhiyun 			rc -= len;
905*4882a593Smuzhiyun 			start = 0;
906*4882a593Smuzhiyun 		}
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 		npages += cur_npages;
909*4882a593Smuzhiyun 	}
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	kvfree(pages);
912*4882a593Smuzhiyun 	ctx->bv = bv;
913*4882a593Smuzhiyun 	ctx->len = saved_len - count;
914*4882a593Smuzhiyun 	ctx->npages = npages;
915*4882a593Smuzhiyun 	iov_iter_bvec(&ctx->iter, rw, ctx->bv, npages, ctx->len);
916*4882a593Smuzhiyun 	return 0;
917*4882a593Smuzhiyun }
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun /**
920*4882a593Smuzhiyun  * cifs_alloc_hash - allocate hash and hash context together
921*4882a593Smuzhiyun  *
922*4882a593Smuzhiyun  * The caller has to make sure @sdesc is initialized to either NULL or
923*4882a593Smuzhiyun  * a valid context. Both can be freed via cifs_free_hash().
924*4882a593Smuzhiyun  */
925*4882a593Smuzhiyun int
cifs_alloc_hash(const char * name,struct crypto_shash ** shash,struct sdesc ** sdesc)926*4882a593Smuzhiyun cifs_alloc_hash(const char *name,
927*4882a593Smuzhiyun 		struct crypto_shash **shash, struct sdesc **sdesc)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun 	int rc = 0;
930*4882a593Smuzhiyun 	size_t size;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	if (*sdesc != NULL)
933*4882a593Smuzhiyun 		return 0;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	*shash = crypto_alloc_shash(name, 0, 0);
936*4882a593Smuzhiyun 	if (IS_ERR(*shash)) {
937*4882a593Smuzhiyun 		cifs_dbg(VFS, "Could not allocate crypto %s\n", name);
938*4882a593Smuzhiyun 		rc = PTR_ERR(*shash);
939*4882a593Smuzhiyun 		*shash = NULL;
940*4882a593Smuzhiyun 		*sdesc = NULL;
941*4882a593Smuzhiyun 		return rc;
942*4882a593Smuzhiyun 	}
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	size = sizeof(struct shash_desc) + crypto_shash_descsize(*shash);
945*4882a593Smuzhiyun 	*sdesc = kmalloc(size, GFP_KERNEL);
946*4882a593Smuzhiyun 	if (*sdesc == NULL) {
947*4882a593Smuzhiyun 		cifs_dbg(VFS, "no memory left to allocate crypto %s\n", name);
948*4882a593Smuzhiyun 		crypto_free_shash(*shash);
949*4882a593Smuzhiyun 		*shash = NULL;
950*4882a593Smuzhiyun 		return -ENOMEM;
951*4882a593Smuzhiyun 	}
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	(*sdesc)->shash.tfm = *shash;
954*4882a593Smuzhiyun 	return 0;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun /**
958*4882a593Smuzhiyun  * cifs_free_hash - free hash and hash context together
959*4882a593Smuzhiyun  *
960*4882a593Smuzhiyun  * Freeing a NULL hash or context is safe.
961*4882a593Smuzhiyun  */
962*4882a593Smuzhiyun void
cifs_free_hash(struct crypto_shash ** shash,struct sdesc ** sdesc)963*4882a593Smuzhiyun cifs_free_hash(struct crypto_shash **shash, struct sdesc **sdesc)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun 	kfree(*sdesc);
966*4882a593Smuzhiyun 	*sdesc = NULL;
967*4882a593Smuzhiyun 	if (*shash)
968*4882a593Smuzhiyun 		crypto_free_shash(*shash);
969*4882a593Smuzhiyun 	*shash = NULL;
970*4882a593Smuzhiyun }
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun /**
973*4882a593Smuzhiyun  * rqst_page_get_length - obtain the length and offset for a page in smb_rqst
974*4882a593Smuzhiyun  * Input: rqst - a smb_rqst, page - a page index for rqst
975*4882a593Smuzhiyun  * Output: *len - the length for this page, *offset - the offset for this page
976*4882a593Smuzhiyun  */
rqst_page_get_length(struct smb_rqst * rqst,unsigned int page,unsigned int * len,unsigned int * offset)977*4882a593Smuzhiyun void rqst_page_get_length(struct smb_rqst *rqst, unsigned int page,
978*4882a593Smuzhiyun 				unsigned int *len, unsigned int *offset)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun 	*len = rqst->rq_pagesz;
981*4882a593Smuzhiyun 	*offset = (page == 0) ? rqst->rq_offset : 0;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	if (rqst->rq_npages == 1 || page == rqst->rq_npages-1)
984*4882a593Smuzhiyun 		*len = rqst->rq_tailsz;
985*4882a593Smuzhiyun 	else if (page == 0)
986*4882a593Smuzhiyun 		*len = rqst->rq_pagesz - rqst->rq_offset;
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
extract_unc_hostname(const char * unc,const char ** h,size_t * len)989*4882a593Smuzhiyun void extract_unc_hostname(const char *unc, const char **h, size_t *len)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	const char *end;
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	/* skip initial slashes */
994*4882a593Smuzhiyun 	while (*unc && (*unc == '\\' || *unc == '/'))
995*4882a593Smuzhiyun 		unc++;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	end = unc;
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	while (*end && !(*end == '\\' || *end == '/'))
1000*4882a593Smuzhiyun 		end++;
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	*h = unc;
1003*4882a593Smuzhiyun 	*len = end - unc;
1004*4882a593Smuzhiyun }
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun /**
1007*4882a593Smuzhiyun  * copy_path_name - copy src path to dst, possibly truncating
1008*4882a593Smuzhiyun  *
1009*4882a593Smuzhiyun  * returns number of bytes written (including trailing nul)
1010*4882a593Smuzhiyun  */
copy_path_name(char * dst,const char * src)1011*4882a593Smuzhiyun int copy_path_name(char *dst, const char *src)
1012*4882a593Smuzhiyun {
1013*4882a593Smuzhiyun 	int name_len;
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun 	/*
1016*4882a593Smuzhiyun 	 * PATH_MAX includes nul, so if strlen(src) >= PATH_MAX it
1017*4882a593Smuzhiyun 	 * will truncate and strlen(dst) will be PATH_MAX-1
1018*4882a593Smuzhiyun 	 */
1019*4882a593Smuzhiyun 	name_len = strscpy(dst, src, PATH_MAX);
1020*4882a593Smuzhiyun 	if (WARN_ON_ONCE(name_len < 0))
1021*4882a593Smuzhiyun 		name_len = PATH_MAX-1;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	/* we count the trailing nul */
1024*4882a593Smuzhiyun 	name_len++;
1025*4882a593Smuzhiyun 	return name_len;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun struct super_cb_data {
1029*4882a593Smuzhiyun 	void *data;
1030*4882a593Smuzhiyun 	struct super_block *sb;
1031*4882a593Smuzhiyun };
1032*4882a593Smuzhiyun 
tcp_super_cb(struct super_block * sb,void * arg)1033*4882a593Smuzhiyun static void tcp_super_cb(struct super_block *sb, void *arg)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	struct super_cb_data *sd = arg;
1036*4882a593Smuzhiyun 	struct TCP_Server_Info *server = sd->data;
1037*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb;
1038*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	if (sd->sb)
1041*4882a593Smuzhiyun 		return;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	cifs_sb = CIFS_SB(sb);
1044*4882a593Smuzhiyun 	tcon = cifs_sb_master_tcon(cifs_sb);
1045*4882a593Smuzhiyun 	if (tcon->ses->server == server)
1046*4882a593Smuzhiyun 		sd->sb = sb;
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun 
__cifs_get_super(void (* f)(struct super_block *,void *),void * data)1049*4882a593Smuzhiyun static struct super_block *__cifs_get_super(void (*f)(struct super_block *, void *),
1050*4882a593Smuzhiyun 					    void *data)
1051*4882a593Smuzhiyun {
1052*4882a593Smuzhiyun 	struct super_cb_data sd = {
1053*4882a593Smuzhiyun 		.data = data,
1054*4882a593Smuzhiyun 		.sb = NULL,
1055*4882a593Smuzhiyun 	};
1056*4882a593Smuzhiyun 	struct file_system_type **fs_type = (struct file_system_type *[]) {
1057*4882a593Smuzhiyun 		&cifs_fs_type, &smb3_fs_type, NULL,
1058*4882a593Smuzhiyun 	};
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	for (; *fs_type; fs_type++) {
1061*4882a593Smuzhiyun 		iterate_supers_type(*fs_type, f, &sd);
1062*4882a593Smuzhiyun 		if (sd.sb) {
1063*4882a593Smuzhiyun 			/*
1064*4882a593Smuzhiyun 			 * Grab an active reference in order to prevent automounts (DFS links)
1065*4882a593Smuzhiyun 			 * of expiring and then freeing up our cifs superblock pointer while
1066*4882a593Smuzhiyun 			 * we're doing failover.
1067*4882a593Smuzhiyun 			 */
1068*4882a593Smuzhiyun 			cifs_sb_active(sd.sb);
1069*4882a593Smuzhiyun 			return sd.sb;
1070*4882a593Smuzhiyun 		}
1071*4882a593Smuzhiyun 	}
1072*4882a593Smuzhiyun 	return ERR_PTR(-EINVAL);
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
__cifs_put_super(struct super_block * sb)1075*4882a593Smuzhiyun static void __cifs_put_super(struct super_block *sb)
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun 	if (!IS_ERR_OR_NULL(sb))
1078*4882a593Smuzhiyun 		cifs_sb_deactive(sb);
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun 
cifs_get_tcp_super(struct TCP_Server_Info * server)1081*4882a593Smuzhiyun struct super_block *cifs_get_tcp_super(struct TCP_Server_Info *server)
1082*4882a593Smuzhiyun {
1083*4882a593Smuzhiyun 	return __cifs_get_super(tcp_super_cb, server);
1084*4882a593Smuzhiyun }
1085*4882a593Smuzhiyun 
cifs_put_tcp_super(struct super_block * sb)1086*4882a593Smuzhiyun void cifs_put_tcp_super(struct super_block *sb)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun 	__cifs_put_super(sb);
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun 
1091*4882a593Smuzhiyun #ifdef CONFIG_CIFS_DFS_UPCALL
match_target_ip(struct TCP_Server_Info * server,const char * share,size_t share_len,bool * result)1092*4882a593Smuzhiyun int match_target_ip(struct TCP_Server_Info *server,
1093*4882a593Smuzhiyun 		    const char *share, size_t share_len,
1094*4882a593Smuzhiyun 		    bool *result)
1095*4882a593Smuzhiyun {
1096*4882a593Smuzhiyun 	int rc;
1097*4882a593Smuzhiyun 	char *target, *tip = NULL;
1098*4882a593Smuzhiyun 	struct sockaddr tipaddr;
1099*4882a593Smuzhiyun 
1100*4882a593Smuzhiyun 	*result = false;
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	target = kzalloc(share_len + 3, GFP_KERNEL);
1103*4882a593Smuzhiyun 	if (!target) {
1104*4882a593Smuzhiyun 		rc = -ENOMEM;
1105*4882a593Smuzhiyun 		goto out;
1106*4882a593Smuzhiyun 	}
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	scnprintf(target, share_len + 3, "\\\\%.*s", (int)share_len, share);
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: target name: %s\n", __func__, target + 2);
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	rc = dns_resolve_server_name_to_ip(target, &tip);
1113*4882a593Smuzhiyun 	if (rc < 0)
1114*4882a593Smuzhiyun 		goto out;
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: target ip: %s\n", __func__, tip);
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 	if (!cifs_convert_address(&tipaddr, tip, strlen(tip))) {
1119*4882a593Smuzhiyun 		cifs_dbg(VFS, "%s: failed to convert target ip address\n",
1120*4882a593Smuzhiyun 			 __func__);
1121*4882a593Smuzhiyun 		rc = -EINVAL;
1122*4882a593Smuzhiyun 		goto out;
1123*4882a593Smuzhiyun 	}
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	*result = cifs_match_ipaddr((struct sockaddr *)&server->dstaddr,
1126*4882a593Smuzhiyun 				    &tipaddr);
1127*4882a593Smuzhiyun 	cifs_dbg(FYI, "%s: ip addresses match: %u\n", __func__, *result);
1128*4882a593Smuzhiyun 	rc = 0;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun out:
1131*4882a593Smuzhiyun 	kfree(target);
1132*4882a593Smuzhiyun 	kfree(tip);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	return rc;
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun 
tcon_super_cb(struct super_block * sb,void * arg)1137*4882a593Smuzhiyun static void tcon_super_cb(struct super_block *sb, void *arg)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	struct super_cb_data *sd = arg;
1140*4882a593Smuzhiyun 	struct cifs_tcon *tcon = sd->data;
1141*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	if (sd->sb)
1144*4882a593Smuzhiyun 		return;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	cifs_sb = CIFS_SB(sb);
1147*4882a593Smuzhiyun 	if (tcon->dfs_path && cifs_sb->origin_fullpath &&
1148*4882a593Smuzhiyun 	    !strcasecmp(tcon->dfs_path, cifs_sb->origin_fullpath))
1149*4882a593Smuzhiyun 		sd->sb = sb;
1150*4882a593Smuzhiyun }
1151*4882a593Smuzhiyun 
cifs_get_tcon_super(struct cifs_tcon * tcon)1152*4882a593Smuzhiyun static inline struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon)
1153*4882a593Smuzhiyun {
1154*4882a593Smuzhiyun 	return __cifs_get_super(tcon_super_cb, tcon);
1155*4882a593Smuzhiyun }
1156*4882a593Smuzhiyun 
cifs_put_tcon_super(struct super_block * sb)1157*4882a593Smuzhiyun static inline void cifs_put_tcon_super(struct super_block *sb)
1158*4882a593Smuzhiyun {
1159*4882a593Smuzhiyun 	__cifs_put_super(sb);
1160*4882a593Smuzhiyun }
1161*4882a593Smuzhiyun #else
cifs_get_tcon_super(struct cifs_tcon * tcon)1162*4882a593Smuzhiyun static inline struct super_block *cifs_get_tcon_super(struct cifs_tcon *tcon)
1163*4882a593Smuzhiyun {
1164*4882a593Smuzhiyun 	return ERR_PTR(-EOPNOTSUPP);
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun 
cifs_put_tcon_super(struct super_block * sb)1167*4882a593Smuzhiyun static inline void cifs_put_tcon_super(struct super_block *sb)
1168*4882a593Smuzhiyun {
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun #endif
1171*4882a593Smuzhiyun 
update_super_prepath(struct cifs_tcon * tcon,char * prefix)1172*4882a593Smuzhiyun int update_super_prepath(struct cifs_tcon *tcon, char *prefix)
1173*4882a593Smuzhiyun {
1174*4882a593Smuzhiyun 	struct super_block *sb;
1175*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb;
1176*4882a593Smuzhiyun 	int rc = 0;
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	sb = cifs_get_tcon_super(tcon);
1179*4882a593Smuzhiyun 	if (IS_ERR(sb))
1180*4882a593Smuzhiyun 		return PTR_ERR(sb);
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	cifs_sb = CIFS_SB(sb);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	kfree(cifs_sb->prepath);
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	if (prefix && *prefix) {
1187*4882a593Smuzhiyun 		cifs_sb->prepath = kstrndup(prefix, strlen(prefix), GFP_ATOMIC);
1188*4882a593Smuzhiyun 		if (!cifs_sb->prepath) {
1189*4882a593Smuzhiyun 			rc = -ENOMEM;
1190*4882a593Smuzhiyun 			goto out;
1191*4882a593Smuzhiyun 		}
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 		convert_delimiter(cifs_sb->prepath, CIFS_DIR_SEP(cifs_sb));
1194*4882a593Smuzhiyun 	} else
1195*4882a593Smuzhiyun 		cifs_sb->prepath = NULL;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun out:
1200*4882a593Smuzhiyun 	cifs_put_tcon_super(sb);
1201*4882a593Smuzhiyun 	return rc;
1202*4882a593Smuzhiyun }
1203