perf intel-pt: Factor out intel_pt_8b_tsc()
[linux/fpc-iii.git] / fs / overlayfs / util.c
blob4035e640f40211713fca5fea72fe7fb28babcd29
1 /*
2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, 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/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/exportfs.h>
16 #include <linux/uuid.h>
17 #include <linux/namei.h>
18 #include <linux/ratelimit.h>
19 #include "overlayfs.h"
21 int ovl_want_write(struct dentry *dentry)
23 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
24 return mnt_want_write(ofs->upper_mnt);
27 void ovl_drop_write(struct dentry *dentry)
29 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
30 mnt_drop_write(ofs->upper_mnt);
33 struct dentry *ovl_workdir(struct dentry *dentry)
35 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 return ofs->workdir;
39 const struct cred *ovl_override_creds(struct super_block *sb)
41 struct ovl_fs *ofs = sb->s_fs_info;
43 return override_creds(ofs->creator_cred);
46 struct super_block *ovl_same_sb(struct super_block *sb)
48 struct ovl_fs *ofs = sb->s_fs_info;
50 if (!ofs->numlowerfs)
51 return ofs->upper_mnt->mnt_sb;
52 else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
53 return ofs->lower_fs[0].sb;
54 else
55 return NULL;
59 * Check if underlying fs supports file handles and try to determine encoding
60 * type, in order to deduce maximum inode number used by fs.
62 * Return 0 if file handles are not supported.
63 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
64 * Return -1 if fs uses a non default encoding with unknown inode size.
66 int ovl_can_decode_fh(struct super_block *sb)
68 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
69 return 0;
71 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
74 struct dentry *ovl_indexdir(struct super_block *sb)
76 struct ovl_fs *ofs = sb->s_fs_info;
78 return ofs->indexdir;
81 /* Index all files on copy up. For now only enabled for NFS export */
82 bool ovl_index_all(struct super_block *sb)
84 struct ovl_fs *ofs = sb->s_fs_info;
86 return ofs->config.nfs_export && ofs->config.index;
89 /* Verify lower origin on lookup. For now only enabled for NFS export */
90 bool ovl_verify_lower(struct super_block *sb)
92 struct ovl_fs *ofs = sb->s_fs_info;
94 return ofs->config.nfs_export && ofs->config.index;
97 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
99 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
100 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
102 if (oe)
103 oe->numlower = numlower;
105 return oe;
108 bool ovl_dentry_remote(struct dentry *dentry)
110 return dentry->d_flags &
111 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
112 DCACHE_OP_REAL);
115 bool ovl_dentry_weird(struct dentry *dentry)
117 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
118 DCACHE_MANAGE_TRANSIT |
119 DCACHE_OP_HASH |
120 DCACHE_OP_COMPARE);
123 enum ovl_path_type ovl_path_type(struct dentry *dentry)
125 struct ovl_entry *oe = dentry->d_fsdata;
126 enum ovl_path_type type = 0;
128 if (ovl_dentry_upper(dentry)) {
129 type = __OVL_PATH_UPPER;
132 * Non-dir dentry can hold lower dentry of its copy up origin.
134 if (oe->numlower) {
135 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
136 type |= __OVL_PATH_ORIGIN;
137 if (d_is_dir(dentry) ||
138 !ovl_has_upperdata(d_inode(dentry)))
139 type |= __OVL_PATH_MERGE;
141 } else {
142 if (oe->numlower > 1)
143 type |= __OVL_PATH_MERGE;
145 return type;
148 void ovl_path_upper(struct dentry *dentry, struct path *path)
150 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
152 path->mnt = ofs->upper_mnt;
153 path->dentry = ovl_dentry_upper(dentry);
156 void ovl_path_lower(struct dentry *dentry, struct path *path)
158 struct ovl_entry *oe = dentry->d_fsdata;
160 if (oe->numlower) {
161 path->mnt = oe->lowerstack[0].layer->mnt;
162 path->dentry = oe->lowerstack[0].dentry;
163 } else {
164 *path = (struct path) { };
168 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
170 struct ovl_entry *oe = dentry->d_fsdata;
172 if (oe->numlower) {
173 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
174 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
175 } else {
176 *path = (struct path) { };
180 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
182 enum ovl_path_type type = ovl_path_type(dentry);
184 if (!OVL_TYPE_UPPER(type))
185 ovl_path_lower(dentry, path);
186 else
187 ovl_path_upper(dentry, path);
189 return type;
192 struct dentry *ovl_dentry_upper(struct dentry *dentry)
194 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
197 struct dentry *ovl_dentry_lower(struct dentry *dentry)
199 struct ovl_entry *oe = dentry->d_fsdata;
201 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
204 struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
206 struct ovl_entry *oe = dentry->d_fsdata;
208 return oe->numlower ? oe->lowerstack[0].layer : NULL;
212 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
213 * dependig on what is stored in lowerstack[0]. At times we need to find
214 * lower dentry which has data (and not metacopy dentry). This helper
215 * returns the lower data dentry.
217 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
219 struct ovl_entry *oe = dentry->d_fsdata;
221 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
224 struct dentry *ovl_dentry_real(struct dentry *dentry)
226 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
229 struct dentry *ovl_i_dentry_upper(struct inode *inode)
231 return ovl_upperdentry_dereference(OVL_I(inode));
234 struct inode *ovl_inode_upper(struct inode *inode)
236 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
238 return upperdentry ? d_inode(upperdentry) : NULL;
241 struct inode *ovl_inode_lower(struct inode *inode)
243 return OVL_I(inode)->lower;
246 struct inode *ovl_inode_real(struct inode *inode)
248 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
251 /* Return inode which contains lower data. Do not return metacopy */
252 struct inode *ovl_inode_lowerdata(struct inode *inode)
254 if (WARN_ON(!S_ISREG(inode->i_mode)))
255 return NULL;
257 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
260 /* Return real inode which contains data. Does not return metacopy inode */
261 struct inode *ovl_inode_realdata(struct inode *inode)
263 struct inode *upperinode;
265 upperinode = ovl_inode_upper(inode);
266 if (upperinode && ovl_has_upperdata(inode))
267 return upperinode;
269 return ovl_inode_lowerdata(inode);
272 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
274 return OVL_I(inode)->cache;
277 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
279 OVL_I(inode)->cache = cache;
282 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
284 set_bit(flag, &OVL_E(dentry)->flags);
287 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
289 clear_bit(flag, &OVL_E(dentry)->flags);
292 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
294 return test_bit(flag, &OVL_E(dentry)->flags);
297 bool ovl_dentry_is_opaque(struct dentry *dentry)
299 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
302 bool ovl_dentry_is_whiteout(struct dentry *dentry)
304 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
307 void ovl_dentry_set_opaque(struct dentry *dentry)
309 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
313 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
314 * to return positive, while there's no actual upper alias for the inode.
315 * Copy up code needs to know about the existence of the upper alias, so it
316 * can't use ovl_dentry_upper().
318 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
320 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
323 void ovl_dentry_set_upper_alias(struct dentry *dentry)
325 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
328 static bool ovl_should_check_upperdata(struct inode *inode)
330 if (!S_ISREG(inode->i_mode))
331 return false;
333 if (!ovl_inode_lower(inode))
334 return false;
336 return true;
339 bool ovl_has_upperdata(struct inode *inode)
341 if (!ovl_should_check_upperdata(inode))
342 return true;
344 if (!ovl_test_flag(OVL_UPPERDATA, inode))
345 return false;
347 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
348 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
349 * if setting of OVL_UPPERDATA is visible, then effects of writes
350 * before that are visible too.
352 smp_rmb();
353 return true;
356 void ovl_set_upperdata(struct inode *inode)
359 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
360 * if OVL_UPPERDATA flag is visible, then effects of write operations
361 * before it are visible as well.
363 smp_wmb();
364 ovl_set_flag(OVL_UPPERDATA, inode);
367 /* Caller should hold ovl_inode->lock */
368 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
370 if (!ovl_open_flags_need_copy_up(flags))
371 return false;
373 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
376 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
378 if (!ovl_open_flags_need_copy_up(flags))
379 return false;
381 return !ovl_has_upperdata(d_inode(dentry));
384 bool ovl_redirect_dir(struct super_block *sb)
386 struct ovl_fs *ofs = sb->s_fs_info;
388 return ofs->config.redirect_dir && !ofs->noxattr;
391 const char *ovl_dentry_get_redirect(struct dentry *dentry)
393 return OVL_I(d_inode(dentry))->redirect;
396 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
398 struct ovl_inode *oi = OVL_I(d_inode(dentry));
400 kfree(oi->redirect);
401 oi->redirect = redirect;
404 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
405 struct dentry *lowerdentry, struct dentry *lowerdata)
407 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
409 if (upperdentry)
410 OVL_I(inode)->__upperdentry = upperdentry;
411 if (lowerdentry)
412 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
413 if (lowerdata)
414 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
416 ovl_copyattr(realinode, inode);
417 ovl_copyflags(realinode, inode);
418 if (!inode->i_ino)
419 inode->i_ino = realinode->i_ino;
422 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
424 struct inode *upperinode = d_inode(upperdentry);
426 WARN_ON(OVL_I(inode)->__upperdentry);
429 * Make sure upperdentry is consistent before making it visible
431 smp_wmb();
432 OVL_I(inode)->__upperdentry = upperdentry;
433 if (inode_unhashed(inode)) {
434 if (!inode->i_ino)
435 inode->i_ino = upperinode->i_ino;
436 inode->i_private = upperinode;
437 __insert_inode_hash(inode, (unsigned long) upperinode);
441 static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
443 struct inode *inode = d_inode(dentry);
445 WARN_ON(!inode_is_locked(inode));
447 * Version is used by readdir code to keep cache consistent. For merge
448 * dirs all changes need to be noted. For non-merge dirs, cache only
449 * contains impure (ones which have been copied up and have origins)
450 * entries, so only need to note changes to impure entries.
452 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
453 OVL_I(inode)->version++;
456 void ovl_dir_modified(struct dentry *dentry, bool impurity)
458 /* Copy mtime/ctime */
459 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
461 ovl_dentry_version_inc(dentry, impurity);
464 u64 ovl_dentry_version_get(struct dentry *dentry)
466 struct inode *inode = d_inode(dentry);
468 WARN_ON(!inode_is_locked(inode));
469 return OVL_I(inode)->version;
472 bool ovl_is_whiteout(struct dentry *dentry)
474 struct inode *inode = dentry->d_inode;
476 return inode && IS_WHITEOUT(inode);
479 struct file *ovl_path_open(struct path *path, int flags)
481 return dentry_open(path, flags | O_NOATIME, current_cred());
484 /* Caller should hold ovl_inode->lock */
485 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
487 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
489 if (ovl_dentry_upper(dentry) &&
490 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
491 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
492 return true;
494 return false;
497 bool ovl_already_copied_up(struct dentry *dentry, int flags)
499 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
502 * Check if copy-up has happened as well as for upper alias (in
503 * case of hard links) is there.
505 * Both checks are lockless:
506 * - false negatives: will recheck under oi->lock
507 * - false positives:
508 * + ovl_dentry_upper() uses memory barriers to ensure the
509 * upper dentry is up-to-date
510 * + ovl_dentry_has_upper_alias() relies on locking of
511 * upper parent i_rwsem to prevent reordering copy-up
512 * with rename.
514 if (ovl_dentry_upper(dentry) &&
515 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
516 !ovl_dentry_needs_data_copy_up(dentry, flags))
517 return true;
519 return false;
522 int ovl_copy_up_start(struct dentry *dentry, int flags)
524 struct inode *inode = d_inode(dentry);
525 int err;
527 err = ovl_inode_lock(inode);
528 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
529 err = 1; /* Already copied up */
530 ovl_inode_unlock(inode);
533 return err;
536 void ovl_copy_up_end(struct dentry *dentry)
538 ovl_inode_unlock(d_inode(dentry));
541 bool ovl_check_origin_xattr(struct dentry *dentry)
543 int res;
545 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
547 /* Zero size value means "copied up but origin unknown" */
548 if (res >= 0)
549 return true;
551 return false;
554 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
556 int res;
557 char val;
559 if (!d_is_dir(dentry))
560 return false;
562 res = vfs_getxattr(dentry, name, &val, 1);
563 if (res == 1 && val == 'y')
564 return true;
566 return false;
569 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
570 const char *name, const void *value, size_t size,
571 int xerr)
573 int err;
574 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
576 if (ofs->noxattr)
577 return xerr;
579 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
581 if (err == -EOPNOTSUPP) {
582 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
583 ofs->noxattr = true;
584 return xerr;
587 return err;
590 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
592 int err;
594 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
595 return 0;
598 * Do not fail when upper doesn't support xattrs.
599 * Upper inodes won't have origin nor redirect xattr anyway.
601 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
602 "y", 1, 0);
603 if (!err)
604 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
606 return err;
609 void ovl_set_flag(unsigned long flag, struct inode *inode)
611 set_bit(flag, &OVL_I(inode)->flags);
614 void ovl_clear_flag(unsigned long flag, struct inode *inode)
616 clear_bit(flag, &OVL_I(inode)->flags);
619 bool ovl_test_flag(unsigned long flag, struct inode *inode)
621 return test_bit(flag, &OVL_I(inode)->flags);
625 * Caller must hold a reference to inode to prevent it from being freed while
626 * it is marked inuse.
628 bool ovl_inuse_trylock(struct dentry *dentry)
630 struct inode *inode = d_inode(dentry);
631 bool locked = false;
633 spin_lock(&inode->i_lock);
634 if (!(inode->i_state & I_OVL_INUSE)) {
635 inode->i_state |= I_OVL_INUSE;
636 locked = true;
638 spin_unlock(&inode->i_lock);
640 return locked;
643 void ovl_inuse_unlock(struct dentry *dentry)
645 if (dentry) {
646 struct inode *inode = d_inode(dentry);
648 spin_lock(&inode->i_lock);
649 WARN_ON(!(inode->i_state & I_OVL_INUSE));
650 inode->i_state &= ~I_OVL_INUSE;
651 spin_unlock(&inode->i_lock);
656 * Does this overlay dentry need to be indexed on copy up?
658 bool ovl_need_index(struct dentry *dentry)
660 struct dentry *lower = ovl_dentry_lower(dentry);
662 if (!lower || !ovl_indexdir(dentry->d_sb))
663 return false;
665 /* Index all files for NFS export and consistency verification */
666 if (ovl_index_all(dentry->d_sb))
667 return true;
669 /* Index only lower hardlinks on copy up */
670 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
671 return true;
673 return false;
676 /* Caller must hold OVL_I(inode)->lock */
677 static void ovl_cleanup_index(struct dentry *dentry)
679 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
680 struct inode *dir = indexdir->d_inode;
681 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
682 struct dentry *upperdentry = ovl_dentry_upper(dentry);
683 struct dentry *index = NULL;
684 struct inode *inode;
685 struct qstr name = { };
686 int err;
688 err = ovl_get_index_name(lowerdentry, &name);
689 if (err)
690 goto fail;
692 inode = d_inode(upperdentry);
693 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
694 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
695 upperdentry, inode->i_ino, inode->i_nlink);
697 * We either have a bug with persistent union nlink or a lower
698 * hardlink was added while overlay is mounted. Adding a lower
699 * hardlink and then unlinking all overlay hardlinks would drop
700 * overlay nlink to zero before all upper inodes are unlinked.
701 * As a safety measure, when that situation is detected, set
702 * the overlay nlink to the index inode nlink minus one for the
703 * index entry itself.
705 set_nlink(d_inode(dentry), inode->i_nlink - 1);
706 ovl_set_nlink_upper(dentry);
707 goto out;
710 inode_lock_nested(dir, I_MUTEX_PARENT);
711 index = lookup_one_len(name.name, indexdir, name.len);
712 err = PTR_ERR(index);
713 if (IS_ERR(index)) {
714 index = NULL;
715 } else if (ovl_index_all(dentry->d_sb)) {
716 /* Whiteout orphan index to block future open by handle */
717 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
718 } else {
719 /* Cleanup orphan index entries */
720 err = ovl_cleanup(dir, index);
723 inode_unlock(dir);
724 if (err)
725 goto fail;
727 out:
728 kfree(name.name);
729 dput(index);
730 return;
732 fail:
733 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
734 goto out;
738 * Operations that change overlay inode and upper inode nlink need to be
739 * synchronized with copy up for persistent nlink accounting.
741 int ovl_nlink_start(struct dentry *dentry)
743 struct inode *inode = d_inode(dentry);
744 const struct cred *old_cred;
745 int err;
747 if (WARN_ON(!inode))
748 return -ENOENT;
751 * With inodes index is enabled, we store the union overlay nlink
752 * in an xattr on the index inode. When whiting out an indexed lower,
753 * we need to decrement the overlay persistent nlink, but before the
754 * first copy up, we have no upper index inode to store the xattr.
756 * As a workaround, before whiteout/rename over an indexed lower,
757 * copy up to create the upper index. Creating the upper index will
758 * initialize the overlay nlink, so it could be dropped if unlink
759 * or rename succeeds.
761 * TODO: implement metadata only index copy up when called with
762 * ovl_copy_up_flags(dentry, O_PATH).
764 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
765 err = ovl_copy_up(dentry);
766 if (err)
767 return err;
770 err = ovl_inode_lock(inode);
771 if (err)
772 return err;
774 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
775 goto out;
777 old_cred = ovl_override_creds(dentry->d_sb);
779 * The overlay inode nlink should be incremented/decremented IFF the
780 * upper operation succeeds, along with nlink change of upper inode.
781 * Therefore, before link/unlink/rename, we store the union nlink
782 * value relative to the upper inode nlink in an upper inode xattr.
784 err = ovl_set_nlink_upper(dentry);
785 revert_creds(old_cred);
787 out:
788 if (err)
789 ovl_inode_unlock(inode);
791 return err;
794 void ovl_nlink_end(struct dentry *dentry)
796 struct inode *inode = d_inode(dentry);
798 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
799 const struct cred *old_cred;
801 old_cred = ovl_override_creds(dentry->d_sb);
802 ovl_cleanup_index(dentry);
803 revert_creds(old_cred);
806 ovl_inode_unlock(inode);
809 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
811 /* Workdir should not be the same as upperdir */
812 if (workdir == upperdir)
813 goto err;
815 /* Workdir should not be subdir of upperdir and vice versa */
816 if (lock_rename(workdir, upperdir) != NULL)
817 goto err_unlock;
819 return 0;
821 err_unlock:
822 unlock_rename(workdir, upperdir);
823 err:
824 pr_err("overlayfs: failed to lock workdir+upperdir\n");
825 return -EIO;
828 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
829 int ovl_check_metacopy_xattr(struct dentry *dentry)
831 int res;
833 /* Only regular files can have metacopy xattr */
834 if (!S_ISREG(d_inode(dentry)->i_mode))
835 return 0;
837 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
838 if (res < 0) {
839 if (res == -ENODATA || res == -EOPNOTSUPP)
840 return 0;
841 goto out;
844 return 1;
845 out:
846 pr_warn_ratelimited("overlayfs: failed to get metacopy (%i)\n", res);
847 return res;
850 bool ovl_is_metacopy_dentry(struct dentry *dentry)
852 struct ovl_entry *oe = dentry->d_fsdata;
854 if (!d_is_reg(dentry))
855 return false;
857 if (ovl_dentry_upper(dentry)) {
858 if (!ovl_has_upperdata(d_inode(dentry)))
859 return true;
860 return false;
863 return (oe->numlower > 1);
866 ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
867 size_t padding)
869 ssize_t res;
870 char *buf = NULL;
872 res = vfs_getxattr(dentry, name, NULL, 0);
873 if (res < 0) {
874 if (res == -ENODATA || res == -EOPNOTSUPP)
875 return -ENODATA;
876 goto fail;
879 if (res != 0) {
880 buf = kzalloc(res + padding, GFP_KERNEL);
881 if (!buf)
882 return -ENOMEM;
884 res = vfs_getxattr(dentry, name, buf, res);
885 if (res < 0)
886 goto fail;
888 *value = buf;
890 return res;
892 fail:
893 pr_warn_ratelimited("overlayfs: failed to get xattr %s: err=%zi)\n",
894 name, res);
895 kfree(buf);
896 return res;
899 char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
901 int res;
902 char *s, *next, *buf = NULL;
904 res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
905 if (res == -ENODATA)
906 return NULL;
907 if (res < 0)
908 return ERR_PTR(res);
909 if (res == 0)
910 goto invalid;
912 if (buf[0] == '/') {
913 for (s = buf; *s++ == '/'; s = next) {
914 next = strchrnul(s, '/');
915 if (s == next)
916 goto invalid;
918 } else {
919 if (strchr(buf, '/') != NULL)
920 goto invalid;
923 return buf;
924 invalid:
925 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
926 res = -EINVAL;
927 kfree(buf);
928 return ERR_PTR(res);