xref: /linux/fs/overlayfs/dir.c (revision 3ba310f2a3ca70f0497aab5c2e8aa85a12e19406)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/namei.h>
9 #include <linux/xattr.h>
10 #include <linux/security.h>
11 #include <linux/cred.h>
12 #include <linux/module.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/atomic.h>
16 #include <linux/ratelimit.h>
17 #include <linux/backing-file.h>
18 #include "overlayfs.h"
19 
20 static unsigned short ovl_redirect_max = 256;
21 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
22 MODULE_PARM_DESC(redirect_max,
23 		 "Maximum length of absolute redirect xattr value");
24 
25 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
26 
27 static int ovl_cleanup_locked(struct ovl_fs *ofs, struct inode *wdir,
28 			      struct dentry *wdentry)
29 {
30 	int err;
31 
32 	dget(wdentry);
33 	if (d_is_dir(wdentry))
34 		err = ovl_do_rmdir(ofs, wdir, wdentry);
35 	else
36 		err = ovl_do_unlink(ofs, wdir, wdentry);
37 	dput(wdentry);
38 
39 	if (err) {
40 		pr_err("cleanup of '%pd2' failed (%i)\n",
41 		       wdentry, err);
42 	}
43 
44 	return err;
45 }
46 
47 int ovl_cleanup(struct ovl_fs *ofs, struct dentry *workdir,
48 		struct dentry *wdentry)
49 {
50 	wdentry = start_removing_dentry(workdir, wdentry);
51 	if (IS_ERR(wdentry))
52 		return PTR_ERR(wdentry);
53 
54 	ovl_cleanup_locked(ofs, workdir->d_inode, wdentry);
55 	end_removing(wdentry);
56 
57 	return 0;
58 }
59 
60 void ovl_tempname(char name[OVL_TEMPNAME_SIZE])
61 {
62 	static atomic_t temp_id = ATOMIC_INIT(0);
63 
64 	/* counter is allowed to wrap, since temp dentries are ephemeral */
65 	snprintf(name, OVL_TEMPNAME_SIZE, "#%x", atomic_inc_return(&temp_id));
66 }
67 
68 static struct dentry *ovl_start_creating_temp(struct ovl_fs *ofs,
69 					      struct dentry *workdir,
70 					      char name[OVL_TEMPNAME_SIZE])
71 {
72 	ovl_tempname(name);
73 	return start_creating(ovl_upper_mnt_idmap(ofs), workdir,
74 			      &QSTR(name));
75 }
76 
77 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
78 {
79 	int err;
80 	struct dentry *whiteout, *link;
81 	struct dentry *workdir = ofs->workdir;
82 	struct inode *wdir = workdir->d_inode;
83 	char name[OVL_TEMPNAME_SIZE];
84 
85 	guard(mutex)(&ofs->whiteout_lock);
86 
87 	if (!ofs->whiteout) {
88 		whiteout = ovl_start_creating_temp(ofs, workdir, name);
89 		if (IS_ERR(whiteout))
90 			return whiteout;
91 		err = ovl_do_whiteout(ofs, wdir, whiteout);
92 		if (!err)
93 			ofs->whiteout = dget(whiteout);
94 		end_creating(whiteout);
95 		if (err)
96 			return ERR_PTR(err);
97 	}
98 
99 	if (!ofs->no_shared_whiteout) {
100 		link = ovl_start_creating_temp(ofs, workdir, name);
101 		if (IS_ERR(link))
102 			return link;
103 		err = ovl_do_link(ofs, ofs->whiteout, wdir, link);
104 		if (!err)
105 			whiteout = dget(link);
106 		end_creating(link);
107 		if (!err)
108 			return whiteout;
109 
110 		if (err != -EMLINK) {
111 			pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%u)\n",
112 				ofs->whiteout->d_inode->i_nlink,
113 				err);
114 			ofs->no_shared_whiteout = true;
115 		}
116 	}
117 	whiteout = ofs->whiteout;
118 	ofs->whiteout = NULL;
119 	return whiteout;
120 }
121 
122 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct dentry *dir,
123 			     struct dentry *dentry)
124 {
125 	struct dentry *whiteout;
126 	struct renamedata rd = {};
127 	int err;
128 	int flags = 0;
129 
130 	whiteout = ovl_whiteout(ofs);
131 	err = PTR_ERR(whiteout);
132 	if (IS_ERR(whiteout))
133 		return err;
134 
135 	if (d_is_dir(dentry))
136 		flags = RENAME_EXCHANGE;
137 
138 	rd.mnt_idmap = ovl_upper_mnt_idmap(ofs);
139 	rd.old_parent = ofs->workdir;
140 	rd.new_parent = dir;
141 	rd.flags = flags;
142 	err = start_renaming_two_dentries(&rd, whiteout, dentry);
143 	if (!err) {
144 		err = ovl_do_rename_rd(&rd);
145 		end_renaming(&rd);
146 	}
147 	if (err)
148 		goto kill_whiteout;
149 	if (flags)
150 		ovl_cleanup(ofs, ofs->workdir, dentry);
151 
152 out:
153 	dput(whiteout);
154 	return err;
155 
156 kill_whiteout:
157 	ovl_cleanup(ofs, ofs->workdir, whiteout);
158 	goto out;
159 }
160 
161 struct dentry *ovl_create_real(struct ovl_fs *ofs, struct dentry *parent,
162 			       struct dentry *newdentry, struct qstr *qname,
163 			       struct ovl_cattr *attr)
164 {
165 	struct inode *dir = parent->d_inode;
166 	int err;
167 
168 	if (IS_ERR(newdentry))
169 		return newdentry;
170 
171 	err = -ESTALE;
172 	if (newdentry->d_inode)
173 		goto out;
174 
175 	if (attr->hardlink) {
176 		err = ovl_do_link(ofs, attr->hardlink, dir, newdentry);
177 	} else {
178 		switch (attr->mode & S_IFMT) {
179 		case S_IFREG:
180 			err = ovl_do_create(ofs, dir, newdentry, attr->mode);
181 			break;
182 
183 		case S_IFDIR:
184 			/* mkdir is special... */
185 			newdentry =  ovl_do_mkdir(ofs, dir, newdentry, attr->mode);
186 			err = PTR_ERR_OR_ZERO(newdentry);
187 			/* expect to inherit casefolding from workdir/upperdir */
188 			if (!err && ofs->casefold != ovl_dentry_casefolded(newdentry)) {
189 				pr_warn_ratelimited("wrong inherited casefold (%pd2)\n",
190 						    newdentry);
191 				end_creating(newdentry);
192 				err = -EINVAL;
193 			}
194 			break;
195 
196 		case S_IFCHR:
197 		case S_IFBLK:
198 		case S_IFIFO:
199 		case S_IFSOCK:
200 			err = ovl_do_mknod(ofs, dir, newdentry, attr->mode,
201 					   attr->rdev);
202 			break;
203 
204 		case S_IFLNK:
205 			err = ovl_do_symlink(ofs, dir, newdentry, attr->link);
206 			break;
207 
208 		default:
209 			err = -EPERM;
210 		}
211 	}
212 	if (err)
213 		goto out;
214 
215 	if (WARN_ON(!newdentry->d_inode)) {
216 		/*
217 		 * Not quite sure if non-instantiated dentry is legal or not.
218 		 * VFS doesn't seem to care so check and warn here.
219 		 */
220 		err = -EIO;
221 	} else if (d_unhashed(newdentry)) {
222 		struct dentry *d;
223 		/*
224 		 * Some filesystems (i.e. casefolded) may return an unhashed
225 		 * negative dentry from the ovl_start_creating_upper() call before
226 		 * ovl_create_real().
227 		 * In that case, lookup again after making the newdentry
228 		 * positive, so ovl_create_upper() always returns a hashed
229 		 * positive dentry.  We lookup using qname which should be
230 		 * the same name as newentry, but is certain not to change.
231 		 * As we have to drop the lock before the lookup a race
232 		 * could result in a lookup failure.  In that case we return
233 		 * an error.
234 		 */
235 		end_creating_keep(newdentry);
236 		d = ovl_start_creating_upper(ofs, parent, qname);
237 
238 		if (IS_ERR_OR_NULL(d)) {
239 			err = d ? PTR_ERR(d) : -ENOENT;
240 		} else if (d->d_inode != newdentry->d_inode) {
241 			err = -EIO;
242 		} else {
243 			dput(newdentry);
244 			return d;
245 		}
246 		end_creating(d);
247 		dput(newdentry);
248 		return ERR_PTR(err);
249 	}
250 out:
251 	if (err) {
252 		end_creating(newdentry);
253 		return ERR_PTR(err);
254 	}
255 	return newdentry;
256 }
257 
258 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
259 			       struct ovl_cattr *attr)
260 {
261 	struct dentry *ret;
262 	char name[OVL_TEMPNAME_SIZE];
263 
264 	ret = ovl_start_creating_temp(ofs, workdir, name);
265 	if (IS_ERR(ret))
266 		return ret;
267 	ret = ovl_create_real(ofs, workdir, ret, &QSTR(name), attr);
268 	return end_creating_keep(ret);
269 }
270 
271 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
272 			       int xerr)
273 {
274 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
275 	int err;
276 
277 	err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
278 	if (!err)
279 		ovl_dentry_set_opaque(dentry);
280 
281 	return err;
282 }
283 
284 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
285 {
286 	/*
287 	 * Fail with -EIO when trying to create opaque dir and upper doesn't
288 	 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
289 	 * return a specific error for noxattr case.
290 	 */
291 	return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
292 }
293 
294 /*
295  * Common operations required to be done after creation of file on upper.
296  * If @hardlink is false, then @inode is a pre-allocated inode, we may or
297  * may not use to instantiate the new dentry.
298  */
299 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
300 			   struct dentry *newdentry, bool hardlink, struct file *tmpfile)
301 {
302 	struct ovl_inode_params oip = {
303 		.upperdentry = newdentry,
304 		.newinode = inode,
305 	};
306 
307 	ovl_dentry_set_upper_alias(dentry);
308 	ovl_dentry_init_reval(dentry, newdentry, NULL);
309 
310 	if (!hardlink) {
311 		/*
312 		 * ovl_obtain_alias() can be called after ovl_create_real()
313 		 * and before we get here, so we may get an inode from cache
314 		 * with the same real upperdentry that is not the inode we
315 		 * pre-allocated.  In this case we will use the cached inode
316 		 * to instantiate the new dentry.
317 		 *
318 		 * XXX: if we ever use ovl_obtain_alias() to decode directory
319 		 * file handles, need to use ovl_get_inode_locked() and
320 		 * d_instantiate_new() here to prevent from creating two
321 		 * hashed directory inode aliases.  We then need to return
322 		 * the obtained alias to ovl_mkdir().
323 		 */
324 		inode = ovl_get_inode(dentry->d_sb, &oip);
325 		if (IS_ERR(inode))
326 			return PTR_ERR(inode);
327 		if (inode == oip.newinode)
328 			ovl_set_flag(OVL_UPPERDATA, inode);
329 	} else {
330 		WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
331 		dput(newdentry);
332 		inc_nlink(inode);
333 	}
334 
335 	if (tmpfile)
336 		d_mark_tmpfile(tmpfile, inode);
337 
338 	d_instantiate(dentry, inode);
339 	if (inode != oip.newinode) {
340 		pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
341 				    dentry);
342 	}
343 
344 	/* Force lookup of new upper hardlink to find its lower */
345 	if (hardlink)
346 		d_drop(dentry);
347 
348 	return 0;
349 }
350 
351 static bool ovl_type_merge(struct dentry *dentry)
352 {
353 	return OVL_TYPE_MERGE(ovl_path_type(dentry));
354 }
355 
356 static bool ovl_type_origin(struct dentry *dentry)
357 {
358 	return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
359 }
360 
361 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
362 			    struct ovl_cattr *attr)
363 {
364 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
365 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
366 	struct dentry *newdentry;
367 	struct qstr qname = QSTR_LEN(dentry->d_name.name,
368 				     dentry->d_name.len);
369 	int err;
370 
371 	newdentry = ovl_start_creating_upper(ofs, upperdir,
372 					     &qname);
373 	if (IS_ERR(newdentry))
374 		return PTR_ERR(newdentry);
375 	newdentry = ovl_create_real(ofs, upperdir, newdentry, &qname, attr);
376 	if (IS_ERR(newdentry))
377 		return PTR_ERR(newdentry);
378 
379 	end_creating_keep(newdentry);
380 
381 	if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
382 	    !ovl_allow_offline_changes(ofs)) {
383 		/* Setting opaque here is just an optimization, allow to fail */
384 		ovl_set_opaque(dentry, newdentry);
385 	}
386 
387 	ovl_dir_modified(dentry->d_parent, false);
388 	err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink, NULL);
389 	if (err)
390 		goto out_cleanup;
391 	return 0;
392 
393 out_cleanup:
394 	ovl_cleanup(ofs, upperdir, newdentry);
395 	dput(newdentry);
396 	return err;
397 }
398 
399 static struct dentry *ovl_clear_empty(struct dentry *dentry,
400 				      struct list_head *list)
401 {
402 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
403 	struct dentry *workdir = ovl_workdir(dentry);
404 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
405 	struct renamedata rd = {};
406 	struct path upperpath;
407 	struct dentry *upper;
408 	struct dentry *opaquedir;
409 	struct kstat stat;
410 	int err;
411 
412 	if (WARN_ON(!workdir))
413 		return ERR_PTR(-EROFS);
414 
415 	ovl_path_upper(dentry, &upperpath);
416 	err = vfs_getattr(&upperpath, &stat,
417 			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
418 	if (err)
419 		goto out;
420 
421 	err = -ESTALE;
422 	if (!S_ISDIR(stat.mode))
423 		goto out;
424 	upper = upperpath.dentry;
425 
426 	opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
427 	err = PTR_ERR(opaquedir);
428 	if (IS_ERR(opaquedir))
429 		goto out;
430 
431 	rd.mnt_idmap = ovl_upper_mnt_idmap(ofs);
432 	rd.old_parent = workdir;
433 	rd.new_parent = upperdir;
434 	rd.flags = RENAME_EXCHANGE;
435 	err = start_renaming_two_dentries(&rd, opaquedir, upper);
436 	if (err)
437 		goto out_cleanup_unlocked;
438 
439 	err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
440 	if (err)
441 		goto out_cleanup;
442 
443 	err = ovl_set_opaque(dentry, opaquedir);
444 	if (err)
445 		goto out_cleanup;
446 
447 	inode_lock(opaquedir->d_inode);
448 	err = ovl_set_attr(ofs, opaquedir, &stat);
449 	inode_unlock(opaquedir->d_inode);
450 	if (err)
451 		goto out_cleanup;
452 
453 	err = ovl_do_rename_rd(&rd);
454 	end_renaming(&rd);
455 	if (err)
456 		goto out_cleanup_unlocked;
457 
458 	ovl_cleanup_whiteouts(ofs, upper, list);
459 	ovl_cleanup(ofs, workdir, upper);
460 
461 	/* dentry's upper doesn't match now, get rid of it */
462 	d_drop(dentry);
463 
464 	return opaquedir;
465 
466 out_cleanup:
467 	end_renaming(&rd);
468 out_cleanup_unlocked:
469 	ovl_cleanup(ofs, workdir, opaquedir);
470 	dput(opaquedir);
471 out:
472 	return ERR_PTR(err);
473 }
474 
475 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
476 			     const char *acl_name, struct posix_acl *acl)
477 {
478 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
479 		return 0;
480 
481 	return ovl_do_set_acl(ofs, upperdentry, acl_name, acl);
482 }
483 
484 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
485 				    struct ovl_cattr *cattr)
486 {
487 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
488 	struct dentry *workdir = ovl_workdir(dentry);
489 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
490 	struct renamedata rd = {};
491 	struct dentry *upper;
492 	struct dentry *newdentry;
493 	int err;
494 	struct posix_acl *acl, *default_acl;
495 	bool hardlink = !!cattr->hardlink;
496 
497 	if (WARN_ON(!workdir))
498 		return -EROFS;
499 
500 	if (!hardlink) {
501 		err = posix_acl_create(dentry->d_parent->d_inode,
502 				       &cattr->mode, &default_acl, &acl);
503 		if (err)
504 			return err;
505 	}
506 
507 	upper = ovl_lookup_upper_unlocked(ofs, dentry->d_name.name, upperdir,
508 					  dentry->d_name.len);
509 	err = PTR_ERR(upper);
510 	if (IS_ERR(upper))
511 		goto out;
512 
513 	err = -ESTALE;
514 	if (d_is_negative(upper) || !ovl_upper_is_whiteout(ofs, upper))
515 		goto out_dput;
516 
517 	newdentry = ovl_create_temp(ofs, workdir, cattr);
518 	err = PTR_ERR(newdentry);
519 	if (IS_ERR(newdentry))
520 		goto out_dput;
521 
522 	rd.mnt_idmap = ovl_upper_mnt_idmap(ofs);
523 	rd.old_parent = workdir;
524 	rd.new_parent = upperdir;
525 	rd.flags = 0;
526 	err = start_renaming_two_dentries(&rd, newdentry, upper);
527 	if (err)
528 		goto out_cleanup_unlocked;
529 
530 	/*
531 	 * mode could have been mutilated due to umask (e.g. sgid directory)
532 	 */
533 	if (!hardlink &&
534 	    !S_ISLNK(cattr->mode) &&
535 	    newdentry->d_inode->i_mode != cattr->mode) {
536 		struct iattr attr = {
537 			.ia_valid = ATTR_MODE,
538 			.ia_mode = cattr->mode,
539 		};
540 		inode_lock(newdentry->d_inode);
541 		err = ovl_do_notify_change(ofs, newdentry, &attr);
542 		inode_unlock(newdentry->d_inode);
543 		if (err)
544 			goto out_cleanup;
545 	}
546 	if (!hardlink) {
547 		err = ovl_set_upper_acl(ofs, newdentry,
548 					XATTR_NAME_POSIX_ACL_ACCESS, acl);
549 		if (err)
550 			goto out_cleanup;
551 
552 		err = ovl_set_upper_acl(ofs, newdentry,
553 					XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
554 		if (err)
555 			goto out_cleanup;
556 	}
557 
558 	if (!hardlink && S_ISDIR(cattr->mode)) {
559 		err = ovl_set_opaque(dentry, newdentry);
560 		if (err)
561 			goto out_cleanup;
562 
563 		rd.flags = RENAME_EXCHANGE;
564 		err = ovl_do_rename_rd(&rd);
565 		end_renaming(&rd);
566 		if (err)
567 			goto out_cleanup_unlocked;
568 
569 		ovl_cleanup(ofs, workdir, upper);
570 	} else {
571 		err = ovl_do_rename_rd(&rd);
572 		end_renaming(&rd);
573 		if (err)
574 			goto out_cleanup_unlocked;
575 	}
576 	ovl_dir_modified(dentry->d_parent, false);
577 	err = ovl_instantiate(dentry, inode, newdentry, hardlink, NULL);
578 	if (err) {
579 		ovl_cleanup(ofs, upperdir, newdentry);
580 		dput(newdentry);
581 	}
582 out_dput:
583 	dput(upper);
584 out:
585 	if (!hardlink) {
586 		posix_acl_release(acl);
587 		posix_acl_release(default_acl);
588 	}
589 	return err;
590 
591 out_cleanup:
592 	end_renaming(&rd);
593 out_cleanup_unlocked:
594 	ovl_cleanup(ofs, workdir, newdentry);
595 	dput(newdentry);
596 	goto out_dput;
597 }
598 
599 static const struct cred *ovl_override_creator_creds(const struct cred *original_creds,
600 						     struct dentry *dentry, struct inode *inode, umode_t mode)
601 {
602 	int err;
603 
604 	if (WARN_ON_ONCE(current->cred != ovl_creds(dentry->d_sb)))
605 		return ERR_PTR(-EINVAL);
606 
607 	CLASS(prepare_creds, override_cred)();
608 	if (!override_cred)
609 		return ERR_PTR(-ENOMEM);
610 
611 	override_cred->fsuid = inode->i_uid;
612 	override_cred->fsgid = inode->i_gid;
613 
614 	err = security_dentry_create_files_as(dentry, mode, &dentry->d_name,
615 					      original_creds, override_cred);
616 	if (err)
617 		return ERR_PTR(err);
618 
619 	return override_creds(no_free_ptr(override_cred));
620 }
621 
622 static void ovl_revert_creator_creds(const struct cred *old_cred)
623 {
624 	const struct cred *override_cred;
625 
626 	override_cred = revert_creds(old_cred);
627 	put_cred(override_cred);
628 }
629 
630 DEFINE_CLASS(ovl_override_creator_creds,
631 	     const struct cred *,
632 	     if (!IS_ERR_OR_NULL(_T)) ovl_revert_creator_creds(_T),
633 	     ovl_override_creator_creds(original_creds, dentry, inode, mode),
634 	     const struct cred *original_creds,
635 	     struct dentry *dentry,
636 	     struct inode *inode,
637 	     umode_t mode)
638 
639 static int ovl_create_handle_whiteouts(struct dentry *dentry,
640 				       struct inode *inode,
641 				       struct ovl_cattr *attr)
642 {
643 	if (!ovl_dentry_is_whiteout(dentry))
644 		return ovl_create_upper(dentry, inode, attr);
645 
646 	return ovl_create_over_whiteout(dentry, inode, attr);
647 }
648 
649 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
650 			      struct ovl_cattr *attr, bool origin)
651 {
652 	int err;
653 	struct dentry *parent = dentry->d_parent;
654 
655 	scoped_class(override_creds_ovl, original_creds, dentry->d_sb) {
656 		/*
657 		 * When linking a file with copy up origin into a new parent, mark the
658 		 * new parent dir "impure".
659 		 */
660 		if (origin) {
661 			err = ovl_set_impure(parent, ovl_dentry_upper(parent));
662 			if (err)
663 				return err;
664 		}
665 
666 		/*
667 		 * In the creation cases(create, mkdir, mknod, symlink),
668 		 * ovl should transfer current's fs{u,g}id to underlying
669 		 * fs. Because underlying fs want to initialize its new
670 		 * inode owner using current's fs{u,g}id. And in this
671 		 * case, the @inode is a new inode that is initialized
672 		 * in inode_init_owner() to current's fs{u,g}id. So use
673 		 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
674 		 *
675 		 * But in the other hardlink case, ovl_link() does not
676 		 * create a new inode, so just use the ovl mounter's
677 		 * fs{u,g}id.
678 		 */
679 
680 		if (attr->hardlink)
681 			return ovl_create_handle_whiteouts(dentry, inode, attr);
682 
683 		scoped_class(ovl_override_creator_creds, cred, original_creds, dentry, inode, attr->mode) {
684 			if (IS_ERR(cred))
685 				return PTR_ERR(cred);
686 			return ovl_create_handle_whiteouts(dentry, inode, attr);
687 		}
688 	}
689 	return err;
690 }
691 
692 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
693 			     const char *link)
694 {
695 	int err;
696 	struct inode *inode;
697 	struct ovl_cattr attr = {
698 		.rdev = rdev,
699 		.link = link,
700 	};
701 
702 	err = ovl_copy_up(dentry->d_parent);
703 	if (err)
704 		return err;
705 
706 	err = ovl_want_write(dentry);
707 	if (err)
708 		goto out;
709 
710 	/* Preallocate inode to be used by ovl_get_inode() */
711 	err = -ENOMEM;
712 	inode = ovl_new_inode(dentry->d_sb, mode, rdev);
713 	if (!inode)
714 		goto out_drop_write;
715 
716 	spin_lock(&inode->i_lock);
717 	inode_state_set(inode, I_CREATING);
718 	spin_unlock(&inode->i_lock);
719 
720 	inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode);
721 	attr.mode = inode->i_mode;
722 
723 	err = ovl_create_or_link(dentry, inode, &attr, false);
724 	/* Did we end up using the preallocated inode? */
725 	if (inode != d_inode(dentry))
726 		iput(inode);
727 
728 out_drop_write:
729 	ovl_drop_write(dentry);
730 out:
731 	return err;
732 }
733 
734 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir,
735 		      struct dentry *dentry, umode_t mode, bool excl)
736 {
737 	return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
738 }
739 
740 static struct dentry *ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir,
741 				struct dentry *dentry, umode_t mode)
742 {
743 	return ERR_PTR(ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL));
744 }
745 
746 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
747 		     struct dentry *dentry, umode_t mode, dev_t rdev)
748 {
749 	/* Don't allow creation of "whiteout" on overlay */
750 	if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
751 		return -EPERM;
752 
753 	return ovl_create_object(dentry, mode, rdev, NULL);
754 }
755 
756 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,
757 		       struct dentry *dentry, const char *link)
758 {
759 	return ovl_create_object(dentry, S_IFLNK, 0, link);
760 }
761 
762 static int ovl_set_link_redirect(struct dentry *dentry)
763 {
764 	with_ovl_creds(dentry->d_sb)
765 		return ovl_set_redirect(dentry, false);
766 }
767 
768 static int ovl_link(struct dentry *old, struct inode *newdir,
769 		    struct dentry *new)
770 {
771 	int err;
772 	struct inode *inode;
773 
774 	err = ovl_copy_up(old);
775 	if (err)
776 		goto out;
777 
778 	err = ovl_copy_up(new->d_parent);
779 	if (err)
780 		goto out;
781 
782 	err = ovl_nlink_start(old);
783 	if (err)
784 		goto out;
785 
786 	if (ovl_is_metacopy_dentry(old)) {
787 		err = ovl_set_link_redirect(old);
788 		if (err)
789 			goto out_nlink_end;
790 	}
791 
792 	inode = d_inode(old);
793 	ihold(inode);
794 
795 	err = ovl_create_or_link(new, inode,
796 			&(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
797 			ovl_type_origin(old));
798 	if (err)
799 		iput(inode);
800 
801 out_nlink_end:
802 	ovl_nlink_end(old);
803 out:
804 	return err;
805 }
806 
807 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
808 {
809 	return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
810 }
811 
812 static int ovl_remove_and_whiteout(struct dentry *dentry,
813 				   struct list_head *list)
814 {
815 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
816 	struct dentry *workdir = ovl_workdir(dentry);
817 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
818 	struct dentry *upper;
819 	struct dentry *opaquedir = NULL;
820 	int err;
821 
822 	if (WARN_ON(!workdir))
823 		return -EROFS;
824 
825 	if (!list_empty(list)) {
826 		opaquedir = ovl_clear_empty(dentry, list);
827 		err = PTR_ERR(opaquedir);
828 		if (IS_ERR(opaquedir))
829 			goto out;
830 	}
831 
832 	upper = ovl_lookup_upper_unlocked(ofs, dentry->d_name.name, upperdir,
833 					  dentry->d_name.len);
834 	err = PTR_ERR(upper);
835 	if (IS_ERR(upper))
836 		goto out_dput;
837 
838 	err = -ESTALE;
839 	if ((opaquedir && upper != opaquedir) ||
840 	    (!opaquedir && ovl_dentry_upper(dentry) &&
841 	     !ovl_matches_upper(dentry, upper))) {
842 		goto out_dput_upper;
843 	}
844 
845 	err = ovl_cleanup_and_whiteout(ofs, upperdir, upper);
846 	if (!err)
847 		ovl_dir_modified(dentry->d_parent, true);
848 
849 	d_drop(dentry);
850 out_dput_upper:
851 	dput(upper);
852 out_dput:
853 	dput(opaquedir);
854 out:
855 	return err;
856 }
857 
858 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
859 			    struct list_head *list)
860 {
861 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
862 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
863 	struct inode *dir = upperdir->d_inode;
864 	struct dentry *upper;
865 	struct dentry *opaquedir = NULL;
866 	int err;
867 
868 	if (!list_empty(list)) {
869 		opaquedir = ovl_clear_empty(dentry, list);
870 		err = PTR_ERR(opaquedir);
871 		if (IS_ERR(opaquedir))
872 			goto out;
873 	}
874 
875 	upper = ovl_start_removing_upper(ofs, upperdir,
876 					 &QSTR_LEN(dentry->d_name.name,
877 						   dentry->d_name.len));
878 	err = PTR_ERR(upper);
879 	if (IS_ERR(upper))
880 		goto out_dput;
881 
882 	err = -ESTALE;
883 	if ((opaquedir && upper != opaquedir) ||
884 	    (!opaquedir && !ovl_matches_upper(dentry, upper)))
885 		goto out_unlock;
886 
887 	if (is_dir)
888 		err = ovl_do_rmdir(ofs, dir, upper);
889 	else
890 		err = ovl_do_unlink(ofs, dir, upper);
891 	ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
892 
893 	/*
894 	 * Keeping this dentry hashed would mean having to release
895 	 * upperpath/lowerpath, which could only be done if we are the
896 	 * sole user of this dentry.  Too tricky...  Just unhash for
897 	 * now.
898 	 */
899 	if (!err)
900 		d_drop(dentry);
901 out_unlock:
902 	end_removing(upper);
903 out_dput:
904 	dput(opaquedir);
905 out:
906 	return err;
907 }
908 
909 static bool ovl_pure_upper(struct dentry *dentry)
910 {
911 	return !ovl_dentry_lower(dentry) &&
912 	       !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
913 }
914 
915 static void ovl_drop_nlink(struct dentry *dentry)
916 {
917 	struct inode *inode = d_inode(dentry);
918 	struct dentry *alias;
919 
920 	/* Try to find another, hashed alias */
921 	spin_lock(&inode->i_lock);
922 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
923 		if (alias != dentry && !d_unhashed(alias))
924 			break;
925 	}
926 	spin_unlock(&inode->i_lock);
927 
928 	/*
929 	 * Changes to underlying layers may cause i_nlink to lose sync with
930 	 * reality.  In this case prevent the link count from going to zero
931 	 * prematurely.
932 	 */
933 	if (inode->i_nlink > !!alias)
934 		drop_nlink(inode);
935 }
936 
937 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
938 {
939 	int err;
940 	bool lower_positive = ovl_lower_positive(dentry);
941 	LIST_HEAD(list);
942 
943 	/* No need to clean pure upper removed by vfs_rmdir() */
944 	if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
945 		err = ovl_check_empty_dir(dentry, &list);
946 		if (err)
947 			goto out;
948 	}
949 
950 	err = ovl_copy_up(dentry->d_parent);
951 	if (err)
952 		goto out;
953 
954 	err = ovl_nlink_start(dentry);
955 	if (err)
956 		goto out;
957 
958 	with_ovl_creds(dentry->d_sb) {
959 		if (!lower_positive)
960 			err = ovl_remove_upper(dentry, is_dir, &list);
961 		else
962 			err = ovl_remove_and_whiteout(dentry, &list);
963 	}
964 	if (!err) {
965 		if (is_dir)
966 			clear_nlink(dentry->d_inode);
967 		else
968 			ovl_drop_nlink(dentry);
969 	}
970 	ovl_nlink_end(dentry);
971 
972 	/*
973 	 * Copy ctime
974 	 *
975 	 * Note: we fail to update ctime if there was no copy-up, only a
976 	 * whiteout
977 	 */
978 	if (ovl_dentry_upper(dentry))
979 		ovl_copyattr(d_inode(dentry));
980 
981 out:
982 	ovl_cache_free(&list);
983 	return err;
984 }
985 
986 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
987 {
988 	return ovl_do_remove(dentry, false);
989 }
990 
991 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
992 {
993 	return ovl_do_remove(dentry, true);
994 }
995 
996 static bool ovl_type_merge_or_lower(struct dentry *dentry)
997 {
998 	enum ovl_path_type type = ovl_path_type(dentry);
999 
1000 	return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
1001 }
1002 
1003 static bool ovl_can_move(struct dentry *dentry)
1004 {
1005 	return ovl_redirect_dir(OVL_FS(dentry->d_sb)) ||
1006 		!d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
1007 }
1008 
1009 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
1010 {
1011 	char *buf, *ret;
1012 	struct dentry *d, *tmp;
1013 	int buflen = ovl_redirect_max + 1;
1014 
1015 	if (!abs_redirect) {
1016 		ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
1017 			       GFP_KERNEL);
1018 		goto out;
1019 	}
1020 
1021 	buf = ret = kmalloc(buflen, GFP_KERNEL);
1022 	if (!buf)
1023 		goto out;
1024 
1025 	buflen--;
1026 	buf[buflen] = '\0';
1027 	for (d = dget(dentry); !IS_ROOT(d);) {
1028 		const char *name;
1029 		int thislen;
1030 
1031 		spin_lock(&d->d_lock);
1032 		name = ovl_dentry_get_redirect(d);
1033 		if (name) {
1034 			thislen = strlen(name);
1035 		} else {
1036 			name = d->d_name.name;
1037 			thislen = d->d_name.len;
1038 		}
1039 
1040 		/* If path is too long, fall back to userspace move */
1041 		if (thislen + (name[0] != '/') > buflen) {
1042 			ret = ERR_PTR(-EXDEV);
1043 			spin_unlock(&d->d_lock);
1044 			goto out_put;
1045 		}
1046 
1047 		buflen -= thislen;
1048 		memcpy(&buf[buflen], name, thislen);
1049 		spin_unlock(&d->d_lock);
1050 		tmp = dget_parent(d);
1051 
1052 		dput(d);
1053 		d = tmp;
1054 
1055 		/* Absolute redirect: finished */
1056 		if (buf[buflen] == '/')
1057 			break;
1058 		buflen--;
1059 		buf[buflen] = '/';
1060 	}
1061 	ret = kstrdup(&buf[buflen], GFP_KERNEL);
1062 out_put:
1063 	dput(d);
1064 	kfree(buf);
1065 out:
1066 	return ret ? ret : ERR_PTR(-ENOMEM);
1067 }
1068 
1069 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1070 {
1071 	struct dentry *lowerdentry;
1072 
1073 	if (!samedir)
1074 		return true;
1075 
1076 	if (d_is_dir(dentry))
1077 		return false;
1078 
1079 	/*
1080 	 * For non-dir hardlinked files, we need absolute redirects
1081 	 * in general as two upper hardlinks could be in different
1082 	 * dirs. We could put a relative redirect now and convert
1083 	 * it to absolute redirect later. But when nlink > 1 and
1084 	 * indexing is on, that means relative redirect needs to be
1085 	 * converted to absolute during copy up of another lower
1086 	 * hardllink as well.
1087 	 *
1088 	 * So without optimizing too much, just check if lower is
1089 	 * a hard link or not. If lower is hard link, put absolute
1090 	 * redirect.
1091 	 */
1092 	lowerdentry = ovl_dentry_lower(dentry);
1093 	return (d_inode(lowerdentry)->i_nlink > 1);
1094 }
1095 
1096 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1097 {
1098 	int err;
1099 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1100 	const char *redirect = ovl_dentry_get_redirect(dentry);
1101 	bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1102 
1103 	if (redirect && (!absolute_redirect || redirect[0] == '/'))
1104 		return 0;
1105 
1106 	redirect = ovl_get_redirect(dentry, absolute_redirect);
1107 	if (IS_ERR(redirect))
1108 		return PTR_ERR(redirect);
1109 
1110 	err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1111 				 OVL_XATTR_REDIRECT,
1112 				 redirect, strlen(redirect), -EXDEV);
1113 	if (!err) {
1114 		spin_lock(&dentry->d_lock);
1115 		ovl_dentry_set_redirect(dentry, redirect);
1116 		spin_unlock(&dentry->d_lock);
1117 	} else {
1118 		kfree(redirect);
1119 		pr_warn_ratelimited("failed to set redirect (%i)\n",
1120 				    err);
1121 		/* Fall back to userspace copy-up */
1122 		err = -EXDEV;
1123 	}
1124 	return err;
1125 }
1126 
1127 struct ovl_renamedata {
1128 	struct renamedata;
1129 	struct dentry *opaquedir;
1130 	bool cleanup_whiteout;
1131 	bool update_nlink;
1132 	bool overwrite;
1133 };
1134 
1135 static int ovl_rename_start(struct ovl_renamedata *ovlrd, struct list_head *list)
1136 {
1137 	struct dentry *old = ovlrd->old_dentry;
1138 	struct dentry *new = ovlrd->new_dentry;
1139 	bool is_dir = d_is_dir(old);
1140 	bool new_is_dir = d_is_dir(new);
1141 	int err;
1142 
1143 	if (ovlrd->flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1144 		return -EINVAL;
1145 
1146 	ovlrd->flags &= ~RENAME_NOREPLACE;
1147 
1148 	/* Don't copy up directory trees */
1149 	err = -EXDEV;
1150 	if (!ovl_can_move(old))
1151 		return err;
1152 	if (!ovlrd->overwrite && !ovl_can_move(new))
1153 		return err;
1154 
1155 	if (ovlrd->overwrite && new_is_dir && !ovl_pure_upper(new)) {
1156 		err = ovl_check_empty_dir(new, list);
1157 		if (err)
1158 			return err;
1159 	}
1160 
1161 	if (ovlrd->overwrite) {
1162 		if (ovl_lower_positive(old)) {
1163 			if (!ovl_dentry_is_whiteout(new)) {
1164 				/* Whiteout source */
1165 				ovlrd->flags |= RENAME_WHITEOUT;
1166 			} else {
1167 				/* Switch whiteouts */
1168 				ovlrd->flags |= RENAME_EXCHANGE;
1169 			}
1170 		} else if (is_dir && ovl_dentry_is_whiteout(new)) {
1171 			ovlrd->flags |= RENAME_EXCHANGE;
1172 			ovlrd->cleanup_whiteout = true;
1173 		}
1174 	}
1175 
1176 	err = ovl_copy_up(old);
1177 	if (err)
1178 		return err;
1179 
1180 	err = ovl_copy_up(new->d_parent);
1181 	if (err)
1182 		return err;
1183 
1184 	if (!ovlrd->overwrite) {
1185 		err = ovl_copy_up(new);
1186 		if (err)
1187 			return err;
1188 	} else if (d_inode(new)) {
1189 		err = ovl_nlink_start(new);
1190 		if (err)
1191 			return err;
1192 
1193 		ovlrd->update_nlink = true;
1194 	}
1195 
1196 	if (!ovlrd->update_nlink) {
1197 		/* ovl_nlink_start() took ovl_want_write() */
1198 		err = ovl_want_write(old);
1199 		if (err)
1200 			return err;
1201 	}
1202 
1203 	return 0;
1204 }
1205 
1206 static int ovl_rename_upper(struct ovl_renamedata *ovlrd, struct list_head *list)
1207 {
1208 	struct dentry *old = ovlrd->old_dentry;
1209 	struct dentry *new = ovlrd->new_dentry;
1210 	struct ovl_fs *ofs = OVL_FS(old->d_sb);
1211 	struct dentry *old_upperdir = ovl_dentry_upper(old->d_parent);
1212 	struct dentry *new_upperdir = ovl_dentry_upper(new->d_parent);
1213 	bool is_dir = d_is_dir(old);
1214 	bool new_is_dir = d_is_dir(new);
1215 	bool samedir = old->d_parent == new->d_parent;
1216 	struct renamedata rd = {};
1217 	struct dentry *de;
1218 	struct dentry *whiteout = NULL;
1219 	bool old_opaque, new_opaque;
1220 	int err;
1221 
1222 	if (!list_empty(list)) {
1223 		de = ovl_clear_empty(new, list);
1224 		if (IS_ERR(de))
1225 			return PTR_ERR(de);
1226 		ovlrd->opaquedir = de;
1227 	}
1228 
1229 	if (!samedir) {
1230 		/*
1231 		 * When moving a merge dir or non-dir with copy up origin into
1232 		 * a new parent, we are marking the new parent dir "impure".
1233 		 * When ovl_iterate() iterates an "impure" upper dir, it will
1234 		 * lookup the origin inodes of the entries to fill d_ino.
1235 		 */
1236 		if (ovl_type_origin(old)) {
1237 			err = ovl_set_impure(new->d_parent, new_upperdir);
1238 			if (err)
1239 				return err;
1240 		}
1241 		if (!ovlrd->overwrite && ovl_type_origin(new)) {
1242 			err = ovl_set_impure(old->d_parent, old_upperdir);
1243 			if (err)
1244 				return err;
1245 		}
1246 	}
1247 
1248 	rd.mnt_idmap = ovl_upper_mnt_idmap(ofs);
1249 	rd.old_parent = old_upperdir;
1250 	rd.new_parent = new_upperdir;
1251 	rd.flags = ovlrd->flags;
1252 
1253 	err = start_renaming(&rd, 0,
1254 			     &QSTR_LEN(old->d_name.name, old->d_name.len),
1255 			     &QSTR_LEN(new->d_name.name, new->d_name.len));
1256 	if (err)
1257 		return err;
1258 
1259 	err = -ESTALE;
1260 	if (!ovl_matches_upper(old, rd.old_dentry))
1261 		goto out_unlock;
1262 
1263 	old_opaque = ovl_dentry_is_opaque(old);
1264 	new_opaque = ovl_dentry_is_opaque(new);
1265 
1266 	err = -ESTALE;
1267 	if (d_inode(new) && ovl_dentry_upper(new)) {
1268 		if (ovlrd->opaquedir) {
1269 			if (rd.new_dentry != ovlrd->opaquedir)
1270 				goto out_unlock;
1271 		} else {
1272 			if (!ovl_matches_upper(new, rd.new_dentry))
1273 				goto out_unlock;
1274 		}
1275 	} else {
1276 		if (!d_is_negative(rd.new_dentry)) {
1277 			if (!new_opaque || !ovl_upper_is_whiteout(ofs, rd.new_dentry))
1278 				goto out_unlock;
1279 		} else {
1280 			if (ovlrd->flags & RENAME_EXCHANGE)
1281 				goto out_unlock;
1282 		}
1283 	}
1284 
1285 	if (rd.old_dentry->d_inode == rd.new_dentry->d_inode)
1286 		goto out_unlock;
1287 
1288 	err = 0;
1289 	if (ovl_type_merge_or_lower(old))
1290 		err = ovl_set_redirect(old, samedir);
1291 	else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1292 		err = ovl_set_opaque_xerr(old, rd.old_dentry, -EXDEV);
1293 	if (err)
1294 		goto out_unlock;
1295 
1296 	if (!ovlrd->overwrite && ovl_type_merge_or_lower(new))
1297 		err = ovl_set_redirect(new, samedir);
1298 	else if (!ovlrd->overwrite && new_is_dir && !new_opaque &&
1299 		 ovl_type_merge(old->d_parent))
1300 		err = ovl_set_opaque_xerr(new, rd.new_dentry, -EXDEV);
1301 	if (err)
1302 		goto out_unlock;
1303 
1304 	err = ovl_do_rename_rd(&rd);
1305 
1306 	if (!err && ovlrd->cleanup_whiteout)
1307 		whiteout = dget(rd.new_dentry);
1308 
1309 out_unlock:
1310 	end_renaming(&rd);
1311 
1312 	if (err)
1313 		return err;
1314 
1315 	if (whiteout) {
1316 		ovl_cleanup(ofs, old_upperdir, whiteout);
1317 		dput(whiteout);
1318 	}
1319 
1320 	if (ovlrd->overwrite && d_inode(new)) {
1321 		if (new_is_dir)
1322 			clear_nlink(d_inode(new));
1323 		else
1324 			ovl_drop_nlink(new);
1325 	}
1326 
1327 	ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1328 			 (!ovlrd->overwrite && ovl_type_origin(new)));
1329 	ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1330 			 (d_inode(new) && ovl_type_origin(new)));
1331 
1332 	/* copy ctime: */
1333 	ovl_copyattr(d_inode(old));
1334 	if (d_inode(new) && ovl_dentry_upper(new))
1335 		ovl_copyattr(d_inode(new));
1336 
1337 	return err;
1338 }
1339 
1340 static void ovl_rename_end(struct ovl_renamedata *ovlrd)
1341 {
1342 	if (ovlrd->update_nlink)
1343 		ovl_nlink_end(ovlrd->new_dentry);
1344 	else
1345 		ovl_drop_write(ovlrd->old_dentry);
1346 }
1347 
1348 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
1349 		      struct dentry *old, struct inode *newdir,
1350 		      struct dentry *new, unsigned int flags)
1351 {
1352 	struct ovl_renamedata ovlrd = {
1353 		.old_parent		= old->d_parent,
1354 		.old_dentry		= old,
1355 		.new_parent		= new->d_parent,
1356 		.new_dentry		= new,
1357 		.flags			= flags,
1358 		.overwrite		= !(flags & RENAME_EXCHANGE),
1359 	};
1360 	LIST_HEAD(list);
1361 	int err;
1362 
1363 	err = ovl_rename_start(&ovlrd, &list);
1364 	if (!err) {
1365 		with_ovl_creds(old->d_sb)
1366 			err = ovl_rename_upper(&ovlrd, &list);
1367 		ovl_rename_end(&ovlrd);
1368 	}
1369 
1370 	dput(ovlrd.opaquedir);
1371 	ovl_cache_free(&list);
1372 	return err;
1373 }
1374 
1375 static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,
1376 			      struct inode *inode, umode_t mode)
1377 {
1378 	struct path realparentpath;
1379 	struct file *realfile;
1380 	struct ovl_file *of;
1381 	struct dentry *newdentry;
1382 	/* It's okay to set O_NOATIME, since the owner will be current fsuid */
1383 	int flags = file->f_flags | OVL_OPEN_FLAGS;
1384 	int err;
1385 
1386 	scoped_class(override_creds_ovl, original_creds, dentry->d_sb) {
1387 		scoped_class(ovl_override_creator_creds, cred, original_creds, dentry, inode, mode) {
1388 			if (IS_ERR(cred))
1389 				return PTR_ERR(cred);
1390 
1391 			ovl_path_upper(dentry->d_parent, &realparentpath);
1392 			realfile = backing_tmpfile_open(file, flags, &realparentpath,
1393 							mode, current_cred());
1394 			err = PTR_ERR_OR_ZERO(realfile);
1395 			pr_debug("tmpfile/open(%pd2, 0%o) = %i\n", realparentpath.dentry, mode, err);
1396 			if (err)
1397 				return err;
1398 
1399 			of = ovl_file_alloc(realfile);
1400 			if (!of) {
1401 				fput(realfile);
1402 				return -ENOMEM;
1403 			}
1404 
1405 			/* ovl_instantiate() consumes the newdentry reference on success */
1406 			newdentry = dget(realfile->f_path.dentry);
1407 			err = ovl_instantiate(dentry, inode, newdentry, false, file);
1408 			if (!err) {
1409 				file->private_data = of;
1410 			} else {
1411 				dput(newdentry);
1412 				ovl_file_free(of);
1413 			}
1414 		}
1415 	}
1416 	return err;
1417 }
1418 
1419 static int ovl_dummy_open(struct inode *inode, struct file *file)
1420 {
1421 	return 0;
1422 }
1423 
1424 static int ovl_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
1425 		       struct file *file, umode_t mode)
1426 {
1427 	int err;
1428 	struct dentry *dentry = file->f_path.dentry;
1429 	struct inode *inode;
1430 
1431 	if (!OVL_FS(dentry->d_sb)->tmpfile)
1432 		return -EOPNOTSUPP;
1433 
1434 	err = ovl_copy_up(dentry->d_parent);
1435 	if (err)
1436 		return err;
1437 
1438 	err = ovl_want_write(dentry);
1439 	if (err)
1440 		return err;
1441 
1442 	err = -ENOMEM;
1443 	inode = ovl_new_inode(dentry->d_sb, mode, 0);
1444 	if (!inode)
1445 		goto drop_write;
1446 
1447 	inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
1448 	err = ovl_create_tmpfile(file, dentry, inode, inode->i_mode);
1449 	if (err)
1450 		goto put_inode;
1451 
1452 	/*
1453 	 * Check if the preallocated inode was actually used.  Having something
1454 	 * else assigned to the dentry shouldn't happen as that would indicate
1455 	 * that the backing tmpfile "leaked" out of overlayfs.
1456 	 */
1457 	err = -EIO;
1458 	if (WARN_ON(inode != d_inode(dentry)))
1459 		goto put_realfile;
1460 
1461 	/* inode reference was transferred to dentry */
1462 	inode = NULL;
1463 	err = finish_open(file, dentry, ovl_dummy_open);
1464 put_realfile:
1465 	/* Without FMODE_OPENED ->release() won't be called on @file */
1466 	if (!(file->f_mode & FMODE_OPENED))
1467 		ovl_file_free(file->private_data);
1468 put_inode:
1469 	iput(inode);
1470 drop_write:
1471 	ovl_drop_write(dentry);
1472 	return err;
1473 }
1474 
1475 const struct inode_operations ovl_dir_inode_operations = {
1476 	.lookup		= ovl_lookup,
1477 	.mkdir		= ovl_mkdir,
1478 	.symlink	= ovl_symlink,
1479 	.unlink		= ovl_unlink,
1480 	.rmdir		= ovl_rmdir,
1481 	.rename		= ovl_rename,
1482 	.link		= ovl_link,
1483 	.setattr	= ovl_setattr,
1484 	.create		= ovl_create,
1485 	.mknod		= ovl_mknod,
1486 	.permission	= ovl_permission,
1487 	.getattr	= ovl_getattr,
1488 	.listxattr	= ovl_listxattr,
1489 	.get_inode_acl	= ovl_get_inode_acl,
1490 	.get_acl	= ovl_get_acl,
1491 	.set_acl	= ovl_set_acl,
1492 	.update_time	= ovl_update_time,
1493 	.fileattr_get	= ovl_fileattr_get,
1494 	.fileattr_set	= ovl_fileattr_set,
1495 	.tmpfile	= ovl_tmpfile,
1496 };
1497