net: Convert ipv4_net_ops
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blob839709c7803a59bdd2ee965d4ef768e60eae20a7
1 /*
3 * Copyright (C) 2011 Novell Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
10 #include <linux/fs.h>
11 #include <linux/namei.h>
12 #include <linux/xattr.h>
13 #include <linux/security.h>
14 #include <linux/cred.h>
15 #include <linux/module.h>
16 #include <linux/posix_acl.h>
17 #include <linux/posix_acl_xattr.h>
18 #include <linux/atomic.h>
19 #include <linux/ratelimit.h>
20 #include "overlayfs.h"
22 static unsigned short ovl_redirect_max = 256;
23 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
24 MODULE_PARM_DESC(ovl_redirect_max,
25 "Maximum length of absolute redirect xattr value");
27 int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
29 int err;
31 dget(wdentry);
32 if (d_is_dir(wdentry))
33 err = ovl_do_rmdir(wdir, wdentry);
34 else
35 err = ovl_do_unlink(wdir, wdentry);
36 dput(wdentry);
38 if (err) {
39 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
40 wdentry, err);
43 return err;
46 struct dentry *ovl_lookup_temp(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 = lookup_one_len(name, workdir, strlen(name));
56 if (!IS_ERR(temp) && temp->d_inode) {
57 pr_err("overlayfs: 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 dentry *workdir)
68 int err;
69 struct dentry *whiteout;
70 struct inode *wdir = workdir->d_inode;
72 whiteout = ovl_lookup_temp(workdir);
73 if (IS_ERR(whiteout))
74 return whiteout;
76 err = ovl_do_whiteout(wdir, whiteout);
77 if (err) {
78 dput(whiteout);
79 whiteout = ERR_PTR(err);
82 return whiteout;
85 /* Caller must hold i_mutex on both workdir and dir */
86 int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
87 struct dentry *dentry)
89 struct inode *wdir = workdir->d_inode;
90 struct dentry *whiteout;
91 int err;
92 int flags = 0;
94 whiteout = ovl_whiteout(workdir);
95 err = PTR_ERR(whiteout);
96 if (IS_ERR(whiteout))
97 return err;
99 if (d_is_dir(dentry))
100 flags = RENAME_EXCHANGE;
102 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
103 if (err)
104 goto kill_whiteout;
105 if (flags)
106 ovl_cleanup(wdir, dentry);
108 out:
109 dput(whiteout);
110 return err;
112 kill_whiteout:
113 ovl_cleanup(wdir, whiteout);
114 goto out;
117 int ovl_create_real(struct inode *dir, struct dentry *newdentry,
118 struct cattr *attr, struct dentry *hardlink, bool debug)
120 int err;
122 if (newdentry->d_inode)
123 return -ESTALE;
125 if (hardlink) {
126 err = ovl_do_link(hardlink, dir, newdentry, debug);
127 } else {
128 switch (attr->mode & S_IFMT) {
129 case S_IFREG:
130 err = ovl_do_create(dir, newdentry, attr->mode, debug);
131 break;
133 case S_IFDIR:
134 err = ovl_do_mkdir(dir, newdentry, attr->mode, debug);
135 break;
137 case S_IFCHR:
138 case S_IFBLK:
139 case S_IFIFO:
140 case S_IFSOCK:
141 err = ovl_do_mknod(dir, newdentry,
142 attr->mode, attr->rdev, debug);
143 break;
145 case S_IFLNK:
146 err = ovl_do_symlink(dir, newdentry, attr->link, debug);
147 break;
149 default:
150 err = -EPERM;
153 if (!err && WARN_ON(!newdentry->d_inode)) {
155 * Not quite sure if non-instantiated dentry is legal or not.
156 * VFS doesn't seem to care so check and warn here.
158 err = -ENOENT;
160 return err;
163 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
164 int xerr)
166 int err;
168 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
169 if (!err)
170 ovl_dentry_set_opaque(dentry);
172 return err;
175 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
178 * Fail with -EIO when trying to create opaque dir and upper doesn't
179 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
180 * return a specific error for noxattr case.
182 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
185 /* Common operations required to be done after creation of file on upper */
186 static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
187 struct dentry *newdentry, bool hardlink)
189 ovl_dentry_version_inc(dentry->d_parent, false);
190 ovl_dentry_set_upper_alias(dentry);
191 if (!hardlink) {
192 ovl_inode_update(inode, newdentry);
193 ovl_copyattr(newdentry->d_inode, inode);
194 } else {
195 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
196 dput(newdentry);
197 inc_nlink(inode);
199 d_instantiate(dentry, inode);
200 /* Force lookup of new upper hardlink to find its lower */
201 if (hardlink)
202 d_drop(dentry);
205 static bool ovl_type_merge(struct dentry *dentry)
207 return OVL_TYPE_MERGE(ovl_path_type(dentry));
210 static bool ovl_type_origin(struct dentry *dentry)
212 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
215 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
216 struct cattr *attr, struct dentry *hardlink)
218 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
219 struct inode *udir = upperdir->d_inode;
220 struct dentry *newdentry;
221 int err;
223 if (!hardlink && !IS_POSIXACL(udir))
224 attr->mode &= ~current_umask();
226 inode_lock_nested(udir, I_MUTEX_PARENT);
227 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
228 dentry->d_name.len);
229 err = PTR_ERR(newdentry);
230 if (IS_ERR(newdentry))
231 goto out_unlock;
232 err = ovl_create_real(udir, newdentry, attr, hardlink, false);
233 if (err)
234 goto out_dput;
236 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
237 /* Setting opaque here is just an optimization, allow to fail */
238 ovl_set_opaque(dentry, newdentry);
241 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
242 newdentry = NULL;
243 out_dput:
244 dput(newdentry);
245 out_unlock:
246 inode_unlock(udir);
247 return err;
250 static struct dentry *ovl_clear_empty(struct dentry *dentry,
251 struct list_head *list)
253 struct dentry *workdir = ovl_workdir(dentry);
254 struct inode *wdir = workdir->d_inode;
255 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
256 struct inode *udir = upperdir->d_inode;
257 struct path upperpath;
258 struct dentry *upper;
259 struct dentry *opaquedir;
260 struct kstat stat;
261 int err;
263 if (WARN_ON(!workdir))
264 return ERR_PTR(-EROFS);
266 err = ovl_lock_rename_workdir(workdir, upperdir);
267 if (err)
268 goto out;
270 ovl_path_upper(dentry, &upperpath);
271 err = vfs_getattr(&upperpath, &stat,
272 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
273 if (err)
274 goto out_unlock;
276 err = -ESTALE;
277 if (!S_ISDIR(stat.mode))
278 goto out_unlock;
279 upper = upperpath.dentry;
280 if (upper->d_parent->d_inode != udir)
281 goto out_unlock;
283 opaquedir = ovl_lookup_temp(workdir);
284 err = PTR_ERR(opaquedir);
285 if (IS_ERR(opaquedir))
286 goto out_unlock;
288 err = ovl_create_real(wdir, opaquedir,
289 &(struct cattr){.mode = stat.mode}, NULL, true);
290 if (err)
291 goto out_dput;
293 err = ovl_copy_xattr(upper, opaquedir);
294 if (err)
295 goto out_cleanup;
297 err = ovl_set_opaque(dentry, opaquedir);
298 if (err)
299 goto out_cleanup;
301 inode_lock(opaquedir->d_inode);
302 err = ovl_set_attr(opaquedir, &stat);
303 inode_unlock(opaquedir->d_inode);
304 if (err)
305 goto out_cleanup;
307 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
308 if (err)
309 goto out_cleanup;
311 ovl_cleanup_whiteouts(upper, list);
312 ovl_cleanup(wdir, upper);
313 unlock_rename(workdir, upperdir);
315 /* dentry's upper doesn't match now, get rid of it */
316 d_drop(dentry);
318 return opaquedir;
320 out_cleanup:
321 ovl_cleanup(wdir, opaquedir);
322 out_dput:
323 dput(opaquedir);
324 out_unlock:
325 unlock_rename(workdir, upperdir);
326 out:
327 return ERR_PTR(err);
330 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
331 const struct posix_acl *acl)
333 void *buffer;
334 size_t size;
335 int err;
337 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
338 return 0;
340 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
341 buffer = kmalloc(size, GFP_KERNEL);
342 if (!buffer)
343 return -ENOMEM;
345 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
346 err = size;
347 if (err < 0)
348 goto out_free;
350 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
351 out_free:
352 kfree(buffer);
353 return err;
356 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
357 struct cattr *cattr,
358 struct dentry *hardlink)
360 struct dentry *workdir = ovl_workdir(dentry);
361 struct inode *wdir = workdir->d_inode;
362 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
363 struct inode *udir = upperdir->d_inode;
364 struct dentry *upper;
365 struct dentry *newdentry;
366 int err;
367 struct posix_acl *acl, *default_acl;
369 if (WARN_ON(!workdir))
370 return -EROFS;
372 if (!hardlink) {
373 err = posix_acl_create(dentry->d_parent->d_inode,
374 &cattr->mode, &default_acl, &acl);
375 if (err)
376 return err;
379 err = ovl_lock_rename_workdir(workdir, upperdir);
380 if (err)
381 goto out;
383 newdentry = ovl_lookup_temp(workdir);
384 err = PTR_ERR(newdentry);
385 if (IS_ERR(newdentry))
386 goto out_unlock;
388 upper = lookup_one_len(dentry->d_name.name, upperdir,
389 dentry->d_name.len);
390 err = PTR_ERR(upper);
391 if (IS_ERR(upper))
392 goto out_dput;
394 err = ovl_create_real(wdir, newdentry, cattr, hardlink, true);
395 if (err)
396 goto out_dput2;
399 * mode could have been mutilated due to umask (e.g. sgid directory)
401 if (!hardlink &&
402 !S_ISLNK(cattr->mode) &&
403 newdentry->d_inode->i_mode != cattr->mode) {
404 struct iattr attr = {
405 .ia_valid = ATTR_MODE,
406 .ia_mode = cattr->mode,
408 inode_lock(newdentry->d_inode);
409 err = notify_change(newdentry, &attr, NULL);
410 inode_unlock(newdentry->d_inode);
411 if (err)
412 goto out_cleanup;
414 if (!hardlink) {
415 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
416 acl);
417 if (err)
418 goto out_cleanup;
420 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
421 default_acl);
422 if (err)
423 goto out_cleanup;
426 if (!hardlink && S_ISDIR(cattr->mode)) {
427 err = ovl_set_opaque(dentry, newdentry);
428 if (err)
429 goto out_cleanup;
431 err = ovl_do_rename(wdir, newdentry, udir, upper,
432 RENAME_EXCHANGE);
433 if (err)
434 goto out_cleanup;
436 ovl_cleanup(wdir, upper);
437 } else {
438 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
439 if (err)
440 goto out_cleanup;
442 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
443 newdentry = NULL;
444 out_dput2:
445 dput(upper);
446 out_dput:
447 dput(newdentry);
448 out_unlock:
449 unlock_rename(workdir, upperdir);
450 out:
451 if (!hardlink) {
452 posix_acl_release(acl);
453 posix_acl_release(default_acl);
455 return err;
457 out_cleanup:
458 ovl_cleanup(wdir, newdentry);
459 goto out_dput2;
462 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
463 struct cattr *attr, struct dentry *hardlink,
464 bool origin)
466 int err;
467 const struct cred *old_cred;
468 struct cred *override_cred;
469 struct dentry *parent = dentry->d_parent;
471 err = ovl_copy_up(parent);
472 if (err)
473 return err;
475 old_cred = ovl_override_creds(dentry->d_sb);
478 * When linking a file with copy up origin into a new parent, mark the
479 * new parent dir "impure".
481 if (origin) {
482 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
483 if (err)
484 goto out_revert_creds;
487 err = -ENOMEM;
488 override_cred = prepare_creds();
489 if (override_cred) {
490 override_cred->fsuid = inode->i_uid;
491 override_cred->fsgid = inode->i_gid;
492 if (!hardlink) {
493 err = security_dentry_create_files_as(dentry,
494 attr->mode, &dentry->d_name, old_cred,
495 override_cred);
496 if (err) {
497 put_cred(override_cred);
498 goto out_revert_creds;
501 put_cred(override_creds(override_cred));
502 put_cred(override_cred);
504 if (!ovl_dentry_is_whiteout(dentry))
505 err = ovl_create_upper(dentry, inode, attr,
506 hardlink);
507 else
508 err = ovl_create_over_whiteout(dentry, inode, attr,
509 hardlink);
511 out_revert_creds:
512 revert_creds(old_cred);
513 if (!err) {
514 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
516 WARN_ON(inode->i_mode != realinode->i_mode);
517 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
518 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
520 return err;
523 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
524 const char *link)
526 int err;
527 struct inode *inode;
528 struct cattr attr = {
529 .rdev = rdev,
530 .link = link,
533 err = ovl_want_write(dentry);
534 if (err)
535 goto out;
537 err = -ENOMEM;
538 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
539 if (!inode)
540 goto out_drop_write;
542 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
543 attr.mode = inode->i_mode;
545 err = ovl_create_or_link(dentry, inode, &attr, NULL, false);
546 if (err)
547 iput(inode);
549 out_drop_write:
550 ovl_drop_write(dentry);
551 out:
552 return err;
555 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
556 bool excl)
558 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
561 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
563 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
566 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
567 dev_t rdev)
569 /* Don't allow creation of "whiteout" on overlay */
570 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
571 return -EPERM;
573 return ovl_create_object(dentry, mode, rdev, NULL);
576 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
577 const char *link)
579 return ovl_create_object(dentry, S_IFLNK, 0, link);
582 static int ovl_link(struct dentry *old, struct inode *newdir,
583 struct dentry *new)
585 int err;
586 bool locked = false;
587 struct inode *inode;
589 err = ovl_want_write(old);
590 if (err)
591 goto out;
593 err = ovl_copy_up(old);
594 if (err)
595 goto out_drop_write;
597 err = ovl_nlink_start(old, &locked);
598 if (err)
599 goto out_drop_write;
601 inode = d_inode(old);
602 ihold(inode);
604 err = ovl_create_or_link(new, inode, NULL, ovl_dentry_upper(old),
605 ovl_type_origin(old));
606 if (err)
607 iput(inode);
609 ovl_nlink_end(old, locked);
610 out_drop_write:
611 ovl_drop_write(old);
612 out:
613 return err;
616 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
618 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
621 static int ovl_remove_and_whiteout(struct dentry *dentry,
622 struct list_head *list)
624 struct dentry *workdir = ovl_workdir(dentry);
625 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
626 struct dentry *upper;
627 struct dentry *opaquedir = NULL;
628 int err;
630 if (WARN_ON(!workdir))
631 return -EROFS;
633 if (!list_empty(list)) {
634 opaquedir = ovl_clear_empty(dentry, list);
635 err = PTR_ERR(opaquedir);
636 if (IS_ERR(opaquedir))
637 goto out;
640 err = ovl_lock_rename_workdir(workdir, upperdir);
641 if (err)
642 goto out_dput;
644 upper = lookup_one_len(dentry->d_name.name, upperdir,
645 dentry->d_name.len);
646 err = PTR_ERR(upper);
647 if (IS_ERR(upper))
648 goto out_unlock;
650 err = -ESTALE;
651 if ((opaquedir && upper != opaquedir) ||
652 (!opaquedir && ovl_dentry_upper(dentry) &&
653 !ovl_matches_upper(dentry, upper))) {
654 goto out_dput_upper;
657 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
658 if (err)
659 goto out_d_drop;
661 ovl_dentry_version_inc(dentry->d_parent, true);
662 out_d_drop:
663 d_drop(dentry);
664 out_dput_upper:
665 dput(upper);
666 out_unlock:
667 unlock_rename(workdir, upperdir);
668 out_dput:
669 dput(opaquedir);
670 out:
671 return err;
674 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
675 struct list_head *list)
677 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
678 struct inode *dir = upperdir->d_inode;
679 struct dentry *upper;
680 struct dentry *opaquedir = NULL;
681 int err;
683 if (!list_empty(list)) {
684 opaquedir = ovl_clear_empty(dentry, list);
685 err = PTR_ERR(opaquedir);
686 if (IS_ERR(opaquedir))
687 goto out;
690 inode_lock_nested(dir, I_MUTEX_PARENT);
691 upper = lookup_one_len(dentry->d_name.name, upperdir,
692 dentry->d_name.len);
693 err = PTR_ERR(upper);
694 if (IS_ERR(upper))
695 goto out_unlock;
697 err = -ESTALE;
698 if ((opaquedir && upper != opaquedir) ||
699 (!opaquedir && !ovl_matches_upper(dentry, upper)))
700 goto out_dput_upper;
702 if (is_dir)
703 err = vfs_rmdir(dir, upper);
704 else
705 err = vfs_unlink(dir, upper, NULL);
706 ovl_dentry_version_inc(dentry->d_parent, ovl_type_origin(dentry));
709 * Keeping this dentry hashed would mean having to release
710 * upperpath/lowerpath, which could only be done if we are the
711 * sole user of this dentry. Too tricky... Just unhash for
712 * now.
714 if (!err)
715 d_drop(dentry);
716 out_dput_upper:
717 dput(upper);
718 out_unlock:
719 inode_unlock(dir);
720 dput(opaquedir);
721 out:
722 return err;
725 static bool ovl_pure_upper(struct dentry *dentry)
727 return !ovl_dentry_lower(dentry) &&
728 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
731 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
733 int err;
734 bool locked = false;
735 const struct cred *old_cred;
736 bool lower_positive = ovl_lower_positive(dentry);
737 LIST_HEAD(list);
739 /* No need to clean pure upper removed by vfs_rmdir() */
740 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
741 err = ovl_check_empty_dir(dentry, &list);
742 if (err)
743 goto out;
746 err = ovl_want_write(dentry);
747 if (err)
748 goto out;
750 err = ovl_copy_up(dentry->d_parent);
751 if (err)
752 goto out_drop_write;
754 err = ovl_nlink_start(dentry, &locked);
755 if (err)
756 goto out_drop_write;
758 old_cred = ovl_override_creds(dentry->d_sb);
759 if (!lower_positive)
760 err = ovl_remove_upper(dentry, is_dir, &list);
761 else
762 err = ovl_remove_and_whiteout(dentry, &list);
763 revert_creds(old_cred);
764 if (!err) {
765 if (is_dir)
766 clear_nlink(dentry->d_inode);
767 else
768 drop_nlink(dentry->d_inode);
770 ovl_nlink_end(dentry, locked);
771 out_drop_write:
772 ovl_drop_write(dentry);
773 out:
774 ovl_cache_free(&list);
775 return err;
778 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
780 return ovl_do_remove(dentry, false);
783 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
785 return ovl_do_remove(dentry, true);
788 static bool ovl_type_merge_or_lower(struct dentry *dentry)
790 enum ovl_path_type type = ovl_path_type(dentry);
792 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
795 static bool ovl_can_move(struct dentry *dentry)
797 return ovl_redirect_dir(dentry->d_sb) ||
798 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
801 static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
803 char *buf, *ret;
804 struct dentry *d, *tmp;
805 int buflen = ovl_redirect_max + 1;
807 if (samedir) {
808 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
809 GFP_KERNEL);
810 goto out;
813 buf = ret = kmalloc(buflen, GFP_KERNEL);
814 if (!buf)
815 goto out;
817 buflen--;
818 buf[buflen] = '\0';
819 for (d = dget(dentry); !IS_ROOT(d);) {
820 const char *name;
821 int thislen;
823 spin_lock(&d->d_lock);
824 name = ovl_dentry_get_redirect(d);
825 if (name) {
826 thislen = strlen(name);
827 } else {
828 name = d->d_name.name;
829 thislen = d->d_name.len;
832 /* If path is too long, fall back to userspace move */
833 if (thislen + (name[0] != '/') > buflen) {
834 ret = ERR_PTR(-EXDEV);
835 spin_unlock(&d->d_lock);
836 goto out_put;
839 buflen -= thislen;
840 memcpy(&buf[buflen], name, thislen);
841 tmp = dget_dlock(d->d_parent);
842 spin_unlock(&d->d_lock);
844 dput(d);
845 d = tmp;
847 /* Absolute redirect: finished */
848 if (buf[buflen] == '/')
849 break;
850 buflen--;
851 buf[buflen] = '/';
853 ret = kstrdup(&buf[buflen], GFP_KERNEL);
854 out_put:
855 dput(d);
856 kfree(buf);
857 out:
858 return ret ? ret : ERR_PTR(-ENOMEM);
861 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
863 int err;
864 const char *redirect = ovl_dentry_get_redirect(dentry);
866 if (redirect && (samedir || redirect[0] == '/'))
867 return 0;
869 redirect = ovl_get_redirect(dentry, samedir);
870 if (IS_ERR(redirect))
871 return PTR_ERR(redirect);
873 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
874 OVL_XATTR_REDIRECT,
875 redirect, strlen(redirect), -EXDEV);
876 if (!err) {
877 spin_lock(&dentry->d_lock);
878 ovl_dentry_set_redirect(dentry, redirect);
879 spin_unlock(&dentry->d_lock);
880 } else {
881 kfree(redirect);
882 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
883 err);
884 /* Fall back to userspace copy-up */
885 err = -EXDEV;
887 return err;
890 static int ovl_rename(struct inode *olddir, struct dentry *old,
891 struct inode *newdir, struct dentry *new,
892 unsigned int flags)
894 int err;
895 bool locked = false;
896 struct dentry *old_upperdir;
897 struct dentry *new_upperdir;
898 struct dentry *olddentry;
899 struct dentry *newdentry;
900 struct dentry *trap;
901 bool old_opaque;
902 bool new_opaque;
903 bool cleanup_whiteout = false;
904 bool overwrite = !(flags & RENAME_EXCHANGE);
905 bool is_dir = d_is_dir(old);
906 bool new_is_dir = d_is_dir(new);
907 bool samedir = olddir == newdir;
908 struct dentry *opaquedir = NULL;
909 const struct cred *old_cred = NULL;
910 LIST_HEAD(list);
912 err = -EINVAL;
913 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
914 goto out;
916 flags &= ~RENAME_NOREPLACE;
918 /* Don't copy up directory trees */
919 err = -EXDEV;
920 if (!ovl_can_move(old))
921 goto out;
922 if (!overwrite && !ovl_can_move(new))
923 goto out;
925 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
926 err = ovl_check_empty_dir(new, &list);
927 if (err)
928 goto out;
931 if (overwrite) {
932 if (ovl_lower_positive(old)) {
933 if (!ovl_dentry_is_whiteout(new)) {
934 /* Whiteout source */
935 flags |= RENAME_WHITEOUT;
936 } else {
937 /* Switch whiteouts */
938 flags |= RENAME_EXCHANGE;
940 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
941 flags |= RENAME_EXCHANGE;
942 cleanup_whiteout = true;
946 err = ovl_want_write(old);
947 if (err)
948 goto out;
950 err = ovl_copy_up(old);
951 if (err)
952 goto out_drop_write;
954 err = ovl_copy_up(new->d_parent);
955 if (err)
956 goto out_drop_write;
957 if (!overwrite) {
958 err = ovl_copy_up(new);
959 if (err)
960 goto out_drop_write;
961 } else {
962 err = ovl_nlink_start(new, &locked);
963 if (err)
964 goto out_drop_write;
967 old_cred = ovl_override_creds(old->d_sb);
969 if (!list_empty(&list)) {
970 opaquedir = ovl_clear_empty(new, &list);
971 err = PTR_ERR(opaquedir);
972 if (IS_ERR(opaquedir)) {
973 opaquedir = NULL;
974 goto out_revert_creds;
978 old_upperdir = ovl_dentry_upper(old->d_parent);
979 new_upperdir = ovl_dentry_upper(new->d_parent);
981 if (!samedir) {
983 * When moving a merge dir or non-dir with copy up origin into
984 * a new parent, we are marking the new parent dir "impure".
985 * When ovl_iterate() iterates an "impure" upper dir, it will
986 * lookup the origin inodes of the entries to fill d_ino.
988 if (ovl_type_origin(old)) {
989 err = ovl_set_impure(new->d_parent, new_upperdir);
990 if (err)
991 goto out_revert_creds;
993 if (!overwrite && ovl_type_origin(new)) {
994 err = ovl_set_impure(old->d_parent, old_upperdir);
995 if (err)
996 goto out_revert_creds;
1000 trap = lock_rename(new_upperdir, old_upperdir);
1002 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1003 old->d_name.len);
1004 err = PTR_ERR(olddentry);
1005 if (IS_ERR(olddentry))
1006 goto out_unlock;
1008 err = -ESTALE;
1009 if (!ovl_matches_upper(old, olddentry))
1010 goto out_dput_old;
1012 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1013 new->d_name.len);
1014 err = PTR_ERR(newdentry);
1015 if (IS_ERR(newdentry))
1016 goto out_dput_old;
1018 old_opaque = ovl_dentry_is_opaque(old);
1019 new_opaque = ovl_dentry_is_opaque(new);
1021 err = -ESTALE;
1022 if (d_inode(new) && ovl_dentry_upper(new)) {
1023 if (opaquedir) {
1024 if (newdentry != opaquedir)
1025 goto out_dput;
1026 } else {
1027 if (!ovl_matches_upper(new, newdentry))
1028 goto out_dput;
1030 } else {
1031 if (!d_is_negative(newdentry) &&
1032 (!new_opaque || !ovl_is_whiteout(newdentry)))
1033 goto out_dput;
1036 if (olddentry == trap)
1037 goto out_dput;
1038 if (newdentry == trap)
1039 goto out_dput;
1041 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1042 goto out_dput;
1044 err = 0;
1045 if (is_dir) {
1046 if (ovl_type_merge_or_lower(old))
1047 err = ovl_set_redirect(old, samedir);
1048 else if (!old_opaque && ovl_type_merge(new->d_parent))
1049 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1050 if (err)
1051 goto out_dput;
1053 if (!overwrite && new_is_dir) {
1054 if (ovl_type_merge_or_lower(new))
1055 err = ovl_set_redirect(new, samedir);
1056 else if (!new_opaque && ovl_type_merge(old->d_parent))
1057 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1058 if (err)
1059 goto out_dput;
1062 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1063 new_upperdir->d_inode, newdentry, flags);
1064 if (err)
1065 goto out_dput;
1067 if (cleanup_whiteout)
1068 ovl_cleanup(old_upperdir->d_inode, newdentry);
1070 if (overwrite && d_inode(new)) {
1071 if (new_is_dir)
1072 clear_nlink(d_inode(new));
1073 else
1074 drop_nlink(d_inode(new));
1077 ovl_dentry_version_inc(old->d_parent, ovl_type_origin(old) ||
1078 (!overwrite && ovl_type_origin(new)));
1079 ovl_dentry_version_inc(new->d_parent, ovl_type_origin(old) ||
1080 (d_inode(new) && ovl_type_origin(new)));
1082 out_dput:
1083 dput(newdentry);
1084 out_dput_old:
1085 dput(olddentry);
1086 out_unlock:
1087 unlock_rename(new_upperdir, old_upperdir);
1088 out_revert_creds:
1089 revert_creds(old_cred);
1090 ovl_nlink_end(new, locked);
1091 out_drop_write:
1092 ovl_drop_write(old);
1093 out:
1094 dput(opaquedir);
1095 ovl_cache_free(&list);
1096 return err;
1099 const struct inode_operations ovl_dir_inode_operations = {
1100 .lookup = ovl_lookup,
1101 .mkdir = ovl_mkdir,
1102 .symlink = ovl_symlink,
1103 .unlink = ovl_unlink,
1104 .rmdir = ovl_rmdir,
1105 .rename = ovl_rename,
1106 .link = ovl_link,
1107 .setattr = ovl_setattr,
1108 .create = ovl_create,
1109 .mknod = ovl_mknod,
1110 .permission = ovl_permission,
1111 .getattr = ovl_getattr,
1112 .listxattr = ovl_listxattr,
1113 .get_acl = ovl_get_acl,
1114 .update_time = ovl_update_time,