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