xref: /linux/fs/smb/client/dir.c (revision 81dc1e4d32b064ac47abc60b0acbf49b66a34d52)
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 "cifsglob.h"
18 #include "cifsproto.h"
19 #include "cifs_debug.h"
20 #include "cifs_fs_sb.h"
21 #include "cifs_unicode.h"
22 #include "fs_context.h"
23 #include "cifs_ioctl.h"
24 #include "fscache.h"
25 #include "cached_dir.h"
26 
27 static void
renew_parental_timestamps(struct dentry * direntry)28 renew_parental_timestamps(struct dentry *direntry)
29 {
30 	/* BB check if there is a way to get the kernel to do this or if we
31 	   really need this */
32 	do {
33 		cifs_set_time(direntry, jiffies);
34 		direntry = direntry->d_parent;
35 	} while (!IS_ROOT(direntry));
36 }
37 
38 char *
cifs_build_path_to_root(struct smb3_fs_context * ctx,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,int add_treename)39 cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
40 			struct cifs_tcon *tcon, int add_treename)
41 {
42 	int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
43 	int dfsplen;
44 	char *full_path = NULL;
45 
46 	/* if no prefix path, simply set path to the root of share to "" */
47 	if (pplen == 0) {
48 		full_path = kzalloc(1, GFP_KERNEL);
49 		return full_path;
50 	}
51 
52 	if (add_treename)
53 		dfsplen = strnlen(tcon->tree_name, MAX_TREE_SIZE + 1);
54 	else
55 		dfsplen = 0;
56 
57 	full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
58 	if (full_path == NULL)
59 		return full_path;
60 
61 	if (dfsplen)
62 		memcpy(full_path, tcon->tree_name, dfsplen);
63 	full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
64 	memcpy(full_path + dfsplen + 1, ctx->prepath, pplen);
65 	convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
66 	return full_path;
67 }
68 
69 /* Note: caller must free return buffer */
70 const char *
build_path_from_dentry(struct dentry * direntry,void * page)71 build_path_from_dentry(struct dentry *direntry, void *page)
72 {
73 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
74 	struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
75 	bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
76 
77 	return build_path_from_dentry_optional_prefix(direntry, page,
78 						      prefix);
79 }
80 
__build_path_from_dentry_optional_prefix(struct dentry * direntry,void * page,const char * tree,int tree_len,bool prefix)81 char *__build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page,
82 					       const char *tree, int tree_len,
83 					       bool prefix)
84 {
85 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry);
86 	unsigned int sbflags = cifs_sb_flags(cifs_sb);
87 	char dirsep = CIFS_DIR_SEP(cifs_sb);
88 	int pplen = 0;
89 	int dfsplen;
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 (sbflags & 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 (sbflags & 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 
build_path_from_dentry_optional_prefix(struct dentry * direntry,void * page,bool prefix)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
check_name(struct dentry * direntry,struct cifs_tcon * tcon)154 check_name(struct dentry *direntry, struct cifs_tcon *tcon)
155 {
156 	struct cifs_sb_info *cifs_sb = CIFS_SB(direntry);
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_flags(cifs_sb) & 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 
alloc_parent_path(struct dentry * dentry,size_t namelen)175 static char *alloc_parent_path(struct dentry *dentry, size_t namelen)
176 {
177 	struct cifs_sb_info *cifs_sb = CIFS_SB(dentry);
178 	void *page = alloc_dentry_path();
179 	const char *path;
180 	size_t size;
181 	char *npath;
182 
183 	path = build_path_from_dentry(dentry->d_parent, page);
184 	if (IS_ERR(path)) {
185 		npath = ERR_CAST(path);
186 		goto out;
187 	}
188 
189 	size = strlen(path) + namelen + 2;
190 	npath = kmalloc(size, GFP_KERNEL);
191 	if (!npath)
192 		npath = ERR_PTR(-ENOMEM);
193 	else
194 		scnprintf(npath, size, "%s%c", path, CIFS_DIR_SEP(cifs_sb));
195 out:
196 	free_dentry_path(page);
197 	return npath;
198 }
199 
200 /* Inode operations in similar order to how they appear in Linux file fs.h */
__cifs_do_create(struct inode * dir,struct dentry * direntry,const char * full_path,unsigned int xid,struct tcon_link * tlink,unsigned int oflags,umode_t mode,__u32 * oplock,struct cifs_fid * fid,struct cifs_open_info_data * buf,struct inode ** inode)201 static int __cifs_do_create(struct inode *dir, struct dentry *direntry,
202 			    const char *full_path, unsigned int xid,
203 			    struct tcon_link *tlink, unsigned int oflags,
204 			    umode_t mode, __u32 *oplock, struct cifs_fid *fid,
205 			    struct cifs_open_info_data *buf,
206 			    struct inode **inode)
207 {
208 	int rc = -ENOENT;
209 	int create_options = CREATE_NOT_DIR;
210 	int desired_access;
211 	struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
212 	struct cifs_tcon *tcon = tlink_tcon(tlink);
213 	struct inode *newinode = NULL;
214 	unsigned int sbflags = cifs_sb_flags(cifs_sb);
215 	int disposition;
216 	struct TCP_Server_Info *server = tcon->ses->server;
217 	struct cifs_open_parms oparms;
218 	struct cached_fid *parent_cfid = NULL;
219 	int rdwr_for_fscache = 0;
220 	__le32 lease_flags = 0;
221 
222 	*inode = NULL;
223 	*oplock = 0;
224 	if (tcon->ses->server->oplocks)
225 		*oplock = REQ_OPLOCK;
226 
227 	/* If we're caching, we need to be able to fill in around partial writes. */
228 	if (cifs_fscache_enabled(dir) && (oflags & O_ACCMODE) == O_WRONLY)
229 		rdwr_for_fscache = 1;
230 
231 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
232 	if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
233 	    (CIFS_UNIX_POSIX_PATH_OPS_CAP &
234 			le64_to_cpu(tcon->fsUnixInfo.Capability))) {
235 		rc = cifs_posix_open(full_path, &newinode, dir->i_sb, mode,
236 				     oflags, oplock, &fid->netfid, xid);
237 		switch (rc) {
238 		case 0:
239 			if (newinode == NULL) {
240 				/* query inode info */
241 				goto cifs_create_get_file_info;
242 			}
243 
244 			if (S_ISDIR(newinode->i_mode)) {
245 				CIFSSMBClose(xid, tcon, fid->netfid);
246 				iput(newinode);
247 				return -EISDIR;
248 			}
249 
250 			if (!S_ISREG(newinode->i_mode)) {
251 				/*
252 				 * The server may allow us to open things like
253 				 * FIFOs, but the client isn't set up to deal
254 				 * with that. If it's not a regular file, just
255 				 * close it and proceed as if it were a normal
256 				 * lookup.
257 				 */
258 				CIFSSMBClose(xid, tcon, fid->netfid);
259 				goto cifs_create_get_file_info;
260 			}
261 			/* success, no need to query */
262 			goto cifs_create_set_dentry;
263 
264 		case -ENOENT:
265 			goto cifs_create_get_file_info;
266 
267 		case -EIO:
268 		case -EINVAL:
269 			/*
270 			 * EIO could indicate that (posix open) operation is not
271 			 * supported, despite what server claimed in capability
272 			 * negotiation.
273 			 *
274 			 * POSIX open in samba versions 3.3.1 and earlier could
275 			 * incorrectly fail with invalid parameter.
276 			 */
277 			tcon->broken_posix_open = true;
278 			break;
279 
280 		case -EREMOTE:
281 		case -EOPNOTSUPP:
282 			/*
283 			 * EREMOTE indicates DFS junction, which is not handled
284 			 * in posix open.  If either that or op not supported
285 			 * returned, follow the normal lookup.
286 			 */
287 			break;
288 
289 		default:
290 			return rc;
291 		}
292 		/*
293 		 * fallthrough to retry, using older open call, this is case
294 		 * where server does not support this SMB level, and falsely
295 		 * claims capability (also get here for DFS case which should be
296 		 * rare for path not covered on files)
297 		 */
298 	}
299 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
300 
301 	desired_access = 0;
302 	if (OPEN_FMODE(oflags) & FMODE_READ)
303 		desired_access |= GENERIC_READ; /* is this too little? */
304 	if (OPEN_FMODE(oflags) & FMODE_WRITE)
305 		desired_access |= GENERIC_WRITE;
306 	if (rdwr_for_fscache == 1)
307 		desired_access |= GENERIC_READ;
308 	if (oflags & O_TMPFILE)
309 		desired_access |= DELETE;
310 
311 	disposition = FILE_OVERWRITE_IF;
312 	if (oflags & O_CREAT) {
313 		if (oflags & O_EXCL)
314 			disposition = FILE_CREATE;
315 		else if (oflags & O_TRUNC)
316 			disposition = FILE_OVERWRITE_IF;
317 		else
318 			disposition = FILE_OPEN_IF;
319 	} else if (oflags & O_TMPFILE) {
320 		disposition = FILE_CREATE;
321 	} else {
322 		cifs_dbg(FYI, "Create flag not set in create function\n");
323 	}
324 
325 	/*
326 	 * BB add processing to set equivalent of mode - e.g. via CreateX with
327 	 * ACLs
328 	 */
329 
330 	if (!server->ops->open)
331 		return -EOPNOTSUPP;
332 
333 	create_options |= cifs_open_create_options(oflags, create_options);
334 	/*
335 	 * if we're not using unix extensions, see if we need to set
336 	 * ATTR_READONLY on the create call
337 	 */
338 	if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
339 		create_options |= CREATE_OPTION_READONLY;
340 
341 
342 retry_open:
343 	if (tcon->cfids && direntry->d_parent && server->dialect >= SMB30_PROT_ID) {
344 		parent_cfid = NULL;
345 		spin_lock(&tcon->cfids->cfid_list_lock);
346 		list_for_each_entry(parent_cfid, &tcon->cfids->entries, entry) {
347 			if (parent_cfid->dentry == direntry->d_parent) {
348 				cifs_dbg(FYI, "found a parent cached file handle\n");
349 				if (is_valid_cached_dir(parent_cfid)) {
350 					lease_flags
351 						|= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
352 					memcpy(fid->parent_lease_key,
353 					       parent_cfid->fid.lease_key,
354 					       SMB2_LEASE_KEY_SIZE);
355 					parent_cfid->dirents.is_valid = false;
356 					parent_cfid->dirents.is_failed = true;
357 				}
358 				break;
359 			}
360 		}
361 		spin_unlock(&tcon->cfids->cfid_list_lock);
362 	}
363 
364 	oparms = (struct cifs_open_parms) {
365 		.tcon = tcon,
366 		.cifs_sb = cifs_sb,
367 		.desired_access = desired_access,
368 		.create_options = cifs_create_options(cifs_sb, create_options),
369 		.disposition = disposition,
370 		.path = full_path,
371 		.fid = fid,
372 		.lease_flags = lease_flags,
373 		.mode = mode,
374 	};
375 	rc = server->ops->open(xid, &oparms, oplock, buf);
376 	if (rc) {
377 		cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
378 		if (rc == -EACCES && rdwr_for_fscache == 1) {
379 			desired_access &= ~GENERIC_READ;
380 			rdwr_for_fscache = 2;
381 			goto retry_open;
382 		}
383 		return rc;
384 	}
385 	if (rdwr_for_fscache == 2)
386 		cifs_invalidate_cache(dir, FSCACHE_INVAL_DIO_WRITE);
387 
388 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
389 	/*
390 	 * If Open reported that we actually created a file then we now have to
391 	 * set the mode if possible.
392 	 */
393 	if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
394 		struct cifs_unix_set_info_args args = {
395 				.mode	= mode,
396 				.ctime	= NO_CHANGE_64,
397 				.atime	= NO_CHANGE_64,
398 				.mtime	= NO_CHANGE_64,
399 				.device	= 0,
400 		};
401 
402 		if (sbflags & CIFS_MOUNT_SET_UID) {
403 			args.uid = current_fsuid();
404 			if (dir->i_mode & S_ISGID)
405 				args.gid = dir->i_gid;
406 			else
407 				args.gid = current_fsgid();
408 		} else {
409 			args.uid = INVALID_UID; /* no change */
410 			args.gid = INVALID_GID; /* no change */
411 		}
412 		CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
413 				       current->tgid);
414 	} else {
415 		/*
416 		 * BB implement mode setting via Windows security
417 		 * descriptors e.g.
418 		 */
419 		/* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
420 
421 		/* Could set r/o dos attribute if mode & 0222 == 0 */
422 	}
423 
424 cifs_create_get_file_info:
425 	/* server might mask mode so we have to query for it */
426 	if (tcon->unix_ext)
427 		rc = cifs_get_inode_info_unix(&newinode, full_path, dir->i_sb,
428 					      xid);
429 	else {
430 #else
431 	{
432 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
433 		/* TODO: Add support for calling POSIX query info here, but passing in fid */
434 		rc = cifs_get_inode_info(&newinode, full_path, buf, dir->i_sb, xid, fid);
435 		if (newinode) {
436 			if (server->ops->set_lease_key)
437 				server->ops->set_lease_key(newinode, fid);
438 			if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) {
439 				if (sbflags & CIFS_MOUNT_DYNPERM)
440 					newinode->i_mode = mode;
441 				if (sbflags & CIFS_MOUNT_SET_UID) {
442 					newinode->i_uid = current_fsuid();
443 					if (dir->i_mode & S_ISGID)
444 						newinode->i_gid = dir->i_gid;
445 					else
446 						newinode->i_gid = current_fsgid();
447 				}
448 			}
449 		}
450 	}
451 
452 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
453 cifs_create_set_dentry:
454 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
455 	if (rc != 0) {
456 		cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
457 			 rc);
458 		goto out_err;
459 	}
460 
461 	if (newinode && S_ISDIR(newinode->i_mode)) {
462 		rc = -EISDIR;
463 		goto out_err;
464 	}
465 
466 	*inode = newinode;
467 	return rc;
468 
469 out_err:
470 	if (server->ops->close)
471 		server->ops->close(xid, tcon, fid);
472 	if (newinode)
473 		iput(newinode);
474 	return rc;
475 }
476 
477 static int cifs_do_create(struct inode *dir, struct dentry *direntry,
478 			  unsigned int xid, struct tcon_link *tlink,
479 			  unsigned int oflags, umode_t mode,
480 			  __u32 *oplock, struct cifs_fid *fid,
481 			  struct cifs_open_info_data *buf,
482 			  struct inode **inode)
483 {
484 	void *page = alloc_dentry_path();
485 	const char *full_path;
486 	int rc;
487 
488 	full_path = build_path_from_dentry(direntry, page);
489 	if (IS_ERR(full_path)) {
490 		rc = PTR_ERR(full_path);
491 	} else {
492 		rc = __cifs_do_create(dir, direntry, full_path, xid,
493 				      tlink, oflags, mode, oplock,
494 				      fid, buf, inode);
495 	}
496 	free_dentry_path(page);
497 	return rc;
498 }
499 
500 
501 /*
502  * Look up, create and open a CIFS file.
503  *
504  * The initial dentry state is in-lookup or hashed-negative.  On success, dentry
505  * will become hashed-positive by calling d_splice_alias() if in-lookup,
506  * otherwise d_instantiate().
507  */
508 int cifs_atomic_open(struct inode *dir, struct dentry *direntry,
509 		     struct file *file, unsigned int oflags, umode_t mode)
510 {
511 	struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
512 	struct cifs_open_info_data buf = {};
513 	struct TCP_Server_Info *server;
514 	struct cifsFileInfo *file_info;
515 	struct cifs_pending_open open;
516 	struct cifs_fid fid = {};
517 	struct tcon_link *tlink;
518 	struct cifs_tcon *tcon;
519 	unsigned int sbflags;
520 	struct dentry *alias;
521 	struct inode *inode;
522 	unsigned int xid;
523 	__u32 oplock;
524 	int rc;
525 
526 	if (unlikely(cifs_forced_shutdown(cifs_sb)))
527 		return smb_EIO(smb_eio_trace_forced_shutdown);
528 
529 	/*
530 	 * Posix open is only called (at lookup time) for file create now. For
531 	 * opens (rather than creates), because we do not know if it is a file
532 	 * or directory yet, and current Samba no longer allows us to do posix
533 	 * open on dirs, we could end up wasting an open call on what turns out
534 	 * to be a dir. For file opens, we wait to call posix open till
535 	 * cifs_open.  It could be added to atomic_open in the future but the
536 	 * performance tradeoff of the extra network request when EISDIR or
537 	 * EACCES is returned would have to be weighed against the 50% reduction
538 	 * in network traffic in the other paths.
539 	 */
540 	if (!(oflags & O_CREAT)) {
541 		/*
542 		 * Check for hashed negative dentry. We have already revalidated
543 		 * the dentry and it is fine. No need to perform another lookup.
544 		 */
545 		if (!d_in_lookup(direntry))
546 			return -ENOENT;
547 
548 		return finish_no_open(file, cifs_lookup(dir, direntry, 0));
549 	}
550 
551 	xid = get_xid();
552 
553 	cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
554 		 dir, direntry, direntry);
555 
556 	tlink = cifs_sb_tlink(cifs_sb);
557 	if (IS_ERR(tlink)) {
558 		rc = PTR_ERR(tlink);
559 		goto out_free_xid;
560 	}
561 
562 	tcon = tlink_tcon(tlink);
563 
564 	rc = check_name(direntry, tcon);
565 	if (rc)
566 		goto out;
567 
568 	server = tcon->ses->server;
569 
570 	if (server->ops->new_lease_key)
571 		server->ops->new_lease_key(&fid);
572 
573 	cifs_add_pending_open(&fid, tlink, &open);
574 
575 	rc = cifs_do_create(dir, direntry, xid, tlink, oflags, mode,
576 			    &oplock, &fid, &buf, &inode);
577 	if (rc) {
578 		cifs_del_pending_open(&open);
579 		goto out;
580 	}
581 
582 	if (d_in_lookup(direntry)) {
583 		alias = d_splice_alias(inode, direntry);
584 		if (!IS_ERR_OR_NULL(alias))
585 			direntry = alias;
586 	} else {
587 		d_instantiate(direntry, inode);
588 	}
589 
590 	if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
591 		file->f_mode |= FMODE_CREATED;
592 
593 	rc = finish_open(file, direntry, generic_file_open);
594 	if (rc) {
595 		if (server->ops->close)
596 			server->ops->close(xid, tcon, &fid);
597 		cifs_del_pending_open(&open);
598 		goto out;
599 	}
600 
601 	sbflags = cifs_sb_flags(cifs_sb);
602 	if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) {
603 		if (sbflags & CIFS_MOUNT_NO_BRL)
604 			file->f_op = &cifs_file_direct_nobrl_ops;
605 		else
606 			file->f_op = &cifs_file_direct_ops;
607 	}
608 
609 	file_info = cifs_new_fileinfo(&fid, file, tlink, oplock, buf.symlink_target);
610 	if (file_info == NULL) {
611 		if (server->ops->close)
612 			server->ops->close(xid, tcon, &fid);
613 		cifs_del_pending_open(&open);
614 		rc = -ENOMEM;
615 		goto out;
616 	}
617 
618 	fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
619 			   file->f_mode & FMODE_WRITE);
620 
621 out:
622 	cifs_put_tlink(tlink);
623 out_free_xid:
624 	free_xid(xid);
625 	cifs_free_open_info(&buf);
626 	return rc;
627 }
628 
629 /*
630  * Create a CIFS file.
631  *
632  * The initial dentry state is hashed-negative.  On success, dentry will become
633  * hashed-positive by calling d_instantiate().
634  */
635 int cifs_create(struct mnt_idmap *idmap, struct inode *dir,
636 		struct dentry *direntry, umode_t mode, bool excl)
637 {
638 	struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
639 	int rc;
640 	unsigned int xid = get_xid();
641 	/*
642 	 * BB below access is probably too much for mknod to request
643 	 *    but we have to do query and setpathinfo so requesting
644 	 *    less could fail (unless we want to request getatr and setatr
645 	 *    permissions (only).  At least for POSIX we do not have to
646 	 *    request so much.
647 	 */
648 	unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
649 	struct tcon_link *tlink;
650 	struct cifs_tcon *tcon;
651 	struct TCP_Server_Info *server;
652 	struct inode *inode;
653 	struct cifs_fid fid;
654 	__u32 oplock;
655 	struct cifs_open_info_data buf = {};
656 
657 	cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
658 		 dir, direntry, direntry);
659 
660 	if (unlikely(cifs_forced_shutdown(cifs_sb))) {
661 		rc = smb_EIO(smb_eio_trace_forced_shutdown);
662 		goto out_free_xid;
663 	}
664 
665 	tlink = cifs_sb_tlink(cifs_sb);
666 	rc = PTR_ERR(tlink);
667 	if (IS_ERR(tlink))
668 		goto out_free_xid;
669 
670 	tcon = tlink_tcon(tlink);
671 	server = tcon->ses->server;
672 
673 	if (server->ops->new_lease_key)
674 		server->ops->new_lease_key(&fid);
675 
676 	rc = cifs_do_create(dir, direntry, xid, tlink, oflags,
677 			    mode, &oplock, &fid, &buf, &inode);
678 	if (!rc) {
679 		d_instantiate(direntry, inode);
680 		if (server->ops->close)
681 			server->ops->close(xid, tcon, &fid);
682 	}
683 
684 	cifs_free_open_info(&buf);
685 	cifs_put_tlink(tlink);
686 out_free_xid:
687 	free_xid(xid);
688 	return rc;
689 }
690 
691 int cifs_mknod(struct mnt_idmap *idmap, struct inode *inode,
692 	       struct dentry *direntry, umode_t mode, dev_t device_number)
693 {
694 	int rc = -EPERM;
695 	unsigned int xid;
696 	struct cifs_sb_info *cifs_sb;
697 	struct tcon_link *tlink;
698 	struct cifs_tcon *tcon;
699 	const char *full_path;
700 	void *page;
701 
702 	if (!old_valid_dev(device_number))
703 		return -EINVAL;
704 
705 	cifs_sb = CIFS_SB(inode->i_sb);
706 	if (unlikely(cifs_forced_shutdown(cifs_sb)))
707 		return smb_EIO(smb_eio_trace_forced_shutdown);
708 
709 	tlink = cifs_sb_tlink(cifs_sb);
710 	if (IS_ERR(tlink))
711 		return PTR_ERR(tlink);
712 
713 	page = alloc_dentry_path();
714 	tcon = tlink_tcon(tlink);
715 	xid = get_xid();
716 
717 	full_path = build_path_from_dentry(direntry, page);
718 	if (IS_ERR(full_path)) {
719 		rc = PTR_ERR(full_path);
720 		goto mknod_out;
721 	}
722 
723 	trace_smb3_mknod_enter(xid, tcon->tid, tcon->ses->Suid, full_path);
724 
725 	rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
726 					       full_path, mode,
727 					       device_number);
728 
729 mknod_out:
730 	if (rc)
731 		trace_smb3_mknod_err(xid,  tcon->tid, tcon->ses->Suid, rc);
732 	else
733 		trace_smb3_mknod_done(xid, tcon->tid, tcon->ses->Suid);
734 
735 	free_dentry_path(page);
736 	free_xid(xid);
737 	cifs_put_tlink(tlink);
738 	return rc;
739 }
740 
741 struct dentry *
742 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
743 	    unsigned int flags)
744 {
745 	unsigned int xid;
746 	int rc = 0; /* to get around spurious gcc warning, set to zero here */
747 	struct cifs_sb_info *cifs_sb;
748 	struct tcon_link *tlink;
749 	struct cifs_tcon *pTcon;
750 	struct inode *newInode = NULL;
751 	const char *full_path;
752 	void *page;
753 	int retry_count = 0;
754 	struct dentry *de;
755 
756 	xid = get_xid();
757 
758 	cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
759 		 parent_dir_inode, direntry, direntry);
760 
761 	/* check whether path exists */
762 
763 	cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
764 	tlink = cifs_sb_tlink(cifs_sb);
765 	if (IS_ERR(tlink)) {
766 		de = ERR_CAST(tlink);
767 		goto free_xid;
768 	}
769 	pTcon = tlink_tcon(tlink);
770 
771 	rc = check_name(direntry, pTcon);
772 	if (unlikely(rc)) {
773 		de = ERR_PTR(rc);
774 		goto put_tlink;
775 	}
776 
777 	/* can not grab the rename sem here since it would
778 	deadlock in the cases (beginning of sys_rename itself)
779 	in which we already have the sb rename sem */
780 	page = alloc_dentry_path();
781 	full_path = build_path_from_dentry(direntry, page);
782 	if (IS_ERR(full_path)) {
783 		de = ERR_CAST(full_path);
784 		goto free_dentry_path;
785 	}
786 
787 	if (d_really_is_positive(direntry)) {
788 		cifs_dbg(FYI, "non-NULL inode in lookup\n");
789 	} else {
790 		struct cached_fid *cfid = NULL;
791 
792 		cifs_dbg(FYI, "NULL inode in lookup\n");
793 
794 		/*
795 		 * We can only rely on negative dentries having the same
796 		 * spelling as the cached dirent if case insensitivity is
797 		 * forced on mount.
798 		 *
799 		 * XXX: if servers correctly announce Case Sensitivity Search
800 		 * on GetInfo of FileFSAttributeInformation, then we can take
801 		 * correct action even if case insensitive is not forced on
802 		 * mount.
803 		 */
804 		if (pTcon->nocase && !open_cached_dir_by_dentry(pTcon, direntry->d_parent, &cfid)) {
805 			/*
806 			 * dentry is negative and parent is fully cached:
807 			 * we can assume file does not exist
808 			 */
809 			if (cfid->dirents.is_valid) {
810 				close_cached_dir(cfid);
811 				goto out;
812 			}
813 			close_cached_dir(cfid);
814 		}
815 	}
816 	cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
817 		 full_path, d_inode(direntry));
818 
819 again:
820 	if (pTcon->posix_extensions) {
821 		rc = smb311_posix_get_inode_info(&newInode, full_path, NULL,
822 						 parent_dir_inode->i_sb, xid);
823 	} else if (pTcon->unix_ext) {
824 		rc = cifs_get_inode_info_unix(&newInode, full_path,
825 					      parent_dir_inode->i_sb, xid);
826 	} else {
827 		rc = cifs_get_inode_info(&newInode, full_path, NULL,
828 				parent_dir_inode->i_sb, xid, NULL);
829 	}
830 
831 	if (rc == 0) {
832 		/* since paths are not looked up by component - the parent
833 		   directories are presumed to be good here */
834 		renew_parental_timestamps(direntry);
835 	} else if (rc == -EAGAIN && retry_count++ < 10) {
836 		goto again;
837 	} else if (rc == -ENOENT) {
838 		cifs_set_time(direntry, jiffies);
839 		newInode = NULL;
840 	} else {
841 		if (rc != -EACCES) {
842 			cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
843 			/* We special case check for Access Denied - since that
844 			is a common return code */
845 		}
846 		newInode = ERR_PTR(rc);
847 	}
848 
849 out:
850 	de = d_splice_alias(newInode, direntry);
851 free_dentry_path:
852 	free_dentry_path(page);
853 put_tlink:
854 	cifs_put_tlink(tlink);
855 free_xid:
856 	free_xid(xid);
857 	return de;
858 }
859 
860 static int
861 cifs_d_revalidate(struct inode *dir, const struct qstr *name,
862 		  struct dentry *direntry, unsigned int flags)
863 {
864 	if (flags & LOOKUP_RCU)
865 		return -ECHILD;
866 
867 	if (d_really_is_positive(direntry)) {
868 		int rc;
869 		struct inode *inode = d_inode(direntry);
870 
871 		if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
872 			CIFS_I(inode)->time = 0; /* force reval */
873 
874 		rc = cifs_revalidate_dentry(direntry);
875 		if (rc) {
876 			cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
877 			switch (rc) {
878 			case -ENOENT:
879 			case -ESTALE:
880 				/*
881 				 * Those errors mean the dentry is invalid
882 				 * (file was deleted or recreated)
883 				 */
884 				return 0;
885 			default:
886 				/*
887 				 * Otherwise some unexpected error happened
888 				 * report it as-is to VFS layer
889 				 */
890 				return rc;
891 			}
892 		}
893 		else {
894 			/*
895 			 * If the inode wasn't known to be a dfs entry when
896 			 * the dentry was instantiated, such as when created
897 			 * via ->readdir(), it needs to be set now since the
898 			 * attributes will have been updated by
899 			 * cifs_revalidate_dentry().
900 			 */
901 			if (IS_AUTOMOUNT(inode) &&
902 			   !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
903 				spin_lock(&direntry->d_lock);
904 				direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
905 				spin_unlock(&direntry->d_lock);
906 			}
907 
908 			return 1;
909 		}
910 	} else {
911 		struct cifs_sb_info *cifs_sb = CIFS_SB(dir->i_sb);
912 		struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
913 		struct cached_fid *cfid;
914 
915 		if (!open_cached_dir_by_dentry(tcon, direntry->d_parent, &cfid)) {
916 			/*
917 			 * dentry is negative and parent is fully cached:
918 			 * we can assume file does not exist
919 			 */
920 			if (cfid->dirents.is_valid) {
921 				close_cached_dir(cfid);
922 				return 1;
923 			}
924 			close_cached_dir(cfid);
925 		}
926 	}
927 
928 	/*
929 	 * This may be nfsd (or something), anyway, we can't see the
930 	 * intent of this. So, since this can be for creation, drop it.
931 	 */
932 	if (!flags)
933 		return 0;
934 
935 	/*
936 	 * Drop the negative dentry, in order to make sure to use the
937 	 * case sensitive name which is specified by user if this is
938 	 * for creation.
939 	 */
940 	if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
941 		return 0;
942 
943 	if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
944 		return 0;
945 
946 	return 1;
947 }
948 
949 /* static int cifs_d_delete(struct dentry *direntry)
950 {
951 	int rc = 0;
952 
953 	cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
954 
955 	return rc;
956 }     */
957 
958 const struct dentry_operations cifs_dentry_ops = {
959 	.d_revalidate = cifs_d_revalidate,
960 	.d_automount = cifs_d_automount,
961 /* d_delete:       cifs_d_delete,      */ /* not needed except for debugging */
962 };
963 
964 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
965 {
966 	struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
967 	unsigned long hash;
968 	wchar_t c;
969 	int i, charlen;
970 
971 	hash = init_name_hash(dentry);
972 	for (i = 0; i < q->len; i += charlen) {
973 		charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
974 		/* error out if we can't convert the character */
975 		if (unlikely(charlen < 0))
976 			return charlen;
977 		hash = partial_name_hash(cifs_toupper(c), hash);
978 	}
979 	q->hash = end_name_hash(hash);
980 
981 	return 0;
982 }
983 
984 static int cifs_ci_compare(const struct dentry *dentry,
985 		unsigned int len, const char *str, const struct qstr *name)
986 {
987 	struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
988 	wchar_t c1, c2;
989 	int i, l1, l2;
990 
991 	/*
992 	 * We make the assumption here that uppercase characters in the local
993 	 * codepage are always the same length as their lowercase counterparts.
994 	 *
995 	 * If that's ever not the case, then this will fail to match it.
996 	 */
997 	if (name->len != len)
998 		return 1;
999 
1000 	for (i = 0; i < len; i += l1) {
1001 		/* Convert characters in both strings to UTF-16. */
1002 		l1 = codepage->char2uni(&str[i], len - i, &c1);
1003 		l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
1004 
1005 		/*
1006 		 * If we can't convert either character, just declare it to
1007 		 * be 1 byte long and compare the original byte.
1008 		 */
1009 		if (unlikely(l1 < 0 && l2 < 0)) {
1010 			if (str[i] != name->name[i])
1011 				return 1;
1012 			l1 = 1;
1013 			continue;
1014 		}
1015 
1016 		/*
1017 		 * Here, we again ass|u|me that upper/lowercase versions of
1018 		 * a character are the same length in the local NLS.
1019 		 */
1020 		if (l1 != l2)
1021 			return 1;
1022 
1023 		/* Now compare uppercase versions of these characters */
1024 		if (cifs_toupper(c1) != cifs_toupper(c2))
1025 			return 1;
1026 	}
1027 
1028 	return 0;
1029 }
1030 
1031 static int set_tmpfile_attr(const unsigned int xid, unsigned int oflags,
1032 			    struct inode *inode, const char *full_path,
1033 			    struct TCP_Server_Info *server)
1034 {
1035 	struct cifsInodeInfo *cinode = CIFS_I(inode);
1036 	FILE_BASIC_INFO fi;
1037 
1038 	cinode->cifsAttrs |= ATTR_HIDDEN;
1039 	if (oflags & O_EXCL)
1040 		cinode->cifsAttrs |= ATTR_TEMPORARY;
1041 
1042 	fi = (FILE_BASIC_INFO) {
1043 		.Attributes = cpu_to_le32(cinode->cifsAttrs),
1044 	};
1045 	return server->ops->set_file_info(inode, full_path, &fi, xid);
1046 }
1047 
1048 /*
1049  * Create a hidden temporary CIFS file with delete-on-close bit set.
1050  *
1051  * The initial dentry state is unhashed-negative.  On success, dentry will
1052  * become unhashed-positive by calling d_instantiate().
1053  */
1054 int cifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
1055 		 struct file *file, umode_t mode)
1056 {
1057 	struct dentry *dentry = file->f_path.dentry;
1058 	struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
1059 	char *path __free(kfree) = NULL, *name;
1060 	unsigned int oflags = file->f_flags;
1061 	size_t size = CIFS_TMPNAME_LEN + 1;
1062 	int retries = 0, max_retries = 16;
1063 	struct TCP_Server_Info *server;
1064 	struct cifs_pending_open open;
1065 	struct cifsFileInfo *cfile;
1066 	struct cifs_fid fid = {};
1067 	struct tcon_link *tlink;
1068 	struct cifs_tcon *tcon;
1069 	unsigned int sbflags;
1070 	struct inode *inode;
1071 	unsigned int xid;
1072 	__u32 oplock;
1073 	int rc;
1074 
1075 	if (unlikely(cifs_forced_shutdown(cifs_sb)))
1076 		return smb_EIO(smb_eio_trace_forced_shutdown);
1077 
1078 	tlink = cifs_sb_tlink(cifs_sb);
1079 	if (IS_ERR(tlink))
1080 		return PTR_ERR(tlink);
1081 	tcon = tlink_tcon(tlink);
1082 	server = tcon->ses->server;
1083 
1084 	xid = get_xid();
1085 
1086 	if (server->vals->protocol_id < SMB20_PROT_ID) {
1087 		cifs_dbg(VFS | ONCE, "O_TMPFILE is supported only in SMB2+\n");
1088 		rc = -EOPNOTSUPP;
1089 		goto out;
1090 	}
1091 
1092 	if (server->ops->new_lease_key)
1093 		server->ops->new_lease_key(&fid);
1094 	cifs_add_pending_open(&fid, tlink, &open);
1095 
1096 	path = alloc_parent_path(dentry, size - 1);
1097 	if (IS_ERR(path)) {
1098 		cifs_del_pending_open(&open);
1099 		rc = PTR_ERR(path);
1100 		path = NULL;
1101 		goto out;
1102 	}
1103 
1104 	name = path + strlen(path);
1105 	do {
1106 		scnprintf(name, size,
1107 			  CIFS_TMPNAME_PREFIX "%0*x",
1108 			  CIFS_TMPNAME_COUNTER_LEN,
1109 			  atomic_inc_return(&cifs_tmpcounter));
1110 		rc = __cifs_do_create(dir, dentry, path, xid, tlink, oflags,
1111 				      mode, &oplock, &fid, NULL, &inode);
1112 		if (!rc) {
1113 			set_nlink(inode, 0);
1114 			mark_inode_dirty(inode);
1115 			d_mark_tmpfile_name(file, &QSTR_LEN(name, size - 1));
1116 			d_instantiate(dentry, inode);
1117 			break;
1118 		}
1119 	} while (unlikely(rc == -EEXIST) && ++retries < max_retries);
1120 
1121 	if (rc) {
1122 		cifs_del_pending_open(&open);
1123 		goto out;
1124 	}
1125 
1126 	rc = finish_open(file, dentry, generic_file_open);
1127 	if (rc)
1128 		goto err_open;
1129 
1130 	sbflags = cifs_sb_flags(cifs_sb);
1131 	if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) {
1132 		if (sbflags & CIFS_MOUNT_NO_BRL)
1133 			file->f_op = &cifs_file_direct_nobrl_ops;
1134 		else
1135 			file->f_op = &cifs_file_direct_ops;
1136 	}
1137 
1138 	cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, NULL);
1139 	if (!cfile) {
1140 		rc = -ENOMEM;
1141 		goto err_open;
1142 	}
1143 
1144 	rc = set_tmpfile_attr(xid, oflags, inode, path, server);
1145 	if (rc)
1146 		goto out;
1147 
1148 	fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
1149 			   file->f_mode & FMODE_WRITE);
1150 out:
1151 	cifs_put_tlink(tlink);
1152 	free_xid(xid);
1153 	return rc;
1154 err_open:
1155 	cifs_del_pending_open(&open);
1156 	if (server->ops->close)
1157 		server->ops->close(xid, tcon, &fid);
1158 	goto out;
1159 }
1160 
1161 char *cifs_silly_fullpath(struct dentry *dentry)
1162 {
1163 	unsigned char name[CIFS_SILLYNAME_LEN + 1];
1164 	int retries = 0, max_retries = 16;
1165 	size_t namesize = sizeof(name);
1166 	struct dentry *sdentry = NULL;
1167 	char *path;
1168 
1169 	do {
1170 		dput(sdentry);
1171 		scnprintf(name, namesize,
1172 			  CIFS_SILLYNAME_PREFIX "%0*x",
1173 			  CIFS_SILLYNAME_COUNTER_LEN,
1174 			  atomic_inc_return(&cifs_sillycounter));
1175 		sdentry = lookup_noperm(&QSTR(name), dentry->d_parent);
1176 		if (IS_ERR(sdentry))
1177 			return ERR_CAST(sdentry);
1178 		if (d_is_negative(sdentry)) {
1179 			dput(sdentry);
1180 			path = alloc_parent_path(dentry, CIFS_SILLYNAME_LEN);
1181 			if (!IS_ERR(path))
1182 				strcat(path, name);
1183 			return path;
1184 		}
1185 	} while (++retries < max_retries);
1186 	dput(sdentry);
1187 	return ERR_PTR(-EBUSY);
1188 }
1189 
1190 const struct dentry_operations cifs_ci_dentry_ops = {
1191 	.d_revalidate = cifs_d_revalidate,
1192 	.d_hash = cifs_ci_hash,
1193 	.d_compare = cifs_ci_compare,
1194 	.d_automount = cifs_d_automount,
1195 };
1196