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