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