spi: efm32: Convert to use GPIO descriptors
[linux/fpc-iii.git] / fs / overlayfs / util.c
blobea005085803f0c5a6eb54ae19f8c6b19d2089c38
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
5 */
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/uuid.h>
14 #include <linux/namei.h>
15 #include <linux/ratelimit.h>
16 #include "overlayfs.h"
18 int ovl_want_write(struct dentry *dentry)
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 return mnt_want_write(ofs->upper_mnt);
24 void ovl_drop_write(struct dentry *dentry)
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 mnt_drop_write(ofs->upper_mnt);
30 struct dentry *ovl_workdir(struct dentry *dentry)
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 return ofs->workdir;
36 const struct cred *ovl_override_creds(struct super_block *sb)
38 struct ovl_fs *ofs = sb->s_fs_info;
40 return override_creds(ofs->creator_cred);
44 * Check if underlying fs supports file handles and try to determine encoding
45 * type, in order to deduce maximum inode number used by fs.
47 * Return 0 if file handles are not supported.
48 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
49 * Return -1 if fs uses a non default encoding with unknown inode size.
51 int ovl_can_decode_fh(struct super_block *sb)
53 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
54 return 0;
56 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
59 struct dentry *ovl_indexdir(struct super_block *sb)
61 struct ovl_fs *ofs = sb->s_fs_info;
63 return ofs->indexdir;
66 /* Index all files on copy up. For now only enabled for NFS export */
67 bool ovl_index_all(struct super_block *sb)
69 struct ovl_fs *ofs = sb->s_fs_info;
71 return ofs->config.nfs_export && ofs->config.index;
74 /* Verify lower origin on lookup. For now only enabled for NFS export */
75 bool ovl_verify_lower(struct super_block *sb)
77 struct ovl_fs *ofs = sb->s_fs_info;
79 return ofs->config.nfs_export && ofs->config.index;
82 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
84 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
85 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
87 if (oe)
88 oe->numlower = numlower;
90 return oe;
93 bool ovl_dentry_remote(struct dentry *dentry)
95 return dentry->d_flags &
96 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
97 DCACHE_OP_REAL);
100 bool ovl_dentry_weird(struct dentry *dentry)
102 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
103 DCACHE_MANAGE_TRANSIT |
104 DCACHE_OP_HASH |
105 DCACHE_OP_COMPARE);
108 enum ovl_path_type ovl_path_type(struct dentry *dentry)
110 struct ovl_entry *oe = dentry->d_fsdata;
111 enum ovl_path_type type = 0;
113 if (ovl_dentry_upper(dentry)) {
114 type = __OVL_PATH_UPPER;
117 * Non-dir dentry can hold lower dentry of its copy up origin.
119 if (oe->numlower) {
120 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
121 type |= __OVL_PATH_ORIGIN;
122 if (d_is_dir(dentry) ||
123 !ovl_has_upperdata(d_inode(dentry)))
124 type |= __OVL_PATH_MERGE;
126 } else {
127 if (oe->numlower > 1)
128 type |= __OVL_PATH_MERGE;
130 return type;
133 void ovl_path_upper(struct dentry *dentry, struct path *path)
135 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
137 path->mnt = ofs->upper_mnt;
138 path->dentry = ovl_dentry_upper(dentry);
141 void ovl_path_lower(struct dentry *dentry, struct path *path)
143 struct ovl_entry *oe = dentry->d_fsdata;
145 if (oe->numlower) {
146 path->mnt = oe->lowerstack[0].layer->mnt;
147 path->dentry = oe->lowerstack[0].dentry;
148 } else {
149 *path = (struct path) { };
153 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
155 struct ovl_entry *oe = dentry->d_fsdata;
157 if (oe->numlower) {
158 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
159 path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
160 } else {
161 *path = (struct path) { };
165 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
167 enum ovl_path_type type = ovl_path_type(dentry);
169 if (!OVL_TYPE_UPPER(type))
170 ovl_path_lower(dentry, path);
171 else
172 ovl_path_upper(dentry, path);
174 return type;
177 struct dentry *ovl_dentry_upper(struct dentry *dentry)
179 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
182 struct dentry *ovl_dentry_lower(struct dentry *dentry)
184 struct ovl_entry *oe = dentry->d_fsdata;
186 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
189 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
191 struct ovl_entry *oe = dentry->d_fsdata;
193 return oe->numlower ? oe->lowerstack[0].layer : NULL;
197 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
198 * dependig on what is stored in lowerstack[0]. At times we need to find
199 * lower dentry which has data (and not metacopy dentry). This helper
200 * returns the lower data dentry.
202 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
204 struct ovl_entry *oe = dentry->d_fsdata;
206 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
209 struct dentry *ovl_dentry_real(struct dentry *dentry)
211 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
214 struct dentry *ovl_i_dentry_upper(struct inode *inode)
216 return ovl_upperdentry_dereference(OVL_I(inode));
219 struct inode *ovl_inode_upper(struct inode *inode)
221 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
223 return upperdentry ? d_inode(upperdentry) : NULL;
226 struct inode *ovl_inode_lower(struct inode *inode)
228 return OVL_I(inode)->lower;
231 struct inode *ovl_inode_real(struct inode *inode)
233 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
236 /* Return inode which contains lower data. Do not return metacopy */
237 struct inode *ovl_inode_lowerdata(struct inode *inode)
239 if (WARN_ON(!S_ISREG(inode->i_mode)))
240 return NULL;
242 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
245 /* Return real inode which contains data. Does not return metacopy inode */
246 struct inode *ovl_inode_realdata(struct inode *inode)
248 struct inode *upperinode;
250 upperinode = ovl_inode_upper(inode);
251 if (upperinode && ovl_has_upperdata(inode))
252 return upperinode;
254 return ovl_inode_lowerdata(inode);
257 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
259 return OVL_I(inode)->cache;
262 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
264 OVL_I(inode)->cache = cache;
267 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
269 set_bit(flag, &OVL_E(dentry)->flags);
272 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
274 clear_bit(flag, &OVL_E(dentry)->flags);
277 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
279 return test_bit(flag, &OVL_E(dentry)->flags);
282 bool ovl_dentry_is_opaque(struct dentry *dentry)
284 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
287 bool ovl_dentry_is_whiteout(struct dentry *dentry)
289 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
292 void ovl_dentry_set_opaque(struct dentry *dentry)
294 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
298 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
299 * to return positive, while there's no actual upper alias for the inode.
300 * Copy up code needs to know about the existence of the upper alias, so it
301 * can't use ovl_dentry_upper().
303 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
305 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
308 void ovl_dentry_set_upper_alias(struct dentry *dentry)
310 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
313 static bool ovl_should_check_upperdata(struct inode *inode)
315 if (!S_ISREG(inode->i_mode))
316 return false;
318 if (!ovl_inode_lower(inode))
319 return false;
321 return true;
324 bool ovl_has_upperdata(struct inode *inode)
326 if (!ovl_should_check_upperdata(inode))
327 return true;
329 if (!ovl_test_flag(OVL_UPPERDATA, inode))
330 return false;
332 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
333 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
334 * if setting of OVL_UPPERDATA is visible, then effects of writes
335 * before that are visible too.
337 smp_rmb();
338 return true;
341 void ovl_set_upperdata(struct inode *inode)
344 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
345 * if OVL_UPPERDATA flag is visible, then effects of write operations
346 * before it are visible as well.
348 smp_wmb();
349 ovl_set_flag(OVL_UPPERDATA, inode);
352 /* Caller should hold ovl_inode->lock */
353 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
355 if (!ovl_open_flags_need_copy_up(flags))
356 return false;
358 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
361 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
363 if (!ovl_open_flags_need_copy_up(flags))
364 return false;
366 return !ovl_has_upperdata(d_inode(dentry));
369 bool ovl_redirect_dir(struct super_block *sb)
371 struct ovl_fs *ofs = sb->s_fs_info;
373 return ofs->config.redirect_dir && !ofs->noxattr;
376 const char *ovl_dentry_get_redirect(struct dentry *dentry)
378 return OVL_I(d_inode(dentry))->redirect;
381 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
383 struct ovl_inode *oi = OVL_I(d_inode(dentry));
385 kfree(oi->redirect);
386 oi->redirect = redirect;
389 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
390 struct dentry *lowerdentry, struct dentry *lowerdata)
392 struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
394 if (upperdentry)
395 OVL_I(inode)->__upperdentry = upperdentry;
396 if (lowerdentry)
397 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
398 if (lowerdata)
399 OVL_I(inode)->lowerdata = igrab(d_inode(lowerdata));
401 ovl_copyattr(realinode, inode);
402 ovl_copyflags(realinode, inode);
403 if (!inode->i_ino)
404 inode->i_ino = realinode->i_ino;
407 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
409 struct inode *upperinode = d_inode(upperdentry);
411 WARN_ON(OVL_I(inode)->__upperdentry);
414 * Make sure upperdentry is consistent before making it visible
416 smp_wmb();
417 OVL_I(inode)->__upperdentry = upperdentry;
418 if (inode_unhashed(inode)) {
419 if (!inode->i_ino)
420 inode->i_ino = upperinode->i_ino;
421 inode->i_private = upperinode;
422 __insert_inode_hash(inode, (unsigned long) upperinode);
426 static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
428 struct inode *inode = d_inode(dentry);
430 WARN_ON(!inode_is_locked(inode));
432 * Version is used by readdir code to keep cache consistent. For merge
433 * dirs all changes need to be noted. For non-merge dirs, cache only
434 * contains impure (ones which have been copied up and have origins)
435 * entries, so only need to note changes to impure entries.
437 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
438 OVL_I(inode)->version++;
441 void ovl_dir_modified(struct dentry *dentry, bool impurity)
443 /* Copy mtime/ctime */
444 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
446 ovl_dentry_version_inc(dentry, impurity);
449 u64 ovl_dentry_version_get(struct dentry *dentry)
451 struct inode *inode = d_inode(dentry);
453 WARN_ON(!inode_is_locked(inode));
454 return OVL_I(inode)->version;
457 bool ovl_is_whiteout(struct dentry *dentry)
459 struct inode *inode = dentry->d_inode;
461 return inode && IS_WHITEOUT(inode);
464 struct file *ovl_path_open(struct path *path, int flags)
466 return dentry_open(path, flags | O_NOATIME, current_cred());
469 /* Caller should hold ovl_inode->lock */
470 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
472 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
474 if (ovl_dentry_upper(dentry) &&
475 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
476 !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
477 return true;
479 return false;
482 bool ovl_already_copied_up(struct dentry *dentry, int flags)
484 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
487 * Check if copy-up has happened as well as for upper alias (in
488 * case of hard links) is there.
490 * Both checks are lockless:
491 * - false negatives: will recheck under oi->lock
492 * - false positives:
493 * + ovl_dentry_upper() uses memory barriers to ensure the
494 * upper dentry is up-to-date
495 * + ovl_dentry_has_upper_alias() relies on locking of
496 * upper parent i_rwsem to prevent reordering copy-up
497 * with rename.
499 if (ovl_dentry_upper(dentry) &&
500 (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
501 !ovl_dentry_needs_data_copy_up(dentry, flags))
502 return true;
504 return false;
507 int ovl_copy_up_start(struct dentry *dentry, int flags)
509 struct inode *inode = d_inode(dentry);
510 int err;
512 err = ovl_inode_lock(inode);
513 if (!err && ovl_already_copied_up_locked(dentry, flags)) {
514 err = 1; /* Already copied up */
515 ovl_inode_unlock(inode);
518 return err;
521 void ovl_copy_up_end(struct dentry *dentry)
523 ovl_inode_unlock(d_inode(dentry));
526 bool ovl_check_origin_xattr(struct dentry *dentry)
528 int res;
530 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
532 /* Zero size value means "copied up but origin unknown" */
533 if (res >= 0)
534 return true;
536 return false;
539 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
541 int res;
542 char val;
544 if (!d_is_dir(dentry))
545 return false;
547 res = vfs_getxattr(dentry, name, &val, 1);
548 if (res == 1 && val == 'y')
549 return true;
551 return false;
554 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
555 const char *name, const void *value, size_t size,
556 int xerr)
558 int err;
559 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
561 if (ofs->noxattr)
562 return xerr;
564 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
566 if (err == -EOPNOTSUPP) {
567 pr_warn("cannot set %s xattr on upper\n", name);
568 ofs->noxattr = true;
569 return xerr;
572 return err;
575 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
577 int err;
579 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
580 return 0;
583 * Do not fail when upper doesn't support xattrs.
584 * Upper inodes won't have origin nor redirect xattr anyway.
586 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
587 "y", 1, 0);
588 if (!err)
589 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
591 return err;
594 void ovl_set_flag(unsigned long flag, struct inode *inode)
596 set_bit(flag, &OVL_I(inode)->flags);
599 void ovl_clear_flag(unsigned long flag, struct inode *inode)
601 clear_bit(flag, &OVL_I(inode)->flags);
604 bool ovl_test_flag(unsigned long flag, struct inode *inode)
606 return test_bit(flag, &OVL_I(inode)->flags);
610 * Caller must hold a reference to inode to prevent it from being freed while
611 * it is marked inuse.
613 bool ovl_inuse_trylock(struct dentry *dentry)
615 struct inode *inode = d_inode(dentry);
616 bool locked = false;
618 spin_lock(&inode->i_lock);
619 if (!(inode->i_state & I_OVL_INUSE)) {
620 inode->i_state |= I_OVL_INUSE;
621 locked = true;
623 spin_unlock(&inode->i_lock);
625 return locked;
628 void ovl_inuse_unlock(struct dentry *dentry)
630 if (dentry) {
631 struct inode *inode = d_inode(dentry);
633 spin_lock(&inode->i_lock);
634 WARN_ON(!(inode->i_state & I_OVL_INUSE));
635 inode->i_state &= ~I_OVL_INUSE;
636 spin_unlock(&inode->i_lock);
640 bool ovl_is_inuse(struct dentry *dentry)
642 struct inode *inode = d_inode(dentry);
643 bool inuse;
645 spin_lock(&inode->i_lock);
646 inuse = (inode->i_state & I_OVL_INUSE);
647 spin_unlock(&inode->i_lock);
649 return inuse;
653 * Does this overlay dentry need to be indexed on copy up?
655 bool ovl_need_index(struct dentry *dentry)
657 struct dentry *lower = ovl_dentry_lower(dentry);
659 if (!lower || !ovl_indexdir(dentry->d_sb))
660 return false;
662 /* Index all files for NFS export and consistency verification */
663 if (ovl_index_all(dentry->d_sb))
664 return true;
666 /* Index only lower hardlinks on copy up */
667 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
668 return true;
670 return false;
673 /* Caller must hold OVL_I(inode)->lock */
674 static void ovl_cleanup_index(struct dentry *dentry)
676 struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
677 struct inode *dir = indexdir->d_inode;
678 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
679 struct dentry *upperdentry = ovl_dentry_upper(dentry);
680 struct dentry *index = NULL;
681 struct inode *inode;
682 struct qstr name = { };
683 int err;
685 err = ovl_get_index_name(lowerdentry, &name);
686 if (err)
687 goto fail;
689 inode = d_inode(upperdentry);
690 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
691 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
692 upperdentry, inode->i_ino, inode->i_nlink);
694 * We either have a bug with persistent union nlink or a lower
695 * hardlink was added while overlay is mounted. Adding a lower
696 * hardlink and then unlinking all overlay hardlinks would drop
697 * overlay nlink to zero before all upper inodes are unlinked.
698 * As a safety measure, when that situation is detected, set
699 * the overlay nlink to the index inode nlink minus one for the
700 * index entry itself.
702 set_nlink(d_inode(dentry), inode->i_nlink - 1);
703 ovl_set_nlink_upper(dentry);
704 goto out;
707 inode_lock_nested(dir, I_MUTEX_PARENT);
708 index = lookup_one_len(name.name, indexdir, name.len);
709 err = PTR_ERR(index);
710 if (IS_ERR(index)) {
711 index = NULL;
712 } else if (ovl_index_all(dentry->d_sb)) {
713 /* Whiteout orphan index to block future open by handle */
714 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
715 } else {
716 /* Cleanup orphan index entries */
717 err = ovl_cleanup(dir, index);
720 inode_unlock(dir);
721 if (err)
722 goto fail;
724 out:
725 kfree(name.name);
726 dput(index);
727 return;
729 fail:
730 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
731 goto out;
735 * Operations that change overlay inode and upper inode nlink need to be
736 * synchronized with copy up for persistent nlink accounting.
738 int ovl_nlink_start(struct dentry *dentry)
740 struct inode *inode = d_inode(dentry);
741 const struct cred *old_cred;
742 int err;
744 if (WARN_ON(!inode))
745 return -ENOENT;
748 * With inodes index is enabled, we store the union overlay nlink
749 * in an xattr on the index inode. When whiting out an indexed lower,
750 * we need to decrement the overlay persistent nlink, but before the
751 * first copy up, we have no upper index inode to store the xattr.
753 * As a workaround, before whiteout/rename over an indexed lower,
754 * copy up to create the upper index. Creating the upper index will
755 * initialize the overlay nlink, so it could be dropped if unlink
756 * or rename succeeds.
758 * TODO: implement metadata only index copy up when called with
759 * ovl_copy_up_flags(dentry, O_PATH).
761 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
762 err = ovl_copy_up(dentry);
763 if (err)
764 return err;
767 err = ovl_inode_lock(inode);
768 if (err)
769 return err;
771 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
772 goto out;
774 old_cred = ovl_override_creds(dentry->d_sb);
776 * The overlay inode nlink should be incremented/decremented IFF the
777 * upper operation succeeds, along with nlink change of upper inode.
778 * Therefore, before link/unlink/rename, we store the union nlink
779 * value relative to the upper inode nlink in an upper inode xattr.
781 err = ovl_set_nlink_upper(dentry);
782 revert_creds(old_cred);
784 out:
785 if (err)
786 ovl_inode_unlock(inode);
788 return err;
791 void ovl_nlink_end(struct dentry *dentry)
793 struct inode *inode = d_inode(dentry);
795 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
796 const struct cred *old_cred;
798 old_cred = ovl_override_creds(dentry->d_sb);
799 ovl_cleanup_index(dentry);
800 revert_creds(old_cred);
803 ovl_inode_unlock(inode);
806 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
808 /* Workdir should not be the same as upperdir */
809 if (workdir == upperdir)
810 goto err;
812 /* Workdir should not be subdir of upperdir and vice versa */
813 if (lock_rename(workdir, upperdir) != NULL)
814 goto err_unlock;
816 return 0;
818 err_unlock:
819 unlock_rename(workdir, upperdir);
820 err:
821 pr_err("failed to lock workdir+upperdir\n");
822 return -EIO;
825 /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
826 int ovl_check_metacopy_xattr(struct dentry *dentry)
828 int res;
830 /* Only regular files can have metacopy xattr */
831 if (!S_ISREG(d_inode(dentry)->i_mode))
832 return 0;
834 res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
835 if (res < 0) {
836 if (res == -ENODATA || res == -EOPNOTSUPP)
837 return 0;
838 goto out;
841 return 1;
842 out:
843 pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
844 return res;
847 bool ovl_is_metacopy_dentry(struct dentry *dentry)
849 struct ovl_entry *oe = dentry->d_fsdata;
851 if (!d_is_reg(dentry))
852 return false;
854 if (ovl_dentry_upper(dentry)) {
855 if (!ovl_has_upperdata(d_inode(dentry)))
856 return true;
857 return false;
860 return (oe->numlower > 1);
863 ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
864 size_t padding)
866 ssize_t res;
867 char *buf = NULL;
869 res = vfs_getxattr(dentry, name, NULL, 0);
870 if (res < 0) {
871 if (res == -ENODATA || res == -EOPNOTSUPP)
872 return -ENODATA;
873 goto fail;
876 if (res != 0) {
877 buf = kzalloc(res + padding, GFP_KERNEL);
878 if (!buf)
879 return -ENOMEM;
881 res = vfs_getxattr(dentry, name, buf, res);
882 if (res < 0)
883 goto fail;
885 *value = buf;
887 return res;
889 fail:
890 pr_warn_ratelimited("failed to get xattr %s: err=%zi)\n",
891 name, res);
892 kfree(buf);
893 return res;
896 char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
898 int res;
899 char *s, *next, *buf = NULL;
901 res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
902 if (res == -ENODATA)
903 return NULL;
904 if (res < 0)
905 return ERR_PTR(res);
906 if (res == 0)
907 goto invalid;
909 if (buf[0] == '/') {
910 for (s = buf; *s++ == '/'; s = next) {
911 next = strchrnul(s, '/');
912 if (s == next)
913 goto invalid;
915 } else {
916 if (strchr(buf, '/') != NULL)
917 goto invalid;
920 return buf;
921 invalid:
922 pr_warn_ratelimited("invalid redirect (%s)\n", buf);
923 res = -EINVAL;
924 kfree(buf);
925 return ERR_PTR(res);