xref: /OK3568_Linux_fs/kernel/fs/cifs/dir.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *   fs/cifs/dir.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *   vfs operations that deal with dentries
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *   Copyright (C) International Business Machines  Corp., 2002,2009
7*4882a593Smuzhiyun  *   Author(s): Steve French (sfrench@us.ibm.com)
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *   This library is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  *   it under the terms of the GNU Lesser General Public License as published
11*4882a593Smuzhiyun  *   by the Free Software Foundation; either version 2.1 of the License, or
12*4882a593Smuzhiyun  *   (at your option) any later version.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *   This library is distributed in the hope that it will be useful,
15*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17*4882a593Smuzhiyun  *   the GNU Lesser General Public License for more details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *   You should have received a copy of the GNU Lesser General Public License
20*4882a593Smuzhiyun  *   along with this library; if not, write to the Free Software
21*4882a593Smuzhiyun  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*4882a593Smuzhiyun  */
23*4882a593Smuzhiyun #include <linux/fs.h>
24*4882a593Smuzhiyun #include <linux/stat.h>
25*4882a593Smuzhiyun #include <linux/slab.h>
26*4882a593Smuzhiyun #include <linux/namei.h>
27*4882a593Smuzhiyun #include <linux/mount.h>
28*4882a593Smuzhiyun #include <linux/file.h>
29*4882a593Smuzhiyun #include "cifsfs.h"
30*4882a593Smuzhiyun #include "cifspdu.h"
31*4882a593Smuzhiyun #include "cifsglob.h"
32*4882a593Smuzhiyun #include "cifsproto.h"
33*4882a593Smuzhiyun #include "cifs_debug.h"
34*4882a593Smuzhiyun #include "cifs_fs_sb.h"
35*4882a593Smuzhiyun #include "cifs_unicode.h"
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun static void
renew_parental_timestamps(struct dentry * direntry)38*4882a593Smuzhiyun renew_parental_timestamps(struct dentry *direntry)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	/* BB check if there is a way to get the kernel to do this or if we
41*4882a593Smuzhiyun 	   really need this */
42*4882a593Smuzhiyun 	do {
43*4882a593Smuzhiyun 		cifs_set_time(direntry, jiffies);
44*4882a593Smuzhiyun 		direntry = direntry->d_parent;
45*4882a593Smuzhiyun 	} while (!IS_ROOT(direntry));
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun char *
cifs_build_path_to_root(struct smb_vol * vol,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,int add_treename)49*4882a593Smuzhiyun cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
50*4882a593Smuzhiyun 			struct cifs_tcon *tcon, int add_treename)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
53*4882a593Smuzhiyun 	int dfsplen;
54*4882a593Smuzhiyun 	char *full_path = NULL;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	/* if no prefix path, simply set path to the root of share to "" */
57*4882a593Smuzhiyun 	if (pplen == 0) {
58*4882a593Smuzhiyun 		full_path = kzalloc(1, GFP_KERNEL);
59*4882a593Smuzhiyun 		return full_path;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	if (add_treename)
63*4882a593Smuzhiyun 		dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
64*4882a593Smuzhiyun 	else
65*4882a593Smuzhiyun 		dfsplen = 0;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
68*4882a593Smuzhiyun 	if (full_path == NULL)
69*4882a593Smuzhiyun 		return full_path;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	if (dfsplen)
72*4882a593Smuzhiyun 		memcpy(full_path, tcon->treeName, dfsplen);
73*4882a593Smuzhiyun 	full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
74*4882a593Smuzhiyun 	memcpy(full_path + dfsplen + 1, vol->prepath, pplen);
75*4882a593Smuzhiyun 	convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
76*4882a593Smuzhiyun 	return full_path;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /* Note: caller must free return buffer */
80*4882a593Smuzhiyun char *
build_path_from_dentry(struct dentry * direntry)81*4882a593Smuzhiyun build_path_from_dentry(struct dentry *direntry)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
84*4882a593Smuzhiyun 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
85*4882a593Smuzhiyun 	bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return build_path_from_dentry_optional_prefix(direntry,
88*4882a593Smuzhiyun 						      prefix);
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun char *
build_path_from_dentry_optional_prefix(struct dentry * direntry,bool prefix)92*4882a593Smuzhiyun build_path_from_dentry_optional_prefix(struct dentry *direntry, bool prefix)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	struct dentry *temp;
95*4882a593Smuzhiyun 	int namelen;
96*4882a593Smuzhiyun 	int dfsplen;
97*4882a593Smuzhiyun 	int pplen = 0;
98*4882a593Smuzhiyun 	char *full_path;
99*4882a593Smuzhiyun 	char dirsep;
100*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
101*4882a593Smuzhiyun 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
102*4882a593Smuzhiyun 	unsigned seq;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	dirsep = CIFS_DIR_SEP(cifs_sb);
105*4882a593Smuzhiyun 	if (prefix)
106*4882a593Smuzhiyun 		dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
107*4882a593Smuzhiyun 	else
108*4882a593Smuzhiyun 		dfsplen = 0;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
111*4882a593Smuzhiyun 		pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun cifs_bp_rename_retry:
114*4882a593Smuzhiyun 	namelen = dfsplen + pplen;
115*4882a593Smuzhiyun 	seq = read_seqbegin(&rename_lock);
116*4882a593Smuzhiyun 	rcu_read_lock();
117*4882a593Smuzhiyun 	for (temp = direntry; !IS_ROOT(temp);) {
118*4882a593Smuzhiyun 		namelen += (1 + temp->d_name.len);
119*4882a593Smuzhiyun 		temp = temp->d_parent;
120*4882a593Smuzhiyun 		if (temp == NULL) {
121*4882a593Smuzhiyun 			cifs_dbg(VFS, "corrupt dentry\n");
122*4882a593Smuzhiyun 			rcu_read_unlock();
123*4882a593Smuzhiyun 			return NULL;
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 	}
126*4882a593Smuzhiyun 	rcu_read_unlock();
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	full_path = kmalloc(namelen+1, GFP_ATOMIC);
129*4882a593Smuzhiyun 	if (full_path == NULL)
130*4882a593Smuzhiyun 		return full_path;
131*4882a593Smuzhiyun 	full_path[namelen] = 0;	/* trailing null */
132*4882a593Smuzhiyun 	rcu_read_lock();
133*4882a593Smuzhiyun 	for (temp = direntry; !IS_ROOT(temp);) {
134*4882a593Smuzhiyun 		spin_lock(&temp->d_lock);
135*4882a593Smuzhiyun 		namelen -= 1 + temp->d_name.len;
136*4882a593Smuzhiyun 		if (namelen < 0) {
137*4882a593Smuzhiyun 			spin_unlock(&temp->d_lock);
138*4882a593Smuzhiyun 			break;
139*4882a593Smuzhiyun 		} else {
140*4882a593Smuzhiyun 			full_path[namelen] = dirsep;
141*4882a593Smuzhiyun 			strncpy(full_path + namelen + 1, temp->d_name.name,
142*4882a593Smuzhiyun 				temp->d_name.len);
143*4882a593Smuzhiyun 			cifs_dbg(FYI, "name: %s\n", full_path + namelen);
144*4882a593Smuzhiyun 		}
145*4882a593Smuzhiyun 		spin_unlock(&temp->d_lock);
146*4882a593Smuzhiyun 		temp = temp->d_parent;
147*4882a593Smuzhiyun 		if (temp == NULL) {
148*4882a593Smuzhiyun 			cifs_dbg(VFS, "corrupt dentry\n");
149*4882a593Smuzhiyun 			rcu_read_unlock();
150*4882a593Smuzhiyun 			kfree(full_path);
151*4882a593Smuzhiyun 			return NULL;
152*4882a593Smuzhiyun 		}
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 	rcu_read_unlock();
155*4882a593Smuzhiyun 	if (namelen != dfsplen + pplen || read_seqretry(&rename_lock, seq)) {
156*4882a593Smuzhiyun 		cifs_dbg(FYI, "did not end path lookup where expected. namelen=%ddfsplen=%d\n",
157*4882a593Smuzhiyun 			 namelen, dfsplen);
158*4882a593Smuzhiyun 		/* presumably this is only possible if racing with a rename
159*4882a593Smuzhiyun 		of one of the parent directories  (we can not lock the dentries
160*4882a593Smuzhiyun 		above us to prevent this, but retrying should be harmless) */
161*4882a593Smuzhiyun 		kfree(full_path);
162*4882a593Smuzhiyun 		goto cifs_bp_rename_retry;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 	/* DIR_SEP already set for byte  0 / vs \ but not for
165*4882a593Smuzhiyun 	   subsequent slashes in prepath which currently must
166*4882a593Smuzhiyun 	   be entered the right way - not sure if there is an alternative
167*4882a593Smuzhiyun 	   since the '\' is a valid posix character so we can not switch
168*4882a593Smuzhiyun 	   those safely to '/' if any are found in the middle of the prepath */
169*4882a593Smuzhiyun 	/* BB test paths to Windows with '/' in the midst of prepath */
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	if (pplen) {
172*4882a593Smuzhiyun 		int i;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
175*4882a593Smuzhiyun 		memcpy(full_path+dfsplen+1, cifs_sb->prepath, pplen-1);
176*4882a593Smuzhiyun 		full_path[dfsplen] = dirsep;
177*4882a593Smuzhiyun 		for (i = 0; i < pplen-1; i++)
178*4882a593Smuzhiyun 			if (full_path[dfsplen+1+i] == '/')
179*4882a593Smuzhiyun 				full_path[dfsplen+1+i] = CIFS_DIR_SEP(cifs_sb);
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (dfsplen) {
183*4882a593Smuzhiyun 		strncpy(full_path, tcon->treeName, dfsplen);
184*4882a593Smuzhiyun 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
185*4882a593Smuzhiyun 			int i;
186*4882a593Smuzhiyun 			for (i = 0; i < dfsplen; i++) {
187*4882a593Smuzhiyun 				if (full_path[i] == '\\')
188*4882a593Smuzhiyun 					full_path[i] = '/';
189*4882a593Smuzhiyun 			}
190*4882a593Smuzhiyun 		}
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 	return full_path;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun  * Don't allow path components longer than the server max.
197*4882a593Smuzhiyun  * Don't allow the separator character in a path component.
198*4882a593Smuzhiyun  * The VFS will not allow "/", but "\" is allowed by posix.
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun static int
check_name(struct dentry * direntry,struct cifs_tcon * tcon)201*4882a593Smuzhiyun check_name(struct dentry *direntry, struct cifs_tcon *tcon)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
204*4882a593Smuzhiyun 	int i;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
207*4882a593Smuzhiyun 		     direntry->d_name.len >
208*4882a593Smuzhiyun 		     le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
209*4882a593Smuzhiyun 		return -ENAMETOOLONG;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
212*4882a593Smuzhiyun 		for (i = 0; i < direntry->d_name.len; i++) {
213*4882a593Smuzhiyun 			if (direntry->d_name.name[i] == '\\') {
214*4882a593Smuzhiyun 				cifs_dbg(FYI, "Invalid file name\n");
215*4882a593Smuzhiyun 				return -EINVAL;
216*4882a593Smuzhiyun 			}
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 	}
219*4882a593Smuzhiyun 	return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun /* Inode operations in similar order to how they appear in Linux file fs.h */
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun static int
cifs_do_create(struct inode * inode,struct dentry * direntry,unsigned int xid,struct tcon_link * tlink,unsigned oflags,umode_t mode,__u32 * oplock,struct cifs_fid * fid)226*4882a593Smuzhiyun cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
227*4882a593Smuzhiyun 	       struct tcon_link *tlink, unsigned oflags, umode_t mode,
228*4882a593Smuzhiyun 	       __u32 *oplock, struct cifs_fid *fid)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	int rc = -ENOENT;
231*4882a593Smuzhiyun 	int create_options = CREATE_NOT_DIR;
232*4882a593Smuzhiyun 	int desired_access;
233*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
234*4882a593Smuzhiyun 	struct cifs_tcon *tcon = tlink_tcon(tlink);
235*4882a593Smuzhiyun 	char *full_path = NULL;
236*4882a593Smuzhiyun 	FILE_ALL_INFO *buf = NULL;
237*4882a593Smuzhiyun 	struct inode *newinode = NULL;
238*4882a593Smuzhiyun 	int disposition;
239*4882a593Smuzhiyun 	struct TCP_Server_Info *server = tcon->ses->server;
240*4882a593Smuzhiyun 	struct cifs_open_parms oparms;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	*oplock = 0;
243*4882a593Smuzhiyun 	if (tcon->ses->server->oplocks)
244*4882a593Smuzhiyun 		*oplock = REQ_OPLOCK;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	full_path = build_path_from_dentry(direntry);
247*4882a593Smuzhiyun 	if (!full_path)
248*4882a593Smuzhiyun 		return -ENOMEM;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
251*4882a593Smuzhiyun 	    (CIFS_UNIX_POSIX_PATH_OPS_CAP &
252*4882a593Smuzhiyun 			le64_to_cpu(tcon->fsUnixInfo.Capability))) {
253*4882a593Smuzhiyun 		rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
254*4882a593Smuzhiyun 				     oflags, oplock, &fid->netfid, xid);
255*4882a593Smuzhiyun 		switch (rc) {
256*4882a593Smuzhiyun 		case 0:
257*4882a593Smuzhiyun 			if (newinode == NULL) {
258*4882a593Smuzhiyun 				/* query inode info */
259*4882a593Smuzhiyun 				goto cifs_create_get_file_info;
260*4882a593Smuzhiyun 			}
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 			if (S_ISDIR(newinode->i_mode)) {
263*4882a593Smuzhiyun 				CIFSSMBClose(xid, tcon, fid->netfid);
264*4882a593Smuzhiyun 				iput(newinode);
265*4882a593Smuzhiyun 				rc = -EISDIR;
266*4882a593Smuzhiyun 				goto out;
267*4882a593Smuzhiyun 			}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 			if (!S_ISREG(newinode->i_mode)) {
270*4882a593Smuzhiyun 				/*
271*4882a593Smuzhiyun 				 * The server may allow us to open things like
272*4882a593Smuzhiyun 				 * FIFOs, but the client isn't set up to deal
273*4882a593Smuzhiyun 				 * with that. If it's not a regular file, just
274*4882a593Smuzhiyun 				 * close it and proceed as if it were a normal
275*4882a593Smuzhiyun 				 * lookup.
276*4882a593Smuzhiyun 				 */
277*4882a593Smuzhiyun 				CIFSSMBClose(xid, tcon, fid->netfid);
278*4882a593Smuzhiyun 				goto cifs_create_get_file_info;
279*4882a593Smuzhiyun 			}
280*4882a593Smuzhiyun 			/* success, no need to query */
281*4882a593Smuzhiyun 			goto cifs_create_set_dentry;
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 		case -ENOENT:
284*4882a593Smuzhiyun 			goto cifs_create_get_file_info;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		case -EIO:
287*4882a593Smuzhiyun 		case -EINVAL:
288*4882a593Smuzhiyun 			/*
289*4882a593Smuzhiyun 			 * EIO could indicate that (posix open) operation is not
290*4882a593Smuzhiyun 			 * supported, despite what server claimed in capability
291*4882a593Smuzhiyun 			 * negotiation.
292*4882a593Smuzhiyun 			 *
293*4882a593Smuzhiyun 			 * POSIX open in samba versions 3.3.1 and earlier could
294*4882a593Smuzhiyun 			 * incorrectly fail with invalid parameter.
295*4882a593Smuzhiyun 			 */
296*4882a593Smuzhiyun 			tcon->broken_posix_open = true;
297*4882a593Smuzhiyun 			break;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		case -EREMOTE:
300*4882a593Smuzhiyun 		case -EOPNOTSUPP:
301*4882a593Smuzhiyun 			/*
302*4882a593Smuzhiyun 			 * EREMOTE indicates DFS junction, which is not handled
303*4882a593Smuzhiyun 			 * in posix open.  If either that or op not supported
304*4882a593Smuzhiyun 			 * returned, follow the normal lookup.
305*4882a593Smuzhiyun 			 */
306*4882a593Smuzhiyun 			break;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		default:
309*4882a593Smuzhiyun 			goto out;
310*4882a593Smuzhiyun 		}
311*4882a593Smuzhiyun 		/*
312*4882a593Smuzhiyun 		 * fallthrough to retry, using older open call, this is case
313*4882a593Smuzhiyun 		 * where server does not support this SMB level, and falsely
314*4882a593Smuzhiyun 		 * claims capability (also get here for DFS case which should be
315*4882a593Smuzhiyun 		 * rare for path not covered on files)
316*4882a593Smuzhiyun 		 */
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	desired_access = 0;
320*4882a593Smuzhiyun 	if (OPEN_FMODE(oflags) & FMODE_READ)
321*4882a593Smuzhiyun 		desired_access |= GENERIC_READ; /* is this too little? */
322*4882a593Smuzhiyun 	if (OPEN_FMODE(oflags) & FMODE_WRITE)
323*4882a593Smuzhiyun 		desired_access |= GENERIC_WRITE;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	disposition = FILE_OVERWRITE_IF;
326*4882a593Smuzhiyun 	if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
327*4882a593Smuzhiyun 		disposition = FILE_CREATE;
328*4882a593Smuzhiyun 	else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
329*4882a593Smuzhiyun 		disposition = FILE_OVERWRITE_IF;
330*4882a593Smuzhiyun 	else if ((oflags & O_CREAT) == O_CREAT)
331*4882a593Smuzhiyun 		disposition = FILE_OPEN_IF;
332*4882a593Smuzhiyun 	else
333*4882a593Smuzhiyun 		cifs_dbg(FYI, "Create flag not set in create function\n");
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	/*
336*4882a593Smuzhiyun 	 * BB add processing to set equivalent of mode - e.g. via CreateX with
337*4882a593Smuzhiyun 	 * ACLs
338*4882a593Smuzhiyun 	 */
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	if (!server->ops->open) {
341*4882a593Smuzhiyun 		rc = -ENOSYS;
342*4882a593Smuzhiyun 		goto out;
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
346*4882a593Smuzhiyun 	if (buf == NULL) {
347*4882a593Smuzhiyun 		rc = -ENOMEM;
348*4882a593Smuzhiyun 		goto out;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	/*
352*4882a593Smuzhiyun 	 * if we're not using unix extensions, see if we need to set
353*4882a593Smuzhiyun 	 * ATTR_READONLY on the create call
354*4882a593Smuzhiyun 	 */
355*4882a593Smuzhiyun 	if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
356*4882a593Smuzhiyun 		create_options |= CREATE_OPTION_READONLY;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	oparms.tcon = tcon;
359*4882a593Smuzhiyun 	oparms.cifs_sb = cifs_sb;
360*4882a593Smuzhiyun 	oparms.desired_access = desired_access;
361*4882a593Smuzhiyun 	oparms.create_options = cifs_create_options(cifs_sb, create_options);
362*4882a593Smuzhiyun 	oparms.disposition = disposition;
363*4882a593Smuzhiyun 	oparms.path = full_path;
364*4882a593Smuzhiyun 	oparms.fid = fid;
365*4882a593Smuzhiyun 	oparms.reconnect = false;
366*4882a593Smuzhiyun 	oparms.mode = mode;
367*4882a593Smuzhiyun 	rc = server->ops->open(xid, &oparms, oplock, buf);
368*4882a593Smuzhiyun 	if (rc) {
369*4882a593Smuzhiyun 		cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
370*4882a593Smuzhiyun 		goto out;
371*4882a593Smuzhiyun 	}
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	/*
374*4882a593Smuzhiyun 	 * If Open reported that we actually created a file then we now have to
375*4882a593Smuzhiyun 	 * set the mode if possible.
376*4882a593Smuzhiyun 	 */
377*4882a593Smuzhiyun 	if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
378*4882a593Smuzhiyun 		struct cifs_unix_set_info_args args = {
379*4882a593Smuzhiyun 				.mode	= mode,
380*4882a593Smuzhiyun 				.ctime	= NO_CHANGE_64,
381*4882a593Smuzhiyun 				.atime	= NO_CHANGE_64,
382*4882a593Smuzhiyun 				.mtime	= NO_CHANGE_64,
383*4882a593Smuzhiyun 				.device	= 0,
384*4882a593Smuzhiyun 		};
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 		if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
387*4882a593Smuzhiyun 			args.uid = current_fsuid();
388*4882a593Smuzhiyun 			if (inode->i_mode & S_ISGID)
389*4882a593Smuzhiyun 				args.gid = inode->i_gid;
390*4882a593Smuzhiyun 			else
391*4882a593Smuzhiyun 				args.gid = current_fsgid();
392*4882a593Smuzhiyun 		} else {
393*4882a593Smuzhiyun 			args.uid = INVALID_UID; /* no change */
394*4882a593Smuzhiyun 			args.gid = INVALID_GID; /* no change */
395*4882a593Smuzhiyun 		}
396*4882a593Smuzhiyun 		CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
397*4882a593Smuzhiyun 				       current->tgid);
398*4882a593Smuzhiyun 	} else {
399*4882a593Smuzhiyun 		/*
400*4882a593Smuzhiyun 		 * BB implement mode setting via Windows security
401*4882a593Smuzhiyun 		 * descriptors e.g.
402*4882a593Smuzhiyun 		 */
403*4882a593Smuzhiyun 		/* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 		/* Could set r/o dos attribute if mode & 0222 == 0 */
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun cifs_create_get_file_info:
409*4882a593Smuzhiyun 	/* server might mask mode so we have to query for it */
410*4882a593Smuzhiyun 	if (tcon->unix_ext)
411*4882a593Smuzhiyun 		rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
412*4882a593Smuzhiyun 					      xid);
413*4882a593Smuzhiyun 	else {
414*4882a593Smuzhiyun 		/* TODO: Add support for calling POSIX query info here, but passing in fid */
415*4882a593Smuzhiyun 		rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
416*4882a593Smuzhiyun 					 xid, fid);
417*4882a593Smuzhiyun 		if (newinode) {
418*4882a593Smuzhiyun 			if (server->ops->set_lease_key)
419*4882a593Smuzhiyun 				server->ops->set_lease_key(newinode, fid);
420*4882a593Smuzhiyun 			if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
421*4882a593Smuzhiyun 				newinode->i_mode = mode;
422*4882a593Smuzhiyun 			if ((*oplock & CIFS_CREATE_ACTION) &&
423*4882a593Smuzhiyun 			    (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
424*4882a593Smuzhiyun 				newinode->i_uid = current_fsuid();
425*4882a593Smuzhiyun 				if (inode->i_mode & S_ISGID)
426*4882a593Smuzhiyun 					newinode->i_gid = inode->i_gid;
427*4882a593Smuzhiyun 				else
428*4882a593Smuzhiyun 					newinode->i_gid = current_fsgid();
429*4882a593Smuzhiyun 			}
430*4882a593Smuzhiyun 		}
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun cifs_create_set_dentry:
434*4882a593Smuzhiyun 	if (rc != 0) {
435*4882a593Smuzhiyun 		cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
436*4882a593Smuzhiyun 			 rc);
437*4882a593Smuzhiyun 		goto out_err;
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (S_ISDIR(newinode->i_mode)) {
441*4882a593Smuzhiyun 		rc = -EISDIR;
442*4882a593Smuzhiyun 		goto out_err;
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	d_drop(direntry);
446*4882a593Smuzhiyun 	d_add(direntry, newinode);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun out:
449*4882a593Smuzhiyun 	kfree(buf);
450*4882a593Smuzhiyun 	kfree(full_path);
451*4882a593Smuzhiyun 	return rc;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun out_err:
454*4882a593Smuzhiyun 	if (server->ops->close)
455*4882a593Smuzhiyun 		server->ops->close(xid, tcon, fid);
456*4882a593Smuzhiyun 	if (newinode)
457*4882a593Smuzhiyun 		iput(newinode);
458*4882a593Smuzhiyun 	goto out;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun int
cifs_atomic_open(struct inode * inode,struct dentry * direntry,struct file * file,unsigned oflags,umode_t mode)462*4882a593Smuzhiyun cifs_atomic_open(struct inode *inode, struct dentry *direntry,
463*4882a593Smuzhiyun 		 struct file *file, unsigned oflags, umode_t mode)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun 	int rc;
466*4882a593Smuzhiyun 	unsigned int xid;
467*4882a593Smuzhiyun 	struct tcon_link *tlink;
468*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
469*4882a593Smuzhiyun 	struct TCP_Server_Info *server;
470*4882a593Smuzhiyun 	struct cifs_fid fid = {};
471*4882a593Smuzhiyun 	struct cifs_pending_open open;
472*4882a593Smuzhiyun 	__u32 oplock;
473*4882a593Smuzhiyun 	struct cifsFileInfo *file_info;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun 	/*
476*4882a593Smuzhiyun 	 * Posix open is only called (at lookup time) for file create now. For
477*4882a593Smuzhiyun 	 * opens (rather than creates), because we do not know if it is a file
478*4882a593Smuzhiyun 	 * or directory yet, and current Samba no longer allows us to do posix
479*4882a593Smuzhiyun 	 * open on dirs, we could end up wasting an open call on what turns out
480*4882a593Smuzhiyun 	 * to be a dir. For file opens, we wait to call posix open till
481*4882a593Smuzhiyun 	 * cifs_open.  It could be added to atomic_open in the future but the
482*4882a593Smuzhiyun 	 * performance tradeoff of the extra network request when EISDIR or
483*4882a593Smuzhiyun 	 * EACCES is returned would have to be weighed against the 50% reduction
484*4882a593Smuzhiyun 	 * in network traffic in the other paths.
485*4882a593Smuzhiyun 	 */
486*4882a593Smuzhiyun 	if (!(oflags & O_CREAT)) {
487*4882a593Smuzhiyun 		struct dentry *res;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 		/*
490*4882a593Smuzhiyun 		 * Check for hashed negative dentry. We have already revalidated
491*4882a593Smuzhiyun 		 * the dentry and it is fine. No need to perform another lookup.
492*4882a593Smuzhiyun 		 */
493*4882a593Smuzhiyun 		if (!d_in_lookup(direntry))
494*4882a593Smuzhiyun 			return -ENOENT;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 		res = cifs_lookup(inode, direntry, 0);
497*4882a593Smuzhiyun 		if (IS_ERR(res))
498*4882a593Smuzhiyun 			return PTR_ERR(res);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 		return finish_no_open(file, res);
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	xid = get_xid();
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
506*4882a593Smuzhiyun 		 inode, direntry, direntry);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
509*4882a593Smuzhiyun 	if (IS_ERR(tlink)) {
510*4882a593Smuzhiyun 		rc = PTR_ERR(tlink);
511*4882a593Smuzhiyun 		goto out_free_xid;
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	tcon = tlink_tcon(tlink);
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	rc = check_name(direntry, tcon);
517*4882a593Smuzhiyun 	if (rc)
518*4882a593Smuzhiyun 		goto out;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	server = tcon->ses->server;
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	if (server->ops->new_lease_key)
523*4882a593Smuzhiyun 		server->ops->new_lease_key(&fid);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	cifs_add_pending_open(&fid, tlink, &open);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
528*4882a593Smuzhiyun 			    &oplock, &fid);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	if (rc) {
531*4882a593Smuzhiyun 		cifs_del_pending_open(&open);
532*4882a593Smuzhiyun 		goto out;
533*4882a593Smuzhiyun 	}
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
536*4882a593Smuzhiyun 		file->f_mode |= FMODE_CREATED;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	rc = finish_open(file, direntry, generic_file_open);
539*4882a593Smuzhiyun 	if (rc) {
540*4882a593Smuzhiyun 		if (server->ops->close)
541*4882a593Smuzhiyun 			server->ops->close(xid, tcon, &fid);
542*4882a593Smuzhiyun 		cifs_del_pending_open(&open);
543*4882a593Smuzhiyun 		goto out;
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	if (file->f_flags & O_DIRECT &&
547*4882a593Smuzhiyun 	    CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
548*4882a593Smuzhiyun 		if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
549*4882a593Smuzhiyun 			file->f_op = &cifs_file_direct_nobrl_ops;
550*4882a593Smuzhiyun 		else
551*4882a593Smuzhiyun 			file->f_op = &cifs_file_direct_ops;
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
555*4882a593Smuzhiyun 	if (file_info == NULL) {
556*4882a593Smuzhiyun 		if (server->ops->close)
557*4882a593Smuzhiyun 			server->ops->close(xid, tcon, &fid);
558*4882a593Smuzhiyun 		cifs_del_pending_open(&open);
559*4882a593Smuzhiyun 		rc = -ENOMEM;
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun out:
563*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
564*4882a593Smuzhiyun out_free_xid:
565*4882a593Smuzhiyun 	free_xid(xid);
566*4882a593Smuzhiyun 	return rc;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
cifs_create(struct inode * inode,struct dentry * direntry,umode_t mode,bool excl)569*4882a593Smuzhiyun int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
570*4882a593Smuzhiyun 		bool excl)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	int rc;
573*4882a593Smuzhiyun 	unsigned int xid = get_xid();
574*4882a593Smuzhiyun 	/*
575*4882a593Smuzhiyun 	 * BB below access is probably too much for mknod to request
576*4882a593Smuzhiyun 	 *    but we have to do query and setpathinfo so requesting
577*4882a593Smuzhiyun 	 *    less could fail (unless we want to request getatr and setatr
578*4882a593Smuzhiyun 	 *    permissions (only).  At least for POSIX we do not have to
579*4882a593Smuzhiyun 	 *    request so much.
580*4882a593Smuzhiyun 	 */
581*4882a593Smuzhiyun 	unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
582*4882a593Smuzhiyun 	struct tcon_link *tlink;
583*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
584*4882a593Smuzhiyun 	struct TCP_Server_Info *server;
585*4882a593Smuzhiyun 	struct cifs_fid fid;
586*4882a593Smuzhiyun 	__u32 oplock;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
589*4882a593Smuzhiyun 		 inode, direntry, direntry);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
592*4882a593Smuzhiyun 	rc = PTR_ERR(tlink);
593*4882a593Smuzhiyun 	if (IS_ERR(tlink))
594*4882a593Smuzhiyun 		goto out_free_xid;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	tcon = tlink_tcon(tlink);
597*4882a593Smuzhiyun 	server = tcon->ses->server;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 	if (server->ops->new_lease_key)
600*4882a593Smuzhiyun 		server->ops->new_lease_key(&fid);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
603*4882a593Smuzhiyun 			    &oplock, &fid);
604*4882a593Smuzhiyun 	if (!rc && server->ops->close)
605*4882a593Smuzhiyun 		server->ops->close(xid, tcon, &fid);
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
608*4882a593Smuzhiyun out_free_xid:
609*4882a593Smuzhiyun 	free_xid(xid);
610*4882a593Smuzhiyun 	return rc;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
cifs_mknod(struct inode * inode,struct dentry * direntry,umode_t mode,dev_t device_number)613*4882a593Smuzhiyun int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
614*4882a593Smuzhiyun 		dev_t device_number)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	int rc = -EPERM;
617*4882a593Smuzhiyun 	unsigned int xid;
618*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb;
619*4882a593Smuzhiyun 	struct tcon_link *tlink;
620*4882a593Smuzhiyun 	struct cifs_tcon *tcon;
621*4882a593Smuzhiyun 	char *full_path = NULL;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	if (!old_valid_dev(device_number))
624*4882a593Smuzhiyun 		return -EINVAL;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	cifs_sb = CIFS_SB(inode->i_sb);
627*4882a593Smuzhiyun 	tlink = cifs_sb_tlink(cifs_sb);
628*4882a593Smuzhiyun 	if (IS_ERR(tlink))
629*4882a593Smuzhiyun 		return PTR_ERR(tlink);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	tcon = tlink_tcon(tlink);
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	xid = get_xid();
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	full_path = build_path_from_dentry(direntry);
636*4882a593Smuzhiyun 	if (full_path == NULL) {
637*4882a593Smuzhiyun 		rc = -ENOMEM;
638*4882a593Smuzhiyun 		goto mknod_out;
639*4882a593Smuzhiyun 	}
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
642*4882a593Smuzhiyun 					       full_path, mode,
643*4882a593Smuzhiyun 					       device_number);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun mknod_out:
646*4882a593Smuzhiyun 	kfree(full_path);
647*4882a593Smuzhiyun 	free_xid(xid);
648*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
649*4882a593Smuzhiyun 	return rc;
650*4882a593Smuzhiyun }
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun struct dentry *
cifs_lookup(struct inode * parent_dir_inode,struct dentry * direntry,unsigned int flags)653*4882a593Smuzhiyun cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
654*4882a593Smuzhiyun 	    unsigned int flags)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	unsigned int xid;
657*4882a593Smuzhiyun 	int rc = 0; /* to get around spurious gcc warning, set to zero here */
658*4882a593Smuzhiyun 	struct cifs_sb_info *cifs_sb;
659*4882a593Smuzhiyun 	struct tcon_link *tlink;
660*4882a593Smuzhiyun 	struct cifs_tcon *pTcon;
661*4882a593Smuzhiyun 	struct inode *newInode = NULL;
662*4882a593Smuzhiyun 	char *full_path = NULL;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	xid = get_xid();
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
667*4882a593Smuzhiyun 		 parent_dir_inode, direntry, direntry);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* check whether path exists */
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
672*4882a593Smuzhiyun 	tlink = cifs_sb_tlink(cifs_sb);
673*4882a593Smuzhiyun 	if (IS_ERR(tlink)) {
674*4882a593Smuzhiyun 		free_xid(xid);
675*4882a593Smuzhiyun 		return ERR_CAST(tlink);
676*4882a593Smuzhiyun 	}
677*4882a593Smuzhiyun 	pTcon = tlink_tcon(tlink);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	rc = check_name(direntry, pTcon);
680*4882a593Smuzhiyun 	if (unlikely(rc)) {
681*4882a593Smuzhiyun 		cifs_put_tlink(tlink);
682*4882a593Smuzhiyun 		free_xid(xid);
683*4882a593Smuzhiyun 		return ERR_PTR(rc);
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	/* can not grab the rename sem here since it would
687*4882a593Smuzhiyun 	deadlock in the cases (beginning of sys_rename itself)
688*4882a593Smuzhiyun 	in which we already have the sb rename sem */
689*4882a593Smuzhiyun 	full_path = build_path_from_dentry(direntry);
690*4882a593Smuzhiyun 	if (full_path == NULL) {
691*4882a593Smuzhiyun 		cifs_put_tlink(tlink);
692*4882a593Smuzhiyun 		free_xid(xid);
693*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	if (d_really_is_positive(direntry)) {
697*4882a593Smuzhiyun 		cifs_dbg(FYI, "non-NULL inode in lookup\n");
698*4882a593Smuzhiyun 	} else {
699*4882a593Smuzhiyun 		cifs_dbg(FYI, "NULL inode in lookup\n");
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 	cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
702*4882a593Smuzhiyun 		 full_path, d_inode(direntry));
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 	if (pTcon->posix_extensions)
705*4882a593Smuzhiyun 		rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid);
706*4882a593Smuzhiyun 	else if (pTcon->unix_ext) {
707*4882a593Smuzhiyun 		rc = cifs_get_inode_info_unix(&newInode, full_path,
708*4882a593Smuzhiyun 					      parent_dir_inode->i_sb, xid);
709*4882a593Smuzhiyun 	} else {
710*4882a593Smuzhiyun 		rc = cifs_get_inode_info(&newInode, full_path, NULL,
711*4882a593Smuzhiyun 				parent_dir_inode->i_sb, xid, NULL);
712*4882a593Smuzhiyun 	}
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	if (rc == 0) {
715*4882a593Smuzhiyun 		/* since paths are not looked up by component - the parent
716*4882a593Smuzhiyun 		   directories are presumed to be good here */
717*4882a593Smuzhiyun 		renew_parental_timestamps(direntry);
718*4882a593Smuzhiyun 	} else if (rc == -ENOENT) {
719*4882a593Smuzhiyun 		cifs_set_time(direntry, jiffies);
720*4882a593Smuzhiyun 		newInode = NULL;
721*4882a593Smuzhiyun 	} else {
722*4882a593Smuzhiyun 		if (rc != -EACCES) {
723*4882a593Smuzhiyun 			cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
724*4882a593Smuzhiyun 			/* We special case check for Access Denied - since that
725*4882a593Smuzhiyun 			is a common return code */
726*4882a593Smuzhiyun 		}
727*4882a593Smuzhiyun 		newInode = ERR_PTR(rc);
728*4882a593Smuzhiyun 	}
729*4882a593Smuzhiyun 	kfree(full_path);
730*4882a593Smuzhiyun 	cifs_put_tlink(tlink);
731*4882a593Smuzhiyun 	free_xid(xid);
732*4882a593Smuzhiyun 	return d_splice_alias(newInode, direntry);
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun static int
cifs_d_revalidate(struct dentry * direntry,unsigned int flags)736*4882a593Smuzhiyun cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun 	struct inode *inode;
739*4882a593Smuzhiyun 	int rc;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	if (flags & LOOKUP_RCU)
742*4882a593Smuzhiyun 		return -ECHILD;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	if (d_really_is_positive(direntry)) {
745*4882a593Smuzhiyun 		inode = d_inode(direntry);
746*4882a593Smuzhiyun 		if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
747*4882a593Smuzhiyun 			CIFS_I(inode)->time = 0; /* force reval */
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 		rc = cifs_revalidate_dentry(direntry);
750*4882a593Smuzhiyun 		if (rc) {
751*4882a593Smuzhiyun 			cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
752*4882a593Smuzhiyun 			switch (rc) {
753*4882a593Smuzhiyun 			case -ENOENT:
754*4882a593Smuzhiyun 			case -ESTALE:
755*4882a593Smuzhiyun 				/*
756*4882a593Smuzhiyun 				 * Those errors mean the dentry is invalid
757*4882a593Smuzhiyun 				 * (file was deleted or recreated)
758*4882a593Smuzhiyun 				 */
759*4882a593Smuzhiyun 				return 0;
760*4882a593Smuzhiyun 			default:
761*4882a593Smuzhiyun 				/*
762*4882a593Smuzhiyun 				 * Otherwise some unexpected error happened
763*4882a593Smuzhiyun 				 * report it as-is to VFS layer
764*4882a593Smuzhiyun 				 */
765*4882a593Smuzhiyun 				return rc;
766*4882a593Smuzhiyun 			}
767*4882a593Smuzhiyun 		}
768*4882a593Smuzhiyun 		else {
769*4882a593Smuzhiyun 			/*
770*4882a593Smuzhiyun 			 * If the inode wasn't known to be a dfs entry when
771*4882a593Smuzhiyun 			 * the dentry was instantiated, such as when created
772*4882a593Smuzhiyun 			 * via ->readdir(), it needs to be set now since the
773*4882a593Smuzhiyun 			 * attributes will have been updated by
774*4882a593Smuzhiyun 			 * cifs_revalidate_dentry().
775*4882a593Smuzhiyun 			 */
776*4882a593Smuzhiyun 			if (IS_AUTOMOUNT(inode) &&
777*4882a593Smuzhiyun 			   !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
778*4882a593Smuzhiyun 				spin_lock(&direntry->d_lock);
779*4882a593Smuzhiyun 				direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
780*4882a593Smuzhiyun 				spin_unlock(&direntry->d_lock);
781*4882a593Smuzhiyun 			}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 			return 1;
784*4882a593Smuzhiyun 		}
785*4882a593Smuzhiyun 	}
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	/*
788*4882a593Smuzhiyun 	 * This may be nfsd (or something), anyway, we can't see the
789*4882a593Smuzhiyun 	 * intent of this. So, since this can be for creation, drop it.
790*4882a593Smuzhiyun 	 */
791*4882a593Smuzhiyun 	if (!flags)
792*4882a593Smuzhiyun 		return 0;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	/*
795*4882a593Smuzhiyun 	 * Drop the negative dentry, in order to make sure to use the
796*4882a593Smuzhiyun 	 * case sensitive name which is specified by user if this is
797*4882a593Smuzhiyun 	 * for creation.
798*4882a593Smuzhiyun 	 */
799*4882a593Smuzhiyun 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
800*4882a593Smuzhiyun 		return 0;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
803*4882a593Smuzhiyun 		return 0;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	return 1;
806*4882a593Smuzhiyun }
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun /* static int cifs_d_delete(struct dentry *direntry)
809*4882a593Smuzhiyun {
810*4882a593Smuzhiyun 	int rc = 0;
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	return rc;
815*4882a593Smuzhiyun }     */
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun const struct dentry_operations cifs_dentry_ops = {
818*4882a593Smuzhiyun 	.d_revalidate = cifs_d_revalidate,
819*4882a593Smuzhiyun 	.d_automount = cifs_dfs_d_automount,
820*4882a593Smuzhiyun /* d_delete:       cifs_d_delete,      */ /* not needed except for debugging */
821*4882a593Smuzhiyun };
822*4882a593Smuzhiyun 
cifs_ci_hash(const struct dentry * dentry,struct qstr * q)823*4882a593Smuzhiyun static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun 	struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
826*4882a593Smuzhiyun 	unsigned long hash;
827*4882a593Smuzhiyun 	wchar_t c;
828*4882a593Smuzhiyun 	int i, charlen;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	hash = init_name_hash(dentry);
831*4882a593Smuzhiyun 	for (i = 0; i < q->len; i += charlen) {
832*4882a593Smuzhiyun 		charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
833*4882a593Smuzhiyun 		/* error out if we can't convert the character */
834*4882a593Smuzhiyun 		if (unlikely(charlen < 0))
835*4882a593Smuzhiyun 			return charlen;
836*4882a593Smuzhiyun 		hash = partial_name_hash(cifs_toupper(c), hash);
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 	q->hash = end_name_hash(hash);
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	return 0;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun 
cifs_ci_compare(const struct dentry * dentry,unsigned int len,const char * str,const struct qstr * name)843*4882a593Smuzhiyun static int cifs_ci_compare(const struct dentry *dentry,
844*4882a593Smuzhiyun 		unsigned int len, const char *str, const struct qstr *name)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun 	struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
847*4882a593Smuzhiyun 	wchar_t c1, c2;
848*4882a593Smuzhiyun 	int i, l1, l2;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	/*
851*4882a593Smuzhiyun 	 * We make the assumption here that uppercase characters in the local
852*4882a593Smuzhiyun 	 * codepage are always the same length as their lowercase counterparts.
853*4882a593Smuzhiyun 	 *
854*4882a593Smuzhiyun 	 * If that's ever not the case, then this will fail to match it.
855*4882a593Smuzhiyun 	 */
856*4882a593Smuzhiyun 	if (name->len != len)
857*4882a593Smuzhiyun 		return 1;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 	for (i = 0; i < len; i += l1) {
860*4882a593Smuzhiyun 		/* Convert characters in both strings to UTF-16. */
861*4882a593Smuzhiyun 		l1 = codepage->char2uni(&str[i], len - i, &c1);
862*4882a593Smuzhiyun 		l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 		/*
865*4882a593Smuzhiyun 		 * If we can't convert either character, just declare it to
866*4882a593Smuzhiyun 		 * be 1 byte long and compare the original byte.
867*4882a593Smuzhiyun 		 */
868*4882a593Smuzhiyun 		if (unlikely(l1 < 0 && l2 < 0)) {
869*4882a593Smuzhiyun 			if (str[i] != name->name[i])
870*4882a593Smuzhiyun 				return 1;
871*4882a593Smuzhiyun 			l1 = 1;
872*4882a593Smuzhiyun 			continue;
873*4882a593Smuzhiyun 		}
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 		/*
876*4882a593Smuzhiyun 		 * Here, we again ass|u|me that upper/lowercase versions of
877*4882a593Smuzhiyun 		 * a character are the same length in the local NLS.
878*4882a593Smuzhiyun 		 */
879*4882a593Smuzhiyun 		if (l1 != l2)
880*4882a593Smuzhiyun 			return 1;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 		/* Now compare uppercase versions of these characters */
883*4882a593Smuzhiyun 		if (cifs_toupper(c1) != cifs_toupper(c2))
884*4882a593Smuzhiyun 			return 1;
885*4882a593Smuzhiyun 	}
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	return 0;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun const struct dentry_operations cifs_ci_dentry_ops = {
891*4882a593Smuzhiyun 	.d_revalidate = cifs_d_revalidate,
892*4882a593Smuzhiyun 	.d_hash = cifs_ci_hash,
893*4882a593Smuzhiyun 	.d_compare = cifs_ci_compare,
894*4882a593Smuzhiyun 	.d_automount = cifs_dfs_d_automount,
895*4882a593Smuzhiyun };
896