printf: Remove unused 'bprintf'
[drm/drm-misc.git] / fs / overlayfs / dir.c
blob08e683917d121b1fe8f0f0b4d4ba4f0f3c72f47d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Copyright (C) 2011 Novell Inc.
5 */
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"
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");
25 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
27 int ovl_cleanup(struct ovl_fs *ofs, struct inode *wdir, struct dentry *wdentry)
29 int err;
31 dget(wdentry);
32 if (d_is_dir(wdentry))
33 err = ovl_do_rmdir(ofs, wdir, wdentry);
34 else
35 err = ovl_do_unlink(ofs, wdir, wdentry);
36 dput(wdentry);
38 if (err) {
39 pr_err("cleanup of '%pd2' failed (%i)\n",
40 wdentry, err);
43 return err;
46 struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir)
48 struct dentry *temp;
49 char name[20];
50 static atomic_t temp_id = ATOMIC_INIT(0);
52 /* counter is allowed to wrap, since temp dentries are ephemeral */
53 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
55 temp = ovl_lookup_upper(ofs, name, workdir, strlen(name));
56 if (!IS_ERR(temp) && temp->d_inode) {
57 pr_err("workdir/%s already exists\n", name);
58 dput(temp);
59 temp = ERR_PTR(-EIO);
62 return temp;
65 /* caller holds i_mutex on workdir */
66 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
68 int err;
69 struct dentry *whiteout;
70 struct dentry *workdir = ofs->workdir;
71 struct inode *wdir = workdir->d_inode;
73 if (!ofs->whiteout) {
74 whiteout = ovl_lookup_temp(ofs, workdir);
75 if (IS_ERR(whiteout))
76 goto out;
78 err = ovl_do_whiteout(ofs, wdir, whiteout);
79 if (err) {
80 dput(whiteout);
81 whiteout = ERR_PTR(err);
82 goto out;
84 ofs->whiteout = whiteout;
87 if (!ofs->no_shared_whiteout) {
88 whiteout = ovl_lookup_temp(ofs, workdir);
89 if (IS_ERR(whiteout))
90 goto out;
92 err = ovl_do_link(ofs, ofs->whiteout, wdir, whiteout);
93 if (!err)
94 goto out;
96 if (err != -EMLINK) {
97 pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
98 ofs->whiteout->d_inode->i_nlink, err);
99 ofs->no_shared_whiteout = true;
101 dput(whiteout);
103 whiteout = ofs->whiteout;
104 ofs->whiteout = NULL;
105 out:
106 return whiteout;
109 /* Caller must hold i_mutex on both workdir and dir */
110 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
111 struct dentry *dentry)
113 struct inode *wdir = ofs->workdir->d_inode;
114 struct dentry *whiteout;
115 int err;
116 int flags = 0;
118 whiteout = ovl_whiteout(ofs);
119 err = PTR_ERR(whiteout);
120 if (IS_ERR(whiteout))
121 return err;
123 if (d_is_dir(dentry))
124 flags = RENAME_EXCHANGE;
126 err = ovl_do_rename(ofs, wdir, whiteout, dir, dentry, flags);
127 if (err)
128 goto kill_whiteout;
129 if (flags)
130 ovl_cleanup(ofs, wdir, dentry);
132 out:
133 dput(whiteout);
134 return err;
136 kill_whiteout:
137 ovl_cleanup(ofs, wdir, whiteout);
138 goto out;
141 int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
142 struct dentry **newdentry, umode_t mode)
144 int err;
145 struct dentry *d, *dentry = *newdentry;
147 err = ovl_do_mkdir(ofs, dir, dentry, mode);
148 if (err)
149 return err;
151 if (likely(!d_unhashed(dentry)))
152 return 0;
155 * vfs_mkdir() may succeed and leave the dentry passed
156 * to it unhashed and negative. If that happens, try to
157 * lookup a new hashed and positive dentry.
159 d = ovl_lookup_upper(ofs, dentry->d_name.name, dentry->d_parent,
160 dentry->d_name.len);
161 if (IS_ERR(d)) {
162 pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
163 dentry, err);
164 return PTR_ERR(d);
166 dput(dentry);
167 *newdentry = d;
169 return 0;
172 struct dentry *ovl_create_real(struct ovl_fs *ofs, struct inode *dir,
173 struct dentry *newdentry, struct ovl_cattr *attr)
175 int err;
177 if (IS_ERR(newdentry))
178 return newdentry;
180 err = -ESTALE;
181 if (newdentry->d_inode)
182 goto out;
184 if (attr->hardlink) {
185 err = ovl_do_link(ofs, attr->hardlink, dir, newdentry);
186 } else {
187 switch (attr->mode & S_IFMT) {
188 case S_IFREG:
189 err = ovl_do_create(ofs, dir, newdentry, attr->mode);
190 break;
192 case S_IFDIR:
193 /* mkdir is special... */
194 err = ovl_mkdir_real(ofs, dir, &newdentry, attr->mode);
195 break;
197 case S_IFCHR:
198 case S_IFBLK:
199 case S_IFIFO:
200 case S_IFSOCK:
201 err = ovl_do_mknod(ofs, dir, newdentry, attr->mode,
202 attr->rdev);
203 break;
205 case S_IFLNK:
206 err = ovl_do_symlink(ofs, dir, newdentry, attr->link);
207 break;
209 default:
210 err = -EPERM;
213 if (!err && WARN_ON(!newdentry->d_inode)) {
215 * Not quite sure if non-instantiated dentry is legal or not.
216 * VFS doesn't seem to care so check and warn here.
218 err = -EIO;
220 out:
221 if (err) {
222 dput(newdentry);
223 return ERR_PTR(err);
225 return newdentry;
228 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
229 struct ovl_cattr *attr)
231 return ovl_create_real(ofs, d_inode(workdir),
232 ovl_lookup_temp(ofs, workdir), attr);
235 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
236 int xerr)
238 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
239 int err;
241 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
242 if (!err)
243 ovl_dentry_set_opaque(dentry);
245 return err;
248 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
251 * Fail with -EIO when trying to create opaque dir and upper doesn't
252 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
253 * return a specific error for noxattr case.
255 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
259 * Common operations required to be done after creation of file on upper.
260 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
261 * may not use to instantiate the new dentry.
263 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
264 struct dentry *newdentry, bool hardlink, struct file *tmpfile)
266 struct ovl_inode_params oip = {
267 .upperdentry = newdentry,
268 .newinode = inode,
271 ovl_dentry_set_upper_alias(dentry);
272 ovl_dentry_init_reval(dentry, newdentry, NULL);
274 if (!hardlink) {
276 * ovl_obtain_alias() can be called after ovl_create_real()
277 * and before we get here, so we may get an inode from cache
278 * with the same real upperdentry that is not the inode we
279 * pre-allocated. In this case we will use the cached inode
280 * to instantiate the new dentry.
282 * XXX: if we ever use ovl_obtain_alias() to decode directory
283 * file handles, need to use ovl_get_inode_locked() and
284 * d_instantiate_new() here to prevent from creating two
285 * hashed directory inode aliases.
287 inode = ovl_get_inode(dentry->d_sb, &oip);
288 if (IS_ERR(inode))
289 return PTR_ERR(inode);
290 if (inode == oip.newinode)
291 ovl_set_flag(OVL_UPPERDATA, inode);
292 } else {
293 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
294 dput(newdentry);
295 inc_nlink(inode);
298 if (tmpfile)
299 d_mark_tmpfile(tmpfile, inode);
301 d_instantiate(dentry, inode);
302 if (inode != oip.newinode) {
303 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
304 dentry);
307 /* Force lookup of new upper hardlink to find its lower */
308 if (hardlink)
309 d_drop(dentry);
311 return 0;
314 static bool ovl_type_merge(struct dentry *dentry)
316 return OVL_TYPE_MERGE(ovl_path_type(dentry));
319 static bool ovl_type_origin(struct dentry *dentry)
321 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
324 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
325 struct ovl_cattr *attr)
327 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
328 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
329 struct inode *udir = upperdir->d_inode;
330 struct dentry *newdentry;
331 int err;
333 inode_lock_nested(udir, I_MUTEX_PARENT);
334 newdentry = ovl_create_real(ofs, udir,
335 ovl_lookup_upper(ofs, dentry->d_name.name,
336 upperdir, dentry->d_name.len),
337 attr);
338 err = PTR_ERR(newdentry);
339 if (IS_ERR(newdentry))
340 goto out_unlock;
342 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
343 !ovl_allow_offline_changes(ofs)) {
344 /* Setting opaque here is just an optimization, allow to fail */
345 ovl_set_opaque(dentry, newdentry);
348 ovl_dir_modified(dentry->d_parent, false);
349 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink, NULL);
350 if (err)
351 goto out_cleanup;
352 out_unlock:
353 inode_unlock(udir);
354 return err;
356 out_cleanup:
357 ovl_cleanup(ofs, udir, newdentry);
358 dput(newdentry);
359 goto out_unlock;
362 static struct dentry *ovl_clear_empty(struct dentry *dentry,
363 struct list_head *list)
365 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
366 struct dentry *workdir = ovl_workdir(dentry);
367 struct inode *wdir = workdir->d_inode;
368 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
369 struct inode *udir = upperdir->d_inode;
370 struct path upperpath;
371 struct dentry *upper;
372 struct dentry *opaquedir;
373 struct kstat stat;
374 int err;
376 if (WARN_ON(!workdir))
377 return ERR_PTR(-EROFS);
379 err = ovl_lock_rename_workdir(workdir, upperdir);
380 if (err)
381 goto out;
383 ovl_path_upper(dentry, &upperpath);
384 err = vfs_getattr(&upperpath, &stat,
385 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
386 if (err)
387 goto out_unlock;
389 err = -ESTALE;
390 if (!S_ISDIR(stat.mode))
391 goto out_unlock;
392 upper = upperpath.dentry;
393 if (upper->d_parent->d_inode != udir)
394 goto out_unlock;
396 opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
397 err = PTR_ERR(opaquedir);
398 if (IS_ERR(opaquedir))
399 goto out_unlock;
401 err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
402 if (err)
403 goto out_cleanup;
405 err = ovl_set_opaque(dentry, opaquedir);
406 if (err)
407 goto out_cleanup;
409 inode_lock(opaquedir->d_inode);
410 err = ovl_set_attr(ofs, opaquedir, &stat);
411 inode_unlock(opaquedir->d_inode);
412 if (err)
413 goto out_cleanup;
415 err = ovl_do_rename(ofs, wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
416 if (err)
417 goto out_cleanup;
419 ovl_cleanup_whiteouts(ofs, upper, list);
420 ovl_cleanup(ofs, wdir, upper);
421 unlock_rename(workdir, upperdir);
423 /* dentry's upper doesn't match now, get rid of it */
424 d_drop(dentry);
426 return opaquedir;
428 out_cleanup:
429 ovl_cleanup(ofs, wdir, opaquedir);
430 dput(opaquedir);
431 out_unlock:
432 unlock_rename(workdir, upperdir);
433 out:
434 return ERR_PTR(err);
437 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
438 const char *acl_name, struct posix_acl *acl)
440 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
441 return 0;
443 return ovl_do_set_acl(ofs, upperdentry, acl_name, acl);
446 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
447 struct ovl_cattr *cattr)
449 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
450 struct dentry *workdir = ovl_workdir(dentry);
451 struct inode *wdir = workdir->d_inode;
452 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
453 struct inode *udir = upperdir->d_inode;
454 struct dentry *upper;
455 struct dentry *newdentry;
456 int err;
457 struct posix_acl *acl, *default_acl;
458 bool hardlink = !!cattr->hardlink;
460 if (WARN_ON(!workdir))
461 return -EROFS;
463 if (!hardlink) {
464 err = posix_acl_create(dentry->d_parent->d_inode,
465 &cattr->mode, &default_acl, &acl);
466 if (err)
467 return err;
470 err = ovl_lock_rename_workdir(workdir, upperdir);
471 if (err)
472 goto out;
474 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
475 dentry->d_name.len);
476 err = PTR_ERR(upper);
477 if (IS_ERR(upper))
478 goto out_unlock;
480 err = -ESTALE;
481 if (d_is_negative(upper) || !ovl_upper_is_whiteout(ofs, upper))
482 goto out_dput;
484 newdentry = ovl_create_temp(ofs, workdir, cattr);
485 err = PTR_ERR(newdentry);
486 if (IS_ERR(newdentry))
487 goto out_dput;
490 * mode could have been mutilated due to umask (e.g. sgid directory)
492 if (!hardlink &&
493 !S_ISLNK(cattr->mode) &&
494 newdentry->d_inode->i_mode != cattr->mode) {
495 struct iattr attr = {
496 .ia_valid = ATTR_MODE,
497 .ia_mode = cattr->mode,
499 inode_lock(newdentry->d_inode);
500 err = ovl_do_notify_change(ofs, newdentry, &attr);
501 inode_unlock(newdentry->d_inode);
502 if (err)
503 goto out_cleanup;
505 if (!hardlink) {
506 err = ovl_set_upper_acl(ofs, newdentry,
507 XATTR_NAME_POSIX_ACL_ACCESS, acl);
508 if (err)
509 goto out_cleanup;
511 err = ovl_set_upper_acl(ofs, newdentry,
512 XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
513 if (err)
514 goto out_cleanup;
517 if (!hardlink && S_ISDIR(cattr->mode)) {
518 err = ovl_set_opaque(dentry, newdentry);
519 if (err)
520 goto out_cleanup;
522 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper,
523 RENAME_EXCHANGE);
524 if (err)
525 goto out_cleanup;
527 ovl_cleanup(ofs, wdir, upper);
528 } else {
529 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 0);
530 if (err)
531 goto out_cleanup;
533 ovl_dir_modified(dentry->d_parent, false);
534 err = ovl_instantiate(dentry, inode, newdentry, hardlink, NULL);
535 if (err) {
536 ovl_cleanup(ofs, udir, newdentry);
537 dput(newdentry);
539 out_dput:
540 dput(upper);
541 out_unlock:
542 unlock_rename(workdir, upperdir);
543 out:
544 if (!hardlink) {
545 posix_acl_release(acl);
546 posix_acl_release(default_acl);
548 return err;
550 out_cleanup:
551 ovl_cleanup(ofs, wdir, newdentry);
552 dput(newdentry);
553 goto out_dput;
556 static const struct cred *ovl_setup_cred_for_create(struct dentry *dentry,
557 struct inode *inode,
558 umode_t mode,
559 const struct cred *old_cred)
561 int err;
562 struct cred *override_cred;
564 override_cred = prepare_creds();
565 if (!override_cred)
566 return ERR_PTR(-ENOMEM);
568 override_cred->fsuid = inode->i_uid;
569 override_cred->fsgid = inode->i_gid;
570 err = security_dentry_create_files_as(dentry, mode, &dentry->d_name,
571 old_cred, override_cred);
572 if (err) {
573 put_cred(override_cred);
574 return ERR_PTR(err);
578 * Caller is going to match this with revert_creds_light() and drop
579 * referenec on the returned creds.
580 * We must be called with creator creds already, otherwise we risk
581 * leaking creds.
583 old_cred = override_creds_light(override_cred);
584 WARN_ON_ONCE(old_cred != ovl_creds(dentry->d_sb));
586 return override_cred;
589 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
590 struct ovl_cattr *attr, bool origin)
592 int err;
593 const struct cred *old_cred, *new_cred = NULL;
594 struct dentry *parent = dentry->d_parent;
596 old_cred = ovl_override_creds(dentry->d_sb);
599 * When linking a file with copy up origin into a new parent, mark the
600 * new parent dir "impure".
602 if (origin) {
603 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
604 if (err)
605 goto out_revert_creds;
608 if (!attr->hardlink) {
610 * In the creation cases(create, mkdir, mknod, symlink),
611 * ovl should transfer current's fs{u,g}id to underlying
612 * fs. Because underlying fs want to initialize its new
613 * inode owner using current's fs{u,g}id. And in this
614 * case, the @inode is a new inode that is initialized
615 * in inode_init_owner() to current's fs{u,g}id. So use
616 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
618 * But in the other hardlink case, ovl_link() does not
619 * create a new inode, so just use the ovl mounter's
620 * fs{u,g}id.
622 new_cred = ovl_setup_cred_for_create(dentry, inode, attr->mode,
623 old_cred);
624 err = PTR_ERR(new_cred);
625 if (IS_ERR(new_cred)) {
626 new_cred = NULL;
627 goto out_revert_creds;
631 if (!ovl_dentry_is_whiteout(dentry))
632 err = ovl_create_upper(dentry, inode, attr);
633 else
634 err = ovl_create_over_whiteout(dentry, inode, attr);
636 out_revert_creds:
637 ovl_revert_creds(old_cred);
638 put_cred(new_cred);
639 return err;
642 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
643 const char *link)
645 int err;
646 struct inode *inode;
647 struct ovl_cattr attr = {
648 .rdev = rdev,
649 .link = link,
652 err = ovl_copy_up(dentry->d_parent);
653 if (err)
654 return err;
656 err = ovl_want_write(dentry);
657 if (err)
658 goto out;
660 /* Preallocate inode to be used by ovl_get_inode() */
661 err = -ENOMEM;
662 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
663 if (!inode)
664 goto out_drop_write;
666 spin_lock(&inode->i_lock);
667 inode->i_state |= I_CREATING;
668 spin_unlock(&inode->i_lock);
670 inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode);
671 attr.mode = inode->i_mode;
673 err = ovl_create_or_link(dentry, inode, &attr, false);
674 /* Did we end up using the preallocated inode? */
675 if (inode != d_inode(dentry))
676 iput(inode);
678 out_drop_write:
679 ovl_drop_write(dentry);
680 out:
681 return err;
684 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir,
685 struct dentry *dentry, umode_t mode, bool excl)
687 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
690 static int ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir,
691 struct dentry *dentry, umode_t mode)
693 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
696 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
697 struct dentry *dentry, umode_t mode, dev_t rdev)
699 /* Don't allow creation of "whiteout" on overlay */
700 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
701 return -EPERM;
703 return ovl_create_object(dentry, mode, rdev, NULL);
706 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,
707 struct dentry *dentry, const char *link)
709 return ovl_create_object(dentry, S_IFLNK, 0, link);
712 static int ovl_set_link_redirect(struct dentry *dentry)
714 const struct cred *old_cred;
715 int err;
717 old_cred = ovl_override_creds(dentry->d_sb);
718 err = ovl_set_redirect(dentry, false);
719 ovl_revert_creds(old_cred);
721 return err;
724 static int ovl_link(struct dentry *old, struct inode *newdir,
725 struct dentry *new)
727 int err;
728 struct inode *inode;
730 err = ovl_copy_up(old);
731 if (err)
732 goto out;
734 err = ovl_copy_up(new->d_parent);
735 if (err)
736 goto out;
738 err = ovl_nlink_start(old);
739 if (err)
740 goto out;
742 if (ovl_is_metacopy_dentry(old)) {
743 err = ovl_set_link_redirect(old);
744 if (err)
745 goto out_nlink_end;
748 inode = d_inode(old);
749 ihold(inode);
751 err = ovl_create_or_link(new, inode,
752 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
753 ovl_type_origin(old));
754 if (err)
755 iput(inode);
757 out_nlink_end:
758 ovl_nlink_end(old);
759 out:
760 return err;
763 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
765 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
768 static int ovl_remove_and_whiteout(struct dentry *dentry,
769 struct list_head *list)
771 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
772 struct dentry *workdir = ovl_workdir(dentry);
773 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
774 struct dentry *upper;
775 struct dentry *opaquedir = NULL;
776 int err;
778 if (WARN_ON(!workdir))
779 return -EROFS;
781 if (!list_empty(list)) {
782 opaquedir = ovl_clear_empty(dentry, list);
783 err = PTR_ERR(opaquedir);
784 if (IS_ERR(opaquedir))
785 goto out;
788 err = ovl_lock_rename_workdir(workdir, upperdir);
789 if (err)
790 goto out_dput;
792 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
793 dentry->d_name.len);
794 err = PTR_ERR(upper);
795 if (IS_ERR(upper))
796 goto out_unlock;
798 err = -ESTALE;
799 if ((opaquedir && upper != opaquedir) ||
800 (!opaquedir && ovl_dentry_upper(dentry) &&
801 !ovl_matches_upper(dentry, upper))) {
802 goto out_dput_upper;
805 err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
806 if (err)
807 goto out_d_drop;
809 ovl_dir_modified(dentry->d_parent, true);
810 out_d_drop:
811 d_drop(dentry);
812 out_dput_upper:
813 dput(upper);
814 out_unlock:
815 unlock_rename(workdir, upperdir);
816 out_dput:
817 dput(opaquedir);
818 out:
819 return err;
822 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
823 struct list_head *list)
825 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
826 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
827 struct inode *dir = upperdir->d_inode;
828 struct dentry *upper;
829 struct dentry *opaquedir = NULL;
830 int err;
832 if (!list_empty(list)) {
833 opaquedir = ovl_clear_empty(dentry, list);
834 err = PTR_ERR(opaquedir);
835 if (IS_ERR(opaquedir))
836 goto out;
839 inode_lock_nested(dir, I_MUTEX_PARENT);
840 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
841 dentry->d_name.len);
842 err = PTR_ERR(upper);
843 if (IS_ERR(upper))
844 goto out_unlock;
846 err = -ESTALE;
847 if ((opaquedir && upper != opaquedir) ||
848 (!opaquedir && !ovl_matches_upper(dentry, upper)))
849 goto out_dput_upper;
851 if (is_dir)
852 err = ovl_do_rmdir(ofs, dir, upper);
853 else
854 err = ovl_do_unlink(ofs, dir, upper);
855 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
858 * Keeping this dentry hashed would mean having to release
859 * upperpath/lowerpath, which could only be done if we are the
860 * sole user of this dentry. Too tricky... Just unhash for
861 * now.
863 if (!err)
864 d_drop(dentry);
865 out_dput_upper:
866 dput(upper);
867 out_unlock:
868 inode_unlock(dir);
869 dput(opaquedir);
870 out:
871 return err;
874 static bool ovl_pure_upper(struct dentry *dentry)
876 return !ovl_dentry_lower(dentry) &&
877 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
880 static void ovl_drop_nlink(struct dentry *dentry)
882 struct inode *inode = d_inode(dentry);
883 struct dentry *alias;
885 /* Try to find another, hashed alias */
886 spin_lock(&inode->i_lock);
887 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
888 if (alias != dentry && !d_unhashed(alias))
889 break;
891 spin_unlock(&inode->i_lock);
894 * Changes to underlying layers may cause i_nlink to lose sync with
895 * reality. In this case prevent the link count from going to zero
896 * prematurely.
898 if (inode->i_nlink > !!alias)
899 drop_nlink(inode);
902 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
904 int err;
905 const struct cred *old_cred;
906 bool lower_positive = ovl_lower_positive(dentry);
907 LIST_HEAD(list);
909 /* No need to clean pure upper removed by vfs_rmdir() */
910 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
911 err = ovl_check_empty_dir(dentry, &list);
912 if (err)
913 goto out;
916 err = ovl_copy_up(dentry->d_parent);
917 if (err)
918 goto out;
920 err = ovl_nlink_start(dentry);
921 if (err)
922 goto out;
924 old_cred = ovl_override_creds(dentry->d_sb);
925 if (!lower_positive)
926 err = ovl_remove_upper(dentry, is_dir, &list);
927 else
928 err = ovl_remove_and_whiteout(dentry, &list);
929 ovl_revert_creds(old_cred);
930 if (!err) {
931 if (is_dir)
932 clear_nlink(dentry->d_inode);
933 else
934 ovl_drop_nlink(dentry);
936 ovl_nlink_end(dentry);
939 * Copy ctime
941 * Note: we fail to update ctime if there was no copy-up, only a
942 * whiteout
944 if (ovl_dentry_upper(dentry))
945 ovl_copyattr(d_inode(dentry));
947 out:
948 ovl_cache_free(&list);
949 return err;
952 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
954 return ovl_do_remove(dentry, false);
957 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
959 return ovl_do_remove(dentry, true);
962 static bool ovl_type_merge_or_lower(struct dentry *dentry)
964 enum ovl_path_type type = ovl_path_type(dentry);
966 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
969 static bool ovl_can_move(struct dentry *dentry)
971 return ovl_redirect_dir(OVL_FS(dentry->d_sb)) ||
972 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
975 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
977 char *buf, *ret;
978 struct dentry *d, *tmp;
979 int buflen = ovl_redirect_max + 1;
981 if (!abs_redirect) {
982 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
983 GFP_KERNEL);
984 goto out;
987 buf = ret = kmalloc(buflen, GFP_KERNEL);
988 if (!buf)
989 goto out;
991 buflen--;
992 buf[buflen] = '\0';
993 for (d = dget(dentry); !IS_ROOT(d);) {
994 const char *name;
995 int thislen;
997 spin_lock(&d->d_lock);
998 name = ovl_dentry_get_redirect(d);
999 if (name) {
1000 thislen = strlen(name);
1001 } else {
1002 name = d->d_name.name;
1003 thislen = d->d_name.len;
1006 /* If path is too long, fall back to userspace move */
1007 if (thislen + (name[0] != '/') > buflen) {
1008 ret = ERR_PTR(-EXDEV);
1009 spin_unlock(&d->d_lock);
1010 goto out_put;
1013 buflen -= thislen;
1014 memcpy(&buf[buflen], name, thislen);
1015 spin_unlock(&d->d_lock);
1016 tmp = dget_parent(d);
1018 dput(d);
1019 d = tmp;
1021 /* Absolute redirect: finished */
1022 if (buf[buflen] == '/')
1023 break;
1024 buflen--;
1025 buf[buflen] = '/';
1027 ret = kstrdup(&buf[buflen], GFP_KERNEL);
1028 out_put:
1029 dput(d);
1030 kfree(buf);
1031 out:
1032 return ret ? ret : ERR_PTR(-ENOMEM);
1035 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1037 struct dentry *lowerdentry;
1039 if (!samedir)
1040 return true;
1042 if (d_is_dir(dentry))
1043 return false;
1046 * For non-dir hardlinked files, we need absolute redirects
1047 * in general as two upper hardlinks could be in different
1048 * dirs. We could put a relative redirect now and convert
1049 * it to absolute redirect later. But when nlink > 1 and
1050 * indexing is on, that means relative redirect needs to be
1051 * converted to absolute during copy up of another lower
1052 * hardllink as well.
1054 * So without optimizing too much, just check if lower is
1055 * a hard link or not. If lower is hard link, put absolute
1056 * redirect.
1058 lowerdentry = ovl_dentry_lower(dentry);
1059 return (d_inode(lowerdentry)->i_nlink > 1);
1062 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1064 int err;
1065 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1066 const char *redirect = ovl_dentry_get_redirect(dentry);
1067 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1069 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1070 return 0;
1072 redirect = ovl_get_redirect(dentry, absolute_redirect);
1073 if (IS_ERR(redirect))
1074 return PTR_ERR(redirect);
1076 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1077 OVL_XATTR_REDIRECT,
1078 redirect, strlen(redirect), -EXDEV);
1079 if (!err) {
1080 spin_lock(&dentry->d_lock);
1081 ovl_dentry_set_redirect(dentry, redirect);
1082 spin_unlock(&dentry->d_lock);
1083 } else {
1084 kfree(redirect);
1085 pr_warn_ratelimited("failed to set redirect (%i)\n",
1086 err);
1087 /* Fall back to userspace copy-up */
1088 err = -EXDEV;
1090 return err;
1093 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
1094 struct dentry *old, struct inode *newdir,
1095 struct dentry *new, unsigned int flags)
1097 int err;
1098 struct dentry *old_upperdir;
1099 struct dentry *new_upperdir;
1100 struct dentry *olddentry;
1101 struct dentry *newdentry;
1102 struct dentry *trap;
1103 bool old_opaque;
1104 bool new_opaque;
1105 bool cleanup_whiteout = false;
1106 bool update_nlink = false;
1107 bool overwrite = !(flags & RENAME_EXCHANGE);
1108 bool is_dir = d_is_dir(old);
1109 bool new_is_dir = d_is_dir(new);
1110 bool samedir = olddir == newdir;
1111 struct dentry *opaquedir = NULL;
1112 const struct cred *old_cred = NULL;
1113 struct ovl_fs *ofs = OVL_FS(old->d_sb);
1114 LIST_HEAD(list);
1116 err = -EINVAL;
1117 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1118 goto out;
1120 flags &= ~RENAME_NOREPLACE;
1122 /* Don't copy up directory trees */
1123 err = -EXDEV;
1124 if (!ovl_can_move(old))
1125 goto out;
1126 if (!overwrite && !ovl_can_move(new))
1127 goto out;
1129 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1130 err = ovl_check_empty_dir(new, &list);
1131 if (err)
1132 goto out;
1135 if (overwrite) {
1136 if (ovl_lower_positive(old)) {
1137 if (!ovl_dentry_is_whiteout(new)) {
1138 /* Whiteout source */
1139 flags |= RENAME_WHITEOUT;
1140 } else {
1141 /* Switch whiteouts */
1142 flags |= RENAME_EXCHANGE;
1144 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1145 flags |= RENAME_EXCHANGE;
1146 cleanup_whiteout = true;
1150 err = ovl_copy_up(old);
1151 if (err)
1152 goto out;
1154 err = ovl_copy_up(new->d_parent);
1155 if (err)
1156 goto out;
1157 if (!overwrite) {
1158 err = ovl_copy_up(new);
1159 if (err)
1160 goto out;
1161 } else if (d_inode(new)) {
1162 err = ovl_nlink_start(new);
1163 if (err)
1164 goto out;
1166 update_nlink = true;
1169 if (!update_nlink) {
1170 /* ovl_nlink_start() took ovl_want_write() */
1171 err = ovl_want_write(old);
1172 if (err)
1173 goto out;
1176 old_cred = ovl_override_creds(old->d_sb);
1178 if (!list_empty(&list)) {
1179 opaquedir = ovl_clear_empty(new, &list);
1180 err = PTR_ERR(opaquedir);
1181 if (IS_ERR(opaquedir)) {
1182 opaquedir = NULL;
1183 goto out_revert_creds;
1187 old_upperdir = ovl_dentry_upper(old->d_parent);
1188 new_upperdir = ovl_dentry_upper(new->d_parent);
1190 if (!samedir) {
1192 * When moving a merge dir or non-dir with copy up origin into
1193 * a new parent, we are marking the new parent dir "impure".
1194 * When ovl_iterate() iterates an "impure" upper dir, it will
1195 * lookup the origin inodes of the entries to fill d_ino.
1197 if (ovl_type_origin(old)) {
1198 err = ovl_set_impure(new->d_parent, new_upperdir);
1199 if (err)
1200 goto out_revert_creds;
1202 if (!overwrite && ovl_type_origin(new)) {
1203 err = ovl_set_impure(old->d_parent, old_upperdir);
1204 if (err)
1205 goto out_revert_creds;
1209 trap = lock_rename(new_upperdir, old_upperdir);
1210 if (IS_ERR(trap)) {
1211 err = PTR_ERR(trap);
1212 goto out_revert_creds;
1215 olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir,
1216 old->d_name.len);
1217 err = PTR_ERR(olddentry);
1218 if (IS_ERR(olddentry))
1219 goto out_unlock;
1221 err = -ESTALE;
1222 if (!ovl_matches_upper(old, olddentry))
1223 goto out_dput_old;
1225 newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir,
1226 new->d_name.len);
1227 err = PTR_ERR(newdentry);
1228 if (IS_ERR(newdentry))
1229 goto out_dput_old;
1231 old_opaque = ovl_dentry_is_opaque(old);
1232 new_opaque = ovl_dentry_is_opaque(new);
1234 err = -ESTALE;
1235 if (d_inode(new) && ovl_dentry_upper(new)) {
1236 if (opaquedir) {
1237 if (newdentry != opaquedir)
1238 goto out_dput;
1239 } else {
1240 if (!ovl_matches_upper(new, newdentry))
1241 goto out_dput;
1243 } else {
1244 if (!d_is_negative(newdentry)) {
1245 if (!new_opaque || !ovl_upper_is_whiteout(ofs, newdentry))
1246 goto out_dput;
1247 } else {
1248 if (flags & RENAME_EXCHANGE)
1249 goto out_dput;
1253 if (olddentry == trap)
1254 goto out_dput;
1255 if (newdentry == trap)
1256 goto out_dput;
1258 if (olddentry->d_inode == newdentry->d_inode)
1259 goto out_dput;
1261 err = 0;
1262 if (ovl_type_merge_or_lower(old))
1263 err = ovl_set_redirect(old, samedir);
1264 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1265 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1266 if (err)
1267 goto out_dput;
1269 if (!overwrite && ovl_type_merge_or_lower(new))
1270 err = ovl_set_redirect(new, samedir);
1271 else if (!overwrite && new_is_dir && !new_opaque &&
1272 ovl_type_merge(old->d_parent))
1273 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1274 if (err)
1275 goto out_dput;
1277 err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry,
1278 new_upperdir->d_inode, newdentry, flags);
1279 if (err)
1280 goto out_dput;
1282 if (cleanup_whiteout)
1283 ovl_cleanup(ofs, old_upperdir->d_inode, newdentry);
1285 if (overwrite && d_inode(new)) {
1286 if (new_is_dir)
1287 clear_nlink(d_inode(new));
1288 else
1289 ovl_drop_nlink(new);
1292 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1293 (!overwrite && ovl_type_origin(new)));
1294 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1295 (d_inode(new) && ovl_type_origin(new)));
1297 /* copy ctime: */
1298 ovl_copyattr(d_inode(old));
1299 if (d_inode(new) && ovl_dentry_upper(new))
1300 ovl_copyattr(d_inode(new));
1302 out_dput:
1303 dput(newdentry);
1304 out_dput_old:
1305 dput(olddentry);
1306 out_unlock:
1307 unlock_rename(new_upperdir, old_upperdir);
1308 out_revert_creds:
1309 ovl_revert_creds(old_cred);
1310 if (update_nlink)
1311 ovl_nlink_end(new);
1312 else
1313 ovl_drop_write(old);
1314 out:
1315 dput(opaquedir);
1316 ovl_cache_free(&list);
1317 return err;
1320 static int ovl_create_tmpfile(struct file *file, struct dentry *dentry,
1321 struct inode *inode, umode_t mode)
1323 const struct cred *old_cred, *new_cred = NULL;
1324 struct path realparentpath;
1325 struct file *realfile;
1326 struct ovl_file *of;
1327 struct dentry *newdentry;
1328 /* It's okay to set O_NOATIME, since the owner will be current fsuid */
1329 int flags = file->f_flags | OVL_OPEN_FLAGS;
1330 int err;
1332 old_cred = ovl_override_creds(dentry->d_sb);
1333 new_cred = ovl_setup_cred_for_create(dentry, inode, mode, old_cred);
1334 err = PTR_ERR(new_cred);
1335 if (IS_ERR(new_cred)) {
1336 new_cred = NULL;
1337 goto out_revert_creds;
1340 ovl_path_upper(dentry->d_parent, &realparentpath);
1341 realfile = backing_tmpfile_open(&file->f_path, flags, &realparentpath,
1342 mode, current_cred());
1343 err = PTR_ERR_OR_ZERO(realfile);
1344 pr_debug("tmpfile/open(%pd2, 0%o) = %i\n", realparentpath.dentry, mode, err);
1345 if (err)
1346 goto out_revert_creds;
1348 of = ovl_file_alloc(realfile);
1349 if (!of) {
1350 fput(realfile);
1351 err = -ENOMEM;
1352 goto out_revert_creds;
1355 /* ovl_instantiate() consumes the newdentry reference on success */
1356 newdentry = dget(realfile->f_path.dentry);
1357 err = ovl_instantiate(dentry, inode, newdentry, false, file);
1358 if (!err) {
1359 file->private_data = of;
1360 } else {
1361 dput(newdentry);
1362 ovl_file_free(of);
1364 out_revert_creds:
1365 ovl_revert_creds(old_cred);
1366 put_cred(new_cred);
1367 return err;
1370 static int ovl_dummy_open(struct inode *inode, struct file *file)
1372 return 0;
1375 static int ovl_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
1376 struct file *file, umode_t mode)
1378 int err;
1379 struct dentry *dentry = file->f_path.dentry;
1380 struct inode *inode;
1382 if (!OVL_FS(dentry->d_sb)->tmpfile)
1383 return -EOPNOTSUPP;
1385 err = ovl_copy_up(dentry->d_parent);
1386 if (err)
1387 return err;
1389 err = ovl_want_write(dentry);
1390 if (err)
1391 return err;
1393 err = -ENOMEM;
1394 inode = ovl_new_inode(dentry->d_sb, mode, 0);
1395 if (!inode)
1396 goto drop_write;
1398 inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
1399 err = ovl_create_tmpfile(file, dentry, inode, inode->i_mode);
1400 if (err)
1401 goto put_inode;
1404 * Check if the preallocated inode was actually used. Having something
1405 * else assigned to the dentry shouldn't happen as that would indicate
1406 * that the backing tmpfile "leaked" out of overlayfs.
1408 err = -EIO;
1409 if (WARN_ON(inode != d_inode(dentry)))
1410 goto put_realfile;
1412 /* inode reference was transferred to dentry */
1413 inode = NULL;
1414 err = finish_open(file, dentry, ovl_dummy_open);
1415 put_realfile:
1416 /* Without FMODE_OPENED ->release() won't be called on @file */
1417 if (!(file->f_mode & FMODE_OPENED))
1418 ovl_file_free(file->private_data);
1419 put_inode:
1420 iput(inode);
1421 drop_write:
1422 ovl_drop_write(dentry);
1423 return err;
1426 const struct inode_operations ovl_dir_inode_operations = {
1427 .lookup = ovl_lookup,
1428 .mkdir = ovl_mkdir,
1429 .symlink = ovl_symlink,
1430 .unlink = ovl_unlink,
1431 .rmdir = ovl_rmdir,
1432 .rename = ovl_rename,
1433 .link = ovl_link,
1434 .setattr = ovl_setattr,
1435 .create = ovl_create,
1436 .mknod = ovl_mknod,
1437 .permission = ovl_permission,
1438 .getattr = ovl_getattr,
1439 .listxattr = ovl_listxattr,
1440 .get_inode_acl = ovl_get_inode_acl,
1441 .get_acl = ovl_get_acl,
1442 .set_acl = ovl_set_acl,
1443 .update_time = ovl_update_time,
1444 .fileattr_get = ovl_fileattr_get,
1445 .fileattr_set = ovl_fileattr_set,
1446 .tmpfile = ovl_tmpfile,