ceph: fail the open_by_handle_at() if the dentry is being unlinked
PATCH v2 1/1 ceph: fail the open_by_handle_at() if the dentry is being unlinked
From: Xiubo Li <[email protected]>
When unlinking a file the kclient will send a unlink request to MDS by holding the dentry reference, and then the MDS will return 2 replies, which are unsafe reply and a deferred safe reply.
After the unsafe reply received the kernel will return and succeed the unlink request to user space apps.
Only when the safe reply received the dentry's reference will be released. Or the dentry will only be unhashed from dcache. But when the open_by_handle_at() begins to open the unlinked files it will succeed.
URL: https://tracker.ceph.com/issues/56524
---
V2:
- If the dentry was released and inode is evicted such as by dropping the caches, it will allocate a new dentry, which is also unhashed.
fs/ceph/export.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
fs/ceph/export.c +16 -1
@@ -182,6 +182,7 @@ struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)static struct dentry *__fh_to_dentry (struct super_block *sb,u64 ino) {struct inode *inode =__lookup_inode (sb, ino); +struct dentry *dentry;int err;if (IS_ERR (inode))
@@ -197,7 +198,21 @@ static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)iput (inode);return ERR_PTR (-ESTALE ); } -return d_obtain_alias (inode); + +/* +* -ESTALE if the dentry exists and is unhashed, +* which should be being released +*/ + dentry =d_find_any_alias (inode); +if (dentry &&unlikely (d_unhashed (dentry))) { +dput (dentry); +return ERR_PTR (-ESTALE ); + } + +if (!dentry) + dentry =d_obtain_alias (inode); + +return dentry; } static struct dentry *__snapfh_to_dentry(struct super_block *sb,
Signature · 1 line
2.36.0.rc1
Quoted 51 lines · On Mon, 2022-08-29 at 12:57 +0800, [email protected] wrote:
From: Xiubo Li <[email protected]>
When unlinking a file the kclient will send a unlink request to MDS by holding the dentry reference, and then the MDS will return 2 replies, which are unsafe reply and a deferred safe reply.
After the unsafe reply received the kernel will return and succeed the unlink request to user space apps.
Only when the safe reply received the dentry's reference will be released. Or the dentry will only be unhashed from dcache. But when the
open_by_handle_at()begins to open the unlinked files it will succeed.URL: https://tracker.ceph.com/issues/56524
Signed-off-by Xiubo Li <[email protected]>---
V2:
- If the dentry was released and inode is evicted such as by dropping the caches, it will allocate a new dentry, which is also unhashed.fs/ceph/export.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)fs/ceph/export.c +9
-1@@ -182,6 +182,7 @@ struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)static struct dentry *__fh_to_dentry (struct super_block *sb,u64 ino) {struct inode *inode =__lookup_inode (sb, ino); +struct dentry *dentry;int err;if (IS_ERR (inode))@@ -197,7 +198,21 @@ static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)iput (inode);return ERR_PTR (-ESTALE ); } -return d_obtain_alias (inode); + +/* +* -ESTALE if the dentry exists and is unhashed, +* which should be being released +*/ + dentry =d_find_any_alias (inode); +if (dentry &&unlikely (d_unhashed (dentry))) { +dput (dentry);
fs/ceph/export.c static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino) · line 218
Quoted 10 lines
fs/ceph/export.c +7
-0+return ERR_PTR (-ESTALE ); + } + +if (!dentry) + dentry =d_obtain_alias (inode); + +return dentry; } static struct dentry *__snapfh_to_dentry(struct super_block *sb,
This looks racy.
Suppose we have 2 racing tasks calling __fh_to_dentry for the same inode. The first one races in and doesn't find anything. d_obtain alias creates a disconnected dentry and returns it. The next task then finds it, sees that it's disconnected and gets back -ESTALE.
I think you may need to detect this situation in a different way.
Signature · 1 line
Jeff Layton <[email protected]>
Quoted 64 lines · Jeff Layton
Quoted 51 lines · On Mon, 2022-08-29 at 12:57 +0800, [email protected] wrote:
From: Xiubo Li <[email protected]>
When unlinking a file the kclient will send a unlink request to MDS by holding the dentry reference, and then the MDS will return 2 replies, which are unsafe reply and a deferred safe reply.
After the unsafe reply received the kernel will return and succeed the unlink request to user space apps.
Only when the safe reply received the dentry's reference will be released. Or the dentry will only be unhashed from dcache. But when the
open_by_handle_at()begins to open the unlinked files it will succeed.URL: https://tracker.ceph.com/issues/56524
Signed-off-by Xiubo Li <[email protected]>---
V2:
- If the dentry was released and inode is evicted such as by dropping the caches, it will allocate a new dentry, which is also unhashed.fs/ceph/export.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)fs/ceph/export.c +9
-1@@ -182,6 +182,7 @@ struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)static struct dentry *__fh_to_dentry (struct super_block *sb,u64 ino) {struct inode *inode =__lookup_inode (sb, ino); +struct dentry *dentry;int err;if (IS_ERR (inode))@@ -197,7 +198,21 @@ static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)iput (inode);return ERR_PTR (-ESTALE ); } -return d_obtain_alias (inode); + +/* +* -ESTALE if the dentry exists and is unhashed, +* which should be being released +*/ + dentry =d_find_any_alias (inode); +if (dentry &&unlikely (d_unhashed (dentry))) { +dput (dentry);fs/ceph/export.c static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino) · line 218
Quoted 10 lines
fs/ceph/export.c +7
-0+return ERR_PTR (-ESTALE ); + } + +if (!dentry) + dentry =d_obtain_alias (inode); + +return dentry; } static struct dentry *__snapfh_to_dentry(struct super_block *sb,This looks racy.
Quoted 6 lines
Suppose we have 2 racing tasks calling
__fh_to_dentryfor the same inode. The first one races in and doesn't find anything.d_obtainalias creates a disconnected dentry and returns it. The next task then finds it, sees that it's disconnected and gets back-ESTALE.I think you may need to detect this situation in a different way.
Yeah, you're right. Locally I have another version of patch, which will add one di->flags bit, which is "CEPH_DENTRY_IS_UNLINKING".
If the file have hard links and there are more than one alias and one of them is being unlinked, shouldn't we make sure we will pick a normal one here ? If so we should iterate all the alias and filter out the being unlinked ones.
At the same time I found another issue for the "ceph_fh_to_dentry()". That is we never check the inode->i_generation like other filesystems, which will make sure the inode we are trying to open is the exactly the same one saved in userspace. The inode maybe deleted and created before this.
Thanks
Xiubo
Quoted 71 lines · Xiubo Li
Quoted 69 lines · Jeff Layton
Quoted 51 lines · On Mon, 2022-08-29 at 12:57 +0800, [email protected] wrote:
From: Xiubo Li <[email protected]>
When unlinking a file the kclient will send a unlink request to MDS by holding the dentry reference, and then the MDS will return 2 replies, which are unsafe reply and a deferred safe reply.
After the unsafe reply received the kernel will return and succeed the unlink request to user space apps.
Only when the safe reply received the dentry's reference will be released. Or the dentry will only be unhashed from dcache. But when the
open_by_handle_at()begins to open the unlinked files it will succeed.URL: https://tracker.ceph.com/issues/56524
Signed-off-by Xiubo Li <[email protected]>---
V2:
- If the dentry was released and inode is evicted such as by dropping the caches, it will allocate a new dentry, which is also unhashed.
fs/ceph/export.c| 17 ++++++++++++++++-1 file changed, 16 insertions(+), 1 deletion(-)fs/ceph/export.c +8
-1@@ -182,6 +182,7 @@ struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino) static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino) { struct inode *inode = __lookup_inode(sb, ino); + struct dentry *dentry; int err; if (IS_ERR(inode))@@ -197,7 +198,21 @@ static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino) iput(inode); return ERR_PTR(-ESTALE); } - return d_obtain_alias(inode); + + /* + * -ESTALE if the dentry exists and is unhashed, + * which should be being released + */ + dentry = d_find_any_alias(inode); + if (dentry && unlikely(d_unhashed(dentry))) {fs/ceph/export.c static struct dentry *__fh_to_dentry(struct · line 218
Quoted 10 lines
fs/ceph/export.c +8
-0+ dput(dentry); + return ERR_PTR(-ESTALE); + } + + if (!dentry) + dentry = d_obtain_alias(inode); + + return dentry; } static struct dentry *__snapfh_to_dentry(struct super_block *sb,This looks racy.
Suppose we have 2 racing tasks calling
__fh_to_dentryfor the same inode. The first one races in and doesn't find anything.d_obtainalias creates a disconnected dentry and returns it. The next task then finds it, sees that it's disconnected and gets back-ESTALE.
Quoted 8 lines
Quoted 1 line
I think you may need to detect this situation in a different way.
Yeah, you're right. Locally I have another version of patch, which will add one
di->flagsbit, which is "CEPH_DENTRY_IS_UNLINKING".If the file have hard links and there are more than one alias and one of them is being unlinked, shouldn't we make sure we will pick a normal one here ? If so we should iterate all the alias and filter out the being unlinked ones.
At the same time I found another issue for the "
ceph_fh_to_dentry()". That is we never check theinode->i_generationlike other filesystems, which will make sure the inode we are trying to open is the exactly the same one saved in userspace. The inode maybe deleted and created before this.
It seems never happening for ceph. The MDS will make sure it won't reuse the freed ino#.
- Xiubo
Quoted 5 lines
Thanks
Xiubo
Quoted 72 lines · Xiubo Li
Quoted 64 lines · Jeff Layton
Quoted 51 lines · On Mon, 2022-08-29 at 12:57 +0800, [email protected] wrote:
From: Xiubo Li <[email protected]>
When unlinking a file the kclient will send a unlink request to MDS by holding the dentry reference, and then the MDS will return 2 replies, which are unsafe reply and a deferred safe reply.
After the unsafe reply received the kernel will return and succeed the unlink request to user space apps.
Only when the safe reply received the dentry's reference will be released. Or the dentry will only be unhashed from dcache. But when the
open_by_handle_at()begins to open the unlinked files it will succeed.URL: https://tracker.ceph.com/issues/56524
Signed-off-by Xiubo Li <[email protected]>---
V2:
- If the dentry was released and inode is evicted such as by dropping the caches, it will allocate a new dentry, which is also unhashed.fs/ceph/export.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)fs/ceph/export.c +9
-1@@ -182,6 +182,7 @@ struct inode *ceph_lookup_inode(struct super_block *sb, u64 ino)static struct dentry *__fh_to_dentry (struct super_block *sb,u64 ino) {struct inode *inode =__lookup_inode (sb, ino); +struct dentry *dentry;int err;if (IS_ERR (inode))@@ -197,7 +198,21 @@ static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)iput (inode);return ERR_PTR (-ESTALE ); } -return d_obtain_alias (inode); + +/* +* -ESTALE if the dentry exists and is unhashed, +* which should be being released +*/ + dentry =d_find_any_alias (inode); +if (dentry &&unlikely (d_unhashed (dentry))) { +dput (dentry);fs/ceph/export.c static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino) · line 218
Quoted 10 lines
fs/ceph/export.c +7
-0+return ERR_PTR (-ESTALE ); + } + +if (!dentry) + dentry =d_obtain_alias (inode); + +return dentry; } static struct dentry *__snapfh_to_dentry(struct super_block *sb,This looks racy.
Quoted 6 lines
Suppose we have 2 racing tasks calling
__fh_to_dentryfor the same inode. The first one races in and doesn't find anything.d_obtainalias creates a disconnected dentry and returns it. The next task then finds it, sees that it's disconnected and gets back-ESTALE.I think you may need to detect this situation in a different way.
Quoted 8 lines
Yeah, you're right. Locally I have another version of patch, which will add one
di->flagsbit, which is "CEPH_DENTRY_IS_UNLINKING".If the file have hard links and there are more than one alias and one of them is being unlinked, shouldn't we make sure we will pick a normal one here ? If so we should iterate all the alias and filter out the being unlinked ones.
Possibly. Another option might be to try to catch this in the open codepath instead. Test whether the dentry you're trying to open has the IS_UNLINKING flag set, and return -ESTALE on open if so.
If the problem goes beyond open though, then that may not be sufficient.
Quoted 11 lines
At the same time I found another issue for the "
ceph_fh_to_dentry()". That is we never check theinode->i_generationlike other filesystems, which will make sure the inode we are trying to open is the exactly the same one saved in userspace. The inode maybe deleted and created before this.Thanks
Xiubo
Signature · 1 line
Jeff Layton <[email protected]>